Repository navigation
fix(agent-server): distinct error for stall-watchdog kill vs max-duration timeout (#1094) - #1147
Conversation
…tion timeout (#1094) The per-tool no-output stall watchdog and the max-duration budget are two distinct termination paths, but the terminal 504 always claimed "Task execution timed out after {effective_timeout} seconds" regardless of which fired. A 300s stall-kill at ~531s wall-clock was therefore persisted as "timed out after 2400 seconds", misleading operators into bumping the execution timeout — the wrong knob (the 300s no-output threshold is what actually fired). The watchdog already distinguished the two causes for its log line but discarded that for the persisted error. Now: - `HeadlessRunContext` carries `termination_reason` ("stall_no_output" | "max_duration") and `stalled_tool`, set in the watchdog's `except subprocess.TimeoutExpired` before re-raising. - The orchestrator builds a reason-specific 504. Stall returns a structured dict detail `{message, termination_reason, stalled_tool}`; max-duration returns `{message, termination_reason}`. The backend's existing HTTPError handler (task_execution_service.py:1019-1021) reads `detail["message"]`, so the persisted `schedule_executions.error` now reflects the real cause and the structured reason rides along for the UI/API — no DB migration. - The "timed out after Ns" label is now reserved for genuine budget exhaustion (elapsed ≈ N), as the issue asked. Out of scope (per issue): a per-task override of the 300s stall threshold, and a dedicated termination_reason DB column. Tests: tests/unit/test_1094_stall_termination_reason.py asserts both ctx outcomes via the #970 fake-Popen harness; full #970 wait-loop suite still green (11 passed). Base image rebuilt (trinity-agent-base:0.6.0/latest); fix verified inside the image. Related to #1094 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The regression-diff CI runs `cd tests; pytest unit/` and collects each changed test file in isolation, where `tests/unit` is not on sys.path — so `from test_headless_executor_970_timeout import ...` raised ModuleNotFoundError at collection (reported as a new [E] under HEAD). It only passed locally because I ran it next to the #970 file. Inline the small fake-Popen harness (ctx/_FakePopen/_FakePipe/ patched_loop/_install_popen) instead of importing from the sibling test module. Verified CI-style: `pytest unit/test_1094_...py` from tests/ under pytest-randomly now passes (2), and 11 pass alongside the #970 file. Related to #1094 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
| @@ -1021,11 +1035,35 @@ async def execute_headless_task( | |||
| detail=f"Task execution timed out after {ctx.effective_timeout} seconds" | |||
There was a problem hiding this comment.
Asymmetric structured reason — outer-timeout path drops termination_reason.
This PR makes the inner subprocess.TimeoutExpired max-duration branch (just below) return a structured detail={"message": ..., "termination_reason": "max_duration"}, but this sibling asyncio.TimeoutError safety-net raise — same semantic cause (budget exhausted) — still emits a plain string with no termination_reason.
Failure scenario: a UI/API consumer filtering executions by termination_reason == "max_duration" silently misses any budget timeout that fired via the outer net instead of the inner wait-loop. The backend still persists the message (it handles both string and dict detail), so this is a structured-field gap, not a crash.
Suggest making the two max-duration exits symmetric:
| detail=f"Task execution timed out after {ctx.effective_timeout} seconds" | |
| detail={ | |
| "message": f"Task execution timed out after {ctx.effective_timeout} seconds", | |
| "termination_reason": "max_duration", | |
| } |
There was a problem hiding this comment.
Fixed in 0a81fb6 — outer asyncio.TimeoutError path now raises the same structured {message, termination_reason: "max_duration"} detail. Both max-duration exits are symmetric; base image rebuilt and verified (count=2 in image).
…h too (#1094 review) Review follow-up: the inner subprocess.TimeoutExpired max-duration branch returned a structured detail dict carrying termination_reason, but the sibling outer asyncio.TimeoutError safety-net raise — same semantic cause (budget exhausted) — still emitted a plain string. A consumer filtering executions on termination_reason="max_duration" would miss budget timeouts that fired via the outer net. Make the two exits symmetric. Related to #1094 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Resolve by running |
vybe
left a comment
There was a problem hiding this comment.
Validated via /validate-pr: distinct termination reasons verified end-to-end — backend dict-detail parsing confirmed at task_execution_service.py:1018-1021, unit tests cover both paths, CI green on all seeds. Approving.
Summary
The headless executor's per-tool no-output stall watchdog (300s) and the max-duration budget are two distinct termination paths, but both raised a bare
subprocess.TimeoutExpiredand the terminal 504 always claimedTask execution timed out after {effective_timeout} seconds. So a 300s stall-kill at ~531s wall-clock was persisted astimed out after 2400 seconds— operators misdiagnosed it and bumped the execution timeout (the wrong knob; the 300s no-output threshold is what fired).Root cause (confirmed)
docker/base-image/agent_server/services/headless_executor.py:except(:653) computed a distinctreasonbut only logged it, then re-raised bare.except subprocess.TimeoutExpired(:1023) stamped the generic string unconditionally.raise_for_status→httpx.HTTPErrorhandler (task_execution_service.py:1019-1025) writesdetailtoschedule_executions.error. So the agent's label is the persisted error → fix is agent-side only.Changes
HeadlessRunContextgainstermination_reason("stall_no_output"|"max_duration") +stalled_tool, set in the watchdog before re-raise.{message: "Killed: tool '<name>' produced no output for 300s (stall watchdog)", termination_reason: "stall_no_output", stalled_tool}{message: "Task execution timed out after Ns", termination_reason: "max_duration"}detail["message"]) → persisted error now reflects the real cause; structured reason rides along for UI/API. No DB migration.asyncio.TimeoutErrorsafety net (genuine budget exhaustion, elapsed ≈ N) keeps the plain "timed out after Ns" string.Cross-references
termination_reasonDB column.Verification
tests/unit/test_1094_stall_termination_reason.py— asserts both ctx outcomes via the bug: headless_executor reader thread leak causes 2h execution hangs #970 fake-Popen harness.py_compileclean. Base image rebuilttrinity-agent-base:0.6.0/latest; fix grep-verified inside the image.(Live repro needs a >300s silent tool call — covered by unit tests instead of a 5-min live wait.)
AC
stall_no_outputvsmax_duration)Related to #1094
🤖 Generated with Claude Code