Files
plugins/packages/fovea/benchmark.mjs
T
webkubor d4eeba4493 fovea 0.2.2: a new middleware, old history to head and tail on a cache-stable boundary (webkubor, #640)
A new gateway middleware, @magpie-community/middleware-fovea 0.2.2 (webkubor, #640): old tool output and text in a long history are cut to head and tail, with the boundary only on multiples of `cut_step` (24) so the cut prefix stays byte-identical between turns and the prompt cache holds. The leading user items before the first answer (Codex's AGENTS.md, environment_context and the task; Claude Code's first message) stay whole. `saccade` ("off" by default) drops the old context on a new topic, judged against that same task. Old images become a text note that keeps their `cache_control`. Ships an evidence page (Plugins > Fovea evidence) that computes a paste-in A/B in the frame, with no network.

What was run, at the reviewed head b9883cf206814fdc01950a00d413a36856de4e11, on mote (macOS, Bun 1.4.2, sandbox HOME):
- `bun test packages/fovea`: 33 pass, 0 fail. The two new Codex saccade cases fail with 0357f1c's middleware (and pass at the head).
- `bun scripts/check.mjs fovea` and `bun scripts/check.mjs`: pass. `page/measure.js` is byte-identical to a fresh `bun build.mjs`.
- `npm pack --dry-run`: 18 files, 131.8 kB unpacked; no brand/, no live-benchmark.mjs.
- The head merged onto origin/main 82b254f2 (registry.json auto-merged): `bun scripts/check.mjs` passes and `HOME=$(mktemp -d) bun test` (the whole repo, as pr.yml runs it): 5477 pass, 0 fail.
- A probe calling onRequest turn after turn. Codex-shaped Responses (instructions, developer permissions item, AGENTS.md, environment_context, the task as input_text, then reasoning with encrypted_content, function_call and function_call_output, 60 turns) and Claude Code-shaped Messages (system with cache_control, a first message with system-reminder, task and an image with cache_control, then thinking + signature, text, tool_use and a 6400-char tool_result with emoji, 70 turns). `instructions`, `system`, `tools`, every reasoning, function_call and assistant item, ids, counts and the cache_control count are unchanged; the four leading Codex items reach upstream byte-identical; no lone surrogate. The shared prefix breaks once per cut_step (Codex 6 of 60 turns, Messages 5 of 70; 0 without fovea), with `saccade: "auto"` too. With auto, a same-task follow-up on Codex ("scheduler retry timing assertion still fails in internal/sched, fix it") and on Messages collapses nothing; a new topic collapses from that turn on (one extra prefix break). Bodies went 16.1 MB to 9.4 MB (Codex) and 26.1 MB to 13.9 MB (Messages) over the run.
- Text the middleware adds upstream is only `[fovea: N chars of old tool output omitted]` and `[fovea: prior topic not carried]`: neither names magpie, a vendor or a limit. Codex's instructions are never touched.

Not tried: a live vendor request with real cache-hit numbers (the evidence is the byte-level prefix). pr.yml's runs were waiting on approval for a first-time contributor, so the CI record is the merged-tree run above. A new topic that shares a common word such as "the" with the task stays below the 0.08 overlap bar only when it has enough words; with saccade off by default that is a tuning note, not a blocker. @magpie-community/middleware-fovea is new to npm, so its first version needs an owner (trusted publishing can't create a package); magpie serves it from the dist branch meanwhile.
2026-11-04 16:56:37 +08:00

112 lines
4.8 KiB
JavaScript

// A reproducible, offline A/B on the same request. No prompt body is
// written to the report: only counts and prefix comparisons leave here.
//
// bun packages/fovea/benchmark.mjs
// bun packages/fovea/benchmark.mjs /path/to/request.json
// bun packages/fovea/benchmark.mjs /path/to/request.json '{"saccade":"auto"}'
//
// The report is read by this package's page. It is not a cost claim:
// actual provider input/cache tokens and cost need a controlled live A/B.
import { readFileSync, writeFileSync } from "node:fs"
import { dirname, join } from "node:path"
import { fileURLToPath } from "node:url"
import { onRequest } from "./fovea.middleware.js"
const HERE = dirname(fileURLToPath(import.meta.url))
const DEFAULTS = {
threshold: 0, keep_recent: 4, keep_recent_tokens: 0, cut_step: 4,
tool_head: 80, tool_tail: 40, text_head: 120, text_tail: 60,
saccade: "off",
}
const clone = (v) => JSON.parse(JSON.stringify(v))
const chars = (v) => JSON.stringify(v).length
function fixture(rounds = 16, question = "Finish the module and explain the result") {
const messages = [{ role: "user", content: "Implement a parser and check every test" }]
for (let n = 0; n < rounds; n++) {
messages.push({ role: "assistant", content: "Read the next file", tool_calls: [
{ id: `read_${n}`, type: "function", function: { name: "read_file", arguments: "{}" } },
] })
messages.push({ role: "tool", tool_call_id: `read_${n}`, content: `FILE ${n}\n` + `line ${n}: parser trace and test result\n`.repeat(72) })
}
messages.push({ role: "user", content: question })
return { model: "example-model", messages }
}
function transform(raw, options) {
const body = clone(raw)
onRequest(body, { options })
return body
}
function commonPrefix(a, b) {
const max = Math.min(a.length, b.length)
let i = 0
while (i < max && a.charCodeAt(i) === b.charCodeAt(i)) i++
return i
}
function messages(body) {
return Array.isArray(body.messages) ? body.messages : Array.isArray(body.input) ? body.input : []
}
function compare(raw, options) {
const result = transform(raw, options)
const a = messages(raw)
const b = messages(result)
return {
before: chars(raw), after: chars(result),
saved: chars(raw) - chars(result),
changed: a.reduce((n, item, i) => n + (JSON.stringify(item) !== JSON.stringify(b[i]) ? 1 : 0), 0),
messages: a.length,
}
}
export function benchmark(raw = fixture(), options = DEFAULTS, source = "synthetic") {
if (!raw || (!Array.isArray(raw.messages) && !Array.isArray(raw.input))) throw new Error("request must have messages or input array")
const fixed = { ...DEFAULTS, ...options }
const before = compare(raw, { ...fixed, threshold: 1_000_000_000 })
const after = compare(raw, fixed)
const pairA = fixture(15)
const pairB = fixture(16)
const baselineA = JSON.stringify(pairA)
const baselineB = JSON.stringify(pairB)
const transformedA = transform(pairA, fixed)
const transformedB = transform(pairB, fixed)
const processedA = JSON.stringify(transformedA)
const processedB = JSON.stringify(transformedB)
const baselinePrefix = commonPrefix(baselineA, baselineB)
const processedPrefix = commonPrefix(processedA, processedB)
const oldA = messages(pairA)
const slimA = messages(transformedA)
const slimB = messages(transformedB)
let cutEnd = 0
for (let i = 0; i < oldA.length; i++) {
if (JSON.stringify(oldA[i]) !== JSON.stringify(slimA[i])) cutEnd = i + 1
}
const stablePrefix = cutEnd > 0 && JSON.stringify(slimA.slice(0, cutEnd)) === JSON.stringify(slimB.slice(0, cutEnd))
return {
version: 1, source, options: fixed,
request: { before: before.before, after: after.after, saved: after.saved, changed: after.changed, messages: after.messages },
consecutive: {
first_messages: oldA.length, second_messages: messages(pairB).length,
baseline_prefix_chars: baselinePrefix, fovea_prefix_chars: processedPrefix,
baseline_prefix_share: baselinePrefix / baselineA.length,
fovea_prefix_share: processedPrefix / processedA.length,
stable_cut_messages: cutEnd,
stable_cut_prefix: stablePrefix,
},
limits: "字符数与共同前缀占比是确定性的代理指标,不是计费 token、缓存命中、延迟或实际美元;system/developer 与最近窗口里的摘要仍会发送。",
}
}
if (process.argv[1] === fileURLToPath(import.meta.url)) {
const arg = process.argv[2]
const raw = arg ? JSON.parse(readFileSync(arg, "utf8")) : fixture()
const options = process.argv[3] ? JSON.parse(process.argv[3]) : DEFAULTS
const report = benchmark(raw, options, arg ? "local request (content omitted)" : "synthetic")
const out = join(HERE, "page", "report.js")
writeFileSync(out, `globalThis.FoveaReport = ${JSON.stringify(report, null, 2)};\n`)
console.log(JSON.stringify({ report: out, ...report.request, consecutive: report.consecutive }, null, 2))
}