refactor(tools): split wait_for_message into respond_to_user + wait_for_agents

One tool was doing three jobs (wait on the user, wait on other agents, and
- wrongly - wait for a long-running command), so the driver had to guess which
one an agent meant and used parent_id as the proxy: the root waits for a human,
everyone else waits for agents. That proxy is wrong, since the user can message
any agent from the TUI's agent tree.

Tool identity now carries the intent, and the coordinator records it as a
wait_kind that survives snapshot/restore:

  respond_to_user  -> wait_kind="user",   never auto-resumed (root or not)
  wait_for_agents  -> wait_kind="agents", auto-resumed on a 300s timer
  recovery exhaust -> wait_kind="stalled"

respond_to_user fuses the message and the yield into one call, so there is no
way to answer and then forget to stop - the two-step that gpt-4o-mini skipped
2/2 in live testing. Plain text still renders as before.

Auto-resume is also bounded now: an agent that re-parks after every timeout
burned a model turn every 300s for the rest of the scan (and, since parked
children notify their parent, spammed the parent's inbox on the same cycle).
After _MAX_IDLE_AUTO_RESUMES it stays parked until a real message arrives.
This commit is contained in:
Ahmed Allam
2026-08-01 22:13:07 +00:00
parent b7bf52c468
commit 8b464dae5d
20 changed files with 539 additions and 128 deletions
+60 -5
View File
@@ -847,15 +847,15 @@ async def test_interactive_text_only_turn_is_nudged_instead_of_parking(
# The retry carries an explicit "call a tool" nudge rather than empty input.
nudge = calls[1][0]["content"]
assert "without a tool call" in nudge
assert "wait_for_message" in nudge
assert "respond_to_user" in nudge
assert coordinator.statuses["root"] == "completed"
@pytest.mark.asyncio
async def test_interactive_wait_for_message_parks_without_a_nudge(
async def test_interactive_explicit_park_gets_no_nudge(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""``waiting`` is now only reachable by an explicit wait_for_message call."""
"""``waiting`` is only reachable via respond_to_user / wait_for_agents."""
coordinator = AgentCoordinator()
await coordinator.register("root", "strix", parent_id=None)
calls: list[Any] = []
@@ -898,7 +898,7 @@ async def test_interactive_subagent_exhaustion_tells_its_parent(
"""A parked child must report up so its parent stops waiting on it.
The parent is an agent, not a watching human, so a parent blocked in
wait_for_message otherwise burns its whole timeout on a completion
wait_for_agents otherwise burns its whole timeout on a completion
report the child can no longer send.
"""
coordinator = AgentCoordinator()
@@ -978,7 +978,7 @@ async def test_tool_required_message_is_persisted_to_the_session(tmp_path: Any)
stored = [cast("dict[str, Any]", i) for i in await session.get_items()]
assert "finish_scan" in stored[0]["content"]
assert "wait_for_message" in stored[0]["content"]
assert "respond_to_user" in stored[0]["content"]
session.close()
@@ -1033,3 +1033,58 @@ async def test_recovery_count_is_cleared_by_a_lifecycle_tool(
await _drive(coordinator, "root", interactive=True)
assert "root" not in coordinator.recovery_counts
@pytest.mark.asyncio
async def test_agent_awaiting_a_human_is_never_auto_resumed() -> None:
"""The user can message any agent, so parking for one is not root-only."""
coordinator = AgentCoordinator()
await coordinator.register("root", "strix", parent_id=None)
await coordinator.register("child", "recon", parent_id="root")
for agent_id in ("root", "child"):
await coordinator.park_waiting(agent_id, wait_kind="user")
assert await execution._plain_waiting_timeout(coordinator, agent_id) is None
@pytest.mark.asyncio
async def test_agent_awaiting_other_agents_is_re_checked_on_a_timer() -> None:
coordinator = AgentCoordinator()
await coordinator.register("root", "strix", parent_id=None)
await coordinator.park_waiting("root", wait_kind="agents")
timeout = await execution._plain_waiting_timeout(coordinator, "root")
assert timeout == execution._WAITING_AUTO_RESUME_TIMEOUT_S
@pytest.mark.asyncio
async def test_idle_auto_resumes_stop_after_their_budget() -> None:
"""A wedged agent must not burn a model turn per timeout for the whole scan."""
coordinator = AgentCoordinator()
await coordinator.register("child", "recon", parent_id="root")
await coordinator.park_waiting("child", wait_kind="agents")
for _ in range(execution._MAX_IDLE_AUTO_RESUMES):
assert await execution._plain_waiting_timeout(coordinator, "child") is not None
await coordinator.record_idle_resume("child")
assert await execution._plain_waiting_timeout(coordinator, "child") is None
# A real message is real progress, so the budget starts over.
await coordinator.reset_idle_resumes("child")
assert await execution._plain_waiting_timeout(coordinator, "child") is not None
@pytest.mark.asyncio
async def test_wait_kind_survives_a_snapshot_round_trip() -> None:
coordinator = AgentCoordinator()
await coordinator.register("root", "strix", parent_id=None)
await coordinator.park_waiting("root", wait_kind="user")
await coordinator.record_idle_resume("root")
restored = AgentCoordinator()
await restored.restore(await coordinator.snapshot())
assert restored.wait_kinds["root"] == "user"
assert restored.idle_resume_counts["root"] == 1
assert await execution._plain_waiting_timeout(restored, "root") is None