diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d50f4a635..905fc88bd6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Versions follow the merge of a `*_release-v*` branch; CI publishes to npm on tag ## [Unreleased] ### Fixed +- **压缩「总是失败、要重试很多次才成功」的活锁(#4521)**:长会话里模型重报已被折叠覆盖的区间、或把 endId 猜到尚未发号的 ref 之上、或整段落在保护区,这些「其实无事可做」的请求一律被判 FAILED——原子批整体拒绝、断路器计数、模型照失败提示再猜再试,形成「失败→重试→再失败」循环(实测会话 01a18a4d 在 20:09 连续 4 次 FAILED 才成功)。现在五处修正:①endId 越界自动钳到最高已发 ref(警告提示,不整批拒);②区间全覆盖既有活动块=幂等 no-op 成功收据(`noop: true`,提示用 bN..bM 改写而非重发原文),不失败、不进断路器;③纯保护区区间同样 no-op(警告明说勿重发);④保护区 fatal 不再中止整批,只降级该区间;⑤新增 armed-but-silent 状态行:tier 级 nudge 已备好但本轮不投横幅时(退避/节拍/模型定时),以临时 user 消息刷新「最高已发 ref + 当前可压区间」,模型不再凭记忆猜号;全新会话的低于阈值计划保持完全静默(#196 域隔离、#3910 基线不破坏)。 - **自动升级换代旋转的「假存活」误判(#5167)**:旧实例接受 `/__bili/shutdown` 后毫秒级就关 listener(口已释放),但存量 keep-alive 连接照常应答——这是排水的本意(#3890 后最坏 (1+10)×10s 才退进程)。而两条旋转等待(launcher `rotateStalePredecessor` 每 200ms `fetchHealthInfo`、dsh `rotatePinnedStaleProxy` 每 500ms `fetchProxyVersion`)都用全局 fetch 的连接池轮询,轮询节拍远小于服务端 5s keepAliveTimeout,永远落在预热过的池化 socket 上 → 把「正在排水」误判为「没退出」,12s 预算耗尽得到假阴性:dsh 宿主 attach-anyway 骑上将被强退的旧实例、launcher 回退 spawn 阶梯,新旧两进程同写一份 machine-global 会话存储(index.db 双写横幅、#5046 SQLITE_BUSY、JSON 状态互踩)。修复:旋转等待的全部请求(含 shutdown POST)改带 `Connection: close` 每轮全新连接,「应答」重新等于「listener 还在收新连接」=真正的口释放信号;同步修正 12s 预算的过时注释(它只需覆盖 shutdown 请求被处理的延迟,不需覆盖进程退出)。`tests/issue5167-drain-aware-rotation.test.ts` 用真实排水 http 服务器(无 fetch mock、单连接 dispatcher 钉住池化拓扑)双向钉住:排水中的 resident 必须判为已释放,真占口不退的 holder 仍走超时回退。 - **shadow 摘要漂移的单实例成因(#4392 / #4986)**:`canonicalize` 曾按 `Object.entries` 插入序参与哈希,而 live 腿(kernel 状态,`byRef` 按发号顺序)与 replay 腿(`loadLegacyView` 按账本 `ord` 重建 `messageRefs`)对同一份等值状态给出不同键序 → 单进程自我报告 `[diverged: state]`,连续 3 次即冻结本进程的信封写入并清掉入库水位。现在任意深度按键排序(数组保序,Map/Set 形态不变),跳过的键(去掉 `storageMode`/`tailGaps`)集中成 `canonical.ts` 导出的 `DIGEST_SKIPPED_KEYS`,供流式摘要路径共用;升级文案不再断言「多个 bili 实例」。storage-corpus manifest 随摘要定义重钉(只有 10 个摘要值变化,文件清单/格式不动);storage-render 金标无需改动——那套驱动在 harness 侧已先 `sortObjectKeys`,排键只改 shadow 腿与 corpus 摘要。`tests/issue4986-digest-drift-ref-order.test.ts`(取自 #4997)钉住 #4986 的 folded-corpus 面。 - 出站不变量判官新增 I3 配对不变量(#4862 出口校验缺口):此前判官只对账入站↔出站连续性(I1/I2),折叠区内完全免检,于是「折叠拆断一对 tool_use/tool_result」的出站体拿到 clean fold verdict 直接放行,直到上游用 400 `tool_use ids were found without tool_result blocks` 说出真相。现在每个 tool-call 必须有同 id 的 tool-result 在其后、每个 tool-result 必须有同 id 的 tool-call 在其前,折叠区不再豁免;归因保持“谁的错”职责(宿主自己发来的断对不计 bili、compress 在途调用豁免、id 重铸不可洗白)。纯检测不拦截,受 diagnostics.invariantJudge 旋钮控制。 diff --git a/kernel/package-lock.json b/kernel/package-lock.json index 9a50787c32..c93403deb6 100644 --- a/kernel/package-lock.json +++ b/kernel/package-lock.json @@ -1,12 +1,12 @@ { "name": "acp-kernel", - "version": "0.0.171", + "version": "0.0.172", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "acp-kernel", - "version": "0.0.171", + "version": "0.0.172", "license": "MIT", "devDependencies": { "@types/node": "^22.0.0", diff --git a/kernel/package.json b/kernel/package.json index 37f51d6aef..4554262457 100644 --- a/kernel/package.json +++ b/kernel/package.json @@ -1,6 +1,6 @@ { "name": "acp-kernel", - "version": "0.0.171", + "version": "0.0.172", "description": "Framework-agnostic context-compression engine (model-driven, 3-tier LSM). Pure core: no host dependency.", "license": "MIT", "author": "ranxianglei", diff --git a/kernel/src/boundaries.ts b/kernel/src/boundaries.ts index e1524f1395..97620ef207 100644 --- a/kernel/src/boundaries.ts +++ b/kernel/src/boundaries.ts @@ -216,6 +216,28 @@ function resolveAnchorIndex( // hole (pruned or skipped) and must not be read as cross-session. const highest = highestUsedIndex(state.messageRefs); if (boundary.numericId > highest) { + // #4521: an endId above the highest allocated ref is the model aiming + // at "the newest tail" without knowing where it ends — the single most + // common compress failure on long sessions (it cannot see refs minted + // after the last banner). START anchors keep the hard refusal (a + // cross-generation start ref is a real targeting error), but the END + // anchor clamps down to the highest visible message: the range still + // folds everything the model meant ("up to now"), and the snapped note + // teaches the correct ceiling for next time. + if (endpoint === "end") { + const neighbor = snapToNeighborVisible( + state, + indexByMessageId, + boundary.numericId, + endpoint, + ); + if (neighbor !== null) { + return { + index: neighbor, + snapped: `${label}="${boundary.raw}" is above this session's highest allocated ref (${highest > 0 ? formatPaddedRef(highest) : "none"}) — clamped to it (refs are minted in order and never pre-allocated, so higher numbers do not exist yet). The range folds up to the last visible message.`, + }; + } + } throw new BoundaryNotFoundError( "unknown", endpoint, diff --git a/kernel/src/compress.ts b/kernel/src/compress.ts index 5be1706e1d..1449938830 100644 --- a/kernel/src/compress.ts +++ b/kernel/src/compress.ts @@ -165,6 +165,10 @@ function numericBlockId(id: string): number { return parsed ? Number(parsed[1]) : 0; } +function formatK(n: number): string { + return n >= 1000 ? `${(n / 1000).toFixed(1)}K` : `${n}`; +} + function refGateDiagnostics( state: CompressionState, requestedRanges: number, @@ -593,6 +597,33 @@ export function createCore(ports: Ports = {}): CompressionCore { gateMessage += ` ${reversalNotes.join(" ")}`; } + // #4521: a PURE resubmit (every requested range already consumed, + // no dangling/dead refs, nothing countable left) is the idempotent + // retry of an already-folded bill of work — historically refused with + // "Requested range(s) already compressed", which read as a failure and + // dragged another retry. Return it as the no-op success class instead: + // same informative text, zero errors, host renders a "[Compression + // no-op:" receipt that folds away (#4289 class). Mixed batches (some + // consumed + some live-but-too-small) keep the refusal — there is real + // work left to re-plan. + const pureResubmit = + countedRanges === 0 && + consumedRanges.length > 0 && + danglingRefs.length === 0 && + unknownCount === 0; + if (pureResubmit) { + return { + state, + result: { + blocksCreated: 0, + tokensCompressed: 0, + errors: [], + warnings: [`${gateMessage} Nothing failed and nothing needed redoing — do not resend these ranges.`], + noop: true, + }, + }; + } + // Rejected batches still carry their side effect: the dead-ref // tombstones marked above (#2362). The clone differs from input.state // ONLY in that field before the gate, so returning it is safe. @@ -731,7 +762,11 @@ export function createCore(ports: Ports = {}): CompressionCore { countTokens, preExistingCoverage, }); - blocksCreated += outcome.refolded ? outcome.refolded.length : 1; + blocksCreated += outcome.noop + ? 0 + : outcome.refolded + ? outcome.refolded.length + : 1; tokensCompressed += outcome.tokens; warnings.push(...outcome.warnings); } catch (error) { @@ -829,6 +864,11 @@ export function createCore(ports: Ports = {}): CompressionCore { errors, warnings, ...(notes.length > 0 ? { notes } : {}), + // #4521: a batch where every range was a no-op/skip (covered, + // protected, refold-blocked skips) committed nothing and failed + // nothing — surface the no-op class so the host renders the + // success-shaped receipt instead of "FAILED: no blocks created". + ...(blocksCreated === 0 && errors.length === 0 ? { noop: true } : {}), }, }; } @@ -875,10 +915,38 @@ export function createCore(ports: Ports = {}): CompressionCore { } const state = { ...result.state, lastPassIds: inboundIds, deadRefs }; const ccrEffect = result.effects.ccr as CcrEffect | undefined; + // #4521: between banner deliveries a compression round can stay armed for + // many requests (backoff, tier cadence, model-decided timing). During that + // window the model sees NO current refs and starts guessing beyond the + // highest allocated ref — every guess a failed round-trip. Emit a compact + // boundary line whenever a nudge plan carries offered ranges but its + // banner is not being delivered this turn; hosts append it as an ephemeral + // trailing user message (same carrier as the nudge, same cache safety). + let statusLine: string | undefined; + const nudge = result.effects.nudge; + // Two shapes authorize the refresh line: a real tiered arm whose banner + // is deferred this turn (tier !== null: pressure crossed a threshold but + // cadence/backoff holds it), or any plan after at least one banner was + // already shown (lastNudgeShownTokens > 0: the model HAS seen refs and + // this line only refreshes them). A fresh session's below-threshold plan + // (tier === null, nothing shown yet — e.g. an optimistic local estimate) + // must stay fully silent: same regime promise as the nudge itself + // (#196 regime scoping, #3910 lane baseline). + const bannerShown = (result.state.nudge?.lastNudgeShownTokens ?? 0) > 0 || Object.keys(result.state.nudge?.lastShownByTier ?? {}).length > 0; + if (nudge && !nudge.shouldInject && (nudge.tier !== null || bannerShown) && (nudge.compressibleRanges?.length ?? 0) > 0) { + const highest = highestUsedIndex(state.messageRefs); + const offered = nudge.compressibleRanges! + .slice(0, 3) + .map((r) => `${r.startRef}–${r.endRef} (${formatK(r.tokens)})`) + .join(", "); + const extra = nudge.compressibleRanges!.length > 3 ? `, +${nudge.compressibleRanges!.length - 3} more` : ""; + statusLine = `[ACP status: highest allocated ref ${highest > 0 ? indexToRef(highest) : "none"} — refs above it are not minted yet, never guess them; compressible now: ${offered}${extra}; the newest messages stay protected. Use these refs directly; acp_status only if you need the full list.]`; + } return { messages: result.messages, state, nudge: result.effects.nudge, + statusLine, terminalEscape: result.effects.terminalEscape, truncationSkipped: result.effects.truncationSkipped, contentStore: ccrEffect?.store ?? contentStore, @@ -1492,6 +1560,11 @@ interface SingleRangeOutcome { /** #400: set when the range ended as an in-place refold instead of creating * a fresh block — the ids of the blocks updated (caller counts these, not 1). */ refolded?: string[]; + /** #4521: set when the range resolved legally but folded nothing (all + * content already covered, or entirely inside the protected zone). The + * caller counts zero blocks and records the warning instead of rejecting + * the batch — an idempotent resubmit is success-shaped, not a failure. */ + noop?: boolean; } function applySingleRange(input: SingleRangeInput): SingleRangeOutcome { @@ -1509,7 +1582,26 @@ function applySingleRange(input: SingleRangeInput): SingleRangeOutcome { protectedMessageIds: input.protectedMessageIds, warnings, }); - if (projection.fatal) throw projection.fatal.error; + if (projection.fatal) { + // #4521: a range that lies ENTIRELY inside the protected zone is a legal + // no-op, not a failure — the model aimed at the newest tail (exactly the + // messages it just produced) and there is nothing it may fold there. + // Rejecting it sent retry loops back into the same zone (live incident: + // four consecutive zone refusals in session 01a18a4d, 20:09). The + // recommendation side still demotes zone fatals to gaps, so advertised + // ranges never point here (#3236 unchanged). + if (projection.fatal.kind === "zone") { + const highest = highestUsedIndex(input.state.messageRefs); + return { + tokens: 0, + warnings: [ + `Range ${input.spec.startRef}..${input.spec.endRef} folded nothing — it lies entirely inside the protected zone (the last ${input.config.preserveRecentMessages} messages and/or the most recent user message), which always stays visible. Nothing failed; do not resend this range. Session highest allocated ref is ${highest > 0 ? indexToRef(highest) : "none"} — target older ranges only.`, + ], + noop: true, + }; + } + throw projection.fatal.error; + } const { filteredIds, consumedBlockIds, @@ -1554,14 +1646,40 @@ function applySingleRange(input: SingleRangeInput): SingleRangeOutcome { } const first = consumedBlockIds[0]!; const last = consumedBlockIds[consumedBlockIds.length - 1]!; - throw new Error( - `Range ${input.spec.startRef}..${input.spec.endRef} contains no new compressible messages — every message in it is already covered by active block(s) ${consumedBlockIds.join( - ", ", - )}. Nothing was compressed. To rewrite or merge those blocks, reference them by block ID (${first}..${last}); otherwise run acp_status and compress a range it reports as compressible.`, - ); + // #4521: full-log hosts resolve covered spans as "ok" (never classified + // consumed), so an idempotent resubmit of an already-folded range used to + // land HERE and reject the whole batch ("Batch rejected before folding any + // range") — the dominant retry-loop driver on pi/plugin sessions. A + // covered span with nothing new is a no-op success: zero blocks, zero + // errors, one warning that names the covering blocks and how to rewrite + // them if that was the intent. + return { + tokens: 0, + warnings: [ + `Range ${input.spec.startRef}..${input.spec.endRef} folded nothing — every message in it is already covered by active block(s) ${consumedBlockIds.join( + ", ", + )}. Nothing failed and nothing needed redoing; do not resend this range. To rewrite or merge those blocks, reference them by block ID (${first}..${last}) instead.`, + ], + noop: true, + }; } - validateCompressionRange(input, filteredIds, consumedBlockIds.length); + validateCompressionRange(input); + + if (filteredIds.length === 0 && consumedBlockIds.length === 0) { + // #4521: everything in the range was dropped by content filters + // (protected tool pairs, media, …) with no zone hit and no consumed + // blocks — same idempotent no-op class as the two above, so the batch is + // not poisoned by it. + const highest = highestUsedIndex(input.state.messageRefs); + return { + tokens: 0, + warnings: [ + `Range ${input.spec.startRef}..${input.spec.endRef} folded nothing — all of its content is protected or filtered (protected tool calls/results, media, or the recent-message zone), which always stays visible. Nothing failed; do not resend this range. Session highest allocated ref is ${highest > 0 ? indexToRef(highest) : "none"}.`, + ], + noop: true, + }; + } let compressedTokens = 0; for (const id of filteredIds) { @@ -1671,19 +1789,14 @@ function resolveCoverageInterval( return merged; } -function validateCompressionRange( - input: SingleRangeInput, - directMessageIds: string[], - consumedBlockCount: number, -): void { +function validateCompressionRange(input: SingleRangeInput): void { const cfg = input.config.compress; validateSummaryLength(input.spec, cfg); - if (directMessageIds.length === 0 && consumedBlockCount === 0) { - throw new Error( - "Range contains no compressible messages — all are already covered by active blocks or protected.", - ); - } + // #4521: the empty-range case (no direct messages, no consumed blocks) + // moved to the caller as an idempotent NO-OP outcome — see applySingleRange. + // Throwing here rejected whole batches for ranges that were legal but had + // nothing left to fold. } function validateSummaryLength( diff --git a/kernel/src/parse-compress-input.ts b/kernel/src/parse-compress-input.ts index 1fbdfa1941..b5e98c2b28 100644 --- a/kernel/src/parse-compress-input.ts +++ b/kernel/src/parse-compress-input.ts @@ -503,7 +503,11 @@ function parseLineEntry( afterRefs = single.index + single[0].length; } if (summary.length === 0) - return { reason: "line entry: missing summary after the refs header line" }; + // #4521: name the two most common shapes behind this parse error — a + // stray note line about an already-folded range, and a block rewrite + // written as refs — so the model fixes the entry instead of resending it + // verbatim (each resend was a full failed round-trip). + return { reason: "line entry: missing summary after the refs header line (every entry needs refs THEN its summary text on the same or the next line; a note about an already-folded range does not belong in the batch — drop it; to rewrite an existing block use a bN..bM block-id header entry)" }; const explicitTopic = head.slice(afterRefs).trim(); const range: CompressRangeSpec = { startRef, diff --git a/kernel/src/protected.ts b/kernel/src/protected.ts index 7432225cb7..2a1bbbce73 100644 --- a/kernel/src/protected.ts +++ b/kernel/src/protected.ts @@ -25,10 +25,14 @@ export const ALWAYS_PROTECTED_TOOLS = ["compress", "acp_rule"] as const; * #4307: both classes leave zero blocks committed, so both are dead weight, * not metadata. Successful and partial receipts start with "[Compressed" and * stay protected — a partial fold's arguments still cover the ranges that - * DID apply. */ + * DID apply. #4521: "[Compression no-op: …]" is the idempotent-resubmit + * class — parse ok, ranges legal, nothing left to fold (already covered / + * protected zone). Zero blocks committed, so it folds away like the other + * zero-applied results instead of lingering as protected residue. */ export const FAILED_COMPRESS_RESULT_PREFIXES = [ "[Compression FAILED", "[Compression refused", + "[Compression no-op", ] as const; /** True when toolName denotes a compress call in ANY hosted dialect (#4289): diff --git a/kernel/src/types.ts b/kernel/src/types.ts index 3ac5c6a348..f9fb045f32 100644 --- a/kernel/src/types.ts +++ b/kernel/src/types.ts @@ -681,6 +681,15 @@ export interface ApplyCompressionResult { * result so the model knows its input was changed (#310). Omitted when * empty. */ notes?: string[]; + /** True when every requested range resolved legally but NOTHING needed + * folding — all content already covered by active blocks or inside the + * protected zone (#4521 no-op class). Not an error: the model + * re-submitting an already-folded bill of work gets an idempotent + * success-shaped receipt instead of a refusal, so the retry ladder that + * used to spin on "Batch rejected before folding any range" dies at the + * source. Hosts render it with the "[Compression no-op:" prefix and fold + * the exchange away like other zero-applied compress results (#4289). */ + noop?: true; }; } @@ -688,6 +697,14 @@ export interface ProcessTurnResult { messages: CoreMessage[]; state: CompressionState; nudge?: NudgeDecision; + /** Compact always-current boundary line, present only on turns where a + * compression round is live (nudge plan with offered ranges) but the + * advisory banner itself is NOT being delivered this request. Carries the + * session's highest allocated ref and the currently offered ranges so the + * model never has to guess refs between banner deliveries (#4521). Hosts + * append it as an ephemeral trailing user message, same carrier as a + * nudge. */ + statusLine?: string; /** Terminal floor detected (#300): compression and truncation are both * exhausted while usage stays at/above truncate.threshold. Emitted every * event while the condition holds; hosts surface it once. */ diff --git a/kernel/tests/carrier-advertise-validate.test.ts b/kernel/tests/carrier-advertise-validate.test.ts index 09a7276c40..db393f40fc 100644 --- a/kernel/tests/carrier-advertise-validate.test.ts +++ b/kernel/tests/carrier-advertise-validate.test.ts @@ -169,8 +169,9 @@ test("selector never advertises spans the apply side structurally rejects (#2638 } } - // Negative control: the OLD advertised dead spans still fail on apply — - // proving they were dead, so screening them out changes behavior. + // Negative control: the OLD advertised dead spans still fold nothing on + // apply — #4521 renders them as the covered no-op (0 blocks, 0 errors, + // warning naming the covering block), proving they were dead just the same. const deadSpan = core.applyCompression({ ranges: [{ startRef: "m00017", endRef: "m00022", summary: "dead span" }], messages, @@ -178,9 +179,12 @@ test("selector never advertises spans the apply side structurally rejects (#2638 config: cfg, }); assert.equal(deadSpan.result.blocksCreated, 0); - assert.ok( - deadSpan.result.errors.some((e) => /contains no (new )?compressible messages/.test(e)), - deadSpan.result.errors.join("; "), + assert.equal(deadSpan.result.errors.length, 0); + assert.equal(deadSpan.result.noop, true); + assert.match( + deadSpan.result.warnings.join(" "), + /already covered by active block\(s\)|protected or filtered/, + deadSpan.result.warnings.join("; "), ); const loneCarrier = core.applyCompression({ ranges: [{ startRef: "m00007", endRef: "m00007", summary: "lone carrier" }], @@ -189,10 +193,8 @@ test("selector never advertises spans the apply side structurally rejects (#2638 config: cfg, }); assert.equal(loneCarrier.result.blocksCreated, 0); - assert.ok( - loneCarrier.result.errors.some((e) => /contains no compressible messages/.test(e)), - loneCarrier.result.errors.join("; "), - ); + assert.equal(loneCarrier.result.errors.length, 0); + assert.equal(loneCarrier.result.noop, true, "lone carrier folds nothing, as the no-op class"); }); test("buildCompressibleRanges treats a live carrier as a boundary; stale carriers fold", () => { diff --git a/kernel/tests/compress.test.ts b/kernel/tests/compress.test.ts index 0d8383d033..a7b9a08c9a 100644 --- a/kernel/tests/compress.test.ts +++ b/kernel/tests/compress.test.ts @@ -576,18 +576,24 @@ test("retrying a consumed range reports already-compressed guidance, not too-sma }); assert.equal(retry.result.blocksCreated, 0); - assert.equal(retry.result.errors.length, 1); - assert.match(retry.result.errors[0]!, /already compressed/); + // #4521: a pure resubmit of an already-consumed range is an idempotent + // no-op success — guidance moved from errors[] to warnings[] and the + // result is flagged noop so hosts render a success-shaped receipt. + assert.equal(retry.result.errors.length, 0); + assert.equal(retry.result.noop, true); + assert.ok(retry.result.warnings.length > 0); + assert.match(retry.result.warnings[0]!, /already compressed/); assert.match( - retry.result.errors[0]!, + retry.result.warnings[0]!, /already summarized in active block\(s\) b1/, ); + assert.match(retry.result.warnings[0]!, /do not resend these ranges/); assert.doesNotMatch( - retry.result.errors[0]!, + retry.result.warnings[0]!, /retry with startId\/endId set to active block IDs/, ); assert.doesNotMatch( - retry.result.errors[0]!, + retry.result.warnings[0]!, /Total compressible content too small/, ); }); @@ -1426,17 +1432,22 @@ test("gate error names the covering block when anchors stay consumed", () => { }); assert.equal(result.result.blocksCreated, 0); - assert.equal(result.result.errors.length, 1); + // #4521: consumed-anchors-only resubmit is the idempotent no-op class — + // guidance lives in warnings[], errors[] stays empty, noop is flagged. + assert.equal(result.result.errors.length, 0); + assert.equal(result.result.noop, true); + assert.ok(result.result.warnings.length > 0); assert.match( - result.result.errors[0]!, + result.result.warnings[0]!, /Requested range\(s\) already compressed \(e\.g\. b2\.\.b110\)/, ); assert.match( - result.result.errors[0]!, + result.result.warnings[0]!, /its content is already summarized in active block\(s\) b110 — use search_context or decompress b110 if you need details from it/, ); + assert.match(result.result.warnings[0]!, /do not resend these ranges/); assert.doesNotMatch( - result.result.errors[0]!, + result.result.warnings[0]!, /retry with startId\/endId set to active block IDs/, ); }); diff --git a/kernel/tests/dead-refs.test.ts b/kernel/tests/dead-refs.test.ts index 30ba994231..3aba586157 100644 --- a/kernel/tests/dead-refs.test.ts +++ b/kernel/tests/dead-refs.test.ts @@ -209,8 +209,12 @@ test("covered endpoints keep the 'already compressed' verdict; nothing is marked }); assert.equal(result.result.blocksCreated, 0); - assert.match(result.result.errors[0]!, /already compressed/); - assert.match(result.result.errors[0]!, /b1/); - assert.doesNotMatch(result.result.errors[0]!, /DEAD/); + // #4521: covered resubmit is the idempotent no-op — verdict moved from + // errors[] to warnings[] (still names b1, still marks nothing dead). + assert.equal(result.result.errors.length, 0); + assert.equal(result.result.noop, true); + assert.match(result.result.warnings.join(" "), /already compressed/); + assert.match(result.result.warnings.join(" "), /b1/); + assert.match(result.result.warnings.join(" "), /do not resend/); assert.equal(result.state.deadRefs, undefined); }); diff --git a/kernel/tests/issue3371-consumed-unit-screen.test.ts b/kernel/tests/issue3371-consumed-unit-screen.test.ts index c9791db5c2..82ca883e7e 100644 --- a/kernel/tests/issue3371-consumed-unit-screen.test.ts +++ b/kernel/tests/issue3371-consumed-unit-screen.test.ts @@ -1,13 +1,11 @@ // #3371: screenRecommendedUnits must not advertise a message-ref unit whose -// entire span is already owned by an active, NOT restored-inline block — the -// apply lane's livelock guard (compress.ts, pi#199) is GUARANTEED to reject -// exactly that shape ("Range ... contains no new compressible messages") -// unless the shared refold verdict classifies it a genuine in-place refold -// (#398). Before the fix the two lanes disagreed on the shape: recommend -// advertised it, apply threw, forever — the #3236 advertise→reject loop -// family reached through the consumed-unit shape. The verdict lives in -// refold.ts, extracted from compress.ts so BOTH lanes consult one -// definition (projectUnit discipline). +// entire span is already owned by an active, NOT restored-inline block. +// #4521 note: the apply lane (compress.ts) no longer THROWS on that shape — it +// returns the idempotent no-op outcome (covered-range warning, zero blocks) — +// so the two lanes now agree by both declining to fold it without failing +// the request. Before #3371 the two lanes disagreed: recommend advertised it, +// apply threw, forever. The verdict lives in refold.ts, extracted from +// compress.ts so BOTH lanes consult one definition (projectUnit discipline). import { test } from "node:test"; import assert from "node:assert/strict"; @@ -70,19 +68,26 @@ test("#3371: consumed, not-restored span is screened OUT and apply agrees (no ad const screened = screenRecommendedUnits([consumedUnit], all, state, cfg, new Set()); assert.equal(screened.length, 0, "consumed & not restored-inline must not be advertised"); - // apply-side agreement: the same range is a livelock-guard rejection + // apply-side agreement (#4521): the same range is now the idempotent + // no-op class — zero errors, zero blocks, guidance in warnings[] (it used + // to be the livelock-guard throw; refusing it whole-batch is what fed the + // retry loops). Recommend still never advertises it, so the lanes agree: + // nothing advertised, nothing folded, nothing failed. const applied = core.applyCompression({ ranges: [{ startRef: consumedUnit.startRef, endRef: consumedUnit.endRef, summary: "nope" }], messages: all, state: structuredClone(state), config: cfg, }); - assert.ok(applied.result.errors.length > 0, "apply must reject the same shape"); + assert.equal(applied.result.errors.length, 0, applied.result.errors.join("; ")); + assert.equal(applied.result.blocksCreated, 0); + assert.equal(applied.result.noop, true); assert.match( - applied.result.errors.join("; "), - /contains no new compressible messages/, - "and it must be the livelock rejection, not some other error", + applied.result.warnings.join("; "), + /already covered by active block\(s\) b1/, + "and it must be the covered no-op warning, not some other error", ); + assert.match(applied.result.warnings.join("; "), /do not resend this range/); }); test("#3371: a genuine refold (restored-inline) stays advertised and applies cleanly", () => { diff --git a/kernel/tests/issue3939-fold-boundary-pins.test.ts b/kernel/tests/issue3939-fold-boundary-pins.test.ts index 2b0dda2aa1..c5c16fbcb1 100644 --- a/kernel/tests/issue3939-fold-boundary-pins.test.ts +++ b/kernel/tests/issue3939-fold-boundary-pins.test.ts @@ -333,7 +333,7 @@ test("pin: unknown-ref sibling errors do not poison valid ranges (classification ); }); -test("pin: a range that dies DURING application rejects the whole batch all-or-nothing (#3939 ④, closes the #3003 double-seam)", () => { +test("pin: a range that dies DURING application folds its siblings and no-ops the dead range (#4521 supersedes #3939 ④ for the zone class)", () => { const core = createCore(); const state = createInitialState(); const messages: CoreMessage[] = [ @@ -357,11 +357,19 @@ test("pin: a range that dies DURING application rejects the whole batch all-or-n protectedMessageIds: new Set(["m00003"]), }); - assert.equal(result.result.blocksCreated, 0); - assert.equal(result.state.blocks.length, 0); - assert.equal(result.state.stats.compressionCount, 0); + // #3939 ④ pinned all-or-nothing here because a mid-batch death used to + // leave a seam the model would re-enter (retry the dead range → covered / + // protected rejection → livelock). #4521 removes the seam's teeth: the + // dead range is an idempotent no-op (resubmitting it later returns the + // same no-op receipt, never a failure), so the healthy sibling folds + // instead of being held hostage. + assert.equal(result.result.blocksCreated, 1, "the healthy sibling folds"); + assert.equal(result.state.blocks.length, 1); + assert.equal(result.state.stats.compressionCount, 1); + assert.equal(result.result.errors.length, 0, "the zone death is not an error"); assert.match( - result.result.errors[0]!, - /Batch rejected before folding any range.*entirely within the protected zone/s, + result.result.warnings.join(" "), + /m00003\.\.m00003 folded nothing.*protected zone/s, + "the dead range is reported as the zone no-op", ); }); diff --git a/kernel/tests/issue4521-retry-livelock.test.ts b/kernel/tests/issue4521-retry-livelock.test.ts new file mode 100644 index 0000000000..2dce3f1311 --- /dev/null +++ b/kernel/tests/issue4521-retry-livelock.test.ts @@ -0,0 +1,195 @@ +// #4521: long sessions died in compress retry livelocks — the model kept +// aiming at the newest tail (refs minted after the last banner delivery, or +// the protected zone itself) and every legal-looking call came back FAILED: +// endId above the highest allocated ref (whole batch refused), ranges already +// covered by a fold (livelock throw), ranges inside the protected zone +// (zone fatal). Live incident: session 01a18a4d, four consecutive refusals +// between 20:09:23 and 20:09:46 over ranges that were 100% legal targets for +// "compress what just happened". These tests pin the retry-livelock kills: +// (a) endId above highest CLAMPS to the highest visible message instead of +// refusing (start anchors keep the hard refusal — that is a real +// targeting error), +// (b) a range entirely inside the protected zone is an idempotent no-op +// (errors empty, noop flagged, guidance in warnings), +// (c) a range whose every message is content-filtered (protected tool pairs) +// is the same no-op class, +// (d) the armed-but-silent nudge window emits a status line naming the +// highest allocated ref and the offered ranges, so the model stops +// guessing refs it cannot see. + +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 type { Config, CoreMessage } from "../src/types.js"; + +const LIMIT = 872_000; + +function config(overrides: Partial = {}): Config { + return { + tiers: { enabled: true, tier2Trigger: 5, tier3Trigger: 10 }, + nudge: { + maxContextLimitPct: 0.75, + minContextLimitPct: 0.05, + frequency: 1, + iterationThreshold: 15, + force: "soft", + growthRatio: 0.06, + growthFloor: 50_000, + growthCap: 50_000, + minGrowthFloor: 22_500, + minGrowthRatio: 0.45, + emergencyThresholdPct: 0.98, + }, + promotionThreshold: 5, + truncate: { threshold: 1 }, + merge: { maxSummaryLength: 3000, minOldGenBlocks: 3 }, + compress: { minCompressRange: 100, maxSummaryLength: 20000, minSummaryLength: 1 }, + protectedTools: [], + preserveRecentMessages: 0, + preserveRecentTokens: 0, + modelContextLimit: LIMIT, + ...overrides, + }; +} + +function text(id: string, role: CoreMessage["role"], len = 3000): CoreMessage { + return { id, role, contentType: "text", text: "x".repeat(len) }; +} + +function toolCall(id: string, callId: string, toolName = "bash"): CoreMessage { + return { id, role: "assistant", contentType: "tool-call", toolName, toolCallId: callId, text: `args-${callId}` }; +} + +function toolResult(id: string, callId: string, toolName = "bash", len = 3000): CoreMessage { + return { id, role: "tool", contentType: "tool-result", toolName, toolCallId: callId, text: "x".repeat(len) }; +} + +function seed(messages: CoreMessage[]) { + const state = createInitialState(); + state.messageRefs = assignRefs(messages, { existing: state.messageRefs, nextIndex: 1 }).map; + return state; +} + +test("#4521: endId above highest allocated ref clamps down instead of refusing the batch", () => { + const core = createCore(); + const messages = [text("u0", "user"), text("a1", "assistant"), text("a2", "assistant"), text("a3", "assistant")]; + const state = seed(messages); + + const r = core.applyCompression({ + ranges: [{ startRef: "m00002", endRef: "m00099", summary: "work recap" }], + messages, + state, + config: config(), + }); + + assert.equal(r.result.errors.length, 0, r.result.errors.join("; ")); + assert.equal(r.result.blocksCreated, 1); + assert.ok( + r.result.warnings.some((w) => /clamped to it/.test(w) && /highest allocated ref \(m00004\)/.test(w)), + `expected clamp warning, got: ${JSON.stringify(r.result.warnings)}`, + ); + const block = r.state.blocks.find((b) => b.active); + assert.ok(block, "a block was created"); + assert.deepEqual( + [...block.directMessageIds].sort(), + ["a1", "a2", "a3"], + "the range folded up to the last visible message", + ); +}); + +test("#4521: startId above highest allocated ref keeps the hard refusal (real targeting error)", () => { + const core = createCore(); + const messages = [text("u0", "user"), text("a1", "assistant"), text("a2", "assistant")]; + const state = seed(messages); + + const r = core.applyCompression({ + ranges: [{ startRef: "m00099", endRef: "m00104", summary: "phantom work" }], + messages, + state, + config: config(), + }); + + assert.equal(r.result.blocksCreated, 0); + assert.ok(r.result.errors.length > 0, "a start anchor beyond the frontier is a real error"); + assert.match(r.result.errors.join("; "), /above this session's highest allocated ref/); + assert.doesNotMatch(r.result.errors.join("; "), /clamped/); + assert.notEqual(r.result.noop, true, "not the no-op class — the guidance matters here"); +}); + +test("#4521: a range entirely inside the protected zone is a no-op, not a batch failure", () => { + const core = createCore(); + const messages = [text("u0", "user"), text("a1", "assistant"), text("a2", "assistant"), text("a3", "assistant")]; + const state = seed(messages); + + const r = core.applyCompression({ + ranges: [{ startRef: "m00001", endRef: "m00004", summary: "newest work" }], + messages, + state, + config: config({ preserveRecentMessages: 4 }), + }); + + assert.equal(r.result.errors.length, 0, r.result.errors.join("; ")); + assert.equal(r.result.blocksCreated, 0); + assert.equal(r.result.noop, true); + assert.ok( + r.result.warnings.some((w) => /protected zone/.test(w) && /do not resend this range/.test(w)), + `expected zone no-op warning, got: ${JSON.stringify(r.result.warnings)}`, + ); + assert.equal(r.state.blocks.length, 0, "nothing was folded"); +}); + +test("#4521: a range of pure protected tool pairs is the same no-op class", () => { + const core = createCore(); + const messages = [ + toolCall("c1", "call1"), + toolResult("r1", "call1"), + toolCall("c2", "call2"), + toolResult("r2", "call2"), + ]; + const state = seed(messages); + + const r = core.applyCompression({ + ranges: [{ startRef: "m00001", endRef: "m00004", summary: "tool work" }], + messages, + state, + config: config({ protectedTools: ["bash"] }), + }); + + assert.equal(r.result.errors.length, 0, r.result.errors.join("; ")); + assert.equal(r.result.blocksCreated, 0); + assert.equal(r.result.noop, true); + assert.ok( + r.result.warnings.some((w) => /protected or filtered/.test(w) && /do not resend this range/.test(w)), + `expected filter no-op warning, got: ${JSON.stringify(r.result.warnings)}`, + ); +}); + +test("#4521: armed-but-silent nudge window emits a status line naming the highest ref and offered ranges", () => { + const core = createCore(); + const cfg = config(); + const messages = Array.from({ length: 40 }, (_, i) => + text(`m${i}`, i % 2 === 0 ? "user" : "assistant", 24_000), + ); + let state = createInitialState(); + + // First turn injects via the first-sight mass bypass (240K pending ≥ + // threshold, no baseline yet) — the banner carries the refs. + const warm = core.processTurn({ messages, state, config: cfg, tokenCount: 600_000 }); + assert.equal(warm.nudge?.shouldInject, true, "first-sight mass injects"); + assert.equal(warm.statusLine, undefined, "no status line beside a delivered banner"); + state = warm.state; + + // Next turn: growth 10K < 22.5K floor, usage 70% < 75% band → the round + // stays armed (offered ranges exist) but the banner is withheld — the + // window where refs used to go stale in the model's view and compress + // calls guessed beyond the highest allocated ref. + const idle = core.processTurn({ messages, state, config: cfg, tokenCount: 610_000 }); + assert.equal(idle.nudge?.shouldInject, false, `armed-but-silent window, got reason: ${idle.nudge?.reason}`); + assert.ok((idle.nudge?.compressibleRanges?.length ?? 0) > 0, "plan carries offered ranges"); + assert.ok(idle.statusLine, "status line emitted in the armed-but-silent window"); + assert.match(idle.statusLine!, /highest allocated ref m00040/); + assert.match(idle.statusLine!, /compressible now: m\d+–m\d+/); + assert.match(idle.statusLine!, /never guess them/); +}); diff --git a/kernel/tests/message-protection.test.ts b/kernel/tests/message-protection.test.ts index ad039b2f66..c36e1d7d63 100644 --- a/kernel/tests/message-protection.test.ts +++ b/kernel/tests/message-protection.test.ts @@ -201,7 +201,7 @@ test("isMessageProtected: legacy refs assigned before protection still cannot be assert.ok(block.effectiveMessageIds.includes("d"), "msg 'd' folded"); }); -test("isMessageProtected: range consisting solely of protected messages fails with a clear error", () => { +test("isMessageProtected: range consisting solely of protected messages is a clean no-op", () => { const core = createCore(); const messages = [msg("catalog", catalogText)]; const state = setupRefs(messages); @@ -215,10 +215,14 @@ test("isMessageProtected: range consisting solely of protected messages fails wi }); assert.equal(result.result.blocksCreated, 0, "nothing may be created"); - assert.equal(result.result.errors.length, 1); + // #4521: entirely-protected range is the idempotent no-op class — loud + // enough (warning names the cause) without poisoning a mixed batch or + // feeding the retry loops a FAILED receipt for a legal request. + assert.equal(result.result.errors.length, 0); + assert.equal(result.result.noop, true); assert.match( - result.result.errors[0]!, - /no compressible messages/, - "an entirely-protected range must fail loudly, not fake-compress", + result.result.warnings.join(" "), + /protected or filtered/, + "the warning must say everything in the range is protected", ); }); diff --git a/kernel/tests/protect-recent.test.ts b/kernel/tests/protect-recent.test.ts index 3e0ebd8d4a..91ee13e42e 100644 --- a/kernel/tests/protect-recent.test.ts +++ b/kernel/tests/protect-recent.test.ts @@ -64,14 +64,17 @@ const EIGHT = [ msg("h", "eighth detailed message body content theta"), ]; -test("applyCompression fails when the range is ENTIRELY within the recent zone", () => { +test("applyCompression no-ops when the range is ENTIRELY within the recent zone", () => { const core = createCore(); const messages = [...EIGHT]; const state = seededState(messages); const cfg = config({ preserveRecentMessages: 3 }); // m00006..m00008 are the last 3 → entirely protected. After filtering there - // is nothing left to compress, so the range must still fail. + // is nothing left to compress. #4521: that is the idempotent no-op class — + // the range was legal to request (it aimed at the newest tail, exactly the + // messages the model just produced), so refusing it whole-batch only fed + // retry livelocks. Zero blocks, zero errors, guidance in warnings[]. const result = core.applyCompression({ ranges: [ { @@ -87,12 +90,14 @@ test("applyCompression fails when the range is ENTIRELY within the recent zone", }); assert.equal(result.result.blocksCreated, 0, "no block created"); - assert.equal(result.result.errors.length, 1); + assert.equal(result.result.errors.length, 0, "no failure — nothing was left to fold"); + assert.equal(result.result.noop, true, "flagged as the no-op class"); assert.match( - result.result.errors[0]!, - /entirely.*protected.*last 3|last 3.*protected/i, - `error should mention the protected recent zone with nothing left, got: ${result.result.errors[0]}`, + result.result.warnings.join(" "), + /protected zone/i, + `warning should mention the protected recent zone, got: ${JSON.stringify(result.result.warnings)}`, ); + assert.match(result.result.warnings.join(" "), /do not resend this range/); }); test("applyCompression excludes protected tail and compresses the rest (partial overlap)", () => { @@ -219,10 +224,11 @@ test("applyCompression still allows compressing messages strictly before the rec ); }); -test("applyCompression fails by default when the whole range is protected (no explicit set)", () => { +test("applyCompression no-ops by default when the whole range is protected (no explicit set)", () => { // No explicit protectedMessageIds passed — applyCompression must compute the // soft-protected zone itself (recent-N + last user message). When the entire - // range falls in that zone, it still fails. + // range falls in that zone it folds nothing — #4521 no-op class, not a + // failure (the zone always stays visible; refusing it fed retry loops). const core = createCore(); const messages = [ msg("a", "old user msg alpha", "user"), @@ -252,7 +258,9 @@ test("applyCompression fails by default when the whole range is protected (no ex 0, "default protection applies without explicit set", ); - assert.match(result.result.errors[0]!, /protected/i); + assert.equal(result.result.errors.length, 0); + assert.equal(result.result.noop, true); + assert.match(result.result.warnings.join(" "), /protected zone/i); }); // --- decompress results are excluded from the recent-protected zone --- @@ -494,6 +502,8 @@ test("applyCompression A/B: empty list protects a fresh read result; default rec ]; // With preserveRecentMessages=3 and nothing excluded, visible tail = d,e,f // → m00004 is inside the protected zone under the empty list. + // #4521: zone-only range = idempotent no-op (0 blocks, 0 errors, warning) + // — still "protected with empty exclusion list", just not a failure. const refused = core.applyCompression({ ranges: [ { @@ -512,8 +522,9 @@ test("applyCompression A/B: empty list protects a fresh read result; default rec 0, "fresh read result protected with empty exclusion list", ); - assert.equal(refused.result.errors.length, 1); - assert.match(refused.result.errors[0]!, /protected/i); + assert.equal(refused.result.errors.length, 0); + assert.equal(refused.result.noop, true); + assert.match(refused.result.warnings.join(" "), /protected zone/i); // Same session shape under the default list: read is out of the zone → reclaimable now. const allowed = core.applyCompression({ @@ -754,7 +765,10 @@ test('applyCompression: preserveRecentTools ["read"] is the one-line #1198 remed 0, "fresh read result protected by the subtraction knob", ); - assert.match(refused.result.errors[0]!, /protected/i); + // #4521: protection verdict lives in warnings (no-op class), not errors. + assert.equal(refused.result.errors.length, 0); + assert.equal(refused.result.noop, true); + assert.match(refused.result.warnings.join(" "), /protected zone/i); }); test("validateConfig checks preserveRecentTools shape and accepts []", () => { diff --git a/kernel/tests/refold.test.ts b/kernel/tests/refold.test.ts index 8391a5c5f1..0267140ef7 100644 --- a/kernel/tests/refold.test.ts +++ b/kernel/tests/refold.test.ts @@ -207,7 +207,7 @@ test("refold updates the block in place: same id, replaced summary, flag cleared assert.equal(result.state.nextBlockId, 2, "no new block id allocated"); }); -test("partial coverage of a restored block is still rejected, naming the blocking block id", () => { +test("partial coverage of a restored block no-ops, naming the blocking block id", () => { const core = createCore(); const messages = makeMessages(10); const ids = messages.map((m) => m.id); @@ -225,11 +225,18 @@ test("partial coverage of a restored block is still rejected, naming the blockin config: config(), }); - assert.match(result.result.errors[0]!, /already compressed/); - assert.match(result.result.errors[0]!, /partially covered b1/); + // #4521: the partial-refold verdict is the covered no-op class — guidance + // (refold the WHOLE restored span, or anchor by block id) rides warnings, + // and nothing mutates. Previously this rejected the whole batch with the + // same wording; the model then re-sent the identical range (a retry loop). + assert.equal(result.result.errors.length, 0); + assert.equal(result.result.blocksCreated, 0); + assert.equal(result.result.noop, true); + const w = result.result.warnings.join(" "); + assert.match(w, /partially covered b1/); const block = result.state.blocks[0]!; - assert.equal(block.summary, OLD_SUMMARY, "no mutation on rejection"); - assert.equal(block.restoredInline, true, "marker survives rejection"); + assert.equal(block.summary, OLD_SUMMARY, "no mutation on the no-op"); + assert.equal(block.restoredInline, true, "marker survives the no-op"); assert.equal(result.state.nextBlockId, 2); }); @@ -257,10 +264,13 @@ test("an unrecovered higher-tier folded block above a restored block is rejected config: config(), }); - assert.match(result.result.errors[0]!, /already compressed/); - assert.match(result.result.errors[0]!, /blocked by b2 \(not restored\)/); + assert.equal(result.result.errors.length, 0); + assert.equal(result.result.blocksCreated, 0); + assert.equal(result.result.noop, true, "#4521: blocked refold is the covered no-op class"); + const w = result.result.warnings.join(" "); + assert.match(w, /blocked by b2 \(not restored\)/); assert.doesNotMatch( - result.result.errors[0]!, + w, /blocked by b2.*blocked by b2/, "blocker listed once", ); @@ -517,7 +527,7 @@ test("full anchor view: range mixing a restored block with live tail creates a f assert.equal(result.state.nextBlockId, 3, "exactly one new id allocated"); }); -test("full anchor view: unmarked block keeps the legacy livelock rejection verbatim", () => { +test("full anchor view: unmarked block gets the covered no-op with the block-id guidance", () => { const core = createCore(); const messages = longMessages(10); const state = makeState(messages, [ @@ -537,14 +547,16 @@ test("full anchor view: unmarked block keeps the legacy livelock rejection verba config: config(), }); - assert.match( - result.result.errors[0]!, - /Range m00001\.\.m00010 contains no new compressible messages — every message in it is already covered by active block\(s\) b1/, - ); - assert.match( - result.result.errors[0]!, - /reference them by block ID \(b1\.\.b1\)/, - ); + // #4521: the legacy livelock throw is now the covered no-op — same span, + // same guidance, delivered as a warning on a success-shaped result so the + // model stops re-sending the range after a FAILED receipt. + assert.equal(result.result.errors.length, 0); + assert.equal(result.result.blocksCreated, 0); + assert.equal(result.result.noop, true); + const w = result.result.warnings.join(" "); + assert.match(w, /already covered by active block\(s\) b1/); + assert.match(w, /reference them by block ID \(b1\.\.b1\)/); + assert.match(w, /do not resend this range/); assert.equal(result.state.blocks.length, 1, "nothing applied"); assert.equal(result.state.blocks[0]!.summary, OLD_SUMMARY); assert.equal(result.state.nextBlockId, 2); @@ -648,8 +660,11 @@ test("old persisted format without restoredInline reads as false: refold rejecte state: loaded, config: config(), }); - assert.match(rejected.result.errors[0]!, /already compressed/); - assert.match(rejected.result.errors[0]!, /blocked by b1 \(not restored\)/); + // #4521: unmarked → not refoldable → covered no-op (warning, not error). + assert.equal(rejected.result.errors.length, 0); + assert.equal(rejected.result.blocksCreated, 0); + assert.equal(rejected.result.noop, true); + assert.match(rejected.result.warnings.join(" "), /blocked by b1 \(not restored\)/); assert.equal(rejected.state.blocks[0]!.summary, OLD_SUMMARY); // Same load, then marked inline: refold now succeeds. diff --git a/kernel/tests/regression-bili-470.test.ts b/kernel/tests/regression-bili-470.test.ts index 989a7ca12a..c076fb7312 100644 --- a/kernel/tests/regression-bili-470.test.ts +++ b/kernel/tests/regression-bili-470.test.ts @@ -119,32 +119,38 @@ test("retrying a sub-range of a fresh T1 block names the covering block, not a s }), }); + // #4521: the consumed-slice retry is the idempotent no-op — same #470 + // guidance (names b1, no block-id retry advice), now in warnings[] on a + // success-shaped result instead of a FAILED receipt the model re-sent. assert.equal(retry.result.blocksCreated, 0); - assert.equal(retry.result.errors.length, 1); + assert.equal(retry.result.errors.length, 0); + assert.equal(retry.result.noop, true); + const w = retry.result.warnings.join(" "); assert.match( - retry.result.errors[0]!, + w, /Requested range\(s\) already compressed \(e\.g\. m00018\.\.m00020\)/, ); assert.match( - retry.result.errors[0]!, + w, /its content is already summarized in active block\(s\) b1/, ); assert.match( - retry.result.errors[0]!, + w, /use search_context or decompress b1 if you need details from it/, ); assert.match( - retry.result.errors[0]!, + w, /Nothing new to compress in that window/, ); - assert.match(retry.result.errors[0]!, /Continue the task/); + assert.match(w, /Continue the task/); + assert.match(w, /do not resend these ranges/); assert.doesNotMatch( - retry.result.errors[0]!, + w, /retry with startId\/endId set to active block IDs/, ); - assert.doesNotMatch(retry.result.errors[0]!, /Current active blocks span/); + assert.doesNotMatch(w, /Current active blocks span/); assert.doesNotMatch( - retry.result.errors[0]!, + w, /Tier distillation is actionable/, ); }); @@ -177,8 +183,12 @@ test("gate error offers tier distillation only once the trigger count is reached }); assert.equal(retry.result.blocksCreated, 0); + // #4521: consumed-slice retry = no-op; the tier-distillation offer rides + // warnings[] alongside the do-not-resend note. + assert.equal(retry.result.errors.length, 0); + assert.equal(retry.result.noop, true); assert.match( - retry.result.errors[0]!, + retry.result.warnings.join(" "), /Tier distillation is actionable now: compress\(\{ content: \[\{ startId: "b1", endId: "b5", summary: "\.\.\.", topic: "\.\.\." \}\] \}\) merges those tier-1 blocks into one tier-2 block/, ); }); @@ -269,17 +279,21 @@ test("gate error falls back to stale-ref wording when no active block covers the }); assert.equal(result.result.blocksCreated, 0); - assert.equal(result.result.errors.length, 1); + // #4521: stale consumed-block retry = the no-op class; the stale-ref + // wording survives in warnings[]. + assert.equal(result.result.errors.length, 0); + assert.equal(result.result.noop, true); + const w = result.result.warnings.join(" "); assert.match( - result.result.errors[0]!, + w, /Requested range\(s\) already compressed \(e\.g\. b2\.\.b110\)/, ); assert.match( - result.result.errors[0]!, + w, /its refs no longer point to directly compressible content \(stale block ref\(s\) distilled or consumed by higher-tier blocks\)/, ); assert.doesNotMatch( - result.result.errors[0]!, + w, /already summarized in active block/, ); }); diff --git a/kernel/tests/regression-empty-rewrap.test.ts b/kernel/tests/regression-empty-rewrap.test.ts index 25414f04e8..ff1a25dd82 100644 --- a/kernel/tests/regression-empty-rewrap.test.ts +++ b/kernel/tests/regression-empty-rewrap.test.ts @@ -149,11 +149,17 @@ test("#199: message-ref range that only re-wraps an active block is rejected, no config: cfg, }); + // #4521: the re-wrap attempt is now the covered no-op — still zero blocks, + // b1 untouched, but delivered as a success-shaped receipt with the + // block-id guidance in warnings[] (the old hard error is exactly what the + // looping session kept re-sending into 33 times). assert.equal(applied.result.blocksCreated, 0, "must not create a block"); - assert.equal(applied.result.errors.length, 1); - assert.match(applied.result.errors[0]!, /no new compressible/i); - assert.match(applied.result.errors[0]!, /b1/); - assert.match(applied.result.errors[0]!, /b1\.\.b1|block ID/i); + assert.equal(applied.result.errors.length, 0); + assert.equal(applied.result.noop, true); + const w = applied.result.warnings.join(" "); + assert.match(w, /already covered by active block\(s\) b1/); + assert.match(w, /b1\.\.b1|block ID/i); + assert.match(w, /do not resend this range/); assert.equal(applied.state.blocks.length, 1, "no wrapper block appended"); assert.equal(applied.state.blocks[0]!.active, true, "b1 must stay active"); }); diff --git a/kernel/tests/validation.test.ts b/kernel/tests/validation.test.ts index c32cb46c5a..afc24b01f3 100644 --- a/kernel/tests/validation.test.ts +++ b/kernel/tests/validation.test.ts @@ -176,7 +176,7 @@ test("validation: batch aggregate — many small ranges pass if total >= minComp assert.equal(result.result.errors.length, 0); }); -test("validation: range with zero compressible messages (covered by non-nested block) is rejected", () => { +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), @@ -200,7 +200,9 @@ test("validation: range with zero compressible messages (covered by non-nested b // 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=[]. Validation fires. + // 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, @@ -208,7 +210,15 @@ test("validation: range with zero compressible messages (covered by non-nested b config, }); assert.equal(second.result.blocksCreated, 0); - assert.match(second.result.errors[0]!, /no compressible/i); + 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", () => { diff --git a/package-lock.json b/package-lock.json index bfc642e9f9..33d91bf752 100644 --- a/package-lock.json +++ b/package-lock.json @@ -50,7 +50,7 @@ }, "kernel": { "name": "acp-kernel", - "version": "0.0.171", + "version": "0.0.172", "dev": true, "license": "MIT", "devDependencies": { diff --git a/src/dead-ranges.ts b/src/dead-ranges.ts index e41b791d4b..78325e5390 100644 --- a/src/dead-ranges.ts +++ b/src/dead-ranges.ts @@ -46,17 +46,28 @@ function activeBlockIds(session: Session): string[] { return session.state.blocks.filter((b) => b.active).map((b) => b.blockId).sort(); } -/** Both structural-empty wordings the kernel emits for a span with zero foldable content: +/** The structural-empty verdicts the kernel emits for a span with zero foldable content. + * Failure-era wordings (kept so downgraded kernels still classify): * - the #199 livelock guard: "...contains no new compressible messages — every message in it * is already covered by active block(s) b3..." * - validateCompressionRange: "Range contains no compressible messages — all are already * covered by active blocks or protected." - * Returns the named covering blocks ([] for the unnamed variant); null when the verdict is - * NOT structural-empty — transient, zone, min-gate and length rejections must stay retryable - * (they can clear mid-invocation or next turn). Feed the RAW kernel error strings, not a - * truncated/joined rendering: the named-block list must survive intact. */ + * #4521 no-op era: the same structural deaths now arrive as SUCCESS warnings + * (applySingleRange in kernel/src/compress.ts) + * - covered: "...folded nothing — every message in it is already covered by active block(s) b2..." + * - filter/protected: "...folded nothing — all of its content is protected or filtered (...)" + * (unnamed structural empty). + * The zone variant ("folded nothing — it lies entirely inside the protected zone") is NOT + * structural here: the recent zone shifts as newer messages arrive, so it can clear next + * turn and must stay retryable. Transient, min-gate and length rejections likewise. + * Returns the named covering blocks ([] for the unnamed variants); null when the verdict is + * NOT structural-empty. Feed the RAW kernel errors+warnings strings, not a truncated/joined + * rendering: the named-block list must survive intact. */ +const STRUCTURAL_EMPTY_VERDICTS = + /no (?:new )?compressible messages|folded nothing — every message in it is already covered by active block\(s\)|folded nothing — all of its content is protected or filtered/; + export function parseStructuralEmptyVerdict(verdict: string): string[] | null { - if (!/no (?:new )?compressible messages/.test(verdict)) return null; + if (!STRUCTURAL_EMPTY_VERDICTS.test(verdict)) return null; const named = /active block\(s\) ([A-Za-z0-9]+(?:,\s*[A-Za-z0-9]+)*)/.exec(verdict)?.[1] ?? ""; return named ? named.split(/,\s*/).filter(Boolean) : []; } diff --git a/src/preflight.ts b/src/preflight.ts index 0af1155dd3..88a0004ec1 100644 --- a/src/preflight.ts +++ b/src/preflight.ts @@ -2762,9 +2762,11 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa // regime-independent — coverage-based, they hold under the #330 relax flip // too, so the span is skipped for the rest of this invocation and // remembered across invocations. Zone/min-gate/length verdicts can clear - // mid-invocation (relax, newer messages) and stay retryable. Raw errors, - // not the truncated verdict: the named-block list must survive intact. - const named = parseStructuralEmptyVerdict(preview.result.errors.join("\n")); + // mid-invocation (relax, newer messages) and stay retryable. #4521 moved + // the structural-empty verdicts from FAILED errors to success warnings + // ("folded nothing — ..."); raw errors AND warnings, not a truncated + // rendering: the named-block list must survive intact. + const named = parseStructuralEmptyVerdict([...preview.result.errors, ...preview.result.warnings].join("\n")); if (named !== null) { skipSet.add(skipKey); recordDeadRange(deps.session, startRef, endRef, named); diff --git a/src/server/prepare-anthropic.ts b/src/server/prepare-anthropic.ts index 3e81710168..5e0e94977e 100644 --- a/src/server/prepare-anthropic.ts +++ b/src/server/prepare-anthropic.ts @@ -559,6 +559,16 @@ export async function prepareAnthropic( } else if (emergencyPressureHeld) { nudgeOutcome = "gated"; } + // #4521: armed-but-silent window — a nudge plan with offered ranges + // that is NOT being delivered this round (backoff/cadence/decide + // veto). Without current refs the model guesses beyond the highest + // allocated ref on its next compress attempt, and every guess is a + // failed round-trip. Ride the same ephemeral trailing-user-message + // carrier as the nudge (never persisted, never in the client's + // re-sent history, prefix-cache safe). + if (turn.statusLine) { + rebuiltMessages = [...rebuiltMessages, { role: "user", content: turn.statusLine }]; + } // [#1343/#1457] surface any earlier undelivered retrieve as an ephemeral // trailing user note (kept last so it never reorders cached messages). // Snapshot WITHOUT consuming: forward() commits the ids only once diff --git a/src/server/prepare-google.ts b/src/server/prepare-google.ts index 4748cd6e59..00eb236453 100644 --- a/src/server/prepare-google.ts +++ b/src/server/prepare-google.ts @@ -372,6 +372,12 @@ export async function prepareGoogle( } else if (emergencyPressureHeld) { nudgeOutcome = "gated"; } + // #4521: armed-but-silent window — same carrier + rationale as the + // anthropic lane (ephemeral trailing user message, current refs + // instead of model-guessed ones). + if (turn.statusLine) { + rebuiltContents = [...rebuiltContents, { role: "user", parts: [{ text: turn.statusLine }] }]; + } } catch (err) { log("warn", `[${sessionId}] kernel transform failed, forwarding unchanged: ${String(err)}`); processedMessages = []; diff --git a/src/server/prepare-openai.ts b/src/server/prepare-openai.ts index 97589eea07..51f6f94359 100644 --- a/src/server/prepare-openai.ts +++ b/src/server/prepare-openai.ts @@ -429,6 +429,12 @@ export async function prepareOpenai( } else if (emergencyPressureHeld) { nudgeOutcome = "gated"; } + // #4521: armed-but-silent window — same carrier + rationale as the + // anthropic lane (ephemeral trailing user message, current refs + // instead of model-guessed ones). + if (turn.statusLine) { + rebuiltMessages = [...rebuiltMessages, { role: "user", content: turn.statusLine }]; + } // [#1343/#1457] surface any earlier undelivered retrieve as an ephemeral // trailing user note (kept last so it never reorders cached messages). // Snapshot WITHOUT consuming: forward() commits the ids only once diff --git a/src/server/prepare-responses.ts b/src/server/prepare-responses.ts index c2025896d3..38d33032a7 100644 --- a/src/server/prepare-responses.ts +++ b/src/server/prepare-responses.ts @@ -550,6 +550,12 @@ reconcileFoldCoverage(session, msgs, { mode: foldMode, sessionId, log, markerStr } else if (emergencyPressureHeld) { nudgeOutcome = "gated"; } + // #4521: armed-but-silent window — same carrier + rationale as the + // anthropic lane (ephemeral trailing user item, current refs instead + // of model-guessed ones). + if (turn.statusLine && Array.isArray(rebuiltInput)) { + rebuiltInput = [...(rebuiltInput as ResponseInputItem[]), { type: "message", role: "user", content: turn.statusLine }]; + } transformOk = true; } catch (err) { log("warn", `[${sessionId}] kernel transform failed, forwarding unchanged: ${String(err)}`); diff --git a/src/stream.ts b/src/stream.ts index 2a18273036..b147daf3df 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -1186,6 +1186,26 @@ export function applyRanges(parsed: ReturnType, ctx: } if (r.blocksCreated === 0) { + // #4521: the idempotent no-op class — kernel folded nothing because + // there was nothing left to fold (every range already covered, or + // entirely inside the protected zone), and that is NOT a failure: + // no breaker arm, no loop-guard streak, success-shaped receipt. + // This was the dominant "compression FAILED, retry, FAILED again" + // driver on long sessions (live incident: session 01a18a4d, 4 + // consecutive refusals 20:09 over ranges that were 100% legal). + if (r.errors.length === 0) { + const warnText = r.warnings.length > 0 ? r.warnings.join(" ") : "every requested range was already folded or protected."; + ctx.log(`[acp-proxy: compress no-op ${detail} → 0 blocks (nothing left to fold; nothing failed)]`); + // Reuse the failure-path recovery hint verbatim: a model that + // keeps aiming at covered/protected ranges needs the live + // pointer (eligible ranges, distillation target, or the explicit + // stop wording) — without it the no-op receipt says "do not + // resend" but never says what TO do, which is how the retry + // loops persisted (#4521). eligibilityFailure stays guarded by + // compressionRecovery itself (never a guessed span). + const recovery = compressionRecovery(ctx); + return compressResult(`[Compression no-op: nothing was folded and nothing failed — ${warnText}${recovery.hint} Continue your current task.]`, "applied", 0, "noop"); + } const errs = r.errors.join("; ") || "no blocks created"; // #2362: machine-readable failure class for the [plugin] execution // line — outcome=refused alone never says why in the logs. diff --git a/tests/compress-fingerprint-refold.test.ts b/tests/compress-fingerprint-refold.test.ts index fa622cedc4..5b7feead46 100644 --- a/tests/compress-fingerprint-refold.test.ts +++ b/tests/compress-fingerprint-refold.test.ts @@ -152,7 +152,10 @@ test("#1294 P1: a failed re-compress emits no fingerprint lines and keeps its re const ok = applyRanges(parseCompressInput({ content: [{ startId: "m00001", endId: "m00002", summary: "one" }] }), ctx).text; assert.ok(ok.includes("tokens saved"), ok); const again = applyRanges(parseCompressInput({ content: [{ startId: "m00001", endId: "m00002", summary: "two" }] }), ctx).text; - assert.ok(again.startsWith("[Compression FAILED"), again.split("\n")[0]); + // #4521: re-compressing the same range is the idempotent no-op — success- + // shaped receipt, still no fingerprint lines (nothing was stored). + assert.ok(again.startsWith("[Compression no-op"), again.split("\n")[0]); + assert.match(again, /do not resend/); assert.doesNotMatch(again, /… tail "/); }); @@ -326,9 +329,9 @@ test("#2544: receipt states summaries are stored in full and steers to search_co } assert.ok(lastFp > 0, `fingerprint lines present: ${out}`); assert.equal(lines[lastFp + 1], STEERING_2544, "steering directly after the last fingerprint, nothing between"); - // A failed re-compress has no fingerprints and must not carry the note. + // A no-op re-compress (#4521) has no fingerprints and must not carry the note. const again = applyRanges(parseCompressInput({ content: [{ startId: "m00001", endId: "m00002", summary: "two" }] }), ctx).text; - assert.ok(again.startsWith("[Compression FAILED"), again.split("\n")[0]); + assert.ok(again.startsWith("[Compression no-op"), again.split("\n")[0]); assert.ok(!again.includes(STEERING_2544), "no steering without fingerprints"); // The log copy diverges by design (#1718): length-only fingerprints, no note. const logLine = ctx.logs.find((l) => l.startsWith("[acp-proxy: [Compressed")) ?? ""; diff --git a/tests/compress-recovery-ranges.test.ts b/tests/compress-recovery-ranges.test.ts index f934138b83..db6d4ae9dd 100644 --- a/tests/compress-recovery-ranges.test.ts +++ b/tests/compress-recovery-ranges.test.ts @@ -51,18 +51,20 @@ function advertisedRanges(text: string): Array<[string, string]> { return [...hint.matchAll(/(m\d+)\s*[–-]\s*(m\d+)/g)].map((match) => [match[1], match[2]]); } -test("already-folded rejection recommends ranges that exclude protected messages and actually compress", () => { +test("already-folded retry is a no-op receipt that recommends ranges which exclude protected messages and actually compress", () => { const ctx = makeCtx(12); fold(ctx, "m00001", "m00002"); const rejected = request(ctx, "m00001", "m00002"); - assert.equal(rejected.outcome, "refused"); + // #4521: the covered retry is the idempotent no-op — success-shaped, single line. + assert.equal(rejected.outcome, "applied"); + assert.ok(rejected.text.startsWith("[Compression no-op"), rejected.text); const ranges = advertisedRanges(rejected.text); assert.ok(ranges.length > 0); for (const [start, end] of ranges) { assert.ok(Number(start.slice(1)) >= 3 && Number(end.slice(1)) <= 7, `${start}..${end} must exclude covered and protected refs`); assert.equal(request(ctx, start, end).outcome, "applied", `${start}..${end} must pass the real submit gate`); } - assert.equal(rejected.text.split("\n").length, 1, "failure remains visible as one complete marker line"); + assert.equal(rejected.text.split("\n").length, 1, "receipt remains visible as one complete marker line"); }); test("fully protected tail yields a stop instruction rather than a misleading retry span", () => { @@ -70,11 +72,15 @@ test("fully protected tail yields a stop instruction rather than a misleading re fold(ctx, "m00001", "m00002"); const blocksBefore = JSON.stringify(ctx.session.state.blocks); for (let attempt = 0; attempt < 2; attempt++) { + // #4521: protected-range retries are no-ops now, but the protective + // contract is unchanged — never advertise a protected span, always + // steer to "continue without compressing" when nothing is eligible. const rejected = request(ctx, "m00003", "m00007"); - assert.equal(rejected.outcome, "refused"); + assert.equal(rejected.outcome, "applied"); + assert.match(rejected.text, /protected zone/); assert.match(rejected.text, /continue the task without compressing/); assert.doesNotMatch(rejected.text, /Live compressible refs|Retry NOW|extend your range/); - assert.equal(JSON.stringify(ctx.session.state.blocks), blocksBefore, "failed attempts must not change history coverage"); + assert.equal(JSON.stringify(ctx.session.state.blocks), blocksBefore, "no-op attempts must not change history coverage"); } }); @@ -93,7 +99,7 @@ test("sub-minimum raw leftovers are not advertised as compressible", () => { ctx.messages[2].text = "a".repeat(600); ctx.messages[3].text = "b".repeat(600); const result = request(ctx, "m00001", "m00002"); - assert.equal(result.outcome, "refused"); + assert.equal(result.outcome, "applied"); assert.match(result.text, /continue the task without compressing/); assert.doesNotMatch(result.text, /Live compressible refs|Retry NOW/); }); @@ -103,7 +109,7 @@ test("recovery includes eligible gaps before the highest active block boundary", fold(ctx, "m00004", "m00008"); fold(ctx, "m00012", "m00016"); const result = request(ctx, "m00012", "m00016"); - assert.equal(result.outcome, "refused"); + assert.equal(result.outcome, "applied"); const ranges = advertisedRanges(result.text); assert.ok(ranges.some(([start]) => start === "m00001"), "eligible old gaps must remain discoverable"); assert.ok(ranges.some(([start, end]) => Number(start.slice(1)) <= 9 && Number(end.slice(1)) >= 11), `eligible middle gaps must remain discoverable: ${result.text}`); @@ -119,7 +125,7 @@ test("token-based recent protection is honored even beyond the last-message coun fold(ctx, "m00001", "m00002"); ctx.config.preserveRecentTokens = 10000; const result = request(ctx, "m00003", "m00004"); - assert.equal(result.outcome, "refused"); + assert.equal(result.outcome, "applied"); assert.match(result.text, /protected zone/); assert.match(result.text, /continue the task without compressing/); assert.doesNotMatch(result.text, /Live compressible refs|Retry NOW/); @@ -139,7 +145,9 @@ test("failure recovery does not stop an active block-distillation nudge", () => assert.equal(result.outcome, "applied", result.text); } const result = request(ctx, "m00009", "m00012"); - assert.equal(result.outcome, "refused"); + // #4521: covered retry = no-op; the recovery hint must still surface the + // actionable distillation nudge instead of a stop verdict. + assert.equal(result.outcome, "applied"); assert.match(result.text, /block-distillation nudge is active/); assert.doesNotMatch(result.text, /continue the task without compressing|Retry NOW/); }); @@ -149,10 +157,12 @@ test("failed live eligibility check never falls back to a guessed ref span", () fold(ctx, "m00001", "m00002"); ctx.core = { ...ctx.core, processTurn: () => { throw new Error("synthetic live eligibility failure"); } }; const result = request(ctx, "m00001", "m00002"); - assert.equal(result.outcome, "refused"); + // #4521: the no-op receipt still owes the model a truthful eligibility + // verdict; when the live check throws, the old guard wording fires verbatim. + assert.equal(result.outcome, "applied"); assert.match(result.text, /eligibility could not be determined/); assert.match(result.text, /acp_status before retrying/); - assert.doesNotMatch(result.text, /Live compressible refs|Retry NOW/); + assert.doesNotMatch(result.text, /Live compressible refs|Current compressible ranges|Retry NOW/); }); test("unexpected compression exceptions and their repeat guard use current eligibility", () => { diff --git a/tests/compress-success-tail.test.ts b/tests/compress-success-tail.test.ts index 1d2ab243b2..fc661fdda2 100644 --- a/tests/compress-success-tail.test.ts +++ b/tests/compress-success-tail.test.ts @@ -239,9 +239,16 @@ test("#1387: partial failure (some ranges rejected) keeps the old silent tail", assert.ok(true, "atomic batch rejection (kernel-owned semantics)"); return; } + // #4521: a valid range paired with an all-protected range is now partial + // SUCCESS, not failure — the protected range becomes a no-op warning on + // the receipt instead of a batch rejection. Since nothing failed and the + // recompute finds nothing else compressible, the stop signal correctly + // fires (this is the loop-terminator the failing session never got). assert.ok(out.startsWith("[Compressed"), `partial success expected, got: ${out.slice(0, 120)}`); - assert.ok(!out.includes(NO_RANGES_REMAIN_TEXT), "stop signal suppressed on partial failure:\n" + out); - assert.ok(!out.includes(RANGES_HEADER), "no ranges advertised on partial failure:\n" + out); + assert.match(out, /Range m00003\.\.m00007 folded nothing.*protected zone/s); + assert.match(out, /do not resend this range/); + assert.ok(out.includes(NO_RANGES_REMAIN_TEXT), "stop signal after a warning-only partial success:\n" + out); + assert.ok(!out.includes(RANGES_HEADER), "no ranges advertised when nothing compressible remains:\n" + out); }); test("#1387: tail recompute respects the kernel's own eligibility (shouldInject=false leftovers are not advertised)", () => { diff --git a/tests/issue2146-compress-loop-breaker.test.ts b/tests/issue2146-compress-loop-breaker.test.ts index 02e2e7344c..1618a0a14c 100644 --- a/tests/issue2146-compress-loop-breaker.test.ts +++ b/tests/issue2146-compress-loop-breaker.test.ts @@ -191,7 +191,8 @@ test("#2146: stale-generation receipts scrub the phantom refs; in-frontier failu assert.match(stale, /\[stale-ref\]/, "scrubbed marker takes their place"); const mixed = applyRanges(parseCompressInput(compressArgs("m00001", "m00009")), ctx).text; - assert.ok(mixed.startsWith("[Compression FAILED:"), "still fails"); - assert.ok(!mixed.includes("above this session's highest ref"), "note suppressed (start inside frontier)"); - assert.match(mixed, /m00009/, "an in-frontier unknown endpoint stays verbatim — a real typo the model can correct"); + assert.ok(mixed.startsWith("[Compressed "), "#4521 flip: an out-frontier endId clamps to the highest allocated ref instead of failing"); + assert.match(mixed, /endId="m00009" is above this session's highest allocated ref \(m00008\)/, "the receipt names the clamp"); + assert.match(mixed, /clamped to it/, "the model is told what happened to its endpoint"); + assert.match(mixed, /m00009/, "the above-frontier endpoint stays verbatim (typo-correctable)"); }); diff --git a/tests/issue2360-compress-receipt.test.ts b/tests/issue2360-compress-receipt.test.ts index d0fa0818da..b8f07a04f4 100644 --- a/tests/issue2360-compress-receipt.test.ts +++ b/tests/issue2360-compress-receipt.test.ts @@ -125,7 +125,7 @@ test("#2360: unknown-ref failures label stale-ref; covered ranges label covered- const ok = applyRanges(args("m00003", "m00008"), fullCtx).text; assert.ok(ok.startsWith("[Compressed "), `large range compresses (got: ${ok.slice(0, 120)})`); const again = applyRanges(args("m00003", "m00008"), fullCtx).text; - assert.ok(again.startsWith("[Compression FAILED:")); - assert.match(again, /covered by active block/); - assert.match(again, /\[cause: covered-by-block/); + assert.ok(again.startsWith("[Compression no-op:"), "#4521 flip: a fully covered range is an idempotent no-op success, not a failure"); + assert.match(again, /covered by active block/, "the no-op receipt still labels the cause"); + assert.doesNotMatch(again, /\[cause: covered-by-block\]/, "no-op receipts carry no failure cause tag"); }); diff --git a/tests/issue2929-codex-fold-double-miss.test.ts b/tests/issue2929-codex-fold-double-miss.test.ts index 4803085424..81b61ca727 100644 --- a/tests/issue2929-codex-fold-double-miss.test.ts +++ b/tests/issue2929-codex-fold-double-miss.test.ts @@ -35,9 +35,14 @@ type Item = Record; const SUMMARY_MARKER = "[Compressed conversation section]"; const NUDGE_MARKER = "Compressible ranges ("; // Indices verified against the kernel's nudge gate with nudgeGrowthTokens=2000 -// and ~9KiB filler turns: the first T1 injection lands on upstream request 7. -const PROXY_TOOL_AT = 7; // upstream request index whose response = acp_status call (round 1) -const FOLD_AT = 8; // upstream request index whose response = compress call (round 2) +// and ~9KiB filler turns. Pre-#4521 (master) the injections landed on +// requests {7,8,9}; since #4521 the armed-but-silent status line rides an +// ephemeral carrier on the gated turns and its shown-marker feeds the nudge +// gate back, so the schedule shifts to {4,6,8,9} — the fold/proxy-tool-round +// mechanics under test are unchanged, only this probe's anchor re-calibrates +// to the next nudge-carrying request pair (6 → tool round, 7 → fold round). +const PROXY_TOOL_AT = 6; // upstream request index whose response = acp_status call (round 1) +const FOLD_AT = 7; // upstream request index whose response = compress call (round 2) // FOLD_AT+1 = post-fold refreshed re-request (round 3); FOLD_AT+2 = next steady client turn. const TOTAL_TURNS = 13; diff --git a/tests/issue3302-sparse-ref-tombstones.test.ts b/tests/issue3302-sparse-ref-tombstones.test.ts index 7ab0090d73..1e8127f1d0 100644 --- a/tests/issue3302-sparse-ref-tombstones.test.ts +++ b/tests/issue3302-sparse-ref-tombstones.test.ts @@ -107,7 +107,10 @@ test("failure receipt falls back to the last nudge's compressible table when no }; request(ctx, "m00001", "m00002"); - const rejected = request(ctx, "m00001", "m00002"); + // #4521 flip: re-requesting an already-covered range is now an idempotent no-op SUCCESS, + // so the refusal trigger moves to refs unknown to the session (stale generation) — the + // fallback-to-last-nudge-table behavior under test is unchanged. + const rejected = request(ctx, "m00099", "m00100"); assert.equal(rejected.outcome, "refused"); assert.match(rejected.text, /Current compressible ranges from the last compression check/); assert.doesNotMatch(rejected.text, /Call acp_status before retrying/); diff --git a/tests/never-preserve-recent-tools.test.ts b/tests/never-preserve-recent-tools.test.ts index b32ec950f2..8194b46288 100644 --- a/tests/never-preserve-recent-tools.test.ts +++ b/tests/never-preserve-recent-tools.test.ts @@ -109,7 +109,7 @@ function buildBody(): AnthropicRequestBody { return body; } -function foldReadTurn(neverList: string[] | undefined, preserveList?: string[]): { errors: string[]; blocksCreated: number; readCovered: boolean } { +function foldReadTurn(neverList: string[] | undefined, preserveList?: string[]): { errors: string[]; warnings: string[]; blocksCreated: number; readCovered: boolean } { const core = createCore(); const state = createInitialState(); const config = { @@ -137,6 +137,9 @@ function foldReadTurn(neverList: string[] | undefined, preserveList?: string[]): const covered = coveredMessageIds(res.state); return { errors: res.result.errors, + // #4521: an all-protected range is now an idempotent no-op success + // whose verdict rides result.warnings (not errors). + warnings: res.result.warnings, blocksCreated: res.result.blocksCreated, readCovered: covered.has(readResult.id), }; @@ -151,21 +154,24 @@ test("kernel default list: fresh read inside the recent window folds immediately test("read removed from the list: the fresh read pair gains recent-zone protection (#1277)", () => { const out = foldReadTurn(["decompress", "search_context", "bash"]); assert.equal(out.blocksCreated, 0, "nothing folds — the whole range is protected"); - assert.match(out.errors[0] ?? "", /protected/i); + // #4521: protected-only ranges report as a no-op success (warning), not an error. + assert.match([...out.errors, ...out.warnings].join(" "), /protected/i); assert.ok(!out.readCovered, "read result untouched"); }); test("empty list []: same protection as removing read (max-protection escape hatch)", () => { const out = foldReadTurn([]); assert.equal(out.blocksCreated, 0); - assert.match(out.errors[0] ?? "", /protected/i); + // #4521: verdict now arrives as a no-op success warning. + assert.match([...out.errors, ...out.warnings].join(" "), /protected/i); assert.ok(!out.readCovered); }); test("kernel positive knob: preserveRecentTools [\"read\"] is the one-line remedy", { skip: SKIP_PRESCRIPTION }, () => { const out = foldReadTurn(undefined, ["read"]); assert.equal(out.blocksCreated, 0, "nothing folds — the read pair gained zone protection"); - assert.match(out.errors[0] ?? "", /protected/i); + // #4521: verdict now arrives as a no-op success warning. + assert.match([...out.errors, ...out.warnings].join(" "), /protected/i); assert.ok(!out.readCovered, "read result untouched"); }); diff --git a/tests/preflight-dead-ranges.test.ts b/tests/preflight-dead-ranges.test.ts index 981803b0f9..fbe92d18a7 100644 --- a/tests/preflight-dead-ranges.test.ts +++ b/tests/preflight-dead-ranges.test.ts @@ -123,11 +123,16 @@ test("#2638 a structurally-dead span is preview-rejected once, remembered, and n // Turn 1: the dead span is nominated, previewed, rejected ONCE, remembered; the // foldable tail still folds. Without the memory the same span is re-previewed in // later rounds of this very invocation ("nominated twice within one request"). + // Both kernel-era verdict wordings count as the structural rejection: the failure- + // era error text and the #4521 success-warning ("folded nothing — ..."). + const isStructuralReject = (l: string) => + l.includes("preview rejected") + && (l.includes("no new compressible messages") || l.includes("folded nothing")); H.push({ id: `msg-${H.length + 1}`, role: "user", contentType: "text", text: "NEW TURN 1 ".repeat(60) }); H.push({ id: `msg-${H.length + 1}`, role: "assistant", contentType: "text", text: "REPLY 1 ".repeat(60) }); const logs1: string[] = []; const r1 = await runPreflight(logs1); - const rejects1 = logs1.filter((l) => l.includes("preview rejected") && l.includes("no new compressible messages")); + const rejects1 = logs1.filter(isStructuralReject); assert.equal(rejects1.length, 1, `expected exactly one structural rejection in invocation 1, got: ${logs1.join("\n")}`); const rejectsKey = /preview rejected range (m\d+:m\d+)/.exec(rejects1[0])?.[1]; assert.equal(rejectsKey, "m00007:m00022", `the dead span is the carrier cluster A..M22, got: ${logs1.join("\n")}`); @@ -142,7 +147,7 @@ test("#2638 a structurally-dead span is preview-rejected once, remembered, and n H.push({ id: `msg-${H.length + 1}`, role: "assistant", contentType: "text", text: "REPLY 2 ".repeat(60) }); const logs2: string[] = []; const r2 = await runPreflight(logs2); - const rejects2 = logs2.filter((l) => l.includes("preview rejected") && l.includes("no new compressible messages")); + const rejects2 = logs2.filter(isStructuralReject); assert.equal(rejects2.length, 0, `dead span must not be re-nominated in invocation 2, got: ${logs2.join("\n")}`); assert.ok(r2.compressedRanges >= 1, "fresh tail must still fold on invocation 2"); assert.ok(r2.hadFoldableCandidates);