Files
magpie/internal/plugin/host_flow_test.go
T
forever c162a98a11 plugin tests: TestHostJSCancellation and TestFlowAbortDuringSetupStaysHealthy stop node at the run's deadline, catch ready-wait and held-window leaks by name, and fail a case that never finishes (#1120)
* plugin tests: TestHostJSCancellation's ready-wait counts every listener on its fake stdin and stdout, so a cancelled bootstrap wait that leaves one there fails by name

ready-wait counted stdout's drain listeners only. A listener that a
cancelled wait left for any other event, on stdout or on stdin's
readline, went through every cycle. A fetch that adds a stdout 'close'
listener and removes it only once it starts (a request cancelled while
it waits for init never starts) passed with nothing but a
MaxListenersExceededWarning, which the Go test only logs. A stdin 'line'
listener per fetch was caught at 30,000 cycles only by the 10s
deadline, since every line walks every listener; at 1,000 (d9b2cdc5) it
passes in 0.6s.

The case now records every event's listener count on both once stdout
is blocked, and checks the counts are the same after the cancelled
waits. Today they are { stdin: { line: 1, close: 1 }, stdout: { drain:
1, error: 1, close: 1 } }. host.js is unchanged.

Verified by putting such a listener into host.js and running ready-wait
on Node 22.22.2 and 24.21.0 (CI's) and 26.8.1. Each passed before and
now fails 'cancelled bootstrap waits leave no listener on stdin or
stdout', with the count that grew (line: 1001, expected 1):
- a stdin 'line' listener per fetch, which 30,000 cycles caught only by
  the deadline
- a stdout 'close' listener per fetch: holding the request, holding 16
  KiB to 256 KiB more (160 KiB ran 30,000 cycles out of heap), holding
  nothing, or one shared function added again each time
- a stdin 'error' listener per fetch, or a listener on a symbol event
Unchanged host.js passes, and every leak main's case fails still fails
on the same assertion.

* plugin tests: TestHostJSCancellation's ready-wait forces a garbage collection after its cancelled waits and fails when anything still holds one of their requests

ready-wait counts what the cancelled waits share by name: waiters,
reactions on ready, queued lines, listeners on stdin and stdout. A wait
that left its request held by anything else passed every count. A
listener left on an AbortSignal every fetch shares was caught at 30,000
cycles only by the 10s deadline, since Node's EventTarget walks its
listeners on every add; at 1,000 (d9b2cdc5) it passes in 0.2s. A
reaction on a promise derived from ready rather than on ready itself, a
chain of closures in place of readyWaiters, or a list of the waiting
fetches, their controllers or their credit gates that the cancel path
never prunes passed at 30,000 too.

host.js says a cancelled wait lets "its controller, gate and body go".
The case now takes a WeakRef to each request, its controller, its
signal and its credit gate as its fetch is read. After the cycles it
forces a full collection (node runs with --expose-gc) and checks that
none of them is still alive. Waiters are counted before the collection,
so a FinalizationRegistry that tidies them up afterwards can't hide
them. A fetch that has no record in flight while it waits fails by name
rather than throwing from WeakRef. host.js is unchanged.

node also runs with --no-concurrent-recompilation, so V8 optimizes on
the main thread. On Node 23 and later it optimizes host.js's
per-request closures on a worker thread from about the 500th cycle,
and a compile job holds its function, and through it the request it
was made for, until a worker has freed the job. A job still held at
the collection makes unchanged host.js fail with retainedRequests 1.
V8 flags that delay Turbofan's queue hold one there and fail it that
way on all six local versions, 22.14.0 to 26.8.1; one worker with a
delay per compile does on 23.9.0 and later. With
--no-concurrent-recompilation each of those runs passes, and the case
takes about the same CPU time (0.2 to 0.3s).

Verified by putting such a leak into host.js and running ready-wait on
Node 22.22.2 and 24.21.0 (CI's) and 26.8.1. Each passed before and now
fails 'cancelled requests are garbage collected' with retainedRequests
1000:
- a listener on a shared AbortSignal: one aborted when stdin closes,
  linked directly or through an abortOn helper, or one per init (4
  variants)
- a reaction on a ready.then(...) every fetch shares, or a chain of
  closures in place of readyWaiters (7 variants)
- an unpruned list of the waiting fetches (2 variants), of their
  controllers or of their credit gates
Keeping each wait's abort by fetch id fails with retainedRequests 1,
since the case reuses id 1.

Unchanged host.js passes 100 of 100 runs on each of Node 22.14.0,
22.22.2, 23.9.0, 24.19.0, 24.21.0 and 26.8.1, and 25 of 25 under each of
13 V8 flag sets (--stress-compaction, --always-sparkplug, --jitless,
--single-threaded-gc, --stress-incremental-marking, --gc-interval=1000,
--minor-ms, --stress-scavenge=100, --predictable, --max-lazy,
--max-semi-space-size=1 and --conservative-stack-scanning on 26.8.1;
--always-turbofan on 23.9.0). Under each of those, each of 11 retaining
variants fails 3 of 3 runs.

* plugin tests: TestHostJSCancellation's ready-wait looks for late errors a turn after its forced collection, so an error sent when a cancelled request is collected fails on CI's Node too

ready-wait checks after every cycle that a cancelled request gets no
late error. One sent from a FinalizationRegistry callback once the
request is collected reaches that check only if node collects garbage
during the cycles. At 30,000 cycles it always did. At 1,000 (d9b2cdc5)
Node 26.8.1 still does, and Node 22 to 24 don't: such an error passes
on 22.14.0, 22.22.2, 23.9.0, 24.19.0 and 24.21.0, and CI's
ubuntu-latest runs 22 and macos-latest 24.

The case now waits a turn after the collection it forces, when such
callbacks have run, and checks again that nothing was queued. host.js
is unchanged.

Verified with a host.js whose FinalizationRegistry sends an error for
each collected request that never answered. It passed on those five
versions and now fails 'collected requests emit no late error' on each.
On 26.8.1 it still fails the check after each cycle, as on main. With
--max-semi-space-size=128 --min-semi-space-size=128, under which the
forced collection is the only one in the run, it fails 'collected
requests emit no late error' on 22.22.2, 24.21.0 and 26.8.1. Unchanged
host.js passes.

* plugin tests: TestHostJSCancellation and TestFlowAbortDuringSetupStaysHealthy stop node at 90% of the run's deadline, not at a fixed 10s a starved machine can spend starting it

Each case runs in its own `node -e` under a fixed 10s context. d9b2cdc5
took ready-wait from 30,000 rounds to 1,000, and that fixed the reported
failure: beside 16 `yes > /dev/null` loops (load 50-113), 30,000 rounds
were killed at 10.01s in 4 of 4 runs, and 1,000 rounds passed 4 of 4
(ready-wait 0.76-2.74s). Per round the work is ~100µs and flat with the
round count.

What is left is the fixed 10s itself, and it is the same for every case.
Under `taskpolicy -c background` beside 16 busy loops (load ~114), main
had 10 of the 11 TestHostJSCancellation cases killed, the one-step
stopped-gate among them. Under background QoS alone, main passed, but
stopped-gate, split-chunk and blocked-head took 5.7-7.6s, most of it
starting node. ready-wait took 2.6s there.

So both tests now run node through runHostJSCase, which stops it at 90%
of t.Deadline()'s remaining time, as e13f1513 did for
TestConcurrentRedactedCountTokens' children. A case that really hangs
still fails before the run's own deadline with "signal: killed". With
no deadline (-timeout 0), node is not stopped, the same as the test
itself.

Verified with a node stand-in on PATH that sleeps 12s before starting
node: on main, stopped-gate, ready-wait and mid-setup are killed at
10s and fail; with this change all three pass. With a stand-in that
never returns and -test.timeout 30s, the case fails at 27.00s with
"signal: killed", and no process is left behind.
go vet ./...; go build (darwin), GOOS=linux go build -tags nogui,
GOOS=windows go build; gofmt -l; -race -count=20 of both tests (ok,
43s); go test -tags nogui ./... all ok (load 34-96).
No production code changed.

* plugin tests: a host.js case that hangs is stopped after two minutes at most, not at 90% of what's left of a 10m -timeout or never under -timeout 0

runHostJSCase took 9/10 of the time left before t.Deadline(), so under
the default -timeout 10m a case that really hangs held the run for about
nine minutes before it failed, and under -timeout 0 (no deadline) it was
never stopped. The limit is now two minutes, or 9/10 of what's left if
that is sooner: still far above the node start-up a loaded machine needs.

Tested: TestHostJSCancellation and TestFlowAbortDuringSetupStaysHealthy
pass with the default -timeout and -count=3 -timeout 0; go vet, gofmt.
With the limit set to 1ms in a scratch edit, TestHostJSCancellation's
cases fail with "signal: killed", so the limit applies without a
deadline too.

* plugin tests: TestHostJSCancellation checks that a cancel ends a fetch holding at its credit window

A fetch whose reader has credited nothing back spends its window and waits
in makeGate's take for credit. A cancel ends that wait through
transitionFetch → gate.stop(), which wakes the waiting take with -1, so the
body loop leaves and the fetch's finally frees its record. No case reached
that wait. stopped-gate stops a gate with no take waiting, and split-chunk's
and normal-reply's bodies fit inside the 512 KiB window. So a stop() that
dropped the waiting take without waking it passed every case, while each
cancelled fetch held at its window stayed in inflight for good.

The new held-window case streams a 1 MiB body with no credit returned. It
waits until the chunks sent, plus FRAME_OVERHEAD for each, add up to the
whole window (8 frames). It checks that no further frame goes and that the
fetch is still in flight, then aborts it and needs the host idle with no
result or error owed for it.

Fails without the fix it guards: in a copy of host.js, makeGate's stop() is
changed to `if (wake) { wake = null }` (the waiting take is dropped, never
woken). held-window then fails with "cancel wakes a fetch waiting for
credit". Every other case, and TestHostJSCancellation on main without the
new case, passes on that mutation. A take that ignores the window
(fit = want) fails it too, with "fetch spends its whole window". The case
waits through until(), so it doesn't rely on node noticing a promise that
never settles.

Every case passes on host.js unchanged. -race -count=20 of
TestHostJSCancellation and TestFlowAbortDuringSetupStaysHealthy passes. go
test -tags nogui ./..., go vet ./..., gofmt -l, and the darwin, linux (nogui)
and windows builds pass. No production code changed.

* plugin tests: a hostJSCancellationTest case that never reaches the end of main() fails, rather than passing when node runs out of work and exits 0

TestHostJSCancellation and TestFlowAbortDuringSetupStaysHealthy run
`node -e hostJSCancellationTest <case>` with host.js on stdin and pass
when node exits 0. The fixture ended with main().catch(...), and node
exits 0 once nothing is left to run, even while main() still awaits a
promise that never settles, so a case could pass without reaching its
assertions. Waits through until() are bounded by 30 setImmediate ticks
and fail; a raw await of host code is not. stopped-gate has two,
`await run('gate.take(64)')` on an open gate and then on a stopped one.
With makeGate's `if (stopped) return Promise.resolve(-1)` changed to
`if (stopped) return new Promise(() => {})`, a stopped gate's take never
answers, and stopped-gate passed.

The fixture now marks main() finished when it resolves. An exit listener
sets exitCode 1 when node would otherwise exit 0 before that, and names
the case. A case that throws still prints only its error and exits 1.

With that broken gate, the old fixture passes every case, and the new
one fails stopped-gate:

    --- FAIL: TestHostJSCancellation/stopped-gate (0.21s)
        host_flow_test.go:35: JavaScript cancellation regression: exit status 1
            stopped-gate never finished: main() was still awaiting a promise that never settled when node ran out of work

and passes the other ten cases and mid-setup. With an open gate's take
broken the same way (`if (n > 0) return new Promise(() => {})`), the old
fixture passes stopped-gate too, and the new one fails it with the same
line (split-chunk and normal-reply fail under both, through until()).
On the real host.js every case passes. node v23.9.0, v24.19.0, v24.21.0,
v26.8.1 and v26.10.0 give the same results. CI's ubuntu runner has Node
22.23.3, not run here; its src/api/hooks.cc reads the exit code again
after emitting 'exit', which the listener relies on.

go vet ./..., go build, GOOS=linux go build -tags nogui, GOOS=windows
go build and go test -tags nogui ./... pass under a temp HOME with Go's
caches pinned, and go test -race -count=20 of both tests passes. No
production code changed.
2026-10-13 09:06:42 +08:00

733 lines
27 KiB
Go

package plugin
import (
"bytes"
"context"
"crypto/sha256"
"encoding/base64"
"io"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"reflect"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
)
func TestHostJSCancellation(t *testing.T) {
node, err := exec.LookPath("node")
if err != nil {
t.Skip("no node on PATH")
}
for _, name := range []string{"stopped-gate", "split-chunk", "terminal", "blocked-head", "blocked-head-cancel-error", "ready-wait", "ready-success", "ready-error", "local-error", "local-error-cancel", "normal-reply", "held-window"} {
t.Run(name, func(t *testing.T) {
out, err := runHostJSCase(t, node, name)
if err != nil {
t.Fatalf("JavaScript cancellation regression: %v\n%s", err, out)
}
if len(out) > 0 {
t.Logf("%s", out)
}
})
}
}
// runHostJSCase runs one case of hostJSCancellationTest in node against the
// embedded host.js. A case that hangs is stopped after two minutes, or before
// this run's own deadline if that comes first, rather than at a fixed 10s a
// loaded machine can spend starting node. The cap keeps a real hang from
// using up most of a 10m -timeout, and from hanging a -timeout 0 run.
func runHostJSCase(t *testing.T, node, name string) ([]byte, error) {
limit := 2 * time.Minute
if d, ok := t.Deadline(); ok {
limit = min(limit, time.Until(d)*9/10)
}
ctx, cancel := context.WithTimeout(context.Background(), limit)
defer cancel()
// ready-wait forces a garbage collection (--expose-gc). A function V8 is
// optimizing on a worker thread is held until a worker is done with it,
// and with it the request it was made for, so V8 optimizes on the main
// thread (--no-concurrent-recompilation).
cmd := exec.CommandContext(ctx, node, "--expose-gc", "--no-concurrent-recompilation", "-e", hostJSCancellationTest, name)
cmd.Stdin = bytes.NewReader(hostJS)
return cmd.CombinedOutput()
}
const hostJSCancellationTest = `
const assert = require('node:assert/strict')
const fs = require('node:fs')
const vm = require('node:vm')
const { EventEmitter } = require('node:events')
const source = fs.readFileSync(0, 'utf8')
const section = (from, to) => {
const start = source.indexOf(from)
const end = to ? source.indexOf(to, start) : source.length
assert.ok(start >= 0 && end > start, 'production section exists')
return source.slice(start, end)
}
const stdout = new EventEmitter()
const lines = []
let blockOn = ''
stdout.write = line => {
const msg = JSON.parse(line)
lines.push(msg)
return blockOn !== 'all' && !(blockOn === 'chunk' && msg.event === 'chunk')
}
const rl = new EventEmitter()
const context = vm.createContext({
Buffer, Headers, AbortController, DOMException, ReadableStream,
process: { stdout, stdin: {}, exit: () => { throw Error('unexpected exit') } },
readline: { createInterface: () => rl },
options: async () => ({ fetch: async () => context.reply }),
info: async () => ({ models: {} }),
sdkHeaders: () => ({}), bodyOf: () => undefined,
accountKey: () => '',
via: { run: (_, fn) => fn() }, inScope: (_, key, fn) => fn(),
})
const run = code => vm.runInContext(code, context)
run(section('const rpcWrite =', '// A plugin writing to stdout'))
run('const hooks = []; const inflight = new Map(); const handlers = {}')
run(section('async function doFetch(', '// ---- the loop'))
run(section('// Everything waits for init;'))
const message = value => rl.emit('line', JSON.stringify(value))
const tick = () => new Promise(resolve => setImmediate(resolve))
const until = async (check, label) => {
for (let iteration = 0; iteration < 30; iteration++) {
if (check()) return
await tick()
}
assert.ok(check(), label)
}
const idle = () => run('pending === 0 && inflight.size === 0')
const queued = () => run('outq.length')
const fetchOne = () => message({ id: 1, method: 'fetch', params: { window: 512 << 10, provider: 'test', model: 'test', url: 'http://localhost/' } })
const abortOne = () => message({ method: 'abort', params: { id: 1 } })
const initialize = promise => {
context.initialization = promise
run('typeof setReady === "function" ? setReady(initialization) : ready = initialization')
}
async function main() {
const name = process.argv[1]
initialize(Promise.resolve())
await tick()
context.reply = { status: 200, headers: new Headers(), body: null }
if (name === 'stopped-gate') {
run('globalThis.gate = makeGate(512 << 10)')
assert.equal(await run('gate.take(64)'), 64)
run('gate.stop(); gate.add(1)')
assert.equal(await run('gate.take(64)'), -1, 'stopped gate refuses remaining credit')
} else if (name === 'split-chunk') {
blockOn = 'chunk'
context.reply.body = (async function* () { yield Buffer.alloc(3 * (64 << 10)) })()
fetchOne()
await until(() => queued() === 1 && run('outq[0].line.includes("chunk")'), 'second slice queued while credit remains')
abortOne()
await until(idle, 'cancelled split-chunk fetch cleans registries and pending count')
assert.equal(queued(), 0, 'no late chunk or terminal queued')
assert.equal(lines.filter(msg => msg.event === 'chunk').length, 1)
assert.equal(lines.filter(msg => msg.id === 1 && ('result' in msg || 'error' in msg)).length, 0)
} else if (name === 'terminal') {
blockOn = 'all'
fetchOne()
await until(() => queued() === 1, 'terminal queued behind head awaiting drain')
abortOne()
await until(idle, 'cancelled terminal wait cleans registries')
assert.equal(queued(), 0, 'cancelled terminal removed')
} else if (name === 'blocked-head' || name === 'blocked-head-cancel-error') {
blockOn = 'all'
run('send({ id: 99, result: null })')
let bodyCancelCalls = 0
context.reply.body = new ReadableStream({ cancel() {
bodyCancelCalls++
if (name === 'blocked-head-cancel-error') throw Error('body cancellation failed')
} })
fetchOne()
await until(() => queued() === 1 && run('outq[0].line.includes("head")'), 'head queued behind blocked stdout')
abortOne()
await until(idle, 'cancelled blocked head cleans registries')
console.log(JSON.stringify({ idle: idle(), queued: queued(), bodyCancelCalls, bodyLocked: context.reply.body.locked }))
assert.equal(bodyCancelCalls, 1, 'acquired body cancelled before iteration')
assert.equal(queued(), 0, 'cancelled head removed')
assert.equal(context.reply.body.locked, false)
assert.equal(lines.length, 1, 'body cancellation emits no duplicate error')
} else if (name === 'ready-wait') {
blockOn = 'all'
let readyReactions = 0
const unresolved = new Promise(() => {})
const originalThen = unresolved.then.bind(unresolved)
unresolved.then = (...args) => { readyReactions++; return originalThen(...args) }
initialize(unresolved)
run('send({id: 99, result: null})')
const counts = emitter => Object.fromEntries(emitter.eventNames().map(event => [event, emitter.listenerCount(event)]))
const listeners = () => ({ stdin: counts(rl), stdout: counts(stdout) })
const sharedListeners = listeners()
// A cancelled wait lets its request, controller, signal and credit gate
// go. watch() reads them itself, so none stays in a variable of main,
// which main keeps while it waits.
const requests = []
const watch = () => {
const request = run('inflight.get(1)')
assert.ok(request, 'a fetch waiting for init is in flight')
requests.push([request, request.controller, request.controller.signal, request.gate].map(part => new WeakRef(part)))
}
for (let iteration = 0; iteration < 1000; iteration++) {
fetchOne()
watch()
abortOne()
await until(idle, 'cancelled bootstrap wait cleans registries')
assert.equal(queued(), 0, 'cancelled bootstrap emits no late error')
}
assert.equal(lines.length, 1, 'stdout stayed blocked at original line')
assert.equal(stdout.listenerCount('drain'), 1, 'only shared drain listener remains')
assert.deepEqual(listeners(), sharedListeners, 'cancelled bootstrap waits leave no listener on stdin or stdout')
const retainedWaiters = run('typeof readyWaiters === "undefined" ? -1 : readyWaiters.size')
const retainedReadyReactions = readyReactions - (run('typeof setReady') === 'function' ? 1 : 0)
// Waiters are counted before the collection: a FinalizationRegistry could
// tidy them up after it. A WeakRef keeps its target until the turn that
// made it ends, and what runs once a request is collected (a registry's
// callback) runs on a later turn.
await tick()
gc()
await tick()
const retainedRequests = requests.filter(parts => parts.some(part => part.deref() !== undefined)).length
console.log(JSON.stringify({ cancellations: 1000, idle: idle(), queued: queued(), readyReactions, retainedReadyReactions, retainedWaiters, retainedRequests }))
assert.equal(queued(), 0, 'collected requests emit no late error')
assert.equal(retainedWaiters, 0, 'cancelled bootstrap leaves no retained waiters')
assert.equal(retainedReadyReactions, 0, 'cancelled requests leave no reactions on shared ready')
assert.equal(readyReactions, 1, 'only shared initialization reaction remains')
assert.equal(retainedRequests, 0, 'cancelled requests are garbage collected')
} else if (name === 'ready-success' || name === 'ready-error') {
let settle
initialize(new Promise((resolve, reject) => { settle = name === 'ready-success' ? resolve : reject }))
fetchOne()
assert.equal(run('readyWaiters.size'), 1, 'bootstrap waiter registered')
settle(name === 'ready-error' ? Error('init failed') : undefined)
await until(idle, 'settled initialization cleans fetch')
assert.equal(run('readyWaiters.size'), 0, 'settled initialization releases waiters')
if (name === 'ready-error') {
assert.equal(lines.length, 1)
assert.equal(lines[0].error.message, 'init failed')
} else {
assert.deepEqual(lines.map(msg => msg.event || 'result'), ['head', 'result'])
}
} else if (name === 'mid-setup') {
for (const stage of ['options', 'info']) {
let release
let entered = false
let vendorCalls = 0
const barrier = new Promise(resolve => { release = resolve })
context.options = async () => {
if (stage === 'options') { entered = true; await barrier }
return { fetch: async () => { vendorCalls++; return context.reply } }
}
context.info = async () => {
if (stage === 'info') { entered = true; await barrier }
return { models: {} }
}
fetchOne()
await until(() => entered, 'plugin setup entered its deferred await')
assert.equal(run('inflight.get(1).state'), 'setup')
abortOne()
assert.equal(run('inflight.get(1).state'), 'cancelled', 'JS processed abort before setup resumes')
assert.equal(run('inflight.get(1).controller.signal.aborted'), true)
release()
await until(idle, 'cancelled setup finishes')
assert.equal(vendorCalls, 0, 'cancelled setup never starts vendor')
context.options = async () => ({ fetch: async () => { vendorCalls++; return context.reply } })
context.info = async () => ({ models: {} })
fetchOne()
await until(idle, 'healthy fetch after cancelled setup')
assert.equal(vendorCalls, 1)
}
assert.deepEqual(lines.map(msg => msg.event || 'result'), ['head', 'result', 'head', 'result'])
} else if (name === 'local-error') {
context.options = async () => {
run('inflight.get(1).controller.abort()')
throw Error('local protocol failure')
}
fetchOne()
await until(idle, 'local error settles')
assert.equal(lines.length, 1)
assert.equal(lines[0].error.message, 'local protocol failure', 'local abort does not suppress error owed to Go')
} else if (name === 'local-error-cancel') {
blockOn = 'all'
run('send({ id: 99, result: null })')
context.options = async () => {
run('inflight.get(1).controller.abort()')
throw Error('local protocol failure')
}
fetchOne()
await until(() => queued() === 1, 'local error queued after local abort')
abortOne()
await until(idle, 'host cancel ends error wait even after local signal aborted')
assert.equal(queued(), 0, 'late error removed after host cancel')
} else if (name === 'held-window') {
// the reader credits nothing back, so the fetch spends its whole window
// and waits in gate.take; a cancel must wake that wait, or the fetch
// never finishes
context.reply.body = (async function* () { yield Buffer.alloc(1 << 20) })()
fetchOne()
const chunks = () => lines.filter(msg => msg.event === 'chunk')
const spent = () => chunks().reduce((n, msg) => n + Buffer.from(msg.data, 'base64').length + run('FRAME_OVERHEAD'), 0)
await until(() => spent() === 512 << 10, 'fetch spends its whole window')
const held = chunks().length
for (let iteration = 0; iteration < 5; iteration++) await tick()
assert.equal(chunks().length, held, 'no frame past the window')
assert.equal(idle(), false, 'fetch waits for credit')
abortOne()
await until(idle, 'cancel wakes a fetch waiting for credit')
assert.equal(lines.filter(msg => msg.id === 1 && ('result' in msg || 'error' in msg)).length, 0)
} else if (name === 'normal-reply') {
context.reply.body = (async function* () { yield Buffer.from('complete reply') })()
fetchOne()
await until(idle, 'normal reply settles')
assert.deepEqual(lines.map(msg => msg.event || 'result'), ['head', 'chunk', 'result'])
assert.equal(Buffer.from(lines[1].data, 'base64').toString(), 'complete reply')
assert.equal(lines[2].result, null)
} else {
throw Error('unknown test case')
}
}
// node exits 0 once nothing is left to run, even while main() still awaits a
// promise that never settles, so a case passes only by reaching its end
let finished = false
process.on('exit', code => {
if (finished || code !== 0) return
process.exitCode = 1
console.error(process.argv[1] + ' never finished: main() was still awaiting a promise that never settled when node ran out of work')
})
main().then(() => { finished = true }, err => { console.error(err); process.exitCode = 1 })
`
// fakeSignedIn starts a real Bun host with the fake plugin signed in to, its
// requests going to base, and gives the plugin's base URL.
func fakeSignedIn(t *testing.T, base string) string {
t.Helper()
sandbox(t)
t.Setenv("FAKE_BASE", base)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
t.Cleanup(cancel)
abs, _ := filepath.Abs("testdata/fake/index.js")
if _, err := Add(ctx, abs); err != nil {
t.Fatal(err)
}
if _, err := Providers(ctx); err != nil {
t.Fatal(err)
}
if _, err := APIKey(ctx, "fakeco", 0, nil, "k1", NewAccount); err != nil {
t.Fatal(err)
}
o, err := LoaderOptions(ctx, "fakeco", "")
if err != nil {
t.Fatal(err)
}
return o.BaseURL
}
// TestFlowSlowReaderGetsTheWholeReply is the flow control's point: a reader
// slower than the vendor still gets every byte — credit pauses the upstream
// rather than the host cutting the reply at a fixed size.
func TestFlowSlowReaderGetsTheWholeReply(t *testing.T) {
const size = 4 << 20
payload := bytes.Repeat([]byte("0123456789abcdef"), size/16)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
for off := 0; off < len(payload); off += 64 << 10 {
end := min(off+(64<<10), len(payload))
_, _ = w.Write(payload[off:end])
w.(http.Flusher).Flush()
}
}))
defer srv.Close()
base := fakeSignedIn(t, srv.URL+"/v1")
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
res, err := Fetch(ctx, FetchRequest{
Provider: "fakeco", Model: "fake-1", NPM: "@ai-sdk/openai-compatible",
URL: base + "/big", Method: "POST", Body: []byte(`{}`),
})
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
// read slower than the vendor writes, so the credit window must pause it
// and let it go again rather than the reply being cut
buf := make([]byte, 64<<10)
var got []byte
for {
n, err := res.Body.Read(buf)
if n > 0 {
got = append(got, buf[:n]...)
time.Sleep(time.Millisecond)
}
if err == io.EOF {
break
}
if err != nil {
t.Fatalf("read at %d bytes: %v", len(got), err)
}
}
if !bytes.Equal(got, payload) {
t.Fatalf("read %d bytes, want %d", len(got), len(payload))
}
}
// TestFlowCancelAfterHeadStopsTheVendor: a request cancelled after its head,
// with nothing reading or closing the body, still stops the upstream fetch —
// the vendor's own request is cancelled.
func TestFlowCancelAfterHeadStopsTheVendor(t *testing.T) {
hit := make(chan struct{})
gone := make(chan struct{})
var once sync.Once
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/event-stream")
w.WriteHeader(200)
w.(http.Flusher).Flush()
once.Do(func() { close(hit) })
<-r.Context().Done()
close(gone)
}))
defer srv.Close()
base := fakeSignedIn(t, srv.URL+"/v1")
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
rctx, rcancel := context.WithCancel(ctx)
res, err := Fetch(rctx, FetchRequest{
Provider: "fakeco", Model: "fake-1", NPM: "@ai-sdk/openai-compatible",
URL: base + "/hang", Method: "POST", Body: []byte(`{}`),
})
if err != nil {
t.Fatal(err)
}
<-hit // the vendor is in the request, its head has reached magpie
rcancel() // nothing reads or closes the body
select {
case <-gone:
case <-time.After(10 * time.Second):
t.Fatal("cancelling the request did not stop the vendor")
}
_ = res
}
// TestFlowTinyFramesStillArrive: a vendor that yields tiny pieces, read slowly,
// still gets every byte. The frame overhead pauses such a producer — it can
// keep at most window/(payload+overhead) frames queued — rather than the host
// cutting it, and the reader gets the whole reply.
func TestFlowTinyFramesStillArrive(t *testing.T) {
const size = 4096
payload := bytes.Repeat([]byte("abcdefgh"), size/8)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
fl := w.(http.Flusher)
for i := range payload {
_, _ = w.Write(payload[i : i+1])
if i%16 == 0 {
fl.Flush()
}
}
}))
defer srv.Close()
base := fakeSignedIn(t, srv.URL+"/v1")
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
res, err := Fetch(ctx, FetchRequest{
Provider: "fakeco", Model: "fake-1", NPM: "@ai-sdk/openai-compatible",
URL: base + "/tiny", Method: "POST", Body: []byte(`{}`),
})
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
buf := make([]byte, 512)
var got []byte
for {
n, err := res.Body.Read(buf)
if n > 0 {
got = append(got, buf[:n]...)
time.Sleep(time.Millisecond)
}
if err == io.EOF {
break
}
if err != nil {
t.Fatalf("read at %d bytes: %v", len(got), err)
}
}
if !bytes.Equal(got, payload) {
t.Fatalf("read %d bytes, want %d", len(got), len(payload))
}
}
// bigPiece is one piece of the production-scale stream: the 64 KiB block, or
// the same block as an SSE data event.
func bigPiece(block []byte, sse bool) []byte {
if !sse {
return block
}
return []byte("data: " + base64.StdEncoding.EncodeToString(block) + "\n\n")
}
// writeBig writes about total bytes of the pattern to w (nil to only measure),
// giving the exact length and its SHA-256. The vendor and the check build the
// same bytes, so a truncation anywhere shows up as a length or hash mismatch.
func writeBig(w io.Writer, total int, sse bool, block []byte) (int64, [32]byte) {
h := sha256.New()
var n int64
for n < int64(total) {
piece := bigPiece(block, sse)
if w != nil {
if _, err := w.Write(piece); err != nil {
break
}
}
h.Write(piece)
n += int64(len(piece))
}
var sum [32]byte
copy(sum[:], h.Sum(nil))
return n, sum
}
// TestFlowBigReplyProd is the production-scale check: 100 MiB of a non-SSE body
// and of an SSE stream, each read through the real host and hashed; an SSE
// stream a reader pauses 12 s in the middle of and then finishes. It is behind
// MAGPIE_FLOW_BIG because each case moves 100 MiB, and it should be run against
// the Bun magpie itself picks (MAGPIE_BUN set to it).
func TestFlowBigReplyProd(t *testing.T) {
if os.Getenv("MAGPIE_FLOW_BIG") == "" {
t.Skip("set MAGPIE_FLOW_BIG=1 to run the 100 MiB flow cases")
}
const total = 100 << 20
block := make([]byte, 64<<10)
for i := range block {
block[i] = byte(i * 7)
}
for _, tc := range []struct {
name string
sse bool
pause time.Duration
}{
{"non-sse", false, 0},
{"sse", true, 0},
{"sse-paused", true, 12 * time.Second},
} {
t.Run(tc.name, func(t *testing.T) {
ctype := "application/json"
if tc.sse {
ctype = "text/event-stream"
}
// how far the vendor has got, and whether it finished, so a pause
// the reader takes shows whether the upstream was held back too
var written atomic.Int64
finished := make(chan struct{})
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer close(finished)
w.Header().Set("Content-Type", ctype)
w.WriteHeader(200)
fl := w.(http.Flusher)
var n int64
for n < int64(total) {
piece := bigPiece(block, tc.sse)
m, err := w.Write(piece)
written.Add(int64(m))
if err != nil {
return
}
n += int64(len(piece))
if n%(8<<20) < int64(len(piece)) {
fl.Flush()
}
}
}))
defer srv.Close()
base := fakeSignedIn(t, srv.URL+"/v1")
wantLen, wantSum := writeBig(nil, total, tc.sse, block)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
res, err := Fetch(ctx, FetchRequest{
Provider: "fakeco", Model: "fake-1", NPM: "@ai-sdk/openai-compatible",
URL: base + "/big", Method: "POST", Body: []byte(`{}`),
})
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
h := sha256.New()
var n int64
paused := false
var pausedAt, resumedAt, pauseOwed, pauseWindow int64
var finishedBeforeResume, windowExhausted bool
buf := make([]byte, 256<<10)
for {
m, err := res.Body.Read(buf)
if m > 0 {
h.Write(buf[:m])
n += int64(m)
if tc.pause > 0 && !paused && n >= 1<<20 {
paused = true
pausedAt = written.Load()
t.Logf("pause_start: delivered=%d producer_written=%d expected=%d", n, pausedAt, wantLen)
// Reflection keeps this public-API fixture compilable with
// the old host.go overlay, which has no credit counters.
b := res.Body.(*body)
pauseEnd := time.Now().Add(tc.pause)
for time.Now().Before(pauseEnd) {
b.c.mu.Lock()
fields := reflect.ValueOf(b.c).Elem()
owed, window := fields.FieldByName("owed"), fields.FieldByName("window")
if owed.IsValid() && window.IsValid() {
pauseOwed, pauseWindow = owed.Int(), window.Int()
windowExhausted = pauseWindow > 0 && pauseWindow-pauseOwed <= 2048
}
b.c.mu.Unlock()
if windowExhausted {
time.Sleep(time.Until(pauseEnd))
break
}
time.Sleep(10 * time.Millisecond)
}
resumedAt = written.Load()
select {
case <-finished:
finishedBeforeResume = true
default:
}
t.Logf("pause_end_before_read: producer_written=%d expected=%d finished=%t window_exhausted=%t owed=%d window=%d", resumedAt, wantLen, finishedBeforeResume, windowExhausted, pauseOwed, pauseWindow)
}
}
if err == io.EOF {
break
}
if err != nil {
t.Fatalf("read at %d bytes: %v", n, err)
}
}
got := h.Sum(nil)
if n != wantLen {
t.Fatalf("read %d bytes, want %d", n, wantLen)
}
if !bytes.Equal(got, wantSum[:]) {
t.Fatalf("sha256 = %x, want %x", got, wantSum)
}
if tc.pause > 0 {
// Preserve the original length/hash failure as the negative
// control; only these new producer assertions are optional.
if os.Getenv("MAGPIE_FLOW_ORIGINAL_BEHAVIOR_ONLY") != "1" {
if !windowExhausted {
t.Error("the paused reader did not exhaust the Go credit window")
}
if finishedBeforeResume || resumedAt >= wantLen {
t.Errorf("producer finished before resume: written=%d expected=%d finished=%t", resumedAt, wantLen, finishedBeforeResume)
}
}
select {
case <-finished:
case <-time.After(5 * time.Second):
t.Fatal("producer did not finish after resume")
}
t.Logf("producer_after_resume: written=%d expected=%d finished=true", written.Load(), wantLen)
}
t.Logf("%s: %d bytes, sha256 %x", tc.name, n, got)
})
}
}
// TestFlowAbortDuringSetupStaysHealthy: many fetches cancelled as soon as they
// are made — while the plugin is still setting them up, or waiting for a slow
// vendor's head — leave the host able to answer a later request. It is the Go
// side of the lifecycle: the host's slots come back, and the JS side's own
// finally (covered by construction) lets each request's controller and gate go.
func TestFlowAbortDuringSetupStaysHealthy(t *testing.T) {
node, err := exec.LookPath("node")
if err != nil {
t.Skip("no node on PATH")
}
if out, err := runHostJSCase(t, node, "mid-setup"); err != nil {
t.Fatalf("mid-setup cancellation: %v\n%s", err, out)
}
}
func TestFlowEarlyCancellationStressStaysHealthy(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
select {
case <-time.After(300 * time.Millisecond):
case <-r.Context().Done():
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
_, _ = w.Write([]byte(`{"ok":true}`))
}))
defer srv.Close()
base := fakeSignedIn(t, srv.URL+"/v1")
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
r := FetchRequest{
Provider: "fakeco", Model: "fake-1", NPM: "@ai-sdk/openai-compatible",
URL: base + "/slow", Method: "POST", Body: []byte(`{}`),
}
for i := range 20 {
fctx, fcancel := context.WithCancel(ctx)
done := make(chan struct{})
go func() {
defer close(done)
res, err := Fetch(fctx, r)
if err == nil {
_, _ = io.Copy(io.Discard, res.Body)
res.Body.Close()
}
}()
fcancel() // as early as possible: setup, or the head wait
select {
case <-done:
case <-time.After(10 * time.Second):
t.Fatalf("fetch %d did not return after cancel", i)
}
}
// the host still answers a request that is not cancelled
res, err := Fetch(ctx, r)
if err != nil {
t.Fatalf("a fetch after the cancelled ones: %v", err)
}
defer res.Body.Close()
if body, err := io.ReadAll(res.Body); err != nil || !bytes.Contains(body, []byte(`"ok":true`)) {
t.Fatalf("body = %q, %v", body, err)
}
}
// TestFlowHeadEnvelopeRejectsHugeHeaders: a vendor that answers with more
// headers than magpie's head envelope allows fails the fetch cleanly rather
// than the host taking an unbounded zero-charge head line.
func TestFlowHeadEnvelopeRejectsHugeHeaders(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Big", strings.Repeat("v", 96<<10)) // past the 64 KiB envelope
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
_, _ = w.Write([]byte(`{"ok":true}`))
}))
defer srv.Close()
base := fakeSignedIn(t, srv.URL+"/v1")
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
_, err := Fetch(ctx, FetchRequest{
Provider: "fakeco", Model: "fake-1", NPM: "@ai-sdk/openai-compatible",
URL: base + "/bighead", Method: "POST", Body: []byte(`{}`),
})
if err == nil {
t.Fatal("a reply with an oversized head was accepted")
}
}