Skip to content

Fork Claude sessions in a worker so an oversized fork cannot crash other threads - #6855

Merged
ymichael merged 1 commit into
mainfrom
bb/isolate-claude-fork-thr_skxt32ek5a
Nov 2, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/isolate-claude-fork-thr_skxt32ek5a

Conversation

@ymichael

@ymichael ymichael commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Editing a message or forking a Claude Code thread loads the session transcript into memory inside the Claude provider bridge (forkClaudeSession in plugins/provider-claude-code/src/bridge/session-fork.ts). bb runs one bridge process per environment, shared by every Claude thread in that environment. #6782 (issue #6781) removed Node's 512 MiB string limit, so the fork now reads transcripts of any size. That turned the clean "Session not found" failure above 512 MiB into a heap exhaustion for the largest transcripts: the bridge aborts with SIGABRT and every Claude thread in that environment dies mid-turn. Measured on real-shaped transcripts at the default heap (about 4.3 GB on a 48 GB Mac): 2.0–3.2 GB forked, 4.0 GB crashed. Machines with less RAM get a smaller default heap and hit this sooner, for example the 2.3 GB heap on an 8 GB Windows VM.

What changed

  • plugins/provider-claude-code/src/bridge/session-fork.ts: forkClaudeSession runs the existing fork in a worker_threads worker that loads the plugin's own bundle (new URL(import.meta.url)) and runs the fork when its workerData asks for one.
    • The worker posts back the new session ID. Errors keep their message, for example the SDK's Session <id> not found.
    • ERR_WORKER_OUT_OF_MEMORY becomes Claude session <id> is too large to fork: forking it ran out of memory. Only the worker stops; the bridge and its other sessions keep running.
    • Worker stdout goes to the bridge's stderr, so it cannot corrupt the bridge's JSON-RPC stdout.
  • The fork store is unchanged and uses only the SDK's documented SessionStore interface. The size that forks successfully is unchanged; only the failure mode changes. Starting the worker costs about 0.1 s per fork.
  • Tests: the bridge tests mocked the SDK's forkSession, which a worker cannot see. bridge.conformance.test.ts now mocks bb's forkClaudeSession, and the unused forkSession mocks in bridge.test.ts and bridge.calibration.test.ts are removed. The mock-based test from Stream Claude fork transcripts so long threads can edit and fork #6782 is replaced, because vi.mock doesn't reach the worker.
  • No wire or protocol change, so no HOST_DAEMON_PROTOCOL_VERSION bump.

How you verified

New tests in session-fork.test.ts:

Test On main Here
A child process with a 32 MB heap forks a 128 MB transcript; it must receive "too large to fork" and keep running ❌ The process dies: FATAL ERROR: Reached heap limit … JavaScript heap out of memory ✅
A missing source session rejects with the SDK's Session <id> not found n/a ✅

pnpm exec turbo run typecheck lint test --filter='...[origin/main]' (scope: bb-plugin-provider-claude-code, @bb/bundled-plugins): 10/10 tasks, 416 tests passed. The existing #6325 bridge test now runs a real checkpoint fork through the worker.

Manual checks on a pnpm start:worktree server with the bb CLI and the UI. The oversized transcript was padded to 4 GB with copies of a real transcript. The bystander was a second Claude thread in the same environment, running a 90 s command.

Scenario main This branch
Edit a message in the 4 GB thread Toast: Provider "claude-code" exited unexpectedly (signal SIGABRT) plus a V8 stack trace Toast: Claude session … is too large to fork: forking it ran out of memory after about 13 s
Fork into new thread from the 4 GB thread The new thread shows thread.start failed with the SIGABRT stack trace The new thread shows thread.start failed with "too large to fork"
Bystander in the same environment Killed mid-command (1 interrupted, SIGABRT) Finishes and replies done
Normal-size fork and edit (CLI) ALPHA, BRAVO / ALPHA, DELTA Same, with calls taking about 1 s

Runtime and platform checks:

Runtime Result
macOS: system Node 22.23 and bb's Electron Node 24.15 A worker out of memory gives ERR_WORKER_OUT_OF_MEMORY; the main thread stays alive with its data intact
Windows 11 ARM64 VM, Node 24.22 Checkpoint fork, full fork and the missing-session error work. A 160 MB transcript under a small heap is rejected cleanly here; with main's code the process exits with code 134 (FATAL ERROR: Reached heap limit)

Refs #6781

🤖 Generated with Claude Code

AGENT GENERATED

…e bridge

Editing or forking a Claude thread loads its whole transcript into the
provider bridge, which serves every Claude thread in the environment.
A transcript too large for the heap aborted the bridge and killed
every Claude thread in that environment mid-turn.

The fork now runs in a worker thread that loads the same plugin
bundle. If it runs out of memory, only the worker stops: the edit or
fork fails with "Claude session <id> is too large to fork" and the
other threads keep running. Fork results and errors are unchanged
otherwise, and the store still uses only the SDK's documented
interface. Starting the worker adds about 0.1 s per fork.

The bridge tests mocked the SDK's forkSession, which a worker cannot
see. The conformance suite now mocks forkClaudeSession instead, and the
unused forkSession mocks are gone.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit cdd1d66 into main Nov 2, 2026
31 checks passed
@ymichael
ymichael deleted the bb/isolate-claude-fork-thr_skxt32ek5a branch November 2, 2026 23:23
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