@@ -129,6 +129,7 @@ const TranscriptRecord = Schema.Struct({
129129 message : Schema . optional ( Schema . String ) ,
130130 model : Schema . optional ( Schema . String ) ,
131131 cwd : Schema . optional ( Schema . String ) ,
132+ history_base : Schema . optional ( Schema . Unknown ) ,
132133 content : Schema . optional ( Schema . Array ( TranscriptContentBlock ) ) ,
133134 internal_chat_message_metadata_passthrough : Schema . optional ( Schema . Unknown ) ,
134135 } ) ,
@@ -153,6 +154,12 @@ const decodePiEntryLinks = Schema.decodeUnknownOption(
153154) ;
154155const selectTranscriptPath = createTranscriptJsonSelector ( TranscriptRecord ) ;
155156const decodeCodexTurnMetadata = Schema . decodeUnknownOption ( CodexTurnMetadata ) ;
157+ const decodeCodexHistoryBase = Schema . decodeUnknownOption (
158+ Schema . Struct ( { thread_id : Schema . String , end_byte_offset : Schema . Number } ) ,
159+ ) ;
160+ // Original rollouts end in their thread ID, rewound ones in `<thread ID>_<rollout ID>`.
161+ const CODEX_ROLLOUT_ID_PATTERN =
162+ / [ - _ ] ( [ 0 - 9 a - f ] { 8 } - [ 0 - 9 a - f ] { 4 } - [ 0 - 9 a - f ] { 4 } - [ 0 - 9 a - f ] { 4 } - [ 0 - 9 a - f ] { 12 } ) \. j s o n l $ / i;
156163const decodeCodexSessionIndexEntry = Schema . decodeUnknownOption (
157164 Schema . fromJsonString ( CodexSessionIndexEntry ) ,
158165) ;
@@ -344,6 +351,21 @@ function parseCodexThreadNames(contents: string): ReadonlyMap<string, string> {
344351 return names ;
345352}
346353
354+ // A rewound paginated Codex thread continues in a new rollout that inherits a
355+ // byte prefix of the rollout it replaced.
356+ function codexHistoryBase ( records : ReadonlyArray < DecodedTranscriptRecord > ) {
357+ const base = Option . getOrUndefined (
358+ decodeCodexHistoryBase (
359+ records . find ( ( record ) => record . type === "session_meta" ) ?. payload ?. history_base ,
360+ ) ,
361+ ) ;
362+ return base !== undefined &&
363+ Number . isSafeInteger ( base . end_byte_offset ) &&
364+ base . end_byte_offset >= 0
365+ ? base
366+ : undefined ;
367+ }
368+
347369function deriveTitle ( source : AgentSessionSource , text : string ) : string | undefined {
348370 const request = source === "codex" ? text . replace ( CODEX_TITLE_PREAMBLE , "" ) : text ;
349371 return request . trim ( ) . split ( "\n" ) [ 0 ] ?. slice ( 0 , 100 ) . trim ( ) || undefined ;
@@ -1006,8 +1028,10 @@ export const make = Effect.gen(function* () {
10061028 filePath : string ,
10071029 expected : ReturnType < typeof transcriptIdentity > ,
10081030 source : AgentSessionSource ,
1031+ prefix ?: { readonly endByteOffset : number ; readonly historyBytes : number } ,
10091032 ) {
1010- if ( expected . size > MAX_IMPORTED_TRANSCRIPT_BYTES ) return null ;
1033+ const readBytes = prefix ?. endByteOffset ?? expected . size ;
1034+ if ( readBytes > expected . size || readBytes > MAX_IMPORTED_TRANSCRIPT_BYTES ) return null ;
10111035
10121036 return yield * Effect . scoped (
10131037 fileSystem . open ( filePath , { flag : "r" } ) . pipe (
@@ -1019,7 +1043,7 @@ export const make = Effect.gen(function* () {
10191043 const records : Array < DecodedTranscriptRecord > = [ ] ;
10201044 let cwd = Option . none < string > ( ) ;
10211045 let claudeFields = emptyClaudeFields ;
1022- let historyBytes = 0 ;
1046+ let historyBytes = prefix ?. historyBytes ?? 0 ;
10231047 let recordBytes = 0 ;
10241048 let recordCount = 0 ;
10251049 let bytesRead = 0 ;
@@ -1071,9 +1095,9 @@ export const make = Effect.gen(function* () {
10711095 return true ;
10721096 } ;
10731097
1074- while ( bytesRead < expected . size ) {
1098+ while ( bytesRead < readBytes ) {
10751099 const next = yield * file . readAlloc (
1076- Math . min ( TRANSCRIPT_PREFIX_BYTES , expected . size - bytesRead ) ,
1100+ Math . min ( TRANSCRIPT_PREFIX_BYTES , readBytes - bytesRead ) ,
10771101 ) ;
10781102 if ( Option . isNone ( next ) ) {
10791103 return null ;
@@ -1108,7 +1132,7 @@ export const make = Effect.gen(function* () {
11081132 )
11091133 return null ;
11101134 return sameTranscriptIdentity ( expected , transcriptIdentity ( filePath , yield * file . stat ) )
1111- ? { records, cwd, claudeFields }
1135+ ? { records, cwd, claudeFields, historyBytes }
11121136 : null ;
11131137 } ) ,
11141138 ) ,
@@ -1686,6 +1710,56 @@ export const make = Effect.gen(function* () {
16861710 const codexThreadNamesByHome = new Map < string , ReadonlyMap < string , string > > ( ) ;
16871711 let bytesRemaining = MAX_IMPORT_BYTES ;
16881712 let transcriptsRemaining = MAX_IMPORT_TRANSCRIPTS ;
1713+ const codexRolloutPaths = new Map < ProviderInstanceId , ReadonlyMap < string , string > > ( ) ;
1714+ const codexRolloutPath = ( providerInstanceId : ProviderInstanceId , rolloutId : string ) => {
1715+ let paths = codexRolloutPaths . get ( providerInstanceId ) ;
1716+ if ( paths === undefined ) {
1717+ paths = new Map (
1718+ candidates . flatMap ( ( candidate ) =>
1719+ candidate . source === "codex" && candidate . providerInstanceId === providerInstanceId
1720+ ? candidate . transcripts . flatMap ( ( transcript ) => {
1721+ const id = CODEX_ROLLOUT_ID_PATTERN . exec ( transcript . filePath ) ?. [ 1 ] ;
1722+ return id === undefined ? [ ] : [ [ id , transcript . filePath ] as const ] ;
1723+ } )
1724+ : [ ] ,
1725+ ) ,
1726+ ) ;
1727+ codexRolloutPaths . set ( providerInstanceId , paths ) ;
1728+ }
1729+ return paths . get ( rolloutId ) ;
1730+ } ;
1731+ // Reads the prefixes a rewound rollout inherits, oldest first. Without all of
1732+ // them the rollout is imported on its own, as before.
1733+ const readCodexHistoryBase = Effect . fn ( "AgentSessionScanner.readCodexHistoryBase" ) ( function * (
1734+ providerInstanceId : ProviderInstanceId ,
1735+ records : ReadonlyArray < DecodedTranscriptRecord > ,
1736+ historyBytes : number ,
1737+ ) {
1738+ const prefixes : Array < ReadonlyArray < DecodedTranscriptRecord > > = [ ] ;
1739+ const visited = new Set < string > ( ) ;
1740+ for ( let base = codexHistoryBase ( records ) ; base !== undefined ; ) {
1741+ const filePath = visited . has ( base . thread_id )
1742+ ? undefined
1743+ : codexRolloutPath ( providerInstanceId , base . thread_id ) ;
1744+ if ( filePath === undefined || base . end_byte_offset > bytesRemaining ) return null ;
1745+ visited . add ( base . thread_id ) ;
1746+ const stats = yield * statOption ( filePath ) ;
1747+ if ( Option . isNone ( stats ) || stats . value . type !== "File" ) return null ;
1748+ bytesRemaining -= base . end_byte_offset ;
1749+ const inherited = yield * readTranscript (
1750+ filePath ,
1751+ transcriptIdentity ( filePath , stats . value ) ,
1752+ "codex" ,
1753+ { endByteOffset : base . end_byte_offset , historyBytes } ,
1754+ ) ;
1755+ if ( inherited === null ) return null ;
1756+ historyBytes = inherited . historyBytes ;
1757+ // The rollout's own session_meta names the thread; inherited ones may name a fork parent.
1758+ prefixes . unshift ( inherited . records . filter ( ( record ) => record . type !== "session_meta" ) ) ;
1759+ base = codexHistoryBase ( inherited . records ) ;
1760+ }
1761+ return prefixes . flat ( ) ;
1762+ } ) ;
16891763 return Stream . fromIteratorSucceed ( eligibleTranscripts . values ( ) , 1 ) . pipe (
16901764 Stream . mapEffect ( ( { candidate, transcript } ) =>
16911765 Effect . gen ( function * ( ) {
@@ -1738,14 +1812,24 @@ export const make = Effect.gen(function* () {
17381812 return Option . some < AgentSessionRecentThread > ( { _tag : "Skipped" } ) ;
17391813 }
17401814
1815+ const inheritedRecords =
1816+ candidate . source === "codex"
1817+ ? yield * readCodexHistoryBase (
1818+ candidate . providerInstanceId ,
1819+ snapshot . records ,
1820+ snapshot . historyBytes ,
1821+ )
1822+ : null ;
17411823 const parsedThread = parseAgentSessionRecords (
17421824 {
17431825 source : candidate . source ,
17441826 providerInstanceId : candidate . providerInstanceId ,
17451827 fallbackSessionId : path . basename ( transcript . filePath , ".jsonl" ) ,
17461828 lastActiveAtMs : transcript . mtimeMs ,
17471829 } ,
1748- snapshot . records ,
1830+ inheritedRecords === null
1831+ ? snapshot . records
1832+ : [ ...inheritedRecords , ...snapshot . records ] ,
17491833 snapshot . claudeFields ,
17501834 ) ;
17511835 if ( parsedThread === null ) {
0 commit comments