Skip to content

Commit 01b690b

Browse files
fix(codex): try alternate timestamp fields when top-level timestamp is malformed
The model-date resolution chain in codex_model_timestamp_from_result used raw_or_normalized_codex_timestamp, which returned any non-empty string as-is regardless of whether it parsed as a date. That short-circuited the or_else chain, so a malformed top-level `timestamp` would prevent the parser from looking at `created_at`, `created_at_camel`, nested result-field timestamps, or the file mtime fallback, and forced events through the conservative gpt-5 mapping. Validate the raw string against codex_timestamp_date before short-circuiting; fall through to normalize_codex_timestamp / normalize_value_timestamp and finally None so the rest of the chain can supply a valid date. Mirror the same change for the serde_json::Value path. Also fix the models.dev fallback generator so a gpt-5.x decimal base model is only dropped when a gpt-5.x-codex variant exists on the SAME release date. When the codex variant ships later, keeping the base entry lets events in the gap still resolve to the most recent model actually available then. Co-authored-by: Codesmith <[email protected]>
1 parent 3d80f23 commit 01b690b

3 files changed

Lines changed: 68 additions & 6 deletions

File tree

‎nix/models-dev-gen.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,19 @@ function generateCodexAutoReviewFallbacks(
128128
);
129129

130130
return entries
131-
.filter(([modelId]) => {
131+
.filter(([modelId, model]) => {
132132
const version = baseDecimalVersion(openAiModelName(modelId));
133-
return version == null || !codexDecimalVersions.has(version);
133+
if (version == null || !codexDecimalVersions.has(version)) {
134+
return true;
135+
}
136+
// Drop the base entry only when a `-codex` variant shipped on the same
137+
// date. If the codex variant ships later, keep the base so events in
138+
// the gap still resolve to the most recent model available then.
139+
return !entries.some(
140+
([candidateId, candidateModel]) =>
141+
codexDecimalVersion(openAiModelName(candidateId)) === version &&
142+
candidateModel.release_date === model.release_date,
143+
);
134144
})
135145
.map(([modelId, model]) => ({
136146
releasedOn: model.release_date!,

‎rust/crates/ccusage/src/adapter/codex/loader.rs‎

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,11 @@ mod tests {
581581
}
582582

583583
#[test]
584-
fn resolves_codex_auto_review_with_invalid_event_date_to_conservative_model() {
584+
fn resolves_codex_auto_review_with_invalid_event_date_falls_back_to_file_mtime() {
585+
// When the log's own timestamp fields are unparseable AND no alternate
586+
// timestamp fields are present, the parser falls back to the file's
587+
// modified time so the date-based fallback table still resolves to a
588+
// real model rather than locking in a misleading malformed string.
585589
let fixture = fs_fixture!({
586590
"invalid-month.jsonl": json!({
587591
"type": "turn.completed",
@@ -610,12 +614,43 @@ mod tests {
610614
let events = load_codex_events_from_directory(fixture.root(), true).unwrap();
611615

612616
assert_eq!(events.len(), 2);
617+
// File mtime is "now" at fixture creation, which is on or after every
618+
// entry currently in the fallback table, so resolution lands on the
619+
// newest known model. The exact value updates when the table grows.
613620
assert!(events
614621
.iter()
615-
.all(|event| event.model.as_deref() == Some("gpt-5")));
622+
.all(|event| event.model.as_deref().is_some_and(|model| model
623+
.starts_with("gpt-5"))));
616624
assert!(events.iter().all(|event| event.is_fallback_model));
617625
}
618626

627+
#[test]
628+
fn resolves_codex_auto_review_uses_created_at_when_top_level_timestamp_is_malformed() {
629+
// Regression test for the model-date resolution chain short-circuiting
630+
// on a malformed top-level `timestamp` and ignoring the `created_at`
631+
// fallback field that holds a parseable date.
632+
let fixture = fs_fixture!({
633+
"run.jsonl": json!({
634+
"type": "turn.completed",
635+
"timestamp": "not-a-date",
636+
"created_at": "2025-12-11T00:00:00.000Z",
637+
"model": "codex-auto-review",
638+
"usage": {
639+
"input_tokens": 10,
640+
"output_tokens": 5,
641+
"total_tokens": 15,
642+
},
643+
})
644+
.to_string(),
645+
});
646+
647+
let events = load_codex_events_from_directory(fixture.root(), true).unwrap();
648+
649+
assert_eq!(events.len(), 1);
650+
assert_eq!(events[0].model.as_deref(), Some("gpt-5.2-codex"));
651+
assert!(events[0].is_fallback_model);
652+
}
653+
619654
#[test]
620655
fn resolves_codex_auto_review_turn_context_for_each_event_date() {
621656
let fixture = fs_fixture!({

‎rust/crates/ccusage/src/adapter/codex/parser.rs‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,17 @@ fn raw_or_normalized_codex_timestamp(value: Option<&CodexTimestamp<'_>>) -> Opti
839839
match value? {
840840
CodexTimestamp::String(text) => {
841841
let text = text.trim();
842-
(!text.is_empty()).then(|| text.to_string())
842+
if text.is_empty() {
843+
return None;
844+
}
845+
if codex_timestamp_date(text).is_some() {
846+
return Some(text.to_string());
847+
}
848+
// Malformed string: try parsing-based normalization. If that also
849+
// fails, return None so the caller's `or_else` chain can try the
850+
// next available timestamp field instead of locking in a string
851+
// that downstream date resolution will reject.
852+
normalize_codex_timestamp(value)
843853
}
844854
CodexTimestamp::Number(_) => normalize_codex_timestamp(value),
845855
}
@@ -874,7 +884,14 @@ fn raw_or_normalized_value_timestamp(value: Option<&Value>) -> Option<String> {
874884
if text.is_empty() {
875885
return None;
876886
}
877-
return Some(text.to_string());
887+
if codex_timestamp_date(text).is_some() {
888+
return Some(text.to_string());
889+
}
890+
// Malformed string: try parsing-based normalization. If that also
891+
// fails, return None so the caller's `or_else` chain can try the
892+
// next available timestamp field instead of locking in a string
893+
// that downstream date resolution will reject.
894+
return normalize_value_timestamp(Some(value));
878895
}
879896
normalize_value_timestamp(Some(value))
880897
}

0 commit comments

Comments
 (0)