From 248221bbdd4a1c8a9b408dd108b4658dd5245ef6 Mon Sep 17 00:00:00 2001 From: Afloat Date: Sun, 15 Nov 2026 22:37:48 +0800 Subject: [PATCH 01/24] perf: scan cache body boundaries in linear time Avoid repeated suffix searches when settling a long transcript and defer boundary work until append/head checks need it. Fixes ranxianglei/billion-context#6839 (cherry picked from commit 43dfba5870a8ae9d68a6d852b11a7e3ba7de07ab) --- scripts/bench-cache-array-scan.mjs | 28 ++++++++++ src/cache-ledger.ts | 37 ++++++------- tests/cache-body-boundary-scan.test.ts | 74 ++++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 22 deletions(-) create mode 100644 scripts/bench-cache-array-scan.mjs create mode 100644 tests/cache-body-boundary-scan.test.ts diff --git a/scripts/bench-cache-array-scan.mjs b/scripts/bench-cache-array-scan.mjs new file mode 100644 index 0000000000..f9548547d1 --- /dev/null +++ b/scripts/bench-cache-array-scan.mjs @@ -0,0 +1,28 @@ +import { performance } from "node:perf_hooks"; +import { getCacheLedger, noteForwardedBody, settleUsageReport } from "../src/cache-ledger.ts"; + +const counts = process.argv.slice(2).map(Number); +for (const count of counts.length ? counts : [1000, 4000, 8000]) { + const messages = Array.from({ length: count }, (_, i) => ({ + role: i % 2 === 0 ? "user" : "assistant", + content: `Turn ${i}: ${"Conversation text with ordinary words. ".repeat(10)}`, + })); + const first = JSON.stringify({ model: "m", messages, max_tokens: 16384, stop: ["end-of-turn", "stop-marker-one", "stop-marker-two", "presence-padding-field"] }); + const next = JSON.stringify({ model: "m", messages: [...messages, { role: "user", content: "Continue the conversation." }], max_tokens: 16384, stop: ["end-of-turn", "stop-marker-one", "stop-marker-two", "presence-padding-field"] }); + const times = []; + for (let run = 0; run < 4; run++) { + const session = { id: `array-scan-${count}-${run}`, metadata: {}, stats: {}, state: { blocks: [], messageRefs: { byRaw: {}, byRef: {} }, tokenSnapshot: {} } }; + const report = { total: 100_000, reportedCached: 99_000, output: 0, protocol: "openai", upstream: "http://fixture" }; + noteForwardedBody(session, first, count); + settleUsageReport(session, report); + const start = performance.now(); + noteForwardedBody(session, next, count + 1); + settleUsageReport(session, report); + const elapsed = performance.now() - start; + const bodyClass = getCacheLedger(session).lines.at(-1)?.bs; + if (bodyClass !== "tail") throw new Error(`unexpected body class: ${bodyClass}`); + if (run > 0) times.push(elapsed); + } + times.sort((a, b) => a - b); + console.log(JSON.stringify({ count, bytes: Buffer.byteLength(first), medianMs: times[1], samplesMs: times })); +} diff --git a/src/cache-ledger.ts b/src/cache-ledger.ts index 2dfeb9d0c8..4fceb4daa0 100644 --- a/src/cache-ledger.ts +++ b/src/cache-ledger.ts @@ -1040,14 +1040,9 @@ function messagesArrayCloseOffset(body: Buffer): number | null { i++; continue; } - // String content: hop to the next quote or backslash (native - // scans) instead of branching per byte — the bulk of any body. - let m = n; - const q = body.indexOf(34, i + 1); - if (q !== -1 && q < m) m = q; - const b = body.indexOf(92, i + 1); - if (b !== -1 && b < m) m = b; - i = m; + // Advance once per byte. Separate delimiter searches would each + // rescan the whole remaining body when a delimiter is absent. + i++; continue; } if (c === 34) { inStr = true; keyAt = depth === 1 ? i : -1; i++; continue; } @@ -1058,14 +1053,8 @@ function messagesArrayCloseOffset(body: Buffer): number | null { i++; continue; } - // Inter-token filler (whitespace/commas/colons/literals): hop to the - // next structural byte, again natively. - let m = n; - for (const code of [34, 123, 125, 91, 93]) { - const p = body.indexOf(code, i + 1); - if (p !== -1 && p < m) m = p; - } - i = m; + // Whitespace, commas, colons and literals need no separate search. + i++; } return null; } @@ -2082,12 +2071,16 @@ export function settleUsageReport( // side would misread the pair as mid. No array locatable // (unparseable body) keeps the conservative pre-fix class. const shorterFull = prevSlot.full.length <= curSlot.full.length ? prevSlot.full : curSlot.full; - const arrClose = messagesArrayCloseOffset(shorterFull); - // #3476: a growing logical unit (single giant message whose interior - // container extends in place) is byte-wise a mid-body insertion but - // semantically an append — recognize it in the would-be-mid slot. - const midInsert = isMidInsertionAppend(prevSlot.full, curSlot.full, lcp); - curLine.bs = lcp >= Math.min(prevSlot.full.length, curSlot.full.length) - 64 ? "append" : lcp < 1024 ? "head" : arrClose !== null && lcp >= arrClose ? "tail" : midInsert ? "append" : "mid"; + if (lcp >= shorterFull.length - 64) curLine.bs = "append"; + else if (lcp < 1024) curLine.bs = "head"; + else { + const arrClose = messagesArrayCloseOffset(shorterFull); + // #3476: a growing logical unit (single giant message whose interior + // container extends in place) is byte-wise a mid-body insertion but + // semantically an append — recognize it in the would-be-mid slot. + curLine.bs = arrClose !== null && lcp >= arrClose ? "tail" + : isMidInsertionAppend(prevSlot.full, curSlot.full, lcp) ? "append" : "mid"; + } } else { curLine.bs = "unknown"; } diff --git a/tests/cache-body-boundary-scan.test.ts b/tests/cache-body-boundary-scan.test.ts new file mode 100644 index 0000000000..46499b04a6 --- /dev/null +++ b/tests/cache-body-boundary-scan.test.ts @@ -0,0 +1,74 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; +import { getCacheLedger, noteForwardedBody, settleUsageReport } from "../src/cache-ledger.ts"; +import type { Session } from "../src/session.ts"; + +let serial = 0; +function session(): Session { + return { + id: `boundary-scan-${++serial}`, + metadata: {}, + stats: {}, + state: { blocks: [], messageRefs: { byRaw: {}, byRef: {} }, tokenSnapshot: {} }, + } as unknown as Session; +} + +function settle(s: Session, body: string): void { + noteForwardedBody(s, body); + settleUsageReport(s, { total: 100_000, reportedCached: 99_000, output: 0, protocol: "openai", upstream: "http://fixture" }); +} + +const tail = { max_tokens: 16384, stop: ["end-of-turn", "stop-marker-one", "stop-marker-two", "presence-padding-field"] }; + +test("ordinary long-history appends do not repeatedly scan the retained body suffix", () => { + const s = session(); + const messages = Array.from({ length: 600 }, (_, i) => ({ role: i % 2 ? "assistant" : "user", content: `Turn ${i}: ${"ordinary conversation text ".repeat(12)}` })); + settle(s, JSON.stringify({ model: "m", messages, ...tail })); + const previous = s.transient?.settledBody?.full; + assert.ok(previous); + const originalIndexOf = previous.indexOf; + const search = originalIndexOf.bind(previous); + let scannedBytes = 0; + // Count actual native search spans on this captured body only. No clock + // threshold: a search that finds nothing has inspected the whole suffix. + previous.indexOf = (value: string | number | Uint8Array, offsetOrEncoding?: number | BufferEncoding, encoding?: BufferEncoding): number => { + const found = typeof offsetOrEncoding === "string" ? search(value, offsetOrEncoding) : search(value, offsetOrEncoding, encoding); + const start = typeof offsetOrEncoding === "number" ? offsetOrEncoding : 0; + scannedBytes += Math.max(0, (found === -1 ? previous.length : found + 1) - start); + return found; + }; + try { + settle(s, JSON.stringify({ model: "m", messages: [...messages, { role: "user", content: "Continue." }], ...tail })); + } finally { + previous.indexOf = originalIndexOf; + } + assert.equal(getCacheLedger(s).lines.at(-1)?.bs, "tail"); + assert.ok(scannedBytes <= previous.length * 8, `searched ${scannedBytes} bytes for a ${previous.length}-byte body`); +}); + +for (const key of ["messages", "input", "contents"]) { + test(`${key}: nested arrays and escaped delimiters retain tail and mid classifications`, () => { + const messages = Array.from({ length: 25 }, (_, i) => ({ + role: "user", + content: [{ type: "text", text: `Turn ${i}: ${'汉字 ] } " \\ \\\\ '.repeat(12)}` }], + })); + const first = JSON.stringify({ model: "m", [key]: messages, ...tail }); + const s = session(); + settle(s, first); + settle(s, JSON.stringify({ model: "m", [key]: messages, ...tail, max_tokens: 4096 })); + assert.equal(getCacheLedger(s).lines.at(-1)?.bs, "tail"); + + const rewritten = structuredClone(messages); + rewritten[12]!.content[0]!.text = rewritten[12]!.content[0]!.text.replace("汉字", "改写"); + settle(s, JSON.stringify({ model: "m", [key]: rewritten, ...tail, max_tokens: 4096 })); + assert.equal(getCacheLedger(s).lines.at(-1)?.bs, "mid"); + }); +} + +test("an unlocatable history array keeps the conservative mid classification", () => { + const s = session(); + const payload = "ordinary prose ".repeat(200); + settle(s, JSON.stringify({ model: "m", payload, ...tail })); + settle(s, JSON.stringify({ model: "m", payload, ...tail, max_tokens: 4096 })); + assert.equal(getCacheLedger(s).lines.at(-1)?.bs, "mid"); +}); From 7f3c8c5020d3baa1f1d39476f60c1c964b6433c8 Mon Sep 17 00:00:00 2001 From: Afloat Date: Sun, 15 Nov 2026 23:04:06 +0800 Subject: [PATCH 02/24] fix: bound retained image encode cache payloads Fixes ranxianglei/billion-context#6846 (cherry picked from commit c39ace7aabe8c15f5d924f8162e6d727cac58fe8) --- scripts/bench-image-encode-cache.mjs | 42 +++++++ src/image-rescale.ts | 85 +++++++++---- tests/image-rescale-cache-budget.test.ts | 149 +++++++++++++++++++++++ 3 files changed, 253 insertions(+), 23 deletions(-) create mode 100644 scripts/bench-image-encode-cache.mjs create mode 100644 tests/image-rescale-cache-budget.test.ts diff --git a/scripts/bench-image-encode-cache.mjs b/scripts/bench-image-encode-cache.mjs new file mode 100644 index 0000000000..c2e78a8b9a --- /dev/null +++ b/scripts/bench-image-encode-cache.mjs @@ -0,0 +1,42 @@ +import { Jimp } from "jimp"; +import { applyImageRescalePass, clearRescaleEncodeCache } from "../src/image-rescale.ts"; + +if (typeof globalThis.gc !== "function") throw new Error("Run with node --expose-gc"); +const count = Number(process.argv[2] ?? 20); +const image = new Jimp({ width: 2400, height: 1600 }); +let seed = 42; +for (let i = 0; i < image.bitmap.data.length; i += 4) { + seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0; + image.bitmap.data[i] = seed & 255; + image.bitmap.data[i + 1] = (seed >>> 8) & 255; + image.bitmap.data[i + 2] = (seed >>> 16) & 255; + image.bitmap.data[i + 3] = 255; +} +const ctx = { session: { state: { messageRefs: { byRaw: {} } } }, sessionId: "bench-image-cache", maxDim: 1600, toolArmed: false, log: () => {} }; +async function one(index) { + image.bitmap.data[0] = index; + const input = (await image.getBuffer("image/png")).toString("base64"); + const [out] = await applyImageRescalePass([{ id: `image-${index}`, role: "user", contentType: "text", imageBase64: input, imageMediaType: "image/png" }], ctx); + if (!out || out.imageBase64 === input) throw new Error("Expected a real rescale"); + return { inputBytes: input.length, outputBytes: out.imageBase64.length }; +} +async function memoryAfterGc() { + // V8's external-memory counters can lag one collection. Give reclaimed + // codec temporaries several collections and event-loop turns to settle. + for (let i = 0; i < 3; i++) { + globalThis.gc(); + await new Promise(resolve => setImmediate(resolve)); + } + return process.memoryUsage(); +} +await one(255); // load the encoder before measuring retained cache bytes +clearRescaleEncodeCache(); +const before = await memoryAfterGc(); +let sizes; +for (let i = 0; i < count; i++) sizes = await one(i); +const retained = await memoryAfterGc(); +clearRescaleEncodeCache(); +const cleared = await memoryAfterGc(); +const mb = bytes => Math.round(bytes / 1024 / 1024 * 10) / 10; +const memory = usage => ({ heapMiB: mb(usage.heapUsed), externalMiB: mb(usage.external), rssMiB: mb(usage.rss) }); +console.log(JSON.stringify({ count, sizes, before: memory(before), retained: memory(retained), cleared: memory(cleared), cacheReleaseMiB: mb(retained.heapUsed + retained.external - cleared.heapUsed - cleared.external) }, null, 2)); diff --git a/src/image-rescale.ts b/src/image-rescale.ts index acc495376b..35ee4a2995 100644 --- a/src/image-rescale.ts +++ b/src/image-rescale.ts @@ -12,8 +12,8 @@ // - ZERO kernel changes: no version bump, no golden ledger, no kernel source. // - Deterministic: same input bytes → same output bytes AND same annotation // text, so the outbound body is byte-stable across rounds for the same -// history (the #3266 75%→28% cache-hit lesson). A process-lifetime hash -// cache encodes each unique image once. +// history (the #3266 75%→28% cache-hit lesson). A bounded process-wide hash +// cache reuses completed encodes and de-duplicates in-flight work. // - Structural loop guard: images carried by a acp_read_original_image tool // result are NEVER re-shrunk (that exemption is what kills the #3266 // read→resend→re-shrink cycle). Zero state, survives restarts. @@ -46,6 +46,8 @@ const KEEP_RATIO = 0.8; const JPEG_QUALITY = 80; /** Process-lifetime encode cache cap (unique images per process). */ const ENCODE_CACHE_MAX = 512; +/** Retained base64 payloads, excluding temporary buffers used by the codec. */ +const ENCODE_CACHE_MAX_BYTES = 64 * 1024 * 1024; type RescaledMime = "image/png" | "image/jpeg" | "image/bmp"; @@ -82,7 +84,53 @@ interface RescaledImage { // hash → pending/finished encoding. A promise map doubles as the in-flight // de-duplication: concurrent requests for the same image share one encode. -const encodeCache = new Map>(); +interface EncodeCacheEntry { + pending: Promise; + bytes: number; +} +const encodeCache = new Map(); +let encodeCacheBytes = 0; +let encodeCacheByteLimit = ENCODE_CACHE_MAX_BYTES; + +function trimEncodeCache(): void { + while (encodeCache.size > ENCODE_CACHE_MAX) { + const oldest = encodeCache.entries().next().value; + if (oldest === undefined) break; + encodeCache.delete(oldest[0]); + encodeCacheBytes -= oldest[1].bytes; + } + // Pending encodes have no retained output yet. Keep their de-duplication + // entries while releasing the oldest completed payloads under pressure. + for (const [hash, entry] of encodeCache) { + if (encodeCacheBytes <= encodeCacheByteLimit) break; + if (entry.bytes === 0) continue; + encodeCache.delete(hash); + encodeCacheBytes -= entry.bytes; + } +} + +function cacheEncode(hash: string, pending: Promise): Promise { + const entry: EncodeCacheEntry = { pending, bytes: 0 }; + entry.pending = pending.then(result => { + // A cleared/evicted encode must not account bytes against a newer + // entry with the same key, or resurrect itself after cache reset. + if (encodeCache.get(hash) !== entry || result === null) return result; + const bytes = result.base64.length; // base64 is ASCII: one byte per character + if (bytes > encodeCacheByteLimit) { + // Serve the caller but retain no oversized result; smaller useful + // entries must not all be evicted to accommodate a single image. + encodeCache.delete(hash); + return result; + } + entry.bytes = bytes; + encodeCacheBytes += bytes; + trimEncodeCache(); + return result; + }); + encodeCache.set(hash, entry); + trimEncodeCache(); + return entry.pending; +} type JimpModule = typeof import("jimp"); @@ -141,8 +189,8 @@ async function doRescale(mime: RescaledMime, b64: string, origBytes: number, max /** Decide + encode ONE image occurrence. Returns null when the original must * be forwarded untouched (unsupported format, below the gate, low benefit, - * or decode failure). Cached per content hash — each unique image encodes - * once per process. */ + * or decode failure). Cached per content hash while the entry fits within + * the process-wide count and retained-payload limits. */ async function rescaleImage(mediaType: string, b64: string, maxDim: number): Promise { const mime = normalizeMime(mediaType); if (!mime || typeof b64 !== "string" || b64.length === 0) return null; @@ -151,16 +199,10 @@ async function rescaleImage(mediaType: string, b64: string, maxDim: number): Pro const gated = (dims !== undefined && Math.max(dims.w, dims.h) > maxDim) || origBytes > RESCALE_TRIGGER_BYTES; if (!gated) return null; const hash = createHash("sha256").update(`${mime}:${maxDim}:${b64}`, "utf8").digest("hex"); - let pending = encodeCache.get(hash); + let pending = encodeCache.get(hash)?.pending; const cacheHit = pending !== undefined; if (!pending) { - pending = doRescale(mime, b64, origBytes, maxDim); - encodeCache.set(hash, pending); - while (encodeCache.size > ENCODE_CACHE_MAX) { - const oldest = encodeCache.keys().next().value; - if (oldest === undefined || oldest === hash) break; - encodeCache.delete(oldest); - } + pending = cacheEncode(hash, doRescale(mime, b64, origBytes, maxDim)); } const result = await pending; // A cache hit serves the stored encode result again — mark it non-fresh so @@ -169,9 +211,12 @@ async function rescaleImage(mediaType: string, b64: string, maxDim: number): Pro return result; } -/** Test seam: drop the process-lifetime encode cache between cases. */ -export function clearRescaleEncodeCache(): void { +/** Test seam: reset cache state; an optional small budget avoids huge fixtures. + * Calling without arguments restores the production budget. */ +export function clearRescaleEncodeCache(maxBytesForTest = ENCODE_CACHE_MAX_BYTES): void { encodeCache.clear(); + encodeCacheBytes = 0; + encodeCacheByteLimit = maxBytesForTest; } /** #3701 byte-shed ladder step: forced re-encode at an explicit longest-edge dim. @@ -184,15 +229,9 @@ export async function shedDownscaleImage(mediaType: string, b64: string, maxDim: if (!mime || typeof b64 !== "string" || b64.length === 0 || !Number.isFinite(maxDim) || maxDim <= 0) return null; const dim = Math.floor(maxDim); const hash = createHash("sha256").update(`${dim}:${b64}`, "utf8").digest("hex"); - let pending = encodeCache.get(hash); + let pending = encodeCache.get(hash)?.pending; if (!pending) { - pending = doRescale(mime, b64, b64Bytes(b64), dim, true); - encodeCache.set(hash, pending); - while (encodeCache.size > ENCODE_CACHE_MAX) { - const oldest = encodeCache.keys().next().value; - if (oldest === undefined || oldest === hash) break; - encodeCache.delete(oldest); - } + pending = cacheEncode(hash, doRescale(mime, b64, b64Bytes(b64), dim, true)); } const r = await pending; return r === null ? null : r.base64; diff --git a/tests/image-rescale-cache-budget.test.ts b/tests/image-rescale-cache-budget.test.ts new file mode 100644 index 0000000000..96bf919bc5 --- /dev/null +++ b/tests/image-rescale-cache-budget.test.ts @@ -0,0 +1,149 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { Jimp } from "jimp"; +import { createSession } from "../src/session.ts"; +import { applyImageRescalePass, clearRescaleEncodeCache, shedDownscaleImage } from "../src/image-rescale.ts"; + +// Exercise the production admission/eviction paths with small payloads; the +// real-codec benchmark uses the unchanged default 64 MiB budget. +test.beforeEach(() => clearRescaleEncodeCache(64)); +test.afterEach(() => clearRescaleEncodeCache()); + +function input(index: number): string { + const header = Buffer.alloc(128); + header.set([0x89, 0x50, 0x4e, 0x47]); + header.writeUInt32BE(2, 16); + header.writeUInt32BE(1, 20); + header.writeUInt32BE(index, 24); + return header.toString("base64"); +} + +test("image encodes evict retained payloads before the 512-entry count cap", async (t) => { + // Isolate cache ownership from codec cost: seven 12-byte base64 results + // exceed the test budget. Both egress paths share that same budget. + const pixels = new Jimp({ width: 2, height: 1 }); + const encoded = Buffer.alloc(8); + let reads = 0; + t.mock.method(pixels, "getBuffer", async () => encoded); + t.mock.method(pixels, "resize", () => pixels); + t.mock.method(Jimp, "read", async () => { + reads++; + return pixels; + }); + const session = createSession("image-cache-budget"); + const ctx = { session, sessionId: session.id, maxDim: 1, toolArmed: false, log: () => {} }; + const encode = async (index: number) => { + const [out] = await applyImageRescalePass([{ id: `image-${index}`, role: "user", contentType: "text", imageBase64: input(index), imageMediaType: "image/png" }], ctx); + assert.equal(out!.imageBase64!.length, encoded.toString("base64").length); + }; + for (let i = 0; i < 6; i++) await encode(i); + assert.equal(await shedDownscaleImage("image/png", input(99), 1), encoded.toString("base64")); + assert.equal(reads, 7); + await encode(5); + assert.equal(reads, 7, "the newest image stays cached"); + await encode(0); + assert.equal(reads, 8, "the oldest payload must be released before 512 large images accumulate"); +}); + +test("simultaneous requests share one image encode", async (t) => { + const pixels = new Jimp({ width: 2, height: 1 }); + const encoded = Buffer.from("small encoded output"); + let release: (() => void) | undefined; + const gate = new Promise(resolve => { release = resolve; }); + let reads = 0; + t.mock.method(pixels, "getBuffer", async () => encoded); + t.mock.method(Jimp, "read", async () => { reads++; await gate; return pixels; }); + const a = shedDownscaleImage("image/png", input(0), 1); + const b = shedDownscaleImage("image/png", input(0), 1); + release!(); + assert.equal(await a, encoded.toString("base64")); + assert.equal(await b, encoded.toString("base64")); + assert.equal(reads, 1); +}); + +test("an oversized result is returned without evicting smaller cached images", async (t) => { + const pixels = new Jimp({ width: 2, height: 1 }); + let encoded = Buffer.alloc(8); + let reads = 0; + t.mock.method(pixels, "resize", () => pixels); + t.mock.method(pixels, "getBuffer", async () => encoded); + t.mock.method(Jimp, "read", async () => { reads++; return pixels; }); + const small = await shedDownscaleImage("image/png", input(0), 1); + encoded = Buffer.alloc(60); // 80 base64 bytes, larger than the entire budget + const large = await shedDownscaleImage("image/png", input(1), 1); + assert.equal(large, encoded.toString("base64")); + assert.equal(await shedDownscaleImage("image/png", input(0), 1), small); + assert.equal(reads, 2, "an oversized result must not displace the useful small entry"); + assert.equal(await shedDownscaleImage("image/png", input(1), 1), large, "re-encoding is byte-identical"); + assert.equal(reads, 3, "the oversized result is not retained"); +}); + +test("clearing during an encode does not let its completion alter a newer entry", async (t) => { + let release: (() => void) | undefined; + const gate = new Promise(resolve => { release = resolve; }); + let reads = 0; + t.mock.method(Jimp, "read", async () => { + const call = ++reads; + const pixels = new Jimp({ width: 2, height: 1 }); + t.mock.method(pixels, "getBuffer", async () => { + if (call === 1) await gate; + return Buffer.alloc(call === 1 ? 60 : 8); + }); + return pixels; + }); + const old = shedDownscaleImage("image/png", input(0), 1); + clearRescaleEncodeCache(64); + const current = await shedDownscaleImage("image/png", input(0), 1); + release!(); + assert.equal(await old, Buffer.alloc(60).toString("base64")); + assert.equal(await shedDownscaleImage("image/png", input(0), 1), current); + assert.equal(reads, 2, "the older completion cannot evict/account the replacement entry"); +}); + +test("failed encodes retain the existing 512-entry bound", async (t) => { + let reads = 0; + t.mock.method(Jimp, "read", async () => { reads++; throw new Error("decode failure"); }); + for (let i = 0; i < 513; i++) assert.equal(await shedDownscaleImage("image/png", input(i), 1), null); + assert.equal(reads, 513); + assert.equal(await shedDownscaleImage("image/png", input(512), 1), null); + assert.equal(reads, 513, "negative results still share the cache"); + assert.equal(await shedDownscaleImage("image/png", input(0), 1), null); + assert.equal(reads, 514, "zero-byte results cannot grow without the count cap"); +}); + +test("payload pressure retains pending entries for in-flight de-duplication", async (t) => { + let release: (() => void) | undefined; + const gate = new Promise(resolve => { release = resolve; }); + let reads = 0; + t.mock.method(Jimp, "read", async () => { + const call = ++reads; + const pixels = new Jimp({ width: 2, height: 1 }); + t.mock.method(pixels, "getBuffer", async () => { + if (call === 1) await gate; + return Buffer.alloc(8); + }); + return pixels; + }); + const first = shedDownscaleImage("image/png", input(0), 1); + for (let i = 1; i <= 6; i++) await shedDownscaleImage("image/png", input(i), 1); + const same = shedDownscaleImage("image/png", input(0), 1); + release!(); + assert.equal(await first, await same); + assert.equal(reads, 7, "byte eviction only removes completed payloads"); +}); + +test("a normal reset restores the production budget after a test override", async (t) => { + const pixels = new Jimp({ width: 2, height: 1 }); + let reads = 0; + t.mock.method(pixels, "resize", () => pixels); + t.mock.method(pixels, "getBuffer", async () => Buffer.alloc(8)); + t.mock.method(Jimp, "read", async () => { reads++; return pixels; }); + clearRescaleEncodeCache(1); + await shedDownscaleImage("image/png", input(0), 1); + await shedDownscaleImage("image/png", input(0), 1); + assert.equal(reads, 2); + clearRescaleEncodeCache(); + await shedDownscaleImage("image/png", input(0), 1); + await shedDownscaleImage("image/png", input(0), 1); + assert.equal(reads, 3, "the default reset restores normal cache admission"); +}); From a6006e58da4f49249aacc652bb5e54153121f5f8 Mon Sep 17 00:00:00 2001 From: ci-check Date: Mon, 16 Nov 2026 01:32:59 +0800 Subject: [PATCH 03/24] test: isolate rescale knob from host config in cache-budget suite Match the issue3400 suite's BILI_CONFIG_FILE isolation so the admission/eviction tests run on the default (enabled) rescale posture regardless of any host config file. (cherry picked from commit 01ff01ad02801963ebce9c88e192d1ed0ea018cc) --- tests/image-rescale-cache-budget.test.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/image-rescale-cache-budget.test.ts b/tests/image-rescale-cache-budget.test.ts index 96bf919bc5..8fe5b6f501 100644 --- a/tests/image-rescale-cache-budget.test.ts +++ b/tests/image-rescale-cache-budget.test.ts @@ -4,6 +4,10 @@ import { Jimp } from "jimp"; import { createSession } from "../src/session.ts"; import { applyImageRescalePass, clearRescaleEncodeCache, shedDownscaleImage } from "../src/image-rescale.ts"; +// Isolate the rescale knob from any host config: the pass must run on the +// default (enabled) posture, matching the issue3400 suite's isolation. +process.env.BILI_CONFIG_FILE = "/nonexistent/bili-image-rescale-cache-budget.json"; + // Exercise the production admission/eviction paths with small payloads; the // real-codec benchmark uses the unchanged default 64 MiB budget. test.beforeEach(() => clearRescaleEncodeCache(64)); From 466fdaae58de87dd549698c2deb79e584ae6da65 Mon Sep 17 00:00:00 2001 From: Afloat Date: Sun, 15 Nov 2026 23:24:53 +0800 Subject: [PATCH 04/24] perf: frame loop SSE chunks in linear time Preserve existing newline and EOF semantics while scanning only newly received text in the four SSE loop adapters. Fixes #6849 (cherry picked from commit 7a1f401b6bb073193e11f8a8cfe6d34651094026) --- scripts/bench-stream-frame-scan.mjs | 30 +++++++++ src/loop/adapter-anthropic.ts | 18 ++---- src/loop/adapter-google.ts | 20 ++---- src/loop/adapter-openai.ts | 18 ++---- src/loop/adapter-responses.ts | 18 ++---- src/sse-util.ts | 50 +++++++++++++++ tests/loop-sse-framing-scan.test.ts | 95 +++++++++++++++++++++++++++++ 7 files changed, 192 insertions(+), 57 deletions(-) create mode 100644 scripts/bench-stream-frame-scan.mjs create mode 100644 tests/loop-sse-framing-scan.test.ts diff --git a/scripts/bench-stream-frame-scan.mjs b/scripts/bench-stream-frame-scan.mjs new file mode 100644 index 0000000000..eeb8d51648 --- /dev/null +++ b/scripts/bench-stream-frame-scan.mjs @@ -0,0 +1,30 @@ +import { performance } from "node:perf_hooks"; +import { createGoogleAdapter } from "../src/loop/adapter-google.ts"; + +const chunkSize = 16 * 1024; +const sizes = process.argv.slice(2).map(Number); +for (const size of sizes.length ? sizes : [1, 4, 8]) { + const payload = "A".repeat(size * 1024 * 1024); + const wire = Buffer.from(`data: ${JSON.stringify({ candidates: [{ content: { role: "model", parts: [{ text: "Generated image." }, { inlineData: { mimeType: "image/png", data: payload } }] } }] })}\n\ndata: ${JSON.stringify({ candidates: [{ finishReason: "STOP" }] })}\n\n`); + const times = []; + for (let pass = 0; pass < 4; pass++) { + let at = 0; + const stream = new ReadableStream({ pull(controller) { + if (at >= wire.length) return controller.close(); + controller.enqueue(wire.subarray(at, at + chunkSize)); + at += chunkSize; + } }); + const adapter = createGoogleAdapter({}, undefined, undefined, "gemini-test"); + let outputBytes = 0; + const start = performance.now(); + for await (const event of adapter.parseStream(stream, 1)) { + if (event.kind === "meta") outputBytes += event.chunk.length; + else if ("raw" in event && event.raw) outputBytes += event.raw.length; + } + const elapsed = performance.now() - start; + if (outputBytes < payload.length) throw new Error("Image frame was not forwarded"); + if (pass > 0) times.push(elapsed); + } + times.sort((a, b) => a - b); + console.log(JSON.stringify({ payloadMiB: size, wireBytes: wire.length, networkChunkBytes: chunkSize, medianMs: Number(times[1].toFixed(2)) })); +} diff --git a/src/loop/adapter-anthropic.ts b/src/loop/adapter-anthropic.ts index 0ed396aa03..468f08e675 100644 --- a/src/loop/adapter-anthropic.ts +++ b/src/loop/adapter-anthropic.ts @@ -7,7 +7,7 @@ import { buildVisibilityMarker } from "./core.js"; import { composeStreamFilters, createBiliArtifactFilter, createMarkerLineFilter, createTagEchoFilter } from "./tag-echo-filter.js"; import { degenerateTurnWarning } from "../degenerate-turn.js"; import { log as loggerLog } from "../logger.js"; -import { normalizeSseLineEndings, finalizeSseLineEndings, sseEventData, replaceSseEventData } from "../sse-util.js"; +import { createSseFramer, sseEventData, replaceSseEventData } from "../sse-util.js"; import type { CompressLoopAdapter, EmitCompletionOpts, @@ -24,7 +24,7 @@ interface ToolUseBuffer { async function* iterSseEvents(stream: ReadableStream): AsyncGenerator { const reader = stream.getReader(); const decoder = new TextDecoder(); - let buf = ""; + const framer = createSseFramer(); try { while (true) { let done: boolean; @@ -35,23 +35,13 @@ async function* iterSseEvents(stream: ReadableStream): AsyncGenerato break; } if (done) break; - buf += decoder.decode(value, { stream: true }); - buf = normalizeSseLineEndings(buf); - let idx: number; - while ((idx = buf.indexOf("\n\n")) >= 0) { - const raw = buf.slice(0, idx); - buf = buf.slice(idx + 2); + for (const raw of framer.push(decoder.decode(value, { stream: true }))) { if (raw.trim().length > 0) yield raw; } } - buf = finalizeSseLineEndings(buf); - let idx: number; - while ((idx = buf.indexOf("\n\n")) >= 0) { - const raw = buf.slice(0, idx); - buf = buf.slice(idx + 2); + for (const raw of framer.finish()) { if (raw.trim().length > 0) yield raw; } - if (buf.trim().length > 0) yield buf; } finally { reader.releaseLock(); } diff --git a/src/loop/adapter-google.ts b/src/loop/adapter-google.ts index 4d17d5d374..327c75344c 100644 --- a/src/loop/adapter-google.ts +++ b/src/loop/adapter-google.ts @@ -5,7 +5,7 @@ import { buildVisibilityMarker } from "./core.js"; import { composeStreamFilters, createBiliArtifactFilter, createMarkerLineFilter, createTagEchoFilter } from "./tag-echo-filter.js"; import { degenerateTurnWarning } from "../degenerate-turn.js"; import { log as loggerLog } from "../logger.js"; -import { normalizeSseLineEndings, finalizeSseLineEndings, sseEventData } from "../sse-util.js"; +import { createSseFramer, sseEventData } from "../sse-util.js"; import type { CompressLoopAdapter, @@ -169,6 +169,7 @@ interface GoogleChunk { async function* iterGoogleChunks(stream: ReadableStream): AsyncGenerator { const reader = stream.getReader(); const decoder = new TextDecoder(); + const framer = createSseFramer(); let buf = ""; let mode: "unknown" | "sse" | "array" = "unknown"; let cursor = 0; @@ -200,14 +201,11 @@ async function* iterGoogleChunks(stream: ReadableStream): AsyncGener } } if (mode === "sse") { - buf = normalizeSseLineEndings(buf); - let idx: number; - while ((idx = buf.indexOf("\n\n")) >= 0) { - const raw = buf.slice(0, idx); - buf = buf.slice(idx + 2); + for (const raw of framer.push(buf)) { const json = dataLine(raw); if (json !== null) yield { frame: raw, json }; } + buf = ""; continue; } for (; cursor < buf.length && !arrayDone; cursor++) { @@ -241,18 +239,10 @@ async function* iterGoogleChunks(stream: ReadableStream): AsyncGener } } if (mode === "sse") { - buf = finalizeSseLineEndings(buf); - let idx: number; - while ((idx = buf.indexOf("\n\n")) >= 0) { - const raw = buf.slice(0, idx); - buf = buf.slice(idx + 2); + for (const raw of framer.finish()) { const json = dataLine(raw); if (json !== null) yield { frame: raw, json }; } - if (buf.trim().length > 0) { - const json = dataLine(buf); - if (json !== null) yield { frame: buf, json }; - } } } finally { reader.releaseLock(); diff --git a/src/loop/adapter-openai.ts b/src/loop/adapter-openai.ts index 009c8fbd79..a195352343 100644 --- a/src/loop/adapter-openai.ts +++ b/src/loop/adapter-openai.ts @@ -5,7 +5,7 @@ import { buildVisibilityMarker } from "./core.js"; import { composeStreamFilters, createBiliArtifactFilter, createIdentityStreamFilter, createMarkerLineFilter, createTagEchoFilter } from "./tag-echo-filter.js"; import { degenerateTurnWarning } from "../degenerate-turn.js"; import { log as loggerLog } from "../logger.js"; -import { normalizeSseLineEndings, finalizeSseLineEndings, sseEventData, replaceSseEventData } from "../sse-util.js"; +import { createSseFramer, sseEventData, replaceSseEventData } from "../sse-util.js"; import { hardenOpenaiAssistantContent, systemToUser } from "../util.js"; import type { @@ -87,7 +87,7 @@ interface ToolCallBuffer { async function* iterSseChunks(stream: ReadableStream): AsyncGenerator { const reader = stream.getReader(); const decoder = new TextDecoder(); - let buf = ""; + const framer = createSseFramer(); try { while (true) { let done: boolean; @@ -98,23 +98,13 @@ async function* iterSseChunks(stream: ReadableStream): AsyncGenerato break; } if (done) break; - buf += decoder.decode(value, { stream: true }); - buf = normalizeSseLineEndings(buf); - let idx: number; - while ((idx = buf.indexOf("\n\n")) >= 0) { - const raw = buf.slice(0, idx); - buf = buf.slice(idx + 2); + for (const raw of framer.push(decoder.decode(value, { stream: true }))) { if (raw.trim().length > 0) yield raw; } } - buf = finalizeSseLineEndings(buf); - let idx: number; - while ((idx = buf.indexOf("\n\n")) >= 0) { - const raw = buf.slice(0, idx); - buf = buf.slice(idx + 2); + for (const raw of framer.finish()) { if (raw.trim().length > 0) yield raw; } - if (buf.trim().length > 0) yield buf; } finally { reader.releaseLock(); } diff --git a/src/loop/adapter-responses.ts b/src/loop/adapter-responses.ts index 4ed195e215..24f9d14605 100644 --- a/src/loop/adapter-responses.ts +++ b/src/loop/adapter-responses.ts @@ -9,7 +9,7 @@ import { hashId, strippedResponseIdWarning } from "../util.js"; import { composeStreamFilters, createBiliArtifactFilter, createIdentityStreamFilter, createMarkerLineFilter, createTagEchoFilter, stripResponsesText, containsBiliInternalText, containsMarkerLineText, containsRenderTagText, containsToolCallEmissionText, ACP_NAME_ALT, type TagEchoFilter } from "./tag-echo-filter.js"; import { degenerateTurnWarning } from "../degenerate-turn.js"; import { log as loggerLog } from "../logger.js"; -import { normalizeSseLineEndings, finalizeSseLineEndings } from "../sse-util.js"; +import { createSseFramer } from "../sse-util.js"; import { extractResponsesTextTriggers, PROXY_TOOL_NAMES } from "../compress-tool.js"; import type { BiliMessage } from "acp-kernel/wire"; import type { @@ -183,7 +183,7 @@ function rebuildResponsesEvent(type: string, obj: Record): Buff async function* iterSseEvents(stream: ReadableStream): AsyncGenerator { const reader = stream.getReader(); const decoder = new TextDecoder(); - let buf = ""; + const framer = createSseFramer(); try { while (true) { let done: boolean; @@ -194,23 +194,13 @@ async function* iterSseEvents(stream: ReadableStream): AsyncGenerato break; } if (done) break; - buf += decoder.decode(value, { stream: true }); - buf = normalizeSseLineEndings(buf); - let idx: number; - while ((idx = buf.indexOf("\n\n")) >= 0) { - const raw = buf.slice(0, idx); - buf = buf.slice(idx + 2); + for (const raw of framer.push(decoder.decode(value, { stream: true }))) { if (raw.trim().length > 0) yield raw; } } - buf = finalizeSseLineEndings(buf); - let idx: number; - while ((idx = buf.indexOf("\n\n")) >= 0) { - const raw = buf.slice(0, idx); - buf = buf.slice(idx + 2); + for (const raw of framer.finish()) { if (raw.trim().length > 0) yield raw; } - if (buf.trim().length > 0) yield buf; } finally { reader.releaseLock(); } diff --git a/src/sse-util.ts b/src/sse-util.ts index 9aa7f03a7f..0d7261fa1e 100644 --- a/src/sse-util.ts +++ b/src/sse-util.ts @@ -31,6 +31,56 @@ export function finalizeSseLineEndings(buf: string): string { return buf.replace(/\r\n|\r/g, "\n"); } +/** Frame newly decoded chunks without repeatedly scanning an unfinished + * event. Normalize only the new chunk plus a possible split CR, and join + * incomplete line fragments once when that line ends. Frames preserve the + * existing LF-normalized `split("\n\n")` semantics, including EOF residuals. */ +export function createSseFramer(): { + push(chunk: string): Generator; + finish(): Generator; +} { + let pendingCr = ""; + let lineParts: string[] = []; + let frameParts: string[] = []; + + function* push(chunk: string): Generator { + let text = normalizeSseLineEndings(pendingCr + chunk); + pendingCr = text.endsWith("\r") ? "\r" : ""; + if (pendingCr) text = text.slice(0, -1); + let start = 0; + let end: number; + while ((end = text.indexOf("\n", start)) !== -1) { + if (end > start) lineParts.push(text.slice(start, end)); + if (lineParts.length === 0 && frameParts.length > 0) { + // The preceding line's LF and this blank line's LF are the + // separator; neither belongs to the returned raw frame. + frameParts.pop(); + const frame = frameParts.join(""); + frameParts = []; + yield frame; + } else { + if (lineParts.length > 0) frameParts.push(lineParts.join("")); + frameParts.push("\n"); + } + lineParts = []; + start = end + 1; + } + if (start < text.length) lineParts.push(text.slice(start)); + } + + function* finish(): Generator { + // A final lone CR is one terminator. Resolving it with an LF uses + // the same split-CRLF path and contributes exactly one normalized LF. + if (pendingCr) yield* push("\n"); + const tail = [...frameParts, ...lineParts].join(""); + frameParts = []; + lineParts = []; + if (tail.length > 0) yield tail; + } + + return { push, finish }; +} + /** Data fields of one already-framed, LF-normalized SSE event form ONE * payload, joined by LF. Remove only the optional first space after `:`; * indentation and empty fields are part of the payload. */ diff --git a/tests/loop-sse-framing-scan.test.ts b/tests/loop-sse-framing-scan.test.ts new file mode 100644 index 0000000000..98b3725691 --- /dev/null +++ b/tests/loop-sse-framing-scan.test.ts @@ -0,0 +1,95 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { createGoogleAdapter } from "../src/loop/adapter-google.ts"; +import { createSseFramer, finalizeSseLineEndings } from "../src/sse-util.ts"; + +function chunks(wire: Buffer, chunkSize: number): ReadableStream { + let offset = 0; + return new ReadableStream({ pull(controller) { + if (offset >= wire.length) { controller.close(); return; } + controller.enqueue(wire.subarray(offset, offset + chunkSize)); + offset += chunkSize; + } }); +} + +function googleFrame(payload: string): string { + return `data: ${JSON.stringify({ candidates: [{ content: { role: "model", parts: [{ text: "Generated image." }, { inlineData: { mimeType: "image/png", data: payload } }] } }] })}\n\n`; +} + +async function googleOutput(wire: Buffer, chunkSize: number): Promise { + const adapter = createGoogleAdapter({}, undefined, undefined, "gemini-test"); + const parts: Buffer[] = []; + for await (const event of adapter.parseStream(chunks(wire, chunkSize), 1)) { + if (event.kind === "meta") parts.push(event.chunk); + else if ("raw" in event && event.raw) parts.push(event.raw); + } + return Buffer.concat(parts).toString("utf8"); +} + +test("Google loop framing scans newly received bytes, not the accumulated image prefix", async (t) => { + const frame = googleFrame("A".repeat(1024 * 1024)); + const wire = Buffer.from(frame); + const indexOf = String.prototype.indexOf; + let searchedChars = 0; + t.mock.method(String.prototype, "indexOf", function (this: string, needle: string, position?: number): number { + const value = String(this); + const result = indexOf.call(value, needle, position); + if (needle === "\r" || needle === "\n\n" || needle === "\n") { + const start = Math.max(0, position ?? 0); + searchedChars += Math.max(0, (result < 0 ? value.length : result + needle.length) - start); + } + return result; + }); + assert.equal(await googleOutput(wire, 4 * 1024), frame, "the complete image/text frame stays byte-exact"); + t.diagnostic(`SSE searches examined ${searchedChars} characters for ${wire.length} received bytes`); + assert.ok(searchedChars <= wire.length * 8, `SSE searches examined ${searchedChars} characters for ${wire.length} received bytes`); +}); + +for (const ending of ["\n", "\r\n", "\r"]) { + test(`Google loop framing retains Unicode across one-byte ${JSON.stringify(ending)} splits`, async () => { + const normalized = googleFrame("你好🔥"); + const wire = Buffer.from(normalized.replaceAll("\n", ending)); + assert.equal(await googleOutput(wire, 1), normalized); + }); +} + +function frameChunks(parts: string[]): string[] { + const framer = createSseFramer(); + return [...parts.flatMap(part => [...framer.push(part)]), ...framer.finish()]; +} + +function wholeFrames(text: string): string[] { + const frames = finalizeSseLineEndings(text).split("\n\n"); + if (frames.at(-1) === "") frames.pop(); + return frames; +} + +test("incremental framing preserves complete-field whitespace and EOF residuals at every split", () => { + const inputs = [ + "", "\n", "\r", "\r\n", "\n\n\n", "\n\ndata: tail", "\ndata: tail\n", + ":comment\r\nid: 1\r\nevent: update\r\ndata: {\r\ndata: \"text\": \"你好🔥\"\r\ndata: }\r\n\r\n", + "data\ndata: indented\ndata:\n\ndata: residual\n", + "event: partial\rdata: {\"text\":\"tail\"}\r", + ]; + for (const input of inputs) { + const expected = wholeFrames(input); + for (let cut = 0; cut <= input.length; cut++) { + assert.deepEqual(frameChunks([input.slice(0, cut), input.slice(cut)]), expected, `split ${cut}: ${JSON.stringify(input)}`); + } + assert.deepEqual(frameChunks(input.split("")), expected, `one-character chunks: ${JSON.stringify(input)}`); + } +}); + +test("all short CR/LF combinations match the previous whole-buffer framing", () => { + const alphabet = ["a", "\r", "\n"]; + let values = [""]; + for (let length = 0; length <= 6; length++) { + for (const value of values) { + const expected = wholeFrames(value); + for (let cut = 0; cut <= value.length; cut++) { + assert.deepEqual(frameChunks([value.slice(0, cut), value.slice(cut)]), expected); + } + } + values = values.flatMap(value => alphabet.map(char => value + char)); + } +}); From fb7047c777147b9c7434e8d19e4062ee6d3b96ec Mon Sep 17 00:00:00 2001 From: Afloat Date: Sun, 15 Nov 2026 23:28:44 +0800 Subject: [PATCH 05/24] fix: recover each Responses image from its own ref Use split image sidecars instead of expanding the whole carrier under every image ref; preserve unsplit and tool-result image groups. Fixes #6851 (cherry picked from commit f5aeb2827cd828a7562afd8d98424639c8b7d99a) --- src/image-restore.ts | 9 ++++++ tests/image-restore.test.ts | 61 +++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/src/image-restore.ts b/src/image-restore.ts index 5c59fe4d40..9ce61045a4 100644 --- a/src/image-restore.ts +++ b/src/image-restore.ts @@ -125,6 +125,15 @@ export function messageImageBytes(m: CoreMessage): RestorableImage[] { // there, so the index must too (#1995 nested-tool-image parity). const ri = mm.rawResponsesItem; if (isObj(ri)) { + // A split Responses image core still points at the WHOLE wire item. + // Its imageBase64 sidecar identifies this occurrence's own pixels; + // walking the carrier would bind every sibling image to every ref. + // A URL-only occurrence has no carried bytes, even if a sibling does. + if (mm.imagePart === true) { + return typeof mm.imageBase64 === "string" + ? [{ mediaType: mm.imageMediaType ?? "image/png", b64: mm.imageBase64, bytes: b64Bytes(mm.imageBase64) }] + : []; + } const arr = ri.type === "function_call_output" || ri.type === "custom_tool_call_output" ? (Array.isArray(ri.output) ? ri.output : undefined) : (Array.isArray(ri.content) ? ri.content : undefined); diff --git a/tests/image-restore.test.ts b/tests/image-restore.test.ts index 5dc702d26d..5e54494152 100644 --- a/tests/image-restore.test.ts +++ b/tests/image-restore.test.ts @@ -26,6 +26,13 @@ const PNG = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYPhfDwA const pngImg: RestorableImage = { mediaType: "image/png", b64: PNG, bytes: Buffer.byteLength(PNG, "base64") }; const dataUrl = `data:image/png;base64,${PNG}`; +const RED_PNG = "iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAE0lEQVR4AWP8z8DwnwEImBigAAAfFwICgH3ifwAAAABJRU5ErkJggg=="; +const BLUE_PNG = "iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAE0lEQVR4AWNkYPj/nwEImBigAAAdGQICxBbJ1AAAAABJRU5ErkJggg=="; + +function responseImage(b64: string) { + return { type: "input_image", image_url: `data:image/png;base64,${b64}` }; +} + // Each wire converter has a distinct concrete signature; the test drives them // uniformly on already-shaped bodies, so they are widened at the single call // site below rather than forced into one callable type here. @@ -343,3 +350,57 @@ test("buildIncomingImageIndex: a Gemini $ref without inline bytes adds no image" assert.deepEqual(imagePlaceholdersForSummary(message, "m00001"), []); assert.equal(buildIndex("google", body).size, 0); }); + +test("Responses split image refs restore only their own pixels", () => { + const index = buildIndex("responses", { model: "gpt", input: [{ type: "message", role: "user", content: [ + { type: "input_text", text: "Compare these two screenshots." }, responseImage(RED_PNG), responseImage(BLUE_PNG), + ] }] }, false, "responses-split-own-image"); + const entries = [...index.entries()]; + assert.equal(entries.length, 2); + assert.deepEqual(entries.map(([, images]) => images.map(image => readFileSync(image.path).toString("base64"))), [[RED_PNG], [BLUE_PNG]]); + const [blueRef, blueImages] = entries[1]; + const receipt = resolveDecompress({ imageRef: blueRef }, ctxWith(index)); + assert.equal(receipt.outcome, "success"); + assert.match(receipt.text, /Restored 1 image\(s\)/); + assert.ok(receipt.text.includes(blueImages[0].path)); + assert.ok(!receipt.text.includes(entries[0][1][0].path), "retrieving the second image must not return its sibling"); +}); + +test("Responses remote image refs do not borrow a carried sibling image", () => { + const body = { model: "gpt", input: [{ type: "message", role: "user", content: [ + { type: "input_image", image_url: "https://example.invalid/remote.png" }, responseImage(BLUE_PNG), + ] }] }; + const index = buildIndex("responses", body, false, "responses-remote-own-image"); + assert.deepEqual([...index.keys()], ["m00002"], "the remote-only ref has no carried pixels"); + assert.deepEqual(readFileSync(index.get("m00002")![0].path), Buffer.from(BLUE_PNG, "base64")); + assert.equal(resolveDecompress({ imageRef: "m00001" }, ctxWith(index)).outcome, "failure"); +}); + +test("Responses repeated identical images remain distinct one-image occurrences", () => { + const index = buildIndex("responses", { model: "gpt", input: [{ type: "message", role: "user", content: [responseImage(RED_PNG), responseImage(RED_PNG)] }] }, false, "responses-repeated-own-image"); + const images = [...index.values()]; + assert.deepEqual(images.map(value => value.length), [1, 1]); + assert.notEqual(images[0][0].path, images[1][0].path); + for (const value of images) assert.deepEqual(readFileSync(value[0].path), Buffer.from(RED_PNG, "base64")); +}); + +test("Responses image recovery creates one spill per split image, not a carrier cross-product", () => { + const content = Array.from({ length: 20 }, (_, i) => responseImage(i % 2 === 0 ? RED_PNG : BLUE_PNG)); + const index = buildIndex("responses", { model: "gpt", input: [{ type: "message", role: "user", content }] }, false, "responses-twenty-own-images"); + assert.equal(index.size, 20); + const files = [...index.values()].flatMap(images => images.map(image => image.path)); + assert.equal(files.length, 20); + assert.equal(new Set(files).size, 20); + assert.equal(files.reduce((total, file) => total + statSync(file).size, 0), 20 * Buffer.byteLength(RED_PNG, "base64")); +}); + +test("Responses unsplit carriers keep all images, including tool-result carriers", () => { + for (const [label, item] of [ + ["unknown-part", { type: "message", role: "user", content: [{ type: "input_text", text: "keep opaque carrier" }, { type: "future_part", payload: "opaque" }, responseImage(RED_PNG), responseImage(BLUE_PNG)] }], + ["tool-output", { type: "function_call_output", call_id: "tool-images", output: [responseImage(RED_PNG), responseImage(BLUE_PNG)] }], + ] as const) { + const index = buildIndex("responses", { model: "gpt", input: [item] }, false, `responses-unsplit-${label}`); + assert.equal(index.size, 1, label); + assert.deepEqual([...index.values()][0].map(image => readFileSync(image.path).toString("base64")), [RED_PNG, BLUE_PNG], label); + } +}); From ee1d2784ddf6e16206925275317f71ee196929cf Mon Sep 17 00:00:00 2001 From: Afloat Date: Tue, 17 Nov 2026 10:24:38 +0800 Subject: [PATCH 06/24] fix: retain active lanes during conversation recall eviction (cherry picked from commit 1689c96772bd027ccc92f8d6da13cc4a37242dff) --- src/agent/conversation-recall.ts | 13 +++-- ...conversation-recall-lane-retention.test.ts | 57 +++++++++++++++++++ 2 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 tests/conversation-recall-lane-retention.test.ts diff --git a/src/agent/conversation-recall.ts b/src/agent/conversation-recall.ts index a20e132d94..bfb0b8a469 100644 --- a/src/agent/conversation-recall.ts +++ b/src/agent/conversation-recall.ts @@ -82,11 +82,13 @@ function sweep(now: number): void { let oldestKey: string | undefined; let oldestAt = Infinity; for (const [laneKey, perLane] of lanes) { + let latestAt = -Infinity; for (const entry of perLane.values()) { - if (entry.at < oldestAt) { - oldestAt = entry.at; - oldestKey = laneKey; - } + latestAt = Math.max(latestAt, entry.at); + } + if (latestAt < oldestAt) { + oldestAt = latestAt; + oldestKey = laneKey; } } if (oldestKey === undefined) break; @@ -200,8 +202,7 @@ export function learnConversation(laneKey: string, sid: string, boundaryFp: stri } perLane.set(sid, { sid, boundaryFp, at: now }); stats.learned++; - stats.lanes = lanes.size; - stats.sessions = [...lanes.values()].reduce((n, l) => n + l.size, 0); + sweep(now); } /** diff --git a/tests/conversation-recall-lane-retention.test.ts b/tests/conversation-recall-lane-retention.test.ts new file mode 100644 index 0000000000..85f37a4213 --- /dev/null +++ b/tests/conversation-recall-lane-retention.test.ts @@ -0,0 +1,57 @@ +import assert from "node:assert/strict"; +import test from "node:test"; + +import { + __resetConversationRecallForTests, + conversationRecallStats, + learnConversation, + recallConversation, +} from "../src/agent/conversation-recall.ts"; + +test("lane eviction keeps a recent session even when its lane also has an older sibling", (t) => { + __resetConversationRecallForTests(); + t.after(__resetConversationRecallForTests); + let now = 1_000; + t.mock.method(Date, "now", () => now); + learnConversation("active-lane", "old-sibling", "old-boundary"); + for (let i = 0; i < 63; i++) { + now++; + learnConversation(`idle-lane-${i}`, `idle-${i}`, `idle-boundary-${i}`); + } + now++; + learnConversation("active-lane", "recent-session", "recent-boundary"); + now++; + learnConversation("new-lane", "new-session", "new-boundary"); + + assert.equal(recallConversation("active-lane", new Set(["recent-boundary"])), "recent-session"); + assert.equal(recallConversation("idle-lane-0", new Set(["idle-boundary-0"])), undefined); + assert.equal(recallConversation("new-lane", new Set(["new-boundary"])), "new-session"); + assert.equal(conversationRecallStats().lanes, 64); +}); + +test("record-only traffic leaves at most 64 remembered lanes", (t) => { + __resetConversationRecallForTests(); + t.after(__resetConversationRecallForTests); + let now = 1_000; + t.mock.method(Date, "now", () => now++); + for (let i = 0; i < 100; i++) { + learnConversation(`lane-${i}`, `session-${i}`, `boundary-${i}`); + assert.ok(conversationRecallStats().lanes <= 64); + } + assert.equal(recallConversation("lane-99", new Set(["boundary-99"])), "session-99"); + assert.equal(recallConversation("lane-0", new Set(["boundary-0"])), undefined); +}); + +test("a successful recall refreshes the activity of its entire lane", (t) => { + __resetConversationRecallForTests(); + t.after(__resetConversationRecallForTests); + let now = 1_000; + t.mock.method(Date, "now", () => now++); + learnConversation("active-lane", "old-sibling", "old-boundary"); + learnConversation("active-lane", "recent-session", "recent-boundary"); + for (let i = 0; i < 63; i++) learnConversation(`idle-lane-${i}`, `idle-${i}`, `idle-boundary-${i}`); + assert.equal(recallConversation("active-lane", new Set(["recent-boundary"])), "recent-session"); + learnConversation("new-lane", "new-session", "new-boundary"); + assert.equal(recallConversation("active-lane", new Set(["recent-boundary"])), "recent-session"); + assert.equal(recallConversation("idle-lane-0", new Set(["idle-boundary-0"])), undefined); +}); From aa4a23dad1ccf8f97807afbd55960f5a6ace854d Mon Sep 17 00:00:00 2001 From: ework-agent Date: Tue, 17 Nov 2026 11:07:44 +0800 Subject: [PATCH 07/24] refactor(recall): drop the now-redundant leading sweep in learnConversation The fix adds a trailing sweep(now) that enforces the 64-lane limit and refreshes stats after the insertion. The leading sweep(now) ran the same TTL + lane-limit + stats work with the same `now` moments earlier, so it is redundant. Removing it keeps one sweep per learn (the hot path taken on every attributed dispatch) with no behavior change: the per-lane LRU plus the trailing sweep reach the identical final state (verified: 18/18 targeted recall/fingerprint/dsh tests pass with and without it). (cherry picked from commit 3c9d19b37aede4088ad780a5d3c48b7c624ab629) --- src/agent/conversation-recall.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/agent/conversation-recall.ts b/src/agent/conversation-recall.ts index bfb0b8a469..e5a6acc1f2 100644 --- a/src/agent/conversation-recall.ts +++ b/src/agent/conversation-recall.ts @@ -180,7 +180,6 @@ export function recallBoundaryOfMessages(messages: unknown): string | undefined /** Learn (attribution ok): this session's latest dispatch boundary on this lane. */ export function learnConversation(laneKey: string, sid: string, boundaryFp: string): void { const now = Date.now(); - sweep(now); let perLane = lanes.get(laneKey); if (perLane === undefined) { perLane = new Map(); From 16db722f5c57eb5084811be68ec4a1db38f3b7be Mon Sep 17 00:00:00 2001 From: Afloat Date: Tue, 17 Nov 2026 10:42:58 +0800 Subject: [PATCH 08/24] perf: consume repeated canonical pointers in linear time (cherry picked from commit 244d52638520fe200e74b6299690630e2bbfd680) --- scripts/bench-canonical-repeated-pointers.ts | 29 +++++++++++ src/incremental-lane.ts | 3 +- tests/inc-canonical-repeated-pointers.test.ts | 51 +++++++++++++++++++ 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 scripts/bench-canonical-repeated-pointers.ts create mode 100644 tests/inc-canonical-repeated-pointers.test.ts diff --git a/scripts/bench-canonical-repeated-pointers.ts b/scripts/bench-canonical-repeated-pointers.ts new file mode 100644 index 0000000000..7de3b3851e --- /dev/null +++ b/scripts/bench-canonical-repeated-pointers.ts @@ -0,0 +1,29 @@ +/** node --import ./tests/preload-test-env.mjs --import tsx scripts/bench-canonical-repeated-pointers.ts */ +import assert from "node:assert/strict"; +import { performance } from "node:perf_hooks"; +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import type { BiliMessage } from "acp-kernel/wire"; +import { SessionStore, _setStoreForTest, _resetStoreForTest } from "../src/persist.ts"; +import { recordCanonicalPointers } from "../src/incremental-lane.ts"; +const dir = mkdtempSync(join(tmpdir(), "bili-pointer-bench-")); +const store = new SessionStore({ dir, enabled: true }); +_setStoreForTest(store); +try { + for (const count of [10_000, 50_000, 100_000]) { + const messages: BiliMessage[] = Array.from({ length: count }, () => ({ id: "h_0123456789abcdef", role: "assistant", contentType: "text", text: "ok" })); + const rows = messages.map((message, i) => ({ seq: i, content: JSON.stringify({ ...message, id: `${message.id}_${i + 1}` }), ts: null })); + store.unifiedRawMessages = () => rows; + const samples: number[] = []; + for (let run = 0; run < 3; run++) { + const start = performance.now(); + recordCanonicalPointers(`bench-${count}-${run}`, messages, () => {}); + samples.push(performance.now() - start); + assert.equal((store.getSessionExt(`bench-${count}-${run}`, "inc:canonical-ptrs") as number[]).length, count); + } + console.log(JSON.stringify({ count, milliseconds: samples, medianMs: [...samples].sort((a,b)=>a-b)[1] })); + } +} finally { + _resetStoreForTest(); rmSync(dir, { recursive: true, force: true }); +} diff --git a/src/incremental-lane.ts b/src/incremental-lane.ts index 9b8f6b4d9c..27c7da2589 100644 --- a/src/incremental-lane.ts +++ b/src/incremental-lane.ts @@ -460,10 +460,11 @@ export function recordCanonicalPointers(sessionId: string, msgs: BiliMessage[], // unparseable row -> skip; a miss keeps the previous pointers } } + for (const bucket of byStable.values()) bucket.reverse(); for (let i = ptrs.length; i < msgs.length; i++) { const m = msgs[i]!; const bucket = byStable.get(stableMessageKey(m as unknown as Record).replace(CLUSTER_ID_SUFFIX_RE, "$1")); - const seq = bucket === undefined ? undefined : bucket.shift(); + const seq = bucket?.pop(); if (seq === undefined) { // #5155: with the debounced save the unsettled tail is the // common case right after a turn — record the settled prefix diff --git a/tests/inc-canonical-repeated-pointers.test.ts b/tests/inc-canonical-repeated-pointers.test.ts new file mode 100644 index 0000000000..21fc44ed50 --- /dev/null +++ b/tests/inc-canonical-repeated-pointers.test.ts @@ -0,0 +1,51 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import type { BiliMessage } from "acp-kernel/wire"; +import { SessionStore, _setStoreForTest, _resetStoreForTest } from "../src/persist.ts"; +import { recordCanonicalPointers } from "../src/incremental-lane.ts"; + +const repeated = (id = "h_0123456789abcdef"): BiliMessage => ({ id, role: "assistant", contentType: "text", text: "ok" }); + +test("repeated content consumes settled row pointers in order, including across incremental updates", (t) => { + const dir = mkdtempSync(join(tmpdir(), "bili-repeat-ptrs-")); + const store = new SessionStore({ dir, enabled: true }); + _setStoreForTest(store); + t.after(() => { _resetStoreForTest(); rmSync(dir, { recursive: true, force: true }); }); + const count = 25_000; + const rows = Array.from({ length: count + 2 }, (_, i) => ({ + seq: i * 2 + 10, content: JSON.stringify(repeated(`h_0123456789abcdef_${i + 1}`)), ts: null, + })); + t.mock.method(store, "unifiedRawMessages", (_sid: string, from?: number) => rows.filter((row) => from === undefined || row.seq > from)); + const messages = Array.from({ length: count }, () => repeated()); + const warnings: string[] = []; + const log = (level: string, text: string) => { if (level === "warn") warnings.push(text); }; + recordCanonicalPointers("repeated", messages, log); + assert.deepEqual(store.getSessionExt("repeated", "inc:canonical-ptrs"), rows.slice(0, count).map((row) => row.seq)); + assert.deepEqual(messages.map((message) => message.ledgerSeq), rows.slice(0, count).map((row) => row.seq + 1)); + messages.push(repeated(), repeated()); + recordCanonicalPointers("repeated", messages, log); + assert.deepEqual(store.getSessionExt("repeated", "inc:canonical-ptrs"), rows.map((row) => row.seq)); + assert.deepEqual(messages.slice(-2).map((message) => message.ledgerSeq), rows.slice(-2).map((row) => row.seq + 1)); + assert.deepEqual(warnings, []); +}); + +test("interleaved content buckets preserve canonical order and stop at the first unsettled message", (t) => { + const dir = mkdtempSync(join(tmpdir(), "bili-mixed-ptrs-")); + const store = new SessionStore({ dir, enabled: true }); + _setStoreForTest(store); + t.after(() => { _resetStoreForTest(); rmSync(dir, { recursive: true, force: true }); }); + const other: BiliMessage = { id: "other", role: "user", contentType: "text", text: "next" }; + const rows = [ + { seq: 2, content: JSON.stringify(repeated("h_0123456789abcdef_1")), ts: null }, + { seq: 7, content: JSON.stringify(other), ts: null }, + { seq: 11, content: JSON.stringify(repeated("h_0123456789abcdef_2")), ts: null }, + ]; + t.mock.method(store, "unifiedRawMessages", () => rows); + const messages = [repeated(), { ...other }, repeated(), { ...other }, repeated()]; + recordCanonicalPointers("mixed", messages, () => {}); + assert.deepEqual(store.getSessionExt("mixed", "inc:canonical-ptrs"), [2, 7, 11]); + assert.deepEqual(messages.map((message) => message.ledgerSeq), [3, 8, 12, undefined, undefined]); +}); From 7f432cbfe52e4c0e63ee90535ad51ee5b5a54f7f Mon Sep 17 00:00:00 2001 From: Afloat Date: Tue, 17 Nov 2026 17:34:31 +0800 Subject: [PATCH 09/24] test: pin repeated-pointer fixtures to unified storage The repeated-pointer assertions use the unified session sidecar. Pin the fixture to full storage mode, matching the neighboring canonical-pointer suite, so the legacy CI default does not prevent the tests from reaching their assertions. (cherry picked from commit 5704fc5aca4f182a2320317a72b227dd27903c08) --- tests/inc-canonical-repeated-pointers.test.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/inc-canonical-repeated-pointers.test.ts b/tests/inc-canonical-repeated-pointers.test.ts index 21fc44ed50..1f7b5a39ff 100644 --- a/tests/inc-canonical-repeated-pointers.test.ts +++ b/tests/inc-canonical-repeated-pointers.test.ts @@ -7,6 +7,8 @@ import type { BiliMessage } from "acp-kernel/wire"; import { SessionStore, _setStoreForTest, _resetStoreForTest } from "../src/persist.ts"; import { recordCanonicalPointers } from "../src/incremental-lane.ts"; +process.env.BILI_STORAGE_UNIFIED = "full"; + const repeated = (id = "h_0123456789abcdef"): BiliMessage => ({ id, role: "assistant", contentType: "text", text: "ok" }); test("repeated content consumes settled row pointers in order, including across incremental updates", (t) => { From 4dd000d03fd6330c4cbf818c67298c06417117f1 Mon Sep 17 00:00:00 2001 From: Afloat Date: Tue, 17 Nov 2026 10:52:44 +0800 Subject: [PATCH 10/24] fix: search ancestor originals in derived sessions (cherry picked from commit 30f92ff16cc02f60f9b83203e33c79a0e0937da1) --- src/decompress-shared.ts | 4 +- tests/derived-original-search.test.ts | 72 +++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 tests/derived-original-search.test.ts diff --git a/src/decompress-shared.ts b/src/decompress-shared.ts index f5eabd5603..ae17b50e0a 100644 --- a/src/decompress-shared.ts +++ b/src/decompress-shared.ts @@ -997,8 +997,8 @@ export function executeSearchContextTarget( const self2 = peekSession(sessionId); if (self2) { for (const anc of derivedAncestorSessions(self2)) { - if (!searchBlocks(blockDocs(anc.state), String(args.query ?? ""), { limit: anc.state.blocks.length, algorithm: "substring", minScore: 1e-9 }).length) continue; - return executeSearchContext(args, core, anc.state, anc.id, undefined, originalTextsOf(anc)); + const inherited = executeSearchContext(args, core, anc.state, anc.id, undefined, originalTextsOf(anc)); + if (inherited.text.startsWith("Found ")) return inherited; } } } diff --git a/tests/derived-original-search.test.ts b/tests/derived-original-search.test.ts new file mode 100644 index 0000000000..780fd07039 --- /dev/null +++ b/tests/derived-original-search.test.ts @@ -0,0 +1,72 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { createCore, createInitialState, defaultConfig, type CoreMessage } from "acp-kernel"; +import { executeSearchContextTarget } from "../src/decompress-shared.ts"; +import { getSession, _resetSessionsForTest, type Session } from "../src/session.ts"; +import { SessionStore, _setStoreForTest, _resetStoreForTest } from "../src/persist.ts"; + +const core = createCore({ countTokens: (text) => Math.ceil(text.length / 4) }); +const config = defaultConfig(100000, { compress: { minCompressRange: 0, minSummaryLength: 1, maxSummaryLength: 5000 }, preserveRecentMessages: 2, preserveRecentTokens: 0 }); + +function session(id: string): Session { + return getSession(id, { protocol: "openai", upstreamOrigin: "http://upstream" }); +} + +function folded(id: string, original: string): Session { + const target = session(id); + const messages: CoreMessage[] = Array.from({ length: 12 }, (_, i) => ({ + id: `message-${i}`, role: i % 2 ? "assistant" : "user", contentType: "text", text: `Conversation message ${i}: ${original} `.repeat(10), + })); + const turn = core.processTurn({ messages, state: createInitialState(), config, tokenCount: 2000 }); + const compressed = core.applyCompression({ ranges: [{ startRef: "m00001", endRef: "m00008", summary: "A general discussion of project decisions and earlier work." }], state: turn.state, messages: turn.messages, config }); + assert.equal(compressed.result.blocksCreated, 1); + target.state = compressed.state; + target.blockContents.set(target.state.blocks[0]!.blockId, { one: { text: "A general discussion.", count: 8 }, full: { text: original, count: 8 } }); + return target; +} + +test.beforeEach(() => { _resetSessionsForTest(); _setStoreForTest(new SessionStore({ enabled: false })); }); +test.afterEach(() => { _resetSessionsForTest(); _resetStoreForTest(); }); + +test("a derived child can find text retained only in its parent's folded originals", () => { + const parent = folded("original-parent", "The deployment codename is zephyronlyarchive."); + const child = session("original-child"); + child.metadata.derivedFromSessionId = parent.id; + const before = JSON.stringify({ state: parent.state, contents: [...parent.blockContents] }); + const direct = executeSearchContextTarget({ query: "zephyronlyarchive", conversation_id: parent.id }, core, child.id, child.state); + assert.match(direct.text, /Original-text hit\(s\)/); + const inherited = executeSearchContextTarget({ query: "zephyronlyarchive" }, core, child.id, child.state); + assert.match(inherited.text, /Original-text hit\(s\)/); + assert.match(inherited.text, /in session original-parent/); + assert.match(inherited.text, /zephyronlyarchive/); + assert.equal(JSON.stringify({ state: parent.state, contents: [...parent.blockContents] }), before); +}); + +test("ancestor fallback continues past a parent with no match to a grandparent's originals", () => { + const grandparent = folded("original-grandparent", "The earlier launch key is grandarchivekey."); + const parent = folded("unmatched-parent", "Different retained content."); + parent.metadata.derivedFromSessionId = grandparent.id; + const child = session("grandchild"); + child.metadata.derivedFromSessionId = parent.id; + const found = executeSearchContextTarget({ query: "grandarchivekey" }, core, child.id, child.state); + assert.match(found.text, /in session original-grandparent/); + assert.match(found.text, /Original-text hit\(s\)/); +}); + +test("current-session original hits win, and unlinked sessions remain isolated", () => { + const parent = folded("priority-parent", "sharedarchivekey in parent"); + const child = folded("priority-child", "sharedarchivekey in child"); + child.metadata.derivedFromSessionId = parent.id; + const local = executeSearchContextTarget({ query: "sharedarchivekey" }, core, child.id, child.state, { session: child, config, messages: [], log: () => {} }); + assert.match(local.text, /sharedarchivekey in child/); + assert.doesNotMatch(local.text, /in session priority-parent/); + const unlinked = session("unlinked"); + assert.match(executeSearchContextTarget({ query: "sharedarchivekey" }, core, unlinked.id, unlinked.state).text, /^\[No compressed blocks exist yet/); +}); + +test("an empty ancestor search keeps the child's original empty result", () => { + const parent = folded("empty-parent", "Unrelated original."); + const child = session("empty-child"); + child.metadata.derivedFromSessionId = parent.id; + assert.equal(executeSearchContextTarget({ query: "missingarchivekey" }, core, child.id, child.state).text, "[No compressed blocks exist yet — nothing to search.]"); +}); From 6ff8939492e1456142f9b1fed410809db3df258c Mon Sep 17 00:00:00 2001 From: Afloat Date: Tue, 17 Nov 2026 11:12:24 +0800 Subject: [PATCH 11/24] fix: preserve long histories when replacing the compression view (cherry picked from commit cb4b5af8df4fb7f7edd63c797d1753916bed8f34) --- src/loop/core.ts | 16 ++++--- tests/loop-compress.test.ts | 91 +++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 6 deletions(-) diff --git a/src/loop/core.ts b/src/loop/core.ts index fa301f487c..5f73b25bea 100644 --- a/src/loop/core.ts +++ b/src/loop/core.ts @@ -53,6 +53,13 @@ import { safePrefix, safeSuffix, scrubLoneSurrogateEscapesOnJson } from "../text export const MAX_LOOP_ROUNDS = 10; +/** Keep the shared view's identity without expanding long histories into function arguments. */ +function replaceMessages(target: CoreMessage[], source: readonly CoreMessage[]): void { + if (target === source) return; + target.length = 0; + for (const message of source) target.push(message); +} + /** #5220: canonical signature of a failed proxy call — sorted-key JSON so * argument-order noise doesn't defeat the repeat-detection set. Unparseable * arguments fall back to the raw string (byte equality still catches the @@ -1645,8 +1652,7 @@ export async function* runCompressLoop( const hidden = hideConsumedCompressCalls(ctx.session.state, coreMessages, ctx.config.isCompressTool); if (hidden.hidden > 0) { ctx.log(`[acp-loop] round ${round} hideConsumed hid ${hidden.hidden} compress record(s)`); - coreMessages.length = 0; - coreMessages.push(...hidden.messages); + replaceMessages(coreMessages, hidden.messages); } } // #422: a successful compress changed the session state — refresh @@ -1665,8 +1671,7 @@ export async function* runCompressLoop( try { const refreshed = await ctx.refreshFolded(coreMessages); if (refreshed.length > 0) { - coreMessages.length = 0; - coreMessages.push(...refreshed); + replaceMessages(coreMessages, refreshed); // The fold has now materialized in a request the // model actually sees — the next usage report is // post-fold reality; keeping the credit would net @@ -1852,8 +1857,7 @@ export async function* runCompressLoop( } degradedRetried = true; const stripped = stripLoopThinking(coreMessages); - coreMessages.length = 0; - coreMessages.push(...stripped); + replaceMessages(coreMessages, stripped); ctx.log(`[acp-loop] round ${round}: re-request rejected (${e.status}: ${e.body.slice(0, 200)}); retrying without replayed thinking blocks`); loggerLog("warn", `[acp-loop] re-request rejected (${e.status}); retrying without thinking replay: ${e.body.slice(0, 200)}`); newBody = await buildLoopRequestBody(); diff --git a/tests/loop-compress.test.ts b/tests/loop-compress.test.ts index 9920f4fa5c..0cc7ca8236 100644 --- a/tests/loop-compress.test.ts +++ b/tests/loop-compress.test.ts @@ -54,6 +54,97 @@ function fcEvents(outputIndex: number, callId: string, name: string, args: strin const COMPLETED = sse("response.completed", { response: { id: "resp_done", status: "completed", output: [] } }); +test("successful compress keeps every message in a large refreshed history", async () => { + const ctx = withRefs(makeCtx(Array.from({ length: 7 }, (_, i) => + textMsg(`raw_${i}`, i % 2 === 0 ? "user" : "assistant", `original ${i} ${bigText(5000)}`), + ))); + const refreshed = Array.from({ length: 250_000 }, (_, i) => + textMsg(`folded_${i}`, i % 2 === 0 ? "user" : "assistant", `retained history ${i}`), + ); + const logs: string[] = []; + ctx.log = (line) => logs.push(line); + let refreshCalls = 0; + ctx.refreshFolded = () => { refreshCalls++; return refreshed; }; + const args = JSON.stringify({ content: [{ startId: "m00001", endId: "m00002", summary: "The first two messages were compressed; all remaining history must reach the next request." }] }); + const originalFetch = globalThis.fetch; + const requests: string[] = []; + globalThis.fetch = async (_url, init) => { + requests.push(String(init?.body)); + return new Response(COMPLETED); + }; + try { + const out = await drain(new Response(fcEvents(0, "large_fold", "compress", args) + COMPLETED).body!, ctx, + { model: "test", input: [], stream: true }, { url: "http://mock", headers: {} }); + assert.equal(refreshCalls, 1, "the real compression succeeded"); + assert.equal(requests.length, 1); + const history = requests[0]!.match(/retained history \d+/g) ?? []; + assert.equal(history.length, refreshed.length); + assert.equal(history[0], "retained history 0"); + assert.equal(history.at(-1), "retained history 249999"); + assert.equal(refreshed.length, 250_000, "the host-owned view is not mutated"); + assert.equal(logs.some((line) => line.includes("fold refresh failed")), false); + assert.match(out, /response\.completed/); + } finally { + globalThis.fetch = originalFetch; + } +}); + +test("successful compress accepts a refresh hook returning its current view", async () => { + const ctx = withRefs(makeCtx(Array.from({ length: 7 }, (_, i) => + textMsg(`raw_${i}`, i % 2 === 0 ? "user" : "assistant", `identity history ${i} ${bigText(5000)}`), + ))); + let refreshCalls = 0; + ctx.refreshFolded = (current) => { refreshCalls++; return current; }; + const args = JSON.stringify({ content: [{ startId: "m00001", endId: "m00002", summary: "A successful compression with a host hook retaining its existing in-place view." }] }); + const originalFetch = globalThis.fetch; + const requests: string[] = []; + globalThis.fetch = async (_url, init) => { + requests.push(String(init?.body)); + return new Response(COMPLETED); + }; + try { + await drain(new Response(fcEvents(0, "same_fold", "compress", args) + COMPLETED).body!, ctx, + { model: "test", input: [], stream: true }, { url: "http://mock", headers: {} }); + assert.equal(refreshCalls, 1); + assert.equal(requests.length, 1); + assert.ok(requests[0]!.includes("identity history 0")); + assert.ok(requests[0]!.includes("identity history 6")); + assert.ok(requests[0]!.includes("[Compressed m00001")); + } finally { + globalThis.fetch = originalFetch; + } +}); + +test("consumed-call cleanup preserves a large history during a proxy-tool round", async () => { + const history = Array.from({ length: 250_000 }, (_, i) => + textMsg(`history_${i}`, i % 2 === 0 ? "user" : "assistant", `kept message ${i}`), + ); + const ctx = makeCtx(history); + ctx.messages.push( + { id: "consumed_call", role: "assistant", contentType: "tool-call", toolName: "compress", toolCallId: "old_compress", text: "{}" }, + { id: "consumed_result", role: "tool", contentType: "tool-result", toolName: "compress", toolCallId: "old_compress", text: "obsolete compression result" }, + ); + ctx.session.state.sweptOrphanCallIds = ["old_compress"]; + const originalFetch = globalThis.fetch; + const requests: string[] = []; + globalThis.fetch = async (_url, init) => { + requests.push(String(init?.body)); + return new Response(COMPLETED); + }; + try { + await drain(new Response(fcEvents(0, "check_status", "acp_status", "{}") + COMPLETED).body!, ctx, + { model: "test", input: [], stream: true }, { url: "http://mock", headers: {} }); + assert.equal(requests.length, 1); + assert.equal((requests[0]!.match(/kept message \d+/g) ?? []).length, 250_000); + assert.match(requests[0]!, /kept message 0/); + assert.match(requests[0]!, /kept message 249999/); + assert.doesNotMatch(requests[0]!, /obsolete compression result/); + assert.equal(ctx.messages.length, 250_002, "the caller's history is not rewritten"); + } finally { + globalThis.fetch = originalFetch; + } +}); + const SYS_PROMPT = buildCompressSystemPrompt(); async function drain( From 4f90659830de1ebff9e5cec3ecf929f321f97a60 Mon Sep 17 00:00:00 2001 From: Afloat Date: Tue, 17 Nov 2026 14:22:40 +0800 Subject: [PATCH 12/24] test: cover large histories in the thinking-strip retry (cherry picked from commit 009ea1633be2d6a383ebbb18c30d43cad3b867d7) --- tests/loop-compress.test.ts | 58 ++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/tests/loop-compress.test.ts b/tests/loop-compress.test.ts index 0cc7ca8236..ac3cb7807a 100644 --- a/tests/loop-compress.test.ts +++ b/tests/loop-compress.test.ts @@ -5,7 +5,7 @@ import { tmpdir } from "node:os"; import type { Config, CoreMessage } from "acp-kernel"; import { createCore, createInitialState, assignRefs, emptyRefMap, defaultConfig } from "acp-kernel"; import type { Session } from "../src/session.ts"; -import { runCompressLoop, createResponsesAdapter } from "../src/loop/index.ts"; +import { runCompressLoop, createResponsesAdapter, createAnthropicAdapter } from "../src/loop/index.ts"; import { buildCompressSystemPrompt } from "../src/compress-tool.ts"; import { REPLAY_MAX_ATTEMPTS } from "../src/fetch-util.ts"; @@ -17,6 +17,7 @@ function makeCtx(messages: CoreMessage[] = []): { log: (m: string) => void; proxyUrl?: string; textProtocol?: boolean; + protocol?: "anthropic" | "google"; refreshFolded?: (current: CoreMessage[]) => CoreMessage[] | Promise; } { return { @@ -145,6 +146,61 @@ test("consumed-call cleanup preserves a large history during a proxy-tool round" } }); +test("thinking-strip retry keeps a large history after an Anthropic 400", async () => { + const count = 130_000; + const history = Array.from({ length: count }, (_, i) => + textMsg(`retry_${i}`, i % 2 === 0 ? "user" : "assistant", `retained retry history ${i}`), + ); + const ctx = makeCtx(history); + ctx.protocol = "anthropic"; + const firstRound = [ + sse("message_start", { type: "message_start", message: { id: "msg_first", role: "assistant", content: [], usage: { input_tokens: 100, output_tokens: 5 } } }), + sse("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "thinking", thinking: "" } }), + sse("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "thinking_delta", thinking: "optional reasoning from this round" } }), + sse("content_block_stop", { type: "content_block_stop", index: 0 }), + sse("content_block_start", { type: "content_block_start", index: 1, content_block: { type: "tool_use", id: "retry_status", name: "acp_status", input: {} } }), + sse("content_block_delta", { type: "content_block_delta", index: 1, delta: { type: "input_json_delta", partial_json: "{}" } }), + sse("content_block_stop", { type: "content_block_stop", index: 1 }), + sse("message_delta", { type: "message_delta", delta: { stop_reason: "tool_use" }, usage: { output_tokens: 5 } }), + sse("message_stop", { type: "message_stop" }), + ].join(""); + const terminal = [ + sse("message_start", { type: "message_start", message: { id: "msg_done", role: "assistant", content: [], usage: { input_tokens: 10, output_tokens: 2 } } }), + sse("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }), + sse("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "recovered" } }), + sse("content_block_stop", { type: "content_block_stop", index: 0 }), + sse("message_delta", { type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 2 } }), + sse("message_stop", { type: "message_stop" }), + ].join(""); + const originalFetch = globalThis.fetch; + const requests: string[] = []; + globalThis.fetch = async (_url, init) => { + requests.push(String(init?.body)); + return requests.length === 1 + ? new Response(JSON.stringify({ error: { message: "internal policy violation" } }), { status: 400, headers: { "content-type": "application/json" } }) + : new Response(terminal, { headers: { "content-type": "text/event-stream" } }); + }; + try { + const body = { model: "claude-test", max_tokens: 512, stream: true }; + const chunks: Buffer[] = []; + for await (const chunk of runCompressLoop(new Response(firstRound).body!, ctx, body, + { url: "http://mock", headers: {} }, createAnthropicAdapter(body), SYS_PROMPT)) { + chunks.push(chunk); + } + assert.equal(requests.length, 2, "a neutral 400 gets the one permitted thinking-strip retry"); + const second = JSON.parse(requests[1]!) as { messages: Array<{ content?: Array> }> }; + assert.equal(second.messages.flatMap((m) => Array.isArray(m.content) ? m.content : []).some((c) => c.type === "thinking"), false); + const retained = requests[1]!.match(/retained retry history \d+/g) ?? []; + assert.equal(retained.length, count); + assert.equal(retained[0], "retained retry history 0"); + assert.equal(retained.at(-1), "retained retry history 129999"); + assert.match(Buffer.concat(chunks).toString("utf8"), /recovered/); + assert.equal(history.length, count, "the caller's original view is unchanged"); + } finally { + globalThis.fetch = originalFetch; + } +}); + const SYS_PROMPT = buildCompressSystemPrompt(); async function drain( From cf083aa7a0dc25a52642dd3f9ef825a5a3584ee9 Mon Sep 17 00:00:00 2001 From: Afloat Date: Tue, 17 Nov 2026 11:19:05 +0800 Subject: [PATCH 13/24] fix: retain the longest active upstream throttle deadline (cherry picked from commit ea4f9c782143e80047b5d1cebf216551c81cc15a) --- src/upstream-throttle.ts | 2 ++ tests/issue4204-throttle-governor.test.ts | 36 +++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/upstream-throttle.ts b/src/upstream-throttle.ts index 7a2c7b50ac..ae530aaa4f 100644 --- a/src/upstream-throttle.ts +++ b/src/upstream-throttle.ts @@ -746,6 +746,8 @@ export function noteUpstreamThrottle429( const stateKey = scopeModel === undefined ? key : modelHoldKey(key, scopeModel); const previous = holds.get(stateKey); const observedState = createHoldState(key, host, cred, retryAfterHeader, now, previous, scopeModel, false); + // Concurrent refusals may omit Retry-After; they cannot shorten an existing hold for this scope. + if (previous) observedState.nextAttemptAt = Math.max(observedState.nextAttemptAt, previous.nextAttemptAt); if (previous === undefined) { if (!storeHold(stateKey, observedState, scopeModel !== undefined)) { // #5199/#5889: the hold map is saturated (MAX_HOLDS full, nothing diff --git a/tests/issue4204-throttle-governor.test.ts b/tests/issue4204-throttle-governor.test.ts index b78cde6a44..4e5b9b2f53 100644 --- a/tests/issue4204-throttle-governor.test.ts +++ b/tests/issue4204-throttle-governor.test.ts @@ -50,6 +50,42 @@ function withKnobs(vars: Partial>, fn } } +for (const model of [undefined, "limited-model"]) { + for (const retryAfter of [null, "1", "invalid"]) { + test(`a later ${model ? "model" : "account"} 429 with Retry-After ${String(retryAfter)} keeps the existing longer hold`, () => { + withKnobs({ BILI_THROTTLE_429: "1", BILI_THROTTLE_429_MAX_CONSECUTIVE: "8", BILI_THROTTLE_429_BASE_DELAY_MS: "1000", BILI_THROTTLE_429_MAX_DELAY_MS: "8000" }, () => { + _resetUpstreamThrottleForTest(); + noteUpstreamThrottle429(URL_A, H_CRED_A, "300", T0, model); + noteUpstreamThrottle429(URL_A, H_CRED_A, retryAfter, T0 + 1000, model); + const hold = activeThrottleHolds()[0]!; + assert.equal(hold.nextAttemptAt, T0 + 300_000); + assert.equal(hold.streak, 2); + const rejected = upstreamThrottleGate(URL_A, H_CRED_A, T0 + 20_000, model).reject; + assert.ok(rejected, "concurrent responses must not admit an early provider probe"); + assert.equal(rejected.status, 429); + assert.equal(rejected.retryAfterSec, 280); + assert.equal(upstreamThrottleGate(URL_A, H_CRED_B, T0 + 20_000, model).reject, undefined); + if (model) assert.equal(upstreamThrottleGate(URL_A, H_CRED_A, T0 + 20_000, "other-model").reject, undefined); + const probe = upstreamThrottleGate(URL_A, H_CRED_A, hold.nextAttemptAt, model); + assert.equal(probe.reject, undefined); + assert.equal(typeof probe.probeToken, "symbol"); + noteUpstreamThrottleResolved(URL_A, H_CRED_A, 200, hold.nextAttemptAt + 1, model, probe.probeToken); + assert.equal(activeThrottleHolds().length, 0); + }); + }); + } +} + +test("a later HTTP-date Retry-After can extend an existing hold", () => { + withKnobs({ BILI_THROTTLE_429: "1", BILI_THROTTLE_429_MAX_CONSECUTIVE: "8", BILI_THROTTLE_429_BASE_DELAY_MS: "1000", BILI_THROTTLE_429_MAX_DELAY_MS: "8000" }, () => { + _resetUpstreamThrottleForTest(); + noteUpstreamThrottle429(URL_A, H_CRED_A, "30", T0); + const deadline = T0 + 120_000; + noteUpstreamThrottle429(URL_A, H_CRED_A, new Date(deadline).toUTCString(), T0 + 1000); + assert.equal(activeThrottleHolds()[0]!.nextAttemptAt, deadline); + }); +}); + test("#4204: request model extraction is not limited by the error-body cap", () => { const model = "deepseek-v4.1-flash"; const body = JSON.stringify({ model, messages: [{ content: "x".repeat(20 * 1024) }] }); From f8f58d3ab14f48530f3a6eb17e6f69b44da696f9 Mon Sep 17 00:00:00 2001 From: Afloat Date: Tue, 17 Nov 2026 11:32:03 +0800 Subject: [PATCH 14/24] fix: close pending WebSocket upgrades during server shutdown (cherry picked from commit 6b08f615ddd3f50c1a632277bb9e24373615309f) --- src/ws-bridge.ts | 21 ++++++- tests/ws-bridge-pending-close.test.ts | 91 +++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 tests/ws-bridge-pending-close.test.ts diff --git a/src/ws-bridge.ts b/src/ws-bridge.ts index 2b5debf985..a2e60ea92f 100644 --- a/src/ws-bridge.ts +++ b/src/ws-bridge.ts @@ -93,9 +93,17 @@ export function installWebSocketBridge( ): (req: http.IncomingMessage, socket: Duplex, head: Buffer) => boolean { const wss = new WebSocketServer({ noServer: true, maxPayload: limitsReceiveBytes(), perMessageDeflate: false }); const sessions = new Set<{ session: WsBridgeSession; peer: WebSocket }>(); + const pendingUpgrades = new Set(); + let closing = false; + const stopPendingUpgrades = (): void => { + closing = true; + for (const socket of pendingUpgrades) socket.destroy(); + pendingUpgrades.clear(); + }; let connectionId = 0; const close = server.close.bind(server); server.close = callback => { + stopPendingUpgrades(); const live = [...sessions]; sessions.clear(); // exactly-once: the 'close' event backstop below must not re-run for (const { session } of live) session.shutdown("server-close"); @@ -103,6 +111,7 @@ export function installWebSocketBridge( return close(callback); }; server.on("close", () => { + stopPendingUpgrades(); for (const { session } of sessions) session.shutdown("server-close"); for (const peer of wss.clients) peer.terminate(); wss.close(); @@ -118,7 +127,14 @@ export function installWebSocketBridge( return upstream === undefined ? [] : [{ codec: candidate, upstream, conversationId }]; })[0] : undefined; if (!claim) return false; + if (closing) { socket.destroy(); return true; } const { codec, upstream, conversationId } = claim; + // HTTP has already handed this socket to the upgrade listener, but it + // is not a WebSocket client until async destination admission finishes. + // Keep it in the shutdown set throughout that gap, including DNS waits. + pendingUpgrades.add(socket); + const forgetPending = (): void => { pendingUpgrades.delete(socket); }; + socket.once("close", forgetPending); // Stamp the codec name onto the upgrade request BEFORE the session is // created: codecs rebuild in-process HTTP envelopes from `source`, so // every envelope inherits this marker and the request pipeline can tell @@ -128,11 +144,11 @@ export function installWebSocketBridge( source.headers["x-bili-ws-lane"] = codec.name; void (async () => { const verdict = await checkTunnelDestination(upstream, { selfPort: source.socket.localPort, clientLoopback: true, allowlist: tunnelAllowlistFromEnv(), egressProxied: isEgressProxied?.(upstream) }); + if (closing || socket.destroyed) return; if (!verdict.ok) { socket.end("HTTP/1.1 403 Forbidden\r\nConnection: close\r\nContent-Length: 0\r\n\r\n"); return; } - if (socket.destroyed) return; wss.handleUpgrade(source, socket, head, peer => { const label = `[${codec.name}] [conn=${++connectionId}] [session=${JSON.stringify(conversationId.slice(0, 128))}]`; const trace: WsBridgeLog = (level, message) => log(level, `${label} ${message}`); @@ -149,6 +165,9 @@ export function installWebSocketBridge( })().catch(() => { log("warn", `[${codec.name}] upgrade failed`); if (!socket.destroyed) socket.end("HTTP/1.1 502 Bad Gateway\r\nConnection: close\r\nContent-Length: 0\r\n\r\n"); + }).finally(() => { + forgetPending(); + socket.off("close", forgetPending); }); return true; }; diff --git a/tests/ws-bridge-pending-close.test.ts b/tests/ws-bridge-pending-close.test.ts new file mode 100644 index 0000000000..05ab031718 --- /dev/null +++ b/tests/ws-bridge-pending-close.test.ts @@ -0,0 +1,91 @@ +import assert from "node:assert/strict"; +import dns from "node:dns/promises"; +import { once } from "node:events"; +import http from "node:http"; +import { syncBuiltinESMExports } from "node:module"; +import test from "node:test"; +import { setTimeout as delay } from "node:timers/promises"; +import { WebSocket } from "ws"; +import { installWebSocketBridge, type WsBridgeCodec } from "../src/ws-bridge.ts"; + +const headers = { "x-bili-plugin": "close-fixture", "x-bili-plugin-conversation": "pending-close" }; + +function fixture(proxied: boolean) { + const server = http.createServer((_req, res) => res.writeHead(404).end()); + let created = 0; + const codec: WsBridgeCodec = { + name: "close-fixture", pluginMarker: "close-fixture", + matchUpgrade: () => "https://pending-admission.example/echo", + createSession: () => { + created++; + return { onMessage() {}, onClose() {}, shutdown() {} }; + }, + }; + const handler = installWebSocketBridge(server, async () => {}, () => {}, [codec], () => proxied); + return { server, handler, created: () => created }; +} + +test("server close does not admit a WebSocket whose destination check has not settled", async () => { + const { server, handler, created } = fixture(true); + let closed = Promise.resolve(); + server.on("upgrade", (request, socket, head) => { + assert.equal(handler(request, socket, head), true); + closed = new Promise((resolve) => server.close(() => resolve())); + }); + await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); + const address = server.address(); + assert.ok(address && typeof address !== "string"); + const peer = new WebSocket(`ws://127.0.0.1:${address.port}/bridge`, { headers }); + const peerClosed = new Promise((resolve) => peer.on("close", () => resolve())); + peer.on("error", () => {}); + try { + const outcome = await Promise.race([ + new Promise((resolve) => peer.on("open", () => resolve("opened"))), + peerClosed.then(() => "closed"), + ]); + assert.equal(outcome, "closed", "shutdown must not create a session after its teardown pass"); + await closed; + assert.equal(created(), 0); + } finally { + peer.terminate(); + await peerClosed; + await closed; + } +}); + +test("server close releases an upgrade socket without waiting for DNS admission", async (t) => { + const lookupEntered = Promise.withResolvers(); + const lookupReleased = Promise.withResolvers>(); + const lookup = t.mock.method(dns, "lookup", async () => { + lookupEntered.resolve(); + return lookupReleased.promise; + }); + syncBuiltinESMExports(); + const { server, handler, created } = fixture(false); + server.on("upgrade", (request, socket, head) => { assert.equal(handler(request, socket, head), true); }); + await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); + const address = server.address(); + assert.ok(address && typeof address !== "string"); + const peer = new WebSocket(`ws://127.0.0.1:${address.port}/bridge`, { headers }); + const peerClosed = new Promise((resolve) => peer.on("close", () => resolve())); + peer.on("error", () => {}); + let closed: Promise | undefined; + try { + await lookupEntered.promise; + closed = new Promise((resolve) => server.close(() => resolve())); + const outcome = await Promise.race([closed.then(() => "closed"), delay(500).then(() => "still waiting for DNS")]); + assert.equal(outcome, "closed"); + await peerClosed; + lookupReleased.resolve([{ address: "203.0.113.1", family: 4 }]); + await delay(0); + assert.equal(created(), 0, "a late successful DNS answer cannot revive the upgrade"); + } finally { + lookupReleased.resolve([]); + lookup.mock.restore(); + syncBuiltinESMExports(); + peer.terminate(); + await peerClosed; + if (closed) await closed; + else { server.close(); await once(server, "close"); } + } +}); From 116b42d7f8725620ff93baee1002971da12b67ec Mon Sep 17 00:00:00 2001 From: Afloat Date: Tue, 17 Nov 2026 11:51:19 +0800 Subject: [PATCH 15/24] fix: keep catalog invalidation effective across overlapping requests (cherry picked from commit b22e9c1c4e11143c055f4d1e22641bbc7444540f) --- src/server/codex-catalog-cache.ts | 99 +++++++---- .../codex-catalog-invalidation-races.test.ts | 168 ++++++++++++++++++ 2 files changed, 232 insertions(+), 35 deletions(-) create mode 100644 tests/codex-catalog-invalidation-races.test.ts diff --git a/src/server/codex-catalog-cache.ts b/src/server/codex-catalog-cache.ts index ea15629a73..141b8ac27f 100644 --- a/src/server/codex-catalog-cache.ts +++ b/src/server/codex-catalog-cache.ts @@ -113,6 +113,10 @@ export function createCodexCatalogFetch( ) { const memory = new Map(); const invalidated = new Set(); + // Invalidating a key also invalidates requests already reading its old + // catalog. Bound the generation registry like the existing tombstones. + const generations = new Map(); + const diskUpdates = new Map>(); const offSpec304Noted = new Set(); const clock = options.now ?? Date.now; const maxAge = options.maxAgeMs ?? 6 * 60 * 60 * 1000; @@ -124,6 +128,13 @@ export function createCodexCatalogFetch( clock() >= entry.savedAt && clock() - entry.savedAt <= maxAge && validCatalog(entry.body); + const updateDisk = async (key: string, operation: () => Promise) => { + const previous = diskUpdates.get(key) ?? Promise.resolve(); + const pending = previous.catch(() => {}).then(operation); + diskUpdates.set(key, pending); + try { await pending; } + finally { if (diskUpdates.get(key) === pending) diskUpdates.delete(key); } + }; return async ( operation: (signal?: AbortSignal) => Promise, url: string, @@ -142,6 +153,13 @@ export function createCodexCatalogFetch( ]), ) .digest("hex"); + let generation = generations.get(key); + if (!generation) { + generation = {}; + generations.set(key, generation); + if (generations.size > 256) generations.delete(generations.keys().next().value!); + } + const current = () => generations.get(key) === generation; const directory = options.directory === undefined ? path.join(cacheDir(), "codex-model-catalog-v1") : options.directory; const file = directory ? path.join(directory, `${key}.json`) : null; let entry = invalidated.has(key) ? undefined : memory.get(key); @@ -155,49 +173,60 @@ export function createCodexCatalogFetch( /* Cache absence or corruption is a miss. */ } } - if (!fresh(entry)) entry = undefined; + if (!current() || !fresh(entry)) entry = undefined; const remember = async (value: Entry) => { + if (!current()) return false; entry = value; invalidated.delete(key); memory.delete(key); memory.set(key, value); while (memory.size > MAX_ENTRIES) memory.delete(memory.keys().next().value!); - if (!file || !directory) return; - const temp = `${file}.${randomUUID()}.tmp`; - try { - await fs.mkdir(directory, { recursive: true, mode: 0o700 }); - await fs.writeFile(temp, JSON.stringify(value), { mode: 0o600 }); - await fs.rename(temp, file); - const names = (await fs.readdir(directory)).filter((name) => /^[a-f0-9]{64}\.json$/.test(name)); - if (names.length > MAX_ENTRIES) { - const dated = await Promise.all( - names.map(async (name) => ({ - name, - mtime: (await fs.stat(path.join(directory, name))).mtimeMs, - })), - ); - dated.sort((a, b) => b.mtime - a.mtime); - await Promise.all(dated.slice(MAX_ENTRIES).map((item) => fs.unlink(path.join(directory, item.name)).catch(() => {}))); + if (!file || !directory) return true; + await updateDisk(key, async () => { + if (!current()) return; + const temp = `${file}.${randomUUID()}.tmp`; + try { + await fs.mkdir(directory, { recursive: true, mode: 0o700 }); + await fs.writeFile(temp, JSON.stringify(value), { mode: 0o600 }); + await fs.rename(temp, file); + const names = (await fs.readdir(directory)).filter((name) => /^[a-f0-9]{64}\.json$/.test(name)); + if (names.length > MAX_ENTRIES) { + const dated = await Promise.all( + names.map(async (name) => ({ + name, + mtime: (await fs.stat(path.join(directory, name))).mtimeMs, + })), + ); + dated.sort((a, b) => b.mtime - a.mtime); + await Promise.all(dated.slice(MAX_ENTRIES).map((item) => fs.unlink(path.join(directory, item.name)).catch(() => {}))); + } + } catch { + log("warn", "[model-catalog] private cache write unavailable; using memory cache"); + } finally { + await fs.unlink(temp).catch(() => {}); } - } catch { - log("warn", "[model-catalog] private cache write unavailable; using memory cache"); - } finally { - await fs.unlink(temp).catch(() => {}); - } + }); + return current(); }; const invalidate = async () => { + if (!current()) return; entry = undefined; memory.delete(key); invalidated.add(key); + generations.set(key, {}); if (invalidated.size > 256) invalidated.delete(invalidated.values().next().value!); if (file) { - // A tombstone prevents a failed deletion from reviving old capabilities. - await fs - .writeFile(file, JSON.stringify({ key, invalidated: true }), { - mode: 0o600, - }) - .catch(() => {}); - await fs.unlink(file).catch(() => {}); + // Wait for an already-started rename before deleting: otherwise + // that rename can publish the invalid catalog after deletion. + await updateDisk(key, async () => { + // A tombstone prevents a failed deletion from reviving old capabilities. + await fs + .writeFile(file, JSON.stringify({ key, invalidated: true }), { + mode: 0o600, + }) + .catch(() => {}); + await fs.unlink(file).catch(() => {}); + }); } }; const discard = (result?: FetchResult) => { @@ -234,9 +263,9 @@ export function createCodexCatalogFetch( result = await operation(linked); signal?.throwIfAborted(); const status = result.response.status; - if ([500, 502, 503, 504].includes(status)) return fresh(entry) ? cached(`temporary HTTP ${status}`, result) : result; + if ([500, 502, 503, 504].includes(status)) return current() && fresh(entry) ? cached(`temporary HTTP ${status}`, result) : result; if (status === 304) { - if (!entry) return result; + if (!current() || !entry) return result; // A 304 only means "not modified" when THIS request was // conditional — the wrapper itself never sends a validator, // but the end client may have added if-none-match / @@ -261,7 +290,7 @@ export function createCodexCatalogFetch( await invalidate(); return result; } - await remember({ ...entry, savedAt: clock() }); + if (!await remember({ ...entry, savedAt: clock() })) return result; discard(result); return replay(entry, false); } @@ -293,11 +322,11 @@ export function createCodexCatalogFetch( if (validCatalog(text)) { const etag = result.response.headers.get("etag"); const lastModified = result.response.headers.get("last-modified"); - await remember({ key, savedAt: clock(), body: text, + const saved = await remember({ key, savedAt: clock(), body: text, ...(etag !== null ? { etag } : {}), ...(lastModified !== null ? { lastModified } : {}), }); - log("info", "[model-catalog] complete catalog refreshed; offline protection ready"); + if (saved) log("info", "[model-catalog] complete catalog refreshed; offline protection ready"); } else { await invalidate(); } @@ -307,7 +336,7 @@ export function createCodexCatalogFetch( discard(result); throw error; } - if (fresh(entry) && (deadline.signal.aborted || transient(error))) return cached("network interruption", result); + if (current() && fresh(entry) && (deadline.signal.aborted || transient(error))) return cached("network interruption", result); discard(result); throw error; } finally { diff --git a/tests/codex-catalog-invalidation-races.test.ts b/tests/codex-catalog-invalidation-races.test.ts new file mode 100644 index 0000000000..077d190b7c --- /dev/null +++ b/tests/codex-catalog-invalidation-races.test.ts @@ -0,0 +1,168 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; +import fs from "node:fs/promises"; +import { unlinkSync, writeFileSync } from "node:fs"; +import { syncBuiltinESMExports } from "node:module"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { createCodexCatalogFetch } from "../src/server/codex-catalog-cache.js"; +import type { fetchWithTimeout } from "../src/fetch-util.js"; + +type FetchResult = Awaited>; +const url = "https://chatgpt.com/backend-api/codex/models?client_version=race-fixture"; +const init = { method: "GET", headers: { authorization: "Bearer SYNTHETIC_CATALOG_RACE" } }; +const catalog = (slug = "fixture-model") => JSON.stringify({ models: [{ slug, context_window: 200000, model_messages: {} }] }); +const result = (status = 200, body: string | ReadableStream | null = catalog()): FetchResult => ({ + response: new Response(body, { status }), clearTimer() {}, stopIdleTimer() {}, +}); +const offline = async (): Promise => { throw Object.assign(new Error("fixture offline"), { code: "ENETUNREACH" }); }; +function deferred() { + let resolve!: (value: T) => void; + let reject!: (reason: unknown) => void; + const promise = new Promise((yes, no) => { resolve = yes; reject = no; }); + return { promise, resolve, reject }; +} + +for (const failure of ["http", "network"] as const) { + test(`an overlapping ${failure} failure cannot replay a catalog invalidated by 401`, async () => { + const cache = createCodexCatalogFetch({ directory: null }); + await cache(async () => result(), url, init); + const gate = deferred(); + const entered = deferred(); + const old = cache(() => { entered.resolve(); return gate.promise; }, url, init); + await entered.promise; + assert.equal((await cache(async () => result(401, "denied"), url, init)).response.status, 401); + if (failure === "http") { + gate.resolve(result(503, "temporarily unavailable")); + assert.equal((await old).response.status, 503); + } else { + gate.reject(Object.assign(new Error("fixture offline"), { code: "ENETUNREACH" })); + await assert.rejects(old, /fixture offline/); + } + await assert.rejects(cache(offline, url, init), /fixture offline/); + }); +} + +test("a successful body already being read cannot revive a subsequently invalidated memory or disk catalog", async () => { + const dir = await fs.mkdtemp(join(tmpdir(), "bili-catalog-body-race-")); + let release!: () => void; + try { + const cache = createCodexCatalogFetch({ directory: dir }); + await cache(async () => result(), url, init); + const entered = deferred(); + const body = new ReadableStream({ + start(controller) { + controller.enqueue(new TextEncoder().encode('{"models":')); + release = () => { release = () => {}; controller.enqueue(new TextEncoder().encode(catalog("old-response").slice('{"models":'.length))); controller.close(); }; + }, + }); + const old = cache(() => { entered.resolve(); return Promise.resolve(result(200, body)); }, url, init); + await entered.promise; + assert.equal((await cache(async () => result(403, "denied"), url, init)).response.status, 403); + release(); + assert.equal(await (await old).response.text(), catalog("old-response"), "the original successful response still reaches its caller"); + await assert.rejects(cache(offline, url, init), /fixture offline/); + await assert.rejects(createCodexCatalogFetch({ directory: dir })(offline, url, init), /fixture offline/); + } finally { release?.(); await fs.rm(dir, { recursive: true, force: true }); } +}); + +test("a newer post-invalidation refresh survives an older response finishing last", async () => { + const cache = createCodexCatalogFetch({ directory: null }); + await cache(async () => result(), url, init); + const gate = deferred(); + const entered = deferred(); + const old = cache(() => { entered.resolve(); return gate.promise; }, url, init); + await entered.promise; + await cache(async () => result(401, "denied"), url, init); + await cache(async () => result(200, catalog("new-generation")), url, init); + gate.resolve(result(200, catalog("old-generation"))); + await old; + assert.equal(await (await cache(offline, url, init)).response.text(), catalog("new-generation")); +}); + +test("an old conditional 304 cannot replay a catalog invalidated while it was in flight", async () => { + const cache = createCodexCatalogFetch({ directory: null }); + const seed = result(); + seed.response.headers.set("etag", '"fixture-v1"'); + await cache(async () => seed, url, init); + const gate = deferred(); + const entered = deferred(); + const old = cache(() => { entered.resolve(); return gate.promise; }, url, { + ...init, headers: { ...init.headers, "if-none-match": '"fixture-v1"' }, + }); + await entered.promise; + await cache(async () => result(401, "denied"), url, init); + gate.resolve(result(304, null)); + assert.equal((await old).response.status, 304); + await assert.rejects(cache(offline, url, init), /fixture offline/); +}); + +test("a stale denial cannot erase a refreshed catalog from after the invalidation", async () => { + const cache = createCodexCatalogFetch({ directory: null }); + await cache(async () => result(), url, init); + const gate = deferred(); + const entered = deferred(); + const old = cache(() => { entered.resolve(); return gate.promise; }, url, init); + await entered.promise; + await cache(async () => result(401, "denied"), url, init); + await cache(async () => result(200, catalog("fresh-login")), url, init); + gate.resolve(result(403, "old denial")); + assert.equal((await old).response.status, 403); + assert.equal(await (await cache(offline, url, init)).response.text(), catalog("fresh-login")); +}); + +test("invalidation and queued writes cannot be overtaken by an already pending disk rename", async (t) => { + const dir = await fs.mkdtemp(join(tmpdir(), "bili-catalog-rename-race-")); + const resume = deferred(); + let old: Promise | undefined; + let denial: Promise | undefined; + try { + const cache = createCodexCatalogFetch({ directory: dir }); + await cache(async () => result(), url, init); + const file = join(dir, (await fs.readdir(dir))[0]); + const entered = deferred(); + const originalRename = fs.rename; + const originalWrite = fs.writeFile; + const originalUnlink = fs.unlink; + t.mock.method(fs, "rename", async (...args: Parameters) => { + if (args[1] === file) { entered.resolve(); await resume.promise; } + return originalRename(...args); + }); + // Make the invalidation's fixture-only disk steps synchronous, so one + // event-loop turn determines their ordering without a timing threshold. + t.mock.method(fs, "writeFile", async (...args: Parameters) => { + if (args[0] === file && typeof args[1] === "string" && args[1].includes('"invalidated":true')) { + writeFileSync(file, args[1], { mode: 0o600 }); + return; + } + return originalWrite(...args); + }); + t.mock.method(fs, "unlink", async (...args: Parameters) => { + if (args[0] === file) { unlinkSync(file); return; } + return originalUnlink(...args); + }); + syncBuiltinESMExports(); + old = cache(async () => result(200, catalog("pending-rename")), url, init); + await entered.promise; + denial = cache(async () => result(401, "denied"), url, init); + await new Promise((resolve) => setImmediate(resolve)); + resume.resolve(); + await Promise.all([old, denial]); + await assert.rejects(createCodexCatalogFetch({ directory: dir })(offline, url, init), /fixture offline/); + } finally { + resume.resolve(); + await Promise.allSettled([old, denial]); + t.mock.restoreAll(); + syncBuiltinESMExports(); + await fs.rm(dir, { recursive: true, force: true }); + } +}); + +test("invalidating one credential leaves another credential's offline protection intact", async () => { + const cache = createCodexCatalogFetch({ directory: null }); + const other = { ...init, headers: { authorization: "Bearer SYNTHETIC_OTHER_CATALOG" } }; + await cache(async () => result(), url, init); + await cache(async () => result(200, catalog("other-account")), url, other); + await cache(async () => result(401, "denied"), url, init); + assert.equal(await (await cache(offline, url, other)).response.text(), catalog("other-account")); +}); From 122e6df3c0a18a44dd1f3d1dd00670f7b662b5d2 Mon Sep 17 00:00:00 2001 From: Afloat Date: Tue, 17 Nov 2026 12:31:44 +0800 Subject: [PATCH 16/24] fix: retain prior summaries across Threads reforges (cherry picked from commit 6e107571b4a6d31395ef4643a60cedba20b5e324) --- src/server/threads-lane.ts | 8 ++++- tests/threads-lane.test.ts | 60 +++++++++++++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/src/server/threads-lane.ts b/src/server/threads-lane.ts index 0218e2557e..6f8f017eeb 100644 --- a/src/server/threads-lane.ts +++ b/src/server/threads-lane.ts @@ -338,12 +338,18 @@ function renderTranscript(entries: ThreadsMirrorEntry[]): string { function buildSummaryRequestBody(shape: ThreadsMirrorShape | undefined, model: string | undefined, meta: ThreadsMirrorMeta, entries: ThreadsMirrorEntry[], cut: number, billingBlock: { type: "text"; text: string } | undefined): Record { const system: unknown[] = billingBlock !== undefined ? [billingBlock, { type: "text", text: SUMMARIZER_SYSTEM }] : [{ type: "text", text: SUMMARIZER_SYSTEM }]; + const transcript = renderTranscript(entries.slice(Math.min(meta.foldedBefore, cut), cut)); + // A reforge replaces the entire old thread. The next summary must carry + // forward the previously folded prefix as well as the newly folded slice. + const content = meta.foldedSummary + ? `# Previously folded history\n${meta.foldedSummary}\n\n# New history to fold\n${transcript}` + : transcript; return { model: model ?? shape?.model ?? "claude-sonnet-4-5", max_tokens: SUMMARY_MAX_TOKENS, stream: false, system, - messages: [{ role: "user", content: renderTranscript(entries.slice(Math.min(meta.foldedBefore, cut), cut)) }], + messages: [{ role: "user", content }], }; } diff --git a/tests/threads-lane.test.ts b/tests/threads-lane.test.ts index 83b0aba3aa..eafa9a8039 100644 --- a/tests/threads-lane.test.ts +++ b/tests/threads-lane.test.ts @@ -40,6 +40,7 @@ const sseReply = (res: http.ServerResponse, id: string, text: string): void => { interface HarnessOpts { window?: number; + pluginHeader?: string; /** First non-stream (summary side-call) request fails. */ failFirstSideCall?: boolean; /** Status the failed first side-call answers with (default 500). */ @@ -110,7 +111,7 @@ async function harness(storageMode: "full" | "legacy", opts: HarnessOpts = {}): if (!(proxy as unknown as { listening?: boolean }).listening) await once(proxy, "listening"); const proxyUrl = `http://127.0.0.1:${(proxy.address() as { port: number }).port}`; const post = async (body: Record, sid = "threads-1") => { - const response = await fetch(`${proxyUrl}/v1/messages`, { method: "POST", headers: { "content-type": "application/json", "x-api-key": "test-key", "anthropic-version": "2023-06-01", "x-acp-session": sid }, body: JSON.stringify(body) }); + const response = await fetch(`${proxyUrl}/v1/messages`, { method: "POST", headers: { "content-type": "application/json", "x-api-key": "test-key", "anthropic-version": "2023-06-01", "x-acp-session": sid, ...(opts.pluginHeader ? { "x-bili-plugin": opts.pluginHeader } : {}) }, body: JSON.stringify(body) }); const text = await response.text(); let parsed: unknown = text; try { @@ -278,6 +279,63 @@ test("threads lane: post-reforge continue passes through (budget below the windo } }); +for (const reloadMirror of [false, true]) { + test(`threads lane: repeated reforge carries the previous summary${reloadMirror ? " after reopening its store" : ""}`, async () => { + process.env.BILI_ROLLOUT_ANTHROPIC_THREADS_REFORGE = "1"; + const h = await harness("full", { window: 5000 }); + try { + await h.post(createBody([ + { role: "user", content: turnText(1, 400) }, { role: "assistant", content: "answer 1" }, + { role: "user", content: turnText(2, 400) }, { role: "assistant", content: "answer 2" }, + { role: "user", content: turnText(3, 400) }, { role: "assistant", content: "answer 3" }, + ])); + for (let turn = 4; turn <= 6; turn++) { + if (reloadMirror && turn > 4) __resetThreadsLaneForTests(); + const reply = await h.post(continueBody([{ role: "user", content: turnText(turn, 400) }], `msg_${h.forwarded.length}`)); + assert.equal(reply.status, 200); + const sideCalls = h.forwarded.filter((body) => body.stream === false && body.thread === undefined); + const round = turn - 3; + assert.equal(sideCalls.length, round, "each newly over-budget turn performs one reforge"); + const summaryInput = JSON.stringify(sideCalls.at(-1)?.messages); + if (round > 1) { + assert.ok(summaryInput.includes(`SUMMARY-${round - 1}`), "the existing folded history must reach the next summarizer"); + assert.ok(!summaryInput.includes("turn payload 1 "), "already-folded raw entries are not replayed alongside their summary"); + } + assert.ok(summaryInput.includes(`turn payload ${round} `), "the newly folded slice accompanies the previous summary"); + const rebuilt = h.forwarded.at(-1) as { thread?: { type?: string }; system?: unknown; tools?: unknown; messages?: { content?: unknown }[] }; + assert.equal(rebuilt.thread?.type, "create"); + assert.deepEqual(rebuilt.system, CLIENT_SYSTEM); + assert.deepEqual(rebuilt.tools, CLIENT_TOOLS); + assert.equal(rebuilt.messages?.at(-1)?.content, turnText(turn, 400), "the current delta stays verbatim"); + assert.equal(h.readMirror("threads-1").meta?.foldedSummary, `SUMMARY-${round}`); + } + } finally { + delete process.env.BILI_ROLLOUT_ANTHROPIC_THREADS_REFORGE; + await h.close(); + } + }); +} + +test("threads lane: plugin mode keeps ownership of its compression", async () => { + process.env.BILI_ROLLOUT_ANTHROPIC_THREADS_REFORGE = "1"; + const h = await harness("full", { window: 5000, pluginHeader: "pi" }); + const sid = "threads-plugin-owned"; + try { + await h.post(createBody([ + { role: "user", content: turnText(1, 400) }, { role: "assistant", content: "answer 1" }, + { role: "user", content: turnText(2, 400) }, { role: "assistant", content: "answer 2" }, + { role: "user", content: turnText(3, 400) }, { role: "assistant", content: "answer 3" }, + ]), sid); + const reply = await h.post(continueBody([{ role: "user", content: turnText(4, 400) }], "msg_1"), sid); + assert.equal(reply.status, 200); + assert.equal(h.readMirror(sid).meta, undefined, "plugin mode does not enter the Threads mirror/reforge path"); + assert.ok(!h.forwarded.some((body) => (JSON.stringify(body.system) ?? "").includes("You are compressing an older slice of a coding-agent conversation")), "the Threads summarizer is not called for a plugin-owned turn"); + } finally { + delete process.env.BILI_ROLLOUT_ANTHROPIC_THREADS_REFORGE; + await h.close(); + } +}); + test("threads lane: rollout off keeps over-window continues untouched (no side-call)", async () => { const h = await harness("full", { window: 5000 }); try { From 3868cc0c569c2d7b7aa3562be0fefa6ee0a7bf0f Mon Sep 17 00:00:00 2001 From: Afloat Date: Tue, 17 Nov 2026 12:59:51 +0800 Subject: [PATCH 17/24] fix: relay root JSON arrays without conversation preparation (cherry picked from commit bd03db0b0fb31f68643ef57d7ba4def6e731ea9c) --- src/server/handle.ts | 2 +- tests/nonobject-conversation-body.test.ts | 86 +++++++++++++++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 tests/nonobject-conversation-body.test.ts diff --git a/src/server/handle.ts b/src/server/handle.ts index 11dbc5d5b5..0229a0d658 100644 --- a/src/server/handle.ts +++ b/src/server/handle.ts @@ -1088,7 +1088,7 @@ async function handleRequest( // this request — skip the whole pipeline (prepared stays null) so the // passthrough path below forwards it verbatim. #1421: `chainSkip` is the // content-level twin of the same decision (verifiable checkpoint present). - if (!opts.passthrough && !routePassthrough && !chainSkip && hopMarker === undefined && protocol && parsed && typeof parsed === "object") { + if (!opts.passthrough && !routePassthrough && !chainSkip && hopMarker === undefined && protocol && parsed && typeof parsed === "object" && !Array.isArray(parsed)) { const sessionHeader = headerValue(req, opts.sessionHeader); // Plugin mode (issue #1, "内外呼应"): a cooperative agent-side plugin // announces itself with x-bili-plugin. The proxy then treats the diff --git a/tests/nonobject-conversation-body.test.ts b/tests/nonobject-conversation-body.test.ts new file mode 100644 index 0000000000..b2c2bd85e8 --- /dev/null +++ b/tests/nonobject-conversation-body.test.ts @@ -0,0 +1,86 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { once } from "node:events"; +import http from "node:http"; +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { closeServers } from "./server-close-guard.ts"; + +async function harness(plugin: boolean, passthrough: boolean, exercise: (send: (body: string, identity?: boolean) => Promise<{ status: number; body: string; received: string | undefined }>) => Promise, endpoint: string): Promise { + const received: string[] = []; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (chunk: Buffer) => chunks.push(chunk)); + req.on("end", () => { + received.push(Buffer.concat(chunks).toString("utf8")); + res.writeHead(202, { "content-type": "application/json" }); + res.end('{"accepted":true}'); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const origin = `http://127.0.0.1:${(upstream.address() as import("node:net").AddressInfo).port}`; + let proxy: http.Server | undefined; + try { + proxy = await startServer({ + port: 0, host: "127.0.0.1", upstream: origin, routes: {}, + modelContextLimit: 200_000, kernelConfig: defaultConfig(200_000), + compress: { injectTool: false, injectNudge: false }, + promptCache: { routing: "auto" }, sessionHeader: "x-acp-session", + log: false, debug: false, passthrough, autoUpdate: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + const port = (proxy.address() as import("node:net").AddressInfo).port; + await exercise(async (body, identity = true) => { + const response = await fetch(`http://127.0.0.1:${port}/bili/${origin}${endpoint}`, { + method: "POST", headers: { "content-type": "application/json", + ...(identity ? { "x-acp-session": "root-array-fixture" } : {}), + ...(plugin ? { "x-bili-plugin": "pi" } : {}) }, body, + }); + return { status: response.status, body: await response.text(), received: received.at(-1) }; + }); + } finally { + await closeServers(proxy ? [proxy, upstream] : [upstream], "root-array relay"); + } +} + +for (const endpoint of ["/v1/responses", "/v1beta/models/test:generateContent"]) { + for (const plugin of [false, true]) { + test(`root JSON arrays are relayed intact with or without conversation identity (${endpoint}, ${plugin ? "plugin" : "proxy"})`, { timeout: 20_000 }, async () => { + await harness(plugin, false, async send => { + for (const identity of [true, false]) { + for (const body of [' [1, {"field":"untouched"}]\n', "[]"]) { + const result = await send(body, identity); + assert.equal(result.status, 202, result.body); + assert.equal(result.received, body); + } + } + }, endpoint); + }); + } + test(`explicit passthrough retains the same root-array bytes (${endpoint})`, { timeout: 20_000 }, async () => { + await harness(false, true, async send => { + const body = ' [1, {"field":"untouched"}]\n'; + const result = await send(body, false); + assert.equal(result.status, 202, result.body); + assert.equal(result.received, body); + }, endpoint); + }); + test(`valid object conversations with nested arrays still enter preparation (${endpoint})`, { timeout: 20_000 }, async () => { + await harness(false, false, async send => { + const body = endpoint.includes("responses") + ? { model: "test", input: [{ role: "user", content: "hello" }], instructions: "control system" } + : { contents: [{ role: "user", parts: [{ text: "hello" }] }] }; + const result = await send(JSON.stringify(body)); + assert.equal(result.status, 202, result.body); + const forwarded: unknown = JSON.parse(result.received!); + assert.ok(forwarded && typeof forwarded === "object" && !Array.isArray(forwarded)); + const record = forwarded as Record; + assert.ok(Array.isArray(endpoint.includes("responses") ? record.input : record.contents)); + assert.match(result.received!, /hello/); + assert.match(result.received!, /ACP TAGS/); + }, endpoint); + }); +} From a706d6f34a746ceef30497106e76d4ee8d48f65c Mon Sep 17 00:00:00 2001 From: Afloat Date: Tue, 17 Nov 2026 13:10:28 +0800 Subject: [PATCH 18/24] fix: preserve image exports while session twins survive (cherry picked from commit 2140072091d23034b552cb63f1284ea6e77189b5) --- src/session-gc.ts | 8 +- tests/session-gc-image-twins.test.ts | 125 +++++++++++++++++++++++++++ 2 files changed, 130 insertions(+), 3 deletions(-) create mode 100644 tests/session-gc-image-twins.test.ts diff --git a/src/session-gc.ts b/src/session-gc.ts index 6c0024f500..094a97ef7d 100644 --- a/src/session-gc.ts +++ b/src/session-gc.ts @@ -3,7 +3,7 @@ import type { Stats } from "node:fs"; import path from "node:path"; import { log as loggerLog } from "./logger.js"; import { defaultCountTokens } from "acp-kernel"; -import { getStore, type SessionStore } from "./persist.js"; +import { getStore, relPathFor, type SessionStore } from "./persist.js"; import { sessionsDir } from "./paths.js"; import { dropSessionForGc, peekSession } from "./session.js"; import { stateDir } from "./paths.js"; @@ -312,11 +312,13 @@ export async function gcSessionFiles(opts?: { dir?: string; store?: SessionStore deletedSessions.add(file); // #1995 review ②: the image-restore spill tree // (/retrieve/img//) shares this session's lifecycle — - // refs are per-session, so once the file is gone the spills are + // refs are per-session, so once its last file is gone the spills are // unreachable. Co-delete; anything this deterministic path misses (null // id, a pre-salt dir name from an older build) is collected by the // stale-mtime pass below. - if (view.id !== null) { + const hasSurvivingTwin = view.id !== null && matchingSessions(path.basename(relPathFor(view.id), ".json")) + .some((candidate) => !deletedSessions.has(candidate)); + if (view.id !== null && !hasSurvivingTwin) { try { const imgDir = opts?.imgRoot !== undefined ? path.join(opts.imgRoot, restoreExportDirName(view.id)) diff --git a/tests/session-gc-image-twins.test.ts b/tests/session-gc-image-twins.test.ts new file mode 100644 index 0000000000..9cab4273d0 --- /dev/null +++ b/tests/session-gc-image-twins.test.ts @@ -0,0 +1,125 @@ +import assert from "node:assert/strict"; +import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, utimesSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import { after, test } from "node:test"; +import { SessionStore, relPathFor } from "../src/persist.ts"; +import { gcSessionFiles } from "../src/session-gc.ts"; +import { restoreExportDirName } from "../src/image-restore.ts"; + +const now = Date.now(); +const day = 86_400_000; +const root = mkdtempSync(path.join(tmpdir(), "bili-gc-image-twins-")); +const env = { + BILI_CONFIG_FILE: path.join(root, "no-config.json"), BILI_STORAGE_UNIFIED: "legacy", + BILI_PERSIST_ZSTD: "0", BILI_SESSION_GC: "1", BILI_SESSION_GC_MAX_AGE_DAYS: "7", + BILI_SESSION_GC_MAX_TOKENS: "1000000", +}; +const previous = Object.fromEntries(Object.keys(env).map(key => [key, process.env[key]])); +Object.assign(process.env, env); +after(() => { + for (const [key, value] of Object.entries(previous)) { + if (value === undefined) delete process.env[key]; + else process.env[key] = value; + } + rmSync(root, { recursive: true, force: true }); +}); + +async function fixture(exercise: (f: { + session: (id: string, protocol: string | undefined, old: boolean, corrupt?: boolean) => string; + image: (id: string, old?: boolean) => string; + sweep: () => ReturnType; +}) => Promise): Promise { + const dir = mkdtempSync(path.join(root, "case-")); + const imgRoot = path.join(dir, "spills"); + const sessionDir = path.join(dir, "sessions"); + const store = new SessionStore({ dir: sessionDir }); + try { + await exercise({ + session: (id, protocol, old, corrupt = false) => { + const file = path.join(sessionDir, relPathFor(id, protocol, "https://api.example.test")); + mkdirSync(path.dirname(file), { recursive: true }); + const savedAt = old ? now - 10 * day : now; + writeFileSync(file, corrupt ? "not JSON" : JSON.stringify({ version: 1, id, savedAt, state: { blocks: [] }, contextTokens: 12 })); + utimesSync(file, new Date(savedAt), new Date(savedAt)); + return file; + }, + image: (id, old = false) => { + const folder = path.join(imgRoot, restoreExportDirName(id)); + mkdirSync(folder, { recursive: true }); + const file = path.join(folder, "m00002.png"); + writeFileSync(file, "fresh image bytes"); + const at = new Date(old ? now - 10 * day : now); + utimesSync(folder, at, at); + return file; + }, + sweep: () => gcSessionFiles({ dir: sessionDir, store, now, imgRoot }), + }); + } finally { + store.closeQuietly(); + } +} + +for (const oldProtocol of [undefined, "anthropic"]) { + test(`removing a stale ${oldProtocol ?? "unknown"} twin keeps the surviving session's fresh image`, async () => { + await fixture(async ({ session, image, sweep }) => { + const id = "migrated-image-session"; + const stale = session(id, oldProtocol, true); + const live = session(id, oldProtocol === undefined ? "anthropic" : undefined, false); + const file = image(id); + const result = await sweep(); + assert.equal(result.removed, 1); + assert.equal(existsSync(stale), false); + assert.equal(existsSync(live), true); + assert.equal(readFileSync(file, "utf8"), "fresh image bytes"); + assert.equal(result.imgDirsRemoved, 0); + }); + }); +} + +test("an unreadable surviving twin conservatively keeps fresh image exports", async () => { + await fixture(async ({ session, image, sweep }) => { + const id = "unreadable-twin"; + session(id, undefined, true); + session(id, "openai", false, true); + const file = image(id); + assert.equal((await sweep()).removed, 1); + assert.equal(readFileSync(file, "utf8"), "fresh image bytes"); + }); +}); + +test("the last deleted twin releases the shared export directory once", async () => { + await fixture(async ({ session, image, sweep }) => { + const id = "both-old"; + session(id, undefined, true); + session(id, "anthropic", true); + const file = image(id); + const result = await sweep(); + assert.equal(result.removed, 2); + assert.equal(existsSync(file), false); + assert.equal(result.imgDirsRemoved, 1); + }); +}); + +test("an unrelated surviving session does not retain the deleted session's fresh exports", async () => { + await fixture(async ({ session, image, sweep }) => { + session("old-only", undefined, true); + session("other-live", "anthropic", false); + const file = image("old-only"); + assert.equal((await sweep()).removed, 1); + assert.equal(existsSync(file), false); + }); +}); + +test("a surviving twin does not disable the existing stale-export age policy", async () => { + await fixture(async ({ session, image, sweep }) => { + const id = "stale-export"; + session(id, undefined, true); + session(id, "openai", false); + const file = image(id, true); + const result = await sweep(); + assert.equal(result.removed, 1); + assert.equal(existsSync(file), false); + assert.equal(result.imgDirsRemoved, 1); + }); +}); From 059aaf78cd73f41545ffce775a049a3aef0783fc Mon Sep 17 00:00:00 2001 From: Afloat Date: Tue, 17 Nov 2026 13:18:34 +0800 Subject: [PATCH 19/24] fix: expire writer conflict verdicts with session retention (cherry picked from commit de7616b4e34bc699781261ddda89c3f01f5e1033) --- src/served-markers.ts | 10 ++- tests/served-marker-verdict-retention.test.ts | 83 +++++++++++++++++++ 2 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 tests/served-marker-verdict-retention.test.ts diff --git a/src/served-markers.ts b/src/served-markers.ts index c79e93a6f2..6d06f5a9b5 100644 --- a/src/served-markers.ts +++ b/src/served-markers.ts @@ -138,7 +138,7 @@ export interface ServedMarkerWriter { dispose(): void; } -export function createServedMarkerWriter(identity: WriterIdentity, dir: string, log: (level: string, msg: string) => void, opts?: ServedMarkerOptions): ServedMarkerWriter & { __flushForTest(): void; __servedForTest(): Record } { +export function createServedMarkerWriter(identity: WriterIdentity, dir: string, log: (level: string, msg: string) => void, opts?: ServedMarkerOptions): ServedMarkerWriter & { __flushForTest(): void; __servedForTest(): Record; __verdictCountForTest(): number } { const servedAt = new Map(); const verdicts = new Map(); let flushTimer: ReturnType | undefined; @@ -157,6 +157,11 @@ export function createServedMarkerWriter(identity: WriterIdentity, dir: string, if (oldest === undefined) break; servedAt.delete(oldest); } + // Verdicts only throttle a tracked session inside their TTL. Keeping + // them after either lifetime ends retains every session ever served. + for (const [id, at] of verdicts) { + if (!servedAt.has(id) || now - at >= VERDICT_TTL_MS) verdicts.delete(id); + } }; const writeMarker = (final: boolean): void => { @@ -239,6 +244,9 @@ export function createServedMarkerWriter(identity: WriterIdentity, dir: string, __servedForTest(): Record { return Object.fromEntries(servedAt); }, + __verdictCountForTest(): number { + return verdicts.size; + }, }; } diff --git a/tests/served-marker-verdict-retention.test.ts b/tests/served-marker-verdict-retention.test.ts new file mode 100644 index 0000000000..627c0e2887 --- /dev/null +++ b/tests/served-marker-verdict-retention.test.ts @@ -0,0 +1,83 @@ +import assert from "node:assert/strict"; +import { mkdtempSync, mkdirSync, readFileSync, rmSync, utimesSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import { test } from "node:test"; +import { createServedMarkerWriter } from "../src/served-markers.ts"; + +const identity = { instanceId: "bounded-writer", pid: process.pid, origin: "http://127.0.0.1:18080", version: "test" }; + +function fixture() { + const root = mkdtempSync(path.join(tmpdir(), "bili-verdict-retention-")); + const dir = path.join(root, "writers"); + const logs: string[] = []; + const writer = createServedMarkerWriter(identity, dir, (_level, message) => logs.push(message)); + return { + dir, logs, writer, + peer(id: string) { + mkdirSync(dir, { recursive: true }); + const file = path.join(dir, "peer.json"); + writeFileSync(file, JSON.stringify({ v: 1, instanceId: "peer", pid: 999_999_999, origin: "http://127.0.0.1:18081", version: "test", servedAt: { [id]: Date.now() } })); + utimesSync(file, new Date(), new Date()); + }, + close() { writer.dispose(); rmSync(root, { recursive: true, force: true }); }, + }; +} + +test("verdict memory is bounded alongside the persisted session window", t => { + const f = fixture(); + t.after(() => f.close()); + for (let batch = 0; batch < 4; batch++) { + for (let i = 0; i < 500; i++) f.writer.noteServed(`session-${batch}-${i}`); + f.writer.__flushForTest(); + assert.equal(Object.keys(f.writer.__servedForTest()).length, 128); + assert.equal(f.writer.__verdictCountForTest(), 128); + } + const marker = JSON.parse(readFileSync(path.join(f.dir, identity.instanceId + ".json"), "utf8")); + assert.equal(Object.keys(marker.servedAt).length, 128); + assert.ok(Object.values(marker.servedAt).every(value => typeof value === "number")); +}); + +test("expired verdicts disappear while the longer-lived served marker remains", t => { + t.mock.timers.enable({ apis: ["Date"], now: Date.now() }); + const f = fixture(); + t.after(() => f.close()); + f.writer.noteServed("idle-session"); + f.writer.__flushForTest(); + assert.equal(f.writer.__verdictCountForTest(), 1); + t.mock.timers.tick(60_000); + f.writer.__flushForTest(); + assert.equal(f.writer.__verdictCountForTest(), 0); + assert.equal(Object.keys(f.writer.__servedForTest()).length, 1); +}); + +test("recent active verdicts still suppress repeats across marker flushes", t => { + t.mock.timers.enable({ apis: ["Date"], now: Date.now() }); + const f = fixture(); + t.after(() => f.close()); + f.peer("contested"); + f.writer.noteServed("contested"); + assert.equal(f.logs.length, 1); + t.mock.timers.tick(5_000); + f.writer.__flushForTest(); + f.writer.noteServed("contested"); + assert.equal(f.logs.length, 1); + assert.equal(f.writer.__verdictCountForTest(), 1); + t.mock.timers.tick(55_000); + f.peer("contested"); + f.writer.__flushForTest(); + f.writer.noteServed("contested"); + assert.equal(f.logs.length, 2); +}); + +test("a returning evicted session is checked again against current peer activity", t => { + const f = fixture(); + t.after(() => f.close()); + f.writer.noteServed("returning"); + for (let i = 0; i < 128; i++) f.writer.noteServed(`replacement-${i}`); + f.writer.__flushForTest(); + assert.equal(f.writer.__servedForTest().returning, undefined); + f.peer("returning"); + f.writer.noteServed("returning"); + assert.equal(f.logs.length, 1); +}); From 22cc56d89323604da45c1cc652de19b63beeb237 Mon Sep 17 00:00:00 2001 From: Afloat Date: Tue, 17 Nov 2026 13:30:36 +0800 Subject: [PATCH 20/24] fix: scope restored-image shedding exemptions to their tool result (cherry picked from commit 7b55d5fa20afcc0a771b70a7facf21e1f64dae54) --- src/egress-shed.ts | 2 +- tests/egress-shed-tool-result-http.test.ts | 77 +++++++++++++++++++++ tests/egress-shed-tool-result-scope.test.ts | 58 ++++++++++++++++ 3 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 tests/egress-shed-tool-result-http.test.ts create mode 100644 tests/egress-shed-tool-result-scope.test.ts diff --git a/src/egress-shed.ts b/src/egress-shed.ts index 9221848377..05bf852bec 100644 --- a/src/egress-shed.ts +++ b/src/egress-shed.ts @@ -218,7 +218,6 @@ function collectOccurrences(protocol: WireProtocol, body: Part): Occurrence[] { messages.forEach((m, entryIndex) => { if (!isObj(m)) return; if (protocol === "openai" && m.role === "tool" && isExemptOutput(protocol, m, names)) return; - if (protocol === "anthropic" && Array.isArray(m.content) && m.content.some((b) => isObj(b) && b.type === "tool_result" && isExemptOutput(protocol, b, names))) return; if (!Array.isArray(m.content)) return; const content = m.content as unknown[]; if (protocol === "anthropic") addAnthropicImages(content, entryIndex, false); @@ -242,6 +241,7 @@ function collectOccurrences(protocol: WireProtocol, body: Part): Occurrence[] { applyShrink: (b64) => { p.image_url = typeof iu === "string" ? dataUrl(d.mediaType, b64) : { ...(iu ?? {}), url: dataUrl(d.mediaType, b64) }; }, }); } else if (p.type === "tool_result" && Array.isArray(p.content)) { + if (isExemptOutput(protocol, p, names)) return; addAnthropicImages(p.content, entryIndex, true); } }); diff --git a/tests/egress-shed-tool-result-http.test.ts b/tests/egress-shed-tool-result-http.test.ts new file mode 100644 index 0000000000..deb7f305f2 --- /dev/null +++ b/tests/egress-shed-tool-result-http.test.ts @@ -0,0 +1,77 @@ +import assert from "node:assert/strict"; +import { once } from "node:events"; +import http from "node:http"; +import test from "node:test"; +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { _setStoreForTest, SessionStore } from "../src/persist.ts"; +import { _resetSessionsForTest } from "../src/session.ts"; +import { READ_ORIGINAL_IMAGE_TOOL_NAME } from "../src/compress-tool.ts"; +import { closeServers } from "./server-close-guard.ts"; + +for (const plugin of [false, true]) { + test(`the real ${plugin ? "plugin" : "proxy"} relay sheds ordinary images beside a restored original`, { timeout: 20_000 }, async () => { + const oldBudget = process.env.BILI_HTTP_OUTBOUND_BYTES; + process.env.BILI_HTTP_OUTBOUND_BYTES = "50000"; + _resetSessionsForTest(); + _setStoreForTest(new SessionStore({ enabled: false })); + const received: string[] = []; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (chunk: Buffer) => chunks.push(chunk)); + req.on("end", () => { + received.push(Buffer.concat(chunks).toString("utf8")); + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ id: "msg-ok", type: "message", role: "assistant", model: "test", content: [{ type: "text", text: "ok" }], stop_reason: "end_turn", usage: { input_tokens: 10, output_tokens: 1 } })); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const origin = `http://127.0.0.1:${(upstream.address() as import("node:net").AddressInfo).port}`; + let proxy: http.Server | undefined; + try { + proxy = await startServer({ + port: 0, host: "127.0.0.1", upstream: origin, routes: {}, + modelContextLimit: 200_000, kernelConfig: defaultConfig(200_000), + compress: { injectTool: false, injectNudge: false }, + promptCache: { routing: "auto" }, sessionHeader: "x-acp-session", + log: false, debug: false, passthrough: false, autoUpdate: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + const port = (proxy.address() as import("node:net").AddressInfo).port; + const restored = Buffer.alloc(3000, 3).toString("base64"); + const ordinary = Buffer.alloc(60000, 7).toString("base64"); + const image = (data: string) => ({ type: "image", source: { type: "base64", media_type: "image/png", data } }); + const messages = [ + { role: "user", content: "Inspect both pictures" }, + { role: "assistant", content: [ + { type: "tool_use", id: "restore", name: READ_ORIGINAL_IMAGE_TOOL_NAME, input: { imageRef: "m00123" } }, + { type: "tool_use", id: "screen", name: "screenshot", input: {} }, + ] }, + { role: "user", content: [ + { type: "tool_result", tool_use_id: "restore", content: [image(restored)] }, + { type: "tool_result", tool_use_id: "screen", content: [image(ordinary)] }, + ] }, + ...Array.from({ length: 8 }, (_, i) => ({ role: i % 2 ? "user" : "assistant", content: `tail ${i}` })), + ]; + const response = await fetch(`http://127.0.0.1:${port}/bili/${origin}/v1/messages`, { + method: "POST", headers: { "content-type": "application/json", "x-acp-session": `tool-result-shed-${plugin}`, + ...(plugin ? { "x-bili-plugin": "pi" } : {}) }, + body: JSON.stringify({ model: "test", stream: false, max_tokens: 1024, messages }), + }); + assert.equal(response.status, 200, await response.text()); + assert.equal(received.length, 1); + assert.ok(Buffer.byteLength(received[0]) < 50000, "the actual transmitted request fits the byte budget"); + assert.match(received[0], /omitted for size limit/); + assert.ok(received[0].includes(restored), "the restored original is still intact"); + assert.ok(!received[0].includes(ordinary), "the ordinary sibling is archived"); + assert.match(received[0], /tail 7/); + } finally { + if (oldBudget === undefined) delete process.env.BILI_HTTP_OUTBOUND_BYTES; + else process.env.BILI_HTTP_OUTBOUND_BYTES = oldBudget; + await closeServers(proxy ? [proxy, upstream] : [upstream], "tool-result image relay"); + } + }); +} diff --git a/tests/egress-shed-tool-result-scope.test.ts b/tests/egress-shed-tool-result-scope.test.ts new file mode 100644 index 0000000000..65dacd6c4b --- /dev/null +++ b/tests/egress-shed-tool-result-scope.test.ts @@ -0,0 +1,58 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { READ_ORIGINAL_IMAGE_TOOL_NAME } from "../src/compress-tool.ts"; +import { shedWireImages, shedWireImagesToCount } from "../src/egress-shed.ts"; + +const image = (size: number, fill: number) => ({ type: "image", source: { type: "base64", media_type: "image/png", data: Buffer.alloc(size, fill).toString("base64") } }); +const restored = { type: "tool_result", tool_use_id: "restore", content: [image(3000, 3)] }; +const ordinary = { type: "tool_result", tool_use_id: "screen", content: [image(3000, 7)] }; +const calls = { role: "assistant", content: [ + { type: "tool_use", id: "restore", name: READ_ORIGINAL_IMAGE_TOOL_NAME, input: { imageRef: "m00123" } }, + { type: "tool_use", id: "screen", name: "screenshot", input: {} }, +] }; +const tail = Array.from({ length: 8 }, (_, i) => ({ role: i % 2 ? "assistant" : "user", content: `tail ${i}` })); + +for (const reverse of [false, true]) { + test(`image count exempts only the matching tool result (${reverse ? "last" : "first"} restored output)`, async () => { + const content = reverse ? [ordinary, restored] : [restored, ordinary]; + const body = JSON.stringify({ messages: [calls, { role: "user", content }] }); + const out = await shedWireImagesToCount(body, "anthropic", 0); + const next = JSON.parse(out.body).messages[1].content; + assert.equal(out.stats.archived, 1); + assert.deepEqual(next[reverse ? 1 : 0], restored); + assert.match(next[reverse ? 0 : 1].content[0].text, /omitted for size limit/); + assert.equal(JSON.stringify({ messages: [calls, { role: "user", content }] }), body); + }); +} + +test("byte shedding can archive a large sibling output beside a restored original", async () => { + const body = JSON.stringify({ messages: [calls, { role: "user", content: [restored, ordinary] }, ...tail] }); + const out = await shedWireImages(body, "anthropic", 6000); + const content = JSON.parse(out.body).messages[1].content; + assert.equal(out.stats.archived, 1); + assert.ok(out.stats.finalBytes <= 6000); + assert.deepEqual(content[0], restored); + assert.match(content[1].content[0].text, /omitted for size limit/); +}); + +test("ordinary top-level images stay eligible beside an exempt tool result", async () => { + const body = JSON.stringify({ messages: [calls, { role: "user", content: [restored, image(3000, 9)] }] }); + const out = await shedWireImagesToCount(body, "anthropic", 0); + const content = JSON.parse(out.body).messages[1].content; + assert.equal(out.stats.archived, 1); + assert.deepEqual(content[0], restored); + assert.equal(content[1].type, "text"); +}); + +test("a message containing only a restored output remains byte-for-byte unchanged", async () => { + const body = JSON.stringify({ messages: [calls, { role: "user", content: [restored] }] }, null, 2); + assert.equal((await shedWireImagesToCount(body, "anthropic", 0)).body, body); + assert.equal((await shedWireImages(body, "anthropic", 1)).body, body); +}); + +test("ordinary sibling outputs in the protected tail are not archived for byte pressure", async () => { + const body = JSON.stringify({ messages: [calls, { role: "user", content: [restored, ordinary] }] }); + const out = await shedWireImages(body, "anthropic", 6000); + assert.equal(out.stats.archived, 0); + assert.equal(out.body, body, "the synthetic image cannot downscale, so protected-tail behavior is unchanged"); +}); From 8dcc5cda6d4093c15acb37f6be56e67962a26990 Mon Sep 17 00:00:00 2001 From: Afloat Date: Tue, 17 Nov 2026 13:41:35 +0800 Subject: [PATCH 21/24] fix: verify image spill bytes before advertising restore refs (cherry picked from commit a48e71aa00144057bf2b3ea83ff0d5f0406d6277) --- src/image-rescale.ts | 4 +- tests/image-ref-byte-match.test.ts | 106 +++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 tests/image-ref-byte-match.test.ts diff --git a/src/image-rescale.ts b/src/image-rescale.ts index 35ee4a2995..b8d1c54332 100644 --- a/src/image-rescale.ts +++ b/src/image-rescale.ts @@ -567,7 +567,9 @@ export async function refForItemBytes(session: Session, mediaType: string, b64: } } if (candidates.length === 0) return undefined; - if (candidates.length === 1) return candidates[0].ref; + // A resized outbound image can share its byte count with a different + // original. Size/MIME only narrow the search; even a sole candidate must + // still match its readable spill before the model is sent to that ref. let matched: string | undefined; for (const c of candidates) { try { diff --git a/tests/image-ref-byte-match.test.ts b/tests/image-ref-byte-match.test.ts new file mode 100644 index 0000000000..a5d6f94b8d --- /dev/null +++ b/tests/image-ref-byte-match.test.ts @@ -0,0 +1,106 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { Jimp } from "jimp"; +import { createSession } from "../src/session.js"; +import { applyImageRescaleItems, executeReadOriginalImage, refForItemBytes } from "../src/image-rescale.js"; +import { shedWireImages } from "../src/egress-shed.js"; +import type { IndexedImage } from "../src/image-restore.js"; + +const root = mkdtempSync(join(tmpdir(), "image-ref-byte-match-")); +test.after(() => rmSync(root, { recursive: true, force: true })); +let next = 0; +function fixture() { + const session = createSession(`image-ref-byte-match-${next++}`); + session.incomingImageIndex = new Map(); + const add = (ref: string, bytes: Buffer, present = true): void => { + const path = join(root, `${next++}.png`); + if (present) writeFileSync(path, bytes); + const entries = session.incomingImageIndex!.get(ref) ?? []; + entries.push({ mediaType: "image/png", bytes: bytes.length, path }); + session.incomingImageIndex!.set(ref, entries); + }; + return { session, add }; +} + +test("a sole same-size candidate must match the actual image bytes", async () => { + const { session, add } = fixture(); + add("m12345", Buffer.from("other")); + assert.equal(await refForItemBytes(session, "image/png", Buffer.from("image").toString("base64")), undefined); +}); + +test("a sole evicted spill does not advertise an unreadable original", async () => { + const { session, add } = fixture(); + const bytes = Buffer.from("image"); + add("m12345", bytes, false); + assert.equal(await refForItemBytes(session, "image/png", bytes.toString("base64")), undefined); +}); + +test("a sole exact match still resolves, with MIME and length prefilters", async () => { + const { session, add } = fixture(); + const bytes = Buffer.from("image"); + add("m12345", bytes); + assert.equal(await refForItemBytes(session, "image/png", bytes.toString("base64")), "m12345"); + assert.equal(await refForItemBytes(session, "image/jpeg", bytes.toString("base64")), undefined); + assert.equal(await refForItemBytes(session, "image/png", Buffer.from("longer").toString("base64")), undefined); +}); + +test("distinct same-size candidates select only the matching original", async () => { + const { session, add } = fixture(); + add("m11111", Buffer.from("other")); + add("m22222", Buffer.from("image")); + assert.equal(await refForItemBytes(session, "image/png", Buffer.from("image").toString("base64")), "m22222"); +}); + +test("identical bytes under different refs remain ambiguous", async () => { + const { session, add } = fixture(); + const bytes = Buffer.from("image"); + add("m11111", bytes); + add("m22222", bytes); + assert.equal(await refForItemBytes(session, "image/png", bytes.toString("base64")), undefined); +}); + +test("duplicate entries under one ref are not ambiguous", async () => { + const { session, add } = fixture(); + const bytes = Buffer.from("image"); + add("m11111", bytes); + add("m11111", bytes); + assert.equal(await refForItemBytes(session, "image/png", bytes.toString("base64")), "m11111"); +}); + +test("rescale then byte-shed never labels a red image with a same-size blue original", async () => { + const oldEnabled = process.env.BILI_IMAGE_RESCALE; + process.env.BILI_IMAGE_RESCALE = "1"; + try { + const red = await new Jimp({ width: 2400, height: 100, color: 0xff0000ff }).getBuffer("image/png"); + const blue = await new Jimp({ width: 1600, height: 67, color: 0x0000ffff }).getBuffer("image/png"); + const { session, add } = fixture(); + add("m11111", red); + add("m22222", blue); + const image = (bytes: Buffer) => ({ type: "message", role: "user", content: [{ type: "input_image", image_url: `data:image/png;base64,${bytes.toString("base64")}` }] }); + const input = [image(red), image(blue), ...Array.from({ length: 8 }, (_, i) => ({ type: "message", role: "user", content: [{ type: "input_text", text: `tail ${i}` }] }))]; + const original = JSON.stringify(input); + const scaled = await applyImageRescaleItems(input, { session, sessionId: session.id, log: () => {}, maxDim: 1600, toolArmed: true }) as { content: { type: string; image_url?: string; text?: string }[] }[]; + const scaledRed = Buffer.from(scaled[0].content[0].image_url!.split(",")[1], "base64"); + assert.equal(scaledRed.length, blue.length, "real red/blue PNGs have identical encoded lengths"); + assert.notDeepEqual(scaledRed, blue, "the equal-size PNGs have different pixels"); + assert.match(scaled[0].content[1].text!, /m11111/); + const body = JSON.stringify({ model: "fixture", input: scaled }); + const shed = await shedWireImages(body, "responses", Buffer.byteLength(body) - 500, { + resolveRef: (mime, b64) => refForItemBytes(session, mime, b64), + }); + assert.equal(shed.stats.archived, 1); + assert.ok(shed.stats.finalBytes <= Buffer.byteLength(body) - 500); + const note = JSON.parse(shed.body).input[0].content[0].text as string; + assert.match(note, /omitted for size limit/); + assert.doesNotMatch(note, /m22222/, "the archive note must not send the model to the unrelated blue image"); + const restored = await executeReadOriginalImage({ ref: "m11111" }, session, () => {}); + assert.equal(restored.image?.base64, red.toString("base64")); + assert.equal(JSON.stringify(input), original, "inbound history is unchanged"); + } finally { + if (oldEnabled === undefined) delete process.env.BILI_IMAGE_RESCALE; + else process.env.BILI_IMAGE_RESCALE = oldEnabled; + } +}); From c32deee0e209d9ba4c933812e1e1088b0591fc8e Mon Sep 17 00:00:00 2001 From: Afloat Date: Tue, 17 Nov 2026 14:10:59 +0800 Subject: [PATCH 22/24] fix: release completed reasoning-round response bodies (cherry picked from commit 9f33b3adc8fa6ce4a94f989ae7334e055f187a94) --- src/reasoning-guard.ts | 7 +- tests/reasoning-guard-body-release.test.ts | 97 ++++++++++++++++++++++ 2 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 tests/reasoning-guard-body-release.test.ts diff --git a/src/reasoning-guard.ts b/src/reasoning-guard.ts index 3cfe160655..a2e8a42c29 100644 --- a/src/reasoning-guard.ts +++ b/src/reasoning-guard.ts @@ -346,6 +346,7 @@ async function consumeRound( const reader = stream.getReader(); const decoder = new TextDecoder(); let buf = ""; + let completed = false; const drainOnce = (): { terminal: Record } | null => { let idx: number; while ((idx = buf.indexOf("\n\n")) !== -1) { @@ -361,7 +362,7 @@ async function consumeRound( try { for (;;) { const { done, value } = await reader.read(); - if (done) break; + if (done) { completed = true; break; } buf += decoder.decode(value, { stream: true }); buf = normalizeSseLineEndings(buf); const term = drainOnce(); @@ -374,6 +375,10 @@ async function consumeRound( } catch (e) { return { error: e instanceof Error ? e.message : String(e) }; } finally { + // A terminal event ends the round before HTTP EOF; release its body + // before a continuation needs that upstream connection. A tee's + // cancellation may wait for its other consumer, so never await it here. + if (!completed) void reader.cancel().catch(() => {}); reader.releaseLock(); } } diff --git a/tests/reasoning-guard-body-release.test.ts b/tests/reasoning-guard-body-release.test.ts new file mode 100644 index 0000000000..0c91d7e654 --- /dev/null +++ b/tests/reasoning-guard-body-release.test.ts @@ -0,0 +1,97 @@ +import assert from "node:assert/strict"; +import { EventEmitter, once } from "node:events"; +import http from "node:http"; +import test from "node:test"; +import { Pool } from "undici"; +import { fetchWithTimeout } from "../src/fetch-util.js"; +import { runReasoningGuard, type SseSink } from "../src/reasoning-guard.js"; + +class Sink extends EventEmitter implements SseSink { + headersSent = false; + writableEnded = false; + frames: string[] = []; + writeHead() { this.headersSent = true; } + write(chunk: string | Uint8Array) { this.frames.push(Buffer.from(chunk).toString()); return true; } + end() { this.writableEnded = true; } +} + +const frame = (value: unknown) => `data: ${JSON.stringify(value)}\n\n`; +const encoder = new TextEncoder(); +const originalBody = JSON.stringify({ model: "fixture-model", input: [{ role: "user", content: "retain this input" }], stream: true }); +const terminal = (type = "response.completed", tokens = 0) => ({ type, response: { id: "fixture-response", status: type.slice(9), output: [], usage: { input_tokens: 10, output_tokens: tokens, output_tokens_details: { reasoning_tokens: tokens } } } }); +const firstRound = frame({ type: "response.output_item.added", output_index: 0, item: { type: "reasoning", id: "fixture-reasoning" } }) + + frame({ type: "response.output_item.done", output_index: 0, item: { type: "reasoning", id: "fixture-reasoning", encrypted_content: "fixture-encrypted" } }) + + frame(terminal("response.completed", 516)); + +async function run(body: ReadableStream, res = new Sink()) { + await runReasoningGuard({ firstResponse: new Response(body), clearFirstTimer() {}, upstreamUrl: "http://127.0.0.1:1", reqHeaders: {}, originalBody, + signal: new AbortController().signal, res, config: { enabled: true, maxContinue: 0 }, log() {} }); + return res; +} + +for (const type of ["response.completed", "response.failed", "response.incomplete"]) { + test(`releases an open round body after ${type} without changing the terminal event`, async () => { + let cancelled = 0; + const stream = new ReadableStream({ start(c) { c.enqueue(encoder.encode(frame(terminal(type)))); }, cancel() { cancelled++; } }); + const sink = await run(stream); + assert.equal(cancelled, 1); + assert.equal(stream.locked, false); + assert.equal(JSON.parse(sink.frames.at(-1)!.slice(6)).type, type); + }); +} + +test("body cancellation cannot delay a completed response or reject its fold", { timeout: 2000 }, async () => { + for (const cancel of [() => new Promise(() => {}), () => Promise.reject(new Error("fixture cancellation rejected"))]) { + const stream = new ReadableStream({ start(c) { c.enqueue(encoder.encode(frame(terminal()))); }, cancel }); + const sink = await run(stream); + assert.equal(sink.writableEnded, true); + assert.equal(stream.locked, false); + } +}); + +test("ordinary upstream EOF still produces the existing incomplete terminal", async () => { + let cancelled = 0; + const stream = new ReadableStream({ start(c) { c.close(); }, cancel() { cancelled++; } }); + const sink = await run(stream); + assert.equal(cancelled, 0); + assert.equal(JSON.parse(sink.frames.at(-1)!.slice(6)).response.incomplete_details.reason, "upstream_eof"); +}); + +test("a continuation can reuse a one-connection HTTP pool after the first terminal keeps its body open", async () => { + const requests: string[] = []; + const server = http.createServer((req, res) => { + let body = ""; + req.on("data", (chunk) => { body += chunk; }); + req.on("end", () => { + requests.push(body); + res.writeHead(200, { "content-type": "text/event-stream" }); + if (requests.length === 1) res.write(firstRound); + else res.end(frame(terminal("response.completed", 10))); + }); + }); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + const origin = `http://127.0.0.1:${(server.address() as { port: number }).port}`; + const pool = new Pool(origin, { connections: 1, pipelining: 1 }); + const abort = new AbortController(); + const fuse = setTimeout(() => abort.abort(new Error("fixture deadline")), 5000); + let clearFirst = () => {}; + try { + const first = await fetchWithTimeout(origin, { method: "POST", body: originalBody, dispatcher: pool }, 3000, abort.signal); + clearFirst = first.clearTimer; + const sink = new Sink(); + await runReasoningGuard({ firstResponse: first.response, clearFirstTimer: first.clearTimer, upstreamUrl: origin, reqHeaders: {}, dispatcher: pool, + originalBody, signal: abort.signal, res: sink, config: { enabled: true }, log() {} }); + assert.equal(requests.length, 2, "the completed first body must release its pool slot before the next round"); + assert.match(requests[1], /retain this input/); + assert.match(requests[1], /fixture-encrypted/); + assert.equal(JSON.parse(sink.frames.at(-1)!.slice(6)).type, "response.completed"); + } finally { + clearTimeout(fuse); + abort.abort(); + clearFirst(); + await pool.destroy(); + server.closeAllConnections(); + await new Promise((resolve) => server.close(() => resolve())); + } +}); From 7fd2974bbd80773ac32d506f7c2b5c6d40b2c72d Mon Sep 17 00:00:00 2001 From: Afloat Date: Tue, 17 Nov 2026 14:16:24 +0800 Subject: [PATCH 23/24] fix: isolate egress module cache generations across reloads (cherry picked from commit ea20b38b4c27061b7c0adfa77dc9eb61aab3ebb2) --- src/egress-transforms.ts | 5 +- tests/egress-module-reload-race.test.ts | 105 ++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 tests/egress-module-reload-race.test.ts diff --git a/src/egress-transforms.ts b/src/egress-transforms.ts index a4bcb8f0b3..bb0b04a3e4 100644 --- a/src/egress-transforms.ts +++ b/src/egress-transforms.ts @@ -138,12 +138,14 @@ type LoadedModule = | { ok: false; err: string }; const moduleCache = new Map(); +let moduleGeneration = 0; /** Clear the loaded-module cache. Called on `POST /__bili/config/reload` so a * plugin's code (and any newly-added/removed entry) takes effect on the next * request without a restart. Modules stay stable WITHIN a generation so a * running session never swaps transform behavior mid-flight. */ export function resetEgressModuleCache(): void { + moduleGeneration++; moduleCache.clear(); } @@ -160,6 +162,7 @@ function coerceModule(def: unknown): EgressTransformModule | null { async function loadModule(absPath: string): Promise { const cached = moduleCache.get(absPath); if (cached) return cached; + const generation = moduleGeneration; let result: LoadedModule; try { if (!existsSync(absPath)) throw new Error(`module not found: ${absPath}`); @@ -175,7 +178,7 @@ async function loadModule(absPath: string): Promise { } catch (e) { result = { ok: false, err: String(e instanceof Error ? e.message : e) }; } - moduleCache.set(absPath, result); + if (generation === moduleGeneration) moduleCache.set(absPath, result); return result; } diff --git a/tests/egress-module-reload-race.test.ts b/tests/egress-module-reload-race.test.ts new file mode 100644 index 0000000000..b8445c89f9 --- /dev/null +++ b/tests/egress-module-reload-race.test.ts @@ -0,0 +1,105 @@ +import assert from "node:assert/strict"; +import { once } from "node:events"; +import { mkdtempSync, rmSync, utimesSync, writeFileSync } from "node:fs"; +import http from "node:http"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import test from "node:test"; +import { defaultConfig } from "acp-kernel"; +import { applyEgressTransforms, parseEgressTransformEntries, resetEgressModuleCache, type EgressTransformBase } from "../src/egress-transforms.js"; +import { startServer } from "../src/server.js"; +import { SessionStore, _setStoreForTest } from "../src/persist.js"; +import { _setForTest as setRegistryForTest } from "../src/registry.js"; +import type { ProxyOptions } from "../src/config.js"; + +function moduleFixture(rejectOld = false) { + resetEgressModuleCache(); + const dir = mkdtempSync(join(tmpdir(), "bili-egress-reload-")); + const path = join(dir, "transform.mjs"); + const key = `egress-reload:${dir}`; + let entered!: () => void, release!: () => void; + const started = new Promise((resolve) => { entered = resolve; }); + const wait = new Promise((resolve) => { release = resolve; }); + Reflect.set(globalThis, Symbol.for(key), { entered, wait }); + const old = `const gate = globalThis[Symbol.for(${JSON.stringify(key)})]; gate.entered(); await gate.wait;\n` + + (rejectOld ? 'throw new Error("old generation failed");\n' : '') + + 'export default { transform() { return { headers: { "x-plugin-generation": "old" } }; } };'; + writeFileSync(path, old); + const entries = parseEgressTransformEntries([{ module: path }]); + const run = async () => { + const base: EgressTransformBase = { sessionId: undefined, upstreamUrl: "https://upstream.fixture.invalid", method: "POST", url: "/test", headers: {}, protocol: "openai", model: "fixture", stage: "initial" }; + await applyEgressTransforms("body", base, entries, () => {}); + return base.headers["x-plugin-generation"]; + }; + return { dir, path, started, release, run, entries, + edit: () => { + writeFileSync(path, 'export default { transform() { return { headers: { "x-plugin-generation": "new" } }; } };'); + const next = new Date(Date.now() + 1000); + utimesSync(path, next, next); + }, + close: () => { release(); Reflect.deleteProperty(globalThis, Symbol.for(key)); resetEgressModuleCache(); rmSync(dir, { recursive: true, force: true }); }, + }; +} + +for (const rejectOld of [false, true]) { + test(`a pre-reload module ${rejectOld ? "failure" : "success"} cannot replace the new generation's cache`, async () => { + const f = moduleFixture(rejectOld); + try { + const pending = f.run(); + await f.started; + resetEgressModuleCache(); + f.edit(); + assert.equal(await f.run(), "new"); + f.release(); + assert.equal(await pending, rejectOld ? undefined : "old"); + assert.equal(await f.run(), "new", "later requests must retain the reloaded transform"); + } finally { f.close(); } + }); +} + +test("real config reload keeps the new plugin after an older HTTP request finishes importing", async () => { + const f = moduleFixture(); + const previous = process.env.BILI_CONFIG_FILE; + process.env.BILI_CONFIG_FILE = join(f.dir, "config.json"); + writeFileSync(process.env.BILI_CONFIG_FILE, JSON.stringify({ egressTransforms: [{ module: f.path }], compress: { injectTool: false, injectNudge: false } })); + const upstream = http.createServer((req, res) => { + req.resume(); + req.on("end", () => { res.writeHead(200, { "content-type": "application/json" }); res.end(JSON.stringify({ generation: req.headers["x-plugin-generation"] })); }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const proxy = await startServer({ port: 0, host: "127.0.0.1", upstream: "http://127.0.0.1", routes: {}, modelContextLimit: 10_000, kernelConfig: defaultConfig(10_000), + compress: { injectTool: false, injectNudge: false }, sessionHeader: "x-acp-session", log: false, debug: false, passthrough: true, + autoUpdate: false, mitm: { enabled: false, domains: [] }, egressTransforms: f.entries } as unknown as ProxyOptions); + await once(proxy, "listening"); + const origin = `http://127.0.0.1:${(proxy.address() as { port: number }).port}`; + const request = async () => { + const response = await fetch(`${origin}/bili/http://127.0.0.1:${upstreamPort}/test`, { method: "POST", body: "body", signal: AbortSignal.timeout(10_000) }); + assert.equal(response.status, 200); + return (await response.json() as { generation: string }).generation; + }; + let pending: Promise | undefined; + try { + pending = request(); + await f.started; + f.edit(); + const reload = await fetch(origin + "/__bili/config/reload", { method: "POST" }); + assert.equal(reload.status, 200, await reload.text()); + assert.equal(await request(), "new"); + f.release(); + assert.equal(await pending, "old"); + assert.equal(await request(), "new"); + } finally { + f.release(); + await pending?.catch(() => {}); + proxy.closeAllConnections(); + upstream.closeAllConnections(); + await Promise.all([new Promise((resolve) => proxy.close(() => resolve())), new Promise((resolve) => upstream.close(() => resolve()))]); + if (previous === undefined) delete process.env.BILI_CONFIG_FILE; + else process.env.BILI_CONFIG_FILE = previous; + f.close(); + } +}); From 23a9108a3651e6855914b8975b9262983d6e3213 Mon Sep 17 00:00:00 2001 From: Afloat Date: Tue, 17 Nov 2026 14:19:48 +0800 Subject: [PATCH 24/24] fix: isolate binary body drafts from failed egress plugins (cherry picked from commit 8e6a19275ba8b3a51fc45fc396e240b21e6c1feb) --- src/egress-transforms.ts | 5 +- tests/egress-body-isolation.test.ts | 152 ++++++++++++++++++++++++++++ 2 files changed, 155 insertions(+), 2 deletions(-) create mode 100644 tests/egress-body-isolation.test.ts diff --git a/src/egress-transforms.ts b/src/egress-transforms.ts index bb0b04a3e4..a4da30a823 100644 --- a/src/egress-transforms.ts +++ b/src/egress-transforms.ts @@ -45,7 +45,8 @@ export type EgressTransformEntry = { /** Context handed to each plugin's `transform`. `headers` is a writable draft: * successful direct mutations commit to the outgoing map before returned * headers merge; failed or timed-out drafts are discarded. `body` is the - * current stage's outbound copy (string JSON text or Buffer). */ + * current stage's string or a private Buffer draft. Buffer edits take effect + * only when the plugin successfully returns that draft as `body`. */ export type EgressTransformCtx = { sessionId: string | undefined; upstreamUrl: string; @@ -264,7 +265,7 @@ export async function applyEgressTransforms( for (const t of resolved) { if (!matchUpstream(t.match, base.upstreamUrl)) continue; const headers = { ...base.headers }; - const ctx: EgressTransformCtx = { ...base, headers, body: current }; + const ctx: EgressTransformCtx = { ...base, headers, body: Buffer.isBuffer(current) ? Buffer.from(current) : current }; const inBytes = byteLen(current); let result: EgressTransformResult; try { diff --git a/tests/egress-body-isolation.test.ts b/tests/egress-body-isolation.test.ts new file mode 100644 index 0000000000..f3b64a1b05 --- /dev/null +++ b/tests/egress-body-isolation.test.ts @@ -0,0 +1,152 @@ +import assert from "node:assert/strict"; +import { once } from "node:events"; +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import http from "node:http"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { pathToFileURL } from "node:url"; +import { after, test } from "node:test"; +import { defaultConfig } from "acp-kernel"; +import { applyEgressTransforms, parseEgressTransformEntries, resetEgressModuleCache, type EgressTransformBase } from "../src/egress-transforms.js"; +import type { ProxyOptions } from "../src/config.js"; +import { SessionStore, _setStoreForTest } from "../src/persist.js"; +import { _setForTest as setRegistryForTest } from "../src/registry.js"; +import { startServer } from "../src/server.js"; + +const dir = mkdtempSync(join(tmpdir(), "bili-egress-body-")); +after(() => { resetEgressModuleCache(); rmSync(dir, { recursive: true, force: true }); }); +let nextPlugin = 0; +function plugin(code: string): string { + const path = join(dir, `plugin-${nextPlugin++}.mjs`); + writeFileSync(path, code); + return path; +} +function base(): EgressTransformBase { + return { sessionId: "body-isolation", upstreamUrl: "https://example.com/upload", method: "POST", + url: "/upload", headers: {}, protocol: "openai", model: "test", stage: "initial" }; +} +const original = () => Buffer.from([0x00, 0xff, 0x20, 0x7f, 0x80, 0x42]); + +for (const [name, outcome] of [ + ["throw", 'throw new Error("plugin failed");'], + ["timeout", "await new Promise(() => {});"], + ["invalid result", "return { body: 123 };"], +]) { + test(`a plugin's Buffer mutations are discarded on ${name} before the next stage`, async () => { + const input = original(); + const ctx = base(); + const entries = parseEgressTransformEntries([ + { module: plugin(`export default { timeoutMs: 20, async transform(c) { c.body.fill(0); ${outcome} } };`) }, + { module: plugin('export default { transform(c) { return { headers: { "x-observed": c.body.toString("hex") } }; } };') }, + ]); + const result = await applyEgressTransforms(input, ctx, entries, () => {}); + assert.equal(result, input, "fail-open keeps the accepted body reference"); + assert.deepEqual(input, original(), "the caller's input is not corrupted"); + assert.equal(ctx.headers["x-observed"], original().toString("hex")); + }); +} + +for (const outcome of ["undefined", "null"]) { + test(`returning ${outcome} leaves Buffer bytes unchanged even after direct edits`, async () => { + const input = original(); + const ctx = base(); + const entries = parseEgressTransformEntries([{ module: plugin(`export default { transform(c) { + c.body.fill(0); c.headers["x-accepted"] = "yes"; return ${outcome}; + } };`) }]); + assert.equal(await applyEgressTransforms(input, ctx, entries, () => {}), input); + assert.deepEqual(input, original()); + assert.equal(ctx.headers["x-accepted"], "yes", "successful header-only edits still commit"); + }); +} + +test("returning a modified Buffer draft commits its bytes without modifying the caller's input", async () => { + const input = original(); + const expected = original(); + expected[0] = 0x99; + const entries = parseEgressTransformEntries([{ module: plugin('export default { transform(c) { c.body[0] = 0x99; return { body: c.body }; } };') }]); + const result = await applyEgressTransforms(input, base(), entries, () => {}); + assert.deepEqual(result, expected); + assert.deepEqual(input, original()); +}); + +test("a later failed plugin cannot damage an earlier plugin's accepted Buffer", async () => { + const entries = parseEgressTransformEntries([ + { module: plugin('export default { transform() { return { body: Buffer.from("accepted") }; } };') }, + { module: plugin('export default { transform(c) { c.body.fill(0); throw new Error("failed"); } };') }, + ]); + const result = await applyEgressTransforms("initial string", base(), entries, () => {}); + assert.deepEqual(result, Buffer.from("accepted")); +}); + +test("a timed-out plugin cannot corrupt the returned body when it later resumes", async () => { + const coordinator = plugin(` + let release; let complete; + export const gate = new Promise(resolve => { release = resolve; }); + export const done = new Promise(resolve => { complete = resolve; }); + export { release, complete }; + `); + const control = await import(pathToFileURL(coordinator).href) as { release: () => void; done: Promise }; + const late = plugin(` + import { gate, complete } from ${JSON.stringify(pathToFileURL(coordinator).href)}; + export default { timeoutMs: 20, async transform(c) { + await gate; c.body.fill(0); complete(); return { body: c.body }; + } }; + `); + const input = original(); + let result: string | Buffer; + try { + result = await applyEgressTransforms(input, base(), parseEgressTransformEntries([{ module: late }]), () => {}); + assert.deepEqual(result, original()); + } finally { + control.release(); + await control.done; + } + assert.deepEqual(result, original()); + assert.deepEqual(input, original()); +}); + +test("an empty or unmatched pipeline preserves Buffer identity and bytes", async () => { + const input = original(); + assert.equal(await applyEgressTransforms(input, base(), [], () => {}), input); + const entries = parseEgressTransformEntries([{ match: { urlPrefix: "https://unmatched.invalid/" }, + module: plugin('export default { transform(c) { c.body.fill(0); return { body: c.body }; } };') }]); + assert.equal(await applyEgressTransforms(input, base(), entries, () => {}), input); + assert.deepEqual(input, original()); +}); + +test("real proxy delivers the original binary payload after an egress plugin corrupts its draft and throws", async () => { + const seen: Buffer[] = []; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (chunk: Buffer) => chunks.push(chunk)); + req.on("end", () => { + const received = Buffer.concat(chunks); + seen.push(received); + res.writeHead(received.equals(original()) ? 200 : 400, { "content-type": "application/json" }); + res.end(JSON.stringify({ received: received.toString("hex") })); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const proxy = await startServer({ port: 0, host: "127.0.0.1", upstream: "http://127.0.0.1", routes: {}, + modelContextLimit: 10_000, kernelConfig: defaultConfig(10_000), compress: { injectTool: false, injectNudge: false }, + sessionHeader: "x-acp-session", log: false, debug: false, passthrough: true, autoUpdate: false, + mitm: { enabled: false, domains: [] }, egressTransforms: parseEgressTransformEntries([{ module: plugin( + 'export default { transform(c) { if (!Buffer.isBuffer(c.body)) throw new Error("expected binary Buffer"); c.body.fill(0); throw new Error("plugin failed"); } };', + ) }]), + } as unknown as ProxyOptions); + await once(proxy, "listening"); + try { + const response = await fetch(`http://127.0.0.1:${(proxy.address() as { port: number }).port}/bili/http://127.0.0.1:${upstreamPort}/upload`, { + method: "POST", headers: { "content-type": "application/octet-stream" }, body: original(), signal: AbortSignal.timeout(5_000), + }); + await response.text(); + assert.deepEqual({ status: response.status, seen }, { status: 200, seen: [original()] }); + } finally { + proxy.closeAllConnections(); upstream.closeAllConnections(); + await Promise.all([new Promise((resolve) => proxy.close(() => resolve())), new Promise((resolve) => upstream.close(() => resolve()))]); + } +});