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>
164 lines
6.9 KiB
JavaScript
164 lines
6.9 KiB
JavaScript
// The migration of the legacy agent-lens samples into harnessweight is done
|
|
// on the way to the path, so two hosts (the hook and the service) can reach
|
|
// it at the same time. These tests pin the outcome of that race: one path,
|
|
// and no sample left behind.
|
|
import { test } from "node:test"
|
|
import assert from "node:assert/strict"
|
|
import { mkdtempSync, mkdirSync, writeFileSync, existsSync, readFileSync, chmodSync, realpathSync, rmSync, readdirSync, symlinkSync } from "node:fs"
|
|
import { tmpdir } from "node:os"
|
|
import { join, dirname, basename } from "node:path"
|
|
import { spawn } from "node:child_process"
|
|
import { magpieDir, toolsLog, usageLog } from "../lib/paths.mjs"
|
|
|
|
const PATHS_URL = new URL("../lib/paths.mjs", import.meta.url).href
|
|
|
|
function sandbox() {
|
|
const dir = realpathSync(mkdtempSync(join(tmpdir(), "hw-migration-")))
|
|
return dir
|
|
}
|
|
|
|
// A config dir that still holds the legacy samples.
|
|
function legacy(base, rows = [{ t: Date.now(), agent: "dsh", id: "legacy" }]) {
|
|
writeFileSync(join(base, "plugins.json"), "{}")
|
|
mkdirSync(join(base, "agent-lens"))
|
|
writeFileSync(join(base, "agent-lens", "tools.jsonl"), rows.map((r) => JSON.stringify(r) + "\n").join(""))
|
|
return base
|
|
}
|
|
|
|
function cleanup(dir) {
|
|
try { chmodSync(dir, 0o755) } catch { /* already gone */ }
|
|
rmSync(dir, { recursive: true, force: true })
|
|
}
|
|
|
|
test("a host that already migrated is never sent back to the legacy directory", () => {
|
|
const base = sandbox()
|
|
try {
|
|
// The other host won the race: the new directory is there, the old one
|
|
// is gone.
|
|
mkdirSync(join(base, "harnessweight"))
|
|
writeFileSync(join(base, "harnessweight", "tools.jsonl"), "")
|
|
assert.equal(toolsLog({ directory: base }), join(base, "harnessweight", "tools.jsonl"))
|
|
} finally {
|
|
cleanup(base)
|
|
}
|
|
})
|
|
|
|
test("both directories present keeps the new one and leaves the legacy samples alone", () => {
|
|
const base = sandbox()
|
|
try {
|
|
legacy(base)
|
|
mkdirSync(join(base, "harnessweight"))
|
|
writeFileSync(join(base, "harnessweight", "tools.jsonl"), "")
|
|
assert.equal(toolsLog({ directory: base }), join(base, "harnessweight", "tools.jsonl"))
|
|
// The legacy samples are untouched, not moved and not deleted.
|
|
assert.equal(existsSync(join(base, "agent-lens", "tools.jsonl")), true)
|
|
} finally {
|
|
cleanup(base)
|
|
}
|
|
})
|
|
|
|
test("a migration that cannot happen keeps the legacy directory usable", { skip: process.getuid?.() === 0 ? "root ignores directory permissions" : false }, () => {
|
|
const base = sandbox()
|
|
try {
|
|
legacy(base)
|
|
chmodSync(base, 0o555)
|
|
// The rename cannot succeed here, so the samples must stay readable
|
|
// where they are instead of pointing at a directory that never appears.
|
|
assert.equal(toolsLog({ directory: base }), join(base, "agent-lens", "tools.jsonl"))
|
|
assert.equal(existsSync(join(base, "agent-lens", "tools.jsonl")), true)
|
|
} finally {
|
|
cleanup(base)
|
|
}
|
|
})
|
|
|
|
// Hosts that ask for the path at the same time must get the same answer.
|
|
// The loser of the rename is the interesting one: the samples are already
|
|
// moved, so answering with the legacy path would hide them.
|
|
test("concurrent hosts agree on one path and every sample survives", async () => {
|
|
const base = sandbox()
|
|
try {
|
|
legacy(base, Array.from({ length: 50 }, (_, i) => ({ t: Date.now(), agent: "dsh", id: "row-" + i })))
|
|
const workers = 8
|
|
mkdirSync(join(base, "ready"))
|
|
const child = `
|
|
import { writeFileSync, existsSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
import { toolsLog } from ${JSON.stringify(PATHS_URL)};
|
|
const base = ${JSON.stringify(base)};
|
|
writeFileSync(join(base, "ready", ${JSON.stringify("x")} + process.argv[1]), "");
|
|
while (!existsSync(join(base, "go"))) await new Promise((r) => setTimeout(r, 2));
|
|
process.stdout.write(toolsLog({ directory: base }));
|
|
`
|
|
const runs = Array.from({ length: workers }, (_, i) => new Promise((resolve, reject) => {
|
|
const child1 = spawn(process.execPath, ["--input-type=module", "-e", child, String(i)], { encoding: "utf8" })
|
|
let out = ""
|
|
let err = ""
|
|
child1.stdout.on("data", (c) => { out += c })
|
|
child1.stderr.on("data", (c) => { err += c })
|
|
child1.on("error", reject)
|
|
child1.on("close", (code) => resolve({ code, out, err }))
|
|
}))
|
|
// Release them together.
|
|
const deadline = Date.now() + 10000
|
|
while (existsSync(join(base, "ready")) && Date.now() < deadline) {
|
|
const ready = readdirSync(join(base, "ready"))
|
|
if (ready.length >= workers) break
|
|
await new Promise((r) => setTimeout(r, 10))
|
|
}
|
|
writeFileSync(join(base, "go"), "")
|
|
const results = await Promise.all(runs)
|
|
for (const r of results) assert.equal(r.code, 0, r.err)
|
|
const paths = new Set(results.map((r) => r.out))
|
|
assert.equal(paths.size, 1, "every host must name the same file: " + [...paths].join(", "))
|
|
const expected = join(base, "harnessweight", "tools.jsonl")
|
|
assert.equal([...paths][0], expected)
|
|
// The migration happened exactly once and the samples are all there.
|
|
assert.equal(existsSync(join(base, "agent-lens")), false)
|
|
const rows = readFileSync(expected, "utf8").trim().split("\n").map((l) => JSON.parse(l))
|
|
assert.equal(rows.length, 50)
|
|
assert.equal(new Set(rows.map((r) => r.id)).size, 50)
|
|
} finally {
|
|
cleanup(base)
|
|
}
|
|
})
|
|
|
|
test("hook and service agree on the path in every branch", () => {
|
|
const base = sandbox()
|
|
try {
|
|
legacy(base)
|
|
// The host names the directory (service) and runs the hook with the
|
|
// same directory as its working directory (hook). Both must land on
|
|
// the same file, whichever spelling they use.
|
|
const fromService = toolsLog({ directory: base })
|
|
const fromHook = toolsLog({ directory: join(base, "..", base.split("/").pop()) })
|
|
assert.equal(fromService, fromHook)
|
|
assert.equal(fromService, join(base, "harnessweight", "tools.jsonl"))
|
|
// The usage log follows the same directory.
|
|
assert.equal(usageLog({ directory: base }), join(base, "usage.jsonl"))
|
|
// An explicit data directory is used as given, by both sides.
|
|
const data = join(base, "samples")
|
|
mkdirSync(data)
|
|
assert.equal(toolsLog({ directory: base, dataDir: data }), join(data, "tools.jsonl"))
|
|
assert.equal(usageLog({ directory: base, usageLog: join(base, "u.jsonl") }), join(base, "u.jsonl"))
|
|
} finally {
|
|
cleanup(base)
|
|
}
|
|
})
|
|
|
|
test("a symlinked config dir is named the same way by both hosts", () => {
|
|
const base = sandbox()
|
|
const linkPath = join(dirname(base), "hw-migration-link-" + basename(base))
|
|
try {
|
|
legacy(base)
|
|
symlinkSync(base, linkPath)
|
|
const real = realpathSync(base)
|
|
// The service is told the symlinked path, the hook sees the real one.
|
|
assert.equal(toolsLog({ directory: linkPath }), join(real, "harnessweight", "tools.jsonl"))
|
|
assert.equal(toolsLog({ directory: real }), join(real, "harnessweight", "tools.jsonl"))
|
|
assert.equal(magpieDir({ directory: linkPath }), real)
|
|
} finally {
|
|
rmSync(linkPath, { force: true })
|
|
cleanup(base)
|
|
}
|
|
})
|