gateway: tool results for another conversation's rewrite of a run's history no longer end the run that made the calls (#2933)

A request whose tool calls are a parked Claude Code run's own, by their
ids, but whose earlier messages were rewritten since the run last heard
them, claimed the run and ended it: a fork sub-agent or a resumed session
of the same conversation answers the very calls the run waits on, so its
request ended a run whose own results were still to come. The client that
had not rewritten anything then found no run waiting, and its
conversation was told to a new Claude Code whole — hundreds of messages
written to the prompt cache again, at each round a rewrite touched.

The rewrite is still not answered from the run: its agent holds the
conversation from before, and would tell the client a context as large
as ever. But the request is answered by a run of its own, told the
conversation as the request has it, and the run that made the calls goes
on waiting for the results of the client that has not rewritten
anything, parkLongest more, as a run whose caller stopped does — the
results of a concurrent session's client come seconds behind the
rewrite's. The log line says where the results went instead of that the
run was let go.

RED: TestRunLetGoWhenTheClientRewroteTheConversation ended the run at
the rewritten request and found its own results answered by a third run;
TestRewrittenRunWaitsParkLongestForItsOwnResults found the run ended at
once. GREEN: the rewritten request is answered by a run of its own, the
run's own results go on in it, and past parkLongest with no results of
its own it is let go. Mutations: dropping released's timer reset leaves
the run waiting past its reap deadline; keeping the claim held sends the
run's own results to a third run.

Generated-by: GLM-5.3-Flash (ZCode)

Landed from ggbdpq's branch (patch posted on the issue; sha256/patch-id
checked against the issue) by yetone, rebased on origin/main 2026-11-03.
Run at the pushed tree, on mote (macOS): go test -tags nogui -count=1 of
internal/gateway with -run over this batch's tests and their families
(PlainSchema, Antigravity, AutoMode, WebP, ClaudeTUI, NativeRelay,
Namespace, Grok, Zed, Codex, ClinePass, rewrite) ok; -race -count=3 of
the new tests ok; GOOS=linux and GOOS=windows go vet of internal/gateway
and gofmt -l clean. The full gateway-and-importers run, started at an
earlier base, was still running at the push; its reds get issues.
Changed in review: released sets the run's end through endIn, so the
run's recorded end matches its timer.
Broken by hand: the product change reverted, both rewrite tests red;
restored, green (also -race -count=3).
Cost: a run whose own client rewrote the conversation (Pi compacting)
now waits parkLongest (5m) for results that never come, instead of
being ended at once. Not tried with real concurrent Claude Code forks.
This commit is contained in:
ggbdpq
2026-11-03 02:27:57 +08:00
committed by yetone
parent 3b961f2949
commit fb872a68e5
2 changed files with 172 additions and 15 deletions
+27 -7
View File
@@ -1537,6 +1537,19 @@ func (r *subscriptionRun) claimResume() bool {
return true
}
// released gives up a claimed handoff whose request turned out to carry
// another conversation's rewrite of the run's: the run goes on waiting for
// the results of the client that has not rewritten anything, parkLongest
// more, as a run whose caller stopped does (#2933).
func (r *subscriptionRun) released() {
r.mu.Lock()
r.resuming = false
r.mu.Unlock()
if r.timer != nil {
r.endIn(parkLongest)
}
}
// resume hands a conversation's next turn to the run that had its last
// one, when one is waiting: the user's messages since, and nothing else.
func (b *subscriptionBridge) resume(req *Request, owner string) (*subscriptionRun, <-chan Event) {
@@ -5772,14 +5785,21 @@ func (s *Server) serveSubscription(w http.ResponseWriter, r *http.Request, from
run.abort()
run, how = nil, accountChanged
}
// the client rewrote the conversation since the run's last reply, as Pi
// does compacting it mid-turn: the run's agent holds the one from before,
// would answer from it, and tell the client a context as large as ever,
// so the client compacts again at each tool call. It is let go, and a
// run started anew is told the conversation as the client now has it.
// the request's conversation was rewritten since the run's last reply —
// the client's own, as Pi does compacting it mid-turn, or another
// session's: Claude Code's fork sub-agents and resumed sessions answer
// the very calls the run waits on, by their ids, and theirs is the
// history this request carries. The run's agent holds the conversation
// from before, would answer from it, and tell the client a context as
// large as ever, so the client compacted again at each tool call: these
// results go on in a run of their own, told the conversation as the
// request has it. The run that made the calls is not ended for them
// (#2933): the results of the client that has not rewritten anything are
// still to come, and it goes on waiting for them, parkLongest more, as a
// run whose caller stopped does.
if run != nil && !run.follows(req.Messages) {
log.Printf("%s run on %s let go: earlier messages changed while it waited for tool results", name, model)
run.abort()
log.Printf("%s on %s: tool results go on in a run of their own: earlier messages changed while the run that made the calls waits for them", name, model)
run.released()
run, how = nil, ""
}
// the client offers a tool the run's agent was never told of, as Claude
@@ -7,6 +7,7 @@ import (
"strconv"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
@@ -14,9 +15,15 @@ import (
// A client that rewrote its conversation between a tool call and its result,
// as Pi does compacting it mid-turn, is answered by a run told the
// conversation as it now is: the run that made the call still holds the one
// from before, and went on telling the client a context as large as ever, so
// Pi compacted again at each tool call. A conversation that only went on
// from before, and would tell the client a context as large as ever, so the
// client compacted again at each tool call. A conversation that only went on
// keeps its run.
//
// The run that made the calls is not ended by the rewrite: a fork or a
// resumed session of the same conversation answers the very calls it waits
// on (#2933), by their ids, and its request must not end a run whose own
// results are still to come. It waits parkLongest more, as a run whose
// caller stopped does, and is let go if they never come.
func TestRunLetGoWhenTheClientRewroteTheConversation(t *testing.T) {
s := New()
b := s.subscription
@@ -101,12 +108,18 @@ func TestRunLetGoWhenTheClientRewroteTheConversation(t *testing.T) {
if got := ask(`[` + conv + `]`); got != "tool_use call_2" || len(started) != 1 {
t.Fatalf("a conversation that went on: %q from %d runs", got, len(started))
}
// the client compacts, and sends call_2's result after its summary
// the client compacts, and sends call_2's result after its summary; the
// rewrite is answered by a run of its own, and the run that made the
// calls goes on waiting for the results of the client that has not
// rewritten anything
go func(run *subscriptionRun, waiter chan mcpToolResult) {
if r, ok := <-waiter; ok {
t.Errorf("the run from before the rewrite was handed %v", r)
says(run, "from the conversation before")
if r, ok := <-waiter; !ok {
t.Errorf("the run's call was closed before its own results came")
return
} else if r.Content[0]["text"] != "B2" {
t.Errorf("the run's own results answered %v", r)
}
says(run, "from the conversation it was told")
}(started[0], <-second)
rewritten := `[` + user("a summary of the conversation so far") + `,` + call("call_2") + `,` + result("call_2", "B") + `]`
if got := ask(rewritten); got != "text from the conversation as it now is" {
@@ -118,7 +131,131 @@ func TestRunLetGoWhenTheClientRewroteTheConversation(t *testing.T) {
started[0].mu.Lock()
closed := started[0].closed
started[0].mu.Unlock()
if !closed {
t.Fatal("the run from before the rewrite is still waiting")
if closed {
t.Fatal("the run that made the calls was ended by another conversation's results")
}
// its own results, a moment later: they go on in the run that made the
// calls, which holds the conversation they follow
conv += `,` + call("call_2") + `,` + result("call_2", "B2")
if got := ask(`[` + conv + `]`); got != "text from the conversation it was told" || len(started) != 2 {
t.Fatalf("the run's own results: %q from %d runs", got, len(started))
}
started[0].mu.Lock()
closed = started[0].closed
started[0].mu.Unlock()
if closed {
t.Fatal("the run that made the calls was ended though its results came")
}
}
// nopStdin stands for Claude Code's open input: a run kept for its
// conversation's next turn, whose park does not reset its timer (parkLongest
// is what released promises, not what park leaves).
type nopStdin struct{}
func (nopStdin) Write(p []byte) (int, error) { return len(p), nil }
func (nopStdin) Close() error { return nil }
// A run whose conversation was rewritten by another's request goes on
// waiting parkLongest for the results of the client that has not rewritten
// anything, and is let go then: a rewrite leaves no Claude Code waiting an
// hour for results that came by another conversation (#2933).
func TestRewrittenRunWaitsParkLongestForItsOwnResults(t *testing.T) {
oldPark := parkLongest
parkLongest = time.Second
defer func() { parkLongest = oldPark }()
s := New()
b := s.subscription
calls := func(run *subscriptionRun, id string) chan mcpToolResult {
waiter := make(chan mcpToolResult, 1)
run.mu.Lock()
run.pending[id] = waiter
run.mu.Unlock()
b.mu.Lock()
b.calls[id] = run
b.mu.Unlock()
run.emit(Event{Kind: KStart, MsgID: "m", Model: "claude-sonnet-5"})
run.emit(Event{Kind: KToolStart, ID: id, Name: "read"})
run.emit(Event{Kind: KToolArgs, Text: "{}"})
run.emit(Event{Kind: KStop, Stop: "tool"})
run.endSegment()
return waiter
}
says := func(run *subscriptionRun, text string) {
run.emit(Event{Kind: KStart, MsgID: "m", Model: "claude-sonnet-5"})
run.emit(Event{Kind: KText, Text: text})
run.emit(Event{Kind: KStop, Stop: "stop"})
run.endSegment()
}
var started []*subscriptionRun
start := func(_ context.Context, req *Request) (*subscriptionRun, <-chan Event, error) {
run := &subscriptionRun{bridge: b, token: "run" + strconv.Itoa(len(started)), pending: map[string]chan mcpToolResult{}, stdin: nopStdin{}}
b.mu.Lock()
b.runs[run.token] = run
b.mu.Unlock()
events := run.attach()
run.timer = time.AfterFunc(time.Hour, func() { run.abortFor("it waited as long as it may") })
if len(started) == 0 {
calls(run, "call_1")
} else {
says(run, "from the conversation as it now is")
}
started = append(started, run)
return run, events, nil
}
ask := func(msgs string) string {
t.Helper()
body := `{"model":"claude-sonnet-5","max_tokens":100,"tools":[{"name":"read","input_schema":{"type":"object"}}],"messages":` + msgs + `}`
rec := httptest.NewRecorder()
var u Usage
if code, msg := s.serveSubscription(rec, httptest.NewRequest("POST", "/v1/messages", strings.NewReader(body)), provider.Anthropic, "Claude Code", "claude-sonnet-5", "", []byte(body), &u, start); code != 200 {
t.Fatalf("%d %s", code, msg)
}
var res struct {
Content []struct{ Type, Text, ID string } `json:"content"`
}
json.Unmarshal(rec.Body.Bytes(), &res)
if len(res.Content) == 0 {
t.Fatalf("no answer: %s", rec.Body)
}
return res.Content[0].Type + " " + res.Content[0].Text + res.Content[0].ID
}
user := func(text string) string { return `{"role":"user","content":` + strconv.Quote(text) + `}` }
call := func(id string) string {
return `{"role":"assistant","content":[{"type":"tool_use","id":"` + id + `","name":"read","input":{}}]}`
}
result := func(id, text string) string {
return `{"role":"user","content":[{"type":"tool_result","tool_use_id":"` + id + `","content":` + strconv.Quote(text) + `}]}`
}
if got := ask(`[` + user("read a") + `]`); got != "tool_use call_1" {
t.Fatalf("the first turn: %q", got)
}
// a rewritten request for the same call's results: answered by a run of
// its own, and the run that made the call is not ended for it
rewritten := `[` + user("a summary of the conversation so far") + `,` + call("call_1") + `,` + result("call_1", "A") + `]`
if got := ask(rewritten); got != "text from the conversation as it now is" || len(started) != 2 {
t.Fatalf("after the rewrite: %q from %d runs", got, len(started))
}
started[0].mu.Lock()
closed := started[0].closed
started[0].mu.Unlock()
if closed {
t.Fatal("the run that made the calls was ended at once by another conversation's results")
}
// past parkLongest with no results of its own, it is let go
deadline := time.Now().Add(10 * time.Second)
for {
started[0].mu.Lock()
closed = started[0].closed
started[0].mu.Unlock()
if closed {
break
}
if time.Now().After(deadline) {
t.Fatal("the run whose conversation was rewritten is still waiting")
}
time.Sleep(20 * time.Millisecond)
}
}