Skip to content
Merged
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
3 changes: 2 additions & 1 deletion src/preflight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1372,7 +1372,8 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa
log: (msg) => deps.log("info", msg),
};
const creditBefore = deps.session.stats.compressCreditTokens;
const applied = applyRanges(parseCompressInput({ content: [{ startId: startRef, endId: endRef, summary, topic: "preflight overflow compress" }] }), ctx);
// #2146: internal lane — must not arm the model-facing loop breaker.
const applied = applyRanges(parseCompressInput({ content: [{ startId: startRef, endId: endRef, summary, topic: "preflight overflow compress" }] }), ctx, { loopTracking: false });
if (applied.outcome === "refused") {
deps.log("warn", `[preflight] ${applied.text}`);
noteSkip(`${skipKey}: apply failed — ${safePrefix(applied.text.replace(/^\[Compression FAILED[:\s]*/, ""), 200)}`);
Expand Down
130 changes: 123 additions & 7 deletions src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,86 @@ function clearCompressFailures(session: Session): void {
}
}

// #2146: the exact-spec repeat guard above is defeated by a model that walks
// spec space — one fresh (also failing) range per call. In the incident,
// glm-5.3-flash burned 2h42m on 161 DISTINCT failing specs after a client
// restart left stale-generation refs in its persisted history, never calling
// acp_status despite every receipt asking it to; the #1026/#1029 hints were
// in the receipts and were ignored. A session-level streak of consecutive
// TOTAL failures (any spec) catches that shape: at N failures the receipt
// switches from diagnostic hints to a hard-stop instruction. Hardcoded by
// design (config-surface discipline, cf. DEGENERATE_FOLD_COVERAGE). Disarm is
// success or 10 min of quiet — refs keep advancing DURING the loop (each
// failed round appends messages), so a frontier-based decay would disarm
// mid-loop and be defeated the same way.
const COMPRESS_LOOP_KEY = "compressFailStreak";
const COMPRESS_LOOP_THRESHOLD = 3;
const COMPRESS_LOOP_DECAY_MS = 10 * 60 * 1000;

type CompressLoopStreak = { n: number; lastAt: number };

function readCompressLoopStreak(session: Session): CompressLoopStreak | undefined {
const v = session.metadata[COMPRESS_LOOP_KEY];
if (!v || typeof v !== "object") return undefined;
const o = v as Record<string, unknown>;
if (typeof o["n"] !== "number" || !Number.isFinite(o["n"]) || typeof o["lastAt"] !== "number") return undefined;
return { n: Math.max(0, Math.floor(o["n"] as number)), lastAt: o["lastAt"] as number };
}

function writeCompressLoopStreak(session: Session, s: CompressLoopStreak | undefined): void {
if (s === undefined) delete session.metadata[COMPRESS_LOOP_KEY];
else session.metadata[COMPRESS_LOOP_KEY] = s;
markDirty(session);
}

/** #2146: record one total compress failure (any spec) and return "" while
* healthy, or the circuit-breaker paragraph to embed in the receipt once the
* streak arms. The breaker NEVER refuses execution — it only changes what a
* FAILED call tells the model, so a legitimate call can always still succeed
* and reset the streak via clearCompressLoopStreak. */
export function noteCompressLoopFailure(ctx: RewriteCtx, specLabel: string): string {
const now = Date.now();
let streak = readCompressLoopStreak(ctx.session);
if (streak && now - streak.lastAt > COMPRESS_LOOP_DECAY_MS) streak = undefined;
const n = (streak?.n ?? 0) + 1;
writeCompressLoopStreak(ctx.session, { n, lastAt: now });
if (n < COMPRESS_LOOP_THRESHOLD) return "";
const label = specLabel || "unparseable call";
if (n === COMPRESS_LOOP_THRESHOLD) {
ctx.log(`[warn: compress-loop] circuit breaker armed after ${n} consecutive compress failures (last spec: ${label}) — receipts now demand a full stop; disarms on success or ${COMPRESS_LOOP_DECAY_MS / 60000} min quiet`);
} else {
ctx.log(`[warn: compress-loop] failure ${n} in armed streak (last spec: ${label})`);
}
return ` [COMPRESS CIRCUIT BREAKER: ${n} consecutive compress failures in this session — every attempt has failed and further attempts will keep failing. STOP calling compress now: do not try other ranges, do not re-issue any previous range, and do not poll acp_status. Continue your actual task without compressing — compression happens again only when there is genuinely new content to fold.]`;
}

export function clearCompressLoopStreak(session: Session): void {
if (readCompressLoopStreak(session)) writeCompressLoopStreak(session, undefined);
}

