Files
magpie/internal/middleware/middleware.go
T
yetone e40675cc19 middleware: onAccount can pass an account over for a request (bm_rabbit)
bm_rabbit (Discord) is building MOD_Probe, a middleware that tells
Codex accounts apart and needs to keep some requests off some of them.
onProvider could only change a try's body, never which account took
it.

A JS middleware may now export onAccount(account, ctx). The gateway
calls it before each try on a subscription account, at the top of the
candidate loop (accountSkipped). That is before anything of the try is
done: no Codex reset is spent, no body is built, nothing is sent.

account is {ref, provider, name, kind, model}. ref is 32 hex
characters: an HMAC-SHA256 of the middleware's name and the account's
agent, lower-cased user and plan. Its key is random, made once per
install in account-ref.key in magpie's config folder. It is written to
a temp file and linked into place, so it never writes over a key that
is there. The ref is the same on every request and after a restart,
and different for each middleware. The email can't be found from it
without the key. A key file that can't be read is never overwritten:
that process uses a key of its own and logs it.

{skip: true} passes the account over for this request. The next
candidate is asked, as past an account that can't be used. The call's
fallback gets a "passed over by middleware <name>" line, and the
account is not rested or held against. When the last candidate is
passed over, nothing is sent (no fail-open). The agent gets the
failure of a candidate asked before it, or, if there was none, the
usual 404 "none of <model>'s models is ready".

Anything else uses the account: undefined, {}, null, a throw, or going
past the 250ms limit. The last three count as failures. A key's try
asks no onAccount. ctx.account holds the same object for the hooks
after it on that try, and is taken away on a key's try. ownAsk clears
the Accounts runs with the Tried ones.

Note: the plan is part of the ref, so a plan change gives the account
a new ref.

Docs: docs/subsystems/gateway-middleware.md (runtime path step 4,
limits, fail open, verification), docs/reference.md,
docs/integrating.md, and site/public/docs/{,zh/,ja/}plugins.html (an
onAccount row in the hooks table). The Bun plugin host is unchanged:
middleware runs in moejs in the gateway, not in host.js.

Tests:
- internal/middleware TestOnAccount: the ref is stable across runs and
  a key reload; it differs between two middlewares, users, plans and
  kinds, and is the same when only case differs; no email or plan
  reaches the hook. {skip:true} names the middleware; undefined,
  {skip:false}, throw and null use the account (2 failures counted);
  ctx.account reaches onProvider; a key's try clears it.
- internal/middleware TestAccountRefKey: the key file is made at 0600,
  read back, never written over when unreadable, and no temp file is
  left behind.
- internal/gateway TestOnAccountSkipMovesToNextAccount: two Codex
  accounts, Ordered. The first is passed over, only acct-2 is sent
  the request, acct-1 doesn't rest, and the same holds on a second
  request.
- internal/gateway TestOnAccountSkipEveryAccount: every account passed
  over gives a 404 "…models is ready", nothing is sent, nobody rests.

Broken by hand, each on mote, each red, then restored:
- skip ignored in accountSkipped: both gateway tests red (acct-1
  reached; 200 instead of 404)
- fail open on the last candidate: SkipEveryAccount red (200)
- the skipped account rested: both gateway tests red (rests)
- a random key per ref: TestOnAccount (refs changed between requests)
  and TestAccountRefKey red
- the middleware name left out of the hash: TestOnAccount red (two
  middlewares told one ref)
- an unreadable key overwritten: TestAccountRefKey red

Run at this commit's tree (rebased on origin/main 6a900e0d0):
- gofmt -l: clean
- mote-run go vet -tags nogui ./internal/... ./plugins/...,
  GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...: ok
