@@ -11,8 +11,9 @@ use jiff::tz::TimeZone as JiffTimeZone;
1111use rustc_hash:: FxHasher ;
1212
1313use crate :: {
14- CodexGroup , CodexServiceTier , CodexTokenUsageEvent , CodexUsageBucket , Result ,
14+ CodexGroup , CodexServiceTier , CodexTokenUsageEvent , CodexUsageBucket , MILLIS_PER_DAY , Result ,
1515 cli:: { AgentReportKind , SharedArgs , WeekDay } ,
16+ date_range_bounds_ms,
1617 fast:: FxHashMap ,
1718 format_date_tz, merge_codex_service_tiers, parse_ts_timestamp, parse_tz, wants_json,
1819 week_start,
@@ -73,7 +74,10 @@ fn load_groups_from_sources(
7374 shared : & SharedArgs ,
7475 kind : AgentReportKind ,
7576) -> Result < BTreeMap < String , CodexGroup > > {
76- let file_groups = paths:: collect_deduped_codex_usage_files ( sources) ;
77+ let mut file_groups = paths:: collect_deduped_codex_usage_files ( sources) ;
78+ for group in & mut file_groups {
79+ retain_report_files ( & mut group. files , shared) ;
80+ }
7781 let replay_plan = CodexReplayPlan :: new (
7882 file_groups
7983 . iter ( )
@@ -106,7 +110,8 @@ pub(super) fn load_groups_from_directory(
106110 shared : & SharedArgs ,
107111 kind : AgentReportKind ,
108112) -> Result < BTreeMap < String , CodexGroup > > {
109- let files = paths:: collect_codex_usage_files ( sessions_dir) ;
113+ let mut files = paths:: collect_codex_usage_files ( sessions_dir) ;
114+ retain_report_files ( & mut files, shared) ;
110115 let replay_plan =
111116 CodexReplayPlan :: new ( [ ( sessions_dir, files. as_slice ( ) ) ] , shared. single_thread ) ;
112117 let run = CodexAggregateRun {
@@ -125,6 +130,31 @@ pub(super) fn load_groups_from_directory(
125130 Ok ( groups)
126131}
127132
133+ fn retain_report_files ( files : & mut Vec < PathBuf > , shared : & SharedArgs ) {
134+ let timezone = parse_tz ( shared. timezone . as_deref ( ) ) . or_else ( || Some ( JiffTimeZone :: system ( ) ) ) ;
135+ let ( Some ( start) , _) = date_range_bounds_ms ( shared. since . as_deref ( ) , None , timezone. as_ref ( ) )
136+ else {
137+ return ;
138+ } ;
139+ let now = std:: time:: SystemTime :: now ( )
140+ . duration_since ( std:: time:: UNIX_EPOCH )
141+ . ok ( )
142+ . and_then ( |duration| i64:: try_from ( duration. as_millis ( ) ) . ok ( ) ) ;
143+ if now. is_none_or ( |now| start > now) {
144+ return ;
145+ }
146+ // ponytail: mtime is a one-day-widened proxy; inspect file tails if preserved mtimes matter.
147+ let cutoff = start. saturating_sub ( MILLIS_PER_DAY ) ;
148+ files. retain ( |file| {
149+ file. metadata ( )
150+ . and_then ( |metadata| metadata. modified ( ) )
151+ . ok ( )
152+ . and_then ( |modified| modified. duration_since ( std:: time:: UNIX_EPOCH ) . ok ( ) )
153+ . and_then ( |duration| i64:: try_from ( duration. as_millis ( ) ) . ok ( ) )
154+ . is_none_or ( |modified| modified >= cutoff)
155+ } ) ;
156+ }
157+
128158fn aggregate_files_with_dedupe (
129159 run : & CodexAggregateRun < ' _ > ,
130160 seen : & CodexDedupeShards ,
@@ -711,6 +741,41 @@ mod tests {
711741 }
712742 }
713743
744+ #[ test]
745+ fn skips_codex_files_older_than_since_window ( ) {
746+ let fixture = fs_fixture ! ( {
747+ "old.jsonl" : "" ,
748+ "recent.jsonl" : "" ,
749+ } ) ;
750+ let old = fixture. path ( "old.jsonl" ) ;
751+ let recent = fixture. path ( "recent.jsonl" ) ;
752+ for ( file, timestamp) in [
753+ ( & old, "2026-05-20T00:00:00Z" ) ,
754+ ( & recent, "2026-05-28T12:00:00Z" ) ,
755+ ] {
756+ let modified = std:: time:: UNIX_EPOCH
757+ + std:: time:: Duration :: from_millis (
758+ parse_ts_timestamp ( timestamp) . unwrap ( ) . as_millis ( ) as u64 ,
759+ ) ;
760+ std:: fs:: File :: options ( )
761+ . write ( true )
762+ . open ( file)
763+ . unwrap ( )
764+ . set_times ( std:: fs:: FileTimes :: new ( ) . set_modified ( modified) )
765+ . unwrap ( ) ;
766+ }
767+ let mut files = vec ! [ old, recent. clone( ) ] ;
768+ let shared = SharedArgs {
769+ since : Some ( "20260529" . to_string ( ) ) ,
770+ timezone : Some ( "UTC" . to_string ( ) ) ,
771+ ..SharedArgs :: default ( )
772+ } ;
773+
774+ retain_report_files ( & mut files, & shared) ;
775+
776+ assert_eq ! ( files, vec![ recent] ) ;
777+ }
778+
714779 #[ test]
715780 fn omits_session_period_without_session_id ( ) {
716781 let timestamp = parse_ts_timestamp ( "2026-05-29T08:01:00.000Z" ) . unwrap ( ) ;
0 commit comments