Repository navigation
Description
Summary
Forking a thread on the claude-code provider whose source thread has already undergone context compaction causes the new forked thread to immediately trigger its own compaction on its very first turn, even when the source thread's actual token usage was low (~15-20% of the context window) at the moment of the fork. Expected: a fork should start with the source's already-compacted context; actual: it reloads the full, pre-compaction history and re-compacts right away.
Versions and environment
- bb 0.42.1, run from source at commit
dba32a469fd820ff6106715db0aaf6ed297d79a5 - Linux
- Provider: claude-code, CLI
claude --version2.1.260 (Claude Code), vendored@anthropic-ai/claude-agent-sdk^0.3.245 - Nothing unusual about the setup (standard worktree environment)
Steps to reproduce
- Have (or create) a
claude-codethread that is long-running enough to have gone through at least one auto-compaction (thread/compactedevent visible viabb thread log <id> --json --all). - Fork it with a trivial prompt:
bb thread fork <source-thread-id> --prompt "This is a test fork. Do nothing, just reply 'ok'." - Wait for the fork to go idle:
bb thread wait <new-thread-id> --status idle - Inspect the new thread's event log:
bb thread log <new-thread-id> --json --all
Reproduced directly: forked a long-running coordinator-style thread (4 prior compactions over its life) with a minimal prompt. Reproduced on the first attempt.
Expected vs actual
Expected: the forked thread starts from the source's current (already-compacted) context and does not immediately re-compact.
Actual: immediately after the fork's own client/thread/start event, before any real reply, the event log shows:
turn/started
turn/input/accepted
item/started {"item": {"type": "contextCompaction", ...}} x5
thread/compacted
item/started {"item": {"type": "agentMessage", "text": "ok"}}
Five separate contextCompaction items fire back-to-back and a new thread/compacted event is recorded, all before the trivial "ok" reply. The thread/tokenUsage/updated event recorded immediately afterward shows usedTokens: 164914 out of modelContextWindow: 1000000 (~16%) — well under any reasonable compaction threshold, which rules out "the source was simply near its limit" as an explanation. The source thread's own usage right before the fork was similarly low: usedTokens: 198499 / 1000000 (~20%), recorded via thread/contextWindowUsage/updated in its event log at the last turn before the fork.
The same behavior was also observed when forking into a side-chat off the same source thread, not only with a full thread fork.
Evidence
- Fork/checkpoint resolution:
resolveThreadForkPointandresolveCheckpointForkDescriptor— this always resolves a checkpoint at or after the source's last completed turn, i.e. after any compaction. This part is not the problem. - Provider-level fork call:
handleThreadFork, specifically the call to the SDK'sforkSession(sourceProviderThreadId, { upToMessageId })at line 2493 andstartAttachedResidentThreadSession(...)at line 2530. - Root cause traced into the vendored
@anthropic-ai/[email protected](minifiedsdk.mjs, not part of this repo):forkSession's implementation copies the full transcript into a new session file and assigns a new UUID to every message (s.set(oldUuid, newUuid)), but does this via a shallow object spread that leavescompactMetadataon any copiedcompact_boundarysystem message pointing at the old session's UUIDs (anchorUuid,headUuid,tailUuid,preservedMessages.uuids[]). The SDK's own context-reconstruction routine skips acompact_boundaryif any of its referenced UUIDs are not found in the transcript being read — which is guaranteed after a fork, since all UUIDs were just remapped. The forked session therefore reconstructs its context by walking the raw, unsuppressed parent chain all the way back through history that was already compacted in the source, immediately exceeding the threshold and forcing a fresh compaction. Every real copied message in the new transcript does carry aforkedFrom.messageUuidfield holding its exact old UUID, so the old→new UUID map is fully reconstructible after the fact purely from the forked session's own JSONL file.
Suggested fix
Not implemented — this is a report of the diagnosis only. Two possible directions, from most to least targeted:
- bb-side post-processing workaround: after
forkSessionreturns (plugins/provider-claude-code/src/bridge/bridge.tsaround line 2498), before the resident session starts (line 2530), read the newly written forked.jsonltranscript file, build an old→new UUID map from each entry'sforkedFrom.messageUuidfield, and rewrite the UUID references inside everycompact_boundaryentry'scompactMetadatato point at the new UUIDs. This is additive, isolated tocompact_boundarylines, and can be made best-effort/no-op if the expected fields aren't present (e.g. on a future SDK release that changes the on-disk format). - Upstream fix in
@anthropic-ai/claude-agent-sdk:forkSessionshould remap the UUIDs it references insidecompactMetadatausing the same map it already builds foruuid/parentUuid/logicalParentUuid, since it demonstrably has that map in scope at the point it emits each copied entry.
What was ruled out
- Not a symptom of the source thread being near its context-window threshold: measured usage was ~16-20% of a 1,000,000-token window at the time of both the fork and the immediate re-compaction.
- Not caused by bb selecting a stale/pre-compaction checkpoint:
resolveThreadForkPoint/resolveCheckpointForkDescriptorresolve to the last completed turn, which is at or after the source's last compaction. - Searched open and closed issues in this repo for "fork" and "compaction"/"compact" — no existing issue describes this exact mechanism. Closest are unrelated symptoms (
#1939"Autocompact is thrashing (Claude)" is a different trigger — repeated compaction during normal growth, not on fork). - Checked
anthropics/claude-codeGitHub issues for related upstream reports: #48937 (forking a compacted session opens blank/invisible tolistSessions) and #48270 (resume picks a stale branch on a forked/branching JSONL, closed as stale) both touch the same general area — compaction-boundary/session-tree bookkeeping breaking under fork/resume — but neither describes the specific UUID-remap-vs-compactMetadatamismatch found here.
Suggested priority and effort
Medium — hits any user who forks a long-running/previously-compacted thread (a common pattern for parallelizing work off a coordinator thread), causes a confusing delay and wasted tokens on every such fork, but has a functional workaround (do nothing; the fork still works, just recompacts). Effort for the bb-side workaround looks Low-Medium (isolated, additive file patch); effort for an upstream SDK fix is unknown since it is outside this repo.
AGENT GENERATED
Activity
bb-slop-cop commentedon Sep 6, 2026
🚨 SLOP COP 🚨 ·
new-issue-autopilotClassified as Bug · Priority: Medium · Effort: Low · labels:
providers,provider-claude-code,confirmed-repro.Report: https://get-bb.github.io/reports/issues/3185.html
confirmed-repro— the focused UUID-integrity regression failed in two clean checkouts at the trusted base.Stream Claude fork transcripts so long threads can edit and fork (get…