Skip to content

--fork-session forks re-trace the parent's turns and model calls as their own #312

Done

Description

Problem

claude --resume <id> --fork-session writes a new transcript that starts with copies of
the parent's records, stamped with the fork's own sessionId. Both tracing paths count
those copies as the fork's own work:

  • bt trace import claude <fork-id> imports every copied turn, model call and tool call
    as the fork's.
  • Live tracing (bt trace run claude) puts the parent's copied model calls inside the
    fork's first turn.

So the parent's calls and tokens are counted twice, once in each trace. #301 fixed this
for desktop-app forks, whose copies keep the parent's sessionId, but not for this
format.

Impact

Every CLI fork hits this, and so do desktop forks that write the fork's id on their
copies. On one engineer's machine, 1,907 model calls (about 487 M tokens) appear as
their own records in two or more session files, across 52 sessions. A fork of a long
session re-counts all of it.

Steps to reproduce

In any directory:

claude -p --model haiku --output-format json "Reply with the single word: apple"          # note its session_id
claude -p --model haiku --resume <that-session-id> --fork-session --output-format json "Reply with the single word: banana"
bt trace import claude <fork-session-id>
  • On disk: the fork's transcript holds 4 user and assistant records, all with the
    fork's sessionId. The first two are the parent's "apple" prompt and reply, with the
    parent's uuids.
  • Today: the fork's trace has Turn 1 "Reply with the single word: apple", with the
    parent's model call (50,787 tokens), and Turn 2 "banana". The parent's trace has the
    same model call.
  • Expected: one turn, "banana", with the parent's conversation only as context for
    its model call.

Live, the same happens:

bt trace run --project my-coding-agent claude -- -p --model haiku --resume <that-session-id> --fork-session --output-format json "Reply with the single word: cherry"

Today, the fork's Turn 1 "cherry" has 2 LLM spans: the parent's "apple" call and its own.
It should have 1.

We ran both with bt 0.33.0 into a Braintrust project, and read the stored traces back
with bt sql. A debug build of bt-daemon at dc5f7ea gives the same result.

Root cause

The import decides which records are inherited by finding the first record whose
sessionId matches the file name
(transcript_import/claude.rs:158-179).
In a --fork-session fork every record has the fork's id, so the inherited prefix is
empty, and the _bt_import_inherited_history path #301 added never runs. Live tracing
reads the fork's transcript from its start and emits every model call it hasn't seen into
the open turn.

Possible fixes

  • Find the copies by their uuids. The copied records keep the parent's uuids.
    When the parent's transcript is on disk, the leading records whose uuids it holds are
    the inherited prefix. That feeds the existing _bt_import_inherited_history path. Limit:
    it needs the parent's transcript.
  • Use the order Claude Code writes the file in. In our runs, the fork's first
    queue-operation record comes before the copied records but has a later timestamp than
    all of them. Records timestamped before the fork process started are inherited. Limit:
    it depends on a write order that isn't documented.
  • Live: treat what's already in the transcript when the fork's SessionStart arrives
    as history. We haven't checked whether the copies are on disk by then.

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions