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
84 changes: 79 additions & 5 deletions src/compress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] };
Expand Down Expand Up @@ -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: [],
},
};
Expand All @@ -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: [],
},
};
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/persist/state-merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
12 changes: 12 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, number>;
/** Persistent reminders recorded via acp_rule (see rules.ts). Optional:
* pre-rules persisted states lack it. */
rules?: RuleRecord[];
Expand Down
6 changes: 6 additions & 0 deletions tests/persist.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading
Loading