fix(agent): resend a stalled model request; keep turns of a timed-out step
Merged
OK
2mo ago+322+322+012345678901234567890123456789-20-20-01234567890123456789
One model request that never answered held an agent.act step until the step's own timeout. Found in a live e2e explore run on the testbed bug garden (apps/testbed/scripts/explore-bench.mjs): step 1 "Browse the catalog" ended STEP_TIMEOUT at 240 s, but the --debug step table added up to 23 s of work (model 19.4 s, observe 3.2 s, action 0.7 s). The Playwright trace showed the page idle for 220.4 s after the last observation. One provider request stalled, and the only bound on it was the step clock (timeout: remainingMs() in packages/e2e/src/agent/tool-loop.ts).
packages/e2e/src/agent/model/stall.ts:withStallGuardwraps the model with the SDK'swrapLanguageModel. EachdoGenerategets a 120 s bound (MODEL_CALL_STALL_MS). When the bound fires, the request is aborted (and abandoned if the provider ignores the abort) and fails with a retryableAPICallError, and the SDK's transport retries (TRANSPORT_RETRIES = 5, with backoff) send it again. A cancellation from the caller (step clock, run abort) passes through unchanged.- The tool loop and the judgment adapter (
model/sdk.ts) both send requests through the guard. The tool loop also notes each resend on the turn before it:[loop] turn 2 got no response in 120s: sending it again. - A step the clock ended used to lose all its turns.
act.tsraces the step against its deadline and settles without waiting for the executor, but the loop only attached its turns when it returned. Turns are now attached as each turn ends and after each note, so aSTEP_TIMEOUTstep keeps them inreport.jsonand in the--debugtranscript. - Loop notes now live in a separate field on the loop's own turn record. The report clips the outcome (600 chars) and the notes (1000 chars) separately, so a note appended after long tool results is no longer cut off. Tool output that happens to start with
[loop]is still clipped like any other output.
sequenceDiagram
participant L as tool loop
participant G as stall guard
participant P as provider
L->>G: doGenerate (turn 2)
G->>P: request
Note over G,P: no answer in 120 s
G-->>L: APICallError (isRetryable)
Note over L: [loop] turn 2 got no response in 120s: sending it again
L->>G: SDK transport retry (backoff)
G->>P: request
P-->>L: tool calls
Design decisions to review
- Why 120 s. luna-fast's slowest call in the explore run was 6.7 s, and slow reasoning turns on big screens can take tens of seconds. The explore step budget is 240 s, so 120 s still leaves room for one full resend inside a step.
- The bound covers the whole request, not time to first byte.
doGeneratedoes not stream, so a healthy request that needs more than 120 s for one answer (a slow local model on a large screen) is also cut off and sent again. After the retries run out it failsMODEL_PROVIDER_FAILED.docs/reference/errors.mdx,docs/debugging.mdx, and the changeset say so. A true idle-stream bound would mean moving the loop todoStream, which is a larger change. - Why a retryable
APICallErrorinstead of our own resend. The SDK's retry keeps the loop state, reuses its backoff and the existing retry budget, and still stops at the caller's step timeout. A second retry path in the loop would duplicate all of that. - Not configurable yet. The config shape is locked for this release. If a knob is needed, it can go on the agents entry after the release.
Verified
Ran it locally: yes
- Stall repro on the testbed (
e2e run --config e2e.stall.config.ts, a scratch config not committed). It wrapsgateway('openai/gpt-6-luna-fast')in a middleware that holds the 2nd request until aborted, then runs oneagent.act('open the Todos page and add a todo named "Stall check"').
| main | this branch | |
|---|---|---|
| result | TEST_TIMEOUT at 240.15 s | passed in 129.27 s |
| model calls | 1 | 4 (1 stalled, resent after 120 s) |
turns in report.json | 0 | 4, turn 1 carries the note |
main:
× a stalled model call is sent again > agent.act "open the Todos page and add a todo named "Stall check"" 239.96s · 1 model call failed
❯ |web| tests-stall/stall.e2e.ts (1 test | 1 failed) 240.15s ai 3.9k tokens · 97% cached · $0.0001
× a stalled model call is sent again (timed-out) 240.15s ai 3.9k tokens · 97% cached · $0.0001
→ test timed out after 240000 ms in phase body
this branch:
✓ a stalled model call is sent again > agent.act "open the Todos page and add a todo named "Stall check"" 128.96s · 4 model calls
✓ |web| tests-stall/stall.e2e.ts (1 test) 129.27s ai 17.6k tokens · 97% cached · $0.0005
report.json turns (index, calls, loop notes):
[1,["tap({\"target\":\"n4\"})"],["[loop] turn 2 got no response in 120s: sending it again"]]
[2,["type({\"target\":\"n6027\",\"value\":\"Stall check\"})"],[]]
[3,["tap({\"target\":\"n6028\"})"],[]]
[4,["complete_step({\"status\":\"passed\",\"summary\":\"Opened Todos and added the todo “Stall check”; ...\"})"],[]]
- Original bug, explore on the bug garden (main build). The 240 s step spent 23 s working:
× 1 Browse the catalog 240.01s · 7 actions · 2 findings ended at its limit
agent.act exceeded its 240000 ms timeout
step total model observe action calls
agent.act "Open Catalog and inspect the listed books, including… 240.0s 19.4s 3180ms 688ms 9
- No regression in the agent loop:
- testbed agent suite live (
--config e2e.agent.config.ts --no-cache): 24/24 passed, 124 model calls. main was also 24/24. - web-benchmark agent replay (
CI=1 ... --config e2e.agent.config.ts --strict-cache): 32/32 passed, noREPLAY_STALE.
- testbed agent suite live (
- Unit tests in
packages/e2e/tests/unit/tool-loop-stall.test.ts:- covered: the guard alone, a provider that ignores the abort or throws synchronously, a stalled turn resent with its note, a stalled judgment resent, turns kept on a step timeout, notes kept after the clip, and tool output that looks like a note still clipped.
- fail on main: the step-timeout-keeps-turns and loop-note tests.
- fail if the loop or the adapter stops routing through the guard: the loop and judgment wiring tests.