Repository navigation
Expand file tree
/
Copy pathverify-counts.mjs
More file actions
1476 lines (1401 loc) · 60.7 KB
/
Copy pathverify-counts.mjs
File metadata and controls
1476 lines (1401 loc) · 60.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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Independent token counter for Claude Code and Codex logs (dev only, not shipped).
// Imports nothing from src/ or dist/; it only reads the bundled price tables
// (src/lib/pricing/*.json, plain data) and runs dist/cli.js as a subprocess.
//
// Usage: npm run build && node scripts/verify-counts.mjs [--why <n>]
// Reads SIPCODE_PROJECTS_DIR / CODEX_HOME like Sipcode does (defaults:
// ~/.claude/projects, ~/.codex) and passes the same folders to Sipcode, so it
// can be pointed at a frozen copy of the logs. Sipcode runs with a temporary
// HOME, so its usage cache starts cold; every command runs twice (cold, warm).
// Prints numbers only (never message text, paths or commands). Exit 1 on any
// mismatch.
//
// Rules re-implemented here (from the Claude Code / Codex log formats):
// - Claude Code writes one line per content block and repeats the request's
// usage on each; a request is message.id + "|" + requestId, and the largest
// value per usage field wins. Cache write = max(cache_creation_input_tokens,
// nested 5m + 1h). Observer folders (claude-mem) are not sessions.
// - Codex: token_usage_record lines are per request (key response_id). Older
// files only have token_count events carrying a cumulative total T: skip
// info:null and repeats (T == previous), start a new baseline when T goes
// backwards, bill T - previous field by field. Cached and cache-write input
// are subsets of input_tokens.
// - A request copied into another file (resumed session, fork) belongs to the
// file that started first; copies are dropped, and the kept copy carries the
// largest value per field across all copies.
// - Days: today = local calendar day; stats/trend/baseline buckets = UTC days.
// - Codex limits (`limits --json --since all`): a reading is payload.rate_limits
// on an event_msg / token_count line, one per non-null window (resets_at in
// epoch seconds; above 1e12 already ms). Readings sorted by time, reset time,
// percent. Window = limit_id + window_minutes. Stale readings are skipped:
// one logged after its own reset time, and one whose reset time is more than
// 15 min earlier than the open period's. A new period starts when the reset
// time moves by more than max(15 min, 1% of the window), when the reading is
// past the period's reset time, or when the reset time moves more than 15 min
// later while the percent is below the period's running maximum. Period
// start = min(newest reset time minus the window, first reading), but never
// before the previous period's newest reading. A rise of the running maximum
// is shared by the requests since the previous rise, by tokens; with none it
// is unattributed. Tokens per 1%: finished periods (not the window's last, or
// reset before the newest reading), 20% or more used, 90% or more of tokens
// from one model; a cut is the first period at or below half the median of
// 2+ earlier ones (same model).
// - Regress (`regress --json`): Claude Code requests with usage, a `version`
// and a model other than <synthetic>; the kept copy is the owner's with the
// largest usage; automated = isSidechain or entrypoint sdk-*; 5-minute write
// = write - 1h when the nested split was logged; the prototype's five checks
// and thresholds (min n 20). Codex: per (line 1 cli_version without
// pre-release, model, plan of the period's newest reading, limit, window),
// the percent its requests were charged rise by rise (by tokens), per 1M
// uncached input + output tokens; groups of 200,000+ such tokens; flagged
// at 2x the upper median of the others on the same plan, limit and window.
// - Codex compresses older rollouts to rollout-*.jsonl.zst. They are decoded
// here with node:zlib directly, frame by frame (Node's decoder stops after
// the first frame), and each frame must be complete. One log per rollout
// name: the plain .jsonl when both exist, else sessions/ before
// archived_sessions/. Without zstd in this Node the script stops (it would
// otherwise agree with a Sipcode that skips them).
import { zstdDecompressSync } from "node:zlib";
import { readFileSync, readdirSync, statSync, existsSync, mkdtempSync, rmSync } from "node:fs";
import { join, dirname } from "node:path";
import { homedir, tmpdir } from "node:os";
import { execFileSync } from "node:child_process";
import { fileURLToPath, pathToFileURL } from "node:url";
const REPO = join(dirname(fileURLToPath(import.meta.url)), "..");
const CLI = join(REPO, "dist", "cli.js");
const DAY = 86_400_000;
const args = process.argv.slice(2);
const WHY_SAMPLES = (args.includes("--why") && Number(args[args.indexOf("--why") + 1])) || 6;
const CLAUDE_ROOT = process.env.SIPCODE_PROJECTS_DIR || join(homedir(), ".claude", "projects");
const CODEX_HOME = process.env.CODEX_HOME || join(homedir(), ".codex");
let failures = 0;
const fmt = (n) =>
typeof n === "number" ? (Number.isInteger(n) ? n.toLocaleString("en-US") : n.toFixed(6)) : String(n);
function check(label, mine, theirs, tol = 0, source = "sipcode") {
const ok = typeof mine === "number" && typeof theirs === "number" ? Math.abs(mine - theirs) <= tol : mine === theirs;
if (!ok) failures++;
console.log(`${ok ? "MATCH " : "MISMATCH"} ${label}: independent ${fmt(mine)}, ${source} ${fmt(theirs)}`);
return ok;
}
const num = (v) => (typeof v === "number" && Number.isFinite(v) && v > 0 ? v : 0);
const ms = (iso) => {
if (!iso) return undefined;
const t = Date.parse(iso);
return Number.isFinite(t) ? t : undefined;
};
// ---------------------------------------------------------------------------
// Prices
// ---------------------------------------------------------------------------
const PRICE_DIR = join(REPO, "src", "lib", "pricing");
const priceFiles = readdirSync(PRICE_DIR);
const DATED = priceFiles
.filter((n) => /^\d{4}-\d{2}-\d{2}\.json$/.test(n))
.sort()
.map((n) => ({ date: n.slice(0, 10), models: JSON.parse(readFileSync(join(PRICE_DIR, n), "utf8")).models }));
const OPENAI_FILE = priceFiles
.filter((n) => /^openai-\d{4}-\d{2}-\d{2}\.json$/.test(n))
.sort()
.at(-1);
const OPENAI = OPENAI_FILE ? JSON.parse(readFileSync(join(PRICE_DIR, OPENAI_FILE), "utf8")).models : {};
const tableMemo = new Map();
/** Prices in force on a UTC day: the newest dated table on or before it, gaps filled from the newest; OpenAI rows always current. */
function tableFor(day) {
let t = tableMemo.get(day);
if (t) return t;
let chosen = DATED[0];
for (const f of DATED) if (f.date <= day) chosen = f;
t = { ...chosen.models };
for (const [k, v] of Object.entries(DATED.at(-1).models)) if (!Object.hasOwn(t, k)) t[k] = v;
for (const [k, v] of Object.entries(OPENAI)) if (!Object.hasOwn(t, k)) t[k] = v;
tableMemo.set(day, t);
return t;
}
const ALIASES = { "claude-opus-4-0": "claude-opus-4", "claude-sonnet-4-0": "claude-sonnet-4" };
function priceRow(table, model) {
if (Object.hasOwn(table, model)) return table[model];
const a = ALIASES[model];
if (a && Object.hasOwn(table, a)) return table[a];
let best;
for (const k of Object.keys(table)) {
if (model.startsWith(k) && /^-(\d{8}|\d{4}-\d{2}-\d{2})$/.test(model.slice(k.length))) {
if (!best || k.length > best.length) best = k;
}
}
return best ? table[best] : undefined;
}
/** USD for one request, or null when the model has no price. `inp` is uncached input. */
function requestCost(table, r) {
if (!r.model) return null;
const row = priceRow(table, r.model);
if (!row) return null;
const prompt = r.inp + r.cr + r.cw;
const rate = row.long_prompt && prompt > row.long_prompt.over_tokens ? row.long_prompt : row;
const oneHour = Math.min(r.cw1h, r.cw);
const oneHourRate = rate.cache_creation_1h_per_mtok ?? rate.input_per_mtok * 2;
return (
(r.inp * rate.input_per_mtok +
r.out * rate.output_per_mtok +
r.cr * rate.cache_read_per_mtok +
(r.cw - oneHour) * rate.cache_creation_per_mtok +
oneHour * oneHourRate) /
1e6
);
}
const FIELDS = ["inp", "out", "cr", "cw", "cw1h"];
const tokensOf = (r) => r.inp + r.out + r.cr + r.cw;
// ---------------------------------------------------------------------------
// Claude Code
// ---------------------------------------------------------------------------
function readClaudeFile(text) {
const byKey = new Map();
const requests = [];
let start;
let end;
let lastUserTool = false;
for (const line of text.split(/\r?\n/)) {
if (!line.includes('"assistant"') && !line.includes('"user"')) continue;
let e;
try {
e = JSON.parse(line);
} catch {
continue;
}
if (!e || typeof e !== "object" || (e.type !== "assistant" && e.type !== "user")) continue;
if (e.timestamp !== undefined && typeof e.timestamp !== "string") continue;
if (e.sessionId !== undefined && typeof e.sessionId !== "string") continue;
const ts = e.timestamp || undefined;
if (ts) {
if (!start || ts < start) start = ts;
if (!end || ts > end) end = ts;
}
if (e.type !== "assistant") {
const c = e.message && typeof e.message === "object" ? e.message.content : undefined;
lastUserTool = Array.isArray(c) && c.some((b) => b && typeof b === "object" && b.type === "tool_result");
continue;
}
const m = e.message && typeof e.message === "object" ? e.message : {};
const u = m.usage && typeof m.usage === "object" ? m.usage : undefined;
const nested = u?.cache_creation && typeof u.cache_creation === "object" ? u.cache_creation : {};
const n5 = num(nested.ephemeral_5m_input_tokens);
const n1 = num(nested.ephemeral_1h_input_tokens);
const v = {
inp: num(u?.input_tokens),
out: num(u?.output_tokens),
cr: num(u?.cache_read_input_tokens),
cw: Math.max(num(u?.cache_creation_input_tokens), n5 + n1),
cw1h: n1,
};
const key =
typeof m.id === "string" && m.id ? `${m.id}|${typeof e.requestId === "string" ? e.requestId : ""}` : undefined;
const split = !!(u?.cache_creation && typeof u.cache_creation === "object");
const prev = key ? byKey.get(key) : undefined;
if (prev) {
for (const f of FIELDS) prev[f] = Math.max(prev[f], v[f]);
prev.hasUsage ||= !!u;
prev.split ||= split;
continue;
}
const r = {
key,
model: typeof m.model === "string" ? m.model : undefined,
ts,
...v,
// regress: the request's tags (its first line wins, like Sipcode).
version: typeof e.version === "string" && e.version ? e.version : undefined,
entry: typeof e.entrypoint === "string" && e.entrypoint ? e.entrypoint : "?",
side: e.isSidechain === true,
afterTool: lastUserTool,
hasUsage: !!u,
split,
};
if (key) byKey.set(key, r);
requests.push(r);
}
return { requests, start, end, turnCount: requests.length };
}
function readClaude() {
const out = [];
if (!existsSync(CLAUDE_ROOT)) return out;
for (const d of readdirSync(CLAUDE_ROOT, { withFileTypes: true })) {
if (!d.isDirectory() || /-observer-sessions$/i.test(d.name) || /claude-mem-observer/i.test(d.name)) continue;
const dir = join(CLAUDE_ROOT, d.name);
let entries;
try {
entries = readdirSync(dir, { withFileTypes: true });
} catch {
continue;
}
for (const f of entries) {
if (!f.isFile() || !f.name.endsWith(".jsonl")) continue;
const p = join(dir, f.name);
const st = statSync(p);
out.push({
id: f.name.slice(0, -6),
project: d.name,
mtimeMs: st.mtimeMs,
...readClaudeFile(readFileSync(p, "utf8")),
});
}
}
return out.sort((a, b) => b.mtimeMs - a.mtimeMs);
}
// ---------------------------------------------------------------------------
// Codex
// ---------------------------------------------------------------------------
const usageVec = (u) => ({
input: num(u?.input_tokens),
cached: num(u?.cached_input_tokens),
write: num(u?.cache_write_input_tokens),
output: num(u?.output_tokens),
total: num(u?.total_tokens),
});
/** Codex input counts cached and cache-write input as subsets; split into disjoint parts. */
function splitCodex(v) {
const cached = Math.min(v.cached, v.input);
const write = Math.min(v.write, v.input - cached);
return { inp: v.input - cached - write, out: v.output, cr: cached, cw: write, cw1h: 0 };
}
function readCodexFile(text) {
let meta;
let start;
let end;
let model;
const turnModel = new Map();
const requests = [];
const keys = new Set();
let records = false;
let prev;
let resets = 0;
let negativeFields = 0;
let finalTotal;
let finalThread;
for (const raw of text.split(/\r?\n/)) {
if (!raw.trim()) continue;
let e;
try {
e = JSON.parse(raw);
} catch {
continue;
}
if (!e || typeof e !== "object" || Array.isArray(e) || !e.payload || typeof e.payload !== "object") continue;
const ts = typeof e.timestamp === "string" && e.timestamp ? e.timestamp : undefined;
if (ts) {
if (!start || ts < start) start = ts;
if (!end || ts > end) end = ts;
}
const p = e.payload;
if (e.type === "session_meta") {
meta ??= p;
continue;
}
const root =
(typeof meta?.session_id === "string" && meta.session_id) ||
(typeof meta?.id === "string" && meta.id) ||
undefined;
if (e.type === "turn_context") {
if (typeof p.model === "string") {
model = p.model;
if (typeof p.turn_id === "string" && p.turn_id) turnModel.set(p.turn_id, p.model);
}
continue;
}
if (e.type === "token_usage_record") {
const usage = usageVec(p.usage && typeof p.usage === "object" ? p.usage : {});
const thread =
p.thread_token_usage && typeof p.thread_token_usage === "object" ? usageVec(p.thread_token_usage) : undefined;
if (thread) finalThread = thread.total;
if (!records) {
records = true;
// Counter events logged before the first record (file resumed under a
// newer Codex) stay only if the thread total puts them before it.
if (thread) {
const before = thread.total - usage.total;
while (requests.length && requests.at(-1).cumulative !== undefined && requests.at(-1).cumulative > before)
requests.pop();
}
}
const key =
(typeof p.response_id === "string" && p.response_id) ||
(thread && root
? `codex:${root}:rec:${thread.total}:${thread.input}:${thread.cached}:${thread.output}`
: undefined);
if (key) {
if (keys.has(key)) continue;
keys.add(key);
}
const turnId = typeof p.turn_id === "string" && p.turn_id ? p.turn_id : undefined;
requests.push({ key, model: (turnId && turnModel.get(turnId)) ?? model, ts, ...splitCodex(usage), raw: usage });
continue;
}
if (e.type === "event_msg" && p.type === "token_count" && !records) {
const T0 = p.info?.total_token_usage;
if (!T0 || typeof T0 !== "object") continue;
const T = usageVec(T0);
finalTotal = T.total;
if (
prev &&
T.input === prev.input &&
T.cached === prev.cached &&
T.output === prev.output &&
T.total === prev.total
)
continue;
if (prev && T.total < prev.total) {
resets++;
prev = T;
continue;
}
const d = {};
for (const f of ["input", "cached", "write", "output", "total"]) {
const x = T[f] - (prev ? prev[f] : 0);
if (x < 0) negativeFields++;
d[f] = Math.max(0, x);
}
prev = T;
if (d.input === 0 && d.output === 0 && d.cached === 0) continue;
const key = root ? `codex:${root}:${T.total}:${T.input}:${T.cached}:${T.output}` : undefined;
if (key) {
if (keys.has(key)) continue;
keys.add(key);
}
requests.push({ key, model, ts, ...splitCodex(d), raw: d, cumulative: T.total });
}
}
const sub =
meta &&
((meta.source && typeof meta.source === "object" && "subagent" in meta.source) ||
typeof meta.parent_thread_id === "string");
return {
metaId: typeof meta?.id === "string" && meta.id ? meta.id : undefined,
cliVersion: typeof meta?.cli_version === "string" && meta.cli_version ? meta.cli_version : undefined,
isSubagent: !!sub,
requests,
start,
end,
turnCount: requests.length,
records,
resets,
negativeFields,
groundTruth: records ? finalThread : finalTotal,
};
}
/**
* Decode a .zst rollout: split into frames by walking their headers (RFC 8878
* 3.1), decode each, check each decoded size against its header, concatenate.
* Throws on anything incomplete.
*/
function decodeZst(buf) {
if (typeof zstdDecompressSync !== "function") {
throw new Error(`compressed Codex logs need Node.js 22.15 or newer to verify (this is ${process.version})`);
}
const parts = [];
let p = 0;
while (p < buf.length) {
const magic = buf.readUInt32LE(p);
if (((magic & 0xfffffff0) >>> 0) === 0x184d2a50) {
p += 8 + buf.readUInt32LE(p + 4);
continue;
}
if (magic !== 0xfd2fb528) throw new Error("not a zstd frame");
const start = p;
const fhd = buf[p + 4];
const single = (fhd >> 5) & 1;
const fcsLen = [single, 2, 4, 8][fhd >> 6];
p += 5 + (single ? 0 : 1) + [0, 1, 2, 4][fhd & 3];
const fcs =
fcsLen === 1 ? buf[p] : fcsLen === 2 ? buf.readUInt16LE(p) + 256 : fcsLen === 4 ? buf.readUInt32LE(p) : fcsLen === 8 ? Number(buf.readBigUInt64LE(p)) : undefined;
p += fcsLen;
for (let last = 0; !last; ) {
if (p + 3 > buf.length) throw new Error("truncated zstd block");
const h = buf[p] | (buf[p + 1] << 8) | (buf[p + 2] << 16);
last = h & 1;
p += 3 + (((h >> 1) & 3) === 1 ? 1 : h >>> 3);
}
if ((fhd >> 2) & 1) p += 4;
if (p > buf.length) throw new Error("truncated zstd frame");
const out = zstdDecompressSync(buf.subarray(start, p));
if (fcs !== undefined && out.length !== fcs) throw new Error("zstd frame decoded to the wrong size");
parts.push(out);
}
return Buffer.concat(parts).toString("utf8");
}
/** A rollout's text, plain or compressed. */
function readRollout(p) {
return p.endsWith(".zst") ? decodeZst(readFileSync(p)) : readFileSync(p, "utf8");
}
/**
* Rollout files by plain name (X.jsonl for X.jsonl and X.jsonl.zst): the
* plain file wins over a compressed one; otherwise sessions/ is walked first
* and wins over archived_sessions/.
*/
function listRollouts() {
const byName = new Map();
for (const sub of ["sessions", "archived_sessions"]) {
const walk = (dir) => {
let entries;
try {
entries = readdirSync(dir, { withFileTypes: true });
} catch {
return;
}
for (const e of entries) {
const p = join(dir, e.name);
if (e.isDirectory()) walk(p);
else if (e.isFile() && e.name.startsWith("rollout-") && (e.name.endsWith(".jsonl") || e.name.endsWith(".jsonl.zst"))) {
const name = e.name.replace(/\.zst$/, "");
const kept = byName.get(name);
if (!kept || (kept.endsWith(".zst") && !p.endsWith(".zst"))) byName.set(name, p);
}
}
};
walk(join(CODEX_HOME, sub));
}
return byName;
}
function readCodex() {
const byName = listRollouts();
const out = [];
for (const [name, p] of byName) {
const st = statSync(p);
const f = readCodexFile(readRollout(p));
out.push({ ...f, id: f.metaId ?? name.slice(0, -6), label: name.slice(8, 27), mtimeMs: st.mtimeMs, compressed: p.endsWith(".zst") });
}
return out.sort((a, b) => b.mtimeMs - a.mtimeMs);
}
// ---------------------------------------------------------------------------
// Cross-file ownership
// ---------------------------------------------------------------------------
/**
* Each request belongs to the file that started first (then: ended first,
* fewer requests, discovery order). Sets f.own (owned requests, largest value
* per field across every copy), f.ownSelf (same requests, this file's values),
* f.dropped, f.startedAt.
*/
function assignOwners(files) {
const max = new Map();
for (const f of files) {
for (const r of f.requests) {
if (!r.key) continue;
const m = max.get(r.key);
if (!m) max.set(r.key, Object.fromEntries(FIELDS.map((k) => [k, r[k]])));
else for (const k of FIELDS) m[k] = Math.max(m[k], r[k]);
}
}
const inf = (t) => (t === undefined ? Infinity : t);
const order = files
.map((f, i) => ({ f, i, s: inf(ms(f.start)), e: inf(ms(f.end)) }))
.sort((a, b) => a.s - b.s || a.e - b.e || a.f.turnCount - b.f.turnCount || a.i - b.i);
const seen = new Set();
for (const { f } of order) {
f.own = [];
f.ownSelf = [];
f.dropped = 0;
for (const r of f.requests) {
if (r.key) {
if (seen.has(r.key)) {
f.dropped++;
continue;
}
seen.add(r.key);
f.own.push({ ...r, ...max.get(r.key) });
} else f.own.push(r);
f.ownSelf.push(r);
}
// A file holding copies starts at its own first request.
let start = f.start;
let end = f.end;
if (f.dropped) {
let lo;
let hi;
for (const r of f.own) {
const t = ms(r.ts);
if (t === undefined) continue;
if (lo === undefined || t < lo) [lo, start] = [t, r.ts];
if (hi === undefined || t > hi) [hi, end] = [t, r.ts];
}
}
f.startedAt = start ?? new Date(f.mtimeMs).toISOString();
f.endedAt = end;
}
}
function summarize(requests, table) {
let tokens = 0;
let output = 0;
let usd = 0;
let unpricedTokens = 0;
let synthetic = 0;
for (const r of requests) {
if (r.model === "<synthetic>" || tokensOf(r) === 0) {
if (r.model === "<synthetic>" && tokensOf(r) > 0) synthetic++;
continue;
}
tokens += tokensOf(r);
output += r.out;
const c = requestCost(table, r);
if (c === null) unpricedTokens += tokensOf(r);
else usd += c;
}
return { tokens, output, usd, unpricedTokens, synthetic };
}
// ---------------------------------------------------------------------------
// Period views (same windows and day boundaries as the commands)
// ---------------------------------------------------------------------------
const utcDay = (t) => new Date(t).toISOString().slice(0, 10);
const localDay = (d) =>
`${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}-${String(d.getDate()).padStart(2, "0")}`;
const median = (xs) => {
if (!xs.length) return 0;
const s = [...xs].sort((a, b) => a - b);
const m = Math.floor(s.length / 2);
return s.length % 2 ? s[m] : (s[m - 1] + s[m]) / 2;
};
/** Non-empty sessions with totals priced at `day`'s table. */
function sessionsAt(files, day) {
const table = tableFor(day);
return files.map((f) => ({ f, startedAt: f.startedAt, ...summarize(f.own, table) })).filter((s) => s.tokens > 0);
}
function todayView(files, now) {
const all = sessionsAt(files, utcDay(now.getTime()));
const today = localDay(now);
const t = all.filter((s) => localDay(new Date(s.startedAt)) === today);
const out = {
sessionCount: t.length,
totalTokens: t.reduce((a, s) => a + s.tokens, 0),
totalSpendUSD: t.reduce((a, s) => a + s.usd, 0),
};
const earliest = Math.min(...all.map((s) => ms(s.startedAt)).filter((x) => x !== undefined));
const days = Math.floor((now.getTime() - earliest) / DAY);
const tier = [30, 14, 7, 3].find((n) => days >= n);
if (all.length && tier) {
const from = utcDay(now.getTime() - tier * DAY);
const perDay = new Map();
for (const s of all) {
if (s.startedAt.slice(0, 10) < from) continue;
const d = perDay.get(s.startedAt.slice(0, 10)) ?? { usd: 0, tokens: 0 };
d.usd += s.usd;
d.tokens += s.tokens;
perDay.set(s.startedAt.slice(0, 10), d);
}
out.baselineDays = tier;
out.medianTokensPerDay = median([...perDay.values()].map((d) => d.tokens));
out.medianSpendPerDayUSD = median([...perDay.values()].map((d) => d.usd));
}
return out;
}
function forecastView(files, now) {
const all = sessionsAt(files, utcDay(now.getTime()));
const t = now.getTime();
const lastDay = new Date(now.getFullYear(), now.getMonth() + 1, 0).getDate();
const remaining = lastDay - now.getDate();
if (!all.length || remaining <= 1 || !all.some((s) => ms(s.startedAt) >= t - 14 * DAY)) return { status: "not-ok" };
const earliest = Math.min(...all.map((s) => ms(s.startedAt)).filter((x) => x !== undefined));
const days = Math.floor((t - earliest) / DAY);
if (days < 7) return { status: "not-ok" };
const windowDays = days >= 14 ? 14 : 7;
const base = t - windowDays * DAY;
const sampled = all.filter((s) => ms(s.startedAt) >= base);
const buckets = new Array(windowDays).fill(0);
for (const s of sampled) buckets[Math.min(windowDays - 1, Math.floor((ms(s.startedAt) - base) / DAY))] += s.usd;
const avg = buckets.reduce((a, b) => a + b, 0) / windowDays;
const monthStart = new Date(now.getFullYear(), now.getMonth(), 1).getTime();
const lastMonthStart = new Date(now.getFullYear(), now.getMonth() - 1, 1).getTime();
const soFar = all.filter((s) => ms(s.startedAt) >= monthStart).reduce((a, s) => a + s.usd, 0);
const lastMonth = all.filter((s) => ms(s.startedAt) >= lastMonthStart && ms(s.startedAt) < monthStart);
return {
status: "ok",
windowDays,
sessionsSampled: sampled.length,
avgDailySpendUSD: avg,
spendSoFarUSD: soFar,
projectedSpendUSD: soFar + avg * remaining,
lastMonthSpendUSD: lastMonth.length ? lastMonth.reduce((a, s) => a + s.usd, 0) : null,
};
}
function trendView(files, now, sinceDays) {
const until = utcDay(now.getTime());
const since = utcDay(now.getTime() - sinceDays * DAY);
const days = new Map();
for (const s of sessionsAt(files, until)) {
const d = s.startedAt.slice(0, 10);
if (d < since || d > until) continue;
const b = days.get(d) ?? { sessions: 0, output: 0, tokens: 0, usd: 0 };
b.sessions++;
b.output += s.output;
b.tokens += s.tokens;
b.usd += s.usd;
days.set(d, b);
}
return { since, until, days };
}
function impactView(files, now, pivotDay) {
const pivot = `${pivotDay}T00:00:00.000Z`;
const before = { sessionCount: 0, totalTokens: 0, usd: 0 };
const after = { sessionCount: 0, totalTokens: 0, usd: 0 };
for (const s of sessionsAt(files, utcDay(now.getTime()))) {
const b = s.startedAt < pivot ? before : after;
b.sessionCount++;
b.totalTokens += s.tokens;
b.usd += s.usd;
}
return { before, after };
}
// ---------------------------------------------------------------------------
// Sipcode runs
// ---------------------------------------------------------------------------
// Temporary HOME (fresh usage cache) and cwd (receipts write there), made in main().
let sipHome;
let sipCwd;
export function sipcode(argv) {
const before = new Date();
let out;
try {
out = execFileSync(process.execPath, [CLI, ...argv], {
encoding: "utf8",
maxBuffer: 1 << 28,
cwd: sipCwd,
stdio: ["ignore", "pipe", "ignore"],
env: {
...process.env,
HOME: sipHome,
USERPROFILE: sipHome,
SIPCODE_PROJECTS_DIR: CLAUDE_ROOT,
CODEX_HOME,
NO_COLOR: "1",
},
});
} catch (err) {
out = err.stdout ?? "";
}
const after = new Date();
let json;
try {
json = JSON.parse(out);
} catch {
json = undefined;
}
return { json, before, after };
}
/** Compare a time-dependent view at the run's start and end instant; both must agree unless a boundary was crossed. */
function atRun(run, view) {
const a = view(run.before);
const b = view(run.after);
return JSON.stringify(a) === JSON.stringify(b) ? [a] : [a, b];
}
const USD = 1e-6;
function compareAgent(agent, files, pass) {
const tag = `[${agent} ${pass}]`;
// stats --since all
const st = sipcode(["stats", "--since", "all", "--json", "--agent", agent]);
const all = sessionsAt(files, utcDay(st.before.getTime())).filter((s) => s.startedAt < st.before.toISOString());
check(`${tag} stats sessions`, all.length, st.json?.sessionCount);
check(
`${tag} stats totalTokens`,
all.reduce((a, s) => a + s.tokens, 0),
st.json?.totals?.totalTokens,
);
const usd = all.reduce((a, s) => a + s.usd, 0);
// --json rounds USD to 4 decimals: compare to that precision (stricter than the cent).
check(`${tag} stats estCostUSD (4 dp)`, usd, st.json?.totals?.estCostUSD, 0.00005 + 1e-9);
// today
const td = sipcode(["today", "--json", "--agent", agent]);
const tv = atRun(td, (now) => todayView(files, now));
const tj = td.json ?? {};
const pick = (f) => tv.find(f) ?? tv[0];
const tMine = pick((v) => v.totalTokens === (tj.today?.totalTokens ?? 0));
check(`${tag} today sessions`, tMine.sessionCount, tj.today?.sessionCount ?? 0);
check(`${tag} today totalTokens`, tMine.totalTokens, tj.today?.totalTokens ?? 0);
check(`${tag} today totalSpendUSD`, tMine.totalSpendUSD, tj.today?.totalSpendUSD ?? 0, USD);
if (tj.baseline) {
check(`${tag} today baseline days`, tMine.baselineDays, tj.baseline.windowDays);
check(`${tag} today baseline medianTokensPerDay`, tMine.medianTokensPerDay, tj.baseline.medianTokensPerDay);
check(
`${tag} today baseline medianSpendPerDayUSD`,
tMine.medianSpendPerDayUSD,
tj.baseline.medianSpendPerDayUSD,
USD,
);
} else {
check(`${tag} today baseline present`, tMine.baselineDays !== undefined, false);
}
// forecast
const fc = sipcode(["forecast", "--json", "--agent", agent]);
const fv = atRun(fc, (now) => forecastView(files, now));
const fj = fc.json ?? {};
const fMine = fv.find((v) => v.sessionsSampled === fj.trajectoryInput?.sessionsSampled) ?? fv[0];
check(`${tag} forecast status ok`, fMine.status === "ok", fj.status === "ok");
if (fMine.status === "ok" && fj.status === "ok") {
check(`${tag} forecast windowDays`, fMine.windowDays, fj.trajectoryInput.windowDays);
check(`${tag} forecast sessionsSampled`, fMine.sessionsSampled, fj.trajectoryInput.sessionsSampled);
check(`${tag} forecast avgDailySpendUSD`, fMine.avgDailySpendUSD, fj.trajectoryInput.avgDailySpendUSD, USD);
check(`${tag} forecast spendSoFarUSD`, fMine.spendSoFarUSD, fj.monthEnd.spendSoFarUSD, USD);
check(`${tag} forecast projectedSpendUSD`, fMine.projectedSpendUSD, fj.monthEnd.projectedSpendUSD, USD);
check(
`${tag} forecast lastMonthSpendUSD`,
fMine.lastMonthSpendUSD ?? -1,
fj.comparison?.lastMonthSpendUSD ?? -1,
USD,
);
}
// trend (30 days, UTC days): tokens and output per day, then cost per day
for (const metric of ["output-ratio", "cost-per-session"]) {
const tr = sipcode(["trend", "--since", "30d", "--metric", metric, "--json", "--agent", agent]);
const views = atRun(tr, (now) => trendView(files, now, 30));
const theirs = (tr.json?.days ?? []).filter((d) => d.sessions > 0);
const mine = views.find((v) => v.since === tr.json?.window?.since) ?? views[0];
let sessions = 0;
let numer = 0;
let denom = 0;
let dayMismatch = 0;
for (const d of theirs) {
const m = mine.days.get(d.date) ?? { sessions: 0, output: 0, tokens: 0, usd: 0 };
const mn = metric === "output-ratio" ? m.output : m.usd;
const md = metric === "output-ratio" ? m.tokens : m.sessions;
if (m.sessions !== d.sessions || Math.abs(mn - d.numerator) > USD || md !== d.denominator) dayMismatch++;
}
for (const m of mine.days.values()) {
sessions += m.sessions;
numer += metric === "output-ratio" ? m.output : m.usd;
denom += metric === "output-ratio" ? m.tokens : m.sessions;
}
check(`${tag} trend ${metric} active days`, mine.days.size, theirs.length);
check(`${tag} trend ${metric} days differing`, 0, dayMismatch);
check(
`${tag} trend ${metric} sessions`,
sessions,
theirs.reduce((a, d) => a + d.sessions, 0),
);
check(
`${tag} trend ${metric} numerator sum`,
numer,
theirs.reduce((a, d) => a + d.numerator, 0),
USD * 100,
);
check(
`${tag} trend ${metric} denominator sum`,
denom,
theirs.reduce((a, d) => a + d.denominator, 0),
USD,
);
}
// impact with an explicit pivot (no install marker needed)
const starts = files
.filter((f) => summarize(f.own, tableFor("9999-12-31")).tokens > 0)
.map((f) => f.startedAt)
.sort();
const pivotDay = starts.length ? starts[Math.floor(starts.length / 2)].slice(0, 10) : "2026-01-01";
const im = sipcode(["impact", "--since", pivotDay, "--json", "--agent", agent]);
const iv = impactView(files, im.before, pivotDay);
for (const side of ["before", "after"]) {
check(`${tag} impact ${side} sessions`, iv[side].sessionCount, im.json?.[side]?.sessionCount);
check(`${tag} impact ${side} totalTokens`, iv[side].totalTokens, im.json?.[side]?.totalTokens);
// impact rounds USD to the cent.
check(`${tag} impact ${side} estCostUSD (to the cent)`, iv[side].usd, im.json?.[side]?.estCostUSD, 0.005 + 1e-9);
}
}
// ---------------------------------------------------------------------------
// Single-session checks: short ids, why / receipt own requests
// ---------------------------------------------------------------------------
function shortId(id, agent) {
if (agent === "codex" && /^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}(?:-|$)/i.test(id)) return id.slice(0, 18);
return id.slice(0, 8);
}
function sessionChecks(agent, files) {
const tag = `[${agent}]`;
let ambiguous = 0;
for (const f of files) {
const s = shortId(f.id, agent);
if (files.filter((g) => g.id.startsWith(s)).length !== 1) ambiguous++;
}
check(`${tag} short ids that match more than one session (of ${files.length})`, 0, ambiguous);
const live = files
.filter((f) => summarize(f.own, tableFor("9999-12-31")).tokens > 0)
.sort((a, b) => (a.id < b.id ? -1 : 1));
const step = Math.max(1, Math.floor(live.length / WHY_SAMPLES));
const sample = new Set(live.filter((_, i) => i % step === 0).slice(0, WHY_SAMPLES));
const resumed = live.filter((f) => f.dropped > 0);
for (const f of resumed) sample.add(f);
console.log(`${tag} why sample: ${sample.size} sessions (${resumed.length} hold copied requests)`);
let ownVsMaxDiffer = 0;
for (const f of sample) {
const s = shortId(f.id, agent);
const w = sipcode(["why", "--session", s, "--agent", agent, "--json"]).json;
const own = summarize(f.ownSelf, tableFor(f.startedAt.slice(0, 10)));
const maxed = summarize(f.own, tableFor(f.startedAt.slice(0, 10)));
if (own.tokens !== maxed.tokens) ownVsMaxDiffer++;
const label = `${tag} why ${f.dropped > 0 ? "resumed " : ""}session (${f.own.length} own requests, ${f.dropped} copied)`;
check(`${label} id resolves`, s, w?.header?.sessionIdShort);
check(`${label} totalTokens`, own.tokens, w?.punchline?.totalTokens);
check(`${label} estCostUSD`, own.usd, w?.totals?.estCostUSD, USD);
if (f.dropped > 0) {
const r = sipcode(["receipt", s, "--agent", agent, "--json", "--html-only", "--no-share"]).json;
// The hero is the session's tokens, or (post-install receipt) the estimated savings why reports.
if (r?.variant === "post-install")
check(`${label} receipt (post-install) savings vs why`, w?.estimatedSavings?.totalTokens, r?.hero?.tokens);
else check(`${label} receipt tokens`, own.tokens, r?.hero?.tokens);
}
}
console.log(`${tag} sampled sessions whose own-file values differ from the max across copies: ${ownVsMaxDiffer}`);
}
// ---------------------------------------------------------------------------
// Codex limits
// ---------------------------------------------------------------------------
const MIN = 60_000;
const AUTO_REVIEW = "codex-auto-review";
const EPS = 1e-9;
/** Every limit reading in the Codex logs, in reading order. */
function limitReadings() {
const out = [];
let malformed = 0;
for (const p of listRollouts().values()) {
for (const raw of readRollout(p).split(/\r?\n/)) {
if (!raw.includes('"rate_limits"')) continue;
let e;
try {
e = JSON.parse(raw);
} catch {
malformed++;
continue;
}
if (!e || typeof e !== "object" || Array.isArray(e)) {
malformed++;
continue;
}
if (e.type !== "event_msg" || e.payload?.type !== "token_count") continue;
const rl = e.payload.rate_limits;
if (rl === null || rl === undefined) continue;
const at = typeof e.timestamp === "string" ? Date.parse(e.timestamp) : NaN;
if (typeof rl !== "object" || Array.isArray(rl) || !Number.isFinite(at)) {
malformed++;
continue;
}
for (const slot of ["primary", "secondary"]) {
const w = rl[slot];
if (w === null || w === undefined) continue;
const obj = typeof w === "object" && !Array.isArray(w);
const ok = obj && [w.used_percent, w.window_minutes, w.resets_at].every((x) => typeof x === "number" && Number.isFinite(x));
if (!ok || w.used_percent < 0 || w.window_minutes <= 0 || w.resets_at <= 0) {
malformed++;
continue;
}
out.push({
at,
limitId: (typeof rl.limit_id === "string" && rl.limit_id) || "codex",
plan: (typeof rl.plan_type === "string" && rl.plan_type) || null,
wm: w.window_minutes,
pct: w.used_percent,
resetsAt: w.resets_at > 1e12 ? w.resets_at : w.resets_at * 1000,
});
}
}
}
out.sort((a, b) => a.at - b.at || a.resetsAt - b.resetsAt || a.pct - b.pct);
return { readings: out, malformed };
}
/** Windows (key "limitId|minutes") with their periods: reset time, max, first and newest time, rises, start. */
function limitWindows(readings) {
const windows = new Map();
for (const r of readings) {
if (r.at > r.resetsAt) continue; // stale: logged after its own reset time
const key = `${r.limitId}|${r.wm}`;
let w = windows.get(key);
if (!w) windows.set(key, (w = { limitId: r.limitId, wm: r.wm, periods: [] }));
const cur = w.periods.at(-1);
if (cur && cur.resetsAt - r.resetsAt > 15 * MIN) continue; // stale: an earlier reset time than the open period's
const shift = cur ? r.resetsAt - cur.resetsAt : 0;
if (
!cur ||
Math.abs(shift) > Math.max(15 * MIN, r.wm * MIN * 0.01) || // beyond drift: a new reset time
r.at > cur.resetsAt || // past the period's reset time
(shift > 15 * MIN && r.pct < cur.max) // early reset
) {
w.periods.push({ resetsAt: r.resetsAt, max: r.pct, first: r.at, newest: r.at, plan: r.plan, rises: r.pct > 0 ? [{ at: r.at, rise: r.pct }] : [] });
continue;
}
if (r.pct > cur.max) {
cur.rises.push({ at: r.at, rise: r.pct - cur.max });
cur.max = r.pct;
}
cur.resetsAt = r.resetsAt;
cur.newest = r.at;
cur.plan = r.plan;
}
for (const w of windows.values())
w.periods.forEach((p, i) => {
p.start = Math.max(Math.min(p.resetsAt - w.wm * MIN, p.first), i > 0 ? w.periods[i - 1].newest : -Infinity);
});
return windows;
}
const inPeriod = (p, requests) =>
requests.filter((r) => r.at > p.start && r.at <= p.newest).sort((a, b) => a.at - b.at);
/** Per line of "what used it" (session id, or codex-auto-review): percent and tokens; plus unattributed. */
function attributeLimit(p, requests) {
const rs = inPeriod(p, requests);
const lines = new Map();
const line = (r) => {
const key = r.model === AUTO_REVIEW ? AUTO_REVIEW : r.session;
let l = lines.get(key);
if (!l) lines.set(key, (l = { percent: 0, tokens: 0 }));
return l;
};
for (const r of rs) line(r).tokens += r.tokens;
let unattributed = 0;
let k = 0;
for (const { at, rise } of p.rises) {
const group = [];
while (k < rs.length && rs[k].at <= at) group.push(rs[k++]);
const t = group.reduce((a, r) => a + r.tokens, 0);
if (t === 0) {
unattributed += rise;
continue;