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.
This commit is contained in:
yetone
2026-10-31 23:35:27 +08:00
parent 90f8a10566
commit c8df9ecbbc
2 changed files with 32 additions and 4 deletions
+8 -4
View File
@@ -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()
+24
View File
@@ -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)
}
}