Skip to content
Open
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
171 changes: 158 additions & 13 deletions src/cache-ledger.ts

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/loop/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { estimateCoreMessages } from "../preflight.js";
import { estimateWireOverhead } from "../server/budget.js";
import { handleAcpStatus } from "../acp-status.js";
import { STATUS_REPORT_CLOSE, STATUS_REPORT_OPEN } from "../acp-panel.js";
import { credentialFingerprint, generationParamFp, handleAcpCache, lastForwardedBody, noteForwardedBody, noteForwardedImageFacts, settleUsageReport } from "../cache-ledger.js";
import { credentialFingerprint, generationParamFp, handleAcpCache, lastForwardedBody, noteForwardedBody, noteForwardedImageFacts, settleUsageReport, toolsArrayFps } from "../cache-ledger.js";
import { billOutputUsage } from "../billing.js";
import { maybeJudgeContinuity } from "../invariant-judge.js";
import type { SeparatorSlot } from "../server/responses-ordering.js";
Expand Down Expand Up @@ -650,7 +650,7 @@ export async function* runCompressLoop(
// noteForwardedBody is a string-only forensics sink; an opaque egress
// envelope (Buffer) is lossy-converted — advisory pairing only, and the
// provenance judge below already skips when the body was rewritten.
noteForwardedBody(ctx.session, typeof wireBodyStr === "string" ? wireBodyStr : wireBodyStr.toString("utf8"), wireArr !== undefined ? wireArr.length : null, credentialFingerprint(requestOptions.headers), generationParamFp(wireObj));
noteForwardedBody(ctx.session, typeof wireBodyStr === "string" ? wireBodyStr : wireBodyStr.toString("utf8"), wireArr !== undefined ? wireArr.length : null, credentialFingerprint(requestOptions.headers), generationParamFp(wireObj), toolsArrayFps(wireObj));
// #564/#2627: provenance is resolved against the array that belongs to
// `wireBodyStr`. Shedding rewrites the body wholesale, so the pre-shed
// `wireArr` no longer describes it; rather than map stale indexes (which
Expand Down
4 changes: 2 additions & 2 deletions src/server/relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { type BiliMessage } from "acp-kernel/wire";
import { absorbToolName, applyAbsorbView } from "../absorb.js";
import { APIG_RESIGN_SCHEME, resignApig } from "../apig-resign.js";
import { noteSuccessfulForward } from "../cache-keepalive.js";
import { credentialFingerprint, generationParamFp, lastForwardedBody, noteClientAbort, noteForwardedBody, noteForwardedImageFacts, noteInboundMessages, noteOutboundContextEstimates, settleOverflowCalibration, settleUsageReport, withCacheCapture } from "../cache-ledger.js";
import { credentialFingerprint, generationParamFp, lastForwardedBody, noteClientAbort, noteForwardedBody, noteForwardedImageFacts, noteInboundMessages, noteOutboundContextEstimates, settleOverflowCalibration, settleUsageReport, toolsArrayFps, withCacheCapture } from "../cache-ledger.js";
import { warnCacheCollapse } from "../cache-warn.js";
import { stampOutbound } from "../chain-checkpoint.js";
import { rewrapCommandcodeBody } from "../commandcode-wire.js";
Expand Down Expand Up @@ -1191,7 +1191,7 @@ const reply = prepared && !prepared.sidePassthrough ? createHandoffReply(prepare
// #3803: the generation-parameter fingerprint rides the SAME full parse as the message count —
// it is extracted from sentParsed (the complete object), never from the capped body head, so a
// trailing-field flip is seen even when the body exceeds SEAM_BODY_CAP.
noteForwardedBody(prepared.session, sentBody, Array.isArray(sentArr) ? (sentArr as unknown[]).length : null, credentialFingerprint(headers), generationParamFp(sentParsed));
noteForwardedBody(prepared.session, sentBody, Array.isArray(sentArr) ? (sentArr as unknown[]).length : null, credentialFingerprint(headers), generationParamFp(sentParsed), toolsArrayFps(sentParsed));
// #3023: the client's own resent view — the protocol conversion of the
// inbound bytes BEFORE processTurn folded/replaced anything, captured
// with the exact same pairing semantics so detectSeam can attribute a
Expand Down
9 changes: 6 additions & 3 deletions src/web/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2539,11 +2539,12 @@ function changelogCatOf(rawLine) {
providerTtl: { count: (raw.providerTtl && Number(raw.providerTtl.count)) || 0, missed: (raw.providerTtl && Number(raw.providerTtl.missed)) || 0 },
rewinds: { count: (raw.rewinds && Number(raw.rewinds.count)) || 0, missed: (raw.rewinds && Number(raw.rewinds.missed)) || 0 },
client: { count: (raw.client && Number(raw.client.count)) || 0, missed: (raw.client && Number(raw.client.missed)) || 0 },
tools: { count: (raw.tools && Number(raw.tools.count)) || 0, missed: (raw.tools && Number(raw.tools.missed)) || 0 },
abortCorrelated: Number(raw.abortCorrelated) || 0,
};
}
function detailSeamActive(seam) {
return !!(seam && (seam.suspects > 0 || seam.missed > 0 || seam.events.length > 0 || seam.providerSide.count > 0 || seam.rewinds.count > 0 || seam.client.count > 0 || seam.abortCorrelated > 0));
return !!(seam && (seam.suspects > 0 || seam.missed > 0 || seam.events.length > 0 || seam.providerSide.count > 0 || seam.rewinds.count > 0 || seam.client.count > 0 || seam.tools.count > 0 || seam.abortCorrelated > 0));
}
// #2489/#3484: zoom-to-selection over the detail charts. The data is already
// client-side, so zooming re-renders the SAME renderer over a filtered slice — no
Expand Down Expand Up @@ -3492,6 +3493,7 @@ if (d.keepalive) kv(parts, t("det.keepalive"), d.keepalive.tierLabel + " · " +
if (seam.providerSide.count > 0) sub.push(legendItem("background:#f0883e", t("det.leg_pside")));
if (seam.rewinds.count > 0) sub.push(legendItem("background:#57606a", t("det.leg_rewind")));
if (seam.client.count > 0) sub.push(legendItem("background:#0969da", t("det.leg_client")));
if (seam.tools.count > 0) sub.push(legendItem("background:#0e7490", t("det.leg_tools")));
sub.push(legendItem("background:#9aa1a8", t("det.leg_rest")));
parts.push('<span style="display:inline-flex;align-items:center;gap:8px;border:1px solid #d0d7de;border-radius:6px;padding:2px 8px;margin:2px 0">'
+ legendItem("background:#8250df", t("det.cause_unattrib"))
Expand Down Expand Up @@ -3655,15 +3657,16 @@ if (d.keepalive) kv(parts, t("det.keepalive"), d.keepalive.tierLabel + " · " +
// tile (label · value · then the parts, top to bottom) — containment
// rendered as layout, not as a caption floating beside the tiles.
const plainUnattrib = () => '<div class="stat"><div class="k">' + t("det.ce_ttl") + '</div><div class="v mono">' + fmtW(tot.ttlRepay || 0) + pctIn(tot.ttlRepay || 0) + "</div></div>";
if (seam && (seam.missed > 0 || seam.providerTtl.count > 0 || seam.providerSide.count > 0 || seam.rewinds.count > 0 || seam.client.count > 0)) {
if (seam && (seam.missed > 0 || seam.providerTtl.count > 0 || seam.providerSide.count > 0 || seam.rewinds.count > 0 || seam.client.count > 0 || seam.tools.count > 0)) {
var uChips = [];
if (seam.missed > 0 || seam.suspects > 0) uChips.push('<span style="color:#cf222e" title="' + escapeHtml(t("det.attr_seam_tip")) + '">' + t("det.attr_seam") + " " + fmtW(seam.missed) + (seam.suspects > 0 ? " ×" + seam.suspects : "") + pctU(seam.missed) + "</span>");
if (seam.providerTtl.missed > 0 || seam.providerTtl.count > 0) uChips.push('<span style="color:#1a7f37" title="' + escapeHtml(t("det.attr_ttl_tip")) + '">' + t("det.attr_ttl") + " " + fmtW(seam.providerTtl.missed) + (seam.providerTtl.count > 0 ? " ×" + seam.providerTtl.count : "") + pctU(seam.providerTtl.missed) + "</span>");
if (seam.providerSide.missed > 0 || seam.providerSide.count > 0) uChips.push('<span style="color:#f0883e" title="' + escapeHtml(t("det.attr_provider_tip")) + '">' + t("det.attr_provider") + " " + fmtW(seam.providerSide.missed) + (seam.providerSide.count > 0 ? " ×" + seam.providerSide.count : "") + pctU(seam.providerSide.missed) + "</span>");
if (seam.rewinds.missed > 0 || seam.rewinds.count > 0) uChips.push('<span style="color:#57606a" title="' + escapeHtml(t("det.attr_rewind_tip")) + '">' + t("det.attr_rewind") + " " + fmtW(seam.rewinds.missed) + (seam.rewinds.count > 0 ? " ×" + seam.rewinds.count : "") + pctU(seam.rewinds.missed) + "</span>");
if (seam.client.missed > 0 || seam.client.count > 0) uChips.push('<span style="color:#0969da" title="' + escapeHtml(t("det.attr_client_tip")) + '">' + t("det.attr_client") + " " + fmtW(seam.client.missed) + (seam.client.count > 0 ? " ×" + seam.client.count : "") + pctU(seam.client.missed) + "</span>");
if (seam.tools.missed > 0 || seam.tools.count > 0) uChips.push('<span style="color:#0e7490" title="' + escapeHtml(t("det.attr_tools_tip")) + '">' + t("det.attr_tools") + " " + fmtW(seam.tools.missed) + (seam.tools.count > 0 ? " ×" + seam.tools.count : "") + pctU(seam.tools.missed) + "</span>");
if (seam.abortCorrelated > 0) uChips.push('<span style="color:#d4a72c" title="' + escapeHtml(t("det.attr_abort_tip")) + '">' + t("det.attr_abort") + " ×" + seam.abortCorrelated + "</span>");
var uRest = Math.max(0, (tot.ttlRepay || 0) - seam.missed - seam.providerTtl.missed - seam.providerSide.missed - seam.rewinds.missed - seam.client.missed);
var uRest = Math.max(0, (tot.ttlRepay || 0) - seam.missed - seam.providerTtl.missed - seam.providerSide.missed - seam.rewinds.missed - seam.client.missed - seam.tools.missed);
if (uRest > 8192) uChips.push('<span style="color:#9aa1a8" title="' + escapeHtml(t("det.attr_rest_tip")) + '">' + t("det.attr_rest", { x: fmtW(uRest) }) + pctU(uRest) + "</span>");
if (uChips.length) {
parts.push('<div class="stat"><div class="k">' + t("det.ce_ttl") + '</div><div class="v mono">' + fmtW(tot.ttlRepay || 0) + pctIn(tot.ttlRepay || 0) + '</div>'
Expand Down
6 changes: 6 additions & 0 deletions src/web/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ export const MESSAGES: Record<Locale, Record<string, string>> = {
"det.leg_pside": "▲ 上游侧失效",
"det.leg_rewind": "↩ 历史回退",
"det.leg_client": "✎ 客户端中段改写",
"det.leg_tools": "🔧 工具表变更",
"det.leg_rest": "其余",
"det.fold_short": "折叠",
"det.rec_title": "压缩回收(推荐 vs 实际折叠)",
Expand Down Expand Up @@ -474,11 +475,13 @@ export const MESSAGES: Record<Locale, Record<string, string>> = {
"det.attr_ttl_tip": "与上一请求间隔超过 10 分钟后,字节稳定/纯追加形态仍 miss——上游前缀缓存已正常过期,属预期损耗,无需处理",
"det.attr_rewind": "↩ 历史回退",
"det.attr_client": "✎ 客户端中段改写",
"det.attr_tools": "🔧 工具表变更",
"det.attr_abort": "⏻ 中断相关",
"det.attr_seam_tip": "出站请求体在历史中段出现字节分叉,且无 fold/模型切换/重启归因——疑似重建缝。数值为嫌疑样本重计费的 token。若此前几分钟内发生过折叠,多半是折叠后客户端回显的重付(#2929 后已归入折叠后重建)",
"det.attr_provider_tip": "与上一请求间隔很短,且整条消息列表逐字节保留(整体一致,或前缀一致、仅尾部追加新消息;超过 512KiB 取证上限的请求以记录头稳定 + 精确消息数增长判定)却仍 miss——异常:上游淘汰/中继节点轮换,非 bili 重建缝;长间隔的同类样本归入「上游 TTL 过期」",
"det.attr_rewind_tip": "客户端 revert/trim 使消息数下降——合法的一次性重计费(保留前缀),非缺陷",
"det.attr_client_tip": "客户端自己重发的历史在中段分叉(插入/改写,如宿主其他插件注入、粘贴挂载、重渲染)——入站消息指纹对在该索引处失配,bili 的忠实重建把这个改动带到了线上。客户端自担的重计费,非 bili 重建缝(#3023)",
"det.attr_tools_tip": "请求的 tools 数组在中段变化(新增/移除/重定义/重排)——Anthropic 线上前缀是 system + tools + messages,工具表先于消息,故缓存缝断在工具边界而非消息。宿主自担的重计费(插件晚注册 / 动态开关工具),非 bili 重建缝(#5466)",
"det.attr_abort_tip": "客户端流中 abort 后 30s 内结算的 miss 样本数——重试重写重发尾部;相关性而非因果,叠加标记不计入分桶",
"det.attr_restart": "⟳ 重启丢窗",
"det.attr_restart_tip": "bili 重启或会话恢复后,账本窗口断裂:重启后的首个请求无法与重启前样本配对,miss 按首账计费(初始内容)",
Expand Down Expand Up @@ -1207,6 +1210,7 @@ export const MESSAGES: Record<Locale, Record<string, string>> = {
"det.leg_pside": "▲ provider-side miss",
"det.leg_rewind": "↩ rewind",
"det.leg_client": "✎ client rewrite",
"det.leg_tools": "🔧 tools change",
"det.leg_rest": "rest",
"det.fold_short": "fold",
"det.rec_title": "compression recovery (recommended vs folded)",
Expand Down Expand Up @@ -1274,11 +1278,13 @@ export const MESSAGES: Record<Locale, Record<string, string>> = {
"det.attr_ttl_tip": "byte-stable / append-shaped misses settled more than 10 minutes after the previous sample — the upstream prefix cache simply aged out; expected cost, no action needed",
"det.attr_rewind": "↩ History rewound",
"det.attr_client": "✎ Client mid-history rewrite",
"det.attr_tools": "🔧 Tools-array change",
"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. If a fold landed minutes earlier this is usually the post-fold client-echo re-bill (fold-owned since #2929)",
"det.attr_provider_tip": "Arrived shortly after the previous request, whose entire message list came back byte-identical (whole body, or its prefix with only tail messages appended; above the 512 KiB forensics cap, proven by the stable recorded head plus exact message-count growth) yet the upstream did not serve its cache — abnormal: eviction / relay node rotation. Long-gap siblings are booked under upstream TTL expiry",
"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_client_tip": "The client's own resent history diverged mid-list (insert/rewrite — another host plugin, paste hoisting, re-render): the inbound message-fingerprint pair mismatches at that index, and bili's faithful rebuild carried the change to the wire. Client-owned re-bill, not a bili rebuild seam (#3023)",
"det.attr_tools_tip": "The request's tools array changed mid-session (added / removed / redefined / reordered) — on the Anthropic wire the prefix is system + tools + messages, so the tool table precedes messages and the cached span breaks at the tool boundary, not a message. Host-owned re-bill (late plugin registration / dynamic tool toggle), not a bili rebuild seam (#5466)",
"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_restart": "⟳ restart drop",
"det.attr_restart_tip": "After a bili restart or session restore the ledger window breaks: the first post-restart request cannot pair with pre-restart samples, so its miss is billed as a first bill (initial content)",
Expand Down
Loading
Loading