Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/compress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1415,6 +1415,13 @@ 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) {
Expand Down Expand Up @@ -1539,6 +1546,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[],
Expand Down
10 changes: 10 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
112 changes: 112 additions & 0 deletions tests/summary-integrity.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
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["compress"]> = {}): 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<Config["compress"]> = {}) {
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);
});
Loading