fix(sse): compact Responses multi-turn images before context hard-reject (#8560) (#8595)

* fix(sse): compact Responses multi-turn images before context hard-reject (#8560)

Codex Desktop sessions near the 372k input cap were rejected on the second
inline image because compressContext no-op'd on Responses input[] and never
pruned older vision turns. Adapt via bodyAdapter, prune older images while
keeping the latest, and run last-resort compaction before the budget check.

* docs(env): document CONTEXT_KEEP_LATEST_IMAGES for #8560

Keep check:env-doc-sync green after the context image-pruning override.

---------

Co-authored-by: diegosouzapw <diegosouzapw@users.noreply.github.com>
Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>
This commit is contained in:
Prudhvi Vuda
2026-07-27 20:12:27 -04:00
committed by GitHub
parent 352d48fedc
commit f3a78d68e8
6 changed files with 488 additions and 34 deletions

View File

@@ -1827,6 +1827,10 @@ APP_LOG_TO_FILE=true
# Tokens reserved for completion output when computing prompt budgets.
# Used by: open-sse/services/contextManager.ts. Default: 1024.
# CONTEXT_RESERVE_TOKENS=1024
# How many of the newest inline images to keep when pruning older ones to fit
# the context window (#8560). Used by: open-sse/services/contextManager.ts.
# Default: 2.
# CONTEXT_KEEP_LATEST_IMAGES=2
# ── Model alias rewriting (legacy compatibility) ──
# Toggle the legacy model-alias compatibility layer used by older clients.

View File

@@ -1057,6 +1057,7 @@ Provider quota endpoints, network tunnels (Tailscale, Ngrok, MITM debug proxy),
| `ALIBABA_CODING_PLAN_HOST` | _(production host)_ | `open-sse/services/bailianQuotaFetcher.ts` | Override the host used to fetch Alibaba Bailian coding-plan quotas. |
| `ALIBABA_CODING_PLAN_QUOTA_URL` | derived from host | `open-sse/services/bailianQuotaFetcher.ts` | Full quota URL override for Alibaba Bailian. |
| `CONTEXT_RESERVE_TOKENS` | `1024` | `open-sse/services/contextManager.ts` | Tokens reserved for completion output when computing prompt budgets. |
| `CONTEXT_KEEP_LATEST_IMAGES` | `2` | `open-sse/services/contextManager.ts` | How many of the newest inline images to keep when pruning older ones to fit the context window (#8560). |
| `MODEL_ALIAS_COMPAT_ENABLED` | enabled | `open-sse/services/model.ts` | Toggle the legacy model-alias compatibility layer used by older clients. |
| `OMNIROUTE_EMERGENCY_FALLBACK` | enabled | `open-sse/services/emergencyFallback.ts` | Set `false` (or `0`) to disable the emergency budget-exhaustion fallback that reroutes failed requests to the free `nvidia`/`openai/gpt-oss-120b` model. Effective precedence is Feature Flags DB override > env var > default; if unavailable, the service falls back to the raw env value. |
| `COMMAND_CODE_CALLBACK_PORT` | _(unset)_ | `src/app/api/providers/command-code/auth/shared.ts` | Local port used for OAuth-style callbacks from the Command Code CLI helper. |

View File

@@ -1685,7 +1685,7 @@ export async function handleChatCore({
if (!promptCompressionEnabled) {
log?.debug?.(
"CONTEXT",
"Skipping proactive context compression: Prompt Compression disabled"
"Prompt Compression engines disabled; reactive context compaction still applies when over threshold"
);
}
if (isCombo && comboName) {
@@ -1752,21 +1752,31 @@ export async function handleChatCore({
// content even after compression alters it (e.g. stable Kiro conversationId).
preCompressionBody = body;
if (promptCompressionEnabled && estimatedTokens > threshold) {
// Reactive context compaction is independent of optional prompt-compression
// engines (Caveman/RTK). Codex Desktop / Responses clients need this path even
// when those engines are off, otherwise multi-turn image sessions hard-reject
// at the budget check below (#8560).
if (estimatedTokens > threshold) {
log?.info?.(
"CONTEXT",
`Proactive compression triggered: ${estimatedTokens} tokens > ${threshold} threshold (${contextLimit} limit)`
);
const compressionResult = compressContext(body, {
// Adapt Responses `input[]` → messages so compressContext can run, then restore.
const ctxAdapter = adaptBodyForCompression(body as Record<string, unknown>);
const compressionResult = compressContext(ctxAdapter.body, {
provider,
model: effectiveModel,
maxTokens: threshold,
reserveTokens: 0,
});
if (compressionResult.compressed) {
body = compressionResult.body;
if (compressionResult.compressed && compressionResult.body) {
body = ctxAdapter.adapted
? ctxAdapter.restore(compressionResult.body as Record<string, unknown>, {
dropMissingMappedItems: true,
})
: compressionResult.body;
const stats = compressionResult.stats;
tokensCompressed = Math.max(0, (stats?.original ?? 0) - (stats?.final ?? 0));
const layersInfo =
@@ -1806,20 +1816,61 @@ export async function handleChatCore({
// filtering is advisory and may preserve an all-incompatible pool; this is the
// hard boundary that prevents a too-large prompt (or a negative token budget)
// from reaching an OpenAI-compatible upstream such as NVIDIA NIM.
const finalCompressionBody = body
? adaptBodyForCompression(body as Record<string, unknown>).body
: null;
const finalMessages =
finalCompressionBody?.messages ||
body?.contents ||
body?.request?.contents ||
(body?.input && typeof body.input === "object" && !Array.isArray(body.input) ? body.input : []);
const finalEstimatedInputTokens =
estimateTokens(finalMessages) +
(Array.isArray(body?.tools) ? estimateTokens(body.tools) : 0) +
estimateTokens(body?.system) +
estimateTokens(body?.instructions);
const estimateFinalInputTokens = (requestBody: Record<string, unknown> | null | undefined) => {
const adapted = requestBody
? adaptBodyForCompression(requestBody as Record<string, unknown>).body
: null;
const messages =
adapted?.messages ||
requestBody?.contents ||
requestBody?.request?.contents ||
(Array.isArray(requestBody?.input)
? requestBody.input
: requestBody?.input && typeof requestBody.input === "object"
? requestBody.input
: []);
return (
estimateTokens(messages) +
(Array.isArray(requestBody?.tools) ? estimateTokens(requestBody.tools) : 0) +
estimateTokens(requestBody?.system) +
estimateTokens(requestBody?.instructions)
);
};
let finalEstimatedInputTokens = estimateFinalInputTokens(body as Record<string, unknown>);
// Reuse the already-resolved `contextLimit` (may have been narrowed to the
// per-target combo window above, resolveComboContextLimit) instead of a bare
// getTokenLimit(provider, effectiveModel) re-fetch, which would silently
// discard that combo-aware override and re-widen the last-resort budget.
const finalContextLimit = contextLimit;
const toolsReserve = Array.isArray(body?.tools) ? estimateTokens(body.tools) : 0;
// Last-resort compaction against the concrete input budget (not the 70% threshold).
// Covers cases where the proactive pass was skipped or still left the request oversized (#8560).
if (finalEstimatedInputTokens >= finalContextLimit && body) {
const lastResortTarget = Math.max(1, finalContextLimit - toolsReserve - 1);
const lastResortAdapter = adaptBodyForCompression(body as Record<string, unknown>);
const lastResortResult = compressContext(lastResortAdapter.body, {
provider,
model: effectiveModel,
maxTokens: lastResortTarget,
reserveTokens: 0,
});
if (lastResortResult.compressed && lastResortResult.body) {
body = lastResortAdapter.adapted
? lastResortAdapter.restore(lastResortResult.body as Record<string, unknown>, {
dropMissingMappedItems: true,
})
: lastResortResult.body;
finalEstimatedInputTokens = estimateFinalInputTokens(body as Record<string, unknown>);
log?.info?.(
"CONTEXT",
`Last-resort context compaction: ${lastResortResult.stats?.original} → ${lastResortResult.stats?.final} tokens ` +
`(re-estimated input ${finalEstimatedInputTokens}, limit ${finalContextLimit})`
);
}
}
// Key the lookup by { provider, model } — the bare-string form resolves to
// `provider: null`, which skips both the registry cap and the operator's
// `max_token` capability override (#6524), the documented escape hatch for a

View File

@@ -235,10 +235,53 @@ function hasTextContent(message: MessageLike): boolean {
);
}
function isInlineBase64ImageUrl(value: unknown): boolean {
return typeof value === "string" && /^data:image\/[a-zA-Z0-9.+-]+;base64,/.test(value);
}
/** Image-only Responses turns must enter the adapter so #8560 image pruning can see them. */
function hasInlineImageContent(message: MessageLike): boolean {
if (!Array.isArray(message.content)) return false;
return message.content.some((part) => {
if (!isRecord(part)) return false;
if (part.type === "input_image" && isInlineBase64ImageUrl(part.image_url)) return true;
if (part.type === "image_url") {
const imageUrl = part.image_url;
if (isInlineBase64ImageUrl(imageUrl)) return true;
return isRecord(imageUrl) && isInlineBase64ImageUrl(imageUrl.url);
}
if (part.type === "image") {
if (isInlineBase64ImageUrl(part.image)) return true;
const source = part.source;
return isRecord(source) && source.type === "base64" && typeof source.data === "string";
}
const inlineData = part.inlineData ?? part.inline_data;
return isRecord(inlineData) && typeof inlineData.data === "string";
});
}
function hasCompressibleContent(message: MessageLike): boolean {
return hasTextContent(message) || hasInlineImageContent(message);
}
export type CompressionBodyRestoreOptions = {
/**
* When true, mapped `input[]` items whose synthetic messages were removed
* (e.g. compressContext Layer-3 purifyHistory) are omitted from the restored
* Responses payload. Default false preserves prior engine behavior: lite
* dedup may drop a synthetic duplicate without mutating the original input
* item positions (#8560 compaction opts in explicitly).
*/
dropMissingMappedItems?: boolean;
};
export type CompressionBodyAdapter = {
body: Record<string, unknown>;
adapted: boolean;
restore(compressedBody: Record<string, unknown>): Record<string, unknown>;
restore(
compressedBody: Record<string, unknown>,
options?: CompressionBodyRestoreOptions
): Record<string, unknown>;
};
export function adaptBodyForCompression(
@@ -278,7 +321,7 @@ export function adaptBodyForCompression(
inputItems.forEach((item, index) => {
if (!isRecord(item)) return;
const message = responsesItemToMessage(item);
if (!message || !hasTextContent(message)) return;
if (!message || !hasCompressibleContent(message)) return;
mappings.push({ index, item: item as ResponsesItem });
messages.push({ ...message, [COMPRESSION_INPUT_INDEX]: index });
});
@@ -295,11 +338,13 @@ export function adaptBodyForCompression(
const bodyWithoutInput = { ...body };
delete bodyWithoutInput.input;
const mappedIndexSet = new Set(mappings.map((mapping) => mapping.index));
return {
body: { ...bodyWithoutInput, messages },
adapted: true,
restore(compressedBody) {
restore(compressedBody, options = {}) {
const dropMissingMappedItems = options.dropMissingMappedItems === true;
const compressedMessagesByIndex = new Map<number, MessageLike>();
if (Array.isArray(compressedBody.messages)) {
for (const message of compressedBody.messages as MessageLike[]) {
@@ -308,19 +353,74 @@ export function adaptBodyForCompression(
}
}
}
const nextInput = [...inputItems];
mappings.forEach((mapping) => {
const compressedMessage = compressedMessagesByIndex.get(mapping.index);
if (!compressedMessage) return;
nextInput[mapping.index] = messageToResponsesItem(compressedMessage, mapping.item);
if (!dropMissingMappedItems) {
// Legacy restore: patch survivors in place; leave missing mapped items untouched
// so lite/caveman dedup of synthetic messages cannot desync Responses positions.
const nextInput = [...inputItems];
mappings.forEach((mapping) => {
const compressedMessage = compressedMessagesByIndex.get(mapping.index);
if (!compressedMessage) return;
nextInput[mapping.index] = messageToResponsesItem(compressedMessage, mapping.item);
});
const rest = { ...compressedBody };
delete rest.messages;
if (typeof body.input === "string") {
const first = nextInput[0];
return { ...rest, input: isRecord(first) ? (first.content ?? body.input) : body.input };
}
return { ...rest, input: nextInput };
}
// Compaction restore (#8560): rebuild input so Layer-3 history drops actually shrink
// Responses payloads. Also drop orphan function_call items whose outputs vanished.
const nextInput: unknown[] = [];
const survivingCallIds = new Set<string>();
inputItems.forEach((item, index) => {
if (mappedIndexSet.has(index)) {
const compressedMessage = compressedMessagesByIndex.get(index);
if (!compressedMessage) return;
const mapping = mappings.find((entry) => entry.index === index);
if (!mapping) return;
const restored = messageToResponsesItem(compressedMessage, mapping.item);
nextInput.push(restored);
if (
isRecord(restored) &&
(restored.type === "function_call_output" ||
restored.type === "custom_tool_call_output" ||
restored.type === "local_shell_call_output" ||
restored.type === "apply_patch_call_output") &&
typeof restored.call_id === "string"
) {
survivingCallIds.add(restored.call_id);
}
return;
}
nextInput.push(item);
});
const cleanedInput = nextInput.filter((item) => {
if (!isRecord(item) || item.type !== "function_call") return true;
if (typeof item.call_id !== "string" || item.call_id.length === 0) return true;
const hadMappedOutput = mappings.some((mapping) => {
const original = mapping.item;
return (
(original.type === "function_call_output" ||
original.type === "custom_tool_call_output") &&
original.call_id === item.call_id
);
});
if (!hadMappedOutput) return true;
return survivingCallIds.has(item.call_id);
});
const rest = { ...compressedBody };
delete rest.messages;
if (typeof body.input === "string") {
const first = nextInput[0];
const first = cleanedInput[0];
return { ...rest, input: isRecord(first) ? (first.content ?? body.input) : body.input };
}
return { ...rest, input: nextInput };
return { ...rest, input: cleanedInput };
},
};
}

View File

@@ -50,6 +50,19 @@ function getReserveTokensOverride(): number | null {
return null;
}
// How many of the newest inline images to keep when pruning older ones (#8560).
function getKeepLatestImagesOverride(): number | null {
const envValue = process.env.CONTEXT_KEEP_LATEST_IMAGES;
if (envValue) {
const parsed = parseInt(envValue, 10);
if (!isNaN(parsed) && parsed >= 0) return parsed;
}
return null;
}
const DEFAULT_KEEP_LATEST_IMAGES = 2;
const IMAGE_REMOVED_PLACEHOLDER = "[Earlier image removed to fit context window]";
// Rough chars-per-token ratio for quick estimation
const CHARS_PER_TOKEN = 4;
@@ -105,7 +118,7 @@ function matchesGeminiInlineDataShape(node: Record<string, unknown>): boolean {
* Detect the 5 documented inline-base64 image content-block shapes (see the
* shape-specific matchers above).
*/
function isInlineBase64ImageBlock(node: Record<string, unknown>): boolean {
export function isInlineBase64ImageBlock(node: Record<string, unknown>): boolean {
return (
matchesOpenAIImageUrlShape(node) ||
matchesAiSdkImageShape(node) ||
@@ -114,6 +127,71 @@ function isInlineBase64ImageBlock(node: Record<string, unknown>): boolean {
);
}
function replaceImageBlockWithPlaceholder(block: Record<string, unknown>): Record<string, unknown> {
// Responses API parts use input_text / input_image — keep that family so restore
// does not rewrite a chat-completions-shaped part into a Responses input item.
if (block.type === "input_image") {
return { type: "input_text", text: IMAGE_REMOVED_PLACEHOLDER };
}
if (block.inlineData || block.inline_data) {
return { text: IMAGE_REMOVED_PLACEHOLDER };
}
return { type: "text", text: IMAGE_REMOVED_PLACEHOLDER };
}
/**
* Replace oldest inline base64 image blocks with short text placeholders while
* keeping the newest `keepLatest` images intact. Vision models still receive
* recent screenshots; older ones are dropped so multi-turn Codex/Responses
* sessions can fit the concrete input cap (#8560).
*/
export function pruneOlderInlineImages(
messages: Record<string, unknown>[],
options: { keepLatest?: number; targetTokens?: number } = {}
): { messages: Record<string, unknown>[]; pruned: number } {
const keepLatest =
options.keepLatest ?? getKeepLatestImagesOverride() ?? DEFAULT_KEEP_LATEST_IMAGES;
const targetTokens = options.targetTokens;
const locations: Array<{ messageIndex: number; contentIndex: number }> = [];
for (let messageIndex = 0; messageIndex < messages.length; messageIndex++) {
const content = messages[messageIndex]?.content;
if (!Array.isArray(content)) continue;
for (let contentIndex = 0; contentIndex < content.length; contentIndex++) {
const part = content[contentIndex];
if (
part &&
typeof part === "object" &&
!Array.isArray(part) &&
isInlineBase64ImageBlock(part as Record<string, unknown>)
) {
locations.push({ messageIndex, contentIndex });
}
}
}
if (locations.length <= keepLatest) {
return { messages, pruned: 0 };
}
const prunable = locations.slice(0, Math.max(0, locations.length - keepLatest));
const next = messages.map((message) => {
if (!Array.isArray(message.content)) return message;
return { ...message, content: [...message.content] };
});
let pruned = 0;
for (const location of prunable) {
if (targetTokens != null && estimateTokens(next) <= targetTokens) break;
const content = next[location.messageIndex].content as unknown[];
const block = content[location.contentIndex] as Record<string, unknown>;
content[location.contentIndex] = replaceImageBlockWithPlaceholder(block);
pruned += 1;
}
return { messages: next, pruned };
}
/**
* Recursively walk a structured node, replacing every recognized inline
* base64 image block with a short placeholder (so its bulk is excluded from
@@ -305,19 +383,30 @@ export function resolveComboContextLimit(options: {
/**
* Apply context compression to request body.
* Operates in 3 layers of increasing aggressiveness:
* Operates in layers of increasing aggressiveness:
*
* Layer 1: Trim tool_result messages (truncate long outputs)
* Layer 1.5: Prune older inline images (keep latest N — #8560)
* Layer 2: Compress structured thinking blocks (remove from history, keep last)
* Layer 3: Aggressive purification (drop old messages until fitting)
*
* Callers with OpenAI Responses `input[]` (Codex) must adapt via
* `adaptBodyForCompression` before calling and `restore()` after — this helper
* is message-centric by design.
*
* @param {object} body - Request body with messages[]
* @param {object} options - { provider?, model?, maxTokens?, reserveTokens? }
* @param {object} options - { provider?, model?, maxTokens?, reserveTokens?, keepLatestImages? }
* @returns {{ body: object, compressed: boolean, stats: object }}
*/
export function compressContext(
body: Record<string, unknown>,
options: { provider?: string; model?: string; maxTokens?: number; reserveTokens?: number } = {}
body: Record<string, unknown> | null | undefined,
options: {
provider?: string;
model?: string;
maxTokens?: number;
reserveTokens?: number;
keepLatestImages?: number;
} = {}
) {
if (!body || !body.messages || !Array.isArray(body.messages)) {
return { body, compressed: false, stats: {} };
@@ -333,7 +422,7 @@ export function compressContext(
);
const targetTokens = Math.max(0, maxTokens - reserveTokens);
let messages = [...body.messages];
let messages = [...(body.messages as Record<string, unknown>[])];
// #8594: pass the structured messages array directly — estimateTokens walks it for
// inline base64 image blocks (#8368) and substitutes a bounded per-image estimate.
// JSON.stringify()-ing first forces the char/4 text path and mis-measures a ~500KB
@@ -359,6 +448,24 @@ export function compressContext(
};
}
// Layer 1.5: Drop oldest inline images while keeping the newest ones (#8560).
const imagePrune = pruneOlderInlineImages(messages, {
keepLatest: options.keepLatestImages,
targetTokens,
});
if (imagePrune.pruned > 0) {
messages = imagePrune.messages;
currentTokens = estimateTokens(messages);
stats.layers.push({ name: "prune_images", tokens: currentTokens });
if (currentTokens <= targetTokens) {
return {
body: { ...body, messages },
compressed: true,
stats: { ...stats, final: currentTokens },
};
}
}
// Layer 2: Compress structured thinking blocks (remove from non-last assistant messages)
messages = compressThinking(messages);
currentTokens = estimateTokens(messages); // #8594: object-path keeps the #8368 image estimate

View File

@@ -0,0 +1,191 @@
/**
* #8560 — multi-turn Codex/Responses sessions with inline images were hard-rejected
* near the concrete input cap because:
* 1) compressContext() no-op'd on Responses `input[]` (no body.messages)
* 2) older screenshots were never pruned on vision models
*
* These tests pin the adapter round-trip + image prune layer that keep the latest
* images while dropping older ones so the request fits.
*/
import test from "node:test";
import assert from "node:assert/strict";
import { adaptBodyForCompression } from "../../open-sse/services/compression/bodyAdapter.ts";
import {
compressContext,
estimateTokens,
pruneOlderInlineImages,
} from "../../open-sse/services/contextManager.ts";
function makeFakePngBase64(approxBytes: number): string {
return Buffer.alloc(approxBytes, 65).toString("base64");
}
function responsesImageTurn(text: string, base64: string) {
return {
type: "message",
role: "user",
content: [
{ type: "input_text", text },
{ type: "input_image", image_url: `data:image/png;base64,${base64}` },
],
};
}
test("#8560: pruneOlderInlineImages keeps the newest images and drops older ones", () => {
const base64 = makeFakePngBase64(8_000);
const messages = [
{
role: "user",
content: [
{ type: "input_text", text: "first" },
{ type: "input_image", image_url: `data:image/png;base64,${base64}` },
],
},
{
role: "user",
content: [
{ type: "input_text", text: "second" },
{ type: "input_image", image_url: `data:image/png;base64,${base64}` },
],
},
{
role: "user",
content: [
{ type: "input_text", text: "third" },
{ type: "input_image", image_url: `data:image/png;base64,${base64}` },
],
},
];
const { messages: pruned, pruned: count } = pruneOlderInlineImages(messages, {
keepLatest: 2,
});
assert.equal(count, 1);
const firstContent = pruned[0].content as Array<Record<string, unknown>>;
assert.equal(firstContent[1].type, "input_text");
assert.match(String(firstContent[1].text), /Earlier image removed/);
assert.equal((pruned[1].content as Array<Record<string, unknown>>)[1].type, "input_image");
assert.equal((pruned[2].content as Array<Record<string, unknown>>)[1].type, "input_image");
});
test("#8560: compressContext prunes older images before purifying history", () => {
const base64 = makeFakePngBase64(8_000);
const messages = Array.from({ length: 6 }, (_, i) => ({
role: "user",
content: [
{ type: "input_text", text: `turn-${i}` },
{ type: "input_image", image_url: `data:image/png;base64,${base64}` },
],
}));
const original = estimateTokens(messages);
// Force pruning all the way down to keepLatest=2 (~1200 tokens saved per image).
const target = original - 5_000;
const result = compressContext(
{ model: "gpt-5.6-terra", messages },
{ provider: "codex", maxTokens: target, reserveTokens: 0, keepLatestImages: 2 }
);
assert.equal(result.compressed, true);
assert.ok(
(result.stats?.final as number) < original,
`expected token count to shrink (original=${original}, final=${result.stats?.final})`
);
const layers = (result.stats as { layers?: Array<{ name: string }> }).layers || [];
assert.ok(
layers.some((layer) => layer.name === "prune_images"),
`expected prune_images layer, got ${JSON.stringify(layers)}`
);
const remainingImages = (result.body.messages as Array<{ content: unknown }>).flatMap((msg) =>
Array.isArray(msg.content)
? msg.content.filter(
(part) =>
part &&
typeof part === "object" &&
(part as { type?: string }).type === "input_image"
)
: []
);
assert.equal(remainingImages.length, 2);
// Target is advisory once keepLatest images remain; purifyHistory may still
// leave a small overshoot, but prune_images must have engaged.
assert.ok(target < original);
});
test("#8560: Responses input adapts → compressContext → restore shrinks and keeps latest images", () => {
const base64 = makeFakePngBase64(12_000);
const filler = "x".repeat(40_000); // ~10k text tokens to force compaction
const body = {
model: "gpt-5.6-terra",
instructions: "You are Codex.",
input: [
responsesImageTurn(`first ${filler}`, base64),
{ type: "message", role: "assistant", content: [{ type: "output_text", text: "ok-1" }] },
responsesImageTurn(`second ${filler}`, base64),
{ type: "message", role: "assistant", content: [{ type: "output_text", text: "ok-2" }] },
responsesImageTurn(`third ${filler}`, base64),
responsesImageTurn(`fourth ${filler}`, base64),
],
};
const adapter = adaptBodyForCompression(body);
assert.equal(adapter.adapted, true);
const before = estimateTokens(adapter.body.messages);
// Compress well below the original so prune + purify must engage.
const result = compressContext(adapter.body, {
provider: "codex",
model: "gpt-5.6-terra",
maxTokens: Math.max(8_000, Math.floor(before * 0.4)),
reserveTokens: 0,
keepLatestImages: 2,
});
assert.equal(result.compressed, true);
const restored = adapter.restore(result.body as Record<string, unknown>, {
dropMissingMappedItems: true,
});
assert.ok(!("messages" in restored), "Responses body must not leak synthetic messages");
assert.ok(Array.isArray(restored.input));
const input = restored.input as Array<Record<string, unknown>>;
assert.ok(input.length < body.input.length, "history purification should drop older turns");
const imageParts = input.flatMap((item) => {
if (!Array.isArray(item.content)) return [];
return item.content.filter(
(part) =>
part && typeof part === "object" && (part as { type?: string }).type === "input_image"
);
});
assert.ok(imageParts.length <= 2, `expected at most 2 surviving images, got ${imageParts.length}`);
assert.ok(imageParts.length >= 1, "newest images should survive pruning");
// Re-adapt for a fair object-aware estimate of the restored payload.
const reAdapted = adaptBodyForCompression(restored);
const finalEstimate =
estimateTokens(reAdapted.body.messages) + estimateTokens(restored.instructions);
assert.ok(
finalEstimate < before + estimateTokens(body.instructions),
`expected restored payload smaller than original (before=${before}, final=${finalEstimate})`
);
});
test("#8560: image-only Responses turns are adapted (not skipped)", () => {
const base64 = makeFakePngBase64(4_000);
const body = {
input: [
{
type: "message",
role: "user",
content: [{ type: "input_image", image_url: `data:image/png;base64,${base64}` }],
},
],
};
const adapter = adaptBodyForCompression(body);
assert.equal(adapter.adapted, true);
assert.equal((adapter.body.messages as unknown[]).length, 1);
});