Skip to content

Commit 06bfe33

Browse files
committed
fix(codex): accept numeric session timestamps
Normalize session token_count timestamps through the same helper used by saved exec rows so numeric millisecond or second values are not skipped. This keeps string timestamp behavior unchanged, removes the now-unused string-only accessor, and adds a regression test for numeric session timestamps.
1 parent c89baa6 commit 06bfe33

1 file changed

Lines changed: 54 additions & 11 deletions

File tree

‎rust/crates/ccusage/src/codex_loader.rs‎

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ fn visit_codex_session_entry(
231231
if entry_type != Some("event_msg") {
232232
return Ok(());
233233
}
234-
let Some(timestamp) = value.timestamp.as_ref().and_then(CodexTimestamp::as_str) else {
234+
let Some(timestamp) = normalize_codex_timestamp(value.timestamp.as_ref()) else {
235235
return Ok(());
236236
};
237237
let Some(payload) = value.payload.as_ref() else {
@@ -282,7 +282,7 @@ fn visit_codex_session_entry(
282282

283283
visit(CodexTokenUsageEvent {
284284
session_id: session_id.to_string(),
285-
timestamp: timestamp.to_string(),
285+
timestamp,
286286
model,
287287
input_tokens: raw_usage.input_tokens,
288288
cached_input_tokens: raw_usage.cached_input_tokens.min(raw_usage.input_tokens),
@@ -637,15 +637,6 @@ enum CodexTimestamp<'a> {
637637
Number(u64),
638638
}
639639

640-
impl CodexTimestamp<'_> {
641-
fn as_str(&self) -> Option<&str> {
642-
match self {
643-
Self::String(text) => Some(text),
644-
Self::Number(_) => None,
645-
}
646-
}
647-
}
648-
649640
#[derive(Default, Deserialize)]
650641
struct CodexPayload<'a> {
651642
#[serde(rename = "type", borrow, default)]
@@ -1060,6 +1051,58 @@ mod tests {
10601051
fs::remove_dir_all(dir).unwrap();
10611052
}
10621053

1054+
#[test]
1055+
fn loads_session_usage_with_numeric_timestamp() {
1056+
let dir = temp_dir("numeric-session-timestamp");
1057+
let file = dir.join("session.jsonl");
1058+
fs::write(
1059+
&file,
1060+
[
1061+
json!({
1062+
"timestamp": "2026-01-02T00:00:00.000Z",
1063+
"type": "turn_context",
1064+
"payload": {
1065+
"model": "gpt-5",
1066+
},
1067+
})
1068+
.to_string(),
1069+
json!({
1070+
"timestamp": 1767312001000_u64,
1071+
"type": "event_msg",
1072+
"payload": {
1073+
"type": "token_count",
1074+
"info": {
1075+
"total_token_usage": {
1076+
"input_tokens": 100,
1077+
"cached_input_tokens": 10,
1078+
"output_tokens": 50,
1079+
"reasoning_output_tokens": 0,
1080+
"total_tokens": 150,
1081+
},
1082+
"model": "gpt-5",
1083+
},
1084+
},
1085+
})
1086+
.to_string(),
1087+
]
1088+
.join("\n"),
1089+
)
1090+
.unwrap();
1091+
1092+
let events = load_codex_events_from_directory(&dir, true).unwrap();
1093+
1094+
assert_eq!(events.len(), 1);
1095+
assert_eq!(events[0].session_id, "session");
1096+
assert_eq!(events[0].timestamp, "2026-01-02T00:00:01.000Z");
1097+
assert_eq!(events[0].model.as_deref(), Some("gpt-5"));
1098+
assert_eq!(events[0].input_tokens, 100);
1099+
assert_eq!(events[0].cached_input_tokens, 10);
1100+
assert_eq!(events[0].output_tokens, 50);
1101+
assert_eq!(events[0].total_tokens, 150);
1102+
1103+
fs::remove_dir_all(dir).unwrap();
1104+
}
1105+
10631106
#[test]
10641107
fn uses_nested_model_name_for_standalone_exec_usage() {
10651108
let dir = temp_dir("model-name");

0 commit comments

Comments
 (0)