ac4af582301a4e8a29245dbba767eba31334ddf9
5
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
d24be3c251 |
gateway: a request over a provider's byte cap is asked again with its oldest images left out (EDwinpzy)
EDwinpzy on Discord (v0.1.2605, Windows, Codex Desktop, compaction on auto/deepseek-v4.1-flash through Command Code): a long session full of screenshots could not compact. Command Code refused the summary request with 400 "Request body is 40.1 MB, over the 32 MB limit for one request (33554432 bytes)". That is not a 413, so summaryTooLarge never sent it to the text-only ask, and Codex's compaction failed three times. The compaction guard then held it for five minutes, and after that the same 40 MB body was sent again. Agent turns of the same session hit the cap the same way. What changed (internal/gateway/body_cap.go, wired in forward and forwardOnce): - byteCapRefusal: a 413, or a refusal of any other status whose words say the body is over a size limit. A 401, 403, 429 or a context overflow (tooLong) never counts. - refitOverCap (forward): the cap is taken from the refusal (the smallest size it names below what was sent, else 3/4 of what was sent) and kept per provider id. The request is then asked again, fitted to 3/4 of the cap, at most three times. - To fit a request, the oldest history images are replaced one by one, each with a text part: "[An earlier image was left out here to keep this request under its size limit.]" (it names neither magpie nor the vendor). The latest user message's images, and every text including tool output, are kept. Every other byte of the body stays as it was. Covers Responses, Chat, Anthropic and Gemini. - withinCap (forwardOnce): once a cap is known, a later request near it is fitted before it goes, so each turn doesn't pay a refusal first. - When leaving out every earlier image still doesn't fit, or every size is refused, the agent gets a 413 request_too_large naming the size, the cap and what was kept. Codex reads that as context_length_exceeded, and its compaction goes on to plainCompact and localSummary instead of sending the same body into the guard's hold. Every route that calls serve goes through forward, including magpie's own: compaction, titles, the classifier, search, vision and the Codex fallback turn. So all of them are fitted. Not changed: magpie's own requests still run no gateway hooks, so a plugin's onRequest doesn't see compaction (EDwinpzy's other ask). Running them there changes the plugin contract: a recall plugin's context would land in summaries and titles, and onResponse would see calls the user never sent. Filed as #4847, and written in gateway-request-hooks.md. gateway-routing.md describes the byte-cap fitting. Tests (body_cap_test.go), built from the reporter's shape: a ~40 MB Codex session of 20 ~2 MB screenshots, from view_image tool outputs and pasted user images, and a latest user message with its own screenshot. The fake upstream refuses anything over 32 MiB with the reporter's literal 400 body. No network. - TestCompactionOverByteCapLeavesOutOldImages (Responses and Chat upstreams): Codex gets the model's SUMMARY. The second ask is under 24 MiB, the oldest screenshot is replaced by the marker, SHOT19 (the latest user message's image) is kept, and every user text and tool output text is kept. - TestTurnOverByteCapLeavesOutOldImages: a turn is fitted the same way, and the next turn goes fitted with one upstream call. - TestOverByteCapNothingToLeaveOut: every image is in the latest message. The body is sent once, and Codex gets context_length_exceeded with "over the 32.0 MB 1017dp takes in one request ... no earlier image". - TestCompactionOverByteCapEveryTime: refused at every size. Asks get smaller, there are at most 6 calls, and it ends in a local summary with no image in it. - TestByteCapRefusal and TestWithoutOldImagesEachProtocol: the classifier (TPM 429, context overflow, 401 and an invalid-schema 400 are not caps), statedCap, and the Chat, Anthropic and Gemini splices. Run on mote (Go 1.26.8, macOS) at this change on |
||
|
|
9c54221814 |
plugins: origin magpie-key only for the sharing key used from this computer; from elsewhere it stays gateway-key
|
||
|
|
387e8572a8 |
plugins: the sharing key magpie made itself reaches a hook as origin magpie-key, any other gateway key stays gateway-key
A gateway hook's ctx.origin was "gateway-key" both for the sharing key
magpie makes and writes into this computer's own agents (a WSL distro
under NAT, MAGPIE_ADDR beyond loopback) and for a named key the owner
made for a friend. A plugin couldn't tell them apart: plugins PR #526
(nowledge-mem)'s "Agents using a Magpie key" switch would have put the
owner's memories into a friend's requests.
Now access.Authenticate marks the default sharing key (Identity.Default:
the key marked lan, the one LANKeyID names, or an older magpie's
settings-only key, as access.Default tells it), and gatewayOrigin
returns "magpie-key" for it. Every other key, on loopback too, stays
"gateway-key": one the owner made, a value a user brought, or a
sk-magpie- key that didn't authenticate. magpie has no "this named key
is mine" mark, so none of those is treated as the owner's.
Why a new value and not a new field: every consumer in
magpie-community/plugins (agent-office's track.mjs, powercontext, and
#526) compares origin to "local" only, and treats anything else, an
unknown value included, as not local. None compares to "gateway-key" in
code. So "magpie-key" lands in their not-local branch, where the
sharing key already was: no plugin gains or loses a request it serves.
host.js passes the context through untouched. A plugin that wants the
owner's agents checks one value.
docs/subsystems/gateway-request-hooks.md says what each value is. Not
changed: the plugins repo has no type or doc for origin; #526's own
README and switch are its author's to scope (asked on the PR).
Tests:
- TestAuthenticateTellsTheDefaultKey (access): the ConfigureLAN key, a
key named by LANKeyID only, and the legacy settings key read Default;
a made key and a user-brought value don't.
- TestAsyncGatewayOrigin (gateway, real Bun host): the sharing key from
10.0.0.1 and from loopback reaches onRequest and onResponse as
magpie-key; a friend's own-value key as gateway-key.
Run (on mote, at this tree):
- go test -timeout 45m ./...: 80 packages ok (access, gateway 760s,
plugin among them); internal/gui red only in its real-window AppKit
tests (TestDockJoinLeadsCommandTab, TestPanelCloseHidesIt,
TestMainWindowCorners and 7 more, each killed at its deadline). Three
of them run at a fresh origin/main
|
||
|
|
ef7f55fe98 |
plugin: Discover lists a repository of gateway hooks alone, as a request hook (#3821)
MengXiangxi (#3821): a GitHub repository tagged magpie-plugin whose package.json has magpie.hooks and no main, exports or index.js, a gateway request hook alone that installs and runs, never showed in the Plugins page's "Unofficial, on GitHub". readTagged read the magpie block's middleware, agent and page but not hooks, so such a package was taken for a provider: the file #1327's check looks for in the repository was its entry as Bun resolves it, index.js, which it hasn't, and the 404 left it out without a word. An inert index.js, as the reporter tried, lists it, as a provider. readTagged reads magpie.hooks. A package with hooks, no main or exports, and none of middleware, agent or page is of kind "hooks", and the file #1327's check looks for is its hooks file, so one whose hooks file isn't committed (a dist built only to publish) is still left out, as a middleware's is. The Plugins page picks "hooks" out with middleware, as both run in the gateway (kindOf), and its chip says Request hook, as an installed one's row does, through the word kindChip takes since the previous commit. docs/subsystems/gateway-request-hooks.md says so. Not changed: a raw 404 that GitHub's cache answers just after a push is still kept for that push in this process (the reporter's second point, #4067). plugins.js grows 51 bytes for it. With the previous commit the core's scripts are 24 bytes under main's, 1,527,948 of 1,528,076. What was run (tree 79884772, on origin/main |
||
|
|
9b04c4e20f |
plugin: async gateway request hooks, run in Bun before routing and after the reply (#2118)
A package's magpie.hooks entry (or a *.hooks.{js,mjs,ts,mts} file) exports
`async onRequest(body, ctx)` and an optional `onResponse(reply, ctx)`. They
run in the Bun plugin host, so a hook can await I/O, for example a context
service like PowerContext, which the sync moejs middleware can't. The
gateway still makes one model request per client request. A hook adds
context to the body; it doesn't register tools or run a loop, so magpie
doesn't become an agent runtime.
Ported from Tsukikage7/magpie#1. Changes from the fork:
- The entry name is magpie.hooks / *.hooks.*. The fork's magpie.gateway /
*.gateway.mjs is already the middleware's alias, so a hook importing
node:fs was also loaded as moejs middleware and failed there.
TestGatewayHookNamesApartFromMiddleware fails if the two sets overlap.
- onRequest is rolled back, and counts as failed with no onResponse, when
it throws, times out, returns non-JSON, or changes tools, tool_choice,
function_call, functions, toolConfig, model or stream.
- onResponse gets the client-facing reply, merged through the gateway's own
stream decoder, collector and renderer (no second merger).
- Codex's native passthrough (/backend-api/codex) runs the hooks too,
without the sync middleware.
- The run ends with the request handler, not the client connection. Codex
hangs up on response.completed, and the reply it got is still captured
as a success. TestGatewayHookSeesReplyAfterClientHangsUp is red on the
old `r.Context().Err()` check and green with -count=3.
- ctx.origin is host-derived (local / gateway-key / remote). Headers, keys
and peer addresses never reach the hook.
- Plugins page: a hook package shows a "Request hook" chip, a diagnostics
line (outcome code, calls, avg ms, bytes, failures; no content), and
Options. Strings are in en, zh, zh-TW, ja and de.
Docs: docs/subsystems/gateway-request-hooks.md (new), provider-plugins.md,
gateway-middleware.md, and site docs plugins.html in en, zh and ja.
Real use, sandbox HOME on 127.0.0.1:3486 with the owner's real Codex Pro
sign-in. The probe hook adds a codename note and logs both callbacks:
- /v1/responses, /v1/messages, Gemini generateContent: stream and
non-stream. onRequest's note reached the model (the answer had the
codename), and onResponse got success=true and the whole reply.
- /v1/chat/completions: with the probe also changing `model`, the change
was rolled back (the reply came from the asked model) and no onResponse
ran. Without that change, earlier: stream and non-stream success.
- The real Codex CLI 0.162.1 `codex exec` through openai_base_url ->
/backend-api/codex: the hook saw origin=local, agent=codex and the
query; the answer had the codename; onResponse success=true (before the
hang-up fix it never ran: the context was already cancelled).
- `magpie plugin list` shows only "async gateway hook onRequest" for the
hook package, no failed middleware.
Tests (base origin/main
|