@@ -534,3 +534,72 @@ it.layer(layerTest)("LegacyV1ThreadImporter", (it) => {
534534 } ) ,
535535 ) ;
536536} ) ;
537+
538+ it . layer ( layerTest ) ( "LegacyV1ThreadImporter transcript lookup" , ( it ) => {
539+ it . effect ( "finds imported messages on the V2 thread index, past the thread's V1 events" , ( ) =>
540+ Effect . gen ( function * ( ) {
541+ const sql = yield * SqlClient . SqlClient ;
542+ const importer = yield * LegacyV1ThreadImporter . LegacyV1ThreadImporter ;
543+ const threadId = ThreadId . make ( "thread:legacy-lookup" ) ;
544+ yield * sql `
545+ INSERT INTO projection_projects (
546+ project_id, title, workspace_root, default_model_selection_json, scripts_json,
547+ created_at, updated_at, deleted_at
548+ ) VALUES (
549+ 'project:legacy-lookup', 'Legacy project', '/tmp/legacy-lookup',
550+ '{"instanceId":"codex","model":"gpt-5.4"}', '[]',
551+ '2026-01-01T00:00:00.000Z', '2026-01-01T00:00:00.000Z', NULL
552+ )
553+ ` ;
554+ yield * sql `
555+ INSERT INTO projection_threads (
556+ thread_id, project_id, title, model_selection_json, runtime_mode, interaction_mode,
557+ created_at, updated_at
558+ ) VALUES (
559+ ${ threadId } , 'project:legacy-lookup', 'Migrated conversation',
560+ '{"instanceId":"codex","model":"gpt-5.4"}', 'full-access', 'default',
561+ '2026-01-01T00:00:00.000Z', '2026-01-01T00:00:00.000Z'
562+ )
563+ ` ;
564+ yield * sql `
565+ INSERT INTO projection_thread_messages (
566+ message_id, thread_id, role, text, is_streaming, created_at, updated_at
567+ ) VALUES (
568+ 'message:legacy-lookup:1', ${ threadId } , 'user', 'Question', 0,
569+ '2026-01-01T01:00:00.000Z', '2026-01-01T01:00:00.000Z'
570+ )
571+ ` ;
572+ yield * importer . reconcileShells ;
573+
574+ const statements : string [ ] = [ ] ;
575+ const tracer = Tracer . make ( {
576+ span ( options ) {
577+ const span = new Tracer . NativeSpan ( options ) ;
578+ const end = span . end . bind ( span ) ;
579+ span . end = ( endTime , exit ) => {
580+ end ( endTime , exit ) ;
581+ const query = span . attributes . get ( "db.query.text" ) ;
582+ if (
583+ typeof query === "string" &&
584+ / ^ \s * S E L E C T e v e n t _ i d \s + F R O M o r c h e s t r a t i o n _ e v e n t s / . test ( query )
585+ ) {
586+ statements . push ( query ) ;
587+ }
588+ } ;
589+ return span ;
590+ } ,
591+ } ) ;
592+ yield * importer . ensureTranscript ( threadId ) . pipe ( Effect . withTracer ( tracer ) ) ;
593+
594+ assert . lengthOf ( statements , 1 ) ;
595+ const plan = yield * sql . unsafe < { readonly detail : string } > (
596+ `EXPLAIN QUERY PLAN ${ statements [ 0 ] } ` ,
597+ [ threadId , "migration:v1:message:%" ] ,
598+ ) ;
599+ assert . match (
600+ plan . map ( ( row ) => row . detail ) . join ( "\n" ) ,
601+ / U S I N G I N D E X i d x _ o r c h e s t r a t i o n _ e v e n t s _ a g e n t _ s t r e a m _ s e q u e n c e \( s t r e a m _ i d = \? \) / ,
602+ ) ;
603+ } ) ,
604+ ) ;
605+ } ) ;
0 commit comments