Files
magpie/internal/gateway/mantle.go
T
yetone 59d9f5c7d9 gateway, redact: compile their 275 package-level regexps on first use, not at every start
magpie is one binary, so every package's init runs on every launch,
including `magpie statusline`, which Claude Code runs on every message.
internal/gateway and internal/redact compiled 275 patterns at init:
3.7 MB + 2.3 MB and ~38k allocations per launch, most of them for
vendors and rules that launch never reaches.

New internal/lazyre: RE holds the source and compiles it through a
sync.Once on first use, with the *regexp.Regexp methods magpie calls
delegating to it (Of wraps one already compiled, for the user's own
privacy rules). A one-off Go AST rewrite turned every regexp.MustCompile
in a package-level var (outside func literals) into lazyre.New; the
struct fields, map and parameter that hold them took the new type. A
bad pattern now panics on first use instead of at start, so
TestAllCompile in gateway and redact compiles every registered one.

Measured on mote (M5 Pro), sandbox HOME with a copy of real accounts,
v0.1.3515 (273b9f5a9) vs this change:
- GODEBUG=inittrace=1: all init 13.0 MB / 106k allocs -> 7.4 MB / 70k;
  gateway 3.68 MB -> 70 KB, redact 2.31 MB -> 261 KB.
- `echo {} | magpie statusline`, /usr/bin/time -l, 3 runs each:
  instructions retired 260-265M -> 212-215M (-18%), peak footprint
  22.1-22.3 MB -> 18.6-19.2 MB.

Run: go vet ./... (mote); GOOS=linux and GOOS=windows go vet of
gateway, redact, lazyre; go test -race ./internal/lazyre (incl. a
bad-pattern test that MustAll panics); go test ./internal/redact (all);
go test ./internal/gateway -run 'TestAllCompile|OtherOff|WorkDir|
WorkPlace|RunNoise': ok. Broken by hand: TestBadPatternPanicsInMustAll
registers `(` and fails if MustAll does not panic.
Not run before the push: the whole internal/gateway package and the
full suite (a full suite of 273b9f5a9 was running on mote).
2026-11-10 12:34:14 +08:00

74 lines
2.8 KiB
Go

package gateway
import (
"bytes"
"context"
"io"
"net/http"
"github.com/tidwall/gjson"
"github.com/yetone/magpie/internal/lazyre"
"github.com/yetone/magpie/internal/provider"
)
// Amazon Bedrock's Mantle endpoint (@marktoown on X) serves its OpenAI APIs
// under two prefixes, each model at one: most at /v1, OpenAI's closed GPT
// models and xAI's Grok at /openai/v1, as each model's card says. A model
// asked at the other is turned away with "model `x` isn't supported on
// this route". provider.MantleBase picks the prefix by the model's id;
// a model it guessed wrong (a maker the rule doesn't know yet) is asked
// again at the other one, and from then on there.
// mantleRoutes is whether a request of p's on to is placed by MantleBase:
// Mantle's chat completions and Responses. Its Messages are at /anthropic
// alone.
func mantleRoutes(p provider.Provider, to provider.Protocol) bool {
return (to == provider.Chat || to == provider.Responses) && p.IsBedrockMantle()
}
// mantleRouteKey is how unfit remembers that Mantle serves model at the
// prefix MantleBase doesn't guess for it.
func mantleRouteKey(model string) string { return "mantle-route:" + model }
// mantleWrongRoute is Mantle's refusal of a model asked at the prefix that
// doesn't serve it.
var mantleWrongRoute = lazyre.New(`(?i)(?:isn't|is not|not) supported on this route`)
// mantleReroute asks a request Mantle refused for its route again at the
// other prefix, once, and remembers that prefix for the model when it is
// taken there. Any other answer is given back as it came.
func (s *Server) mantleReroute(ctx context.Context, p provider.Provider, to provider.Protocol, path string, body []byte, in http.Header, res *http.Response) (*http.Response, error) {
model := gjson.GetBytes(body, "model").String()
if res.StatusCode < 400 || res.StatusCode >= 500 || model == "" || !s.fits(p.ID, mantleRouteKey(model), to) {
return res, nil
}
b, _ := io.ReadAll(io.LimitReader(res.Body, 1<<20))
res.Body.Close()
res.Body = io.NopCloser(bytes.NewReader(b))
if !mantleWrongRoute.Match(b) {
return res, nil
}
s.markUnfit(p.ID, mantleRouteKey(model), to)
again, err := s.forwardOnce(ctx, p, to, path, body, in)
if err == nil && again.StatusCode < 400 {
return again, nil
}
var ab []byte
if err == nil {
ab, _ = io.ReadAll(io.LimitReader(again.Body, 1<<20))
again.Body.Close()
again.Body = io.NopCloser(bytes.NewReader(ab))
}
if err == nil && !mantleWrongRoute.Match(ab) {
// served there, and refused for something else: that answer
return again, nil
}
// served at neither prefix (a model this account or region hasn't):
// the guess stays, and the client gets the first refusal
s.mu.Lock()
delete(s.unfit, p.ID+"\x00"+mantleRouteKey(model)+"\x00"+string(to))
s.mu.Unlock()
return res, nil
}