@@ -44,6 +44,7 @@ import {
4444const TRANSCRIPT_PREFIX_BYTES = 32 * 1024 ;
4545const METADATA_READ_BYTES = 8 * 1024 ;
4646const MAX_TRANSCRIPT_SCAN_BYTES = 1024 * 1024 ;
47+ const MAX_CODEX_SESSION_INDEX_BYTES = 1024 * 1024 ;
4748
4849const MAX_TRANSCRIPTS_PER_SOURCE = 5000 ;
4950
@@ -102,12 +103,20 @@ const TranscriptRecord = Schema.Struct({
102103 ) ,
103104} ) ;
104105
106+ const CodexSessionIndexEntry = Schema . Struct ( {
107+ id : Schema . String ,
108+ thread_name : Schema . String ,
109+ } ) ;
110+
105111const decodeClaudeSettings = Schema . decodeUnknownOption ( ClaudeSettings ) ;
106112const decodeCodexSettings = Schema . decodeUnknownOption ( CodexSettings ) ;
107113const decodeTranscriptRecord = Schema . decodeUnknownOption ( Schema . fromJsonString ( TranscriptRecord ) ) ;
108114const decodeTranscriptValue = Schema . decodeUnknownOption ( TranscriptRecord ) ;
109115const selectTranscriptPath = createTranscriptJsonSelector ( TranscriptRecord ) ;
110116const decodeCodexTurnMetadata = Schema . decodeUnknownOption ( CodexTurnMetadata ) ;
117+ const decodeCodexSessionIndexEntry = Schema . decodeUnknownOption (
118+ Schema . fromJsonString ( CodexSessionIndexEntry ) ,
119+ ) ;
111120
112121type DecodedTranscriptRecord = typeof TranscriptRecord . Type ;
113122
@@ -162,6 +171,7 @@ interface RawCandidate {
162171 readonly cwd : string ;
163172 readonly source : AgentSessionSource ;
164173 readonly providerInstanceId : ProviderInstanceId ;
174+ readonly homePath : string ;
165175 readonly threadCount : number ;
166176 readonly lastActiveAtMs : number | null ;
167177 readonly transcripts : ReadonlyArray < {
@@ -174,6 +184,7 @@ interface TranscriptCandidate {
174184 readonly filePath : string ;
175185 readonly mtimeMs : number ;
176186 readonly providerInstanceId : ProviderInstanceId ;
187+ readonly homePath : string ;
177188 readonly size : number ;
178189}
179190
@@ -282,6 +293,17 @@ function foldClaudeFields(fields: ClaudeFields, record: DecodedTranscriptRecord)
282293const CODEX_TITLE_PREAMBLE =
283294 / ^ (?: \s * (?: < ( r e c o m m e n d e d _ p l u g i n s | e n v i r o n m e n t _ c o n t e x t | u s e r _ i n s t r u c t i o n s | I N S T R U C T I O N S | i n - a p p - b r o w s e r - c o n t e x t ) (?: \s [ ^ > ] * ) ? > [ \s \S ] * ?< \/ \1> | # A G E N T S \. m d i n s t r u c t i o n s \b [ ^ \n ] * | # # M y r e q u e s t f o r C o d e x : ) ) + / ;
284295
296+ // Codex appends an entry per rename; like Codex, the newest nonempty name wins.
297+ function parseCodexThreadNames ( contents : string ) : ReadonlyMap < string , string > {
298+ const names = new Map < string , string > ( ) ;
299+ for ( const line of contents . split ( "\n" ) ) {
300+ const entry = Option . getOrUndefined ( decodeCodexSessionIndexEntry ( line ) ) ;
301+ const name = entry ?. thread_name . trim ( ) ;
302+ if ( entry !== undefined && name ) names . set ( entry . id , name ) ;
303+ }
304+ return names ;
305+ }
306+
285307function deriveTitle ( source : AgentSessionSource , text : string ) : string | undefined {
286308 const request = source === "codex" ? text . replace ( CODEX_TITLE_PREAMBLE , "" ) : text ;
287309 return request . trim ( ) . split ( "\n" ) [ 0 ] ?. slice ( 0 , 100 ) . trim ( ) || undefined ;
@@ -902,6 +924,29 @@ export const make = Effect.gen(function* () {
902924 ) ;
903925 } ) ;
904926
927+ const readCodexThreadNames = Effect . fn ( "AgentSessionScanner.readCodexThreadNames" ) ( function * (
928+ homePath : string ,
929+ ) {
930+ const indexPath = path . join ( homePath , "session_index.jsonl" ) ;
931+ return yield * Effect . scoped (
932+ Effect . gen ( function * ( ) {
933+ const file = yield * fileSystem . open ( indexPath , { flag : "r" } ) ;
934+ const size = Number ( ( yield * file . stat ) . size ) ;
935+ const start = Math . max ( 0 , size - MAX_CODEX_SESSION_INDEX_BYTES ) ;
936+ yield * file . seek ( BigInt ( start ) , "start" ) ;
937+ const bytes = yield * file . readAlloc ( size - start ) ;
938+ const contents = Option . match ( bytes , {
939+ onNone : ( ) => "" ,
940+ onSome : ( value ) => new TextDecoder ( ) . decode ( value ) ,
941+ } ) ;
942+ // A tail read can start mid-record.
943+ return parseCodexThreadNames (
944+ start > 0 ? contents . slice ( contents . indexOf ( "\n" ) + 1 ) : contents ,
945+ ) ;
946+ } ) ,
947+ ) . pipe ( Effect . orElseSucceed ( ( ) : ReadonlyMap < string , string > => new Map ( ) ) ) ;
948+ } ) ;
949+
905950 const resolveClaudeConfigDir = ( homePath : string , environmentHome ?: string ) : string => {
906951 const configured = homePath . trim ( ) ;
907952 if ( configured . length > 0 ) {
@@ -958,6 +1003,7 @@ export const make = Effect.gen(function* () {
9581003 filePath,
9591004 mtimeMs : stats . value . mtime . value . getTime ( ) ,
9601005 providerInstanceId,
1006+ homePath,
9611007 size : Number ( stats . value . size ) ,
9621008 } ) ;
9631009 }
@@ -1019,6 +1065,7 @@ export const make = Effect.gen(function* () {
10191065 filePath,
10201066 mtimeMs : stats . value . mtime . value . getTime ( ) ,
10211067 providerInstanceId,
1068+ homePath,
10221069 size : Number ( stats . value . size ) ,
10231070 } ) ;
10241071 }
@@ -1040,6 +1087,7 @@ export const make = Effect.gen(function* () {
10401087 {
10411088 cwd : string ;
10421089 providerInstanceId : ProviderInstanceId ;
1090+ homePath : string ;
10431091 lastActiveAtMs : number ;
10441092 transcripts : Array < { filePath : string ; mtimeMs : number } > ;
10451093 }
@@ -1057,6 +1105,7 @@ export const make = Effect.gen(function* () {
10571105 byOwnerAndCwd . set ( key , {
10581106 cwd,
10591107 providerInstanceId : transcript . providerInstanceId ,
1108+ homePath : transcript . homePath ,
10601109 lastActiveAtMs : transcript . mtimeMs ,
10611110 transcripts : [ transcript ] ,
10621111 } ) ;
@@ -1067,6 +1116,7 @@ export const make = Effect.gen(function* () {
10671116 cwd : group . cwd ,
10681117 source,
10691118 providerInstanceId : group . providerInstanceId ,
1119+ homePath : group . homePath ,
10701120 threadCount : group . transcripts . length ,
10711121 lastActiveAtMs : group . lastActiveAtMs ,
10721122 transcripts : group . transcripts ,
@@ -1353,6 +1403,7 @@ export const make = Effect.gen(function* () {
13531403 ( source ) => `${ source . providerInstanceId } \0${ source . filePath } ` ,
13541404 ) ;
13551405 const importedSessions = new Set < string > ( ) ;
1406+ const codexThreadNamesByHome = new Map < string , ReadonlyMap < string , string > > ( ) ;
13561407 let bytesRemaining = MAX_IMPORT_BYTES ;
13571408 let transcriptsRemaining = MAX_IMPORT_TRANSCRIPTS ;
13581409 let recordsRemaining = MAX_IMPORT_RECORDS ;
@@ -1431,6 +1482,14 @@ export const make = Effect.gen(function* () {
14311482 if ( parsedThread === null ) {
14321483 return Option . some < AgentSessionRecentThread > ( { _tag : "Skipped" } ) ;
14331484 }
1485+ let savedTitle : string | undefined ;
1486+ if ( candidate . source === "codex" ) {
1487+ const names =
1488+ codexThreadNamesByHome . get ( candidate . homePath ) ??
1489+ ( yield * readCodexThreadNames ( candidate . homePath ) ) ;
1490+ codexThreadNamesByHome . set ( candidate . homePath , names ) ;
1491+ savedTitle = names . get ( parsedThread . providerSessionId ) ;
1492+ }
14341493
14351494 const source : AgentSessionImportSource = {
14361495 ...identity ,
@@ -1445,7 +1504,8 @@ export const make = Effect.gen(function* () {
14451504 importedSessions . add ( sessionKey ) ;
14461505 return Option . some < AgentSessionRecentThread > ( {
14471506 _tag : "Importable" ,
1448- thread : parsedThread ,
1507+ thread :
1508+ savedTitle === undefined ? parsedThread : { ...parsedThread , title : savedTitle } ,
14491509 source,
14501510 } ) ;
14511511 } ) . pipe ( importReadLock . withPermits ( 1 ) ) ,
0 commit comments