From 8b09dd1077b2c7c79fc2967dbf712b16090693e4 Mon Sep 17 00:00:00 2001 From: awork Date: Fri, 23 Oct 2026 01:50:55 +0800 Subject: [PATCH 1/3] =?UTF-8?q?fix(#4331):=20name=20uncovered=20mid-histor?= =?UTF-8?q?y=20runs=20=E2=80=94=20acp=5Fstatus=20section=20+=20honest=20mi?= =?UTF-8?q?nimum=20verdict?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit "no compressible ranges remain — the context is already at its minimum" fired on offer exhaustion alone, with no global coverage audit anywhere: live sessions carried ~16% of context as mid-history refs outside every active block while the receipt declared the global minimum (#4331). - src/uncovered.ts: uncoveredHistoryRuns() — contiguous runs outside every active block's coverage, classified by an empirical protected-zone diff (two processTurn passes: normal config vs preserve-0/0 relaxed, over a cloned state), current-offer exclusion, VIABLE_RANGE_MIN_TOKENS floor. Advisory only: any throw degrades to "no runs found". - acp_status: additive UNCOVERED HISTORY section (top-5 runs by tokens, rest counted), filtered against the live Compressible-ranges list above it; names isolated sub-gate residue between covered blocks that no merge will ever fold. - postCompressTail: on clean whole-offer exhaustion with no tier distillation, run the audit first — uncovered mass -> honest "not at its minimum" text naming the runs; none -> the verbatim #1387 contract string, byte-stable for genuinely-drained sessions. Behavior change (disclosed): old = unconditional minimum claim on offer exhaustion; new = claim only when the global audit verifies it, honest uncovered-listing otherwise. Sub-gate residue became visible in acp_status (was invisible on every surface). No config/env surface added; kernel untouched; nudge prompt text untouched. Known limitation (separate observation, not fixed here): the receipt's remaining-calc removes a whole offered batch when any submitted span overlaps it, so a partially-folded merged batch can under-report. Tests: tests/issue4331-uncovered-history.test.ts (9 gates incl. negative verbatim-contract + tier precedence); updated pinned contracts in tests/compress-success-tail.test.ts (#2404/#1387 now pin "never leaks AS AN OFFER"), tests/issue2029-status-baseline-provenance.test.ts (every status-reader kernel call feeds the provenance baseline), tests/issue847-compress-failure-guards.test.ts (sub-gate residue visible in the diagnostic, still never offered). TECHNICAL-NOTES.md entry appended. --- TECHNICAL-NOTES.md | 14 ++ src/acp-status.ts | 27 +++ src/stream.ts | 26 +++ src/uncovered.ts | 179 ++++++++++++++ tests/compress-success-tail.test.ts | 42 ++-- ...sue2029-status-baseline-provenance.test.ts | 7 +- tests/issue4331-uncovered-history.test.ts | 220 ++++++++++++++++++ .../issue847-compress-failure-guards.test.ts | 6 +- 8 files changed, 504 insertions(+), 17 deletions(-) create mode 100644 src/uncovered.ts create mode 100644 tests/issue4331-uncovered-history.test.ts diff --git a/TECHNICAL-NOTES.md b/TECHNICAL-NOTES.md index 07359b58a7..b5ac86d52a 100644 --- a/TECHNICAL-NOTES.md +++ b/TECHNICAL-NOTES.md @@ -475,3 +475,17 @@ Settled design = **stamp-by-default + empirical self-heal (probe-and-remember)** - Modes: only the plugin lane (registered agent / dsh plugin, internal conversation header present) ever stamps; plain proxy mode stays pure passthrough in both directions, byte-for-byte unchanged. Tests: `tests/issue2645-pck-probe.test.ts` (strict upstream self-heals turn 1, remembers turn 2, lenient pool never probes, poisoned upstream forwards the 400 and learns nothing, client-owned keys drop after ablation, model-granular pairs) + the rewritten matrix in `tests/issue2218-relay-affinity-signal.test.ts`. + +## Uncovered-history audit — honest minimum verdict (#4331) + +The "no compressible ranges remain — the context is already at its minimum" stop signal was structurally unverifiable: it fired whenever the PRE-FOLD OFFER was exhausted (`postCompressTail`, #2404 pinning), but nothing ever compared the union of active-block ranges against the session's ref set. A live ZCode session showed the failure at scale — 138 mid-history refs (~65K tok, ~16% of context) in no active block, largest run `m00312`–`m00411` (100 msgs), while the receipt declared the global minimum three times in a row. The kernel DOES find such spans when they are offered (verified by repro on master); what was missing was a GLOBAL coverage audit plus a verdict that only claims "minimum" when one holds. + +Design (`src/uncovered.ts`, host-side only, no kernel change): + +- `uncoveredHistoryRuns()`: walk the message array for entries outside every active block's coverage (the `collectVisible` predicate), group contiguous runs, then classify each run: skip if any current offer covers part of it (the model can act on it now); otherwise diff TWO `core.processTurn` passes over a `structuredClone(state)` — normal config vs relaxed `{preserveRecentMessages: 0, preserveRecentTokens: 0}` — and treat refs offered only in the relaxed pass as zone-hidden (inside the protected recent window). The empirical zone diff deliberately re-uses the kernel's own protected-zone rules instead of copying them, so it self-maintains as those rules evolve (over-approximation errs toward under-reporting). Runs below `VIABLE_RANGE_MIN_TOKENS` (200) stay silent. Whole function is advisory: any throw degrades to "no runs found" and never breaks receipts or status. +- **acp_status** gains an additive `UNCOVERED HISTORY` section (top-5 runs by tokens, rest counted) listing mass that sits outside every active block AND is absent from the live Compressible-ranges list above — including isolated sub-gate runs between covered blocks that no merge will ever fold (the permanent-hole class of #4331 / #847). Named for visibility, explicitly not as fold suggestions; rows drill down via `scope:"uncompressed"` unchanged. +- **postCompressTail**: on clean whole-offer exhaustion with no tier distillation, the audit runs before the verdict. Runs found → honest text naming them ("not at its minimum", pointer to acp_status). None → the verbatim #1387 contract string fires byte-stable, as before for genuinely-drained sessions. + +Behavior change (disclosed): old = unconditional "already at its minimum" on offer exhaustion; new = that claim only when the global audit verifies it, honest uncovered-listing otherwise. Sub-gate residue became visible in acp_status (previously invisible on every surface). No config/env surface added; nudge prompt text untouched (kernel-owned). Known limitation left as a separate observation: the receipt's remaining-calc removes a WHOLE offered batch when any submitted span overlaps it, so a partially-folded merged batch can under-report rather than over-report. + +Tests: `tests/issue4331-uncovered-history.test.ts` (hole session → honest verdict + named span; drained session → verbatim contract string; sub-floor silence; tier precedence; both acp_status directions; formatter caps) + updated pinned contracts in `tests/compress-success-tail.test.ts` (#2404/#1387 now pin "never leaks AS AN OFFER"), `tests/issue2029-status-baseline-provenance.test.ts` (every status-reader kernel call feeds the provenance baseline), `tests/issue847-compress-failure-guards.test.ts` (sub-gate residue visible in the diagnostic, still never offered). diff --git a/src/acp-status.ts b/src/acp-status.ts index 784aa6aaf3..dbb790f8c0 100644 --- a/src/acp-status.ts +++ b/src/acp-status.ts @@ -19,6 +19,7 @@ import { pckAblations } from "./pck-probe.js"; import { getUnrecognizedPathStats } from "./server/observability.js"; import { ccrEnabled, ccrLoopConfig, contentStoreOf } from "./store.js"; import { coveredRefSpan } from "./decompress-shared.js"; +import { describeUncoveredRuns, shortTokenCount, uncoveredHistoryRuns } from "./uncovered.js"; import { preCompactionArchiveOf, statusInputBaseline, type Session } from "./session.js"; import { compressBreakerDetail, compressLastFailureCause } from "./stream.js"; import { describeAdvisory, getAdvisoryState } from "./advisory.js"; @@ -242,10 +243,12 @@ export function handleAcpStatus(args: Record, ctx: AcpStatusCtx const lastCause = compressLastFailureCause(ctx.session); const staleRef = lastCause !== undefined && lastCause.startsWith("stale-ref"); const substrateDestroyed = lastCause !== undefined && lastCause.startsWith("substrate-destruction"); + let offeredSpans: Array<{ startRef: string; endRef: string }> | undefined; try { const live = liveCompressionRanges(ctx); if (live) { const { nudge, ranges } = live; + offeredSpans = ranges; extra.push(""); extra.push(nudge.shouldInject ? `Nudge: ACTIVE — ${nudge.reason}` : `Nudge: idle — ${nudge.reason}`); // #2366: high billed pressure + exhausted compressible mass is the @@ -275,6 +278,30 @@ export function handleAcpStatus(args: Record, ctx: AcpStatusCtx } catch { // Base-only report; never fall back to a stale snapshot. } + // #4331: uncovered history — contiguous runs outside every active block AND + // outside the live offer above (older than the protected recent window). + // They keep going upstream raw while no surface names them; additive + // section, rendered only when non-trivial. Advisory: any failure skips it. + try { + const runs = uncoveredHistoryRuns({ + core: ctx.core, + messages: ctx.messages, + state: ctx.session.state, + config: ccrLoopConfig(ctx.session, ctx.config), + tokenCount: billed, + contentStore: contentStoreOf(ctx.session), + applyView: ctx.compressMessages, + actionableSpans: offeredSpans ?? [], + }); + if (runs.length > 0) { + const totalMsgs = runs.reduce((s, r) => s + r.count, 0); + const totalTok = runs.reduce((s, r) => s + r.tokens, 0); + extra.push(""); + extra.push(`UNCOVERED HISTORY — ${totalMsgs} msg(s) across ${runs.length} run(s), ${shortTokenCount(totalTok)} tok, sit outside every active block and are absent from the Compressible-ranges list above (older than the protected recent window): ${describeUncoveredRuns(runs)}. They keep going upstream raw (runs may sit below the fold-size gate — named for visibility, not as fold suggestions); inspect their rows with scope:"uncompressed".`); + } + } catch { + // Advisory section; never break the status report. + } // #2362: dead refs — known to the session but no longer backed by any // visible or folded message (client history rewrite / host-native // compaction). Failure receipts point the model at acp_status, so this is diff --git a/src/stream.ts b/src/stream.ts index 260029ccb3..73776f245d 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -18,6 +18,7 @@ import { attachSubagentSessions, subagentSessionNote, subagentSessionsOf, syncSu import { METADATA_DRIFT_ESCALATED } from "./fold-reconcile.js"; import { safePrefix, safeSuffix, scrubLoneSurrogates } from "./text-safe.js"; import { applyConfiguredCompression } from "./external-summary-compress.js"; +import { describeUncoveredRuns, shortTokenCount, uncoveredHistoryRuns } from "./uncovered.js"; export type RewriteCtx = { core: CompressionCore; @@ -605,6 +606,31 @@ function postCompressTail(ctx: RewriteCtx, cleanSuccess: boolean, submitted: Arr // Partial failures stay silent too: the model still owes the errors an answer // before any "you are done" verdict (pi #521 gate). if (!cleanSuccess || tier !== null) return ""; + // #4331: offer exhaustion is NOT proof the context is at its minimum — the + // verdict only sees this call's pre-fold offer, and mid-history runs can sit + // outside every active block AND outside the offer (disjoint folds, dead-ref + // holes, integrity withdrawals). Audit before making the global claim; the + // verbatim contract string (#1387) stays intact for genuinely-drained + // sessions. Advisory only: audit failure degrades to the old wording path. + try { + const runs = uncoveredHistoryRuns({ + core: ctx.core, + messages: ctx.messages, + state: ctx.session.state, + config: ccrEnabled(ctx.session) ? ctx.config : { ...ctx.config, ccr: undefined }, + tokenCount: statusInputBaseline(ctx.session), + contentStore: contentStoreOf(ctx.session), + applyView: ctx.compressMessages, + actionableSpans: remaining, + }); + if (runs.length > 0) { + const totalMsgs = runs.reduce((s, r) => s + r.count, 0); + const totalTok = runs.reduce((s, r) => s + r.tokens, 0); + return `\n\nUncovered history remains — all offered ranges are folded, but ${totalMsgs} msg(s) across ${runs.length} run(s), ${shortTokenCount(totalTok)} tok, sit outside every active block and are not in this call's offer: ${describeUncoveredRuns(runs)}. The context is not at its minimum (runs may sit below the fold-size gate — named for visibility, not as fold suggestions); inspect with acp_status(scope:"uncompressed").`; + } + } catch { + // Fall through to the contract wording below. + } return `\n\n${NO_RANGES_REMAIN_TEXT}`; } diff --git a/src/uncovered.ts b/src/uncovered.ts new file mode 100644 index 0000000000..4398834f7d --- /dev/null +++ b/src/uncovered.ts @@ -0,0 +1,179 @@ +// Uncovered-history audit (#4331). The compress stop verdict and the +// acp_status overview both derive from the per-call OFFER (what this request's +// prepare pass advertised), so mid-history spans that sit outside every active +// block AND outside the current offer are invisible to both surfaces — the +// receipt can then claim "the context is already at its minimum" while tens of +// thousands of raw tokens keep going upstream. This module answers the one +// question neither surface could ask before: which contiguous runs of messages +// are uncovered by any active block, older than the protected recent window, +// and not in the actionable offer right now? +// +// Zone exclusion is computed EMPIRICALLY as the diff between two kernel offers +// (normal config vs preserve-0 config) instead of re-implementing +// computeProtectedRefs host-side: the kernel's zone rules (#2886 pair symmetry, +// neverPreserveRecentTools, …) evolve without host notice, and an empirical +// diff self-maintains. Both passes run on a structuredClone of the session +// state — processTurn is not idempotent in its nudge cadence stamps +// (nudge-inject rides lastNudgeShownTokens down and sets it on shouldInject), +// so the audit must never touch live state. The whole audit is advisory: any +// failure degrades to "no runs" rather than breaking receipts or status. + +import { + BLOCKED_REF, + VIABLE_RANGE_MIN_TOKENS, + coveredMessageIds, + countMessageTokens, + defaultCountTokens, + refForRaw, + type CompressionCore, + type CompressionState, + type Config, + type CoreMessage, + type MessageContentStore, +} from "acp-kernel"; + +export interface UncoveredRun { + startRef: string; + endRef: string; + count: number; + tokens: number; +} + +export interface UncoveredAuditInput { + core: CompressionCore; + /** Full resent log (apply class) — same array class prepare runs on. */ + messages: CoreMessage[]; + state: CompressionState; + config: Config; + /** statusInputBaseline — the token level the live-recompute lanes use. */ + tokenCount?: number; + contentStore?: MessageContentStore; + applyView?: CoreMessage[]; + /** Spans the model can act on right now (post-filter offer / remaining list). + * Runs overlapping these are NOT reported — they are surfaced elsewhere. */ + actionableSpans?: readonly { startRef: string; endRef: string }[]; +} + +interface RunEntry { + index: number; + ref: string; + num: number; + tokens: number; +} + +function refNum(ref: string): number { + const n = Number(ref.replace(/\D/g, "")); + return Number.isFinite(n) ? n : 0; +} + +type SpanWindow = readonly [number, number]; + +function windowsOf(spans: readonly { startRef: string; endRef: string }[]): SpanWindow[] { + const out: SpanWindow[] = []; + for (const s of spans) { + const a = refNum(s.startRef); + const b = refNum(s.endRef); + if (a > 0 && b > 0) out.push(a <= b ? [a, b] : [b, a]); + } + return out; +} + +function inAnyWindow(n: number, windows: readonly SpanWindow[]): boolean { + return windows.some(([lo, hi]) => n >= lo && n <= hi); +} + +export function uncoveredHistoryRuns(input: UncoveredAuditInput): UncoveredRun[] { + const { core, messages, state, config } = input; + + // 1) Uncovered entries — the same predicate report.collectVisible uses: + // numbered, not folded into an active block, carrying tokens. + const covered = coveredMessageIds(state); + const entries: RunEntry[] = []; + messages.forEach((msg, index) => { + const ref = refForRaw(state.messageRefs, msg.id); + if (!ref || ref === BLOCKED_REF) return; + if (covered.has(msg.id)) return; + const tokens = countMessageTokens(msg, defaultCountTokens); + if (tokens <= 0) return; + const num = refNum(ref); + if (num <= 0) return; + entries.push({ index, ref, num, tokens }); + }); + if (entries.length === 0) return []; + + // 2) Contiguous runs: array adjacency defines "contiguous" (what the wire + // actually carries next to itself); covered or zero-token neighbors break + // a run conservatively. + const runs: RunEntry[][] = []; + for (const e of entries) { + const last = runs[runs.length - 1]; + if (last && last[last.length - 1].index === e.index - 1) last.push(e); + else runs.push([e]); + } + + // 3) Empirical zone diff. Cloned state: see header note on cadence stamps. + let normalWindows: SpanWindow[] = []; + let relaxedWindows: SpanWindow[] = []; + try { + const clone = structuredClone(state); + const base = { + messages, + tokenCount: input.tokenCount ?? 0, + renderTags: "none" as const, + contentStore: input.contentStore, + applyView: input.applyView, + }; + normalWindows = windowsOf( + core.processTurn({ ...base, state: clone, config }).nudge?.compressibleRanges ?? [], + ); + relaxedWindows = windowsOf( + core + .processTurn({ + ...base, + state: clone, + config: { ...config, preserveRecentMessages: 0, preserveRecentTokens: 0 }, + }) + .nudge?.compressibleRanges ?? [], + ); + } catch { + return []; + } + + const actionable = windowsOf(input.actionableSpans ?? []); + + // 4) Per-run classification. A ref is zone-hidden when the relaxed pass + // reaches it but the normal pass does not — i.e. only the protected + // recent window keeps it off the offer. Refs in NEITHER window are + // structurally unoffered (dead-ref holes, checkpoint carriers, integrity + // withdrawals, …) and stay reported: that is exactly the mass the user + // cannot fold through ordinary ranges and needs to see. Over-approximate + // window membership errs toward under-reporting, never toward claiming a + // protected tail message is leakable mass. + const out: UncoveredRun[] = []; + for (const run of runs) { + if (run.some((e) => inAnyWindow(e.num, actionable))) continue; + const kept = run.filter((e) => !(inAnyWindow(e.num, relaxedWindows) && !inAnyWindow(e.num, normalWindows))); + if (kept.length === 0) continue; + const tokens = kept.reduce((sum, e) => sum + e.tokens, 0); + if (tokens < VIABLE_RANGE_MIN_TOKENS) continue; + out.push({ + startRef: kept[0].ref, + endRef: kept[kept.length - 1].ref, + count: kept.length, + tokens, + }); + } + return out; +} + +export function shortTokenCount(n: number): string { + if (n < 1000) return `${n}`; + const k = n / 1000; + return `~${k >= 10 ? Math.round(k) : Math.round(k * 10) / 10}K`; +} + +export function describeUncoveredRuns(runs: UncoveredRun[], maxListed = 5): string { + const listed = runs.slice(0, maxListed).map((r) => `${r.startRef}–${r.endRef} (${r.count} msgs, ${shortTokenCount(r.tokens)} tok)`); + const rest = runs.length - listed.length; + return rest > 0 ? `${listed.join(", ")}, +${rest} more run(s)` : listed.join(", "); +} diff --git a/tests/compress-success-tail.test.ts b/tests/compress-success-tail.test.ts index 1d2ab243b2..7f2110770d 100644 --- a/tests/compress-success-tail.test.ts +++ b/tests/compress-success-tail.test.ts @@ -9,6 +9,10 @@ import { applyRanges, type RewriteCtx } from "../src/stream.ts"; // explicit stop signal (pi #521). Cross-host wording must stay verbatim. const RANGES_HEADER = "Current compressible ranges (use these refs exactly as listed):"; const NO_RANGES_REMAIN_TEXT = "No compressible ranges remain — the context is already at its minimum; continue the task without compressing."; +// #4331: honest replacement when offer exhaustion leaves material mass outside +// every active block and every offer — names the runs instead of claiming the +// global minimum. +const UNCOVERED_REMAINS_TEXT = "Uncovered history remains"; type Ctx = Omit & { log: (m: string) => void; logs: string[] }; @@ -75,22 +79,26 @@ test("#2404: folding a subset of the pre-fold offer keeps advertising the rest", assert.ok(!out.includes(NO_RANGES_REMAIN_TEXT), "stop signal must not fire while an offer remains:\n" + out); }); -test("#2404: a range protected at nudge time never leaks into the receipt (no zero-growth chained fold)", () => { +test("#2404: a range protected at nudge time never leaks into the receipt as an offer (no zero-growth chained fold)", () => { // THE regression: after the fold, m00005–m00007 are raw-compressible in LIVE // state (preserveRecentMessages: 2 keeps only m00008–m00009 protected), so a - // post-fold RECOMPUTE would list them and chain a second zero-growth fold. - // But they were NOT part of the pre-fold offer (still inside the protection - // window when the nudge fired) → pinning the tail to that offer keeps them - // out. A regression to the recompute path makes m00005 leak and fail here. + // post-fold RECOMPUTE would list them as an actionable offer and chain a + // second zero-growth fold. They were NOT part of the pre-fold offer → the + // tail must not advertise them under RANGES_HEADER. A regression to the + // recompute path makes an offer leak and fail here. + // #4331 (behavior change): such residue is no longer hidden behind a false + // "already at its minimum" claim — it is NAMED as uncovered history + // (diagnostic note with span, not a fold offer). const msgs = Array.from({ length: 9 }, (_, i) => textMsg(`raw_${i + 1}`, i % 2 === 0 ? "user" : "assistant", "x".repeat(5000))); const ctx = withRefs(makeCtx(msgs, { preserveRecentMessages: 2, preserveRecentTokens: 0 })); ctx.session.stats.lastInputTokens = 100000; setOffer(ctx, [offerRange("m00001", "m00004", 4)]); const out = runApply(ctx, { content: [{ startId: "m00001", endId: "m00004", summary: "TAIL-TEST-SUMMARY-PAYLOAD-LONG-ENOUGH-FOR-THE-KERNEL-MIN-LENGTH-CHECK" }] }); assert.ok(out.startsWith("[Compressed"), `expected success, got: ${out.slice(0, 120)}`); - assert.ok(!out.includes(RANGES_HEADER), "no ranges may be advertised once the whole offer folded:\n" + out); - assert.ok(!out.includes("m00005"), "a not-yet-offered (protected-at-nudge) range must NOT leak:\n" + out); - assert.ok(out.includes(NO_RANGES_REMAIN_TEXT), "stop signal fires once every offered range folded:\n" + out); + assert.ok(!out.includes(RANGES_HEADER), "no actionable ranges may be advertised once the whole offer folded:\n" + out); + assert.ok(out.includes(UNCOVERED_REMAINS_TEXT), "residual mass outside every block must be named (#4331):\n" + out); + assert.ok(out.includes("m00005") && out.includes("m00007"), "the residual run must be spanned in the uncovered note:\n" + out); + assert.ok(!out.includes(NO_RANGES_REMAIN_TEXT), "minimum claim must not fire while uncovered mass exists (#4331):\n" + out); }); test("#2404: folding every offered range emits the stop signal", () => { @@ -244,17 +252,20 @@ test("#1387: partial failure (some ranges rejected) keeps the old silent tail", assert.ok(!out.includes(RANGES_HEADER), "no ranges advertised on partial failure:\n" + out); }); -test("#1387: tail recompute respects the kernel's own eligibility (shouldInject=false leftovers are not advertised)", () => { +test("#1387: tail recompute respects the kernel's own eligibility (shouldInject=false leftovers are not advertised as offers)", () => { // #3058 moved preserveRecentMessages 5 → 3: under the default, folding // m00001–m00002 leaves m00003 outside the protection window, and the live // recompute returns a nudge DECISION STRUCT whose ranges list m00003 even // though the kernel itself judged it below threshold/floor (shouldInject // === false, reason "max compressible … < threshold …"). Advertising such - // ranges in the receipt re-nags at zero pressure — the disease #1387 - // (pi #521) fixed. The tail must honor shouldInject: sub-threshold - // leftovers never list; the stop signal fires instead. (The two tests + // ranges as an actionable offer in the receipt re-nags at zero pressure — + // the disease #1387 (pi #521) fixed. The tail must honor shouldInject: + // sub-threshold leftovers never appear under RANGES_HEADER. (The two tests // above pin preserveRecentMessages: 5 for their protection premise; this // one pins the default-config behavior the unpinned tests used to cover.) + // #4331 (behavior change): the residue is no longer papered over by a + // false "already at its minimum" claim — it is named as uncovered history + // (diagnostic note with span, not a fold offer). const msgs = [ textMsg("raw_1", "user", "x".repeat(20000)), textMsg("raw_2", "assistant", "x".repeat(20000)), @@ -268,9 +279,10 @@ test("#1387: tail recompute respects the kernel's own eligibility (shouldInject= ctx.session.stats.lastInputTokens = 80000; const out = runApply(ctx, { content: [{ startId: "m00001", endId: "m00002", summary: "TAIL-TEST-SUMMARY-PAYLOAD-LONG-ENOUGH-FOR-THE-KERNEL-MIN-LENGTH-CHECK" }] }); assert.ok(out.startsWith("[Compressed"), `expected success, got: ${out.slice(0, 120)}`); - assert.ok(!out.includes(RANGES_HEADER), "a sub-threshold leftover must not be advertised:\n" + out); - assert.ok(!out.includes("m00003"), "the below-threshold residue must stay unlisted:\n" + out); - assert.ok(out.includes(NO_RANGES_REMAIN_TEXT), "stop signal fires when the kernel itself would not nudge:\n" + out); + assert.ok(!out.includes(RANGES_HEADER), "a sub-threshold leftover must not be advertised as an offer:\n" + out); + assert.ok(out.includes(UNCOVERED_REMAINS_TEXT), "below-threshold residue must be named, not hidden (#4331):\n" + out); + assert.ok(out.includes("m00003"), "the residue ref must appear in the uncovered note:\n" + out); + assert.ok(!out.includes(NO_RANGES_REMAIN_TEXT), "minimum claim must not fire while uncovered mass exists (#4331):\n" + out); }); test("#1387: tail recompute still advertises ranges under real pressure (shouldInject=true)", () => { diff --git a/tests/issue2029-status-baseline-provenance.test.ts b/tests/issue2029-status-baseline-provenance.test.ts index 6d0b6b0f85..975bb559f2 100644 --- a/tests/issue2029-status-baseline-provenance.test.ts +++ b/tests/issue2029-status-baseline-provenance.test.ts @@ -104,7 +104,12 @@ test("#2029: acp_status nudge recompute feeds the provenance-aware baseline", () const statsBefore = snap(ctx.session.stats); const stateBefore = snap(ctx.session.state); const out = handleAcpStatus({}, ctx); - assert.deepStrictEqual(ctx.seen, [sc.expected], `${sc.name}: tokenCount handed to the kernel`); + // #4331 added the uncovered-history audit, which runs its own + // processTurn passes on this surface — the contract is that EVERY + // kernel call on a status reader feeds the provenance-aware baseline, + // not that there is exactly one call. + assert.ok(ctx.seen.length >= 1 && ctx.seen.every((v) => v === sc.expected), + `${sc.name}: every kernel call fed the provenance-aware baseline (got ${JSON.stringify(ctx.seen)})`); assert.equal(snap(ctx.session.stats), statsBefore, `${sc.name}: stats must stay read-only`); assert.equal(snap(ctx.session.state), stateBefore, `${sc.name}: state must stay read-only`); const nudgeLine = out.text.split("\n").find((l) => l.startsWith("Nudge:")); diff --git a/tests/issue4331-uncovered-history.test.ts b/tests/issue4331-uncovered-history.test.ts new file mode 100644 index 0000000000..028fb39d9d --- /dev/null +++ b/tests/issue4331-uncovered-history.test.ts @@ -0,0 +1,220 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import type { CompressibleRange, CoreMessage, NudgeDecision } from "acp-kernel"; +import { assignRefs, createCore, createInitialState, defaultConfig, emptyRefMap } from "acp-kernel"; +import { parseCompressInput } from "../src/compress-tool.ts"; +import { applyRanges, type RewriteCtx } from "../src/stream.ts"; +import { handleAcpStatus } from "../src/acp-status.ts"; +import { describeUncoveredRuns, shortTokenCount, uncoveredHistoryRuns } from "../src/uncovered.ts"; + +// #4331: "no compressible ranges remain" fired while large mid-history spans sat +// outside every active block AND every offer. Positive gates: the stop verdict +// must audit uncovered runs before claiming the global minimum, and acp_status +// must name them. Negative gates: genuinely-drained sessions keep the verbatim +// #1387 contract string byte-stable, sub-floor residue stays silent, and tier +// distillation precedence is untouched. +const NO_RANGES_REMAIN_TEXT = "No compressible ranges remain — the context is already at its minimum; continue the task without compressing."; +const UNCOVERED_HEADER = "Uncovered history remains"; +const STATUS_SECTION = "UNCOVERED HISTORY"; +const SUMMARY = "SUMMARY-PAYLOAD-LONG-ENOUGH-FOR-THE-KERNEL-SUMMARY-MINIMUM-LENGTH-CHECK-OK"; +const MSGS_PER_TURN = 9; + +type Ctx = Omit & { log: (m: string) => void; logs: string[] }; + +function makeCtx(messages: CoreMessage[], overrides?: Record): Ctx { + const logs: string[] = []; + return { + core: createCore(), + config: defaultConfig(200000, overrides as never), + messages, + session: { + id: "issue4331-test", + meta: {}, + stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 100000, 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: [], + }, + log: (m: string) => { logs.push(m); }, + logs, + }; +} + +function withRefs(ctx: Ctx): Ctx { + const res = assignRefs(ctx.messages, { existing: emptyRefMap(), nextIndex: 0 }); + ctx.session.state.messageRefs = res.map; + return ctx; +} + +function runApply(ctx: Ctx, args: unknown): string { + return applyRanges(parseCompressInput(args), ctx).text; +} + +function offerRange(startRef: string, endRef: string, count: number, tokens = 8000): CompressibleRange { + return { startRef, endRef, count, tokens, chars: tokens * 4, toolPct: 0, textPct: 100 }; +} +function setOffer(ctx: Ctx, ranges: CompressibleRange[], tier: number | null = null): void { + ctx.lastNudge = { compressibleRanges: ranges, tier } as unknown as NudgeDecision; +} + +// Turn layout (9 msgs): user, reasoning, text, 3x(tool-call, tool-result). +// toolName "write" stays inside recent-zone accounting (unlike bash, which is +// in NEVER_PRESERVE_RECENT_TOOLS) so zone math is exact in the drain tests. +function turnMsgs(turn: number, payloadChars: number, toolName: string): CoreMessage[] { + const id = (k: number) => `t${turn}_${k}`; + const fill = "x".repeat(payloadChars); + return [ + { id: id(0), role: "user", contentType: "text", text: `User request for turn ${turn}. ${payloadChars > 0 ? fill.slice(0, 200) : ""}` }, + { id: id(1), role: "assistant", contentType: "reasoning", text: `Reasoning for turn ${turn}. ${payloadChars > 0 ? fill.slice(0, 200) : ""}` }, + { id: id(2), role: "assistant", contentType: "text", text: `Assistant answer for turn ${turn}. ${fill}` }, + ...[0, 1, 2].flatMap((k): CoreMessage[] => [ + { id: id(3 + k * 2), role: "assistant", contentType: "tool-call", toolName, toolCallId: `tc_${turn}_${k}`, text: JSON.stringify({ input: `${toolName} step-${k} ${fill.slice(0, 600)}` }) }, + { id: id(4 + k * 2), role: "tool", contentType: "tool-result", toolName, toolCallId: `tc_${turn}_${k}`, text: `result of ${toolName} step-${k}: ${fill.slice(0, 600)}` }, + ]), + ]; +} + +function buildSession(turns: number, payloadChars: number, toolName: string): CoreMessage[] { + const out: CoreMessage[] = []; + for (let t = 0; t < turns; t++) out.push(...turnMsgs(t, payloadChars, toolName)); + return out; +} + +// Ref of 0-based message index i (fresh dense assignment: idx i -> m(i+1)). +const refAt = (i: number) => `m${String(i + 1).padStart(5, "0")}`; +const turnSpan = (fromTurn: number, toTurn: number): { startRef: string; endRef: string } => ({ + startRef: refAt(fromTurn * MSGS_PER_TURN), + endRef: refAt((toTurn + 1) * MSGS_PER_TURN - 1), +}); + +function foldArgs(spans: Array<{ startRef: string; endRef: string }>) { + return { content: spans.map((s) => ({ startId: s.startRef, endId: s.endRef, summary: SUMMARY })) }; +} + +// 40 turns; fold turns 1..16 and 28..39, leaving turns 17..27 (99 msgs, +// m00154..m00252) as a mid-history hole plus turn 0 as a head residue. +function holeSession(): { ctx: Ctx; spans: Array<{ startRef: string; endRef: string }> } { + const ctx = withRefs(makeCtx(buildSession(40, 4000, "bash"), { preserveRecentMessages: 3, preserveRecentTokens: 100 })); + const left = turnSpan(1, 16); + const right = turnSpan(28, 39); + setOffer(ctx, [offerRange(left.startRef, left.endRef, 137), offerRange(right.startRef, right.endRef, 108)]); + return { ctx, spans: [left, right] }; +} + +test("#4331 P1: offer exhausted with a mid-history hole -> honest uncovered-history verdict, no false minimum claim", () => { + const { ctx, spans } = holeSession(); + const [left, right] = spans; + const out = runApply(ctx, foldArgs([left, right])); + assert.ok(out.startsWith("[Compressed"), `expected success receipt, got: ${out.slice(0, 160)}`); + assert.ok(out.includes(UNCOVERED_HEADER), "honest uncovered-history verdict expected:\n" + out); + assert.ok(out.includes("m00154") && out.includes("m00252"), "hole span must be named:\n" + out); + assert.ok(!out.includes(NO_RANGES_REMAIN_TEXT), "false minimum claim must not fire while uncovered mass exists:\n" + out); +}); + +test("#4331 P1b: the audit itself finds the post-fold hole on the real applied state", () => { + const { ctx, spans } = holeSession(); + const [left, right] = spans; + const out = runApply(ctx, foldArgs([left, right])); + assert.ok(out.startsWith("[Compressed"), `expected success receipt, got: ${out.slice(0, 160)}`); + const runs = uncoveredHistoryRuns({ + core: ctx.core, + messages: ctx.messages, + state: ctx.session.state, + config: ctx.config, + tokenCount: 100000, + }); + const hole = runs.find((r) => r.startRef === "m00154" && r.endRef === "m00252"); + assert.ok(hole, `hole run missing from audit: ${JSON.stringify(runs)}`); + assert.equal(hole.count, 99, "hole is 11 turns x 9 msgs"); + assert.ok(hole.tokens > 10000, "hole carries substantial raw mass"); +}); + +test("#4331 N1: genuinely-drained session keeps the verbatim #1387 minimum claim", () => { + // 12 turns, fold turns 0..10; the untouched final turn sits EXACTLY inside + // the protected zone (preserveRecentMessages: 9 over all-visible turns). + // The audit must classify it zone-hidden and stay silent. + const ctx = withRefs(makeCtx(buildSession(12, 4000, "write"), { preserveRecentMessages: 9, preserveRecentTokens: 0 })); + const span = turnSpan(0, 10); + setOffer(ctx, [offerRange(span.startRef, span.endRef, 99)]); + const out = runApply(ctx, foldArgs([span])); + assert.ok(out.startsWith("[Compressed"), `expected success receipt, got: ${out.slice(0, 160)}`); + assert.ok(!out.includes(UNCOVERED_HEADER), "protected tail is not leakable mass:\n" + out); + assert.ok(out.includes(NO_RANGES_REMAIN_TEXT), "verbatim contract string must survive for drained sessions:\n" + out); +}); + +test("#4331 N2: sub-floor residue (< 200 tok/run) stays silent, minimum claim stands", () => { + // 8 turns with big payloads except turns 0 and 4 (tiny, < 200 tok each). + // Fold turns 1..3 and 5..7; the two tiny residues fall below the viability + // floor and must not manufacture an uncovered-history verdict. + const msgs = buildSession(8, 4000, "bash"); + msgs.splice(0, MSGS_PER_TURN, ...turnMsgs(0, 0, "bash")); + msgs.splice(4 * MSGS_PER_TURN, MSGS_PER_TURN, ...turnMsgs(4, 0, "bash")); + const ctx = withRefs(makeCtx(msgs, { preserveRecentMessages: 3, preserveRecentTokens: 100 })); + const left = turnSpan(1, 3); + const right = turnSpan(5, 7); + setOffer(ctx, [offerRange(left.startRef, left.endRef, 27), offerRange(right.startRef, right.endRef, 27)]); + const out = runApply(ctx, foldArgs([left, right])); + assert.ok(out.startsWith("[Compressed"), `expected success receipt, got: ${out.slice(0, 160)}`); + assert.ok(!out.includes(UNCOVERED_HEADER), "sub-floor residue must not be reported:\n" + out); + assert.ok(out.includes(NO_RANGES_REMAIN_TEXT), "minimum claim stands below the floor:\n" + out); +}); + +test("#4331 P2: tier-distillation precedence — no stop signal AND no uncovered verdict while a tier is active", () => { + const { ctx, spans } = holeSession(); + const [left, right] = spans; + ctx.lastNudge = { compressibleRanges: [offerRange(left.startRef, left.endRef, 137), offerRange(right.startRef, right.endRef, 108)], tier: 1 } as unknown as NudgeDecision; + const out = runApply(ctx, foldArgs([left, right])); + assert.ok(out.startsWith("[Compressed"), `expected success receipt, got: ${out.slice(0, 160)}`); + assert.ok(!out.includes(NO_RANGES_REMAIN_TEXT), "tier active: no stop signal:\n" + out); + assert.ok(!out.includes(UNCOVERED_HEADER), "tier active: uncovered verdict suppressed too:\n" + out); +}); + +test("#4331 P3: acp_status names uncovered runs that no offer covers (additive section)", () => { + // Raise minCompressRange above every run so the live offer list comes up + // empty; the mid-history hole must then surface in its own section. + const { ctx, spans } = holeSession(); + ctx.config.compress.minCompressRange = 100000; + const [left, right] = spans; + const out = runApply(ctx, foldArgs([left, right])); + assert.ok(out.startsWith("[Compressed"), `expected success receipt, got: ${out.slice(0, 160)}`); + const status = handleAcpStatus({}, ctx); + assert.ok(status.text.includes(STATUS_SECTION), "UNCOVERED HISTORY section expected:\n" + status.text); + assert.ok(status.text.includes("m00154"), "hole span must be named in the section:\n" + status.text); +}); + +test("#4331 P3b: acp_status does not duplicate runs the offer list already carries", () => { + // Default gates: the hole IS offered by the live recompute, so it belongs + // to the Compressible-ranges list above — the section must stay silent. + const { ctx, spans } = holeSession(); + const [left, right] = spans; + const out = runApply(ctx, foldArgs([left, right])); + assert.ok(out.startsWith("[Compressed"), `expected success receipt, got: ${out.slice(0, 160)}`); + const status = handleAcpStatus({}, ctx); + assert.ok(!status.text.includes(STATUS_SECTION), "offered runs must not be duplicated below the list:\n" + status.text); + assert.ok(status.text.includes("m00154"), "the hole must still be visible via the ranges list:\n" + status.text); +}); + +test("#4331 N3: acp_status stays quiet on a drained session (no section)", () => { + const ctx = withRefs(makeCtx(buildSession(12, 4000, "write"), { preserveRecentMessages: 9, preserveRecentTokens: 0 })); + const span = turnSpan(0, 10); + setOffer(ctx, [offerRange(span.startRef, span.endRef, 99)]); + const out = runApply(ctx, foldArgs([span])); + assert.ok(out.startsWith("[Compressed"), `expected success receipt, got: ${out.slice(0, 160)}`); + const status = handleAcpStatus({}, ctx); + assert.ok(!status.text.includes(STATUS_SECTION), "no section for drained sessions:\n" + status.text); +}); + +test("#4331 unit: describeUncoveredRuns caps the listing and counts the rest", () => { + const runs = Array.from({ length: 7 }, (_, i) => ({ startRef: `m00${100 + i}`, endRef: `m00${110 + i}`, count: 9, tokens: 5000 })); + const s = describeUncoveredRuns(runs); + assert.ok(s.includes("+2 more run(s)"), s); + assert.ok(s.includes("~5K tok"), s); + assert.equal(shortTokenCount(950), "950"); + assert.equal(shortTokenCount(1500), "~1.5K"); + assert.equal(shortTokenCount(50000), "~50K"); +}); diff --git a/tests/issue847-compress-failure-guards.test.ts b/tests/issue847-compress-failure-guards.test.ts index 90d6e3cc25..370b9c8836 100644 --- a/tests/issue847-compress-failure-guards.test.ts +++ b/tests/issue847-compress-failure-guards.test.ts @@ -65,7 +65,11 @@ test("#847: acp_status lists only ranges that pass the submit gate (chars >= min assert.equal(sub.map((m) => m.text?.length ?? 0).reduce((a, b) => a + b, 0), 4400, "fixture sanity: whole span is sub-gate"); const subReport = handleAcpStatus({}, subCtx).text; assert.ok(!subReport.includes("Compressible ranges"), "all-sub-gate session must NOT advertise any range"); - assert.ok(!subReport.includes("m00001"), "sub-gate range must NOT be listed"); + // #4331 (behavior change): the run is never OFFERED (gate discipline above + // unchanged), but the uncovered-history diagnostic NAMES it — visibility + // for mass that no offer will ever carry, explicitly not a fold suggestion. + assert.ok(subReport.includes("UNCOVERED HISTORY"), "sub-gate residue must be visible in the diagnostic section:\n" + subReport); + assert.ok(subReport.includes("m00001"), "the sub-gate run must be named in the diagnostic section"); const big = [ textMsg("raw_1", "assistant", "a".repeat(1700)), From 93eb7f7a6a541f531521134aea86b631d856cd68 Mon Sep 17 00:00:00 2001 From: awork Date: Fri, 23 Oct 2026 02:07:54 +0800 Subject: [PATCH 2/3] fix(ci): keep UncoveredRun/UncoveredAuditInput module-internal (unused-exports gate) The two audit types are only used in the signatures of the exported functions; no cross-file import exists, so the unused-exports gate flags them as INTERNAL_ONLY. Drop the leading export per the gate's own guidance; behavior unchanged. --- src/uncovered.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/uncovered.ts b/src/uncovered.ts index 4398834f7d..c8c71b96a2 100644 --- a/src/uncovered.ts +++ b/src/uncovered.ts @@ -32,14 +32,14 @@ import { type MessageContentStore, } from "acp-kernel"; -export interface UncoveredRun { +interface UncoveredRun { startRef: string; endRef: string; count: number; tokens: number; } -export interface UncoveredAuditInput { +interface UncoveredAuditInput { core: CompressionCore; /** Full resent log (apply class) — same array class prepare runs on. */ messages: CoreMessage[]; From 94819f4a2a82f2a6af54ae95495ee2f50e320054 Mon Sep 17 00:00:00 2001 From: awork Date: Fri, 23 Oct 2026 02:27:01 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix(#4331):=20review=20round=20=E2=80=94=20?= =?UTF-8?q?receipt=20materiality=20floor,=20tier-2=20coverage=20pin,=20sta?= =?UTF-8?q?tus-pass=20reuse?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Response to reviewer feedback on the #4331 PR: - postCompressTail: the honest uncovered verdict now fires only when total uncovered mass >= UNCOVERED_RECEIPT_FLOOR_TOKENS (10K); below the floor the verbatim #1387 wording fires byte-identical to master (pi #521 anti-nag: small leftovers must not re-nag on every fold). tests/compress-success-tail. test.ts is back at master bytes — its sub-floor residue fixtures now pin the floor directly (zero diff vs master). - uncoveredHistoryRuns: optional normalRanges input — a caller that already ran the normal-config offer pass may reuse it (acp_status now pays ONE extra processTurn instead of two); undefined = audit runs its own pass. - UncoveredRun/UncoveredAuditInput unexported (unused-exports gate). - New P4 regression test: three adjacent T1 blocks distilled into one T2 parent through the real host path must not leak child spans into the audit (pins coveredMessageIds' active-block union + unit-projection child-id propagation + child deactivation). - TECHNICAL-NOTES: floor, tier-2 invariant, cost profile. --- TECHNICAL-NOTES.md | 9 ++--- src/acp-status.ts | 1 + src/stream.ts | 8 ++++- src/uncovered.ts | 16 ++++++--- tests/compress-success-tail.test.ts | 42 ++++++++--------------- tests/issue4331-uncovered-history.test.ts | 36 ++++++++++++++++++- 6 files changed, 75 insertions(+), 37 deletions(-) diff --git a/TECHNICAL-NOTES.md b/TECHNICAL-NOTES.md index b5ac86d52a..10b2ef94e8 100644 --- a/TECHNICAL-NOTES.md +++ b/TECHNICAL-NOTES.md @@ -482,10 +482,11 @@ The "no compressible ranges remain — the context is already at its minimum" st Design (`src/uncovered.ts`, host-side only, no kernel change): -- `uncoveredHistoryRuns()`: walk the message array for entries outside every active block's coverage (the `collectVisible` predicate), group contiguous runs, then classify each run: skip if any current offer covers part of it (the model can act on it now); otherwise diff TWO `core.processTurn` passes over a `structuredClone(state)` — normal config vs relaxed `{preserveRecentMessages: 0, preserveRecentTokens: 0}` — and treat refs offered only in the relaxed pass as zone-hidden (inside the protected recent window). The empirical zone diff deliberately re-uses the kernel's own protected-zone rules instead of copying them, so it self-maintains as those rules evolve (over-approximation errs toward under-reporting). Runs below `VIABLE_RANGE_MIN_TOKENS` (200) stay silent. Whole function is advisory: any throw degrades to "no runs found" and never breaks receipts or status. +- `uncoveredHistoryRuns()`: walk the message array for entries outside every active block's coverage (the `collectVisible` predicate), group contiguous runs, then classify each run: skip if any current offer covers part of it (the model can act on it now); otherwise diff TWO `core.processTurn` passes over a `structuredClone(state)` — normal config vs relaxed `{preserveRecentMessages: 0, preserveRecentTokens: 0}` — and treat refs offered only in the relaxed pass as zone-hidden (inside the protected recent window). The empirical zone diff deliberately re-uses the kernel's own protected-zone rules instead of copying them, so it self-maintains as those rules evolve (over-approximation errs toward under-reporting). Runs below `VIABLE_RANGE_MIN_TOKENS` (200) stay silent. A caller that already ran the normal offer pass may pass it via `normalRanges` (array = trust-and-reuse, even empty; undefined = no caller data, audit runs its own pass) so acp_status pays ONE extra `processTurn`, not two. Whole function is advisory: any throw degrades to "no runs found" and never breaks receipts or status. - **acp_status** gains an additive `UNCOVERED HISTORY` section (top-5 runs by tokens, rest counted) listing mass that sits outside every active block AND is absent from the live Compressible-ranges list above — including isolated sub-gate runs between covered blocks that no merge will ever fold (the permanent-hole class of #4331 / #847). Named for visibility, explicitly not as fold suggestions; rows drill down via `scope:"uncompressed"` unchanged. -- **postCompressTail**: on clean whole-offer exhaustion with no tier distillation, the audit runs before the verdict. Runs found → honest text naming them ("not at its minimum", pointer to acp_status). None → the verbatim #1387 contract string fires byte-stable, as before for genuinely-drained sessions. +- **Tier-2 coverage invariant (reviewed, pinned by P4)**: the audit's covered-set is `coveredMessageIds` = the union of ACTIVE blocks' `effectiveMessageIds` with NO child recursion (kernel/src/state.ts:57-64). That is safe because every block-creation path unions consumed children's `effectiveMessageIds` into the new parent (kernel/src/unit-projection.ts:178-183) and deactivates the children (kernel/src/compress.ts:1578-1581) — a T2/T3 parent therefore carries its full distilled span in the state model, and hydration preserves it (whole state JSON). DB-side walks of `blocks.payload_json` do NOT have that property and need `directBlockIds` recursion (the reason an earlier independent attempt carried one); the state model does not. +- **postCompressTail**: on clean whole-offer exhaustion with no tier distillation, the audit runs before the verdict. Runs found AND totalling ≥ `UNCOVERED_RECEIPT_FLOOR_TOKENS` (10K, receipt-only) → honest text naming them ("not at its minimum", pointer to acp_status). Below the floor — or with no runs — the verbatim #1387 contract string fires byte-stable, exactly as on master: small declined leftovers must not re-nag on every fold (pi #521 rule), while material leaks override it. The floor is deliberately NOT applied to the acp_status section: an explicit inspection surface is not a nag surface, and option 1 of #4331 specified no size gate there. The 10K total is a product value pending owner ack (matches the reviewer's independent pick). -Behavior change (disclosed): old = unconditional "already at its minimum" on offer exhaustion; new = that claim only when the global audit verifies it, honest uncovered-listing otherwise. Sub-gate residue became visible in acp_status (previously invisible on every surface). No config/env surface added; nudge prompt text untouched (kernel-owned). Known limitation left as a separate observation: the receipt's remaining-calc removes a WHOLE offered batch when any submitted span overlaps it, so a partially-folded merged batch can under-report rather than over-report. +Behavior change (disclosed): old = unconditional "already at its minimum" on offer exhaustion; new = that claim only when the global audit verifies it AND the uncovered mass clears the 10K receipt floor — below the floor the receipt is byte-identical to master (so `tests/compress-success-tail.test.ts` stays at master bytes, zero diff), and its #2404/#1387 sub-floor residue fixtures now double as the floor's pins. Sub-gate residue became visible in acp_status (previously invisible on every surface). No config/env surface added; nudge prompt text untouched (kernel-owned). Known limitation left as a separate observation: the receipt's remaining-calc removes a WHOLE offered batch when any submitted span overlaps it, so a partially-folded merged batch can under-report rather than over-report. -Tests: `tests/issue4331-uncovered-history.test.ts` (hole session → honest verdict + named span; drained session → verbatim contract string; sub-floor silence; tier precedence; both acp_status directions; formatter caps) + updated pinned contracts in `tests/compress-success-tail.test.ts` (#2404/#1387 now pin "never leaks AS AN OFFER"), `tests/issue2029-status-baseline-provenance.test.ts` (every status-reader kernel call feeds the provenance baseline), `tests/issue847-compress-failure-guards.test.ts` (sub-gate residue visible in the diagnostic, still never offered). +Tests: `tests/issue4331-uncovered-history.test.ts` (hole session → honest verdict + named span; drained session → verbatim contract string; sub-floor silence; tier precedence; **tier-2 distillation coverage pin** — three T1s distilled into one T2 parent via the real host path must not leak child spans into the audit; both acp_status directions; formatter caps) + `tests/compress-success-tail.test.ts` UNCHANGED from master (its < 10K-residue fixtures pin the floor), `tests/issue2029-status-baseline-provenance.test.ts` (every status-reader kernel call feeds the provenance baseline), `tests/issue847-compress-failure-guards.test.ts` (sub-gate residue visible in the diagnostic, still never offered). diff --git a/src/acp-status.ts b/src/acp-status.ts index dbb790f8c0..c79722ba99 100644 --- a/src/acp-status.ts +++ b/src/acp-status.ts @@ -292,6 +292,7 @@ export function handleAcpStatus(args: Record, ctx: AcpStatusCtx contentStore: contentStoreOf(ctx.session), applyView: ctx.compressMessages, actionableSpans: offeredSpans ?? [], + normalRanges: offeredSpans, }); if (runs.length > 0) { const totalMsgs = runs.reduce((s, r) => s + r.count, 0); diff --git a/src/stream.ts b/src/stream.ts index 73776f245d..27c1b36522 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -516,6 +516,10 @@ function droppedEntriesNote(diagnostics: CompressParseDiagnostics): string { // past the session tail and burn a round on kernel rejection. Wording is // verbatim pi-side so both hosts speak the same contract. const NO_RANGES_REMAIN_TEXT = "No compressible ranges remain — the context is already at its minimum; continue the task without compressing."; +// #4331 / pi #521 anti-nag rule: below this TOTAL uncovered mass the receipt +// keeps the legacy wording — small leftovers outside the offer must not re-nag +// on every fold. Material leaks (the #4331 class: tens of K tokens raw) override it. +const UNCOVERED_RECEIPT_FLOOR_TOKENS = 10_000; // #2404: the tail's "remaining ranges" is the LAST NUDGE's offer (the pre-fold // set the model acted on) minus the just-folded spans — NOT a fresh post-fold @@ -626,7 +630,9 @@ function postCompressTail(ctx: RewriteCtx, cleanSuccess: boolean, submitted: Arr if (runs.length > 0) { const totalMsgs = runs.reduce((s, r) => s + r.count, 0); const totalTok = runs.reduce((s, r) => s + r.tokens, 0); - return `\n\nUncovered history remains — all offered ranges are folded, but ${totalMsgs} msg(s) across ${runs.length} run(s), ${shortTokenCount(totalTok)} tok, sit outside every active block and are not in this call's offer: ${describeUncoveredRuns(runs)}. The context is not at its minimum (runs may sit below the fold-size gate — named for visibility, not as fold suggestions); inspect with acp_status(scope:"uncompressed").`; + if (totalTok >= UNCOVERED_RECEIPT_FLOOR_TOKENS) { + return `\n\nUncovered history remains — all offered ranges are folded, but ${totalMsgs} msg(s) across ${runs.length} run(s), ${shortTokenCount(totalTok)} tok, sit outside every active block and are not in this call's offer: ${describeUncoveredRuns(runs)}. The context is not at its minimum (runs may sit below the fold-size gate — named for visibility, not as fold suggestions); inspect with acp_status(scope:"uncompressed").`; + } } } catch { // Fall through to the contract wording below. diff --git a/src/uncovered.ts b/src/uncovered.ts index c8c71b96a2..8d508b4c99 100644 --- a/src/uncovered.ts +++ b/src/uncovered.ts @@ -52,6 +52,12 @@ interface UncoveredAuditInput { /** Spans the model can act on right now (post-filter offer / remaining list). * Runs overlapping these are NOT reported — they are surfaced elsewhere. */ actionableSpans?: readonly { startRef: string; endRef: string }[]; + /** A caller that already ran the normal-config offer pass may pass its + * ranges here so the audit skips re-running it (acp_status reuses its live + * Compressible-ranges pass, cutting the audit from two processTurn calls + * to one on that surface). UNDEFINED means "no caller data" — the audit + * runs its own normal pass; an array (even empty) means "trust this". */ + normalRanges?: readonly { startRef: string; endRef: string }[]; } interface RunEntry { @@ -112,7 +118,7 @@ export function uncoveredHistoryRuns(input: UncoveredAuditInput): UncoveredRun[] } // 3) Empirical zone diff. Cloned state: see header note on cadence stamps. - let normalWindows: SpanWindow[] = []; + const normalWindows = input.normalRanges ? windowsOf(input.normalRanges) : []; let relaxedWindows: SpanWindow[] = []; try { const clone = structuredClone(state); @@ -123,9 +129,11 @@ export function uncoveredHistoryRuns(input: UncoveredAuditInput): UncoveredRun[] contentStore: input.contentStore, applyView: input.applyView, }; - normalWindows = windowsOf( - core.processTurn({ ...base, state: clone, config }).nudge?.compressibleRanges ?? [], - ); + if (!input.normalRanges) { + normalWindows.push(...windowsOf( + core.processTurn({ ...base, state: clone, config }).nudge?.compressibleRanges ?? [], + )); + } relaxedWindows = windowsOf( core .processTurn({ diff --git a/tests/compress-success-tail.test.ts b/tests/compress-success-tail.test.ts index 7f2110770d..1d2ab243b2 100644 --- a/tests/compress-success-tail.test.ts +++ b/tests/compress-success-tail.test.ts @@ -9,10 +9,6 @@ import { applyRanges, type RewriteCtx } from "../src/stream.ts"; // explicit stop signal (pi #521). Cross-host wording must stay verbatim. const RANGES_HEADER = "Current compressible ranges (use these refs exactly as listed):"; const NO_RANGES_REMAIN_TEXT = "No compressible ranges remain — the context is already at its minimum; continue the task without compressing."; -// #4331: honest replacement when offer exhaustion leaves material mass outside -// every active block and every offer — names the runs instead of claiming the -// global minimum. -const UNCOVERED_REMAINS_TEXT = "Uncovered history remains"; type Ctx = Omit & { log: (m: string) => void; logs: string[] }; @@ -79,26 +75,22 @@ test("#2404: folding a subset of the pre-fold offer keeps advertising the rest", assert.ok(!out.includes(NO_RANGES_REMAIN_TEXT), "stop signal must not fire while an offer remains:\n" + out); }); -test("#2404: a range protected at nudge time never leaks into the receipt as an offer (no zero-growth chained fold)", () => { +test("#2404: a range protected at nudge time never leaks into the receipt (no zero-growth chained fold)", () => { // THE regression: after the fold, m00005–m00007 are raw-compressible in LIVE // state (preserveRecentMessages: 2 keeps only m00008–m00009 protected), so a - // post-fold RECOMPUTE would list them as an actionable offer and chain a - // second zero-growth fold. They were NOT part of the pre-fold offer → the - // tail must not advertise them under RANGES_HEADER. A regression to the - // recompute path makes an offer leak and fail here. - // #4331 (behavior change): such residue is no longer hidden behind a false - // "already at its minimum" claim — it is NAMED as uncovered history - // (diagnostic note with span, not a fold offer). + // post-fold RECOMPUTE would list them and chain a second zero-growth fold. + // But they were NOT part of the pre-fold offer (still inside the protection + // window when the nudge fired) → pinning the tail to that offer keeps them + // out. A regression to the recompute path makes m00005 leak and fail here. const msgs = Array.from({ length: 9 }, (_, i) => textMsg(`raw_${i + 1}`, i % 2 === 0 ? "user" : "assistant", "x".repeat(5000))); const ctx = withRefs(makeCtx(msgs, { preserveRecentMessages: 2, preserveRecentTokens: 0 })); ctx.session.stats.lastInputTokens = 100000; setOffer(ctx, [offerRange("m00001", "m00004", 4)]); const out = runApply(ctx, { content: [{ startId: "m00001", endId: "m00004", summary: "TAIL-TEST-SUMMARY-PAYLOAD-LONG-ENOUGH-FOR-THE-KERNEL-MIN-LENGTH-CHECK" }] }); assert.ok(out.startsWith("[Compressed"), `expected success, got: ${out.slice(0, 120)}`); - assert.ok(!out.includes(RANGES_HEADER), "no actionable ranges may be advertised once the whole offer folded:\n" + out); - assert.ok(out.includes(UNCOVERED_REMAINS_TEXT), "residual mass outside every block must be named (#4331):\n" + out); - assert.ok(out.includes("m00005") && out.includes("m00007"), "the residual run must be spanned in the uncovered note:\n" + out); - assert.ok(!out.includes(NO_RANGES_REMAIN_TEXT), "minimum claim must not fire while uncovered mass exists (#4331):\n" + out); + assert.ok(!out.includes(RANGES_HEADER), "no ranges may be advertised once the whole offer folded:\n" + out); + assert.ok(!out.includes("m00005"), "a not-yet-offered (protected-at-nudge) range must NOT leak:\n" + out); + assert.ok(out.includes(NO_RANGES_REMAIN_TEXT), "stop signal fires once every offered range folded:\n" + out); }); test("#2404: folding every offered range emits the stop signal", () => { @@ -252,20 +244,17 @@ test("#1387: partial failure (some ranges rejected) keeps the old silent tail", assert.ok(!out.includes(RANGES_HEADER), "no ranges advertised on partial failure:\n" + out); }); -test("#1387: tail recompute respects the kernel's own eligibility (shouldInject=false leftovers are not advertised as offers)", () => { +test("#1387: tail recompute respects the kernel's own eligibility (shouldInject=false leftovers are not advertised)", () => { // #3058 moved preserveRecentMessages 5 → 3: under the default, folding // m00001–m00002 leaves m00003 outside the protection window, and the live // recompute returns a nudge DECISION STRUCT whose ranges list m00003 even // though the kernel itself judged it below threshold/floor (shouldInject // === false, reason "max compressible … < threshold …"). Advertising such - // ranges as an actionable offer in the receipt re-nags at zero pressure — - // the disease #1387 (pi #521) fixed. The tail must honor shouldInject: - // sub-threshold leftovers never appear under RANGES_HEADER. (The two tests + // ranges in the receipt re-nags at zero pressure — the disease #1387 + // (pi #521) fixed. The tail must honor shouldInject: sub-threshold + // leftovers never list; the stop signal fires instead. (The two tests // above pin preserveRecentMessages: 5 for their protection premise; this // one pins the default-config behavior the unpinned tests used to cover.) - // #4331 (behavior change): the residue is no longer papered over by a - // false "already at its minimum" claim — it is named as uncovered history - // (diagnostic note with span, not a fold offer). const msgs = [ textMsg("raw_1", "user", "x".repeat(20000)), textMsg("raw_2", "assistant", "x".repeat(20000)), @@ -279,10 +268,9 @@ test("#1387: tail recompute respects the kernel's own eligibility (shouldInject= ctx.session.stats.lastInputTokens = 80000; const out = runApply(ctx, { content: [{ startId: "m00001", endId: "m00002", summary: "TAIL-TEST-SUMMARY-PAYLOAD-LONG-ENOUGH-FOR-THE-KERNEL-MIN-LENGTH-CHECK" }] }); assert.ok(out.startsWith("[Compressed"), `expected success, got: ${out.slice(0, 120)}`); - assert.ok(!out.includes(RANGES_HEADER), "a sub-threshold leftover must not be advertised as an offer:\n" + out); - assert.ok(out.includes(UNCOVERED_REMAINS_TEXT), "below-threshold residue must be named, not hidden (#4331):\n" + out); - assert.ok(out.includes("m00003"), "the residue ref must appear in the uncovered note:\n" + out); - assert.ok(!out.includes(NO_RANGES_REMAIN_TEXT), "minimum claim must not fire while uncovered mass exists (#4331):\n" + out); + assert.ok(!out.includes(RANGES_HEADER), "a sub-threshold leftover must not be advertised:\n" + out); + assert.ok(!out.includes("m00003"), "the below-threshold residue must stay unlisted:\n" + out); + assert.ok(out.includes(NO_RANGES_REMAIN_TEXT), "stop signal fires when the kernel itself would not nudge:\n" + out); }); test("#1387: tail recompute still advertises ranges under real pressure (shouldInject=true)", () => { diff --git a/tests/issue4331-uncovered-history.test.ts b/tests/issue4331-uncovered-history.test.ts index 028fb39d9d..24eb374e26 100644 --- a/tests/issue4331-uncovered-history.test.ts +++ b/tests/issue4331-uncovered-history.test.ts @@ -1,7 +1,7 @@ import { test } from "node:test"; import assert from "node:assert/strict"; import type { CompressibleRange, CoreMessage, NudgeDecision } from "acp-kernel"; -import { assignRefs, createCore, createInitialState, defaultConfig, emptyRefMap } from "acp-kernel"; +import { assignRefs, coveredMessageIds, createCore, createInitialState, defaultConfig, emptyRefMap } from "acp-kernel"; import { parseCompressInput } from "../src/compress-tool.ts"; import { applyRanges, type RewriteCtx } from "../src/stream.ts"; import { handleAcpStatus } from "../src/acp-status.ts"; @@ -174,6 +174,40 @@ test("#4331 P2: tier-distillation precedence — no stop signal AND no uncovered assert.ok(!out.includes(UNCOVERED_HEADER), "tier active: uncovered verdict suppressed too:\n" + out); }); +test("#4331 P4: tier-2 parents carry their children's coverage — the audit never reports child spans as uncovered", () => { + // Reviewer note on the #4331 thread: in their CED-T session a tier-2 + // parent's STORED effective list looked empty on a DB-side walk while its + // children covered the spans. The state model must not have that hole: + // every block-creation path unions consumed children's effectiveMessageIds + // into the new parent (kernel/src/unit-projection.ts:178-183) and + // deactivates the children (kernel/src/compress.ts:1578-1581), so + // coveredMessageIds — the active-block union the audit relies on + // (kernel/src/state.ts:57-64) — sees the full distilled span. Pin it: + // fold three adjacent T1 blocks, distill them into one T2 parent, then + // the audit must not name any ref inside the distilled region. + const ctx = withRefs(makeCtx(buildSession(16, 4000, "bash"), { preserveRecentMessages: 3, preserveRecentTokens: 100 })); + const s1 = turnSpan(1, 5); + const s2 = turnSpan(6, 10); + const s3 = turnSpan(11, 15); + setOffer(ctx, [offerRange(s1.startRef, s1.endRef, 45), offerRange(s2.startRef, s2.endRef, 45), offerRange(s3.startRef, s3.endRef, 45)]); + const out = runApply(ctx, foldArgs([s1, s2, s3])); + assert.ok(out.startsWith("[Compressed"), `expected success receipt, got: ${out.slice(0, 160)}`); + const refNum = (ref: string) => Number(ref.replace(/\D/g, "")); + const t1s = ctx.session.state.blocks.filter((b) => b.tier === 1 && b.active).sort((a, b) => refNum(a.startRef ?? "") - refNum(b.startRef ?? "")); + assert.equal(t1s.length, 3, "three T1 blocks expected before distillation"); + const distillOut = runApply(ctx, foldArgs([{ startRef: t1s[0].blockId, endRef: t1s[2].blockId }])); + assert.ok(distillOut.startsWith("[Compressed"), `expected distillation receipt, got: ${distillOut.slice(0, 160)}`); + const parent = ctx.session.state.blocks.find((b) => b.tier === 2 && b.active); + assert.ok(parent, "T2 parent expected after distillation"); + assert.equal(ctx.session.state.blocks.filter((b) => b.tier === 1 && b.active).length, 0, "children must be deactivated"); + const covered = coveredMessageIds(ctx.session.state); + for (const i of [9, 89, 134]) { + assert.ok(covered.has(ctx.messages[i].id), `message idx ${i} (inside the distilled span) must stay covered via the T2 parent`); + } + const runs = uncoveredHistoryRuns({ core: ctx.core, messages: ctx.messages, state: ctx.session.state, config: ctx.config, tokenCount: 100000 }); + assert.ok(!runs.some((r) => refNum(r.startRef) <= 135 && refNum(r.endRef) >= 10), `distilled T2 span leaked into the audit: ${JSON.stringify(runs)}`); +}); + test("#4331 P3: acp_status names uncovered runs that no offer covers (additive section)", () => { // Raise minCompressRange above every run so the live offer list comes up // empty; the mid-history hole must then surface in its own section.