diff --git a/docs/technical-notes.md b/docs/technical-notes.md index 0d5fbebcb6..0e5142f790 100644 --- a/docs/technical-notes.md +++ b/docs/technical-notes.md @@ -563,14 +563,14 @@ A long-running standalone proxy (v0.1.208 field report: dsh-harness lane, last l Design: -- **Watchdog** (`src/event-loop-watchdog.ts`): a separate worker thread OWNS the clock — the worker's own event loop keeps ticking even when the main thread is permanently blocked, which is the whole point. Main posts a heartbeat every `WATCHDOG_HEARTBEAT_MS` (5 s); the worker checks every 5 s: gap ≥ `WATCHDOG_SOFT_WARN_MS` (30 s) → ONE forensic warn line (uptime/RSS); gap ≥ `WATCHDOG_HARD_TIMEOUT_MS` (300 s) → FATAL line to bili.log + stderr, then `process.exit(1)` (exit mode). Thresholds are hard-coded by design (the config surface is owner-gated): legitimate big compressions finish in seconds-to-minutes and yield the loop between chunks, so a CONTINUOUS five-minute silence is only ever a deadlock. Armed at the ENTRY of `startServer` (before the twin probe, engine/storage init and hydrate all run under it — a wedge can arrive during startup, and #4762's own repro class is sync-I/O hotspots) — standalone processes ONLY; plugin-injection mode never arms (it would kill the HOST process). Forensic lines route to bili.log whenever a file leg is configured; the worker's STDERR leg ships only when stderr is a TTY or there is no file leg at all — a blocking write into a full, undrained pipe never fires an error event (#5012 class), so a worker writing its FATAL line into exactly that dead pipe would wedge ITSELF instead of tripping the exit; the gate is structural, not defensive. The worker is unref'd so the safety net can never hold a clean natural process exit hostage. The worker runs as inline `eval` code because `dist/index.js` is a single bundle with no sibling files to reference; its only import is a guarded `require("worker_threads")` — the `parentPort` global does NOT exist in eval workers (ReferenceError, verified on Node 22). Worker-spawn failure degrades to unwatched with one warn and never breaks startup (availability over guardrail). +- **Watchdog** (`src/event-loop-watchdog.ts`): a separate worker thread OWNS the clock — the worker's own event loop keeps ticking even when the main thread is permanently blocked, which is the whole point. Main posts a heartbeat every `WATCHDOG_HEARTBEAT_MS` (5 s); the worker checks every 5 s: gap ≥ `WATCHDOG_SOFT_WARN_MS` (30 s) → ONE forensic warn line (uptime/RSS); gap ≥ `WATCHDOG_HARD_TIMEOUT_MS` (300 s) → FATAL line to bili.log + stderr, then a whole-process kill — `process.kill(process.pid, "SIGKILL")` issued FROM THE WORKER (exit mode). Not `process.exit()`: inside a worker thread that ends only the worker, which is exactly how the wedged proxy used to survive its own watchdog and run on unwatched (#6157); SIGKILL needs nothing from the blocked main thread (a JS SIGTERM listener would never run), and on Windows `process.kill` maps to TerminateProcess. Thresholds are hard-coded by design (the config surface is owner-gated): legitimate big compressions finish in seconds-to-minutes and yield the loop between chunks, so a CONTINUOUS five-minute silence is only ever a deadlock. Armed at the ENTRY of `startServer` (before the twin probe, engine/storage init and hydrate all run under it — a wedge can arrive during startup, and #4762's own repro class is sync-I/O hotspots) — standalone processes ONLY; plugin-injection mode never arms (it would kill the HOST process). Forensic lines route to bili.log whenever a file leg is configured; the worker's STDERR leg ships only when stderr is a TTY or there is no file leg at all — a blocking write into a full, undrained pipe never fires an error event (#5012 class), so a worker writing its FATAL line into exactly that dead pipe would wedge ITSELF instead of tripping the exit; the gate is structural, not defensive. The worker is unref'd so the safety net can never hold a clean natural process exit hostage. The worker runs as inline `eval` code because `dist/index.js` is a single bundle with no sibling files to reference; its only import is a guarded `require("worker_threads")` — the `parentPort` global does NOT exist in eval workers (ReferenceError, verified on Node 22). Worker-spawn failure degrades to unwatched with one warn and never breaks startup (availability over guardrail). - **Holder diagnostics** (`src/port-owner.ts` + both EADDRINUSE death paths in `src/server.ts`): at the moment of death, RE-probe the existing listener's `/__bili/health` once (2 s budget; wildcard binds probe through a loopback origin — `holderProbeOrigin`). Answered → the hint states the peer IS serving and the spawner should attach to it; no answer → the hint states the holder appears wedged or dead and names the concrete release: `taskkill /PID N` (Windows) / `kill N` (POSIX) when a pid is known (responder pid beats the stale registry entry; falls back to the same-lane registry pid), else `netstat/lsof`. Logged as `[eaddrinuse-diag]` and written to the spawn port log when `BILI_LAUNCH_TOKEN` is set (this child's stderr is muted into bili.log, #2864). Covers BOTH paths: the pre-engine early twin-collision exit and the post-bind `server.on("error")` terminal branch. - **Why re-probe instead of trusting the boot-time verdict:** the early twin check (`findVerifiedSameLaneTwin`) runs seconds before bind, and the late path's `classifySameLanePeer` is registry-only (pid alive, NO health check). A wedge developing in between — exactly the #4762 shape — flips the truth under both. And one shallow probe cannot distinguish slow-alive from wedged-but-draining (the zombie answered some probes), which is why the wording says "appears wedged" and leaves the kill decision to the operator/spawner rather than acting on it. - **Out of scope (documented):** a parent-side force-kill after repeated EADDRINUSE belongs to the SPAWNER (the DeepSeek Harness), not this repo — the diagnostic line now tells it exactly what to do. -Behavior change (disclosed): OLD = a wedged standalone proxy lived forever holding its port and every respawn died with an opaque EADDRINUSE; NEW = continuous main-loop silence ≥ 5 min ⇒ one warn at 30 s, one FATAL log line, then `process.exit(1)` so a spawner's normal respawn recovers the port, plus a serving/wedged adjudication + kill guidance appended to every terminal EADDRINUSE message. No new config/env/CLI surface — thresholds are module constants. Plugin-mode behavior is unchanged (watchdog not armed there). +Behavior change (disclosed): OLD = a wedged standalone proxy lived forever holding its port and every respawn died with an opaque EADDRINUSE; NEW = continuous main-loop silence ≥ 5 min ⇒ one warn at 30 s, one FATAL log line, then a whole-process SIGKILL from the watchdog worker (#6157: `process.exit(1)` in a worker ends only the worker) so a spawner's normal respawn recovers the port — POSIX children end by signal (`code: null`), Windows via TerminateProcess — plus a serving/wedged adjudication + kill guidance appended to every terminal EADDRINUSE message. No new config/env/CLI surface — thresholds are module constants. Plugin-mode behavior is unchanged (watchdog not armed there). -Tests: `tests/event-loop-watchdog.test.ts` (verdict boundary table incl. warn-once semantics; default pin; REAL-worker wedge integration — a synchronous spin blocks the main loop past the hard threshold, the trip fires, both forensic lines land in the log file with the `[v=…]` tag; healthy stream never trips; spawn-failure seam degrades cleanly) + `tests/port-owner.test.ts` additions (wildcard-origin rewrite; serving/wedged/anonymous hint wording; live-responder pid beats stale registry pid; dead holder reads as not-serving; pre-registration "starting" reads as not-serving). +Tests: `tests/event-loop-watchdog.test.ts` (verdict boundary table incl. warn-once semantics; default pin; REAL-worker wedge integration — a synchronous spin blocks the main loop past the hard threshold, the trip fires, both forensic lines land in the log file with the `[v=…]` tag; healthy stream never trips; spawn-failure seam degrades cleanly; real "exit" mode in a child process — a wedged main thread dies with the whole process and the FATAL line lands first, #6157) + `tests/port-owner.test.ts` additions (wildcard-origin rewrite; serving/wedged/anonymous hint wording; live-responder pid beats stale registry pid; dead holder reads as not-serving; pre-registration "starting" reads as not-serving). ## Dependency audit — zod advisory GHSA-mrhx-c7xc-m3w5 and the jimp nested-zod false positive (#4820) diff --git a/src/codex-compact.ts b/src/codex-compact.ts index 75ded778b5..9b93fe67e8 100644 --- a/src/codex-compact.ts +++ b/src/codex-compact.ts @@ -1,4 +1,4 @@ -import { randomUUID } from "node:crypto"; +import { createHash, randomUUID } from "node:crypto"; import type { CompressionBlock } from "acp-kernel"; import { statusInputBaseline, type Session } from "./session.js"; import { codexCompactMode as knobCodexCompactMode } from "./knobs.js"; @@ -161,6 +161,19 @@ export function isBiliCompactionItem(item: unknown): boolean { return false; } +/** Native opaque checkpoints are explicit epoch boundaries; never decode them. + * The last checkpoint identifies the current epoch across echoes and restarts. */ +export function nativeCompactionKey(input: unknown): string | undefined { + if (!Array.isArray(input)) return undefined; + for (let i = input.length - 1; i >= 0; i--) { + const item = input[i] as { type?: unknown; encrypted_content?: unknown } | null; + if (!item || item.type !== "compaction" || isBiliCompactionItem(item)) continue; + if (typeof item.encrypted_content !== "string" || item.encrypted_content.length === 0) continue; + return createHash("sha256").update(item.encrypted_content).digest("hex"); + } + return undefined; +} + export function stripBiliCompactionItems(input: T[]): T[] { return input.filter((item) => !isBiliCompactionItem(item)); } diff --git a/src/codex-models.ts b/src/codex-models.ts index ff8fd5bbf1..cf9085f8eb 100644 --- a/src/codex-models.ts +++ b/src/codex-models.ts @@ -166,6 +166,19 @@ export function readCodexModelWindow(model: string, codexHome: string): number | return readCodexLiveModelWindow(model, codexHome)?.window; } +/** True when a codex-aligned window ALREADY nets out codex's own output + * headroom: the live table sources multiply by effective_context_window_percent, + * which codex defines as the share "usable for inputs, after reserving headroom + * for system prompts, tool overhead, and model output". Reserving bili's output + * headroom on top of such a window counts the output budget twice and puts the + * preflight wall below the point where codex itself auto-compacts (90% of the + * raw window), so a long codex session hits an unrecoverable 413 before codex + * ever compacts it. Other sources (client metadata, config.toml window, bundled + * snapshot, 272K fallback) are raw windows and keep the reservation. */ +export function codexWindowNetsOutputHeadroom(source: string | undefined): boolean { + return source === "codex-catalog" || source === "codex-models-cache"; +} + function alignedResult(limit: number, w: number, source: CodexWindowSource): { limit: number; clamped: boolean; source: CodexWindowSource } { return limit > w ? { limit: w, clamped: true, source } : { limit, clamped: false, source }; } diff --git a/src/event-loop-watchdog.ts b/src/event-loop-watchdog.ts index cbb98e6af6..fc0aa1f289 100644 --- a/src/event-loop-watchdog.ts +++ b/src/event-loop-watchdog.ts @@ -13,11 +13,16 @@ * Mechanism: a worker thread has its OWN event loop, so it keeps running while * the main thread spins. The main thread posts a heartbeat every 5 s; the * worker measures the gap and, after SOFT_WARN_MS of continuous silence, logs - * a warning; after HARD_TIMEOUT_MS it writes a forensic line and exits the - * WHOLE process (code 1) so the spawner respawns onto the freed port. Legit - * work never blocks the loop for five straight minutes — compression runs in - * sync slices that complete in seconds to minutes and yield between requests — - * so the hard threshold is deadlock detection, not load detection. + * a warning; after HARD_TIMEOUT_MS it writes a forensic line and kills the + * WHOLE process so the spawner respawns onto the freed port. The kill is a + * SIGKILL to its own pid, NOT process.exit(): inside a worker thread + * process.exit() ends only the worker, so the wedged proxy used to survive its + * own watchdog and run on unwatched. SIGKILL needs nothing from the blocked + * main thread (a JS SIGTERM listener would never run); on Windows + * process.kill maps to TerminateProcess. Legit work never blocks the loop for + * five straight minutes — compression runs in sync slices that complete in + * seconds to minutes and yield between requests — so the hard threshold is + * deadlock detection, not load detection. * * Scope discipline: plugin-injection mode (dist/agent/*) never calls * startServer, so a wedged HOST process is never at risk of being killed by @@ -68,9 +73,10 @@ interface ArmEventLoopWatchdogOptions { softWarnMs?: number; hardTimeoutMs?: number; checkIntervalMs?: number; - /** "exit" (production): the worker calls process.exit itself — required, - * because a wedged main thread can never receive a message asking it to - * die. "report": the worker posts {type:"trip"} and stays alive — test + /** "exit" (production): the worker SIGKILLs its own process itself, + * required because a wedged main thread can never receive a message + * asking it to die (and process.exit() in a worker ends only the worker). + * "report": the worker posts {type:"trip"} and stays alive — test * seam so the suite process is not killed. */ fatalMode?: "exit" | "report"; /** Test seam: swap the Worker constructor. */ @@ -138,6 +144,11 @@ const WORKER_CODE = [ " if (emitStderr) { try { process.stderr.write(row); } catch (e) {} }", "}", "", + "function killWholeProcess() {", + " try { process.kill(process.pid, \"SIGKILL\"); } catch (e) {}", + " process.exit(1);", + "}", + "", "function tick() {", " const gap = Date.now() - lastBeat;", " const v = verdict(gap);", @@ -148,7 +159,7 @@ const WORKER_CODE = [ " tripped = true;", " line(\"error\", \"[event-loop-watchdog] FATAL: main event loop silent for \" + fmt(gap) + \" (hard threshold \" + fmt(hardTimeoutMs) + \") — exiting so the spawner can respawn onto the freed port (#4762)\");", " try { parentPort.postMessage({ type: \"trip\", gapMs: gap }); } catch (e) {}", - " if (fatalMode === \"exit\") process.exit(1);", + " if (fatalMode === \"exit\") killWholeProcess();", " }", "}", "", diff --git a/src/preflight.ts b/src/preflight.ts index d4283ac6f2..c2f0177a0e 100644 --- a/src/preflight.ts +++ b/src/preflight.ts @@ -255,8 +255,10 @@ export interface PreflightDeps { proxyUrl?: string; signal?: AbortSignal; log: (level: string, msg: string) => void; - /** #1843: the image reserve for this request — the billed cost of the images riding the payload verbatim (#488). Folding never removes them, so every TEXT-channel decision compares the text estimate against `limit − reserve` instead of adding the reserve to each total; an image-estimate error can no longer move a fold decision. Kernel-facing quantities keep the total view (reserve added back), which is the conservative direction. */ + /** #1843: the image reserve — the billed cost of the images riding the payload verbatim (#488). Every TEXT-channel decision compares the text estimate against `limit − reserve` instead of adding the reserve to each total, so an image-estimate error can no longer move a fold decision; kernel-facing quantities keep the total view (reserve added back), which is the conservative direction. This field is the INITIAL wire view; on lanes where folds archive images off the wire (`measureImageReserve`), the working copy is re-measured from the post-fold view every round (#4932). */ imageReserve?: number; + /** Rebuild the protocol image view after folds; archived images no longer consume the window. */ + measureImageReserve?: (messages: CoreMessage[]) => number; /** Constant wire overhead for this request (system prompt + tool definitions, #470). Folding never removes it, so every fit decision must add it — otherwise the loop stops with "text fits" while the billed input still overflows. */ wireOverhead?: number; /** #553: the caller knows this session's input size is unmeasured AND its @@ -1750,11 +1752,11 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa // text estimate vs `target − imageReserve`. Algebraically the same firing // set as the old total-view check (max(B, T+R) >= C <=> max(max(0,B−R), T) // >= max(0,C−R)), but an image-estimate error can no longer start or stop - // folding. Images ride the payload verbatim through every round, so the - // reserve is constant for this invocation. Kernel-facing quantities below + // folding for a fixed wire view. Archived images leave that view, so the host + // refreshes their reserve after each fold. Kernel-facing quantities below // keep the total view (reserve added back): conservative direction, and the // kernel's own truncation/absorb behavior stays byte-identical. - const imageReserve = deps.imageReserve ?? 0; + let imageReserve = deps.imageReserve ?? 0; const wireOverhead = deps.wireOverhead ?? 0; // #1933 F1: capture k̂ once — it only mutates on usage settlement (outside // this invocation), while every fit judgment in this loop must stay on @@ -1776,7 +1778,25 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa // #5974: measured view-vs-reconstruction divergence (see PreflightDeps.viewDeltaCorrection). // Every fit judgment in this pass runs on the corrected scale; 0 keeps legacy behavior. const viewDeltaCorrection = Math.max(0, deps.viewDeltaCorrection ?? 0); - let textTarget = Math.max(0, Math.min(limit, deps.compressionTarget ?? limit) - imageReserve - viewDeltaCorrection); + let foldTarget = Math.min(limit, deps.compressionTarget ?? limit); + let textTarget = Math.max(0, foldTarget - imageReserve - viewDeltaCorrection); + const refreshImages = (view: CoreMessage[]): void => { + if (deps.measureImageReserve) imageReserve = deps.measureImageReserve(view); + textTarget = Math.max(0, foldTarget - imageReserve - viewDeltaCorrection); + }; + // Removing every nominated source gives an upper bound on image savings. + // This is only a futility bound: the real post-fold view decides fit. + const imageSavingBound = (ranges: Array<{ startRef: string; endRef: string }>, view: CoreMessage[]): number => { + if (!deps.measureImageReserve || imageReserve <= 0) return 0; + const { refToIdx } = refMaps(messages, deps.session.state); + const removed = new Set(); + for (const range of ranges) { + const start = refToIdx.get(range.startRef), end = refToIdx.get(range.endRef); + if (start === undefined || end === undefined || start > end) continue; + for (let i = start; i <= end; i++) removed.add(messages[i]!.id); + } + return Math.max(0, imageReserve - deps.measureImageReserve(view.filter(message => !removed.has(message.id)))); + }; const result: PreflightResult = { compressedRanges: 0, savedTokens: 0, payloadEstimate: applyEstimateCalibration(estimateCoreMessages(messages) + wireOverhead, kFactor, kOrigin, deps.upstreamOrigin) + imageReserve, rangesRemaining: 0, fitsWindow: true, hadFoldableCandidates: false }; // #4288: set when the round-top fit exit fired — the walk ended in a // forward decision, and no later verdict may override it (see the final @@ -1963,6 +1983,7 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa deps.session.state = turn.state; adoptContentStore(deps.session, turn.contentStore); const view = applyAbsorbView(turn.messages, turn.state, activeConfig, currentTokens); + refreshImages(view); const est = applyEstimateCalibration(estimateCoreMessages(view) + wireOverhead, kFactor, kOrigin, deps.upstreamOrigin) + imageReserve; result.payloadEstimate = est; // #2952 review: keep the char-count UPPER bound fresh through recovery. @@ -2202,6 +2223,7 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa const baselineFloor = messages.length > 0 ? ((deps.session.stats.lastInputTokensSource === "usage" || deps.session.stats.lastInputTokensSource === "overflow-arm") ? deps.session.stats.lastInputTokens : 0) : deps.session.stats.lastInputTokens; + refreshImages(turn.messages); const rawRoundText = estimateCoreMessages(turn.messages) + wireOverhead; // #1933 F1: same calibrated caliber as the gate's trigger/fit checks — // per-round exit and final fit must judge this payload identically to @@ -2346,7 +2368,7 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa let potential = resolvableMass(ranges); // #5974: corrected scale — the blind view understates the outbound body by viewDeltaCorrection. const deficit = result.payloadEstimate + viewDeltaCorrection - limit; - if (potential > 0 && deficit > 0 && calibrateFoldMass(potential) * FUTILITY_SLACK < deficit) { + if (potential > 0 && deficit > 0 && imageSavingBound(ranges, turn.messages) === 0 && calibrateFoldMass(potential) * FUTILITY_SLACK < deficit) { if (!relaxed) { // The soft-protected recent zone is not in `ranges` yet; #330 // makes it foldable on relax. Probe the relaxed view (CPU-only, @@ -2364,11 +2386,12 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa // #2638: structural verdicts hold under relax too (coverage-based, zone-independent) — dead mass cannot close the gap either. const relaxedRanges = filterDeadRanges(deps.session, viableRanges(probe.nudge?.compressibleRanges ?? []).filter((r) => minChars <= 0 || (r.chars ?? r.tokens * 4) >= minChars)); const relaxedPotential = resolvableMass(relaxedRanges); - if (calibrateFoldMass(relaxedPotential) * FUTILITY_SLACK >= deficit) { + if (imageSavingBound(relaxedRanges, probe.messages) > 0 || calibrateFoldMass(relaxedPotential) * FUTILITY_SLACK >= deficit) { activeConfig = relaxedConfig(deps.config); relaxed = true; // #5974: keep the correction through the relax reset, or it silently drops out mid-pass. - textTarget = Math.max(0, limit - imageReserve - viewDeltaCorrection); + foldTarget = limit; + textTarget = Math.max(0, foldTarget - imageReserve - viewDeltaCorrection); summaryCalls = 0; budgetHit = false; transientRetryBudget = TRANSIENT_EMPTY_RETRY_BUDGET; @@ -2411,7 +2434,8 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa activeConfig = relaxedConfig(deps.config); relaxed = true; // #5974: keep the correction through the relax reset, or it silently drops out mid-pass. - textTarget = Math.max(0, limit - imageReserve - viewDeltaCorrection); + foldTarget = limit; + textTarget = Math.max(0, foldTarget - imageReserve - viewDeltaCorrection); // #575-merge: the summarization budget counts per protection // regime — reset it on relax, else bad summaries burned under // normal protection can starve the relaxed walk entirely and @@ -2787,7 +2811,8 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa } // #5974: corrected scale — keep parity with the pre-walk gate above (the blind view understates the outbound body by viewDeltaCorrection). const deficit = result.payloadEstimate + viewDeltaCorrection - limit; - if (remaining > 0 && deficit > 0 && calibrateFoldMass(remaining) * FUTILITY_SLACK < deficit) { + const remainingRanges = ordered.slice(oi).filter(r => !skipSet.has(`${r.startRef}:${r.endRef}`)); + if (remaining > 0 && deficit > 0 && imageSavingBound(remainingRanges, turn.messages) === 0 && calibrateFoldMass(remaining) * FUTILITY_SLACK < deficit) { futileBail = true; bailRemaining = calibrateFoldMass(remaining); bailDeficit = deficit; diff --git a/src/responses-tool-output.ts b/src/responses-tool-output.ts index 155f82fda0..a31e7f140e 100644 --- a/src/responses-tool-output.ts +++ b/src/responses-tool-output.ts @@ -3,6 +3,11 @@ import { coreToResponses, patchResponsesInput, responsesToCore, type BiliMessage import { isRenderedSummaryMessage } from "acp-kernel"; import { systemToUser } from "./util.js"; +const responsesPartBinding = Symbol("responsesPartBinding"); +type PartBoundMessage = CoreMessage & { + [responsesPartBinding]?: { source: ResponseInputItem; part: ResponseContentPart; sourceId: string }; +}; + export function responsesToolImageParts(item: unknown): ResponseContentPart[] | undefined { if (typeof item !== "object" || item === null) return undefined; const raw = item as Record; @@ -27,6 +32,23 @@ function outputText(parts: ResponseContentPart[]): string { export function responsesToCoreWithToolImages(body: ResponsesRequestBody): ResponsesProjection { const projection = responsesToCore(body); + // The layout supplies exact source ownership, including duplicate images. + // Keep it on the core while it survives rendering; never guess ownership + // later from text equality or the first image in a multipart carrier. + const byId = new Map(projection.msgs.map(message => [message.id, message])); + for (const slot of projection.layout) { + const source = slot.original; + if (source.role !== "user" || !Array.isArray(source.content)) continue; + const parts = source.content as ResponseContentPart[]; + if (!parts.every(part => part.type === "input_image" || part.type === "input_text" || part.type === "output_text")) continue; + const emitted = parts.filter(part => part.type === "input_image" || (typeof part.text === "string" && part.text.length > 0)); + const ids = slot.coreIds ?? (slot.coreId ? [slot.coreId] : []); + if (ids.length !== emitted.length) continue; + ids.forEach((id, index) => { + const message = byId.get(id) as PartBoundMessage | undefined; + if (message) message[responsesPartBinding] = { source, part: emitted[index]!, sourceId: id }; + }); + } for (const message of projection.msgs) { const parts = responsesToolImageParts(message.rawResponsesItem); // Keep the codec's original content-derived id so persisted refs still @@ -104,9 +126,59 @@ export function mergeAdjacentConfigurationUpdates(items: readonly ResponseInputI // (isRenderedSummaryMessage) — client-origin mid system/developer cores are // conversation data and re-emit verbatim through their rawResponsesItem // sidecar; they are NOT rewritten here. +function boundCarrier(messages: PartBoundMessage[]): ResponseInputItem { + const source = messages[0]![responsesPartBinding]!.source; + const parts = messages.map(message => { + const part = message[responsesPartBinding]!.part; + return part.type === "input_image" || part.text === message.text ? part : { ...part, text: message.text ?? "" }; + }); + const original = source.content; + const unchanged = Array.isArray(original) && original.length === parts.length + && parts.every((part, index) => part === original[index]); + return unchanged ? source : { type: "message", role: "user", content: parts }; +} + +function boundProjection(projection: ResponsesProjection, messages: CoreMessage[]): ResponsesProjection { + const runs = new Map(); + let previous: PartBoundMessage | undefined; + for (const message of messages) { + const bound = message as PartBoundMessage; + const binding = bound[responsesPartBinding]; + if (!binding || message.role !== "user" || isRenderedSummaryMessage(message)) { + previous = undefined; + continue; + } + const groups = runs.get(binding.source) ?? []; + if (previous?.[responsesPartBinding]?.source === binding.source) groups.at(-1)!.push(bound); + else groups.push([bound]); + runs.set(binding.source, groups); + previous = bound; + } + const sources = new Map(projection.msgs.map(message => [message.id, message])); + const replaced = new Set(); + const rebound: BiliMessage[] = []; + const layout = projection.layout.flatMap(slot => { + const groups = runs.get(slot.original); + if (!groups) return [slot]; + for (const id of slot.coreIds ?? (slot.coreId ? [slot.coreId] : [])) replaced.add(id); + return groups.map(group => { + for (const message of group) { + const source = sources.get(message[responsesPartBinding]!.sourceId); + if (source) rebound.push({ ...source, id: message.id, text: message.text }); + } + const ids = group.map(message => message.id); + return { original: boundCarrier(group), coreId: ids[0], ...(ids.length > 1 ? { coreIds: ids } : {}) }; + }); + }); + // The virtual layout retains untracked wire items, but gives each surviving + // part its actual current ID. Separate runs leave an insertion anchor for + // a block summary between parts from the same original carrier. + return { ...projection, layout, msgs: [...projection.msgs.filter(message => !replaced.has(message.id)), ...rebound] }; +} + export function patchResponsesInputWithToolImages(projection: ResponsesProjection, messages: CoreMessage[]): string | ResponseInputItem[] { const voiced = systemToUser(messages, (m) => isRenderedSummaryMessage(m)); - const input = patchResponsesInput(projection, voiced); + const input = patchResponsesInput(boundProjection(projection, voiced), voiced); if (typeof input === "string") return input; const byOriginal = new Map(); const byCall = new Map(); @@ -122,8 +194,24 @@ export function patchResponsesInputWithToolImages(projection: ResponsesProjectio // #1999: same pre-codec re-voicing as patchResponsesInputWithToolImages above. // #4166: carriers only — client-origin mid system/developer stays verbatim. export function coreToResponsesWithToolImages(messages: CoreMessage[], customToolCallIds: Set = new Set()): ResponseInputItem[] { - return systemToUser(messages, (m) => isRenderedSummaryMessage(m)).flatMap((message) => { + const out: ResponseInputItem[] = []; + let pending: PartBoundMessage[] = []; + const flush = (): void => { + if (pending.length === 0) return; + out.push(boundCarrier(pending)); + pending = []; + }; + for (const message of systemToUser(messages, (m) => isRenderedSummaryMessage(m))) { + const bound = message as PartBoundMessage; + if (bound[responsesPartBinding] && message.role === "user" && !isRenderedSummaryMessage(message)) { + if (pending.length && pending[0]![responsesPartBinding]!.source !== bound[responsesPartBinding].source) flush(); + pending.push(bound); + continue; + } + flush(); const rebuilt = rebuildToolImages(message); - return rebuilt ? [rebuilt] : coreToResponses([message], customToolCallIds); - }); + out.push(...(rebuilt ? [rebuilt] : coreToResponses([message], customToolCallIds))); + } + flush(); + return out; } diff --git a/src/server.ts b/src/server.ts index 0f5aa38456..f06ee81a2b 100644 --- a/src/server.ts +++ b/src/server.ts @@ -4136,9 +4136,10 @@ export async function preflightCompressIfNeeded( // only the original tokenCount reads as "nothing happened" even when 16 // folds removed hundreds of thousands of tokens. foldedTokens/rangesLeft // stay undefined when no fold ran, so the original phrasing holds there. + let remainingImageTokens = imageTokens; const failFast = (status: number, detail: string, retryable: boolean, foldedTokens?: number, rangesLeft?: number): PreflightFailFast => { - const imageNote = imageTokens >= limit - ? ` Images alone account for ~${imageTokens} tokens (≥ window ${limit}); compression cannot remove them — shrink or remove the images, or raise the window.` + const imageNote = remainingImageTokens >= limit + ? ` Images alone account for ~${remainingImageTokens} estimated tokens (≥ window ${limit}); reduce the remaining image payload or raise the window.` : ""; const sizeClause = foldedTokens !== undefined && foldedTokens < tokenCount ? `context ~${foldedTokens} tokens (down from ~${tokenCount} before preflight) exceeds the model window ${limit}` @@ -4248,6 +4249,7 @@ export async function preflightCompressIfNeeded( // #3226: re-sign the summarization egress over its own body (same arm as the // main forward); undefined for unsigned traffic. const preflightResignCtx = inboundResignCredential(req.headers, opts, upstreamUrl); + const responsesProjection = prepared.responsesProjection; const preflightDeps: PreflightDeps = { core, session, @@ -4272,6 +4274,9 @@ export async function preflightCompressIfNeeded( signal: clientAbort.signal, log, imageReserve: imageTokens, + measureImageReserve: prepared.protocol === "responses" && responsesProjection + ? (view) => imageReserveFor(session, "responses", { input: patchResponsesInput(responsesProjection, view) }, opts, upstreamUrl) + : undefined, wireOverhead: overheadEstimate, unknownBaseline, upstreamOrigin: currentOrigin, @@ -4336,6 +4341,7 @@ export async function preflightCompressIfNeeded( // sessions keep the loop's own upper-bound judgment (result.fitsWindow, // #553) — the optimistic re-estimate is exactly what that regime distrusts. let outbound: Prepared = prepared; + let reducedImageWire = false; let fits: boolean; let foldedCalibratedSize: number | undefined; if (result.compressedRanges > 0) { @@ -4363,6 +4369,9 @@ export async function preflightCompressIfNeeded( // raw body rides; mirror outboundPayloadBreakdown's fallback). const rebuiltMsgs = rebuilt.processedMessages.length > 0 ? rebuilt.processedMessages : rebuilt.originalMessages; const rebuiltTextSize = estimateCoreMessages(rebuiltMsgs) + overheadEstimate; + const rebuiltImageTokens = imageReserveFor(session, rebuilt.protocol, rebuilt.body, opts, upstreamUrl); + remainingImageTokens = rebuiltImageTokens; + reducedImageWire = result.compressedRanges > 0 && rebuiltImageTokens < imageTokens; if (rebuiltTextSize > session.stats.lastInputTokens) { session.stats.lastInputTokens = rebuiltTextSize; session.stats.lastInputTokensSource = "estimate"; @@ -4372,7 +4381,7 @@ export async function preflightCompressIfNeeded( // The baseline anchor above stays raw deliberately: it is a floor for // future meters, and a deflated (k̂ < 1) value would only delay the // next trigger, never advance it. - const rebuiltCalibrated = applyEstimateCalibration(rebuiltTextSize, kFactor, kOrigin, currentOrigin) + imageTokens; + const rebuiltCalibrated = applyEstimateCalibration(rebuiltTextSize, kFactor, kOrigin, currentOrigin) + rebuiltImageTokens; // #3645: hoist the calibrated post-fold figure so the fail-fast message // quotes the same scale as this fit and the pre-fold tokenCount — quoting // the raw size there claimed a sub-window number exceeds the window when k̂>1. @@ -4386,21 +4395,22 @@ export async function preflightCompressIfNeeded( // already-at-window context. Raw caliber (images included, no k̂ // deflation) mirrors the fit-gate quantity; lifetime is bounded (see // session.ts) so never-reporting upstreams fall back to legacy sizing. - setPostRebuildAnchor(session, rebuiltTextSize + imageTokens); + setPostRebuildAnchor(session, rebuiltTextSize + rebuiltImageTokens); // [#3444] exhausted honest pass: the chain folded everything it could // (no failure) yet the rebuilt payload still sits at/above the growth // target — the target is physically unreachable for this session. // Record the measured floor so the next trigger arms on real growth // past it instead of re-firing every request; latest measurement wins. - if (growthArmed && result.failure === undefined && rebuiltTextSize + imageTokens >= compressionTarget) { - session.metadata.autoFoldFloorTokens = rebuiltTextSize + imageTokens; + if (growthArmed && result.failure === undefined && rebuiltTextSize + rebuiltImageTokens >= compressionTarget) { + session.metadata.autoFoldFloorTokens = rebuiltTextSize + rebuiltImageTokens; markDirty(session); - log("warn", `[${session.id}] auto-fold target ${compressionTarget} unreachable — the session's irreducible baseline measures ~${rebuiltTextSize + imageTokens} tokens (system prompt + tools + live tail); effective target raised to the floor until the gap closes (#3444). Raise compress.externalSummary.autoFoldTargetTokens above ${rebuiltTextSize + imageTokens} for headroom`); + log("warn", `[${session.id}] auto-fold target ${compressionTarget} unreachable — the session's irreducible baseline measures ~${rebuiltTextSize + rebuiltImageTokens} tokens (system prompt + tools + live tail); effective target raised to the floor until the gap closes (#3444). Raise compress.externalSummary.autoFoldTargetTokens above ${rebuiltTextSize + rebuiltImageTokens} for headroom`); } } else { - fits = unknownBaseline - ? result.fitsWindow - : estimateCoreMessages(prepared.processedMessages) + overheadEstimate + imageTokens < limit; + // No block landed: judge the same prepared wire with the same scoped + // calibration used at entry, including its raw-body fallback. A fresh + // core-only estimate can reject fitting requests or admit oversized ones. + fits = unknownBaseline ? result.fitsWindow : payloadFitsWindow; } const f = result.failure; // 重建过程也会让出执行权;所有转发许可都必须在此重新检查取消。 @@ -4555,13 +4565,16 @@ export async function preflightCompressIfNeeded( // upstream overflow evidence. Forward for the upstream to arbitrate billing // instead of fail-fasting a payload whose real bill likely fits (#496). The text // portion was already folded above when foldable; we do NOT re-loop. - if (imageArbitration && !localRefusalHere) { + const imageArbitrationAfterFold = reducedImageWire && resolveImageBilling(opts.imageBilling, upstreamUrl) === "pixels" + && session.stats.lastInputTokensSource !== "overflow-arm"; + if ((imageArbitration || imageArbitrationAfterFold) && !localRefusalHere) { // Same fallback as the fit-gate measurement above: processedMessages // empty ⇒ kernel transform failure ⇒ the raw body rides. const outMsgs = outbound.processedMessages.length > 0 ? outbound.processedMessages : outbound.originalMessages; const outText = estimateCoreMessages(outMsgs); - if (outText + overheadEstimate < limit && outText + overheadEstimate + imageTokens >= limit) { - log("info", `[${session.id}] preflight folded ${result.compressedRanges} range(s) but images alone (~${imageTokens} tokens) keep the estimate over window ${limit} with no upstream overflow evidence — forwarding for the upstream to arbitrate billing (#496/#1800)`); + const remainingImages = imageReserveFor(session, outbound.protocol, outbound.body, opts, upstreamUrl); + if (outText + overheadEstimate < limit && outText + overheadEstimate + remainingImages >= limit) { + log("info", `[${session.id}] preflight folded ${result.compressedRanges} range(s); remaining images (~${remainingImages} estimated tokens) still exceed window ${limit}; forwarding the reduced wire for upstream billing arbitration (#496/#1800)`); return outbound; } } diff --git a/src/server/handle.ts b/src/server/handle.ts index 95e5d5d171..3c25753341 100644 --- a/src/server/handle.ts +++ b/src/server/handle.ts @@ -14,7 +14,7 @@ import { resolveProxyDecision } from "../upstream-proxy.js"; import { contextFromRegistry, peekRegistryContext, peekRegistryOutputLimit, peekRegistryPriceProfile } from "../registry.js"; import { stampBillingPrice } from "../billing.js"; import { recordClientUa } from "../client-ua-registry.js"; -import { codexAlignedWindow } from "../codex-models.js"; +import { codexAlignedWindow, codexWindowNetsOutputHeadroom } from "../codex-models.js"; import { DecodedRequestAdmission, DecodedRequestBusyError } from "../request-body-budget.js"; import { hostIdForLog, maskHeadersForLog, maskUrlForLog, maskUrlsInText } from "../log-mask.js"; import { bodyMightCarryImages, buildIncomingImageIndex, pruneRetrieveImgExports, refreshIncomingImageIndex, type IndexedImage } from "../image-restore.js"; @@ -831,6 +831,10 @@ async function handleRequest( // operator that their own setting, not the upstream, is the wall they hit. let resolvedNativeWindow: number | undefined; let windowShrinkReason: "operator" | "codex" | undefined; + // The window was clamped to a codex live-table value that already reserves + // codex's own output headroom (effective_context_window_percent) — see + // codexWindowNetsOutputHeadroom. Gates the default output reservation below. + let codexWindowNetsOutput = false; // True when the resolved native window came from a low-confidence fallback // (built-in table / env default) instead of an authoritative source — such // windows get an effective-floor after output-headroom reservation (see @@ -1006,6 +1010,7 @@ async function handleRequest( reqConfig = { ...reqConfig, modelContextLimit: aligned.limit }; nativeFromFallback = false; windowShrinkReason = "codex"; + codexWindowNetsOutput = codexWindowNetsOutputHeadroom(aligned.source); log("info", `[codex] effective window clamped ${before} → ${aligned.limit} (window source=${aligned.source} for model=${model}) upstream=${embeddedUrl ? hostIdForLog(embeddedUrl) : "none"} route=${routeLabel}`); } else if (operatorWindowTuned && native !== undefined && reqConfig.modelContextLimit < native) { windowShrinkReason = "operator"; @@ -2383,7 +2388,8 @@ async function handleRequest( // three-level merge; default 0.25, aligned with billion-context-pi). // Resolved once here so the side-request guard below AND the main-path // reservation measure against the SAME capped window. - const headroomCap = resolveOutputHeadroomCap(resolveCompress(opts.routes, providerDestUrl, (parsed as { model?: string }).model, opts.compress).outputHeadroomMaxPct); + const resolvedHeadroomCompress = resolveCompress(opts.routes, providerDestUrl, (parsed as { model?: string }).model, opts.compress); + const headroomCap = resolveOutputHeadroomCap(resolvedHeadroomCompress.outputHeadroomMaxPct); // #1729: dsh native compaction guard — a compaction summarize call // (replayed prefix + COMPACTION_INSTRUCTION as the final user message) // is refused BEFORE any pipeline work: not forwarded, kernel state @@ -2692,7 +2698,21 @@ async function handleRequest( headroomOrigin, (parsed as { model?: string }).model, ); - if (shouldReserveOutputHeadroom(protocol, headroomOrigin, requestModel) || provenReserve > 0) { + // A codex live-table window already nets out codex's own output + // headroom; reserving bili's default share on top double-counts it and + // walls the session below codex's own auto-compact point. An explicit + // operator outputHeadroomMaxPct and a proven completion share still apply. + const operatorHeadroomCap = resolvedHeadroomCompress.outputHeadroomMaxPct !== undefined; + const skipForCodexWindow = codexWindowNetsOutput && !operatorHeadroomCap; + const reserveByProtocol = shouldReserveOutputHeadroom(protocol, headroomOrigin, requestModel) && !skipForCodexWindow; + if (skipForCodexWindow && provenReserve === 0) { + const skipModel = String(requestModel ?? "?"); + if (!headroomFallbackLogged.has(`${skipModel}|codex-netted`)) { + headroomFallbackLogged.add(`${skipModel}|codex-netted`); + log("info", `[headroom] model=${skipModel}: no output reservation — the codex-aligned window ${reqConfig.modelContextLimit} already nets out codex's own output headroom (effective_context_window_percent); set compress.outputHeadroomMaxPct to reserve anyway`); + } + } + if (reserveByProtocol || provenReserve > 0) { const p = parsed as Record; const rawMax = p.max_tokens ?? p.max_completion_tokens ?? p.max_output_tokens; let maxOutput = typeof rawMax === "number" ? rawMax : 0; @@ -2766,7 +2786,7 @@ async function handleRequest( } } } - if (provenReserve > 0 && !shouldReserveOutputHeadroom(protocol, headroomOrigin, requestModel)) { + if (provenReserve > 0 && !reserveByProtocol) { // Proven share outranks the client-side fallbacks: it is what // the upstream already counted into a rejection on this exact // origin+model, so window − proven share is the input it accepts. @@ -2774,7 +2794,7 @@ async function handleRequest( const proofModel = (parsed as { model?: string }).model ?? "?"; if (!headroomFallbackLogged.has(`${proofModel}|proven-completion`)) { headroomFallbackLogged.add(`${proofModel}|proven-completion`); - log("info", `[headroom] model=${proofModel}: reserving the PROVEN completion share ${provenReserve} although protocol ${protocol ?? "unknown"} is exempt (#3450) — this origin+model billed completion inside the wall in an earlier rejection; judging input against ${nativeWindow} - ${provenReserve}`); + log("info", `[headroom] model=${proofModel}: reserving the PROVEN completion share ${provenReserve} although ${skipForCodexWindow ? "the codex-aligned window already nets out codex's own output headroom" : `protocol ${protocol ?? "unknown"} is exempt`} (#3450) — this origin+model billed completion inside the wall in an earlier rejection; judging input against ${nativeWindow} - ${provenReserve}`); } } // #2283 sub-defect 1: emit [window-change] when the per-session resolution diff --git a/src/server/prepare-responses.ts b/src/server/prepare-responses.ts index 1ae2087adb..bd9b491f2a 100644 --- a/src/server/prepare-responses.ts +++ b/src/server/prepare-responses.ts @@ -37,7 +37,7 @@ import { estimateCoreMessages, estimateCoreMessagesUpper } from "../preflight.js import { hoistTrappedToolItems } from "../tool-pair-order.js"; import { isStrictReasoningEcho, modelIdOf, normalizeStrictEchoResponsesInput } from "../strict-echo.js"; import { reconcileSystemAnchor } from "../system-anchor.js"; -import { buildPromptForgeBody, buildTriggerForgeBody, carriesCodexLocalCompactionSummary, codexCompactGate, codexCompactGatePre, codexCompactMode, hasCompactionPrompt, hasCompactionTrigger, isCodexClient, isCodexHost, mergeForgedSummaries, replaceBiliCompactionItems, stripBiliCompactionItems, CODEX_COMPACT_HEALTH_RATIO, CODEX_LOCAL_COMPACTION_MIN_MISSING } from "../codex-compact.js"; +import { buildPromptForgeBody, buildTriggerForgeBody, carriesCodexLocalCompactionSummary, codexCompactGate, codexCompactGatePre, codexCompactMode, hasCompactionPrompt, hasCompactionTrigger, isCodexClient, isCodexHost, mergeForgedSummaries, nativeCompactionKey, replaceBiliCompactionItems, stripBiliCompactionItems, CODEX_COMPACT_HEALTH_RATIO, CODEX_LOCAL_COMPACTION_MIN_MISSING } from "../codex-compact.js"; import type { ConversationIdentity } from "../session-id.js"; import { pckKeyAbated } from "../pck-probe.js"; import { reconcilePluginFoldCallIds } from "../plugin.js"; @@ -79,6 +79,12 @@ export async function prepareResponses( log("info", `[${sessionId}] reconciled ACP state after native Responses compact boundary`); } + // Unlike the legacy text-template heuristic, an opaque native checkpoint + // explicitly announces a replacement history even after old blocks were + // reaped. Persist its identity so replay/restart cannot reset newer folds. + const checkpoint = isCodexHost(req.headers) && !hasCompactionTrigger(parsed.input) + ? nativeCompactionKey(parsed.input) : undefined; + // A codex client echoes our forged compaction item back in the next // request; replace it with a plain summary-carrying user message so the // handoff rides the replayable history (kernel-compressible, retained by @@ -208,6 +214,21 @@ export async function prepareResponses( // client, not just plugin/codex; the head prefix still hoists into // systemParts for strict single-system upstreams (#377). const projection = responsesToCore(parsed); + if (checkpoint && checkpoint !== session.metadata.codexNativeCompactionKey) { + const covered = coveredRealHistoryIds(session.state.blocks); + const gap = foldCoverage(covered, projection.msgs.map(message => message.id)); + // A checkpoint may predate this process's first observation (for + // example after an upgrade). Keep folds whose entire source still + // exists; only missing coverage or an empty substrate needs rebasing. + const rebase = covered.size === 0 || gap !== null; + if (rebase) { + markNativeCompactionBoundary(session); + reconcileNativeCompactionBoundary(session); + } + session.metadata.codexNativeCompactionKey = checkpoint; + markDirty(session); + log("info", `[${sessionId}] native opaque Responses checkpoint observed; ACP state ${rebase ? "rebased" : "retained (coverage intact)"}`); + } responsesProjection = projection; // Client's own system text captured BEFORE the anchor reconciliation // below can replace systemParts — fingerprinting the post-anchor value diff --git a/src/storage/shadow.ts b/src/storage/shadow.ts index 4c9a19120f..e5e96139f7 100644 --- a/src/storage/shadow.ts +++ b/src/storage/shadow.ts @@ -26,6 +26,7 @@ import { log as loggerLog } from "../logger.js"; import { planIngest, planIngestDelta, loadLegacyView, LegacyViewBuilder, appendBlockContents, mergeOrphanBlockContents, completeBlockContents, type LegacySessionLike } from "./ingest.ts"; +import { splitMediaContent } from "./identity.js"; import type { BlockView } from "../session.js"; import { resolveStorageMode, resolveStorageWalJournalSizeLimit, type StorageMode } from "../knobs.ts"; import { UnifiedStore, StorageIntegrityError, type CheckpointRow, type SessionSummaryRow, type WebSummaryRow } from "./store.ts"; @@ -1608,29 +1609,61 @@ class BatchedViewVerification { } private compareMessages(key: "lastMessages" | "pluginSnapshot", seqs: number[] | null): void { + // The two legs intentionally hold different SLICES of the same log. The rows are + // the raw tail interned at save time (`tailSeqs`, oldest-first, the whole resident + // window); the envelope carries only `boundedFoldedSnapshot`, which prunes from the + // FRONT and keeps the NEWEST messages under `persist.tailTokens` (#2960 (5), caveat + // 2a). Measured on live sessions the envelope is an exact SUFFIX of the row tail — + // tail=1095/envelope=10 and tail=752/envelope=22, element-wise aligned at the end, + // zero id and zero content mismatches inside the overlap. + // + // Comparing positionally from index 0 therefore compared the oldest row against the + // oldest RETAINED message, i.e. off by `tail.length - messages.length`, so `want` was + // undefined for every slot outside the envelope window: `changed()` for every + // tailSeq, the streak latched, and each save paid a multi-second FULL ingest + // (measured: 44 full ingests, avg 4522 ms, max 9363 ms, 170.8 s of stalled event + // loop in one episode). Align the envelope to its suffix position instead and + // compare only the overlap — a genuine content divergence inside that window still + // reports, and a leg that is not a suffix produces mismatches rather than silence. + // + // Absence of a leg is handled by the header digest, which distinguishes `undefined` + // from `[]` (`withoutBulk`), so it must not be re-litigated here. Row PRESENCE is + // still checked (tailGaps, #4390) for every seq. const expected = this.legacy[key]; + const tailLen = seqs?.length ?? 0; + const offset = expected === undefined ? 0 : Math.max(0, tailLen - expected.length); let index = 0; for (const seq of seqs ?? []) { const rec = this.messages.get(seq); if (!rec) { + // A recorded tail pointer with no row: the table leg is shorter than the tail + // it claims, so this is a real divergence (#4390), not an alignment artifact. this.tailGaps++; + this.changed(key); + if (key === "lastMessages" && !this.tailSlot) { + this.tailSlot = { index, expect: missingSlot(), got: missingSlot() }; + } + index++; continue; } - const want = expected && index < expected.length ? messageSignature(expected[index]) : undefined; - if (!want || want.digest !== rec.sig.digest) { + const wantIndex = index - offset; + const want = expected !== undefined && wantIndex >= 0 && wantIndex < expected.length ? messageSignature(expected[wantIndex]) : undefined; + if (want !== undefined && want.digest !== rec.sig.digest) { this.changed(key); if (key === "lastMessages" && expected && !this.tailSlot) { - const fields = diffFieldNames(expected[index], rec.raw); + const fields = diffFieldNames(expected[wantIndex], rec.raw); this.tailSlot = fields === undefined - ? { index, expect: want?.slot ?? missingSlot(), got: rec.sig.slot } - : { index, expect: want?.slot ?? missingSlot(), got: rec.sig.slot, fields }; + ? { index, expect: want.slot, got: rec.sig.slot } + : { index, expect: want.slot, got: rec.sig.slot, fields }; } } index++; } - if (seqs && expected && index < expected.length) { + // The envelope claims MORE messages than the row tail holds: the table leg lost rows + // the envelope still carries. That is a real divergence in either direction. + if (seqs && expected && expected.length > tailLen) { this.changed(key); - if (key === "lastMessages" && !this.tailSlot) this.tailSlot = { index, expect: messageSignature(expected[index]).slot, got: missingSlot() }; + if (key === "lastMessages" && !this.tailSlot) this.tailSlot = { index: tailLen, expect: messageSignature(expected[tailLen]).slot, got: missingSlot() }; } } @@ -1651,16 +1684,37 @@ function missingSlot(): DriftSlotFingerprint { return { id: "-", role: "-", length: 0, sha8: "-" }; } +/** #6351: the two legs hold the same message in DIFFERENT media representations. + * + * Rows are written media-SPLIT (`planIngest` -> `splitMediaContent`: an + * `imageBase64` payload and every >=2 KiB data URL become `{"$media":hash}` + * CAS references), but `UnifiedStore.getMessages()` returns them media- + * HYDRATED (`hydrateMedia` inlines the payload again) — the verifier reads the + * tail through exactly that accessor, so its leg is hydrated while the + * envelope's `lastMessages` keeps the split form. + * + * Hashing the canonical JSON as-is therefore compares a ~2 KB placeholder + * against a ~200 KB inlined payload for the SAME message and marks drift: a + * measured 1,991 vs 201,293 bytes on one tool result, and ten 390-byte user + * rows that hydrate to 112-672 KB. That is a digest-shape mismatch (#4392 + * family), so the latch it triggers re-ingests the whole tail forever (#5677). + * + * Normalize BOTH legs to the split form before hashing: it is idempotent on + * already-split input, so the envelope leg passes through unchanged, and + * `splitMediaContent(hydrate(x)) === x` was verified byte-identical on every + * media-bearing row sampled, so a real content edit still changes the digest. + */ function messageSignature(value: unknown): { digest: string; slot: DriftSlotFingerprint } { const json = JSON.stringify(canonicalize(value)) ?? "null"; - const digest = sha256(json); + const normalized = splitMediaContent(json).content; + const digest = sha256(normalized); const message = value as { id?: unknown; role?: unknown } | null; return { digest, slot: !message || typeof message !== "object" ? missingSlot() : { id: typeof message.id === "string" ? message.id : "-", role: typeof message.role === "string" ? message.role : "-", - length: json.length, + length: normalized.length, sha8: digest.slice(0, 8), }, }; diff --git a/tests/codex-local-compaction.test.ts b/tests/codex-local-compaction.test.ts index 824bfc8c92..6298db4ae9 100644 --- a/tests/codex-local-compaction.test.ts +++ b/tests/codex-local-compaction.test.ts @@ -2,15 +2,20 @@ import assert from "node:assert/strict"; import http from "node:http"; import { once } from "node:events"; import test from "node:test"; +import { mkdtempSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { rmrf } from "./tmp-rm.ts"; process.env.NODE_ENV = "test"; -import { defaultConfig } from "acp-kernel"; +import { createCore, defaultConfig } from "acp-kernel"; +import { responsesToCoreWithToolImages as responsesToCore } from "../src/responses-tool-output.js"; import { startServer } from "../src/server.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; import { _setForTest as setRegistryForTest } from "../src/registry.ts"; import { listSessions, _resetSessionsForTest } from "../src/session.ts"; -import { CODEX_LOCAL_COMPACTION_SUMMARY_PREFIX } from "../src/codex-compact.ts"; +import { CODEX_LOCAL_COMPACTION_SUMMARY_PREFIX, CODEX_COMPACT_ID_PREFIX, CODEX_COMPACT_SENTINEL, nativeCompactionKey } from "../src/codex-compact.ts"; // Issue #2372 regression: codex's LOCAL auto-compaction (rollout event // type:"compacted") never transits the proxy — no compaction_trigger item, no @@ -72,10 +77,11 @@ function compactedInput() { type Harness = { url: string; bodies: string[]; + store: SessionStore; close: () => Promise; }; -async function startHarness(): Promise { +async function startHarness(dir?: string): Promise { const bodies: string[] = []; const upstream = http.createServer((req, res) => { const chunks: Buffer[] = []; @@ -108,7 +114,8 @@ async function startHarness(): Promise { await once(upstream, "listening"); const upstreamPort = (upstream.address() as { port: number }).port; - _setStoreForTest(new SessionStore({ enabled: false })); + const store = new SessionStore({ dir, enabled: dir !== undefined }); + _setStoreForTest(store); setRegistryForTest({}); _resetSessionsForTest(); const proxy = await startServer({ @@ -139,6 +146,7 @@ async function startHarness(): Promise { return { url: `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1/responses`, bodies, + store, close: async () => { proxy.close(); proxy.closeIdleConnections?.(); @@ -146,6 +154,7 @@ async function startHarness(): Promise { upstream.close(); upstream.closeIdleConnections?.(); await once(upstream, "close"); + store.closeQuietly(); }, }; } @@ -170,6 +179,110 @@ test("drift pin: codex local-compaction summary template header is versioned (#2 ); }); +test("opaque native compaction recovers a pending epoch with fewer than eight missing ids, once per checkpoint", async () => { + const dir = mkdtempSync(join(tmpdir(), "bili-native-checkpoint-")); + const h = await startHarness(dir); + try { + await seedFoldedSession(h); + const session = listSessions().find(s => s.meta.label === "codex-sess-a")!; + for (const block of session.state.blocks) { + block.effectiveMessageIds = block.effectiveMessageIds.slice(0, 4); + block.directMessageIds = block.directMessageIds.slice(0, 4); + } + session.metadata.foldEpochConfirmPending = true; + session.stats.lastInputTokens = 299_686; + session.stats.lastInputTokensSource = "usage"; + const checkpoint = { type: "compaction", id: "native-checkpoint", encrypted_content: "opaque-native-checkpoint-payload" }; + const input = [checkpoint, ...seedInput().slice(-2)]; + const send = async (items: unknown[]) => { + const response = await fetch(h.url, { + method: "POST", headers: { "content-type": "application/json", "user-agent": CODEX_UA }, + body: JSON.stringify({ model: "gpt-resp", stream: true, session_id: "codex-sess-a", input: items }), + }); + const body = await response.text(); assert.equal(response.status, 200, body); + }; + await send(input); + assert.equal(session.state.blocks.length, 0, "native replacement consumes the stale fold epoch"); + assert.equal(session.metadata.foldEpochConfirmPending, undefined, "preflight is no longer paused"); + assert.ok(session.metadata.nativeCompactionAt); + assert.ok(h.bodies.at(-1)!.includes(checkpoint.encrypted_content), "native blob is forwarded verbatim"); + const continued = [...input, ...Array.from({ length: 8 }, (_, i) => ({ type: "message", role: i % 2 ? "assistant" : "user", content: `Post-checkpoint message ${i}. ` + "retained decision ".repeat(200) }))]; + await send(continued); + const messages = responsesToCore({ input: continued }).msgs; + const selected = messages.slice(-8, -5); + const folded = createCore().applyCompression({ messages, state: session.state, + config: { ...defaultConfig(30_000), preserveRecentMessages: 0, preserveRecentTokens: 0 }, + ranges: [{ startRef: session.state.messageRefs.byRaw[selected[0]!.id]!, endRef: session.state.messageRefs.byRaw[selected[2]!.id]!, summary: "The new post-checkpoint work established three retained decisions and independent validation results for continuing the task." }], + }); + session.state = folded.state; + assert.ok(session.state.blocks.some(block => block.active), "new work can be folded after recovery"); + const before = { refs: { ...session.state.messageRefs.byRaw }, at: session.metadata.nativeCompactionAt, ids: session.state.blocks.map(block => block.blockId) }; + await send(continued); + assert.deepEqual(session.state.messageRefs.byRaw, before.refs, "hot checkpoint echo keeps stable refs"); + await h.store.writeNow(session); + const reloaded = h.store.loadSync(session.id)!; + assert.equal(reloaded.metadata.codexNativeCompactionKey, session.metadata.codexNativeCompactionKey, "checkpoint identity survives persistence"); + // Cold loading may apply the existing one-time ledger alignment + // (#4316). The checkpoint must not reset those aligned refs again. + const alignedRefs = { ...reloaded.state.messageRefs.byRaw }; + session.state = reloaded.state; session.metadata = reloaded.metadata; session.stats = reloaded.stats; + await send(continued); + assert.equal(session.metadata.nativeCompactionAt, before.at, "persisted checkpoint does not rebase twice"); + assert.deepEqual(session.state.blocks.map(block => block.blockId), before.ids, "checkpoint echo preserves newer folds"); + assert.ok(session.state.blocks.some(block => block.active)); + assert.deepEqual(session.state.messageRefs.byRaw, alignedRefs, "persisted checkpoint preserves the loaded ref map"); + } finally { await h.close(); rmrf(dir); } +}); + +test("native checkpoint identity excludes bili carriers and malformed input, and follows the last native checkpoint", () => { + const native = (encrypted_content: string) => ({ type: "compaction", encrypted_content }); + const first = native("opaque-first"), last = native("opaque-last"); + const bili = { ...native("bili"), id: `${CODEX_COMPACT_ID_PREFIX}echo` }; + for (const input of [undefined, {}, [null], [native("")], [bili], [native(`${CODEX_COMPACT_SENTINEL}summary`)]]) { + assert.equal(nativeCompactionKey(input), undefined); + } + assert.notEqual(nativeCompactionKey([first]), nativeCompactionKey([last])); + assert.equal(nativeCompactionKey([first, last, bili]), nativeCompactionKey([last])); +}); + +test("a non-Codex opaque item does not reset active folds", async () => { + const h = await startHarness(); + try { + await seedFoldedSession(h); + const session = listSessions().find(s => s.meta.label === "codex-sess-a")!; + const ids = session.state.blocks.map(block => block.blockId); + const response = await fetch(h.url, { + method: "POST", headers: { "content-type": "application/json", "user-agent": "ordinary-responses-client" }, + body: JSON.stringify({ model: "gpt-resp", stream: true, session_id: "codex-sess-a", input: [ + { type: "compaction", encrypted_content: "opaque-client-data" }, ...seedInput(), + ] }), + }); + assert.equal(response.status, 200, await response.text()); + assert.deepEqual(session.state.blocks.map(block => block.blockId), ids); + assert.equal(session.metadata.codexNativeCompactionKey, undefined); + } finally { await h.close(); } +}); + +test("first observing an older native checkpoint preserves fully covered folds", async () => { + const h = await startHarness(); + try { + await seedFoldedSession(h); + const session = listSessions().find(s => s.meta.label === "codex-sess-a")!; + const ids = session.state.blocks.map(block => block.blockId); + const response = await fetch(h.url, { + method: "POST", headers: { "content-type": "application/json", "user-agent": CODEX_UA }, + body: JSON.stringify({ model: "gpt-resp", stream: true, session_id: "codex-sess-a", input: [ + { type: "compaction", encrypted_content: "older-native-checkpoint" }, ...seedInput(), + ] }), + }); + assert.equal(response.status, 200, await response.text()); + assert.deepEqual(session.state.blocks.map(block => block.blockId), ids); + assert.ok(session.state.blocks.some(block => block.active)); + assert.ok(session.metadata.codexNativeCompactionKey); + assert.equal(session.metadata.nativeCompactionAt, undefined); + } finally { await h.close(); } +}); + test("e2e #2372: codex replaying [summary, retained tail] rebases the ACP state instead of drifting forever", async () => { const h = await startHarness(); try { diff --git a/tests/codex-window-no-double-headroom.test.ts b/tests/codex-window-no-double-headroom.test.ts new file mode 100644 index 0000000000..5e21faafc9 --- /dev/null +++ b/tests/codex-window-no-double-headroom.test.ts @@ -0,0 +1,126 @@ +import assert from "node:assert/strict"; +import fs from "node:fs"; +import http from "node:http"; +import os from "node:os"; +import path from "node:path"; +import { once } from "node:events"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +// A codex live-table window (models_cache.json / model_catalog_json) is +// context_window x effective_context_window_percent, and codex defines that +// percent as the share usable for inputs AFTER reserving model output. bili +// used to reserve its default 25% output headroom on top, so for a 272K model +// codex grows the session to its own auto-compact point (90% of 272K = 244.8K) +// while bili's preflight wall sat at 258.4K x 0.75 = 193.8K: every turn past +// that is an unrecoverable 413 ("futile round") that codex keeps retrying. + +import { codexWindowNetsOutputHeadroom } from "../src/codex-models.ts"; +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { setLogCapture } from "../src/logger.ts"; + +const MODEL = "gpt-codex-window-test"; +const NATIVE = 1_000_000; +const CODEX_CONTEXT = 272_000; +const CODEX_EFFECTIVE = 258_400; // 272000 x 95% +const CODEX_UA = "codex_cli_rs/0.53.0 (linux 6.8.0; x86_64) cli"; + +test("only live codex tables (which apply effective_context_window_percent) count as netted", () => { + assert.equal(codexWindowNetsOutputHeadroom("codex-models-cache"), true); + assert.equal(codexWindowNetsOutputHeadroom("codex-catalog"), true); + for (const raw of ["client-metadata", "codex-config-window", "bundled-snapshot", "codex-fallback", undefined]) { + assert.equal(codexWindowNetsOutputHeadroom(raw), false, `${String(raw)} is a raw window and keeps the reservation`); + } +}); + +function makeUpstream(): http.Server { + return http.createServer((_req, res) => { + res.writeHead(200, { "content-type": "text/event-stream" }); + res.write(`data: ${JSON.stringify({ id: "cw", object: "chat.completion.chunk", created: 1, model: MODEL, choices: [{ index: 0, delta: { role: "assistant", content: "ok" }, finish_reason: null }] })}\n\n`); + res.write(`data: ${JSON.stringify({ id: "cw", object: "chat.completion.chunk", created: 1, model: MODEL, choices: [{ index: 0, delta: {}, finish_reason: "stop" }], usage: { prompt_tokens: 100, completion_tokens: 3, total_tokens: 103 } })}\n\n`); + res.end(`data: [DONE]\n\n`); + }); +} + +async function withProxy( + t: { after(fn: () => void | Promise): void }, + modelCompress: Record | undefined, +): Promise<{ proxyPort: number; upstreamPort: number }> { + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-codex-headroom-")); + fs.writeFileSync(path.join(home, "models_cache.json"), JSON.stringify({ models: [{ slug: MODEL, context_window: CODEX_CONTEXT, max_context_window: 872_000 }] })); + const previous = process.env.CODEX_HOME; + process.env.CODEX_HOME = home; + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const upstream = makeUpstream(); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + const model: Record = { context: NATIVE, output: 128_000 }; + if (modelCompress) model.compress = modelCompress; + const proxy = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { [MODEL]: model } } }, + modelContextLimit: NATIVE, + kernelConfig: defaultConfig(NATIVE), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: true, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + t.after(async () => { + proxy.close(); + upstream.close(); + if (previous === undefined) delete process.env.CODEX_HOME; else process.env.CODEX_HOME = previous; + fs.rmSync(home, { recursive: true, force: true }); + }); + return { proxyPort: (proxy.address() as { port: number }).port, upstreamPort }; +} + +async function drive(proxyPort: number, upstreamPort: number, session: string): Promise { + // No output-budget field, like codex's native Responses traffic: the + // reservation falls back to the declared max output (128000). + const r = await fetch(`http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1/chat/completions`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": session, "user-agent": CODEX_UA }, + body: JSON.stringify({ model: MODEL, stream: true, messages: [{ role: "user", content: "hello" }] }), + }); + assert.equal(r.status, 200, `request must succeed, got ${r.status}: ${await r.text().catch(() => "")}`); + await r.body?.cancel().catch(() => { /* drained */ }); +} + +test("codex live-table window is not reserved a second time: the wall stays at codex's own effective window", async (t) => { + const logs: string[] = []; + setLogCapture((level, msg) => { logs.push(`${level} ${msg}`); }); + t.after(() => setLogCapture(null)); + const h = await withProxy(t, undefined); + await drive(h.proxyPort, h.upstreamPort, "codex-netted"); + assert.ok(logs.some((l) => l.includes(`effective window clamped ${NATIVE} → ${CODEX_EFFECTIVE}`) && l.includes("codex-models-cache")), + `codex clamp to ${CODEX_EFFECTIVE} expected, got: ${JSON.stringify(logs.filter((l) => l.includes("[codex]")))}`); + const reserved = logs.filter((l) => l.includes(`[headroom] model=${MODEL}: effective window`)); + assert.deepEqual(reserved, [], `no second output reservation on a codex-netted window (pre-fix: ${CODEX_EFFECTIVE} -> ${CODEX_EFFECTIVE * 0.75})`); + assert.ok(logs.some((l) => l.includes(`[headroom] model=${MODEL}: no output reservation`)), "the skip must be stated once in the log"); +}); + +test("an explicit operator outputHeadroomMaxPct still reserves on a codex window", async (t) => { + const logs: string[] = []; + setLogCapture((level, msg) => { logs.push(`${level} ${msg}`); }); + t.after(() => setLogCapture(null)); + const h = await withProxy(t, { outputHeadroomMaxPct: 0.1 }); + await drive(h.proxyPort, h.upstreamPort, "codex-operator"); + const reserved = logs.filter((l) => l.includes(`[headroom] model=${MODEL}: effective window`)); + assert.equal(reserved.length, 1, `operator cap must still reserve, got: ${JSON.stringify(reserved)}`); + assert.ok(reserved[0].includes(`effective window ${CODEX_EFFECTIVE} -> ${CODEX_EFFECTIVE - 25_840}`), `10% of ${CODEX_EFFECTIVE} reserved, got: ${reserved[0]}`); +}); diff --git a/tests/event-loop-watchdog.test.ts b/tests/event-loop-watchdog.test.ts index 1143518f8b..1e64753c32 100644 --- a/tests/event-loop-watchdog.test.ts +++ b/tests/event-loop-watchdog.test.ts @@ -4,6 +4,8 @@ import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import { Worker } from "node:worker_threads"; +import { spawnSync } from "node:child_process"; +import { fileURLToPath, pathToFileURL } from "node:url"; import { WATCHDOG_HEARTBEAT_MS, WATCHDOG_HARD_TIMEOUT_MS, @@ -191,3 +193,34 @@ test("with NO file leg the worker's stderr is the sole forensic record and must await sleep(20); } }); + +test("exit mode kills the WHOLE process while the main thread is wedged, not just the worker", () => { + // process.exit() in a worker thread ends only the worker: the wedged proxy + // used to survive its own watchdog, keep the port and log "could not arm + // (... exited with code 1) - continuing unwatched". Run the real "exit" + // path in a child; SURVIVED on stdout means the watchdog failed. + // The child is a module FILE, not `-e --input-type=module`: the eval worker + // inherits execArgv, and an ESM-typed eval worker has no `require`. + const root = fileURLToPath(new URL("..", import.meta.url)); + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "bili-watchdog-exit-")); + const logFile = path.join(dir, "bili.log"); + const child = path.join(dir, "child.mts"); + const modUrl = pathToFileURL(path.join(root, "src", "event-loop-watchdog.ts")).href; + fs.writeFileSync(child, [ + `import { armEventLoopWatchdog } from ${JSON.stringify(modUrl)};`, + `armEventLoopWatchdog({ logFile: ${JSON.stringify(logFile)}, heartbeatMs: 20, checkIntervalMs: 20, softWarnMs: 60, hardTimeoutMs: 150, fatalMode: "exit", stderrTty: false });`, + "setTimeout(() => {", + " const end = Date.now() + 5000;", + " while (Date.now() < end) { /* wedge the main loop */ }", + " process.stdout.write(\"SURVIVED\");", + "}, 100);", + ].join("\n")); + try { + const r = spawnSync(process.execPath, ["--import", "tsx", child], { encoding: "utf8", cwd: root, timeout: 60_000 }); + assert.ok(!r.stdout.includes("SURVIVED"), `wedged process outlived its watchdog (status=${r.status} signal=${r.signal})`); + assert.ok(r.status !== 0 || r.signal !== null, `child must die abnormally (status=${r.status} signal=${r.signal})`); + assert.match(fs.readFileSync(logFile, "utf8"), /FATAL: main event loop silent/); + } finally { + rmrf(dir); + } +}); diff --git a/tests/folded-tail-suffix-alignment.test.ts b/tests/folded-tail-suffix-alignment.test.ts new file mode 100644 index 0000000000..1dfb31d256 --- /dev/null +++ b/tests/folded-tail-suffix-alignment.test.ts @@ -0,0 +1,187 @@ +import { after, test, type TestContext } from "node:test"; +import assert from "node:assert/strict"; +import * as fs from "node:fs"; +import * as os from "node:os"; +import * as path from "node:path"; +import type { CompressionBlock, CoreMessage } from "acp-kernel"; +import { openSqliteDatabase } from "../src/storage/driver.ts"; +import type { LegacySessionLike } from "../src/storage/ingest.ts"; +import { UnifiedShadow } from "../src/storage/shadow.ts"; +import { UnifiedStore } from "../src/storage/store.ts"; + +/** + * Regression: the sampled read-back verifier must align the ENVELOPE leg to the + * ROW tail by SUFFIX, not by index 0. + * + * The two legs intentionally hold different slices of the same log: + * - the rows hold the raw tail interned at save time (`tailSeqs`, oldest-first, + * the whole resident window), and + * - the envelope holds `boundedFoldedSnapshot`, which prunes from the FRONT and + * keeps the NEWEST messages under `persist.tailTokens` (#2960 (5), caveat 2a). + * + * Measured on live sessions, envelope `payload.messages` is an EXACT element-wise + * suffix of `tail_json.tailSeqs` — session A tail=1146/envelope=24 and session B + * tail=832/envelope=38, with zero id mismatches and zero content mismatches inside + * the overlap, stable across a six-sample sweep. + * + * `compareMessages` used to walk both legs from index 0, so every row slot outside + * the envelope window produced `want === undefined` -> `changed(key)`. The sampled + * digest therefore never converged: the streak latched (#5677), the delta fast path + * was disabled for the session, and each save paid a FULL ingest. Measured on + * session B: 44 full ingests, avg 4522 ms, max 9363 ms, 170.8 s of stalled event + * loop in one episode. + * + * The fix compares only the overlap at its suffix offset. This test pins both + * halves of that contract: + * 1. a row tail LONGER than the envelope with a matching overlap verifies clean; + * 2. a content divergence INSIDE the overlap still reports drift. + * + * Sampling cadence matters here: `VERIFY_EVERY_INGESTS = 20`, so the second sampled + * save is ingest #21, not #2. + */ + +const root = fs.mkdtempSync(path.join(os.tmpdir(), "bili-tail-suffix-")); +process.env.BILI_CONFIG_FILE = path.join(root, "no-config.json"); +process.env.BILI_PERSIST_ZSTD = "0"; +after(() => { + try { + fs.rmSync(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 }); + } catch { + /* a leaked temp dir on a Windows runner is harmless */ + } +}); + +const TAIL_LEN = 40; +const ENVELOPE_LEN = 8; + +function message(i: number): CoreMessage { + return { id: `raw-${i}`, role: "user", contentType: "text", text: `message ${i}` }; +} + +function view(opts: { tail?: CoreMessage[]; folded?: boolean } = {}): LegacySessionLike { + const blocks: CompressionBlock[] = []; + const messages = Array.from({ length: TAIL_LEN }, (_, i) => message(i)); + return { + id: "suffix-session", + createdAt: 1_700_000_000_000, + lastSeen: 1_700_000_000_500, + meta: { protocol: "openai" }, + stats: { requests: 1 }, + metadata: {}, + state: { + blocks, + messageRefs: { byRaw: {}, byRef: {} }, + nextBlockId: 1, + nextRunId: 1, + }, + blockContents: new Map(), + lastMessages: opts.tail ?? messages, + lastMessagesFolded: opts.folded === true, + } as LegacySessionLike; +} + +function shadowOf(shadow: UnifiedShadow) { + return shadow as unknown as { + stats: { verifySamples: number; verifyOk: number; verifyDrift: number; lastError?: string | null }; + driftedSessions: Set; + frozenEnvelopeIds: Set; + }; +} + +/** + * Save a long tail (the row now claims TAIL_LEN slots), then keep saving folded + * envelopes while the session row still advertises the long `tailSeqs`. That is + * exactly the production shape: `tailSeqs` length > `payload.messages` length. + */ +function verifyFoldAfterLongTail(t: TestContext, opts: { corruptOverlap?: boolean } = {}) { + const dir = fs.mkdtempSync(path.join(root, "case-")); + const dbPath = path.join(dir, "index.db"); + const store = UnifiedStore.open(dbPath); + let legacy: LegacySessionLike = view(); + const shadow = Reflect.construct(UnifiedShadow, ["shadow", store, (record: unknown) => { + void record; + return legacy; + }, undefined]) as UnifiedShadow; + t.after(async () => { + await shadow.closeAsync(); + fs.rmSync(dir, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 }); + }); + const stats = shadowOf(shadow); + + shadow.record(legacy, { lastMessages: legacy.lastMessages ?? [], lastMessagesFolded: false }); + assert.equal(stats.stats.verifySamples, 1, "first ingest samples the read-back verify"); + assert.equal(stats.stats.verifyDrift, 0, `baseline save must be clean: ${stats.stats.lastError ?? ""}`); + + const longRow = store.getSessionRow(legacy.id); + assert.ok(longRow, "session row must exist after the long save"); + const longTail = JSON.parse(longRow.tailJson) as { tailSeqs: number[] }; + assert.equal(longTail.tailSeqs.length, TAIL_LEN, "row tail must intern the whole resident window"); + + // Keep the row's `tailSeqs` long while each envelope carries only the newest + // slice, and (for the corrupt variant) rewrite one overlap slot's stored bytes + // at the exact seam the verifier uses, i.e. AFTER the ingest transaction has + // committed. Rewriting `messages.content_hash` instead would break the content + // FK/refcount bookkeeping and abort the ingest before any sample runs. + const targetSeq = longTail.tailSeqs[longTail.tailSeqs.length - 1]!; + const donorSeq = longTail.tailSeqs[longTail.tailSeqs.length - ENVELOPE_LEN]!; + const getSessionRow = store.getSessionRow.bind(store); + let served = 0; + let corrupted = false; + t.mock.method(store, "getSessionRow", (id: string) => { + const row = getSessionRow(id) as { tailJson: string } | undefined; + if (!row) return row; + served++; + if (opts.corruptOverlap === true && !corrupted) { + corrupted = true; + const { db } = openSqliteDatabase(dbPath); + try { + const pick = db.prepare("SELECT content_hash FROM messages WHERE session_id = ? AND seq = ?"); + const target = pick.get(legacy.id, targetSeq) as { content_hash: string } | undefined; + const donor = pick.get(legacy.id, donorSeq) as { content_hash: string } | undefined; + assert.ok(target && donor, "both overlap slots must have rows"); + const donorBody = db.prepare("SELECT bytes, byte_len, algo FROM content WHERE hash = ?").get(donor.content_hash) as + | { bytes: unknown; byte_len: number; algo: string } + | undefined; + assert.ok(donorBody, "donor content row must exist"); + db.prepare("UPDATE content SET bytes = ?, byte_len = ?, algo = ? WHERE hash = ?").run( + donorBody.bytes, + donorBody.byte_len, + donorBody.algo, + target.content_hash, + ); + } finally { + db.close(); + } + } + const tail = JSON.parse(row.tailJson) as Record; + return { ...row, tailJson: JSON.stringify({ ...tail, tailSeqs: longTail.tailSeqs }) }; + }); + + const base = view(); + const suffix = (base.lastMessages ?? []).slice(-ENVELOPE_LEN); + for (let i = 0; i < 20; i++) { + legacy = view({ tail: suffix, folded: true }); + shadow.record(legacy, undefined); + } + + assert.ok(served > 0, "the verifier must read the session row (test seam actually engaged)"); + assert.equal(stats.stats.verifySamples, 2, "ingest #21 is the second sampled verify"); + return stats; +} + +test("folded envelope that is a suffix of the row tail must verify clean", (t) => { + const stats = verifyFoldAfterLongTail(t); + assert.equal( + stats.stats.verifyDrift, + 0, + `envelope is a suffix of the row tail: comparing from index 0 flags every slot outside the window (${stats.stats.lastError ?? ""})`, + ); + assert.equal(stats.driftedSessions.size, 0, "no session may latch as drift-latched"); + assert.equal(stats.frozenEnvelopeIds.size, 0, "envelope writes must not be frozen"); +}); + +test("content divergence inside the overlap still reports drift", (t) => { + const stats = verifyFoldAfterLongTail(t, { corruptOverlap: true }); + assert.equal(stats.stats.verifyDrift, 1, "a real content change inside the envelope window must still be caught"); + assert.equal(stats.driftedSessions.size, 1); +}); diff --git a/tests/issue5631-restart-survival.test.ts b/tests/issue5631-restart-survival.test.ts index 45504046f1..e3f768790d 100644 --- a/tests/issue5631-restart-survival.test.ts +++ b/tests/issue5631-restart-survival.test.ts @@ -175,6 +175,41 @@ class UpstreamRecorder { } } + /** The fake's token bill must sit on the SAME scale as a real tokenizer + * (the text the model reads + tool schemas, ÷4) — NOT on whole-body bytes: + * the wire envelope/JSON structure is ~2x the core text here, so bytes/4 + * billing taught the per-route calibration factor (#1933) a false k̂≈1.9, + * and the post-#4933 zero-fold fit verdict (calibrated scale) put the pi + * lane's post-restart baseline ~120 tokens OVER its 12288 wall — a false + * 413 on a payload a consistently-billing upstream would accept. + * Scale-matched billing keeps k̂≈1, so the calibrated and raw meters agree, + * as on real routes. The bill is the WHOLE prompt; the anthropic branch + * must then emit it as a DISJOINT partition (input_tokens = uncached + * remainder, cache_read = cached prefix) because promptInputTotal sums + * the two — reporting the full bill in BOTH fields double-counts the + * cached prefix (the pre-fix failure mode above). OpenAI's prompt_tokens + * is inclusive of cached_tokens, so that branch reports the full bill. */ + static billedTokens(body: string): number { + try { + const j = JSON.parse(body) as Record; + const text: unknown[] = []; + if (j.system !== undefined) text.push(j.system); + if (j.instructions !== undefined) text.push(j.instructions); + if (Array.isArray(j.messages)) text.push(j.messages); + let chars = 0; + const walk = (c: unknown): void => { + if (typeof c === "string") { chars += c.length; } + else if (Array.isArray(c)) { for (const x of c) walk(x); } + else if (c && typeof c === "object") { for (const v of Object.values(c)) walk(v); } + }; + for (const t of text) walk(t); + chars += JSON.stringify(j.tools ?? []).length; + return Math.max(1, Math.ceil(chars / 4)); + } catch { + return Math.max(1, Math.ceil(Buffer.byteLength(body, "utf8") / 4)); + } + } + get recPort(): number { return this.port; } @@ -232,7 +267,7 @@ class UpstreamRecorder { this.hits.push({ url: key, bytes, prompt, lcpPromptBytes, ack }); const modelMatch = body.match(/"model"\s*:\s*"([^"]+)"/); const model = modelMatch?.[1] ?? "unknown"; - const promptTokens = Math.ceil(bytes / 4); + const promptTokens = UpstreamRecorder.billedTokens(body); const cachedTokens = Math.floor(lcpPromptBytes / 4); const json = key.includes("/messages") ? JSON.stringify({ @@ -243,7 +278,7 @@ class UpstreamRecorder { content: [{ type: "text", text: ack }], stop_reason: "end_turn", stop_sequence: null, - usage: { input_tokens: promptTokens, output_tokens: 4, cache_read_input_tokens: cachedTokens }, + usage: { input_tokens: Math.max(0, promptTokens - cachedTokens), output_tokens: 4, cache_read_input_tokens: cachedTokens }, }) : JSON.stringify({ id: `chatcmpl_${seq}`, diff --git a/tests/issue857-image-baseline.test.ts b/tests/issue857-image-baseline.test.ts index 013712c69b..bcfec0714e 100644 --- a/tests/issue857-image-baseline.test.ts +++ b/tests/issue857-image-baseline.test.ts @@ -22,9 +22,8 @@ import { listSessions } from "../src/session.ts"; // // Fix under test (still load-bearing after #987 removed the window learner): // C1 preflight write-back persists text+overhead only (image floor excluded) -// — note: since the #1841 futility gate, a doomed image-over-window round -// is skipped BEFORE any fold, so C1's write-back (which only runs when a -// round folds ≥1 range) is no longer exercised by scenario C below. +// — image-bearing ranges may shrink the wire. Scenario C supplies no +// acceptable summary, so no fold write-back runs in that control. // C2 stats.lastInputTokensSource tags every baseline raise ("usage"|"estimate") // C4 #496 hatch: an untrusted (estimate-derived / legacy-unmarked) baseline // is not overflow evidence — only usage-grounded evidence closes it @@ -219,7 +218,7 @@ test("#857 B: genuine overflow evidence (learned limit == configured window) sti } }); -test("#857 C: usage-grounded over-window baseline — hatch stays closed AND the #1841 futility skip leaves the baseline untouched", async () => { +test("#857 C: unusable image-fold summaries leave the grounded baseline and forwarding guard intact", async () => { const { server: upstream, port: uport, stats } = await startMockUpstream(); // No route model entry: the window is a pure fallback, so the upward // self-heal (which requires nativeFromFallback) can confirm it. @@ -232,35 +231,28 @@ test("#857 C: usage-grounded over-window baseline — hatch stays closed AND the assert.equal(r1.status, 200); await r1.text(); - // A REAL usage report put the baseline above the window (armed, - // usage-grade evidence) — but the image floor exceeds even the window, - // so compression cannot make the payload fit. + // A usage report put the baseline above the window. Historical images + // can now leave the wire, so the reserve alone cannot prove futility. const s = sess("s857c"); s.stats.lastInputTokens = FAKE_CONFIRMED; s.stats.lastInputTokensSource = "usage"; - // Filler history gives the kernel foldable candidates. Under the #1841 - // futility gate this round is provably doomed BEFORE any walk — the - // image floor alone (~1.1M) exceeds the window (~999K) while the whole - // foldable pool (~4.4K of filler) cannot close the ~110K gap — so the - // round is skipped with zero summarization calls: exactly the waste - // #1841 removes (old code walked both ranges, burned the calls, and - // still failed here). + // The mock cannot supply an acceptable summary. The bounded image-fold + // attempts must therefore fail without forwarding the original wire or + // replacing the measured baseline with an image estimate. const r2 = await fetch(url, { method: "POST", headers, body: body(FALLBACK_MODEL, "s857c", [...fillerHistory(16), ...imageInput()]) }); - // #3461: over-window per bili's own meter with no foldable mass left → 413. - assert.equal(r2.status, 413, "usage evidence keeps the hatch closed"); + assert.equal(r2.status, 502, "unusable summaries keep the hatch closed"); const err2 = JSON.parse(await r2.text()) as { error?: { code?: string; message?: string } }; assert.equal(err2.error?.code, "preflight_compress_failed"); - assert.match(err2.error?.message ?? "", /futile round/, `the skipped round names its futility verdict (got: ${err2.error?.message})`); assert.match(err2.error?.message ?? "", /Images alone account for/, "the image remedy wording survives the futility verdict"); assert.equal(stats.streamingForwards, 1); - assert.equal(stats.summaryCalls, 0, "#1841: a provably futile round spends zero summarization calls"); + assert.ok(stats.summaryCalls > 0, "image-bearing candidates receive bounded summary attempts"); // No fold ran, so nothing in the turn may touch the baseline: the armed // usage value stays exactly where the test put it (far below the image // floor — nothing to re-poison) with its grounded provenance intact. const after = sess("s857c"); - assert.equal(after.stats.lastInputTokens, FAKE_CONFIRMED, "skipped round leaves the baseline untouched"); + assert.equal(after.stats.lastInputTokens, FAKE_CONFIRMED, "failed folds leave the baseline untouched"); assert.equal(after.stats.lastInputTokensSource, "usage", "grounded provenance preserved"); assert.equal(confirmedOf("s857c"), undefined, "#987: no window is ever learned from session traffic"); } finally { diff --git a/tests/media-split-digest-parity.test.ts b/tests/media-split-digest-parity.test.ts new file mode 100644 index 0000000000..9755d64f1e --- /dev/null +++ b/tests/media-split-digest-parity.test.ts @@ -0,0 +1,195 @@ +import { after, test, type TestContext } from "node:test"; +import assert from "node:assert/strict"; +import { createHash } from "node:crypto"; +import * as fs from "node:fs"; +import * as os from "node:os"; +import * as path from "node:path"; +import type { CoreMessage } from "acp-kernel"; +import { openSqliteDatabase } from "../src/storage/driver.ts"; +import type { LegacySessionLike } from "../src/storage/ingest.ts"; +import { UnifiedShadow } from "../src/storage/shadow.ts"; +import { UnifiedStore } from "../src/storage/store.ts"; + +/** + * #6351: the sampled digest compared the same message in two different media + * representations and latched drift forever. + * + * Rows are WRITTEN media-split (`planIngest` -> `splitMediaContent`, big + * payloads replaced by `{"$media":hash}` CAS references) but the verifier reads + * them back through `UnifiedStore.getMessages()`, which returns them + * media-HYDRATED. The envelope's `lastMessages` keeps the split form, so a + * positional digest compared a ~2 KB placeholder against a ~200 KB inlined + * payload and reported drift on every sample: the #5677 latch, then a full + * transform of the whole tail on every save (measured 8-9 s per save). + * + * The fix normalizes both legs to the split form before hashing + * (`splitMediaContent` is idempotent on already-split input, and + * `splitMediaContent(hydrate(x)) === x` on every media-bearing row sampled), so + * the same message hashes equal while a real edit still diverges. + */ + +const root = fs.mkdtempSync(path.join(os.tmpdir(), "bili-media-parity-")); +process.env.BILI_CONFIG_FILE = path.join(root, "no-config.json"); +process.env.BILI_PERSIST_ZSTD = "0"; +after(() => fs.rmSync(root, { recursive: true, force: true })); + +const MEDIA_PAYLOAD = "A".repeat(4096); + +function mediaMessage(i: number): CoreMessage { + return { + id: `media-${i}`, + role: "user", + contentType: "image", + imageBase64: MEDIA_PAYLOAD, + } as unknown as CoreMessage; +} + +function splitForm(message: CoreMessage): CoreMessage { + const mediaHash = createHash("sha256").update(MEDIA_PAYLOAD).digest("hex"); + return { ...message, imageBase64: { $media: mediaHash } } as unknown as CoreMessage; +} + +function view(messages: CoreMessage[], folded: boolean): LegacySessionLike { + return { + id: "media-session", + createdAt: 1_700_000_000_000, + lastSeen: 1_700_000_000_000, + meta: { protocol: "openai" }, + stats: { requests: 1 }, + metadata: {}, + state: { + blocks: [], + messageRefs: { byRaw: {}, byRef: {} }, + nextBlockId: 1, + nextRunId: 1, + }, + blockContents: new Map(), + lastMessages: messages, + lastMessagesFolded: folded, + } as LegacySessionLike; +} + +/** Envelope = the newest N messages in SPLIT form; rows stay hydrated. */ +function verifyFold(t: TestContext): void { + const dir = fs.mkdtempSync(path.join(root, "case-")); + const dbPath = path.join(dir, "index.db"); + const store = UnifiedStore.open(dbPath); + let legacy = view([], false); + const shadow = Reflect.construct( + UnifiedShadow, + ["shadow", store, () => legacy, undefined], + ) as UnifiedShadow; + t.after(async () => { + await shadow.closeAsync(); + fs.rmSync(dir, { recursive: true, force: true }); + }); + + const all = Array.from({ length: 6 }, (_, i) => mediaMessage(i)); + // Baseline: full raw tail, so the first sampled verification compares equal. + legacy = view(all, false); + shadow.record({ id: legacy.id }); + + assert.equal(shadow.stats.verifySamples, 1, "the baseline save must be verified"); + assert.equal(shadow.stats.verifyDrift, 0, "the baseline save must not drift"); + + // The bytes on disk must actually be split; otherwise this fixture proves + // nothing about media hydration. + const { db } = openSqliteDatabase(dbPath); + let stored: string; + try { + const row = db + .prepare( + `SELECT c.bytes FROM messages m JOIN content c ON c.hash = m.content_hash + WHERE m.session_id = ? ORDER BY m.seq ASC LIMIT 1`, + ) + .get(legacy.id) as { bytes: unknown } | undefined; + assert.ok(row, "the ingested row must exist"); + stored = Buffer.isBuffer(row!.bytes) + ? row!.bytes.toString("utf8") + : new TextDecoder().decode(row!.bytes as Uint8Array); + } finally { + db.close(); + } + assert.ok(stored.includes("$media"), `stored bytes must be media-split, got: ${stored.slice(0, 120)}`); + + // Now the envelope carries only the newest two messages, in SPLIT form — + // exactly what `boundedFoldedSnapshot` leaves behind for a large session. + legacy = view(all.slice(-2).map(splitForm), true); + for (let i = 0; i < 20; i += 1) { + shadow.record({ id: legacy.id }); + + } + + assert.equal(shadow.stats.verifySamples, 2, "the second sample must have run (VERIFY_EVERY_INGESTS)"); + assert.equal( + shadow.stats.verifyDrift, + 0, + "a split envelope slot must hash equal to its hydrated row", + ); + assert.equal(shadow.driftedSessions.size, 0, "no session may stay latched"); + assert.equal(shadow.frozenEnvelopeIds.size, 0, "the envelope must not be frozen"); +} + +test("split envelope slot must verify clean against its hydrated row", (t) => { + verifyFold(t); +}); + +test("a real media change inside the envelope window still reports drift", (t) => { + const dir = fs.mkdtempSync(path.join(root, "case-edit-")); + const dbPath = path.join(dir, "index.db"); + const store = UnifiedStore.open(dbPath); + let legacy = view([], false); + const shadow = Reflect.construct( + UnifiedShadow, + ["shadow", store, () => legacy, undefined], + ) as UnifiedShadow; + t.after(async () => { + await shadow.closeAsync(); + fs.rmSync(dir, { recursive: true, force: true }); + }); + + const all = Array.from({ length: 6 }, (_, i) => mediaMessage(i)); + legacy = view(all.slice(-2).map(splitForm), true); + // Corrupt the LAST tail row's stored bytes so it can no longer match the + // envelope: the row is hydrated from CAS, so rewriting the content row is + // what a real divergence looks like on the row leg. + const getSessionRow = store.getSessionRow.bind(store); + let corrupted = false; + t.mock.method(store, "getSessionRow", (id: string) => { + if (!corrupted) { + corrupted = true; + const { db } = openSqliteDatabase(dbPath); + try { + const last = db + .prepare( + `SELECT m.content_hash AS hash FROM messages m + WHERE m.session_id = ? ORDER BY m.seq DESC LIMIT 1`, + ) + .get(legacy.id) as { hash: string } | undefined; + if (last) { + db.prepare(`UPDATE content SET bytes = ?, byte_len = ? WHERE hash = ?`).run( + JSON.stringify({ + id: "media-9", + role: "user", + contentType: "image", + imageBase64: "B".repeat(4096), + }), + 4096, + last.hash, + ); + } + } finally { + db.close(); + } + } + return getSessionRow(id); + }); + + for (let i = 0; i < 21; i += 1) { + shadow.record({ id: legacy.id }); + + } + + assert.ok(shadow.stats.verifyDrift >= 1, "a genuine content divergence must still be reported"); + assert.equal(shadow.driftedSessions.size, 1, "the session must be latched for a genuine divergence"); +}); diff --git a/tests/preflight-image-budget.test.ts b/tests/preflight-image-budget.test.ts new file mode 100644 index 0000000000..5ea0323abc --- /dev/null +++ b/tests/preflight-image-budget.test.ts @@ -0,0 +1,128 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import { randomBytes } from "node:crypto"; +import { deflateSync } from "node:zlib"; +import { readFileSync } from "node:fs"; +import { createCore, defaultConfig } from "acp-kernel"; +import { startServer, type ProxyOptions } from "../src/server.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { listSessions, _resetSessionsForTest } from "../src/session.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { resolveDecompress } from "../src/decompress-shared.ts"; + +// An actual PNG: deterministic dimensions, incompressible pixels, and valid CRCs. +function png(): Buffer { + const chunk = (type: string, data: Buffer): Buffer => { + const body = Buffer.concat([Buffer.from(type), data]); + let crc = 0xffffffff; + for (const byte of body) { + crc ^= byte; + for (let bit = 0; bit < 8; bit++) crc = (crc >>> 1) ^ ((crc & 1) ? 0xedb88320 : 0); + } + const header = Buffer.alloc(4), tail = Buffer.alloc(4); + header.writeUInt32BE(data.length); tail.writeUInt32BE((crc ^ 0xffffffff) >>> 0); + return Buffer.concat([header, body, tail]); + }; + const header = Buffer.alloc(13); + header.writeUInt32BE(256, 0); header.writeUInt32BE(256, 4); header[8] = 8; header[9] = 2; + const rows = Array.from({ length: 256 }, () => Buffer.concat([Buffer.from([0]), randomBytes(256 * 3)])); + return Buffer.concat([Buffer.from("89504e470d0a1a0a", "hex"), chunk("IHDR", header), chunk("IDAT", deflateSync(Buffer.concat(rows))), chunk("IEND", Buffer.alloc(0))]); +} + +for (const mode of ["all-images", "remaining-images", "upstream-overflow"] as const) { +test(`preflight image budget: ${mode}`, async () => { + _setStoreForTest(new SessionStore({ enabled: false })); + _resetSessionsForTest(); + setRegistryForTest({}); + const pixels = png(), base64 = pixels.toString("base64"); + const partial = mode !== "all-images"; + const limit = partial ? 8_000 : 12_000; + const liveImage = partial ? `data:image/png;base64,${png().toString("base64")}` : undefined; + const forwards: string[] = [], summaries: string[] = []; + const summary = "The historical screenshot was inspected. Keep its image retrieval reference, the accepted decisions, and the current task constraints when continuing the work."; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (chunk: Buffer) => chunks.push(chunk)); + req.on("end", () => { + const raw = Buffer.concat(chunks).toString("utf8"); + if (JSON.parse(raw).stream === false) { + summaries.push(raw); + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ output_text: summary })); + } else { + forwards.push(raw); + res.writeHead(200, { "content-type": "text/event-stream" }); + res.end(`event: response.completed\ndata: ${JSON.stringify({ response: { id: "done", status: "completed", output: [], usage: { input_tokens: 1000, output_tokens: 5, total_tokens: 1005 } } })}\n\n`); + } + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const address = upstream.address(); assert.ok(address && typeof address !== "string"); + const origin = `http://127.0.0.1:${address.port}`; + const config = defaultConfig(limit); + config.preserveRecentMessages = 2; config.preserveRecentTokens = 500; + const opts: ProxyOptions = { + port: 0, host: "127.0.0.1", upstream: origin, + routes: { [origin]: { models: { test: { context: limit } } } }, + modelContextLimit: limit, kernelConfig: config, imageBilling: partial ? "pixels" : "bytes", + compress: { injectTool: true, injectNudge: true }, promptCache: { routing: "auto" }, sessionHeader: "x-acp-session", + log: false, debug: false, passthrough: false, passthroughSource: null, autoUpdate: false, autoRestartOnUpdate: false, + updateTag: "latest", advisoryCheck: false, releaseNotesCheck: false, compat: { roles: {} }, streamErrorShape: "protocol", mitm: { enabled: false, domains: [] }, + }; + const proxy = await startServer(opts); + await once(proxy, "listening"); + const proxyAddress = proxy.address(); assert.ok(proxyAddress && typeof proxyAddress !== "string"); + try { + const url = `http://127.0.0.1:${proxyAddress.port}/bili/${origin}/responses`; + const seed = await fetch(url, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ model: "test", stream: true, session_id: "image-budget", input: [{ type: "message", role: "user", content: "Begin the screenshot investigation." }] }) }); + assert.equal(seed.status, 200); await seed.text(); + const seeded = listSessions().find(s => s.meta.label === "image-budget"); assert.ok(seeded); + seeded.stats.lastInputTokens = 70_000; + seeded.stats.lastInputTokensSource = mode === "upstream-overflow" ? "overflow-arm" : "usage"; + seeded.stats.lastInputTokensOrigin = origin; + if (partial) seeded.metadata.learnedImageCosts = { "127.0.0.1": { cost: 20_000, seen: 1, ts: Date.now(), fp: "pixels:0" } }; + forwards.length = 0; + const input: unknown[] = [ + { type: "message", role: "user", content: "Begin the screenshot investigation." }, + { type: "message", role: "assistant", content: "The historical evidence and decisions follow." }, + { type: "message", role: "user", content: [{ type: "input_text", text: "Inspect this historical screenshot. " + "evidence details ".repeat(180) }, { type: "input_image", image_url: `data:image/png;base64,${base64}`, detail: "high" }] }, + ]; + for (let i = 0; i < 12; i++) input.push({ type: "message", role: i % 2 ? "user" : "assistant", content: `Historical turn ${i}. ` + "preserve decisions and constraints ".repeat(40) }); + input.push({ type: "message", role: "user", content: partial ? [ + { type: "input_text", text: "Continue using these current screenshots and the accepted findings." }, + { type: "input_image", image_url: liveImage, detail: "high" }, + { type: "input_image", image_url: liveImage, detail: "high" }, + ] : "Continue using the accepted findings." }); + const response = await fetch(url, { + method: "POST", headers: { "content-type": "application/json" }, + body: JSON.stringify({ model: "test", stream: true, session_id: "image-budget", input }), + }); + const body = await response.text(); + assert.ok(summaries.length > 0, "folding must not be declared futile from text savings alone"); + if (mode === "upstream-overflow") { + assert.equal(response.status, 502, body); + assert.equal(forwards.length, 0, "confirmed upstream overflow must not be bypassed by image arbitration"); + return; + } + assert.equal(response.status, 200, body); + assert.equal(forwards.length, 1); + assert.ok(!forwards[0]!.includes(base64), "the folded image must leave the actual outbound wire"); + assert.ok(forwards[0]!.includes(summary)); + assert.equal((forwards[0]!.match(/"input_image"/g) ?? []).length, partial ? 2 : 0, "only the protected live images remain"); + const ref = summaries.join("\n").match(/\[image: png 256x256 · (m\d+)\]/)?.[1]; + assert.ok(ref, "summary input retains a resolvable image reference"); + const session = listSessions().find(s => s.meta.label === "image-budget"); assert.ok(session); + const restored = resolveDecompress({ imageRef: ref }, { core: createCore(), config, messages: [], session, log: () => {} }); + assert.equal(restored.outcome, "success", restored.text); + const file = restored.text.match(/\n (.+\.png)/)?.[1]; assert.ok(file, restored.text); + assert.deepEqual(readFileSync(file), pixels, "retrieval restores the original pixels exactly"); + } finally { + const proxyClosed = once(proxy, "close"), upstreamClosed = once(upstream, "close"); + proxy.close(); proxy.closeAllConnections(); upstream.close(); upstream.closeAllConnections(); + await Promise.all([proxyClosed, upstreamClosed]); + } +}); +} diff --git a/tests/preflight-zero-fold-calibration.test.ts b/tests/preflight-zero-fold-calibration.test.ts new file mode 100644 index 0000000000..7837ff72ba --- /dev/null +++ b/tests/preflight-zero-fold-calibration.test.ts @@ -0,0 +1,189 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import net from "node:net"; +import { once } from "node:events"; +import { randomUUID } from "node:crypto"; +import { afterEach, test } from "node:test"; +import { createCore, defaultConfig, type CoreMessage } from "acp-kernel"; +import type { ProxyOptions } from "../src/config.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { getSession } from "../src/session.ts"; +import { outboundPayloadBreakdown, preflightCompressIfNeeded, type Prepared } from "../src/server.ts"; +import { _liveUpstreamTimersForTest, _resetFetchUtilForTest } from "../src/fetch-util.ts"; + +process.env.NODE_ENV = "test"; +_setStoreForTest(new SessionStore({ enabled: false })); + +afterEach(() => { + assert.equal(_liveUpstreamTimersForTest(), 0); + _resetFetchUtilForTest(); +}); + +type Scenario = { + k?: number; + windowRatio: number; + otherOrigin?: boolean; + otherModel?: boolean; + imageBytes?: number; + anonymous?: boolean; + plugin?: boolean; + codex?: boolean; + emptyProcessed?: boolean; + localRefusal?: boolean; +}; + +// Exercise the real preflight and caller. The only HTTP peer is this local +// summary mock; an intentionally growing summary must never become a fold. +async function run(s: Scenario) { + let summaryCalls = 0; + const summary = "An unusably growing summary. ".repeat(6000); + const upstream = http.createServer((req, res) => { + req.resume(); + req.on("end", () => { + summaryCalls++; + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ output: [{ type: "message", role: "assistant", content: [{ type: "output_text", text: summary }] }] })); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const address = upstream.address(); + assert.ok(address && typeof address === "object"); + const origin = `http://127.0.0.1:${address.port}`; + const url = `${origin}/responses`; + const session = getSession(`zero-fold-calibration-${randomUUID()}`); + const core = createCore(); + const config = defaultConfig(10_000, { preserveRecentMessages: 0, preserveRecentTokens: 0 }); + const messages: CoreMessage[] = [ + { id: "first", role: "user", contentType: "text", text: "Keep the goal and acceptance criteria." }, + { id: "output", role: "assistant", contentType: "text", text: "FILLER_".repeat(4000) }, + { id: "last", role: "user", contentType: "text", text: "Continue." }, + ]; + const input: unknown[] = messages.map(m => ({ role: m.role, content: m.text })); + if (s.imageBytes) input.push({ role: "user", content: [{ type: "input_image", image_url: `data:image/png;base64,${"A".repeat(s.imageBytes)}` }] }); + const prepared: Prepared = { + body: JSON.stringify({ model: "test-model", stream: true, input }), + session, processedMessages: s.emptyProcessed ? [] : messages, + originalMessages: messages, protocol: "responses", stream: true, + compressInjected: false, pluginMode: s.plugin, + }; + const opts: ProxyOptions = { + host: "127.0.0.1", port: 0, upstream: url, routes: {}, + proxy: "", proxyMode: "direct", proxySource: "direct", + modelContextLimit: config.modelContextLimit, kernelConfig: config, + compress: { injectTool: false, injectNudge: false }, + promptCache: { routing: "auto" }, compat: { roles: {} }, + streamErrorShape: "protocol", sessionHeader: "x-acp-session", + log: false, debug: false, passthrough: false, passthroughSource: null, + autoUpdate: false, autoRestartOnUpdate: false, updateTag: "latest", + advisoryCheck: false, releaseNotesCheck: false, mitm: { enabled: false, domains: [] }, + }; + const route = { upstream: url, rewrittenUrl: url }; + const parts = outboundPayloadBreakdown(prepared, opts, route, "/responses"); + const rawText = parts.textEstimate + parts.overheadEstimate; + const limit = Math.ceil(rawText * s.windowRatio); + config.modelContextLimit = limit; + opts.modelContextLimit = limit; + session.stats.lastInputTokens = s.anonymous ? 0 : Math.ceil(limit * (s.codex ? 0.2 : 1.05)); + session.stats.lastInputTokensSource = "usage"; + session.stats.lastInputTokensOrigin = origin; + session.stats.calibratedEstimate = s.k; + session.stats.calibratedEstimateOrigin = s.otherOrigin ? "http://other.invalid" : origin; + session.stats.calibratedEstimateModel = s.otherModel ? "another-model" : "test-model"; + session.metadata.lastModel = "test-model"; + if (s.localRefusal) session.metadata.localRefusals = [{ origin, model: "test-model", window: limit, at: Date.now() }]; + // Keep the normal no-ranges shortcut out of the way: this is the caller's + // post-walk zero-fold gate, not its earlier fitting/no-candidates escape. + const turn = core.processTurn({ messages, state: session.state, config, + tokenCount: Math.ceil(limit * 1.05), renderTags: "text-only" }); + prepared.nudge = turn.nudge ?? undefined; + assert.ok((prepared.nudge?.compressibleRanges.length ?? 0) > 0, "fixture needs an advertised candidate"); + const req = new http.IncomingMessage(new net.Socket()); + req.method = "POST"; + req.url = "/responses"; + req.headers = { "content-type": "application/json", ...(s.codex ? { "user-agent": "codex_cli_rs/0.163.0" } : {}) }; + const res = new http.ServerResponse(req); + const logs: string[] = []; + let rebuilds = 0; + try { + const result = await preflightCompressIfNeeded(prepared, async () => { + rebuilds++; + return prepared; + }, req, Buffer.from(prepared.body), res, opts, core, config, limit, + "test-model", undefined, undefined, route, undefined, s.anonymous ?? false, + (_level, msg) => logs.push(msg), "zero-fold-test"); + assert.equal(rebuilds, 0, "a rejected or unnecessary summary cannot rebuild the payload"); + assert.equal(session.state.blocks.length, 0, "no growing summary may land"); + assert.ok(logs.some(line => line.includes("preflight compressing before forward")), "must exercise preflight"); + return { result, prepared, logs, summaryCalls, rawText, imageTokens: parts.imageTokens, limit }; + } finally { + const closed = once(upstream, "close"); + upstream.close(); + upstream.closeIdleConnections?.(); + upstream.closeAllConnections(); + await closed; + req.destroy(); + res.destroy(); + } +} + +for (const plugin of [false, true]) { + test(`zero folds: fitting calibrated payload forwards (plugin=${plugin})`, { timeout: 15_000 }, async () => { + const f = await run({ k: 0.25, windowRatio: 0.6, plugin }); + assert.ok(f.rawText > f.limit && f.rawText * 0.25 < f.limit); + assert.ok(f.result === f.prepared, JSON.stringify(f.logs)); + }); + test(`zero folds: inflated calibrated payload remains blocked (plugin=${plugin})`, { timeout: 15_000 }, async () => { + const f = await run({ k: 2, windowRatio: 1.5, plugin }); + assert.ok(f.rawText < f.limit && f.rawText * 2 > f.limit); + assert.ok("failFast" in f.result, JSON.stringify(f.logs)); + assert.ok(f.summaryCalls > 0, "must reject a growing summary, not bypass the walk"); + }); +} + +test("Codex health target: a non-shrinking summary does not block a fitting payload", { timeout: 15_000 }, async () => { + const f = await run({ k: 0.25, windowRatio: 0.25 / 0.95, codex: true }); + assert.ok(f.rawText * 0.25 >= f.limit * 0.9 && f.rawText * 0.25 < f.limit); + assert.ok(f.summaryCalls > 0); + assert.ok(f.result === f.prepared, JSON.stringify(f.logs)); + assert.ok(f.logs.some(line => /summary.*(?:grow|larger|not shorter)|no.?shrink/i.test(line))); +}); + +for (const scenario of [ + { name: "no calibration", k: undefined }, + { name: "different upstream", k: 0.25, otherOrigin: true }, + { name: "different model", k: 0.25, otherModel: true }, + { name: "invalid calibration", k: Number.NaN }, + { name: "raw-body fallback after transform failure", emptyProcessed: true }, +]) { + test(`zero folds: ${scenario.name} does not gain a false fit`, { timeout: 15_000 }, async () => { + const f = await run({ windowRatio: 0.6, ...scenario }); + assert.ok("failFast" in f.result, JSON.stringify(f.logs)); + }); +} + +// #5000/#4543 (2e021109, after this PR's base): the measured-body gate now +// dominates inside the arbitration arm — a measured outbound body over the +// window is doomed locally for EVERY baseline regime (the original +// size-arbitration forwarding only remains for measured-fitting bodies, which +// this fixture's est≈measured regime cannot produce). Pin the current policy. +test("zero folds: anonymous unknown baseline — measured over-window body is doomed locally", { timeout: 15_000 }, async () => { + const f = await run({ windowRatio: 0.6, anonymous: true }); + assert.ok("failFast" in f.result, JSON.stringify(f.logs)); + assert.equal(f.result.status, 413); + assert.ok(f.logs.some(line => line.includes("body ITSELF exceeds the window"))); +}); + +test("zero folds: a prior scoped refusal keeps the anonymous body locally doomed", { timeout: 15_000 }, async () => { + const f = await run({ windowRatio: 0.6, anonymous: true, localRefusal: true }); + assert.ok("failFast" in f.result, JSON.stringify(f.logs)); + assert.equal(f.result.status, 413); + assert.ok(f.logs.some(line => line.includes("body ITSELF exceeds the window"))); +}); + +test("zero folds: image reserve is not deflated by text calibration", { timeout: 15_000 }, async () => { + const f = await run({ k: 0.25, windowRatio: 0.6, imageBytes: 60_000 }); + assert.ok(f.imageTokens > 0); + assert.ok(f.rawText * 0.25 < f.limit && f.rawText * 0.25 + f.imageTokens > f.limit); + assert.ok("failFast" in f.result, JSON.stringify(f.logs)); +}); diff --git a/tests/responses-block-wire.test.ts b/tests/responses-block-wire.test.ts new file mode 100644 index 0000000000..57797e70fb --- /dev/null +++ b/tests/responses-block-wire.test.ts @@ -0,0 +1,77 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { createCore, createInitialState, defaultConfig } from "acp-kernel"; +import type { ResponseInputItem } from "acp-kernel/wire"; +import { responsesToCoreWithToolImages as project, patchResponsesInputWithToolImages as patch, coreToResponsesWithToolImages as encode } from "../src/responses-tool-output.ts"; + +function images(input: unknown): unknown[] { + if (Array.isArray(input)) return input.flatMap(images); + if (!input || typeof input !== "object") return []; + const item = input as Record; + return item.type === "input_image" ? [item] : Object.values(item).flatMap(images); +} + +for (const n of [1, 2, 12, 17, 26]) { + test(`host projection preserves ${n} images when current core IDs differ from layout IDs`, () => { + const input: ResponseInputItem[] = [{ type: "message", role: "user", content: Array.from({ length: n }, (_, i) => [ + { type: "input_text", text: `caption ${i}`, annotations: [`source-${i}`] }, + { type: "input_image", image_url: `https://example.invalid/${i}.png`, detail: i % 2 ? "low" : "high" }, + ]).flat() }]; + const projection = project({ input }); + assert.deepEqual(patch(projection, projection.msgs), input); + assert.deepEqual(encode(projection.msgs), input); + const remapped = projection.msgs.map(message => ({ ...message, id: `fresh_${message.id}` })); + assert.deepEqual(images(patch(projection, remapped)), images(input), "each surviving part must be emitted exactly once"); + }); +} + +test("partial block view keeps B, its detail and caption metadata, without resurrecting A", () => { + const a = { type: "input_image", image_url: "https://example.invalid/A.png", detail: "low" }; + const b = { type: "input_image", image_url: "https://example.invalid/B.png", detail: "high" }; + const caption = { type: "input_text", text: "retained B caption", annotations: ["keep"] }; + const projection = project({ input: [{ type: "message", role: "user", content: [a, caption, b] }] }); + const partial = projection.msgs.slice(1); + assert.deepEqual(images(patch(projection, partial)), [b]); + assert.deepEqual((patch(projection, partial) as ResponseInputItem[])[0]!.content, [caption, b]); + const between = { id: "inserted-summary", role: "user" as const, contentType: "text" as const, text: "Retained block summary at its original position." }; + const separated = patch(projection, [projection.msgs[0]!, between, projection.msgs[2]!]) as ResponseInputItem[]; + assert.deepEqual(images(separated[0]), [a]); + assert.ok(JSON.stringify(separated[1]).includes(between.text), "the summary stays between retained source parts"); + assert.deepEqual(images(separated[2]), [b]); +}); + +test("a real block replaces its originals on resends and preserves genuinely new identical instances once", () => { + const image = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="; + const a = { type: "input_image", image_url: image, detail: "low" }; + const b = { type: "input_image", image_url: image, detail: "high" }; + const input: ResponseInputItem[] = [ + { type: "message", role: "user", content: "independent opening" }, + { type: "message", role: "assistant", content: "ready" }, + { type: "message", role: "user", content: [{ type: "input_text", text: "Historical A facts and constraints. ".repeat(200) }, a, + { type: "input_text", text: "Retained B facts." }, b] }, + { type: "message", role: "assistant", content: "Continue from the retained decisions." }, + ]; + const projection = project({ input }); + const core = createCore(); + const config = { ...defaultConfig(100_000), preserveRecentMessages: 0, preserveRecentTokens: 0 }; + const initial = core.processTurn({ messages: projection.msgs, state: createInitialState(), config, tokenCount: 3000, renderTags: "none" }); + const summary = "A facts established the archived screenshot's constraints; retain those decisions while continuing from screenshot B."; + const folded = core.applyCompression({ messages: projection.msgs, state: initial.state, config, ranges: [{ + startRef: initial.state.messageRefs.byRaw[projection.msgs[2]!.id]!, endRef: initial.state.messageRefs.byRaw[projection.msgs[3]!.id]!, summary, + }] }); + assert.equal(folded.result.blocksCreated, 1, JSON.stringify(folded.result)); + for (const prior of [initial.state.lastPassIds ?? [], []]) { + const turn = core.processTurn({ messages: projection.msgs, state: { ...structuredClone(folded.state), lastPassIds: prior }, config, tokenCount: 3000, renderTags: "none" }); + const wire = patch(projection, turn.messages); + if (prior.length > 0) { + assert.ok(turn.state.blocks.some(block => block.active), "the compression block remains active"); + assert.ok(JSON.stringify(wire).includes(summary), "the block summary replaces the original facts"); + assert.deepEqual(images(wire), [b], "archived A must stay out while B remains exactly once"); + } else { + // The kernel deliberately treats absent-prior content as a new + // identical instance (#1476), rather than hiding new work under an + // old fold. The host must emit that current view without n² replay. + assert.deepEqual(images(wire), [a, b]); + } + } +}); diff --git a/tests/responses-tool-images.test.ts b/tests/responses-tool-images.test.ts index 819072a5ba..39a775f530 100644 --- a/tests/responses-tool-images.test.ts +++ b/tests/responses-tool-images.test.ts @@ -2,7 +2,10 @@ import assert from "node:assert/strict"; import http from "node:http"; import { once } from "node:events"; import test from "node:test"; -import { defaultConfig } from "acp-kernel"; +import { createCore, defaultConfig } from "acp-kernel"; +import { mkdtempSync, readFileSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; import { responsesToCore as kernelResponsesToCore, type ResponseInputItem } from "acp-kernel/wire"; import { responsesToCoreWithToolImages as responsesToCore, patchResponsesInputWithToolImages as patchResponsesInput, coreToResponsesWithToolImages as coreToResponses } from "../src/responses-tool-output.ts"; import { imageTokensInParsedBody } from "../src/image-tokens.ts"; @@ -11,6 +14,8 @@ import { createResponsesAdapter } from "../src/loop/adapter-responses.ts"; import { startServer } from "../src/server.ts"; import type { ProxyOptions } from "../src/config.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { getSession, listSessions, _resetSessionsForTest } from "../src/session.ts"; +import { resolveDecompress } from "../src/decompress-shared.ts"; import { _setForTest as setRegistryForTest } from "../src/registry.ts"; process.env.NODE_ENV = "test"; @@ -28,10 +33,105 @@ function pngUrl(width: number, height: number, fill = 0): string { const imageUrl = pngUrl(2048, 1152); const parts = [{ type: "input_text", text: "The screenshot shows a failure." }, { type: "input_image", image_url: imageUrl, detail: "high" }]; +function userImageBody(n: number): { input: ResponseInputItem[] } { + return { input: [{ type: "message", role: "user", content: Array.from({ length: n }, (_, i) => [ + { type: "input_text", text: `caption ${i}`, annotations: [`keep-${i}`] }, + { type: "input_image", image_url: `https://example.test/image-${i}.png`, detail: i % 2 ? "low" : "high" }, + ]).flat() }] }; +} + +test("full encoding binds each multipart image to its source, including partial and duplicate retention", () => { + const first = { type: "message", role: "user", content: [ + { type: "input_text", text: "first source", custom: "keep-text-metadata" }, + { type: "input_image", image_url: imageUrl, detail: "low" }, + { type: "input_image", image_url: imageUrl, detail: "high" }, + ] }; + const second = { type: "message", role: "user", content: [ + { type: "input_text", text: "second source" }, + { type: "input_image", image_url: pngUrl(128, 256), detail: "auto" }, + ] }; + const projection = responsesToCore({ input: [first, second] }); + assert.deepEqual(coreToResponses(projection.msgs), [first, second], "complete carriers round-trip once each"); + const partial = projection.msgs.filter((_, index) => index !== 1); + const rebuilt = coreToResponses(partial); + assert.deepEqual(rebuilt[0]!.content, [first.content[0], first.content[2]], "retained duplicate keeps its own detail rather than taking the first image"); + assert.deepEqual(rebuilt[1], second, "a neighboring source keeps its own image"); + const summary = { id: "summary-carrier", role: "system" as const, contentType: "text" as const, text: "A new summary between retained image parts." }; + const split = coreToResponses([projection.msgs[1]!, summary, projection.msgs[4]!]); + assert.deepEqual(split[0]!.content, [first.content[1]]); + assert.equal(split[1]!.role, "developer", "ordinary system item stays between the image sources"); + assert.deepEqual(split[2]!.content, [second.content[1]]); +}); + function toolBody(type: string, output: unknown = parts): { input: ResponseInputItem[] } { return { input: [{ type, call_id: "screenshot-call", output }] }; } +test("#4767: full encoding preserves one multi-image carrier instead of repeating it per core", () => { + for (const n of [26, 2, 1]) { + const body = userImageBody(n); + const projection = responsesToCore(body); + assert.deepEqual(coreToResponses(projection.msgs, projection.customToolCallIds), body.input); + for (const textProtocol of [false, true]) { + const request = createResponsesAdapter(textProtocol).buildRequest(projection.msgs, "", body); + assert.deepEqual((request.input as ResponseInputItem[]).filter((item) => item.role === "user"), body.input); + } + } +}); + +test("#4767: ordinary multipart runs stay intact on both sides of image tool outputs", () => { + for (const type of ["function_call_output", "custom_tool_call_output"]) { + const before = userImageBody(2).input; + const tool = { type, call_id: "image-call", output: parts }; + const after = userImageBody(3).input; + const body = { input: [...before, tool, ...after] }; + const projection = responsesToCore(body); + const edited = projection.msgs.map((m) => m.role === "tool" ? { ...m, text: "changed" } : m); + assert.deepEqual(coreToResponses(edited, projection.customToolCallIds), [ + ...before, { ...tool, output: [{ ...parts[0], text: "changed" }, parts[1]] }, ...after, + ]); + } +}); + +test("#4767: full encoding preserves repeated identical image instances", () => { + const body = userImageBody(2); + const first = body.input[0]; + assert.ok(Array.isArray(first.content)); + first.content[3] = { ...first.content[1] }; + const projection = responsesToCore(body); + assert.deepEqual(coreToResponses(projection.msgs, projection.customToolCallIds), body.input); +}); + +test("#4767: batching adjacent source carriers must not lose the later carrier's images", () => { + const later = userImageBody(3).input; + const body = { input: [...userImageBody(2).input, ...later] }; + const projection = responsesToCore(body); + const rebuilt = coreToResponses(projection.msgs, projection.customToolCallIds); + assert.deepEqual(rebuilt, body.input, "each adjacent source keeps its own parts without duplication"); +}); + +test("#4767: full encoding preserves empty history and surrounding plain history", () => { + assert.deepEqual(coreToResponses([]), []); + const body = { input: [ + { type: "message", role: "assistant", content: "plain before" }, + ...userImageBody(2).input, + { type: "message", role: "assistant", content: "plain after" }, + ] }; + const projection = responsesToCore(body); + assert.deepEqual(coreToResponses(projection.msgs, projection.customToolCallIds), body.input); +}); + +test("#4767: summary carriers stay separate from a neighboring multipart source", () => { + const body = userImageBody(2); + const projection = responsesToCore(body); + const summary = { id: "acp_summary_b1", role: "system" as const, contentType: "text" as const, text: "folded history" }; + const summaryInput = [{ type: "message", role: "user", content: "folded history" }]; + assert.deepEqual(coreToResponses([summary, ...projection.msgs, summary], projection.customToolCallIds), [ + ...summaryInput, ...body.input, ...summaryInput, + ]); +}); + + test("Responses tool output images are charged by pixels or bytes, not ignored", () => { for (const type of ["function_call_output", "custom_tool_call_output"]) { const body = toolBody(type); @@ -109,7 +209,7 @@ test("ordinary JSON tool outputs retain their existing projection and are not ch type RequestBody = { stream?: boolean; input?: ResponseInputItem[] }; -async function testProxy(window: number, run: (url: string, forwarded: RequestBody[], summaries: RequestBody[]) => Promise): Promise { +async function testProxy(window: number, run: (url: string, forwarded: RequestBody[], summaries: RequestBody[]) => Promise, store = new SessionStore({ enabled: false })): Promise { const forwarded: RequestBody[] = []; const summaries: RequestBody[] = []; const upstream = http.createServer((req, res) => { @@ -131,7 +231,7 @@ async function testProxy(window: number, run: (url: string, forwarded: RequestBo upstream.listen(0, "127.0.0.1"); await once(upstream, "listening"); const upstreamPort = (upstream.address() as { port: number }).port; - _setStoreForTest(new SessionStore({ enabled: false })); + _setStoreForTest(store); setRegistryForTest({}); const proxy = await startServer({ port: 0, host: "127.0.0.1", upstream: "http://127.0.0.1", @@ -156,6 +256,82 @@ async function testProxy(window: number, run: (url: string, forwarded: RequestBo } } +test("a persisted partial compression block keeps its summary, correct live image and archived original on raw resends", async () => { + const dir = mkdtempSync(join(tmpdir(), "bili-partial-block-")); + let store = new SessionStore({ enabled: true, dir }); + const png = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="; + const a = { type: "input_image", image_url: `data:image/png;base64,${png}`, detail: "low" }; + const b = { type: "input_image", image_url: `data:image/png;base64,${png}`, detail: "high" }; + const opaque = { type: "compaction", encrypted_content: "opaque-client-state" }; + const input: ResponseInputItem[] = [ + { type: "message", role: "user", content: "Preserve independent opening." }, + { type: "message", role: "assistant", content: "Ready." }, + { type: "message", role: "user", content: [{ type: "input_text", text: "Archived A decisions and constraints. ".repeat(200) }, a, + { type: "input_text", text: "Current B facts." }, b] }, + opaque, { type: "message", role: "assistant", content: "Continue." }, + ]; + const config = { ...defaultConfig(100_000), preserveRecentMessages: 0, preserveRecentTokens: 0 }; + const summary = "A established the accepted decisions and constraints. Those findings remain authoritative while continuing from B."; + try { + await testProxy(100_000, async (url, forwarded) => { + const send = async (history: ResponseInputItem[] = input): Promise => { + const response = await fetch(url, { method: "POST", headers: { "content-type": "application/json" }, + body: JSON.stringify({ model: "image-test", stream: true, session_id: "persisted-partial-block", input: history }) }); + assert.equal(response.status, 200, await response.text()); + await new Promise(resolve => setImmediate(resolve)); + }; + await send(); + const session = listSessions().find(item => item.meta.label === "persisted-partial-block"); + assert.ok(session); + const projection = responsesToCore({ input }); + const folded = createCore().applyCompression({ messages: projection.msgs, state: session.state, config, + ranges: [{ startRef: session.state.messageRefs.byRaw[projection.msgs[2]!.id]!, + endRef: session.state.messageRefs.byRaw[projection.msgs[3]!.id]!, summary }] }); + assert.equal(folded.result.blocksCreated, 1, JSON.stringify(folded.result)); + session.state = folded.state; + const blockId = session.state.blocks.at(-1)!.blockId; + const check = (): void => { + const wire = forwarded.at(-1)!.input!; + const pictures = wire.flatMap(item => Array.isArray(item.content) ? item.content.filter(part => part.type === "input_image") : []); + assert.deepEqual(pictures, [b], "A stays folded; B remains exactly once with its own detail"); + assert.equal(JSON.stringify(wire).split(summary).length - 1, 1, "one block has one visible summary"); + assert.deepEqual(wire.find(item => item.type === "compaction"), opaque, "untracked client state survives reconstruction"); + assert.ok(getSession(session.id).state.blocks.some(block => block.blockId === blockId && block.active)); + }; + await send(); check(); + // A temporary short view must not make the subsequent full echo + // resurrect the originals hidden by this same compression block. + await send([input[0]!, input[1]!, opaque, input.at(-1)!]); + await send(); check(); + // Conversely, a new identical upload outside the covered span is + // current user content, not an echo to suppress by pixel hash. + await send([...input, { type: "message", role: "user", content: [a] }]); + const withNewImage = forwarded.at(-1)!.input!.flatMap(item => Array.isArray(item.content) + ? item.content.filter(part => part.type === "input_image") : []); + assert.deepEqual(withNewImage, [b, a], "the genuinely new identical image remains once"); + await send(); check(); + await store.writeNow(getSession(session.id)); + await store.closeQuietlyAsync(); + _resetSessionsForTest(); + store = new SessionStore({ enabled: true, dir }); + _setStoreForTest(store); + await send(); check(); + const restoredSession = getSession(session.id); + const imageRef = restoredSession.state.messageRefs.byRaw[projection.msgs[3]!.id]; + assert.ok(imageRef && imageRef !== "BLOCKED"); + const restored = resolveDecompress({ imageRef }, { core: createCore(), config, messages: [], session: restoredSession, log: () => {} }); + assert.equal(restored.outcome, "success", restored.text); + const path = restored.text.match(/\n (.+\.png)/)?.[1]; + assert.ok(path, restored.text); + assert.deepEqual(readFileSync(path), Buffer.from(png, "base64")); + await send(); check(); + }, store); + } finally { + await store.closeQuietlyAsync(); + rmSync(dir, { recursive: true, force: true }); + } +}); + function screenshotItems(): ResponseInputItem[] { return [ { type: "message", role: "user", content: "Investigate the screenshot failure." },