Skip to content
Merged
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 src/cache-ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1536,6 +1536,7 @@ function formatSeam(r: BiliCacheReport): string {
if (r.seam.providerSide.count > 0) {
out.push("▲ PROVIDER-SIDE MISS (previous request's message list fully preserved)");
out.push(` ${r.seam.providerSide.count} sample(s) · ${fmtTok(r.seam.providerSide.missed)} tok — the previous request's message list is preserved in this one (byte-for-byte under the 512 KiB forensics cap; above it, proven by the stable recorded head plus exact message-count growth — any difference is only the appended tail); the upstream did not serve its cache (TTL expiry / eviction / relay node rotation). Not a bili rebuild seam.`);
out.push(` stateful-upstream check (#2483): if your upstream keeps a live session per conversation and validates the echoed history against its own transcript (claude-code-based relays such as magpie), bili's render tags on assistant text trip that check even though bili's own list is unchanged — set diagnostics.renderNone=true until per-route render-tags control ships; expect exactly ONE replay when flipping the flag mid-session`);
}
if (r.seam.abortCorrelated > 0) {
out.push("⏻ ABORT-CORRELATED");
Expand Down
10 changes: 7 additions & 3 deletions src/server/prepare-anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ export async function prepareAnthropic(
let clientCacheControls: Map<string, unknown> | undefined;
let systemOut = parsed.system;
let toolsOut = parsed.tools;
// #2483: single source for the effective render strategy — processTurn, the
// diag log and Prepared must all report what was actually rendered (the
// log label used to hardcode "text-only" and lie under renderNone).
const renderStrategy = knobRenderNone() ? "none" : "text-only";

// #1085: sticky head-system anchor — freeze the client's own `system` text
// at first sight and forward it byte-stable; detected changes ride as
Expand Down Expand Up @@ -201,7 +205,7 @@ export async function prepareAnthropic(
const foldCoveredBefore = session.stats.pendingFoldUsage === true
? new Set(session.state.blocks.flatMap((b) => (b.active ? b.effectiveMessageIds : [])))
: null;
const turn = core.processTurn({ messages: msgs, state: session.state, config: loopConfig, tokenCount, renderTags: knobRenderNone() ? "none" : "text-only", contentStore: contentStoreOf(session) });
const turn = core.processTurn({ messages: msgs, state: session.state, config: loopConfig, tokenCount, renderTags: renderStrategy, contentStore: contentStoreOf(session) });
session.state = turn.state;
adoptContentStore(session, turn.contentStore);
// The fold from last turn's compress has now materialized in state —
Expand All @@ -226,7 +230,7 @@ export async function prepareAnthropic(
const t = deriveTitle(msgs);
if (t) session.meta.title = t;
}
log("info", diagTagSummary(turn.messages, sessionId, "text-only"));
log("info", diagTagSummary(turn.messages, sessionId, renderStrategy));
// #2155: a self-heal-suppressed session (nudge idle / zombie fallback)
// stops nagging — including the emergency path, per session.
const willInjectNudge = opts.compress.injectNudge && !!turn.nudge && !nudgeSuppressed(session) && !compressBreakerArmed(session) && !(autoFoldEngaged(loopConfig, session) && growthFoldingArmed(loopConfig)) && (turn.nudge.shouldInject || emergencyNudge(turn.nudge, undefined, loopConfig.compress.minCompressRange));
Expand Down Expand Up @@ -388,7 +392,7 @@ export async function prepareAnthropic(
+ countSystemAndToolsTokens(extractSystem(systemOut), toolsOut)
+ imageReserveFor(session, "anthropic", rebuilt, opts, upstreamOrigin);
if (upstreamOrigin) session.stats.lastLocalTextEstimateOrigin = upstreamOrigin;
return { body: JSON.stringify(rebuilt), session, attachedRetrievals, attachedRetrievalNoteIds, processedMessages, originalMessages, anthropicSystem: parsed.system, anthropicBillingBlock, anthropicCacheMarks, anthropicClientCacheControls: clientCacheControls, systemNotes: sysNotes, protocol: "anthropic", stream, compressInjected: injectTools, pluginMode, nudge, prompts, surface, renderTags: knobRenderNone() ? "none" : "text-only", dropReasoning: stripReasoning } as Prepared;
return { body: JSON.stringify(rebuilt), session, attachedRetrievals, attachedRetrievalNoteIds, processedMessages, originalMessages, anthropicSystem: parsed.system, anthropicBillingBlock, anthropicCacheMarks, anthropicClientCacheControls: clientCacheControls, systemNotes: sysNotes, protocol: "anthropic", stream, compressInjected: injectTools, pluginMode, nudge, prompts, surface, renderTags: renderStrategy, dropReasoning: stripReasoning } as Prepared;
}


Expand Down
23 changes: 14 additions & 9 deletions src/server/prepare-google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { attachSubagentSessions } from "../subagent-sessions.js";
import { reconcileSystemAnchor } from "../system-anchor.js";
import { stripAcpPanelMessages, stripAcpStatusMarkers } from "../acp-panel.js";
import { stripEmbeddedChainCarriers } from "../chain-checkpoint.js";
import { renderNone as knobRenderNone } from "../knobs.js";
import { clampOutgoingOutput, countSystemAndToolsTokens, dshLedgerFloorTokens, emergencyNudge } from "./budget.js";
import { estimateCoreMessages } from "../preflight.js";
import { effectiveAbsorbBlock } from "./prepare-responses.js";
Expand Down Expand Up @@ -87,6 +88,9 @@ export async function prepareGoogle(
const isTitleGen = maxTokens <= 200;
const shouldInject = opts.compress.injectTool && !isTitleGen;
const injectTools = shouldInject && !pluginMode;
// #2483: this lane used to ignore renderNone entirely (hardcoded "text-only"
// in processTurn, Prepared and countTokens) — the knob was dead config on /v1beta.
const renderStrategy = knobRenderNone() ? "none" : "text-only";

const strippedCarriers = stripEmbeddedChainCarriers(parsed, "google");
if (strippedCarriers > 0) {
Expand Down Expand Up @@ -147,11 +151,11 @@ export async function prepareGoogle(
const foldCoveredBefore = session.stats.pendingFoldUsage === true
? new Set(session.state.blocks.flatMap((b) => (b.active ? b.effectiveMessageIds : [])))
: null;
const turn = core.processTurn({ messages: msgs, state: session.state, config: loopConfig, tokenCount, renderTags: "text-only", contentStore: contentStoreOf(session) });
const turn = core.processTurn({ messages: msgs, state: session.state, config: loopConfig, tokenCount, renderTags: renderStrategy, contentStore: contentStoreOf(session) });
session.state = turn.state;
adoptContentStore(session, turn.contentStore);
// The fold from last turn's compress has materialized in state — future
// usage reports are post-fold reality, drop the credit.
// The fold from last turn's compress has now materialized in state —
// future usage reports are post-fold reality, drop the credit.
session.stats.compressCreditTokens = 0;
if (foldCoveredBefore !== null && !isTitleGen && msgs.length >= REWRITE_MIN_INCOMING_TOTAL) {
const gap = foldCoverage(foldCoveredBefore, msgs.map((m) => m.id));
Expand All @@ -161,8 +165,9 @@ export async function prepareGoogle(
storeEffectiveRules(session, config);
turn.messages = applyAbsorbView(turn.messages, session.state, loopConfig, tokenCount);
turn.messages = attachSubagentSessions(turn.messages, session);
// Drop sub-viability fragments before any consumer sees them (the
// kernel validates a compress batch atomically).
// Drop sub-viability fragments before any consumer sees them: a tiny
// range in the list makes batched compress attempts fail atomically
// (kernel validates the whole batch). Mirrors billion-context-pi.
if (turn.nudge) turn.nudge.compressibleRanges = viableRanges(turn.nudge.compressibleRanges);
nudge = turn.nudge;
session.stats.contextTokens = tokenCount;
Expand All @@ -171,7 +176,7 @@ export async function prepareGoogle(
const t = deriveTitle(msgs);
if (t) session.meta.title = t;
}
log("info", diagTagSummary(turn.messages, sessionId, "text-only"));
log("info", diagTagSummary(turn.messages, sessionId, renderStrategy));
const willInjectNudge = opts.compress.injectNudge && !!turn.nudge && shouldInject && !nudgeSuppressed(session) && !compressBreakerArmed(session) && !(autoFoldEngaged(loopConfig, session) && growthFoldingArmed(loopConfig)) && (turn.nudge.shouldInject || emergencyNudge(turn.nudge, undefined, loopConfig.compress.minCompressRange));
log("info", diagNudge(turn, sessionId, tokenCount, config.modelContextLimit, model, willInjectNudge));
processedMessages = stripKernelSummaries(turn.messages, turn.state);
Expand All @@ -193,7 +198,7 @@ export async function prepareGoogle(
// and round-2 re-requests — skipping them here would fork the prefix
// at every fold and collapse the upstream cache hit.
if (shouldInject) sysParts.push(withMarkerIntegrityNote(withSummaryBudgetNote(buildCompressSystemPrompt(prompts, surface?.promptSections), externalSummaryEnabled(config)), visibilityMarkers));
else if (!isTitleGen) {
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);
Expand Down Expand Up @@ -277,7 +282,7 @@ export async function prepareGoogle(
}
snapshotMessages(session, originalMessages);
markDirty(session);
return { body: JSON.stringify(rebuilt), session, processedMessages, originalMessages, protocol: "google", stream, compressInjected: injectTools, pluginMode, nudge, prompts, surface, google: { system: googleClientSystem, model }, systemNotes: sysNotes, renderTags: "text-only" } as Prepared;
return { body: JSON.stringify(rebuilt), session, processedMessages, originalMessages, protocol: "google", stream, compressInjected: injectTools, pluginMode, nudge, prompts, surface, google: { system: googleClientSystem, model }, systemNotes: sysNotes, renderTags: renderStrategy } as Prepared;
}

/** `POST /v1beta/models/<model>:countTokens` — the fold-prune twin of
Expand All @@ -296,7 +301,7 @@ export function prepareGoogleCountTokens(
const { msgs } = googleToCore(parsed);
// Read-only preview: the store rides in so placeholder substitution is
// counted, but nothing is adopted (state is discarded here too).
const turn = core.processTurn({ messages: msgs, state: session.state, config: ccrLoopConfig(session, config), tokenCount: usageGradeInputBaseline(session), renderTags: "text-only", contentStore: contentStoreOf(session) });
const turn = core.processTurn({ messages: msgs, state: session.state, config: ccrLoopConfig(session, config), tokenCount: usageGradeInputBaseline(session), renderTags: knobRenderNone() ? "none" : "text-only", contentStore: contentStoreOf(session) });
const stripped = stripKernelSummaries(turn.messages, turn.state);
const rebuilt: GoogleRequestBody = { ...parsed, contents: coreToGoogle(stripped as BiliMessage[]) };
log("info", `[${sessionId}] countTokens pruned: ${msgs.length} → ${stripped.length} msgs`);
Expand Down
10 changes: 7 additions & 3 deletions src/server/prepare-openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ export async function prepareOpenai(
let nudge: NudgeDecision | undefined;
let rebuiltMessages = parsed.messages;
let toolsOut = parsed.tools;
// #2483: single source for the effective render strategy — processTurn, the
// diag log and Prepared must all report what was actually rendered (the
// log label used to hardcode "text-only" and lie under renderNone).
const renderStrategy = knobRenderNone() ? "none" : "text-only";

const maxTokens = typeof parsed.max_tokens === "number" ? parsed.max_tokens : 8192;
// Title-generation requests (tiny max_tokens) get no compress tooling so
Expand Down Expand Up @@ -163,7 +167,7 @@ export async function prepareOpenai(
const foldCoveredBefore = session.stats.pendingFoldUsage === true
? new Set(session.state.blocks.flatMap((b) => (b.active ? b.effectiveMessageIds : [])))
: null;
const turn = core.processTurn({ messages: msgs, state: session.state, config: loopConfig, tokenCount, renderTags: knobRenderNone() ? "none" : "text-only", contentStore: contentStoreOf(session) });
const turn = core.processTurn({ messages: msgs, state: session.state, config: loopConfig, tokenCount, renderTags: renderStrategy, contentStore: contentStoreOf(session) });
session.state = turn.state;
adoptContentStore(session, turn.contentStore);
// The fold from last turn's compress has now materialized in state —
Expand All @@ -188,7 +192,7 @@ export async function prepareOpenai(
const t = deriveTitle(msgs);
if (t) session.meta.title = t;
}
log("info", diagTagSummary(turn.messages, sessionId, "text-only"));
log("info", diagTagSummary(turn.messages, sessionId, renderStrategy));
const willInjectNudge = opts.compress.injectNudge && !!turn.nudge && shouldInject && !nudgeSuppressed(session) && !compressBreakerArmed(session) && !(autoFoldEngaged(loopConfig, session) && growthFoldingArmed(loopConfig)) && (turn.nudge.shouldInject || emergencyNudge(turn.nudge, undefined, loopConfig.compress.minCompressRange));
log("info", diagNudge(turn, sessionId, tokenCount, config.modelContextLimit, parsed.model, willInjectNudge));
processedMessages = stripReasoning(stripKernelSummaries(turn.messages, turn.state));
Expand Down Expand Up @@ -374,7 +378,7 @@ export async function prepareOpenai(
}
snapshotMessages(session, originalMessages);
markDirty(session);
return { body: JSON.stringify(rebuilt), session, attachedRetrievals, attachedRetrievalNoteIds, processedMessages, originalMessages, protocol: "openai", stream, compressInjected: injectTools, pluginMode, nudge, prompts, surface, openaiSystemText, systemNotes: sysNotes, renderTags: knobRenderNone() ? "none" : "text-only", dropReasoning: stripReasoning } as Prepared;
return { body: JSON.stringify(rebuilt), session, attachedRetrievals, attachedRetrievalNoteIds, processedMessages, originalMessages, protocol: "openai", stream, compressInjected: injectTools, pluginMode, nudge, prompts, surface, openaiSystemText, systemNotes: sysNotes, renderTags: renderStrategy, dropReasoning: stripReasoning } as Prepared;
}

/** Append the ephemeral nudge to a Gemini `contents` array. Gemini is
Expand Down
2 changes: 1 addition & 1 deletion src/server/prepare-responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ export async function prepareResponses(
const t = deriveTitle(msgs);
if (t) session.meta.title = t;
}
log("info", diagTagSummary(turn.messages, sessionId, "text-only"));
log("info", diagTagSummary(turn.messages, sessionId, renderTags));
const willInjectNudge = opts.compress.injectNudge && !!turn.nudge && shouldInject && !isCompactionTrigger && !nudgeSuppressed(session) && !compressBreakerArmed(session) && !(autoFoldEngaged(loopConfig, session) && growthFoldingArmed(loopConfig, codexLane && codexCompactMode() === "intercept" ? loopConfig.modelContextLimit * CODEX_COMPACT_HEALTH_RATIO : undefined)) && (turn.nudge.shouldInject || emergencyNudge(turn.nudge, undefined, loopConfig.compress.minCompressRange));
log("info", diagNudge(turn, sessionId, tokenCount, config.modelContextLimit, parsed.model, willInjectNudge));
processedMessages = repairResponsesAssistantOrdering(stripReasoning(stripKernelSummaries(turn.messages, turn.state)), originalMessages);
Expand Down
13 changes: 13 additions & 0 deletions tests/cache-seam-detector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,19 @@ test("seam detector: byte-stable resend attributes to PROVIDER-SIDE MISS, not a
assert.match(handleAcpCache(s).text, /PROVIDER-SIDE MISS/);
});

test("seam detector: PROVIDER-SIDE MISS section mentions the stateful-upstream render-tag check (#2483)", () => {
const s = makeSession();
const same = body(["a", "b"]);
noteForwardedBody(s, same);
settle(s, T0, 100_000, 99_000);
noteForwardedBody(s, same);
settle(s, T0 + 1000, 100_000, 20_000);
const text = handleAcpCache(s).text;
assert.match(text, /PROVIDER-SIDE MISS/);
assert.match(text, /stateful-upstream check \(#2483\)/);
assert.match(text, /diagnostics\.renderNone=true/);
});

test("seam detector: tail-append turn attributes to PROVIDER-SIDE MISS, not a seam (#2059)", () => {
const s = makeSession();
noteForwardedBody(s, body(["a", "b"]));
Expand Down
Loading
Loading