@@ -52,6 +52,11 @@ enum CodexLineKind {
5252 Headless ,
5353}
5454
55+ struct CodexExecTimestamps {
56+ event : String ,
57+ model : String ,
58+ }
59+
5560fn is_codex_subagent_session ( path : & Path ) -> bool {
5661 let Ok ( mut file) = fs:: File :: open ( path) else {
5762 return false ;
@@ -311,13 +316,16 @@ fn add_codex_exec_event(
311316 return Ok ( ( ) ) ;
312317 } ;
313318 let parsed_model = codex_model_from_result ( value) ;
314- let timestamp =
315- codex_timestamp_from_result ( value) . unwrap_or_else ( || fallback_timestamp. to_string ( ) ) ;
319+ let timestamps = CodexExecTimestamps {
320+ event : codex_timestamp_from_result ( value) . unwrap_or_else ( || fallback_timestamp. to_string ( ) ) ,
321+ model : codex_model_timestamp_from_result ( value)
322+ . unwrap_or_else ( || fallback_timestamp. to_string ( ) ) ,
323+ } ;
316324 visit_codex_exec_usage_event (
317325 session_id,
318326 raw_usage,
319327 parsed_model,
320- timestamp ,
328+ timestamps ,
321329 current_model,
322330 current_model_is_fallback,
323331 visit,
@@ -339,13 +347,17 @@ fn add_codex_exec_event_from_value(
339347 return Ok ( ( ) ) ;
340348 } ;
341349 let parsed_model = codex_model_from_result_value ( & value) ;
342- let timestamp =
343- codex_timestamp_from_result_value ( & value) . unwrap_or_else ( || fallback_timestamp. to_string ( ) ) ;
350+ let timestamps = CodexExecTimestamps {
351+ event : codex_timestamp_from_result_value ( & value)
352+ . unwrap_or_else ( || fallback_timestamp. to_string ( ) ) ,
353+ model : codex_model_timestamp_from_result_value ( & value)
354+ . unwrap_or_else ( || fallback_timestamp. to_string ( ) ) ,
355+ } ;
344356 visit_codex_exec_usage_event (
345357 session_id,
346358 raw_usage,
347359 parsed_model,
348- timestamp ,
360+ timestamps ,
349361 current_model,
350362 current_model_is_fallback,
351363 visit,
@@ -356,20 +368,20 @@ fn visit_codex_exec_usage_event(
356368 session_id : & str ,
357369 raw_usage : CodexRawUsage ,
358370 parsed_model : Option < String > ,
359- timestamp : String ,
371+ timestamps : CodexExecTimestamps ,
360372 current_model : & mut Option < String > ,
361373 current_model_is_fallback : & mut bool ,
362374 visit : & mut impl FnMut ( CodexTokenUsageEvent ) -> Result < ( ) > ,
363375) -> Result < ( ) > {
364376 let ( model, is_fallback_model) = resolve_codex_usage_model (
365377 parsed_model,
366- & timestamp ,
378+ & timestamps . model ,
367379 current_model,
368380 current_model_is_fallback,
369381 ) ;
370382 visit ( CodexTokenUsageEvent {
371383 session_id : session_id. to_string ( ) ,
372- timestamp,
384+ timestamp : timestamps . event ,
373385 model,
374386 input_tokens : raw_usage. input_tokens ,
375387 cached_input_tokens : raw_usage. cached_input_tokens . min ( raw_usage. input_tokens ) ,
@@ -515,13 +527,41 @@ fn codex_log_model_fallback(model: &str, timestamp: &str) -> Option<&'static str
515527fn codex_timestamp_date ( timestamp : & str ) -> Option < & str > {
516528 let date = timestamp. get ( ..10 ) ?;
517529 let bytes = date. as_bytes ( ) ;
518- ( bytes. len ( ) == 10
530+ if ! ( bytes. len ( ) == 10
519531 && bytes[ 0 ..4 ] . iter ( ) . all ( u8:: is_ascii_digit)
520532 && bytes[ 4 ] == b'-'
521533 && bytes[ 5 ..7 ] . iter ( ) . all ( u8:: is_ascii_digit)
522534 && bytes[ 7 ] == b'-'
523535 && bytes[ 8 ..10 ] . iter ( ) . all ( u8:: is_ascii_digit) )
524- . then_some ( date)
536+ {
537+ return None ;
538+ }
539+ let year = codex_date_part ( & bytes[ 0 ..4 ] ) ?;
540+ let month = codex_date_part ( & bytes[ 5 ..7 ] ) ?;
541+ let day = codex_date_part ( & bytes[ 8 ..10 ] ) ?;
542+ let max_day = codex_days_in_month ( year, month) ?;
543+ ( day >= 1 && day <= max_day) . then_some ( date)
544+ }
545+
546+ fn codex_date_part ( bytes : & [ u8 ] ) -> Option < u16 > {
547+ bytes. iter ( ) . try_fold ( 0u16 , |value, byte| {
548+ let digit = byte. checked_sub ( b'0' ) ?;
549+ ( digit <= 9 ) . then_some ( value * 10 + u16:: from ( digit) )
550+ } )
551+ }
552+
553+ fn codex_days_in_month ( year : u16 , month : u16 ) -> Option < u16 > {
554+ match month {
555+ 1 | 3 | 5 | 7 | 8 | 10 | 12 => Some ( 31 ) ,
556+ 4 | 6 | 9 | 11 => Some ( 30 ) ,
557+ 2 if codex_is_leap_year ( year) => Some ( 29 ) ,
558+ 2 => Some ( 28 ) ,
559+ _ => None ,
560+ }
561+ }
562+
563+ fn codex_is_leap_year ( year : u16 ) -> bool {
564+ year % 4 == 0 && year % 100 != 0 || year % 400 == 0
525565}
526566
527567fn codex_session_id ( sessions_dir : & Path , path : & Path ) -> String {
@@ -704,6 +744,30 @@ fn codex_timestamp_from_result(value: &CodexLogEntry<'_>) -> Option<String> {
704744 } )
705745}
706746
747+ fn codex_model_timestamp_from_result ( value : & CodexLogEntry < ' _ > ) -> Option < String > {
748+ raw_or_normalized_codex_timestamp ( value. timestamp . as_ref ( ) )
749+ . or_else ( || raw_or_normalized_codex_timestamp ( value. created_at . as_ref ( ) ) )
750+ . or_else ( || raw_or_normalized_codex_timestamp ( value. created_at_camel . as_ref ( ) ) )
751+ . or_else ( || {
752+ value
753+ . data
754+ . as_ref ( )
755+ . and_then ( raw_or_normalized_result_fields_timestamp)
756+ } )
757+ . or_else ( || {
758+ value
759+ . result
760+ . as_ref ( )
761+ . and_then ( raw_or_normalized_result_fields_timestamp)
762+ } )
763+ . or_else ( || {
764+ value
765+ . response
766+ . as_ref ( )
767+ . and_then ( raw_or_normalized_result_fields_timestamp)
768+ } )
769+ }
770+
707771fn codex_timestamp_from_result_value ( value : & Value ) -> Option < String > {
708772 normalize_value_fields_timestamp ( value)
709773 . or_else ( || value. get ( "data" ) . and_then ( normalize_value_fields_timestamp) )
@@ -719,18 +783,59 @@ fn codex_timestamp_from_result_value(value: &Value) -> Option<String> {
719783 } )
720784}
721785
786+ fn codex_model_timestamp_from_result_value ( value : & Value ) -> Option < String > {
787+ raw_or_normalized_value_fields_timestamp ( value)
788+ . or_else ( || {
789+ value
790+ . get ( "data" )
791+ . and_then ( raw_or_normalized_value_fields_timestamp)
792+ } )
793+ . or_else ( || {
794+ value
795+ . get ( "result" )
796+ . and_then ( raw_or_normalized_value_fields_timestamp)
797+ } )
798+ . or_else ( || {
799+ value
800+ . get ( "response" )
801+ . and_then ( raw_or_normalized_value_fields_timestamp)
802+ } )
803+ }
804+
722805fn normalize_result_fields_timestamp ( value : & CodexResultFields < ' _ > ) -> Option < String > {
723806 normalize_codex_timestamp ( value. timestamp . as_ref ( ) )
724807 . or_else ( || normalize_codex_timestamp ( value. created_at . as_ref ( ) ) )
725808 . or_else ( || normalize_codex_timestamp ( value. created_at_camel . as_ref ( ) ) )
726809}
727810
811+ fn raw_or_normalized_result_fields_timestamp ( value : & CodexResultFields < ' _ > ) -> Option < String > {
812+ raw_or_normalized_codex_timestamp ( value. timestamp . as_ref ( ) )
813+ . or_else ( || raw_or_normalized_codex_timestamp ( value. created_at . as_ref ( ) ) )
814+ . or_else ( || raw_or_normalized_codex_timestamp ( value. created_at_camel . as_ref ( ) ) )
815+ }
816+
728817fn normalize_value_fields_timestamp ( value : & Value ) -> Option < String > {
729818 normalize_value_timestamp ( value. get ( "timestamp" ) )
730819 . or_else ( || normalize_value_timestamp ( value. get ( "created_at" ) ) )
731820 . or_else ( || normalize_value_timestamp ( value. get ( "createdAt" ) ) )
732821}
733822
823+ fn raw_or_normalized_value_fields_timestamp ( value : & Value ) -> Option < String > {
824+ raw_or_normalized_value_timestamp ( value. get ( "timestamp" ) )
825+ . or_else ( || raw_or_normalized_value_timestamp ( value. get ( "created_at" ) ) )
826+ . or_else ( || raw_or_normalized_value_timestamp ( value. get ( "createdAt" ) ) )
827+ }
828+
829+ fn raw_or_normalized_codex_timestamp ( value : Option < & CodexTimestamp < ' _ > > ) -> Option < String > {
830+ match value? {
831+ CodexTimestamp :: String ( text) => {
832+ let text = text. trim ( ) ;
833+ ( !text. is_empty ( ) ) . then ( || text. to_string ( ) )
834+ }
835+ CodexTimestamp :: Number ( _) => normalize_codex_timestamp ( value) ,
836+ }
837+ }
838+
734839fn normalize_codex_timestamp ( value : Option < & CodexTimestamp < ' _ > > ) -> Option < String > {
735840 match value? {
736841 CodexTimestamp :: String ( text) => {
@@ -753,6 +858,18 @@ fn normalize_codex_timestamp(value: Option<&CodexTimestamp<'_>>) -> Option<Strin
753858 }
754859}
755860
861+ fn raw_or_normalized_value_timestamp ( value : Option < & Value > ) -> Option < String > {
862+ let value = value?;
863+ if let Some ( text) = value. as_str ( ) {
864+ let text = text. trim ( ) ;
865+ if text. is_empty ( ) {
866+ return None ;
867+ }
868+ return Some ( text. to_string ( ) ) ;
869+ }
870+ normalize_value_timestamp ( Some ( value) )
871+ }
872+
756873fn normalize_value_timestamp ( value : Option < & Value > ) -> Option < String > {
757874 let value = value?;
758875 if let Some ( text) = value. as_str ( ) {
0 commit comments