From d4f6887b21d07053e5f92a914ea9ecc42109b85d Mon Sep 17 00:00:00 2001 From: dadaya <1179002658@qq.com> Date: Tue, 20 Oct 2026 22:22:17 +0800 Subject: [PATCH 1/6] fix: charge the image reserve on the outbound wire, not the inbound body MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The window meter counted images on the INBOUND request body while the egress prune had already dropped them. Clients resend their whole history every turn, folded images included, so a fold that removed hundreds of screenshots from the wire left the inbound body untouched: the reserve kept charging for pixels nobody would be billed for, the meter read several hundred percent of the window, and the nudge emergency-suppressed the very folds that would have shrunk the payload. Production trace (a screenshot-heavy session): usage=863% with a 196KiB outbound body, and three consecutive upstream 400s at ~1.75M tokens against a 1.5M ceiling. Learning already measures the OUTBOUND body — noteForwardedImageFacts is fed countImagesInRawBody(wireBody) in relay.ts and loop/core.ts — so consuming the inbound body on the read side was an asymmetry, not a design. outboundImageReserve() walks prune(msgs, session.state) and charges each surviving image through the same base64ImageCost() the wire-body estimators use (now exported), then applies the same learned-truth cap imageReserveFor does. It returns 0 when no image survives — the honest reserve for a text-only wire. All four prepare paths (anthropic, openai, responses, google) now feed it to effectiveTokenCount instead of imageReserveFor(parsed). The two calibers agree exactly when nothing is folded, so the change is inert on unfurled sessions; a fold can only shrink the reserve, never grow it. Side effect worth noting: prepare-anthropic feeds the same number to projectThinkingMass, whose gap is providerInputTokens - local - overhead - imageTokens. A smaller image mass widens that gap, so more of the residual is attributed to signature-only thinking blocks. That residual is genuinely the billed-but-invisible thinking mass, so the attribution moves in the correct direction. --- src/image-tokens.ts | 5 +- src/server.ts | 39 +++- src/server/prepare-anthropic.ts | 11 +- src/server/prepare-google.ts | 4 +- src/server/prepare-openai.ts | 4 +- src/server/prepare-responses.ts | 4 +- .../issue3952-outbound-image-reserve.test.ts | 167 ++++++++++++++++++ 7 files changed, 220 insertions(+), 14 deletions(-) create mode 100644 tests/issue3952-outbound-image-reserve.test.ts diff --git a/src/image-tokens.ts b/src/image-tokens.ts index 91d85e16b9..4baa665320 100644 --- a/src/image-tokens.ts +++ b/src/image-tokens.ts @@ -182,7 +182,10 @@ export function pixelTileEstimate(w: number, h: number): number { return 85 + 170 * tiles; } -function base64ImageCost(b64: string, billing: ResolvedImageBilling, configuredCap?: number): number { +/** Cap-applied token cost of one base64 image under `billing`. Exported so the + * CoreMessage-level reserve (server.ts outboundImageReserve) charges pixels on + * exactly the same scale the wire-body estimators use. */ +export function base64ImageCost(b64: string, billing: ResolvedImageBilling, configuredCap?: number): number { if (billing === "pixels") { const dims = decodeImageDims(b64); return applyCap(dims ? pixelTileEstimate(dims.w, dims.h) : PIXEL_IMAGE_FALLBACK_TOKENS, configuredCap); diff --git a/src/server.ts b/src/server.ts index f960d90726..06fb795a38 100644 --- a/src/server.ts +++ b/src/server.ts @@ -5,7 +5,7 @@ import net from "node:net"; import tls from "node:tls"; import { createHash, randomUUID } from "node:crypto"; import { performance } from "node:perf_hooks"; -import { createCore, type CompressionCore, type CompressionState, type Config, type CoreMessage, type NudgeDecision, type Prompts, type PackSurface, defaultPrompts, deactivateBlock } from "acp-kernel"; +import { createCore, prune, type CompressionCore, type CompressionState, type Config, type CoreMessage, type NudgeDecision, type Prompts, type PackSurface, defaultPrompts, deactivateBlock } from "acp-kernel"; import { resolveCompress, resolveCompressPrompts, resolveCompressSurfaceDetailed, resolveRequestConfig, type ResolvedKernelConfig } from "./compress-settings.js"; import { dropCompressReasoningIds, type CompressReasoningConfig } from "./reasoning-drop.js"; import type { ProxyOptions, ResignSettings } from "./config.js"; @@ -76,7 +76,7 @@ import { import { ABSORB_TOOL_NAME, COMPRESS_TOOL, BILI_ACP_TOOLS_ANTHROPIC, BILI_ACP_TOOLS_ANTHROPIC_NO_RANGE, BILI_ACP_TOOLS_GOOGLE, BILI_ACP_TOOLS_GOOGLE_NO_RANGE, BILI_ACP_TOOLS_OPENAI, BILI_ACP_TOOLS_OPENAI_NO_RANGE, BILI_ACP_TOOLS_RESPONSES, BILI_ACP_TOOLS_RESPONSES_NO_RANGE, BILI_ACP_READONLY_TOOLS_RESPONSES, BILI_ACP_READONLY_TOOLS_RESPONSES_NO_RANGE, COMPRESS_TOOL_NAME, RULE_TOOL, RULE_TOOL_GOOGLE, RULE_TOOL_OPENAI, RULE_TOOL_RESPONSES, absorbToolsFor, retrieveToolsFor, buildAcpTagsOnlyPrompt } from "./compress-tool.js"; import { absorbEnabled, storeEffectiveAbsorb } from "./absorb.js"; import { ccrEnabled, ccrLoopConfig, ccrPluginWireOk, contentStoreOf, executeRetrieve, pruneExpiredRetrievals, reconcileReloadedRetrievals, renderRetrievalNotes, retrieveToolName, snapshotPendingRetrievals, snapshotRetrievalNotes, storeEffectiveCcr, type CcrSettings } from "./store.js"; -import { buildIncomingImageIndex, pruneRetrieveImgExports } from "./image-restore.js"; +import { buildIncomingImageIndex, messageImageBytes, pruneRetrieveImgExports } from "./image-restore.js"; import { rulesEnabled, storeEffectiveRules } from "./rules-feature.js"; import { storeEffectiveSearchPlanAware } from "./decompress-shared.js"; import { armAutoFoldBackoff, autoFoldEngaged, AUTO_FOLD_BACKOFF_MS, externalQueueDroppedAll, withExternalSummaryTools } from "./external-summary-surface.js"; @@ -88,7 +88,7 @@ import { buildSessionCacheReport, handleAcpCache, learnedImageReserve, noteClien import { extractBillingAttributionBlock, extractSummaryFromSse, preflightCompress, estimateCoreMessages, estimateCoreMessagesUpper, estimateCoreMessagesUpperBytes, estimateRawBodyTokens, type PreflightResult } from "./preflight.js"; import { buildDecisionPrompt, buildDirectiveText, consumeFallback, DEFAULT_DECIDE_MAX_TOKENS, DECIDE_TIMEOUT_MS, extractDecisionText, ladderMode, parseDecision, recordDecision, resolveDecisionRange, type DecideConfig, type DecisionOutcome } from "./nudge-decide.js"; import { gcConfigFromEnv, gcSessionFiles } from "./session-gc.js"; -import { countImagesInParsedBody, countImagesInRawBody, imageTokensInRawBody, imageTokensInParsedBody, resolveImageBilling, upstreamHost, type ResolvedImageBilling } from "./image-tokens.js"; +import { base64ImageCost, countImagesInParsedBody, countImagesInRawBody, imageTokensInRawBody, imageTokensInParsedBody, resolveImageBilling, upstreamHost, type ResolvedImageBilling } from "./image-tokens.js"; import { APIG_RESIGN_HEADER, APIG_RESIGN_CREDENTIAL_HEADER, APIG_RESIGN_SCHEME, KNOWN_SIGNATURE_SCHEMES, clearSignedRefusal, decodeApigCredential, inboundSignedScheme, readPendingRefusals, recordSignedRefusal, resignApig, signedRefusal, unresolvedRefusals, type ApigCredential } from "./apig-resign.js"; import { renderUI, handleConfigGet, handleConfigPut, handleSummaryCredentialPut, buildOverview, buildSessionList, buildSessionPage, buildSessionDetail, hiddenEmptyCount } from "./web/index.js"; import { reapOrphanBlocks } from "./orphan-gc.js"; @@ -1693,6 +1693,39 @@ export function inboundResignCredential( return decodeApigCredential(encoded); } +// #3952: the OUTBOUND image reserve — the same image mass imageReserveFor +// charges, but measured on the messages that will actually ride the wire +// instead of on the inbound request body. The client resends its whole history +// every turn, folded images included, so an inbound-body reserve keeps charging +// for pixels the egress prune already dropped: a long screenshot session +// reserves hundreds of images that never leave the proxy, the window meter reads +// several hundred percent, and the nudge emergency-suppresses the very folds +// that would shrink the payload. Learning already measures the OUTBOUND body +// (noteForwardedImageFacts on wireBody), so consuming the inbound one was an +// asymmetry, not a design. Returns 0 when no image survives the prune — the +// honest reserve for a text-only wire. +export function outboundImageReserve( + session: Session, + msgs: CoreMessage[], + opts: ProxyOptions, + upstreamUrl: string | undefined, +): number { + const billing = imageBillingFor(opts, upstreamUrl); + const cap = imageTokenCapFor(opts, upstreamUrl); + let prior = 0; + let nImages = 0; + for (const m of prune(msgs, session.state)) { + for (const img of messageImageBytes(m)) { + if (img.b64.length === 0) continue; + prior += base64ImageCost(img.b64, billing, cap); + nImages += 1; + } + } + if (nImages === 0) return 0; + const learned = learnedImageReserve(session, upstreamHost(upstreamUrl), nImages, `${billing}:${cap}`, cap); + return learned === undefined ? prior : Math.min(learned, prior); +} + // #1843 L1: the IMAGE-channel reserve for a payload — the prior-based estimate // (pixels/bytes per the resolved billing + cap) upgraded to LEARNED truth when // this session holds fresh usage-learned evidence for this route (per-image diff --git a/src/server/prepare-anthropic.ts b/src/server/prepare-anthropic.ts index aafb3fb290..27ebc5527b 100644 --- a/src/server/prepare-anthropic.ts +++ b/src/server/prepare-anthropic.ts @@ -6,7 +6,7 @@ import { rewriteInboundThreadContinue } from "./thread-tracking.js"; import { captureThreadsTurn, estimateThreadsTokens, isThreadsBody, threadsReforgeEnabled } from "./threads-lane.js"; import type { CompressReasoningConfig } from "../reasoning-drop.js"; import type { ProxyOptions } from "../config.js"; -import { applyReasoningDrop, diagNudge, diagTagSummary, deriveTitle, effectiveTokenCount, imageBillingFor, imageReserveFor, imageTokenCapFor, isAutoInjectedNotification, planReasoningDrop, reapOrphansLogged, runNudgeDecision, stripKernelSummaries, warnAnthropicThinkingPairs, type Prepared } from "../server.js"; +import { applyReasoningDrop, diagNudge, diagTagSummary, deriveTitle, effectiveTokenCount, imageBillingFor, imageReserveFor, imageTokenCapFor, isAutoInjectedNotification, outboundImageReserve, planReasoningDrop, reapOrphansLogged, runNudgeDecision, stripKernelSummaries, warnAnthropicThinkingPairs, type Prepared } from "../server.js"; import { buildDecisionPrompt, buildDirectiveText, consumeFallback, ladderMode, resolveDecisionRange, stripDecisionReasoningFields, type DecideConfig } from "../nudge-decide.js"; import { excludeDeadRefRanges } from "../acp-status.js"; import { reconcileFoldCoverage, noteSystemPromptFingerprint, resolveFoldReconcileMode } from "../fold-reconcile.js"; @@ -208,13 +208,16 @@ export async function prepareAnthropic( // #1320: signature-only thinking blocks bill restored thinking tokens upstream // but project as empty text locally — attribute the provider-vs-local residual // to them so every meter sees the billed context (metering-only, wire untouched). - const inboundImageTokens = imageReserveFor(session, "anthropic", parsed, opts, upstreamOrigin); + // #3952: the image mass subtracted from that residual is the OUTBOUND one — + // the residual is provider-minus-local for the payload actually sent, so + // charging images the prune already dropped would swallow the whole gap. + const outboundImageTokens = outboundImageReserve(session, renderMsgs, opts, upstreamOrigin); projectThinkingMass(msgs, { providerInputTokens: session.stats.lastInputTokens, measured: session.stats.lastInputTokensSource === "usage", systemText: extractSystem(parsed.system), tools: parsed.tools, - imageTokens: inboundImageTokens, + imageTokens: outboundImageTokens, storedOverhead: typeof session.metadata.systemPromptTokens === "number" ? session.metadata.systemPromptTokens : undefined, }); // #1001: pre-turn snapshot — processTurn below assigns fresh refs to every @@ -233,7 +236,7 @@ export async function prepareAnthropic( // zero-baseline forks replay their FULL raw history with no measurement, // so feeding 0 blinds the nudge (usage 0%, growth ref 0) and no // compression trigger fires until overflow. See effectiveTokenCount. - const { tokens: tokenCount, source: tokenCountSource } = effectiveTokenCount(session, renderMsgs, inboundImageTokens); + const { tokens: tokenCount, source: tokenCountSource } = effectiveTokenCount(session, renderMsgs, outboundImageTokens); const activeBefore = new Set(session.state.blocks.filter((b) => b.active).map((b) => b.blockId)); // Absorb markers are injected by the kernel's processTurn from // config.absorb. With no channel to call the tool (injection off), diff --git a/src/server/prepare-google.ts b/src/server/prepare-google.ts index 0cd9ed03c9..f1f6f45bff 100644 --- a/src/server/prepare-google.ts +++ b/src/server/prepare-google.ts @@ -4,7 +4,7 @@ import { renderNudgeText, viableRanges } from "acp-kernel"; import { coreToGoogle, googleToCore, normalizeGoogleUserPartOrder, type BiliMessage, type GoogleContent, type GoogleRequestBody, type GoogleSystemInstruction, type GoogleTool } from "acp-kernel/wire"; import type { ProxyOptions } from "../config.js"; import { currentCalibrationFactor } from "../util.js"; -import { diagNudge, diagTagSummary, deriveTitle, effectiveTokenCount, imageBillingFor, imageReserveFor, imageTokenCapFor, isAutoInjectedNotification, reapOrphansLogged, resolveKnownOutputCeiling, runNudgeDecision, stripKernelSummaries, usageGradeInputBaseline, type Prepared } from "../server.js"; +import { diagNudge, diagTagSummary, deriveTitle, effectiveTokenCount, imageBillingFor, imageReserveFor, imageTokenCapFor, isAutoInjectedNotification, outboundImageReserve, reapOrphansLogged, resolveKnownOutputCeiling, runNudgeDecision, stripKernelSummaries, usageGradeInputBaseline, type Prepared } from "../server.js"; import { buildDecisionPrompt, buildDirectiveText, consumeFallback, ladderMode, resolveDecisionRange, stripDecisionReasoningFields, type DecideConfig } from "../nudge-decide.js"; import { excludeDeadRefRanges } from "../acp-status.js"; import { reconcileFoldCoverage, noteSystemPromptFingerprint, resolveFoldReconcileMode } from "../fold-reconcile.js"; @@ -135,7 +135,7 @@ export async function prepareGoogle( // #1001: pre-turn snapshot — processTurn below assigns fresh refs to every // previously-unknown id, which would make rewrite detection read 1.0. const knownRefsBefore = new Set(Object.keys(session.state.messageRefs.byRaw)); - const { tokens: tokenCount, source: tokenCountSource } = effectiveTokenCount(session, renderMsgs, imageReserveFor(session, "google", parsed, opts, upstreamOrigin)); + const { tokens: tokenCount, source: tokenCountSource } = effectiveTokenCount(session, renderMsgs, outboundImageReserve(session, renderMsgs, opts, upstreamOrigin)); const activeBefore = new Set(session.state.blocks.filter((b) => b.active).map((b) => b.blockId)); const absorbBlock = effectiveAbsorbBlock(pluginMode, config, opts.compress.absorb); const absorbTools = absorbToolsFor(absorbBlock?.toolName ?? ABSORB_TOOL_NAME); diff --git a/src/server/prepare-openai.ts b/src/server/prepare-openai.ts index 6b825e22e9..410f695a64 100644 --- a/src/server/prepare-openai.ts +++ b/src/server/prepare-openai.ts @@ -6,7 +6,7 @@ import type { CompressReasoningConfig } from "../reasoning-drop.js"; import type { ProxyOptions } from "../config.js"; import { currentCalibrationFactor, hardenOpenaiAssistantContent, systemToUser } from "../util.js"; import { conversationHeaderSource, resolveRelayAffinityPck } from "../session-id.js"; -import { applyReasoningDrop, diagNudge, diagTagSummary, deriveTitle, effectiveTokenCount, imageBillingFor, imageReserveFor, imageTokenCapFor, isAutoInjectedNotification, planReasoningDrop, reapOrphansLogged, resolveKnownOutputCeiling, runNudgeDecision, stripKernelSummaries, warnDroppedOpenaiParts, warnReasoningPairs, type Prepared } from "../server.js"; +import { applyReasoningDrop, diagNudge, diagTagSummary, deriveTitle, effectiveTokenCount, imageBillingFor, imageReserveFor, imageTokenCapFor, isAutoInjectedNotification, outboundImageReserve, planReasoningDrop, reapOrphansLogged, resolveKnownOutputCeiling, runNudgeDecision, stripKernelSummaries, warnDroppedOpenaiParts, warnReasoningPairs, type Prepared } from "../server.js"; import { buildDecisionPrompt, buildDirectiveText, consumeFallback, ladderMode, resolveDecisionRange, stripDecisionReasoningFields, type DecideConfig } from "../nudge-decide.js"; import { excludeDeadRefRanges } from "../acp-status.js"; import { reconcileFoldCoverage, noteSystemPromptFingerprint, resolveFoldReconcileMode } from "../fold-reconcile.js"; @@ -128,7 +128,7 @@ export async function prepareOpenai( // tokenCount = upstream's real input_tokens from the previous turn // tokenCount = upstream's real input_tokens from the previous turn // (see anthropic branch comment + its #553-follow-up exception). - const { tokens: tokenCount, source: tokenCountSource } = effectiveTokenCount(session, renderMsgs, imageReserveFor(session, "openai", parsed, opts, billingUpstream ?? upstreamOrigin)); + const { tokens: tokenCount, source: tokenCountSource } = effectiveTokenCount(session, renderMsgs, outboundImageReserve(session, renderMsgs, opts, billingUpstream ?? upstreamOrigin)); const activeBefore = new Set(session.state.blocks.filter((b) => b.active).map((b) => b.blockId)); // Absorb markers ride in the kernel's processTurn output (gated by diff --git a/src/server/prepare-responses.ts b/src/server/prepare-responses.ts index 1bbd183c6e..72b43854f2 100644 --- a/src/server/prepare-responses.ts +++ b/src/server/prepare-responses.ts @@ -6,7 +6,7 @@ import type { CompressReasoningConfig } from "../reasoning-drop.js"; import { resolveAbsorbSettings } from "../compress-settings.js"; import { resolveCompressProtocol, type CompressSettings, type ProxyOptions } from "../config.js"; import { currentCalibrationFactor, strippedResponseIdWarning } from "../util.js"; -import { applyReasoningDrop, diagNudge, diagTagSummary, deriveTitle, effectiveTokenCount, imageBillingFor, imageReserveFor, imageTokenCapFor, isAutoInjectedNotification, planReasoningDrop, repairResponsesAssistantOrdering, reapOrphansLogged, resolveKnownOutputCeiling, runNudgeDecision, stripKernelSummaries, usageGradeInputBaseline, warnResponsesReasoningPairs, type Prepared } from "../server.js"; +import { applyReasoningDrop, diagNudge, diagTagSummary, deriveTitle, effectiveTokenCount, imageBillingFor, imageReserveFor, imageTokenCapFor, isAutoInjectedNotification, outboundImageReserve, planReasoningDrop, repairResponsesAssistantOrdering, reapOrphansLogged, resolveKnownOutputCeiling, runNudgeDecision, stripKernelSummaries, usageGradeInputBaseline, warnResponsesReasoningPairs, type Prepared } from "../server.js"; import { buildDecisionPrompt, buildDirectiveText, consumeFallback, ladderMode, resolveDecisionRange, stripDecisionReasoningFields, type DecideConfig } from "../nudge-decide.js"; import { excludeDeadRefRanges } from "../acp-status.js"; import { mergeAdjacentConfigurationUpdates, patchResponsesInputWithToolImages as patchResponsesInput, responsesToCoreWithToolImages as responsesToCore } from "../responses-tool-output.js"; @@ -243,7 +243,7 @@ export async function prepareResponses( if (opts.debug) { log("info", `[${sessionId}] input items: ${Array.isArray(parsed.input) ? parsed.input.map((i: ResponseInputItem) => i.type).join(",") : "(string)"}`); } - const { tokens: tokenCount, source: tokenCountSource } = effectiveTokenCount(session, renderMsgs, imageReserveFor(session, "responses", parsed, opts, billingUpstream ?? upstreamOrigin)); + const { tokens: tokenCount, source: tokenCountSource } = effectiveTokenCount(session, renderMsgs, outboundImageReserve(session, renderMsgs, opts, billingUpstream ?? upstreamOrigin)); // Absorb markers ride in the kernel's processTurn output (gated by // config.absorb). The marker/text protocol has no native tool channel, // so strip absorb from the loop config there (both modes). diff --git a/tests/issue3952-outbound-image-reserve.test.ts b/tests/issue3952-outbound-image-reserve.test.ts new file mode 100644 index 0000000000..c27571ce86 --- /dev/null +++ b/tests/issue3952-outbound-image-reserve.test.ts @@ -0,0 +1,167 @@ +import assert from "node:assert/strict"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +import type { BiliMessage } from "acp-kernel/wire"; +import type { CoreMessage } from "acp-kernel"; +import { imageReserveFor, outboundImageReserve } from "../src/server.ts"; +import type { Session } from "../src/session.ts"; + +// #3952: the window meter counted IMAGES ON THE INBOUND BODY while the egress +// prune had already dropped them. A screenshot-heavy session resends its whole +// history every turn, so a fold that removes 300 screenshots from the wire left +// the inbound body unchanged — the reserve kept charging for pixels nobody was +// billed for, the meter read several hundred percent, and the nudge +// emergency-suppressed the very folds that would have shrunk the payload +// (production: usage=863% with a 196KiB outbound body). Learning already +// measures the OUTBOUND wire body (noteForwardedImageFacts on wireBody), so +// consuming the inbound body was an asymmetry, not a design. +// +// These unit tests pin the new caliber: the reserve is charged on the messages +// that survive the prune, and it can only ever be at or below the inbound +// reading (a fold removes images, it never adds any). + +const UPSTREAM = "https://upstream.example/v1"; +const OPTS_BYTES = { routes: {}, imageBilling: "bytes" } as unknown as Parameters[2]; + +let seq = 0; +function makeSession(blocks: Session["state"]["blocks"] = [], metadata: Record = {}): Session { + seq += 1; + return { + id: `i3952-${seq}`, + metadata, + stats: {}, + state: { blocks, messageRefs: { byRaw: {}, byRef: {} }, tokenSnapshot: {} }, + } as unknown as Session; +} + +function activeBlock(id: string, ids: string[]): Session["state"]["blocks"][number] { + return { + blockId: id, + runId: "r1", + tier: 1, + summary: `summary of ${ids.length} messages`, + directMessageIds: ids, + effectiveMessageIds: ids, + directBlockIds: [], + compressedTokens: 1000, + createdAt: 1, + survivedCount: 0, + generation: "young", + active: true, + } as Session["state"]["blocks"][number]; +} + +/** An anthropic image core: the base64 rides the rawAnthropicBlock sidecar, the + * text projection is a short note — exactly the shape whose mass the text-only + * token model cannot see (#488). */ +function anthropicImage(id: string, b64: string): CoreMessage { + return { + id, + role: "tool", + contentType: "tool-result", + text: "image", + rawAnthropicBlock: { + type: "image", + source: { type: "base64", media_type: "image/png", data: b64 }, + }, + } as unknown as BiliMessage; +} + +function textMessage(id: string, text: string): CoreMessage { + return { id, role: "assistant", contentType: "text", text }; +} + +test("#3952 unit: images covered by an active block are NOT reserved (prune drops them off the wire)", () => { + const b64 = "A".repeat(4000); + // The opening user turn is pinned by the kernel (#3642), so the foldable + // images ride tool results — the production shape (screenshots returned by + // read_image land in tool_result cores). + const msgs = [textMessage("u1", "hi"), anthropicImage("a1", b64), anthropicImage("a2", b64), textMessage("t1", "hello")]; + const session = makeSession([activeBlock("b1", ["a1", "a2"])]); + + // The inbound body still carries both images — the old caliber keeps + // charging for them every turn, forever. + const inbound = { messages: [{ role: "user", content: msgs.slice(1, 3).map((m) => (m as unknown as BiliMessage).rawAnthropicBlock) }] }; + assert.equal(imageReserveFor(session, "anthropic", inbound, OPTS_BYTES, UPSTREAM), Math.ceil(4000 / 4) * 2, "inbound caliber charges the folded images"); + + assert.equal(outboundImageReserve(session, msgs, OPTS_BYTES, UPSTREAM), 0, "outbound caliber charges only what survives the prune"); +}); + +test("#3952 unit: images that survive the prune are charged per-image at the billing caliber", () => { + const b64 = "B".repeat(4000); + const session = makeSession(); + const msgs = [anthropicImage("a1", b64), textMessage("t1", "hello")]; + assert.equal(outboundImageReserve(session, msgs, OPTS_BYTES, UPSTREAM), Math.ceil(4000 / 4)); + + const two = [anthropicImage("a1", b64), anthropicImage("a2", b64)]; + assert.equal(outboundImageReserve(session, two, OPTS_BYTES, UPSTREAM), Math.ceil(4000 / 4) * 2, "n images, n charges"); +}); + +test("#3952 unit: an image nested in an anthropic tool_result counts too (#1995 parity)", () => { + const b64 = "C".repeat(8000); + const msg = { + id: "tr1", + role: "tool", + contentType: "tool-result", + text: "image", + rawAnthropicBlock: { + type: "tool_result", + tool_use_id: "t1", + content: [{ type: "text", text: "ok" }, { type: "image", source: { type: "base64", media_type: "image/png", data: b64 } }], + }, + } as unknown as BiliMessage; + assert.equal(outboundImageReserve(makeSession(), [msg], OPTS_BYTES, UPSTREAM), Math.ceil(8000 / 4)); +}); + +test("#3952 unit: text-only wires reserve zero", () => { + const session = makeSession(); + assert.equal(outboundImageReserve(session, [textMessage("t1", "x".repeat(10_000))], OPTS_BYTES, UPSTREAM), 0); + assert.equal(outboundImageReserve(session, [], OPTS_BYTES, UPSTREAM), 0); +}); + +test("#3952 unit: learned truth bounds the prior but never exceeds it", () => { + const b64 = "D".repeat(4000); // bytes caliber prior: 1000/image + const msgs = [anthropicImage("a1", b64), anthropicImage("a2", b64)]; + const learned = (cost: number) => makeSession([], { learnedImageCosts: { "upstream.example": { cost, seen: 3, ts: Date.now(), fp: "bytes:0" } } }); + + // Cheaper learned per-image cost (a real encoder bills far less than the + // bytes prior) → the learned reserve wins. + assert.equal(outboundImageReserve(learned(100), msgs, OPTS_BYTES, UPSTREAM), 200); + // Dearer learned cost → the physical prior caps it; learning can never + // inflate the reserve past what the payload could possibly bill. + assert.equal(outboundImageReserve(learned(50_000), msgs, OPTS_BYTES, UPSTREAM), 2000); + // Stale fingerprint (billing reconfigured since learning) → prior. + assert.equal( + outboundImageReserve(makeSession([], { learnedImageCosts: { "upstream.example": { cost: 100, seen: 3, ts: Date.now(), fp: "pixels:0" } } }), msgs, OPTS_BYTES, UPSTREAM), + 2000, + ); +}); + +test("#3952 unit: the outbound caliber never exceeds the inbound one — a fold can only shrink the reserve", () => { + const b64 = "E".repeat(4000); + // Three images, one of them folded: the outbound reserve must land exactly + // on the surviving pair, and always at or below what the inbound body bills. + const msgs = [textMessage("u1", "hi"), anthropicImage("a1", b64), anthropicImage("a2", b64), anthropicImage("a3", b64), textMessage("t1", "done")]; + const inbound = { messages: [{ role: "user", content: [1, 2, 3].map((i) => ({ type: "image", source: { type: "base64", media_type: "image/png", data: i === 3 ? "Z".repeat(4000) : b64 } })) }] }; + const session = makeSession([activeBlock("b1", ["a2"])]); + + const inboundTokens = imageReserveFor(session, "anthropic", inbound, OPTS_BYTES, UPSTREAM); + const outboundTokens = outboundImageReserve(session, msgs, OPTS_BYTES, UPSTREAM); + assert.equal(inboundTokens, Math.ceil(4000 / 4) * 3, "inbound bills all three images"); + assert.equal(outboundTokens, Math.ceil(4000 / 4) * 2, "outbound bills only the two that survive the fold"); + assert.ok(outboundTokens <= inboundTokens, "the fold can only shrink the image reserve"); +}); + +test("#3952 unit: a session that never folded keeps the inbound caliber exactly (no regression)", () => { + const b64 = "F".repeat(4000); + const msgs = [textMessage("u1", "hi"), anthropicImage("a1", b64), textMessage("t1", "done")]; + const inbound = { messages: [{ role: "user", content: [{ type: "image", source: { type: "base64", media_type: "image/png", data: b64 } }] }] }; + const session = makeSession(); + assert.equal( + outboundImageReserve(session, msgs, OPTS_BYTES, UPSTREAM), + imageReserveFor(session, "anthropic", inbound, OPTS_BYTES, UPSTREAM), + "with nothing covered, both calibers must agree — the fix is inert on unfurled sessions", + ); +}); From 801c099c6606d987878181e1e7b6286813ddbf89 Mon Sep 17 00:00:00 2001 From: awork Date: Wed, 21 Oct 2026 06:33:49 +0800 Subject: [PATCH 2/6] test(#4060): name the regression suite after its own issue (#3952 was an unrelated closed PR) Review-side fix on this branch: the suite pins #4060's outbound image-reserve caliber, so it is named issue4060-outbound-image-reserve.test.ts (repo convention: tests/-*.test.ts). No behavior change. --- ...> issue4060-outbound-image-reserve.test.ts} | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) rename tests/{issue3952-outbound-image-reserve.test.ts => issue4060-outbound-image-reserve.test.ts} (93%) diff --git a/tests/issue3952-outbound-image-reserve.test.ts b/tests/issue4060-outbound-image-reserve.test.ts similarity index 93% rename from tests/issue3952-outbound-image-reserve.test.ts rename to tests/issue4060-outbound-image-reserve.test.ts index c27571ce86..de14ca2cb3 100644 --- a/tests/issue3952-outbound-image-reserve.test.ts +++ b/tests/issue4060-outbound-image-reserve.test.ts @@ -8,7 +8,7 @@ import type { CoreMessage } from "acp-kernel"; import { imageReserveFor, outboundImageReserve } from "../src/server.ts"; import type { Session } from "../src/session.ts"; -// #3952: the window meter counted IMAGES ON THE INBOUND BODY while the egress +// #4060: the window meter counted IMAGES ON THE INBOUND BODY while the egress // prune had already dropped them. A screenshot-heavy session resends its whole // history every turn, so a fold that removes 300 screenshots from the wire left // the inbound body unchanged — the reserve kept charging for pixels nobody was @@ -29,7 +29,7 @@ let seq = 0; function makeSession(blocks: Session["state"]["blocks"] = [], metadata: Record = {}): Session { seq += 1; return { - id: `i3952-${seq}`, + id: `i4060-${seq}`, metadata, stats: {}, state: { blocks, messageRefs: { byRaw: {}, byRef: {} }, tokenSnapshot: {} }, @@ -73,7 +73,7 @@ function textMessage(id: string, text: string): CoreMessage { return { id, role: "assistant", contentType: "text", text }; } -test("#3952 unit: images covered by an active block are NOT reserved (prune drops them off the wire)", () => { +test("#4060 unit: images covered by an active block are NOT reserved (prune drops them off the wire)", () => { const b64 = "A".repeat(4000); // The opening user turn is pinned by the kernel (#3642), so the foldable // images ride tool results — the production shape (screenshots returned by @@ -89,7 +89,7 @@ test("#3952 unit: images covered by an active block are NOT reserved (prune drop assert.equal(outboundImageReserve(session, msgs, OPTS_BYTES, UPSTREAM), 0, "outbound caliber charges only what survives the prune"); }); -test("#3952 unit: images that survive the prune are charged per-image at the billing caliber", () => { +test("#4060 unit: images that survive the prune are charged per-image at the billing caliber", () => { const b64 = "B".repeat(4000); const session = makeSession(); const msgs = [anthropicImage("a1", b64), textMessage("t1", "hello")]; @@ -99,7 +99,7 @@ test("#3952 unit: images that survive the prune are charged per-image at the bil assert.equal(outboundImageReserve(session, two, OPTS_BYTES, UPSTREAM), Math.ceil(4000 / 4) * 2, "n images, n charges"); }); -test("#3952 unit: an image nested in an anthropic tool_result counts too (#1995 parity)", () => { +test("#4060 unit: an image nested in an anthropic tool_result counts too (#1995 parity)", () => { const b64 = "C".repeat(8000); const msg = { id: "tr1", @@ -115,13 +115,13 @@ test("#3952 unit: an image nested in an anthropic tool_result counts too (#1995 assert.equal(outboundImageReserve(makeSession(), [msg], OPTS_BYTES, UPSTREAM), Math.ceil(8000 / 4)); }); -test("#3952 unit: text-only wires reserve zero", () => { +test("#4060 unit: text-only wires reserve zero", () => { const session = makeSession(); assert.equal(outboundImageReserve(session, [textMessage("t1", "x".repeat(10_000))], OPTS_BYTES, UPSTREAM), 0); assert.equal(outboundImageReserve(session, [], OPTS_BYTES, UPSTREAM), 0); }); -test("#3952 unit: learned truth bounds the prior but never exceeds it", () => { +test("#4060 unit: learned truth bounds the prior but never exceeds it", () => { const b64 = "D".repeat(4000); // bytes caliber prior: 1000/image const msgs = [anthropicImage("a1", b64), anthropicImage("a2", b64)]; const learned = (cost: number) => makeSession([], { learnedImageCosts: { "upstream.example": { cost, seen: 3, ts: Date.now(), fp: "bytes:0" } } }); @@ -139,7 +139,7 @@ test("#3952 unit: learned truth bounds the prior but never exceeds it", () => { ); }); -test("#3952 unit: the outbound caliber never exceeds the inbound one — a fold can only shrink the reserve", () => { +test("#4060 unit: the outbound caliber never exceeds the inbound one — a fold can only shrink the reserve", () => { const b64 = "E".repeat(4000); // Three images, one of them folded: the outbound reserve must land exactly // on the surviving pair, and always at or below what the inbound body bills. @@ -154,7 +154,7 @@ test("#3952 unit: the outbound caliber never exceeds the inbound one — a fold assert.ok(outboundTokens <= inboundTokens, "the fold can only shrink the image reserve"); }); -test("#3952 unit: a session that never folded keeps the inbound caliber exactly (no regression)", () => { +test("#4060 unit: a session that never folded keeps the inbound caliber exactly (no regression)", () => { const b64 = "F".repeat(4000); const msgs = [textMessage("u1", "hi"), anthropicImage("a1", b64), textMessage("t1", "done")]; const inbound = { messages: [{ role: "user", content: [{ type: "image", source: { type: "base64", media_type: "image/png", data: b64 } }] }] }; From 7d0d91c78c1e6dfd15156e937c26754261de0cda Mon Sep 17 00:00:00 2001 From: awork Date: Wed, 21 Oct 2026 06:43:05 +0800 Subject: [PATCH 3/6] test(#4060): pin the no-fold reserve delta for byte-less media shapes Remote-URL images and redacted_thinking payloads are priced by the inbound body walker but carry no indexable image bytes in the pruned outbound view, so the metering reserve shrinks for them even without a fold. Pins the documented scope of the no-fold equality claim (inline base64/data-URL images); overflow stays backstopped by preflight, which prices these shapes through the body walker on the actual forward body. --- .../issue4060-outbound-image-reserve.test.ts | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/issue4060-outbound-image-reserve.test.ts b/tests/issue4060-outbound-image-reserve.test.ts index de14ca2cb3..e5ae4b0e2f 100644 --- a/tests/issue4060-outbound-image-reserve.test.ts +++ b/tests/issue4060-outbound-image-reserve.test.ts @@ -165,3 +165,35 @@ test("#4060 unit: a session that never folded keeps the inbound caliber exactly "with nothing covered, both calibers must agree — the fix is inert on unfurled sessions", ); }); + +test("#4060 unit: byte-less media shapes shrink the reserve without any fold (documented delta)", () => { + // The no-fold equality above holds for inline base64/data-URL images. Media + // shapes the inbound body walker prices but whose bytes messageImageBytes + // cannot carry (remote URLs ride as a small URL string; redacted_thinking + // carries signature payload, not pixels) drop out of the metering reserve + // even on an unfolded session: outbound ≤ inbound still holds, and + // preflight prices these shapes through the body walker on the actual + // forward body, so overflow stays backstopped. + const urlMsg = { + id: "u-img", + role: "user", + contentType: "text", + text: "", + rawOpenaiContentParts: [{ type: "image_url", image_url: { url: "https://img.example/x.png" } }], + } as unknown as BiliMessage; + const urlInbound = { messages: [{ role: "user", content: [{ type: "image_url", image_url: { url: "https://img.example/x.png" } }] }] }; + assert.equal(imageReserveFor(makeSession(), "openai", urlInbound, OPTS_BYTES, UPSTREAM), 4096, "inbound walker flat-prices the remote URL"); + assert.equal(outboundImageReserve(makeSession(), [urlMsg], OPTS_BYTES, UPSTREAM), 0, "outbound view carries no pixels for a URL reference"); + + const rtData = "R".repeat(8000); + const rtMsg = { + id: "rt1", + role: "assistant", + contentType: "reasoning", + text: "", + rawAnthropicBlock: { type: "redacted_thinking", data: rtData }, + } as unknown as BiliMessage; + const rtInbound = { messages: [{ role: "assistant", content: [{ type: "redacted_thinking", data: rtData }] }] }; + assert.equal(imageReserveFor(makeSession(), "anthropic", rtInbound, OPTS_BYTES, UPSTREAM), 2000, "inbound walker bills the redacted payload"); + assert.equal(outboundImageReserve(makeSession(), [rtMsg], OPTS_BYTES, UPSTREAM), 0, "no indexable image bytes in the outbound view"); +}); From 3c4098816f450d314a31d8e5f08c7f7e59dca6f2 Mon Sep 17 00:00:00 2001 From: awork Date: Wed, 21 Oct 2026 06:43:17 +0800 Subject: [PATCH 4/6] docs(#4060): cite #4060 in the outboundImageReserve comment #3952 is an unrelated closed PR (split-brain log observability); the regression this code implements is named after its own issue. --- src/server.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/server.ts b/src/server.ts index 06fb795a38..333e078b35 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1693,7 +1693,7 @@ export function inboundResignCredential( return decodeApigCredential(encoded); } -// #3952: the OUTBOUND image reserve — the same image mass imageReserveFor +// #4060: the OUTBOUND image reserve — the same image mass imageReserveFor // charges, but measured on the messages that will actually ride the wire // instead of on the inbound request body. The client resends its whole history // every turn, folded images included, so an inbound-body reserve keeps charging From ad26ca48c67f51b938fa6c9662a0c6c3a39ea948 Mon Sep 17 00:00:00 2001 From: awork Date: Wed, 21 Oct 2026 06:43:30 +0800 Subject: [PATCH 5/6] refactor(#4060): rename effectiveTokenCount's image-reserve parameter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The third argument now receives the OUTBOUND image reserve (outboundImageReserve) from all four prepare paths, not an inbound-body measurement — the old inboundImageTokens name describes the value this PR stops passing. --- src/server.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/server.ts b/src/server.ts index 333e078b35..a3290b0ec7 100644 --- a/src/server.ts +++ b/src/server.ts @@ -2353,7 +2353,7 @@ export function diagNudge(turn: { nudge?: { shouldInject: boolean; reason: strin // shrinks → the next estimate drops). Turn 1 of a fresh explicit session // still feeds 0 (nothing measured yet; nothing pending either), so // first-turn behavior is byte-identical to pre-#728. -export function effectiveTokenCount(session: Session, msgs: CoreMessage[], inboundImageTokens = 0): { tokens: number; source: "usage" | "estimate" } { +export function effectiveTokenCount(session: Session, msgs: CoreMessage[], imageTokens = 0): { tokens: number; source: "usage" | "estimate" } { // #1492: only usage-grade baselines are authoritative sizing inputs. An // estimate-sourced value describes ONE turn's outbound — possibly the FULL // raw history on an unfolded fallback turn — and pinning the nudge to it @@ -2375,7 +2375,7 @@ export function effectiveTokenCount(session: Session, msgs: CoreMessage[], inbou // reporting upstreams fall back to legacy sizing rather than a frozen meter. const anchored = postRebuildAnchorTokens(session); if (anchored > 0) return { tokens: anchored, source: "estimate" }; - const raw = estimateCoreMessagesUpper(msgs) + inboundImageTokens; + const raw = estimateCoreMessagesUpper(msgs) + imageTokens; // #1569/#1839: while the latest baseline is not usage-grade (the transient // window right after a failed turn), sizing on ANY re-derived view is how // ghosts enter: #1569 first tried min(est, raw) — the char-count upper From 2406d4286f9884b613c3b8ae1818f53beeab870c Mon Sep 17 00:00:00 2001 From: awork Date: Wed, 21 Oct 2026 07:21:10 +0800 Subject: [PATCH 6/6] docs(#4060): note the carrier-shape lockstep between the reserve reader and the raw-body walkers --- src/server.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/server.ts b/src/server.ts index a3290b0ec7..7d90a20443 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1703,7 +1703,10 @@ export function inboundResignCredential( // that would shrink the payload. Learning already measures the OUTBOUND body // (noteForwardedImageFacts on wireBody), so consuming the inbound one was an // asymmetry, not a design. Returns 0 when no image survives the prune — the -// honest reserve for a text-only wire. +// honest reserve for a text-only wire. Carrier-shape lockstep: messageImageBytes +// (this reader) and the image-tokens.ts raw-body walkers (learning-side count + +// inbound cost) are separate walkers — a NEW carrier shape must be added to both +// or the reserve silently undercharges it (#3398 discipline). export function outboundImageReserve( session: Session, msgs: CoreMessage[],