Skip to content

fix(node-claude): keep long node runs alive and resume stalled sessions - #69

Merged
tomorrowflow merged 4 commits into
mainfrom
fix/node-claude-stall-abort
Oct 14, 2026
Merged

tomorrowflow merged 4 commits into
mainfrom
fix/node-claude-stall-abort

Conversation

@tomorrowflow

Copy link
Copy Markdown
Owner

Problem

On 2026-10-13 Workboard node ticket UX-7 ran a claude-cli turn placed on the Mac node. Claude made tool calls every few seconds for 10 minutes, but the Gateway saw no progress after about 20 s. The stall classifier marked the run stalled_agent_run (lastProgress=model_call:stream_progress), and stuck-session recovery aborted it at 633 s (the Mac process exited with 137). The stalled-turn continuation then ran as a fresh Claude session (useResume=false session=none historyPrompt=none) with no brief, and Claude replied that it had no request.

There were two root causes:

  1. The node host capped forwarded stdout at 200 KB. invoke-agent-cli-claude.ts stopped forwarding stdout after 200 KB. Forwarded chunks are the Gateway's only progress and liveness signal for a node run, so any node run that streamed more than 200 KB went quiet to the Gateway and was aborted about 600 s later. The cap came from feat(nodes): continue Claude catalog sessions on paired nodes via streaming CLI agent runs openclaw/openclaw#105833 ("caps streamed output consistently with system.run"). That analogy doesn't hold: system.run returns its whole output, but this stream is parsed incrementally.
  2. The CLI session binding was persisted only at settlement. An aborted fresh turn left no binding. Stalled-turn recovery then started a new Claude session with no history, which also lacked the work the aborted turn had already done on the node.

The Workboard side of the incident (the card staying running after the aborted run) is fixed in a separate PR.

Solution

Fix 1: forward every chunk (node host). onStdoutRaw forwards every decoded chunk. This removes the byte cap, the terminal-result re-injection, and the truncation bookkeeping, which only existed to work around the cap. The node now always reports truncated: false. The Gateway already bounds its own buffers, as local runs do: the stdout capture head is capped at CLI_RUNNER_OUTPUT_PARSE_BYTES, and the stream-json parser enforces 8 MiB / 20,000 lines per turn. The Gateway's truncation guard stays as compatibility for older node hosts, and its comment now says so.

Fix 2: publish a fresh session binding as soon as the CLI reports it. When the stream reports a fresh session id (system/init), the reply path writes the binding through the session-store owner. A new store function, persistStartedCliSessionInStore, handles the write.

  • The write uses the same commit guard as settlement: session id, lifecycle revision, active writer, current settlement owner, and no abort.
  • It only replaces the binding the turn started from.
  • Settlement still writes the full binding later, adding the resume checkpoint and reseed receipt. The early write is awaited before settlement so the two cannot race.
  • The eligibility rules match settlement. Only fresh attempts qualify, never resume, fork, isolated or control runs, or sessionMode: none. The reply path also excludes room events and preserved-provenance turns.
  • An early binding follows the existing failure policy. An abort keeps it and session_expired clears it.
  • buildCliSessionBinding is extracted from settlement so both writes use the same reuse facts.
  • The fork-successor early write already used this pattern.

Docs: docs/gateway/cli-backends.md now says that an aborted reply turn's fresh session is kept, and that a managed live session interrupted after partial text still drops it.

Impact

  • Node-placed Claude runs no longer go quiet to the Gateway after 200 KB of output. Long node sessions report progress and liveness for as long as they stream.
  • Stalled-run recovery resumes the same Claude session, so the continuation sees the original request and the work already done.
  • The Mac node also needs this build. Fix 1 runs on the node host. A new Gateway with an old node still caps at 200 KB, and the Gateway's truncation guard still handles that case.

