diff --git a/src/stream.ts b/src/stream.ts index a0d1e1681c..5fac31245b 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -2,7 +2,7 @@ import { createHash, randomBytes } from "node:crypto"; import { activeBlockSpans, collectBlockContent, countMessageTokens, defaultCountTokens, formatRanges, hideConsumedCompressCalls, storeCoveredOriginals, viableRanges, type CompressionCore, type Config, type CoreMessage, type CompressionState, type NudgeDecision, type CompressParseDiagnostics } from "acp-kernel"; import { excludeDeadRefRanges, handleAcpStatus, liveCompressionRanges } from "./acp-status.js"; import { handleAcpCache, recordCacheFoldsFromBlocks } from "./cache-ledger.js"; -import { type Session, cacheBlockContent, markDirty, scrubTurnSeparatorIds, statusInputBaseline } from "./session.js"; +import { type Session, cacheBlockContent, markDirty, scrubTurnSeparatorIds, setPostRebuildAnchor, statusInputBaseline } from "./session.js"; import { COMPRESS_TOOL_NAME, parseCompressInput, ABSORB_TOOL_NAME, type ParsedRange } from "./compress-tool.js"; import { effectiveAbsorbConfig, executeAbsorb, isProxyToolFor } from "./absorb.js"; import { executeSearchContextTarget, resolveDecompress } from "./decompress-shared.js"; @@ -983,6 +983,25 @@ export function applyRanges(parsed: ReturnType, ctx: 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); + // #3573: a fold that out-sizes the session meter (stale baseline after + // an upgrade cutover, a never-reporting upstream, or simply the first + // fold of a raw-replayed giant history) drives the #695 net below zero + // and clamps it to 0 — the meter baseline is annihilated and the next + // prepare falls through the effectiveTokenCount ladder to the + // resent-history char-count bound, which over-counts 2-4x and fires a + // phantom EMERGENCY nudge / preflight marathon into an ALREADY-folded + // context (observed: usage=289% right after a successful 1.9M-token + // fold, 564% on the field report). Anchor the meter to this fold's + // post-fold view size (same kernel-count space the shrink line + // reports) — the #1820 post-rebuild semantics extended from preflight + // rebuilds to plain folds: a real usage report supersedes it + // immediately and the bounded prepare budget falls back to legacy + // sizing for never-reporting upstreams. + // Degenerate full-coverage folds (marathon strategy, #1911) can leave + // postCtx === 0; the setter no-ops on <=0 and the ghost decision would + // survive one more prepare. Anchor to 1 instead — the preflight fit + // gate stays the hard backstop either way. + setPostRebuildAnchor(ctx.session, Math.max(1, postCtx)); 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}%`); // #1911: a fold covering most of the live context is a degenerate reset — diff --git a/tests/fork-ccr-adoption.test.ts b/tests/fork-ccr-adoption.test.ts index 66617304f8..4bdb1322e1 100644 --- a/tests/fork-ccr-adoption.test.ts +++ b/tests/fork-ccr-adoption.test.ts @@ -303,11 +303,17 @@ test("child persists its own companion and survives a proxy restart (#1341)", () const child = forkChild(armed, forkBody); assert.equal(contentStoreOf(child).byRef["m00003"] ? true : false, true); + // #3586: a fold commit now persists the FOLDED session's own artifacts + // (setPostRebuildAnchor -> markDirty -> synchronous content-store write + // inside scheduleSave), so the armed parent legitimately leaves its + // own companion behind before we get here. Companion files are named + // after the (hashed) envelope path, not the session id — so pin the + // FLUSH DELTA instead of the absolute count: exactly one NEW companion + // appears for the child. + const before = listFiles(dir).filter((f) => f.endsWith(".content-store.json")).length; assert.ok(getStore().flushSync(child), "flush wrote the child's records"); - // Companion files are named after the (hashed) envelope path, not the - // session id; only the child was flushed with a dirty store here. const envelopes = listFiles(dir).filter((f) => f.endsWith(".content-store.json")); - assert.equal(envelopes.length, 1, "exactly one companion envelope for the child"); + assert.equal(envelopes.length - before, 1, "the child's flush adds exactly one companion envelope"); assert.equal(dropSessionForGc(child.id), true, "evict from memory (restart)"); const reloaded = getSession(child.id, META); diff --git a/tests/issue3573-fold-anchor-ghost-meter.test.ts b/tests/issue3573-fold-anchor-ghost-meter.test.ts new file mode 100644 index 0000000000..065df98cab --- /dev/null +++ b/tests/issue3573-fold-anchor-ghost-meter.test.ts @@ -0,0 +1,150 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import type { CoreMessage } from "acp-kernel"; +import { createCore, createInitialState, assignRefs, emptyRefMap, defaultConfig } from "acp-kernel"; +import { postRebuildAnchorTokens } from "../src/session.ts"; +import { effectiveTokenCount } from "../src/server.ts"; +import { estimateCoreMessagesUpper } from "../src/preflight.ts"; +import { parseCompressInput } from "../src/compress-tool.ts"; +import { applyRanges, type RewriteCtx } from "../src/stream.ts"; +import type { Session } from "../src/session.ts"; + +// #3573: a fold that out-sizes the session meter (stale baseline after an +// upgrade cutover, a never-reporting upstream, or the first fold of a +// raw-replayed giant history) drives the #695 credit net below zero and clamps +// the baseline to 0. With the baseline annihilated, the next prepare falls +// through the effectiveTokenCount ladder to the resent-history char-count +// bound — 2-4x the real token mass — so a session that JUST folded 1.9M tokens +// meters usage=289% (field: 564%) and fires a phantom EMERGENCY nudge / a +// preflight marathon into an already-folded context. Fix: plain folds now +// stamp the #1820 post-rebuild anchor with the fold's post-fold view size +// (same kernel-count space the [acp-compress-obs] shrink line reports), so +// the meter rides the anchor until a real usage report supersedes it. + +const WINDOW = 200_000; + +function makeSession(): Session { + return { + id: "issue3573-fold-anchor", + meta: {}, + stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 0, compressCreditTokens: 0, contextTokens: 0, retrieveCalls: 0, retrieveHits: 0, retrieveMisses: 0, storedBytes: 0, storeBytesSaved: 0, rangeRestores: 0 }, + metadata: {}, + state: createInitialState(), + createdAt: Date.now(), + lastSeen: Date.now(), + blockContents: new Map(), + inFlight: 0, + persisted: false, + pendingRetrievals: [], + }; +} + +function textMsg(id: string, role: "user" | "assistant", text: string): CoreMessage { + return { id, role, contentType: "text", text }; +} + +// ~430K chars across 7 messages: the two oldest (folded away) carry the bulk, +// the surviving five are modest. Char-bound raw ≈ 430K ≈ 215% of the window — +// exactly the ghost shape; the post-fold remainder is ~40K ≈ 20%. +function makeCtx(): RewriteCtx { + const messages = [ + textMsg("raw_1", "user", "x".repeat(200_000)), + textMsg("raw_2", "assistant", "x".repeat(200_000)), + textMsg("raw_3", "user", "y".repeat(6_000)), + textMsg("raw_4", "assistant", "y".repeat(6_000)), + textMsg("raw_5", "user", "y".repeat(6_000)), + textMsg("raw_6", "assistant", "y".repeat(6_000)), + textMsg("raw_7", "user", "y".repeat(6_000)), + ]; + const session = makeSession(); + const res = assignRefs(messages, { existing: emptyRefMap(), nextIndex: 0 }); + session.state.messageRefs = res.map; + return { core: createCore(), config: defaultConfig(WINDOW), messages, session, log: () => {} }; +} + +const COMPRESS_ARGS = { content: [{ startId: "m00001", endId: "m00002", summary: "ISSUE3573-SUMMARY-PAYLOAD-LONG-ENOUGH-FOR-THE-KERNEL-MIN-LENGTH-CHECK" }] }; + +test("#3573: a fold that out-sizes the meter anchors the next read to the post-fold view, not the char-count ghost", () => { + const ctx = makeCtx(); + // The ghost shape: a stale estimate-grade baseline far below the fold's + // mass, no usage-grade sample anywhere (never-reporting upstream / + // cutover), no per-turn local estimate yet. + ctx.session.stats.lastInputTokens = 11_579; + ctx.session.stats.lastInputTokensSource = "estimate"; + + const out = applyRanges(parseCompressInput(COMPRESS_ARGS), ctx); + const text = typeof out === "string" ? out : out.text; + assert.ok(text.startsWith("[Compressed"), `fold succeeded (got: ${text})`); + + // The #695 credit net clamps the annihilated baseline to 0 — the mechanism + // that pre-fix dropped the ladder through to the raw char bound. + assert.equal(ctx.session.stats.lastInputTokens, 0, "stale baseline netted to 0 by the fold credit"); + + const anchor = postRebuildAnchorTokens(ctx.session); + assert.ok(anchor > 0, "the fold stamped the post-rebuild anchor"); + const raw = estimateCoreMessagesUpper(ctx.messages); + assert.ok(anchor * 4 < raw, `anchor is the post-fold remainder in kernel-count space (anchor=${anchor}, raw=${raw})`); + + // The very next prepare (full raw RESENT history, usage report not landed + // yet) must meter against the anchor — pre-fix this read returned `raw` + // (≈215% of the window) and lit the EMERGENCY branch into the folded context. + const meter = effectiveTokenCount(ctx.session, ctx.messages); + assert.equal(meter.tokens, anchor, "meter rides the post-fold anchor"); + assert.equal(meter.source, "estimate", "anchor reads as estimate-grade"); + assert.ok(meter.tokens < WINDOW, `folded context meters inside the window (got ${meter.tokens}/${WINDOW}, pre-fix ghost was ${raw})`); +}); + +test("#3573: a STALE per-turn local estimate (the field's 564% shape) is outranked by the anchor", () => { + const ctx = makeCtx(); + ctx.session.stats.lastInputTokens = 11_579; + ctx.session.stats.lastInputTokensSource = "estimate"; + // The field signature: localInputEstimate lags one turn (#1820 lag note), so + // at decision time it still holds the PREVIOUS turn's raw-resent char mass — + // ~430K here, ≈215% of the window. Pre-fix the ladder's min(est, raw) branch + // returned exactly this ghost and lit EMERGENCY on the freshly-folded session. + ctx.session.stats.localInputEstimate = 430_000; + + const out = applyRanges(parseCompressInput(COMPRESS_ARGS), ctx); + const text = typeof out === "string" ? out : out.text; + assert.ok(text.startsWith("[Compressed"), `fold succeeded (got: ${text})`); + + const anchor = postRebuildAnchorTokens(ctx.session); + assert.ok(anchor > 0, "the fold stamped the post-rebuild anchor"); + const meter = effectiveTokenCount(ctx.session, ctx.messages); + assert.equal(meter.tokens, anchor, "anchor branch outranks the stale min(est, raw) ghost branch"); + assert.ok(meter.tokens < WINDOW, `folded context meters inside the window (got ${meter.tokens}/${WINDOW}, pre-fix ghost was 430000)`); +}); + +test("#3573: a degenerate full-coverage fold still stamps an anchor (postCtx=0 edge)", () => { + const ctx = makeCtx(); + ctx.config.preserveRecentMessages = 0; + ctx.config.preserveRecentTokens = 0; + ctx.config.compress.minCompressRange = 0; + ctx.config.compress.minSummaryLength = 0; + ctx.session.stats.lastInputTokens = 11_579; + ctx.session.stats.lastInputTokensSource = "estimate"; + + const out = applyRanges(parseCompressInput({ content: [{ startId: "m00001", endId: "m00007", summary: "ISSUE3573-DEGENERATE-FULL-COVERAGE-SUMMARY-LONG-ENOUGH-FOR-MIN-LENGTH" }] }), ctx); + const text = typeof out === "string" ? out : out.text; + assert.ok(text.startsWith("[Compressed"), `degenerate fold succeeded (got: ${text})`); + + // tokensCompressed covers the whole view -> postCtx === 0 -> the setter's + // <=0 no-op would leave NO anchor and one ghost decision would survive. + assert.ok(postRebuildAnchorTokens(ctx.session) > 0, "anchor stamped even when the fold leaves nothing behind"); +}); + +test("#3573: a usage-grade baseline still outranks the post-fold anchor (no behavior change for healthy sessions)", () => { + const ctx = makeCtx(); + ctx.session.stats.lastInputTokens = 300_000; + ctx.session.stats.lastInputTokensSource = "usage"; + + const out = applyRanges(parseCompressInput(COMPRESS_ARGS), ctx); + const text = typeof out === "string" ? out : out.text; + assert.ok(text.startsWith("[Compressed"), `fold succeeded (got: ${text})`); + // The credit nets the usage-grade baseline down; the anchor is stamped too, + // but branch #1 of the ladder wins — same precedence as #1820. + assert.ok(ctx.session.stats.lastInputTokens > 0, "usage-grade baseline survived the netting (300k exceeds the folded mass)"); + const meter = effectiveTokenCount(ctx.session, ctx.messages); + assert.equal(meter.tokens, ctx.session.stats.lastInputTokens, "usage-grade fast path wins over the anchor"); + assert.equal(meter.source, "usage", "usage-grade source preserved"); +});