





















@@ -252,6 +252,53 @@ describe("session cost usage", () => {
252252});
253253});
254254255+it("treats a pre-upgrade (older-version) durable cache as stale so unpriced usage is rebuilt", async () => {
256+const root = await makeSessionCostRoot("cost-cache-upgrade");
257+const sessionsDir = path.join(root, "agents", "main", "sessions");
258+await fs.mkdir(sessionsDir, { recursive: true });
259+const sessionFile = path.join(sessionsDir, "sess-upgrade.jsonl");
260+const entry = {
261+type: "message",
262+timestamp: "2026-02-05T12:00:00.000Z",
263+message: {
264+role: "assistant",
265+provider: "openai",
266+model: "gpt-5.5",
267+usage: {
268+input: 881,
269+output: 6,
270+cacheRead: 22400,
271+cacheWrite: 0,
272+totalTokens: 23287,
273+cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
274+},
275+},
276+};
277+await fs.writeFile(sessionFile, transcriptText("sess-upgrade", entry), "utf-8");
278+279+clearGatewayModelPricingCacheState();
280+await withStateDir(root, async () => {
281+// Simulate a durable cache written by a build from before this change: refresh
282+// under the current code, then stamp the cache with an older semantics version.
283+await refreshCostUsageCache({ sessionFiles: [sessionFile] });
284+const cachePath = path.join(sessionsDir, ".usage-cost-cache.json");
285+const cache = JSON.parse(await fs.readFile(cachePath, "utf-8")) as { version: number };
286+cache.version = 3;
287+await fs.writeFile(cachePath, `${JSON.stringify(cache)}\n`, "utf-8");
288+289+// The pre-upgrade cache must be treated as stale (not served), forcing a rebuild
290+// under the new missing-cost semantics instead of reusing old complete-$0 totals.
291+const cached = await loadSessionCostSummaryFromCache({
292+sessionId: "sess-upgrade",
293+ sessionFile,
294+startMs: Date.UTC(2026, 1, 5),
295+endMs: Date.UTC(2026, 1, 5) + 24 * 60 * 60 * 1000 - 1,
296+requestRefresh: false,
297+});
298+expect(cached.cacheStatus.status).toBe("stale");
299+});
300+});
301+255302it("ignores compaction checkpoint transcript snapshots in daily totals and discovery", async () => {
256303const root = await makeSessionCostRoot("cost-checkpoint");
257304const sessionsDir = path.join(root, "agents", "main", "sessions");
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。