@@ -91,10 +91,13 @@ pub(super) struct SinceFiles {
9191///
9292/// Claude session JSONL files are append-only, so a file last written before
9393/// the window opened cannot hold an entry inside it. Files are kept or dropped
94- /// per session, the main transcript together with its subagent transcripts,
95- /// because deduplication matches sidechain replays against their parent
96- /// messages within a session; dropping the parent file alone would let a
97- /// replay count twice. Only the file list is narrowed; entries parsed from a
94+ /// per session, the main transcript together with its subagent transcripts
95+ /// and any copy in another config root, because deduplication matches
96+ /// sidechain replays against their parent messages within a session; dropping
97+ /// the parent file alone would let a replay count twice. Copies of a message
98+ /// in other sessions, such as resumed transcripts, keep the original
99+ /// timestamp, so they fall on the same side of the window as the original.
100+ /// Only the file list is narrowed; entries parsed from a
98101/// surviving file are untouched. Every file is kept when `since` is unset or
99102/// unparsable, when the window opens ahead of the current clock, or when a
100103/// file's mtime cannot be read.
@@ -128,32 +131,56 @@ pub(super) fn split_files_before_since(
128131 . iter ( )
129132 . filter ( |file| file_modified_millis ( file) . is_none_or ( |modified| modified >= threshold) )
130133 . map ( |file| session_group_key ( file) )
131- . collect :: < FxHashSet < _ > > ( ) ;
134+ . collect :: < FxHashSet < String > > ( ) ;
132135 let ( kept, pruned) = files
133136 . into_iter ( )
134137 . partition ( |file| live_sessions. contains ( & session_group_key ( file) ) ) ;
135138 SinceFiles { kept, pruned }
136139}
137140
138- /// Identifies the session a usage file belongs to, so `<session>.jsonl` and
139- /// everything under `<session>/` (such as `subagents/`) share one key.
140- /// Paths outside the `projects/<project>/<session>` layout key on themselves.
141- fn session_group_key ( path : & Path ) -> PathBuf {
142- let components = path. components ( ) . collect :: < Vec < _ > > ( ) ;
143- let Some ( projects_index) = components
141+ /// Identifies the session a usage file belongs to, relative to `projects/`
142+ /// so a session copied into another config root shares one key.
143+ ///
144+ /// `<session>.jsonl` and everything under `<session>/` (such as `subagents/`)
145+ /// share a key. Legacy flat `agent-*.jsonl` sidechain transcripts sit beside
146+ /// their parent, so they key on the `sessionId` recorded inside them. Paths
147+ /// outside the `projects/<project>/<session>` layout key on themselves.
148+ fn session_group_key ( path : & Path ) -> String {
149+ let components = path
150+ . components ( )
151+ . map ( |component| component. as_os_str ( ) . to_string_lossy ( ) )
152+ . collect :: < Vec < _ > > ( ) ;
153+ let relative = components
144154 . iter ( )
145- . position ( |component| component. as_os_str ( ) == "projects" )
146- else {
147- return path. to_path_buf ( ) ;
155+ . position ( |component| component == "projects" )
156+ . map ( |index| & components[ index + 1 ..] ) ;
157+ let Some ( [ project, session, rest @ ..] ) = relative else {
158+ return path. to_string_lossy ( ) . into_owned ( ) ;
148159 } ;
149- // `projects/<project>/<session>.jsonl` or `projects/<project>/<session>/...`
150- let Some ( session) = components. get ( projects_index + 2 ) else {
151- return path. to_path_buf ( ) ;
152- } ;
153- let mut key = components[ ..projects_index + 2 ] . iter ( ) . collect :: < PathBuf > ( ) ;
154- let session = session. as_os_str ( ) . to_string_lossy ( ) ;
155- key. push ( session. strip_suffix ( ".jsonl" ) . unwrap_or ( & session) ) ;
156- key
160+ let session = session. strip_suffix ( ".jsonl" ) . unwrap_or ( session) ;
161+ if rest. is_empty ( )
162+ && session. starts_with ( "agent-" )
163+ && let Some ( parent_session) = recorded_session_id ( path)
164+ {
165+ return format ! ( "{project}/{parent_session}" ) ;
166+ }
167+ format ! ( "{project}/{session}" )
168+ }
169+
170+ /// Reads the first `sessionId` recorded in a transcript without parsing it.
171+ fn recorded_session_id ( path : & Path ) -> Option < String > {
172+ use std:: io:: Read as _;
173+
174+ const MARKER : & [ u8 ] = br#""sessionId":""# ;
175+ let mut head = Vec :: new ( ) ;
176+ fs:: File :: open ( path)
177+ . ok ( ) ?
178+ . take ( 64 * 1024 )
179+ . read_to_end ( & mut head)
180+ . ok ( ) ?;
181+ let start = memmem:: find ( & head, MARKER ) ? + MARKER . len ( ) ;
182+ let end = memchr:: memchr ( b'"' , & head[ start..] ) ? + start;
183+ String :: from_utf8 ( head[ start..end] . to_vec ( ) ) . ok ( )
157184}
158185
159186fn file_modified_millis ( path : & Path ) -> Option < i64 > {
@@ -403,4 +430,37 @@ mod tests {
403430 assert_eq ! ( split. kept, [ live_parent, live_agent] ) ;
404431 assert_eq ! ( split. pruned, [ stale_parent, stale_agent] ) ;
405432 }
433+
434+ #[ test]
435+ fn keeps_legacy_flat_agent_transcripts_and_config_root_copies_with_their_session ( ) {
436+ let fixture = fs_fixture ! ( {
437+ "a/projects/p/live.jsonl" : "{}" ,
438+ "a/projects/p/agent-1234.jsonl" : r#"{"sessionId":"live","timestamp":"2026-09-01T12:00:00Z"}"# ,
439+ "a/projects/p/agent-5678.jsonl" : r#"{"sessionId":"stale","timestamp":"2026-09-01T12:00:00Z"}"# ,
440+ "b/projects/p/live.jsonl" : "{}" ,
441+ } ) ;
442+ let root = fixture. root ( ) ;
443+ let live = root. join ( "a/projects/p/live.jsonl" ) ;
444+ let live_agent = root. join ( "a/projects/p/agent-1234.jsonl" ) ;
445+ let stale_agent = root. join ( "a/projects/p/agent-5678.jsonl" ) ;
446+ let other_root_copy = root. join ( "b/projects/p/live.jsonl" ) ;
447+ set_file_modified ( & live, ts ( "2026-09-21T12:00:00Z" ) ) ;
448+ for stale in [ & live_agent, & stale_agent, & other_root_copy] {
449+ set_file_modified ( stale, ts ( "2026-09-01T12:00:00Z" ) ) ;
450+ }
451+
452+ let split = split_files_before_since (
453+ vec ! [
454+ live_agent. clone( ) ,
455+ stale_agent. clone( ) ,
456+ live. clone( ) ,
457+ other_root_copy. clone( ) ,
458+ ] ,
459+ & shared_since ( Some ( "20260920" ) ) ,
460+ ts ( "2026-09-24T00:00:00Z" ) ,
461+ ) ;
462+
463+ assert_eq ! ( split. kept, [ live_agent, live, other_root_copy] ) ;
464+ assert_eq ! ( split. pruned, [ stale_agent] ) ;
465+ }
406466}
0 commit comments