diff --git a/src/compress.ts b/src/compress.ts index f95f76d..5b4db5c 100644 --- a/src/compress.ts +++ b/src/compress.ts @@ -229,6 +229,55 @@ function tierActionHint(config: Config, state: CompressionState): string { return ""; } +/** #311: failures of the SAME normalized range set recorded before the error + * output escalates. The first failure is counted silently; from the second + * on the model gets an explicit do-not-retry directive — a verbatim-repeating + * error is itself the strongest behavioral cue, which is what drove the 7× + * self-reinforcing loop of ranxianglei/billion-context#847. */ +const REPEAT_FAILURE_ESCALATE_AFTER = 2; + +/** Canonical per-range key: endpoints normalized through parseBoundary so + * m5 ≡ m00005, b7 ≡ b007, direction-insensitive (m9..m5 ≡ m5..m9); each + * endpoint keeps its kind so mN and bN never collide. Unparseable refs fall + * back to their trimmed lowercase literal (garbage input keeps its own key). */ +function normalizeRangeKey(spec: { startRef: string; endRef: string }): string { + const start = parseBoundary(spec.startRef); + const end = parseBoundary(spec.endRef); + if (!start || !end) { + return `${spec.startRef.trim().toLowerCase()}..${spec.endRef.trim().toLowerCase()}`; + } + const order = + start.numericId !== end.numericId + ? start.numericId - end.numericId + : start.kind.localeCompare(end.kind); + const [lo, hi] = order <= 0 ? [start, end] : [end, start]; + const tag = (kind: "message" | "block", n: number): string => + `${kind === "message" ? "m" : "b"}${n}`; + return `${tag(lo.kind, lo.numericId)}..${tag(hi.kind, hi.numericId)}`; +} + +/** Whole-request key: per-range keys sorted, so batch order never matters. */ +function normalizeRangeSetKey(ranges: ApplyCompressionInput["ranges"]): string { + return ranges.map(normalizeRangeKey).sort().join("|"); +} + +function repeatFailureEscalation(count: number): string { + return `Repeat-failure guard: you have now requested this exact range set ${count} time(s); it deterministically fails. Do NOT re-issue it. Call acp_status first and pick from its CURRENT compressible ranges.`; +} + +/** #311: record one more failed attempt of this exact range set on the + * working (cloned) state; returns the new count for that key. */ +function recordRepeatFailure( + state: CompressionState, + ranges: ApplyCompressionInput["ranges"], +): number { + const key = normalizeRangeSetKey(ranges); + const prev = state.repeatFailures ?? {}; + const count = (prev[key] ?? 0) + 1; + state.repeatFailures = { ...prev, [key]: count }; + return count; +} + type RefoldDecision = | { kind: "refold"; blocks: CompressionBlock[] } | { kind: "blocked"; reasons: string[] }; @@ -558,19 +607,24 @@ export function createCore(ports: Ports = {}): CompressionCore { ? `Requested range(s) cannot be anchored (e.g. ${firstConsumed!.startRef}..${firstConsumed!.endRef}) — the refs exist in this session's ref map, but the messages they point to are no longer in the visible context and no active block covers them: the message content changed (or the message was filtered out of the view) and now carries a new ref, leaving your old refs dangling. ${diagnostics} Run acp_status, then call the compress tool again using only the refs it reports.` : `Requested range(s) already compressed (e.g. ${firstConsumed!.startRef}..${firstConsumed!.endRef}) — ${coverDetail}${refoldDetail}. Nothing new to compress in that window. ${diagnostics} Continue the task, or run acp_status and target one of the CURRENT compressible ranges it reports.${tierActionHint(input.config, state)}` : countedRanges > 0 - ? `Total compressible content too small (${totalRangeChars} chars across ${countedRanges} range(s), min ${input.config.compress.minCompressRange}). Combine more messages into your range(s) to meet the threshold.${refoldSuffix(okBlockedReasons)}` + ? `Total compressible content too small (${totalRangeChars} chars across ${countedRanges} range(s), min ${input.config.compress.minCompressRange}). Combine more messages into your range(s) to meet the threshold — or run acp_status and target one of the CURRENT compressible ranges it reports, instead of re-issuing a range that keeps coming up short.${refoldSuffix(okBlockedReasons)}` : null; if (gateMessage === null) { // No range was counted (every spec failed classification, e.g. // unparseable refs). The per-range errors name the real cause; a // "too small" verdict here would mislead the model into combining // more messages instead of fixing its refs (#310). + const repeatCount = recordRepeatFailure(state, input.ranges); + const gateErrors = + repeatCount >= REPEAT_FAILURE_ESCALATE_AFTER + ? [repeatFailureEscalation(repeatCount), ...classificationErrors] + : [...classificationErrors]; return { - state: input.state, + state, result: { blocksCreated: 0, tokensCompressed: 0, - errors: [...classificationErrors], + errors: gateErrors, warnings: [], }, }; @@ -586,12 +640,21 @@ export function createCore(ports: Ports = {}): CompressionCore { gateMessage += ` ${reversalNotes.join(" ")}`; } + const repeatCount = recordRepeatFailure(state, input.ranges); + const gateErrors = + repeatCount >= REPEAT_FAILURE_ESCALATE_AFTER + ? [ + repeatFailureEscalation(repeatCount), + gateMessage, + ...classificationErrors, + ] + : [gateMessage, ...classificationErrors]; return { - state: input.state, + state, result: { blocksCreated: 0, tokensCompressed: 0, - errors: [gateMessage, ...classificationErrors], + errors: gateErrors, warnings: [], }, }; @@ -678,6 +741,14 @@ export function createCore(ports: Ports = {}): CompressionCore { // the terminal-floor streak so the escape signal can only fire again // after N fresh stuck events (#300). state.terminalStreak = 0; + // Any success invalidates prior failure counts (#311): the context + // moved, so a previously-deterministic range set may now be viable. + state.repeatFailures = {}; + } else { + const repeatCount = recordRepeatFailure(state, input.ranges); + if (repeatCount >= REPEAT_FAILURE_ESCALATE_AFTER) { + errors.unshift(repeatFailureEscalation(repeatCount)); + } } return { @@ -1995,6 +2066,9 @@ function cloneState(state: CompressionState): CompressionState { rules: (state.rules ?? []).map((rule) => ({ ...rule })), nextRuleId: state.nextRuleId, terminalStreak: state.terminalStreak, + repeatFailures: state.repeatFailures + ? { ...state.repeatFailures } + : undefined, nextBlockId: state.nextBlockId, nextRunId: state.nextRunId, hiddenOrphanRefs: state.hiddenOrphanRefs diff --git a/src/persist/state-merge.ts b/src/persist/state-merge.ts index 456c26b..87b2a99 100644 --- a/src/persist/state-merge.ts +++ b/src/persist/state-merge.ts @@ -25,6 +25,9 @@ export function mergeCompressionState( rules: parsed.rules ?? fresh.rules, nextRuleId: parsed.nextRuleId ?? fresh.nextRuleId, terminalStreak: parsed.terminalStreak, + repeatFailures: parsed.repeatFailures, + hiddenOrphanRefs: parsed.hiddenOrphanRefs, + lastPassIds: parsed.lastPassIds, imageFullRestored: parsed.imageFullRestored ?? fresh.imageFullRestored, imageShrinks: parsed.imageShrinks ?? fresh.imageShrinks, }; diff --git a/src/types.ts b/src/types.ts index 17e8d0f..a29d0a7 100644 --- a/src/types.ts +++ b/src/types.ts @@ -264,6 +264,18 @@ export interface CompressionState { * threshold, viable compression appearing, truncation savings, or a * successful applyCompression. Optional for pre-existing persisted states. */ terminalStreak?: number; + /** Repeat-failure guard (#311): normalized range-set key → how many times + * this exact request has already failed in this session. Incremented on + * every applyCompression call that creates no block; once the count + * reaches 2 the returned errors gain a leading "Repeat-failure guard" + * directive telling the model to stop re-issuing the range and consult + * acp_status instead — the verbatim-repeating error is what drove the + * self-reinforcing retry loop of ranxianglei/billion-context#847. Cleared + * entirely on any successful compression (the context moved). Lives in + * state so plugin-mode hosts, which persist the returned state and own no + * error path of their own, get the circuit breaker from the kernel. + * Optional: pre-feature persisted states lack it. */ + repeatFailures?: Record; /** Persistent reminders recorded via acp_rule (see rules.ts). Optional: * pre-rules persisted states lack it. */ rules?: RuleRecord[]; diff --git a/tests/persist.test.ts b/tests/persist.test.ts index b652c44..132a3d2 100644 --- a/tests/persist.test.ts +++ b/tests/persist.test.ts @@ -428,9 +428,15 @@ test("mergeCompressionState keeps parsed values over defaults", () => { ...createInitialState(), nextBlockId: 41, stats: { tokensCompressed: 1234, compressionCount: 7 }, + hiddenOrphanRefs: ["m00009"], + lastPassIds: ["raw-1", "raw-2"], + repeatFailures: { "m2..m3": 2 }, }; const merged = mergeCompressionState(parsed); assert.equal(merged.nextBlockId, 41); + assert.deepEqual(merged.hiddenOrphanRefs, ["m00009"]); + assert.deepEqual(merged.lastPassIds, ["raw-1", "raw-2"]); + assert.deepEqual(merged.repeatFailures, { "m2..m3": 2 }); assert.deepEqual(merged.stats, { tokensCompressed: 1234, compressionCount: 7, diff --git a/tests/repeat-failure-guard.test.ts b/tests/repeat-failure-guard.test.ts new file mode 100644 index 0000000..c3ba0be --- /dev/null +++ b/tests/repeat-failure-guard.test.ts @@ -0,0 +1,337 @@ +/** + * Regression: #311 — repeated identical compression failures must escalate + * instead of repeating a verbatim error forever (the 7× self-reinforcing loop + * of ranxianglei/billion-context#847), and the size-gate fallback must point + * at acp_status instead of only saying "combine more messages". + */ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { createCore } from "../src/compress.js"; +import { createInitialState } from "../src/state.js"; +import { prune } from "../src/prune.js"; +import { assignRefs } from "../src/refs.js"; +import { mergeCompressionState } from "../src/persist/state-merge.js"; +import type { Config, CoreMessage, CompressionState } from "../src/types.js"; + +function msg( + id: string, + text: string, + role: CoreMessage["role"] = "user", +): CoreMessage { + return { id, role, contentType: "text", text }; +} + +function config(overrides: 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: 5000, + maxSummaryLength: 0, + minSummaryLength: 0, + }, + protectedTools: [], + preserveRecentMessages: 0, + preserveRecentTokens: 0, + modelContextLimit: 100000, + ...overrides, + }; +} + +function seededState(messages: CoreMessage[]): CompressionState { + const state = createInitialState(); + state.messageRefs = assignRefs(messages, { + existing: state.messageRefs, + nextIndex: 1, + }).map; + return state; +} + +test("repeat identical too-small rejection escalates from the second attempt (#311)", () => { + const core = createCore(); + const messages = [msg("u", "the task"), msg("a", "alpha"), msg("b", "beta")]; + let state = seededState(messages); + + const first = core.applyCompression({ + ranges: [{ startRef: "m00002", endRef: "m00003", summary: "small" }], + messages, + state, + config: config(), + }); + assert.equal(first.result.blocksCreated, 0); + assert.equal(first.result.errors.length, 1); + assert.match(first.result.errors[0]!, /Total compressible content too small/); + assert.doesNotMatch(first.result.errors[0]!, /Repeat-failure guard/); + assert.match( + first.result.errors[0]!, + /run acp_status and target one of the CURRENT compressible ranges/, + ); + assert.deepEqual(first.state.repeatFailures, { "m2..m3": 1 }); + + const second = core.applyCompression({ + ranges: [{ startRef: "m00002", endRef: "m00003", summary: "small" }], + messages, + state: first.state, + config: config(), + }); + assert.equal(second.result.blocksCreated, 0); + assert.equal(second.result.errors.length, 2); + assert.match( + second.result.errors[0]!, + /Repeat-failure guard: you have now requested this exact range set 2 time\(s\); it deterministically fails/, + ); + assert.match(second.result.errors[0]!, /Do NOT re-issue it/); + assert.match(second.result.errors[0]!, /Call acp_status first/); + assert.match( + second.result.errors[1]!, + /Total compressible content too small/, + ); + assert.deepEqual(second.state.repeatFailures, { "m2..m3": 2 }); + + const third = core.applyCompression({ + ranges: [{ startRef: "m00002", endRef: "m00003", summary: "small" }], + messages, + state: second.state, + config: config(), + }); + assert.match(third.result.errors[0]!, /3 time\(s\)/); +}); + +test("counter keys are canonical: padded refs, direction, and batch order all collapse (#311)", () => { + const core = createCore(); + const messages = [ + msg("u", "the task"), + msg("a", "alpha"), + msg("b", "beta"), + msg("c", "gamma"), + msg("d", "delta"), + ]; + + let state = seededState(messages); + const r1 = core.applyCompression({ + ranges: [{ startRef: "m00002", endRef: "m00003", summary: "x" }], + messages, + state, + config: config(), + }); + assert.deepEqual(r1.state.repeatFailures, { "m2..m3": 1 }); + + const r2 = core.applyCompression({ + ranges: [{ startRef: "m3", endRef: "m2", summary: "x" }], + messages, + state: r1.state, + config: config(), + }); + assert.match(r2.result.errors[0]!, /Repeat-failure guard/); + assert.deepEqual(r2.state.repeatFailures, { "m2..m3": 2 }); + + let s2 = seededState(messages); + const b1 = core.applyCompression({ + ranges: [ + { startRef: "m00002", endRef: "m00003", summary: "x" }, + { startRef: "m00004", endRef: "m00005", summary: "y" }, + ], + messages, + state: s2, + config: config(), + }); + assert.deepEqual(b1.state.repeatFailures, { "m2..m3|m4..m5": 1 }); + + const b2 = core.applyCompression({ + ranges: [ + { startRef: "m00004", endRef: "m00005", summary: "y" }, + { startRef: "m00002", endRef: "m00003", summary: "x" }, + ], + messages, + state: b1.state, + config: config(), + }); + assert.match(b2.result.errors[0]!, /Repeat-failure guard/); + assert.deepEqual(b2.state.repeatFailures, { "m2..m3|m4..m5": 2 }); +}); + +test("any successful compression clears the whole failure table (#311)", () => { + const core = createCore(); + const big = "x".repeat(6000); + const messages = [ + msg("u", "the task"), + msg("a", "alpha"), + msg("b", "beta"), + msg("c", big), + ]; + let state = seededState(messages); + + const f1 = core.applyCompression({ + ranges: [{ startRef: "m00002", endRef: "m00003", summary: "small" }], + messages, + state, + config: config(), + }); + const f2 = core.applyCompression({ + ranges: [{ startRef: "m00002", endRef: "m00003", summary: "small" }], + messages, + state: f1.state, + config: config(), + }); + assert.match(f2.result.errors[0]!, /Repeat-failure guard/); + + const ok = core.applyCompression({ + ranges: [ + { startRef: "m00004", endRef: "m00004", summary: big.slice(0, 20) }, + ], + messages, + state: f2.state, + config: config(), + }); + assert.equal(ok.result.blocksCreated, 1); + assert.deepEqual(ok.state.repeatFailures, {}); + + const retry = core.applyCompression({ + ranges: [{ startRef: "m00002", endRef: "m00003", summary: "small" }], + messages, + state: ok.state, + config: config(), + }); + assert.doesNotMatch(retry.result.errors[0]!, /Repeat-failure guard/); + assert.deepEqual(retry.state.repeatFailures, { "m2..m3": 1 }); +}); + +test("distinct range sets are counted independently (#311)", () => { + const core = createCore(); + const messages = [ + msg("u", "the task"), + msg("a", "alpha"), + msg("b", "beta"), + msg("c", "gamma"), + ]; + let state = seededState(messages); + + const a1 = core.applyCompression({ + ranges: [{ startRef: "m00002", endRef: "m00002", summary: "x" }], + messages, + state, + config: config(), + }); + const b1 = core.applyCompression({ + ranges: [{ startRef: "m00003", endRef: "m00003", summary: "y" }], + messages, + state: a1.state, + config: config(), + }); + assert.deepEqual(b1.state.repeatFailures, { "m2..m2": 1, "m3..m3": 1 }); + + const a2 = core.applyCompression({ + ranges: [{ startRef: "m00002", endRef: "m00002", summary: "x" }], + messages, + state: b1.state, + config: config(), + }); + assert.match(a2.result.errors[0]!, /Repeat-failure guard/); + assert.deepEqual(a2.state.repeatFailures, { "m2..m2": 2, "m3..m3": 1 }); +}); + +test("already-compressed no-op retries also count and escalate (#311)", () => { + const core = createCore(); + const messages = [ + msg("u", "the task"), + msg("a", "alpha"), + msg("b", "beta"), + msg("c", "gamma"), + ]; + let state = seededState(messages); + + const { state: after } = core.applyCompression({ + ranges: [{ startRef: "m00002", endRef: "m00003", summary: "intro recap" }], + messages, + state, + config: config({ + compress: { + minCompressRange: 0, + maxSummaryLength: 0, + minSummaryLength: 0, + }, + }), + }); + assert.deepEqual(after.repeatFailures, {}); + const pruned = prune(messages, after); + + const r1 = core.applyCompression({ + ranges: [{ startRef: "m00002", endRef: "m00003", summary: "intro recap" }], + messages: pruned, + state: after, + config: config(), + }); + assert.equal(r1.result.blocksCreated, 0); + assert.equal(r1.result.errors.length, 1); + assert.match(r1.result.errors[0]!, /already compressed/); + assert.doesNotMatch(r1.result.errors[0]!, /Repeat-failure guard/); + assert.deepEqual(r1.state.repeatFailures, { "m2..m3": 1 }); + + const r2 = core.applyCompression({ + ranges: [{ startRef: "m00002", endRef: "m00003", summary: "intro recap" }], + messages: pruned, + state: r1.state, + config: config(), + }); + assert.equal(r2.result.errors.length, 2); + assert.match(r2.result.errors[0]!, /Repeat-failure guard/); + assert.match(r2.result.errors[1]!, /already compressed/); +}); + +test("unparseable-ref requests count and escalate on the zero-counted gate (#311)", () => { + const core = createCore(); + const messages = [msg("u", "the task"), msg("a", "alpha"), msg("b", "beta")]; + let state = seededState(messages); + + const g1 = core.applyCompression({ + ranges: [{ startRef: "zzz", endRef: "qqq", summary: "garbage" }], + messages, + state, + config: config(), + }); + assert.equal(g1.result.blocksCreated, 0); + assert.equal(g1.result.errors.length, 1); + assert.match(g1.result.errors[0]!, /Invalid boundary ref/); + assert.doesNotMatch(g1.result.errors[0]!, /Repeat-failure guard/); + assert.deepEqual(g1.state.repeatFailures, { "zzz..qqq": 1 }); + + const g2 = core.applyCompression({ + ranges: [{ startRef: "ZZZ", endRef: "qqq", summary: "garbage" }], + messages, + state: g1.state, + config: config(), + }); + assert.equal(g2.result.errors.length, 2); + assert.match( + g2.result.errors[0]!, + /Repeat-failure guard: you have now requested this exact range set 2 time\(s\)/, + ); + assert.match(g2.result.errors[1]!, /Invalid boundary ref/); + assert.deepEqual(g2.state.repeatFailures, { "zzz..qqq": 2 }); +}); + +test("mergeCompressionState carries repeatFailures through load (#311)", () => { + const withCounter = mergeCompressionState({ + ...createInitialState(), + repeatFailures: { "m2..m3": 2 }, + }); + assert.deepEqual(withCounter.repeatFailures, { "m2..m3": 2 }); + + const without = mergeCompressionState(createInitialState()); + assert.equal(without.repeatFailures, undefined); +});