|
1 | 1 | import { describe, expect, it, vi } from "vitest"; |
2 | 2 | import { createAssistantMessageEventStream } from "../../llm.js"; |
3 | 3 | import type { AssistantMessage, Model, StreamFn } from "../../llm.js"; |
4 | | -import { compact, generateSummary } from "./compaction.js"; |
| 4 | +import { |
| 5 | + calculateContextTokens, |
| 6 | + compact, |
| 7 | + estimateContextTokens, |
| 8 | + generateSummary, |
| 9 | +} from "./compaction.js"; |
5 | 10 | import { createFileOps } from "./utils.js"; |
6 | 11 |
|
| 12 | +describe("calculateContextTokens", () => { |
| 13 | + it("prefers the final-iteration context snapshot over aggregate billing usage", () => { |
| 14 | + expect( |
| 15 | + calculateContextTokens({ |
| 16 | + input: 12, |
| 17 | + output: 15_104, |
| 18 | + cacheRead: 819_661, |
| 19 | + cacheWrite: 93_130, |
| 20 | + contextUsage: { |
| 21 | + state: "available", |
| 22 | + promptTokens: 148_874, |
| 23 | + totalTokens: 163_978, |
| 24 | + }, |
| 25 | + totalTokens: 927_907, |
| 26 | + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, |
| 27 | + }), |
| 28 | + ).toBe(163_978); |
| 29 | + }); |
| 30 | + |
| 31 | + it("preserves the numeric compatibility fallback when the snapshot is unavailable", () => { |
| 32 | + expect( |
| 33 | + calculateContextTokens({ |
| 34 | + input: 12, |
| 35 | + output: 15_104, |
| 36 | + cacheRead: 819_661, |
| 37 | + cacheWrite: 93_130, |
| 38 | + contextUsage: { state: "unavailable" }, |
| 39 | + totalTokens: 927_907, |
| 40 | + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, |
| 41 | + }), |
| 42 | + ).toBe(927_907); |
| 43 | + }); |
| 44 | + |
| 45 | + it("estimates the transcript instead of using aggregate billing when context is unavailable", () => { |
| 46 | + const estimate = estimateContextTokens([ |
| 47 | + { role: "user", content: "hello", timestamp: 0 }, |
| 48 | + { |
| 49 | + role: "assistant", |
| 50 | + content: [{ type: "text", text: "done" }], |
| 51 | + api: "anthropic-messages", |
| 52 | + provider: "anthropic", |
| 53 | + model: "claude-fable-5", |
| 54 | + usage: { |
| 55 | + input: 12, |
| 56 | + output: 15_104, |
| 57 | + cacheRead: 819_661, |
| 58 | + cacheWrite: 93_130, |
| 59 | + contextUsage: { state: "unavailable" }, |
| 60 | + totalTokens: 927_907, |
| 61 | + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, |
| 62 | + }, |
| 63 | + stopReason: "stop", |
| 64 | + timestamp: 1, |
| 65 | + }, |
| 66 | + ]); |
| 67 | + |
| 68 | + expect(estimate.tokens).toBeLessThan(927_907); |
| 69 | + expect(estimate.tokens).toBeGreaterThan(0); |
| 70 | + expect(estimate.usageTokens).toBe(0); |
| 71 | + expect(estimate.lastUsageIndex).toBeNull(); |
| 72 | + }); |
| 73 | + |
| 74 | + it("uses the previous exact snapshot and estimates only the unavailable tail", () => { |
| 75 | + const estimate = estimateContextTokens([ |
| 76 | + { |
| 77 | + role: "assistant", |
| 78 | + content: [{ type: "text", text: "previous" }], |
| 79 | + api: "anthropic-messages", |
| 80 | + provider: "anthropic", |
| 81 | + model: "claude-fable-5", |
| 82 | + usage: { |
| 83 | + input: 12, |
| 84 | + output: 1_000, |
| 85 | + cacheRead: 148_862, |
| 86 | + cacheWrite: 0, |
| 87 | + contextUsage: { |
| 88 | + state: "available", |
| 89 | + promptTokens: 148_874, |
| 90 | + totalTokens: 149_874, |
| 91 | + }, |
| 92 | + totalTokens: 149_874, |
| 93 | + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, |
| 94 | + }, |
| 95 | + stopReason: "stop", |
| 96 | + timestamp: 0, |
| 97 | + }, |
| 98 | + { role: "user", content: "next", timestamp: 1 }, |
| 99 | + { |
| 100 | + role: "assistant", |
| 101 | + content: [{ type: "text", text: "done" }], |
| 102 | + api: "anthropic-messages", |
| 103 | + provider: "anthropic", |
| 104 | + model: "claude-fable-5", |
| 105 | + usage: { |
| 106 | + input: 12, |
| 107 | + output: 15_104, |
| 108 | + cacheRead: 819_661, |
| 109 | + cacheWrite: 93_130, |
| 110 | + contextUsage: { state: "unavailable" }, |
| 111 | + totalTokens: 927_907, |
| 112 | + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, |
| 113 | + }, |
| 114 | + stopReason: "stop", |
| 115 | + timestamp: 2, |
| 116 | + }, |
| 117 | + ]); |
| 118 | + |
| 119 | + expect(estimate.usageTokens).toBe(149_874); |
| 120 | + expect(estimate.tokens).toBeGreaterThan(149_874); |
| 121 | + expect(estimate.tokens).toBeLessThan(927_907); |
| 122 | + expect(estimate.lastUsageIndex).toBe(0); |
| 123 | + }); |
| 124 | +}); |
| 125 | + |
7 | 126 | describe("generateSummary thinking options", () => { |
8 | 127 | it("maps explicit Fable off to low effort for compaction", async () => { |
9 | 128 | const model: Model = { |
|
0 commit comments