@@ -26,7 +26,12 @@ import {
2626 touchTranscriptMutationInTransaction ,
2727} from "./session-accessor.sqlite-transcript-state.js" ;
2828import { appendTranscriptEventInTransaction } from "./session-accessor.sqlite-transcript-store.js" ;
29+ import { invalidateExistingSessionTranscriptDisplayInTransaction } from "./session-transcript-display.js" ;
2930import { reconcileSessionTranscriptIndexInTransaction } from "./session-transcript-index.js" ;
31+ import {
32+ startSessionTranscriptDisplayReconcile ,
33+ startSessionTranscriptIndexReconcile ,
34+ } from "./session-transcript-reconcile.js" ;
3035import type { SessionEntry } from "./types.js" ;
3136
3237/** Internal doctor/migration import target for one legacy session row. */
@@ -48,6 +53,7 @@ type SqliteSessionImportRowsParams = {
4853
4954/** Summary of rows written by an internal doctor/migration import. */
5055type SqliteSessionImportRowsResult = {
56+ displayProjectionInvalidated : boolean ;
5157 sessionId : string ;
5258 sessionKey : string ;
5359 skippedExisting ?: true ;
@@ -82,6 +88,7 @@ function importSqliteSessionRowsInTransaction(
8288 prepared : ReturnType < typeof prepareSqliteSessionImport > ,
8389) : SqliteSessionImportRowsResult {
8490 const { params, resolved } = prepared ;
91+ let displayProjectionInvalidated = false ;
8592 let transcriptEvents = 0 ;
8693 // Doctor may have staged another legacy alias in this database already. Inspect only this
8794 // exact import target; runtime-wide canonical validation runs after the import phase.
@@ -90,6 +97,7 @@ function importSqliteSessionRowsInTransaction(
9097 } ) ?. entry ;
9198 if ( params . skipIfExists === true && currentEntry ) {
9299 return {
100+ displayProjectionInvalidated,
93101 sessionId : params . entry . sessionId ,
94102 sessionKey : resolved . sessionKey ,
95103 skippedExisting : true ,
@@ -153,6 +161,10 @@ function importSqliteSessionRowsInTransaction(
153161 }
154162 transcriptEvents = exactTranscriptRows . length ;
155163 reconcileSessionTranscriptIndexInTransaction ( database . db , params . entry . sessionId ) ;
164+ displayProjectionInvalidated = invalidateExistingSessionTranscriptDisplayInTransaction (
165+ database . db ,
166+ params . entry . sessionId ,
167+ ) ;
156168 publishSessionEntryCacheInvalidation ( database ) ;
157169 }
158170 } else if ( prepared . transcriptEvents ) {
@@ -172,6 +184,7 @@ function importSqliteSessionRowsInTransaction(
172184 if (
173185 appendTranscriptEventInTransaction ( database , transcriptScope , event , {
174186 allowStoredAlias : true ,
187+ maintainDisplayProjection : false ,
175188 scheduleProjectionReconcile : false ,
176189 touchMutation : false ,
177190 } )
@@ -181,6 +194,12 @@ function importSqliteSessionRowsInTransaction(
181194 }
182195 }
183196 reconcileSessionTranscriptIndexInTransaction ( database . db , params . entry . sessionId ) ;
197+ if ( transcriptEvents > 0 ) {
198+ displayProjectionInvalidated = invalidateExistingSessionTranscriptDisplayInTransaction (
199+ database . db ,
200+ params . entry . sessionId ,
201+ ) ;
202+ }
184203 publishSessionEntryCacheInvalidation ( database ) ;
185204 }
186205 if ( params . transcriptMtimeMs !== undefined ) {
@@ -193,6 +212,7 @@ function importSqliteSessionRowsInTransaction(
193212 touchTranscriptMutationInTransaction ( database , params . entry . sessionId ) ;
194213 }
195214 return {
215+ displayProjectionInvalidated,
196216 sessionId : params . entry . sessionId ,
197217 sessionKey : resolved . sessionKey ,
198218 transcriptEvents,
@@ -211,12 +231,25 @@ export async function importSqliteSessionRowsBatch(
211231 if ( prepared . some ( ( row ) => row . resolved . path !== resolved . path ) ) {
212232 throw new Error ( "SQLite session import batch spans multiple stores" ) ;
213233 }
214- return await runExclusiveSqliteSessionWrite ( resolved , async ( ) =>
234+ const results = await runExclusiveSqliteSessionWrite ( resolved , async ( ) =>
215235 runOpenClawAgentWriteTransaction (
216236 ( database ) => prepared . map ( ( row ) => importSqliteSessionRowsInTransaction ( database , row ) ) ,
217237 toDatabaseOptions ( resolved ) ,
218238 ) ,
219239 ) ;
240+ for ( const result of results ) {
241+ if ( result . transcriptEvents === 0 ) {
242+ continue ;
243+ }
244+ const startReconcile = result . displayProjectionInvalidated
245+ ? startSessionTranscriptDisplayReconcile
246+ : startSessionTranscriptIndexReconcile ;
247+ startReconcile ( {
248+ ...toDatabaseOptions ( resolved ) ,
249+ preferredSessionId : result . sessionId ,
250+ } ) ;
251+ }
252+ return results ;
220253}
221254
222255/** Imports one legacy session entry and its transcript rows for doctor migration. */
0 commit comments