@@ -72,7 +72,7 @@ export function countTranscriptEventsForPath(
7272 let events = 0 ;
7373 try {
7474 for ( const line of iterateJsonlLinesSync ( transcriptPath ) ) {
75- if ( ! parseJsonlLine ( line ) ) {
75+ if ( ! JSON . parse ( line ) ) {
7676 continue ;
7777 }
7878 events += 1 ;
@@ -90,88 +90,67 @@ export function createTranscriptEventReader(
9090 sourceFingerprint = readTranscriptFingerprint ( transcriptPath ) ,
9191) : ( append : ( event : TranscriptEvent ) => void ) => ( ) => void {
9292 return ( append ) => {
93- for ( const event of readTranscriptEventsForImport (
93+ // Production import owns the process-wide Gateway/SQLite-maintenance lock
94+ // through commit and archive. Fingerprints catch non-cooperating external edits.
95+ const plan = planTranscriptImport ( transcriptPath , allowMalformedPrefix ) ;
96+ assertTranscriptFileUnchanged ( transcriptPath , sourceFingerprint ) ;
97+ // V1 compactions refer to original row indexes. Stable index-derived IDs let
98+ // the second pass resolve those links without retaining the transcript.
99+ const idPrefix = createHash ( "sha256" )
100+ . update ( transcriptPath )
101+ . update ( "\0" )
102+ . update ( sessionId )
103+ . digest ( "hex" )
104+ . slice ( 0 , 16 ) ;
105+ assertTranscriptFileUnchanged ( transcriptPath , sourceFingerprint ) ;
106+ const migratedTargetIds = new Map < number , string > ( ) ;
107+ const migrationState : SessionFileEntryMigrationState = {
108+ createEntryId : ( originalIndex ) => `${ idPrefix } -${ originalIndex . toString ( 36 ) } ` ,
109+ previousId : null ,
110+ resolveOriginalEntryId : ( originalIndex ) => migratedTargetIds . get ( originalIndex ) ,
111+ sourceVersion : plan . sourceVersion ,
112+ } ;
113+ for ( const { event : loadedEvent , originalIndex } of iterateTranscriptEvents (
94114 transcriptPath ,
95- sessionId ,
96115 allowMalformedPrefix ,
97- sourceFingerprint ,
98116 ) ) {
99- append ( event as TranscriptEvent ) ;
100- }
101- return ( ) => assertTranscriptFileUnchanged ( transcriptPath , sourceFingerprint ) ;
102- } ;
103- }
104-
105- function readTranscriptEventsForImport (
106- transcriptPath : string ,
107- sessionId : string ,
108- allowMalformedPrefix : boolean ,
109- sourceFingerprint : TranscriptFileFingerprint ,
110- ) : Iterable < FileEntry > {
111- // Production import owns the process-wide Gateway/SQLite-maintenance lock
112- // through commit and archive. Fingerprints catch non-cooperating external edits.
113- const plan = planTranscriptImport ( transcriptPath , allowMalformedPrefix ) ;
114- assertTranscriptFileUnchanged ( transcriptPath , sourceFingerprint ) ;
115- // V1 compactions refer to original row indexes. Stable index-derived IDs let
116- // the second pass resolve those links without retaining the transcript.
117- const idPrefix = createHash ( "sha256" )
118- . update ( transcriptPath )
119- . update ( "\0" )
120- . update ( sessionId )
121- . digest ( "hex" )
122- . slice ( 0 , 16 ) ;
123-
124- return {
125- * [ Symbol . iterator ] ( ) {
126- assertTranscriptFileUnchanged ( transcriptPath , sourceFingerprint ) ;
127- const migratedTargetIds = new Map < number , string > ( ) ;
128- const migrationState : SessionFileEntryMigrationState = {
129- createEntryId : ( originalIndex ) => `${ idPrefix } -${ originalIndex . toString ( 36 ) } ` ,
130- previousId : null ,
131- resolveOriginalEntryId : ( originalIndex ) => migratedTargetIds . get ( originalIndex ) ,
132- sourceVersion : plan . sourceVersion ,
133- } ;
134- for ( const { event : loadedEvent , originalIndex } of iterateTranscriptEvents (
135- transcriptPath ,
136- allowMalformedPrefix ,
137- ) ) {
138- let event = loadedEvent ;
139- if ( plan . sourceVersion >= 2 ) {
140- recordLegacyCompactionTarget ( event , plan . compactionTargetIndexes ) ;
141- }
142- let recognizedEvent : FileEntry | undefined ;
143- if ( originalIndex === plan . headerIndex ) {
144- const canonicalHeader = {
145- ...event ,
146- id : sessionId ,
147- type : "session" as const ,
148- timestamp : typeof event . timestamp === "string" ? event . timestamp : "" ,
149- cwd : "cwd" in event && typeof event . cwd === "string" ? event . cwd : "" ,
150- } ;
151- Reflect . deleteProperty ( canonicalHeader , "sessionId" ) ;
152- event = canonicalHeader ;
153- recognizedEvent = event ;
154- } else {
155- const classified = classifySessionFileEntry ( event , plan . sourceVersion ) ;
156- // Runtime retains normalized opaque rows; import preserves their loaded bytes.
157- recognizedEvent = classified . recognized ? classified . entry : undefined ;
158- }
117+ let event = loadedEvent ;
118+ if ( plan . sourceVersion >= 2 ) {
119+ recordLegacyCompactionTarget ( event , plan . compactionTargetIndexes ) ;
120+ }
121+ let recognizedEvent : FileEntry | undefined ;
122+ if ( originalIndex === plan . headerIndex ) {
123+ const canonicalHeader = {
124+ ...event ,
125+ id : sessionId ,
126+ type : "session" as const ,
127+ timestamp : typeof event . timestamp === "string" ? event . timestamp : "" ,
128+ cwd : "cwd" in event && typeof event . cwd === "string" ? event . cwd : "" ,
129+ } ;
130+ Reflect . deleteProperty ( canonicalHeader , "sessionId" ) ;
131+ event = canonicalHeader ;
132+ recognizedEvent = event ;
133+ } else {
134+ const classified = classifySessionFileEntry ( event , plan . sourceVersion ) ;
135+ // Runtime retains normalized opaque rows; import preserves their loaded bytes.
136+ recognizedEvent = classified . recognized ? classified . entry : undefined ;
137+ }
159138
160- if ( recognizedEvent ) {
161- migrateSessionFileEntryToCurrentVersion ( recognizedEvent , originalIndex , migrationState ) ;
162- if (
163- plan . sourceVersion < 2 &&
164- recognizedEvent . type !== "session" &&
165- plan . compactionTargetIndexes . has ( originalIndex )
166- ) {
167- migratedTargetIds . set ( originalIndex , recognizedEvent . id ) ;
168- }
169- event = recognizedEvent ;
139+ if ( recognizedEvent ) {
140+ migrateSessionFileEntryToCurrentVersion ( recognizedEvent , originalIndex , migrationState ) ;
141+ if (
142+ plan . sourceVersion < 2 &&
143+ recognizedEvent . type !== "session" &&
144+ plan . compactionTargetIndexes . has ( originalIndex )
145+ ) {
146+ migratedTargetIds . set ( originalIndex , recognizedEvent . id ) ;
170147 }
171- yield event ;
148+ event = recognizedEvent ;
172149 }
173- assertTranscriptFileUnchanged ( transcriptPath , sourceFingerprint ) ;
174- } ,
150+ append ( event as TranscriptEvent ) ;
151+ }
152+ assertTranscriptFileUnchanged ( transcriptPath , sourceFingerprint ) ;
153+ return ( ) => assertTranscriptFileUnchanged ( transcriptPath , sourceFingerprint ) ;
175154 } ;
176155}
177156
@@ -271,7 +250,7 @@ function* iterateTranscriptEvents(
271250 let originalIndex = 0 ;
272251 try {
273252 for ( const line of iterateJsonlLinesSync ( transcriptPath ) ) {
274- const parsed = parseJsonlLine ( line ) ;
253+ const parsed = JSON . parse ( line ) ;
275254 if ( ! parsed ) {
276255 continue ;
277256 }
@@ -527,7 +506,7 @@ export function listExistingAgentDatabaseTargets(
527506 ) ;
528507}
529508
530- function * iterateJsonlLinesSync ( filePath : string ) : Generator < { lineNumber : number ; text : string } > {
509+ function * iterateJsonlLinesSync ( filePath : string ) : Generator < string > {
531510 const fd = fs . openSync ( filePath , "r" ) ;
532511 const decoder = new TextDecoder ( "utf-8" , { fatal : true } ) ;
533512 const buffer = Buffer . allocUnsafe ( JSONL_READ_CHUNK_BYTES ) ;
@@ -551,15 +530,15 @@ function* iterateJsonlLinesSync(filePath: string): Generator<{ lineNumber: numbe
551530 lineNumber += 1 ;
552531 const text = parts [ index ] ! . trim ( ) ;
553532 if ( text ) {
554- yield { lineNumber , text } ;
533+ yield text ;
555534 }
556535 }
557536 fragments . push ( parts . at ( - 1 ) ! ) ;
558537 }
559538 fragments . push ( decoder . decode ( ) ) ;
560539 const text = fragments . join ( "" ) . trim ( ) ;
561540 if ( text ) {
562- yield { lineNumber : lineNumber + 1 , text } ;
541+ yield text ;
563542 }
564543 } catch ( err ) {
565544 throw new Error ( `${ filePath } :${ lineNumber + 1 } : ${ String ( err ) } ` , { cause : err } ) ;
@@ -577,7 +556,3 @@ function sqliteNumber(value: unknown): number {
577556 }
578557 return 0 ;
579558}
580-
581- function parseJsonlLine ( line : { text : string } ) : unknown {
582- return JSON . parse ( line . text ) ;
583- }
0 commit comments