claude: an interactive run's calls keep the conversation they answered when their line comes late

kay20475 on X (magpie 0.1.3005, local magpie -> shared server magpie ->
Claude subscription, interactive bridge): a Claude Code sub-agent's
requests on one account read 99%, 6%, 6%, 99% from the cache; the two 6%
requests were 169.1k each, 35s apart, about 3s long with a short reply.
That is the shape of Claude Code's AgentSummary: every ~30s it sends the
sub-agent's conversation with its last results and "Describe your most
recent action in 3-5 words", while the sub-agent's own run has already
taken those results.

Cause. Such a request finds no run waiting on its calls and should go on
in a branch of the sub-agent's run's session (branch). branch takes it
only when the conversation stored on the call's line (callLine.told)
matches the one before the reply. park stored that only on lines it
already had. The interactive mode streams a reply first and reads its
session lines from the file afterwards (118-162ms after the call). So the
run parked before noteLines had the call's line, and the line came in
with no conversation. The next park then gave it the next request's
conversation. branch returned nil, and every AgentSummary (and every
fork directive) was told the whole conversation in a new Claude Code,
pasted as one text block. A single block can't match the cache beyond
the tools and system prompt, so 6%, and the next summary missed the same
way because its tail differs.

Fix. park also keeps the conversation for each call the client was shown
(r.shown), as a line with no uuid yet. noteLines keeps it when the line
comes, and branch skips lines with no uuid.

Test: TestClaudeForkBranchesOffTheLeadsSession/interactive now has the
fake write the call's session line after the reply's stream, once the
result is handed over, so after the park, which is the real order.
Broken by hand: with this test and the old claude_subscription.go on
mote, it fails ("told it whole", want "branched at line-...").

Run on mote at this tree (base 4cb7159f1): gofmt -l clean; go vet
./internal/gateway on darwin, GOOS=linux and GOOS=windows ok; go test
-timeout 45m ./internal/gateway (679s) ./internal/claudetui
./internal/claudebridge all ok. The fork/side-question/cut tests
-count=3 ok. The full ./... suite and the GUI suite were not run (no
GUI or other-package change).
Rebased on 351844594 (GUI only since 4cb7159f1): go test -count=1 -run
'Claude|Fork|Branch|Subscription' ./internal/gateway ok (77s) on mote.

Not tried: a real Claude Code AgentSummary against a real account. The
reporter's requests are matched by shape only (size, timing, short
reply). Ruled out: the remote-magpie hop. The bridge doesn't key runs on
headers (only anthropic-beta for safeguards), so losing
x-claude-code-session-id can't move a turn off its run. Not fixed here: a
sub-agent loop with no plain reply, told anew in the interactive mode, is
still pasted as one text, so a new run for it reads only tools+system.
This commit is contained in:
yetone
2026-11-05 14:05:54 +08:00
parent 3518445947
commit 2947685999
3 changed files with 51 additions and 5 deletions
+12 -1
View File
@@ -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
+29 -2
View File
@@ -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 {
@@ -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
) &