TestPiPackage added testdata/pi, and TestGatewayHookMixedPiPackage a
copy in a TempDir. Neither folder has a node_modules above it, so as the
host started, Bun took each import of pi from there as one to install
itself (its auto-install): first pi.js's resolve of
@earendil-works/pi-coding-agent from the package's folder, then the
extension's own imports, which jiti 2.7 imports natively under Bun
(tryNative) from the extension's folder. Each start asked npm for 115
manifests, and init waited on them under its 2 minute bound. This would
explain the red yetone sees under a full run, and with npm slow it is
red alone.
The host then ran npm's latest pi (1.4.0 today), not the 1.2.0
installPinnedPi installs from testdata/pi-lock, so #1879's pin never
reached the host. The test's Version(piAgent) check reads the plugins
folder's copy, so it passed.
Now both tests put their package in the plugins folder
(Dir()/fake-pi-package, Dir()/mixed-pi-package), beside the node_modules
installPinnedPi fills. pi.js and the extension both resolve the pinned
pi, and nothing is asked of npm as the host starts. fakepi.ts tells the
VERSION its import of pi reached, and TestPiPackage checks it is the
pinned one. With the old layout and npm reachable, that check is red.
Not changed: the app. A pi package added from a folder with no
node_modules above it should go the same way (the tests add theirs
through the same Add, as a path): pi.js and its extensions get npm's
latest from Bun as the host starts, and the pi ensurePi put in the
plugins folder for it isn't the one that runs. Making pi.js resolve
from the plugins folder alone isn't enough. Tried here with the old
layout, the host then waited on @earendil-works/pi-ai's manifest for the
extension's import instead, and both tests were still red. Each way out
changes how plugins load: JITI_TRY_NATIVE=0 in pi.js (every pi extension
then goes through jiti's transform and pi's aliases), `bun run
--no-install` for the host (auto-install also stops for OpenCode plugins
on disk), or a Bun.plugin onResolve for pi's scopes. That is the
maintainer's call, so it isn't in this commit.
Also not changed: installPinnedPi's `bun install --frozen-lockfile`
still fetches pi's 118 tarballs on every run, as the sandbox's install
cache starts empty, and the test skips when that fails (noted in #2865).
What was run (tree 8aee6487, on origin/main acdc85ac, go1.26.8,
linux/amd64, Linux 5.15, umask 077). This tree only rewords
TestPiPackage's version check message from tree 859ae995, where the
rest was run.
- At this tree (Bun 1.4.2, npm direct): TestPiPackage and
TestGatewayHookMixedPiPackage pass. Broken by hand (TestPiPackage's
package back in testdata/pi): red, `the extension's import reached pi
1.4.0, not the 1.2.0 the lock installed`.
At 859ae995:
- A local npm relay as BUN_CONFIG_REGISTRY (Bun 1.4.2) that passes
tarballs through at once and holds each manifest 150s, with
`go test -tags nogui -count=1 -v -run
'TestPiPackage$|TestGatewayHookMixedPiPackage$' ./internal/plugin/`,
load 10 to 16:
- origin/main acdc85ac: both red. `mixed package lost Pi providers:
[] starting plugins: context deadline exceeded` (157.92s), and
`Plugins = [], starting plugins: context deadline exceeded`
(157.69s). Each test's host asked the relay for
@earendil-works/pi-coding-agent's manifest as it started.
- this tree: both pass (29.35s, and 181.28s, most of it the frozen
install's tarballs through the relay), and the relay was asked for
no manifest. With a print-only probe around the first Providers or
Plugins call, the host started in 0.5s in each test.
- main's layout with pi.js resolving pi only where a node_modules
above the folder has it (tried, not kept): both still red (host
start 122.0s and 104.8s), the relay asked for
@earendil-works/pi-ai's manifest by the extension's import.
- Broken by hand: both packages back where main puts them (testdata/pi
and a TempDir), the version check kept, the relay passing manifests
through at once. TestPiPackage red on the version check (pi 1.4.0,
not 1.2.0). The mixed test passed with a 60.7s host
start (TestPiPackage's took 27.8s). 230 manifest requests: 115
packages, once per host start.
- go test -tags nogui -count=1 -timeout 45m ./internal/plugin/ (Bun
1.4.2, npm direct): ok, 340 PASS lines counting subtests, 2 skip
(TestFlowBigReplyProd wants MAGPIE_FLOW_BIG, TestRenewOtherHost is run
by TestRenewOnceAcrossHosts). TestPiPackage 10.49s,
TestGatewayHookMixedPiPackage 22.77s.
- The same with -race: ok, the same counts, no race reported.
- gofmt -l clean. go vet -tags nogui for linux, windows and darwin, go
vet for windows, go build -tags nogui for linux and darwin and go
build for windows: exit 0 each. go vet and go build without the tag on
linux: exit 1 (no libsoup-3.0 or gio-unix-2.0 here).
- go test -tags nogui -count=1 -timeout 45m ./... (Bun 1.4.2; 9m49s,
load 16 at its start): 79 packages ok, internal/plugin among them,
and 8 tests red in 2 packages, each filed: agent TestInstalledVersion,
TestCursorFolderFromTheEditorIsNotTheCLI,
TestCopilotFolderFromAnIDEPluginIsNotTheCLI,
TestQoderSettingsFromAnIDEPluginIsNotTheCLI, TestOmpDetectedWithPiDir
and TestZedAppPath (#3141); gui TestGatewayHeldByAContainer and
TestGatewayFixQuitsOnlyAMagpie (#3649). Run by name at origin/main
acdc85ac, all are red there too but the Qoder test, which passed
alone.
Not tried: the real app, and macOS and Windows (the tests ran on Linux
only).
Reviewed and run again by the maintainer at the stack's tree 45994757c
(this commit and the six beside it, on origin/main 90518de26), go1.26.8:
- mote (macOS, arm64): gofmt -l clean; go vet and go build for darwin,
GOOS=linux -tags nogui and GOOS=windows (library, plugin, gui): exit 0.
go test -tags nogui -count=1 -timeout 45m ./...: every package ok but
internal/gui TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and
internal/proc TestNoCommandBypassesProc (#4491), both red on main
from b09349d17 and c59c547c5, in files this stack doesn't touch.
go test -race -run 'TestWSL|TestTagged|TestPinnedPiCache' for
library and plugin: ok.
- magpie-desktop (Linux, wsl-run; WebKit hangs on mote), Chromium and
WebKit: every library-*, sync-*, s3-sync, github-sync and i18n-keys
file, 4 at a time: 1003 of 1020. The 17 reds are in 6 files no commit here
touches: i18n-keys ("Unmarked", #4524), library-chip-fan, -items,
-mcp-health, -mcp-hub (#4483) and -mcp-tools. Those 6 alone, one at a
time, at load ~60: 74 of 92 here, 77 of 92 at 90518de26 itself. Red
in both: i18n-keys, -chip-fan and -items in WebKit, -mcp-tools in
Chromium ja 420; -mcp-health green in both; -mcp-hub's WebKit case
red here only, the case #4483 has red on main. -chip-fan and -items
are #3072's and #4501's kind, WebKit under load.
- Broken by hand on mote: TestPiPackage's package back at testdata/pi
is red, "the extension's import reached pi 1.4.0, not the 1.2.0 the
lock installed".
Every agent request asks plugin.HasGateway (gatewayHooks, and
GatewayBegin again when on). It read plugins.json through lastgood and
steady, cloned the list, and stat'ed and read every enabled plugin's
package.json. With 12 plugins installed and none with hooks:
Windows (Ryzen AI MAX+ 395, Defender on): 457µs, 51KB, 360 allocs
macOS (M4 Pro): 251-286µs, 36KB, 260 allocs
per call. It now keeps its answer for a second, keyed on plugins.json's
path and filememo.Gen, as middleware.current does: magpie's own writes
(plugin add/remove/on/off all go through save, which calls
filememo.Forget) are seen at once, another program's within a second.
Now 0.5µs/640B on Windows and 0.3-0.85µs/208B on macOS.
Run at b9ab17231 plus this change:
- go test -count=1 -timeout 45m ./internal/plugin ./internal/middleware
./internal/gwmw ./internal/gateway: all ok (gateway 541s).
- TestHasGatewayLooksOnceASecond (new) is red with the cache turned off
by hand ("the plugins were read again within the second"), green with
it. It also checks filememo.Forget and an expired look see a change.
- The numbers come from a throwaway benchmark (12 plugin dirs with
package.json, plugins.json with options) run on this Mac and as a
windows/amd64 test binary on the Windows box, old and new code.
- GOOS=linux and GOOS=windows go vet -tags nogui on the three packages,
gofmt.
Not tried: a hooks plugin edited by hand outside magpie in the real app
(the test covers the one-second look).
Rebased onto origin/main 3fd7e1698 (no plugin or filememo change between); go build ./... and go test -count=1 ./internal/plugin ok at the pushed head. Before that, at b5d17b26e: go build ./..., linux and windows go vet -tags nogui ./internal/plugin, and go test -count=1 ./internal/plugin ./internal/middleware ./internal/gwmw all ok.
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 63be47717, macbook at load avg 220-340 from other
sessions' suites):
- gofmt -l: clean. go vet ./..., GOOS=linux go vet -tags nogui ./...,
GOOS=windows go vet ./...: clean. go build and GOOS=linux -tags nogui
build: ok.
- go test ./... -timeout 45m: every package ok except:
- agent, gateway and library timed out at 45m under the load;
- gui's TestSetAnswersWhileDevinAndProvidersHang (#1375, also red on a
clean 63be47717 run alone; commented there);
- library's TestSkillRelinkKeepsUnreadableCopy, which passes -count=2 alone.
Rerun at -timeout 150m: agent ok (4524s), library ok (2581s). gateway was
ok except TestVisionWaitIsntTheDescribersTime's 300ms limit (#2444,
commented), which passes -count=5 alone on this tree and on 63be47717.
- internal/plugin and internal/gateway hook tests: ok.
- GUI (chromium+webkit; en, zh, zh-TW, ja, de): plugin-gateway 12/12 twice.
It failed earlier at load ~340, chromium only, on page.goto's 5s (#2542).
All 43 plugin-*.test.cjs files and mobile-web are green except:
- plugin-move: red on a clean 63be47717 too (#2399, commented);
- plugin-moved-signin: one 5s waitFor under load, then 6/6 twice.
Rebased onto ea3dee96a (21 commits; main touched host.go, host.js, i18n.js and
the plugin docs; clean rebase), then rerun at the rebased head:
- gofmt, vet (darwin, linux nogui, windows) and build: clean.
- go test: plugin ok, cli ok, gateway -run 'Hook|Middleware|Codex|Plugin' ok.
gui ok except TestMainWindowCorners, which was killed under load (#1936)
and passes alone.
- GUI: plugin-gateway 12/12, plugin-middleware 24/24, plugin-tools 22/22,
plugin-agent 22/22.
- Real account with the rebased binary: every protocol and the real Codex
CLI gave the same results as above (codename answered, success=true,
chat's model change rolled back).
Co-authored-by: Tsukikage7 <65526564+Tsukikage7@users.noreply.github.com>