Repository navigation
fix(claude): fork from a reply on transcripts that have no apiBlockIndex - #48153
Conversation
|
No actionable comments were generated in the recent review. Walkthrough
Severity of issue fixed: Medium Merge Risk:
|
2c182e3 to
3694fa0
Compare
Claude Code writes one transcript row per content block, but only newer builds record apiBlockIndex on each row. Fork-through-message matched the streamed block id (block:<messageId>:<index>) only against rows that carried apiBlockIndex, so forking from a reply failed with "could not find the saved context" on transcripts from older Claude Code builds. Fall back to the row's position among rows sharing the same message id when apiBlockIndex is absent. Fixes stablyai#48148 Co-authored-by: Common-Player <[email protected]> Co-authored-by: 2SinceJuly <[email protected]> Co-authored-by: Aditya Chandrajit Deore <[email protected]>
3694fa0 to
35b408b
Compare
ELI5
When you fork a Claude chat from a specific reply, Orca has to find that reply in Claude's saved history file. Older Claude Code builds (for example 2.0.42) save each piece of a reply on its own line, but they don't number the pieces. Orca only looked for numbered pieces, so it couldn't find the reply and showed "Claude could not find the saved context for this response." Now Orca numbers the pieces itself, in the order they appear, so it finds the right spot and the fork works.
What Changed
claudeConversationForkBoundary(src/main/claude/transcripts/claude-conversation-fork-boundary.ts) matches the streamed block idblock:<messageId>:<index>against transcript rows. It used to match only rows that had a numericapiBlockIndex. Now, when a row has noapiBlockIndex, the index comes from the row's position among the chain rows with the samemessage.id. Rows that do haveapiBlockIndexare handled exactly as before. Rows without amessage.id(for example usertool_resultrows between tool calls) are not counted.claude-conversation-fork-boundary.test.ts: runs the same transcript with and withoutapiBlockIndex. The transcript has thinking, twotool_useblocks with atool_resultrow between them, and a second reply. The test checks that each block id resolves to the right row and that out-of-range ids still throw.Why
Claude Code writes one transcript row per content block, in block order. Newer builds add
apiBlockIndexto each row, but older builds don't. Counting a row's position among the rows of the same message gives the same index those builds would have written, so we don't need a second id scheme or any change to the transcript reader.Out of scope:
claude-transcript-item-reader.tsandclaude-transcript-import-plan.tsalso build the block id only fromapiBlockIndex. Counting positions there isn't safe, because catch-up reads a tail window that may not include a message's earlier rows. That needs a separate fix.Linked Issue
Fixes #48148
Reported by @Common-Player. Original fix by @Common-Player in #48153; final version reworked during the bug bash. Thanks also to @2SinceJuly (#48156) and @AdityaxDeore (#48155) for their fixes. All three are co-authors on the commit.
Visual Proof
N/A. The fix changes how a main-process worker looks up a transcript row. The only visible effect is that an error toast no longer appears on older transcripts, and the before/after unit runs below show that directly.
Testing
Repro: on
main, the new test case withoutapiBlockIndexfails withClaude could not find the saved context for this response.With this change, both cases pass.pnpm exec vitest run src/main/claude/transcripts/: 2/2 pass.oxlint,oxfmt --check, the anti-slop oxlint, native code-quality oxlint (--deny-warnings), ast-grep (warning severity) andcheck-changed-code-quality.mjsare clean on the touched files.Platforms: macOS (unit tests). The logic is plain in-memory matching with no paths or platform APIs.
I manually tested these changes locally
Automated tests added/updated, or explained why not below
Every added test fails without this change and avoids the test anti-patterns
Performance
This only runs in the fork worker thread (
claude-conversation-fork-worker-entry.ts), once per fork click, after the JSONL has been read and parsed. Nothing changes on the main or renderer thread, and there is no new I/O, subprocess work or polling. The only addition is one Map get and set per chain row that has amessage.id.Mean ms per call, old vs new, on synthetic chains with 10 blocks per reply (vitest, M-series, machine in use, so the numbers are noisy):
apiBlockIndex, early / lateapiBlockIndex, early / lateapiBlockIndex, early / late / missingapiBlockIndex, early / late / missingapiBlockIndex, early / late / missingapiBlockIndex, early / late / missingThe worst case adds about 2 ms at 99k rows, off the UI thread. That is small next to the JSONL read and parse that runs just before it.
Risk
Level: Low
The fallback only runs for rows that have no
apiBlockIndex, and those rows always failed to match before. If a transcript somehow wrote a message's blocks out of order, the fork would cut at the wrong block of the same reply instead of showing an error. To back this out, revert the commit. No saved data or settings change.Agent skill upstream boundary
Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred)