Repository navigation
Expand file tree
/
Copy pathusageScanCache.ts
More file actions
546 lines (510 loc) · 21.7 KB
/
Copy pathusageScanCache.ts
File metadata and controls
546 lines (510 loc) · 21.7 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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/**
* Durable per-file scan cache.
*
* Transcripts are append-only and a file that has not changed can never yield
* different usage, so parsed records are keyed by `(size, mtime)` and reused.
* Without this every server restart re-parses the whole window: roughly 3.5s
* for a 30-day scan here, against ~11ms to reload this cache.
*
* Caching *per file* rather than per day is deliberate. It is timezone
* independent, so changing the reporting zone does not invalidate anything, and
* it keeps cross-file de-duplication exact: cached entries are de-duplicated
* within their own file only, and the aggregator still applies the global
* dedupe pass over the small surviving key set.
*
* @module usageScanCache
*/
import type { UsageProviderKind } from "@t3tools/contracts";
import { GUARD_LENGTH, type TranscriptParsePosition } from "./usageTranscriptReader.ts";
import type {
CodexScanState,
DedupeKeyScope,
UsageMeasurement,
UsageMeasurementCompleteness,
UsageObservationScope,
UsageRecord,
} from "./usageTranscripts.ts";
// v2: Codex fork-copy suppression changed what a file parses to, so v1
// entries would keep serving double-counted records forever.
// v3: entries carry the parse position and reducer state so a grown file
// re-parses only its appended bytes instead of starting over.
// v4: records carry native request/message/prompt ids. Without the bump, warm
// v3 entries would silently report those levels as unsupported until the file
// next changed. The v4 row later gained validity/completeness and dedupe-key
// scope fields appended after the first v4 rows; the version deliberately
// stayed 4 because those fields are additive.
//
// Supported-format policy:
// - v3 documents are still *read*. The scan retains measured records from
// transcripts that have since been deleted, and those cannot be re-parsed, so
// discarding a v3 cache would destroy 90 days of history. A v3 row decodes
// with its native ids and measurement presence explicitly `unavailable`.
// - A v4 row written by the predecessor (15 fields, no quality metadata)
// decodes conservatively: completeness is `partial`, never `complete`, and
// the entry is `qualityMetadata: "predecessor"` so an extant file is cold
// re-parsed once. No row is discarded for the format change. The marker is
// persisted on the file (`lq`) because the encoder rewrites rows to the
// current 18-field shape, so provenance cannot be recovered from row length
// after a save/restart.
// - Only v1/v2 (no parse position, different fork semantics) are rejected.
const USAGE_SCAN_CACHE_VERSION = 4 as const;
const LEGACY_USAGE_SCAN_CACHE_VERSION = 3 as const;
/** Index of the first appended post-v4 field (completeness code) in a row. */
const POST_V4_FIELD_INDEX = 15;
/**
* Whether a cache entry's rows still carry their native ids and measurement
* presence. A `v3` entry erased both; the projection must report them as
* unavailable rather than as a measured zero or an absent id.
*/
export type ScanCacheIdentity = "declared" | "unavailable";
/**
* Whether a cache entry's rows carry the current numeric quality metadata
* (validity/completeness and dedupe-key scope, appended after the first v4
* rows). Native-id availability alone does not prove that: a `15`-field v4 row
* written before that metadata existed has identity `declared` but no
* completeness, so reading it as complete would silently promote an unknown
* measurement. Such an entry is `predecessor`: its retained rows are treated as
* partial, and an extant file is cold re-parsed once to enrich it.
*/
export type ScanCacheQualityMetadata = "declared" | "predecessor";
export interface CachedFile {
readonly size: number;
readonly mtimeMs: number;
readonly provider: UsageProviderKind;
/** Records from newline-terminated lines, up to `position.resumeOffset`. */
readonly records: readonly UsageRecord[];
/**
* Records from a trailing segment the writer had not newline-terminated at
* parse time. Kept apart from `records` because an incremental parse
* re-reads that segment and would otherwise double count it.
*/
readonly tailRecords: readonly UsageRecord[];
readonly position: TranscriptParsePosition;
/**
* `unavailable` for a legacy row whose ids/presence were erased. Callers must
* not resume such an entry: a cold re-parse is the only way to enrich it.
*/
readonly identity: ScanCacheIdentity;
/**
* `predecessor` for an entry whose rows omit the current quality metadata.
* Callers must not serve it warm or resume it: the numeric quality is
* unasserted, so a cold re-parse is the only way to establish completeness.
*/
readonly qualityMetadata: ScanCacheQualityMetadata;
}
export type ScanCache = Map<string, CachedFile>;
/**
* Row layout for the serialised form. Positional and interned rather than
* object-per-record: on a 30-day window that is the difference between a file
* measured in tens of megabytes and one under six.
*/
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,
providerRequestId: string | null,
providerMessageId: string | null,
promptId: string | null,
measurementCode: number,
scopeCode: number,
/** Appended after the first v4 rows; absent means `complete` when observed. */
completenessCode: number,
/** Appended after the first v4 rows; count of present-but-invalid fields. */
invalidTokenFields: number,
/** Appended after the first v4 rows; absent means `global`. */
dedupeKeyScopeCode: number,
];
// `invalid` is appended last so the existing observed/empty/unavailable codes
// keep their values and older v4 rows keep decoding.
const MEASUREMENT_CODES: readonly UsageMeasurement[] = [
"observed",
"empty",
"unavailable",
"invalid",
];
const SCOPE_CODES: readonly UsageObservationScope[] = ["delta", "snapshot"];
const COMPLETENESS_CODES: readonly UsageMeasurementCompleteness[] = ["complete", "partial"];
const DEDUPE_KEY_SCOPE_CODES: readonly DedupeKeyScope[] = ["global", "source-local"];
function encodeMeasurement(measurement: UsageMeasurement | undefined): number {
const index = MEASUREMENT_CODES.indexOf(measurement ?? "observed");
return index < 0 ? 0 : index;
}
function encodeScope(scope: UsageObservationScope | undefined): number {
const index = SCOPE_CODES.indexOf(scope ?? "delta");
return index < 0 ? 0 : index;
}
function encodeCompleteness(completeness: UsageMeasurementCompleteness | undefined): number {
const index = COMPLETENESS_CODES.indexOf(completeness ?? "complete");
return index < 0 ? 0 : index;
}
function encodeDedupeKeyScope(scope: DedupeKeyScope | undefined): number {
const index = DEDUPE_KEY_SCOPE_CODES.indexOf(scope ?? "global");
return index < 0 ? 0 : index;
}
function decodeCode<Value extends string>(
value: unknown,
codes: readonly Value[],
): Value | undefined {
return typeof value === "number" && Number.isSafeInteger(value) ? codes[value] : undefined;
}
interface SerializedFile {
readonly s: number;
readonly m: number;
readonly p: UsageProviderKind;
readonly r: readonly SerializedRecord[];
/** Tail records; see `CachedFile.tailRecords`. */
readonly t: readonly SerializedRecord[];
/** Parse position: resume offset, guard length, guard hash. */
readonly o: number;
readonly gl: number;
readonly gh: number;
/** Codex reducer state at `o`; `null` for stateless providers. */
readonly cs: CodexScanState | null;
/**
* `1` when this entry's rows predate native ids / measurement presence. Kept
* on the file so an erased-history entry stays `unavailable` across restarts
* until an extant file is cold re-parsed. Absent on a fresh entry.
*/
readonly li?: number;
/**
* `1` when this entry's rows predate the current numeric quality metadata.
* Like `li`, it is a file-level provenance marker: the encoder rewrites every
* row to the current 18-field shape, so row length alone cannot tell a
* predecessor entry apart from a freshly parsed one after a persist. Kept on
* the file so a predecessor entry stays `predecessor` across restarts until a
* successful full parse replaces it. Absent on a fresh entry.
*/
readonly lq?: number;
}
interface SerializedCache {
readonly version: number;
readonly models: readonly string[];
readonly sessions: readonly string[];
readonly files: Readonly<Record<string, SerializedFile>>;
}
/** Serialises the cache, interning the repeated model and session strings. */
export function encodeScanCache(cache: ScanCache): SerializedCache {
const models: string[] = [];
const sessions: string[] = [];
const modelIndex = new Map<string, number>();
const sessionIndex = new Map<string, number>();
const 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;
};
const serializeRecord = (record: UsageRecord): SerializedRecord => [
record.timestampMs,
intern(models, modelIndex, record.model),
intern(sessions, sessionIndex, record.sessionId),
record.totals.uncachedInputTokens,
record.totals.cachedInputTokens,
record.totals.cacheCreationTokens,
record.totals.outputTokens,
record.totals.reasoningTokens,
record.dedupeKey,
record.reportedCostUsd,
record.providerRequestId ?? null,
record.providerMessageId ?? null,
record.promptId ?? null,
encodeMeasurement(record.measurement),
encodeScope(record.scope),
encodeCompleteness(record.measurementCompleteness),
record.invalidTokenFields ?? 0,
encodeDedupeKeyScope(record.dedupeKeyScope),
];
const files: Record<string, SerializedFile> = {};
for (const [path, entry] of cache) {
files[path] = {
s: entry.size,
m: entry.mtimeMs,
p: entry.provider,
r: entry.records.map(serializeRecord),
t: entry.tailRecords.map(serializeRecord),
o: entry.position.resumeOffset,
gl: entry.position.guardLength,
gh: entry.position.guardHash,
cs: entry.position.codexState,
...(entry.identity === "unavailable" ? { li: 1 } : {}),
// Persist the freshness provenance, not just the row shape. The rows are
// always rewritten to the current 18-field form, so without this marker a
// predecessor entry would decode as `declared` after the first persist and
// be served warm forever, skipping the cold re-parse that asserts quality.
...(entry.qualityMetadata === "predecessor" ? { lq: 1 } : {}),
};
}
return { version: USAGE_SCAN_CACHE_VERSION, models, sessions, files };
}
function isRecordArray(value: unknown): value is readonly unknown[] {
return Array.isArray(value);
}
/**
* Rebuilds the cache from a parsed document.
*
* Anything malformed yields an empty cache rather than an error: a corrupt
* cache should cost one cold scan, never a broken page.
*/
export function decodeScanCache(document: unknown): ScanCache {
const cache: ScanCache = new Map();
if (typeof document !== "object" || document === null) return cache;
const root = document as Partial<SerializedCache>;
if (
root.version !== USAGE_SCAN_CACHE_VERSION &&
root.version !== LEGACY_USAGE_SCAN_CACHE_VERSION
) {
return cache;
}
// A v3 document has no native ids or measurement presence anywhere; every
// entry it holds is erased-history. A v4 entry carries its own marker.
const legacyDocument = root.version === LEGACY_USAGE_SCAN_CACHE_VERSION;
if (!isRecordArray(root.models) || !isRecordArray(root.sessions)) return cache;
if (typeof root.files !== "object" || root.files === null) return cache;
// The intern tables must be all strings: a numeric entry would pass the
// undefined guard below, land in a record's model, and crash the aggregate
// at lookupRate. A corrupt table rejects the whole 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[];
// Any corrupt row disqualifies the whole entry. Keeping the survivors
// under the original (size, mtime) would read as a valid warm hit and the
// file would never be re-parsed, silently losing the dropped rows' usage.
const decodeRecords = (
rows: readonly unknown[],
provider: UsageProviderKind,
legacy: boolean,
): { records: UsageRecord[]; qualityDeclared: boolean } | null => {
const records: UsageRecord[] = [];
// Every row must carry the appended post-v4 fields for the entry to be
// current. A 15-field row was written before completeness existed; the
// entry's numeric quality is then unasserted and must not be read as
// complete. Empty row lists are vacuously current: there is no measurement
// to promote.
let qualityDeclared = true;
for (const row of rows) {
if (!isRecordArray(row) || row.length < 10) return null;
if (row.length <= POST_V4_FIELD_INDEX) qualityDeclared = false;
const [
timestampMs,
modelIndex,
sessionIndex,
uncached,
cached,
cacheCreation,
output,
reasoning,
dedupeKey,
reportedCostUsd,
] = row as SerializedRecord;
// Appended in v4. Absent on a hand-built or truncated row, in which case
// the identity is simply not asserted rather than defaulted to a value.
const providerRequestId = row[10];
const providerMessageId = row[11];
const promptId = row[12];
// Appended after v4. A v3 or early-v4 row has no presence information, so
// a nonzero total proves a measurement while an all-zero row stays
// explicitly `unavailable` rather than being read as a measured zero.
const measurementCode = row[13];
const scopeCode = row[14];
// Appended after the first v4 rows: validity/completeness metadata. Absent
// on an older row, whose completeness is then unasserted (`partial`).
const completenessCode = row[15];
const invalidTokenFieldsRaw = row[16];
const dedupeKeyScopeCode = row[17];
const model = typeof modelIndex === "number" ? models[modelIndex] : undefined;
if (
typeof timestampMs !== "number" ||
!Number.isFinite(timestampMs) ||
model === undefined ||
!Number.isFinite(uncached) ||
!Number.isFinite(cached) ||
!Number.isFinite(cacheCreation) ||
!Number.isFinite(output) ||
!Number.isFinite(reasoning)
) {
return null;
}
const measurement: UsageMeasurement =
decodeCode(measurementCode, MEASUREMENT_CODES) ??
(uncached + cached + cacheCreation + output > 0 ? "observed" : "unavailable");
const scope: UsageObservationScope = decodeCode(scopeCode, SCOPE_CODES) ?? "delta";
// A row that omits the completeness code proves nothing about coverage,
// whether it is a legacy row (presence erased) or a predecessor v4 row
// (metadata predates the field). Default to `partial`, never `complete`:
// missing quality metadata must not be silently promoted to a measured
// complete observation. The current writer always emits the code, so this
// only applies to older rows.
const completeness: UsageMeasurementCompleteness | undefined =
measurement === "observed"
? (decodeCode(completenessCode, COMPLETENESS_CODES) ?? "partial")
: undefined;
const invalidTokenFields =
typeof invalidTokenFieldsRaw === "number" &&
Number.isFinite(invalidTokenFieldsRaw) &&
invalidTokenFieldsRaw > 0
? Math.trunc(invalidTokenFieldsRaw)
: 0;
const dedupeKeyScope: DedupeKeyScope | undefined = decodeCode(
dedupeKeyScopeCode,
DEDUPE_KEY_SCOPE_CODES,
);
records.push({
provider,
timestampMs,
model,
sessionId: (typeof sessionIndex === "number" ? sessions[sessionIndex] : undefined) ?? "",
totals: {
uncachedInputTokens: uncached,
cachedInputTokens: cached,
cacheCreationTokens: cacheCreation,
outputTokens: output,
reasoningTokens: reasoning,
},
reportedCostUsd: typeof reportedCostUsd === "number" ? reportedCostUsd : null,
dedupeKey: typeof dedupeKey === "string" ? dedupeKey : null,
// A v3 row cannot carry native ids; they are unavailable, not absent.
...(legacy
? {}
: {
...(typeof providerRequestId === "string" ? { providerRequestId } : {}),
...(typeof providerMessageId === "string" ? { providerMessageId } : {}),
...(typeof promptId === "string" ? { promptId } : {}),
}),
measurement,
...(completeness === undefined ? {} : { measurementCompleteness: completeness }),
...(invalidTokenFields === 0 ? {} : { invalidTokenFields }),
// Legacy rows erased identity; keep that visible so the projection does
// not read an absent native id as a missing one.
...(legacy ? { identityAvailable: false } : {}),
...(dedupeKeyScope === undefined ? {} : { dedupeKeyScope }),
...(scope === "delta" ? {} : { scope }),
});
}
return { records, qualityDeclared };
};
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 !== "claude" && entry.p !== "codex" && entry.p !== "grok") continue;
if (!isRecordArray(entry.r) || !isRecordArray(entry.t)) 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;
}
const codexState = decodeCodexState(entry.cs);
if (codexState === undefined) continue;
const provider: UsageProviderKind = entry.p;
const legacy = legacyDocument || entry.li === 1;
const decodedRecords = decodeRecords(entry.r, provider, legacy);
const decodedTail = decodeRecords(entry.t, provider, legacy);
if (decodedRecords === null || decodedTail === null) continue;
cache.set(path, {
size: entry.s,
mtimeMs: entry.m,
provider,
records: decodedRecords.records,
tailRecords: decodedTail.records,
position: {
resumeOffset: entry.o,
guardLength: entry.gl,
guardHash: entry.gh,
codexState,
},
identity: legacy ? "unavailable" : "declared",
// `lq` records provenance the row shape cannot: the encoder always emits
// the current 18-field rows, so a persisted predecessor entry would
// otherwise decode as `declared` on the next start without any parse.
qualityMetadata:
entry.lq === 1 || !decodedRecords.qualityDeclared || !decodedTail.qualityDeclared
? "predecessor"
: "declared",
});
}
return cache;
}
/**
* Validates a persisted Codex reducer state. Returns `undefined` for a corrupt
* value, which disqualifies the entry: resuming with a bad state would attach
* appended usage to the wrong model or replay fork-copied history.
*/
function decodeCodexState(value: unknown): CodexScanState | null | undefined {
if (value === null) return null;
if (typeof value !== "object") return undefined;
const state = value as Partial<CodexScanState>;
if (
typeof state.model !== "string" ||
typeof state.sessionId !== "string" ||
(state.lastUsageSignature !== null && typeof state.lastUsageSignature !== "string") ||
typeof state.sawSessionMeta !== "boolean" ||
typeof state.suppressingForkCopies !== "boolean" ||
typeof state.forkCopyAnchorMs !== "number" ||
!Number.isFinite(state.forkCopyAnchorMs)
) {
return undefined;
}
return {
model: state.model,
sessionId: state.sessionId,
lastUsageSignature: state.lastUsageSignature ?? null,
sawSessionMeta: state.sawSessionMeta,
suppressingForkCopies: state.suppressingForkCopies,
forkCopyAnchorMs: state.forkCopyAnchorMs,
};
}
/** Keeps saved usage after transcript cleanup, until the reporting retention expires. */
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;
}
/**
* Within-file de-duplication, applied before an entry is cached.
*
* Callers stitching an incremental parse together pass one `seen` set across
* the line and tail record batches so the whole file stays deduplicated as a
* unit; the set is mutated in place.
*/
export function dedupeWithinFile(
records: readonly UsageRecord[],
seen: Set<string> = new Set(),
): readonly UsageRecord[] {
const kept: UsageRecord[] = [];
for (const record of records) {
if (record.dedupeKey !== null) {
if (seen.has(record.dedupeKey)) continue;
seen.add(record.dedupeKey);
}
kept.push(record);
}
return kept;
}