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>
65 lines
2.7 KiB
JavaScript
65 lines
2.7 KiB
JavaScript
// Where HarnessWeight reads and writes. Both the hook and the service resolve
|
|
// paths here, so they always agree.
|
|
import { homedir } from "node:os"
|
|
import { existsSync, realpathSync, renameSync } from "node:fs"
|
|
import { dirname, isAbsolute, join, parse, resolve } from "node:path"
|
|
import { fileURLToPath } from "node:url"
|
|
|
|
const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "..")
|
|
|
|
// One directory, one string. The host may name the config dir through
|
|
// input.directory while the hook sees it as its cwd; a symlinked path
|
|
// (/var -> /private/var on macOS) would otherwise give the two different
|
|
// spellings of the same disk directory. Resolve symlinks as far as the
|
|
// directory exists, so both sides name it identically.
|
|
function canonical(dir) {
|
|
const path = resolve(dir)
|
|
try {
|
|
return realpathSync(path)
|
|
} catch { /* not created yet: canonicalize the nearest existing parent */ }
|
|
const parts = []
|
|
let current = path
|
|
for (;;) {
|
|
const parent = dirname(current)
|
|
if (parent === current) return path
|
|
parts.unshift(parse(current).base)
|
|
try {
|
|
return join(realpathSync(parent), ...parts)
|
|
} catch { /* keep walking up */ }
|
|
current = parent
|
|
}
|
|
}
|
|
|
|
// The host passes its config directory to server() and also starts the hook
|
|
// there. Probe cwd for the hook; standalone launches fall back to XDG/home.
|
|
export function magpieDir(options = {}) {
|
|
if (options.directory) return canonical(options.directory)
|
|
try {
|
|
const cwd = process.cwd()
|
|
if (cwd !== ROOT && ["usage.jsonl", "plugins.json", "providers.json"].some((f) => existsSync(join(cwd, f)))) return canonical(cwd)
|
|
} catch { /* cwd may have been removed */ }
|
|
const xdg = process.env.XDG_CONFIG_HOME
|
|
return canonical(xdg && isAbsolute(xdg) ? join(xdg, "magpie") : join(homedir(), ".config", "magpie"))
|
|
}
|
|
|
|
// magpie's own usage ledger: one JSON line per gateway request.
|
|
export function usageLog(options = {}) {
|
|
return options.usageLog ? resolve(options.usageLog) : join(magpieDir(options), "usage.jsonl")
|
|
}
|
|
|
|
// The tool-weight samples the hook writes. Kept outside the plugin's folder
|
|
// so a reinstall or an update does not wipe them.
|
|
export function toolsLog(options = {}) {
|
|
if (options.dataDir) return join(canonical(options.dataDir), "tools.jsonl")
|
|
const dir = join(magpieDir(options), "harnessweight")
|
|
// the plugin was called Agent Lens until 0.4.0: carry its samples over once
|
|
const old = join(magpieDir(options), "agent-lens")
|
|
if (!existsSync(dir) && existsSync(old)) {
|
|
try { renameSync(old, dir) } catch {
|
|
// Another host may have completed the same migration concurrently.
|
|
if (!existsSync(dir) && existsSync(old)) return join(old, "tools.jsonl")
|
|
}
|
|
}
|
|
return join(dir, "tools.jsonl")
|
|
}
|