From 8b4f65d544a7e47d3de1b8d5debf284904e8ad54 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sun, 4 Oct 2026 16:57:45 +0800 Subject: [PATCH 1/2] fix(cache-ledger): attribute tail-append misses to provider-side, not seam suspects (#2059) detectSeam's upstream-side test demanded the whole body match byte-for-byte (lcpBytes >= cur.length), which a growing conversation never satisfies because each turn appends new messages at the tail. So the common 'tail append + upstream did not serve its cache' case fell through to a false seam-suspect (CACHE SEAM), while the stronger signal seamLcp already computes -- msgIndex == prevMsgs, i.e. the previous request's entire message list survives byte-identically as this request's prefix -- went unused. When msgIndex === prevMsgs and curMsgs > prevMsgs (a pure tail growth) and both bodies are within SEAM_BODY_CAP (a truncated body parses to zero messages and would fake the msgIndex == prevMsgs == 0 signature), book the miss as providerSideMisses instead of seamSuspects. 0 < msgIndex < prevMsgs (real mid-history break) and msgIndex == 0 (head break) keep their original seam-suspect classification, pinned by tests. No forwarded bytes change -- pure observation layer. PROVIDER-SIDE MISS report wording updated to cover both the exact-resend and tail-append cases. --- src/cache-ledger.ts | 18 ++++++- tests/cache-seam-detector.test.ts | 78 ++++++++++++++++++++++++++++++- 2 files changed, 93 insertions(+), 3 deletions(-) diff --git a/src/cache-ledger.ts b/src/cache-ledger.ts index 948b6beefe..5a6f5bdd66 100644 --- a/src/cache-ledger.ts +++ b/src/cache-ledger.ts @@ -375,6 +375,20 @@ function detectSeam(session: Session, led: CacheLedger): void { agg.providerSideMissed += line.tr; return; } + // #2059: tail-append turns — the previous request's ENTIRE message list + // survives byte-identically as this one's prefix (msgIndex == prevMsgs) + // and only the tail grew, so there is NO mid-history break: provider-side + // again. The byte-LCP test above can't see it (it demands the whole body + // match, which a growing conversation never satisfies), so before this the + // common append+no-cache case falsely cried seam. Both bodies must be + // uncapped: one truncated at SEAM_BODY_CAP parses to zero messages and + // would fake the msgIndex == prevMsgs == 0 signature; prevMsgs > 0 keeps + // an empty prior list out. + if (prev.length < SEAM_BODY_CAP && cur.length < SEAM_BODY_CAP && f.prevMsgs > 0 && f.msgIndex === f.prevMsgs && f.curMsgs > f.prevMsgs) { + agg.providerSideMisses += 1; + agg.providerSideMissed += line.tr; + return; + } } agg.seamSuspects += 1; agg.seamMissed += line.tr; @@ -1023,8 +1037,8 @@ function formatSeam(r: BiliCacheReport): string { out.push(` ${r.seam.rewinds.count} sample(s) · ${fmtTok(r.seam.rewinds.missed)} tok re-billed once for the retained prefix — sanctioned client intent, not a rebuild seam`); } if (r.seam.providerSide.count > 0) { - out.push("▲ PROVIDER-SIDE MISS (wire was byte-stable)"); - out.push(` ${r.seam.providerSide.count} sample(s) · ${fmtTok(r.seam.providerSide.missed)} tok — the outbound body matched the previous request's prefix; the upstream did not serve its cache (TTL expiry / eviction / relay node rotation). Not a bili rebuild seam.`); + 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 entire message list is byte-for-byte a prefix of this request (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.`); } if (r.seam.abortCorrelated > 0) { out.push("⏻ ABORT-CORRELATED"); diff --git a/tests/cache-seam-detector.test.ts b/tests/cache-seam-detector.test.ts index 31f0a7cddb..1e2884f1e9 100644 --- a/tests/cache-seam-detector.test.ts +++ b/tests/cache-seam-detector.test.ts @@ -120,7 +120,10 @@ test("seam events are bounded (ring keeps the last 8)", () => { } const led = getCacheLedger(s); assert.equal(led.seamEvents?.length, 8, "bounded ring"); - assert.equal(led.agg.seamSuspects, 12, "aggregate counts all"); + // #2059: the first transition (["a"] -> ["a","v0"]) is a pure tail append and is + // now attributed provider-side; the remaining 11 mutate slot 1 (mid-history breaks). + assert.equal(led.agg.seamSuspects, 11, "aggregate counts all seam-suspects"); + assert.equal(led.agg.providerSideMisses, 1, "the single append is provider-side"); }); test("seam detector: client rewind (fewer messages) attributes to HISTORY REWOUND, not a seam", () => { @@ -153,6 +156,79 @@ test("seam detector: byte-stable resend attributes to PROVIDER-SIDE MISS, not a assert.match(handleAcpCache(s), /PROVIDER-SIDE MISS/); }); +test("seam detector: tail-append turn attributes to PROVIDER-SIDE MISS, not a seam (#2059)", () => { + const s = makeSession(); + noteForwardedBody(s, body(["a", "b"])); + settle(s, T0, 100_000, 99_000); + // Next turn APPENDS one message; the previous list is a byte-identical prefix, + // upstream gave no cache. The whole-body LCP test can't see this (the body grew), + // so pre-fix it falsely cried seam. + noteForwardedBody(s, body(["a", "b", "c"])); + settle(s, T0 + 1000, 100_000, 20_000); + const led = getCacheLedger(s); + assert.equal(led.agg.providerSideMisses, 1, "tail-append miss is provider-side"); + assert.ok(led.agg.providerSideMissed > 0); + assert.equal(led.agg.seamSuspects, 0, "tail-append must not cry seam"); + assert.notEqual(led.lines[led.lines.length - 1]!.seam, 1); + const text = handleAcpCache(s); + assert.match(text, /PROVIDER-SIDE MISS/); + assert.ok(!/CACHE SEAM \(/.test(text), "no seam section for a pure tail-append miss"); +}); + +test("seam detector: multi-message tail append also attributes to PROVIDER-SIDE MISS (#2059)", () => { + const s = makeSession(); + noteForwardedBody(s, body(["a", "b"])); + settle(s, T0, 100_000, 99_000); + noteForwardedBody(s, body(["a", "b", "c", "d"])); + settle(s, T0 + 1000, 100_000, 20_000); + const led = getCacheLedger(s); + assert.equal(led.agg.providerSideMisses, 1); + assert.equal(led.agg.seamSuspects, 0, "appending several messages is still a pure tail growth"); +}); + +test("seam detector: a REAL mid-history break (0 < msgIndex < prevMsgs) stays a seam-suspect (#2059)", () => { + const s = makeSession(); + noteForwardedBody(s, body(["a", "b", "c"])); + settle(s, T0, 100_000, 99_000); + // Diverges at messages[1]: a genuine mid-history rewrite, NOT a tail append — + // the #2059 reclassification must not swallow it. + noteForwardedBody(s, body(["a", "B2", "c"])); + settle(s, T0 + 1000, 100_000, 20_000); + const led = getCacheLedger(s); + assert.equal(led.agg.seamSuspects, 1, "mid-history break stays a seam-suspect"); + assert.equal(led.agg.providerSideMisses, 0, "must not be swallowed into provider-side"); + assert.equal(led.lines[led.lines.length - 1]!.seam, 1); + assert.equal(led.seamEvents?.[0]?.msgIndex, 1); +}); + +test("seam detector: a head break (msgIndex == 0) stays a seam-suspect (#2059)", () => { + const s = makeSession(); + noteForwardedBody(s, body(["a", "b"])); + settle(s, T0, 100_000, 99_000); + // The very first message diverges — not a tail append, must stay flagged. + noteForwardedBody(s, body(["A", "b"])); + settle(s, T0 + 1000, 100_000, 20_000); + const led = getCacheLedger(s); + assert.equal(led.agg.seamSuspects, 1, "head break stays a seam-suspect"); + assert.equal(led.agg.providerSideMisses, 0); + assert.equal(led.lines[led.lines.length - 1]!.seam, 1); + assert.equal(led.seamEvents?.[0]?.msgIndex, 0); +}); + +test("seam detector: an over-cap prior body keeps an append miss a seam-suspect, never a false provider-side (#2059)", () => { + const s = makeSession(); + // Over-cap prior body is stored truncated → parses to zero messages, so the + // message-level forensics are untrustworthy; the guard must keep it out of the + // provider-side reclassification. + noteForwardedBody(s, JSON.stringify({ model: "m", messages: [{ role: "user", content: "x".repeat(600 * 1024) }] })); + settle(s, T0, 100_000, 99_000); + noteForwardedBody(s, body(["q", "r"])); + settle(s, T0 + 1000, 100_000, 20_000); + const led = getCacheLedger(s); + assert.equal(led.agg.seamSuspects, 1, "truncated-pair miss stays a seam-suspect"); + assert.equal(led.agg.providerSideMisses, 0, "truncated bodies must not be read as provider-side"); +}); + test("seam detector: abort correlation marks missed samples near a client abort", () => { const s = makeSession(); noteForwardedBody(s, body(["a", "b"])); From d11fa2074284585c908ca0ffbeb32e39593419b7 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sun, 4 Oct 2026 17:14:51 +0800 Subject: [PATCH 2/2] docs(cache-ledger): sync provider-side bucket wording with the widened criterion (#2059) Labeled in-passing fix from review of this PR: after the tail-append reclassification, providerSideMisses also covers samples whose bodies were NOT fully byte-stable (message list preserved, tail appended), so the old 'wire was byte-stable' wording in the web-panel tip (zh+en) and the providerSideMisses doc comment no longer matched the code fact. Updated to state the actual criterion. --- src/cache-ledger.ts | 8 +++++--- src/web/i18n.ts | 4 ++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/cache-ledger.ts b/src/cache-ledger.ts index 5a6f5bdd66..33e71099c9 100644 --- a/src/cache-ledger.ts +++ b/src/cache-ledger.ts @@ -124,9 +124,11 @@ export interface CacheLedger { nbInput: number; seamSuspects: number; seamMissed: number; - /** #1592 follow-up: misses whose current body was byte-stable vs the - * previous request — the upstream simply did not serve its cache - * (TTL expiry / eviction / relay node rotation). Not a rebuild seam. */ + /** #1592 follow-up: misses with no client-side prefix break — the current + * body is byte-stable vs the previous request, or the previous message + * list comes back byte-identical as a prefix with only tail messages + * appended (#2059). Either way the upstream simply did not serve its + * cache (TTL expiry / eviction / relay node rotation). Not a rebuild seam. */ providerSideMisses: number; providerSideMissed: number; /** #1592 follow-up: misses right after the client rewound history diff --git a/src/web/i18n.ts b/src/web/i18n.ts index 22f79e2219..8a49a938fb 100644 --- a/src/web/i18n.ts +++ b/src/web/i18n.ts @@ -152,7 +152,7 @@ export const MESSAGES: Record> = { "det.attr_rewind": "↩ 历史回退", "det.attr_abort": "⏻ 中断相关", "det.attr_seam_tip": "出站请求体在历史中段出现字节分叉,且无 fold/模型切换/重启归因——疑似重建缝。数值为嫌疑样本重计费的 token", - "det.attr_provider_tip": "相邻出站体字节稳定(前缀匹配上一请求)却仍 miss——上游 TTL 过期/淘汰/中继节点轮换,非 bili 重建缝", + "det.attr_provider_tip": "上一请求的整条消息列表逐字节相同(整体一致,或其前缀一致、仅尾部追加新消息)却仍 miss——上游 TTL 过期/淘汰/中继节点轮换,非 bili 重建缝", "det.attr_rewind_tip": "客户端 revert/trim 使消息数下降——合法的一次性重计费(保留前缀),非缺陷", "det.attr_abort_tip": "客户端流中 abort 后 30s 内结算的 miss 样本数——重试重写重发尾部;相关性而非因果,叠加标记不计入分桶", "det.attr_note": "接缝 / 上游侧 / 回退 互斥分区未解释残差的一部分,中断为叠加标记——均不含在上方 TOTAL 内,不重复计数", @@ -450,7 +450,7 @@ export const MESSAGES: Record> = { "det.attr_rewind": "↩ History rewound", "det.attr_abort": "⏻ Abort-correlated", "det.attr_seam_tip": "Outbound bodies diverged mid-history with no fold/model-switch/restart attribution — suspected rebuild seam. Value = tokens re-billed on suspect samples", - "det.attr_provider_tip": "Wire was byte-stable (prefix of the previous request body) yet the upstream did not serve its cache — TTL expiry / eviction / relay node rotation. Not a bili rebuild seam", + "det.attr_provider_tip": "The previous request's entire message list came back byte-identical (whole body, or its prefix with only tail messages appended) yet the upstream did not serve its cache — TTL expiry / eviction / relay node rotation. Not a bili rebuild seam", "det.attr_rewind_tip": "Client revert/trim shrank the message count — sanctioned one-time re-bill of the retained prefix, not a defect", "det.attr_abort_tip": "Missed samples settled within 30s of a client mid-stream abort — retry churn rewrites the resent tail; correlation, not causation (overlay marker, not a bucket)", "det.attr_note": "seam / provider-side / rewound exclusively partition part of the unexplained residual; abort-correlated is an overlay — none of it is counted in TOTAL above",