/** #2146: every requested endpoint sits strictly ABOVE the session's highest
* mapped m-ref. Such refs cannot exist in this session (refs are never
* pre-allocated), so the kernel's generic "typo or wrong session" wording
* misleads models into hunting for the typo and re-anchoring on other stale
* artifacts. The deterministic cause is stale-generation refs baked into the
* client's persisted history — name it instead. */
export function beyondFrontierNote(state: Pick<CompressionState, "messageRefs">, ranges: Array<{ startRef: string; endRef: string }>): string {
let highest = 0;
for (const ref of Object.keys(state.messageRefs?.byRef ?? {})) {
const m = M_REF_NUM_RE.exec(ref);
if (m) highest = Math.max(highest, Number(m[1]));
}
if (highest <= 0 || ranges.length === 0) return "";
const allAbove = ranges.every((r) => {
const s = M_REF_NUM_RE.exec(r.startRef.trim());
const e = M_REF_NUM_RE.exec(r.endRef.trim());
return !!s && !!e && Number(s[1]) > highest && Number(e[1]) > highest;
});
if (!allAbove) return "";
const hi = `m${String(highest).padStart(5, "0")}`;
return ` All requested refs are above this session's highest ref (${hi}) — they cannot exist here (this session allocates no future refs). They are stale artifacts from an earlier session generation left in your visible history; NO superset of them can ever compress. Request only refs ≤ ${hi}, or call acp_status for the live ranges.`;
}

