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
26 changes: 21 additions & 5 deletions src/cache-ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -375,6 +377,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;
Expand Down Expand Up @@ -1023,8 +1039,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");
Expand Down
4 changes: 2 additions & 2 deletions src/web/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export const MESSAGES: Record<Locale, Record<string, string>> = {
"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 内,不重复计数",
Expand Down Expand Up @@ -450,7 +450,7 @@ export const MESSAGES: Record<Locale, Record<string, string>> = {
"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",
Expand Down
78 changes: 77 additions & 1 deletion tests/cache-seam-detector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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"]));
Expand Down
Loading