Repository navigation
Expand file tree
/
Copy pathcodexUsage.test.ts
More file actions
193 lines (174 loc) · 7.5 KB
/
Copy pathcodexUsage.test.ts
File metadata and controls
193 lines (174 loc) · 7.5 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
import { describe, expect, it } from "@effect/vitest";
import { initialCodexScanState, parseCodexLine } from "./codexUsage.ts";
describe("parseCodexLine", () => {
const sessionMeta = JSON.stringify({
type: "session_meta",
timestamp: "2026-08-01T05:17:41.289Z",
payload: { type: "session_meta", id: "019fbbc1-b12c-7360-a685-28c181f0025f" },
});
const turnContext = JSON.stringify({
type: "turn_context",
timestamp: "2026-08-01T05:17:42.694Z",
payload: { type: "turn_context", model: "gpt-5.6-sol" },
});
const tokenCount = (inputTokens: number, cached: number, output: number, reasoning: number) =>
JSON.stringify({
type: "event_msg",
timestamp: "2026-08-01T05:17:49.919Z",
payload: {
type: "token_count",
info: {
last_token_usage: {
input_tokens: inputTokens,
cached_input_tokens: cached,
cache_write_input_tokens: 0,
output_tokens: output,
reasoning_output_tokens: reasoning,
},
},
},
});
it("attributes usage to the model from the preceding turn context", () => {
const state = initialCodexScanState();
parseCodexLine(sessionMeta, state);
parseCodexLine(turnContext, state);
const record = parseCodexLine(tokenCount(19239, 11008, 299, 116), state);
expect(record?.provider).toBe("codex");
expect(record?.model).toBe("gpt-5.6-sol");
expect(record?.sessionId).toBe("019fbbc1-b12c-7360-a685-28c181f0025f");
// Codex reports input_tokens inclusive of the cached portion.
expect(record?.totals.uncachedInputTokens).toBe(19239 - 11008);
expect(record?.totals.cachedInputTokens).toBe(11008);
expect(record?.totals.reasoningTokens).toBe(116);
});
it("skips a repeated token_count so deltas are not double counted", () => {
const state = initialCodexScanState();
parseCodexLine(turnContext, state);
const first = parseCodexLine(tokenCount(100, 0, 10, 0), state);
const repeat = parseCodexLine(tokenCount(100, 0, 10, 0), state);
expect(first).not.toBeNull();
expect(repeat).toBeNull();
});
it("drops usage that arrives before any model is known", () => {
const state = initialCodexScanState();
expect(parseCodexLine(tokenCount(100, 0, 10, 0), state)).toBeNull();
});
it("does not let a pre-model event poison the duplicate signature", () => {
// A token_count before its turn_context is dropped; the identical event
// re-emitted once the model is known must still be counted.
const state = initialCodexScanState();
expect(parseCodexLine(tokenCount(100, 0, 10, 0), state)).toBeNull();
parseCodexLine(turnContext, state);
expect(parseCodexLine(tokenCount(100, 0, 10, 0), state)).not.toBeNull();
});
it("carries the service tier from the latest thread settings", () => {
const settings = (thread_settings: Record<string, unknown>) =>
JSON.stringify({
type: "event_msg",
timestamp: "2026-08-01T05:17:42.000Z",
payload: { type: "thread_settings_applied", thread_settings },
});
const state = initialCodexScanState();
parseCodexLine(turnContext, state);
const speedAfter = (line: string, output: number) => {
parseCodexLine(line, state);
return parseCodexLine(tokenCount(100, 0, output, 0), state)?.speed;
};
expect(parseCodexLine(tokenCount(100, 0, 1, 0), state)?.speed).toBe("standard");
expect(speedAfter(settings({ service_tier: "ultrafast" }), 2)).toBe("ultrafast");
expect(speedAfter(settings({ service_tier: "priority" }), 3)).toBe("fast");
// Codex omits the field when no tier was requested.
expect(speedAfter(settings({ model: "gpt-6-astra" }), 4)).toBe("standard");
});
// A forked/subagent rollout opens with the parent's history copied in and
// every line re-stamped to the fork instant, then the ancestors' session
// metas. Counting those again multiplied usage ~1.85x on real data (#5758).
describe("forked rollouts", () => {
const meta = (overrides: {
id: string;
timestamp: string;
forkedFromId?: string;
spawnParentId?: string;
}) =>
JSON.stringify({
type: "session_meta",
timestamp: overrides.timestamp,
payload: {
type: "session_meta",
id: overrides.id,
...(overrides.forkedFromId === undefined
? {}
: { forked_from_id: overrides.forkedFromId }),
...(overrides.spawnParentId === undefined
? {}
: {
source: {
subagent: { thread_spawn: { parent_thread_id: overrides.spawnParentId } },
},
}),
},
});
const stamped = (timestamp: string, line: string) => {
const parsed = JSON.parse(line) as { timestamp: string };
parsed.timestamp = timestamp;
return JSON.stringify(parsed);
};
it("keeps the child session id over copied ancestor metas", () => {
const state = initialCodexScanState();
parseCodexLine(meta({ id: "child", timestamp: "2026-08-01T05:00:00.000Z" }), state);
parseCodexLine(meta({ id: "parent", timestamp: "2026-08-01T05:00:00.000Z" }), state);
parseCodexLine(turnContext, state);
const record = parseCodexLine(tokenCount(100, 0, 10, 0), state);
expect(record?.sessionId).toBe("child");
});
it("drops the re-stamped copied burst and keeps the first real event", () => {
const state = initialCodexScanState();
const forkInstant = "2026-08-01T05:00:00.000Z";
parseCodexLine(meta({ id: "child", timestamp: forkInstant, forkedFromId: "parent" }), state);
parseCodexLine(meta({ id: "parent", timestamp: forkInstant }), state);
parseCodexLine(stamped(forkInstant, turnContext), state);
// Copied history: written in one burst at the fork instant.
expect(
parseCodexLine(stamped("2026-08-01T05:00:00.001Z", tokenCount(100, 0, 10, 0)), state),
).toBeNull();
expect(
parseCodexLine(stamped("2026-08-01T05:00:00.002Z", tokenCount(200, 0, 20, 0)), state),
).toBeNull();
// The child's first genuine turn lands seconds later and must count.
const real = parseCodexLine(
stamped("2026-08-01T05:00:06.000Z", tokenCount(300, 0, 30, 0)),
state,
);
expect(real).not.toBeNull();
expect(real?.totals.outputTokens).toBe(30);
// Suppression never restarts, even for closely spaced later events.
const next = parseCodexLine(
stamped("2026-08-01T05:00:06.100Z", tokenCount(400, 0, 40, 0)),
state,
);
expect(next).not.toBeNull();
});
it("recognizes subagent spawns without forked_from_id", () => {
const state = initialCodexScanState();
const spawnInstant = "2026-08-01T05:00:00.000Z";
parseCodexLine(
meta({ id: "child", timestamp: spawnInstant, spawnParentId: "parent" }),
state,
);
parseCodexLine(stamped(spawnInstant, turnContext), state);
expect(
parseCodexLine(stamped("2026-08-01T05:00:00.001Z", tokenCount(100, 0, 10, 0)), state),
).toBeNull();
});
it("does not suppress anything in a rollout that is not a fork", () => {
const state = initialCodexScanState();
parseCodexLine(meta({ id: "root", timestamp: "2026-08-01T05:00:00.000Z" }), state);
parseCodexLine(stamped("2026-08-01T05:00:00.100Z", turnContext), state);
const record = parseCodexLine(
stamped("2026-08-01T05:00:00.200Z", tokenCount(100, 0, 10, 0)),
state,
);
expect(record).not.toBeNull();
});
});
});