diff --git a/kernel/package-lock.json b/kernel/package-lock.json index b414754c41..baab1246cd 100644 --- a/kernel/package-lock.json +++ b/kernel/package-lock.json @@ -1,12 +1,12 @@ { "name": "acp-kernel", - "version": "0.0.114", + "version": "0.0.120", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "acp-kernel", - "version": "0.0.114", + "version": "0.0.120", "license": "MIT", "devDependencies": { "@types/node": "^22.0.0", @@ -508,9 +508,6 @@ "x64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -612,9 +609,6 @@ "arm" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -629,9 +623,6 @@ "arm" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -646,9 +637,6 @@ "arm64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -663,9 +651,6 @@ "arm64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -680,9 +665,6 @@ "loong64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -697,9 +679,6 @@ "loong64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -714,9 +693,6 @@ "ppc64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -731,9 +707,6 @@ "ppc64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -748,9 +721,6 @@ "riscv64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -765,9 +735,6 @@ "riscv64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -782,9 +749,6 @@ "s390x" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -799,9 +763,6 @@ "x64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -816,9 +777,6 @@ "x64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ diff --git a/kernel/package.json b/kernel/package.json index 4ea152f765..e85bec6008 100644 --- a/kernel/package.json +++ b/kernel/package.json @@ -1,6 +1,6 @@ { "name": "acp-kernel", - "version": "0.0.119", + "version": "0.0.120", "description": "Framework-agnostic context-compression engine (model-driven, 3-tier LSM). Pure core: no host dependency.", "license": "MIT", "author": "ranxianglei", diff --git a/kernel/src/cache-report.ts b/kernel/src/cache-report.ts index 47c46a55d0..97e062ca3a 100644 --- a/kernel/src/cache-report.ts +++ b/kernel/src/cache-report.ts @@ -63,6 +63,17 @@ export interface PriceProfile { q?: number; } +/** #2929: a fold claims not only its first following sample but the whole + * post-fold COLD TAIL — until a warm line proves the provider re-cached or + * this many samples have elapsed (#1847 switch discipline, mirrored for + * folds). Rationale: the first post-fold request (the proxy's re-request) + * is attributed today, but the client-echo re-bill 25–66s later carried no + * fold stamp and fell into the unattributed TTL residual, tripping the + * cache-seam detector with "no fold attribution". Warm hit that retires + * the window (same threshold as the host's WARM_HIT_PCT). */ +export const FOLD_CLAIM_WARM_HIT_PCT = 85; +export const FOLD_CLAIM_COLD_ROUNDS = 4; + export interface SampleDecomposition { missed: number; newContent: number; @@ -331,24 +342,49 @@ export function buildCacheReport( type Dec = SampleDecomposition & { foldSeq: number | null }; const decs: Dec[] = []; + // #2929: per-fold cold-tail claim state. A fold claims its FIRST following + // sample unconditionally (the materialization re-bill), then keeps claiming + // subsequent cold samples until a warm line (>= FOLD_CLAIM_WARM_HIT_PCT) + // proves re-cache or it has claimed FOLD_CLAIM_COLD_ROUNDS samples. A warm + // sample is never claimed as a continuation (it proves the damage is over). + const foldClaims: number[] = folds.map(() => 0); + const foldRetired: boolean[] = folds.map(() => false); for (let i = 0; i < samples.length; i++) { const s = samples[i]!; const prev = i > 0 ? samples[i - 1]! : null; - const prevAt = prev ? prev.at : Number.NEGATIVE_INFINITY; + const warm = s.input > 0 && hitPct(s.input, s.cached) >= FOLD_CLAIM_WARM_HIT_PCT; const pending: FoldEvent[] = []; + const pendingIdx: number[] = []; let pendingBase = 0; for (let j = 0; j < folds.length; j++) { const fj = folds[j]!; - if (fj.at <= prevAt) continue; if (fj.at > s.at) break; - if (pending.length === 0) pendingBase = j; - pending.push(fj); + if (foldRetired[j]) continue; + // First claim is unconditional; a warm continuation proves re-cache and + // is not the fold's damage anymore. + if (foldClaims[j] === 0 || !warm) { + if (pending.length === 0) pendingBase = j; + pending.push(fj); + pendingIdx.push(j); + } } const d = decomposeSample(prev, s, pending); decs.push({ ...d, foldSeq: d.foldIndex !== null ? pendingBase + d.foldIndex + 1 : null, }); + for (const j of pendingIdx) { + foldClaims[j] = foldClaims[j]! + 1; + if (foldClaims[j]! >= FOLD_CLAIM_COLD_ROUNDS) foldRetired[j] = true; + } + // A warm line retires EVERY open window — claimed or not this sample + // (#1847 discipline: it proves the provider re-cached, so a later cold + // blip is a new, unrelated event and must not be swallowed). + if (warm) { + for (let j = 0; j < folds.length; j++) { + if (folds[j]!.at <= s.at) foldRetired[j] = true; + } + } } const totals: CacheTotals = { diff --git a/kernel/src/index.ts b/kernel/src/index.ts index eed5390fb5..7474e13930 100644 --- a/kernel/src/index.ts +++ b/kernel/src/index.ts @@ -130,6 +130,8 @@ export { buildCacheReport, computeFoldEconomics, decomposeSample, + FOLD_CLAIM_COLD_ROUNDS, + FOLD_CLAIM_WARM_HIT_PCT, formatCacheReport, summarizeFoldEconomics, } from "./cache-report.js"; diff --git a/kernel/tests/cache-report.test.ts b/kernel/tests/cache-report.test.ts index 49b250c46e..0dacf7fdc4 100644 --- a/kernel/tests/cache-report.test.ts +++ b/kernel/tests/cache-report.test.ts @@ -4,6 +4,7 @@ import { buildCacheReport, computeFoldEconomics, decomposeSample, + FOLD_CLAIM_COLD_ROUNDS, formatCacheReport, type CacheSample, type FoldEvent, @@ -541,3 +542,71 @@ test("formatCacheReport summary lists every fold when few, with TTL spike idle g assert.match(text, /#1 .*S=20\.0K.* → (NOT PAID BACK|PAID BACK|\?)/); assert.ok(!text.includes("more folds omitted")); }); + +test("#2929 fold cold-tail claim: the client-echo re-bill is fold-owned until a warm line retires", () => { + // Degenerate head fold (X=3K): proxy re-request (s2) is claimed today, the + // client echo (s3) and the cold recovery tail (s4) must ALSO carry foldSeq — + // before #2929 s3/s4 fell into the unattributed ttlRepay residual and tripped + // the cache-seam detector. A warm line (s5) retires the window; a later cold + // blip (s6, real provider eviction) is NOT swallowed by the old fold. + const samples = [ + sample(0, 100_000, 99_000), // warm baseline + sample(1, 51_000, 5_000), // proxy re-request after fold: 9.8% + sample(2, 55_000, 5_100), // client echo re-bill: 9.3% + sample(3, 60_000, 42_000), // recovery: 70% (still < 85 warm line) + sample(4, 65_000, 60_000), // warm: 92% — retires the claim window + sample(5, 66_000, 30_000), // unrelated later cold blip: 45% + ]; + const folds: FoldEvent[] = [ + { at: T0 + 0.5 * min, tokensCompressed: 50_000, firstFoldStartTokens: 3_000, viewBefore: 100_000, viewAfter: 50_500 }, + ]; + const r = buildCacheReport(samples, folds); + const [, s2, s3, s4, s5, s6] = r.lines; + assert.equal(s2!.foldSeq, 1, "re-request claimed (first claim, unconditional)"); + assert.equal(s2!.compRepay, 46_000); + assert.equal(s3!.foldSeq, 1, "client-echo re-bill claimed (#2929)"); + assert.equal(s3!.compRepay, 45_900); + assert.equal(s3!.ttlRepay, 0, "echo residual no longer lands in the unattributed bucket"); + assert.equal(s4!.foldSeq, 1, "cold recovery tail claimed"); + assert.equal(s4!.compRepay, 13_000); + assert.equal(s5!.foldSeq, null, "warm line is never claimed as a continuation"); + assert.equal(s5!.compRepay, 0); + assert.equal(s6!.foldSeq, null, "window closed — later cold blip is NOT the fold's"); + assert.equal(s6!.ttlRepay, 35_000, "blip falls to the unattributed residual"); + assert.equal(r.totals.ttlRepay, 36_000, "1K baseline residual + the 35K post-window blip"); + assert.equal(r.folds[0]!.T, 46_000 + 45_900 + 13_000, "fold economics absorb the full cold tail"); +}); + +test("#2929 fold cold-tail claim: FOLD_CLAIM_COLD_ROUNDS bounds the window without a warm line", () => { + const samples = [ + sample(0, 100_000, 99_000), + sample(1, 51_000, 5_000), + sample(2, 52_000, 5_100), + sample(3, 53_000, 5_200), + sample(4, 54_000, 5_300), + sample(5, 55_000, 5_400), // 5th post-fold sample: window exhausted + ]; + const folds: FoldEvent[] = [ + { at: T0 + 0.5 * min, tokensCompressed: 50_000, firstFoldStartTokens: 3_000, viewBefore: 100_000, viewAfter: 50_500 }, + ]; + const r = buildCacheReport(samples, folds); + const claimCount = r.lines.filter((l) => l.foldSeq === 1).length; + assert.equal(claimCount, FOLD_CLAIM_COLD_ROUNDS, "exactly the bound — never an open-ended claim"); + assert.equal(r.lines[5]!.foldSeq, null); + assert.ok(r.lines[5]!.ttlRepay > 0, "post-window residual is unattributed again"); +}); + +test("#2929 fold cold-tail claim: a warm FIRST post-fold sample claims once and retires", () => { + const samples = [ + sample(0, 100_000, 99_000), + sample(1, 51_000, 50_000), // 98% warm immediately after a tiny fold + sample(2, 52_000, 20_000), // later cold blip must not be swallowed + ]; + const folds: FoldEvent[] = [ + { at: T0 + 0.5 * min, tokensCompressed: 5_000, firstFoldStartTokens: 3_000, viewBefore: 100_000, viewAfter: 95_500 }, + ]; + const r = buildCacheReport(samples, folds); + assert.equal(r.lines[1]!.foldSeq, 1, "first claim is unconditional even on a warm line"); + assert.equal(r.lines[2]!.foldSeq, null, "retired by the warm first sample"); + assert.ok(r.lines[2]!.ttlRepay > 0); +}); diff --git a/package-lock.json b/package-lock.json index c100cdea77..ea44ceb69b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -48,7 +48,7 @@ }, "kernel": { "name": "acp-kernel", - "version": "0.0.119", + "version": "0.0.120", "dev": true, "license": "MIT", "devDependencies": { diff --git a/src/cache-ledger.ts b/src/cache-ledger.ts index 3c966b112a..ba11826d09 100644 --- a/src/cache-ledger.ts +++ b/src/cache-ledger.ts @@ -2,6 +2,8 @@ import { createHash, randomUUID } from "node:crypto"; import { computeFoldEconomics, decomposeSample, + FOLD_CLAIM_COLD_ROUNDS, + FOLD_CLAIM_WARM_HIT_PCT, formatCacheReport, summarizeFoldEconomics, type CacheReport, @@ -54,6 +56,14 @@ interface LedgerFold { at: number; S: number; sigma: number; + /** #2929: samples this fold has claimed under the cold-tail window + * (first claim unconditional, then cold continuations until warm or + * FOLD_CLAIM_COLD_ROUNDS). Sparse: absent on ledgers persisted before + * #2929 — treated as 0 claims, which reproduces the first-claim path. */ + claims?: number; + /** #2929: claim window closed (warm line proved re-cache, or the cold-round + * bound elapsed). Sparse: absent = open. */ + retired?: boolean; X?: number; V?: number; Vp?: number; @@ -99,6 +109,13 @@ interface LedgerLine { /** #1592 follow-up: 1 iff this sample settled within 30s of a client * mid-stream abort in the same session (abort/retry churn correlation). */ abortedNear?: 1; + /** #2929: which attribution arm detectSeam booked this sample's residual + * into — "ttl" (long-idle byte-stable miss, normal ageing), "side" + * (short-idle byte-stable miss, abnormal provider failure) or "rewind" + * (client revert/trim re-bill). Sparse: omitted unless booked. The seam + * arm sets `seam` above instead. Lets the web trajectory chart paint the + * same bucket membership the economics card aggregates. */ + prov?: "ttl" | "side" | "rewind"; /** #1535: 1 iff `model` differs from the previous sample's KNOWN model * (unknown sides never flag); marks the re-billed stable prefix on the * first request after a model switch. Sparse: omitted unless set. */ @@ -229,6 +246,11 @@ interface CacheLedger { * cache (TTL expiry / eviction / relay node rotation). Not a rebuild seam. */ providerSideMisses: number; providerSideMissed: number; + /** #2929: the provider-side arm split by idle gap — samples settled more + * than PROVIDER_TTL_GAP_MS after the previous sample are normal TTL + * expiry, not an abnormal provider failure. */ + providerTtlMisses: number; + providerTtlMissed: number; /** #1592 follow-up: misses right after the client rewound history * (revert/trim — fewer message elements than the previous request). * Sanctioned client intent; recorded so the one-time re-bill is @@ -316,6 +338,11 @@ export interface SeamEvent { const SEAM_BODY_CAP = 512 * 1024; const SEAM_EVENTS_CAP = 8; +/** Idle gap past which a byte-stable/append-shaped miss reads as NORMAL upstream + * TTL expiry rather than an abnormal provider-side failure (#2929 taxonomy: + * cold start | normal TTL expiry | abnormal provider miss). Mirrors the web + * chart's GAP_MS. */ +const PROVIDER_TTL_GAP_MS = 600_000; // #2131 reachability: full-body retention cap for per-call body-stability // forensics. Bodies above it are digest-only — SHA-256 still proves byte @@ -559,6 +586,21 @@ function detectSeam(session: Session, led: CacheLedger): void { const agg = led.agg; const cur = session.transient?.sentBody; const prev = session.transient?.settledBody; + // #2929: split the provider-side arms by idle gap — a byte-stable miss + // after a long idle is normal TTL expiry; the same miss seconds after the + // previous request is the abnormal provider-side failure. + const prevSampleAt = led.lines.length >= 2 ? led.lines[led.lines.length - 2].at : line.at; + const ttlExpired = line.at - prevSampleAt > PROVIDER_TTL_GAP_MS; + const bookProvider = (): void => { + line.prov = ttlExpired ? "ttl" : "side"; + if (ttlExpired) { + agg.providerTtlMisses += 1; + agg.providerTtlMissed += line.tr; + } else { + agg.providerSideMisses += 1; + agg.providerSideMissed += line.tr; + } + }; let f: ReturnType | undefined; if (cur !== undefined && prev !== undefined) { f = seamLcp(prev.str, cur.str); @@ -573,6 +615,7 @@ function detectSeam(session: Session, led: CacheLedger): void { // clipped at SEAM_BODY_CAP parses to zero messages and would fake a // shrink (or hide one); those pairs go to the #2131 byte/count arms. if (!prevCapped && !curCapped && f.prevMsgs > 0 && f.curMsgs < f.prevMsgs) { + line.prov = "rewind"; agg.rewinds += 1; agg.rewindMissed += line.tr; return; @@ -584,6 +627,7 @@ function detectSeam(session: Session, led: CacheLedger): void { // a sanctioned client-side trim/fold re-bill of the retained prefix, // NOT a provider miss. Must run before the byte-stable arm, which // would otherwise read "all of cur survives" and book it provider-side. + line.prov = "rewind"; agg.rewinds += 1; agg.rewindMissed += line.tr; return; @@ -593,8 +637,7 @@ function detectSeam(session: Session, led: CacheLedger): void { // upstream simply did not serve its cache. Provider-side. Above // the cap this covers the identical-recorded-head case: anything // past SEAM_BODY_CAP is beyond recorded evidence either way. - agg.providerSideMisses += 1; - agg.providerSideMissed += line.tr; + bookProvider(); return; } // #2059: tail-append turns — the previous request's ENTIRE message list @@ -607,8 +650,7 @@ function detectSeam(session: Session, led: CacheLedger): void { // would fake the msgIndex == prevMsgs == 0 signature; prevMsgs > 0 keeps // an empty prior list out. if (!prevCapped && !curCapped && f.prevMsgs > 0 && f.msgIndex === f.prevMsgs && f.curMsgs > f.prevMsgs) { - agg.providerSideMisses += 1; - agg.providerSideMissed += line.tr; + bookProvider(); return; } if (prev.str.length < cur.str.length && f.lcpBytes >= prev.str.length - 4) { @@ -621,8 +663,7 @@ function detectSeam(session: Session, led: CacheLedger): void { // growth. Without them the pair is indistinguishable from a real // break and falls through to suspect. if (prev.msgs !== null && cur.msgs !== null && cur.msgs > prev.msgs) { - agg.providerSideMisses += 1; - agg.providerSideMissed += line.tr; + bookProvider(); return; } } @@ -660,7 +701,7 @@ export function getCacheLedger(session: Session): CacheLedger { "restartDropMissed", "attributedMissed", "unknownSamples", "unknownInput", "nbSamples", "nbInput", "seamSuspects", "seamMissed", - "providerSideMisses", "providerSideMissed", "rewinds", "rewindMissed", "abortCorrelated", + "providerSideMisses", "providerSideMissed", "providerTtlMisses", "providerTtlMissed", "rewinds", "rewindMissed", "abortCorrelated", ] as const) { if (typeof g[key] !== "number") g[key] = 0; } @@ -677,7 +718,7 @@ export function getCacheLedger(session: Session): CacheLedger { foldSeqCounter: 0, folds: [], lines: [], - agg: { requests: 0, input: 0, cached: 0, output: 0, nc: 0, cr: 0, tr: 0, switches: 0, switchMissed: 0, keySwitches: 0, keySwitchMissed: 0, promptSwitches: 0, promptSwitchMissed: 0, projSwitches: 0, projSwitchMissed: 0, wireSwitches: 0, wireSwitchMissed: 0, upstreamSwitches: 0, upstreamSwitchMissed: 0, restartDrops: 0, restartDropMissed: 0, attributedMissed: 0, unknownSamples: 0, unknownInput: 0, nbSamples: 0, nbInput: 0, seamSuspects: 0, seamMissed: 0, providerSideMisses: 0, providerSideMissed: 0, rewinds: 0, rewindMissed: 0, abortCorrelated: 0 }, + agg: { requests: 0, input: 0, cached: 0, output: 0, nc: 0, cr: 0, tr: 0, switches: 0, switchMissed: 0, keySwitches: 0, keySwitchMissed: 0, promptSwitches: 0, promptSwitchMissed: 0, projSwitches: 0, projSwitchMissed: 0, wireSwitches: 0, wireSwitchMissed: 0, upstreamSwitches: 0, upstreamSwitchMissed: 0, restartDrops: 0, restartDropMissed: 0, attributedMissed: 0, unknownSamples: 0, unknownInput: 0, nbSamples: 0, nbInput: 0, seamSuspects: 0, seamMissed: 0, providerSideMisses: 0, providerSideMissed: 0, providerTtlMisses: 0, providerTtlMissed: 0, rewinds: 0, rewindMissed: 0, abortCorrelated: 0 }, }; meta[LEDGER_KEY] = led; return led; @@ -738,10 +779,28 @@ export function recordCacheSample( const led = getCacheLedger(session); detectNewFolds(session, led); const prevLine = led.lines[led.lines.length - 1]; + // #1536: unknown-cache samples still need a prev/cur pair so the fold chain + // stays consistent, but decompose with cached=0 then QUARANTINE every derived + // bucket (line missed/nc/cr/tr forced to 0 + excluded from agg below). + const known = s.cached !== null; + const effCached: number = s.cached ?? 0; // Same window as buildCacheReport: a fold counts once its timestamp has // elapsed (f.at <= s.at), never earlier — keeps incremental and batch math // identical under clock skew between block.createdAt and settle time. - const pendRefs = led.folds.filter((f) => f.seq > led.consumedFoldSeq && f.at <= s.at); + // #2929: the window is now the fold's COLD TAIL (mirror of the kernel's + // buildCacheReport): the first following sample is claimed + // unconditionally, then cold continuations are claimed until a warm line + // (>= FOLD_CLAIM_WARM_HIT_PCT) proves re-cache or FOLD_CLAIM_COLD_ROUNDS + // samples have elapsed. Before this, the first measurable sample consumed + // the fold, so the client-echo re-bill ~25–66s later carried no fold stamp, + // fell into the unattributed ttlRepay residual, and tripped the seam + // detector with "no fold attribution" (#2929's seven seams were exactly + // this shape, 25–66s after a degenerate head fold). + const warmLine = + known && s.input > 0 && (effCached / s.input) * 100 >= FOLD_CLAIM_WARM_HIT_PCT; + const pendRefs = led.folds.filter( + (f) => f.at <= s.at && !f.retired && ((f.claims ?? 0) === 0 || !warmLine), + ); const pending: FoldEvent[] = pendRefs.map((f) => ({ at: f.at, tokensCompressed: f.S, @@ -750,11 +809,6 @@ export function recordCacheSample( viewBefore: f.V, viewAfter: f.Vp, })); - // #1536: unknown-cache samples still need a prev/cur pair so the fold chain - // stays consistent, but decompose with cached=0 then QUARANTINE every derived - // bucket (line missed/nc/cr/tr forced to 0 + excluded from agg below). - const known = s.cached !== null; - const effCached: number = s.cached ?? 0; const rawDec = decomposeSample( prevLine ? { at: prevLine.at, input: prevLine.input, cached: prevLine.cached } : null, { at: s.at, input: s.input, cached: effCached }, @@ -773,12 +827,30 @@ export function recordCacheSample( : rawDec; let foldSeq: number | null = null; if (known && dec.foldIndex !== null) foldSeq = pendRefs[dec.foldIndex]?.seq ?? null; - // Advance the fold-consume cursor only for measurable samples: an unknown + // Advance the claim window only for measurable samples: an unknown // sample must not eat a fold, or its compRepay would never reach a fold owner. + // #2929: each claimed sample bumps the fold's claim count; warm line or + // the FOLD_CLAIM_COLD_ROUNDS bound retires it (consumedFoldSeq stays the + // high-water mark of RETIRED folds for schema compat). if (known && pendRefs.length > 0) { - let hi = 0; - for (const f of pendRefs) hi = Math.max(hi, f.seq); - led.consumedFoldSeq = Math.max(led.consumedFoldSeq, hi); + for (const f of pendRefs) { + f.claims = (f.claims ?? 0) + 1; + if (f.claims >= FOLD_CLAIM_COLD_ROUNDS) { + f.retired = true; + led.consumedFoldSeq = Math.max(led.consumedFoldSeq, f.seq); + } + } + } + // A warm line retires EVERY open window — claimed or not this sample + // (#1847 discipline): it proves the provider re-cached, so a later cold + // blip is a new, unrelated event and must not be swallowed by the fold. + if (known && warmLine) { + for (const f of led.folds) { + if (f.at <= s.at && !f.retired) { + f.retired = true; + led.consumedFoldSeq = Math.max(led.consumedFoldSeq, f.seq); + } + } } const hitPct: number | null = known && s.input > 0 ? round1((effCached / s.input) * 100) : null; // Target identity (#1535 model, generalized to model|wire|upstream in #1536): @@ -1283,7 +1355,7 @@ interface BodyStability { interface BiliCacheReport extends CacheReport { stability: BodyStability; /** #2131: line set widened with the per-call body-stability fields. */ - lines: Array; + lines: Array; modelSwitches: ModelSwitchStats; /** #2131: relay account rotations (credential fingerprint changes) — * the identity a URL-granular upstream switch cannot see. */ @@ -1305,7 +1377,7 @@ interface BiliCacheReport extends CacheReport { * is booked as new content, never as a prefix re-pay. */ initialBills: { samples: number; inputTokens: number }; invalidation: InvalidationTokenBreakdown; - seam: { suspects: number; missed: number; events: SeamEvent[]; providerSide: { count: number; missed: number }; rewinds: { count: number; missed: number }; abortCorrelated: number }; + seam: { suspects: number; missed: number; events: SeamEvent[]; providerSide: { count: number; missed: number }; providerTtl: { count: number; missed: number }; rewinds: { count: number; missed: number }; abortCorrelated: number }; } export function buildSessionCacheReport(session: Session): BiliCacheReport { @@ -1465,6 +1537,8 @@ export function buildSessionCacheReport(session: Session): BiliCacheReport { compRepay: l.cr, ttlRepay: l.tr, foldSeq: l.foldSeq, + seam: l.seam, + prov: l.prov, bodyDigest: l.bd, keyFp: l.kp, sysFp: l.sp, @@ -1484,7 +1558,7 @@ export function buildSessionCacheReport(session: Session): BiliCacheReport { initialBills: { samples: a.nbSamples, inputTokens: a.nbInput }, invalidation, stability, - seam: { suspects: a.seamSuspects, missed: a.seamMissed, events: led.seamEvents ?? [], providerSide: { count: a.providerSideMisses, missed: a.providerSideMissed }, rewinds: { count: a.rewinds, missed: a.rewindMissed }, abortCorrelated: a.abortCorrelated }, + seam: { suspects: a.seamSuspects, missed: a.seamMissed, events: led.seamEvents ?? [], providerSide: { count: a.providerSideMisses, missed: a.providerSideMissed }, providerTtl: { count: a.providerTtlMisses, missed: a.providerTtlMissed }, rewinds: { count: a.rewinds, missed: a.rewindMissed }, abortCorrelated: a.abortCorrelated }, }; } @@ -1638,9 +1712,13 @@ function formatSeam(r: BiliCacheReport): string { out.push("↩ HISTORY REWOUND (client revert/trim)"); 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.providerTtl.count > 0) { + out.push("⏱ UPSTREAM TTL EXPIRY — NORMAL (long idle gap)"); + out.push(` ${r.seam.providerTtl.count} sample(s) · ${fmtTok(r.seam.providerTtl.missed)} tok — byte-stable/append-shaped misses settled more than ${Math.round(PROVIDER_TTL_GAP_MS / 60_000)} min after the previous sample: the upstream prefix cache simply aged out. Expected cost, not a failure.`); + } if (r.seam.providerSide.count > 0) { 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 message list is preserved in this one (byte-for-byte under the 512 KiB forensics cap; above it, proven by the stable recorded head plus exact message-count growth — 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.`); + out.push(` ${r.seam.providerSide.count} sample(s) · ${fmtTok(r.seam.providerSide.missed)} tok — the previous request's message list is preserved in this one (byte-for-byte under the 512 KiB forensics cap; above it, proven by the stable recorded head plus exact message-count growth — any difference is only the appended tail), and the sample arrived within ${Math.round(PROVIDER_TTL_GAP_MS / 60_000)} min of the previous one, so this is an ABNORMAL provider miss (eviction / relay node rotation), not TTL ageing. Not a bili rebuild seam.`); out.push(` stateful-upstream check (#2483): if your upstream keeps a live session per conversation and validates the echoed history against its own transcript (claude-code-based relays such as magpie), bili's render tags on assistant text trip that check even though bili's own list is unchanged — set diagnostics.renderNone=true until per-route render-tags control ships; expect exactly ONE replay when flipping the flag mid-session`); } if (r.seam.abortCorrelated > 0) { diff --git a/src/web/client.ts b/src/web/client.ts index 5c20421dd3..e2e623f9b2 100644 --- a/src/web/client.ts +++ b/src/web/client.ts @@ -814,17 +814,50 @@ export const WEB_CLIENT = `(function () { area += " L" + x(lines.length - 1).toFixed(1) + "," + (PT + ih).toFixed(1) + " L" + x(0).toFixed(1) + "," + (PT + ih).toFixed(1) + " Z"; // Cache-gap causes: color each sample's un-cached band (input − cached) // by its most likely cause so gaps on the chart explain themselves. - // Heuristics use only fields every ledger era carries (at/input/cached + fold times). + // #2929: mirror the ledger's fold cold-tail window — a fold owns the + // first sample after it AND the cold continuation (hit < 85%) up to 4 + // samples, closing on the first warm line. Ledger lines carry foldSeq + // directly; the fold-between-times heuristic stays as the opener and + // for pre-#2929 ledgers whose lines have no foldSeq. The tail counter + // arms ONLY on that opener path: foldSeq-stamped lines are the + // ledger's own attribution and must not re-arm, or the chart keeps + // coloring comp up to 4 lines past the ledger retiring the window. const GAP_MS = 600_000; - const CAUSE_COLOR = { cold: "#6e7681", comp: "#bf8700", ttl: "#8250df" }; - const CAUSE_KEY = { cold: "det.cause_cold", comp: "det.cause_comp", ttl: "det.cause_ttl" }; + // #2929: per-sample causes follow the LEDGER's authoritative attribution + // (foldSeq / seam / prov), not a parallel heuristic — the chart paints + // the same bucket membership the economics card aggregates. The gap + // heuristic remains only as the legacy fallback for old ledgers whose + // lines carry no attribution fields. + const CAUSE_COLOR = { cold: "#6e7681", comp: "#bf8700", seam: "#cf222e", pt: "#1a7f37", ps: "#f0883e", rw: "#57606a", rest: "#8250df" }; + const CAUSE_KEY = { cold: "det.cause_cold", comp: "det.cause_comp", seam: "det.legend_seam", pt: "det.leg_pttl", ps: "det.leg_pside", rw: "det.leg_rewind", rest: "det.cause_ttl" }; + let foldTailClaims = 0; const causes = lines.map((l, i) => { if (i === 0) return !(l.cached || 0) ? "cold" : "new"; const p = lines[i - 1]; const missed = (l.input || 0) - (l.cached || 0); const growth = Math.max(0, (l.input || 0) - (p.input || 0)); - if ((folds || []).some((f) => (f.at || 0) >= (p.at || 0) && (f.at || 0) <= (l.at || 0)) && missed > growth + 256) return "comp"; - if ((l.at || 0) - (p.at || 0) > GAP_MS && missed > growth + 2048) return "ttl"; + const warm = (l.hitPct || 0) >= 85; + const foldBetween = (folds || []).some((f) => (f.at || 0) >= (p.at || 0) && (f.at || 0) <= (l.at || 0)); + const foldOwned = l.foldSeq != null || foldBetween; + let tailComp = false; + if (warm) { + foldTailClaims = 0; + } else if (foldBetween && l.foldSeq == null) { + // Opener heuristic (pre-#2929 ledgers / unattributed openers): + // 3 continuation rounds after this one (FOLD_CLAIM_COLD_ROUNDS − 1). + foldTailClaims = 3; + } else if (foldTailClaims > 0 && missed > growth + 256) { + foldTailClaims -= 1; + tailComp = true; + } + if ((foldOwned || tailComp) && missed > growth + 256) return "comp"; + // Authoritative unattributed sub-causes (#2929 ledger fields): + if (l.seam === 1) return "seam"; + if (l.prov === "ttl") return "pt"; + if (l.prov === "side") return "ps"; + if (l.prov === "rewind") return "rw"; + // Legacy fallback only: no attribution fields on this line. + if (l.foldSeq == null && l.seam === undefined && l.prov === undefined && (l.at || 0) - (p.at || 0) > GAP_MS && missed > growth + 2048) return "rest"; return "new"; }); let bands = "", hovers = ""; @@ -835,6 +868,10 @@ export const WEB_CLIENT = `(function () { const x0 = Math.max(PL, x(i) - swSeg / 2), x1 = Math.min(W - PR, x(i) + swSeg / 2); if (c !== "new") { bands += ''; + // Vertical spine: the miss band floats above the cache line, so drop + // a thin same-colour rule down to the baseline — the bar the band + // belongs to becomes self-evident instead of floating free. + bands += ''; } const hitPctLine = (l.input || 0) > 0 ? ((l.cached || 0) / l.input * 100).toFixed(1) + "%" : t("common.none"); hovers += '' @@ -898,8 +935,23 @@ export const WEB_CLIENT = `(function () { baseline = '<line x1="' + PL + '" y1="' + yy.toFixed(1) + '" x2="' + (W - PR) + '" y2="' + yy.toFixed(1) + '" stroke="#8b949e" stroke-width="1" stroke-dasharray="2 4"/>' + '<text x="' + PL + '" y="' + Math.max(10, yy - 4).toFixed(1) + '" font-size="9.5" fill="#8b949e">' + t(baseMeasured ? "det.legend_base" : "det.legend_base_est") + " " + fmtW(baseVal) + "</text>"; } - const xt = [0, Math.floor((lines.length - 1) / 2), lines.length - 1] - .map((i) => '<text x="' + x(i).toFixed(1) + '" y="' + (H - 20) + '" text-anchor="middle" font-size="10" fill="var(--text-muted)">' + lines[i].seq + "</text>").join(""); + // #2929: per-sample seq labels — every band must map to a visible + // index, not "somewhere between 5 and 10". Thinning is PIXEL-driven, + // not count-driven: real sessions carry hundreds/thousands of samples + // (~0.4px/sample), where any flat count step either mosaics the axis + // or drops to two naked numbers. Vertical (rotated) ticks reclaim the + // unused Y range; hold a constant ≥24px HORIZONTAL pitch. + // (#2929 cont.) NO Math.max(barW,1) here — that floor clamps + // sub-pixel bars UP to 1 and silently disables thinning exactly on + // the dense sessions this exists for. + const barW = lines.length > 1 ? (W - PL - PR) / (lines.length - 1) : 1; + const tStep = Math.max(1, Math.ceil(24 / barW)); + const xtArr = []; + for (let i = lines.length - 1; i >= 0; i -= tStep) { + const l = lines[i]; + xtArr.push('<text transform="translate(' + x(i).toFixed(1) + ',' + (H - 28) + ') rotate(-90)" text-anchor="end" font-size="10" fill="var(--text-muted)">' + l.seq + "</text>"); + } + const xt = xtArr.join(""); const xtTime = (lines[0] && lines[0].at ? '<text x="' + PL + '" y="' + (H - 8) + '" text-anchor="start" font-size="9.5" fill="var(--text-faint)">' + dt(lines[0].at) + "</text>" : "") + (lines.length > 1 && lines[lines.length - 1].at ? '<text x="' + (W - PR) + '" y="' + (H - 8) + '" text-anchor="end" font-size="9.5" fill="var(--text-faint)">' + dt(lines[lines.length - 1].at) + "</text>" : ""); @@ -1097,37 +1149,12 @@ export const WEB_CLIENT = `(function () { mini(parts, t("ov.cached_tokens"), d.cachedTokens ? fmtW(d.cachedTokens) : null); const mt = d.ledger && d.ledger.totals; const missArgs = mt && mt.input > 0 ? { n: fmtW(mt.newContent || 0), c: fmtW(mt.compRepay || 0), x: fmtW(mt.ttlRepay || 0), pn: (((mt.newContent || 0) / mt.input) * 100).toFixed(1), pc: (((mt.compRepay || 0) / mt.input) * 100).toFixed(1), px: (((mt.ttlRepay || 0) / mt.input) * 100).toFixed(1) } : null; - // #1609/#1606: surface the attribution buckets (seam / provider-side / rewound / - // abort-correlated) in the headline hit-rate subline — nonzero-only, same colors - // as the 归因·未解释残差 card lower on this page. - const lsRaw = d.ledger && typeof d.ledger.seam === "object" ? d.ledger.seam : null; + // #2929: the headline hit-rate subline stays the three-bucket split only — + // the attribution chips (seam / provider / TTL / rewind / abort / prompt / + // projection) used to be appended here too, duplicating the 归因·未解释残差 + // card lower on the page and mixing taxonomy levels in one line. let hitSub = ""; if (missArgs) hitSub += t("det.miss_sub", { x: missArgs.px, c: missArgs.pc, n: missArgs.pn }); - const attrChips = []; - if (lsRaw) { - const seamSm = Number(lsRaw.missed) || 0, seamSn = Number(lsRaw.suspects) || 0; - if (seamSm > 0 || seamSn > 0) attrChips.push('<span style="color:#cf222e" title="' + escapeHtml(t("det.attr_seam_tip")) + '">' + escapeHtml(t("det.attr_seam")) + " " + fmtW(seamSm) + (seamSn > 0 ? " ×" + seamSn : "") + "</span>"); - const provM = (lsRaw.providerSide && Number(lsRaw.providerSide.missed)) || 0, provN = (lsRaw.providerSide && Number(lsRaw.providerSide.count)) || 0; - if (provM > 0 || provN > 0) attrChips.push('<span style="color:#bf8700" title="' + escapeHtml(t("det.attr_provider_tip")) + '">' + escapeHtml(t("det.attr_provider")) + " " + fmtW(provM) + (provN > 0 ? " ×" + provN : "") + "</span>"); - const rewM = (lsRaw.rewinds && Number(lsRaw.rewinds.missed)) || 0, rewN = (lsRaw.rewinds && Number(lsRaw.rewinds.count)) || 0; - if (rewM > 0 || rewN > 0) attrChips.push('<span style="color:#57606a" title="' + escapeHtml(t("det.attr_rewind_tip")) + '">' + escapeHtml(t("det.attr_rewind")) + " " + fmtW(rewM) + (rewN > 0 ? " ×" + rewN : "") + "</span>"); - const abN = Number(lsRaw.abortCorrelated) || 0; - if (abN > 0) attrChips.push('<span style="color:#d4a72c" title="' + escapeHtml(t("det.attr_abort_tip")) + '">' + escapeHtml(t("det.attr_abort")) + " ×" + abN + "</span>"); - } - // #2350: host system-prompt rewrites are a named partition of the same - // residual (cause=prompt), not part of ledger.seam — read them directly. - const pswRaw = d.ledger && typeof d.ledger.promptSwitches === "object" ? d.ledger.promptSwitches : null; - if (pswRaw) { - const pM = Number(pswRaw.missedTokens) || 0, pN = Number(pswRaw.count) || 0; - if (pM > 0 || pN > 0) attrChips.push('<span style="color:#0969da" title="' + escapeHtml(t("det.attr_prompt_tip")) + '">' + escapeHtml(t("det.attr_prompt")) + " " + fmtW(pM) + (pN > 0 ? " ×" + pN : "") + "</span>"); - } - // #2867: proxy projection/mode switches — bili's own partition of the residual. - const jswRaw = d.ledger && typeof d.ledger.projSwitches === "object" ? d.ledger.projSwitches : null; - if (jswRaw) { - const jM = Number(jswRaw.missedTokens) || 0, jN = Number(jswRaw.count) || 0; - if (jM > 0 || jN > 0) attrChips.push('<span style="color:#8250df" title="' + escapeHtml(t("det.attr_proj_tip")) + '">' + escapeHtml(t("det.attr_proj")) + " " + fmtW(jM) + (jN > 0 ? " ×" + jN : "") + "</span>"); - } - if (attrChips.length) hitSub += (hitSub ? " · " : "") + attrChips.join(" · "); // #2734: the ledger only covers requests since its last (re)start — a // restore/restart window drop leaves older requests invisible to hit/miss // stats. Surface the gap and the restart drops explicitly so a frozen- @@ -1193,6 +1220,7 @@ export const WEB_CLIENT = `(function () { missed: Number(seamRaw.missed) || 0, events: Array.isArray(seamRaw.events) ? seamRaw.events.filter((e) => e && e.at > 0) : [], providerSide: { count: (seamRaw.providerSide && Number(seamRaw.providerSide.count)) || 0, missed: (seamRaw.providerSide && Number(seamRaw.providerSide.missed)) || 0 }, + providerTtl: { count: (seamRaw.providerTtl && Number(seamRaw.providerTtl.count)) || 0, missed: (seamRaw.providerTtl && Number(seamRaw.providerTtl.missed)) || 0 }, rewinds: { count: (seamRaw.rewinds && Number(seamRaw.rewinds.count)) || 0, missed: (seamRaw.rewinds && Number(seamRaw.rewinds.missed)) || 0 }, abortCorrelated: Number(seamRaw.abortCorrelated) || 0, } : null; @@ -1206,13 +1234,28 @@ export const WEB_CLIENT = `(function () { parts.push(legendItem("background:var(--accent)", t("det.legend_input"))); parts.push(legendItem("background:var(--accent);opacity:.4", t("det.legend_cached"), false)); parts.push(legendItem("#cf222e", t("det.legend_fold"), true)); - if (seamActive && seam.events.length > 0) - parts.push(legendItem("background:#cf222e", t("det.legend_seam"))); parts.push(legendItem("#cf222e", t("det.legend_window"), true)); - // Swatches carry real backgrounds (a bare hex in style= renders nothing): + // Swatches carry real backgrounds (a bare hex in style= renders nothing). + // #2929: miss-cause swatches mirror the ledger buckets — the + // unattributed family renders as ONE bordered group (外框) with its + // sub-causes inside, so containment is visible, not implied. parts.push(legendItem("background:#bf8700", t("det.cause_comp"))); - parts.push(legendItem("background:#8250df", t("det.cause_ttl"))); parts.push(legendItem("background:#6e7681", t("det.cause_cold"))); + if (seam) { + const sub = []; + if (seam.missed > 0 || seam.suspects > 0) sub.push(legendItem("background:#cf222e", t("det.legend_seam"))); + if (seam.providerTtl.count > 0) sub.push(legendItem("background:#1a7f37", t("det.leg_pttl"))); + 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"))); + sub.push(legendItem("background:#8250df", 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")) + + '<span class="dim">⊇</span>' + + sub.join("") + + "</span>"); + } else { + parts.push(legendItem("background:#8250df", t("det.cause_ttl"))); + } if (d.systemPromptTokens || lines.length >= 20) parts.push(legendItem("border:1.5px solid #8b949e;background:#f2f5f7;", d.systemPromptTokens ? t("det.legend_base") : t("det.legend_base_est"))); parts.push("</div>"); if ((ledger.linesOmitted || 0) > 0) parts.push('<div class="dim small" style="margin-top:6px">' + t("det.omitted", { n: ledger.linesOmitted }) + "</div>"); @@ -1300,24 +1343,32 @@ export const WEB_CLIENT = `(function () { if (tot) { parts.push('<div class="grid cols-4">'); mini(parts, t("det.ce_new"), fmtW(tot.newContent || 0)); - mini(parts, t("det.ce_comp"), fmtW(tot.compRepay || 0)); - mini(parts, t("det.ce_ttl"), fmtW(tot.ttlRepay || 0)); + mini(parts, t("det.ce_comp"), fmtW(tot.compRepay || 0), false, (tot.compRepay || 0) > 0 ? t("det.ce_comp_hint") : ""); + // #2929: the unattributed bucket shows its own composition INSIDE the + // 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) + "</div></div>"; + if (seam && (seam.missed > 0 || seam.providerTtl.count > 0 || seam.providerSide.count > 0 || seam.rewinds.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 : "") + "</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 : "") + "</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 : "") + "</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 : "") + "</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); + if (uRest > 8192) uChips.push('<span style="color:#9aa1a8" title="' + escapeHtml(t("det.attr_rest_tip")) + '">' + t("det.attr_rest", { x: fmtW(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) + '</div>' + + '<div class="dim small mono" style="margin-top:6px;line-height:1.7"><span>' + escapeHtml(t("det.ce_breakdown")) + "</span><br>" + uChips.join("<br>") + "</div></div>"); + } else { + parts.push(plainUnattrib()); + } + } else { + parts.push(plainUnattrib()); + } mini(parts, t("det.ce_residual"), fmtW(tot.residual || 0)); parts.push("</div>"); } - if (seamActive) { - parts.push('<div class="section-label" style="margin-top:14px">' + t("det.attr_title") + "</div>" - + '<div class="grid cols-4">' - + '<div class="mini"><div class="k" style="color:#cf222e" title="' + escapeHtml(t("det.attr_seam_tip")) + '">' + escapeHtml(t("det.attr_seam")) + '</div><div class="v mono" style="color:#cf222e">' + fmtW(seam.missed) + (seam.suspects > 0 ? ' <span class="dim small">×</span>' + seam.suspects : "") + "</div>" - // Per-event attribution inside the seam mini: which samples the aggregate points at. - + (seam.events.length ? '<div class="dim small mono attr-sub" style="margin-top:4px;line-height:1.6">' + seam.events.slice(0, 6).map((ev) => "#" + ev.seq + " " + (ev.at ? fmtDT(ev.at) : "") + (typeof ev.hitPct === "number" ? " " + ev.hitPct.toFixed(1) + "%" : "") + " ≥" + fmtB(ev.lcpBytes || 0) + " @m" + (ev.msgIndex != null ? ev.msgIndex : "?") + (ev.prevMsgs != null && ev.curMsgs != null ? " (" + ev.prevMsgs + "→" + ev.curMsgs + ")" : "") + "<br>").join("") + (seam.events.length > 6 ? "+" + (seam.events.length - 6) + " …<br>" : "") + "</div>" : "") - + "</div>" - + '<div class="mini"><div class="k" style="color:#bf8700" title="' + escapeHtml(t("det.attr_provider_tip")) + '">' + escapeHtml(t("det.attr_provider")) + '</div><div class="v mono" style="color:#bf8700">' + fmtW(seam.providerSide.missed) + (seam.providerSide.count > 0 ? ' <span class="dim small">×</span>' + seam.providerSide.count : "") + "</div></div>" - + '<div class="mini"><div class="k" style="color:#57606a" title="' + escapeHtml(t("det.attr_rewind_tip")) + '">' + escapeHtml(t("det.attr_rewind")) + '</div><div class="v mono" style="color:#57606a">' + fmtW(seam.rewinds.missed) + (seam.rewinds.count > 0 ? ' <span class="dim small">×</span>' + seam.rewinds.count : "") + "</div></div>" - + '<div class="mini"><div class="k" style="color:#d4a72c" title="' + escapeHtml(t("det.attr_abort_tip")) + '">' + escapeHtml(t("det.attr_abort")) + '</div><div class="v mono" style="color:#d4a72c">' + String(seam.abortCorrelated) + "</div></div>" - + "</div>" - + '<div class="dim small" style="margin-top:6px">' + t("det.attr_note") + "</div>"); - } const folds = ledger.folds || []; parts.push('<div class="section-label" style="margin-top:14px">' + t("det.folds") + "</div>"); if (!folds.length) parts.push('<div class="dim small">' + t("det.folds_empty") + "</div>"); diff --git a/src/web/i18n.ts b/src/web/i18n.ts index 541c352ceb..9d8ef8beb9 100644 --- a/src/web/i18n.ts +++ b/src/web/i18n.ts @@ -77,16 +77,16 @@ export const MESSAGES: Record<Locale, Record<string, string>> = { "ov.tokens_saved": "压缩节省", "ov.gross_saved": "压缩累计节省", "ov.net_saved": "净节省", - "ov.sub_repay": "已扣除压缩复付 {r} 与摘要成本 {s}", + "ov.sub_repay": "已扣除折叠后重建 {r} 与摘要成本 {s}", "ov.covlost_note": "其中 {n} 个折叠覆盖丢失,冻结 {x}", "det.covlost_sub": "其中 {n} 个折叠覆盖丢失(冻结 {x}):其覆盖的原始消息已离开重发历史,节省累计已停止", "ses.covlost_tip": "{n} 个折叠覆盖丢失(冻结 {x}):宿主在 bili 不知情下移除了被压缩的原始消息(如 dsh 原生压缩),对应节省已停止累计", - "ov.miss_split": "未命中构成(有账本会话):新增内容 {n} · 压缩折损 {c} · TTL过期等前缀失效 {x}", + "ov.miss_split": "未命中构成(有账本会话):新增内容 {n} · 折叠后重建 {c} · 未归因回补 {x}(上游TTL/淘汰/未知)", "det.hit_pct": "缓存命中率", - "ses.drop_ph": "命中率代价(占总输入的百分比,顺序:新增 / 压缩 / TTL过期)", + "ses.drop_ph": "命中率代价(占总输入的百分比,顺序:新增 / 折叠后重建 / 未归因回补)", "ses.empty_hidden": "另有 {n} 个空会话已隐藏(无请求、无上下文、无账本;直接链接仍可访问)", - "det.miss_split_line": "命中率的未命中部分拆分:新增内容 {n}(−{pn}%)· 压缩折损 {c}(−{pc}%)· TTL过期等前缀失效 {x}(−{px}%)", - "det.miss_sub": "TTL过期 −{x}% · 压缩 −{c}% · 新增 −{n}%", + "det.miss_split_line": "命中率的未命中部分拆分:新增内容 {n}(−{pn}%)· 折叠后重建 {c}(−{pc}%)· 未归因回补 {x}(−{px}%,上游TTL/淘汰/未知)", + "det.miss_sub": "未归因回补 −{x}% · 折叠后重建 −{c}% · 新增 −{n}%", "det.fold_time": "时间", "ov.global_hitpct": "全局缓存命中率", "ov.input_tokens": "输入 token", @@ -236,38 +236,43 @@ export const MESSAGES: Record<Locale, Record<string, string>> = { "det.legend_fold": "压缩折叠点", "det.legend_window": "上下文窗口上限", "det.cause_cold": "冷启动(无历史缓存)", - "det.cause_comp": "压缩后重新建缓存", - "det.cause_ttl": "间隔过长 · 上游 TTL 过期重读", + "det.cause_comp": "折叠后重建", + "det.cause_ttl": "未归因回补 · 其余", + "det.cause_unattrib": "未归因回补", + "det.leg_pttl": "⏱ 上游 TTL 过期", + "det.leg_pside": "▲ 上游侧失效", + "det.leg_rewind": "↩ 历史回退", + "det.leg_rest": "其余", "det.fold_short": "折叠", "ov.gross_note": "按每次折叠的节省量 × 其后每个请求累计(理论上界)", "ov.net_excl": "未含旧版本无采样会话", "det.omitted": "更早的 {n} 条采样已裁除:每个会话只保留最近 4096 条缓存采样,超出部分在写入时裁剪、不可恢复,所以曲线从最早保留的一条开始", - "ov.saved_neg_tip": "净节省为负:压缩复付(摘要复读成本)超过了理论节省——常见于折叠后很快结束的短会话,这些场景下压缩不划算", + "ov.saved_neg_tip": "净节省为负:折叠后重建(摘要复读成本)超过了理论节省——常见于折叠后很快结束的短会话,这些场景下压缩不划算", "det.cache_econ": "缓存经济学", "det.ce_new": "新内容", - "det.ce_comp": "压缩回补", - "det.ce_ttl": "TTL 回补", + "det.ce_comp": "折叠后重建", + "det.ce_comp_hint": "提示:保护最近消息条数(compress.preserveRecentMessages)调小可缓解,建议最低 1", + "det.ce_ttl": "未归因回补", + "det.ce_breakdown": "其中:", + "det.attr_rest": "其余 {x}", + "det.attr_rest_tip": "低于定性门槛的小额残差(单笔 <8K 或 <30% 输入),不逐一归类", "det.ce_residual": "残差", "det.ce_balanced": "账目平衡 ✓", "det.ce_unbalanced": "账目不平衡 ✗", - "det.attr_title": "归因 · 未解释残差", "det.attr_seam": "⚠ 缓存接缝", - "det.attr_provider": "▲ 上游侧失效", + "det.attr_provider": "▲ 上游侧失效(异常)", + "det.attr_ttl": "⏱ 上游 TTL 过期(正常)", + "det.attr_ttl_tip": "与上一请求间隔超过 10 分钟后,字节稳定/纯追加形态仍 miss——上游前缀缓存已正常过期,属预期损耗,无需处理", "det.attr_rewind": "↩ 历史回退", "det.attr_abort": "⏻ 中断相关", - "det.attr_seam_tip": "出站请求体在历史中段出现字节分叉,且无 fold/模型切换/重启归因——疑似重建缝。数值为嫌疑样本重计费的 token", - "det.attr_provider_tip": "上一请求的整条消息列表逐字节保留(整体一致,或前缀一致、仅尾部追加新消息;超过 512KiB 取证上限的请求以记录头稳定 + 精确消息数增长判定)却仍 miss——上游 TTL 过期/淘汰/中继节点轮换,非 bili 重建缝", + "det.attr_seam_tip": "出站请求体在历史中段出现字节分叉,且无 fold/模型切换/重启归因——疑似重建缝。数值为嫌疑样本重计费的 token。若此前几分钟内发生过折叠,多半是折叠后客户端回显的重付(#2929 后已归入折叠后重建)", + "det.attr_provider_tip": "与上一请求间隔很短,且整条消息列表逐字节保留(整体一致,或前缀一致、仅尾部追加新消息;超过 512KiB 取证上限的请求以记录头稳定 + 精确消息数增长判定)却仍 miss——异常:上游淘汰/中继节点轮换,非 bili 重建缝;长间隔的同类样本归入「上游 TTL 过期」", "det.attr_rewind_tip": "客户端 revert/trim 使消息数下降——合法的一次性重计费(保留前缀),非缺陷", "det.attr_abort_tip": "客户端流中 abort 后 30s 内结算的 miss 样本数——重试重写重发尾部;相关性而非因果,叠加标记不计入分桶", "det.attr_restart": "⟳ 重启丢窗", "det.attr_restart_tip": "bili 重启或会话恢复后,账本窗口断裂:重启后的首个请求无法与重启前样本配对,miss 按首账计费(初始内容)", "det.ledger_gap": "账本未覆盖 {n} 请求", "det.ledger_gap_tip": "命中/miss 账本只从最近一次账本(重)建立点开始记录;窗口外的请求不在统计内——多实例共享 state 目录时常见。折叠若发生在窗外,经济性折叠数也不含它", - "det.attr_prompt": "✍️ 宿主改写提示词", - "det.attr_prompt_tip": "相邻两次请求的转发投影指纹不同且原始入站内容也变了=宿主改写了系统提示词(如周期性插入/移除动态段)——下游前缀缓存全部失效;该请求稳定前缀重计费计入此桶(归因优先级 restart>model>key>prompt>projection>wire>upstream,#2350/#2867)", - "det.attr_proj": "🔀 代理投影切换", - "det.attr_proj_tip": "转发的 system/developer 投影变了但原始入站内容未变=代理自身切换了压缩模式/投影规则(如 self-heal 降级 plugin→proxy 把中段 developer 消息重新提升进 system)——下游前缀缓存全部失效;该请求稳定前缀重计费计入此桶(归因优先级 restart>model>key>prompt>projection>wire>upstream,#2867)", - "det.attr_note": "接缝 / 上游侧 / 回退 / 宿主改写 / 代理投影 互斥分区未解释残差的一部分,中断为叠加标记——均不含在上方 TOTAL 内,不重复计数", "det.seam_events": "⚠ 缓存接缝事件 · {n}", "det.seam_events_tip": "相邻出站体的首个字节分叉位置;若可复现,请附上该时间附近的 bili.log(#1548 类问题)", "det.stab_title": "体稳定性(逐调用字节证明)", @@ -526,16 +531,16 @@ export const MESSAGES: Record<Locale, Record<string, string>> = { "ov.tokens_saved": "Saved by compression", "ov.gross_saved": "Compressed savings (cumulative)", "ov.net_saved": "Net savings", - "ov.sub_repay": "after subtracting re-pay {r} and summary cost {s}", + "ov.sub_repay": "after subtracting post-fold rebuild {r} and summary cost {s}", "ov.covlost_note": "{n} fold(s) coverage-lost, {x} frozen", "det.covlost_sub": "{n} fold(s) coverage-lost ({x} frozen): their covered messages left the resent history — accrual stopped", "ses.covlost_tip": "{n} fold(s) coverage-lost ({x} frozen): the host removed the compressed originals outside bili's knowledge (e.g. dsh native compaction) — their savings no longer accrue", - "ov.miss_split": "missed tokens (ledgered sessions): new content {n} · compression re-read {c} · TTL expiry / other prefix invalidation {x}", + "ov.miss_split": "missed tokens (ledgered sessions): new content {n} · post-fold rebuild {c} · unattributed repay {x} (upstream TTL/eviction/unknown)", "det.hit_pct": "cache hit rate", - "ses.drop_ph": "hit-rate cost as a share of total input, order: new / comp / TTL expiry", + "ses.drop_ph": "hit-rate cost as a share of total input, order: new / post-fold rebuild / unattributed repay", "ses.empty_hidden": "{n} empty session(s) hidden (no requests, no context, no ledger; still reachable by direct link)", - "det.miss_split_line": "missed-token split of the hit rate: new content {n} (−{pn}%) · compression re-read {c} (−{pc}%) · TTL expiry / other prefix invalidation {x} (−{px}%)", - "det.miss_sub": "TTL expiry −{x}% · comp −{c}% · new −{n}%", + "det.miss_split_line": "missed-token split of the hit rate: new content {n} (−{pn}%) · post-fold rebuild {c} (−{pc}%) · unattributed repay {x} (−{px}%, upstream TTL/eviction/unknown)", + "det.miss_sub": "unattributed repay −{x}% · post-fold rebuild −{c}% · new −{n}%", "det.fold_time": "when", "ov.global_hitpct": "Global cache hit rate", "ov.input_tokens": "Input tokens", @@ -682,38 +687,43 @@ export const MESSAGES: Record<Locale, Record<string, string>> = { "det.legend_fold": "compression fold", "det.legend_window": "context window limit", "det.cause_cold": "cold start (no prior cache)", - "det.cause_comp": "re-caching after compaction", - "det.cause_ttl": "long gap - upstream TTL expired, re-read", + "det.cause_comp": "post-fold rebuild", +"det.cause_ttl": "unattributed repay · rest", + "det.cause_unattrib": "unattributed repay", + "det.leg_pttl": "⏱ upstream TTL expiry", + "det.leg_pside": "▲ provider-side miss", + "det.leg_rewind": "↩ rewind", + "det.leg_rest": "rest", "det.fold_short": "fold", "ov.gross_note": "sums each fold's savings across every later request (theoretical upper bound)", "ov.net_excl": "pre-tagging sessions without samples not included", "det.omitted": "earliest {n} samples were trimmed at write time: each session keeps only its most recent 4096 cache samples (unrecoverable), so the curve starts at the oldest retained sample", - "ov.saved_neg_tip": "Negative net savings: compression re-pay (summary re-read cost) exceeded the theoretical gain; typical of short sessions that ended right after folding", + "ov.saved_neg_tip": "Negative net savings: post-fold rebuild (summary re-read cost) exceeded the theoretical gain; typical of short sessions that ended right after folding", "det.cache_econ": "Cache economics", "det.ce_new": "New content", - "det.ce_comp": "Compaction repay", - "det.ce_ttl": "TTL repay", + "det.ce_comp": "Post-fold rebuild", + "det.ce_comp_hint": "Tip: lowering compress.preserveRecentMessages (the protected recent-message count) reduces this — minimum 1", + "det.ce_ttl": "Unattributed repay", + "det.ce_breakdown": "of which:", + "det.attr_rest": "rest {x}", + "det.attr_rest_tip": "small residuals below the classification threshold (<8K or <30% of input per sample), not individually classified", "det.ce_residual": "Residual", "det.ce_balanced": "Ledger balanced ✓", "det.ce_unbalanced": "Ledger unbalanced ✗", - "det.attr_title": "Attribution · unexplained residual", "det.attr_seam": "⚠ Cache seam", - "det.attr_provider": "▲ Provider-side miss", + "det.attr_provider": "▲ Provider-side miss (abnormal)", + "det.attr_ttl": "⏱ upstream TTL expiry (normal)", + "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_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": "The previous request's 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 — TTL expiry / eviction / relay node rotation. Not a bili rebuild seam", + "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_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)", "det.ledger_gap": "ledger misses {n} reqs", "det.ledger_gap_tip": "The hit/miss ledger only covers requests since its last (re)build point; requests outside the window are not counted — common with multiple instances sharing a state directory. Folds inside the gap are also missing from fold economics", - "det.attr_prompt": "✍️ Host prompt rewrite", - "det.attr_prompt_tip": "The forwarded-projection fingerprint differs between consecutive requests AND the canonical incoming content also changed = the host rewrote its system prompt (e.g. a periodic dynamic section) — every downstream prefix-cache entry is invalidated; the stable-prefix re-bill is charged here (attribution priority restart > model > key > prompt > projection > wire > upstream, #2350/#2867)", - "det.attr_proj": "🔀 Proxy projection switch", - "det.attr_proj_tip": "Forwarded system/developer projection changed while the canonical incoming content is unchanged = the proxy switched its own compression mode/projection (e.g. self-heal degrade-to-proxy re-hoists mid-history developer messages) — every downstream prefix-cache entry is invalidated; the stable-prefix re-bill is charged here (attribution priority restart > model > key > prompt > projection > wire > upstream, #2867)", - "det.attr_note": "seam / provider-side / rewound / host-rewrite / proxy-projection exclusively partition part of the unexplained residual; abort-correlated is an overlay — none of it is counted in TOTAL above", "det.seam_events": "⚠ Cache-seam events · {n}", "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)", diff --git a/tests/cache-ledger.test.ts b/tests/cache-ledger.test.ts index 5a833fe0e6..71736cbc96 100644 --- a/tests/cache-ledger.test.ts +++ b/tests/cache-ledger.test.ts @@ -82,11 +82,15 @@ test("incremental ledger closes the same identity as kernel batch", () => { assert.equal(inc.totals.ttlRepay, batch.totals.ttlRepay - 1000, "opener residual removed from ttl re-pay"); // Hand-computed expectations. + // #2929: the @3000 sample (28.6% — cold) is now fold b1's SECOND claim + // under the cold-tail window, so its structural excess (7000 − 4500 − + // 1000 growth = 1500) moves from the unattributed ttl bucket into + // compRepay; the batch path claims it identically. assert.equal(inc.totals.input, 30500); assert.equal(inc.totals.cached, 19000); assert.equal(inc.totals.newContent, 2500); - assert.equal(inc.totals.compRepay, 1500); - assert.equal(inc.totals.ttlRepay, 7500); + assert.equal(inc.totals.compRepay, 3000); + assert.equal(inc.totals.ttlRepay, 6000); // Line-level: the opener is no-baseline initial content. The nb flag lives // on the raw ledger line — report lines follow the kernel line shape. const rawOpener = getCacheLedger(session).lines[0]!; @@ -101,9 +105,16 @@ test("incremental ledger closes the same identity as kernel batch", () => { assert.equal(cliff.compRepay, 1500); assert.equal(cliff.ttlRepay, 3500); assert.equal(cliff.foldSeq, 1); + // #2929: the cold @3000 sample is b1's continuation claim — comp absorbs + // the structural excess instead of leaking into the unattributed bucket. + const cont = inc.lines.find((l) => l.seq === 3)!; + assert.equal(cont.newContent, 1000); + assert.equal(cont.compRepay, 1500); + assert.equal(cont.ttlRepay, 2500); + assert.equal(cont.foldSeq, 1); // Fold economics carry the measured values. const f1 = inc.folds.find((f) => f.seq === 1)!; - assert.equal(f1.T, 1500); + assert.equal(f1.T, 3000); assert.equal(f1.hPct, 16.7); assert.equal(f1.S, 5000); assert.equal(f1.sigma, 2048); diff --git a/tests/golden/storage-render/anthropic.json b/tests/golden/storage-render/anthropic.json index 909088df9c..4e2a78c32f 100644 --- a/tests/golden/storage-render/anthropic.json +++ b/tests/golden/storage-render/anthropic.json @@ -80,12 +80,12 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "eb8231ac9bf4cfb89a0bbebe08dae648970237dfa2608849afbb3ff86e5e6238", + "stateDigest": "1088bae87675b4b44d38c604cd1f7456b665e53dd4ba27649048ebda00ec31f8", "sectionDigests": { "state": "b8d5d5c3a1d6423b", "blockContents": "b3a9c59b22ec1a3a", "stats": "5dedce65e3559c6c", - "metadata": "63dd4b94011331dc" + "metadata": "e912f03b1cd341f4" }, "stateKeyDigests": { "blocks": "8a3021f39f1f2eb8", diff --git a/tests/golden/storage-render/chat.json b/tests/golden/storage-render/chat.json index 28d632c3f6..2190701b7d 100644 --- a/tests/golden/storage-render/chat.json +++ b/tests/golden/storage-render/chat.json @@ -80,12 +80,12 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "675803ce7ea9616b2895323f54e9c20cbe1d802e221759665dccd21a747eaf5b", + "stateDigest": "384f7fd038ac18c7517c7a7d452d68a862616063d1d191133fc2e124eb5957b9", "sectionDigests": { "state": "aa312c3572a95958", "blockContents": "dd2de296a5bc6160", "stats": "88eb19a9daefcbc3", - "metadata": "b39ea710ef9d2a61" + "metadata": "0d8b8f419b38f159" }, "stateKeyDigests": { "blocks": "bdde93b0e100e13e", diff --git a/tests/golden/storage-render/google.json b/tests/golden/storage-render/google.json index 6348c3f015..9f4049d79e 100644 --- a/tests/golden/storage-render/google.json +++ b/tests/golden/storage-render/google.json @@ -80,12 +80,12 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "0daa3487b08794976d5754dc1b3a1a70d57b56a3c87e1995b6815d9c2170c8b7", + "stateDigest": "7fd4909d7178239b13237590d0ccd03103dd8e46e7642ea620dcc82155187a78", "sectionDigests": { "state": "3d8afb23f4d63b31", "blockContents": "4cc83a808a659f28", "stats": "c95acb7dd4c8d0b4", - "metadata": "a77709ac04d31f3e" + "metadata": "db5726ac9598a0e2" }, "stateKeyDigests": { "blocks": "9adb53303cd4b786", diff --git a/tests/golden/storage-render/responses.json b/tests/golden/storage-render/responses.json index fc6cbe887b..cb2790f43b 100644 --- a/tests/golden/storage-render/responses.json +++ b/tests/golden/storage-render/responses.json @@ -80,12 +80,12 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "9193eb4e96dbbc19844bc1372d3c7efdf75e5f0099df3244265b030974cdecdc", + "stateDigest": "e6e2a18fc7c9b1f44a0572b54008817e21db85fb0e98829e3bde7d1506c21820", "sectionDigests": { "state": "1361703652576fc5", "blockContents": "1727e2e1faf79b55", "stats": "07d32933c52cb7f8", - "metadata": "afaa2775e026f8d5" + "metadata": "61c8172889bbdc91" }, "stateKeyDigests": { "blocks": "0458e9c8a7bb1a99", diff --git a/tests/issue2929-fold-claim-window.test.ts b/tests/issue2929-fold-claim-window.test.ts new file mode 100644 index 0000000000..fc2dc08157 --- /dev/null +++ b/tests/issue2929-fold-claim-window.test.ts @@ -0,0 +1,137 @@ +// #2929: the fold claim window is the post-fold COLD TAIL, mirroring the +// kernel's buildCacheReport (#1847 switch discipline applied to folds). Before +// this, the first measurable sample after a fold consumed it, so the client's +// echo re-bill ~25–66s later carried no foldSeq, fell into the unattributed +// ttlRepay residual, and tripped the cache-seam detector with "no fold +// attribution" — every one of #2929's seven seams was exactly this shape. +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { noteForwardedBody, settleUsageReport, getCacheLedger } from "../src/cache-ledger.ts"; +import { FOLD_CLAIM_COLD_ROUNDS, FOLD_CLAIM_WARM_HIT_PCT } from "acp-kernel"; +import type { Session } from "../src/session.ts"; + +let seq = 0; +function makeSession(): Session { + seq += 1; + return { + id: `fold-claim-${seq}`, + metadata: {}, + stats: {}, + state: { blocks: [], messageRefs: { byRaw: {}, byRef: {} }, tokenSnapshot: {} }, + } as unknown as Session; +} + +const body = (msgs: string[]): string => JSON.stringify({ model: "m", messages: msgs.map((c) => ({ role: "user", content: c })) }); +const T0 = Date.parse("2026-10-12T10:56:00Z"); + +function settle(session: Session, at: number, input: number, cached: number): void { + settleUsageReport(session, { total: input, reportedCached: cached, output: 0, protocol: "openai", upstream: "http://u" }); +} + +/** Push a fold as a kernel block — same rig as cache-seam-detector.test.ts. */ +function fold(session: Session, at: number): void { + const st = session.state as unknown as { blocks: Array<Record<string, unknown>> }; + st.blocks.push({ blockId: "b1", createdAt: at, compressedTokens: 50_000, summary: "x".repeat(200), startRef: "m00002" }); +} + +test("constants exported for host/kernel parity (#2929)", () => { + assert.equal(FOLD_CLAIM_COLD_ROUNDS, 4); + assert.equal(FOLD_CLAIM_WARM_HIT_PCT, 85); +}); + +test("the client-echo re-bill is fold-owned: no seam, ttl stays clean (#2929)", () => { + const s = makeSession(); + // Healthy baseline. + noteForwardedBody(s, body(["a", "b", "c"])); + settle(s, T0, 100_000, 99_000); + // Degenerate head fold lands between requests. + fold(s, T0 + 500); + // Proxy re-request (folded view) — first claim, as before. + noteForwardedBody(s, body(["a", "SUMMARY"])); + settle(s, T0 + 1_000, 51_000, 5_000); + // Client echo re-bill 25–66s later: mid-history divergence vs the folded + // view, cold (10%), no switch/restart — the exact #2929 seam shape. + noteForwardedBody(s, body(["a", "b", "c", "d"])); + settle(s, T0 + 60_000, 55_000, 5_100); + const led = getCacheLedger(s); + const echo = led.lines[led.lines.length - 1]!; + assert.ok(echo.foldSeq !== null, "echo re-bill claimed by the fold's cold-tail window"); + assert.equal(echo.seam, undefined, "echo must NOT trip the seam detector anymore"); + assert.equal(led.agg.seamSuspects, 0); + assert.ok(echo.cr > 0 && echo.tr === 0, "residual booked as compRepay, not unattributed ttl"); + assert.ok(led.agg.cr >= echo.cr, "fold economics owner absorbs it"); +}); + +test("warm line retires the window: a later cold blip is unattributed again (#2929)", () => { + const s = makeSession(); + noteForwardedBody(s, body(["a", "b"])); + settle(s, T0, 100_000, 99_000); + fold(s, T0 + 500); + noteForwardedBody(s, body(["a", "SUMMARY"])); + settle(s, T0 + 1_000, 51_000, 5_000); // claim 1 (cold) + noteForwardedBody(s, body(["a", "SUMMARY", "d"])); + settle(s, T0 + 30_000, 55_000, 52_000); // 94.5% warm — retires every open window + noteForwardedBody(s, body(["a", "SUMMARY", "d", "e"])); + settle(s, T0 + 90_000, 60_000, 20_000); // unrelated later cold blip (33%) + const led = getCacheLedger(s); + const blip = led.lines[led.lines.length - 1]!; + assert.equal(blip.foldSeq, null, "window closed by the warm line"); + assert.ok(blip.tr > 0, "blip falls to the unattributed residual"); + assert.equal(blip.seam, undefined, "blip is byte-stable append-shaped here; and even a mid-history one would be a NEW seam worth seeing"); +}); + +test("FOLD_CLAIM_COLD_ROUNDS bounds the window without a warm line (#2929)", () => { + const s = makeSession(); + noteForwardedBody(s, body(["a", "b"])); + settle(s, T0, 100_000, 99_000); + fold(s, T0 + 500); + for (let i = 0; i < FOLD_CLAIM_COLD_ROUNDS + 2; i++) { + noteForwardedBody(s, body(["a", "SUMMARY", `t${i}`])); + settle(s, T0 + 1_000 + i * 5_000, 55_000 + i * 100, 5_100); // all cold + } + const led = getCacheLedger(s); + const claimed = led.lines.filter((l) => l.foldSeq !== null).length; + assert.equal(claimed, FOLD_CLAIM_COLD_ROUNDS, "exactly the bound"); + const tail = led.lines[led.lines.length - 1]!; + assert.equal(tail.foldSeq, null); + assert.ok(tail.tr > 0, "post-window residual is unattributed again"); +}); + +test("ledger persisted before #2929 (no claims/retired fields) still claims the first sample", () => { + const s = makeSession(); + noteForwardedBody(s, body(["a", "b"])); + settle(s, T0, 100_000, 99_000); + fold(s, T0 + 500); + noteForwardedBody(s, body(["a", "SUMMARY"])); + settle(s, T0 + 1_000, 51_000, 5_000); + const led = getCacheLedger(s); + // Sparse-field compat: the fold record now carries the new fields. + const f = led.folds[0]!; + assert.equal(f.claims, 1); + assert.notEqual(f.retired, true); + assert.ok(led.lines[led.lines.length - 1]!.foldSeq === f.seq); +}); + +test("#2929 taxonomy: long-idle byte-stable miss = normal upstream TTL, short-gap = abnormal provider-side", () => { + const s = makeSession(); + const realNow = Date.now; + const at = (t: number): void => { Date.now = () => t; }; + try { + at(T0); noteForwardedBody(s, body(["a", "b"])); + settle(s, T0, 100_000, 99_000); + at(T0 + 10_000); noteForwardedBody(s, body(["a", "b"])); + settle(s, T0 + 10_000, 100_000, 40_000); // byte-stable, 10s gap, big tr → ABNORMAL provider-side + // 25 minutes later, identical body again → normal TTL expiry. + const idle = T0 + 10_000 + 25 * 60_000; + at(idle); noteForwardedBody(s, body(["a", "b"])); + settle(s, idle, 100_000, 30_000); + } finally { + Date.now = realNow; + } + const led = getCacheLedger(s); + const a = led.agg as unknown as { providerSideMisses: number; providerSideMissed: number; providerTtlMisses: number; providerTtlMissed: number }; + assert.equal(a.providerSideMisses, 1, "short-gap byte-stable miss stays abnormal"); + assert.ok(a.providerSideMissed > 0); + assert.equal(a.providerTtlMisses, 1, "long-idle byte-stable miss is re-labeled normal TTL"); + assert.ok(a.providerTtlMissed > 0); +});