diff --git a/kernel/package-lock.json b/kernel/package-lock.json index 8a2c4491d7..7ecaea2d8c 100644 --- a/kernel/package-lock.json +++ b/kernel/package-lock.json @@ -1,12 +1,12 @@ { "name": "acp-kernel", - "version": "0.0.201", + "version": "0.0.202", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "acp-kernel", - "version": "0.0.201", + "version": "0.0.202", "license": "MIT", "devDependencies": { "@types/node": "^22.0.0", diff --git a/kernel/package.json b/kernel/package.json index 5ac694d897..80a65d212a 100644 --- a/kernel/package.json +++ b/kernel/package.json @@ -1,6 +1,6 @@ { "name": "acp-kernel", - "version": "0.0.201", + "version": "0.0.202", "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/compress.ts b/kernel/src/compress.ts index 43a0238c6e..f3105fd96b 100644 --- a/kernel/src/compress.ts +++ b/kernel/src/compress.ts @@ -907,6 +907,10 @@ export function createCore(ports: Ports = {}): CompressionCore { state.stats.compressionCount += blocksCreated; state.stats.tokensCompressed += tokensCompressed; + // #6720: tokens of the last tier-1 offer this fold left unharvested — set + // only on a partial tier-1 fold, rendered into the receipt below. + let remainingPending: number | undefined; + if (blocksCreated > 0) { // #3769: record where THIS fold starts so the next processTurn sweeps // orphan failures at/after it cache-neutrally, leaving orphans before it @@ -960,10 +964,17 @@ export function createCore(ports: Ports = {}): CompressionCore { const firePending = state.nudge.lastT1FirePending; if (distilled) { state.nudge.t1HarvestStall = false; + state.nudge.partialFoldReinjectArmed = false; state.nudge.lastT1FirePending = undefined; } else if (firePending !== undefined && firePending > 0) { - state.nudge.t1HarvestStall = - tokensCompressed < STALL_HARVEST_RATIO * firePending; + const partial = tokensCompressed < STALL_HARVEST_RATIO * firePending; + state.nudge.t1HarvestStall = partial; + // #6720: arm the one-shot re-inject on a partial fold so the next + // processTurn re-offers T1 without waiting for the full growth floor. + state.nudge.partialFoldReinjectArmed = partial; + if (partial) { + remainingPending = Math.max(0, firePending - tokensCompressed); + } state.nudge.lastT1FirePending = undefined; } // A successful compression is by definition viable progress — restart @@ -999,6 +1010,11 @@ export function createCore(ports: Ports = {}): CompressionCore { // 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 } : {}), + // #6720: surface the unharvested remainder of a partial tier-1 fold so + // the receipt says "you left X" instead of a done-shaped receipt. + ...(remainingPending !== undefined + ? { remainingPending } + : {}), }, }; } @@ -1587,6 +1603,11 @@ const nudgeNode: PipelineNode = { if (nudge.tier === 1 && nudge.breakdown) { stamped.lastT1FirePending = nudge.breakdown.pendingT1; } + // #6720: a displayed tier-1 nudge consumes the one-shot re-inject — the + // model got its re-offer, so disarm; the next partial fold re-arms it. + if (nudge.tier === 1) { + stamped.partialFoldReinjectArmed = false; + } } return { @@ -2210,6 +2231,25 @@ function decideNudge(input: NudgeInput): NudgeDecision { input.tokenCountSource === "estimate" && config.nudge.requireUsageSampleForPressure !== false && estimateHoldStreak <= maxEstimateHoldRounds; + // #6762: a FAILED tool result at the tail means the model's LAST action just + // failed. A pressure-band nudge fired this round would push the model to + // compress, and the failure context (the error message, the failed command) + // is the first thing a fold drops — the model then "forgets" the error and + // retries blindly. Hold the pressure-band injection for THIS round only: the + // nudge re-arms on the next turn (self-recovering), so a session that keeps + // failing still gets its nudge on the first non-failure turn. Pressure-band + // only — the growth band has a cadence and does not re-fire every turn, so + // the "re-fires on a failed turn" pathology is pressure-specific. The sidecar + // survives the pre-nudge transforms (crush/absorb-prompt/filter spread the + // message object; none append a new tail message), so the last element is + // the original failed tool result. + const lastMsg = input.messages[input.messages.length - 1]; + const toolFailureHold = + pressure && + lastMsg !== undefined && + lastMsg.role === "tool" && + lastMsg.contentType === "tool-result" && + lastMsg.toolIsError === true; const baseline = state.nudge.lastPerMessageNudgeTokens; const hadPendingNudge = state.nudge.lastNudgeShownTokens > 0; @@ -2376,7 +2416,8 @@ function decideNudge(input: NudgeInput): NudgeDecision { if ( best !== null && bestPending >= minPressureBenefit && - !pressureDeferredNoSample + !pressureDeferredNoSample && + !toolFailureHold ) { injectedTier = best; stallEscalated = t1HarvestStall && best !== 1; @@ -2451,6 +2492,29 @@ function decideNudge(input: NudgeInput): NudgeDecision { } } + // #6720: partial-fold re-inject. The last tier-1 fold was PARTIAL (armed the + // one-shot flag in applyCompression) and it left >= half the T1 threshold on + // the table. Re-offer T1 on THIS request without waiting for the full growth + // floor — the ratchet (#6720) is exactly the wait: each partial fold's + // remainder rides along, the tail grows 10-20K by the next nudge, and the + // post-fold floor rises monotonically. Not gated on usage: a low-usage long + // session (1M window, usage <= 24%) never crosses minContextLimitPct, so the + // first-sight/backlog bypasses never fire and the remainder would stall for + // a full growth interval every round. At most one re-inject per partial fold: + // the flag is cleared when the re-injected nudge is displayed (nudge-stamp + // node) or by a compliant full fold; an ignored re-offer keeps + // lastNudgeShownTokens set so the normal growth gate re-asserts and this + // never loops on an unresponsive session. + if ( + injectedTier === null && + !pressure && + state.nudge.partialFoldReinjectArmed === true && + t1Eff >= 0.5 * tierThresholds[1] + ) { + injectedTier = 1; + injectedReason = `T1 partial-fold re-inject: last fold left ${t1Eff} pending (>= 50% of ${tierThresholds[1]}) — re-offering without the full ${growthFloor} growth wait, usage ${Math.round(usage * 100)}%`; + } + const shouldInject = injectedTier !== null; if (shouldInject && firstSightMassReady) { injectedReason += " [first-sight mass]"; @@ -2465,6 +2529,9 @@ function decideNudge(input: NudgeInput): NudgeDecision { } else if (pressure && pressureDeferredNoSample) { const label = emergencyOverride ? "EMERGENCY" : "OVER-LIMIT"; reason = `${label}: usage ${Math.round(usage * 100)}% but the meter reading is estimate-grade (no billing sample landed this cycle, max pending ${maxPending}) — pressure injection held (estimate round ${estimateHoldStreak} of ${maxEstimateHoldRounds}) until a usage-grade reading decides or the estimate-hold backstop elapses; hard-overflow backstop unaffected`; + } else if (pressure && toolFailureHold) { + const label = emergencyOverride ? "EMERGENCY" : "OVER-LIMIT"; + reason = `${label}: usage ${Math.round(usage * 100)}% but the latest tool result FAILED — pressure injection held this round so the model handles the failure first (max pending ${maxPending}); the nudge re-arms on the next turn`; } else if (pressure) { const label = emergencyOverride ? "EMERGENCY" : "OVER-LIMIT"; reason = @@ -2560,6 +2627,7 @@ function decideNudge(input: NudgeInput): NudgeDecision { overLimit: overLimit ? 1 : 0, emergencyOverride: emergencyOverride ? 1 : 0, pressureDeferredNoSample: pressureDeferredNoSample ? 1 : 0, + toolFailureHold: toolFailureHold ? 1 : 0, estimateHoldStreak, minPressureBenefit, pendingT1: tiers[1]!.pending, diff --git a/kernel/src/types.ts b/kernel/src/types.ts index 928504c10a..e030295113 100644 --- a/kernel/src/types.ts +++ b/kernel/src/types.ts @@ -52,6 +52,12 @@ export interface CoreMessage { * The render path only ever READS this stored value — it never calls * Date.now() itself (prefix-cache invariant). */ ts?: number; + /** #6762: the tool result FAILED (Anthropic tool_result.is_error). Set by the + * wire adapters on a `role:"tool"` / `contentType:"tool-result"` core so the + * nudge decision can see a failed turn: while the model's LAST action just + * failed, a pressure-band nudge would push it to compress and the failure + * context is the first thing lost. Metering/decision-only: never rendered. */ + toolIsError?: boolean; } export type CompressionTier = 1 | 2 | 3; @@ -172,6 +178,16 @@ export interface NudgeState { * a mass-qualified T2/T3 fire ahead of T1 instead of starving forever. * Cleared by a compliant harvest or any distillation. */ t1HarvestStall?: boolean; + /** #6720: one-shot re-inject armed by a PARTIAL tier-1 fold (the fold + * harvested < STALL_HARVEST_RATIO of the offered pending, leaving >= half + * the threshold on the table). While armed, the NEXT processTurn re-offers + * T1 without waiting for the full growth floor — this kills the ratchet + * where each partial fold's remainder rides along and the post-fold floor + * rises monotonically (#6720). Cleared when the re-injected T1 nudge is + * displayed (the model gets its one re-offer) or by a compliant full fold; + * an ignored re-offer keeps lastNudgeShownTokens set so the normal growth + * gate re-asserts and this never loops on an unresponsive session. */ + partialFoldReinjectArmed?: boolean; /** #5865 estimate-hold backstop: count of CONSECUTIVE processTurn events * whose meter reading was estimate-grade. Reset to 0 by any usage-grade * reading. Only consulted while nudge.requireUsageSampleForPressure is on: @@ -793,6 +809,10 @@ export interface NudgeBreakdown { * (NudgeConfig.requireUsageSampleForPressure); the host early- * advertising arm (emergencyNudge) must mirror this bit. Absent on * hand-built decisions — pre-fix semantics apply there. + * - toolFailureHold (1/0, #6762): the pressure-band injection was held + * because the latest tool result FAILED — the model is about to act on + * a fresh failure, so a nudge would make it compress and lose the error + * context. Self-recovering: the nudge re-arms on the next turn. */ [key: string]: number; } @@ -827,6 +847,13 @@ export interface ApplyCompressionResult { * source. Hosts render it with the "[Compression no-op:" prefix and fold * the exchange away like other zero-applied compress results (#4289). */ noop?: true; + /** #6720: tokens of the last tier-1 offer that this fold did NOT harvest + * (offered pending minus what actually folded, floored at 0). Present + * only on a PARTIAL tier-1 fold — the host renders it into the compress + * receipt so the model sees the remainder instead of a "done" receipt, + * which is what let weak models treat a partial fold as complete and let + * the floor ratchet up. Omitted on full/compliant folds and no-ops. */ + remainingPending?: number; }; } diff --git a/kernel/tests/issue6720-partial-fold-reinject.test.ts b/kernel/tests/issue6720-partial-fold-reinject.test.ts new file mode 100644 index 0000000000..369158f60e --- /dev/null +++ b/kernel/tests/issue6720-partial-fold-reinject.test.ts @@ -0,0 +1,237 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { createCore, STALL_HARVEST_RATIO } from "../src/compress.js"; +import { createInitialState } from "../src/state.js"; +import type { Config, CoreMessage } from "../src/types.js"; + +// #6720: the partial-fold ratchet. A weak model answers a T1 nudge with a +// PARTIAL fold (harvests < STALL_HARVEST_RATIO of the offered pending), leaving +// the remainder on the table. That remainder rides along, the tail grows 10-20K +// by the next nudge, and the post-fold floor rises monotonically — because the +// next T1 re-offer waits for a FULL growth floor (50K) of new tokens before it +// can fire again, and a low-usage long session (1M window, usage <= 24%) never +// crosses minContextLimitPct, so the first-sight/backlog bypasses never fire +// either. The fix arms a ONE-SHOT re-inject on a partial fold: the next request +// re-offers T1 without the full growth wait, and the receipt names the +// unharvested remainder. These tests pin the whole chain: +// (A) partial fold arms the flag + reports remainingPending; the NEXT request +// (no growth, low usage) re-injects T1 via the re-inject branch; +// (B) the re-inject is NOT gated on usage (fires at 30% < minContextLimitPct); +// (C) the re-inject is one-shot: once displayed, the flag clears and the next +// no-growth request does NOT re-fire (no loop on an ignored re-offer); +// (D) a compliant FULL fold never arms the flag (negative direction, #3617). +// +// The scenarios deliberately run at LOW usage (below minContextLimitPct): after +// a fold the baselines clear, so at HIGH usage the first-sight mass bypass would +// fire T1 through the normal growth path and mask the re-inject branch. Low +// usage is exactly the 1M-window shape the issue reports. +function buildConfig(overrides: Partial = {}): Config { + return { + tiers: { enabled: true, tier2Trigger: 5, tier3Trigger: 10 }, + nudge: { + maxContextLimitPct: 0.9, + minContextLimitPct: 0.45, + frequency: 1, + iterationThreshold: 15, + force: "soft", + growthRatio: 0.05, + growthFloor: 6000, + growthCap: 50000, + minGrowthFloor: 5000, + minGrowthRatio: 0.45, + emergencyThresholdPct: 0.98, + }, + promotionThreshold: 5, + truncate: { threshold: 1 }, + merge: { maxSummaryLength: 3000, minOldGenBlocks: 3 }, + compress: { minCompressRange: 0, maxSummaryLength: 0, minSummaryLength: 0 }, + protectedTools: [], + preserveRecentMessages: 0, + preserveRecentTokens: 0, + modelContextLimit: 100000, + ...overrides, + }; +} + +function textMessage( + role: CoreMessage["role"], + id: string, + text: string, +): CoreMessage { + return { id, role, contentType: "text", text }; +} + +function refOf(state: { messageRefs: { byRaw: Record } }, rawId: string): string { + const ref = state.messageRefs.byRaw[rawId]; + assert.ok(ref, `raw id ${rawId} has an assigned ref`); + return ref; +} + +function makeMessages(count: number): CoreMessage[] { + return Array.from({ length: count }, (_, i) => + textMessage( + i % 2 === 0 ? "user" : "assistant", + `m${i}`, + `message ${i} `.repeat(2000), + ), + ); +} + +// Drive a session to a T1 fire at LOW usage (below minContextLimitPct) and +// return the state + the offered pending, ready for a fold. +function toT1Fire(core: ReturnType, config: Config, messages: CoreMessage[]) { + let state = createInitialState(); + // Turn 1: stamp the baseline at low usage (20% < 45% minContextLimitPct). + state = core.processTurn({ messages, state, config, tokenCount: 20_000 }).state; + // Turn 2: growth 10k >= floor 5k — the growth leg fires T1 (no usage gate). + const turn = core.processTurn({ messages, state, config, tokenCount: 30_000 }); + state = turn.state; + assert.equal(turn.nudge.shouldInject, true, `reason: ${turn.nudge.reason}`); + assert.equal(turn.nudge.tier, 1, `reason: ${turn.nudge.reason}`); + const offered = turn.state.nudge.lastT1FirePending ?? 0; + assert.ok(offered > 0, "the T1 fire banks its offered pending"); + return { state, offered }; +} + +test("#6720 (A): partial fold arms the re-inject; the next no-growth request re-offers T1", () => { + const core = createCore(); + const config = buildConfig(); + const messages = makeMessages(10); + const { state: fired, offered } = toT1Fire(core, config, messages); + + // The model answers with a PARTIAL fold: one tiny range of a huge offer + // (the #6720 log shape: 4-17K delivered against ~50K offered). + const folded = core.applyCompression({ + ranges: [ + { startRef: refOf(fired, "m0"), endRef: refOf(fired, "m0"), summary: "s".repeat(2000) }, + ], + messages, + state: fired, + config, + }); + assert.equal(folded.result.errors.length, 0, JSON.stringify(folded.result)); + const state = folded.state; + assert.ok( + folded.result.tokensCompressed < STALL_HARVEST_RATIO * offered, + `precondition: harvest ${folded.result.tokensCompressed} is partial vs offered ${offered}`, + ); + assert.equal(state.nudge.partialFoldReinjectArmed, true, "partial fold arms the re-inject"); + assert.equal(state.nudge.t1HarvestStall, true, "partial fold also sets the stall"); + // The receipt surface: the unharvested remainder is reported. + assert.ok( + folded.result.remainingPending !== undefined && folded.result.remainingPending > 0, + `remainingPending reported: ${JSON.stringify(folded.result)}`, + ); + assert.equal( + folded.result.remainingPending, + Math.max(0, offered - folded.result.tokensCompressed), + "remainingPending = offered - harvested", + ); + + // Next request: NO growth (same tokenCount), low usage. The normal growth + // gate is blocked (growth 0 < floor 5k; first-sight off at low usage) — only + // the re-inject branch can fire T1. + const next = core.processTurn({ messages, state, config, tokenCount: 30_000 }); + assert.equal(next.nudge.shouldInject, true, `reason: ${next.nudge.reason}`); + assert.equal(next.nudge.tier, 1, `reason: ${next.nudge.reason}`); + assert.match(next.nudge.reason ?? "", /partial-fold re-inject/); +}); + +test("#6720 (B): the re-inject is not gated on usage (fires at 30% < minContextLimitPct)", () => { + const core = createCore(); + const config = buildConfig(); + const messages = makeMessages(10); + const { state: fired, offered } = toT1Fire(core, config, messages); + const folded = core.applyCompression({ + ranges: [ + { startRef: refOf(fired, "m0"), endRef: refOf(fired, "m0"), summary: "s".repeat(2000) }, + ], + messages, + state: fired, + config, + }); + const state = folded.state; + assert.ok(folded.result.tokensCompressed < STALL_HARVEST_RATIO * offered); + assert.equal(state.nudge.partialFoldReinjectArmed, true); + + // 30% usage is below minContextLimitPct (45%): the first-sight mass bypass + // and the backlog drain are both usage-gated OFF. Only the re-inject — which + // is deliberately NOT usage-gated — can re-offer T1 here. + const next = core.processTurn({ messages, state, config, tokenCount: 30_000 }); + assert.equal(next.nudge.shouldInject, true, `reason: ${next.nudge.reason}`); + assert.equal(next.nudge.tier, 1, `reason: ${next.nudge.reason}`); + assert.match(next.nudge.reason ?? "", /partial-fold re-inject/); + assert.match(next.nudge.reason ?? "", /usage 30%/); +}); + +test("#6720 (C): the re-inject is one-shot — an ignored re-offer does not loop", () => { + const core = createCore(); + const config = buildConfig(); + const messages = makeMessages(10); + const { state: fired } = toT1Fire(core, config, messages); + const folded = core.applyCompression({ + ranges: [ + { startRef: refOf(fired, "m0"), endRef: refOf(fired, "m0"), summary: "s".repeat(2000) }, + ], + messages, + state: fired, + config, + }); + const state = folded.state; + assert.equal(state.nudge.partialFoldReinjectArmed, true); + + // The re-inject fires and is DISPLAYED — the nudge-stamp node consumes the + // one-shot flag on display (not on fold). + const reinject = core.processTurn({ messages, state, config, tokenCount: 30_000 }); + assert.equal(reinject.nudge.tier, 1, `reason: ${reinject.nudge.reason}`); + assert.match(reinject.nudge.reason ?? "", /partial-fold re-inject/); + assert.equal( + reinject.state.nudge.partialFoldReinjectArmed, + false, + "displaying the re-inject clears the one-shot flag", + ); + + // The model IGNORES the re-offer (no fold). The next no-growth request must + // NOT re-inject: the flag is cleared and the normal growth gate re-asserts. + const after = core.processTurn({ + messages, + state: reinject.state, + config, + tokenCount: 30_000, + }); + assert.doesNotMatch(after.nudge.reason ?? "", /partial-fold re-inject/); + assert.equal(after.nudge.shouldInject, false, `no loop on an ignored re-offer: ${after.nudge.reason}`); +}); + +test("#6720 (D): a compliant full fold never arms the re-inject (negative direction)", () => { + const core = createCore(); + const config = buildConfig(); + const messages = makeMessages(10); + const { state: fired, offered } = toT1Fire(core, config, messages); + + // Fold EVERYTHING the offer covered (the compliant model). + const folded = core.applyCompression({ + ranges: [ + { startRef: refOf(fired, "m0"), endRef: refOf(fired, "m9"), summary: "s".repeat(48000) }, + ], + messages, + state: fired, + config, + }); + assert.equal(folded.result.errors.length, 0, JSON.stringify(folded.result)); + const state = folded.state; + assert.ok( + folded.result.tokensCompressed >= STALL_HARVEST_RATIO * offered, + `precondition: eager harvest ${folded.result.tokensCompressed} vs offered ${offered}`, + ); + assert.notEqual( + state.nudge.partialFoldReinjectArmed, + true, + "a full fold never arms the re-inject", + ); + assert.equal( + folded.result.remainingPending, + undefined, + "no remainingPending on a compliant fold", + ); +}); diff --git a/kernel/tests/issue6762-tool-failure-nudge-hold.test.ts b/kernel/tests/issue6762-tool-failure-nudge-hold.test.ts new file mode 100644 index 0000000000..44a7a0d24a --- /dev/null +++ b/kernel/tests/issue6762-tool-failure-nudge-hold.test.ts @@ -0,0 +1,209 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { createCore } from "../src/compress.js"; +import { createInitialState } from "../src/state.js"; +import type { Config, CoreMessage } from "../src/types.js"; + +// #6762: the kernel nudge decision (NudgeInput) has no tool-failure signal, so +// the pressure band (usage >= maxContextLimitPct) re-fires on EVERY turn — +// including a turn whose LAST message is a FAILED tool result. On such a turn +// the model is about to act on a fresh failure, and a pressure-band nudge +// pushes it to compress; the failure context (the error message, the failed +// command) is the first thing a fold drops, so the model "forgets" the error +// and retries blindly. The fix holds the PRESSURE-BAND injection for that one +// round only (self-recovering: the nudge re-arms on the next turn); the growth +// band is untouched — it has a cadence and does not re-fire every turn, so the +// "re-fires on a failed turn" pathology is pressure-specific. +// +// These tests pin both directions (#3617 gate discipline): +// (A) pressure band HELD when the latest tool result FAILED (EMERGENCY); +// (B) negative: a SUCCESS tool result tail does NOT hold (no over-suppress); +// (C) negative: a plain text tail does NOT hold; +// (D) the OVER-LIMIT band (not EMERGENCY) is held too; +// (E) self-recovery: the held turn's successor (success tail) injects; +// (F) the GROWTH band is unaffected by a failed tail (pressure-band-only). + +function buildConfig(overrides: Partial = {}): Config { + const base = { + tiers: { enabled: true, tier2Trigger: 5, tier3Trigger: 10 }, + nudge: { + maxContextLimitPct: 0.9, + minContextLimitPct: 0.45, + frequency: 1, + iterationThreshold: 15, + force: "soft" as const, + growthRatio: 0.05, + growthFloor: 6000, + growthCap: 50000, + minGrowthFloor: 5000, + minGrowthRatio: 0.45, + emergencyThresholdPct: 0.98, + }, + promotionThreshold: 5, + truncate: { threshold: 1 }, + merge: { maxSummaryLength: 3000, minOldGenBlocks: 3 }, + compress: { minCompressRange: 0, maxSummaryLength: 0, minSummaryLength: 0 }, + protectedTools: [], + preserveRecentMessages: 0, + preserveRecentTokens: 0, + modelContextLimit: 100000, + }; + return { ...base, ...overrides }; +} + +function textMessage( + role: CoreMessage["role"], + id: string, + text: string, +): CoreMessage { + return { id, role, contentType: "text", text }; +} + +function toolCallMessage(id: string): CoreMessage { + return { + id, + role: "assistant", + contentType: "tool-call", + text: "", + toolName: "bash", + toolCallId: "call_1", + }; +} + +function toolResultMessage(id: string, isError: boolean): CoreMessage { + return { + id, + role: "tool", + contentType: "tool-result", + text: isError ? "command failed: exit 1" : "ok", + toolCallId: "call_1", + toolIsError: isError, + }; +} + +// 8 text messages (≈ 44k T1 pending, well above the default minPressureBenefit +// of 5000) + a final tool call/result pair. The LAST message is the result — +// failed, success, or a plain text tail — so the nudge decision sees each shape. +function makeMessages(tail: "failed" | "success" | "text"): CoreMessage[] { + const msgs = Array.from({ length: 8 }, (_, i) => + textMessage(i % 2 === 0 ? "user" : "assistant", `m${i}`, `message ${i} `.repeat(2000)), + ); + if (tail === "text") { + msgs.push(textMessage("assistant", "m8", "message 8 ".repeat(2000))); + msgs.push(textMessage("user", "m9", "message 9 ".repeat(2000))); + } else { + msgs.push(toolCallMessage("m8")); + msgs.push(toolResultMessage("m9", tail === "failed")); + } + return msgs; +} + +test("#6762 (A): pressure band HELD when the latest tool result FAILED (EMERGENCY)", () => { + const core = createCore(); + const config = buildConfig(); + const messages = makeMessages("failed"); + let state = createInitialState(); + state = core.processTurn({ messages, state, config, tokenCount: 10000 }).state; + const turn = core.processTurn({ messages, state, config, tokenCount: 99000 }); + assert.equal(turn.nudge.breakdown.emergencyOverride, 1, "99% >= 98% → emergency band"); + assert.equal(turn.nudge.breakdown.toolFailureHold, 1, "failed tail arms the hold"); + assert.equal( + turn.nudge.shouldInject, + false, + `held despite pending >= floor: ${turn.nudge.reason}`, + ); + assert.match(turn.nudge.reason, /^EMERGENCY:/); + assert.match(turn.nudge.reason, /latest tool result FAILED/); + assert.match(turn.nudge.reason, /re-arms on the next turn/); +}); + +test("#6762 (B): negative — a SUCCESS tool result tail does NOT hold", () => { + const core = createCore(); + const config = buildConfig(); + const messages = makeMessages("success"); + let state = createInitialState(); + state = core.processTurn({ messages, state, config, tokenCount: 10000 }).state; + const turn = core.processTurn({ messages, state, config, tokenCount: 99000 }); + assert.equal(turn.nudge.breakdown.emergencyOverride, 1); + assert.equal(turn.nudge.breakdown.toolFailureHold, 0, "success tail does not arm the hold"); + assert.equal( + turn.nudge.shouldInject, + true, + `injects on a success tail: ${turn.nudge.reason}`, + ); + assert.match(turn.nudge.reason, /^EMERGENCY/); + assert.doesNotMatch(turn.nudge.reason, /FAILED/); +}); + +test("#6762 (C): negative — a plain text tail does NOT hold", () => { + const core = createCore(); + const config = buildConfig(); + const messages = makeMessages("text"); + let state = createInitialState(); + state = core.processTurn({ messages, state, config, tokenCount: 10000 }).state; + const turn = core.processTurn({ messages, state, config, tokenCount: 99000 }); + assert.equal(turn.nudge.breakdown.emergencyOverride, 1); + assert.equal(turn.nudge.breakdown.toolFailureHold, 0, "text tail does not arm the hold"); + assert.equal(turn.nudge.shouldInject, true, `injects on a text tail: ${turn.nudge.reason}`); +}); + +test("#6762 (D): the OVER-LIMIT band (not EMERGENCY) is held too", () => { + const core = createCore(); + const config = buildConfig(); + const messages = makeMessages("failed"); + let state = createInitialState(); + state = core.processTurn({ messages, state, config, tokenCount: 10000 }).state; + // 95% usage: >= maxContextLimitPct (90%) but < emergencyThresholdPct (98%). + const turn = core.processTurn({ messages, state, config, tokenCount: 95000 }); + assert.equal(turn.nudge.breakdown.overLimit, 1, "95% >= 90% → over-limit band"); + assert.equal(turn.nudge.breakdown.emergencyOverride, 0, "below emergency threshold"); + assert.equal(turn.nudge.breakdown.toolFailureHold, 1, "failed tail arms the hold"); + assert.equal(turn.nudge.shouldInject, false, `held in the over-limit band: ${turn.nudge.reason}`); + assert.match(turn.nudge.reason, /^OVER-LIMIT:/); + assert.match(turn.nudge.reason, /latest tool result FAILED/); +}); + +test("#6762 (E): self-recovery — the held turn's successor (success tail) injects", () => { + const core = createCore(); + const config = buildConfig(); + // Turn 1: failed tail at high usage → held. + let state = createInitialState(); + const failed = makeMessages("failed"); + state = core.processTurn({ messages: failed, state, config, tokenCount: 10000 }).state; + const held = core.processTurn({ messages: failed, state, config, tokenCount: 99000 }); + assert.equal(held.nudge.shouldInject, false, `turn 1 held: ${held.nudge.reason}`); + assert.equal(held.nudge.breakdown.toolFailureHold, 1); + + // Turn 2: the model handled the failure — the tail is now a SUCCESS result. + // Same high usage; the hold is off, so the pressure band injects. + const success = makeMessages("success"); + const next = core.processTurn({ messages: success, state: held.state, config, tokenCount: 99000 }); + assert.equal(next.nudge.breakdown.toolFailureHold, 0, "success tail clears the hold"); + assert.equal( + next.nudge.shouldInject, + true, + `turn 2 re-arms: ${next.nudge.reason}`, + ); +}); + +test("#6762 (F): the GROWTH band is unaffected by a failed tail (pressure-band-only)", () => { + const core = createCore(); + const config = buildConfig(); + const messages = makeMessages("failed"); + let state = createInitialState(); + // Turn 1: stamp the baseline at LOW usage (20% < 90% maxContextLimitPct) — + // no pressure. + state = core.processTurn({ messages, state, config, tokenCount: 20000 }).state; + // Turn 2: growth 10k >= floor 5k at 30% usage (still below pressure). The + // growth leg fires T1 even though the tail is a failed tool result — the + // hold is pressure-band-only. + const turn = core.processTurn({ messages, state, config, tokenCount: 30000 }); + assert.equal(turn.nudge.breakdown.overLimit, 0, "30% < 90% → no pressure band"); + assert.equal(turn.nudge.breakdown.toolFailureHold, 0, "no pressure → no hold"); + assert.equal( + turn.nudge.shouldInject, + true, + `growth band fires despite the failed tail: ${turn.nudge.reason}`, + ); + assert.equal(turn.nudge.tier, 1, `T1 growth: ${turn.nudge.reason}`); +}); diff --git a/kernel/tests/nudge.test.ts b/kernel/tests/nudge.test.ts index b433e07c63..d6dfeeec6d 100644 --- a/kernel/tests/nudge.test.ts +++ b/kernel/tests/nudge.test.ts @@ -349,11 +349,18 @@ test("nudge: compress resets baseline, preventing feedback loop", () => { ); const turn = core.processTurn({ messages, state, config, tokenCount: 30000 }); + // #6720: the fold above was PARTIAL (3 of 10 messages ≈ 30% of the offer), + // so the one-shot re-inject re-offers T1 on this request even at 30% usage — + // the remainder drains instead of waiting for a full growth floor. (Before + // #6720 this asserted `shouldInject === false`; the re-inject is the intended + // behavior change that kills the ratchet.) The baseline still re-establishes + // at the new (lower) token count — the §5.7 reset is unchanged. assert.equal( turn.nudge.shouldInject, - false, - "30% < 45% threshold → no nudge", + true, + "partial fold re-injects T1 at low usage (#6720)", ); + assert.match(turn.nudge.reason, /partial-fold re-inject/); assert.equal( turn.state.nudge.lastPerMessageNudgeTokens, 30000, @@ -390,7 +397,15 @@ test("nudge: full growth cycle — baseline → growth → nudge → compress assert.equal(state.nudge.lastPerMessageNudgeTokens, 0); turn = core.processTurn({ messages, state, config, tokenCount: 30000 }); - assert.equal(turn.nudge.shouldInject, false, "30% < 45%"); + // #6720: the fold above was PARTIAL (5 of 20 messages ≈ 25% of the offer), so + // the one-shot re-inject re-offers T1 at 30% usage instead of the old + // "wait for a full growth floor" silence. The baseline still re-establishes. + assert.equal( + turn.nudge.shouldInject, + true, + "partial fold re-injects T1 at low usage (#6720)", + ); + assert.match(turn.nudge.reason, /partial-fold re-inject/); assert.equal( turn.state.nudge.lastPerMessageNudgeTokens, 30000, @@ -1753,9 +1768,16 @@ test("nudge: first-sight bypass falls silent once usage drops below the band (#1 config, tokenCount: 20000, }); + // #6720: the drain fold above was PARTIAL (5 of 20 messages ≈ 25% of the + // offer), so the one-shot re-inject re-offers T1 at 20% usage. The #194 + // first-sight bypass itself stays SILENT below the band — the nudge that + // fires here is the re-inject, NOT the bypass (assert the reason to keep the + // two mechanisms apart). assert.equal( turn2.nudge.shouldInject, - false, - "20% < 45% band → drain complete, pacing resumes", + true, + "partial fold re-injects T1 at low usage (#6720)", ); + assert.match(turn2.nudge.reason, /partial-fold re-inject/); + assert.doesNotMatch(turn2.nudge.reason, /\[first-sight mass\]/, "the #194 bypass stays silent below the band"); }); diff --git a/package-lock.json b/package-lock.json index 72519f7f77..8f31611801 100644 --- a/package-lock.json +++ b/package-lock.json @@ -51,7 +51,7 @@ }, "kernel": { "name": "acp-kernel", - "version": "0.0.201", + "version": "0.0.202", "dev": true, "license": "MIT", "devDependencies": { diff --git a/src/server.ts b/src/server.ts index 3fb954b5ff..b2681ca0c2 100644 --- a/src/server.ts +++ b/src/server.ts @@ -2835,7 +2835,7 @@ export function diagTagSummary(messages: CoreMessage[], sessionId: string, strat return `[${sessionId}] processTurn: ${messages.length} msgs, renderTags=${strategy}, ${textTagged} text tagged, ${toolTagged} tool tagged (should be 0 with text-only)`; } -export function diagNudge(turn: { nudge?: { shouldInject: boolean; reason: string; contextUsage: number; tier: number | null; breakdown?: Record } | null }, sessionId: string, tokenCount: number, limit: number, model: string | undefined, willInject: boolean, decidePending = false, surfaceOff?: string): string { +export function diagNudge(turn: { nudge?: { shouldInject: boolean; reason: string; contextUsage: number; tier: number | null; breakdown?: Record; compressibleRanges?: { startRef: string; endRef: string; tokens: number }[] } | null }, sessionId: string, tokenCount: number, limit: number, model: string | undefined, willInject: boolean, decidePending = false, surfaceOff?: string): string { const n = turn.nudge; if (!n) return `[${sessionId}] nudge: unavailable`; const b = n.breakdown ?? {}; @@ -2845,6 +2845,14 @@ export function diagNudge(turn: { nudge?: { shouldInject: boolean; reason: strin const interval = b["nudgeGrowthTokens"] ?? 0; const pendingT1 = b["pendingT1"] ?? 0; const ref = b["growthReference"] ?? 0; + // #6720: the offered range list itself — "the model saw this list and didn't + // fold it all" was previously unverifiable (only pendingT1 was logged). Cap + // at 4 ranges so a long list doesn't bloat the line; the full list is in the + // nudge text the model receives. + const ranges = n.compressibleRanges ?? []; + const rangesTag = ranges.length > 0 + ? `, ranges=[${ranges.slice(0, 4).map((r) => `${r.startRef}–${r.endRef}(${r.tokens})`).join(", ")}${ranges.length > 4 ? `, +${ranges.length - 4} more` : ""}]` + : ""; // "INJECT" only when the nudge actually reaches the upstream payload. When // armed but suppressed by config/mode, say so explicitly so the log never // lies about delivery (#451, same class as #413). #3004: when the @@ -2859,7 +2867,7 @@ export function diagNudge(turn: { nudge?: { shouldInject: boolean; reason: strin // the observed top-level tools so the next omnibus rename is greppable here // (`surface=off(top-level=[...])`) instead of arriving as a user incident. const offTag = surfaceOff ? `, ${surfaceOff}` : ""; - return `[${sessionId}] nudge ${inject}: usage=${pct} (${tokenCount}/${limit}), growth=${growth}/${floor} (ref=${ref}, interval=${interval}), pendingT1=${pendingT1}/${interval}${modelTag}${offTag}, reason="${n.reason.slice(0, 120)}"`; + return `[${sessionId}] nudge ${inject}: usage=${pct} (${tokenCount}/${limit}), growth=${growth}/${floor} (ref=${ref}, interval=${interval}), pendingT1=${pendingT1}/${interval}${rangesTag}${modelTag}${offTag}, reason="${n.reason.slice(0, 120)}"`; } // Zero-baseline sessions are judged conservatively ONLY when they arrived diff --git a/src/server/budget.ts b/src/server/budget.ts index 06275265f2..37594de7c9 100644 --- a/src/server/budget.ts +++ b/src/server/budget.ts @@ -445,6 +445,12 @@ export function emergencyNudge(nudge: NudgeDecision | null | undefined, escalati // cycle) — this early-advertising arm must not re-arm against the same // bad reading. Hand-built decisions without the bit keep pre-fix semantics. if ((nudge.breakdown?.pressureDeferredNoSample ?? 0) === 1) return false; + // #6762 mirror: the kernel held the pressure band because the latest tool + // result FAILED — the model is about to act on a fresh failure, and a + // nudge would make it compress and lose the error context. This arm must + // not re-arm against the same round. Hand-built decisions without the bit + // keep pre-fix semantics. + if ((nudge.breakdown?.toolFailureHold ?? 0) === 1) return false; if (!nudge.compressibleRanges.some((r) => minCompressRange <= 0 || (r.chars ?? r.tokens * 4) >= minCompressRange)) return false; const b = nudge.breakdown; const minBenefit = b?.minPressureBenefit; diff --git a/src/stream.ts b/src/stream.ts index b074b1d456..e5023511a2 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -1780,10 +1780,18 @@ export function applyRanges(parsed: ReturnType, ctx: // #1495: apply-layer per-range errors (unknown refs, …) were invisible // whenever some other range in the batch succeeded — surface them on // the success line too, not only on total failure. + // #6720: a partial tier-1 fold must not read as a clean "done" — the + // kernel reports the unharvested remainder so the model sees there is + // more to fold instead of treating the partial fold as complete (the + // ratchet's model-side enabler). + const remainingNote = + r.remainingPending !== undefined + ? ` ${r.remainingPending} tokens of the offered ranges remain unfolded — include them in your next compress call.` + : ""; // #6855: headline echoes the EFFECTIVE (kernel-clamped) range, not the // requested one — the requested number stays in `warn` for correlation. const effectiveDetail = effectiveCompressedDetail(ranges, highestMappedRef(ctx.session.state), r.warnings); - let msg = `[Compressed ${effectiveDetail} → ${r.blocksCreated} block(s), ~${r.tokensCompressed} tokens saved.${warn}${applyErrorNote(r)}]`; + let msg = `[Compressed ${effectiveDetail} → ${r.blocksCreated} block(s), ~${r.tokensCompressed} tokens saved.${remainingNote}${warn}${applyErrorNote(r)}]`; // 2026-11-18 (owner): receipts are the only system lines that quote // user speech (Last-user anchor / summary content) yet carry NO // time tag on the wire — a weak model cannot tell when a fold happened diff --git a/tests/issue6720-partial-fold-receipt.test.ts b/tests/issue6720-partial-fold-receipt.test.ts new file mode 100644 index 0000000000..612ffc576c --- /dev/null +++ b/tests/issue6720-partial-fold-receipt.test.ts @@ -0,0 +1,128 @@ +import assert from "node:assert/strict"; +import { randomUUID } from "node:crypto"; +import { test } from "node:test"; +import type { Config, CoreMessage } from "acp-kernel"; +import { createCore, createInitialState, assignRefs, emptyRefMap, defaultConfig } from "acp-kernel"; +import type { Session } from "../src/session.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { applyRanges, type RewriteCtx } from "../src/stream.ts"; +import { parseCompressInput } from "../src/compress-tool.ts"; +import { diagNudge } from "../src/server.ts"; + +// #6720: a PARTIAL tier-1 fold must not read as a clean "done" — the receipt +// names the unharvested remainder so the model sees there is more to fold +// instead of treating the partial fold as complete (the ratchet's model-side +// enabler). And the nudge inject log line must list the OFFERED ranges so +// "the model saw this list and didn't fold it all" is verifiable from the log +// (previously only pendingT1 was logged, never the list itself). + +_setStoreForTest(new SessionStore({ enabled: false })); + +function textMsg(id: string, role: "user" | "assistant", text: string): CoreMessage { + return { id, role, contentType: "text", text }; +} + +function makeSession(): Session { + return { + id: `issue6720-${randomUUID()}`, + meta: {}, + stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 15000, compressCreditTokens: 0, contextTokens: 0, retrieveCalls: 0, retrieveHits: 0, retrieveMisses: 0, storedBytes: 0, storeBytesSaved: 0, rangeRestores: 0 }, + metadata: {}, + state: createInitialState(), + createdAt: Date.now(), + lastSeen: Date.now(), + blockContents: new Map(), + inFlight: 0, + persisted: false, + pendingRetrievals: [], + }; +} + +type Ctx = RewriteCtx & { session: Session; config: Config }; + +/** Ten ~5k-char messages with refs assigned; the session's nudge state carries + * a banked T1 fire (lastT1FirePending) so a small fold answers it as PARTIAL. */ +function makePartialCtx(firePending: number): { ctx: Ctx; firstRef: string; lastRef: string } { + const all = Array.from({ length: 10 }, (_, i) => + textMsg(`raw_${i + 1}`, i % 2 === 0 ? "user" : "assistant", `m${i} `.repeat(2000)), + ); + const session = makeSession(); + const res = assignRefs(all, { existing: emptyRefMap(), nextIndex: 0 }); + session.state.messageRefs = res.map; + session.state.nudge.lastT1FirePending = firePending; + const ctx: Ctx = { core: createCore(), config: defaultConfig(200000), messages: all, session, log: () => {} }; + return { ctx, firstRef: res.map.byRaw["raw_1"]!, lastRef: res.map.byRaw["raw_10"]! }; +} + +function args(startId: string, endId: string, summary = "s".repeat(200)) { + return parseCompressInput(JSON.parse(JSON.stringify({ content: [{ startId, endId, summary }] }))); +} + +test("#6720: a partial tier-1 fold receipt names the unharvested remainder", () => { + // Bank a T1 fire offering ~15k (the full pending); fold ONE ~6k-char + // message into a tiny summary — a partial harvest far below the 50% stall + // ratio, leaving the rest of the offer on the table. + const { ctx, firstRef } = makePartialCtx(15_000); + const out = applyRanges(args(firstRef, firstRef), ctx, { loopTracking: false }).text; + assert.ok(out.startsWith("[Compressed "), `expected a success receipt, got: ${out.slice(0, 160)}`); + assert.match( + out, + /tokens of the offered ranges remain unfolded/, + `the receipt must name the unharvested remainder: ${out}`, + ); + assert.match(out, /include them in your next compress call/, "the receipt must instruct the model to fold the rest"); +}); + +test("#6720: a compliant full fold receipt carries no remainder note", () => { + // Bank a T1 fire offering ~15k; fold the WHOLE range into a small summary + // — an eager harvest well above the 50% stall ratio, so no remainder is + // reported. + const { ctx, firstRef, lastRef } = makePartialCtx(15_000); + const out = applyRanges(args(firstRef, lastRef), ctx, { loopTracking: false }).text; + assert.ok(out.startsWith("[Compressed "), `expected a success receipt, got: ${out.slice(0, 160)}`); + assert.doesNotMatch(out, /tokens of the offered ranges remain unfolded/, "a full fold reads as done"); +}); + +test("#6720: the nudge inject log line lists the offered ranges", () => { + // The offered range list itself must reach the log — "the model saw this + // list and didn't fold it all" was previously unverifiable (only pendingT1 + // was logged). Capped at 4 ranges with a "+N more" tail. + const turn = { + nudge: { + shouldInject: true, + reason: "T1 effective 50000 >= 50000, growth 10000, usage 24%", + contextUsage: 0.24, + tier: 1, + breakdown: { growth: 10000, growthFloor: 50000, nudgeGrowthTokens: 50000, pendingT1: 50000, growthReference: 400000 }, + compressibleRanges: [ + { startRef: "m00001", endRef: "m00010", tokens: 12000 }, + { startRef: "m00011", endRef: "m00020", tokens: 9000 }, + { startRef: "m00021", endRef: "m00030", tokens: 7000 }, + { startRef: "m00031", endRef: "m00040", tokens: 6000 }, + { startRef: "m00041", endRef: "m00050", tokens: 5000 }, + { startRef: "m00051", endRef: "m00060", tokens: 4000 }, + ], + }, + }; + const line = diagNudge(turn, "sess-1", 100000, 1000000, "deepseek-v4.1-flash", true); + assert.match(line, /ranges=\[/, `the log line must carry the range list: ${line}`); + assert.match(line, /m00001–m00010\(12000\)/, "first range listed"); + assert.match(line, /m00031–m00040\(6000\)/, "fourth range listed (cap)"); + assert.match(line, /\+2 more/, "the tail beyond the cap is counted"); + assert.doesNotMatch(line, /m00041–m00050/, "ranges beyond the cap are not spelled out"); +}); + +test("#6720: the nudge log line omits the range list when none are offered", () => { + const turn = { + nudge: { + shouldInject: true, + reason: "T2 distill ready", + contextUsage: 0.5, + tier: 2, + breakdown: { pendingT1: 0 }, + compressibleRanges: [], + }, + }; + const line = diagNudge(turn, "sess-2", 50000, 100000, "deepseek-v4.1-flash", true); + assert.doesNotMatch(line, /ranges=\[/, "no range list when nothing is offered"); +}); diff --git a/tests/issue6762-tool-failure-nudge-hold.test.ts b/tests/issue6762-tool-failure-nudge-hold.test.ts new file mode 100644 index 0000000000..224285f2e7 --- /dev/null +++ b/tests/issue6762-tool-failure-nudge-hold.test.ts @@ -0,0 +1,58 @@ +import assert from "node:assert/strict"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +import type { NudgeDecision } from "acp-kernel"; +import { emergencyNudge } from "../src/server/budget.ts"; + +// #6762 host mirror: when decideNudge held the pressure band because the +// latest tool result FAILED (the model is about to act on a fresh failure, and +// a nudge would make it compress and lose the error context), the host +// early-advertising arm (the `||` next to nudge.shouldInject in every prepare +// lane) must NOT re-arm against the same round. Hand-built decisions without +// the breakdown bit keep pre-fix semantics. + +function hotDecision(over: Partial = {}): NudgeDecision { + return { + shouldInject: false, + reason: "", + compressibleRanges: [ + { startRef: "m00001", endRef: "m00010", count: 10, tokens: 6000, chars: 24000, toolPct: 0, textPct: 100 }, + ], + contextUsage: 0.99, // above EMERGENCY_NUDGE_ESCALATION_PCT (0.7) + tier: null, + breakdown: { + usage: 0.99, + growth: 0, + growthReference: 0, + effectiveThreshold: 0, + nudgeGrowthTokens: 0, + growthFloor: 0, + hasPendingNudge: 0, + overLimit: 1, + emergencyOverride: 1, + pendingT1: 6000, + pendingT2: 0, + pendingT3: 0, + maxPending: 6000, + minPressureBenefit: 5000, + ...over, + }, + }; +} + +test("#6762: emergencyNudge does not re-arm what the kernel held for a failed tool result", () => { + const d = hotDecision({ toolFailureHold: 1 }); + assert.equal(emergencyNudge(d), false); +}); + +test("#6762: emergencyNudge with an explicit toolFailureHold 0 behaves as pre-fix", () => { + const d = hotDecision({ toolFailureHold: 0 }); + assert.equal(emergencyNudge(d), true); +}); + +test("#6762: hand-built decision without the bit keeps pre-fix semantics", () => { + // No toolFailureHold key at all — the arm arms (pre-fix behavior). + assert.equal(emergencyNudge(hotDecision()), true); +});