From ae1f0f0ceecb07882df8e8931c96efb16d1204d7 Mon Sep 17 00:00:00 2001 From: awork Date: Thu, 22 Oct 2026 21:30:55 +0800 Subject: [PATCH 1/4] fix(#4305): standing generation-boundary note for post-switch stale-ref residue MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After a mid-conversation model/upstream switch the ref ledger restarts at m00001 while the client-persisted history keeps citing previous-generation refs, which look exactly like live ones — the model keeps aiming compress at dead ranges until the circuit breaker fires. The reactive receipts (#1029, #2146, #4252) already name the frontier but the incident shows the model flails through them, so the fix is a STANDING part of the context. One-shot evidence-based detection (>=2 distinct above-frontier citations in visible history), verdict persisted in session metadata, static number-free note appended to the ACP standing system block on every wire plus the round-2 fold rebuild in exact steady-path order. Display-only: no storage, ref-space, gate or breaker changes. --- reference/architecture.md | 1 + src/generation-boundary.ts | 126 ++++++++++++++++++++++++++++ src/server/inject.ts | 10 ++- src/server/prepare-anthropic.ts | 3 +- src/server/prepare-google.ts | 9 +- src/server/prepare-openai.ts | 9 +- src/server/prepare-responses.ts | 7 +- src/server/relay.ts | 8 +- tests/generation-boundary.test.ts | 134 ++++++++++++++++++++++++++++++ 9 files changed, 298 insertions(+), 9 deletions(-) create mode 100644 src/generation-boundary.ts create mode 100644 tests/generation-boundary.test.ts diff --git a/reference/architecture.md b/reference/architecture.md index 1275d438c9..593778a8db 100644 --- a/reference/architecture.md +++ b/reference/architecture.md @@ -72,6 +72,7 @@ billion-context/ │ ├── compress-tool.ts # ACP tool surface — thin re-export from acp-kernel │ ├── decompress-shared.ts # Shared decompress logic + large-decompress tmp retention cap │ ├── fold-reconcile.ts # Fold-anchor reconciliation for stable prefix cache +│ ├── generation-boundary.ts # Standing generation-boundary note for post-switch stale-ref residue (#4305) │ ├── orphan-gc.ts # Orphan block garbage collection │ ├── fork-adoption.ts # Fork block adoption on mid-history divergence (#629) │ ├── prefix-affinity.ts # Anonymous prefix-affinity session resolution (#309) diff --git a/src/generation-boundary.ts b/src/generation-boundary.ts new file mode 100644 index 0000000000..32f5f0213c --- /dev/null +++ b/src/generation-boundary.ts @@ -0,0 +1,126 @@ +// #4305: standing generation-boundary note. When the client switches model or +// upstream mid-conversation, the conversation's ref ledger restarts (a fresh +// session generation; refs re-keyed from m00001) while the client's persisted +// history — old compress receipts, acp_status outputs, block summaries, the +// model's own earlier replies — keeps citing refs from the previous generation. +// Those citations are permanently inert above this session's frontier yet look +// exactly like live refs, so the model keeps aiming compress at dead ranges +// until the circuit breaker fires (#4305 incident: five gates in a row on one +// request burst). The reactive receipts already name the frontier +// (beyondFrontierNote #2146, currentRefsSnapshot #1029), but the incident shows +// the model flails through them — so the fix has to be a STANDING part of the +// context, present from the first request of the new generation. +// +// Detection is one-shot and evidence-based: scan the visible core history for +// message-ref citations STRICTLY ABOVE the session's highest mapped ref — the +// provably-dead class (this session allocates no future refs, so an +// above-frontier citation can only come from another generation). Require ≥2 +// DISTINCT such refs: one stray m-shaped token in prose is not evidence. Once +// detected the verdict is recorded in persisted session metadata and the note +// renders for the session's lifetime — static text, byte-stable across turns, +// prefix-cache safe after the (typically never-visible) one-time append. +// +// Deliberately display-only: no storage rewrite, no ref-space surgery, no gate +// changes. The text carries no numbers: the frontier advances as the session +// allocates, and a fixed ceiling would go stale within turns. Concrete ceilings +// stay with the reactive notes (beyondFrontierNote / frontierClampNote), which +// fire on the actual failure and always quote the live value. + +import { markDirty, type Session } from "./session.js"; +import type { CoreMessage } from "acp-kernel"; + +/** Session-metadata key for the one-shot detection record. */ +const STORAGE_KEY = "generationBoundary"; + +/** Distinct above-frontier citations required before declaring a boundary: + * one stray m-shaped token in prose is not evidence, two distinct ones are. */ +const MIN_DISTINCT_STALE_REFS = 2; + +/** Message-ref citation in visible text — bare (`refs m05448–m05465`) or inside + * a surviving ACP tag (tag delimiters are non-word chars, so the inner id + * still matches). 4+ digits mirrors the kernel's m\d{4,} convention. */ +const REF_CITATION_RE = /\bm(\d{4,})\b/g; + +/** byRef key shape guard (mirrors stream.ts M_REF_NUM_RE usage). */ +const BYREF_KEY_RE = /^m(\d+)$/; + +export interface GenerationBoundaryRecord { + /** First-detection time (ms epoch). */ + at: number; + /** This session's highest mapped ref at detection ("mNNNNN" or "" when none) — evidence anchor, log surface only. */ + frontier: string; + /** Highest stale citation seen at detection — log surface only. */ + maxStaleRef: string; +} + +export function readGenerationBoundary(session: Session): GenerationBoundaryRecord | undefined { + const raw = session.metadata?.[STORAGE_KEY]; + if (!raw || typeof raw !== "object") return undefined; + const r = raw as Record; + if (typeof r.at !== "number" || typeof r.frontier !== "string" || typeof r.maxStaleRef !== "string") return undefined; + return { at: r.at, frontier: r.frontier, maxStaleRef: r.maxStaleRef }; +} + +/** Highest allocated mNNNNN number in the session's ref map (0 when empty). + * Mirrors stream.ts' private highestMappedRef — kept local as a tiny pure + * helper rather than widening stream.ts' export surface for one number. */ +function highestMappedIndex(state: Pick): number { + let highest = 0; + for (const ref of Object.keys(state.messageRefs?.byRef ?? {})) { + const m = BYREF_KEY_RE.exec(ref); + if (m) highest = Math.max(highest, Number(m[1])); + } + return highest; +} + +const indexToRef = (i: number): string => `m${String(i).padStart(5, "0")}`; + +/** + * One-shot generation-boundary detection (#4305). Scans the visible core + * history for message-ref citations strictly above this session's highest + * mapped ref and records the verdict in persisted session metadata when ≥2 + * distinct such citations exist. Returns true when the standing note should + * ride THIS request (already detected, or freshly detected now). + */ +export function detectGenerationBoundary( + session: Session, + messages: readonly CoreMessage[], + log?: (level: string, msg: string) => void, +): boolean { + if (readGenerationBoundary(session)) return true; + if (messages.length === 0) return false; + const frontier = highestMappedIndex(session.state); + const stale = new Set(); + let found = false; + for (const m of messages) { + const text = m.text; + if (!text) continue; + REF_CITATION_RE.lastIndex = 0; + let match: RegExpExecArray | null; + while ((match = REF_CITATION_RE.exec(text)) !== null) { + const n = Number(match[1]); + if (n > frontier) { + stale.add(n); + if (stale.size >= MIN_DISTINCT_STALE_REFS) { found = true; break; } + } + } + if (found) break; + } + if (!found) return false; + let maxStale = 0; + for (const n of stale) maxStale = Math.max(maxStale, n); + const record: GenerationBoundaryRecord = { + at: Date.now(), + frontier: frontier > 0 ? indexToRef(frontier) : "", + maxStaleRef: indexToRef(maxStale), + }; + (session.metadata ?? (session.metadata = {}))[STORAGE_KEY] = record; + markDirty(session); + log?.("info", `[${session.id}] [acp-gen-boundary] ${stale.size} distinct ref citation(s) above this session's frontier (${record.frontier || "no mapped refs yet"}) found in visible history — previous-generation residue confirmed; standing generation-boundary note active (highest stale ${record.maxStaleRef}) (#4305)`); + return true; +} + +/** Standing model-visible note, appended to the ACP standing system block once + * detected. Static by design (see module header) — it must never embed moving + * values or the prefix cache breaks every allocation. */ +export const GENERATION_BOUNDARY_NOTE = "Generation boundary: this conversation's message-ref ledger was reset mid-conversation (a model or upstream switch restarted refs at m00001). Refs quoted earlier in your visible history — tool results, summaries, your own earlier replies — may belong to the previous generation: some are permanently inert (compress/decompress targeting them always fails), and any number that happens to exist in the current ledger now points at a DIFFERENT message than before. Before calling compress, verify every ref you plan to target against the current acp_status report; do not trust refs quoted in older tool results or summaries."; diff --git a/src/server/inject.ts b/src/server/inject.ts index 6c342f7102..6099db2144 100644 --- a/src/server/inject.ts +++ b/src/server/inject.ts @@ -7,6 +7,7 @@ import { absorbEnabled, absorbToolName } from "../absorb.js"; import { BILI_ACP_TOOLS_ANTHROPIC, BILI_ACP_TOOLS_ANTHROPIC_NO_RANGE, BILI_ACP_TOOLS_GOOGLE, BILI_ACP_TOOLS_GOOGLE_NO_RANGE, BILI_ACP_TOOLS_OPENAI, BILI_ACP_TOOLS_OPENAI_NO_RANGE, BILI_ACP_TOOLS_RESPONSES, buildAbsorbSystemPrompt, buildAcpTagsOnlyPrompt, buildCompressSystemPrompt, packOwnsDescription, ptcNoteMode, withAcpNotes, withAcpNotesOpenAI, withMarkerIntegrityNote, withSummaryBudgetNote, withProjectAttribution } from "../compress-tool.js"; import { externalSummaryEnabled, withExternalSummaryTools } from "../external-summary-surface.js"; import { forceTextProtocol as knobForceTextProtocol, renderNone as knobRenderNone } from "../knobs.js"; +import { detectGenerationBoundary, GENERATION_BOUNDARY_NOTE } from "../generation-boundary.js"; export function injectSystem( parsed: AnthropicRequestBody, @@ -16,18 +17,23 @@ export function injectSystem( surface?: PackSurface, visibilityMarkers = true, suppressCompressPrompt = false, + generationBoundaryActive = false, ): string | AnthropicRequestBody["system"] { // ONLY the static compress prompt goes into the system block — it is the // prefix-cache anchor and must stay byte-stable across turns. The nudge // (which changes every turn) is appended as a trailing user message by // the caller (prepareAnthropic), never merged into system. const parts: string[] = []; - if (opts.compress.injectTool && !suppressCompressPrompt) parts.push(withProjectAttribution(withMarkerIntegrityNote(withSummaryBudgetNote(buildCompressSystemPrompt(prompts, surface?.promptSections), externalSummaryEnabled(config)), visibilityMarkers))); + let acpStandingPushed = false; + if (opts.compress.injectTool && !suppressCompressPrompt) { parts.push(withProjectAttribution(withMarkerIntegrityNote(withSummaryBudgetNote(buildCompressSystemPrompt(prompts, surface?.promptSections), externalSummaryEnabled(config)), visibilityMarkers))); acpStandingPushed = true; } else if (!knobRenderNone()) { // #1881: the NEVER-echo prohibition follows the rendered tags, not the tool switch. const tagsOnly = buildAcpTagsOnlyPrompt("function", prompts, surface?.promptSections); - if (tagsOnly) parts.push(tagsOnly); + if (tagsOnly) { parts.push(tagsOnly); acpStandingPushed = true; } } + // #4305: standing generation-boundary note — rides the ACP standing block, + // byte-stable once detected; never on a view that carries no ACP surface. + if (generationBoundaryActive && acpStandingPushed) parts.push(GENERATION_BOUNDARY_NOTE); if (opts.compress.injectTool && absorbEnabled(config)) parts.push(buildAbsorbSystemPrompt(absorbToolName(config))); if (parts.length === 0) return parsed.system; // #1876: APPEND the prompt as a trailing block (client blocks byte-exact) diff --git a/src/server/prepare-anthropic.ts b/src/server/prepare-anthropic.ts index aafb3fb290..2a71525275 100644 --- a/src/server/prepare-anthropic.ts +++ b/src/server/prepare-anthropic.ts @@ -22,6 +22,7 @@ import { adoptContentStore, ccrEnabled, ccrLoopConfig, contentStoreOf, dropRetri import { rulesEnabled, storeEffectiveRules } from "../rules-feature.js"; import { autoFoldEngaged, externalSummaryEnabled, growthFoldingArmed } from "../external-summary-surface.js"; import { filterDeadRanges } from "../dead-ranges.js"; +import { detectGenerationBoundary } from "../generation-boundary.js"; import { attachSubagentSessions } from "../subagent-sessions.js"; import { estimateCoreMessages, estimateCoreMessagesUpper, extractBillingAttributionBlock } from "../preflight.js"; import { dshNativeCompactionWitness, recordConflict } from "../conflict-watch.js"; @@ -418,7 +419,7 @@ export async function prepareAnthropic( rebuiltMessages = [...rebuiltMessages, ...sysNotes.map((text) => ({ role: "user" as const, content: text }))]; } - systemOut = injectSystem(parsed, opts, prompts, loopConfig, surface, visibilityMarkers, laneDecisions.compressSurfaceOff); + systemOut = injectSystem(parsed, opts, prompts, loopConfig, surface, visibilityMarkers, laneDecisions.compressSurfaceOff, detectGenerationBoundary(session, processedMessages, log)); // #1637: stamp the steady body's system. NOTE: parsed.system stays the // #1085 frozen CLIENT head (injectSystem's in-place view) — the round-2 // adapter consumes that frozen head and stamps its own rebuild, so NO diff --git a/src/server/prepare-google.ts b/src/server/prepare-google.ts index 0cd9ed03c9..22b7012002 100644 --- a/src/server/prepare-google.ts +++ b/src/server/prepare-google.ts @@ -21,6 +21,7 @@ import { adoptContentStore, ccrEnabled, ccrLoopConfig, contentStoreOf, imageResc import { rulesEnabled, storeEffectiveRules } from "../rules-feature.js"; import { autoFoldEngaged, externalSummaryEnabled, growthFoldingArmed } from "../external-summary-surface.js"; import { filterDeadRanges } from "../dead-ranges.js"; +import { detectGenerationBoundary, GENERATION_BOUNDARY_NOTE } from "../generation-boundary.js"; import { attachSubagentSessions } from "../subagent-sessions.js"; import { reconcileSystemAnchor } from "../system-anchor.js"; import { logRawStripCounts, markerArtifactCount, stripRawStage } from "../marker/engine.js"; @@ -272,12 +273,16 @@ export async function prepareGoogle( // constants, so the system anchor stays identical across normal turns // and round-2 re-requests — skipping them here would fork the prefix // at every fold and collapse the upstream cache hit. - if (laneDecisions.compressSurfaceOn) sysParts.push(withProjectAttribution(withMarkerIntegrityNote(withSummaryBudgetNote(buildCompressSystemPrompt(prompts, surface?.promptSections), externalSummaryEnabled(config)), visibilityMarkers))); + let acpStandingPushed = false; + if (laneDecisions.compressSurfaceOn) { sysParts.push(withProjectAttribution(withMarkerIntegrityNote(withSummaryBudgetNote(buildCompressSystemPrompt(prompts, surface?.promptSections), externalSummaryEnabled(config)), visibilityMarkers))); acpStandingPushed = true; } else if (!isTitleGen && renderStrategy !== "none") { // #1881: the NEVER-echo prohibition follows the rendered tags, not the tool switch. const tagsOnly = buildAcpTagsOnlyPrompt("function", prompts, surface?.promptSections); - if (tagsOnly) sysParts.push(tagsOnly); + if (tagsOnly) { sysParts.push(tagsOnly); acpStandingPushed = true; } } + // #4305: standing generation-boundary note — rides the ACP standing block, + // byte-stable once detected; never on a view that carries no ACP surface. + if (acpStandingPushed && detectGenerationBoundary(session, processedMessages, log)) sysParts.push(GENERATION_BOUNDARY_NOTE); if (absorbActive) sysParts.push(buildAbsorbSystemPrompt(absorbToolName(loopConfig))); googleOutboundSystem = sysParts.join("\n\n"); // Untouched when nothing was added beyond the client's own text: the diff --git a/src/server/prepare-openai.ts b/src/server/prepare-openai.ts index 9361302dc4..b3a8316096 100644 --- a/src/server/prepare-openai.ts +++ b/src/server/prepare-openai.ts @@ -22,6 +22,7 @@ import { adoptContentStore, ccrEnabled, ccrLoopConfig, contentStoreOf, dropRetri import { rulesEnabled, storeEffectiveRules } from "../rules-feature.js"; import { autoFoldEngaged, externalSummaryEnabled, growthFoldingArmed } from "../external-summary-surface.js"; import { filterDeadRanges } from "../dead-ranges.js"; +import { detectGenerationBoundary, GENERATION_BOUNDARY_NOTE } from "../generation-boundary.js"; import { attachSubagentSessions } from "../subagent-sessions.js"; import { estimateCoreMessages, estimateCoreMessagesUpper } from "../preflight.js"; import { dshNativeCompactionWitness, recordConflict } from "../conflict-watch.js"; @@ -311,12 +312,16 @@ export async function prepareOpenai( // would invalidate the cache every turn. const sysParts: string[] = []; if (openaiSystemText) sysParts.push(openaiSystemText); - if (laneDecisions.compressSurfaceOn) sysParts.push(withProjectAttribution(withMarkerIntegrityNote(withSummaryBudgetNote(buildCompressSystemPrompt(prompts, surface?.promptSections), externalSummaryEnabled(config)), visibilityMarkers))); + let acpStandingPushed = false; + if (laneDecisions.compressSurfaceOn) { sysParts.push(withProjectAttribution(withMarkerIntegrityNote(withSummaryBudgetNote(buildCompressSystemPrompt(prompts, surface?.promptSections), externalSummaryEnabled(config)), visibilityMarkers))); acpStandingPushed = true; } else if (!isTitleGen && !knobRenderNone()) { // #1881: the NEVER-echo prohibition follows the rendered tags, not the tool switch. const tagsOnly = buildAcpTagsOnlyPrompt("function", prompts, surface?.promptSections); - if (tagsOnly) sysParts.push(tagsOnly); + if (tagsOnly) { sysParts.push(tagsOnly); acpStandingPushed = true; } } + // #4305: standing generation-boundary note — rides the ACP standing block, + // byte-stable once detected; never on a view that carries no ACP surface. + if (acpStandingPushed && detectGenerationBoundary(session, processedMessages, log)) sysParts.push(GENERATION_BOUNDARY_NOTE); if (absorbActive) sysParts.push(buildAbsorbSystemPrompt(absorbToolName(loopConfig))); rebuiltMessages = injectOpenaiSystem(rebuiltMessages, sysParts); if (sysNotes.length > 0) { diff --git a/src/server/prepare-responses.ts b/src/server/prepare-responses.ts index 67663b46c4..61a405b155 100644 --- a/src/server/prepare-responses.ts +++ b/src/server/prepare-responses.ts @@ -25,6 +25,7 @@ import { adoptContentStore, ccrEnabled, ccrLoopConfig, contentStoreOf, imageResc import { rulesEnabled, storeEffectiveRules } from "../rules-feature.js"; import { autoFoldEngaged, externalSummaryEnabled, growthFoldingArmed } from "../external-summary-surface.js"; import { filterDeadRanges } from "../dead-ranges.js"; +import { detectGenerationBoundary, GENERATION_BOUNDARY_NOTE } from "../generation-boundary.js"; import { attachSubagentSessions } from "../subagent-sessions.js"; import { estimateCoreMessages, estimateCoreMessagesUpper } from "../preflight.js"; import { hoistTrappedToolItems } from "../tool-pair-order.js"; @@ -392,9 +393,13 @@ export async function prepareResponses( const tagsOnlyPrompt = !compressSurfaceOn && !isCompactionTrigger && !knobNoCompressPrompt() && !knobRenderNone() ? buildAcpTagsOnlyPrompt(responsesTextProtocol ? "hybrid" : "function", prompts, surface?.promptSections) : ""; + // #4305: standing generation-boundary note — byte-stable once detected; + // never on a compaction-trigger passthrough (renderTags="none" there). + const genBoundaryActive = detectGenerationBoundary(session, processedMessages, log); if (compressSurfaceOn && !isCompactionTrigger && !knobNoCompressPrompt()) { const prompt = withProjectAttribution(withMarkerIntegrityNote(withSummaryBudgetNote(responsesTextProtocol ? buildCompressHybridSystemPrompt(prompts, surface?.promptSections) : buildCompressSystemPrompt(prompts, surface?.promptSections), externalSummaryEnabled(config)), visibilityMarkers)); const devParts = [...projection.systemParts, ...forgedSummaries, prompt]; + if (genBoundaryActive) devParts.push(GENERATION_BOUNDARY_NOTE); if (absorbActive) devParts.push(buildAbsorbSystemPrompt(absorbToolName(loopConfig))); const devContent = devParts.join("\n\n---\n\n"); responsesDevContent = devContent; @@ -410,7 +415,7 @@ export async function prepareResponses( : injectResponsesTool(parsed.tools, respExtra.length > 0 ? [...(ccrOn ? BILI_ACP_TOOLS_RESPONSES : BILI_ACP_TOOLS_RESPONSES_NO_RANGE), ...respExtra] : (ccrOn ? BILI_ACP_TOOLS_RESPONSES : BILI_ACP_TOOLS_RESPONSES_NO_RANGE), surface?.toolPrompts, externalSummaryEnabled(config)); } } else if (tagsOnlyPrompt !== "" || projection.systemParts.length > 0 || forgedSummaries.length > 0) { - const devContent = [...projection.systemParts, ...forgedSummaries, ...(tagsOnlyPrompt !== "" ? [tagsOnlyPrompt] : [])].join("\n\n---\n\n"); + const devContent = [...projection.systemParts, ...forgedSummaries, ...(tagsOnlyPrompt !== "" ? [tagsOnlyPrompt] : []), ...(genBoundaryActive && !isCompactionTrigger ? [GENERATION_BOUNDARY_NOTE] : [])].join("\n\n---\n\n"); responsesDevContent = devContent; if (forgedSummaries.length > 0) log("debug", `[${sessionId}] [inject] ${forgedSummaries.length} captured summary block(s) re-injected into developer message`); rebuiltInput = injectResponsesDeveloperMessage(rebuiltInput, devContent); diff --git a/src/server/relay.ts b/src/server/relay.ts index 594cd98736..b6b71536ba 100644 --- a/src/server/relay.ts +++ b/src/server/relay.ts @@ -18,6 +18,7 @@ import { applyCompatRoles, applyCompatRolesJson, detectRoleRejection, detectSyst import { compressLoopResponsesJson } from "../compress-loop-responses.js"; import { resolveCompress } from "../compress-settings.js"; import { buildAbsorbSystemPrompt, buildCompressHybridSystemPrompt, buildCompressSystemPrompt, withMarkerIntegrityNote, withProjectAttribution, withSummaryBudgetNote } from "../compress-tool.js"; +import { detectGenerationBoundary, GENERATION_BOUNDARY_NOTE } from "../generation-boundary.js"; import { findRoute, resolveDeclaredProtocol, type ProxyOptions } from "../config.js"; import { makeContinuationRefetch } from "../degenerate-retry.js"; import { dumpRejectedBody } from "../error-dump.js"; @@ -1856,7 +1857,12 @@ export async function forward( // only THEN pushes the absorb section (sysParts order). Nesting it // inside flipped the two sections on round-2 and broke the // system-element prefix at every fold (cache-seam-probes P1+P2/P3). - const systemPrompt = withProjectAttribution(withMarkerIntegrityNote(withSummaryBudgetNote(textProtocol ? buildCompressHybridSystemPrompt(prepared.prompts ?? defaultPrompts, prepared.surface?.promptSections) : buildCompressSystemPrompt(prepared.prompts ?? defaultPrompts, prepared.surface?.promptSections), externalSummaryEnabled(config)), visibilityMarkers)) + absorbSection; + // #4305 cache-seam parity: the standing generation-boundary note sits + // between the compress prompt and the absorb section on every steady + // prepare* path — the round-2 rebuild must keep that exact order or + // the system-element prefix forks at every fold of an affected session. + const genBoundaryNote = detectGenerationBoundary(prepared.session, prepared.originalMessages, log) ? `\n\n${GENERATION_BOUNDARY_NOTE}` : ""; + const systemPrompt = withProjectAttribution(withMarkerIntegrityNote(withSummaryBudgetNote(textProtocol ? buildCompressHybridSystemPrompt(prepared.prompts ?? defaultPrompts, prepared.surface?.promptSections) : buildCompressSystemPrompt(prepared.prompts ?? defaultPrompts, prepared.surface?.promptSections), externalSummaryEnabled(config)), visibilityMarkers)) + genBoundaryNote + absorbSection; const adapter = pickAdapter(prepared.protocol, parsedReq, textProtocol, prepared.responsesProjection, prepared.anthropicSystem, prepared.openaiSystemText, absorbActive ? absorbToolName(loopConfig) : undefined, prepared.google, prepared.systemNotes, opts.streamErrorShape, prepared.anthropicCacheMarks, prepared.anthropicClientCacheControls, absorbActive); const refreshFolded = async (current: CoreMessage[]): Promise => { return withSessionLock(prepared.session, async () => { diff --git a/tests/generation-boundary.test.ts b/tests/generation-boundary.test.ts new file mode 100644 index 0000000000..48afbb44a1 --- /dev/null +++ b/tests/generation-boundary.test.ts @@ -0,0 +1,134 @@ +// #4305 regression: after a mid-conversation model/upstream switch the ref +// ledger restarts while the visible history keeps citing previous-generation +// refs — the standing generation-boundary note must be detected ONCE from +// that residue and ride every subsequent request byte-stably. + +import assert from "node:assert/strict"; +import { test } from "node:test"; +import { defaultConfig } from "acp-kernel"; +import type { CoreMessage } from "acp-kernel"; +import type { ProxyOptions } from "../src/config.js"; +import { createSession, type Session } from "../src/session.js"; +import { detectGenerationBoundary, GENERATION_BOUNDARY_NOTE, readGenerationBoundary } from "../src/generation-boundary.js"; +import { PROJECT_ATTRIBUTION_LINE } from "../src/compress-tool.js"; +import { injectSystem } from "../src/server/inject.js"; + +const msg = (id: string, text: string): CoreMessage => ({ id, role: "user", contentType: "text", text }); + +function sessionWithFrontier(frontier: number): Session { + const s = createSession(`genb-${Math.random().toString(36).slice(2)}`); + const byRef: Record = {}; + for (let i = 1; i <= frontier; i++) byRef[`m${String(i).padStart(5, "0")}`] = `raw-${i}`; + s.state.messageRefs.byRef = byRef; + return s; +} + +test("#4305: two distinct above-frontier citations detect and persist the verdict", () => { + const s = sessionWithFrontier(10); + const messages = [ + msg("a", "Compressed b7 covering m00042–m00051, summary attached."), + msg("b", "The earlier acp_status report listed range m00060–m00071 as compressible."), + ]; + const logs: string[] = []; + const active = detectGenerationBoundary(s, messages, (_l, m) => logs.push(m)); + assert.equal(active, true, "note rides the request that detected the residue"); + const rec = readGenerationBoundary(s); + assert.ok(rec, "verdict persisted in session metadata"); + assert.equal(rec?.frontier, "m00010"); + // Early-exit by design: the first message already carries two distinct stale + // refs, so the second one is never scanned — maxStaleRef records what was seen. + assert.equal(rec?.maxStaleRef, "m00051"); + assert.equal(logs.length, 1, "detection logged exactly once"); + assert.match(logs[0], /\[acp-gen-boundary\]/); +}); + +test("#4305: citations at or below the frontier never trigger", () => { + const s = sessionWithFrontier(10); + const messages = [msg("a", "compress m00003–m00007 please"), msg("b", "and m00010 was the last one")]; + assert.equal(detectGenerationBoundary(s, messages), false); + assert.equal(readGenerationBoundary(s), undefined, "no metadata written"); +}); + +test("#4305: one stray above-frontier token (repeated) is not evidence — distinctness guard", () => { + const s = sessionWithFrontier(10); + const messages = [msg("a", "refs m00042 m00042 m00042 keep failing")]; + assert.equal(detectGenerationBoundary(s, messages), false); + assert.equal(readGenerationBoundary(s), undefined); +}); + +test("#4305: fresh session (no mapped refs yet) detects on stale residue", () => { + const s = sessionWithFrontier(0); + const messages = [msg("a", "old receipt says compress m09631–m09640"), msg("b", "and m9612 did too")]; + assert.equal(detectGenerationBoundary(s, messages), true); + assert.equal(readGenerationBoundary(s)?.frontier, ""); +}); + +test("#4305: tag-shaped residue (surviving ACP tags) is detected too", () => { + const s = sessionWithFrontier(10); + const tagged = "\x3cacp tokens=\"5\" type=\"text\"\x3em00077\x3c/acp\x3e\nhello"; + const messages = [msg("a", tagged), msg("b", "also saw m00088 quoted earlier")]; + assert.equal(detectGenerationBoundary(s, messages), true); + assert.equal(readGenerationBoundary(s)?.maxStaleRef, "m00088"); +}); + +test("#4305: prose with a single stray m-shaped word does not trigger", () => { + const s = sessionWithFrontier(10); + const messages = [msg("a", "the variable m12345 is out of scope here")]; + assert.equal(detectGenerationBoundary(s, messages), false); +}); + +test("#4305: already-detected sessions short-circuit — no rescan, still active", () => { + const s = sessionWithFrontier(10); + detectGenerationBoundary(s, [msg("a", "m00042 and m00051"), msg("b", "more")]); + const logs: string[] = []; + assert.equal(detectGenerationBoundary(s, [], (_l, m) => logs.push(m)), true, "verdict survives with empty history"); + assert.equal(logs.length, 0, "no second detection log"); +}); + +test("#4305: readGenerationBoundary rejects malformed records", () => { + const s = sessionWithFrontier(10); + s.metadata!.generationBoundary = { at: "nope" }; + assert.equal(readGenerationBoundary(s), undefined); + s.metadata!.generationBoundary = {}; + assert.equal(readGenerationBoundary(s), undefined); + assert.equal(detectGenerationBoundary(s, []), false, "malformed record re-detects instead of trusting it"); +}); + +test("#4305: the note is a static string naming the reset and the authority", () => { + assert.ok(GENERATION_BOUNDARY_NOTE.length > 100); + assert.ok(GENERATION_BOUNDARY_NOTE.includes("m00001"), "names the restart point"); + assert.ok(GENERATION_BOUNDARY_NOTE.includes("acp_status"), "points at the live authority"); +}); + +const OPTS: ProxyOptions = { + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: {}, + modelContextLimit: 400_000, + kernelConfig: defaultConfig(400_000), + compress: { injectTool: true, injectNudge: false }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + autoRestartOnUpdate: false, + advisoryCheck: false, + releaseNotesCheck: false, + compat: { roles: {} }, + streamErrorShape: "protocol", + mitm: { enabled: false, domains: [] }, +}; + +test("#4305: injectSystem appends the note only when the boundary is active", () => { + const parsed = { system: "You are a helpful assistant.", messages: [] }; + const off = injectSystem({ ...parsed }, OPTS, undefined, defaultConfig(400_000), undefined, true, false, false) as string; + assert.ok(!off.includes(GENERATION_BOUNDARY_NOTE), "inactive: system block unchanged by this feature"); + const on = injectSystem({ ...parsed }, OPTS, undefined, defaultConfig(400_000), undefined, true, false, true) as string; + assert.ok(on.includes(GENERATION_BOUNDARY_NOTE), "active: note rides the ACP standing block"); + assert.ok(on.startsWith("You are a helpful assistant."), "client system block stays byte-exact (appended, never merged)"); + // sysParts order contract: client head, standing prompt (attribution outermost), THEN the note. + assert.ok(on.indexOf(GENERATION_BOUNDARY_NOTE) > on.indexOf(PROJECT_ATTRIBUTION_LINE), "note sits AFTER the standing prompt, matching every steady prepare* path"); +}); From 82afc81738e3642d02888627e867a018db00f455 Mon Sep 17 00:00:00 2001 From: awork Date: Thu, 22 Oct 2026 21:38:30 +0800 Subject: [PATCH 2/4] fix(#4305): keep GenerationBoundaryRecord module-internal (unused-exports gate) --- src/generation-boundary.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/generation-boundary.ts b/src/generation-boundary.ts index 32f5f0213c..49fda259f6 100644 --- a/src/generation-boundary.ts +++ b/src/generation-boundary.ts @@ -44,7 +44,7 @@ const REF_CITATION_RE = /\bm(\d{4,})\b/g; /** byRef key shape guard (mirrors stream.ts M_REF_NUM_RE usage). */ const BYREF_KEY_RE = /^m(\d+)$/; -export interface GenerationBoundaryRecord { +interface GenerationBoundaryRecord { /** First-detection time (ms epoch). */ at: number; /** This session's highest mapped ref at detection ("mNNNNN" or "" when none) — evidence anchor, log surface only. */ From 48113a013a300c610ab8329b701ce907f5f7cf8a Mon Sep 17 00:00:00 2001 From: awork Date: Thu, 22 Oct 2026 21:48:53 +0800 Subject: [PATCH 3/4] =?UTF-8?q?fix(#4305):=20align=20round-2=20generation-?= =?UTF-8?q?boundary=20block=20indentation=20(13=E2=86=9212=20spaces)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review nit from #4308 review: the new relay.ts lines were indented one space deeper than the surrounding block. Whitespace-only, no behavior change. --- src/server/relay.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/server/relay.ts b/src/server/relay.ts index b6b71536ba..4901c04faa 100644 --- a/src/server/relay.ts +++ b/src/server/relay.ts @@ -1857,11 +1857,11 @@ export async function forward( // only THEN pushes the absorb section (sysParts order). Nesting it // inside flipped the two sections on round-2 and broke the // system-element prefix at every fold (cache-seam-probes P1+P2/P3). - // #4305 cache-seam parity: the standing generation-boundary note sits - // between the compress prompt and the absorb section on every steady - // prepare* path — the round-2 rebuild must keep that exact order or - // the system-element prefix forks at every fold of an affected session. - const genBoundaryNote = detectGenerationBoundary(prepared.session, prepared.originalMessages, log) ? `\n\n${GENERATION_BOUNDARY_NOTE}` : ""; + // #4305 cache-seam parity: the standing generation-boundary note sits + // between the compress prompt and the absorb section on every steady + // prepare* path — the round-2 rebuild must keep that exact order or + // the system-element prefix forks at every fold of an affected session. + const genBoundaryNote = detectGenerationBoundary(prepared.session, prepared.originalMessages, log) ? `\n\n${GENERATION_BOUNDARY_NOTE}` : ""; const systemPrompt = withProjectAttribution(withMarkerIntegrityNote(withSummaryBudgetNote(textProtocol ? buildCompressHybridSystemPrompt(prepared.prompts ?? defaultPrompts, prepared.surface?.promptSections) : buildCompressSystemPrompt(prepared.prompts ?? defaultPrompts, prepared.surface?.promptSections), externalSummaryEnabled(config)), visibilityMarkers)) + genBoundaryNote + absorbSection; const adapter = pickAdapter(prepared.protocol, parsedReq, textProtocol, prepared.responsesProjection, prepared.anthropicSystem, prepared.openaiSystemText, absorbActive ? absorbToolName(loopConfig) : undefined, prepared.google, prepared.systemNotes, opts.streamErrorShape, prepared.anthropicCacheMarks, prepared.anthropicClientCacheControls, absorbActive); const refreshFolded = async (current: CoreMessage[]): Promise => { From 24ca4e649290ae5e7999025ba61ff49382909537 Mon Sep 17 00:00:00 2001 From: awork Date: Thu, 22 Oct 2026 21:48:53 +0800 Subject: [PATCH 4/4] test(#4305): pin the generation-boundary note on the round-2 seam and tags-only surface MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two coverage gaps found in #4308 review: - The compress-loop round-2 rebuild recomposes the ACP standing system by hand; nothing pinned that a boundary-ACTIVE session keeps byte-identical system bytes between the steady request and the post-fold re-request (the same seam class #3298/#2499 fixed for attribution/cache marks). New tests/issue4305-round2-seam.test.ts drives one real fold through the anthropic wire with previous-generation residue seeded in the early turns and asserts round-2 system === round-1 system byte-for-byte. - The tags-only ACP surface (plugin/dsh lane, compressSurfaceOff — the lane that produced the #4305 incident) had no injection assertion; added a positive+negative pair to tests/generation-boundary.test.ts. --- tests/generation-boundary.test.ts | 10 ++ tests/issue4305-round2-seam.test.ts | 178 ++++++++++++++++++++++++++++ 2 files changed, 188 insertions(+) create mode 100644 tests/issue4305-round2-seam.test.ts diff --git a/tests/generation-boundary.test.ts b/tests/generation-boundary.test.ts index 48afbb44a1..3212f52df5 100644 --- a/tests/generation-boundary.test.ts +++ b/tests/generation-boundary.test.ts @@ -132,3 +132,13 @@ test("#4305: injectSystem appends the note only when the boundary is active", () // sysParts order contract: client head, standing prompt (attribution outermost), THEN the note. assert.ok(on.indexOf(GENERATION_BOUNDARY_NOTE) > on.indexOf(PROJECT_ATTRIBUTION_LINE), "note sits AFTER the standing prompt, matching every steady prepare* path"); }); + +test("#4305: tags-only surface (plugin/dsh lane, no bili tools) also carries the note when active", () => { + const parsed = { system: "You are a helpful assistant.", messages: [] }; + const tagsOnlyOpts = { ...OPTS, compress: { injectTool: false } as typeof OPTS.compress }; + const off = injectSystem({ ...parsed }, tagsOnlyOpts, undefined, defaultConfig(400_000), undefined, true, false, false); + assert.ok(typeof off === "string" && !off.includes(GENERATION_BOUNDARY_NOTE), "inactive: tags-only surface unchanged by this feature"); + const on = injectSystem({ ...parsed }, tagsOnlyOpts, undefined, defaultConfig(400_000), undefined, true, false, true) as string; + assert.ok(on.includes(GENERATION_BOUNDARY_NOTE), "active: note rides the tags-only ACP surface (the #4305 incident lane)"); + assert.ok(on.startsWith("You are a helpful assistant."), "client head stays byte-exact"); +}); diff --git a/tests/issue4305-round2-seam.test.ts b/tests/issue4305-round2-seam.test.ts new file mode 100644 index 0000000000..d32dc33dbb --- /dev/null +++ b/tests/issue4305-round2-seam.test.ts @@ -0,0 +1,178 @@ +// #4305: standing generation-boundary note — round-2 cache-seam parity. +// Once the one-shot detection fires (≥2 distinct above-frontier ref citations +// in the visible history, here seeded as previous-generation receipt/status +// residue in the early turns), the static note must ride EVERY rebuild of the +// ACP standing system block in the exact steady-path order (prompt → note → +// absorb). The compress-loop round-2 rebuild recomposes the system element by +// hand, so this pins that its output stays byte-identical to the steady +// prepare-anthropic system on a boundary-active session — the same seam class +// #3298/#2499 fixed for attribution and cache marks. +import assert from "node:assert"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { GENERATION_BOUNDARY_NOTE } from "../src/generation-boundary.ts"; +import { PROJECT_ATTRIBUTION_LINE } from "../src/compress-tool.ts"; + +const anthropicSse = (event: string, data: unknown): string => + `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`; + +const FILLER = (seed: number, kb: number): string => { + const para = `Paragraph ${seed}: the module under review passed its integration suite across all four regions without any reported regressions. `; + const unit = Math.ceil((kb * 1024) / para.length); + return Array.from({ length: unit }, (_, i) => para.replace(String(seed), `${seed}-${i}`)).join(""); +}; + +// Previous-generation residue: citations STRICTLY ABOVE this session's frontier +// (m00001..m00007 once the seven turns are mapped) — the provably-dead class +// the one-shot detector keys on. Two distinct numbers satisfy the guard. +const RESIDUE_1 = "An old tool receipt still visible upthread says compress m09631-m09640 succeeded earlier."; +const RESIDUE_2 = "An earlier acp_status report listed m09612 as compressible."; + +type TurnMsg = { role: string; content: Array> }; +const buildTurns = (): TurnMsg[] => { + const out: TurnMsg[] = []; + // Each turn is ~8KB so the folded pair clears the kernel's 5000-char + // minCompressRange floor while the trigger turn stays live in the tail. + for (let i = 1; i <= 3; i++) { + const residue = i === 1 ? RESIDUE_1 : i === 2 ? RESIDUE_2 : ""; + out.push({ role: "user", content: [{ type: "text", text: `user turn ${i}: examine subsystem ${i} and report every failure mode you can find in the current implementation. ${residue} ` + FILLER(i, 8) }] }); + out.push({ role: "assistant", content: [{ type: "text", text: `assistant ${i}: subsystem ${i} shows three notable failure modes centered on state recovery under load. ` + FILLER(100 + i, 8) }] }); + } + out.push({ role: "user", content: [{ type: "text", text: "TRIGGER_TURN_C: now patch the refresh-token rotation so a revoked token cannot mint a fresh one. " + FILLER(900, 1.0) }] }); + return out; +}; + +function compressSse(startId: string, endId: string): string { + const args = JSON.stringify({ startId, endId, topic: "fold", summary: "Folded the early subsystem-analysis turns; state-recovery failure modes and their remediation notes are retained in this summary." }); + return [ + anthropicSse("message_start", { type: "message_start", message: { id: "msg_r1", role: "assistant", usage: { input_tokens: 55 } } }), + anthropicSse("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "tool_use", id: "toolu_c_4305", name: "compress", input: {} } }), + anthropicSse("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "input_json_delta", partial_json: args } }), + anthropicSse("content_block_stop", { type: "content_block_stop", index: 0 }), + anthropicSse("message_delta", { type: "message_delta", delta: { stop_reason: "tool_use", stop_sequence: null }, usage: { output_tokens: 12 } }), + anthropicSse("message_stop", { type: "message_stop" }), + ].join(""); +} + +const TEXT_SSE = [ + anthropicSse("message_start", { type: "message_start", message: { id: "msg_r2", role: "assistant", usage: { input_tokens: 20 } } }), + anthropicSse("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }), + anthropicSse("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "Done after compress" } }), + anthropicSse("content_block_stop", { type: "content_block_stop", index: 0 }), + anthropicSse("message_delta", { type: "message_delta", delta: { stop_reason: "end_turn", stop_sequence: null }, usage: { output_tokens: 4 } }), + anthropicSse("message_stop", { type: "message_stop" }), +].join(""); + +interface Harness { + proxyPort: number; + upstreamPort: number; + captured: string[]; + close(): Promise; +} + +async function startHarness(): Promise { + const captured: string[] = []; + let call = 0; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + captured.push(Buffer.concat(chunks).toString("utf8")); + // Fold m00001..m00002 — outside the kernel's last-5 protected zone — + // so the compress commits and round 2 rebuilds the system element. + const sse = call === 0 ? compressSse("m00001", "m00002") : TEXT_SSE; + call += 1; + res.writeHead(200, { "content-type": "text/event-stream" }); + res.end(sse); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const proxy = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { "claude-test": { context: 400_000 } } } }, + modelContextLimit: 400_000, + kernelConfig: defaultConfig(400_000), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + compat: { roles: {} }, + streamErrorShape: "protocol", + passthroughSource: null, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: false, + releaseNotesCheck: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + + return { + proxyPort, + upstreamPort, + captured, + close: async () => { + proxy.close(); + proxy.closeIdleConnections?.(); + await once(proxy, "close"); + upstream.close(); + upstream.closeIdleConnections?.(); + await once(upstream, "close"); + }, + }; +} + +type Block = { type?: string; text?: string }; +type Body = { system?: string | Block[]; messages: Array<{ role: string; content: string | Block[] }> }; +const systemAsBytes = (b: Body): string => (typeof b.system === "string" ? b.system : JSON.stringify(b.system)); + +test("#4305: boundary-active session — round-2 system rebuild stays byte-identical to the steady system", async () => { + const h = await startHarness(); + try { + const resp = await fetch(`http://127.0.0.1:${h.proxyPort}/bili/http://127.0.0.1:${h.upstreamPort}/v1/messages`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "issue4305-round2-seam" }, + body: JSON.stringify({ + model: "claude-test", + max_tokens: 1024, + stream: true, + system: "You are a coding agent.", + messages: buildTurns(), + }), + }); + assert.equal(resp.status, 200); + await resp.text(); + + assert.ok(h.captured.length >= 2, `expected a round-2 re-request, got ${h.captured.length} upstream bodies`); + assert.ok(h.captured[1]!.includes("tokens saved"), "compress committed — the post-fold view actually reached upstream"); + + const r1 = JSON.parse(h.captured[0]!) as Body; + const r2 = JSON.parse(h.captured[1]!) as Body; + assert.ok(systemAsBytes(r1).includes(GENERATION_BOUNDARY_NOTE), "round 1: the standing note rides the steady system once the residue is seen"); + assert.ok(systemAsBytes(r2).includes(GENERATION_BOUNDARY_NOTE), "round 2: the persisted verdict keeps the note after the fold"); + assert.ok(systemAsBytes(r2).indexOf(GENERATION_BOUNDARY_NOTE) > systemAsBytes(r2).indexOf(PROJECT_ATTRIBUTION_LINE), "note sits AFTER the standing prompt (steady-path order)"); + assert.equal(systemAsBytes(r2), systemAsBytes(r1), "round-2 rebuild is byte-identical to the steady system — the prefix-cache seam holds with the boundary note active"); + } finally { + await h.close(); + } +});