@devries-cameron20 / t3code Mirror · read-only / commits / 47e24f6cc2
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 13:30 UTC

fix(server): Grok usage counts turns from resumed sessions (#30694)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Alex Southwell committed Nov 15, 2026 at 11:32 UTC 47e24f6cc2fc9a45a44f71ce0fc5bfd5abb01ceb
8 files changed +117 -10
apps/server/src/usage/UsageService.test.ts
+1 -1
@@ -3207,7 +3207,7 @@ describe("UsageService", () => {
3207 60 + 20 * (version === 4 ? 1 : 2),
3208 );
3209 yield* upgraded.awaitPersisted;
3210 - assert.strictEqual(SCAN_CACHE_FILE_NAME, "usage-scan-cache-v16.json");
3210 + assert.strictEqual(SCAN_CACHE_FILE_NAME, "usage-scan-cache-v17.json");
3211 assert.deepStrictEqual(
3212 (yield* Effect.promise(() => NodeFSP.readdir(stateDir)))
3213 .filter((name) => name.startsWith("usage-scan-cache"))
apps/server/src/usage/usageAggregation.test.ts
+26
@@ -141,6 +141,32 @@ describe("UsageAggregator", () => {
141 }
142 });
143
144 + it("keeps a live Grok turn over a saved pre-v17 maximum in either source order", () => {
145 + const turn = (timestampMs: number, outputTokens: number) =>
146 + record({
147 + provider: "grok",
148 + model: "grok-4.5",
149 + timestampMs,
150 + dedupeKey: `s:p:${timestampMs}:grok-4.5`,
151 + reportedCostUsd: outputTokens / 10,
152 + totals: { ...record().totals, uncachedInputTokens: 0, outputTokens },
153 + });
154 + const first = turn(Date.parse("2026-08-08T10:00:00Z"), 10);
155 + const resumed = turn(Date.parse("2026-08-08T11:00:00Z"), 20);
156 + const savedMaximum = { ...first, totals: resumed.totals, reportedCostUsd: 2 };
157 +
158 + const expected = aggregate([first, resumed]).buckets;
159 +
160 + for (const order of [
161 + [first, resumed, savedMaximum],
162 + [savedMaximum, first, resumed],
163 + ]) {
164 + const result = aggregate(order);
165 + expect(result.duplicatesDropped).toBe(1);
166 + expect(result.buckets).toEqual(expected);
167 + }
168 + });
169 +
170 it("does not count a key whose first copy fell outside the window", () => {
171 const result = aggregate([
172 record({ dedupeKey: "msg_1:", timestampMs: Date.parse("2026-07-01T12:00:00Z") }),
apps/server/src/usage/usageAggregation.ts
+30 -1
@@ -170,6 +170,30 @@ export function mergeUsageCopies(held: UsageRecord, copy: UsageRecord): UsageRec
170 };
171 }
172
173 +/**
174 + * Grok reports each turn once, complete, so real copies are equal. A larger one is a deleted
175 + * transcript's pre-v17 maximum over turns that shared a prompt id, whichever source it came from.
176 + */
177 +function smallestGrokCopy(held: UsageRecord, copy: UsageRecord): UsageRecord {
178 + const a = held.totals;
179 + const b = copy.totals;
180 + const reportedCostUsd =
181 + held.reportedCostUsd === null || copy.reportedCostUsd === null
182 + ? (held.reportedCostUsd ?? copy.reportedCostUsd)
183 + : Math.min(held.reportedCostUsd, copy.reportedCostUsd);
184 + return {
185 + ...held,
186 + totals: {
187 + uncachedInputTokens: Math.min(a.uncachedInputTokens, b.uncachedInputTokens),
188 + cachedInputTokens: Math.min(a.cachedInputTokens, b.cachedInputTokens),
189 + cacheCreationTokens: Math.min(a.cacheCreationTokens, b.cacheCreationTokens),
190 + outputTokens: Math.min(a.outputTokens, b.outputTokens),
191 + reasoningTokens: Math.min(a.reasoningTokens, b.reasoningTokens),
192 + },
193 + reportedCostUsd,
194 + };
195 +}
196 +
197 interface Contribution {
198 record: UsageRecord;
199 /** Null when the first copy fell outside the window. */
@@ -217,7 +241,12 @@ export class UsageAggregator {
241 const held = this.#seen.get(record.dedupeKey);
242 if (held !== undefined) {
243 this.#duplicatesDropped += 1;
220 - if (held.bucket !== null) held.record = mergeUsageCopies(held.record, record);
244 + if (held.bucket !== null) {
245 + held.record =
246 + record.provider === "grok"
247 + ? smallestGrokCopy(held.record, record)
248 + : mergeUsageCopies(held.record, record);
249 + }
250 return false;
251 }
252 }
apps/server/src/usage/usageScanCache.test.ts
+23
@@ -339,6 +339,29 @@ describe("scan cache round trip", () => {
339 });
340 });
341
342 + it.each(["s:p:grok-4.5", "s:p:1786000000000:grok-4.5"])(
343 + "re-parses v16 Grok transcripts with key %s",
344 + (dedupeKey) => {
345 + const original = cacheWith([
346 + ["/grok/updates.jsonl", 100, [record({ provider: "grok", model: "grok-4.5", dedupeKey })]],
347 + ]);
348 + original.set("/grok/updates.jsonl", {
349 + ...original.get("/grok/updates.jsonl")!,
350 + provider: "grok",
351 + });
352 + const encoded = parse(makeScanCacheWriter()(original, {}));
353 + const restored = decodeScanCache({ ...encoded, version: 16 }, TEST_FORMAT_MAP);
354 + expect(restored.get("/grok/updates.jsonl")).toEqual({
355 + ...original.get("/grok/updates.jsonl"),
356 + size: -1,
357 + position: { resumeOffset: 0, guardLength: 0, guardHash: 0, state: null },
358 + records: [
359 + record({ provider: "grok", model: "grok-4.5", dedupeKey: "s:p:1786000000000:grok-4.5" }),
360 + ],
361 + });
362 + },
363 + );
364 +
365 it("rewrites only changed entries and still restores the whole cache", () => {
366 const write = makeScanCacheWriter();
367 const cache = cacheWith([
apps/server/src/usage/usageScanCache.ts
+15 -3
@@ -10,7 +10,7 @@ import { CLAUDE_SYNTHETIC_MODEL } from "../provider/Drivers/claudeUsage.ts";
10 import { mergeUsageCopies } from "./usageAggregation.ts";
11 import { GUARD_LENGTH, type TranscriptParsePosition } from "./usageTranscriptReader.ts";
12
13 -const USAGE_SCAN_CACHE_VERSION = 16 as const;
13 +const USAGE_SCAN_CACHE_VERSION = 17 as const;
14 const SPEED_COMPATIBLE_SINCE_VERSION = 4;
15
16 /**
@@ -18,8 +18,9 @@ const SPEED_COMPATIBLE_SINCE_VERSION = 4;
18 * replace it, dropping saved usage for deleted transcripts. Separate files keep
19 * the newer one; older files are removed once it is saved.
20 */
21 -export const SCAN_CACHE_FILE_NAME = "usage-scan-cache-v16.json";
21 +export const SCAN_CACHE_FILE_NAME = "usage-scan-cache-v17.json";
22 export const LEGACY_SCAN_CACHE_FILE_NAMES = [
23 + "usage-scan-cache-v16.json",
24 "usage-scan-cache-v15.json",
25 "usage-scan-cache-v14.json",
26 "usage-scan-cache-v13.json",
@@ -264,7 +265,16 @@ export function decodeScanCache(
265 ...(version >= 7 && unresolvedServiceTierTurnId != null
266 ? { unresolvedServiceTierTurnId }
267 : {}),
267 - dedupeKey: typeof dedupeKey === "string" ? dedupeKey : null,
268 + dedupeKey:
269 + typeof dedupeKey !== "string"
270 + ? null
271 + : // Saved copies of moved transcripts must still match the re-parsed v17 key.
272 + provider === "grok" &&
273 + version < 17 &&
274 + dedupeKey.endsWith(`:${model}`) &&
275 + !dedupeKey.endsWith(`:${timestampMs}:${model}`)
276 + ? `${dedupeKey.slice(0, -model.length)}${timestampMs}:${model}`
277 + : dedupeKey,
278 });
279 }
280 return records;
@@ -301,8 +311,10 @@ export function decodeScanCache(
311 // Keep history for deleted rollouts, but re-parse incompatible reducer state.
312 // v13 adds Claude 1-hour cache writes; v14 uses Codex request ledgers.
313 // v15 repairs skipped Codex requests whose counters rewound; clean v14 entries stay served.
314 + // v17 keys Grok turns by time, restoring turns previously dropped as repeats.
315 const legacy =
316 (entry.p === "claude" && version < 13) ||
317 + (entry.p === "grok" && version < 17) ||
318 (format.state !== undefined &&
319 (version < 6 ||
320 (entry.p === "codex" && (version < 14 || (version < 15 && malformedRecords > 0)))));
apps/server/src/usage/usageTranscriptStreaming.test.ts
+1 -1
@@ -254,7 +254,7 @@ describe("large usage records", () => {
254 outputTokens: 99,
255 reasoningTokens: 10,
256 },
257 - dedupeKey: "s1:p1:grok-4.5-build",
257 + dedupeKey: "s1:p1:1785578400123:grok-4.5-build",
258 reportedCostUsd: 0.25,
259 });
260 });
packages/provider-grok/src/server/usage.test.ts
+17 -2
@@ -72,7 +72,9 @@ describe("parseGrokLine", () => {
72 reasoningTokens: 180,
73 });
74 expect(record?.reportedCostUsd).toBeCloseTo(230_272_000 / GROK_COST_USD_TICKS_PER_DOLLAR, 12);
75 - expect(record?.dedupeKey).toBe("019fec1a-12f7-72f2-9b1f-7778a00aea3c:prompt-1:grok-4.5-build");
75 + expect(record?.dedupeKey).toBe(
76 + "019fec1a-12f7-72f2-9b1f-7778a00aea3c:prompt-1:1786372566485:grok-4.5-build",
77 + );
78 });
79
80 it("emits one record per model when modelUsage has several entries", () => {
@@ -142,7 +144,9 @@ describe("parseGrokLine", () => {
144 reasoningTokens: 180,
145 });
146 expect(record?.reportedCostUsd).toBeCloseTo(230_272_000 / GROK_COST_USD_TICKS_PER_DOLLAR, 12);
145 - expect(record?.dedupeKey).toBe("019fec1a-12f7-72f2-9b1f-7778a00aea3c:prompt-1:grok");
147 + expect(record?.dedupeKey).toBe(
148 + "019fec1a-12f7-72f2-9b1f-7778a00aea3c:prompt-1:1786372566485:grok",
149 + );
150 });
151
152 it("pro-rates top-level cost ticks across multi-model turns without per-model ticks", () => {
@@ -230,6 +234,17 @@ describe("parseGrokLine", () => {
234 expect(parseGrokLine(line)[0]?.dedupeKey).toBeNull();
235 });
236
237 + it("keeps later turns that reuse a prompt id after the session resumes in a new process", () => {
238 + const first = parseGrokLine(turnCompleted({ promptId: "t3-xai-prompt-1" }))[0];
239 + const resumed = parseGrokLine(
240 + turnCompleted({ promptId: "t3-xai-prompt-1", agentTimestampMs: 1_786_459_000_000 }),
241 + )[0];
242 + const copied = parseGrokLine(turnCompleted({ promptId: "t3-xai-prompt-1" }))[0];
243 +
244 + expect(resumed?.dedupeKey).not.toBe(first?.dedupeKey);
245 + expect(copied?.dedupeKey).toBe(first?.dedupeKey);
246 + });
247 +
248 it("ignores non-turn lines and empty usage", () => {
249 expect(parseGrokLine(JSON.stringify({ method: "session/update", params: {} }))).toEqual([]);
250 expect(parseGrokLine("not json")).toEqual([]);
packages/provider-grok/src/server/usage.ts
+4 -2
@@ -92,6 +92,7 @@ function parseGrokRecord(parsed: unknown): readonly UsageRecord[] {
92 const usageRecord = usage as Record<string, unknown>;
93
94 const sessionId = typeof paramsRecord["sessionId"] === "string" ? paramsRecord["sessionId"] : "";
95 + // T3's prompt ids restart with each Grok process, so a resumed session reuses them.
96 const promptId = typeof updateRecord["prompt_id"] === "string" ? updateRecord["prompt_id"] : null;
97
98 const meta = paramsRecord["_meta"];
@@ -136,7 +137,7 @@ function parseGrokRecord(parsed: unknown): readonly UsageRecord[] {
137 reportedCostUsd: grokCostTicksToUsd(topLevel.costUsdTicks),
138 speed: "standard",
139 // No prompt id means we cannot tell two same-second updates apart.
139 - dedupeKey: promptId === null ? null : `${sessionId}:${promptId}:grok`,
140 + dedupeKey: promptId === null ? null : `${sessionId}:${promptId}:${timestampMs}:grok`,
141 },
142 ];
143 }
@@ -174,7 +175,8 @@ function parseGrokRecord(parsed: unknown): readonly UsageRecord[] {
175 totals,
176 reportedCostUsd,
177 speed: "standard",
177 - dedupeKey: promptId === null ? null : `${sessionId}:${promptId}:${entry.model}`,
178 + dedupeKey:
179 + promptId === null ? null : `${sessionId}:${promptId}:${timestampMs}:${entry.model}`,
180 });
181 }
182 return results;