Commit Graph
5 Commits
Author SHA1 Message Date
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
yetone 9309e28ced plugins: a plugin's provider in front of a vendor's API is that vendor to the gateway (songlairui on X, #5462, #5464)
A plugin whose provider's `api` is on a vendor's preset host (a Mistral
Vibe plugin's https://api.mistral.ai/v1) got none of what magpie does for
that vendor, since its Host() is the plugin's id. An earlier turn's
thinking went to the plugin's fetch as reasoning_content, and Mistral
turned the whole request away (422 extra_forbidden); the plugin had to
rewrite the body itself.

provider.VendorHost and VendorPreset now resolve a plugin's declared `api`
host to the preset that names it (every Chat, Responses and Anthropic base
of every preset and region; local and remote magpie presets excluded, and
an api on loopback, a private address, .local or .localhost names no
vendor). For every other provider, and a moved subscription, they are
Host() and Preset as before. The gateway's vendor checks read them: 38
p.Host() sites, Mistral's, OpenRouter's, DeepSeek's, Kimi's and
DashScope's preset checks. Host() stays the plugin's id where calls are
kept and shown; requests still go only to the plugin's fetch. Search and
the video plan check are unchanged. No community plugin's api is on a
preset host today (checked at plugins 7b52e3fc), so none changes.

Any plugin gets this with no new field; documented in
docs/subsystems/provider-plugins.md and the plugin docs (en, zh, ja).

Run (mote): go vet ./... darwin, GOOS=linux -tags nogui, GOOS=windows;
go test -count=1 -timeout 45m ./internal/provider ./internal/plugin
./internal/gateway: ok (provider 209s, plugin 182s, gateway 1002s). gofmt -l clean.
Broken by hand: pluginVendor resolving nothing (the old behaviour) turns
TestPluginVendor red, and TestPluginInFrontOfMistralIsMistral red on both
halves: the translated Claude Code turn is answered Mistral's 422
extra_forbidden, and the relayed Chat request carries reasoning_content.
Not tried: a real Mistral Vibe key through the reporter's plugin; Windows
and Linux runs of the tests (no OS-specific code).
2026-11-10 10:01:59 +08:00
yetone c4245c4c20 gateway: a Messages body's tools with a root union go folded to every upstream
ziko on X (11-04): ZCode on Zed's subscription, Claude Sonnet refused
every request. Six of the GoAnyAPI MCP server's tools (goanyapi_adsense,
the 36th, among them) have a oneOf at their input_schema's root, ZCode
sends every tool with every request, and Anthropic's API turns such a
request away: "input_schema does not support oneOf, allOf, or anyOf at
the top level".

Cause: a request translated to Messages always had its tools folded to an
object root (objectSchema, #1271), but a Messages body relayed as it came
(ZCode or Claude Code to an Anthropic-protocol provider, the Zed plugin's
models among them, whose plugin provider has an Anthropic URL) never
was: foldsRoots and objectRootsBody handled Chat and Responses only, so
neither the first ask nor the retry after the refusal folded it.

Change (object_root.go):
- foldsRoots is true for every Anthropic-protocol relay, from the first
  ask, as a translated request already was.
- objectRootsBody takes a Messages body: a client tool's input_schema goes
  through provider.ObjectRoot; a server tool (no input_schema) and a
  schema with no root union go as they came. The translated path
  (to != Anthropic guard at gateway.go's objectRootsReq) is unchanged, so
  nothing is folded twice.
- docs/subsystems/gateway-routing.md gains the root-union bullet.

Test: TestMessagesRootUnionFolded, POST /v1/messages (stream) to a relay
with an Anthropic URL whose fake refuses a root union with Anthropic's
own wording: the union tool goes as an object root on the first ask, its
branches' properties merged; a plain tool's schema goes unchanged, and so
does a body with no union.

Run on mote at 69eb93e0a (origin/main, just fetched) plus this change:
gofmt -l clean; go vet ./internal/gateway on darwin, GOOS=windows and
GOOS=linux -tags nogui, ok; go test -timeout 45m ./internal/gateway:
ok (914s); go test ./internal/masking (reads the
subsystem docs), ok. Not run: the whole ./... suite; CI runs it.

Rebased after the run onto 14f6d10de: its three commits since 69eb93e0a
change catalog, provider/remote_quotas and GUI files, none in
internal/gateway.

Broken by hand: object_root.go back to HEAD, TestMessagesRootUnionFolded
answers 400 "tools.1.custom.input_schema: input_schema does not support
oneOf, allOf, or anyOf at the top level", the reported error.

Not tried: a real Zed account with GoAnyAPI's tools; the fake answers as
Anthropic's documented refusal does.
2026-11-04 18:26:37 +08:00
yetone e9ae12f8c4 gateway: a tool with a union at its root, refused in OpenAI's wording, is asked again folded (#1493, jevejoze)
Codex desktop's mcp__codex_app__automation_update has an anyOf root.
An upstream that validates tools the way OpenAI does refuses it with
"schema must have type 'object' and not have
'oneOf'/'anyOf'/'allOf'/'enum'/'const'/'not' at the top level" (the
reporter's message). OpenAI's own API refuses it with "schema must be a
JSON Schema of 'type: \"object\"', got 'type: \"None\"'" (asked today
with the fixture's schema, its bytes in the test). rootUnionRefusal
knew only xAI's and Anthropic's wordings, so these 400s went to the
agent. Both OpenAI wordings now match, so the request is folded and
retried as it already is for xAI and Anthropic. The retries only fire
when folding changes the request.

jevejoze's branch proposed the same regex change for the first wording.

Run:
- TestRootUnionFoldedWhereRefused now runs xAI, OpenAI-compatible and
  OpenAI refusals, passed through and translated.
- New TestRootUnionRefusalWordings, including two unrelated
  invalid-schema 400s that must not match.
- Break check: reverting object_root.go turns the four new subtests and
  the wording test red.
- Sandbox magpie on a real api.openai.com key: direct, the union gets
  400 (wording above). Through the gateway, the folded retry passed
  validation and reached the account's billing check (429 billing not
  active). No answer was possible on that key.
- go test ./internal/gateway: ok. gofmt -l: clean. GOOS=linux and
  GOOS=windows go vet: clean.
2026-10-10 18:54:16 +08:00
yetone c8ac18a2ca gateway: Grok on a relay's key or xAI's own API gets a tool's root-union parameters folded to an object, so Codex's turns aren't refused (#1271)
Only the Grok subscription's requests were folded (grokBody). Grok
anywhere else refused Codex desktop's automation_update the same way:
AiHubMix's grok-4.7 on its Chat API answered the real schema 400
"Failed to start sampling: [invalid_client_tool_schema] ... tool parameter
root must be an object type" (2026-10-08) and 200 once folded; xAI's API
(api.x.ai) had no fold at all. OpenRouter's x-ai/grok-4.7 takes the union.

xAI's API now gets such tools folded from the first request. Any other
upstream that refuses one for its root ("tool parameter root must be an
object type", or Anthropic's "does not support oneOf, allOf, or anyOf at
the top level" behind a relay's Chat API) is asked once more with them
folded, passed through or translated, and that model is folded from then
on. An upstream that takes the union gets it as sent.

TestRootUnionFoldedWhereRefused (Responses passed through, and Codex's
Responses translated to a Chat-only relay, with AiHubMix's bytes) fails
without the gateway.go change: the client gets the 400. TestRootUnionFoldedForXAI
checks xAI's host is folded and another isn't. go test -tags nogui ./...
in a temp HOME: all pass but the root package and internal/gui under load,
which pass rerun alone; vet for darwin, linux and windows; builds for all three.
2026-10-08 17:01:22 +08:00