Files
ranxianglei bc18783719 kernel: already-folded/protected compress ranges become a success no-op, not FAILED (#4521)
Long sessions hit "compression FAILED, retry" loops: the model re-reported
ranges already covered by live blocks, guessed endIds beyond the highest
allocated ref, or aimed entirely inside the protected zone. Every one of
those "nothing left to fold" rounds was classified FAILED — the atomic batch
was refused whole, the breaker counted strikes, and the failure text taught
the model to guess again (live session 01a18a4d: 4 consecutive FAILED at
20:09 before one succeeded). Five fixes:

- endId beyond the highest allocated ref clamps down to it (warning, not a
  batch refusal)
- a range fully covering live blocks is an idempotent no-op success receipt
  (result.noop: true, warning points at bN..bM rewrite instead of resending
  originals) — no failure, no breaker arm
- pure protected-zone ranges return the same no-op shape ("do not resend")
- protected-zone fatals downgrade that range instead of aborting the batch
- new armed-but-silent status line: when a tiered nudge arm is deferred
  (cadence/backoff/model-decided timing) or a banner was already shown, an
  ephemeral trailing user message refreshes "highest allocated ref +
  compressible ranges" so the model stops guessing refs; fresh
  below-threshold sessions stay fully silent (#196 regime scoping, #3910
  lane baseline)

kernel-change: compress result contract gains noop + warnings semantics
(no new config surface); processTurn gains the statusLine refresh. Hosts
render the no-op receipt and append the status line on the
anthropic/openai/google/responses lanes.
2026-10-31 00:40:56 +08:00

304 lines
9.2 KiB
TypeScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { createCore } from "../src/compress.js";
import { createInitialState } from "../src/state.js";
import { assignRefs } from "../src/refs.js";
import { defaultConfig } from "../src/config.js";
import type { CoreMessage } from "../src/types.js";
function msg(
id: string,
text: string,
role: CoreMessage["role"] = "user",
): CoreMessage {
return { id, role, contentType: "text", text };
}
function setupRefs(messages: CoreMessage[]) {
const state = createInitialState();
state.messageRefs = assignRefs(messages, {
existing: state.messageRefs,
nextIndex: 1,
}).map;
return state;
}
const longText = "x".repeat(6000);
const validSummary =
"This is a meaningful summary that captures the key information of the compressed range including paths and decisions.";
test("validation: empty summary is rejected", () => {
const core = createCore();
const messages = [msg("a", longText), msg("b", longText)];
const state = setupRefs(messages);
const config = defaultConfig(200000, {
preserveRecentMessages: 0,
preserveRecentTokens: 0,
});
const result = core.applyCompression({
ranges: [{ startRef: "m00001", endRef: "m00002", summary: "" }],
messages,
state,
config,
});
assert.equal(result.result.blocksCreated, 0);
assert.equal(result.result.errors.length, 1);
assert.match(result.result.errors[0]!, /empty/i);
});
test("validation: whitespace-only summary is rejected", () => {
const core = createCore();
const messages = [msg("a", longText), msg("b", longText)];
const state = setupRefs(messages);
const config = defaultConfig(200000, {
preserveRecentMessages: 0,
preserveRecentTokens: 0,
});
const result = core.applyCompression({
ranges: [{ startRef: "m00001", endRef: "m00002", summary: " \n\t " }],
messages,
state,
config,
});
assert.equal(result.result.blocksCreated, 0);
assert.match(result.result.errors[0]!, /empty/i);
});
test("validation: summary below minSummaryLength is rejected", () => {
const core = createCore();
const messages = [msg("a", longText), msg("b", longText)];
const state = setupRefs(messages);
const config = defaultConfig(200000, {
preserveRecentMessages: 0,
preserveRecentTokens: 0,
compress: {
minCompressRange: 0,
maxSummaryLength: 0,
minSummaryLength: 100,
},
});
const result = core.applyCompression({
ranges: [{ startRef: "m00001", endRef: "m00002", summary: "too short" }],
messages,
state,
config,
});
assert.equal(result.result.blocksCreated, 0);
assert.match(result.result.errors[0]!, /too short/i);
});
test("validation: summary above maxSummaryLength is rejected", () => {
const core = createCore();
const messages = [msg("a", longText), msg("b", longText)];
const state = setupRefs(messages);
const config = defaultConfig(200000, {
preserveRecentMessages: 0,
preserveRecentTokens: 0,
compress: {
minCompressRange: 0,
maxSummaryLength: 50,
minSummaryLength: 0,
},
});
const result = core.applyCompression({
ranges: [{ startRef: "m00001", endRef: "m00002", summary: validSummary }],
messages,
state,
config,
});
assert.equal(result.result.blocksCreated, 0);
assert.match(result.result.errors[0]!, /too long/i);
});
test("validation: range below minCompressRange is rejected", () => {
const core = createCore();
const messages = [msg("a", "short"), msg("b", "text")];
const state = setupRefs(messages);
const config = defaultConfig(200000, {
preserveRecentMessages: 0,
preserveRecentTokens: 0,
compress: {
minCompressRange: 5000,
maxSummaryLength: 0,
minSummaryLength: 0,
},
});
const result = core.applyCompression({
ranges: [{ startRef: "m00001", endRef: "m00002", summary: validSummary }],
messages,
state,
config,
});
assert.equal(result.result.blocksCreated, 0);
assert.match(result.result.errors[0]!, /too small/i);
});
test("validation: batch aggregate — many small ranges pass if total >= minCompressRange", () => {
const core = createCore();
const chunk = "x".repeat(1200);
const messages = [
msg("a", chunk),
msg("b", chunk),
msg("c", chunk),
msg("d", chunk),
msg("e", chunk),
];
const state = setupRefs(messages);
const config = defaultConfig(200000, {
preserveRecentMessages: 0,
preserveRecentTokens: 0,
compress: {
minCompressRange: 5000,
maxSummaryLength: 0,
minSummaryLength: 0,
},
});
// 5 separate ranges, each 1200 chars — individually below 5000 but total 6000
const result = core.applyCompression({
ranges: [
{ startRef: "m00001", endRef: "m00001", summary: validSummary },
{ startRef: "m00002", endRef: "m00002", summary: validSummary },
{ startRef: "m00003", endRef: "m00003", summary: validSummary },
{ startRef: "m00004", endRef: "m00004", summary: validSummary },
{ startRef: "m00005", endRef: "m00005", summary: validSummary },
],
messages,
state,
config,
});
assert.equal(result.result.blocksCreated, 5);
assert.equal(result.result.errors.length, 0);
});
test("validation: range with zero compressible messages (covered by non-nested block) is a covered no-op", () => {
const core = createCore();
const messages = [
msg("a", longText),
msg("b", longText),
msg("c", longText),
msg("d", longText),
];
let state = setupRefs(messages);
const config = defaultConfig(200000, {
preserveRecentMessages: 0,
preserveRecentTokens: 0,
});
const first = core.applyCompression({
ranges: [{ startRef: "m00001", endRef: "m00003", summary: validSummary }],
messages,
state,
config,
});
state = first.state;
// m00002→m00003 maps to b,c. Block b1 covers a,b,c but its anchor is
// at index 0 (a), outside range [1,2]. So b1 is NOT nested → consumedBlockCount=0.
// But b,c are in preExistingCoverage → directMessageIds=[]. #4521: folds
// nothing — reported as the idempotent covered no-op (0 blocks, 0 errors)
// instead of a batch rejection the model would retry verbatim.
const second = core.applyCompression({
ranges: [{ startRef: "m00002", endRef: "m00003", summary: validSummary }],
messages,
state,
config,
});
assert.equal(second.result.blocksCreated, 0);
assert.equal(second.result.errors.length, 0);
assert.equal(second.result.noop, true);
// b1 is NOT nested inside the range, so its covered-content filter (not the
// covered-blocks list) removes b,c — the wording is the filtered variant of
// the same no-op class.
assert.match(
second.result.warnings.join(" "),
/already covered by active block\(s\)|protected or filtered/,
);
});
test("validation: block-boundary compress with 0 direct msgs but consumed blocks is allowed", () => {
const core = createCore();
const messages = [msg("a", longText), msg("b", longText), msg("c", longText)];
let state = setupRefs(messages);
const config = defaultConfig(200000, {
preserveRecentMessages: 0,
preserveRecentTokens: 0,
});
const t1 = core.applyCompression({
ranges: [{ startRef: "m00001", endRef: "m00002", summary: validSummary }],
messages,
state,
config,
});
state = t1.state;
const t2 = core.applyCompression({
ranges: [{ startRef: "b1", endRef: "b1", summary: validSummary }],
messages,
state,
config,
});
assert.equal(t2.result.blocksCreated, 1);
assert.equal(t2.result.errors.length, 0);
assert.equal(t2.state.blocks[1]!.tier, 2);
});
test("validation: all checks disabled (0s) allows any input", () => {
const core = createCore();
const messages = [msg("a", "x"), msg("b", "y")];
const state = setupRefs(messages);
const config = defaultConfig(200000, {
preserveRecentMessages: 0,
preserveRecentTokens: 0,
compress: { minCompressRange: 0, maxSummaryLength: 0, minSummaryLength: 0 },
});
const result = core.applyCompression({
ranges: [{ startRef: "m00001", endRef: "m00002", summary: "ok" }],
messages,
state,
config,
});
assert.equal(result.result.blocksCreated, 1);
assert.equal(result.result.errors.length, 0);
});
test("validation: an empty-summary range rejects the whole batch (#3003)", () => {
const core = createCore();
const messages = [
msg("a", longText),
msg("b", longText),
msg("c", longText),
msg("d", longText),
];
const state = setupRefs(messages);
const config = defaultConfig(200000, {
preserveRecentMessages: 0,
preserveRecentTokens: 0,
});
const result = core.applyCompression({
ranges: [
{ startRef: "m00001", endRef: "m00002", summary: "" },
{ startRef: "m00003", endRef: "m00004", summary: validSummary },
],
messages,
state,
config,
});
// #3003: a length-class failure (empty summary) in ANY candidate rejects the
// WHOLE batch — the valid sibling no longer lands. Content-class failures
// (unknown/invalid refs) remain per-range; see the invalid-refs batch test.
assert.equal(result.result.blocksCreated, 0);
assert.equal(result.state.blocks.length, 0);
assert.equal(result.result.errors.length, 1);
assert.match(result.result.errors[0]!, /Batch rejected before folding any range/);
assert.match(result.result.errors[0]!, /empty/i);
});