A page plugin that compares the agent harnesses behind magpie's gateway by agent: input tokens, cache hits, failures, latency and estimated tool-definition size. A local service on 127.0.0.1:3496 reads magpie's usage ledger, and a read-only gateway hook writes tool counts, sizes and names to <config>/harnessweight/tools.jsonl. Since the review at e0a0ae97 (7c6cd626a, then main merged in): - Port 3492 -> 3496 (window-warmup has 3492; 3496 is used by no other package). - Config dir: input.directory, then a cwd probe, then an absolute XDG_CONFIG_HOME, then ~/.config/magpie. A relative XDG is ignored. - tools.jsonl is append-only. Past 8 MiB it is renamed to tools.jsonl.1. Age is checked at most hourly. Appends from two hosts are serialized by a cross-process lock that is skipped after 100 ms. - MAGPIE_ADDR is honoured; the UI has English and Chinese (zh-TW and ja read English); preview PNGs dropped; /%E0 returns 400. Run at 961b20e63c7e00e1dcca723bc2aa1d302f5dd9a3, in WSL (magpie-desktop, Node 22.23.4): - node --test test/*.test.mjs in the package: 60 pass, 0 fail. - bun scripts/check.mjs: clean (registry.json ok; the package has hooks, so check skips it). - Probes against createHandler: - Origin null without the token: 403. With the token: 200. - A foreign Origin, a loopback Origin on another port, a file:// Origin, a rebound Host and a localhost Host on another port: all 403. - The index HTML carries the token and no ACAO. - ..%2f: 400. /%E0: 400. - Rollover with 40-tool rows (1306 B each), 9.5 MiB written: the file peaked at 8,388,590 B (cap 8,388,608) with one rename to .1. Append p50 was 1.35 ms, p99 9.2 ms and max 24 ms. - Portable dir: the hook's cwd probe and the service's input.directory gave the same tools.jsonl. - Broken by hand: - Size rotation turned off: two rollover tests went red. - The Host check removed: the service test "other hosts, other origins and path traversal are refused" went red. Co-authored-by: webkubor <29485642+webkubor@users.noreply.github.com>
81 lines
4.0 KiB
JavaScript
81 lines
4.0 KiB
JavaScript
import { test } from "node:test"
|
|
import assert from "node:assert/strict"
|
|
import { createServer } from "node:http"
|
|
import { mkdtemp, readFile, readdir, writeFile } from "node:fs/promises"
|
|
import { tmpdir } from "node:os"
|
|
import { join } from "node:path"
|
|
import { createHandler, TOKEN_HEADER } from "../service.mjs"
|
|
|
|
async function start() {
|
|
const dir = await mkdtemp(join(tmpdir(), "al-svc-"))
|
|
const usageLog = join(dir, "usage.jsonl")
|
|
await writeFile(usageLog, JSON.stringify({ t: new Date().toISOString(), agent: "dsh", model: "m", status: 200, in: 1, cache_read: 9, out: 2 }) + "\n")
|
|
const srv = createServer()
|
|
await new Promise((r) => srv.listen(0, "127.0.0.1", r))
|
|
const port = srv.address().port
|
|
srv.on("request", createHandler({ port, usageLog, dataDir: dir, token: "t".repeat(64), gatewayUrl: "http://127.0.0.1:9" }))
|
|
return { port, close: () => srv.close() }
|
|
}
|
|
const get = (port, path, headers = {}) => fetch(`http://127.0.0.1:${port}${path}`, { headers })
|
|
|
|
test("api needs the token; the token is only in the page HTML, which has no CORS header", async () => {
|
|
const s = await start()
|
|
try {
|
|
assert.equal((await get(s.port, "/api/summary")).status, 403)
|
|
assert.equal((await get(s.port, "/api/summary", { [TOKEN_HEADER]: "wrong" })).status, 403)
|
|
const ok = await get(s.port, "/api/summary", { [TOKEN_HEADER]: "t".repeat(64), Origin: "null" })
|
|
assert.equal(ok.status, 200)
|
|
assert.equal((await ok.json()).agents[0].agent, "dsh")
|
|
assert.equal((await get(s.port, `/api/summary?k=${"t".repeat(64)}`, { Origin: "null" })).status, 200)
|
|
const page = await get(s.port, "/", { Origin: "null" })
|
|
assert.equal(page.headers.get("access-control-allow-origin"), null)
|
|
assert.match(await page.text(), /name="harnessweight-token" content="t{64}"/)
|
|
const health = await (await get(s.port, "/api/health")).json()
|
|
assert.deepEqual(Object.keys(health).sort(), ["ok", "service"])
|
|
} finally { s.close() }
|
|
})
|
|
|
|
test("other hosts, other origins and path traversal are refused", async () => {
|
|
const s = await start()
|
|
try {
|
|
const h = { [TOKEN_HEADER]: "t".repeat(64) }
|
|
assert.equal((await get(s.port, "/api/summary", { ...h, Origin: "https://evil.example" })).status, 403)
|
|
// DNS rebinding: same socket, foreign Host
|
|
const r = await new Promise((resolve) => {
|
|
import("node:http").then(({ request }) => {
|
|
const q = request({ host: "127.0.0.1", port: s.port, path: "/api/summary", headers: { ...h, Host: "evil.example" } }, resolve)
|
|
q.end()
|
|
})
|
|
})
|
|
assert.equal(r.statusCode, 403)
|
|
const t = await new Promise((resolve) => {
|
|
import("node:http").then(({ request }) => {
|
|
const q = request({ host: "127.0.0.1", port: s.port, path: "/../service.mjs" }, resolve)
|
|
q.end()
|
|
})
|
|
})
|
|
assert.notEqual(t.statusCode, 200)
|
|
assert.equal((await get(s.port, '/%E0')).status, 400)
|
|
} finally { s.close() }
|
|
})
|
|
|
|
test("the service, page bridge and defaults agree on the community port", async () => {
|
|
const pkg = JSON.parse(await readFile(new URL("../package.json", import.meta.url), "utf8"))
|
|
const boot = await readFile(new URL("../page/boot.js", import.meta.url), "utf8")
|
|
const server = await readFile(new URL("../service.mjs", import.meta.url), "utf8")
|
|
assert.equal(pkg.magpie.page.service.port, 3496)
|
|
assert.equal(pkg.magpie.options.port, 3496)
|
|
assert.match(boot, /let port = 3496\b/)
|
|
assert.match(server, /Number\(options\.port\) > 0 \? Number\(options\.port\) : 3496/)
|
|
})
|
|
|
|
test("the page in magpie's frame reaches no network and declares no reads", async () => {
|
|
const pkg = JSON.parse(await readFile(new URL("../package.json", import.meta.url), "utf8"))
|
|
assert.deepEqual(pkg.magpie.permissions, [])
|
|
const dir = new URL("../page/", import.meta.url)
|
|
for (const f of (await readdir(dir)).filter((f) => /\.(js|html)$/.test(f))) {
|
|
const src = await readFile(new URL(f, dir), "utf8")
|
|
assert.doesNotMatch(src, /\bfetch\s*\(|XMLHttpRequest|WebSocket|EventSource|sendBeacon|magpie\.read\s*\(/, f)
|
|
}
|
|
})
|