@@ -23,7 +23,6 @@ import {
2323} from "./session-accessor.sqlite-scope.js" ;
2424import { bindSessionWindowEntryProjection } from "./session-accessor.sqlite-session-row.js" ;
2525import { parseSessionEntryJson } from "./session-accessor.sqlite-status.js" ;
26- import { ensureTranscriptGenerationInTransaction } from "./session-accessor.sqlite-transcript-state.js" ;
2726import { canonicalSessionKeyMigrationRequiredError } from "./session-canonical-key.js" ;
2827import { invalidateExistingSessionTranscriptDisplayInTransaction } from "./session-transcript-display.js" ;
2928import {
@@ -34,6 +33,11 @@ import {
3433 startSessionTranscriptDisplayReconcile ,
3534 startSessionTranscriptIndexReconcile ,
3635} from "./session-transcript-reconcile.js" ;
36+ import {
37+ ensureSessionTranscriptSourceGenerationInTransaction ,
38+ readSessionTranscriptSourceGenerationInTransaction ,
39+ replaceSessionTranscriptSourceGenerationInTransaction ,
40+ } from "./session-transcript-source-generation.js" ;
3741import { normalizeStoreSessionKey } from "./store-entry.js" ;
3842import type { SessionEntry } from "./types.js" ;
3943
@@ -167,16 +171,15 @@ export async function ensureSqliteTranscriptGenerationsForCanonicalRepair(
167171 ) ,
168172 ] ) ;
169173 const db = getSessionKysely ( database . db ) ;
170- const eventSessionIds = executeSqliteQuerySync (
174+ const windowSessionIds = executeSqliteQuerySync (
171175 database . db ,
172176 db
173- . selectFrom ( "transcript_events " )
177+ . selectFrom ( "session_windows " )
174178 . select ( "session_id" )
175- . where ( "session_id" , "in" , sessionIds )
176- . groupBy ( "session_id" ) ,
179+ . where ( "session_id" , "in" , sessionIds ) ,
177180 ) . rows ;
178- for ( const row of eventSessionIds ) {
179- ensureTranscriptGenerationInTransaction ( database , row . session_id ) ;
181+ for ( const row of windowSessionIds ) {
182+ ensureSessionTranscriptSourceGenerationInTransaction ( database , row . session_id ) ;
180183 }
181184 } , toDatabaseOptions ( group . resolved ) ) ;
182185 } ) ;
@@ -529,13 +532,10 @@ function copySqliteSessionGenerationRows(params: {
529532 . selectAll ( )
530533 . where ( "session_id" , "=" , params . sessionId ) ,
531534 ) . rows ;
532- const rewriteWatermarks = executeSqliteQuerySync (
535+ const sourceGeneration = readSessionTranscriptSourceGenerationInTransaction (
533536 params . source . db ,
534- sourceDb
535- . selectFrom ( "transcript_rewrite_watermarks" )
536- . selectAll ( )
537- . where ( "session_id" , "=" , params . sessionId ) ,
538- ) . rows ;
537+ params . sessionId ,
538+ ) ;
539539 const trajectoryEvents = executeSqliteQuerySync (
540540 params . source . db ,
541541 sourceDb
@@ -554,16 +554,18 @@ function copySqliteSessionGenerationRows(params: {
554554 ! params . sourceWindowPresent &&
555555 transcriptEvents . length === 0 &&
556556 transcriptIdentities . length === 0 &&
557- rewriteWatermarks . length === 0 &&
557+ sourceGeneration === undefined &&
558558 trajectoryEvents . length === 0 &&
559559 parentStreamEvents . length === 0
560560 ) {
561561 return false ;
562562 }
563+ if ( params . sourceWindowPresent && ! sourceGeneration ) {
564+ throw new Error ( `Canonical transcript source ${ params . sessionId } has no generation` ) ;
565+ }
563566 for ( const table of [
564567 "transcript_event_identities" ,
565568 "transcript_events" ,
566- "transcript_rewrite_watermarks" ,
567569 "trajectory_runtime_events" ,
568570 "acp_parent_stream_events" ,
569571 ] as const ) {
@@ -584,12 +586,7 @@ function copySqliteSessionGenerationRows(params: {
584586 destinationDb . insertInto ( "transcript_event_identities" ) . values ( row ) ,
585587 ) ;
586588 }
587- for ( const row of rewriteWatermarks ) {
588- executeSqliteQuerySync (
589- params . destination . db ,
590- destinationDb . insertInto ( "transcript_rewrite_watermarks" ) . values ( row ) ,
591- ) ;
592- }
589+ replaceSessionTranscriptSourceGenerationInTransaction ( params . destination , params . sessionId ) ;
593590 for ( const row of trajectoryEvents ) {
594591 executeSqliteQuerySync (
595592 params . destination . db ,
0 commit comments