1ccacad63281094de8a3372d5a507d38f9969e5c
4
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
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 ( |
||
|
|
244a21d471 |
gateway: a structured-output answer a model wrapped in a code fence reaches the client as the JSON inside it (#2196, lidi1011)
Codex's background memory extraction asks for text.format json_schema and
parses the reply text as JSON. A translated model that answered
```json\n{...}\n``` failed it with "expected value at line 1 column 1".
A client that asked for structured output (a format field, or one told in
the system prompt: Request.FormatTold, set by inSystem) now gets the JSON
inside a single unlabelled or json-labelled fence when that is a complete
valid JSON value: streamed (fenceEncoder, which holds text only while it
could still be a fence and passes other text on at once) and whole
(unfencedResult in render). Invalid or truncated JSON, other languages,
text outside the fence and plain-text clients are left as they came.
Relayed (passthrough) replies stay byte for byte. Rebased onto #2126's
asObject/told format: inSystem keeps its told check and sets FormatTold.
lidi1011's patch from lidi1011/magpie fix/structured-output-code-fence
(e67b4ba7b, 8bc440bbb), taken as is apart from the format.go conflict.
Run (macOS, sandbox HOME, pinned caches):
- go test -tags nogui -run 'Fence|Format|Structured|JSON' ./internal/gateway: ok
- with the encoder and render hooks removed by hand,
TestStructuredOutputFencedAnswerUnfenced (json_schema/json_object, whole
and streamed) and TestStructuredOutputFenceAfterSystemFallback FAIL with
text = "```json\n{...}\n```"; restored, they pass.
- on
|
||
|
|
3ec45e7155 |
gateway: a json_schema an upstream turns away is asked for as a JSON object, the schema in the system prompt, before the format is told in words alone (#2126)
klarkxy's Codex on Xiaomi MiMo's Token Plan got back "text.format type
'json_schema' is not supported, only 'text' and 'json_object' are
supported" and the turn ended there. Codex's /responses reaches the
upstream by passthrough when the protocols match, and passthrough never
looked at a format refusal; the translated path did, but only by moving
the whole format into the system prompt, which drops the object
requirement the vendor would have kept.
Now, on either path, a json_schema the upstream turns away is asked
again as {"type":"json_object"} with the schema appended to the system
prompt (Responses instructions, Chat's first system/developer message,
Anthropic system). If json_object is turned away too, the format field
goes and only the words stay. Each step is remembered per provider,
model and protocol once a request with it is taken, so later turns go
out already rewritten. Anthropic's output_config.format has no object
mode, so it goes straight to words. DeepSeek, which takes json_object
only, gains the same middle step on the translated path.
Run:
- format_refused_test.go, new:
- TestPassthroughSchemaRefusedAskedAsObject replays the reporter's
exact error and probe body through /backend-api/codex/responses.
It covers object taken (2 asks, then json_object) and object refused
too (3 asks, then no format). Turn 2 is checked to take 1 ask, the
instructions to keep "You are Codex." first, and the input to be
unchanged.
- TestPassthroughFormatRefusedChatAndAnthropic.
- TestRefusedFormatToldInWords was rewritten for both outcomes on the
translated path.
- Break check: with the old gateway.go, the passthrough test gets back
the reporter's exact error.
- gofmt -l is clean. go vet passes for darwin, GOOS=linux -tags nogui
and GOOS=windows. go test ./internal/gateway/ -count=1 is ok (1017s).
Not tried: a real Xiaomi MiMo key (none here; the error envelope is
assumed to be OpenAI's), and Gemini passthrough, which has no rewrite.
|
||
|
|
40c5b92a93 |
gateway: structured output crosses every translation: a Responses text.format or a Chat response_format reaches a Chat, Responses, Messages or Gemini upstream in its own words (frostming on Discord: responses.parse through magpie to OpenRouter came back as plain text)
The IR held a schema only from Anthropic's output_config.format, and only Messages (and Claude Code's --json-schema) were asked for one. A Responses request to a provider magpie speaks Chat or Messages to (OpenRouter has no Responses URL) lost text.format; a Chat request translated to Responses or Messages lost response_format; an Anthropic schema translated to Chat or Responses was lost too. Format (format.go) now carries json_schema (name, description, schema, strict) or json_object from Responses, Chat, Anthropic and Gemini, and each builder asks for it in its upstream's field. Where a target can't say it (json_object on Messages or Claude Code; the vendor bridges Cursor, Devin, Kiro, Qoder, Command Code, Zed) it is told in the system prompt. An upstream answering 400/422 naming the field (DeepSeek's "This response_format type is unavailable now") is asked again with it told in words, and so from then on. Relayed requests are unchanged. |