Skip to content

The agent that grows with you

fix(gateway): a finite turn answers clarify headless at once

Teknium27231ae32265
diff --git a/agent/managed_worker.py b/agent/managed_worker.py
index 584d81124d9..83f8c198137 100644
--- a/agent/managed_worker.py
+++ b/agent/managed_worker.py
@@ -212,7 +212,11 @@ class WorkerControls:
def clarify(self, questions):
"""clarify_tool's batch contract: ``callback(questions) -> {answers, outcome, notice?}``.
One owner prompt per question, stopping at the first that is interrupted or unanswered;
- an empty answer is a skip (``None``), as on the other surfaces."""
+ an empty answer is a skip (``None``), as on the other surfaces. A finite turn (``chat -q`` /
+ ``-z``, bound by worker_turn_scope) answers headless at once: its viewer cannot answer."""
+ from gateway.session_finite import finite_clarify_reply, finite_turn_required
+ if finite_turn_required() is True:
+ return finite_clarify_reply()
answers = {}
reply = {'answers': answers, 'outcome': 'submitted'}
for entry in questions:
diff --git a/gateway/run_turn_runner.py b/gateway/run_turn_runner.py
index dbc55cc382f..e5d1f37933f 100644
--- a/gateway/run_turn_runner.py
+++ b/gateway/run_turn_runner.py
@@ -503,6 +503,11 @@ class TurnRunner(GatewayTurnProgressMixin, GatewaySessionAgentMixin):
last question — between two cards it only opens a bubble the next boundary closes."""
from gateway.run_turn_runner_clarify_delivery import UNDELIVERED, UNDELIVERED_DECLINED, UNDELIVERED_NO_SURFACE
from tools.clarify_gateway import CANCELLED, SKIPPED
+ if _finite_viewer_turn():
+ # `chat -q` / `-z`: the viewer detaches on the first prompt, so nobody can answer.
+ # Answer headless at once, as main's _single_query_clarify_callback did in process.
+ from gateway.session_finite import finite_clarify_reply
+ return finite_clarify_reply()
answers: dict[str, Any] = {}
reply: dict[str, Any] = {"answers": answers, "outcome": "submitted"}
last = len(questions) - 1
diff --git a/gateway/session_finite.py b/gateway/session_finite.py
index e27c3011e4f..7a2a58456bb 100644
--- a/gateway/session_finite.py
+++ b/gateway/session_finite.py
@@ -14,6 +14,16 @@ def finite_turn_required():
return _finite_turn.get()
+FINITE_CLARIFY_NOTICE = (
+ "single-query mode: no user available to answer. Pick the best choices using your own "
+ "judgment, or make the most reasonable assumption you can, and continue.")
+
+
+def finite_clarify_reply():
+ """clarify_tool's reply on a finite turn (``chat -q`` / ``-z``): main's headless answer."""
+ return {"answers": {}, "outcome": "undelivered", "notice": FINITE_CLARIFY_NOTICE}
+
+
def unattended_turn():
# `hermes -z`: nobody can answer a prompt, so the classic one-shot contract auto-approves
# (legacy oneshot.py set HERMES_YOLO_MODE). `-q` stays single-query (approvals.single_query_mode).
diff --git a/tests/gateway/test_local_viewer_events_submit.py b/tests/gateway/test_local_viewer_events_submit.py
index ceaf9552212..86714aad2ff 100644
--- a/tests/gateway/test_local_viewer_events_submit.py
+++ b/tests/gateway/test_local_viewer_events_submit.py
@@ -215,3 +215,26 @@ def test_only_a_classic_cli_surface_session_is_a_classic_cli_turn():
assert classic_cli_turn(turn) is expected
turn._ctx.source.platform = Platform.TELEGRAM
assert classic_cli_turn(turn) is False
+
+
+def test_a_finite_turn_answers_clarify_headless_at_once():
+ from gateway.run_turn_runner import TurnRunner
+ from gateway.session_finite import FINITE_CLARIFY_NOTICE, finite_turn_scope
+ turn = object.__new__(TurnRunner)
+ turn._ask_clarify_question = lambda *a, **k: pytest.fail("a finite viewer can never answer")
+ questions = [{"qid": "q1", "question": "Which env?", "choices": ["dev", "prod"], "multi_select": False}]
+ with finite_turn_scope(True):
+ reply = turn._clarify_callback_sync(questions)
+ assert reply == {"answers": {}, "outcome": "undelivered", "notice": FINITE_CLARIFY_NOTICE}
+ # main's -q text, verbatim
+ from hermes_cli.cli_agent_setup_mixin import _single_query_clarify_callback
+ assert reply == _single_query_clarify_callback(questions)
+
+
+def test_a_finite_managed_worker_answers_clarify_headless():
+ from agent.managed_worker import WorkerControls
+ from gateway.session_finite import finite_turn_scope
+ controls = object.__new__(WorkerControls)
+ controls._ask = lambda *a: pytest.fail("a finite viewer can never answer")
+ with finite_turn_scope(True):
+ assert controls.clarify([{"qid": "q", "question": "?", "choices": [], "multi_select": False}])["outcome"] == "undelivered"