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
88 changes: 81 additions & 7 deletions src/cache-ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,13 @@ interface LedgerLine {
* 1 KiB (model/tools/sampling-params/system region → host-side parameter
* change, not a history rewrite); "append": the whole shorter payload
* survives ±64 closing-bracket bytes (pure tail growth/trim); "mid": a
* mid-history rewrite; "unknown": bodies above BODY_FULL_CAP (inequality
* proven by digest, offset unavailable). Sparse. */
bs?: "head" | "append" | "mid" | "unknown";
* mid-history rewrite; "tail" (#2982): the first divergence sits at/past
* the shorter body's message-array closing bracket — only trailing
* top-level params (host-computed max_tokens/…) or an ephemeral tail note
* that appeared/vanished between requests changed; shared history bytes
* untouched, prefix cache unaffected; "unknown": bodies above BODY_FULL_CAP
* (inequality proven by digest, offset unavailable). Sparse. */
bs?: "head" | "append" | "mid" | "tail" | "unknown";
}

interface CacheLedger {
Expand Down Expand Up @@ -564,6 +568,57 @@ function byteLcp(a: string, b: string): number {
return lcp;
}

// #2982: byte offset of the CLOSING bracket terminating the top-level
// messages/input/contents array (same key set as seamLcp's msgsOf), or null
// when the body carries no such array (or is not valid JSON). String-aware
// depth scan over the raw text instead of JSON.parse + re-serialization:
// re-serialized element offsets do not match the original text (whitespace),
// and one linear pass keeps multi-MiB bodies cheap. Runs only on pairs that
// would otherwise class "mid" — the hot path never pays for it.
function messagesArrayCloseOffset(body: string): number | null {
const n = body.length;
let depth = 0; // object/array nesting depth
let inStr = false;
let esc = false;
let want = -1; // depth at which the tracked array closes (-1 = not tracking)
let keyAt = -1; // opening-quote offset of a candidate top-level key
for (let i = 0; i < n; i++) {
const c = body.charCodeAt(i);
if (inStr) {
if (esc) esc = false;
else if (c === 92) esc = true; // backslash
else if (c === 34) {
inStr = false;
if (keyAt >= 0) {
// +1/-1 strip the surrounding quotes from the captured range.
const key = body.slice(keyAt + 1, i);
keyAt = -1;
// A top-level string VALUE is also seen at depth 1 — the
// following-colon check below is what separates keys from it.
if (depth === 1 && (key === "messages" || key === "input" || key === "contents")) {
let j = i + 1;
while (j < n && body.charCodeAt(j) <= 32) j++;
if (body.charCodeAt(j) === 58) { // ':'
j++;
while (j < n && body.charCodeAt(j) <= 32) j++;
if (body.charCodeAt(j) === 91) want = depth + 1; // '['
}
}
}
}
continue;
}
if (c === 34) { inStr = true; keyAt = depth === 1 ? i : -1; continue; }
if (c === 123 || c === 91) { depth++; continue; }
if (c === 125 || c === 93) {
if (want !== -1 && c === 93 && depth === want) return i;
depth--;
continue;
}
}
return null;
}

function detectSeam(session: Session, led: CacheLedger): void {
const line = led.lines[led.lines.length - 1];
if (!line || line.unk === 1 || line.missed <= 0) return;
Expand Down Expand Up @@ -1273,7 +1328,20 @@ export function settleUsageReport(
// Append/trim first: "the whole shorter payload survives"
// is anchored to the END of the payload and stays true on
// small bodies where the head threshold alone would lie.
curLine.bs = lcp >= Math.min(prevSlot.full.length, curSlot.full.length) - 64 ? "append" : lcp < 1024 ? "head" : "mid";
// #2982: the would-be-mid branch pays one extra scan over
// the SHORTER body: when the first divergence sits at/past
// that body's message-array closing bracket, no shared
// history byte moved — only trailing top-level params
// (host-computed max_tokens/…) or an ephemeral tail note
// that appeared/vanished between requests differ (#2980).
// The shorter side is decisive: when one side grew the
// array, the LCP stops at ITS closing bracket (`]` vs `,`),
// which lies inside the LONGER side's array — parsing that
// side would misread the pair as mid. No array locatable
// (unparseable body) keeps the conservative pre-fix class.
const shorterFull = prevSlot.full.length <= curSlot.full.length ? prevSlot.full : curSlot.full;
const arrClose = messagesArrayCloseOffset(shorterFull);
curLine.bs = lcp >= Math.min(prevSlot.full.length, curSlot.full.length) - 64 ? "append" : lcp < 1024 ? "head" : arrClose !== null && lcp >= arrClose ? "tail" : "mid";
} else {
curLine.bs = "unknown";
}
Expand Down Expand Up @@ -1353,14 +1421,16 @@ interface BodyStability {
paired: number;
/** be=1: byte-identical resends (transport retries, re-requests). */
equal: number;
/** be=0: diverged; the four classes partition this count. */
/** be=0: diverged; the five classes partition this count. */
diverged: number;
/** Divergence inside the first 1 KiB — host-side parameter region (model/tools/sampling/system), NOT a history rewrite. */
head: number;
/** Whole shorter payload survives ±64 closing-bracket bytes — pure tail growth/trim. */
append: number;
/** Mid-history byte change — the shape a real rewrite has. */
mid: number;
/** #2982: divergence at/past the shorter body's message-array closing bracket — trailing top-level params or ephemeral tail notes; shared history bytes untouched, prefix cache unaffected. */
tail: number;
/** Diverged but offset unknown (bodies above BODY_FULL_CAP, digest-only mode). */
unknownOffset: number;
/** Median/min hit rate per 20K-token input-size bucket — flat across sizes argues against KV-pool capacity thrashing. */
Expand All @@ -1372,7 +1442,7 @@ interface BodyStability {
interface BiliCacheReport extends CacheReport {
stability: BodyStability;
/** #2131: line set widened with the per-call body-stability fields. */
lines: Array<CacheReportLine & { bodyDigest?: string; bodyEqual?: 0 | 1; bodyLcp?: number; bodyClass?: "head" | "append" | "mid" | "unknown"; keyFp?: string; sysFp?: string; seam?: 1; prov?: "ttl" | "side" | "rewind" }>;
lines: Array<CacheReportLine & { bodyDigest?: string; bodyEqual?: 0 | 1; bodyLcp?: number; bodyClass?: "head" | "append" | "mid" | "tail" | "unknown"; keyFp?: string; sysFp?: string; seam?: 1; prov?: "ttl" | "side" | "rewind" }>;
modelSwitches: ModelSwitchStats;
/** #2131: relay account rotations (credential fingerprint changes) —
* the identity a URL-granular upstream switch cannot see. */
Expand Down Expand Up @@ -1491,6 +1561,7 @@ export function buildSessionCacheReport(session: Session): BiliCacheReport {
let head = 0;
let append = 0;
let mid = 0;
let tail = 0;
let unknownOffset = 0;
for (const l of knownLines) {
if (l.be === undefined) continue;
Expand All @@ -1499,6 +1570,7 @@ export function buildSessionCacheReport(session: Session): BiliCacheReport {
if (l.bs === "head") head += 1;
else if (l.bs === "append") append += 1;
else if (l.bs === "mid") mid += 1;
else if (l.bs === "tail") tail += 1;
else unknownOffset += 1;
}
const sizeBuckets: BodyStability["sizeBuckets"] = [];
Expand Down Expand Up @@ -1532,6 +1604,7 @@ export function buildSessionCacheReport(session: Session): BiliCacheReport {
head,
append,
mid,
tail,
unknownOffset,
sizeBuckets,
gapSplit: { lowHitMedGapMs: median(lowGaps), highHitMedGapMs: median(highGaps) },
Expand Down Expand Up @@ -1635,9 +1708,10 @@ function formatStability(st: BodyStability): string {
if (st.paired === 0) return "";
const out: string[] = ["BODY STABILITY (per-call outbound byte proof, vs previous settled request)"];
out.push(` ${st.paired} pair(s): ${st.equal} byte-identical · ${st.diverged} diverged`);
out.push(` divergence class: head <1KiB ${st.head} · tail-append ${st.append} · mid-history ${st.mid} · offset-unknown(capped) ${st.unknownOffset}`);
out.push(` divergence class: head <1KiB ${st.head} · tail-append ${st.append} · mid-history ${st.mid} · tail(params/notes) ${st.tail} · offset-unknown(capped) ${st.unknownOffset}`);
if (st.head > 0) out.push(` ⚠ ${st.head} head-region divergence(s) — model/tools/sampling/system changed between calls (host-side parameter change, NOT a bili history rewrite)`);
if (st.mid > 0) out.push(` ⚠ ${st.mid} mid-history divergence(s) — real prefix rewrites; inspect the seam events above`);
if (st.tail > 0) out.push(` ${st.tail} tail-region divergence(s) — trailing top-level params or ephemeral tail notes at/past the message-array end; shared history bytes untouched, prefix cache unaffected (#2982)`);
const sb = st.sizeBuckets.filter((b) => b.n >= 3);
if (sb.length >= 2) out.push(" hit% by input size (median): " + sb.map((b) => `${Math.round(b.lo / 1000)}K=${b.hitMedian}%`).join(" "));
const g = st.gapSplit;
Expand Down
2 changes: 2 additions & 0 deletions src/web/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1345,6 +1345,7 @@ export const WEB_CLIENT = `(function () {
head: Number(stRaw.head) || 0,
append: Number(stRaw.append) || 0,
mid: Number(stRaw.mid) || 0,
tail: Number(stRaw.tail) || 0,
unknownOffset: Number(stRaw.unknownOffset) || 0,
sizeBuckets: Array.isArray(stRaw.sizeBuckets) ? stRaw.sizeBuckets : [],
gapSplit: stRaw.gapSplit && typeof stRaw.gapSplit === "object" ? stRaw.gapSplit : null,
Expand All @@ -1354,6 +1355,7 @@ export const WEB_CLIENT = `(function () {
if (st.head > 0) divParts.push('<span style="color:#bf8700">' + escapeHtml(t("det.stab_head")) + " <b>" + st.head + "</b></span>");
if (st.append > 0) divParts.push('<span style="color:#57606a">' + escapeHtml(t("det.stab_append")) + " <b>" + st.append + "</b></span>");
if (st.mid > 0) divParts.push('<span style="color:#cf222e">' + escapeHtml(t("det.stab_mid")) + " <b>" + st.mid + "</b></span>");
if (st.tail > 0) divParts.push('<span style="color:#57606a">' + escapeHtml(t("det.stab_tail")) + " <b>" + st.tail + "</b></span>");
if (st.unknownOffset > 0) divParts.push('<span class="dim">' + escapeHtml(t("det.stab_unknown")) + " <b>" + st.unknownOffset + "</b></span>");
let stabBody = '<div class="mono small" style="line-height:1.8">' + escapeHtml(t("det.stab_pairs", { n: st.paired })) + ": " + (st.diverged > 0 ? '<span style="color:' + (st.mid > 0 ? "#cf222e" : "inherit") + '">' + st.diverged + " ↓</span>" : "0") + (divParts.length ? " — " + divParts.join(" · ") : "") + "</div>";
const sb = st.sizeBuckets.filter((b) => b && (Number(b.n) || 0) >= 3);
Expand Down
6 changes: 4 additions & 2 deletions src/web/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,12 @@ export const MESSAGES: Record<Locale, Record<string, string>> = {
"det.seam_events_tip": "相邻出站体的首个字节分叉位置;若可复现,请附上该时间附近的 bili.log(#1548 类问题)",
"det.stab_title": "体稳定性(逐调用字节证明)",
"det.stab_pairs": "相邻调用对 {n}",
"det.stab_tip": "每次出站请求体与上一个已结算请求做 SHA-256 字节比较:identical=逐字节相同(如传输重试);diverged 按首个分叉点分类——head<1KiB 是宿主侧参数变更(model/tools/采样),tail-append 是纯尾部增长,mid 是真实前缀改写,offset-unknown 表示 body 超保留上限(不等式仍由摘要证明)",
"det.stab_tip": "每次出站请求体与上一个已结算请求做 SHA-256 字节比较:identical=逐字节相同(如传输重试);diverged 按首个分叉点分类——head<1KiB 是宿主侧参数变更(model/tools/采样),tail-append 是纯尾部增长,mid 是真实前缀改写,tail 是消息数组结束之后的顶层参数或临时尾注抖动(历史字节未动、缓存不受影响),offset-unknown 表示 body 超保留上限(不等式仍由摘要证明)",
"det.stab_identical": "逐字节相同",
"det.stab_head": "头部<1KiB(参数变更)",
"det.stab_append": "尾部追加",
"det.stab_mid": "中段改写",
"det.stab_tail": "尾部参数/尾注",
"det.stab_unknown": "偏移未知(截断)",
"det.stab_size": "按输入尺寸的命中率(中位)",
"det.stab_gap": "请求间隔中位:低命中(<80%) {a}s vs 高命中 {b}s",
Expand Down Expand Up @@ -734,11 +735,12 @@ export const MESSAGES: Record<Locale, Record<string, string>> = {
"det.seam_events_tip": "First divergence between consecutive outbound bodies; if reproducible attach bili.log around this time (#1548-family)",
"det.stab_title": "Body Stability (per-call byte proof)",
"det.stab_pairs": "{n} adjacent pairs",
"det.stab_tip": "SHA-256 byte comparison of each outbound body against the previous settled request: identical = byte-exact (e.g. transport retry); diverged pairs classified by first divergence — head <1KiB = host-side parameter change (model/tools/sampling), tail-append = pure tail growth, mid-history = a real prefix rewrite, offset-unknown = bodies above the retention cap (inequality still proven by digest)",
"det.stab_tip": "SHA-256 byte comparison of each outbound body against the previous settled request: identical = byte-exact (e.g. transport retry); diverged pairs classified by first divergence — head <1KiB = host-side parameter change (model/tools/sampling), tail-append = pure tail growth, mid-history = a real prefix rewrite, tail = trailing top-level params or ephemeral tail notes at/past the message-array end (history bytes untouched, cache unaffected), offset-unknown = bodies above the retention cap (inequality still proven by digest)",
"det.stab_identical": "byte-identical",
"det.stab_head": "head <1KiB (param change)",
"det.stab_append": "tail-append",
"det.stab_mid": "mid-history rewrite",
"det.stab_tail": "tail (params / ephemeral notes)",
"det.stab_unknown": "offset unknown (capped)",
"det.stab_size": "hit% by input size (median)",
"det.stab_gap": "inter-request gap median: low-hit(<80%) {a}s vs high-hit {b}s",
Expand Down
4 changes: 2 additions & 2 deletions tests/golden/storage-render/anthropic.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@
"foldAt": 9,
"editedAt": 12,
"summaryFrom": 9,
"stateDigest": "844d9a2cb33b5fd245855a2c59d9990bed56e081258979cca424db897388d9da",
"stateDigest": "eb6c69fb93598aeebe0d0e3db9f8d27ff5394431a2647585302a14305da392e7",
"sectionDigests": {
"state": "285aa622a037321b",
"blockContents": "b3a9c59b22ec1a3a",
"stats": "4271a6483e02458a",
"metadata": "4fce29292d9e44ac"
"metadata": "91e1dc401ea4af6c"
},
"stateKeyDigests": {
"blocks": "8a3021f39f1f2eb8",
Expand Down
4 changes: 2 additions & 2 deletions tests/golden/storage-render/chat.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@
"foldAt": 9,
"editedAt": 12,
"summaryFrom": 9,
"stateDigest": "b26f1509735ed4c9ee93d4fd3311d0f7926d8207a1cd4fecf59671c293e2050d",
"stateDigest": "d56dfe9eb890f9768ef1ad962f9d3667109e13cd4705929f8d6c711b53d46116",
"sectionDigests": {
"state": "6e905e4ae1e678b8",
"blockContents": "dd2de296a5bc6160",
"stats": "ed7d3e3f253e0942",
"metadata": "6d4760889050e964"
"metadata": "201a63bff674729d"
},
"stateKeyDigests": {
"blocks": "bdde93b0e100e13e",
Expand Down
4 changes: 2 additions & 2 deletions tests/golden/storage-render/google.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@
"foldAt": 9,
"editedAt": 12,
"summaryFrom": 9,
"stateDigest": "2c70cf418e7d35e8a70774ce2a4341187ed3a00452b67480b742c1a1400e2b87",
"stateDigest": "e4ef707b7d0bbec43a2e87090cc30f19b96331f577a5790c8ec05b1865e3b02f",
"sectionDigests": {
"state": "0725cfe81690b748",
"blockContents": "4cc83a808a659f28",
"stats": "096da9a1f61d84d0",
"metadata": "b7ecdf2a5b9a97b8"
"metadata": "f4a1fae6aca16c5e"
},
"stateKeyDigests": {
"blocks": "9adb53303cd4b786",
Expand Down
Loading
Loading