Repository navigation
Fork Claude sessions from the session's config dir and keep fork intent across failed starts - #6358
Merged
Merged
Conversation
…nt across failed starts The Claude bridge forked sessions with the Agent SDK's in-process forkSession, which resolves transcripts from the bridge process's CLAUDE_CONFIG_DIR. A CLAUDE_CONFIG_DIR set only as a project environment variable never reached that lookup, so forks failed with "Session <id> not found". The bridge now passes forkSession a session store bound to the config directory of the session's own environment. A failed fork start could not be retried, and a later message started an empty session: ready starts always sent fork: null, and the startup context holding the fork descriptor was cleared as soon as a ready start moved the thread to active. Ready thread starts now carry the dispatched startup fork, and the startup context is kept until the thread settles idle with an environment. Fixes #6325 Co-Authored-By: Claude Opus 5.5 <[email protected]>
mistakeknot
added a commit
to mistakeknot/aleph
that referenced
this pull request
Oct 27, 2026
Backport five upstream bb fixes (get-bb#6247, get-bb#6255, get-bb#6243, get-bb#6334, get-bb#6358)
This was referenced Nov 1, 2026
ymichael
pushed a commit
that referenced
this pull request
Nov 2, 2026
## Human comments ## What was wrong Edit message and Fork into new thread fail with `Session <id> not found` in Claude Code threads whose transcript is over 512 MiB (#6781). Since #6358, `forkClaudeSession` passes the SDK a session store bound to the session's config dir, which keeps #6325 fixed. That store reads the transcript with `readFile(path, "utf8")` and writes the fork by joining every entry into one string. Both build a transcript-sized string, which Node cannot do above 536,870,888 characters. The read's `RangeError` was then caught by the directory scan and reported as "not found". ## What changed - `plugins/provider-claude-code/src/bridge/session-fork.ts`: the store reads the transcript one line at a time (`FileHandle.readLines`, 1 MiB chunks) and writes the fork one line per entry through `stream/promises` `pipeline`, still with `flags: "wx"` and mode `0o600`. The store design, the config-dir lookup and the fork placement are unchanged, so #6325 stays fixed. - The parsed entries are still held in memory, because the SDK store interface returns them as an array. The next ceiling is the bridge process heap (V8 default, about 4 GB), which these runs put near a 1.8 GB transcript; that figure is extrapolated, not measured. - A transcript that opens but fails while being read now reports its own error instead of "not found". A missing file in a project directory is still skipped, and so is a file with no valid entries. - No wire, protocol or SDK change, and no `HOST_DAEMON_PROTOCOL_VERSION` bump. - I kept the store rather than switching to the SDK's own file fork (as T3 Code does). That path reads the whole file into one Buffer, caps at 2 GiB, and reports that cap as "not found" too. On the same runtime it peaked at 3.55 GB RSS for a 1.26 GB transcript, against 2.84 GB here. It would also need a worker with its own env to see a project-level `CLAUDE_CONFIG_DIR`, which is the machinery the store already avoids. - Raising the bundled SDK past 0.3.284 fixes #3185 separately. I'll send that as its own PR. ## How you verified - New `src/bridge/session-fork.test.ts`: `readFile` and `writeFile` reject with `RangeError: Invalid string length`, which is what Node throws above the limit, so the test fails if the store reads the transcript or writes the fork as one string. It runs a checkpoint fork and a full fork through the real SDK `forkSession`. On main it fails with `Session <id> not found`, and with only the old joined-string writer restored it fails with `RangeError: Invalid string length`; here it passes. V8's limit cannot be lowered in a test, so the real-size check is the manual run below. - The existing #6325 test in `__tests__/bridge.test.ts` ("forks a Claude session from the session config directory through the requested checkpoint") still passes. - `pnpm exec turbo run typecheck lint test --filter=bb-plugin-provider-claude-code`: 10 of 10 tasks successful; 29 test files, 415 tests passed. `oxfmt --check` on both files, `git diff --check`, `scripts/check-source-nul.mjs` and `scripts/check-provider-literal-ratchet.mjs` pass. - Manual, synthetic transcripts only, with the locked SDK 0.3.245, run with the bb app's own Node (`ELECTRON_RUN_AS_NODE=1`, Electron 44.9.3, Node 24.22.0) and the script from the issue: - 537,025,136 characters: checkpoint and full forks fail on main with "not found"; both succeed here. - 545,416,156 characters: both forks succeed, 1.31 GB peak RSS (SDK file fork on the same runtime: 2.44 GB). - 1,258,653,056 characters: both forks succeed, 2.84 GB peak RSS (SDK file fork: 3.55 GB). - Peak RSS is `/usr/bin/time -l` for the whole script, single runs on a loaded machine. Fixes #6781 > AGENT GENERATED --------- 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
Forking a Claude Code thread failed with
Session <id> not foundwhenCLAUDE_CONFIG_DIRwas set only as a project environment variable (#6325). The bridge forks with the Agent SDK's in-processforkSession, which looks up transcripts in the bridge process's ownCLAUDE_CONFIG_DIR(default~/.claude). Project env vars reach only the session env, so the transcript, which lives in the project's config dir, was never found. A failed fork start then could not recover:fork: null, sobb thread retrysent athread.startwith no input and no fork, and the daemon rejected it (Invalid host RPC command).startupContext) were deleted as soon as a ready start set the thread toactive, so even a correct retry would lose them after a second failure.What changed
plugins/provider-claude-code/src/bridge/session-fork.ts(new),bridge.ts:handleThreadForkworks out the session's config dir from the session env and passesforkSessionasessionStorebound to it. The store reads<configDir>/projects/*/<id>.jsonland writes the fork next to the source. Checkpoint forks (upToMessageId) behave as before. Scopingprocess.envaround the call was rejected because the SDK reads it after awaits, which would race between concurrent forks.thread-lifecycle.ts,thread-startup-store.ts,thread-send.ts,parent-system-messages.ts:prepareReadyThreadTurnCommandnow uses the saved fork forthread.startwhile the thread has no provider session of its own. This covers retry, send and parent system messages.packages/db/src/data/threads.ts(lifecycle):startupContextis now cleared only when the thread settlesidlewith an environment, no longer on any move toactive. The other readers only look at it before the thread has an environment or while it is still starting.HOST_DAEMON_PROTOCOL_VERSIONbump.thread.startalready accepts a fork with empty input.How you verified
bridge.test.ts): runs the real SDKforkSessionwith the transcript only in a project-configuredCLAUDE_CONFIG_DIRand an empty process config dir. It fails on main withSession <id> not foundand passes here, including the checkpoint cut-off.public-thread-fork.test.ts): a fork start fails, then is retried. The retriedthread.startcarries the fork with empty input and passeshostDaemonCommandSchema. It fails again, then a message is sent, and that start still carries the fork. Fails on main, passes here.pnpm exec turbo run typecheck lint --filter=@bb/server --filter=@bb/db --filter=bb-plugin-provider-claude-codepasses after rebasing on main. Plugin tests (406) and@bb/dbtests (633) pass. The@bb/serversuite passes apart from 2 load flakes inbuiltin-plugins.test.ts, which passes 28/28 alone.pnpm start:worktree): project withCLAUDE_CONFIG_DIR=/tmp/...as a project env var. The source thread's transcript existed only in that dir.bb thread retry, and also sending a message, each produced a session that recalled the codeword.Fixes #6325
🤖 Generated with Claude Code