// #1029: after a session-generation change (client restart/fork starts a fresh
// session instance whose refs restart at m00001), stale refs from the previous
// generation fail resolution. The kernel gate points at acp_status; inline the
Expand Down Expand Up @@ -310,7 +390,11 @@ function applyErrorNote(r: { errors: string[] }): string {
// hosts that want to penalize this shape grep the warn marker below.
const DEGENERATE_FOLD_COVERAGE = 0.8;

export function applyRanges(parsed: ReturnType<typeof parseCompressInput>, ctx: RewriteCtx): ProxyToolResult {
export function applyRanges(parsed: ReturnType<typeof parseCompressInput>, ctx: RewriteCtx, opts?: { loopTracking?: boolean }): ProxyToolResult {
// #2146: preflight overflow-compress is bili's OWN internal lane — its
// failures have separate reporting (noteSkip) and must not arm the
// model-facing circuit breaker on attempts the model never made.
const trackLoop = opts?.loopTracking !== false;
const { ranges, diagnostics } = parsed;
if (ranges.length === 0) {
ctx.log("[acp-proxy: compress call had no valid ranges; nothing compressed.]");
Expand Down Expand Up @@ -344,23 +428,25 @@ export function applyRanges(parsed: ReturnType<typeof parseCompressInput>, ctx:
!isEmptyCall &&
argLen !== undefined &&
(diagnostics.kind === "malformed-json" || diagnostics.kind === "truncated");
const parseKey = `parse:${diagnostics.kind}:${diagnostics.invalidItems}:${rawReasons.slice(0, 3).join("|")}`;
const guard = recordCompressFailure(
ctx.session,
`parse:${diagnostics.kind}:${diagnostics.invalidItems}:${rawReasons.slice(0, 3).join("|")}`,
parseKey,
isEmptyCall
? "An empty call fails identically on every retry — drop it instead of re-issuing."
: argCorruption
? "Do not retry the same corrupt byte string — re-issue the call as one well-formed JSON object."
: "Fix the argument shape against the format below instead of re-issuing the same malformed call.",
);
const loopGuard = trackLoop ? noteCompressLoopFailure(ctx, parseKey) : "";
if (isEmptyCall) {
return compressResult(`[Compression FAILED: the call carried no content at all (kind=${diagnostics.kind}) — an empty compress() compresses nothing and can never succeed. Do NOT re-issue an empty call; if you meant to compress, put the non-empty 'content' array (elements {startId, endId, summary}) in that SAME single call.${guard}]`, "refused", 0);
return compressResult(`[Compression FAILED: the call carried no content at all (kind=${diagnostics.kind}) — an empty compress() compresses nothing and can never succeed. Do NOT re-issue an empty call; if you meant to compress, put the non-empty 'content' array (elements {startId, endId, summary}) in that SAME single call.${guard}${loopGuard}]`, "refused", 0);
}
if (argCorruption) {
const truncNote = diagnostics.kind === "truncated" ? " (looks truncated)" : "";
return compressResult(`[Compression FAILED: the call's arguments (${argLen} chars) were not parseable JSON${truncNote} — the intended content was lost and nothing was compressed. Re-issue the compress call as well-formed JSON: a single object with a non-empty 'content' array of {startId, endId, summary} elements.${guard}]`, "refused", 0);
return compressResult(`[Compression FAILED: the call's arguments (${argLen} chars) were not parseable JSON${truncNote} — the intended content was lost and nothing was compressed. Re-issue the compress call as well-formed JSON: a single object with a non-empty 'content' array of {startId, endId, summary} elements.${guard}${loopGuard}]`, "refused", 0);
}
return compressResult(`[Compression FAILED: no valid ranges parsed (kind=${diagnostics.kind}, dropped=${diagnostics.invalidItems}).${why} compress requires a non-empty 'content' array where each element is EITHER an object {startId, endId, summary} OR one line-form string whose first line is 'mNNNNN–mNNNNN optional topic' with the summary markdown on the following lines (a separate summary-only element right after a bare header line is also accepted). startId/endId are mNNNNN message refs from the conversation (call acp_status to see current refs).${compressibleSpanHint(ctx.session.state)} Re-issue the compress call with a valid content array.${guard}]`, "refused", 0);
return compressResult(`[Compression FAILED: no valid ranges parsed (kind=${diagnostics.kind}, dropped=${diagnostics.invalidItems}).${why} compress requires a non-empty 'content' array where each element is EITHER an object {startId, endId, summary} OR one line-form string whose first line is 'mNNNNN–mNNNNN optional topic' with the summary markdown on the following lines (a separate summary-only element right after a bare header line is also accepted). startId/endId are mNNNNN message refs from the conversation (call acp_status to see current refs).${compressibleSpanHint(ctx.session.state)} Re-issue the compress call with a valid content array.${guard}${loopGuard}]`, "refused", 0);
}
// #847: detect reversed refs as SUBMITTED, before #1001 normalization
// rewrites them (order matters — normalizeRangeOrder mutates in place).
Expand Down Expand Up @@ -449,9 +535,38 @@ export function applyRanges(parsed: ReturnType<typeof parseCompressInput>, ctx:
? ` This conversation holds only ${totalChars} char(s) — below the ${minChars}-char minimum, so NO range can succeed yet; do not retry compress or call acp_status/search_context about it — continue answering the user's task.`
: "";
const dropped = droppedEntriesNote(diagnostics);
return compressResult(`[Compression FAILED: ${errs}${revNote}${currentRefsSnapshot(ctx)}${recordCompressFailure(ctx.session, normalizedSpecKey(ranges))}${spanHint}${noViableAnywhere}${dropped ? " " + dropped : ""}${applyErrorNote(r)}]`, "refused", 0);
const specKey = normalizedSpecKey(ranges);
const repeatGuard = recordCompressFailure(ctx.session, specKey);
const loopGuard = trackLoop ? noteCompressLoopFailure(ctx, specKey) : "";
const beyond = beyondFrontierNote(ctx.session.state, ranges);
// #2146: once the breaker arms, the diagnostic hint cluster it used
// to lead with has proven unread by the looping model — swap it for
// the hard-stop paragraph instead of stacking more ignored advice.
const hints = loopGuard === ""
? `${beyond}${currentRefsSnapshot(ctx)}${repeatGuard}${spanHint}${noViableAnywhere}`
: `${beyond}${loopGuard}`;
let receipt = `[Compression FAILED: ${errs}${revNote}${hints}${dropped ? " " + dropped : ""}${applyErrorNote(r)}]`;
// #2146: when every requested ref is provably stale-generation, the
// kernel's per-range error text hands those exact phantom numbers
// straight back into the client-persisted history — where the
// looping model reads them as unfinished work and slides further up
// the same ladder (incident log: m29971–m30020 → m30021–m30070 → …
// across hours). Scrub the requested endpoints from the model-
// visible receipt so that echo channel dies; the ctx.log line above
// keeps the real refs for the operator. Live refs are untouched:
// when the note fires, every requested ref exceeds the session's
// highest mapped ref, so no live snapshot number can collide.
if (beyond !== "") {
for (const rg of ranges) {
for (const id of [rg.startRef, rg.endRef]) {
receipt = receipt.split(id).join("[stale-ref]");
}
}
}
return compressResult(receipt, "refused", 0);
}
clearCompressFailures(ctx.session);
clearCompressLoopStreak(ctx.session);

// #189 observability: record the rewrite magnitude + fold point so a
// downstream transient rejection (GLM 3007) can be correlated with it.
Expand Down Expand Up @@ -577,7 +692,8 @@ export function applyRanges(parsed: ReturnType<typeof parseCompressInput>, ctx:
return compressResult(msg, dropped !== "" || r.errors.length > 0 ? "partial" : "applied", r.blocksCreated);
} catch (err) {
ctx.log(`[acp-proxy: compress failed: ${String(err)}]`);
return compressResult(`[Compression FAILED: ${String(err)}${recordCompressFailure(ctx.session, normalizedSpecKey(ranges))}]`, "refused", 0);
const specKey = normalizedSpecKey(ranges);
return compressResult(`[Compression FAILED: ${String(err)}${recordCompressFailure(ctx.session, specKey)}${trackLoop ? noteCompressLoopFailure(ctx, specKey) : ""}]`, "refused", 0);
}
}

Expand Down
Loading
Loading