Files
plugins/packages/page-harnessweight/lib/measure.mjs
T
webkuborandwebkubor 186b642f5a page-harnessweight 0.4.0: weigh each agent harness behind the gateway (#922)
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>
2026-11-10 17:37:33 +08:00

66 lines
2.3 KiB
JavaScript

// Tool-declaration measurement, shared by the hook and the tests.
// The size of a tool declaration, per protocol. magpie speaks four shapes
// to four families of models, and the tools live in a different place in
// each:
//
// anthropic (/v1/messages): tools: [{ name, description, input_schema }]
// chat (/v1/chat/completions): tools: [{ type, function: {...} }]
// responses (/v1/responses): tools: [{ type, name, parameters }]
// gemini (…/streamGenerateContent): tools: [{ functionDeclarations: [...] }]
//
// The numbers that matter for a cost comparison are the count and the
// bytes: the bytes are what the model reads again on every turn.
export function measureTools(body) {
const out = { count: 0, bytes: 0, names: [] }
if (!body || typeof body !== "object") return out
const tools = Array.isArray(body.tools) ? body.tools : null
if (!tools) return out
for (const t of tools) {
if (!t || typeof t !== "object") continue
// A namespace's tools are its own tools (OpenAI responses).
if (Array.isArray(t.tools)) {
const inner = measureTools({ tools: t.tools })
out.count += inner.count
out.bytes += inner.bytes
out.names.push(...inner.names)
continue
}
// Gemini puts them one level deeper, under functionDeclarations.
if (Array.isArray(t.functionDeclarations)) {
for (const fd of t.functionDeclarations) {
out.count++
out.bytes += sizeOf(fd)
if (fd && typeof fd.name === "string") out.names.push(fd.name)
}
continue
}
// OpenAI chat wraps them in a function object.
if (t.function && typeof t.function === "object") {
out.count++
out.bytes += sizeOf(t.function)
if (typeof t.function.name === "string") out.names.push(t.function.name)
continue
}
// Anthropic and OpenAI responses keep them flat.
out.count++
out.bytes += sizeOf(t)
if (typeof t.name === "string") out.names.push(t.name)
}
return out
}
// The size of one declaration, in bytes of JSON. This is a proxy for the
// tokens the model reads, not a token count: JSON is not BPE, and no
// tokenizer is exact for every provider. It is the same measure for every
// agent, which is what a comparison needs.
export function sizeOf(value) {
try {
return Buffer.byteLength(JSON.stringify(value) ?? "", "utf8")
} catch {
return 0
}
}