The second P3 extension point (docs/subsystems/plugin-runtime.md). A
router strategy orders one pool of routes: a provider's accounts or keys,
or the heads of a group's members. The gateway still lists the pool and
keeps rests, affinity, rules, pins and failover; a strategy only orders.
- internal/routes: Seat, Strategy{Order, Exact, ByUse}, Register (stacks,
the last serves), Find, Served, and Apply, which fails open: a strategy
that panics or answers what isn't a permutation leaves the pool in its
listed order, logged. Smart is the core's; Ordered, Rotate, LeastUsed,
Pace and Weighted are routes.Builtin, moved out of the gateway with the
rotate turns and the smooth weighted round (Round, MoveRound, Forget).
- plugins/rotation (Essential): registers routes.Builtin. InUse counts the
providers and groups providers.json routes by one of them
(provider.Routings), an unreadable file is an error, so it stays on. The
gateway's KeepFresh watches providers.json too.
- Off, a routing no strategy serves is its listed order, and routingOf
reads it as "order" wherever the core asks (a turn's mark, sinks, a
rotate group's sessions, a group's heads, weightedKept).
- GUI: the routing pickers (provider editor, Routing page pools and group
editor) mark the five as data-feature="rotation" (routingSegs), so off
they offer Smart alone. i18n zh, zh-TW, ja, de.
- Docs: plugin-runtime.md router strategy section, gateway-routing.md
(also drops a duplicated sentence about a lone key), reference.md.
Run in the worktree at this change (on origin/main fbfb76149):
- go test -count=1 ./... : 57 packages ok (gateway 587s)
- gofmt -l clean; go vet ./... darwin, GOOS=linux -tags nogui, GOOS=windows
- GUI: full suite, chromium and webkit, every language: 9805 tests, 29
red in 6 files. Each red alone the same way on origin/main bc470c6b6
(just fetched): quota-count-wrap, quota-name-translated (#2228),
refusal-retries-setting (#2258), routing-held-member (#2252), usage-fold
(#2163), usage-agents-hide (#2276, from cf455ebd5). routing-save, builtins and
rate-limited-sink pass in both engines.
- internal/gateway TestMain registers the strategies, so every routing test
is the parity run with the plugin on; TestRotationOnAndOff turns it off
(three Rotate sessions start k1,k2,k3 on, k1,k1,k1 off, no turn mark;
Smart still served).
Rebased on origin/main 2b0037f41: go test ./... again, 56 packages ok at the
default timeout; internal/agent and internal/gateway passed with
-timeout 40m (1093s, 1039s: the machine at load ~17 with other sessions'
suites; the 10m panics stood in TestCodexAPIDisconnectStays and
TestCodexHeardWithinItsIdleTimeout, 1-2s in, no stack waiting on them).
Vets and gofmt clean again; routing-save, builtins, rate-limited-sink,
i18n-keys and the GUI files upstream changed pass.
Broken by hand, each test went red then restored:
- Apply's permutation check off: TestApplyFailsOpen
- Register's remove a no-op: TestRegisterStacks
- the plugin's Apply registers nothing: TestOnServesOffDoesNot
- InUse swallows providers.json's error: TestInUse
- routingOf passes an unserved routing through: TestRotationOnAndOff
- routingSegs not marking data-feature: routing-save.test.cjs "Smart alone"
293 lines
24 KiB
Markdown
293 lines
24 KiB
Markdown
# Plugin runtime
|
||
|
||
magpie's built-in plugins are the parts of magpie not everyone needs, each
|
||
one a package under `plugins/` that the user can switch off. The runtime in
|
||
`internal/pluginrt` keeps their registry, applies the ones that are on in the
|
||
order their services make, and disposes of what each registered when it is
|
||
turned off. `plugins.yml` says which are on. This page describes P0 (the
|
||
runtime, the file and the window's switch) and P1 (the window's kernel: a
|
||
plugin's page, its error boundary, the kit it draws with and its settings
|
||
form, with the Gateway page as the first plugin to draw its own page), and
|
||
P2 so far (a page moved out of the core whole: its code, styles and words:
|
||
Sessions, Library, Routing and Usage), and P3 so far (the gateway's
|
||
extension points: the observer, with OTel export as its first plugin). OpenCode plugins (npm packages in
|
||
the plugin host) are a different thing; see
|
||
[provider and plugin ownership](provider-plugins.md) and
|
||
[gateway middleware](gateway-middleware.md).
|
||
|
||
## Responsibilities and boundaries
|
||
|
||
| Part | What it does | Source |
|
||
| --- | --- | --- |
|
||
| Registry | A plugin calls `pluginrt.Register(Manifest)` from `init`. `Registered` lists them by id; two with one id panic. `plugins/all` imports every plugin, and only `main.go` imports `plugins/all`, so core code and its tests run without any. | [`pluginrt.go`](../../internal/pluginrt/pluginrt.go), [`plugins/all/all.go`](../../plugins/all/all.go) |
|
||
| Manifest | `ID`, `API` (a caret range of the host API, `"^1"`; `pluginrt.API` is `"1.0"`), `Requires` and `Provides` (services), `Essential`, `Name`/`Sub` (English; the window translates them), `One`/`Many` (what is in use, of 1 or `{n}`), `InUse`, `Apply`, `Parts` (`{Slot, Key}`: the window's parts it is), `Defaults` (its config), `Settings` (a struct of its settings, made into `Defaults` and a schema at `Register`) and `Client` (its files in the window, under `internal/gui/assets/plugins/<id>/`: scripts, its `i18n.js` and a `.css`). | [`pluginrt.go`](../../internal/pluginrt/pluginrt.go) |
|
||
| Lifecycle | `Runtime.Reconcile(on, config)`. Off first: every plugin no longer wanted, and every one requiring a service one of those provided, is disposed newest first. Then on: each wanted plugin whose requirements are provided (by `Core`, `settings`, `providers`, `gateway`, `window`, or an active plugin) is applied, again and again until no more can be. States: `off`, `pending` (waiting for a service, logged), `active`, `failed` (Apply returned an error or panicked, or the API range doesn't match; what it registered before is undone), `disposed`. A failed plugin never stops the others. | [`pluginrt.go`](../../internal/pluginrt/pluginrt.go) |
|
||
| Ctx | What `Apply` gets: `OnDispose(f)`, `Contribute(slot, key)`, `Provide(name, v)` (withdrawn when it goes), `Service(name)`, and its composed `Config`. Disposers run in reverse, each one's panic logged and the rest still run. | [`pluginrt.go`](../../internal/pluginrt/pluginrt.go) |
|
||
| plugins.yml | `appdir.Config()/plugins.yml`: `preset` (`essentials` or `all`) and `plugins`, rows of `{id, enabled, config}` holding only what the user set. | [`config.go`](../../internal/pluginrt/config.go) |
|
||
| Settings schema | `SchemaOf(Settings)` makes a JSON Schema object of the struct, in its fields' order (`x-order`): each exported field with a json name is a setting, of `bool`, an int, `float64` or `string`, with tags `title` (English; the window translates it), `description`, `enum` (comma separated) and `min`/`max`. Its values are the defaults. A field a form can't draw, or a tag that doesn't parse, panics at `Register`. `Schema.Validate` takes a value only as the schema allows it (a JSON float64 or a YAML int for an integer); `SetConfig(id, config)` writes the user's row through it, and `Compose` leaves out a value edited into plugins.yml by hand that it doesn't allow, so the default stands. | [`schema.go`](../../internal/pluginrt/schema.go), [`config.go`](../../internal/pluginrt/config.go) |
|
||
| Built-in plugins | `groups`, `library`, `otel`, `sessions`, `warmups` (P0), `gateway` (P1), the Gateway page, which is Essential and in use while a gateway key is there (`access.List`), and `routing` (P2), the Routing page, Essential and in use while the user has a routing group, since the page is where groups are made and changed. `rotation` (P3), the routings besides Smart, is Essential and in use while a provider or group is routed by one of them. `usage` (P2), the Usage page, is Essential too, with no `InUse`: what it shows the gateway counts anyway, and the tray panel's Usage tab still shows the allowances with it off. The Gateway page's one setting, `callsPage` (10, 20, 50 or 100; 20), is how many recent calls the page shows at a time. The other five aren't Essential. Each one's `InUse` counts what the user made with it: routing groups not Auto and not hidden, library items given to agents, a collector sent to, gateway conversations recorded, warm-ups and check-ins on. | [`plugins/`](../../plugins) |
|
||
|
||
The boundary is checked by `TestPluginsImportOnlyCore`: no package outside
|
||
`plugins/` imports one (but `main.go`, through `plugins/all`), a plugin never
|
||
imports another, and `plugins/all` imports every plugin folder. Plugins reach
|
||
each other only through services.
|
||
|
||
## The window's kernel (P1)
|
||
|
||
`internal/gui/assets/kernel.js` loads after `i18n.js` and before the
|
||
plugins' scripts and `app.js`, all classic scripts sharing one scope. It is
|
||
`window.magpie`:
|
||
|
||
- `magpie.plugin(id, setup)`: a plugin's script registers, and `setup` gets
|
||
a context: `config` (from boot.js's `pluginConfig`, then Plugins ›
|
||
Built-in), `onConfig(fn)`, `onDispose(fn)`, `slots.add(slot, part)` and
|
||
`ui`.
|
||
- **Slots.** The one slot is `page`: a part `{key, html, loads, mount,
|
||
…hooks}` becomes `<main class="view" id="view-<key>"
|
||
data-feature="<plugin>">` where index.html's `<template data-slot="page">`
|
||
is, at once, so app.js finds the page as it finds its own. app.js calls
|
||
`magpie.start()` when it is set up, which mounts every page, and then each
|
||
hook through `magpie.call(key, hook, …)`: `loading`, `draft` and `render`
|
||
around `/api/providers` for a page that `loads: "providers"`, `open` when
|
||
`show` opens it (a promise it gives back puts the reader back where they
|
||
were once it settles), `relang` when the language changes on it.
|
||
`title` is the page's header (English; `show` translates it).
|
||
`magpie.pages()` adds the plugins' pages to the views `show` and
|
||
`?view=` know.
|
||
- **Words.** A plugin's `i18n.js` calls `magpie.strings({zh, "zh-TW", ja,
|
||
de})`, added to i18n.js's `I18N` before app.js translates anything. A key
|
||
already there is logged and not taken. `tests/strings.cjs` reads them as
|
||
the page does, for `gui-de`, `gui-ja`, `gui-zh-tw` and `i18n-keys`
|
||
(no key in two files, every key of a plugin's file in all four
|
||
languages).
|
||
- **Error boundary.** A hook that throws, or whose promise fails, is caught
|
||
in `run`: logged with `[magpie] <plugin>: <key>.<hook>`, and the page is
|
||
replaced by a card saying "This page stopped working", the error's message
|
||
and Try again. Further hooks of a broken page do nothing, so nothing half
|
||
drawn comes back, and the other pages go on. Try again puts the page's
|
||
markup back, translates it, mounts it and runs the hook that broke with
|
||
what it was given. A plugin whose `setup` throws is logged and has no page.
|
||
- **Kit** (`magpie.ui`): `el`, `button`, `toggle` (the switch Plugins uses,
|
||
`role="switch"`), `menu` (the app's menu through `openProtoMenu`, never a
|
||
native `<select>`), `row` (a settings row) and `form(schema, values,
|
||
onChange)`: a switch for a boolean, a menu for an enum (numbers in the
|
||
window's locale), a field for a number (kept to its range, `aria-invalid`
|
||
when not) or text.
|
||
- **Settings.** Plugins › Built-in draws the rows of each plugin on, with a
|
||
schema, from `ui.form` under its row (`.feat-set`), and a change posts
|
||
`{id, config}`; `applyFeatures` hands each plugin's config to
|
||
`magpie.configure`, which calls its `onConfig` only when it changed.
|
||
|
||
The Gateway page is `plugins/gateway` with `Client: keys.js, client.js`
|
||
(`internal/gui/assets/plugins/gateway/`): `keys.js` is the gateway-key
|
||
manager that was `caller-keys.js`, and `client.js` is the page's code that
|
||
was in app.js, ending in its `magpie.plugin("gateway", …)`. Still in
|
||
app.js and app.css: its styles, its sidebar and header buttons
|
||
(`data-feature="gateway"` in index.html), and `decideEntry`, which the model
|
||
info card shares. `TestBuiltinClientsLoaded` holds index.html to load every
|
||
plugin's `Client` script between kernel.js and app.js, and its stylesheet in
|
||
the head, each with its `?v=`.
|
||
|
||
## Pages moved out (P2)
|
||
|
||
Each page moves in a commit of its own, whole: its code into
|
||
`assets/plugins/<id>/client.js`, its styles beside it, and the words only it
|
||
says from i18n.js into its `i18n.js` (a key app.js, another page, index.html
|
||
or Go also says stays in i18n.js). It stays so after: `i18n-keys`' "a
|
||
plugin's words are its own" fails when anything outside a plugin's folder
|
||
says one of its words, which goes back to i18n.js.
|
||
|
||
- **Sessions** (`plugins/sessions`, `Client: i18n.js, sessions.css,
|
||
client.js`): the page that was `sessions.js`, its markup all drawn by its
|
||
code, so its part has no `html`. Its hooks: `open` and `relang` load it,
|
||
and `reveal(s, e)` is the Usage page's way in (a session's row and All
|
||
sessions, #752), called through `magpie.call("sessions", "reveal", …)`,
|
||
so with no Sessions plugin it does nothing. Still in app.js: the Usage
|
||
page's Sessions tab (`/api/sessions`) and Settings' session terminal row
|
||
(`data-feature="sessions"`).
|
||
- **Library** (`plugins/library`, `Client: i18n.js, library.css,
|
||
client.js`): the page that was `library.js`. Its code runs at `mount`
|
||
only, where it wires its listeners (the dialog's backdrop and Escape,
|
||
focus and visibility reading it again, its lists' scroll), since the
|
||
script is in before app.js. Its hooks: `open` and `relang` read
|
||
`/api/library`, `redraw` draws it again once the state says which agents
|
||
are hidden, and `close`, `dirty` and `discard` are what `show()` asks
|
||
before the reader leaves it. `window.loadLibrary`, `redrawLibrary`,
|
||
`closeLibraryModal`, `libraryDirty` and `discardLibrary` are gone.
|
||
- **Routing** (`plugins/routing`, `Client: i18n.js, routing.css,
|
||
client.js`): the page that was `routing.js`, Essential. Its part's `html`
|
||
is the page's head and boxes that were index.html's; the rest of the page
|
||
is drawn by `routingPage`, run at `mount` because it builds on app.js. It
|
||
keeps itself after that, as it did (its own `hidden`, the language, the
|
||
window's focus), so its hooks are only `dirty` and `discard`, a routing
|
||
group being edited, which `show()` and the window's close ask through
|
||
`magpie.call("routing", …)`: `window.routingDirty` and `discardRouting`
|
||
are gone. Its tab in the tray panel and the panel's section are marked
|
||
`data-feature="routing"`. What other pages use of it is still on
|
||
`window` (`openRoute` for context.js; `kindName`, `swapWhy` and
|
||
`routedWhy` for Usage's Requests; `newGroupWith` for Providers;
|
||
`panelRoutingShown` for the panel). With the plugin off, what leads to
|
||
the page goes with it: the nav and panel tabs, the guide's Open Routing,
|
||
Providers' "Make a routing group of …" (`data-feature="routing"`), the
|
||
Usage list's View routing and its time as a link, and the context card's
|
||
Latest request; a panel left on its Routing tab shows Agents.
|
||
- **Usage** (`plugins/usage`, `Client: i18n.js, client.js, page.js`):
|
||
the page that was the largest part of app.js (about 3000 lines: the
|
||
Overview's cards, the Requests ledger, the Sessions tab and the Agents
|
||
list). Its part's `html` is the page's markup that was index.html's
|
||
`#view-usage`. Its code builds on app.js at the top level (its timers,
|
||
`?view=usage`'s tab and filters), so `page.js` is the one script a
|
||
plugin loads after app.js: index.html loads kernel.js, the plugins'
|
||
other scripts, app.js and the scripts beside it, then each `page.js`,
|
||
then `start.js`, which is app.js's old tail (`magpie.start()`, the
|
||
first page shown, the first `load()`), so the first page has its code.
|
||
`TestBuiltinClientsLoaded` holds that order. Its hooks: `open` loads
|
||
it, `refresh(again)` is `load()`'s read of it (coming back to the
|
||
Overview is opening the allowances, #1588), `reload` is Refresh now,
|
||
`costs` draws the costs again when Settings' currency changes, and
|
||
`tab` says which tab it is on, for `focusQuotaCard`. Still in app.js,
|
||
since the tray panel draws them too: the allowance cards,
|
||
`quotasOpened`, `panelQuotaFocus` and `panelUseFocus`. With the plugin
|
||
off, its nav tab goes, and so do the ways to the page
|
||
(`data-feature="usage"`): the tray panel's Open Usage and Routing's View
|
||
usage. A `show("usage")` left anywhere gives way to Agents. The panel's
|
||
Allowances tab stays.
|
||
|
||
## Gateway extension points (P3): observer
|
||
|
||
P3 opens the gateway to plugins at four points: observer, router strategy,
|
||
middleware and tool. The observer is the first. It only reads, so it is
|
||
the safest to move first. `plugins/otel` is its first consumer, and with the
|
||
plugin off the gateway does no observing work.
|
||
|
||
| Part | What it does | Source |
|
||
| --- | --- | --- |
|
||
| `usage.Observer` | `Serve() (stop)`, `Bodies()`, `Begin(Request) Watch` and `Ledger(Record)`. An observer watches what the gateway serves and what the ledger is written, and never changes either. `Observe(o)` registers one until its remove; `Observers` lists them. It lives in `internal/usage`, not the gateway, because every row is a usage `Record` and the ledger's other writers reach it too. | [`observe.go`](../../internal/usage/observe.go) |
|
||
| Serving | `ServeObservers()` starts every observer registered, and each one registered later, until the last of its stops (it is counted). Only the process serving the gateway calls it (`Server.ListenAndServe`), so a CLI that writes a usage row exports nothing. Remove stops a serving observer. | [`observe.go`](../../internal/usage/observe.go), [`gateway.go`](../../internal/gateway/gateway.go) |
|
||
| `Begin` and `Watch` | As a request starts the gateway asks each observer (`observeRequest`) with `usage.Request`: its header, agent, session (the native one when the agent names it), call kind (`auxiliary` for Claude's tool-less helper calls) and `Plain` (loopback, no gateway key, not passed on from another magpie). A `Watch` it returns gets `Attempt` for each routing attempt, unbilled failures and retries too, and `End` once. With no observer, or none watching, the request carries nothing. | [`observe.go`](../../internal/gateway/observe.go) |
|
||
| Bodies | `Bodies()` is `NoBodies`, `CapturedBodies` (the first 256 KiB, cut with `usage.BodyCut`) or `WholeBodies` (the reply spooled whole as it streams, #538). The gateway takes the most any observer wants now (`ObservedBodies`): it keeps the whole reply only when one asks for it, and puts bodies on an attempt's record only when one asks for any. | [`observe.go`](../../internal/gateway/observe.go), [`capture.go`](../../internal/gateway/capture.go) |
|
||
| `Ledger` and `Watched` | Every row `Append` writes is passed to each observer. A row of a request some observer watched has `Watched` set (`appendUsage`), so an observer that sent the request's attempts doesn't send its row again. | [`usage.go`](../../internal/usage/usage.go), [`lan.go`](../../internal/gateway/lan.go) |
|
||
| `internal/otlp` | The OTLP/HTTP exporter, moved whole out of `internal/usage`: `otlp.New()` is an `Exporter`, a `usage.Observer`. Its Begin leaves a local agent's own turn to the agent's transcript when session tracing reads it, and takes a W3C `traceparent` as the parent. Settings are read on each call, as before. | [`otlp/`](../../internal/otlp) |
|
||
| `plugins/otel` | Requires `window`, `settings` and `gateway`. Apply registers `otlp.New()` and disposes of it when turned off. `InUse` is 1 while `settings.OTelExport` says to send (settings.json or `MAGPIE_OTEL_*`), so a user who exports only through the environment keeps it on under Essentials. | [`otel.go`](../../plugins/otel/otel.go) |
|
||
|
||
## Gateway extension points (P3): router strategy
|
||
|
||
The router strategy point says the order a pool of routes is tried in: a
|
||
provider's accounts or keys, or the heads of a group's members. The gateway
|
||
still lists the pool, and keeps rests, affinity, rules, pins and the walk
|
||
over the order itself; a strategy only orders. Smart, the default, stays in
|
||
the core. The five other routings are the built-in plugin `rotation`'s.
|
||
|
||
| Part | What it does | Source |
|
||
| --- | --- | --- |
|
||
| `routes.Strategy` | `Order(pool, seats) []int`: a permutation of the seats' indexes. `pool` is a provider's id or `group/<id>`, for a strategy that keeps a round per pool. `Exact` keeps the order as given; otherwise a provider's API keys that fit the request better go first, each in its order. `ByUse` puts each seat's recent tokens in the trace. | [`routes.go`](../../internal/routes/routes.go) |
|
||
| `routes.Seat` | What a strategy weighs, read by `weighRouted` under the routing state's lock: its rest key, whether it is an account, whether its allowance is known or it learns it as it answers, the share used, when its windows renew, its weekly pace, its key's weight, whether it rests now and its tokens of the last hours. | [`routes.go`](../../internal/routes/routes.go), [`routing.go`](../../internal/gateway/routing.go) |
|
||
| `Register`, `Find`, `Apply` | `Register(routing, s)` serves a routing until its remove; registering again stacks, and the last one serves. `Find("")` is `Smart` always. `Apply` fails open: a strategy that panics, or answers what isn't a permutation, leaves the pool in its listed order, logged. Every route is still tried. | [`routes.go`](../../internal/routes/routes.go) |
|
||
| Strategies | `Smart` (core), and `Ordered`, `Rotate`, `LeastUsed`, `Pace` and `Weighted` in `routes.Builtin`. `Rotate` keeps a turn per pool and `Weighted` a smooth weighted round (`Round`, `MoveRound`), both moved out of the gateway; `Forget` clears them. | [`strategies.go`](../../internal/routes/strategies.go), [`round.go`](../../internal/routes/round.go) |
|
||
| Off | A routing no strategy serves is its listed order. `routingOf` in the gateway reads it as `order` wherever the core asks about a routing (a turn's mark, `sinks`, a rotate group's sessions, a group's heads), so nothing of the routing is left half on. | [`routing.go`](../../internal/gateway/routing.go) |
|
||
| `plugins/rotation` | Requires `providers` and `gateway`, `Essential`. Apply registers every `routes.Builtin` and removes them when turned off. `InUse` counts the providers and groups providers.json routes by one of them (`provider.Routings`, the file alone, not the catalog); an unreadable providers.json is an error, so it stays on. The gateway's `KeepFresh` watches providers.json too, so a routing set from the CLI or the TUI turns it on within 2s. Off, the window's routing pickers (the provider editor, the Routing page's pools and groups) offer Smart alone: their options are marked `data-feature="rotation"` (`routingSegs`). | [`rotation.go`](../../plugins/rotation/rotation.go), [`provider.go`](../../internal/provider/provider.go), [`app.js`](../../internal/gui/assets/app.js) |
|
||
|
||
Groups are next on this point: `planGroup` behind it, the `groups` plugin
|
||
serving it.
|
||
|
||
The other points follow in this order: middleware (the JS gateway middleware becomes one consumer of it, with its
|
||
place, API and fail-open policy unchanged; redaction fails closed), then
|
||
tool (search) and budget.
|
||
|
||
## Source of truth and composing
|
||
|
||
What a plugin runs with is composed field by field (`Compose`), each value
|
||
remembering its layer (`Source`):
|
||
|
||
1. `default`: every plugin on, its `Manifest.Defaults`;
|
||
2. `preset`: Essentials leaves every plugin not `Essential` off;
|
||
3. `user`: the rows of plugins.yml (`enabled`, and `config` merged into the
|
||
defaults key by key, maps deeply);
|
||
4. `in use`: a plugin whose `InUse` is above 0 is on whatever the rows say,
|
||
since off its things would go on working with nowhere to see them. An
|
||
`InUse` error counts as in use.
|
||
|
||
`Composed.Chosen` is plugins.yml's say before step 4. `magpie plugin dump`
|
||
(`--json`) prints the composed list with each plugin's state and why.
|
||
|
||
## Runtime paths
|
||
|
||
- **Start** (`internal/cli/main.go`, after `settings.Migrate`):
|
||
`pluginrt.Settle(!settings.RanBefore())` writes a first plugins.yml when
|
||
there is none: `essentials` on a new install, `all` on a magpie that ran
|
||
here before (one of `settings.ranBefore`'s files is there; one that can't
|
||
be looked at counts as there), so an existing user keeps everything. Then
|
||
`pluginrt.Refresh` makes the process's runtime and reconciles it. An error
|
||
is printed and magpie starts.
|
||
- **Every read**: `Refresh` loads plugins.yml again, composes it and
|
||
reconciles. The window's `/api/state`, `/api/plugins/builtin` and boot.js
|
||
each call it (`builtinsState`), so a change made by the CLI shows on the
|
||
next read.
|
||
- **Switch**: `Set(id, on)` (`POST /api/plugins/builtin`, `magpie plugin
|
||
builtin on|off <id>`) writes the user's row; `SetPreset` the preset
|
||
(`magpie plugin preset essentials|all`). Off is refused while the plugin
|
||
is in use.
|
||
- **The window**: see [GUI app shell](gui-shell.md), "Built-in plugins".
|
||
- **The gateway**: the process serving it runs `pluginrt.KeepFresh` (every
|
||
2s), which refreshes when plugins.yml, settings.json or providers.json
|
||
changes, so a
|
||
plugin turned on or off in the window or by `magpie plugin builtin` comes
|
||
and goes in the gateway without a restart, and a headless `magpie serve`
|
||
follows the file too.
|
||
|
||
## Failure and compatibility
|
||
|
||
- A plugins.yml that can't be read or doesn't parse, or names another
|
||
preset, is an error, never an empty file. `Refresh` returns it and leaves
|
||
the runtime as it was; before the first good read every plugin is on, as
|
||
before plugins existed. `Set` and `SetPreset` refuse to write over it, the
|
||
window says it above the list and disables every switch, and the CLI exits
|
||
with it. The user fixes the file or removes it.
|
||
- Writes hold `filelock.Mutex` (`plugins.yml.lock`) against other magpies
|
||
and a mutex in the process, are written whole (temp file, sync, rename),
|
||
and keep the file they replace as `plugins.yml.bak`.
|
||
- A plugin written for another major API, or a newer minor, is `failed`
|
||
with the reason; the rest run.
|
||
- A plugin's page whose code throws breaks only that page (the boundary
|
||
above); a POST of a config the schema refuses is a 400 and writes nothing.
|
||
- A page plugin's off is the window's alone: its page, sections and rows
|
||
leave the window. The gateway, the TUI and the CLI serve the same either
|
||
way, and nothing the user made is deleted. A gateway plugin's off is the
|
||
gateway's: with `otel` off nothing is exported and no reply is kept
|
||
whole for it. An observer can't fail a request; it only reads. With
|
||
`rotation` off a pool set to one of its routings is tried in its listed
|
||
order; a strategy that fails falls back to the same order, never to an
|
||
error. Davsync doesn't carry
|
||
plugins.yml.
|
||
|
||
## Verification
|
||
|
||
- `go test ./internal/pluginrt/` (`TestReconcile`, `TestCompatible`,
|
||
`TestPluginsImportOnlyCore`, `TestCompose`, `TestSettleAndSet`,
|
||
`TestBrokenFileIsKept`), `go test ./internal/settings/ -run TestRanBefore`.
|
||
- `go test ./internal/pluginrt/ -run 'TestSchemaOf|TestValidate|TestSetConfig'`.
|
||
- `go test ./internal/gui/ -run 'TestBuiltin|TestBootSaysBuiltinsOff'`
|
||
(`TestBuiltinConfig`, `TestBuiltinClientsLoaded`).
|
||
- `i18n-keys`, `gui-de`, `gui-ja`, `gui-zh-tw`, and a moved page's own
|
||
tests (`sessions-*`, `session-*`, `sess-*`; `library-*`, `skill-*`,
|
||
`mcp-*`, `found-skill-*`) in Chromium and WebKit.
|
||
- `internal/gui/tests/builtins.test.cjs` (the settings form) and
|
||
`plugin-kernel.test.cjs` (the boundary, Try again, `callsPage` from
|
||
boot.js and from a change) in Chromium and WebKit, every language, 1100,
|
||
800 and 420 points; the Gateway page's own tests (`gateway-*`,
|
||
`recent-calls-*`) unchanged.
|
||
- P3 observer: `go test ./internal/usage/ -run 'TestObserver|TestBodiesStayOutOfTheLog'`,
|
||
`go test ./internal/gateway/ -run 'TestObserverPoint|OTel|Deferred'`,
|
||
`go test ./internal/otlp/ ./plugins/otel/`
|
||
(`TestOnExportsOffSendsNothing`, `TestInUseByEnvironment`) and
|
||
`go test ./internal/pluginrt/ -run TestKeepFreshFollowsPluginsYml`.
|
||
- P3 router strategy: `go test ./internal/routes/ ./plugins/rotation/`
|
||
(`TestRegisterStacks`, `TestApplyFailsOpen`, the strategies' orders,
|
||
`TestOnServesOffDoesNot`, `TestInUse`), `go test ./internal/gateway/`
|
||
whole (its `TestMain` registers the strategies, so every routing test is
|
||
the parity run with the plugin on; `TestRotationOnAndOff` turns it off),
|
||
and `routing-save.test.cjs` (Smart alone with it off, every width and
|
||
engine).
|
||
- By hand, under a sandbox HOME: `magpie plugin builtin`, `magpie plugin
|
||
builtin on groups`, `magpie plugin dump`, and a plugins.yml broken by hand.
|