Skip to content

fix(agent-server): distinct error for stall-watchdog kill vs max-duration timeout (#1094) - #1147

Merged
vybe merged 3 commits into
devfrom
feature/1094-stall-watchdog-label
Jun 11, 2026
Merged

vybe merged 3 commits into
devfrom
feature/1094-stall-watchdog-label

Conversation

@dolho

@dolho dolho commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

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.TimeoutExpired and the terminal 504 always claimed Task execution timed out after {effective_timeout} seconds. So a 300s stall-kill at ~531s wall-clock was persisted as timed 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:

  • Watchdog except (:653) computed a distinct reason but only logged it, then re-raised bare.
  • Orchestrator except subprocess.TimeoutExpired (:1023) stamped the generic string unconditionally.
  • Persist chain: agent 504 detail → backend raise_for_status → httpx.HTTPError handler (task_execution_service.py:1019-1025) writes detail to schedule_executions.error. So the agent's label is the persisted error → fix is agent-side only.

Changes

  • HeadlessRunContext gains termination_reason ("stall_no_output" | "max_duration") + stalled_tool, set in the watchdog before re-raise.
  • Orchestrator builds a reason-specific 504 with a structured dict detail:
    • stall → {message: "Killed: tool '<name>' produced no output for 300s (stall watchdog)", termination_reason: "stall_no_output", stalled_tool}
    • max-duration → {message: "Task execution timed out after Ns", termination_reason: "max_duration"}
  • Backend already parses dict detail (detail["message"]) → persisted error now reflects the real cause; structured reason rides along for UI/API. No DB migration.
  • The outer asyncio.TimeoutError safety net (genuine budget exhaustion, elapsed ≈ N) keeps the plain "timed out after Ns" string.

Cross-references

Verification

(Live repro needs a >300s silent tool call — covered by unit tests instead of a 5-min live wait.)

AC

  • Stall-kill no longer reuses the max-duration string
  • Distinct reason surfaced (stall_no_output vs max_duration)
  • "timed out after Ns" reserved for genuine budget exhaustion
  • Base image rebuilt and verified

Related to #1094

🤖 Generated with Claude Code

dolho and others added 2 commits June 11, 2026 12:27
…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"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Suggested change
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",
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]>
@dolho
dolho requested a review from vybe June 11, 2026 10:24
@github-actions

Copy link
Copy Markdown

⚠️ Nightly unit-suite check skipped — merge conflict against dev.

Resolve by running git merge dev locally and pushing the result. The next nightly run will re-test once the conflict is gone.

@vybe vybe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@vybe
vybe merged commit 9284529 into dev Jun 11, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants