Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 123 additions & 7 deletions src/codex-compact.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createHash, randomUUID } from "node:crypto";
import type { CompressionBlock } from "acp-kernel";
import { baseIdOf, summaryMessageId, type CompressionBlock } from "acp-kernel";
import { evidenceInputBaseline, type Session } from "./session.js";
import { codexCompactMode as knobCodexCompactMode } from "./knobs.js";

Expand Down Expand Up @@ -334,19 +334,135 @@ export function renderForgedSummary(block: Pick<CompressionBlock, "summary" | "t
return body.length === 0 ? topicLine : `${topicLine}\n${body}`;
}

// #4600: one handoff-ledger entry. `blockId` carries provenance so superseded
// entries can be pruned; legacy persisted states hold plain strings (entries
// without provenance) that stay conservative forever.
interface ForgedSummaryEntry {
text: string;
blockId?: string;
}

// Accepts every shape the ledger has ever persisted (undefined, legacy
// string[], object entries, mixed garbage) into normalized entries, keeping
// the append-only exact-text dedup invariant.
export function normalizeForgedEntries(raw: unknown): ForgedSummaryEntry[] {
if (!Array.isArray(raw)) return [];
const out: ForgedSummaryEntry[] = [];
const seen = new Set<string>();
for (const item of raw) {
let text: string | undefined;
let blockId: string | undefined;
if (typeof item === "string") {
text = item;
} else if (item !== null && typeof item === "object") {
const rec = item as { text?: unknown; blockId?: unknown };
if (typeof rec.text === "string") {
text = rec.text;
if (typeof rec.blockId === "string" && rec.blockId.length > 0) blockId = rec.blockId;
}
}
if (text === undefined || seen.has(text)) continue;
seen.add(text);
out.push(blockId !== undefined ? { text, blockId } : { text });
}
return out;
}

