From 3b5d6064ab341f219280473683ca93f576ff95ba Mon Sep 17 00:00:00 2001 From: ework-agent Date: Wed, 14 Oct 2026 13:41:01 +0800 Subject: [PATCH] fix(#3126): refuse empty-view compress up front instead of poisoning refs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When the proxy holds no live message view of a session (in-memory copy dropped between model requests by resident-pool eviction or restart) but the refs table is intact, applyRanges fed an empty message list into the kernel: every requested ref was judged dangling, marked DEAD in deadRefs (committed + persisted even on total failure), and receipts blamed content-changed / substrate-destruction while the breaker armed with a false cause. The refs were never actually changed — the next model request restores the view and clears the tombstones anyway. - new gate in applyRanges (+ external-summary lane): refuse with reason gate:no-visible-context and an honest receipt (refs intact, nothing recorded as dead, do-not-retry-until-next-model-request); writes no deadRefs; feeds the loop breaker the true cause - the gate YIELDS when any requested range overlaps an active compression block (#398/#403 refold works from state alone under a proxy-shaped empty view) and for fresh ref-less sessions (unchanged kernel diagnostics) - noteCompressLoopFailure gains forcedCause so the breaker records the honest cause without re-running failure-cause inference - resident-pool evictions are now logged (session id, weight, budget) so the empty-view window is observable instead of silent --- src/external-summary-compress.ts | 4 +- src/session.ts | 5 + src/stream.ts | 61 +++++++++- tests/issue3126-no-visible-context.test.ts | 131 +++++++++++++++++++++ 4 files changed, 195 insertions(+), 6 deletions(-) create mode 100644 tests/issue3126-no-visible-context.test.ts diff --git a/src/external-summary-compress.ts b/src/external-summary-compress.ts index 3f76683fe7..3214e90cf0 100644 --- a/src/external-summary-compress.ts +++ b/src/external-summary-compress.ts @@ -2,7 +2,7 @@ import { collectBlockContent } from "acp-kernel"; import { buildCompressSystemPrompt, parseCompressInput, type ParsedRange } from "./compress-tool.js"; import { configuredSummaryPlan } from "./external-summary-runtime.js"; import type { ResolvedKernelConfig } from "./compress-settings.js"; -import { applyRanges, normalizeRangeOrder, type RewriteCtx } from "./stream.js"; +import { applyRanges, noVisibleContextRefusal, normalizeRangeOrder, type RewriteCtx } from "./stream.js"; import { compressResult, type ProxyToolResult } from "./proxy-tool-result.js"; import { imagePlaceholders } from "./image-note.js"; import type { SummaryWork } from "./external-summary.js"; @@ -44,6 +44,8 @@ export async function applyConfiguredCompression(input: unknown, ctx: RewriteCtx const parsed = parseCompressInput(withOptionalSummaries(input), callId); if (parsed.ranges.length === 0) return applyRanges(parsed, ctx); normalizeRangeOrder(parsed.ranges); + const noView = noVisibleContextRefusal(ctx, parsed.ranges); + if (noView) return noView; const state = ctx.session.state; const revision = ctx.session.revisionEpoch; const messages = (ctx.compressMessages ?? ctx.messages).map((message) => { diff --git a/src/session.ts b/src/session.ts index 93d0906325..feb3cf13aa 100644 --- a/src/session.ts +++ b/src/session.ts @@ -1445,6 +1445,11 @@ function evictOldest(): boolean { return false; } sessions.delete(oldestId); + // #3126: eviction is the trigger for the empty-view compress window (the + // session's message bytes live only here; state reloads from disk but the + // view self-heals only on the next model request). Log it so operators can + // correlate ctx=0 receipts with the exact moment a session left memory. + loggerLog("info", `[session] evicted ${oldestId} from resident pool (weight ${sessionWeight(s)} bytes, ${sessions.size} session(s) remain, budget ${MAX_RESIDENT_BYTES} bytes)`); return true; } diff --git a/src/stream.ts b/src/stream.ts index 4fffad0704..8fd3253d79 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -1,5 +1,5 @@ import { createHash, randomBytes } from "node:crypto"; -import { collectBlockContent, countMessageTokens, defaultCountTokens, formatRanges, storeCoveredOriginals, viableRanges, type CompressionCore, type Config, type CoreMessage, type CompressionState, type NudgeDecision, type CompressParseDiagnostics } from "acp-kernel"; +import { activeBlockSpans, collectBlockContent, countMessageTokens, defaultCountTokens, formatRanges, storeCoveredOriginals, viableRanges, type CompressionCore, type Config, type CoreMessage, type CompressionState, type NudgeDecision, type CompressParseDiagnostics } from "acp-kernel"; import { handleAcpStatus, liveCompressionRanges } from "./acp-status.js"; import { handleAcpCache, recordCacheFoldsFromBlocks } from "./cache-ledger.js"; import { type Session, cacheBlockContent, markDirty, scrubTurnSeparatorIds, statusInputBaseline } from "./session.js"; @@ -250,7 +250,7 @@ function substrateDestructionPauseParagraph(n: number): string { * (clearCompressLoopStreak). The pause paragraph branches on the failure * cause (#2432): substrate-destruction gets the single-recovery-step * wording, everything else keeps the verbatim loop-noise paragraph. */ -function noteCompressLoopFailure(ctx: RewriteCtx, specLabel: string, errs?: string): string { +function noteCompressLoopFailure(ctx: RewriteCtx, specLabel: string, errs?: string, forcedCause?: string): string { const now = Date.now(); let streak = readCompressLoopStreak(ctx.session); if (streak && now - streak.lastAt > COMPRESS_LOOP_DECAY_MS) streak = undefined; @@ -259,8 +259,10 @@ function noteCompressLoopFailure(ctx: RewriteCtx, specLabel: string, errs?: stri // receipt (acp_status) can react to a substrate/stale-ref verdict at n=1 // instead of waiting for the third failure to arm the breaker. A cause // only ever moves forward: an unknown-cause failure keeps the last known - // verdict rather than erasing it. - const freshCause = errs !== undefined ? compressFailureCause(errs, ctx.session) : ""; + // verdict rather than erasing it. #3126: host-side gates that know their + // own verdict before the kernel runs pass it as forcedCause — deriving it + // from kernel error text would misattribute a gate refusal to content. + const freshCause = forcedCause ?? (errs !== undefined ? compressFailureCause(errs, ctx.session) : ""); const cause = freshCause !== "" ? freshCause : streak?.cause; writeCompressLoopStreak(ctx.session, { n, lastAt: now, ...(cause !== undefined ? { cause } : {}) }); if (n < COMPRESS_LOOP_THRESHOLD) return ""; @@ -270,7 +272,7 @@ function noteCompressLoopFailure(ctx: RewriteCtx, specLabel: string, errs?: stri } else { ctx.log(`[warn: compress-loop] failure ${n} in armed streak (last spec: ${label})`); } - if (errs !== undefined && compressFailureCause(errs, ctx.session).startsWith("substrate-destruction")) { + if (freshCause.startsWith("substrate-destruction")) { return substrateDestructionPauseParagraph(n); } return loopNoisePauseParagraph(n); @@ -574,6 +576,53 @@ function applyErrorNote(r: { errors: string[] }): string { // hosts that want to penalize this shape grep the warn marker below. const DEGENERATE_FOLD_COVERAGE = 0.8; +/** #3126: refuse up front when the proxy holds NO message view of this + * conversation while its ref table says refs exist. The plugin tool endpoint + * serves compress from a volatile in-memory view (session.transient.pluginMessages, + * with the fork-only pluginSnapshot as fallback) that is legitimately empty + * between model requests once the session object left the resident pool + * (512 MiB byte budget, evictOldest) or the process restarted — state (every + * ref mapping) survives on disk, the message bytes do not (transient is never + * persisted; pluginSnapshot persists only for fork sessions, #2077). Feeding + * an empty view to the kernel dangles EVERY requested ref, records them all + * DEAD (the host commits applyCompression's cloned state even when zero blocks + * were created), and the receipt blames "content-changed" — none of which is + * true: the refs are intact and the view self-heals on the next model request + * (rememberPluginMessages after forward). Null = no refusal, proceed. */ +export function noVisibleContextRefusal( + ctx: RewriteCtx, + ranges: ParsedRange[], + opts?: { loopTracking?: boolean }, +): ProxyToolResult | null { + const view = ctx.compressMessages ?? ctx.messages; + const mappedRefs = Object.keys(ctx.session.state.messageRefs?.byRef ?? {}).length; + if (view.length > 0 || mappedRefs === 0) return null; + // #398/#403 in-place refold works FROM state alone: after an inline + // decompress the originals leave the wire view (proxy mode) yet a + // re-compress of the restored span must still land on its block. The + // refusal therefore applies only when NO active block touches ANY + // requested ref — then nothing in the call can be served without live + // messages and every retry fails identically. Any overlap hands the + // decision back to the kernel, which keeps its own honest diagnostics + // for those shapes (refold / already-compressed / no-net-shrink). + const spans = activeBlockSpans(ctx.session.state); + const touchable = ranges.some((r) => { + const lo = refNum(r.startRef); + const hi = refNum(r.endRef); + if (lo === 0 || hi === 0) return true; + const a = Math.min(lo, hi), b = Math.max(lo, hi); + return spans.some((s) => refNum(s.startRef) <= b && refNum(s.endRef) >= a); + }); + if (touchable) return null; + const detail = ranges.map((r) => `${r.startRef}–${r.endRef}`).join(", "); + ctx.log(`[acp-proxy: compress REFUSED ${detail} — no visible context (view=0 message(s), ${mappedRefs} ref(s) mapped); refs left untouched and nothing recorded as dead — the view self-heals on the next model request (#3126)]`); + const specKey = normalizedSpecKey(ranges); + const repeatGuard = recordCompressFailure(ctx.session, specKey, "This failure is the proxy's missing in-memory message view, not your refs — it clears on the next model request. Wait for that turn instead of re-issuing."); + const loopGuard = opts?.loopTracking === false ? "" : noteCompressLoopFailure(ctx, specKey, undefined, "no-visible-context"); + const receipt = `[Compression FAILED: no visible context — the proxy holds no live message view of this conversation right now (its in-memory copy was dropped between model requests, e.g. by memory eviction or a restart). Your refs are intact and NOTHING was marked dead: ${mappedRefs} ref(s) remain mapped, and this is NOT a ref problem. Do NOT retry now — every retry fails identically until the next model request re-sends the conversation and restores the view. Continue your task; after the next turn you can compress the same ranges again. [cause: no-visible-context]${repeatGuard}${loopGuard}]`; + return compressResult(receipt, "refused", 0, "gate:no-visible-context"); +} + export function applyRanges(parsed: ReturnType, ctx: RewriteCtx, opts?: { loopTracking?: boolean }): ProxyToolResult { // #2146: preflight overflow-compress is bili's OWN internal lane — its // failures have separate reporting (noteSkip) and must not arm the @@ -647,6 +696,8 @@ export function applyRanges(parsed: ReturnType, ctx: const ids = ctx.messages.slice(0, 10).map((m) => `${saltedMsgIdForLog(m.id)}(${(m.text ?? "").length}c)`).join(", "); ctx.log(`[acp-proxy: first msg ids: ${ids}]`); } + const noView = noVisibleContextRefusal(ctx, ranges, { loopTracking: trackLoop }); + if (noView) return noView; try { const res = ctx.core.applyCompression({ ranges, diff --git a/tests/issue3126-no-visible-context.test.ts b/tests/issue3126-no-visible-context.test.ts new file mode 100644 index 0000000000..eb971bab82 --- /dev/null +++ b/tests/issue3126-no-visible-context.test.ts @@ -0,0 +1,131 @@ +// #3126: compress served from an EMPTY message view while refs are still +// mapped (session evicted from the resident pool / restarted between model +// requests) used to feed the kernel zero messages, dangle EVERY requested ref, +// record them all DEAD, and blame "content-changed". Now the host refuses up +// front with the honest cause and writes nothing. These tests pin: the +// refusal + its machine-readable reason, that NO deadRefs are recorded, that a +// fresh (ref-less) session still falls through to the kernel path unchanged, +// that a normal fold still applies, that the breaker arms with the honest +// cause, and that the external-summary lane hits the same gate pre-network. +import { test } from "node:test"; +import assert from "node:assert/strict"; +import type { CoreMessage } from "acp-kernel"; +import { createCore, createInitialState, defaultConfig, assignRefs, emptyRefMap } from "acp-kernel"; +import { parseCompressInput } from "../src/compress-tool.ts"; +import { applyRanges, type RewriteCtx } from "../src/stream.ts"; +import { applyConfiguredCompression } from "../src/external-summary-compress.ts"; + +type Ctx = Omit & { log: (m: string) => void; logs: string[] }; + +const MESSAGES = Array.from({ length: 80 }, (_, i) => ({ + id: `raw${i}`, + role: (i % 2 ? "assistant" : "user") as "assistant" | "user", + text: "lorem ipsum dolor sit amet conseq ".repeat(8) + i, +})) as unknown as CoreMessage[]; + +function makeCtx(opts?: { messages?: CoreMessage[]; refs?: boolean }): Ctx { + const logs: string[] = []; + const ctx: Ctx = { + core: createCore(), + // Explicit protect-zone override (same recipe as tests/compress-span-hint.test.ts): + // the bare default would fence m00010..m00040 under "most recent user + // message" protection and mask the gate behavior under test. + config: defaultConfig(200000, { preserveRecentMessages: 5, preserveRecentTokens: 0 }), + messages: opts?.messages ?? [], + session: { + id: "issue3126-test", + meta: {}, + stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 0, 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: [], + }, + log: (m: string) => { logs.push(m); }, + logs, + }; + if (opts?.refs) { + // Refs m00001..m00080 mapped WITHOUT their messages present in the view — + // the evicted/restarted shape: state survived, the message bytes did not. + ctx.session.state.messageRefs = assignRefs(MESSAGES, { existing: emptyRefMap(), nextIndex: 0 }).map; + } + return ctx; +} + +const SPEC = () => parseCompressInput({ content: [{ startId: "m00010", endId: "m00040", summary: "y".repeat(100) }] }); + +test("#3126: empty view + mapped refs refuses up front, records nothing", () => { + const ctx = makeCtx({ refs: true }); + const res = applyRanges(SPEC(), ctx); + assert.equal(res.outcome, "refused"); + assert.equal(res.reason, "gate:no-visible-context"); + assert.equal(res.blocksCreated, 0); + assert.ok(res.text.includes("no visible context"), `receipt names the cause: ${res.text}`); + assert.ok(res.text.includes("[cause: no-visible-context]"), `machine cause present: ${res.text}`); + assert.ok(res.text.includes("Do NOT retry now"), `retry veto present: ${res.text}`); + // The core of the fix: NOTHING was marked dead and no block was created. + assert.equal(ctx.session.state.deadRefs, undefined, "no deadRefs recorded"); + assert.equal(ctx.session.state.blocks.length, 0, "no blocks created"); + // Operator log carries the correlation marker. + assert.ok(ctx.logs.some((l) => l.includes("no visible context")), `operator log present: ${JSON.stringify(ctx.logs)}`); +}); + +test("#3126: a fresh (ref-less) session still falls through to the kernel path", () => { + const ctx = makeCtx(); // no refs, empty view + const res = applyRanges(SPEC(), ctx); + assert.equal(res.outcome, "refused"); + assert.ok(!res.text.includes("[cause: no-visible-context]"), `must NOT hit the new gate: ${res.text}`); + assert.ok(!res.text.includes("no visible context"), `new-gate wording absent: ${res.text}`); +}); + +test("#3126: a normal fold (view present) still applies past the gate", () => { + const ctx = makeCtx({ messages: MESSAGES, refs: true }); + const res = applyRanges(SPEC(), ctx); + assert.equal(res.outcome, "applied", `expected applied, got: ${res.text}`); + assert.ok((res.blocksCreated ?? 0) >= 1, `a block was created: ${res.text}`); + assert.equal(ctx.session.state.deadRefs, undefined, "no deadRefs on a clean fold"); +}); + +test("#3126: three consecutive refusals arm the breaker with the honest cause", () => { + const ctx = makeCtx({ refs: true }); + const r1 = applyRanges(SPEC(), ctx); + const r2 = applyRanges(SPEC(), ctx); + const r3 = applyRanges(SPEC(), ctx); + assert.ok(!r1.text.includes("COMPRESS CIRCUIT BREAKER"), "not armed at n=1"); + assert.ok(r2.text.includes("Repeat-failure guard"), `repeat guard at n=2: ${r2.text}`); + assert.ok(r3.text.includes("COMPRESS CIRCUIT BREAKER"), `armed at n=3: ${r3.text}`); + const streak = ctx.session.metadata["compressFailStreak"] as { n?: number; cause?: string }; + assert.equal(streak.n, 3); + assert.equal(streak.cause, "no-visible-context", "honest cause persisted on the streak"); +}); + +test("#3126: a block-touching request passes the gate to the kernel (refold path intact)", () => { + const ctx = makeCtx({ messages: MESSAGES, refs: true }); + const first = applyRanges(parseCompressInput({ content: [{ startId: "m00001", endId: "m00020", summary: "z".repeat(60) }] }), ctx); + assert.equal(first.outcome, "applied", `setup fold lands: ${first.text}`); + // Proxy-shaped next round: the originals left the view; SPEC (m00010–m00040) + // partially overlaps the fresh block m00001–m00020, so the gate must yield + // and let the kernel apply its own per-range diagnostics. + ctx.messages = []; + ctx.compressMessages = []; + const res = applyRanges(SPEC(), ctx); + assert.notEqual(res.reason, "gate:no-visible-context", `block-touching request must reach the kernel: ${res.text}`); +}); + +test("#3126: the external-summary lane hits the same gate before any network call", async () => { + const ctx = makeCtx({ refs: true }); + // Expanded-shape target (what rides the request Config rail after + // expandExternalSummaryChain): plan construction must SUCCEED here — the + // assertion under test is that the shared gate refuses before any of these + // fully-usable candidates is ever dispatched. + ctx.config = { ...defaultConfig(200000), externalSummary: { enabled: true, targets: [{ name: "sum-sm", protocol: "openai", url: "https://summary.example.test/v1/chat/completions", model: "sm", apiKey: "sk-test-3126", contextWindow: 128000, outputTokens: 8192, stream: false }] } } as typeof ctx.config; + const res = await applyConfiguredCompression({ content: [{ startId: "m00010", endId: "m00040", summary: "hint" }] }, ctx); + assert.equal(res.outcome, "refused"); + assert.equal(res.reason, "gate:no-visible-context"); + assert.ok(res.text.includes("no visible context"), `external lane uses the shared gate: ${res.text}`); + assert.equal(ctx.session.state.deadRefs, undefined, "no deadRefs recorded"); +});