From c8df9ecbbc6d1c3caf19a6a801ddbafedcd8f38c Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 31 Oct 2026 22:42:44 +0800 Subject: [PATCH] gateway: a search round ended without its stop hands on its held text (#4562 review) textCallRound holds text from where a written call may begin, and gives it back at KStop. A round whose channel closes without a KStop is one searchRounds takes as ended, and its held text was lost with it. The routes without a search give it back at their reply's end (gateway.go's held(), continuation.go's release), and the round now does the same after its loop. The previous commit's doc comment for textCallRound had also been put between searchReply's comment and its func. searchReply's comment is back on searchReply. TestTextCallRoundReleasesHeldAtEnd: a round that sends "Let me look." and the start of a DSML call, then closes. Broken by hand: the t.release() taken out. Red: it handed on only "Let me look.\n\n". Run (on mote, through mote-run): - Full `go test -tags nogui -count=1 -timeout 45m ./...` with this stack on c30d3b7b6: 82 packages ok. internal/gui was red only on TestSettingsSaveKeepsWhatItDoesNotSend (#4514, SearchAll from b09349d17) and TestColdTurnsTaken (#4510). Both are filed, and neither touches this stack. - Rebased onto 29d0ab39f, which changed internal/gateway since then: gofmt -l, go vet (darwin, GOOS=linux -tags nogui, GOOS=windows) and go test -count=1 ./internal/gateway ./internal/usage ./internal/provider ./internal/sessions all ok at the pushed head. go build ./... ok for darwin, linux (nogui) and windows. - go test -race of the gateway tests this stack adds or touches, on the stack at fb29cd30f: ok. --- internal/gateway/search.go | 12 ++++++++---- internal/gateway/search_textcalls_test.go | 24 +++++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/internal/gateway/search.go b/internal/gateway/search.go index f19efc64e..f1c8df00f 100644 --- a/internal/gateway/search.go +++ b/internal/gateway/search.go @@ -1053,10 +1053,6 @@ const SearchAgent = "magpie-search/1" // and message of a failure. type round func(ctx context.Context, req *Request) (<-chan Event, int, string) -// searchReply answers req through ask, running the searches its model -// asks for and asking it again with what they found; the client sees one -// reply. The first round's failure is a status, as another provider may -// take over. // textCallRound is a round whose reply has the calls written into its // text made calls (textCallSee), as the replies of the routes without a // search have them (streamTranslated, forwardTranslated): every search @@ -1089,11 +1085,19 @@ func textCallRound(tools []Tool, ask round) round { t.event(ev) } } + // a round that ended without its stop still hands on the + // text held from a mark begun, as the routes without a + // search do at their reply's end + t.release() }() return out, status, msg } } +// searchReply answers req through ask, running the searches its model +// asks for and asking it again with what they found; the client sees one +// reply. The first round's failure is a status, as another provider may +// take over. func (s *Server) searchReply(w http.ResponseWriter, r *http.Request, from provider.Protocol, name string, req *Request, usage *Usage, ask round) (int, string) { ctx, cancel := context.WithCancel(context.WithValue(r.Context(), searchForKey{}, &CallFor{Agent: callerOf(r).agent, Model: unprefixed(req.Model)})) defer cancel() diff --git a/internal/gateway/search_textcalls_test.go b/internal/gateway/search_textcalls_test.go index b0124d164..db97b0130 100644 --- a/internal/gateway/search_textcalls_test.go +++ b/internal/gateway/search_textcalls_test.go @@ -1,6 +1,7 @@ package gateway import ( + "context" "encoding/json" "io" "net/http" @@ -155,3 +156,26 @@ func TestTextWrittenCallSearchRouted(t *testing.T) { }) } } + +// A round whose events end without its stop (searchRounds takes such a +// round as ended) still hands on the text textCallRound held from a mark +// begun: it is the reply's, not lost with the channel (#4562 review). +func TestTextCallRoundReleasesHeldAtEnd(t *testing.T) { + text := "Let me look.\n\n" + issueDSML[:40] + ask := func(ctx context.Context, req *Request) (<-chan Event, int, string) { + in := make(chan Event, 1) + in <- Event{Kind: KText, Text: text} + close(in) + return in, 200, "" + } + out, _, _ := textCallRound([]Tool{{Name: "Bash"}}, ask)(t.Context(), &Request{}) + got := "" + for ev := range out { + if ev.Kind == KText { + got += ev.Text + } + } + if got != text { + t.Fatalf("a round ended without its stop handed on %q, not %q", got, text) + } +}