Skip to content

fix(agent-server): wall-clock timeout on chat-mode subprocess (#313) - #436

Merged
vybe merged 1 commit into
mainfrom
fix/313-chat-wallclock-timeout
Apr 21, 2026
Merged

vybe merged 1 commit into
mainfrom
fix/313-chat-wallclock-timeout

Conversation

@dolho

@dolho dolho commented Apr 21, 2026

Copy link
Copy Markdown
Contributor

Summary

Closes #313. GUARD-003 follow-up — chat mode now has the wall-clock timeout that task mode already had.

Root cause

execute_claude_code in docker/base-image/agent_server/services/claude_code.py called:

return_code = process.wait()                    # no timeout
stderr_output, return_code = await loop.run_in_executor(...)  # no asyncio.wait_for

If the Claude CLI subprocess stalls — billing error, stuck stream, hook grandchild wedge that --max-turns doesn't catch — the chat session hangs until the container is killed externally. Task mode (execute_headless_task) correctly uses asyncio.wait_for + process.kill(); chat mode silently lacked the parity.

Fix

Mirror task mode's two-layer pattern:

  • New constant: _DEFAULT_EXECUTION_TIMEOUT_SEC = 1800 (30 min).
  • Guardrails read: timeout_seconds = int(guardrails.get("execution_timeout_sec") or _DEFAULT_EXECUTION_TIMEOUT_SEC). Field already exists in the shared guardrails schema (agent_config.py:497, range 60–7200).
  • Inner bound: process.wait(timeout=timeout_seconds). On subprocess.TimeoutExpired: _terminate_process_group(graceful_timeout=5) → drain readers → re-raise.
  • Outer safety net: asyncio.wait_for(..., timeout=timeout_seconds + 60). On asyncio.TimeoutError: _terminate_process_group(graceful_timeout=2) + _safe_close_pipes. Both timeout paths raise HTTPException(504).
  • registry.unregister(execution_id) still runs via the existing finally block.

Test plan

  • tests/unit/test_chat_wallclock_timeout.py — 6/6 pass in 0.32s.
    • AST-level regression guards: default constant (1800), guardrails read wired, inner process.wait(timeout=…) + TimeoutExpired handling, outer asyncio.wait_for + asyncio.TimeoutError handling, ≥2 HTTP 504 raises.
    • Behavioural smoke: subprocess.Popen.wait(timeout=0.3) raises TimeoutExpired on this platform (guards against a primitive-level regression).
  • Python syntax check clean.
  • /review pre-landing pass: 0 critical, 1 non-blocking informational (timeout source-of-truth note).

Scope

  • docker/base-image/agent_server/services/claude_code.py — 49+/5-.
  • tests/unit/test_chat_wallclock_timeout.py — new, 159 lines.

No router, DB schema, or external API change. No behaviour change when the subprocess completes before the timeout.

Notes

The unit tests are source-AST regression guards rather than runtime tests of the full execute_claude_code. Reason: the module has relative imports (from ..models, from ..state) that require the full agent_server package to be importable. The helpers the fix composes (_terminate_process_group, _drain_reader_threads, _safe_close_pipes) are already behaviourally covered by tests/unit/test_subprocess_pgroup.py, and the pattern mirrors the production-proven task-mode code. Behavioural regression of the full chat-mode timeout path would require the agent-container integration harness.

🤖 Generated with Claude Code

`execute_claude_code` called `process.wait()` with no timeout and ran the
subprocess executor without `asyncio.wait_for`. If the Claude CLI stalled
on a billing error, infinite stream, or a hook grandchild wedge that
`--max-turns` couldn't catch, chat sessions hung indefinitely until the
container was killed externally. This is the GUARD-003 gap that was
explicitly deferred from Phase 1.

Fix mirrors the existing task-mode pattern in `execute_headless_task`:

- Read `execution_timeout_sec` from the guardrails runtime config with a
  30-minute default (`_DEFAULT_EXECUTION_TIMEOUT_SEC = 1800`).
- Inner `process.wait(timeout=timeout_seconds)` bounds the executor thread
  — on `TimeoutExpired` we terminate the process group (graceful→SIGKILL),
  drain reader threads, and re-raise.
- Outer `asyncio.wait_for(..., timeout=timeout_seconds + 60)` is the async
  safety net with a 60-s grace buffer for drain/cleanup.
- Both timeout paths raise `HTTPException(504)`; `registry.unregister`
  still fires via the existing `finally` block.

Tests: AST-level regression guards on the constant, guardrails read,
inner/outer wait pattern, and 504 raise count, plus a behavioural smoke
test that `subprocess.Popen.wait(timeout=X)` raises `TimeoutExpired` on
this platform (guards against a primitive-level regression).

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

@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: mirrors task-mode wall-clock timeout pattern. ✅

@vybe
vybe merged commit 5f3841d into main Apr 21, 2026
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.

bug: chat mode has no wall-clock timeout (GUARD-003 follow-up)

2 participants