Tradeoffs, deliberate and noted from review:

  • Size limits now match local runs. Node runs above 8 MiB or 20,000 stream-json lines now fail with the same format error local runs already get. Before, the node never sent enough for the Gateway to reach that limit.
  • No node-side backpressure. The node queues progress writes and sends them one at a time without pausing stdout reads. If the Gateway round trip is slower than Claude's output, pending text grows with total stdout, where the old cap bounded it at about 1.2 MB.
  • Early bindings outlast failures like established ones. If a fallback-classified result or a thrown non-session_expired failover follows the early write, the fresh binding stays. That matches what already happens to a binding the turn started with. A later same-provider fallback candidate resumes that session instead of starting fresh.
  • Reseed receipt is missing until settlement. An interrupted fresh turn that reseeded OpenClaw history has no reseed receipt, because only settlement creates it. Imported native history may then show the synthetic reseed prompt instead of stripping it. This affects display only.
  • Live-session interruptions still drop the session. A managed local live session interrupted after streaming partial text still clears its unfinished fresh session. This keeps the upstream rule from fix(cli): keep conversation context after cancellation openclaw/openclaw#147139 for returned interruptions, so local live-session stall recovery of that shape is not covered here.
  • agent_end is unchanged. The aborted run still reports a failed agent_end. The recovery run builds hook messages and its success flag the same way. It now resumes the session (useResume=true) instead of starting fresh.

Evidence

Each regression test failed before its fix and passed after it. I checked red by restoring only the production file from the parent commit:

  • src/node-host/invoke-agent-cli-claude.test.ts, "forwards every stdout byte as progress past the former 200 KB cap". Red: the progress stream stopped at 200 KB. Green: the full 300 KB stream arrives, including the split multibyte character, the late tool_use line and the result line, with truncated: false.
  • src/auto-reply/reply/agent-runner-execution-cli-admission.test.ts, "keeps an aborted %s turn's fresh session only when it may establish continuity". This test runs the real reply candidate against a real SQLite session store. Red (ordinary): expected undefined to deeply equal {…}. Green: an ordinary aborted fresh turn keeps the binding, and room-event and preserved-provenance turns publish none.
  • src/agents/cli-runner/execute.supervisor-capture.test.ts, "publishes a fresh session as soon as the CLI reports it, even if the turn then fails". Red: the callback was called 0 times. Green: it is called once with the streamed session id, before the failure settles.

Wall time with pnpm test <file> --maxWorkers=1 (warm, this host):

  • src/node-host/invoke-agent-cli-claude.test.ts: 33 s wall, 10.7 s Vitest. The new case takes about 50 ms.
  • src/auto-reply/reply/agent-runner-execution-cli-admission.test.ts: 32 s wall, 30.5 s Vitest. The three new cases take about 0.5 s each.
  • src/agents/cli-runner/execute.supervisor-capture.test.ts: 39 s wall, 37 s Vitest. The new case takes about 30 ms.

The new tests add about 2 CI seconds in total and use no real timers or sleeps.

Suites:

  • src/node-host, src/agents/cli-runner/, src/agents/cli-session*, cli-runner.reliability, cli-runner.spawn, the admission suite and command/attempt-execution.cli all pass.
  • cli-runner.spawn.test.ts failed to load once while tsgo shards were running in parallel. It passed alone (37/37).

Typecheck: pnpm tsgo:core is clean. The test shards agents-root, agents-other, agents-tools, messaging, other, gateway-* and infra are clean. One pre-existing unrelated error appears in state-logging: src/audit/message-delivery-progress-store.test.ts cannot find .github/actions/git-owner/test-prerequisites.json.

Lint and format: scripts/run-oxlint.mjs on the touched files, oxfmt, pnpm format:docs:check and git diff --check are clean.

Not verified: a live node run against the Mac. This PR is not deployed.

🤖 Generated with Claude Code

tomorrowflow and others added 4 commits October 14, 2026 07:43
A node-placed Claude run capped forwarded stdout at 200 KB. After the cap
the Gateway saw no further progress, so a long session went blind and the
stall classifier aborted it about 600 s later. The Gateway parses the
stream incrementally and bounds its own buffers, so forward every chunk and
drop the truncation and terminal-result re-injection machinery.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The CLI session binding was written only at settlement. A fresh turn that
stalled-run recovery aborted left no binding, so the recovery continuation
started a new Claude session with no history and answered that it had no
request. Publish the binding through the session store owner when the
stream reports the session id, under the turn's commit guard. Settlement
still writes the full binding afterwards. Room-event and
preserved-provenance turns stay excluded, matching settlement, and an
early binding follows the established failure-clear policy.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Narrow the docs to aborted reply turns, since a managed live session
interrupted after partial text still drops an unfinished fresh session.
Mark the gateway truncation guard as old-node compatibility, and test
that preserved-provenance turns never publish an early binding.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
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.

1 participant