From 84fba9ba470f865e7ad0c818cd17913fac242cd9 Mon Sep 17 00:00:00 2001 From: miaomiaozii Date: Sun, 15 Nov 2026 16:52:58 +0800 Subject: [PATCH 1/2] feat(compress): reject truncated summaries; optional proportional summary floor - rejectTruncatedSummary (default true): reject summaries ending in an ellipsis or with an unclosed ``` fence. A cut-off summary becomes the only record of the range and is silently lossy. - minSummaryRatio (default 0 = off): min summary chars per token of the range's direct messages. Consumed blocks are excluded so tier-2/3 distillation is unaffected; floor is capped at maxSummaryLength/2. --- src/compress.ts | 40 ++++++++++++++++ src/types.ts | 10 ++++ tests/summary-integrity.test.ts | 85 +++++++++++++++++++++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 tests/summary-integrity.test.ts diff --git a/src/compress.ts b/src/compress.ts index f95f76d..760318c 100644 --- a/src/compress.ts +++ b/src/compress.ts @@ -1415,6 +1415,9 @@ function applySingleRange(input: SingleRangeInput): SingleRangeOutcome { ? countMessageTokens(message, input.countTokens) : 0; } + // Direct-message tokens only (consumed blocks not yet added): the + // proportional floor must not punish tier-2/3 distillation. + validateSummaryProportion(input.spec, input.config.compress, compressedTokens); for (const consumedId of consumedBlockIds) { const consumed = blockById(input.state, consumedId); if (consumed) { @@ -1539,6 +1542,43 @@ function validateSummaryLength( } } +/** Heuristic truncation detector. Deliberately narrow (low false-positive): + * only an ellipsis tail or an odd number of ``` fences counts. */ +export function detectTruncatedSummary(summary: string): string | null { + const s = summary.trimEnd(); + if (/(?:…|\.\.\.)$/.test(s)) return "ends with an ellipsis"; + const fences = s.match(/^\s*```/gm)?.length ?? 0; + if (fences % 2 === 1) return "has an unclosed ``` code fence"; + return null; +} + +function validateSummaryProportion( + spec: { summary: string; summaryMaxChars?: number }, + cfg: Config["compress"], + directTokens: number, +): void { + const summary = spec.summary?.trim() ?? ""; + if (cfg.rejectTruncatedSummary !== false) { + const why = detectTruncatedSummary(summary); + if (why) { + throw new Error( + `Summary looks truncated (${why}). It would become the only record of this range — finish the last sentence/fence and resubmit.`, + ); + } + } + const ratio = cfg.minSummaryRatio ?? 0; + if (ratio > 0 && directTokens > 0) { + const ceiling = spec.summaryMaxChars ?? cfg.maxSummaryLength; + let floor = Math.ceil(directTokens * ratio); + if (ceiling > 0) floor = Math.min(floor, Math.floor(ceiling / 2)); + if (summary.length < floor) { + throw new Error( + `Summary too short for its range (${summary.length} chars for ~${directTokens} tokens of direct messages; min ${floor} at minSummaryRatio ${ratio}). Keep paths, decisions, exact values and open objectives.`, + ); + } + } +} + function filterProtectedToolMessages( directMessageIds: string[], messages: CoreMessage[], diff --git a/src/types.ts b/src/types.ts index 17e8d0f..4fcb431 100644 --- a/src/types.ts +++ b/src/types.ts @@ -358,6 +358,16 @@ export interface CompressValidationConfig { maxSummaryLength: number; /** Minimum summary length (chars). Summary shorter than this is rejected. 0 = disabled. Default 50. */ minSummaryLength: number; + /** Proportional floor: minimum summary chars per original token of the + * range's DIRECT messages (consumed blocks are excluded — tier-2/3 + * distillation is meant to be short). The floor is capped at half of + * maxSummaryLength so it can never conflict with the ceiling. + * Example: 0.02 → a 20K-token range needs ≥ 400 chars. 0 = disabled. Default 0. */ + minSummaryRatio?: number; + /** Reject summaries that look cut off: trailing "…" / "..." or an + * unclosed ``` code fence. A truncated summary is silently lossy — it + * becomes the only record of the range. Default true. */ + rejectTruncatedSummary?: boolean; } export interface Config { diff --git a/tests/summary-integrity.test.ts b/tests/summary-integrity.test.ts new file mode 100644 index 0000000..630f1dd --- /dev/null +++ b/tests/summary-integrity.test.ts @@ -0,0 +1,85 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { createCore, detectTruncatedSummary } from "../src/compress.js"; +import { createInitialState } from "../src/state.js"; +import { assignRefs } from "../src/refs.js"; +import type { Config, CoreMessage } from "../src/types.js"; + +function msg(id: string, text: string): CoreMessage { + return { id, role: "user", contentType: "text", text }; +} + +function config(compress: Partial = {}): Config { + return { + tiers: { enabled: true, tier2Trigger: 5, tier3Trigger: 10 }, + nudge: { + maxContextLimitPct: 0.55, minContextLimitPct: 0.45, frequency: 5, + iterationThreshold: 15, force: "soft", growthRatio: 0.05, growthFloor: 6000, + growthCap: 50000, minGrowthFloor: 5000, minGrowthRatio: 0.45, emergencyThresholdPct: 0.98, + }, + promotionThreshold: 5, + truncate: { threshold: 1 }, + merge: { maxSummaryLength: 3000, minOldGenBlocks: 3 }, + compress: { minCompressRange: 0, maxSummaryLength: 0, minSummaryLength: 0, ...compress }, + protectedTools: [], + preserveRecentMessages: 0, + preserveRecentTokens: 0, + modelContextLimit: 100000, + } as Config; +} + +// One ~1000-token message (countTokens below is 1 token per char). +function run(summary: string, compress: Partial = {}) { + const core = createCore({ countTokens: (text: string) => text.length }); + const state = createInitialState(); + const messages = [msg("a", "x".repeat(1000)), msg("b", "tail")]; + state.messageRefs = assignRefs(messages, { existing: state.messageRefs, nextIndex: 1 }).map; + return core.applyCompression({ + ranges: [{ startRef: "m00001", endRef: "m00001", summary }], + messages, + state, + config: config(compress), + }).result; +} + +test("detectTruncatedSummary flags ellipsis tails and unclosed fences only", () => { + assert.equal(detectTruncatedSummary("done."), null); + assert.equal(detectTruncatedSummary("- item one\n- item two"), null); + assert.equal(detectTruncatedSummary("see:\n```ts\ncode\n```"), null); + assert.match(detectTruncatedSummary("verify comm…")!, /ellipsis/); + assert.match(detectTruncatedSummary("and then... ")!, /ellipsis/); + assert.match(detectTruncatedSummary("```ts\nconst a = 1")!, /fence/); +}); + +test("truncated summary is rejected by default", () => { + const r = run("Recorded findings, then verify comm…"); + assert.equal(r.blocksCreated, 0); + assert.match(r.errors[0]!, /Summary looks truncated \(ends with an ellipsis\)/); +}); + +test("rejectTruncatedSummary: false restores the old behavior", () => { + const r = run("Recorded findings, then verify comm…", { rejectTruncatedSummary: false }); + assert.equal(r.blocksCreated, 1); +}); + +test("minSummaryRatio rejects a summary too short for its range", () => { + const r = run("y".repeat(15) + ".", { minSummaryRatio: 0.02 }); + assert.equal(r.blocksCreated, 0); + assert.match(r.errors[0]!, /Summary too short for its range \(16 chars .*min 20/); +}); + +test("minSummaryRatio passes once the summary meets the floor", () => { + const r = run("y".repeat(25) + ".", { minSummaryRatio: 0.02 }); + assert.equal(r.blocksCreated, 1); +}); + +test("ratio floor is capped at half of maxSummaryLength", () => { + // 1000 tokens * 1.0 = 1000, but ceiling 40 caps the floor at 20. + const r = run("y".repeat(25) + ".", { minSummaryRatio: 1, maxSummaryLength: 40 }); + assert.equal(r.blocksCreated, 1); +}); + +test("minSummaryRatio is off by default", () => { + const r = run("short."); + assert.equal(r.blocksCreated, 1); +}); From fef29fe53833706183860ceafb0f964af1b39424 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sun, 15 Nov 2026 17:13:15 +0800 Subject: [PATCH 2/2] style: prettier format (fixes red format:check) The PR was committed with two lines over the 80-col print width, so format:check was red. Rewrap only; no logic change. Verified: 7/7 summary-integrity tests pass, tsc --noEmit clean, format:check green. --- src/compress.ts | 6 ++++- tests/summary-integrity.test.ts | 45 ++++++++++++++++++++++++++------- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/compress.ts b/src/compress.ts index 760318c..a7f1f1d 100644 --- a/src/compress.ts +++ b/src/compress.ts @@ -1417,7 +1417,11 @@ function applySingleRange(input: SingleRangeInput): SingleRangeOutcome { } // Direct-message tokens only (consumed blocks not yet added): the // proportional floor must not punish tier-2/3 distillation. - validateSummaryProportion(input.spec, input.config.compress, compressedTokens); + validateSummaryProportion( + input.spec, + input.config.compress, + compressedTokens, + ); for (const consumedId of consumedBlockIds) { const consumed = blockById(input.state, consumedId); if (consumed) { diff --git a/tests/summary-integrity.test.ts b/tests/summary-integrity.test.ts index 630f1dd..bc43e0e 100644 --- a/tests/summary-integrity.test.ts +++ b/tests/summary-integrity.test.ts @@ -13,14 +13,27 @@ function config(compress: Partial = {}): Config { return { tiers: { enabled: true, tier2Trigger: 5, tier3Trigger: 10 }, nudge: { - maxContextLimitPct: 0.55, minContextLimitPct: 0.45, frequency: 5, - iterationThreshold: 15, force: "soft", growthRatio: 0.05, growthFloor: 6000, - growthCap: 50000, minGrowthFloor: 5000, minGrowthRatio: 0.45, emergencyThresholdPct: 0.98, + maxContextLimitPct: 0.55, + minContextLimitPct: 0.45, + frequency: 5, + iterationThreshold: 15, + force: "soft", + growthRatio: 0.05, + growthFloor: 6000, + growthCap: 50000, + minGrowthFloor: 5000, + minGrowthRatio: 0.45, + emergencyThresholdPct: 0.98, }, promotionThreshold: 5, truncate: { threshold: 1 }, merge: { maxSummaryLength: 3000, minOldGenBlocks: 3 }, - compress: { minCompressRange: 0, maxSummaryLength: 0, minSummaryLength: 0, ...compress }, + compress: { + minCompressRange: 0, + maxSummaryLength: 0, + minSummaryLength: 0, + ...compress, + }, protectedTools: [], preserveRecentMessages: 0, preserveRecentTokens: 0, @@ -33,7 +46,10 @@ function run(summary: string, compress: Partial = {}) { const core = createCore({ countTokens: (text: string) => text.length }); const state = createInitialState(); const messages = [msg("a", "x".repeat(1000)), msg("b", "tail")]; - state.messageRefs = assignRefs(messages, { existing: state.messageRefs, nextIndex: 1 }).map; + state.messageRefs = assignRefs(messages, { + existing: state.messageRefs, + nextIndex: 1, + }).map; return core.applyCompression({ ranges: [{ startRef: "m00001", endRef: "m00001", summary }], messages, @@ -54,18 +70,26 @@ test("detectTruncatedSummary flags ellipsis tails and unclosed fences only", () test("truncated summary is rejected by default", () => { const r = run("Recorded findings, then verify comm…"); assert.equal(r.blocksCreated, 0); - assert.match(r.errors[0]!, /Summary looks truncated \(ends with an ellipsis\)/); + assert.match( + r.errors[0]!, + /Summary looks truncated \(ends with an ellipsis\)/, + ); }); test("rejectTruncatedSummary: false restores the old behavior", () => { - const r = run("Recorded findings, then verify comm…", { rejectTruncatedSummary: false }); + const r = run("Recorded findings, then verify comm…", { + rejectTruncatedSummary: false, + }); assert.equal(r.blocksCreated, 1); }); test("minSummaryRatio rejects a summary too short for its range", () => { const r = run("y".repeat(15) + ".", { minSummaryRatio: 0.02 }); assert.equal(r.blocksCreated, 0); - assert.match(r.errors[0]!, /Summary too short for its range \(16 chars .*min 20/); + assert.match( + r.errors[0]!, + /Summary too short for its range \(16 chars .*min 20/, + ); }); test("minSummaryRatio passes once the summary meets the floor", () => { @@ -75,7 +99,10 @@ test("minSummaryRatio passes once the summary meets the floor", () => { test("ratio floor is capped at half of maxSummaryLength", () => { // 1000 tokens * 1.0 = 1000, but ceiling 40 caps the floor at 20. - const r = run("y".repeat(25) + ".", { minSummaryRatio: 1, maxSummaryLength: 40 }); + const r = run("y".repeat(25) + ".", { + minSummaryRatio: 1, + maxSummaryLength: 40, + }); assert.equal(r.blocksCreated, 1); });