@@ -120,6 +120,7 @@ const TranscriptRecord = Schema.Struct({
120120 message : Schema . optional ( Schema . String ) ,
121121 model : Schema . optional ( Schema . String ) ,
122122 cwd : Schema . optional ( Schema . String ) ,
123+ history_base : Schema . optional ( Schema . Unknown ) ,
123124 content : Schema . optional ( Schema . Array ( TranscriptContentBlock ) ) ,
124125 internal_chat_message_metadata_passthrough : Schema . optional ( Schema . Unknown ) ,
125126 } ) ,
@@ -144,6 +145,12 @@ const decodePiEntryLinks = Schema.decodeUnknownOption(
144145) ;
145146const selectTranscriptPath = createTranscriptJsonSelector ( TranscriptRecord ) ;
146147const decodeCodexTurnMetadata = Schema . decodeUnknownOption ( CodexTurnMetadata ) ;
148+ const decodeCodexHistoryBase = Schema . decodeUnknownOption (
149+ Schema . Struct ( { thread_id : Schema . String , end_byte_offset : Schema . Number } ) ,
150+ ) ;
151+ // Original rollouts end in their thread ID, rewound ones in `<thread ID>_<rollout ID>`.
152+ const CODEX_ROLLOUT_ID_PATTERN =
153+ / [ - _ ] ( [ 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;
147154const decodeCodexSessionIndexEntry = Schema . decodeUnknownOption (
148155 Schema . fromJsonString ( CodexSessionIndexEntry ) ,
149156) ;
@@ -335,6 +342,21 @@ function parseCodexThreadNames(contents: string): ReadonlyMap<string, string> {
335342 return names ;
336343}
337344
345+ // A rewound paginated Codex thread continues in a new rollout that inherits a
346+ // byte prefix of the rollout it replaced.
347+ function codexHistoryBase ( records : ReadonlyArray < DecodedTranscriptRecord > ) {
348+ const base = Option . getOrUndefined (
349+ decodeCodexHistoryBase (
350+ records . find ( ( record ) => record . type === "session_meta" ) ?. payload ?. history_base ,
351+ ) ,
352+ ) ;
353+ return base !== undefined &&
354+ Number . isSafeInteger ( base . end_byte_offset ) &&
355+ base . end_byte_offset >= 0
356+ ? base
357+ : undefined ;
358+ }
359+
338360function deriveTitle ( source : AgentSessionSource , text : string ) : string | undefined {
339361 const request = source === "codex" ? text . replace ( CODEX_TITLE_PREAMBLE , "" ) : text ;
340362 return request . trim ( ) . split ( "\n" ) [ 0 ] ?. slice ( 0 , 100 ) . trim ( ) || undefined ;
@@ -997,8 +1019,10 @@ export const make = Effect.gen(function* () {
9971019 filePath : string ,
9981020 expected : ReturnType < typeof transcriptIdentity > ,
9991021 source : AgentSessionSource ,
1022+ prefix ?: { readonly endByteOffset : number ; readonly historyBytes : number } ,
10001023 ) {
1001- if ( expected . size > MAX_IMPORTED_TRANSCRIPT_BYTES ) return null ;
1024+ const readBytes = prefix ?. endByteOffset ?? expected . size ;
1025+ if ( readBytes > expected . size || readBytes > MAX_IMPORTED_TRANSCRIPT_BYTES ) return null ;
10021026
10031027 return yield * Effect . scoped (
10041028 fileSystem . open ( filePath , { flag : "r" } ) . pipe (
@@ -1010,7 +1034,7 @@ export const make = Effect.gen(function* () {
10101034 const records : Array < DecodedTranscriptRecord > = [ ] ;
10111035 let cwd = Option . none < string > ( ) ;
10121036 let claudeFields = emptyClaudeFields ;
1013- let historyBytes = 0 ;
1037+ let historyBytes = prefix ?. historyBytes ?? 0 ;
10141038 let recordBytes = 0 ;
10151039 let recordCount = 0 ;
10161040 let bytesRead = 0 ;
@@ -1062,9 +1086,9 @@ export const make = Effect.gen(function* () {
10621086 return true ;
10631087 } ;
10641088
1065- while ( bytesRead < expected . size ) {
1089+ while ( bytesRead < readBytes ) {
10661090 const next = yield * file . readAlloc (
1067- Math . min ( TRANSCRIPT_PREFIX_BYTES , expected . size - bytesRead ) ,
1091+ Math . min ( TRANSCRIPT_PREFIX_BYTES , readBytes - bytesRead ) ,
10681092 ) ;
10691093 if ( Option . isNone ( next ) ) {
10701094 return null ;
@@ -1089,7 +1113,7 @@ export const make = Effect.gen(function* () {
10891113
10901114 if ( recordStarted && ! ( yield * Effect . try ( finishRecord ) ) ) return null ;
10911115 return sameTranscriptIdentity ( expected , transcriptIdentity ( filePath , yield * file . stat ) )
1092- ? { records, cwd, claudeFields }
1116+ ? { records, cwd, claudeFields, historyBytes }
10931117 : null ;
10941118 } ) ,
10951119 ) ,
@@ -1667,6 +1691,56 @@ export const make = Effect.gen(function* () {
16671691 const codexThreadNamesByHome = new Map < string , ReadonlyMap < string , string > > ( ) ;
16681692 let bytesRemaining = MAX_IMPORT_BYTES ;
16691693 let transcriptsRemaining = MAX_IMPORT_TRANSCRIPTS ;
1694+ const codexRolloutPaths = new Map < ProviderInstanceId , ReadonlyMap < string , string > > ( ) ;
1695+ const codexRolloutPath = ( providerInstanceId : ProviderInstanceId , rolloutId : string ) => {
1696+ let paths = codexRolloutPaths . get ( providerInstanceId ) ;
1697+ if ( paths === undefined ) {
1698+ paths = new Map (
1699+ candidates . flatMap ( ( candidate ) =>
1700+ candidate . source === "codex" && candidate . providerInstanceId === providerInstanceId
1701+ ? candidate . transcripts . flatMap ( ( transcript ) => {
1702+ const id = CODEX_ROLLOUT_ID_PATTERN . exec ( transcript . filePath ) ?. [ 1 ] ;
1703+ return id === undefined ? [ ] : [ [ id , transcript . filePath ] as const ] ;
1704+ } )
1705+ : [ ] ,
1706+ ) ,
1707+ ) ;
1708+ codexRolloutPaths . set ( providerInstanceId , paths ) ;
1709+ }
1710+ return paths . get ( rolloutId ) ;
1711+ } ;
1712+ // Reads the prefixes a rewound rollout inherits, oldest first. Without all of
1713+ // them the rollout is imported on its own, as before.
1714+ const readCodexHistoryBase = Effect . fn ( "AgentSessionScanner.readCodexHistoryBase" ) ( function * (
1715+ providerInstanceId : ProviderInstanceId ,
1716+ records : ReadonlyArray < DecodedTranscriptRecord > ,
1717+ historyBytes : number ,
1718+ ) {
1719+ const prefixes : Array < ReadonlyArray < DecodedTranscriptRecord > > = [ ] ;
1720+ const visited = new Set < string > ( ) ;
1721+ for ( let base = codexHistoryBase ( records ) ; base !== undefined ; ) {
1722+ const filePath = visited . has ( base . thread_id )
1723+ ? undefined
1724+ : codexRolloutPath ( providerInstanceId , base . thread_id ) ;
1725+ if ( filePath === undefined || base . end_byte_offset > bytesRemaining ) return null ;
1726+ visited . add ( base . thread_id ) ;
1727+ const stats = yield * statOption ( filePath ) ;
1728+ if ( Option . isNone ( stats ) || stats . value . type !== "File" ) return null ;
1729+ bytesRemaining -= base . end_byte_offset ;
1730+ const inherited = yield * readTranscript (
1731+ filePath ,
1732+ transcriptIdentity ( filePath , stats . value ) ,
1733+ "codex" ,
1734+ { endByteOffset : base . end_byte_offset , historyBytes } ,
1735+ ) ;
1736+ if ( inherited === null ) return null ;
1737+ historyBytes = inherited . historyBytes ;
1738+ // The rollout's own session_meta names the thread; inherited ones may name a fork parent.
1739+ prefixes . unshift ( inherited . records . filter ( ( record ) => record . type !== "session_meta" ) ) ;
1740+ base = codexHistoryBase ( inherited . records ) ;
1741+ }
1742+ return prefixes . flat ( ) ;
1743+ } ) ;
16701744 return Stream . fromIteratorSucceed ( eligibleTranscripts . values ( ) , 1 ) . pipe (
16711745 Stream . mapEffect ( ( { candidate, transcript } ) =>
16721746 Effect . gen ( function * ( ) {
@@ -1719,14 +1793,24 @@ export const make = Effect.gen(function* () {
17191793 return Option . some < AgentSessionRecentThread > ( { _tag : "Skipped" } ) ;
17201794 }
17211795
1796+ const inheritedRecords =
1797+ candidate . source === "codex"
1798+ ? yield * readCodexHistoryBase (
1799+ candidate . providerInstanceId ,
1800+ snapshot . records ,
1801+ snapshot . historyBytes ,
1802+ )
1803+ : null ;
17221804 const parsedThread = parseAgentSessionRecords (
17231805 {
17241806 source : candidate . source ,
17251807 providerInstanceId : candidate . providerInstanceId ,
17261808 fallbackSessionId : path . basename ( transcript . filePath , ".jsonl" ) ,
17271809 lastActiveAtMs : transcript . mtimeMs ,
17281810 } ,
1729- snapshot . records ,
1811+ inheritedRecords === null
1812+ ? snapshot . records
1813+ : [ ...inheritedRecords , ...snapshot . records ] ,
17301814 snapshot . claudeFields ,
17311815 ) ;
17321816 if ( parsedThread === null ) {
0 commit comments