gateway: a request's body is looked at for web search without a copy, and not parsed for a provider that searches by itself
attempt() asked searchAsked first, on every relayed request, and only
then whether the provider searches by itself. searchAsked looked for
three words with strings.Contains(string(body), …), each conversion a
copy of the whole body (a 211KB body: ~640KB and ms a request), and a
body offering web_search was then parsed whole: Codex offers it on
every turn, so each relayed Codex turn was parsed only to be told
"Codex searches by itself". searchedForIt now asks what is known of the
provider and model first, and searchAsked looks with bytes.Contains.
The same copy in the siblings that look at an error body
(fallback.passing, patience.busy, routing.failure's insufficient_quota)
goes too. lan.go's and the lower-cased look-ups in gateway.go need the
string they make and are left as they are.
Test: TestSearchAskedLooksCheaply. Broken by hand (string(body) and the
old order put back): red, "a 211KB body with no search in it: 3
allocations, want 0" and "a provider that searches by itself: 29
allocations, the body parsed".
BenchmarkSearchedForIt (211KB Responses body; load average ~450):
codex-asks old 2.55-5.02 ms/op 665KB/op 29-30 allocs/op
new 0.1-1.9 us/op 0B/op 0 allocs/op
other-plain old 1.6-4.8 ms/op 663KB/op 3 allocs/op
new 0.26-1.15 ms/op 0B/op 0 allocs/op
Run: go test ./internal/gateway -run
'TestSearchAskedLooksCheaply|TestWebSearchAskedOfEachAPI|Search|Busy|
Passing|Failure|Credit|Quota' -count 1: ok (140s).
This commit is contained in:
@@ -1354,7 +1354,7 @@ func passing(status int, header http.Header, body []byte, again int) (time.Durat
|
||||
case wait > longestPause:
|
||||
return 0, false
|
||||
case status == 408, status == 500, status == 502, status == 503, status == 504, status == 529:
|
||||
if strings.Contains(string(body), didNotStream) {
|
||||
if bytes.Contains(body, []byte(didNotStream)) {
|
||||
return 0, false
|
||||
}
|
||||
return wait, again < lastRetries
|
||||
|
||||
@@ -3539,7 +3539,7 @@ func (s *Server) attempt(w http.ResponseWriter, r *http.Request, from provider.P
|
||||
ownAPI := false
|
||||
// except a subagent's task its lead sealed (#1109): only the server
|
||||
// that sealed it reads it, and a translation would drop it
|
||||
if relay && searchAsked(from, body) && (from == provider.Chat && !p.IsRemoteMagpie() || !searchesModel(p, from, model)) && !(from == provider.Responses && hasSealedAgentMessage(body)) {
|
||||
if relay && searchedForIt(p, from, model, body) && !(from == provider.Responses && hasSealedAgentMessage(body)) {
|
||||
relay, ownAPI = false, true
|
||||
}
|
||||
// Copilot seals an argument the tools mark "encrypted" (spawn_agent's
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"net/http"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
@@ -59,7 +59,7 @@ func busy(status int, body []byte) bool {
|
||||
switch status {
|
||||
case 408, 500, 502, 503, 504, 529:
|
||||
return failure(status, body) == failOther && !protectionRefused(status, body) &&
|
||||
!strings.Contains(string(body), didNotStream)
|
||||
!bytes.Contains(body, []byte(didNotStream))
|
||||
case 429:
|
||||
return failure(status, body) == failRate && !creditWords.Match(wordsOf(body))
|
||||
}
|
||||
|
||||
@@ -297,14 +297,14 @@ func failure(status int, body []byte) string {
|
||||
// Kimi's "exceeded model token limit" is the conversation's
|
||||
// length, not the plan's
|
||||
return failOther
|
||||
case status == 402 && usedUpWords.Match(body) && !creditWords.Match(body) && !strings.Contains(string(body), "insufficient_quota"):
|
||||
case status == 402 && usedUpWords.Match(body) && !creditWords.Match(body) && !bytes.Contains(body, []byte("insufficient_quota")):
|
||||
// a 402 that says a usage limit and nothing of money is out of
|
||||
// quota, not of credit: Factory's "You've reached your weekly
|
||||
// standard usage limit (resets in …). Switch to Droid Core …" is
|
||||
// Standard's pool, which counts some models only, and read as the
|
||||
// account out of credit it rested Droid Core with it (#1979)
|
||||
return failQuota
|
||||
case status == 402, creditWords.Match(body) && (status != 429 || brokeWords.Match(body)) || strings.Contains(string(body), "insufficient_quota"):
|
||||
case status == 402, creditWords.Match(body) && (status != 429 || brokeWords.Match(body)) || bytes.Contains(body, []byte("insufficient_quota")):
|
||||
return failCredit
|
||||
case status == 429 && resetsWhen.Match(body):
|
||||
// out until the time it names, in whichever language it said so:
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"cmp"
|
||||
"context"
|
||||
"encoding/json"
|
||||
@@ -215,13 +216,24 @@ var searchHosts = map[provider.Protocol][]string{
|
||||
// searchAsked is whether a body offers the model web search, looked for
|
||||
// cheaply before it is parsed.
|
||||
func searchAsked(proto provider.Protocol, body []byte) bool {
|
||||
if !strings.Contains(string(body), "web_search") && !strings.Contains(string(body), "oogle_search") && !strings.Contains(string(body), "googleSearch") {
|
||||
// bytes, not string(body): each conversion copied the whole body, three
|
||||
// times a request (a 200KB body: 640KB and 3ms)
|
||||
if !bytes.Contains(body, []byte("web_search")) && !bytes.Contains(body, []byte("oogle_search")) && !bytes.Contains(body, []byte("googleSearch")) {
|
||||
return false
|
||||
}
|
||||
req, err := parse(proto, body)
|
||||
return err == nil && req.WebSearch
|
||||
}
|
||||
|
||||
// searchedForIt is whether a relayed request's web search is done by magpie
|
||||
// for the provider, which can't be relayed: a body offering it to a model
|
||||
// that doesn't search by itself. What is known of the provider is asked
|
||||
// first: Codex offers web_search on every turn, and each relayed turn was
|
||||
// parsed whole to learn it, for a provider that searches by itself.
|
||||
func searchedForIt(p provider.Provider, from provider.Protocol, model string, body []byte) bool {
|
||||
return (from == provider.Chat && !p.IsRemoteMagpie() || !searchesModel(p, from, model)) && searchAsked(from, body)
|
||||
}
|
||||
|
||||
// searcher is the model magpie searches with: the one Settings names, while
|
||||
// it can (chosenSearcher), else the first of the providers that search by
|
||||
// themselves, with a small model of theirs, as searching needs no more.
|
||||
|
||||
@@ -656,3 +656,49 @@ func TestPluginSaidToSearch(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// A request's body is looked at for web search without a copy of it, and
|
||||
// not parsed at all for a provider that searches by itself: Codex offers
|
||||
// web_search on every turn, and each relayed turn was parsed whole.
|
||||
func TestSearchAskedLooksCheaply(t *testing.T) {
|
||||
long := strings.Repeat("a long prompt with words in it ", 7000)
|
||||
plain := []byte(`{"model":"m","input":"` + long + `"}`)
|
||||
if n := testing.AllocsPerRun(20, func() { searchAsked(provider.Responses, plain) }); n != 0 {
|
||||
t.Errorf("a %dKB body with no search in it: %v allocations, want 0", len(plain)/1024, n)
|
||||
}
|
||||
asks := []byte(`{"model":"gpt-5","input":"` + long + `","tools":[{"type":"web_search"}]}`)
|
||||
codex := provider.Provider{ID: "codex", Responses: provider.CodexBase, Account: &provider.Account{Agent: "codex"}}
|
||||
if searchedForIt(codex, provider.Responses, "gpt-5", asks) {
|
||||
t.Error("Codex searches by itself, and magpie searched for it")
|
||||
}
|
||||
if n := testing.AllocsPerRun(20, func() { searchedForIt(codex, provider.Responses, "gpt-5", asks) }); n > 20 {
|
||||
t.Errorf("a provider that searches by itself: %v allocations, the body parsed", n)
|
||||
}
|
||||
other := provider.Provider{ID: "x", Responses: "https://example.test/v1"}
|
||||
if !searchedForIt(other, provider.Responses, "gpt-5", asks) {
|
||||
t.Error("a provider that doesn't search: magpie didn't search for it")
|
||||
}
|
||||
if searchedForIt(other, provider.Responses, "gpt-5", plain) {
|
||||
t.Error("a body offering no search: magpie searched for it")
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkSearchedForIt(b *testing.B) {
|
||||
long := strings.Repeat("a long prompt with words in it ", 7000)
|
||||
asks := []byte(`{"model":"gpt-5","input":"` + long + `","tools":[{"type":"web_search"}]}`)
|
||||
plain := []byte(`{"model":"gpt-5","input":"` + long + `"}`)
|
||||
codex := provider.Provider{ID: "codex", Responses: provider.CodexBase, Account: &provider.Account{Agent: "codex"}}
|
||||
other := provider.Provider{ID: "x", Responses: "https://example.test/v1"}
|
||||
b.Run("codex-asks", func(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
for b.Loop() {
|
||||
searchedForIt(codex, provider.Responses, "gpt-5", asks)
|
||||
}
|
||||
})
|
||||
b.Run("other-plain", func(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
for b.Loop() {
|
||||
searchedForIt(other, provider.Responses, "gpt-5", plain)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user