Skip to content
Merged
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
17 changes: 16 additions & 1 deletion src/preflight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
type CoreMessage,
type Prompts,
type PackSurface,
type NudgeDecision,
} from "acp-kernel";
import { buildCompressSystemPrompt, parseCompressInput } from "./compress-tool.js";
import { IMAGE_PLACEHOLDER, imagePlaceholdersForSummary } from "./image-note.js";
Expand Down Expand Up @@ -1669,11 +1670,16 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa
const attemptTierRecovery = async (): Promise<TierRecoveryResult> => {
let callsUsed = 0;
let netSaved = 0;
// #4227: the most recent measurement's nudge offer — pinned into the
// distill fold's ctx so its receipt tail judges from the same
// source/scope/moment as the recovery's own processTurn call.
let measuredNudge: NudgeDecision | undefined;
const measureEst = (): number => {
const turn = deps.core.processTurn({
messages, state: deps.session.state, config: noEmergencyTruncate(ccrLoopConfig(deps.session, activeConfig)),
tokenCount: currentTokens, renderTags: "text-only", contentStore: contentStoreOf(deps.session),
});
measuredNudge = turn.nudge;
deps.session.state = turn.state;
adoptContentStore(deps.session, turn.contentStore);
const view = applyAbsorbView(turn.messages, turn.state, activeConfig, currentTokens);
Expand Down Expand Up @@ -1815,7 +1821,7 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa
stopReason = "unavailable";
break;
}
const ctx: RewriteCtx = { core: deps.core, config: activeConfig, messages, session: deps.session, log: (m) => deps.log("info", m) };
const ctx: RewriteCtx = { core: deps.core, config: activeConfig, messages, session: deps.session, lastNudge: measuredNudge, log: (m) => deps.log("info", m) };
const fold = parseCompressInput({ content: [{ startId: first.blockId, endId: last.blockId, summary: outcome.summary, topic: `preflight tier-${sourceTier + 1} distill (#2952)` }] });
const applied = applyRanges(fold, ctx, { loopTracking: false });
if (applied.outcome === "refused") {
Expand Down Expand Up @@ -2135,6 +2141,15 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa
config: activeConfig,
messages,
session: deps.session,
// #4227: pin the receipt tail to THIS round's own nudge offer —
// the same source/scope/moment the walk above just acted on.
// Without it, postCompressTail falls back to a live recompute
// judged on statusInputBaseline (the previous request's usage-
// grade input tokens): in an overflow marathon that baseline is
// far below the window while the ACTUAL payload is 19x over, so
// the tail printed "No compressible ranges remain" in the very
// fold whose walk logged "102 viable range(s)" (#4224/#4227).
lastNudge: turn.nudge,
log: (msg) => deps.log("info", msg),
};
const creditBefore = deps.session.stats.compressCreditTokens;
Expand Down
41 changes: 34 additions & 7 deletions src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ export type RewriteCtx = {
* acted on. postCompressTail bases "remaining ranges" on it (minus the
* just-folded spans) instead of a post-fold recompute, which would advance
* the protection window and leak newly-de-protected ranges into the receipt
* (zero-growth chained folds + prefix-cache thrash). Absent on internal
* lanes (preflight) → they fall back to the live recompute. */
* (zero-growth chained folds + prefix-cache thrash). Preflight internal
* lanes pin their round's own processTurn offer (#4227); a lane without
* one falls back to the live recompute below. */
lastNudge?: NudgeDecision;
};

Expand Down Expand Up @@ -557,8 +558,8 @@ function postCompressTail(ctx: RewriteCtx, cleanSuccess: boolean, submitted: Arr
base = excludeDeadRefRanges(viableRanges(ctx.lastNudge.compressibleRanges).filter(submitGate), ctx.session.state.deadRefs);
tier = ctx.lastNudge.tier ?? null;
} else {
// Internal lanes (preflight overflow-compress) capture no prepare-time
// nudge → fall back to the live recompute (same source as #389).
// Lane without a pinned offer → fall back to the live recompute (same
// source as #389); preflight now pins its round's own nudge (#4227).
// processTurn is pure; the returned state is deliberately NOT adopted.
let nudge: NudgeDecision | undefined;
try {
Expand Down Expand Up @@ -1104,8 +1105,16 @@ export function applyRanges(parsed: ReturnType<typeof parseCompressInput>, ctx:
// denominator makes the ratio structurally ≤1, so #1839's
// "trustworthy baseline" gate is subsumed and no longer needed.
const viewMessages = ctx.compressMessages ?? ctx.messages;
// #4227: id→tokens map — one scan of the view serves both meters below.
const viewTokById = new Map<string, number>();
let viewTokens = 0;
for (const m of viewMessages) viewTokens += countMessageTokens(m);
for (const m of viewMessages) {
const t = countMessageTokens(m);
viewTokById.set(m.id, t);
viewTokens += t;
}
// This-fold ratio only: lastCompress, the degenerate-fold gate and the
// staged-compress note all judge ONE rewrite, never the session (#4227).
const shrinkRatio = viewTokens > 0 ? Math.min(1, r.tokensCompressed / viewTokens) : 0;
const foldPoint = [...ranges].sort((a, b) => refNum(a.startRef) - refNum(b.startRef))[0]?.startRef ?? "unknown";
ctx.session.lastCompress = { at: Date.now(), shrinkRatio, foldPoint, blocks: r.blocksCreated, tokensCompressed: r.tokensCompressed };
Expand All @@ -1117,9 +1126,27 @@ export function applyRanges(parsed: ReturnType<typeof parseCompressInput>, ctx:
// upstream eviction.
const anchorTok = res.state.blocks.reduce((n, b) => n + (b.active ? Math.ceil(b.summary.length / 4) : 0), 0);
const activeBlockCount = res.state.blocks.filter((b) => b.active).length;
const postCtx = Math.max(0, viewTokens - r.tokensCompressed);
// #4227: postCtx is the RENDERED post-fold context — raw view minus what
// EVERY active block covers (union, so overlapping merged blocks count
// once), not "raw view minus this fold". The old value ignored all
// earlier blocks' net savings: with 86+ active blocks it read as the
// full resend mass (#4224: postCtx≈14.5M while most of it was already
// covered) and printed "shrink 0%" + "ceiling ≥0%" for a heavily
// compressed session. Single-block sessions are byte-identical to the
// old value (union == this fold); re-entering anchors stay reported
// separately as anchor≈ below, so nothing double-counts.
const coveredIds = new Set<string>();
for (const b of res.state.blocks) if (b.active) for (const id of b.effectiveMessageIds) coveredIds.add(id);
let coveredTok = 0;
for (const id of coveredIds) {
const t = viewTokById.get(id);
if (t !== undefined) coveredTok += t;
}
const postCtx = Math.max(0, viewTokens - coveredTok);
const cumSaved = Math.max(0, viewTokens - postCtx);
const cumShrinkRatio = viewTokens > 0 ? Math.min(1, cumSaved / viewTokens) : 0;
const ceiling = postCtx > 0 ? Math.floor((100 * anchorTok) / postCtx) : 0;
ctx.log(`[acp-compress-obs] shrink ${Math.round(shrinkRatio * 100)}% (~${r.tokensCompressed}/${viewTokens} tok) foldPoint=${foldPoint} blocks=${r.blocksCreated} anchor≈${anchorTok} tok (${activeBlockCount} active blocks, sys excluded) postCtx≈${postCtx} → next-request cache ceiling ≥${ceiling}%`);
ctx.log(`[acp-compress-obs] shrink ${Math.round(shrinkRatio * 100)}% this-fold (~${r.tokensCompressed}/${viewTokens} tok) · cumulative ${Math.round(cumShrinkRatio * 100)}% (~${cumSaved}/${viewTokens} tok) foldPoint=${foldPoint} blocks=${r.blocksCreated} anchor≈${anchorTok} tok (${activeBlockCount} active blocks, sys excluded) postCtx≈${postCtx} → next-request cache ceiling ≥${ceiling}%`);
// #1911: a fold covering most of the live context is a degenerate reset —
// legitimate as a marathon-session strategy, but it rewrites the whole
// prefix and leaves only the anchor summaries. Hosts need a
Expand Down
Loading
Loading