55} from "../../infra/kysely-sync.js" ;
66import {
77 openOpenClawAgentDatabase ,
8+ runOpenClawAgentWriteTransaction ,
89 type OpenClawAgentDatabase ,
910} from "../../state/openclaw-agent-db.js" ;
1011import { listSqliteSessionEntriesWithCanonicalOwnerEvidence } from "./session-accessor.sqlite-canonical-inventory.js" ;
@@ -19,10 +20,12 @@ import { collectSessionStateIdsForEntry } from "./session-accessor.sqlite-refere
1920import {
2021 getSessionKysely ,
2122 resolveSqliteStoreScope ,
23+ runExclusiveSqliteSessionWrite ,
2224 toDatabaseOptions ,
2325} from "./session-accessor.sqlite-scope.js" ;
2426import { bindSessionWindowEntryProjection } from "./session-accessor.sqlite-session-row.js" ;
2527import { parseSessionEntryJson } from "./session-accessor.sqlite-status.js" ;
28+ import { ensureTranscriptGenerationInTransaction } from "./session-accessor.sqlite-transcript-state.js" ;
2629import type { SessionEntryListScope } from "./session-accessor.types.js" ;
2730import { canonicalSessionKeyMigrationRequiredError } from "./session-canonical-key.js" ;
2831import {
@@ -135,6 +138,55 @@ export function listSqliteSessionGenerationIdsForCanonicalRepair(params: {
135138 } ) ;
136139}
137140
141+ /** Doctor-only normalization of imported transcript rows before copy or archival. */
142+ export async function ensureSqliteTranscriptGenerationsForCanonicalRepair (
143+ sources : readonly {
144+ agentId : string ;
145+ entry : SessionEntry ;
146+ sessionKey : string ;
147+ storePath : string ;
148+ } [ ] ,
149+ ) : Promise < void > {
150+ const byDatabase = new Map <
151+ string ,
152+ { resolved : ReturnType < typeof resolveSqliteStoreScope > ; sources : typeof sources }
153+ > ( ) ;
154+ for ( const source of sources ) {
155+ const resolved = resolveSqliteStoreScope ( source . storePath , { agentId : source . agentId } ) ;
156+ const key = `${ resolved . path ?? source . storePath } \0${ resolved . databaseAgentId ?? resolved . agentId } ` ;
157+ const grouped = byDatabase . get ( key ) ?? { resolved, sources : [ ] } ;
158+ byDatabase . set ( key , { ...grouped , sources : [ ...grouped . sources , source ] } ) ;
159+ }
160+ for ( const group of byDatabase . values ( ) ) {
161+ await runExclusiveSqliteSessionWrite ( group . resolved , async ( ) => {
162+ runOpenClawAgentWriteTransaction ( ( database ) => {
163+ // Inventory and generation creation share one snapshot so copied rows and later archive
164+ // plans observe the same immutable identity for each imported transcript.
165+ const sessionIds = uniqueStrings ( [
166+ ...group . sources . flatMap ( ( source ) => [ ...collectSessionStateIdsForEntry ( source . entry ) ] ) ,
167+ ...readSessionGenerationIdsForKeys (
168+ database ,
169+ group . sources . map ( ( source ) => source . sessionKey ) ,
170+ { exactStoredKeys : true } ,
171+ ) ,
172+ ] ) ;
173+ const db = getSessionKysely ( database . db ) ;
174+ const eventSessionIds = executeSqliteQuerySync (
175+ database . db ,
176+ db
177+ . selectFrom ( "transcript_events" )
178+ . select ( "session_id" )
179+ . where ( "session_id" , "in" , sessionIds )
180+ . groupBy ( "session_id" ) ,
181+ ) . rows ;
182+ for ( const row of eventSessionIds ) {
183+ ensureTranscriptGenerationInTransaction ( database , row . session_id ) ;
184+ }
185+ } , toDatabaseOptions ( group . resolved ) ) ;
186+ } ) ;
187+ }
188+ }
189+
138190/** Doctor-only same-store rewrite for delivery attribution owned by removed aliases. */
139191export function rehomeSqliteSessionDeliveryReferencesForCanonicalRepair (
140192 database : OpenClawAgentDatabase ,
0 commit comments