Skip to content

The agent that grows with you

Merge remote-tracking branch 'origin/main' into fix/106742-thread-resolution

teknium1485d5f6848c2
diff --cc gateway/run_turn_runner.py
index f472ddbfe47,dc9537b7a50..a553efc2a61
--- a/gateway/run_turn_runner.py
+++ b/gateway/run_turn_runner.py
@@@ -340,17 -1294,46 +340,53 @@@ class TurnRunner(GatewayTurnProgressMix
logger.warning("%s boundary timed out or failed: %s", reason, err)
return False
+ async def _send_shared_clarify(self, entry, **kwargs):
+ result = await self._ctx._status_adapter.send_clarify(**kwargs)
+ if self._approval_owner is not None and result.success:
+ authority, session_id, generation = self._approval_owner
+ authority.register_clarify(session_id, generation, entry)
+ return result
+
- def _clarify_callback_sync(self, question: str, choices, multi_select: bool = False) -> str:
+ def _clarify_callback_sync(self, question: str, choices, multi_select: bool = False,
+ questions=None) -> str:
"""Present a clarify prompt and block on a response (clarify_tool's synchronous contract):
schedule send_clarify on the gateway loop, block on the primitive's threading.Event with a
- timeout. Returns the response string, or a sentinel when none arrived."""
+ timeout. Returns the response string, or a sentinel when none arrived.
+
+ ``questions`` (clarify_tool's batch form) is answered here, one card per question, because
+ this surface knows whether an answer arrived: the loop that would otherwise call this
+ callback once per question can only recognize "no answer" from the returned sentinel text,
+ and treated that text as the question's answer.
+ """
+ if questions:
+ return self._clarify_batch_sync(questions)
+ response, _answered = self._ask_clarify_question(question, choices, multi_select)
+ return response
+
+ def _clarify_batch_sync(self, questions) -> str:
+ """Answer a batch: one card per question, stop at the first the user never answers.
+ Returns the JSON shape clarify_tool's batch path reads. The stream/typing re-arm waits for
+ the last question — between two cards it only opens a bubble the next boundary closes."""
+ answers: Dict[str, Any] = {}
+ payload: Dict[str, Any] = {"answers": answers, "timed_out": False}
+ last = len(questions) - 1
+ for index, entry in enumerate(questions):
+ raw, answered = self._ask_clarify_question(
+ entry.get("question", ""), entry.get("choices"), bool(entry.get("multi_select")),
+ rearm=index == last)
+ if not answered:
+ # The surface's own no-answer text ("could not be delivered", "did not respond
+ # within Nm") rides along as ``notice``: blank answers alone read as user
+ # inactivity, which is the misreport #112684 describes for an undelivered card.
+ payload.update(timed_out=True, notice=raw)
+ break
+ answers[entry.get("qid") or f"q{index}"] = raw
+ return json.dumps(payload, ensure_ascii=False)
+
+ def _ask_clarify_question(self, question, choices, multi_select, rearm: bool = True) -> tuple[str, bool]:
+ """One card: register, send, wait, then retire it (no answer) or re-arm (answer).
+ Returns ``(response, answered)``; the caller decides what "no answer" means — a sentinel
+ for a single question, the batch's ``timed_out`` flag."""
from gateway.run import _clarify_send_then_wait
from tools import clarify_gateway as clarify_mod
import uuid
@@@ -419,18 -1399,9 +455,18 @@@
ctx._status_adapter.resume_typing_for_chat(ctx._status_chat_id)
except Exception:
logger.debug("resume_typing_for_chat after clarify answer failed", exc_info=True)
- return response
+ return response, answered
def _approval_notify_sync(self, approval_data: dict) -> None:
+ if self._approval_owner is not None:
+ authority, session_id, generation = self._approval_owner
+ authority.check_approval_generation(session_id, generation)
+ # A native decline must finish before any observer can authorize work.
+ self._render_approval_sync(approval_data)
+ if self._approval_owner is not None:
+ authority.register_approval(session_id, generation, self._ctx.session_key, approval_data)
+
+ def _render_approval_sync(self, approval_data: dict) -> None:
"""Send the approval request from the agent thread: the adapter's interactive button
approvals (``send_exec_approval``) when available, else plain text with ``/approve`` steps."""
from gateway.run import _approval_send_outcome, _format_exec_approval_fallback, _interim_metadata, _redact_approval_command
diff --cc tests/gateway/test_clarify_card_retire_on_timeout.py
index 4cf15d4979a,3dbfbe66fcf..4606dba06e2
--- a/tests/gateway/test_clarify_card_retire_on_timeout.py
+++ b/tests/gateway/test_clarify_card_retire_on_timeout.py
@@@ -33,13 -39,17 +39,18 @@@ class _TextAdapter(_CardAdapter)
retire_clarify_card = None # type: ignore[assignment]
- def _run_clarify(adapter, answer=None):
- """Returns (clarify response, labels of every coroutine the runner scheduled).
- ``answer`` resolves the pending clarify with that text instead of letting it time out."""
+ def _run_clarify(adapter, answer=None, questions=None, answers=(), via_tool=False):
+ """Returns (clarify result, labels of every coroutine the runner scheduled).
+
+ ``answer`` resolves a single question with that text instead of letting it time out.
+ ``questions`` drives clarify_tool's batch form instead: question i resolves from
+ ``answers[i]``, and ``None`` leaves it unanswered so it expires. ``via_tool`` routes the
+ batch through ``clarify_tool`` itself rather than calling the runner's callback directly."""
from gateway.run_turn_runner import TurnRunner
+ from tools import clarify_gateway as cm
runner = object.__new__(TurnRunner)
+ runner._approval_owner = None # no owning authority: the controls snapshot is skipped
runner._ctx = SimpleNamespace(
_status_adapter=adapter, _status_chat_id="C1", _status_thread_metadata={},
session_key="sk1", stream_consumer_holder=[None])