- mote-run go test -tags nogui -count=1 -timeout 45m ./... (log kept
  whole): every package ok except three. internal/middleware's
  TestPointsLoadErrors was mine: its load-error text now names
  onAccount. The test is fixed in this commit, and the package and
  plugins/middleware were rerun ok at this tree. The other two are red
  on main and already filed: internal/cli TestLibraryMCPHub (#4140,
  TempDir cleanup) and internal/gui TestSettingsSaveKeepsWhatItDoesNotSend
  (#4514, SearchAll from b09349d17)
- Rebased again on origin/main 20482d71f (it changed
  internal/gateway/affinity.go): mote-run go test -tags nogui -count=1
  ./internal/middleware ./internal/gateway ./plugins/middleware: all ok
  (gateway 846s)

Not tried: a real MOD_Probe against real Codex accounts. Not changed:
probe keys can still reach deactivated accounts (out of scope).
2026-11-01 02:19:26 +08:00

481 lines
12 KiB
Go

// Package middleware runs the gateway middleware plugins declare: a
// module with onRequest, onAccount, onProvider, onEvent and onResponse
// hooks that the gateway calls on every request an agent sends it, each
// account and provider it tries the request on, every event of the reply's
// stream and every whole reply.
//
// It runs in the gateway's own process, in moejs, a JavaScript engine
// written in Go: a hook on one streamed event costs about a microsecond,
// where a round trip to the plugin host's Bun costs tens of them, and a
// reply streams thousands of events. Each middleware is compiled once;
// each request takes a runtime of its own from the middleware's pool, so
// requests run their hooks in parallel and a request's hooks share its
// ctx.state.
package middleware
import (
"errors"
"fmt"
"log"
"os"
"path/filepath"
"sort"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/MoeForge/moejs"
"github.com/yetone/magpie/internal/filememo"
"github.com/yetone/magpie/internal/jsmod"
"github.com/yetone/magpie/internal/plugin"
)
// How long a hook may run before it is stopped and its input kept as it
// was: a request or a whole reply, and one event of a stream.
var (
requestLimit = 250 * time.Millisecond
eventLimit = 50 * time.Millisecond
loadLimit = time.Second
)
var errTimeout = errors.New("took too long")
// mw is one middleware, compiled.
type mw struct {
spec, name string
mod *moejs.Module
onReq moejs.Hook
onProv moejs.Hook
onAcct moejs.Hook
onEv moejs.Hook
onResp moejs.Hook
hasReq bool
hasProv bool
hasAcct bool
hasEv bool
hasResp bool
// events are the stream events onEvent is called for, by name; nil
// is every one
events map[string]bool
options []byte // the plugin's options, as JSON
pool sync.Pool
// strats are its strategies and onUse its onUsage (points.go), which
// run in pt
strats []strat
onUse moejs.Hook
hasUse bool
pt *points
calls, nanos, failures atomic.Int64
orders, rows, dropped atomic.Int64 // strategy calls, onUsage calls, rows dropped
lastErr atomic.Pointer[string]
logged atomic.Int64 // lines logged this minute, and the minute
}
// set is the middleware in force, in plugins.json's order.
type set struct {
stamp string
list []*mw
errs map[string]string // by spec: why it didn't load
// hooked: some module of list has onRequest, onAccount, onProvider,
// onEvent or onResponse
hooked bool
droppedSaid atomic.Int64
}
var (
loadMu sync.Mutex
cur atomic.Pointer[set]
nextCheck atomic.Int64
// checkedGen is filememo's generation when current last looked
checkedGen atomic.Uint64
)
// current is the middleware in force, loaded again when plugins.json or a
// middleware's file changed, which is looked at once a second, and at
// once after magpie wrote a file of its own.
func current() *set {
s := cur.Load()
now := time.Now().UnixNano()
n := nextCheck.Load()
g := filememo.Gen()
if s != nil && g == checkedGen.Load() && (now < n || !nextCheck.CompareAndSwap(n, now+int64(time.Second))) {
return s
}
checkedGen.Store(g)
st := stamp()
if s != nil && s.stamp == st {
return s
}
loadMu.Lock()
defer loadMu.Unlock()
if s := cur.Load(); s != nil && s.stamp == st {
return s
}
s = load(st)
cur.Store(s)
return s
}
// Reload drops the middleware loaded, so the next request loads it again.
func Reload() {
loadMu.Lock()
cur.Store(nil)
loadMu.Unlock()
}
type found struct {
e plugin.Entry
file string
}
func middlewares() []found {
var out []found
for _, e := range plugin.Load().Plugins {
if e.Off {
continue
}
if file, _ := plugin.Middleware(plugin.Target(e.Spec)); file != "" {
out = append(out, found{e, file})
}
}
return out
}
// stamp changes when plugins.json or a middleware's file does.
func stamp() string {
var b strings.Builder
b.WriteString(plugin.ListStamp())
for _, f := range middlewares() {
b.WriteString("|" + f.file)
if fi, err := os.Stat(f.file); err == nil {
fmt.Fprint(&b, ":", fi.ModTime().UnixNano(), ":", fi.Size())
}
}
return b.String()
}
func load(st string) *set {
s := &set{stamp: st, errs: map[string]string{}}
for _, f := range middlewares() {
m, err := compile(f.e, f.file)
if err != nil {
s.errs[f.e.Spec] = err.Error()
log.Printf("middleware %s didn't load: %v", plugin.Name(f.e.Spec), err)
continue
}
s.list = append(s.list, m)
s.hooked = s.hooked || m.hasReq || m.hasAcct || m.hasProv || m.hasEv || m.hasResp
}
return s
}
// compile compiles the middleware in file and the modules it imports,
// which are files beside it: there is no node_modules here, so a package
// is bundled into the middleware.
func compile(e plugin.Entry, file string) (*mw, error) {
root := filepath.Dir(file)
if t := plugin.Target(e.Spec); t != file {
root = t
}
mod, err := jsmod.Compile(root, file, "a middleware")
if err != nil {
return nil, err
}
m := &mw{spec: e.Spec, name: plugin.Name(e.Spec), mod: mod, options: []byte("{}")}
if len(e.Options) > 0 {
if b, err := jsonMarshal(e.Options); err == nil {
m.options = b
}
}
// a runtime of its own says which hooks it has, and is the pool's first
in, err := m.newInst()
if err != nil {
return nil, err
}
m.onReq, m.hasReq = in.hook("onRequest")
m.onProv, m.hasProv = in.hook("onProvider")
m.onAcct, m.hasAcct = in.hook("onAccount")
m.onEv, m.hasEv = in.hook("onEvent")
m.onResp, m.hasResp = in.hook("onResponse")
if err := m.discover(in); err != nil {
return nil, err
}
if !m.hasReq && !m.hasAcct && !m.hasProv && !m.hasEv && !m.hasResp && len(m.strats) == 0 && !m.hasUse {
return nil, errors.New("exports none of onRequest, onAccount, onProvider, onEvent, onResponse, strategies and onUsage")
}
if names, ok := in.strings("events"); ok {
m.events = map[string]bool{}
for _, n := range names {
m.events[n] = true
}
}
m.pool.Put(in)
return m, nil
}
// inst is one runtime of a middleware.
type inst struct {
m *mw
rt *moejs.Runtime
timer *time.Timer
mu sync.Mutex
armed bool // a call is running, which the timer may stop
fired bool
run *Run // the request it serves
}
func (m *mw) newInst() (*inst, error) {
in := &inst{m: m, rt: moejs.NewRuntime(moejs.Options{})}
in.timer = time.AfterFunc(time.Hour, func() {
in.mu.Lock()
if in.armed {
in.fired = true
in.rt.Interrupt(errTimeout)
}
in.mu.Unlock()
})
in.timer.Stop()
if err := in.rt.SetGlobal("console", in.console()); err != nil {
return nil, err
}
in.arm(loadLimit)
err := in.rt.Load(m.mod)
if in.disarm() {
err = errTimeout
}
if err != nil {
return nil, err
}
return in, nil
}
func (m *mw) get() (*inst, error) {
if in, ok := m.pool.Get().(*inst); ok {
return in, nil
}
return m.newInst()
}
func (m *mw) put(in *inst) {
in.run = nil
in.rt.ReleaseCallData()
m.pool.Put(in)
}
func (in *inst) arm(d time.Duration) {
in.mu.Lock()
in.armed, in.fired = true, false
in.mu.Unlock()
in.timer.Reset(d)
}
// disarm ends what arm began, and says whether the timer stopped it.
func (in *inst) disarm() bool {
in.mu.Lock()
in.armed = false
fired := in.fired
in.mu.Unlock()
in.timer.Stop()
if fired {
in.rt.ClearInterrupt()
}
return fired
}
// call calls h with the time it has, and settles the promise an async
// hook returns: there is nothing for one to wait on, so it has settled by
// the time the call returns, unless it waits on a promise that never does.
func (in *inst) call(h moejs.Hook, limit time.Duration, args ...moejs.Value) (moejs.Value, error) {
in.arm(limit)
v, err := in.rt.Call(h, args...)
if in.disarm() {
err = errTimeout
}
if err != nil {
return v, err
}
switch st, res, ok := moejs.PromiseResult(v); {
case !ok:
return v, nil
case st == moejs.PromiseFulfilled:
return res, nil
case st == moejs.PromiseRejected:
return v, fmt.Errorf("rejected: %s", in.text(res))
default:
return v, errors.New("returned a promise that never settled")
}
}
// hook is the export name, or default's member name, when it's a function.
func (in *inst) hook(name string) (moejs.Hook, bool) {
if h, err := in.m.mod.Hook(name); err == nil {
if ok, _ := in.rt.Has(h); ok {
return h, true
}
}
if h, err := in.m.mod.Hook("default", name); err == nil {
if ok, _ := in.rt.Has(h); ok {
return h, true
}
}
return moejs.Hook{}, false
}
// strings is the export name, or default's member name, as strings.
func (in *inst) strings(name string) ([]string, bool) {
v, ok := in.rt.Export(name)
if !ok || v.IsUndefined() {
d, ok := in.rt.Export("default")
if !ok || !d.IsObject() {
return nil, false
}
if v, _ = in.rt.Get(d, name); v.IsUndefined() {
return nil, false
}
}
g, err := in.rt.ToGo(v)
if err != nil {
return nil, false
}
list, ok := g.([]any)
if !ok {
return nil, false
}
out := make([]string, 0, len(list))
for _, x := range list {
if s, ok := x.(string); ok {
out = append(out, s)
}
}
return out, true
}
// text is v as console.log prints it: a string as it is, else JSON.
func (in *inst) text(v moejs.Value) string {
if v.IsString() {
if s, err := in.rt.Realm().ToString(v); err == nil {
return s.GoString()
}
}
if b, err := in.rt.AppendJSON(nil, v); err == nil && len(b) > 0 {
return string(b)
}
if s, err := in.rt.Realm().ToString(v); err == nil {
return s.GoString()
}
return "?"
}
func (in *inst) console() map[string]any {
say := func(level string) moejs.NativeFunc {
return func(_ *moejs.Realm, _ moejs.Value, args []moejs.Value) (moejs.Value, error) {
parts := make([]string, len(args))
for i, a := range args {
parts[i] = in.text(a)
}
in.m.say(level, strings.Join(parts, " "))
return moejs.Undefined(), nil
}
}
return map[string]any{"log": say("info"), "info": say("info"), "debug": say("debug"), "warn": say("warn"), "error": say("error")}
}
// linesAMinute is how many lines a middleware may log a minute, so one
// that logs every event doesn't fill magpie's log.
const linesAMinute = 60
func (m *mw) say(level, msg string) {
minute := time.Now().Unix() / 60
for {
v := m.logged.Load()
n, at := v&0xffff, v>>16
if at != minute {
n = 0
}
if n >= linesAMinute {
return
}
if m.logged.CompareAndSwap(v, minute<<16|(n+1)) {
break
}
}
if len(msg) > 2000 {
msg = msg[:2000] + "…"
}
log.Printf("middleware %s [%s]: %s", m.name, level, msg)
}
// fail notes a hook that failed, whose input goes on as it was.
func (m *mw) fail(hook string, err error) {
m.failures.Add(1)
msg := hook + ": " + errText(err)
m.lastErr.Store(&msg)
m.say("error", msg)
}
func errText(err error) string {
var exc *moejs.Exception
if errors.As(err, &exc) {
return exc.Name() + ": " + exc.Message()
}
return err.Error()
}
// State is a middleware as the Plugins page shows it.
type State struct {
Hooks []string `json:"hooks"`
Events []string `json:"events,omitempty"`
Error string `json:"error,omitempty"` // why it didn't load
Calls int64 `json:"calls"`
AvgMicro float64 `json:"avgMicros"`
Failures int64 `json:"failures"`
Last string `json:"lastError,omitempty"`
// Strategies are the routings it serves ("<plugin>/<name>"), Orders
// how many times they were asked; Usage says it has onUsage, Rows how
// many rows it was given and Dropped how many it missed, the queue
// full
Strategies []string `json:"strategies,omitempty"`
Orders int64 `json:"orders,omitempty"`
Usage bool `json:"usage,omitempty"`
Rows int64 `json:"rows,omitempty"`
Dropped int64 `json:"dropped,omitempty"`
}
// States are the middleware plugins, by spec.
func States() map[string]State {
s := current()
out := map[string]State{}
for spec, e := range s.errs {
out[spec] = State{Hooks: []string{}, Error: e}
}
for _, m := range s.list {
st := State{Hooks: []string{}, Calls: m.calls.Load(), Failures: m.failures.Load()}
for _, h := range []struct {
ok bool
name string
}{{m.hasReq, "onRequest"}, {m.hasAcct, "onAccount"}, {m.hasProv, "onProvider"}, {m.hasEv, "onEvent"}, {m.hasResp, "onResponse"}} {
if h.ok {
st.Hooks = append(st.Hooks, h.name)
}
}
for _, x := range m.strats {
st.Strategies = append(st.Strategies, m.routing(x.name))
}
st.Orders, st.Usage, st.Rows, st.Dropped = m.orders.Load(), m.hasUse, m.rows.Load(), m.dropped.Load()
for n := range m.events {
st.Events = append(st.Events, n)
}
sort.Strings(st.Events)
if st.Calls > 0 {
st.AvgMicro = float64(m.nanos.Load()) / float64(st.Calls) / 1e3
}
if p := m.lastErr.Load(); p != nil {
st.Last = *p
}
out[m.spec] = st
}
return out
}