@@ -306,24 +306,42 @@ fn add_deduped_event_to_groups(
306306 shared : & SharedArgs ,
307307 groups : & mut BTreeMap < String , CodexGroup > ,
308308) -> Result < ( ) > {
309+ let Some ( period) = codex_period_for (
310+ timestamp,
311+ Some ( event. session_id . as_str ( ) ) ,
312+ kind,
313+ timezone,
314+ shared,
315+ ) else {
316+ return Ok ( ( ) ) ;
317+ } ;
318+ let group = groups. entry ( period) . or_default ( ) ;
319+ accumulate_codex_event_into_group ( group, event, model, false ) ;
320+ Ok ( ( ) )
321+ }
322+
323+ fn codex_period_for (
324+ timestamp : crate :: TimestampMs ,
325+ session_id : Option < & str > ,
326+ kind : AgentReportKind ,
327+ timezone : Option < & JiffTimeZone > ,
328+ shared : & SharedArgs ,
329+ ) -> Option < String > {
309330 let date = format_date_tz ( timestamp, timezone) ;
310331 if shared. since . is_some ( ) || shared. until . is_some ( ) {
311332 let date_key = date. replace ( '-' , "" ) ;
312333 if shared. since . as_ref ( ) . is_some_and ( |since| & date_key < since)
313334 || shared. until . as_ref ( ) . is_some_and ( |until| & date_key > until)
314335 {
315- return Ok ( ( ) ) ;
336+ return None ;
316337 }
317338 }
318- let period = match kind {
339+ Some ( match kind {
319340 AgentReportKind :: Daily => date,
320341 AgentReportKind :: Weekly => week_start ( & date, WeekDay :: Monday ) . unwrap_or ( date) ,
321342 AgentReportKind :: Monthly => date[ ..7 ] . to_string ( ) ,
322- AgentReportKind :: Session => event. session_id . clone ( ) ,
323- } ;
324- let group = groups. entry ( period) . or_default ( ) ;
325- accumulate_codex_event_into_group ( group, event, model, false ) ;
326- Ok ( ( ) )
343+ AgentReportKind :: Session => session_id?. to_string ( ) ,
344+ } )
327345}
328346
329347fn accumulate_codex_event_into_group (
@@ -422,25 +440,14 @@ fn apply_recorded_usage_entries<'a>(
422440 let Some ( service_tier) = record. service_tier else {
423441 continue ;
424442 } ;
425- let date = format_date_tz ( key. timestamp , timezone. as_ref ( ) ) ;
426- if shared. since . is_some ( ) || shared. until . is_some ( ) {
427- let date_key = date. replace ( '-' , "" ) ;
428- if shared. since . as_ref ( ) . is_some_and ( |since| & date_key < since)
429- || shared. until . as_ref ( ) . is_some_and ( |until| & date_key > until)
430- {
431- continue ;
432- }
433- }
434- let period = match kind {
435- AgentReportKind :: Daily => date,
436- AgentReportKind :: Weekly => week_start ( & date, WeekDay :: Monday ) . unwrap_or ( date) ,
437- AgentReportKind :: Monthly => date[ ..7 ] . to_string ( ) ,
438- AgentReportKind :: Session => {
439- let Some ( session_id) = record. session_id . as_ref ( ) else {
440- continue ;
441- } ;
442- session_id. to_string ( )
443- }
443+ let Some ( period) = codex_period_for (
444+ key. timestamp ,
445+ record. session_id . as_deref ( ) ,
446+ kind,
447+ timezone. as_ref ( ) ,
448+ shared,
449+ ) else {
450+ continue ;
444451 } ;
445452 let Some ( model_usage) = groups
446453 . get_mut ( & period)
@@ -660,6 +667,72 @@ mod tests {
660667 model_aliases:: set_model_aliases_for_tests,
661668 } ;
662669
670+ #[ test]
671+ fn selects_codex_period_for_each_report_kind ( ) {
672+ let timestamp = parse_ts_timestamp ( "2026-05-29T08:01:00.000Z" ) . unwrap ( ) ;
673+ let timezone = parse_tz ( Some ( "UTC" ) ) . unwrap ( ) ;
674+ let shared = SharedArgs :: default ( ) ;
675+
676+ for ( kind, expected) in [
677+ ( AgentReportKind :: Daily , "2026-05-29" ) ,
678+ ( AgentReportKind :: Weekly , "2026-05-25" ) ,
679+ ( AgentReportKind :: Monthly , "2026-05" ) ,
680+ ( AgentReportKind :: Session , "sessions/child.jsonl" ) ,
681+ ] {
682+ assert_eq ! (
683+ codex_period_for(
684+ timestamp,
685+ Some ( "sessions/child.jsonl" ) ,
686+ kind,
687+ Some ( & timezone) ,
688+ & shared,
689+ )
690+ . as_deref( ) ,
691+ Some ( expected) ,
692+ ) ;
693+ }
694+ }
695+
696+ #[ test]
697+ fn omits_codex_period_outside_date_bounds ( ) {
698+ let timezone = parse_tz ( Some ( "UTC" ) ) . unwrap ( ) ;
699+ let shared = SharedArgs {
700+ since : Some ( "20260528" . to_string ( ) ) ,
701+ until : Some ( "20260530" . to_string ( ) ) ,
702+ ..SharedArgs :: default ( )
703+ } ;
704+
705+ for timestamp in [ "2026-05-27T23:59:59.000Z" , "2026-05-31T00:00:00.000Z" ] {
706+ assert_eq ! (
707+ codex_period_for(
708+ parse_ts_timestamp( timestamp) . unwrap( ) ,
709+ Some ( "sessions/child.jsonl" ) ,
710+ AgentReportKind :: Daily ,
711+ Some ( & timezone) ,
712+ & shared,
713+ ) ,
714+ None ,
715+ ) ;
716+ }
717+ }
718+
719+ #[ test]
720+ fn omits_session_period_without_session_id ( ) {
721+ let timestamp = parse_ts_timestamp ( "2026-05-29T08:01:00.000Z" ) . unwrap ( ) ;
722+ let timezone = parse_tz ( Some ( "UTC" ) ) . unwrap ( ) ;
723+
724+ assert_eq ! (
725+ codex_period_for(
726+ timestamp,
727+ None ,
728+ AgentReportKind :: Session ,
729+ Some ( & timezone) ,
730+ & SharedArgs :: default ( ) ,
731+ ) ,
732+ None ,
733+ ) ;
734+ }
735+
663736 #[ test]
664737 fn dedupes_copied_token_usage_across_session_files ( ) {
665738 let usage_line = json ! ( {
0 commit comments