Files
magpie/internal/claudetui
yetone 8215164296 claude: a branch is a cut copy of its run's session, in its run's mode (bbfatx on X)
A sub-agent's first request that answers calls another request already
answered branches off the lead's run's session (branch). That branch was
`claude -p --resume S --fork-session --resume-session-at U`, and
claudePrint forced every branch to -p, because Claude Code's interactive
mode ignores --resume-session-at (Claude Code 2.1.316 on mote: it resumes
the whole session). Runs are the interactive mode by default now, so the
lead's cache has the interactive prompt, and a -p branch of it read only
tools and system from the cache and wrote the conversation again
(measured on mote, sandbox HOME, 30 MCP tools: an interactive fork of a
-p session read only tools and system; the other way round was not
measured). With "Interactive only" no branch was made at all,
and the fork was told the whole conversation.

Now the branch's run copies the session's lines up to U into a new
session file next to it, with sessionId set to the copy's id
(cutSession), and resumes it with --resume <copy> in the mode its source
run had (savedSession.tui). claudePrint keeps "branch" only for a branch
of a -p run. Under "Interactive only" a -p run's session isn't branched.
The run owns the copy, as it owns an unshelved session. The copy waits up
to 2s for U's line to be fully written; if it never is, the turn is told
whole, as before. claudetui's translator now passes each assistant row's
uuid through, as -p does, so an interactive run's lines can be cut.
Measured on mote with real Claude Code 2.1.316: a cut copy resumed
interactively read 7648 and wrote 14; with -p it read 24009 and wrote 13.

Run (mote-run, this tree):
- go test -tags nogui -count=1 -timeout 30m -run 'Claude|Bridge|ToolChoice|Subscription|Retire|Rewrit|Fork|Branch|Shelf|Shelve' ./internal/gateway: ok 63s
- go test -tags nogui -count=1 ./internal/claudetui: ok
- go vet -tags nogui ./internal/gateway ./internal/claudetui; gofmt -l: clean

Broken by hand (each red, then put back):
- old claude_subscription.go and transcript.go (cutSession stubbed):
  TestClaudeForkBranchesOffTheLeadsSession interactive (branch ran -p),
  interactive only (told it whole), print (old flags),
  TestClaudeCutSessionCopiesAsFarAsTheLine and
  TestTranslatorTellsTheRowsUUID red
- claudePrint's branch reason for every branch again: interactive and
  interactive only red
- the copy taking the whole file: all three subtests and the cut test red
- branch() refusing every run under Interactive only: interactive only red

Not tried: a real fork sub-agent through the gateway. The cache numbers
come from copying by hand with real Claude Code; the tests use a
stand-in. Windows and WSL keep no session on disk, so they never
branch, as before.

Not taken from the suggestion: the fork's own runtime and TypeScript
wrapper. The cut is done here at the one place a branch starts.
2026-11-01 16:33:07 +08:00
..