Skip to content

Commit e417520

Browse files
committed
fix(codex): preserve exec model names
Teach the shared Codex model extractor to read model_name at the root of direct and nested exec JSON payloads. This prevents saved codex exec usage rows from falling back to the legacy model when model_name is the only model field, and narrows JSONL markers to key-colon matches.
1 parent 85f0b33 commit e417520

1 file changed

Lines changed: 36 additions & 4 deletions

File tree

‎apps/ccusage/src/adapter/codex/parser.ts‎

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ const CODEX_JSONL_MARKERS = [
2727
'turn_context',
2828
'"type":"token_count"',
2929
'"type": "token_count"',
30-
'"usage"',
31-
'"input_tokens"',
32-
'"prompt_tokens"',
30+
'"usage":',
31+
'"input_tokens":',
32+
'"prompt_tokens":',
3333
];
3434
const ENCODED_CODEX_EVENT_NUMBER_STRIDE = 5;
3535
const TOKEN_USAGE_EVENT_KEY_SEPARATOR = '\0';
@@ -174,7 +174,7 @@ function extractModel(value: unknown): string | undefined {
174174
}
175175
}
176176

177-
const fallbackModel = asNonEmptyString(payload.model);
177+
const fallbackModel = asNonEmptyString(payload.model) ?? asNonEmptyString(payload.model_name);
178178
if (fallbackModel != null) {
179179
return fallbackModel;
180180
}
@@ -935,6 +935,38 @@ if (import.meta.vitest != null) {
935935
]);
936936
});
937937

938+
it('uses nested model_name when saved codex exec usage has no earlier model', async () => {
939+
await using fixture = await createFixture({
940+
codex: {},
941+
exec: {
942+
'solo.jsonl': JSON.stringify({
943+
type: 'result',
944+
data: {
945+
timestamp: '2026-03-01T00:00:00.000Z',
946+
model_name: 'gpt-5.2-codex',
947+
usage: {
948+
input_tokens: 10,
949+
output_tokens: 5,
950+
total_tokens: 15,
951+
},
952+
},
953+
}),
954+
},
955+
});
956+
vi.stubEnv('CODEX_HOME', fixture.getPath('codex'));
957+
vi.stubEnv('CODEX_EXEC_LOG_DIR', fixture.getPath('exec'));
958+
959+
await expect(loadTokenUsageEvents()).resolves.toMatchObject([
960+
{
961+
sessionId: 'solo',
962+
model: 'gpt-5.2-codex',
963+
inputTokens: 10,
964+
outputTokens: 5,
965+
totalTokens: 15,
966+
},
967+
]);
968+
});
969+
938970
it('deduplicates copied branch history across session files', async () => {
939971
const copiedHistory = [
940972
JSON.stringify({

0 commit comments

Comments
 (0)