From 632b28a150dacc8f48975fc4159ab9bcddaa6953 Mon Sep 17 00:00:00 2001 From: ci-check Date: Sat, 24 Oct 2026 15:35:19 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20learn=20upstream=20per-prompt=20image-c?= =?UTF-8?q?ount=20caps=20and=20converge=20outbound=20images=20=E2=80=94=20?= =?UTF-8?q?#4124?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Upstreams with a hard per-prompt image-count wall (e.g. vLLM --limit-mm-per-prompt) reject the whole request ("Too many images in request: 165 > 30"); bili forwarded that 400 verbatim every round and the session dead-ended. The reject vocabulary only knew token/context shapes, so the error was not even classified as a capacity rejection. - inspectContextOverflow detects image-count rejections on an independent channel (imageCountCap/imageCountSeen); isOverflow keeps its token-only meaning so no token-space arm can be minted from a count wall (parseOverflowWindow/toTokenNumber would reject <1000). - shedWireImagesToCount: count-space twin of shedWireImages (rung A only — downscaling cannot reduce count; tail yields to the hard cap; no MAX_SHED_IMAGES slice) converging the outbound copy to cap-1. Originals stay in session storage behind stable placeholder refs (decompress({imageRef}) recovers them). - Relay: same-request rescue (converge + re-send once; the client sees success), the learned cap persists per-origin in stats.imageCountCaps, later requests to the same route converge pre-send (no rejected round-trip), and the pck A/B probe skips content-wall 400s instead of white-running a key-less ablation that fails identically. --- src/egress-shed.ts | 60 ++++ src/server/relay.ts | 108 ++++++- src/session.ts | 9 + src/util.ts | 70 ++++- tests/issue4124-image-count-cap.test.ts | 400 ++++++++++++++++++++++++ 5 files changed, 638 insertions(+), 9 deletions(-) create mode 100644 tests/issue4124-image-count-cap.test.ts diff --git a/src/egress-shed.ts b/src/egress-shed.ts index 6aac645c30..d79628cef1 100644 --- a/src/egress-shed.ts +++ b/src/egress-shed.ts @@ -307,6 +307,66 @@ export async function shedWireImages( }; } +/** #4124: shed images from a serialized request until at most maxImages remain. + * Count-space twin of shedWireImages — RUNG A ONLY, because archiving is the only + * action that reduces COUNT (rung B downscaling leaves the image count untouched). + * Same placeholder/archive machinery, so every original stays reachable via + * decompress({imageRef}). Two deliberate differences from shedWireImages: + * - the work list is NOT sliced at MAX_SHED_IMAGES — that bound caps jimp CPU, + * while archiving is a cheap string swap and a 165→29 convergence needs ~136; + * - the protected tail YIELDS to the cap: an upstream count wall is HARD (sending + * over it fails the whole request), unlike the WS byte budget which is soft — + * once non-tail images are exhausted, tail images are archived too (oldest-first). + * Exempt read_original_image occurrences never enter the walk (collectOccurrences + * skips them); if exempt images alone exceed the cap the body cannot be brought + * under it and the rejection stays visible. Pure on the string; byte-identical + * passthrough when the count is already within maxImages or nothing was sheddable. */ +export async function shedWireImagesToCount( + body: string, + protocol: WireProtocol, + maxImages: number, + opts: ShedOptions = {}, +): Promise<{ body: string; stats: ShedStats }> { + const originalBytes = byteLen(body); + const noop: ShedStats = { originalBytes, finalBytes: originalBytes, archived: 0, downscaled: 0, changed: false }; + if (!Number.isInteger(maxImages) || maxImages < 0) return { body, stats: noop }; + let parsed: unknown; + try { + parsed = JSON.parse(body); + } catch { + return { body, stats: noop }; + } + if (!isObj(parsed)) return { body, stats: noop }; + const occurrences = collectOccurrences(protocol, parsed); + if (occurrences.length <= maxImages) return { body, stats: noop }; + + const totalEntries = protocol === "google" ? (Array.isArray(parsed.contents) ? parsed.contents.length : 0) + : protocol === "responses" ? (Array.isArray(parsed.input) ? parsed.input.length : 0) + : Array.isArray(parsed.messages) ? parsed.messages.length : 0; + const protectFrom = Math.max(0, totalEntries - PROTECTED_TAIL_ENTRIES); + for (const o of occurrences) o.protectedTail = o.entryIndex >= protectFrom; + + let remaining = occurrences.length; + let archived = 0; + for (const allowTail of [false, true]) { + if (remaining <= maxImages) break; + for (const o of occurrences) { + if (remaining <= maxImages) break; + if (o.archived) continue; + if (!allowTail && o.protectedTail) continue; + const note = await shedNote(o.mediaType, o.b64, opts.resolveRef); + o.applyArchive(note); + o.archived = true; + remaining--; + archived++; + } + } + + if (archived === 0) return { body, stats: noop }; + const next = JSON.stringify(parsed); + return { body: next, stats: { originalBytes, finalBytes: byteLen(next), archived, downscaled: 0, changed: true } }; +} + interface ShedContext { sessionId?: string; session?: Session; diff --git a/src/server/relay.ts b/src/server/relay.ts index 8745f844c8..bf3010f5aa 100644 --- a/src/server/relay.ts +++ b/src/server/relay.ts @@ -21,7 +21,7 @@ import { buildAbsorbSystemPrompt, buildCompressHybridSystemPrompt, buildCompress import { findRoute, resolveDeclaredProtocol, type ProxyOptions } from "../config.js"; import { makeContinuationRefetch } from "../degenerate-retry.js"; import { dumpRejectedBody } from "../error-dump.js"; -import { extractWsOversizeError, formatMiB, shedEgressIfOverBudget, shedWireImages, type ShedStats } from "../egress-shed.js"; +import { extractWsOversizeError, formatMiB, shedEgressIfOverBudget, shedWireImages, shedWireImagesToCount, type ShedStats } from "../egress-shed.js"; import { conversationPromptSections, externalSummaryEnabled } from "../external-summary-surface.js"; import { fakeBufCap, injectFakeCompletionHint, isFakeCompletion, maxFakeCompletionRetries } from "../fake-completion.js"; import { fetchWithTimeout, fetchWithTransportRetry } from "../fetch-util.js"; @@ -552,6 +552,24 @@ export async function forward( if (prepared && !prepared.sidePassthrough && prepared.protocol && req.method !== "GET" && req.method !== "HEAD" && typeof wireBody === "string") { const shed = await shedEgressIfOverBudget(wireBody, prepared.protocol, { sessionId: prepared.session.id, session: prepared.session, log, phase: "pre-send" }); wireBody = shed.body; + // #4124: proactive per-prompt image-count cap — once this route PROVED a + // count wall via an upstream rejection (learned into stats.imageCountCaps), + // converge the outbound copy under it BEFORE send so the request never 400s + // on count again. Only acts when the count is over; under-cap bodies pass + // through byte-identical (deterministic walk → prefix-cache stable). + const imgOrigin = normalizeUpstreamOrigin(upstreamUrl); + const imgCap = imgOrigin !== undefined ? prepared.session.stats.imageCountCaps?.[imgOrigin] : undefined; + if (typeof imgCap === "number" && imgCap > 0) { + const sess = prepared.session; + const countShed = await shedWireImagesToCount(wireBody, prepared.protocol, imgCap - 1, { + resolveRef: (mt, b64) => refForItemBytes(sess, mt, b64), + }); + if (countShed.stats.changed) { + sess.metadata.imageCountShed = { at: Date.now(), phase: "pre-send", cap: imgCap, archived: countShed.stats.archived }; + log("info", `[${sess.id}] [image-count-shed] pre-send: archived ${countShed.stats.archived} oldest images to stay under the learned per-prompt cap ${imgCap} on this route (#4124) — originals stay in session storage`); + wireBody = countShed.body; + } + } } // #1884: sign the FINAL wire body right before the send — everything // upstream of this point (prepare* injection, compat, steering) already @@ -1002,7 +1020,17 @@ export async function forward( } catch { strippedPckBody = null; } - if (strippedPckBody !== null) { + // #4124: a 400 whose body names a CONTENT wall (image-count or + // token/context overflow) exonerates prompt_cache_key by inspection — + // the key-less re-send can only fail identically, and in the field it + // hit a secondary byte-wall 413 instead (#4124 turn 44). Skip the + // probe; the overflow paths below own this error. + const pckInspection = strippedPckBody !== null ? inspectContextOverflow(400, pckErrText) : null; + const contentWall = pckInspection !== null && (pckInspection.isOverflow || pckInspection.imageCountCap !== undefined); + if (contentWall) { + log("debug", `[${prepared.session.id}] [pck] 400 names a content wall (${pckInspection!.imageCountCap !== undefined ? `image count > ${pckInspection!.imageCountCap}` : "context overflow"}) — prompt_cache_key exonerated by inspection, A/B probe skipped (#4124)`); + } + if (strippedPckBody !== null && !contentWall) { let retried: Awaited> | null = null; try { applyResign(headers, strippedPckBody); @@ -1219,7 +1247,67 @@ export async function forward( stopIdleTimer: upstreamResult.stopIdleTimer, }; const overflowInfo = inspectContextOverflow(upstreamResult.response.status, overflowText); - if (overflowInfo.isOverflow) { + if (overflowInfo.imageCountCap !== undefined && prepared!.protocol && typeof wireBody === "string") { + // #4124: image-COUNT wall — a different dimension than the token + // overflow below; armOverflowShrink must NOT run (its no-window + // branch would arm a bogus token-space shrink at the payload + // estimate). Learn the cap per-origin, converge the outbound copy + // to cap−1 with the count-shed machinery, and re-send ONCE within + // this same request. An unchanged body (nothing archivable), a + // Buffer body (raw passthrough bytes), or a missing protocol falls + // through to the verbatim passthrough below, which still learns + // the cap there. + const s = prepared!.session; + const imgOrigin = normalizeUpstreamOrigin(upstreamUrl); + if (imgOrigin !== undefined) { + const caps = s.stats.imageCountCaps ?? {}; + caps[imgOrigin] = overflowInfo.imageCountCap; + s.stats.imageCountCaps = caps; + markDirty(s); + } + const target = Math.max(0, overflowInfo.imageCountCap - 1); + const countShed = await shedWireImagesToCount(wireBody, prepared!.protocol, target, { + resolveRef: (mt, b64) => refForItemBytes(s, mt, b64), + }); + if (!countShed.stats.changed) { + log("warn", `[${s.id}] image-count overflow (upstream cap ${overflowInfo.imageCountCap}, request carried ${overflowInfo.imageCountSeen ?? "?"}) — nothing archivable in the outbound copy (images sit in exempt tool outputs?) — passing the rejection through (#4124)`); + } else { + try { + s.metadata.imageCountShed = { at: Date.now(), phase: "same-request-retry", cap: overflowInfo.imageCountCap, archived: countShed.stats.archived }; + applyResign(headers, countShed.body); + const retried = await fetchWithTimeout(upstreamUrl, { ...init, body: v1Wrap(countShed.body) }, undefined, clientAbort.signal); + if (retried.response.ok) { + upstreamResult.clearTimer(); + wireBody = countShed.body; // track the accepted re-send as the wire base + upstreamResult = retried; + log("info", `[${s.id}] image-count overflow (upstream cap ${overflowInfo.imageCountCap}${overflowInfo.imageCountSeen !== undefined ? `, request carried ${overflowInfo.imageCountSeen}` : ""}) — archived ${countShed.stats.archived} oldest images, re-sent within the same request, upstream accepted (#4124)`); + } else { + let retryErrText: string | null = null; + if (retried.response.body) { + try { + retryErrText = (await readStreamToBuffer(retried.response.body)).toString("utf8"); + } catch { + retryErrText = null; + } + } + // Answer with the retry's own verdict, not the stale first 400. + upstreamResult.clearTimer(); + upstreamResult = { + response: new Response(retryErrText ?? "", { + status: retried.response.status, + statusText: retried.response.statusText, + headers: new Headers(retried.response.headers), + }), + clearTimer: retried.clearTimer, + stopIdleTimer: retried.stopIdleTimer, + }; + log("warn", `[${s.id}] image-count overflow — converged to ${target} images but the re-send was still rejected (HTTP ${retried.response.status}; upstream counted ${overflowInfo.imageCountSeen ?? "?"}) — passing the retry verdict through (#4124)`); + } + } catch { + // transport failure — the buffered original 400 answers below + } + } + } else if (overflowInfo.isOverflow) { armOverflowShrink(overflowInfo); // #3787 follow-up: a NUMBERLESS overflow states no window; the // armed baseline (min of declared and the rejected payload's @@ -1350,6 +1438,20 @@ export async function forward( const info = inspectContextOverflow(upstream.status, errBody.toString("utf8")); if (info.isOverflow) { armOverflowShrink(info); + } else if (info.imageCountCap !== undefined) { + // #4124: an image-count wall reached this path without the + // same-request rescue above (no refold hook / response not + // writable / no protocol) — still LEARN the cap per-origin so + // the next request on this route converges pre-send. Never arms + // the token-space shrink (a count is not tokens). + const imgOrigin = normalizeUpstreamOrigin(upstreamUrl); + if (imgOrigin !== undefined) { + const caps = s.stats.imageCountCaps ?? {}; + caps[imgOrigin] = info.imageCountCap; + s.stats.imageCountCaps = caps; + markDirty(s); + } + log("warn", `[${s.id}] upstream image-count wall (cap ${info.imageCountCap}, request carried ${info.imageCountSeen ?? "?"}) — learned for pre-send convergence on this route; no token-space arm (#4124)`); } // #762: learn strict-echo on the MAIN request path too. The loop-only // learner (src/loop/core.ts) never sees client-originated 400s, so a diff --git a/src/session.ts b/src/session.ts index 13ee9cf544..27ff6f039f 100644 --- a/src/session.ts +++ b/src/session.ts @@ -346,6 +346,15 @@ export type Session = { /** #3450: one-time mismatch warning already emitted for this session * (upstream-stated window ≠ window in use). Provenance only. */ overflowArmMismatchWarned?: boolean; + /** #4124: per-origin learned per-prompt image-count caps, from upstream + * rejections ("Too many images in request: N > M"). Key = + * normalizeUpstreamOrigin(url) — the same origin stamping as the token + * overflow arm (#2313), because the cap belongs to the ROUTE, not the + * session. Separate from overflowArmTokens by construction: that channel + * rejects sub-1000 numbers and drives TOKEN-space shrink; an image cap + * is a count. Persisted with the session; no TTL — a stale (too low) cap + * can only over-shed, never under-shed. */ + imageCountCaps?: Record; /** Tokens compressed THIS turn whose fold has not yet materialized in * an upstream usage report (the post-compress re-request re-sends the * UNFOLDED history for prefix-cache reasons, so its usage report diff --git a/src/util.ts b/src/util.ts index 95f8aba135..dabe182630 100644 --- a/src/util.ts +++ b/src/util.ts @@ -260,6 +260,15 @@ export interface ContextOverflowInfo { * provider bills OUTPUT against the same wall as input, so preflight * must fold against window − reservation. */ requestedCompletion?: number; + /** #4124: per-prompt IMAGE-COUNT hard cap stated in the error body + * ("Too many images in request: 165 > 30"). A COUNT, not tokens — it must + * never ride the window/lastInputTokens channel (toTokenNumber rejects + * sub-1000 numbers, and that channel drives TOKEN-space shrink). Presence + * marks an image-count overflow; isOverflow stays token/context-only. */ + imageCountCap?: number; + /** #4124: the request's own image count as the upstream counted it ("165") + * — cross-check/log evidence only; bili counts occurrences itself. */ + imageCountSeen?: number; /** Truncated error-body text, for logging. */ message: string; } @@ -332,6 +341,45 @@ function toTokenNumber(s: string): number | undefined { return Number.isFinite(n) && n >= 1000 ? n : undefined; } +// #4124: per-prompt IMAGE-COUNT walls — a different dimension from the token +// patterns above. vLLM (--limit-mm-per-prompt) and gateway equivalents reject +// by COUNT of images, so a payload far under its token window still 400s on +// every round: images meter into tokens, but no fold decision ever looks at +// count. The cap is small (tens), which is exactly why it cannot ride the +// token-window channel (toTokenNumber rejects <1000; that channel drives +// token-space shrink). Known shapes: +// vLLM: "Too many images in request: 165 > 30" — gateways re-serialize error +// bodies with HTML-style escapes, so the wire form of ">" is "\u003e" +// (#4124 field log); both spellings must match. $1 = the request's own +// count (cross-check only), $2 = the cap. +// generic: "At most N image(s) may be provided" — cap only. +const IMAGE_COUNT_OVERFLOW_PATTERNS: RegExp[] = [ + /too many images in request:\s*(\d+)\s*(?:>|\\u003e)\s*(\d+)/i, + /at most\s+(\d+)\s+images?\s+may be provided/i, +]; + +/** #4124: extract the per-prompt image-count cap (+ the request's own count, + * when the body states both) from an error body. Returns an empty object + * unless a positive finite cap is present — a wrong guess would shed images + * the upstream actually accepts. */ +function parseImageCountCap(text: string): { cap?: number; seen?: number } { + let m = text.match(IMAGE_COUNT_OVERFLOW_PATTERNS[0]); + if (m) { + const seen = Number(m[1]); + const cap = Number(m[2]); + if (Number.isFinite(cap) && cap > 0) { + return { cap, ...(seen > 0 ? { seen } : {}) }; + } + return {}; + } + m = text.match(IMAGE_COUNT_OVERFLOW_PATTERNS[1]); + if (m) { + const cap = Number(m[1]); + if (Number.isFinite(cap) && cap > 0) return { cap }; + } + return {}; +} + /** Best-effort extraction of the real context window from an overflow error * body. Returns undefined when no confident window number is present — a wrong * guess (e.g. the prompt size, not the limit) is worse than no guess. */ @@ -416,14 +464,24 @@ export function inspectContextOverflow(status: number, bodyText: string): Contex if (!plausible) return { isOverflow: false, message }; if (!bodyText) return { isOverflow: false, message }; const isOverflow = CONTEXT_OVERFLOW_PATTERNS.some((p) => p.test(bodyText)); - if (!isOverflow) return { isOverflow: false, message }; + // #4124: the image-count wall is checked INDEPENDENTLY of the token markers + // — a "Too many images" body matches none of them (and must not arm any + // token-space machinery), yet every call site sees ONE inspection result. + const imageCount = parseImageCountCap(bodyText); + if (!isOverflow && imageCount.cap === undefined) return { isOverflow: false, message }; const counts = parseOverflowCounts(bodyText); return { - isOverflow: true, - window: parseOverflowWindow(bodyText), - ...(counts.total !== undefined ? { requestedTotal: counts.total } : {}), - ...(counts.messages !== undefined ? { requestedMessages: counts.messages } : {}), - ...(counts.completion !== undefined ? { requestedCompletion: counts.completion } : {}), + isOverflow, + ...(imageCount.cap !== undefined ? { imageCountCap: imageCount.cap } : {}), + ...(imageCount.seen !== undefined ? { imageCountSeen: imageCount.seen } : {}), + ...(isOverflow + ? { + window: parseOverflowWindow(bodyText), + ...(counts.total !== undefined ? { requestedTotal: counts.total } : {}), + ...(counts.messages !== undefined ? { requestedMessages: counts.messages } : {}), + ...(counts.completion !== undefined ? { requestedCompletion: counts.completion } : {}), + } + : {}), message, }; } diff --git a/tests/issue4124-image-count-cap.test.ts b/tests/issue4124-image-count-cap.test.ts new file mode 100644 index 0000000000..72f2f991d0 --- /dev/null +++ b/tests/issue4124-image-count-cap.test.ts @@ -0,0 +1,400 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +// #4124: an upstream with a per-prompt IMAGE-COUNT hard wall (vLLM +// --limit-mm-per-prompt and gateway equivalents) answers 400 "Too many images in +// request: N > M". bili used to forward that rejection verbatim: no vocab match +// (isOverflow false), nothing armed, nothing shed — a permanent 400 loop every +// round, with a byte-wall 413 cascade behind it (field: 165 > 30 across three +// independent providers, same shared model service). The fix adds: +// D1. inspectContextOverflow detects the count wall on its OWN channel +// (imageCountCap/imageCountSeen); isOverflow stays token/context-only — +// a cap of tens must never ride the token-window channel (toTokenNumber +// rejects <1000; that channel drives token-space shrink). +// D2. shedWireImagesToCount — count-space twin of shedWireImages (rung A only; +// downscaling cannot reduce count; the protected tail yields to the HARD +// cap, unlike the soft WS byte budget). +// D3. relay integration — same-request rescue through the #1195 seam (learn +// per-origin + converge to cap−1 + re-send once), proactive pre-send +// convergence for later requests, learn-on-passthrough when the rescue +// cannot run, and the pck A/B probe skipping content-wall 400s. + +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 { listSessions, _resetSessionsForTest } from "../src/session.ts"; +import { setLogCapture } from "../src/logger.ts"; +import { inspectContextOverflow } from "../src/util.ts"; +import { shedWireImagesToCount } from "../src/egress-shed.ts"; + +// ── D1: detection ───────────────────────────────────────────────────────────── + +const vllmBareBody = (seen: number, cap: number): string => + `{"error":{"message":"Upstream request failed: [invalid_request_error] Too many images in request: ${seen} > ${cap}","type":"invalid_request_error","param":"","code":null}}`; +// The field wire form: gateways re-serialize error bodies with HTML-style +// escapes, so ">" arrives as the six characters \u003e inside the raw JSON text. +const vllmEscapedBody = (seen: number, cap: number): string => + `{"error":{"message":"Upstream request failed: [invalid_request_error] Too many images in request: ${seen} \\u003e ${cap}","type":"invalid_request_error","param":"","code":null}}`; + +test("#4124 D1: vLLM image-count rejection is detected on its own channel (bare >)", () => { + const info = inspectContextOverflow(400, vllmBareBody(165, 30)); + assert.equal(info.isOverflow, false, "an image-count wall is NOT a token overflow"); + assert.equal(info.imageCountCap, 30); + assert.equal(info.imageCountSeen, 165); +}); + +test("#4124 D1: the gateway-escaped \\u003e wire form matches too", () => { + const info = inspectContextOverflow(400, vllmEscapedBody(165, 30)); + assert.equal(info.imageCountCap, 30); + assert.equal(info.imageCountSeen, 165); +}); + +test("#4124 D1: generic 'At most N image(s) may be provided' form (cap only)", () => { + const plural = inspectContextOverflow(400, '{"error":{"message":"At most 30 images may be provided"}}'); + assert.equal(plural.isOverflow, false); + assert.equal(plural.imageCountCap, 30); + assert.equal(plural.imageCountSeen, undefined); + const singular = inspectContextOverflow(400, '{"error":{"message":"At most 1 image may be provided"}}'); + assert.equal(singular.imageCountCap, 1); +}); + +test("#4124 D1: status gate — plausible statuses only", () => { + assert.equal(inspectContextOverflow(200, vllmBareBody(5, 2)).imageCountCap, undefined, "200 is dark"); + assert.equal(inspectContextOverflow(418, vllmBareBody(5, 2)).imageCountCap, undefined, "418 is dark"); + assert.equal(inspectContextOverflow(413, vllmBareBody(5, 2)).imageCountCap, 2, "413 is plausible"); + assert.equal(inspectContextOverflow(502, vllmBareBody(5, 2)).imageCountCap, 2, "5xx relays are plausible (#2484 parity)"); +}); + +test("#4124 D1: token overflows stay token-only; non-overflow bodies stay dark", () => { + const token = inspectContextOverflow(400, '{"error":{"message":"maximum context length is 131072 tokens"}}'); + assert.equal(token.isOverflow, true); + assert.equal(token.imageCountCap, undefined); + // Bedrock throttle phrase ("too many tokens") must not light up either channel. + const throttle = inspectContextOverflow(429, '{"error":{"message":"Too many tokens, please wait before trying again."}}'); + assert.equal(throttle.isOverflow, false); + assert.equal(throttle.imageCountCap, undefined); + assert.equal(inspectContextOverflow(400, '{"error":{"message":"Invalid API key"}}').imageCountCap, undefined); + assert.equal(inspectContextOverflow(400, "触发限流,请降低并发").imageCountCap, undefined); + // A degenerate cap (zero) is not a wall — refuse rather than shed everything. + assert.equal(inspectContextOverflow(400, vllmBareBody(5, 0)).imageCountCap, undefined); +}); + +// ── D2: count-space shedding ────────────────────────────────────────────────── + +const b64 = (i: number): string => "AAAA" + i.toString().padStart(8, "0"); + +function openaiBodyWithImages(n: number): string { + return JSON.stringify({ + model: "gpt-test", + messages: Array.from({ length: n }, (_, i) => ({ + role: "user", + content: [ + { type: "text", text: `turn ${i}` }, + { type: "image_url", image_url: { url: `data:image/png;base64,${b64(i)}` } }, + ], + })), + }); +} + +function countOpenaiImages(body: string): number { + const parsed = JSON.parse(body) as { messages?: Array<{ content?: unknown[] }> }; + let n = 0; + for (const m of parsed.messages ?? []) { + if (!Array.isArray(m.content)) continue; + for (const p of m.content) if (typeof p === "object" && p !== null && (p as { type?: unknown }).type === "image_url") n++; + } + return n; +} + +test("#4124 D2: over-cap sheds oldest-first down to maxImages (openai)", async () => { + const body = openaiBodyWithImages(10); // protectFrom = 10 − 8 = 2 + const r = await shedWireImagesToCount(body, "openai", 2); + assert.equal(r.stats.changed, true); + assert.equal(r.stats.archived, 8); + assert.equal(countOpenaiImages(r.body), 2); + const parsed = JSON.parse(r.body) as { messages: Array<{ content: Array<{ type?: string; text?: unknown }> }> }; + assert.ok(parsed.messages[8].content.some((p) => p.type === "image_url"), "newest survives"); + assert.ok(parsed.messages[9].content.some((p) => p.type === "image_url"), "newest survives"); + assert.ok(!parsed.messages[0].content.some((p) => p.type === "image_url"), "oldest archived"); + assert.ok(String(parsed.messages[0].content[1].text).includes("omitted for size limit"), "stable placeholder note"); +}); + +test("#4124 D2: deterministic — two runs on identical input produce identical bytes", async () => { + const body = openaiBodyWithImages(10); + const a = await shedWireImagesToCount(body, "openai", 2); + const b = await shedWireImagesToCount(body, "openai", 2); + assert.equal(a.body, b.body); +}); + +test("#4124 D2: under-cap bodies pass through byte-identical (gate negative direction)", async () => { + const body = openaiBodyWithImages(2); + const atCap = await shedWireImagesToCount(body, "openai", 2); + assert.equal(atCap.stats.changed, false); + assert.equal(atCap.body, body); + const under = await shedWireImagesToCount(openaiBodyWithImages(1), "openai", 2); + assert.equal(under.stats.changed, false); + assert.equal(under.body, openaiBodyWithImages(1)); +}); + +test("#4124 D2: the protected tail YIELDS to the hard cap (all images in-tail)", async () => { + const body = openaiBodyWithImages(4); // 4 entries are all inside the tail of 8 + const r = await shedWireImagesToCount(body, "openai", 1); + assert.equal(r.stats.archived, 3); + assert.equal(countOpenaiImages(r.body), 1); + const parsed = JSON.parse(r.body) as { messages: Array<{ content: Array<{ type?: string }> }> }; + assert.ok(parsed.messages[3].content.some((p) => p.type === "image_url"), "the newest one survives"); +}); + +test("#4124 D2: maxImages=0 archives every archivable image", async () => { + const r = await shedWireImagesToCount(openaiBodyWithImages(3), "openai", 0); + assert.equal(r.stats.archived, 3); + assert.equal(countOpenaiImages(r.body), 0); +}); + +test("#4124 D2: anthropic protocol shape", async () => { + const body = JSON.stringify({ + model: "claude-test", + messages: Array.from({ length: 5 }, (_, i) => ({ + role: "user", + content: [ + { type: "text", text: `t${i}` }, + { type: "image", source: { type: "base64", media_type: "image/png", data: b64(i) } }, + ], + })), + }); + const r = await shedWireImagesToCount(body, "anthropic", 2); + assert.equal(r.stats.changed, true); + assert.equal(r.stats.archived, 3); + const parsed = JSON.parse(r.body) as { messages: Array<{ content: Array<{ type?: string }> }> }; + const remaining = parsed.messages.filter((m) => m.content.some((p) => p.type === "image")).length; + assert.equal(remaining, 2); +}); + +test("#4124 D2: responses protocol shape", async () => { + const body = JSON.stringify({ + model: "gpt-test", + input: Array.from({ length: 5 }, (_, i) => ({ + type: "message", + role: "user", + content: [ + { type: "input_text", text: `t${i}` }, + { type: "input_image", image_url: `data:image/png;base64,${b64(i)}` }, + ], + })), + }); + const r = await shedWireImagesToCount(body, "responses", 2); + assert.equal(r.stats.archived, 3); + const parsed = JSON.parse(r.body) as { input: Array<{ content?: unknown[] }> }; + let remaining = 0; + for (const item of parsed.input) { + if (!Array.isArray(item.content)) continue; + for (const p of item.content) if ((p as { type?: unknown }).type === "input_image") remaining++; + } + assert.equal(remaining, 2); +}); + +test("#4124 D2: google protocol shape", async () => { + const body = JSON.stringify({ + contents: Array.from({ length: 5 }, (_, i) => ({ + parts: [ + { text: `t${i}` }, + { inlineData: { mimeType: "image/png", data: b64(i) } }, + ], + })), + }); + const r = await shedWireImagesToCount(body, "google", 2); + assert.equal(r.stats.archived, 3); + const parsed = JSON.parse(r.body) as { contents: Array<{ parts: Array<{ inlineData?: unknown }> }> }; + const remaining = parsed.contents.filter((c) => c.parts.some((p) => p.inlineData !== undefined)).length; + assert.equal(remaining, 2); +}); + +test("#4124 D2: read_original_image outputs stay exempt (best-effort boundary)", async () => { + const body = JSON.stringify({ + model: "gpt-test", + input: [ + { type: "function_call", call_id: "c1", name: "acp_read_original_image", arguments: "{}" }, + { type: "function_call_output", call_id: "c1", output: [{ type: "input_image", image_url: `data:image/png;base64,${b64(99)}` }] }, + ...Array.from({ length: 3 }, (_, i) => ({ + type: "message", + role: "user", + content: [{ type: "input_text", text: `t${i}` }, { type: "input_image", image_url: `data:image/png;base64,${b64(i)}` }], + })), + ], + }); + const r = await shedWireImagesToCount(body, "responses", 1); + assert.equal(r.stats.archived, 2, "only the two oldest ARCHIVABLE images go"); + const parsed = JSON.parse(r.body) as { input: Array> }; + const out = parsed.input.find((x) => x.type === "function_call_output")!; + assert.ok(JSON.stringify(out.output).includes(b64(99)), "the exempt output rides untouched"); +}); + +test("#4124 D2: unparseable bodies and no-image bodies pass through unchanged", async () => { + const bad = await shedWireImagesToCount("not json at all", "openai", 0); + assert.equal(bad.body, "not json at all"); + assert.equal(bad.stats.changed, false); + const plain = openaiBodyWithImages(0); + const r = await shedWireImagesToCount(plain, "openai", 0); + assert.equal(r.body, plain); +}); + +test("#4124 D2: placeholders quote the resolved mNNNNN ref", async () => { + const r = await shedWireImagesToCount(openaiBodyWithImages(3), "openai", 1, { resolveRef: async () => "m00012" }); + assert.ok(r.body.includes("m00012")); + assert.ok(r.body.includes("omitted for size limit")); +}); + +// ── D3: relay integration (real startServer + scripted mock upstream) ───────── + +const OK_BODY = JSON.stringify({ + id: "c1", + object: "chat.completion", + created: 1, + model: "gpt-test", + choices: [{ index: 0, message: { role: "assistant", content: "ok" }, finish_reason: "stop" }], + usage: { prompt_tokens: 10, completion_tokens: 1, total_tokens: 11 }, +}); + +function imagesMsgs(n: number): Array<{ role: string; content: unknown[] }> { + return Array.from({ length: n }, (_, i) => ({ + role: "user", + content: [ + { type: "text", text: `turn ${i}` }, + { type: "image_url", image_url: { url: `data:image/png;base64,${b64(i)}` } }, + ], + })); +} + +interface Rig { + post(messages: unknown[], extra?: Record): Promise; + upstreamBodies: string[]; + logs: string[]; + close(): Promise; +} + +async function startRig(script: Array<{ status: number; body: string }>): Promise { + const logs: string[] = []; + setLogCapture((level, msg) => { logs.push(`${level} ${msg}`); }); + _resetSessionsForTest(); + const upstreamBodies: string[] = []; + let call = 0; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + upstreamBodies.push(Buffer.concat(chunks).toString("utf8")); + const step = script[Math.min(call, script.length - 1)]; + call++; + res.writeHead(step.status, { "content-type": "application/json" }); + res.end(step.body); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const proxy = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { "gpt-test": { context: 100_000 } } } }, + modelContextLimit: 100_000, + kernelConfig: defaultConfig(100_000), + 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"); + const proxyPort = (proxy.address() as { port: number }).port; + const post = (messages: unknown[], extra: Record = {}): Promise => + 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": "sess-4124" }, + body: JSON.stringify({ model: "gpt-test", max_tokens: 1024, stream: false, messages, ...extra }), + }); + const close = async (): Promise => { + setLogCapture(null); + await new Promise((r) => proxy.close(() => r())); + await new Promise((r) => upstream.close(() => r())); + }; + return { post, upstreamBodies, logs, close }; +} + +test("#4124 D3a: image-count 400 → learn + same-request converge-to-cap−1 + re-send (client sees success)", async () => { + const rig = await startRig([ + { status: 400, body: vllmEscapedBody(5, 2) }, + { status: 200, body: OK_BODY }, + ]); + try { + const r = await rig.post(imagesMsgs(5)); + assert.equal(r.status, 200, "the same-request rescue answers the client with success, not the 400"); + await r.text(); + assert.equal(rig.upstreamBodies.length, 2, "one rejected send + one converged re-send"); + assert.equal(countOpenaiImages(rig.upstreamBodies[0]), 5, "the first send carried all five"); + assert.equal(countOpenaiImages(rig.upstreamBodies[1]), 1, "the re-send carries cap−1 = 1 image"); + const s = listSessions().find((x) => x.stats.imageCountCaps && Object.keys(x.stats.imageCountCaps).length > 0); + assert.ok(s, "a session exists with a learned per-origin cap"); + assert.deepEqual(Object.values(s!.stats.imageCountCaps!), [2]); + assert.ok( + rig.logs.some((l) => l.includes("image-count overflow (upstream cap 2") && l.includes("re-sent within the same request")), + "structured info line names cap, archived count and outcome", + ); + assert.ok(!rig.logs.some((l) => l.includes("[pck]")), "no pck activity without a key"); + } finally { + await rig.close(); + } +}); + +test("#4124 D3b: the learned cap converges PRE-SEND on the next request (no rejected round-trip)", async () => { + const rig = await startRig([ + { status: 400, body: vllmEscapedBody(5, 2) }, + { status: 200, body: OK_BODY }, + { status: 200, body: OK_BODY }, + ]); + try { + const first = await rig.post(imagesMsgs(5)); + assert.equal(first.status, 200); + await first.text(); + assert.equal(rig.upstreamBodies.length, 2, "turn one needed the reactive rescue"); + const second = await rig.post(imagesMsgs(4)); + assert.equal(second.status, 200); + await second.text(); + assert.equal(rig.upstreamBodies.length, 3, "turn two sent exactly ONE request — proactive convergence, no 400 round-trip"); + assert.equal(countOpenaiImages(rig.upstreamBodies[2]), 1, "the outbound copy was converged to cap−1 before send"); + assert.ok(rig.logs.some((l) => l.includes("[image-count-shed] pre-send")), "pre-send shed logged with archive count"); + } finally { + await rig.close(); + } +}); + +test("#4124 D3c: the pck A/B probe skips content-wall 400s (key survives; the image rescue owns the retry)", async () => { + const rig = await startRig([ + { status: 400, body: vllmBareBody(3, 2) }, + { status: 200, body: OK_BODY }, + ]); + try { + const r = await rig.post(imagesMsgs(3), { prompt_cache_key: "k1" }); + assert.equal(r.status, 200); + await r.text(); + assert.equal(rig.upstreamBodies.length, 2, "exactly one retry — the image rescue's, not a probe's"); + const second = JSON.parse(rig.upstreamBodies[1]) as Record; + assert.equal(second.prompt_cache_key, "k1", "the key-less A/B probe did NOT consume the retry"); + assert.equal(countOpenaiImages(rig.upstreamBodies[1]), 1, "the retry is CONVERGED (cap−1), not the untouched key-less ablation (all 3)"); + assert.ok(rig.logs.some((l) => l.includes("[pck] 400 names a content wall")), "skip logged at debug"); + assert.ok(!rig.logs.some((l) => l.includes("egress carried prompt_cache_key")), "no false key-causation recorded"); + } finally { + await rig.close(); + } +});