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
4 changes: 2 additions & 2 deletions kernel/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions src/persist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1343,6 +1343,17 @@ export function applyRanges(parsed: ReturnType<typeof parseCompressInput>, 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
Expand Down
12 changes: 7 additions & 5 deletions src/web/sessions-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
124 changes: 124 additions & 0 deletions tests/issue4700-tokens-saved.test.ts
Original file line number Diff line number Diff line change
@@ -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<RewriteCtx, "log"> & { 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");
});
33 changes: 33 additions & 0 deletions tests/web-sessions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | undefined> = [process.env.BILI_STORAGE_UNIFIED, process.env.BILI_ENCRYPTION_KEY];
const setEnv = (unified: string | undefined, key: string | undefined) => {
Expand Down
Loading