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..7d90a20443 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,42 @@ export function inboundResignCredential( return decodeApigCredential(encoded); } +// #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 +// 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. 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[], + 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 @@ -2320,7 +2356,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 @@ -2342,7 +2378,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 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/issue4060-outbound-image-reserve.test.ts b/tests/issue4060-outbound-image-reserve.test.ts new file mode 100644 index 0000000000..e5ae4b0e2f --- /dev/null +++ b/tests/issue4060-outbound-image-reserve.test.ts @@ -0,0 +1,199 @@ +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"; + +// #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 +// 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: `i4060-${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("#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 + // 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("#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")]; + 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("#4060 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("#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("#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" } } }); + + // 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("#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. + 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("#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 } }] }] }; + 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", + ); +}); + +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"); +});