The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | import type { ThreadStructureTurnItem } from "@t3tools/client-runtime/state/shell"; |
| 3 | import { OrchestrationV2ProviderTurnTokenUsage } from "@t3tools/contracts"; |
| 4 | import * as Schema from "effect/Schema"; |
| 5 | import { formatContextWindowCompactionMessage } from "../components/chat/compaction.logic"; |
| 6 | import * as DateTime from "effect/DateTime"; |
| 7 | import { |
| 8 | ContextTransferId, |
| 9 | ProviderDriverKind, |
| 10 | ProviderInstanceId, |
| 11 | ProviderThreadId, |
| 12 | RunId, |
| 13 | ThreadId, |
| 14 | } from "@t3tools/contracts"; |
| 15 | import { makeThreadProjectionFixture } from "../test-fixtures"; |
| 16 | import { |
| 17 | deriveLatestContextWindowSnapshot, |
| 18 | latestProviderTurnTokenUsage, |
| 19 | shouldOfferResumeCompaction, |
| 20 | } from "@t3tools/client-runtime/state/context-window"; |
| 21 | |
| 22 | const decodeTokenUsage = Schema.decodeSync(OrchestrationV2ProviderTurnTokenUsage); |
| 23 | |
| 24 | const compactionEntry = (providerThreadId: string | null) => |
| 25 | ({ |
| 26 | item: { |
| 27 | id: "compaction-1" as never, |
| 28 | threadId: "thread-1" as never, |
| 29 | runId: null, |
| 30 | nodeId: null, |
| 31 | providerThreadId: providerThreadId as never, |
| 32 | providerTurnId: null, |
| 33 | nativeItemRef: null, |
| 34 | parentItemId: null, |
| 35 | ordinal: 1, |
| 36 | status: "completed", |
| 37 | title: null, |
| 38 | startedAt: null, |
| 39 | completedAt: null, |
| 40 | updatedAt: DateTime.makeUnsafe("2026-06-20T00:00:00.000Z"), |
| 41 | type: "compaction", |
| 42 | driver: null, |
| 43 | beforeTokenCount: 10_000, |
| 44 | afterTokenCount: 2_000, |
| 45 | }, |
| 46 | }) satisfies { item: ThreadStructureTurnItem }; |
| 47 | |
| 48 | describe("V2 context window presentation", () => { |
| 49 | it("uses retained compaction token data when available", () => { |
| 50 | const snapshot = deriveLatestContextWindowSnapshot([compactionEntry(null)]); |
| 51 | expect(snapshot?.usedTokens).toBe(2_000); |
| 52 | expect(snapshot?.totalProcessedTokens).toBe(10_000); |
| 53 | }); |
| 54 | |
| 55 | it("ignores a retired provider thread's compaction after a provider switch", () => { |
| 56 | const activeProviderThread = { |
| 57 | id: ProviderThreadId.make("acp-thread"), |
| 58 | contextUsage: null, |
| 59 | updatedAt: DateTime.makeUnsafe("2026-10-19T12:10:00.000Z"), |
| 60 | }; |
| 61 | const retired = compactionEntry(ProviderThreadId.make("pi-thread")); |
| 62 | const current = compactionEntry(ProviderThreadId.make("acp-thread")); |
| 63 | expect(deriveLatestContextWindowSnapshot([retired], null, activeProviderThread)).toBeNull(); |
| 64 | expect( |
| 65 | deriveLatestContextWindowSnapshot([current], null, activeProviderThread)?.usedTokens, |
| 66 | ).toBe(2_000); |
| 67 | // Items without a provider thread predate the split and stay visible. |
| 68 | expect( |
| 69 | deriveLatestContextWindowSnapshot([compactionEntry(null)], null, activeProviderThread) |
| 70 | ?.usedTokens, |
| 71 | ).toBe(2_000); |
| 72 | }); |
| 73 | |
| 74 | it("prefers a completed compaction over usage reported before it", () => { |
| 75 | const usageBeforeCompaction = decodeTokenUsage({ |
| 76 | usedTokens: 985_000, |
| 77 | maxTokens: 1_000_000, |
| 78 | updatedAt: "2026-06-19T23:55:00.000Z", |
| 79 | }); |
| 80 | const snapshot = deriveLatestContextWindowSnapshot( |
| 81 | [compactionEntry(null)], |
| 82 | usageBeforeCompaction, |
| 83 | ); |
| 84 | expect(snapshot?.usedTokens).toBe(2_000); |
| 85 | expect(snapshot?.maxTokens).toBe(1_000_000); |
| 86 | |
| 87 | // A report in the compaction's own millisecond is the provider's post-compaction usage. |
| 88 | const reportedWithCompaction = decodeTokenUsage({ |
| 89 | usedTokens: 2_000, |
| 90 | maxTokens: 1_000_000, |
| 91 | autoCompactThreshold: 900_000, |
| 92 | updatedAt: "2026-06-20T00:00:00.000Z", |
| 93 | }); |
| 94 | expect( |
| 95 | deriveLatestContextWindowSnapshot([compactionEntry(null)], reportedWithCompaction) |
| 96 | ?.autoCompactThreshold, |
| 97 | ).toBe(900_000); |
| 98 | |
| 99 | const { afterTokenCount: _, ...withoutCount } = compactionEntry(null).item; |
| 100 | expect( |
| 101 | deriveLatestContextWindowSnapshot([{ item: withoutCount }], usageBeforeCompaction), |
| 102 | ).toBeNull(); |
| 103 | // A metadata-only provider thread update reports no usage, so an older count stays hidden. |
| 104 | const olderCompaction = { |
| 105 | item: { |
| 106 | ...compactionEntry(null).item, |
| 107 | updatedAt: DateTime.makeUnsafe("2026-06-19T00:00:00.000Z"), |
| 108 | }, |
| 109 | }; |
| 110 | expect( |
| 111 | deriveLatestContextWindowSnapshot([olderCompaction, { item: withoutCount }], null, { |
| 112 | id: ProviderThreadId.make("thread"), |
| 113 | contextUsage: null, |
| 114 | updatedAt: DateTime.makeUnsafe("2026-06-21T00:00:00.000Z"), |
| 115 | }), |
| 116 | ).toBeNull(); |
| 117 | }); |
| 118 | }); |
| 119 | |
| 120 | describe("live provider-turn usage (#8144)", () => { |
| 121 | it("prefers the provider's live report over compaction items", () => { |
| 122 | const usage = decodeTokenUsage({ |
| 123 | usedTokens: 42_000, |
| 124 | maxTokens: 200_000, |
| 125 | inputTokens: 40_000, |
| 126 | outputTokens: 2_000, |
| 127 | autoCompactThreshold: 150_000, |
| 128 | updatedAt: "2026-08-27T00:00:00.000Z", |
| 129 | }); |
| 130 | const snapshot = deriveLatestContextWindowSnapshot([], usage); |
| 131 | expect(snapshot).not.toBeNull(); |
| 132 | expect(snapshot?.usedTokens).toBe(42_000); |
| 133 | expect(snapshot?.maxTokens).toBe(200_000); |
| 134 | expect(snapshot?.remainingTokens).toBe(158_000); |
| 135 | expect(snapshot?.usedPercentage).toBe(21); |
| 136 | expect(snapshot?.autoCompactThreshold).toBe(150_000); |
| 137 | expect(formatContextWindowCompactionMessage(snapshot?.autoCompactThreshold)).toBe( |
| 138 | "Compacts automatically at about 150,000 tokens.", |
| 139 | ); |
| 140 | }); |
| 141 | }); |
| 142 | |
| 143 | describe("latestProviderTurnTokenUsage", () => { |
| 144 | it("picks the newest report, not the last turn in snapshot order", () => { |
| 145 | const providerThreadId = ProviderThreadId.make("provider-thread"); |
| 146 | const usage = (usedTokens: number, updatedAt: string) => ({ |
| 147 | providerThreadId, |
| 148 | tokenUsage: { usedTokens, updatedAt }, |
| 149 | }); |
| 150 | const latest = latestProviderTurnTokenUsage([ |
| 151 | usage(300_000, "2026-06-20T12:00:00.000Z"), |
| 152 | { providerThreadId }, |
| 153 | usage(90_000, "2026-06-20T09:00:00.000Z"), |
| 154 | ]); |
| 155 | expect(latest?.usedTokens).toBe(300_000); |
| 156 | }); |
| 157 | |
| 158 | const sourceUsage = { |
| 159 | usedTokens: 513_000, |
| 160 | maxTokens: 1_000_000, |
| 161 | promptCacheTtlMs: 5 * 60_000, |
| 162 | updatedAt: "2026-10-19T12:00:00.000Z", |
| 163 | }; |
| 164 | function forkProjection() { |
| 165 | const projection = makeThreadProjectionFixture(); |
| 166 | const sourceThreadId = ThreadId.make("parent"); |
| 167 | const runId = RunId.make("fork-point"); |
| 168 | return { |
| 169 | thread: { |
| 170 | ...projection.thread, |
| 171 | forkedFrom: { type: "run" as const, threadId: sourceThreadId, runId }, |
| 172 | }, |
| 173 | contextTransfers: [ |
| 174 | { |
| 175 | id: ContextTransferId.make("fork-transfer"), |
| 176 | type: "fork" as const, |
| 177 | sourceThreadId, |
| 178 | targetThreadId: projection.thread.id, |
| 179 | sourcePoint: { threadId: sourceThreadId, runId }, |
| 180 | basePoint: null, |
| 181 | sourceProviderInstanceId: projection.thread.modelSelection.instanceId, |
| 182 | sourceTokenUsage: sourceUsage, |
| 183 | targetProviderInstanceId: null, |
| 184 | targetRunId: null, |
| 185 | status: "pending" as const, |
| 186 | resolution: null, |
| 187 | createdBy: "user" as const, |
| 188 | error: null, |
| 189 | createdAt: projection.updatedAt, |
| 190 | updatedAt: projection.updatedAt, |
| 191 | consumedAt: null, |
| 192 | }, |
| 193 | ], |
| 194 | }; |
| 195 | } |
| 196 | |
| 197 | it("keeps fork-point context and its cache expiry before the fork reports usage", () => { |
| 198 | const usage = latestProviderTurnTokenUsage([], forkProjection()); |
| 199 | expect(usage).toEqual(sourceUsage); |
| 200 | const snapshot = deriveLatestContextWindowSnapshot([], usage); |
| 201 | expect(snapshot?.usedTokens).toBe(513_000); |
| 202 | expect( |
| 203 | shouldOfferResumeCompaction({ |
| 204 | provider: "claudeAgent", |
| 205 | usedTokens: snapshot?.usedTokens, |
| 206 | updatedAt: snapshot?.updatedAt, |
| 207 | promptCacheTtlMs: usage?.promptCacheTtlMs, |
| 208 | now: "2026-10-19T12:06:00.000Z", |
| 209 | }), |
| 210 | ).toBe(true); |
| 211 | }); |
| 212 | |
| 213 | it("replaces inherited usage with the fork's own report", () => { |
| 214 | const tokenUsage = { usedTokens: 42_000, updatedAt: "2026-10-19T12:10:00.000Z" }; |
| 215 | expect( |
| 216 | latestProviderTurnTokenUsage( |
| 217 | [{ providerThreadId: ProviderThreadId.make("provider-thread"), tokenUsage }], |
| 218 | forkProjection(), |
| 219 | ), |
| 220 | ).toEqual(tokenUsage); |
| 221 | }); |
| 222 | |
| 223 | it("prefers the active provider thread's older report over a newer retired one", () => { |
| 224 | const turns = [ |
| 225 | { |
| 226 | providerThreadId: ProviderThreadId.make("pi-thread"), |
| 227 | tokenUsage: decodeTokenUsage({ |
| 228 | usedTokens: 180_000, |
| 229 | maxTokens: 200_000, |
| 230 | updatedAt: "2026-10-19T12:10:00.000Z", |
| 231 | }), |
| 232 | }, |
| 233 | { |
| 234 | providerThreadId: ProviderThreadId.make("acp-thread"), |
| 235 | tokenUsage: decodeTokenUsage({ |
| 236 | usedTokens: 12_000, |
| 237 | maxTokens: 200_000, |
| 238 | updatedAt: "2026-10-19T11:00:00.000Z", |
| 239 | }), |
| 240 | }, |
| 241 | ]; |
| 242 | const liveUsage = latestProviderTurnTokenUsage(turns, null, { |
| 243 | id: ProviderThreadId.make("acp-thread"), |
| 244 | contextUsage: null, |
| 245 | }); |
| 246 | expect(liveUsage?.usedTokens).toBe(12_000); |
| 247 | }); |
| 248 | |
| 249 | it.each([0, 42_000])( |
| 250 | "prefers target provider-thread usage of %d tokens over inherited usage", |
| 251 | (usedTokens) => { |
| 252 | const providerThread = { |
| 253 | id: ProviderThreadId.make("provider-thread"), |
| 254 | contextUsage: { |
| 255 | usedTokens, |
| 256 | maxTokens: 200_000, |
| 257 | cost: { amount: 0.42, currency: "USD" }, |
| 258 | }, |
| 259 | updatedAt: DateTime.makeUnsafe("2026-10-19T12:10:00.000Z"), |
| 260 | }; |
| 261 | const usage = latestProviderTurnTokenUsage([], forkProjection(), providerThread); |
| 262 | const snapshot = deriveLatestContextWindowSnapshot([], usage, providerThread); |
| 263 | |
| 264 | expect(snapshot).toMatchObject({ |
| 265 | usedTokens, |
| 266 | maxTokens: 200_000, |
| 267 | cost: { amount: 0.42, currency: "USD" }, |
| 268 | updatedAt: "2026-10-19T12:10:00.000Z", |
| 269 | }); |
| 270 | }, |
| 271 | ); |
| 272 | |
| 273 | it("keeps the fallback after a native fork is consumed", () => { |
| 274 | const projection = forkProjection(); |
| 275 | const transfer = projection.contextTransfers[0]!; |
| 276 | expect( |
| 277 | latestProviderTurnTokenUsage([], { |
| 278 | ...projection, |
| 279 | contextTransfers: [ |
| 280 | { |
| 281 | ...transfer, |
| 282 | status: "consumed", |
| 283 | resolution: { |
| 284 | strategy: "native_fork", |
| 285 | providerThreadRef: { |
| 286 | driver: ProviderDriverKind.make("claudeAgent"), |
| 287 | nativeId: "fork", |
| 288 | strength: "strong", |
| 289 | }, |
| 290 | }, |
| 291 | }, |
| 292 | ], |
| 293 | }), |
| 294 | ).toEqual(sourceUsage); |
| 295 | }); |
| 296 | |
| 297 | it("does not reuse usage for portable handoffs, different providers, or missing history", () => { |
| 298 | const projection = forkProjection(); |
| 299 | const transfer = projection.contextTransfers[0]!; |
| 300 | for (const contextTransfers of [ |
| 301 | [], |
| 302 | [{ ...transfer, sourceTokenUsage: undefined }], |
| 303 | [{ ...transfer, status: "resolved_portable" as const }], |
| 304 | [{ ...transfer, sourceProviderInstanceId: ProviderInstanceId.make("other") }], |
| 305 | [{ ...transfer, sourcePoint: { ...transfer.sourcePoint, runId: RunId.make("later") } }], |
| 306 | ]) { |
| 307 | expect(latestProviderTurnTokenUsage([], { ...projection, contextTransfers })).toBeNull(); |
| 308 | } |
| 309 | }); |
| 310 | }); |