Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
b81c421
kernel: require in-range evidence for open-objective status changes (…
Oct 18, 2026
448485d
fix: recognize externalSummary in COMPRESS_SETTING_FIELDS so misplace…
ranxianglei Oct 19, 2026
6930e77
fix: MCP structuredContent carries the full report text (#3472)
Oct 17, 2026
9addf98
fix: pin favicon.ico to a local 204 and declare an empty panel icon (…
Oct 17, 2026
67488b4
fix: plugin manifest serves top-level promptPack tool descriptions (#…
Oct 15, 2026
4a9e28e
fix: acp-cache renders per-line WIRE/UPSTREAM/RESTART sections (#3307)
Oct 16, 2026
f4a21bb
fix: re-anchor folded history across host wire-shape flips (#3301)
Oct 15, 2026
e2b404c
fix: warn when compress-block keys sit inert on a provider route root…
Oct 18, 2026
69f3a22
fix(server): confirm parent death over consecutive probes; drain befo…
Oct 20, 2026
826523d
Revert "kernel: require in-range evidence for open-objective status c…
ranxianglei Oct 24, 2026
b233d70
fix: acp-cache seam report keeps suspect/client sections disjoint (#3…
Oct 16, 2026
470ab2d
fix: log per-fold firstFoldStartTokens (X) + V/Vp for #3307 attributi…
Oct 16, 2026
c266a5f
fix: fold-attribution log adds compressed-view total (Vproc) for #330…
Oct 18, 2026
0200f6a
fix(cache): measure fold-shrink V over the live view, not the raw res…
Oct 19, 2026
de193f4
fix(cache): attribute fold re-pay via wire-measured prefix, not the i…
Oct 19, 2026
8459b24
test(cache): pin P1 re-folding V basis (live view after a prior fold …
Oct 19, 2026
fb34510
Merge remote-tracking branch 'origin/master' into 2026-10-24_tier4-ds…
ranxianglei Oct 24, 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
1 change: 1 addition & 0 deletions scripts/live-files.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const LIVE_FILES = [
"tests/index-entry-guard.test.ts",
"tests/issue1958-routed-warn.test.ts",
"tests/issue2752-forward-partial-outcome.test.ts",
"tests/issue3472-mcp-structured-text.test.ts",
"tests/issue4341-early-twin-adoption.test.ts",
"tests/issue760-percall-conversation.test.ts",
"tests/issue927-opencode-install.test.ts",
Expand Down
58 changes: 52 additions & 6 deletions src/cache-ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ function round1(n: number): number {
* sum of first-render token counts of all refs chronologically before it.
* Refs are assigned in message order and never reused (kernel contract),
* so numeric order IS view order. */
function prefixTokensBeforeRef(session: Session, ref: string): number {
export function prefixTokensBeforeRef(session: Session, ref: string): number {
const byRef = session.state?.messageRefs?.byRef;
const snap = session.state?.tokenSnapshot;
if (!byRef || !snap || refNum(ref) === 0) return 0;
Expand Down Expand Up @@ -1276,6 +1276,40 @@ export function recordCacheSample(
viewBefore: f.V,
viewAfter: f.Vp,
}));
// #3307 P2-A: f.X (firstFoldStartTokens) is prefixTokensBeforeRef over the
// client's UN-SHRUNKEN resent history — for a stateless proxy client that far
// exceeds the billed input, so decomposeSample zeroes structuralExcess and the
// fold's re-pay falls into the unattributed seam bucket. When a genuine
// bili-side mid-history break is present, measure the stable prefix straight
// off the wire instead: the byte-LCP between this outbound and the previous
// marks where a summary replaced folded content, so (lcpBytes / cur.bytes) *
// billed input is a same-space lower bound on the pre-fold prefix. min() with
// the snapshot X leaves already-correct folds untouched. Gated to mid-list
// breaks only (a tail append lands lcpBytes near the end -> xMeasured ~= input
// -> no effect) and never on a client-side inbound rewrite (#3023), whose
// earlier divergence would understate the fold and mis-credit it.
if (pending.length > 0 && known && s.input > 0) {
const rawMissed = s.input - effCached;
if (rawMissed > 8192 && rawMissed > 0.3 * s.input) {
const curB = session.transient?.sentBody;
const prevB = session.transient?.settledBody;
if (curB !== undefined && prevB !== undefined && curB.bytes <= SEAM_BODY_CAP && prevB.bytes <= SEAM_BODY_CAP) {
const curIn = session.transient?.inBody;
const prevIn = session.transient?.settledInBody;
const clientBreak = curIn !== undefined && prevIn !== undefined && curIn.msgs <= INBOUND_MSG_CAP && prevIn.msgs <= INBOUND_MSG_CAP && (() => {
const k = firstDivergentInboundMsg(prevIn, curIn);
return k !== null && k < Math.min(prevIn.msgs, curIn.msgs);
})();
if (!clientBreak) {
const lcp = seamLcp(prevB.head, curB.head);
if (lcp.msgIndex !== null && lcp.prevMsgs > 0 && lcp.msgIndex < lcp.prevMsgs && lcp.lcpBytes > 0 && lcp.lcpBytes < curB.head.byteLength) {
const xMeasured = Math.round(s.input * (lcp.lcpBytes / curB.bytes));
for (const p of pending) p.firstFoldStartTokens = Math.min(p.firstFoldStartTokens ?? Number.POSITIVE_INFINITY, xMeasured);
}
}
}
}
}
const rawDec = decomposeSample(
prevLine ? { at: prevLine.at, input: prevLine.input, cached: prevLine.cached } : null,
{ at: s.at, input: s.input, cached: effCached },
Expand Down Expand Up @@ -2282,16 +2316,22 @@ export function handleAcpCache(session: Session, args?: Record<string, unknown>)
// #2867: proxy-side mode/projection switches get their own section only
// when observed — a session that never switches modes stays byte-identical.
const projText = report.projSwitches.count > 0 ? "\n\n" + formatModelSwitches(report.projSwitches, detail, "PROJECTION SWITCHES (proxy mode/projection change — bili's own doing, e.g. self-heal degrade-to-proxy)") : "";
// #3307: wire/upstream/restart carry per-event data in the report (switchEvents /
// restartEvents) but only their aggregate counts reached CACHE INVALIDATION — no
// per-line timestamps, so cross-checks (e.g. seam-vs-restart) were impossible.
const wireText = report.wireSwitches.count > 0 ? "\n\n" + formatModelSwitches(report.wireSwitches, detail, "WIRE SWITCHES (client switched protocol wire between requests)") : "";
const upstreamText = report.upstreamSwitches.count > 0 ? "\n\n" + formatModelSwitches(report.upstreamSwitches, detail, "UPSTREAM SWITCHES (relay/upstream origin changed between requests)") : "";
const restartText = report.restartDrops.count > 0 ? "\n\n" + formatModelSwitches(report.restartDrops, detail, "RESTART/REFORK (first bill under a fresh daemon boot — prefix re-billed once)") : "";
if (detail === "full" && report.lines.length > FULL_DETAIL_LINES) {
const dropped = report.lines.length - FULL_DETAIL_LINES;
const capped = formatCacheReport(
{ ...report, lines: report.lines.slice(-FULL_DETAIL_LINES), linesOmitted: report.linesOmitted + dropped },
session.id,
{ detail },
);
return toolOk(tail(capped + "\n\n" + formatModelSwitches(report.modelSwitches, detail) + effText + keyText + promptText + projText + "\n\n" + formatInvalidation(report)));
return toolOk(tail(capped + "\n\n" + formatModelSwitches(report.modelSwitches, detail) + effText + keyText + promptText + projText + wireText + upstreamText + restartText + "\n\n" + formatInvalidation(report)));
}
return toolOk(tail(formatCacheReport(report, session.id, { detail }) + "\n\n" + formatModelSwitches(report.modelSwitches, detail) + effText + keyText + promptText + projText + "\n\n" + formatInvalidation(report)));
return toolOk(tail(formatCacheReport(report, session.id, { detail }) + "\n\n" + formatModelSwitches(report.modelSwitches, detail) + effText + keyText + promptText + projText + wireText + upstreamText + restartText + "\n\n" + formatInvalidation(report)));
} catch (err) {
loggerLog("warn", `[${session.id}] [acp_cache] report failed: ${String(err)}`);
return toolFail(`[acp_cache FAILED: ${String(err)}]`);
Expand Down Expand Up @@ -2337,11 +2377,17 @@ function formatSeam(r: BiliCacheReport): string {
if (r.seam.suspects > 0) {
out.push("⚠ CACHE SEAM (suspected mid-history prefix breaks)");
out.push(` ${r.seam.suspects} sample(s) · ${fmtTok(r.seam.missed)} tok re-billed with no fold/switch/restart attribution`);
for (const e of r.seam.events) {
// #3307: seamEvents holds BOTH suspect and #3023 client-side events
// (one shared capped ring), so listing every event here echoed host-owned
// rewrites under the "suspected bili rebuild" header — the ✎ CLIENT
// section below already owns those. List only suspects here so the two
// sections stay disjoint and a client insert is never misread as a seam.
const suspectEvents = r.seam.events.filter((e) => !e.client);
for (const e of suspectEvents) {
out.push(` #${e.seq} ${fmtTime(e.at)} hit ${e.hitPct.toFixed(1)}% · input ${fmtTok(e.input)} · divergence ≥${fmtTok(e.lcpBytes)}B at message[${e.msgIndex ?? "?"}] of ${e.prevMsgs}→${e.curMsgs}`);
}
if (r.seam.events.length === 0) {
out.push(" (no body-pair forensics on this lane — aggregate flag only; report the session + log if this persists)");
if (suspectEvents.length === 0) {
out.push(" (no body-pair forensics retained for the suspect samples on this lane — aggregate flag only; client-side rewrites are listed separately below)");
}
out.push(" if reproducible: /acp-cache detail:\"full\" + bili.log around the timestamps above (likely a #1548-family round-2/steady render seam)");
}
Expand Down
26 changes: 25 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,26 @@ function warnInertMitmLaneKeys(routes: ProviderRoutes, env: NodeJS.ProcessEnv):
}
}

/** #3675: compress-block keys written on the ROUTE ROOT (URL lane or named
* entry) are silently dropped by parseRouteEntry — the same silent-inert
* class as the #1469 routing fields, one level down. Recognition is
* COMPRESS_SETTING_FIELDS (defined below; kept in sync with
* parseCompressSettings): every member is inert at a route root, and none of
* them may be added to parseRouteEntry's whitelist while this warning exists.
* Dialing recipes are skipped — their own parser rejects unknown root fields
* loudly (parseNamedProviderRecipe throws → chain disabled with a warning),
* so a second signal here would only add noise. */
function warnCompressOnlyFieldsAtRouteRoot(key: string, obj: Record<string, unknown> | null): void {
if (!obj) return;
if (obj.baseUrl !== undefined || obj.api !== undefined) return;
const misplaced = Object.keys(obj).filter((f) => obj[f] !== undefined && COMPRESS_SETTING_FIELDS.has(f));
if (misplaced.length === 0) return;
warnNamedProviderOnce(
`route-root-compress:${key}:${misplaced.join(",")}`,
`providers."${key}" carries compress-block keys [${misplaced.join(", ")}] at the route root — they are inert there (move them under providers."${key}".compress to apply)`,
);
}

/** True when the providers-map key is itself a URL lane (http/https/mitm
* scheme — mitm:// is the README-documented lookup key for MITM traffic).
* Anything else is a NAMED key (provider id): routing-inert on its own,
Expand Down Expand Up @@ -1257,6 +1277,10 @@ export function loadRoutes(env: NodeJS.ProcessEnv = process.env): ProviderRoutes
if (!route) return;
const obj = value && typeof value === "object" && !Array.isArray(value) ? value as Record<string, unknown> : null;
const urlLike = isUrlLikeKey(key);
// Before the bind branch on purpose: these keys are inert even on a
// validly bound alias — parseRouteEntry never parses them, so the
// fold onto the target lane cannot carry them.
warnCompressOnlyFieldsAtRouteRoot(key, obj);
if (obj && "bind" in obj) {
const bind = obj.bind;
if (typeof bind !== "string") {
Expand Down Expand Up @@ -2207,7 +2231,7 @@ const COMPRESS_SETTING_FIELDS = new Set([
"visibilityMarkers", "rules", "injectTool", "injectNudge",
"acknowledgePromptsRisk", "absorb", "ccr", "search",
"prompts", "promptPack", "reasoningGuard", "outputSteering", "priceProfile",
"reconcile", "streamSummary",
"reconcile", "streamSummary", "externalSummary",
// #3635: summary-length caps — previously accepted nowhere, silently dropped.
"maxSummaryLength", "minSummaryLength",
]);
Expand Down
125 changes: 125 additions & 0 deletions src/fold-decomposition-lookup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// #3301: stored-decomposition source for fold-anchor backfill.
//
// Anchors persisted before turn keys existed (pre-#2644) carry no p/x, and the
// wire-side backfill can only fire while the old id is still on the wire — a
// wire-shape flip (host metadata refresh re-serializing every past thinking
// turn) puts it off the wire forever — and the flip pass is the LAST pass in
// which those ids sit in the order backbone (claims only iterate backbone ids).
// This module pulls those ids' original message decompositions out of the
// unified index (full mode only) so reconcileFoldCoverage can rebuild p/x
// BEFORE its plan runs and the flip pass itself claims. Ids absent from the index are
// marked tried once (absence is stable for folded-out history); an UNAVAILABLE
// index is not marked tried (it may appear later).

import type { CoreMessage } from "acp-kernel";
import type { Session } from "./session.js";
import { getStore } from "./persist.js";
import { log as loggerLog } from "./logger.js";
import { METADATA_ANCHORS, type FoldAnchor } from "./fold-reconcile.js";

export interface StoredRow {
seq: number;
msgId: string;
content: string;
}

export interface DecompositionSource {
/** null = no index available (legacy/shadow/degraded); [] = index has none of them. */
rows(sessionId: string, msgIds: readonly string[]): StoredRow[] | null;
}

const MAX_LOOKUP_PER_PASS = 256;
const TRIED_METADATA_KEY = "foldStoredTried";
const MAX_TRIED = 8192;

function defaultSource(): DecompositionSource {
return {
rows(sessionId, msgIds) {
try {
return getStore().unifiedMessageRows(sessionId, msgIds);
} catch (err) {
loggerLog("warn", `[fold-reconcile] stored decomposition lookup failed: ${String(err)}`);
return null;
}
},
};
}

/** Wire-site convenience: build the wire-id set and run the lookup in one
* call (used by all four prepare-* lanes). */
export function storedMessagesFor(session: Session, sessionId: string, msgs: readonly CoreMessage[]): CoreMessage[] | undefined {
const wireIds = new Set<string>();
for (const m of msgs) if (m.id !== undefined) wireIds.add(m.id);
return resolveStoredDecompositions(session, sessionId, wireIds);
}

export function resolveStoredDecompositions(
session: Session,
sessionId: string,
wireIds: ReadonlySet<string>,
source: DecompositionSource = defaultSource(),
): CoreMessage[] | undefined {
const metadata = session.metadata;
if (metadata === undefined) return undefined;
const anchors = (metadata[METADATA_ANCHORS] as Record<string, FoldAnchor> | undefined) ?? {};
const anchorIds = Object.keys(anchors);
if (anchorIds.length === 0) return undefined;
const tried = (metadata[TRIED_METADATA_KEY] as Record<string, number> | undefined) ?? {};
const needed: string[] = [];
for (const id of anchorIds) {
const a = anchors[id];
if (a.p !== undefined || a.r !== "assistant" || a.t !== undefined) continue;
if (wireIds.has(id)) continue;
if (tried[id] !== undefined) continue;
needed.push(id);
if (needed.length >= MAX_LOOKUP_PER_PASS) break;
}
if (needed.length === 0) return undefined;
const rows = source.rows(sessionId, needed);
if (rows === null) return undefined; // index unavailable: retry next pass
for (const id of needed) tried[id] = 1;
const pruned: Record<string, number> = {};
let n = 0;
for (const [id, v] of Object.entries(tried)) {
if (!anchors[id]) continue;
pruned[id] = v;
if (++n >= MAX_TRIED) break;
}
metadata[TRIED_METADATA_KEY] = pruned;
// Latest seq per id wins (edits append rows); neighbors stay in so rc
// pairs keep adjacency for turn-key derivation. Unparseable rows are
// auxiliary-evidence misses, not errors — count them, don't throw.
let skipped = 0;
const latestSeq = new Map<string, number>();
const ordered: { seq: number; m: CoreMessage }[] = [];
const orderedIdx = new Map<string, number>();
for (const row of rows) {
let parsed: unknown;
try {
parsed = JSON.parse(row.content);
} catch {
skipped++;
continue;
}
if (typeof parsed !== "object" || parsed === null) {
skipped++;
continue;
}
const m = parsed as CoreMessage;
if (typeof m.id !== "string" || typeof m.role !== "string") {
skipped++;
continue;
}
const prev = latestSeq.get(row.msgId);
if (prev !== undefined && prev >= row.seq) continue;
latestSeq.set(row.msgId, row.seq);
const prevIdx = orderedIdx.get(row.msgId);
if (prevIdx !== undefined) ordered.splice(prevIdx, 1);
ordered.push({ seq: row.seq, m });
orderedIdx.set(row.msgId, ordered.length - 1);
}
if (skipped > 0) loggerLog("debug", `[fold-reconcile] stored decomposition: ${skipped} unparseable row(s) skipped`);
if (ordered.length === 0) return undefined;
ordered.sort((a, b) => a.seq - b.seq);
return ordered.map((o) => o.m);
}
Loading
Loading