Repository navigation
Expand file tree
/
Copy pathusageScanCache.ts
More file actions
374 lines (345 loc) · 13 KB
/
Copy pathusageScanCache.ts
File metadata and controls
374 lines (345 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import type { UsageProviderKind } from "@t3tools/contracts";
import type {
TranscriptUsageFormat,
UsageRecord,
UsageSpeed,
} from "@t3tools/provider-core/server/usage";
import * as Schema from "effect/Schema";
import { CLAUDE_SYNTHETIC_MODEL } from "../provider/Drivers/claudeUsage.ts";
import { mergeUsageCopies } from "./usageAggregation.ts";
import { GUARD_LENGTH, type TranscriptParsePosition } from "./usageTranscriptReader.ts";
const USAGE_SCAN_CACHE_VERSION = 12 as const;
const SPEED_COMPATIBLE_SINCE_VERSION = 4;
/**
* An older server sharing that directory cannot read a newer cache and would
* replace it, dropping saved usage for deleted transcripts. Separate files keep
* the newer one; older files are removed once it is saved.
*/
export const SCAN_CACHE_FILE_NAME = "usage-scan-cache-v12.json";
export const LEGACY_SCAN_CACHE_FILE_NAMES = [
"usage-scan-cache-v11.json",
"usage-scan-cache-v10.json",
"usage-scan-cache-v9.json",
"usage-scan-cache-v8.json",
"usage-scan-cache-v7.json",
"usage-scan-cache-v6.json",
"usage-scan-cache-v5.json",
"usage-scan-cache.json",
] as const;
const SPEEDS: readonly UsageSpeed[] = ["standard", "fast", "ultrafast"];
export interface CachedFile {
readonly size: number;
readonly mtimeMs: number;
readonly provider: UsageProviderKind;
readonly records: readonly UsageRecord[];
/** Unterminated tail records stay separate because incremental parsing rereads them. */
readonly tailRecords: readonly UsageRecord[];
/** Whole-file count, including diagnostics from the tentative tail. */
readonly malformedRecords: number;
readonly position: TranscriptParsePosition;
}
export type ScanCache = Map<string, CachedFile>;
type SerializedRecord = readonly [
timestampMs: number,
modelIndex: number,
sessionIndex: number,
uncachedInputTokens: number,
cachedInputTokens: number,
cacheCreationTokens: number,
outputTokens: number,
reasoningTokens: number,
dedupeKey: string | null,
reportedCostUsd: number | null,
speed: number,
rateModelIndex?: number | null,
unresolvedServiceTierTurnId?: string,
];
interface SerializedFile {
readonly s: number;
readonly m: number;
readonly p: UsageProviderKind;
readonly r: readonly SerializedRecord[];
readonly t: readonly SerializedRecord[];
readonly mr: number;
readonly o: number;
readonly gl: number;
readonly gh: number;
readonly cs: unknown;
}
interface SerializedCache {
readonly version: number;
readonly models: readonly string[];
readonly sessions: readonly string[];
readonly files: Readonly<Record<string, SerializedFile>>;
}
interface InternTables {
readonly models: string[];
readonly sessions: string[];
readonly modelIndex: Map<string, number>;
readonly sessionIndex: Map<string, number>;
}
function makeInternTables(): InternTables {
return { models: [], sessions: [], modelIndex: new Map(), sessionIndex: new Map() };
}
function intern(table: string[], index: Map<string, number>, value: string): number {
const existing = index.get(value);
if (existing !== undefined) return existing;
const next = table.length;
table.push(value);
index.set(value, next);
return next;
}
function serializeFile(entry: CachedFile, tables: InternTables): SerializedFile {
const serializeRecord = (record: UsageRecord): SerializedRecord => [
record.timestampMs,
intern(tables.models, tables.modelIndex, record.model),
intern(tables.sessions, tables.sessionIndex, record.sessionId),
record.totals.uncachedInputTokens,
record.totals.cachedInputTokens,
record.totals.cacheCreationTokens,
record.totals.outputTokens,
record.totals.reasoningTokens,
record.dedupeKey,
record.reportedCostUsd,
SPEEDS.indexOf(record.speed),
record.rateModel === undefined
? null
: intern(tables.models, tables.modelIndex, record.rateModel),
...(record.unresolvedServiceTierTurnId === undefined
? ([] as const)
: ([record.unresolvedServiceTierTurnId] as const)),
];
return {
s: entry.size,
m: entry.mtimeMs,
p: entry.provider,
r: entry.records.map(serializeRecord),
t: entry.tailRecords.map(serializeRecord),
mr: entry.malformedRecords,
o: entry.position.resumeOffset,
gl: entry.position.guardLength,
gh: entry.position.guardHash,
cs: entry.position.state,
};
}
/** Yields the document in pieces so large caches never become one string; growing intern tables preserve indexes, retaining unused strings until restart. */
export function makeScanCacheWriter(): (
cache: ScanCache,
extra: Readonly<Record<string, unknown>>,
) => Iterable<string> {
const tables = makeInternTables();
function* encode(entries: readonly (readonly [string, CachedFile])[], extra: object) {
yield '{"files":{';
for (const [index, [path, entry]] of entries.entries()) {
yield `${index === 0 ? "" : ","}${JSON.stringify(path)}:${JSON.stringify(serializeFile(entry, tables))}`;
}
// Encoded after the files, which may have added to the intern tables.
const tail = JSON.stringify({
...extra,
version: USAGE_SCAN_CACHE_VERSION,
models: tables.models,
sessions: tables.sessions,
});
yield `},${tail.slice(1)}`;
}
// Snapshot now: the encoding runs later, while the file is written.
return (cache, extra) => encode([...cache], extra);
}
function isRecordArray(value: unknown): value is readonly unknown[] {
return Array.isArray(value);
}
export function decodeScanCache(
document: unknown,
formats: ReadonlyMap<UsageProviderKind, TranscriptUsageFormat<unknown>>,
): ScanCache {
const cache: ScanCache = new Map();
const isValidState = makeStateValidators(formats);
if (typeof document !== "object" || document === null) return cache;
const root = document as Partial<SerializedCache>;
const version = root.version;
if (
typeof version !== "number" ||
version < SPEED_COMPATIBLE_SINCE_VERSION ||
version > USAGE_SCAN_CACHE_VERSION
) {
return cache;
}
if (!isRecordArray(root.models) || !isRecordArray(root.sessions)) return cache;
if (typeof root.files !== "object" || root.files === null) return cache;
if (!root.models.every((value) => typeof value === "string")) return cache;
if (!root.sessions.every((value) => typeof value === "string")) return cache;
const models = root.models as readonly string[];
const sessions = root.sessions as readonly string[];
// Reject entire corrupt entries; partial survivors would appear warm and hide usage until the file changes.
const decodeRecords = (
rows: readonly unknown[],
provider: UsageProviderKind,
): UsageRecord[] | null => {
const records: UsageRecord[] = [];
for (const row of rows) {
if (!isRecordArray(row) || row.length < 11) return null;
const [
timestampMs,
modelIndex,
sessionIndex,
uncached,
cached,
cacheCreation,
output,
reasoning,
dedupeKey,
reportedCostUsd,
speedIndex,
rateModelIndex,
unresolvedServiceTierTurnId,
] = row as SerializedRecord;
const speed = typeof speedIndex === "number" ? SPEEDS[speedIndex] : undefined;
const model = typeof modelIndex === "number" ? models[modelIndex] : undefined;
const rateModel = typeof rateModelIndex === "number" ? models[rateModelIndex] : undefined;
if (
typeof timestampMs !== "number" ||
!Number.isFinite(timestampMs) ||
model === undefined ||
(unresolvedServiceTierTurnId !== undefined &&
typeof unresolvedServiceTierTurnId !== "string") ||
(rateModelIndex != null && rateModel === undefined) ||
!Number.isFinite(uncached) ||
!Number.isFinite(cached) ||
!Number.isFinite(cacheCreation) ||
!Number.isFinite(output) ||
!Number.isFinite(reasoning) ||
speed === undefined
) {
return null;
}
// Unchanged and deleted transcripts may only exist in the cache, so the
// parser cannot remove their previously saved local placeholders.
if (provider === "claude" && model === CLAUDE_SYNTHETIC_MODEL) continue;
records.push({
provider,
timestampMs,
model,
...(rateModel === undefined ? {} : { rateModel }),
sessionId: (typeof sessionIndex === "number" ? sessions[sessionIndex] : undefined) ?? "",
totals: {
uncachedInputTokens: uncached,
cachedInputTokens: cached,
cacheCreationTokens: cacheCreation,
outputTokens: output,
reasoningTokens: reasoning,
},
reportedCostUsd: typeof reportedCostUsd === "number" ? reportedCostUsd : null,
speed,
...(version >= 7 && unresolvedServiceTierTurnId !== undefined
? { unresolvedServiceTierTurnId }
: {}),
dedupeKey: typeof dedupeKey === "string" ? dedupeKey : null,
});
}
return records;
};
for (const [path, raw] of Object.entries(root.files)) {
if (typeof raw !== "object" || raw === null) continue;
const entry = raw as Partial<SerializedFile>;
if (typeof entry.s !== "number" || typeof entry.m !== "number") continue;
if (entry.p === undefined) continue;
const format = formats.get(entry.p);
if (format === undefined) continue;
if (!isRecordArray(entry.r) || !isRecordArray(entry.t)) continue;
const malformedRecords = entry.mr === undefined ? 0 : entry.mr;
if (!Number.isSafeInteger(malformedRecords) || malformedRecords < 0) continue;
// Position fields feed byte offsets and a Buffer allocation in the reader,
// so anything outside their real ranges must reject the entry: a bogus
// guard length would otherwise fail every parse of the file, silently
// dropping its usage instead of costing the documented cold re-parse.
if (
typeof entry.o !== "number" ||
!Number.isSafeInteger(entry.o) ||
entry.o < 0 ||
typeof entry.gl !== "number" ||
!Number.isSafeInteger(entry.gl) ||
entry.gl < 0 ||
entry.gl > GUARD_LENGTH ||
entry.gl > entry.o ||
typeof entry.gh !== "number" ||
!Number.isFinite(entry.gh)
) {
continue;
}
// Keep history for deleted rollouts, but re-parse incompatible reducer state.
// v9 and v11 repair skipped Codex requests; clean v8+ entries stay served while unchanged.
// v10 adds Claude advisor calls; before v12 a repeated Claude key kept its first, possibly partial, copy.
const legacy =
(entry.p === "claude" && version < 12) ||
(format.state !== undefined &&
(version < 6 ||
(entry.p === "codex" && (version < 8 || (version < 11 && malformedRecords > 0)))));
// A corrupt state disqualifies the entry: resuming with it would attach
// appended usage to the wrong model or replay fork-copied history.
if (!legacy && !isValidState.get(entry.p)?.(entry.cs)) continue;
// Before v11 a clean scan could end past an interrupted request whose usage its state dropped,
// so a grown file re-parses whole instead of resuming.
const resumable = !legacy && !(entry.p === "codex" && version < 11);
const provider = entry.p;
const records = decodeRecords(entry.r, provider);
const tailRecords = decodeRecords(entry.t, provider);
if (records === null || tailRecords === null) continue;
cache.set(path, {
size: legacy ? -1 : entry.s,
mtimeMs: entry.m,
provider,
records,
tailRecords,
malformedRecords,
position: resumable
? {
resumeOffset: entry.o,
guardLength: entry.gl,
guardHash: entry.gh,
state: entry.cs,
}
: { resumeOffset: 0, guardLength: 0, guardHash: 0, state: null },
});
}
return cache;
}
function makeStateValidators(
formats: ReadonlyMap<UsageProviderKind, TranscriptUsageFormat<unknown>>,
): ReadonlyMap<UsageProviderKind, (value: unknown) => boolean> {
return new Map(
[...formats].map(([provider, format]) => {
const isState = format.state === undefined ? undefined : Schema.is(format.state.schema);
return [
provider,
(value: unknown) => value === null || (isState !== undefined && isState(value)),
] as const;
}),
);
}
export function pruneScanCache(cache: ScanCache, retentionCutoffMs: number): number {
let removed = 0;
for (const [path, entry] of cache) {
if (entry.mtimeMs < retentionCutoffMs) {
cache.delete(path);
removed += 1;
}
}
return removed;
}
/** Pass only newline-terminated records: an unterminated tail can still change, so the aggregator merges it per scan. */
export function dedupeWithinFile(records: readonly UsageRecord[]): readonly UsageRecord[] {
const kept: UsageRecord[] = [];
const firstIndex = new Map<string, number>();
for (const record of records) {
if (record.dedupeKey !== null) {
const index = firstIndex.get(record.dedupeKey);
if (index !== undefined) {
kept[index] = mergeUsageCopies(kept[index]!, record);
continue;
}
firstIndex.set(record.dedupeKey, kept.length);
}
kept.push(record);
}
return kept;
}