From cf80a1eb06c80e0f28038658d8aac2cc710e7a92 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Sat, 17 Oct 2026 22:10:50 +0800 Subject: [PATCH 1/3] =?UTF-8?q?fix:=20stamp=20the=20#1820=20post-rebuild?= =?UTF-8?q?=20anchor=20on=20plain=20folds=20=E2=80=94=20the=20post-fold=20?= =?UTF-8?q?meter=20no=20longer=20ghosts=20>100%=20(#3573)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 report: 564%) and fires a phantom EMERGENCY nudge / a preflight marathon into an already-folded context. Plain folds now stamp the post-rebuild anchor with the fold's post-fold view size (postCtx, same kernel-count space the [acp-compress-obs] shrink line reports) — the exact #1820 post-rebuild semantics extended from preflight rebuilds to tool-executed folds. A real usage report supersedes it immediately (settleUsageReport clears it) and the bounded prepare budget (3) falls back to legacy sizing for never-reporting upstreams, so no behavior changes for healthy sessions. Fixes #3573 --- src/stream.ts | 17 ++- .../issue3573-fold-anchor-ghost-meter.test.ts | 111 ++++++++++++++++++ 2 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 tests/issue3573-fold-anchor-ghost-meter.test.ts diff --git a/src/stream.ts b/src/stream.ts index a0d1e1681c..2fa194724b 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,21 @@ 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. + setPostRebuildAnchor(ctx.session, 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/issue3573-fold-anchor-ghost-meter.test.ts b/tests/issue3573-fold-anchor-ghost-meter.test.ts new file mode 100644 index 0000000000..30b112968b --- /dev/null +++ b/tests/issue3573-fold-anchor-ghost-meter.test.ts @@ -0,0 +1,111 @@ +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 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"); +}); From 72bb201ec0dc291aa63b2aec91c5f5f07c01a1c2 Mon Sep 17 00:00:00 2001 From: awork Date: Sat, 17 Oct 2026 22:25:14 +0800 Subject: [PATCH 2/3] fix: #3573 anchor zero-edge + stale-estimate field-shape regression (review supplement) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two review supplements on the ghost-meter fix, same failure domain: - Degenerate full-coverage folds (#1911 marathon strategy) leave postCtx === 0; setPostRebuildAnchor no-ops on <=0 so no anchor was stamped and one ghost decision survived. Clamp to Math.max(1, postCtx) — the preflight fit gate stays the hard backstop. - The field's 564% shape traveled the min(staleEst, raw) ladder branch via a one-turn-lagged localInputEstimate; the original suite only covered the est=0 blind case. Add a stale-estimate test (anchor branch outranks it) and a full-coverage fold test pinning the zero-edge. --- src/stream.ts | 6 ++- .../issue3573-fold-anchor-ghost-meter.test.ts | 39 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/stream.ts b/src/stream.ts index 2fa194724b..5fac31245b 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -997,7 +997,11 @@ export function applyRanges(parsed: ReturnType, ctx: // 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. - setPostRebuildAnchor(ctx.session, postCtx); + // 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/issue3573-fold-anchor-ghost-meter.test.ts b/tests/issue3573-fold-anchor-ghost-meter.test.ts index 30b112968b..065df98cab 100644 --- a/tests/issue3573-fold-anchor-ghost-meter.test.ts +++ b/tests/issue3573-fold-anchor-ghost-meter.test.ts @@ -94,6 +94,45 @@ test("#3573: a fold that out-sizes the meter anchors the next read to the post-f 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; From 280186d561f1d4e031c9e41e9f469dba82c128f3 Mon Sep 17 00:00:00 2001 From: awork Date: Sat, 17 Oct 2026 23:12:47 +0800 Subject: [PATCH 3/3] =?UTF-8?q?test(#3586=20review):=20pin=20fork-ccr=20co?= =?UTF-8?q?mpanion=20FLUSH=20DELTA=20=E2=80=94=20#3586=20fold=20commits=20?= =?UTF-8?q?now=20persist=20the=20folded=20session's=20own=20artifact=20(#1?= =?UTF-8?q?341)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/fork-ccr-adoption.test.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) 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);