ac4af582301a4e8a29245dbba767eba31334ddf9
6
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
e8d42c02db |
gateway: a masked request tells the model its placeholders are used as written (treeMan)
treeMan on Discord: with masking on, weaker models refuse to search
with a {{NAME_xxxxxxxx}} placeholder, ask to verify it, or rewrite it as
"an example name", which breaks long tasks. A request in which masking
swapped a value (maskHits.n > 0) now carries one short note, on each try:
Values written like {{NAME_ab12cd34}} are placeholders for real values.
Use them exactly as written, in replies and in tool calls alike: they
are filled in with the real values before anything runs. Don't ask to
verify them, and don't replace them with examples.
It names neither magpie nor a vendor or client (TestPlaceholderNoteIsNeutral
fails on magpie, Codex, Claude, a vendor's name, "provider", "gateway"...).
Its example uses a '1', outside the placeholder alphabet, so it is never
taken for a real placeholder or restored.
Where: where placeholders first appear, so it is new text the first time
and the same text on every later turn (the cached prefix holds). In the
instructions when they or the tools hold one (Anthropic system's last
block, Chat's first system/developer message, Gemini systemInstruction's
parts); otherwise on the earliest message that holds one, as a block or
part after its content (a string content becomes [text, note] blocks on
Anthropic, a paragraph on Chat), and on Responses as a developer message
after that item and the tool outputs following it. Responses
`instructions` (Codex's base_instructions) is never touched. A note
appended after text starts with a blank line, so a translator that joins
blocks (Anthropic to Chat) keeps it apart from the user's words.
Not added:
- a Claude subscription: the bridge puts the caller's instructions into
the run's first message, which Anthropic classifies; text added there is
risk for no gain;
- a ChatGPT/Codex sign-in (Account.Agent codex); Codex's own turns on
codexUpstreamOn don't pass this loop either;
- a Responses turn with previous_response_id: the note would pile up in
the stored conversation.
count_tokens counts without it.
A first version put the note in the system prompt. The full suite showed
TestMaskedConversationKeepsItsCachedPrefix red (a secret first found in a
tool output changed the system at turn 1 and rewrote the cache), hence
the first-appearance placement.
Run (mote, through mote-run):
- go test ./internal/gateway ./internal/redact -run
'TestPlaceholderNote|TestMaskedRequestTold|TestMaskedConversation|
TestUnredacted|TestRedacted|TestMask|TestMiddlewareThroughGateway': ok.
- Full suite at 46bdbad8f (-timeout 45m, log kept whole): red in
internal/gateway TestMiddlewareThroughGateway (mine: its fake echoes the
last message, which now holds the note; the fake strips it now), and
five tests red alone on origin/main
|
||
|
|
430e3050d8 |
gateway: magpie's own searches and image descriptions go without the agent's middleware (#3695)
aiirobyte (#3695): the gateway exited five times on 2026-10-26, and every client was refused until it was started again. The one exit with a stack died of "fatal error: concurrent map read and map write": two of seenBody's goroutines in middleware.(*Run).Tried at once, one of them in moejs's InternGoString. A request's middleware runs in runtimes of the request's own (serveAgent). Only one goroutine may use a runtime at a time (moejs), and Run has no lock to hold callers to it. serve runs each try's onProvider on them (triedOn) through the Tried the request's context carries. For a model that can't see, magpie has each image of the request described (seenBody), up to four at a time, and each description is a request it makes of its own gateway on a context made from the agent's (magpieChose(ctx)). So the descriptions carried the agent's Tried and ran onProvider on one runtime from as many goroutines. A model that can't search has its searches made the same way (searchWith): a search is asked beside the agent's own stream, which goes through the middleware's onEvent, and beside the round's other searches, so one search is enough to share the runtime when the middleware has onEvent. The runtime's maps were read and written from two goroutines and the gateway died. When it didn't, the calls' values crossed. In the runs below, hooks threw on values that weren't there ("Cannot read properties of undefined (reading 'push')", and "(reading 'messages')"), and a description went out as {"0":0,"messages":[0,{"content":[0,{"image_url":0}],...}],...}, its max_tokens, system prompt, text and image each turned into 0. An earlier run of the same break (tree e1c90a00, this code with older comments) had one image asked about twice and another never. ownAsk takes the request's Tried off the context of those two asks, so they go without the agent's middleware, as serveAgent's comment already said of them ("search and vision stand-ins") and as magpie's other own requests (titles, the classifier) go. The agent's own tries are as before: each runs onProvider once, on its runtime. The subsystem page now says why, and the user docs (en, zh, ja) name searches and image descriptions among magpie's own requests. Going without also keeps what the docs tell a hook. Its ctx.protocol, ctx.model and ctx.stream are the agent's request's, while a description's body is a Chat request and a search's an Anthropic one, whatever the agent speaks. A description is also kept for later requests (sights), so one asked through a request's middleware was handed to later requests, other agents' too. The one community middleware with onProvider, antigravity-cloak, rewrites Claude Code's and Codex's lines in the system prompt, which a description's or a search's prompt, magpie's own, doesn't have. Not changed: the report's other fixes, a lock over the runtime, one ask at a time, or a Run per ask. Each keeps the gateway up but still runs the agent's middleware on asks that are magpie's own, which the docs say go without it, and with the first two onProvider still gets bodies its ctx doesn't describe. With ownAsk nothing else uses a request's Run from two goroutines (holdWriter's keepalive writes under the h.mu the handler's writes hold). magpieChose is left as it is: it says whose pick a call is, and its other callers, Codex's thread titles, descriptions and predictions (codex_titles.go), run before serveAgent with no Tried to take off. What was run (tree 4f97ac18, on origin/main |
||
|
|
cf1498f506 |
tests: the middleware tests start fresh, so a rest another test left on "a" no longer skips it (#3273)
TestMiddlewareOnProviderPerTry ( |
||
|
|
e44b9aef33 |
middleware: onProvider changes what one provider a request is tried on is sent
A JS gateway middleware can export onProvider(body, ctx). The gateway
calls it on each try, just before the body goes to that provider, with
ctx.provider = {id, name, account, model}: account is the subscription's
kind (claude, antigravity, …), absent for a key. Every try starts again
from the body onRequest left, so a change for one provider never reaches
another in a routing group. null and ctx.reject turn nothing away there.
A hook that throws or runs past 250ms leaves the try's body as it was.
A plugin could act only before routing until now, by the model name the
agent asked for. Claude Desktop asks for opaque aliases, so a plugin
couldn't tell which vendor a request was going to. The first user is
@magpie-community/middleware-antigravity-cloak: it changes only what an
Antigravity account is sent, so that Antigravity stops refusing Claude
Code's system prompt (xzplzpl on X). magpie itself still sends every
prompt as the agent wrote it.
- gwmw.To and gwmw.Tried; middleware.Run.Tried runs the hooks.
- serveAgent puts the runs on the request's context, and serve's
candidate loop calls triedOn before the Antigravity foresight, so it
judges the body that will be sent.
- Docs: gateway-middleware.md, reference.md, integrating.md, and the
hooks table and community list in site docs plugins.html (en, zh, ja).
Run:
- go test ./... -timeout 45m in a sandbox HOME (macOS, at
|
||
|
|
920d96af55 |
tests: provider, plugin, gateway, codexcat, gui and tui tests pass in any order and on any -count (#1146)
Squashed from wei's #1146, onto main. Each test now starts from state of its own instead of what an earlier test (or an earlier -count run) left: - plugin: changed() counts the OnChange hooks still running; Told waits for them, and Settle ends with Told and UseCached(nil), so a change one test made isn't told in the next and the next test's plugin list isn't read as changed. Only tests call Settle/Told; in production changed() only counts. New TestSettleWaitsForTheHooksItTold and TestSettleForgetsTheFoldersPlugins. - provider: isolate forgets Copilot's state, the plans' quota cards and balances; the warm-up, Cursor sign-in, hold and moved-plugin tests wait for what they started. - gateway: fresh() forgets refused seals, and TestRouting, TestSmartRouting and TestQuotaRestsUntilItsReset start from forgetRouting; codexSignedIn and TestForeignReasoningIsLeftOut start from fresh. - codexcat: XDG_CONFIG_HOME under the test's home. tui: costAt reset. gui: the fake Cursor account no longer outlives its test. cli: Settle. Left out as already on main: the cli_identity keepIdentities switch ( |
||
|
|
eebd0b4222 | plugin, gateway, gui: a plugin can be gateway middleware — onRequest, onEvent and onResponse run in magpie's gateway on what agents send and get back (moejs) — a plugin could only be a subscription provider, run in the Bun host, a process away from the gateway. A plugin may now declare middleware (package.json magpie.middleware, or a *.middleware.js / .mjs file), run in-process by moejs (pure-Go JS engine): compiled once, one runtime per request from a pool, so every hook of a request shares ctx.state. onRequest(body, ctx) sees the agent's body in its own API before routing; what it returns is the body the next middleware and the gateway get, undefined leaves the bytes as they were, ctx.reject(status, msg) answers with an error in the agent's API's shape. onEvent(ev, ctx) sees each SSE event's JSON (export const events = [...] filters by name; an object changes it, null drops it, undefined passes the bytes through untouched; data: [DONE] is no JSON and passes by). onResponse(body, ctx) sees a whole JSON reply, with ctx.status. ctx has protocol, model, stream, path, agent, the plugin's options and state. A hook that throws, returns a non-object or runs past its limit (250ms request/response, 50ms an event, 1s load; interrupted) leaves the request or reply as it was and is counted and logged (60 lines a minute); a compressed reply passes as-is. Files are reloaded within a second of a change. Only agents' requests run through it (serveAgent at the gateway's agent entry points and the Codex backend), never magpie's own titles, classifier or stand-ins; it sees bodies before redaction. A middleware-only package is skipped by the Bun host. The Plugins page marks a middleware with a chip, its hooks, calls and µs per call (JSON in and out counted), failures in red with the last error, and a load error. en/zh/ja/de; docs/reference.md and usemagpie.ai/docs/plugins#middleware (en/zh/ja) with an alias example. Tested with a real DeepSeek account through magpie web --gateway in a sandbox HOME: an alias middleware served "fast" and named it in message_start, a reject came back as 403 permission_error, a Chat stream uppercased; real Claude Code (sandbox CLAUDE_CONFIG_DIR) did Read, Bash and Glob tool turns through three middlewares with 0 failures: ≈1–1.6µs an event, 0.2–0.7ms an onRequest that parses and rewrites Claude Code's ≈80KB body. Real use found two bugs fixed here: [DONE] parsed as JSON (SyntaxError on every Chat stream) and stats leaving out the JSON time. internal/middleware tests (chain, undefined keeps bytes, reject, fail-open on throw/spin/string with the runtime reused, async hook, load errors, off and Bun skip, reload, split/CRLF/filtered/dropped stream events, [DONE] untouched, whole reply and gzip, 64 in parallel; BenchmarkEvent ≈0.9µs, 11 allocs) and TestMiddlewareThroughGateway (alias routed, events by protocol, redaction still upstream, reject in Anthropic's shape; fails without the change) are new; plugin-middleware.test.cjs (Chromium and WebKit, en and zh: chip, hooks and stats, failures with last error, broken and off, no left border) fails without it. The other plugin GUI tests and click-scroll pass in WebKit. go vet, go build (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui ./... under a temp HOME pass (internal/provider failed once under load and passes alone). |