Python
Deliver ask_user answers to the reviewer's history; steer agents off chat-asks
Devika Verma0dfa59612258
diff --git a/coworker/engine.py b/coworker/engine.pyindex 57a09972..a0d5e46c 100644--- a/coworker/engine.py+++ b/coworker/engine.py@@ -138,6 +138,18 @@ class TurnEngine:# re-proposal with even slightly different arguments does not match and goes back# through the reviewer/card — deliberately narrow, deliberately not standing.self._allow_anyway: set[tuple[str, str]] = set()+ # ask_user answers for the reviewer's history (§8.2 — the missing third of the+ # reply-tag feature: render_history prints the tag and the §8.3 instructions say to+ # weigh it lower; this is the extractor that finally delivers the data). Captured at+ # the moment the asker returns — the one point where the engine KNOWS the text came+ # from the human, whichever authenticated surface answered (inline card, Inbox, or a+ # bound channel; the same trust approval clicks already carry). ANSWERS ONLY, never+ # the agent's question: agent-authored text stays out of the judge's view — showing+ # the question too is step 2, evidence-gated on shadow data. Each entry is+ # (anchor, text) where anchor = how many user messages existed at capture, so the+ # merge in `_user_history` stays chronological. Runtime-only on purpose: a restart+ # costs the reviewer context (more cards), never correctness.+ self._ask_replies: list[tuple[int, str]] = []# Extra user-facing fields for a tool's approval card, merged into the# PERMISSION_REQUIRED payload — e.g. web_search's live provider name, so the card# can say where queries actually go (§1.9). Set post-construction by the surface@@ -737,7 +749,13 @@ class TurnEngine:def _user_history(self) -> tuple[str, list[dict[str, Any]]]:"""(current request, earlier user messages) — the user's own words only, extracted- mechanically (§8.2). Never agent output, never tool results, never a summary."""+ mechanically (§8.2). Never agent output, never tool results, never a summary.++ `ask_user` answers are merged in from `_ask_replies` (captured as they arrived, not+ parsed out of tool envelopes), tagged `is_reply` so `render_history` prints the+ "[reply to a question the agent asked]" marker the §8.3 instructions already know+ how to weigh. A reply is always HISTORY, never the current request — "ok proceed"+ must not become the headline the action is judged against."""texts: list[str] = []for msg in self.messages:if msg.get("role") != "user":@@ -757,8 +775,22 @@ class TurnEngine:if text:texts.append(text)if not texts:- return "", []- return texts[-1], [{"text": t} for t in texts[:-1]]+ return "", [{"text": t, "is_reply": True} for _, t in self._ask_replies]+ history: list[dict[str, Any]] = []+ for i, t in enumerate(texts[:-1], start=1):+ history.append({"text": t})+ history.extend(+ {"text": r, "is_reply": True} for a, r in self._ask_replies if a == i+ )+ # Replies captured during the current turn (anchor == len(texts)) — or after an+ # anchor message that was itself empty/skipped — land at the tail, so a same-turn+ # consent is already visible to the reviewer for the very next action.+ history.extend(+ {"text": r, "is_reply": True}+ for a, r in self._ask_replies+ if a >= len(texts)+ )+ return texts[-1], historyasync def _preconsult_reviewer(self, tool_calls: list[ToolCall]) -> None:"""Fire one reviewer request per call that will escalate, all concurrently, and@@ -1315,6 +1347,8 @@ class TurnEngine:}status = "ok" if (result.get("answer") or result.get("answers")) else "denied"+ if status == "ok":+ self._note_ask_replies(result)self.messages.append(_tool_result_message(tool_call, result))self._audit(tool_call,@@ -1332,6 +1366,23 @@ class TurnEngine:},)+ def _note_ask_replies(self, result: dict[str, Any]) -> None:+ """Record the user's ask_user answer(s) for the reviewer's history (§8.2). Values+ only — a grouped form's keys are the agent's own question headers, and agent text+ never enters the judge's view. Anchored to the number of user messages present now,+ so the merge stays chronological however the session continues."""+ anchor = sum(1 for m in self.messages if m.get("role") == "user")+ answers = result.get("answers")+ values = (+ [str(v) for v in answers.values()]+ if isinstance(answers, dict)+ else [str(result.get("answer") or "")]+ )+ for text in values:+ text = text.strip()+ if text:+ self._ask_replies.append((anchor, text))+def _inject_steering(self) -> None:for text, source in self._steering:message: dict[str, Any] = {diff --git a/coworker/tools/ask.py b/coworker/tools/ask.pyindex 3268109d..a426232d 100644--- a/coworker/tools/ask.py+++ b/coworker/tools/ask.py@@ -50,8 +50,11 @@ _ASK_SCHEMA = {"name": "ask_user","description": ("Ask the user one or more questions and wait for their answer. Use for decisions or "- "information only the user can provide. Group related questions (up to "- f"{MAX_GROUPED_QUESTIONS}) into one call via `questions` instead of asking serially."+ "information only the user can provide. Do not use it to ask permission for a "+ "specific action you are about to take — propose the action instead; the approval "+ "flow shows the user exactly what would run and does the asking. Group related "+ f"questions (up to {MAX_GROUPED_QUESTIONS}) into one call via `questions` instead "+ "of asking serially."),"parameters": {"type": "object",@@ -132,6 +135,10 @@ def ask_user_tool() -> object:decision or information you can't infer (a preference, a missing fact, a choice between realalternatives). Prefer this over guessing or stalling.+ Never use it to ask permission for a specific action you are about to take ("shall I+ open a PR?") — propose the action instead: the approval flow shows the user the exact+ command/arguments and does the asking, which is stronger consent than a chat yes.+Single form returns `{"answer": "..."}` — the chosen option label(s) or the typed text.Grouped form (`questions`) returns `{"answers": {"<header or question>": "..."}}` — oneentry per question. Don't ask what you can reasonably decide yourself; reserve this for@@ -152,7 +159,8 @@ def ask_user_tool() -> object:capabilities=["ask_user"],description=("Ask the user a question (free-text or multiple-choice) and wait for their answer. "- "Use for decisions or information only the user can provide."+ "Use for decisions or information only the user can provide — never to ask "+ "permission for a specific action; propose the action and let the approval flow ask."),),)diff --git a/tests/test_auto_approve.py b/tests/test_auto_approve.pyindex 641aac71..2c007540 100644--- a/tests/test_auto_approve.py+++ b/tests/test_auto_approve.py@@ -158,6 +158,87 @@ def test_reply_tag_is_rendered():assert "[reply to a question the agent asked]" in rendered+# -- ask_user answers reach the reviewer history (§8.2 reply capture) --------------+++def _drain(agen):+ async def _go():+ return [e async for e in agen]++ return asyncio.run(_go())+++def test_ask_user_answer_lands_in_history_tagged_and_never_becomes_the_request(tmp_path):+ engine, _rows, _approvals = _engine(tmp_path, [])+ engine.messages.append({"role": "user", "content": "test the migration"})++ async def asker(args, tool_call_id=None):+ return {"answer": "yes, staging is fine"}++ engine.question_asker = asker+ _drain(+ engine._handle_ask_user(+ ToolCall(id="q1", name="ask_user", arguments={"question": "Use the staging DB?"})+ )+ )++ # Same turn: the consent is already visible to the reviewer for the NEXT action…+ request, history = engine._user_history()+ assert request == "test the migration" # …and never becomes the current request+ assert {"text": "yes, staging is fine", "is_reply": True} in history+ rendered = reviewer_mod.render_history(history)+ assert "[reply to a question the agent asked]" in rendered+ # Step 1 is answers-only: the agent's question text stays out of the judge's view.+ assert "Use the staging DB?" not in rendered++ # Next turn: the reply keeps its chronological slot after the message it followed.+ engine.messages.append({"role": "user", "content": "now update the changelog"})+ request, history = engine._user_history()+ assert request == "now update the changelog"+ assert history == [+ {"text": "test the migration"},+ {"text": "yes, staging is fine", "is_reply": True},+ ]+++def test_grouped_ask_answers_record_values_only(tmp_path):+ engine, _rows, _approvals = _engine(tmp_path, [])+ engine.messages.append({"role": "user", "content": "set up the client"})++ async def asker(args, tool_call_id=None):+ return {"answers": {"Which region?": "eu-west-1", "Which account?": "work"}}++ engine.question_asker = asker+ _drain(+ engine._handle_ask_user(+ ToolCall(id="q1", name="ask_user", arguments={"question": "Config?"})+ )+ )+ _, history = engine._user_history()+ replies = [h["text"] for h in history if h.get("is_reply")]+ assert replies == ["eu-west-1", "work"]+ # The grouped form's keys are the agent's own question headers — never recorded.+ assert engine._ask_replies == [(1, "eu-west-1"), (1, "work")]+++def test_unanswered_ask_records_nothing(tmp_path):+ engine, _rows, _approvals = _engine(tmp_path, [])+ engine.messages.append({"role": "user", "content": "hi"})++ async def asker(args, tool_call_id=None):+ return {"answer": "", "error": "interrupted by user"}++ engine.question_asker = asker+ _drain(+ engine._handle_ask_user(+ ToolCall(id="q1", name="ask_user", arguments={"question": "anything?"})+ )+ )+ assert engine._ask_replies == []+ _, history = engine._user_history()+ assert all(not h.get("is_reply") for h in history)++# -- Reviewer.review: never raises ------------------------------------------------