Open History to see the last commit
1import { describe, expect, it } from "@effect/vitest";
2
3import { initialCodexScanState, parseCodexLine } from "./codexUsage.ts";
4
5describe("parseCodexLine", () => {
6 const sessionMeta = JSON.stringify({
7 type: "session_meta",
8 timestamp: "2026-08-01T05:17:41.289Z",
9 payload: { type: "session_meta", id: "019fbbc1-b12c-7360-a685-28c181f0025f" },
10 });
11 const turnContext = JSON.stringify({
12 type: "turn_context",
13 timestamp: "2026-08-01T05:17:42.694Z",
14 payload: { type: "turn_context", model: "gpt-5.6-sol" },
15 });
16 const tokenCount = (inputTokens: number, cached: number, output: number, reasoning: number) =>
17 JSON.stringify({
18 type: "event_msg",
19 timestamp: "2026-08-01T05:17:49.919Z",
20 payload: {
21 type: "token_count",
22 info: {
23 last_token_usage: {
24 input_tokens: inputTokens,
25 cached_input_tokens: cached,
26 cache_write_input_tokens: 0,
27 output_tokens: output,
28 reasoning_output_tokens: reasoning,
29 },
30 },
31 },
32 });
33
34 it("attributes usage to the model from the preceding turn context", () => {
35 const state = initialCodexScanState();
36 parseCodexLine(sessionMeta, state);
37 parseCodexLine(turnContext, state);
38 const record = parseCodexLine(tokenCount(19239, 11008, 299, 116), state);
39
40 expect(record?.provider).toBe("codex");
41 expect(record?.model).toBe("gpt-5.6-sol");
42 expect(record?.sessionId).toBe("019fbbc1-b12c-7360-a685-28c181f0025f");
43 expect(record?.totals.uncachedInputTokens).toBe(19239 - 11008);
44 expect(record?.totals.cachedInputTokens).toBe(11008);
45 expect(record?.totals.reasoningTokens).toBe(116);
46 });
47
48 it("skips a repeated token_count so deltas are not double counted", () => {
49 const state = initialCodexScanState();
50 parseCodexLine(turnContext, state);
51 const first = parseCodexLine(tokenCount(100, 0, 10, 0), state);
52 const repeat = parseCodexLine(tokenCount(100, 0, 10, 0), state);
53
54 expect(first).not.toBeNull();
55 expect(repeat).toBeNull();
56 });
57
58 it("drops usage that arrives before any model is known", () => {
59 const state = initialCodexScanState();
60 expect(parseCodexLine(tokenCount(100, 0, 10, 0), state)).toBeNull();
61 });
62
63 it("does not let a pre-model event poison the duplicate signature", () => {
64 const state = initialCodexScanState();
65 expect(parseCodexLine(tokenCount(100, 0, 10, 0), state)).toBeNull();
66 parseCodexLine(turnContext, state);
67 expect(parseCodexLine(tokenCount(100, 0, 10, 0), state)).not.toBeNull();
68 });
69
70 it("carries the service tier from the latest thread settings", () => {
71 const settings = (thread_settings: Record<string, unknown>) =>
72 JSON.stringify({
73 type: "event_msg",
74 timestamp: "2026-08-01T05:17:42.000Z",
75 payload: { type: "thread_settings_applied", thread_settings },
76 });
77 const state = initialCodexScanState();
78 parseCodexLine(turnContext, state);
79 const speedAfter = (line: string, output: number) => {
80 parseCodexLine(line, state);
81 return parseCodexLine(tokenCount(100, 0, output, 0), state)?.speed;
82 };
83
84 expect(parseCodexLine(tokenCount(100, 0, 1, 0), state)?.speed).toBe("standard");
85 expect(speedAfter(settings({ service_tier: "ultrafast" }), 2)).toBe("ultrafast");
86 expect(speedAfter(settings({ service_tier: "priority" }), 3)).toBe("fast");
87 expect(speedAfter(settings({ model: "gpt-6-astra" }), 4)).toBe("standard");
88 });
89
90 // A forked/subagent rollout opens with the parent's history copied in and
91 // every line re-stamped to the fork instant, then the ancestors' session
92 // metas. Counting those again multiplied usage ~1.85x on real data (#5758).
93 describe("forked rollouts", () => {
94 const meta = (overrides: {
95 id: string;
96 timestamp: string;
97 forkedFromId?: string;
98 spawnParentId?: string;
99 }) =>
100 JSON.stringify({
101 type: "session_meta",
102 timestamp: overrides.timestamp,
103 payload: {
104 type: "session_meta",
105 id: overrides.id,
106 ...(overrides.forkedFromId === undefined
107 ? {}
108 : { forked_from_id: overrides.forkedFromId }),
109 ...(overrides.spawnParentId === undefined
110 ? {}
111 : {
112 source: {
113 subagent: { thread_spawn: { parent_thread_id: overrides.spawnParentId } },
114 },
115 }),
116 },
117 });
118 const stamped = (timestamp: string, line: string) => {
119 const parsed = JSON.parse(line) as { timestamp: string };
120 parsed.timestamp = timestamp;
121 return JSON.stringify(parsed);
122 };
123
124 it("keeps the child session id over copied ancestor metas", () => {
125 const state = initialCodexScanState();
126 parseCodexLine(meta({ id: "child", timestamp: "2026-08-01T05:00:00.000Z" }), state);
127 parseCodexLine(meta({ id: "parent", timestamp: "2026-08-01T05:00:00.000Z" }), state);
128 parseCodexLine(turnContext, state);
129 const record = parseCodexLine(tokenCount(100, 0, 10, 0), state);
130
131 expect(record?.sessionId).toBe("child");
132 });
133
134 it("drops the re-stamped copied burst and keeps the first real event", () => {
135 const state = initialCodexScanState();
136 const forkInstant = "2026-08-01T05:00:00.000Z";
137 parseCodexLine(meta({ id: "child", timestamp: forkInstant, forkedFromId: "parent" }), state);
138 parseCodexLine(meta({ id: "parent", timestamp: forkInstant }), state);
139 parseCodexLine(stamped(forkInstant, turnContext), state);
140
141 expect(
142 parseCodexLine(stamped("2026-08-01T05:00:00.001Z", tokenCount(100, 0, 10, 0)), state),
143 ).toBeNull();
144 expect(
145 parseCodexLine(stamped("2026-08-01T05:00:00.002Z", tokenCount(200, 0, 20, 0)), state),
146 ).toBeNull();
147
148 const real = parseCodexLine(
149 stamped("2026-08-01T05:00:06.000Z", tokenCount(300, 0, 30, 0)),
150 state,
151 );
152 expect(real).not.toBeNull();
153 expect(real?.totals.outputTokens).toBe(30);
154
155 const next = parseCodexLine(
156 stamped("2026-08-01T05:00:06.100Z", tokenCount(400, 0, 40, 0)),
157 state,
158 );
159 expect(next).not.toBeNull();
160 });
161
162 it("recognizes subagent spawns without forked_from_id", () => {
163 const state = initialCodexScanState();
164 const spawnInstant = "2026-08-01T05:00:00.000Z";
165 parseCodexLine(
166 meta({ id: "child", timestamp: spawnInstant, spawnParentId: "parent" }),
167 state,
168 );
169 parseCodexLine(stamped(spawnInstant, turnContext), state);
170 expect(
171 parseCodexLine(stamped("2026-08-01T05:00:00.001Z", tokenCount(100, 0, 10, 0)), state),
172 ).toBeNull();
173 });
174
175 it("does not suppress anything in a rollout that is not a fork", () => {
176 const state = initialCodexScanState();
177 parseCodexLine(meta({ id: "root", timestamp: "2026-08-01T05:00:00.000Z" }), state);
178 parseCodexLine(stamped("2026-08-01T05:00:00.100Z", turnContext), state);
179 const record = parseCodexLine(
180 stamped("2026-08-01T05:00:00.200Z", tokenCount(100, 0, 10, 0)),
181 state,
182 );
183 expect(record).not.toBeNull();
184 });
185 });
186});