Repository navigation
Fork Claude sessions in a worker so an oversized fork cannot crash other threads - #6855
Merged
Merged
Conversation
…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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 (
forkClaudeSessioninplugins/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 withSIGABRTand 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:forkClaudeSessionruns the existing fork in aworker_threadsworker that loads the plugin's own bundle (new URL(import.meta.url)) and runs the fork when itsworkerDataasks for one.Session <id> not found.ERR_WORKER_OUT_OF_MEMORYbecomesClaude 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.SessionStoreinterface. The size that forks successfully is unchanged; only the failure mode changes. Starting the worker costs about 0.1 s per fork.forkSession, which a worker cannot see.bridge.conformance.test.tsnow mocks bb'sforkClaudeSession, and the unusedforkSessionmocks inbridge.test.tsandbridge.calibration.test.tsare removed. The mock-based test from Stream Claude fork transcripts so long threads can edit and fork #6782 is replaced, becausevi.mockdoesn't reach the worker.HOST_DAEMON_PROTOCOL_VERSIONbump.How you verified
New tests in
session-fork.test.ts:FATAL ERROR: Reached heap limit … JavaScript heap out of memorySession <id> not foundpnpm 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:worktreeserver with thebbCLI 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.Provider "claude-code" exited unexpectedly (signal SIGABRT)plus a V8 stack traceClaude session … is too large to fork: forking it ran out of memoryafter about 13 sthread.start failedwith the SIGABRT stack tracethread.start failedwith "too large to fork"1 interrupted, SIGABRT)doneRuntime and platform checks:
ERR_WORKER_OUT_OF_MEMORY; the main thread stays alive with its data intactFATAL ERROR: Reached heap limit)Refs #6781
🤖 Generated with Claude Code