|
1 | 1 | import { describe, expect, it } from "vite-plus/test"; |
2 | 2 | import * as DateTime from "effect/DateTime"; |
| 3 | +import { |
| 4 | + ContextTransferId, |
| 5 | + ProviderDriverKind, |
| 6 | + ProviderInstanceId, |
| 7 | + RunId, |
| 8 | + ThreadId, |
| 9 | +} from "@t3tools/contracts"; |
| 10 | +import { makeThreadProjectionFixture } from "../test-fixtures"; |
| 11 | +import { shouldOfferResumeCompaction } from "../components/chat/ContextWindowMeter.logic"; |
3 | 12 | import { |
4 | 13 | deriveLatestContextWindowSnapshot, |
5 | 14 | formatContextWindowTokens, |
@@ -108,4 +117,125 @@ describe("latestProviderTurnTokenUsage", () => { |
108 | 117 | ]); |
109 | 118 | expect(latest?.usedTokens).toBe(300_000); |
110 | 119 | }); |
| 120 | + |
| 121 | + const sourceUsage = { |
| 122 | + usedTokens: 513_000, |
| 123 | + maxTokens: 1_000_000, |
| 124 | + promptCacheTtlMs: 5 * 60_000, |
| 125 | + updatedAt: "2026-10-19T12:00:00.000Z", |
| 126 | + }; |
| 127 | + function forkProjection() { |
| 128 | + const projection = makeThreadProjectionFixture(); |
| 129 | + const sourceThreadId = ThreadId.make("parent"); |
| 130 | + const runId = RunId.make("fork-point"); |
| 131 | + return { |
| 132 | + thread: { |
| 133 | + ...projection.thread, |
| 134 | + forkedFrom: { type: "run" as const, threadId: sourceThreadId, runId }, |
| 135 | + }, |
| 136 | + contextTransfers: [ |
| 137 | + { |
| 138 | + id: ContextTransferId.make("fork-transfer"), |
| 139 | + type: "fork" as const, |
| 140 | + sourceThreadId, |
| 141 | + targetThreadId: projection.thread.id, |
| 142 | + sourcePoint: { threadId: sourceThreadId, runId }, |
| 143 | + basePoint: null, |
| 144 | + sourceProviderInstanceId: projection.thread.modelSelection.instanceId, |
| 145 | + sourceTokenUsage: sourceUsage, |
| 146 | + targetProviderInstanceId: null, |
| 147 | + targetRunId: null, |
| 148 | + status: "pending" as const, |
| 149 | + resolution: null, |
| 150 | + createdBy: "user" as const, |
| 151 | + error: null, |
| 152 | + createdAt: projection.updatedAt, |
| 153 | + updatedAt: projection.updatedAt, |
| 154 | + consumedAt: null, |
| 155 | + }, |
| 156 | + ], |
| 157 | + }; |
| 158 | + } |
| 159 | + |
| 160 | + it("keeps fork-point context and its cache expiry before the fork reports usage", () => { |
| 161 | + const usage = latestProviderTurnTokenUsage([], forkProjection()); |
| 162 | + expect(usage).toEqual(sourceUsage); |
| 163 | + const snapshot = deriveLatestContextWindowSnapshot([], usage); |
| 164 | + expect(snapshot?.usedTokens).toBe(513_000); |
| 165 | + expect( |
| 166 | + shouldOfferResumeCompaction({ |
| 167 | + provider: "claudeAgent", |
| 168 | + usedTokens: snapshot?.usedTokens, |
| 169 | + updatedAt: snapshot?.updatedAt, |
| 170 | + promptCacheTtlMs: usage?.promptCacheTtlMs, |
| 171 | + now: "2026-10-19T12:06:00.000Z", |
| 172 | + }), |
| 173 | + ).toBe(true); |
| 174 | + }); |
| 175 | + |
| 176 | + it("replaces inherited usage with the fork's own report", () => { |
| 177 | + const tokenUsage = { usedTokens: 42_000, updatedAt: "2026-10-19T12:10:00.000Z" }; |
| 178 | + expect(latestProviderTurnTokenUsage([{ tokenUsage }], forkProjection())).toEqual(tokenUsage); |
| 179 | + }); |
| 180 | + |
| 181 | + it.each([0, 42_000])( |
| 182 | + "prefers target provider-thread usage of %d tokens over inherited usage", |
| 183 | + (usedTokens) => { |
| 184 | + const providerThread = { |
| 185 | + contextUsage: { |
| 186 | + usedTokens, |
| 187 | + maxTokens: 200_000, |
| 188 | + cost: { amount: 0.42, currency: "USD" }, |
| 189 | + }, |
| 190 | + updatedAt: DateTime.makeUnsafe("2026-10-19T12:10:00.000Z"), |
| 191 | + }; |
| 192 | + const usage = latestProviderTurnTokenUsage([], forkProjection(), providerThread); |
| 193 | + const snapshot = deriveLatestContextWindowSnapshot([], usage, providerThread); |
| 194 | + |
| 195 | + expect(snapshot).toMatchObject({ |
| 196 | + usedTokens, |
| 197 | + maxTokens: 200_000, |
| 198 | + cost: { amount: 0.42, currency: "USD" }, |
| 199 | + updatedAt: "2026-10-19T12:10:00.000Z", |
| 200 | + }); |
| 201 | + }, |
| 202 | + ); |
| 203 | + |
| 204 | + it("keeps the fallback after a native fork is consumed", () => { |
| 205 | + const projection = forkProjection(); |
| 206 | + const transfer = projection.contextTransfers[0]!; |
| 207 | + expect( |
| 208 | + latestProviderTurnTokenUsage([], { |
| 209 | + ...projection, |
| 210 | + contextTransfers: [ |
| 211 | + { |
| 212 | + ...transfer, |
| 213 | + status: "consumed", |
| 214 | + resolution: { |
| 215 | + strategy: "native_fork", |
| 216 | + providerThreadRef: { |
| 217 | + driver: ProviderDriverKind.make("claudeAgent"), |
| 218 | + nativeId: "fork", |
| 219 | + strength: "strong", |
| 220 | + }, |
| 221 | + }, |
| 222 | + }, |
| 223 | + ], |
| 224 | + }), |
| 225 | + ).toEqual(sourceUsage); |
| 226 | + }); |
| 227 | + |
| 228 | + it("does not reuse usage for portable handoffs, different providers, or missing history", () => { |
| 229 | + const projection = forkProjection(); |
| 230 | + const transfer = projection.contextTransfers[0]!; |
| 231 | + for (const contextTransfers of [ |
| 232 | + [], |
| 233 | + [{ ...transfer, sourceTokenUsage: undefined }], |
| 234 | + [{ ...transfer, status: "resolved_portable" as const }], |
| 235 | + [{ ...transfer, sourceProviderInstanceId: ProviderInstanceId.make("other") }], |
| 236 | + [{ ...transfer, sourcePoint: { ...transfer.sourcePoint, runId: RunId.make("later") } }], |
| 237 | + ]) { |
| 238 | + expect(latestProviderTurnTokenUsage([], { ...projection, contextTransfers })).toBeNull(); |
| 239 | + } |
| 240 | + }); |
111 | 241 | }); |
0 commit comments