Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
f25ff3d
feat(web): nudge-recommended vs actually-folded recovery view (#6729)
Nov 14, 2026
c3a0333
feat(web): recovery-per-nudge-round line chart (#6729)
Nov 14, 2026
c522dda
Merge remote-tracking branch 'origin/master' into 2026-11-14_recovery…
Nov 14, 2026
0df852d
docs(web): recovery measurement caveats — T1-only pct, >100% burst, w…
Nov 14, 2026
2d6b9dd
Merge remote-tracking branch 'origin/master' into 2026-11-14_recovery…
Nov 14, 2026
5141179
feat(web): rework recovery view into one self-contained chart (#6729)
Nov 15, 2026
e75a2a0
Merge remote-tracking branch 'origin/2026-11-14_recovery-visibility'
Nov 15, 2026
c2f0b5c
fix(web): drop dead recovery zoom channel + restore lost newline (#6729)
Nov 15, 2026
d643f0f
feat(web): baseline-100% recovery line chart + honest over/under acco…
Nov 15, 2026
9929279
fix(web): recovery point tip lost tier + report old-stock bracket nev…
Nov 15, 2026
6279145
Merge remote-tracking branch 'origin/master' into wt-6745-ci-engineer-r1
Nov 15, 2026
c5f573f
refactor(web): recovery chart as a true line chart — bars become tick…
Nov 15, 2026
9a30dee
fix: remove dead activesBefore field
Nov 15, 2026
fe4b75b
feat(web): recovery chart plots EVERY compression as a line point (#6…
Nov 15, 2026
47e59a9
feat(web): add the growth-recovery line — folded vs. new context grow…
Nov 16, 2026
72f5c2e
fix: restore the 1.1 axis margin in the report's recovery chart
Nov 16, 2026
aff1543
style(web): move the growth line to its own teal color with hollow ma…
Nov 16, 2026
a4d9f2e
fix(web): drop color names from the active-compression legend text (#…
Nov 16, 2026
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
183 changes: 183 additions & 0 deletions src/cache-ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,19 @@ interface LedgerFold {
bid?: string;
}

// #6729: nudge-vs-fold recovery ledger (#6720 ratchet). A nudge lists the
// recommended compressible ranges (pendingT1 = their token sum); the model
// often folds only part of it, so the post-fold floor climbs turn over turn.
// Nothing visualized the recommended-vs-actually-folded gap until now, so the
// session panel and the downloadable report render both per event.
const RECOVERY_BURST_MS = 10 * 60_000;
const RECOVERY_STALE_MS = 30 * 60_000;
const RECOVERY_CAP = 500;

type RecoveryEvent =
| { kind: "n"; at: number; p: number; tier: number | null }
| { kind: "f"; at: number; S: number; seq: number; p: number; mode: "n" | "a"; pct?: number };

interface LedgerLine {
captureId?: string;
captureRound?: number;
Expand Down Expand Up @@ -212,6 +225,9 @@ interface LedgerLine {
interface CacheLedger {
v: 1;
lastBlockId: number;
/** #6729: nudge-injection / fold recovery events (sqlite-persisted with
* the rest of the ledger; sparse — absent on pre-#6729 ledgers). */
recovery?: RecoveryEvent[];
/** #3769: high-water of session.state.sweptOrphanCallIds length — detects a
* proactive failed-compress cleanup between samples so detectSeam can
* attribute a mid-history break to the cleanup instead of "unexplained". */
Expand Down Expand Up @@ -1275,6 +1291,58 @@ function pushFold(led: CacheLedger, f: Omit<LedgerFold, "seq" | "T" | "hPct" | "
}
led.foldSeqCounter += 1;
led.folds.push({ ...f, seq: led.foldSeqCounter, T: 0, hPct: null, requestsAfter: 0, k: null });
recordFoldRecovery(led, f.at, f.S, led.foldSeqCounter);
}

/** #6729: book a nudge that ACTUALLY reached the upstream payload (call from
* the prepare return when nudgeOutcome === "injected" — vetoed/skipped/armed-
* suppressed arms never landed, so they never arm attribution). p is the
* recommended token sum (pendingT1, T1 only); higher tiers pass 0. */
export function recordNudgeInject(session: Session, nudge: { tier?: number | null; breakdown?: Record<string, number> } | null | undefined): void {
if (!nudge) return;
const led = getCacheLedger(session);
const evs = led.recovery ?? (led.recovery = []);
if (evs.length === 0) {
// First real injection: backfill the folds that already happened —
// they all predate any injection, so every one of them is an active
// compression. (Backfilling here instead of at fold time keeps the
// persisted metadata byte-identical for sessions that never get a
// nudge — the storage-render goldens hash metadata_json.)
for (const f of led.folds) {
evs.push({ kind: "f", at: f.at, S: f.S, seq: f.seq, p: 0, mode: "a" });
}
}
evs.push({ kind: "n", at: Date.now(), p: nudge.breakdown?.["pendingT1"] ?? 0, tier: typeof nudge.tier === "number" ? nudge.tier : null });
if (evs.length > RECOVERY_CAP) evs.splice(0, evs.length - RECOVERY_CAP);
markDirty(session);
}

/** #6729: pair a fold with the nudge that armed it. Owner attribution ruling
* (2026-11-14): a fold in the same or next round after an injection, plus the
* consecutive burst that follows it (≤ RECOVERY_BURST_MS between folds), all
* belong to that nudge; a fold whose preceding message carried no injection
* (and is not in a burst chain) is an ACTIVE (model-initiated) compression. */
function recordFoldRecovery(led: CacheLedger, at: number, S: number, seq: number): void {
// The field is only ever created by the FIRST real nudge injection (which
// backfills every earlier fold as active) — otherwise no-injection sessions
// would grow an empty field and drift the persisted metadata byte-for-byte.
if (led.recovery === undefined) return;
const evs = led.recovery;
let lastF: RecoveryEvent | undefined;
let lastN: RecoveryEvent | undefined;
for (const ev of evs) {
if (ev.kind === "f") lastF = ev;
else if (ev.at <= at) lastN = ev;
}
const gapArmed = lastN !== undefined && lastN.at > (lastF?.at ?? 0);
const chainArmed = lastF !== undefined && at - lastF.at <= RECOVERY_BURST_MS;
const nudgeFresh = lastN !== undefined && at - lastN.at <= RECOVERY_STALE_MS;
const mode = lastN && nudgeFresh && (gapArmed || chainArmed) ? ("n" as const) : ("a" as const);
// A stale nudge's pending must not label a newer fold — the report would
// compare this fold against a recommendation the fold predates nothing of.
const p = lastN && nudgeFresh ? lastN.p : 0;
evs.push({ kind: "f", at, S, seq, p, mode, pct: mode === "n" && p > 0 ? Math.round((S / p) * 1000) / 10 : undefined });
if (evs.length > RECOVERY_CAP) evs.splice(0, evs.length - RECOVERY_CAP);
}

/** Record compression folds materialized as new kernel blocks. Proxy mode
Expand Down Expand Up @@ -2134,7 +2202,24 @@ interface BodyStability {
gapSplit: { lowHitMedGapMs: number | null; highHitMedGapMs: number | null };
}

/** #6729: nudge-recommended vs actually-folded per event, plus the session
* average recovery rate (Σ folded / Σ recommended over all nudge arms, a
* never-folded arm counting as 0 recovered). */
interface RecoveryReport {
events: Array<{ at: number; kind: "n" | "f"; tier?: number | null; pending: number; folded: number | null; foldSeq: number | null; mode: "n" | "a" | null; pct: number | null; paired: boolean }>;
summary: { nudges: number; folds: number; nudgeFolds: number; activeFolds: number; totalPending: number; totalFolded: number; pct: number | null };
/** #6729 follow-up: one point PER NUDGE ROUND for the recovery line
* chart — a round is the nudge plus every fold until the next nudge,
* so several partial folds in one round sum up (the demo's 4×25% folds
* are ONE 100% point, the ratchet's single-folds stay low). pct is null
* when the nudge carried no measurable pending (non-T1 tiers). */
episodes: Array<{ at: number; tier: number | null; pending: number; folded: number; pct: number | null; credited: number; extra: number; activeFolds: number }>;
}

interface BiliCacheReport extends CacheReport {
/** #6729: recommended-vs-folded recovery (absent when the session has no
* recovery events yet — pre-upgrade ledgers or no injections so far). */
recovery?: RecoveryReport;
stability: BodyStability;
/** #2131: line set widened with the per-call body-stability fields. */
lines: Array<CacheReportLine & { captureId?: string; captureRound?: number; evidence?: "out-of-order"; bodyDigest?: string; bodyEqual?: 0 | 1; bodyLcp?: number; bodyClass?: "head" | "append" | "mid" | "tail" | "unknown"; keyFp?: string; sysFp?: string; efp?: string; seam?: 1; prov?: "ttl" | "side" | "rewind" | "client" | "orphan-sweep" }>;
Expand Down Expand Up @@ -2307,6 +2392,103 @@ export function buildSessionCacheReport(session: Session): BiliCacheReport {
sizeBuckets,
gapSplit: { lowHitMedGapMs: median(lowGaps), highHitMedGapMs: median(highGaps) },
};
// #6729: pair each nudge arm with the NEXT nudge-attributed fold it armed
// (one fold can answer only one arm); an arm with no follow-on fold is the
// ratchet's direct signal. totalFolded counts EVERY nudge-attributed fold
// (burst folds included) — pairing is per-arm bookkeeping, not a filter.
let recovery: RecoveryReport | undefined;
// No injection ever landed → the persisted field stays absent (metadata
// golden parity), but the report still lists every fold as an active
// compression — that view exists independently of the nudge stream.
const recSource: RecoveryEvent[] = (led.recovery && led.recovery.length > 0)
? led.recovery
: led.folds.map((f) => ({ kind: "f" as const, at: f.at, S: f.S, seq: f.seq, p: 0, mode: "a" as const }));
if (recSource.length > 0) {
const paired = new Set<number>();
const recEvents = recSource.map((ev, i) => ({
at: ev.at,
kind: ev.kind,
...(ev.kind === "n" ? { tier: ev.tier } : {}),
pending: ev.p,
folded: ev.kind === "f" ? ev.S : null,
foldSeq: ev.kind === "f" ? ev.seq : null,
mode: (ev.kind === "f" ? ev.mode : null) as "n" | "a" | null,
pct: ev.kind === "f" ? (ev.pct ?? null) : null,
paired: false,
i,
}));
for (let i = 0; i < recEvents.length; i++) {
if (recEvents[i].kind !== "n") continue;
for (let j = i + 1; j < recEvents.length; j++) {
const f = recEvents[j];
if (f.kind === "n") break;
if (paired.has(j)) continue;
paired.add(j);
recEvents[i].paired = true;
break;
}
}
let nudges = 0;
let folds = 0;
let nudgeFolds = 0;
let activeFolds = 0;
let totalPending = 0;
let totalFolded = 0;
for (const [j, f] of recEvents.entries()) {
if (f.kind === "n") {
nudges += 1;
totalPending += f.pending;
} else {
folds += 1;
if (f.mode === "n") {
nudgeFolds += 1;
totalFolded += f.folded ?? 0;
} else {
activeFolds += 1;
}
}
}
// Round aggregation: nudge i owns every fold until nudge i+1 (or the
// end). Active folds inside the window ride along as a marker count —
// they never count toward the round's recovery. credited/extra split
// the round's folded mass against the OUTSTANDING recommended pool
// (the client re-derives the same split per zoom window):
// what came from recommended stock vs older stock the model cleaned
// on its own — so a 370% round reads as "list + 182K old stock",
// not 370K of phantom recovery.
const episodes: RecoveryReport["episodes"] = [];
let recPool = 0;
for (let j = 0; j < recEvents.length; j++) {
const ev = recEvents[j];
if (ev.kind === "n") {
recPool += ev.pending;
let epFolded = 0;
let epActive = 0;
for (let k = j + 1; k < recEvents.length && recEvents[k].kind !== "n"; k++) {
const f = recEvents[k];
if (f.mode === "n") epFolded += f.folded ?? 0;
else epActive += 1;
}
const credited = Math.min(epFolded, recPool);
recPool = Math.max(0, recPool - epFolded);
episodes.push({
at: ev.at,
tier: ev.tier ?? null,
pending: ev.pending,
folded: epFolded,
pct: ev.pending > 0 ? Math.round((epFolded / ev.pending) * 1000) / 10 : null,
credited,
extra: epFolded - credited,
activeFolds: epActive,
});
}
}
recovery = {
events: recEvents.map(({ i: _i, ...ev }) => ev),
summary: { nudges, folds, nudgeFolds, activeFolds, totalPending, totalFolded, pct: totalPending > 0 ? Math.round((totalFolded / totalPending) * 1000) / 10 : null },
episodes,
};
}
return {
generatedAt: Date.now(),
profile: effective,
Expand Down Expand Up @@ -2351,6 +2533,7 @@ export function buildSessionCacheReport(session: Session): BiliCacheReport {
initialBills: { samples: a.nbSamples, inputTokens: a.nbInput },
invalidation,
stability,
recovery,
seam: { suspects: a.seamSuspects, missed: a.seamMissed, events: led.seamEvents ?? [], providerSide: { count: a.providerSideMisses, missed: a.providerSideMissed }, providerTtl: { count: a.providerTtlMisses, missed: a.providerTtlMissed }, rewinds: { count: a.rewinds, missed: a.rewindMissed }, abortCorrelated: a.abortCorrelated, client: { count: a.clientInserts, missed: a.clientInsertMissed }, orphanSweep: { count: a.orphanSweepSeams, missed: a.orphanSweepMissed } },
};
}
Expand Down
2 changes: 2 additions & 0 deletions src/server/prepare-anthropic.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import http from "node:http";
import type { CompressionCore, Config, CoreMessage, NudgeDecision, PackSurface, Prompts } from "acp-kernel";
import { renderNudgeText, viableRanges } from "acp-kernel";
import { recordNudgeInject } from "../cache-ledger.js";
import { anthropicToCore, buildSystem, coreToAnthropic, extractSystem, healAnthropicSystemPositions, insertBeforeTrailingSystemRun, type AnthropicRequestBody, type AnthropicWireMessage, type BiliMessage } from "acp-kernel/wire";
import { rewriteInboundThreadContinue } from "./thread-tracking.js";
import { captureThreadsTurn, estimateThreadsTokens, isThreadsBody, threadsReforgeEnabled } from "./threads-lane.js";
Expand Down Expand Up @@ -691,6 +692,7 @@ export async function prepareAnthropic(
// main-scale tiebreak sees this request's own upper bound.
probeRequestMembership(session, collectRequestIds(incomingMsgIds, originalMessages), session.stats.localInputEstimate ?? 0);
if (upstreamOrigin) session.stats.lastLocalTextEstimateOrigin = upstreamOrigin;
if (nudgeOutcome === "injected") recordNudgeInject(session, nudge);
return { body: JSON.stringify(rebuilt), parsedBody: rebuilt, session, attachedRetrievals, attachedRetrievalNoteIds, processedMessages, originalMessages, judgeBaseline, anthropicSystem: parsed.system, anthropicBillingBlock, anthropicCacheMarks, anthropicClientCacheControls: clientCacheControls, systemNotes: sysNotes, protocol: "anthropic", stream, compressInjected: injectTools, pluginMode, mcpDialectNote: laneDecisions.mcpDialect, nudge, nudgeOutcome, nudgeStampsBefore: nudgeOutcome === "injected" ? nudgeStampsBefore : undefined, prompts, surface, renderTags: renderStrategy, dropReasoning: (msgs: BiliMessage[]) => applyReasoningDrop(msgs, reasoningPlan, log, sessionId) } as Prepared;
}

Expand Down
2 changes: 2 additions & 0 deletions src/server/prepare-google.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import http from "node:http";
import type { CompressionCore, Config, CoreMessage, NudgeDecision, PackSurface, Prompts } from "acp-kernel";
import { renderNudgeText, viableRanges } from "acp-kernel";
import { recordNudgeInject } from "../cache-ledger.js";
import { coreToGoogle, googleToCore, normalizeGoogleUserPartOrder, type BiliMessage, type GoogleContent, type GoogleRequestBody, type GoogleSystemInstruction, type GoogleTool } from "acp-kernel/wire";
import type { ProxyOptions } from "../config.js";
import { pluginAgentHeader } from "../plugin.js";
Expand Down Expand Up @@ -426,6 +427,7 @@ export async function prepareGoogle(
// #3397 durability-before-render (see prepare-anthropic.ts): rows durable NOW, before the model call.
getStore().ingestRowsNow(session);
markDirty(session);
if (nudgeOutcome === "injected") recordNudgeInject(session, nudge);
return { body: JSON.stringify(rebuilt), parsedBody: rebuilt, session, processedMessages, originalMessages, judgeBaseline, protocol: "google", stream, compressInjected: injectTools, pluginMode, mcpDialectNote: laneDecisions.mcpDialect, nudge, nudgeOutcome, nudgeStampsBefore: nudgeOutcome === "injected" ? nudgeStampsBefore : undefined, prompts, surface, google: { system: googleClientSystem, model }, systemNotes: sysNotes, renderTags: renderStrategy } as Prepared;
}

Expand Down
2 changes: 2 additions & 0 deletions src/server/prepare-openai.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import http from "node:http";
import type { CompressionCore, Config, CoreMessage, NudgeDecision, PackSurface, Prompts } from "acp-kernel";
import { isRenderedSummaryMessage, renderNudgeText, viableRanges } from "acp-kernel";
import { recordNudgeInject } from "../cache-ledger.js";
import { coreToOpenai, injectOpenaiSystem, openaiToCore, type BiliMessage, type OpenAIRequestBody, type OpenAITool } from "acp-kernel/wire";
import type { CompressReasoningConfig } from "../reasoning-drop.js";
import type { ProxyOptions } from "../config.js";
Expand Down Expand Up @@ -659,6 +660,7 @@ export async function prepareOpenai(
});
if (Object.keys(wpResponseHeaders).length === 0) wpResponseHeaders = undefined;
}
if (nudgeOutcome === "injected") recordNudgeInject(session, nudge);
return { body: JSON.stringify(rebuilt), parsedBody: rebuilt, session, attachedRetrievals, attachedRetrievalNoteIds, processedMessages, originalMessages, judgeBaseline, protocol: "openai", stream, compressInjected: injectTools, pluginMode, mcpDialectNote: laneDecisions.mcpDialect, nudge, nudgeOutcome, nudgeStampsBefore: nudgeOutcome === "injected" ? nudgeStampsBefore : undefined, prompts, surface, openaiSystemText, systemNotes: sysNotes, renderTags: renderStrategy, responseExtraHeaders: wpResponseHeaders, dropReasoning: (msgs: BiliMessage[]) => applyReasoningDrop(msgs, reasoningPlan, log, sessionId) } as Prepared;
}

Expand Down
2 changes: 2 additions & 0 deletions src/server/prepare-responses.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import http from "node:http";
import type { AbsorbConfig, CompressionCore, Config, CoreMessage, NudgeDecision, PackSurface, Prompts } from "acp-kernel";
import { defaultCountTokens, buildBlockOpsSystemPrompt, renderNudgeText, viableRanges } from "acp-kernel";
import { recordNudgeInject } from "../cache-ledger.js";
import { injectResponsesDeveloperMessage, type BiliMessage, type ResponseInputItem, type ResponsesProjection, type ResponsesRequestBody } from "acp-kernel/wire";
import type { CompressReasoningConfig } from "../reasoning-drop.js";
import { resolveAbsorbSettings } from "../compress-settings.js";
Expand Down Expand Up @@ -755,6 +756,7 @@ reconcileFoldCoverage(session, msgs, { mode: foldMode, sessionId, log, markerStr
separatorSlots,
sentBodyStr,
);
if (nudgeOutcome === "injected") recordNudgeInject(session, nudge);
return {
body: sentBodyStr,
parsedBody: rebuilt,
Expand Down
Loading
Loading