diff --git a/docs/subsystems/claude-subscription-bridge.md b/docs/subsystems/claude-subscription-bridge.md index 1dc98997b..11c3a7c81 100644 --- a/docs/subsystems/claude-subscription-bridge.md +++ b/docs/subsystems/claude-subscription-bridge.md @@ -1300,7 +1300,18 @@ Each cost about a fifth of a Pro plan's five hours. The run that made the calls instead keeps where in its session each call of the client's tools was written: the uuid on Claude Code's `assistant` line (`noteLines`). As it parks on them, it also keeps the conversation those calls answered -(`historyKey`, set in `park`). A request no run takes, whose last reply's +(`historyKey`, set in `park`), by the ids of the calls the client was +shown (`shown`): the interactive mode tells a reply's session lines after +its stream, read from the file, so the run parks before it has them, and +`noteLines` keeps that conversation when the line comes. Before 11-05 only +lines already heard were given it, and a late one took the next park's +conversation, so an interactive run's branch missed in the usual order: +a fork's directive, or (as kay20475 on X saw it, by its shape) Claude +Code's AgentSummary for a sub-agent, carrying the results the sub-agent's +own next turn had taken, was told the whole conversation in a new Claude +Code each time, pasted as one text, and read only the tools and system +prompt from the cache (6% of 169k, twice 35s apart). A +request no run takes, whose last reply's calls a live run of the same owner made, from the same conversation, goes on in a branch of that run's session. The branch's run copies the session's lines up to the reply's uuid into a session file of its own diff --git a/internal/gateway/claude_subscription.go b/internal/gateway/claude_subscription.go index c6bf3df99..511adf7b7 100644 --- a/internal/gateway/claude_subscription.go +++ b/internal/gateway/claude_subscription.go @@ -2505,7 +2505,9 @@ func (r *subscriptionRun) noteLines(uuid, model, msg string, ids []string) { } r.lineSeq++ for _, id := range ids { - r.lines[id] = callLine{uuid: uuid, seq: r.lineSeq} + // the conversation its reply answered, kept as the run parked on + // the call before its line was read (park) + r.lines[id] = callLine{uuid: uuid, seq: r.lineSeq, told: r.lines[id].told} } if len(r.lines) > linesMost { for id, l := range r.lines { @@ -2577,7 +2579,7 @@ func (b *subscriptionBridge) branch(req *Request, owner string) *savedSession { run.mu.Lock() at, seq, said := "", 0, "" for _, id := range calls { - if l, ok := run.lines[id]; ok && l.seq > seq { + if l, ok := run.lines[id]; ok && l.uuid != "" && l.seq > seq { at, seq, said = l.uuid, l.seq, l.told } } @@ -3080,6 +3082,31 @@ func (r *subscriptionRun) park() { r.lines[id] = l } } + // The interactive mode streams a reply first and its session lines + // come after, read from the file Claude Code writes (118 to 162ms + // after the call), so the run parks before it has the line of a call + // it made. The calls the client was shown are this reply's: they + // answer the conversation heard now, whenever their line comes + // (noteLines keeps it). Before, the line came with no conversation + // and the next park gave it the next request's, so no request that + // answered those calls after another did matched it (branch): Claude + // Code's AgentSummary, asking its sub-agent's progress with the + // results the sub-agent's own next turn had taken, and a fork's + // directive were told the whole conversation by a new Claude Code, + // pasted as one text, and read only the tools and system prompt from + // the cache, 6% of 169k twice in 35s (kay20475 on X). + if r.lines == nil { + r.lines = map[string]callLine{} + } + for _, c := range r.shown { + if l := r.lines[c.id]; l.told == "" { + if l.uuid == "" { + l.seq = r.lineSeq // aged as a line noted now (linesMost) + } + l.told = r.told + r.lines[c.id] = l + } + } r.parkedSeq = r.lineSeq r.mu.Unlock() switch { diff --git a/internal/gateway/claude_subscription_fork_test.go b/internal/gateway/claude_subscription_fork_test.go index 0ced0fdb4..9662abe51 100644 --- a/internal/gateway/claude_subscription_fork_test.go +++ b/internal/gateway/claude_subscription_fork_test.go @@ -250,7 +250,10 @@ func forkBranches(t *testing.T, mode string) { // written after it. Resumed, it goes on from the session's last line, // or as -p from the one --resume-session-at names, and says which, and // whether every line is that session's; the interactive mode takes no - // --resume-session-at (Claude Code 2.1.316). + // --resume-session-at (Claude Code 2.1.316). The interactive mode's + // line of a call comes after the reply's stream, once the run has + // parked on it, as the real one's is read from the file (kay20475 on + // X: told before the park here, the branch was never missed). dir, logs := t.TempDir(), t.TempDir() testenv.Program(t, filepath.Join(dir, "claude"), `#!/bin/sh if [ "$1" = auth ]; then echo '{"loggedIn":true,"authMethod":"claude.ai","apiProvider":"firstParty","email":"lead@example.com","subscriptionType":"max"}'; exit 0; fi @@ -301,12 +304,17 @@ while read -r line; do echo '{"type":"stream_event","event":{"type":"content_block_stop","index":0}}' echo '{"type":"user","uuid":"ask-'$$'","sessionId":"'$sid'"}' >> "$file" echo '{"type":"assistant","uuid":"line-'$$'","sessionId":"'$sid'","message":{"id":"m'$$'","model":"claude-opus-5-5"}}' >> "$file" - echo '{"type":"assistant","uuid":"line-'$$'","message":{"id":"m'$$'","model":"claude-opus-5-5","content":[{"type":"tool_use","id":"toolu_L'$$'","name":"mcp__magpie__Agent","input":{"part":1}}]}}' + row='{"type":"assistant","uuid":"line-'$$'","message":{"id":"m'$$'","model":"claude-opus-5-5","content":[{"type":"tool_use","id":"toolu_L'$$'","name":"mcp__magpie__Agent","input":{"part":1}}]}}' + [ -n "$FAKE_TUI" ] || echo "$row" echo '{"type":"stream_event","event":{"type":"message_delta","delta":{"stop_reason":"tool_use"},"usage":{"output_tokens":3}}}' echo '{"type":"stream_event","event":{"type":"message_stop"}}' ( call='{"tool_call_id":"toolu_L'$$'","name":"Agent","arguments":{"part":1}}' got=$(curl --noproxy '*' -s -X POST -H 'Content-Type: application/json' --data "$call" "$cb") + # the interactive mode tells a reply's session line after its stream, + # read from the file, so the run has parked on the call by then: here + # once its result is handed over, after the park for certain + [ -z "$FAKE_TUI" ] || echo "$row" echo '{"type":"user","uuid":"result-'$$'","sessionId":"'$sid'"}' >> "$file" case "$got" in *"agent launched"*) say "heard:agent launched" ;; *) say "heard:something else" ;; esac ) &