Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions reference/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
126 changes: 126 additions & 0 deletions src/generation-boundary.ts
Original file line number Diff line number Diff line change
@@ -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+)$/;

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<string, unknown>;
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<Session["state"], "messageRefs">): 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<number>();
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.";
10 changes: 8 additions & 2 deletions src/server/inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion src/server/prepare-anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions src/server/prepare-google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions src/server/prepare-openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 6 additions & 1 deletion src/server/prepare-responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down
Loading
Loading