Files
plugins/packages/fovea/fovea.test.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

263 lines
13 KiB
JavaScript

import { test, expect } from "bun:test"
import { readFileSync } from "node:fs"
import { join } from "node:path"
import * as mod from "./fovea.middleware.js"
import { check, run } from "../../scripts/middleware.mjs"
import { benchmark } from "./benchmark.mjs"
check(import.meta.dir, mod)
test("the same-request report excludes prompt text and distinguishes size from cache evidence", () => {
const report = benchmark()
expect(report.request.before).toBeGreaterThan(report.request.after)
expect(report.consecutive.stable_cut_prefix).toBe(true)
expect(report.consecutive.baseline_prefix_share).toBeGreaterThan(0)
expect(report.consecutive.fovea_prefix_share).toBeGreaterThan(0)
expect(report.limits).toContain("不是计费 token")
const privateText = "sample-private-prompt-text-".repeat(8)
const custom = benchmark({ messages: [
{ role: "user", content: "task" },
{ role: "assistant", content: privateText },
{ role: "user", content: "next" },
] }, { threshold: 0, keep_recent: 1, keep_recent_tokens: 0, cut_step: 1 })
expect(JSON.stringify(custom)).not.toContain(privateText)
})
const options = {
threshold: 0, keep_recent: 4, keep_recent_tokens: 0, cut_step: 4,
tool_head: 10, tool_tail: 5, text_head: 20, text_tail: 5,
}
function turn(rounds) {
const messages = [{ role: "user", content: "task" }]
for (let n = 0; n < rounds; n++) {
messages.push({ role: "assistant", content: "reading", tool_calls: [
{ id: `c${n}`, type: "function", function: { name: "read", arguments: "{}" } },
] })
messages.push({ role: "tool", tool_call_id: `c${n}`, content: "L".repeat(300) })
}
messages.push({ role: "user", content: "go on" })
return { model: "m", messages }
}
test("two consecutive rounds keep the already-cut prefix byte-identical", async () => {
// 7 rounds: at=12; 8 rounds: at=14. With cut_step=4 both start at 12.
const a = (await run(mod, { options, request: turn(7) })).request
const b = (await run(mod, { options, request: turn(8) })).request
expect(JSON.stringify(a.messages.slice(0, 12))).toBe(JSON.stringify(b.messages.slice(0, 12)))
expect(a.messages[2].content).toContain("[fovea:")
expect(b.messages[12].content).toBe("L".repeat(300))
})
test("a block boundary changes the prefix only once", async () => {
const a = (await run(mod, { options, request: turn(8) })).request
const b = (await run(mod, { options, request: turn(9) })).request
// at=14 -> start=12; at=16 -> start=16.
expect(a.messages[14].content).toBe("L".repeat(300))
expect(b.messages[14].content).toContain("[fovea:")
expect(JSON.stringify(a.messages.slice(0, 12))).toBe(JSON.stringify(b.messages.slice(0, 12)))
})
test("saccade respects protect even for an old tool result", async () => {
const protectedOutput = "-----BEGIN PROTECTED BLOCK-----\n" + "S".repeat(300)
const request = { messages: [
{ role: "user", content: "task" },
{ role: "assistant", tool_calls: [{ id: "one", type: "function", function: { name: "read", arguments: "{}" } }] },
{ role: "tool", tool_call_id: "one", content: protectedOutput },
{ role: "user", content: "new topic" },
] }
const r = (await run(mod, { options: { ...options, keep_recent: 1, cut_step: 1, saccade: "always", protect: ["BEGIN PROTECTED BLOCK"] }, request })).request
expect(r.messages[2].content).toBe(protectedOutput)
expect(r.messages[2].tool_call_id).toBe("one")
})
test("saccade collapses array function_call_output without losing call_id", async () => {
const request = { input: [
{ type: "message", role: "user", content: "task" },
{ type: "function_call", call_id: "call_a", name: "read", arguments: "{}" },
{ type: "function_call_output", call_id: "call_a", output: [{ type: "text", text: "L".repeat(300) }] },
{ type: "message", role: "user", content: "new topic" },
] }
const r = (await run(mod, { options: { ...options, keep_recent: 1, cut_step: 1, saccade: "always" }, request })).request
expect(r.input[2].output).toEqual([{ type: "text", text: "[fovea: prior topic not carried]" }])
expect(r.input[2].call_id).toBe("call_a")
expect(r.input[1]).toEqual(request.input[1])
})
test("a PDF document is preserved in both modes", async () => {
for (const saccade of ["off", "always"]) {
const document = { type: "document", source: { type: "base64", media_type: "application/pdf", data: "Z".repeat(400) } }
const request = { messages: [
{ role: "user", content: "task" },
{ role: "user", content: [document] },
{ role: "assistant", content: "thinking" },
{ role: "user", content: "new topic" },
] }
const r = (await run(mod, { options: { ...options, keep_recent: 1, cut_step: 1, saccade }, request })).request
expect(r.messages[1].content[0]).toEqual(document)
}
})
test("cuts never split a UTF-16 surrogate pair at head or tail", async () => {
const text = "x".repeat(599) + "😀" + "y".repeat(2000) + "😀" + "z".repeat(100)
const request = { messages: [
{ role: "user", content: "task" },
{ role: "tool", tool_call_id: "a", content: text },
{ role: "user", content: "now" },
] }
const r = (await run(mod, { options: { ...options, keep_recent: 1, cut_step: 1, tool_head: 600, tool_tail: 101 }, request })).request
const cut = r.messages[1].content
expect(cut.startsWith("x".repeat(599) + "\n[fovea:")).toBe(true)
// tail_start points at the low surrogate; safeTail skips it along with
// the high surrogate, so the complete emoji is omitted instead of split.
expect(cut.endsWith("z".repeat(100))).toBe(true)
expect(/[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]/u.test(cut)).toBe(false)
})
test("system summaries and the recent window are untouched by saccade", async () => {
const summary = "Previous compressed context: " + "S".repeat(1000)
const request = { messages: [
{ role: "system", content: summary },
{ role: "user", content: "task" },
{ role: "assistant", content: "A".repeat(300) },
{ role: "user", content: "new question" },
] }
const r = (await run(mod, { options: { ...options, keep_recent: 1, cut_step: 1, saccade: "always" }, request })).request
expect(r.messages[0].content).toBe(summary)
expect(r.messages[3].content).toBe("new question")
})
const STATE = "<!-- fovea:state -->"
test("a state block is never touched and lets the raw task message be thinned", async () => {
const state = STATE + "\n" + JSON.stringify({ task: "ship the parser", done: ["read the file"] })
const task = "T".repeat(400)
const request = { messages: [
{ role: "user", content: task },
{ role: "assistant", content: state },
{ role: "assistant", content: "A".repeat(300) },
{ role: "user", content: "next" },
] }
const r = (await run(mod, { options: { ...options, keep_recent: 1, cut_step: 1, state_marker: STATE }, request })).request
expect(r.messages[1].content).toBe(state)
expect(r.messages[0].content).toContain("[fovea:")
expect(r.messages[0].content).not.toBe(task)
})
test("without a state block the raw task message stays protected", async () => {
const task = "T".repeat(400)
const request = { messages: [
{ role: "user", content: task },
{ role: "assistant", content: "A".repeat(300) },
{ role: "user", content: "next" },
] }
const r = (await run(mod, { options: { ...options, keep_recent: 1, cut_step: 1, state_marker: STATE }, request })).request
expect(r.messages[0].content).toBe(task)
})
test("a state block survives a saccade that collapses everything else", async () => {
const state = STATE + "\n" + JSON.stringify({ task: "ship the parser" })
const request = { messages: [
{ role: "user", content: "T".repeat(400) },
{ role: "assistant", content: state },
{ role: "user", content: "完全不同的新话题 番茄炒蛋 蛋炒饭 紫菜汤" },
] }
const r = (await run(mod, { options: { ...options, keep_recent: 1, cut_step: 1, saccade: "always", state_marker: STATE }, request })).request
expect(r.messages[1].content).toBe(state)
expect(r.messages[0].content).toBe("[fovea: prior topic not carried]")
})
test("the page is wired and offline: every id app.js uses exists, and no page file references the network", () => {
const html = readFileSync(join(import.meta.dir, "page", "index.html"), "utf8")
const js = readFileSync(join(import.meta.dir, "page", "app.js"), "utf8")
for (const id of [...js.matchAll(/\$\("([a-z-]+)"\)/g)].map((m) => m[1])) {
expect(html.includes(`id="${id}"`)).toBe(true)
}
for (const f of ["index.html", "style.css", "app.js", "report.js", "live.js"]) {
const t = readFileSync(join(import.meta.dir, "page", f), "utf8")
expect(t.includes("http://")).toBe(false)
expect(t.includes("https://")).toBe(false)
}
})
test("page/measure.js is the middleware, freshly built — a stale bundle fails here, not in magpie", async () => {
const src = readFileSync(join(import.meta.dir, "fovea.middleware.js"), "utf8")
const expected = src.replace("export function onRequest(", "function onRequest(") + "\nglobalThis.FoveaOnRequest = onRequest;\n"
const got = readFileSync(join(import.meta.dir, "page", "measure.js"), "utf8")
expect(got).toBe(expected)
expect(got.includes("globalThis.FoveaOnRequest")).toBe(true)
})
test("an old image's cache_control survives the note, in both modes", async () => {
for (const saccade of ["off", "always"]) {
const cc = { type: "ephemeral" }
const image = { type: "image", source: { type: "base64", media_type: "image/png", data: "i" + "V".repeat(400) }, cache_control: cc }
const request = { messages: [
{ role: "user", content: "task" },
{ role: "assistant", content: "ack" },
{ role: "user", content: [image] },
{ role: "assistant", content: "seen" },
{ role: "user", content: "next" },
] }
const r = (await run(mod, { options: { ...options, keep_recent: 1, cut_step: 1, saccade }, request })).request
const noted = r.messages[2].content[0]
expect(noted.type).toBe("text")
expect(noted.cache_control).toEqual(cc)
expect(noted.text).toContain("[fovea:")
}
})
test("Codex shape: AGENTS.md, environment_context and the task all stay whole; the first tool output is cut", async () => {
const agents = "# AGENTS.md instructions\n" + "A".repeat(300)
const env = "<environment_context>\n" + "E".repeat(300)
const task = "T".repeat(400)
const request = { input: [
{ type: "message", role: "developer", content: "policy" },
{ type: "message", role: "user", content: agents },
{ type: "message", role: "user", content: env },
{ type: "message", role: "user", content: task },
{ type: "function_call", call_id: "c1", name: "read", arguments: "{}" },
{ type: "function_call_output", call_id: "c1", output: "L".repeat(300) },
{ type: "message", role: "user", content: "go on" },
] }
const r = (await run(mod, { options: { ...options, keep_recent: 1, cut_step: 1 }, request })).request
expect(r.input[1].content).toBe(agents)
expect(r.input[3].content).toBe(task)
expect(r.input[5].output).toContain("[fovea:")
})
// yetone's probe on PR #640: on Codex the first user item is AGENTS.md, so
// saccade: auto judged the topic against it, not against the task.
function codexBody(question) {
const agents = "# AGENTS.md instructions\nUse pnpm for packages, prefer small commits, document every public function in the module header. " + "A".repeat(300)
const env = "<environment_context>\n<cwd>/repo</cwd>\n" + "E".repeat(300)
const task = "Fix the flaky scheduler test in internal/sched: the retry timing assertion fails under load. " + "T".repeat(300)
const input = [
{ type: "message", role: "developer", content: "policy" },
{ type: "message", role: "user", content: agents },
{ type: "message", role: "user", content: env },
{ type: "message", role: "user", content: task },
]
for (let i = 0; i < 6; i++) {
input.push({ type: "function_call", call_id: "c" + i, name: "read", arguments: "{}" })
input.push({ type: "function_call_output", call_id: "c" + i, output: "L".repeat(400) })
}
input.push({ type: "message", role: "user", content: question })
return { agents, env, task, request: { input } }
}
test("Codex shape, saccade auto: a follow-up to the task (third user item) is not a new topic", async () => {
const { agents, task, request } = codexBody("scheduler retry timing assertion still fails in internal/sched, fix it")
const r = (await run(mod, { options: { ...options, keep_recent: 1, cut_step: 1, saccade: "auto" }, request })).request
expect(r.input[1].content).toBe(agents)
expect(r.input[3].content).toBe(task)
expect(JSON.stringify(r)).not.toContain("prior topic not carried")
expect(r.input[5].output).toContain("[fovea:")
})
test("Codex shape, saccade auto: a new topic that shares words with AGENTS.md still collapses", async () => {
const { request } = codexBody("document every public function using pnpm commits header module")
const r = (await run(mod, { options: { ...options, keep_recent: 1, cut_step: 1, saccade: "auto" }, request })).request
expect(r.input[5].output).toContain("prior topic not carried")
})