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) + } +}