Files
plugins/packages/fovea/page/app.js
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

123 lines
5.6 KiB
JavaScript

// Offline A/B evidence for one request. No prompt content is kept: only
// counts, percentages and prefix lengths are shown. The paste-in form is
// parsed and measured in this frame, never stored or sent anywhere.
//
// The report cards render from report.js/live.js data alone. The paste-in
// A/B needs the middleware itself, which loads on demand and degrades to
// a friendly note when the page is opened where plugin files beyond the
// page folder are not served (e.g. straight from the filesystem).
(async () => {
const $ = (id) => document.getElementById(id)
const nf = new Intl.NumberFormat(undefined, { maximumFractionDigits: 0 })
const fmt = (n) => nf.format(Math.round(n))
const pct = (n) => `${Math.round(n * 100)}%`
function paintBar(fill, value, max, output) {
fill.style.width = `${Math.max(1.5, Math.round((value / (max || 1)) * 100))}%`
output.textContent = fmt(value)
}
function paintSummary(m) {
$("before").textContent = fmt(m.before)
$("after").textContent = fmt(m.after)
$("saved").textContent = fmt(m.saved)
$("changed").textContent = `${fmt(m.changed)} / ${fmt(m.messages)} 条消息被裁`
$("ratio").textContent = `缩小到 ${pct(Math.min(1, m.after / m.before))}`
$("strip-note").textContent = `${fmt(m.messages)} 条消息 · ${fmt(m.messages - m.changed)} 条原样保留`
paintBar(document.querySelector(".fill.raw"), m.before, m.before, $("raw-bar"))
paintBar($("slim-fill"), m.after, m.before, $("slim-bar"))
}
const report = globalThis.FoveaReport
if (report) {
$("source").textContent = report.source || "synthetic"
paintSummary(report.request)
const c = report.consecutive
const maxPrefix = Math.max(c.baseline_prefix_share, c.fovea_prefix_share)
paintBar($("baseline-prefix-fill"), c.baseline_prefix_share * 100, maxPrefix * 100, $("baseline-prefix"))
paintBar($("fovea-prefix-fill"), c.fovea_prefix_share * 100, maxPrefix * 100, $("fovea-prefix"))
$("baseline-prefix").textContent = pct(c.baseline_prefix_share)
$("fovea-prefix").textContent = pct(c.fovea_prefix_share)
const badge = $("prefix-badge")
badge.textContent = c.stable_cut_prefix ? `${fmt(c.stable_cut_messages)} 条已裁消息逐字稳定` : "未验证稳定的已裁前缀"
badge.classList.toggle("bad", !c.stable_cut_prefix)
if (report.limits) $("limits").textContent = report.limits
}
const live = globalThis.FoveaLive
if (live?.samples?.length === 4) {
$("live-source").textContent = live.same_model ? `${live.model} · ${live.measured_at}` : "两次调用不是同一个模型,结果不可比"
const warm = [live.samples[3], live.samples[2]] // ABBA: raw2, fovea2
const labels = ["原始 · 暖缓存", "fovea · 暖缓存"]
$("live-summary").replaceChildren(...warm.map((sample, i) => {
const card = document.createElement("div")
card.className = "live-card"
const title = document.createElement("h3")
title.textContent = labels[i]
card.append(title)
for (const [label, value] of [
["输入 token", sample.input_tokens],
["缓存读取 token", sample.cache_read_tokens],
["响应毫秒", sample.elapsed_ms],
]) {
const row = document.createElement("div")
const key = document.createElement("span")
key.textContent = label
const num = document.createElement("strong")
num.textContent = value == null ? "未提供" : fmt(value)
row.append(key, num)
card.append(row)
}
return card
}))
}
$("clear").addEventListener("click", () => { $("input").value = ""; $("error").textContent = "" })
let onRequest = globalThis.FoveaOnRequest
if (!onRequest) {
try { ({ onRequest } = await import("../fovea.middleware.js")) } catch { /* page folder only, as in magpie's sandbox */ }
}
try {
if (!onRequest) throw new Error("no middleware")
const measure = (raw, options) => {
const list = Array.isArray(raw.messages) ? raw.messages : raw.input
if (!list) throw new Error("shape")
const before = JSON.stringify(raw).length
const body = JSON.parse(JSON.stringify(raw))
onRequest(body, { options })
const after = JSON.stringify(body).length
const out = Array.isArray(body.messages) ? body.messages : body.input
const changed = list.reduce((n, m, i) => n + (JSON.stringify(m) !== JSON.stringify(out[i]) ? 1 : 0), 0)
return { before, after, saved: before - after, changed, messages: list.length }
}
$("analyze").addEventListener("click", () => {
const err = $("error")
err.textContent = ""
try {
const raw = JSON.parse($("input").value)
const options = {
threshold: 0,
keep_recent: Number($("keep").value) || 0,
cut_step: Math.max(1, Number($("step").value) || 4),
saccade: $("mode").value,
tool_head: 80, tool_tail: 40, text_head: 120, text_tail: 60,
}
paintSummary(measure(raw, options))
$("source").textContent = "本机粘贴的请求(内容不显示)"
} catch (e) {
err.textContent = e.message === "shape"
? "没找到消息列表:请粘贴包含 messages 或 input 的整段请求。"
: "这段内容读不出来:请粘贴完整的请求 JSON(从 { 到 } 整段复制)。"
}
})
} catch {
const note = document.createElement("p")
note.className = "cap"
note.textContent = "当前环境加载不了裁剪逻辑,粘贴复验暂不可用;上方的对照数字不受影响。"
$("try-title")?.closest("section")?.append(note)
$("analyze").disabled = true
$("analyze").textContent = "此环境不可用"
}
})()