Skip to content

[bugfix] agent: end a turn when the model read times out with no data (#345) - #419

Open
Birfy wants to merge 2 commits into
ThinkFlowLab:mainfrom
Birfy:fix/345-model-read-timeout
Open

Birfy wants to merge 2 commits into
ThinkFlowLab:mainfrom
Birfy:fix/345-model-read-timeout

Conversation

@Birfy

@Birfy Birfy commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #345.

When the model gateway waited longer than the fixed 120s read deadline and forwarded no bytes, it returned 502 and the bundled OpenCode runtime retried the same request (five hardcoded retries on 502, with 2s-scale backoff). The live turn never reached a terminal state: the panel stayed on "thinking", and interrupted only appeared after a reload.

This change:

  • Treats an unanswered model failure (forwardedBytes == 0) as the end of the turn: close the gateway, stop the harness process, and stream an end event with status: interrupted and a timeout error. A user stop remains stopped.
  • Keeps 120s as the default, applied to the whole read (including a mid-stream stall), not only the first byte.
  • Makes the deadline configurable with OPENDIRECTOR_MODEL_READ_TIMEOUT (seconds). Invalid, non-positive, or missing values keep the default. The inbound harness HTTP parse still has its own 180s bound; that bound no longer caps a longer configured upstream read.

OpenCode 1.19.5 still hardcodes retry-on-502 (up to five times). There is no supported OPENCODE_CONFIG_CONTENT retry policy in this version, so this PR does not invent a config key the runtime would ignore. Ending the turn after the first unanswered failure is what stops the silent re-sends.

Suggested labels: type:bug, area:agent, area:harness, area:provider.

Test plan

Reproduction used a silent local TCP listener as the model endpoint (no provider account, no paid calls). A harness stand-in retried 502 up to six times and would otherwise hang. After the fix, the assistant turn ends after one attempt.

Commands and results (Asia/Shanghai, Python 3.13.5, httpx 0.28.1, isolated checkout /tmp/od-wt-345 from main ffa16c46, PYTHONPATH=packages/core/src:apps/api/src):

python -m pytest tests/test_model_gateway.py -q
# 13 passed in 0.46s

python -m pytest tests/test_native_runtime.py::test_unanswered_model_timeout_ends_the_turn_without_silent_retries -q
# 1 passed in 1.01s
# The turn ended with an English `end` error ("did not respond in time"),
# conversation status `interrupted`, and exactly one upstream attempt.

python -m pytest tests/test_native_runtime.py tests/test_runtime_readiness.py -q -k "not real_opencode"
# 43 passed, 1 failed, 5 deselected in 79s

The one failure, test_rejects_removed_runtime_and_invalid_selection_before_acceptance, returned 503 (default 未安装,请安装 CLI 并登录。) instead of 409. This checkout does not have the bundled OpenCode install (npm ci was not run; the box was at inode capacity). That path is unchanged by this PR and was not treated as a regression.

Not run / not claimed:

  • Full Python suite, TypeScript/i18n/build, and release-check aggregate (not run in this environment).
  • OPENDIRECTOR_REAL_CLI=1 bundled OpenCode integration (binary not installed here).
  • Browser UI interaction. The live panel already applies a streamed end event (useAssistantChat); the API test asserts that event and the persisted interrupted status. A headed click-through of the assistant panel was not performed, so visual wording/layout was not independently verified.
  • Real DeepSeek / Zhipu / Moonshot calls. No paid generation was issued.

CI on this PR should run the repository release checks, including the Python and merge-marked native tests.

…ThinkFlowLab#345)

A first-byte or mid-stream stall longer than the read deadline used to
return 502 and leave the bundled OpenCode runtime retrying silently, so
the chat stayed on "thinking" until reload. End the live turn after an
unanswered failure, stream the terminal state, and keep 120s as the
default whole-read timeout, overridable with OPENDIRECTOR_MODEL_READ_TIMEOUT.
@github-actions github-actions Bot added area:agent Agent runtime, assistant chat and tools area:harness Harness and skill integration (Default, OpenCode, Codex, Claude Code, Jiuwen, DSH) area:provider Model providers and generation protocols type:bug Something is broken labels Nov 21, 2026
@FayeSpica

Copy link
Copy Markdown
Collaborator

@Birfy please resolve conflicts

…meout

# Conflicts:
#	packages/core/src/opendirector/agent/native.py
@Zhangbo-cn

Copy link
Copy Markdown
Contributor

PASS 8248c59 Fixes #345

  • model_read_timeout() falls back to the 120 s default on None, '', 0, -1, non-numeric, nan and ±inf, so a bad env value cannot disable the deadline
  • the zero-byte path closes the gateway, terminates the harness process and raises 504 model_read_timeout; forwardedBytes > 0 is deliberately excluded, so a partial reply is never reported as a complete body (the closing 0\r\n\r\n is only written on successful exhaustion)
  • the 180 s asyncio.timeout at agent/model_gateway.py:113 wraps inbound request parsing only and is independent of the configured read timeout, so the two deadlines cannot truncate each other
  • on_unanswered_failure is new API, and the tests exercise both the helper and a harness stand-in that retries 502 six times, so they cannot pass on the previous revision
  • Chinese at the raise site plus model_read_timeout → English in apps/api/.../localization.py follows the existing bilingual split

Ran: Release checks green on 8248c59 (13/13); local pytest tests/test_model_gateway.py tests/test_native_runtime.py → 41 passed, 1 failed; that failure reproduces identically on base bacea89 (503, no bundled runtime in this worktree), so it is not caused by this tip
Not run: a real provider call (the author used a silent local listener); packaged-runtime smoke

Non-blocking question: a mid-stream stall also ends with the same "no data in time" copy as a zero-byte timeout, but skips on_unanswered_failure because bytes were already forwarded. Please state what the harness does with that truncated stream, so nobody reads this as covering mid-stream stalls end to end.

Coordinator: ping after this Reviewer PASS and green CI on this SHA. This skill does not merge.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:agent Agent runtime, assistant chat and tools area:harness Harness and skill integration (Default, OpenCode, Codex, Claude Code, Jiuwen, DSH) area:provider Model providers and generation protocols type:bug Something is broken

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] agent: a turn hangs forever when the provider's first byte exceeds the fixed 120s read timeout

3 participants