// The forged compaction item is opaque to the model and codex truncates its
// history to [compaction_item, tail…], so the kernel deactivates the blocks
// covering the truncated prefix on the next replay and their summaries would
// vanish from the model's view. Capture them (kernel render format) at forge
// time; the server re-injects them into the developer message every turn
// (endpoint-form semantics). Append-only + exact-text dedup: a second forge
// accumulates, re-capture of deactivated blocks adds nothing.
export function mergeForgedSummaries(existing: string[] | undefined, blocks: readonly CompressionBlock[]): string[] {
const merged = existing ? [...existing] : [];
// time; the server re-injects them into the developer message whenever the
// echo is absent (#1064). Exact-text dedup: re-capture of an already-listed
// block adds nothing; each NEW active block appends one entry per forge.
export function mergeForgedSummaries(existing: unknown, blocks: readonly CompressionBlock[]): ForgedSummaryEntry[] {
const merged = normalizeForgedEntries(existing);
const byText = new Set(merged.map((e) => e.text));
for (const block of blocks) {
if (!block.active) continue;
const rendered = renderForgedSummary(block);
if (!merged.includes(rendered)) merged.push(rendered);
if (byText.has(rendered)) continue;
byText.add(rendered);
merged.push({ text: rendered, blockId: block.blockId });
}
return merged;
}

// #4600: drop ledger entries that are provably redundant. An entry is
// redundant once a newer-generation block folded the message carrying its
// summary (summaryMessageId ∈ consumer.effectiveMessageIds) AND that consumer
// is itself represented — still active (its summary renders live in the
// conversation) or holding its own ledger entry. Entries whose block was
// merely deactivated by the post-forge truncation have no consumer: they are
// the load-bearing fallback debt and stay. Unknown-provenance entries (legacy
// strings), blocks absent from state, and still-active blocks are kept —
// pruning is strictly shrinking and never speculative. Returns the same array
// reference when nothing changed.
export function pruneForgedEntries(entries: ForgedSummaryEntry[], blocks: readonly CompressionBlock[]): ForgedSummaryEntry[] {
if (entries.length === 0) return entries;
const byBlockId = new Map(blocks.map((b) => [b.blockId, b]));
const represented = new Set(entries.filter((e) => e.blockId !== undefined).map((e) => e.blockId as string));
// Only summary ids the ledger actually references need coverage lookups.
const consumers = new Map<string, CompressionBlock[]>();
for (const e of entries) {
if (e.blockId === undefined) continue;
const self = byBlockId.get(e.blockId);
if (!self || self.active) continue;
consumers.set(summaryMessageId(e.blockId), []);
}
if (consumers.size > 0) {
for (const b of blocks) {
for (const id of b.effectiveMessageIds ?? []) {
const list = consumers.get(baseIdOf(id));
if (list) list.push(b);
}
}
}
let changed = false;
const kept: ForgedSummaryEntry[] = [];
for (const e of entries) {
if (e.blockId === undefined) { kept.push(e); continue; }
const self = byBlockId.get(e.blockId);
if (!self || self.active) { kept.push(e); continue; }
const superseded = (consumers.get(summaryMessageId(e.blockId)) ?? []).some((c) => c.active || represented.has(c.blockId));
if (superseded) { changed = true; continue; }
kept.push(e);
}
return changed ? kept : entries;
}

// #4600: evidence that a request replays an actual conversation (completed
// turns) rather than context-only auxiliary traffic (prewarms, sidecars,
// probes — the #4594 shape). The fallback re-injection exists to protect
// replayed history from losing its pre-compaction handoff; a request that
// replays no conversation has nothing to protect and must not pay the
// accumulated-ledger tax — which used to fail such requests outright with a
// 413 once the ledger grew past the window.
const CONVERSATION_ITEM_TYPES = new Set([
"function_call",
"function_call_output",
"custom_tool_call",
"custom_tool_call_output",
"computer_call",
"mcp_call",
"reasoning",
]);

export function carriesConversationTurns(input: unknown): boolean {
if (!Array.isArray(input)) return false;
for (const item of input) {
if (!item || typeof item !== "object") continue;
const it = item as { type?: unknown; role?: unknown };
// Type-less assistant roles count too (OpenAI easy-input shorthand):
// a false positive only wastes tokens, a false negative loses the
// handoff — the predicate errs toward injecting.
if ((it.type === "message" || it.type === undefined) && it.role === "assistant") return true;
if (typeof it.type === "string" && CONVERSATION_ITEM_TYPES.has(it.type)) return true;
}
return false;
}

// #4600: observability threshold — warn when the fallback re-injection eats a
// significant slice of the window (the field case hit ~1.2M tokens against a
// 375K window before failing preflight). 10% keeps ordinary sessions quiet.
export const FORGED_SUMMARY_INJECT_WARN_PCT = 0.1;
2 changes: 1 addition & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ import { isStrictReasoningEcho, modelIdOf, normalizeStrictEchoReasoning, normali
export { isStrictReasoningEcho, normalizeStrictEchoReasoning, normalizeStrictEchoResponsesInput };
import { sanitizeResponsesInputIds, dropWhitespaceResponsesMessages, normalizeResponsesMessageItems } from "./loop/adapter-responses.js";
import type { CommandcodeEnvelopeMeta } from "./commandcode-wire.js";
import { CODEX_COMPACT_HEALTH_RATIO, codexCompactMode, codexHealthBarApplies, hasCompactionTrigger, stripBiliCompactionItems, replaceBiliCompactionItems, codexCompactGate, codexCompactGatePre, buildTriggerForgeBody, mergeForgedSummaries } from "./codex-compact.js";
import { CODEX_COMPACT_HEALTH_RATIO, codexCompactMode, codexHealthBarApplies, hasCompactionTrigger, stripBiliCompactionItems, replaceBiliCompactionItems, codexCompactGate, codexCompactGatePre, buildTriggerForgeBody } from "./codex-compact.js";
import { emitPreflightError } from "./stream-error.js";
import { agentProviderRecipes } from "./agent-providers.js";
import { affinityToken, claudeSubagentAgentId, claudeSubagentSplit, clientConversationHeader, codexTurnIdentity, conversationHeaderSource, dshPersonaFingerprintApplies, openaiSystemTextForPersona, preferPromptCacheKeyIdentity, type ConversationIdentity } from "./session-id.js";
Expand Down
42 changes: 35 additions & 7 deletions src/server/prepare-responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import { estimateCoreMessages, estimateCoreMessagesUpper } from "../preflight.js
import { hoistTrappedToolItems } from "../tool-pair-order.js";
import { isStrictReasoningEcho, modelIdOf, normalizeStrictEchoResponsesInput } from "../strict-echo.js";
import { reconcileSystemAnchor } from "../system-anchor.js";
import { buildPromptForgeBody, buildTriggerForgeBody, carriesCodexLocalCompactionSummary, codexCompactGate, codexCompactGatePre, codexCompactMode, hasCompactionPrompt, hasCompactionTrigger, isCodexClient, isCodexHost, mergeForgedSummaries, nativeCompactionKey, replaceBiliCompactionItems, stripBiliCompactionItems, CODEX_COMPACT_HEALTH_RATIO, CODEX_LOCAL_COMPACTION_MIN_MISSING } from "../codex-compact.js";
import { buildPromptForgeBody, buildTriggerForgeBody, carriesCodexLocalCompactionSummary, carriesConversationTurns, codexCompactGate, codexCompactGatePre, codexCompactMode, hasCompactionPrompt, hasCompactionTrigger, isCodexClient, isCodexHost, mergeForgedSummaries, nativeCompactionKey, normalizeForgedEntries, pruneForgedEntries, replaceBiliCompactionItems, stripBiliCompactionItems, CODEX_COMPACT_HEALTH_RATIO, CODEX_LOCAL_COMPACTION_MIN_MISSING, FORGED_SUMMARY_INJECT_WARN_PCT } from "../codex-compact.js";
import type { ConversationIdentity } from "../session-id.js";
import { pckKeyAbated } from "../pck-probe.js";
import { reconcilePluginFoldCallIds } from "../plugin.js";
Expand Down Expand Up @@ -474,9 +474,35 @@ reconcileFoldCoverage(session, msgs, { mode: foldMode, sessionId, log, markerStr
// message so the pre-compaction content is never lost. When the echo
// DID come back, the replacement message carries the summaries and
// the injection is suppressed to avoid duplicating them.
const forgedSummaries = echoReplaced
? []
: (session.metadata.codexForgedSummaries as string[] | undefined) ?? [];
// #4600: the ledger is debt with a lifecycle — prune entries whose
// block's summary was folded into a represented newer block (provably
// redundant, persisted), and inject nothing into requests that replay
// no conversation (context-only probes/sidecars have no replayed
// history to protect and must not pay the accumulated tax, which used
// to fail them with 413 once the ledger exceeded the window).
let forgedSummaries: string[] = [];
if (!echoReplaced) {
const forgedEntries = normalizeForgedEntries(session.metadata.codexForgedSummaries);
if (forgedEntries.length > 0) {
if (carriesConversationTurns(parsed.input)) {
const kept = pruneForgedEntries(forgedEntries, session.state.blocks);
forgedSummaries = kept.map((e) => e.text);
if (kept.length < forgedEntries.length) {
session.metadata.codexForgedSummaries = kept;
markDirty(session);
log("info", `[${sessionId}] [inject] pruned ${forgedEntries.length - kept.length} superseded forged-summary entrie(s) from the handoff ledger (${kept.length} remain)`);
}
} else {
log("debug", `[${sessionId}] [inject] skipped ${forgedEntries.length} forged-summary entrie(s): request replays no conversation (auxiliary/probe traffic)`);
}
}
}
if (forgedSummaries.length > 0 && config.modelContextLimit > 0) {
const injectedTokens = defaultCountTokens(forgedSummaries.join("\n\n---\n\n"));
if (injectedTokens >= config.modelContextLimit * FORGED_SUMMARY_INJECT_WARN_PCT) {
log("warn", `[${sessionId}] [inject] large forged-summary re-injection: ${forgedSummaries.length} entrie(s) ≈ ${injectedTokens} tokens (${Math.round((injectedTokens / config.modelContextLimit) * 100)}% of the ${config.modelContextLimit}-token window)`);
}
}
// #1881: the NEVER-echo prohibition follows the rendered tags, not the tool switch.
const tagsOnlyPrompt = !compressSurfaceOn && !isCompactionTrigger && !knobNoCompressPrompt() && !knobRenderNone()
? buildAcpTagsOnlyPrompt(responsesTextProtocol ? "hybrid" : "function", prompts, messageTimestampSections(surface?.promptSections, responsesTextProtocol ? "hybrid" : "function"))
Expand Down Expand Up @@ -621,9 +647,11 @@ reconcileFoldCoverage(session, msgs, { mode: foldMode, sessionId, log, markerStr
&& isCodexHost(req.headers) // #3686: host policy, not wire profile
&& codexCompactGate(session, config.modelContextLimit, transformOk)) {
const summaries = session.state.blocks.filter((b) => b.active).map((b) => b.summary);
const prevForged = session.metadata.codexForgedSummaries as string[] | undefined;
const captured = mergeForgedSummaries(prevForged, session.state.blocks);
if (captured.length !== (prevForged?.length ?? 0)) {
// #4600: capture + prune in one pass so the persisted ledger never
// keeps entries superseded by an already-represented newer block.
const prevEntries = normalizeForgedEntries(session.metadata.codexForgedSummaries);
const captured = pruneForgedEntries(mergeForgedSummaries(session.metadata.codexForgedSummaries, session.state.blocks), session.state.blocks);
if (captured.length !== prevEntries.length) {
session.metadata.codexForgedSummaries = captured;
markDirty(session);
}
Expand Down
Loading
Loading