diff --git a/kernel/package-lock.json b/kernel/package-lock.json index da011d94e1..4c5e2d79e9 100644 --- a/kernel/package-lock.json +++ b/kernel/package-lock.json @@ -1,12 +1,12 @@ { "name": "acp-kernel", - "version": "0.0.164", + "version": "0.0.165", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "acp-kernel", - "version": "0.0.164", + "version": "0.0.165", "license": "MIT", "devDependencies": { "@types/node": "^22.0.0", diff --git a/package-lock.json b/package-lock.json index 380212c96e..15a6e3f4e9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -50,7 +50,7 @@ }, "kernel": { "name": "acp-kernel", - "version": "0.0.163", + "version": "0.0.165", "dev": true, "license": "MIT", "devDependencies": { diff --git a/src/persist.ts b/src/persist.ts index 7600224a08..b8679fb74f 100644 --- a/src/persist.ts +++ b/src/persist.ts @@ -1418,10 +1418,12 @@ export class SessionStore { * signature in one grep: >0 proves compression ran in this session's * lifetime, so zero blocks means folds existed and are now missing * (investigate); 0 means the session was likely never folded (raw replay - * is its normal shape). (#2734 removed the last writer of the legacy - * stats.tokensSaved counter — reading it here logged a dead 0 and broke - * exactly this disambiguation, so the diagnostic now reads the kernel's - * live counter instead.) The warn stays broad on purpose — a loss mode + * is its normal shape). (#2734 moved this diagnostic off the legacy + * stats.tokensSaved counter — dead since the kernel import, reading it + * logged a misleading 0 that broke exactly this disambiguation — onto the + * kernel's live counter. #4700 restored the host-side writer; the kernel + * counter stays the right read here regardless.) The warn stays broad on + * purpose — a loss mode * that erases stats must not be able to go silent again. */ private logColdLoad(id: string, source: string, session: Session, savedAt?: number): void { const refs = Object.keys(session.state.messageRefs.byRef ?? {}).length; diff --git a/src/stream.ts b/src/stream.ts index ae55d2de1b..2cd2d9bb80 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -1343,6 +1343,17 @@ export function applyRanges(parsed: ReturnType, ctx: // so the next nudge decision sees post-compress reality instead of // re-firing on the stale pre-compress number (#252 double-inject). ctx.session.stats.compressCreditTokens = (ctx.session.stats.compressCreditTokens ?? 0) + r.tokensCompressed; + // #4700: restore the writer of the cumulative savings counter. It died + // with the kernel import, and #2734's web derivation from ACTIVE blocks' + // compressedTokens is the only live source left — so whenever folded + // blocks deactivate (client-side /compact archive, orphan-GC, tier + // distillation, host expand) the SAVED column collapses to 0 even + // though real folds happened. Credit the gross at this single commit + // point shared by every lane (tool-driven proxy/plugin folds AND + // preflight): refused paths return earlier, partial folds' + // r.tokensCompressed counts only successful ranges, and in-place + // refolds report tokens 0, so nothing double-counts. + ctx.session.stats.tokensSaved += r.tokensCompressed; // #1911: deliberately NO estimate fallback here. Writing viewTokens − S // into lastInputTokens would leave an estimate-grade value in a // usage-grade field (source stays "usage") and arm kernel context-space diff --git a/src/web/sessions-data.ts b/src/web/sessions-data.ts index 0d48585668..af859ff690 100644 --- a/src/web/sessions-data.ts +++ b/src/web/sessions-data.ts @@ -696,11 +696,13 @@ function summaryOf(s: SummarySource, live: boolean): WebSessionSummary { coverageLostFrozenTokens += Math.max(0, avoided); } } - // #2734: stats.tokensSaved is a dead counter upstream (no incrementer since - // the kernel import — restored sessions carry a stale stamp, newer ones 0). - // Derive the display value from active blocks (gross original tokens under - // fold) and keep the stored stat as a floor, so pre-tagging sessions that - // still have a real accumulated value never regress. + // #2734/#4700: derive the display value from active blocks (gross original + // tokens under fold) and keep the stored stat as a floor, so pre-tagging + // sessions that still have a real accumulated value never regress. Since + // #4700 stats.tokensSaved has a live writer again (credited at the + // applyRanges commit point); the floor also covers folds whose blocks + // deactivated afterwards (client-side /compact archive, orphan-GC, tier + // distillation, host expand) — exactly when the active-block sum collapses. let blocksSaved = 0; for (const b of s.state.blocks) if (b.active !== false) blocksSaved += b.compressedTokens ?? 0; const tokensSaved = Math.max(s.stats.tokensSaved ?? 0, blocksSaved); diff --git a/tests/issue4700-tokens-saved.test.ts b/tests/issue4700-tokens-saved.test.ts new file mode 100644 index 0000000000..6b96726bb6 --- /dev/null +++ b/tests/issue4700-tokens-saved.test.ts @@ -0,0 +1,124 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import type { CoreMessage } from "acp-kernel"; +import { createCore, createInitialState, assignRefs, emptyRefMap, defaultConfig } from "acp-kernel"; +import { parseCompressInput } from "../src/compress-tool.ts"; +import { applyRanges, type RewriteCtx } from "../src/stream.ts"; + +type Ctx = Omit & { log: (m: string) => void; logs: string[] }; + +function makeCtx(messages: CoreMessage[]): Ctx { + const logs: string[] = []; + return { + core: createCore(), + config: defaultConfig(200000), + messages, + session: { + id: "issue4700-tokens-saved", + meta: {}, + stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 0, compressCreditTokens: 0, contextTokens: 0, retrieveCalls: 0, retrieveHits: 0, retrieveMisses: 0, storedBytes: 0, storeBytesSaved: 0, rangeRestores: 0 }, + metadata: {}, + state: createInitialState(), + createdAt: Date.now(), + lastSeen: Date.now(), + blockContents: new Map(), + inFlight: 0, + persisted: false, + pendingRetrievals: [], + }, + log: (m: string) => { logs.push(m); }, + logs, + }; +} + +function withRefs(ctx: Ctx): Ctx { + const res = assignRefs(ctx.messages, { existing: emptyRefMap(), nextIndex: 0 }); + ctx.session.state.messageRefs = res.map; + return ctx; +} + +function textMsg(id: string, role: "user" | "assistant", text: string): CoreMessage { + return { id, role, contentType: "text", text }; +} + +// Same shape as tests/compress-observability.test.ts: seven compressible +// messages, refs m00001–m00007, m00005–m00007 inside the protected zone +// (last three + most recent user message). +function makeCompressibleCtx(): Ctx { + return withRefs(makeCtx([ + textMsg("raw_1", "user", "x".repeat(20000)), + textMsg("raw_2", "assistant", "x".repeat(20000)), + textMsg("raw_3", "user", "x".repeat(5000)), + textMsg("raw_4", "assistant", "x".repeat(5000)), + textMsg("raw_5", "user", "x".repeat(5000)), + textMsg("raw_6", "assistant", "x".repeat(5000)), + textMsg("raw_7", "user", "x".repeat(5000)), + ])); +} + +const SUMMARY = "OBS-TEST-SUMMARY-PAYLOAD-LONG-ENOUGH-FOR-THE-KERNEL-MIN-LENGTH-CHECK"; + +function runApply(ctx: Ctx, args: unknown): string { + const ranges = parseCompressInput(args); + return applyRanges(ranges, ctx).text; +} + +test("#4700: tool-driven fold credits session.stats.tokensSaved with the gross covered tokens", () => { + const ctx = makeCompressibleCtx(); + const out = runApply(ctx, { content: [{ startId: "m00001", endId: "m00002", summary: SUMMARY }] }); + assert.ok(out.startsWith("[Compressed"), `expected success, got: ${out}`); + const blocks = ctx.session.state.blocks; + assert.equal(blocks.length, 1, "one block created"); + const credited = ctx.session.stats.tokensSaved; + assert.ok(credited > 0, `tokensSaved credited (got ${credited})`); + assert.equal(credited, blocks[0].compressedTokens, "credit == the block's gross compressedTokens"); + assert.equal(credited, ctx.session.lastCompress?.tokensCompressed, "credit == lastCompress.tokensCompressed (same caliber the obs log reports)"); +}); + +test("#4700: consecutive folds accumulate (reporter's 12789+8214 → 21003 shape)", () => { + const ctx = makeCompressibleCtx(); + runApply(ctx, { content: [{ startId: "m00001", endId: "m00002", summary: SUMMARY }] }); + const first = ctx.session.stats.tokensSaved; + assert.ok(first > 0, `first fold credited (got ${first})`); + runApply(ctx, { content: [{ startId: "m00003", endId: "m00004", summary: SUMMARY }] }); + const second = ctx.session.stats.tokensSaved - first; + assert.equal(second, ctx.session.state.blocks[1].compressedTokens, "second credit == its block's gross"); + assert.equal(ctx.session.stats.tokensSaved, ctx.session.state.blocks[0].compressedTokens + ctx.session.state.blocks[1].compressedTokens, "lifetime total == sum over created blocks"); +}); + +test("#4700: refused fold credits nothing", () => { + const ctx = makeCompressibleCtx(); + // Entirely inside the protected zone (last three + most recent user message). + const out = runApply(ctx, { content: [{ startId: "m00005", endId: "m00007", summary: SUMMARY }] }); + assert.ok(!out.startsWith("[Compressed"), `expected refusal, got: ${out}`); + assert.equal(ctx.session.stats.tokensSaved, 0, "no credit on refusal"); + assert.equal(ctx.session.state.blocks.length, 0, "no block created"); +}); + +test("#4700: repeat call over an already folded range credits nothing again", () => { + const ctx = makeCompressibleCtx(); + runApply(ctx, { content: [{ startId: "m00001", endId: "m00002", summary: SUMMARY }] }); + const once = ctx.session.stats.tokensSaved; + assert.ok(once > 0, `fold credited (got ${once})`); + const out = runApply(ctx, { content: [{ startId: "m00001", endId: "m00002", summary: SUMMARY }] }); + assert.ok(!out.startsWith("[Compressed"), `expected refusal, got: ${out}`); + assert.equal(ctx.session.stats.tokensSaved, once, "no double credit"); +}); + +test("#4700: SAVED column survives full deactivation — mirrors the web derivation", () => { + const ctx = makeCompressibleCtx(); + runApply(ctx, { content: [{ startId: "m00001", endId: "m00002", summary: SUMMARY }] }); + runApply(ctx, { content: [{ startId: "m00003", endId: "m00004", summary: SUMMARY }] }); + const total = ctx.session.stats.tokensSaved; + assert.ok(total > 0, `both folds credited (got ${total})`); + // Client-side /compact archives the pre-compaction blocks: every fold + // block flips to active:false while its mass stays out of the wire. + for (const b of ctx.session.state.blocks) b.active = false; + // Mirrors src/web/sessions-data.ts:693-695 — before #4700 this derived + // 0 here (dead counter + zero active blocks), which IS the reported bug. + let blocksSaved = 0; + for (const b of ctx.session.state.blocks) if (b.active !== false) blocksSaved += b.compressedTokens ?? 0; + const displayed = Math.max(ctx.session.stats.tokensSaved ?? 0, blocksSaved); + assert.equal(displayed, total, "SAVED keeps the lifetime total after deactivation"); + assert.ok(displayed > 0, "column no longer collapses to 0"); +}); diff --git a/tests/web-sessions.test.ts b/tests/web-sessions.test.ts index dadf735c97..696b4745d5 100644 --- a/tests/web-sessions.test.ts +++ b/tests/web-sessions.test.ts @@ -327,6 +327,39 @@ withSessionsDir("session detail exposes decompress usage counters (#3671 directi assert.equal(dz.rangeRestores, 0); }); +withSessionsDir("credited tokensSaved survives full deactivation (#4700)", async (dir) => { + const store = new SessionStore({ dir, debounceMs: 0, enabled: true }); + const mk = (blockId: string, compressedTokens: number, active: boolean): CompressionBlock => ({ + blockId, + runId: "r1", + tier: 1, + topic: "T", + summary: "S", + directMessageIds: [], + effectiveMessageIds: [], + directBlockIds: [], + compressedTokens, + createdAt: Date.now(), + survivedCount: 1, + generation: "young", + active, + }); + // Post-#4700 state: the tool-driven folds credited the lifetime total + // (12789+8214 shape from the issue), then a client-side /compact + // deactivated every block. The column must keep showing the total. + const s = makeSession("det-save-4700", { protocol: "anthropic" }, { requests: 107, tokensSaved: 21003, contextTokens: 100 }); + s.state.blocks.push(mk("b1", 12789, false), mk("b2", 8214, false)); + await store.writeNow(s); + + _setStoreForTest(new SessionStore({ enabled: false })); + _resetDiskCacheForTest(); + const d = await buildSessionDetail("det-save-4700"); + assert.ok(d); + assert.equal(d.tokensSaved, 21003, "counter floor keeps SAVED alive when every block is inactive"); + const row = (await buildSessionList()).find((x) => x.id === "det-save-4700"); + assert.equal(row?.tokensSaved, 21003, "list rows derive the same value"); +}); + withSessionsDir("session detail surfaces the per-file storage-mode stamp (#2671)", async (dir) => { const saved: Array = [process.env.BILI_STORAGE_UNIFIED, process.env.BILI_ENCRYPTION_KEY]; const setEnv = (unified: string | undefined, key: string | undefined) => {