Skip to content

Fork Claude sessions from the session's config dir and keep fork intent across failed starts - #6358

Merged
ymichael merged 1 commit into
mainfrom
fix-claude-fork-project-config-dir
Oct 27, 2026
Merged

ymichael merged 1 commit into
mainfrom
fix-claude-fork-project-config-dir

Conversation

@ymichael

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Forking a Claude Code thread failed with Session <id> not found when CLAUDE_CONFIG_DIR was set only as a project environment variable (#6325). The bridge forks with the Agent SDK's in-process forkSession, which looks up transcripts in the bridge process's own CLAUDE_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:

  • Retry: ready starts always sent fork: null, so bb thread retry sent a thread.start with no input and no fork, and the daemon rejected it (Invalid host RPC command).
  • Send: a later message started a new empty session.
  • Fork details erased early: the saved fork details (startupContext) were deleted as soon as a ready start set the thread to active, 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: handleThreadFork works out the session's config dir from the session env and passes forkSession a sessionStore bound to it. The store reads <configDir>/projects/*/<id>.jsonl and writes the fork next to the source. Checkpoint forks (upToMessageId) behave as before. Scoping process.env around 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: prepareReadyThreadTurnCommand now uses the saved fork for thread.start while the thread has no provider session of its own. This covers retry, send and parent system messages.
  • packages/db/src/data/threads.ts (lifecycle): startupContext is now cleared only when the thread settles idle with an environment, no longer on any move to active. The other readers only look at it before the thread has an environment or while it is still starting.
  • Wire and protocol: no changes, so no HOST_DAEMON_PROTOCOL_VERSION bump. thread.start already accepts a fork with empty input.

How you verified

  • Bridge test (bridge.test.ts): runs the real SDK forkSession with the transcript only in a project-configured CLAUDE_CONFIG_DIR and an empty process config dir. It fails on main with Session <id> not found and passes here, including the checkpoint cut-off.
  • Server test (public-thread-fork.test.ts): a fork start fails, then is retried. The retried thread.start carries the fork with empty input and passes hostDaemonCommandSchema. It fails again, then a message is sent, and that start still carries the fork. Fails on main, passes here.
  • Checks: pnpm exec turbo run typecheck lint --filter=@bb/server --filter=@bb/db --filter=bb-plugin-provider-claude-code passes after rebasing on main. Plugin tests (406) and @bb/db tests (633) pass. The @bb/server suite passes apart from 2 load flakes in builtin-plugins.test.ts, which passes 28/28 alone.
  • Manual (pnpm start:worktree): project with CLAUDE_CONFIG_DIR=/tmp/... as a project env var. The source thread's transcript existed only in that dir.
    • A CLI fork recalled a codeword from the source thread.
    • Forks made to fail by hiding the transcript recovered after the transcript was restored. bb thread retry, and also sending a message, each produced a session that recalled the codeword.

Fixes #6325

🤖 Generated with Claude Code

AGENT GENERATED

…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]>
@ymichael
ymichael merged commit 61b2fe7 into main Oct 27, 2026
34 checks passed
@ymichael
ymichael deleted the fix-claude-fork-project-config-dir branch October 27, 2026 04:04
mistakeknot added a commit to mistakeknot/aleph that referenced this pull request Oct 27, 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]>
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.

Claude Code fork fails with "Session … not found" when CLAUDE_CONFIG_DIR comes from project environment variables

1 participant