Skip to content
Open
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
56 changes: 55 additions & 1 deletion src/compress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,8 @@ function evaluateRefold(

/** In-place refold (#398): same block id/coverage/tier, new summary (+topic),
* fresh runId, restoredInline flag cleared. Summary passes the same length
* checks as a fresh compression. */
* and truncation checks as a fresh compression (the proportional floor does
* not apply: a refold has no new direct messages). */
function applyRefolds(input: {
spec: ApplyCompressionInput["ranges"][number];
state: CompressionState;
Expand All @@ -321,6 +322,7 @@ function applyRefolds(input: {
blockIds: string[];
}): void {
validateSummaryLength(input.spec, input.config.compress);
validateSummaryNotTruncated(input.spec, input.config.compress);
const targets = new Set(input.blockIds);
input.state.blocks = input.state.blocks.map((block) =>
targets.has(block.blockId)
Expand Down Expand Up @@ -1415,6 +1417,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 +1548,51 @@ 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;
}

/** Shared by fresh compression and in-place refold so a truncated summary
* cannot slip in through either path (#519). */
function validateSummaryNotTruncated(
spec: { summary: string },
cfg: Config["compress"],
): void {
if (cfg.rejectTruncatedSummary === false) return;
const why = detectTruncatedSummary(spec.summary?.trim() ?? "");
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.`,
);
}
}

function validateSummaryProportion(
spec: { summary: string; summaryMaxChars?: number },
cfg: Config["compress"],
directTokens: number,
): void {
validateSummaryNotTruncated(spec, cfg);
const summary = spec.summary?.trim() ?? "";
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
57 changes: 57 additions & 0 deletions tests/refold.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -772,3 +772,60 @@ test("a block-ref range (b1..b1) over a restored hidden block refolds in place",
assert.equal(block.restoredInline, false);
assert.equal(result.state.nextBlockId, 2, "no new block id allocated");
});

test("refold rejects a truncated summary and leaves the block untouched (#519)", () => {
const core = createCore();
const messages = makeMessages(10);
const ids = messages.map((m) => m.id);
const before = makeState(messages, [
makeBlock({
blockId: "b1",
effectiveMessageIds: ids,
startRef: "m00001",
endRef: "m00010",
}),
]);
const { state: marked } = markBlockRestoredInline(before, "b1");

for (const summary of [
`${NEW_SUMMARY} and then the next step was to verify comm…`,
`${NEW_SUMMARY}\n\`\`\`ts\nconst x = 1;`,
]) {
const result = core.applyCompression({
ranges: [{ startRef: "m00001", endRef: "m00010", summary }],
messages: [],
state: marked,
config: config(),
});
assert.equal(result.result.blocksCreated, 0);
assert.match(result.result.errors.join("\n"), /looks truncated/);
assert.equal(result.state.blocks[0]!.summary, OLD_SUMMARY);
assert.equal(result.state.blocks[0]!.restoredInline, true);
}
});

test("refold accepts a truncated-looking summary when rejectTruncatedSummary is false", () => {
const core = createCore();
const messages = makeMessages(10);
const before = makeState(messages, [
makeBlock({
blockId: "b1",
effectiveMessageIds: messages.map((m) => m.id),
startRef: "m00001",
endRef: "m00010",
}),
]);
const { state: marked } = markBlockRestoredInline(before, "b1");
const summary = `${NEW_SUMMARY} to be continued...`;
const base = config();
const result = core.applyCompression({
ranges: [{ startRef: "m00001", endRef: "m00010", summary }],
messages: [],
state: marked,
config: config({
compress: { ...base.compress, rejectTruncatedSummary: false },
}),
});
assert.deepEqual(result.result.errors, []);
assert.equal(result.state.blocks[0]!.summary, summary);
});
151 changes: 151 additions & 0 deletions tests/summary-integrity.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
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 { CompressionBlock, 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 },
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);
});

test("minSummaryRatio floor counts direct messages only, not consumed blocks", () => {
const core = createCore({ countTokens: (text: string) => text.length });
const state = createInitialState();
const messages = Array.from({ length: 10 }, (_, i) =>
msg(`m${i + 1}`, "x".repeat(100)),
);
state.messageRefs = assignRefs(messages, {
existing: state.messageRefs,
nextIndex: 1,
}).map;
const covered = messages.slice(0, 5).map((m) => m.id);
state.blocks.push({
blockId: "b1",
runId: "r1",
tier: 1,
summary: "y".repeat(400),
directMessageIds: covered,
effectiveMessageIds: covered,
directBlockIds: [],
compressedTokens: 500,
createdAt: 1,
survivedCount: 0,
generation: "young",
active: true,
} satisfies CompressionBlock);
// 300 chars clears the direct-only floor (5 × 100 tokens × 0.5 = 250) but
// would fail if the consumed block's summary tokens were counted too
// ((500 + 400) × 0.5 = 450).
const r = core.applyCompression({
ranges: [
{ startRef: "m00001", endRef: "m00010", summary: "y".repeat(300) },
],
messages,
state,
config: config({ minSummaryRatio: 0.5 }),
}).result;
assert.equal(r.errors.length, 0);
assert.equal(r.blocksCreated, 1);
});
Loading