Skip to content

Stream Claude fork transcripts so long threads can edit and fork - #6782

Merged
ymichael merged 2 commits into
get-bb:mainfrom
Danielalnajjar:fix/claude-fork-large-transcripts
Nov 2, 2026
Merged

ymichael merged 2 commits into
get-bb:mainfrom
Danielalnajjar:fix/claude-fork-large-transcripts

Conversation

@Danielalnajjar

@Danielalnajjar Danielalnajjar commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

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 Claude Code fork fails with "Session … not found" when CLAUDE_CONFIG_DIR comes from project environment variables #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 Forking a thread whose source has already been compacted causes the fork to immediately re-compact (claude-code) #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 Claude Code fork fails with "Session … not found" when CLAUDE_CONFIG_DIR comes from project environment variables #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

The fork session store read the whole transcript into one string and
wrote the fork as one joined string. Both fail above Node's 512 MiB
string limit, and the read error surfaced as "Session <id> not found".
Read the transcript line by line and write the fork one entry at a
time, keeping the store, its config-dir lookup and its exclusive
create.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-01T07:07:17.751669Z 7b33e8c PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

The test mocked only readFile, so restoring the joined-string writer
still passed. Also reject writeFile, and fork both through a checkpoint
and the whole session.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit 9dec2ae into get-bb:main Nov 2, 2026
30 checks passed
ymichael added a commit that referenced this pull request Nov 2, 2026
…her threads (#6855)

## 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 #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](https://claude.com/claude-code)

> 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

2 participants