33import { randomInt } from "node:crypto" ;
44import path from "node:path" ;
55import { fileURLToPath , pathToFileURL } from "node:url" ;
6- import { Worker } from "node:worker_threads" ;
6+ import { Worker , type WorkerOptions } from "node:worker_threads" ;
77import { createSubsystemLogger } from "../../logging/subsystem.js" ;
88import {
99 resolveOpenClawAgentSqlitePath ,
@@ -17,7 +17,10 @@ import {
1717 runExclusiveSqliteSessionWrite ,
1818 toDatabaseOptions ,
1919} from "./session-accessor.sqlite-scope.js" ;
20- import { deleteOrphanedTranscriptIndexRowsInTransaction } from "./session-transcript-index.js" ;
20+ import {
21+ deleteOrphanedTranscriptIndexRowsInTransaction ,
22+ listSessionsNeedingTranscriptIndexReconcile ,
23+ } from "./session-transcript-index.js" ;
2124import {
2225 appendPreparedSessionTranscriptProjectionChunkInTransaction ,
2326 claimPreparedSessionTranscriptProjectionInTransaction ,
@@ -47,6 +50,7 @@ export type SessionTranscriptReconcileResult = {
4750} ;
4851
4952type SessionTranscriptReconcileParams = OpenClawAgentDatabaseOptions & {
53+ createWorker ?: ( filename : string | URL , options : WorkerOptions ) => Worker ;
5054 preferredSessionId ?: string ;
5155} ;
5256
@@ -198,7 +202,7 @@ async function finalizePreparedProjection(
198202}
199203
200204/** Prepares full trees off-thread, then commits bounded chunks through the runtime writer owner. */
201- export function reconcileSessionTranscriptIndexes (
205+ export async function reconcileSessionTranscriptIndexes (
202206 params : SessionTranscriptReconcileParams ,
203207) : Promise < SessionTranscriptReconcileResult > {
204208 const databasePath = resolveOpenClawAgentSqlitePath ( params ) ;
@@ -207,6 +211,19 @@ export function reconcileSessionTranscriptIndexes(
207211 ...( params . env ? { env : params . env } : { } ) ,
208212 path : databasePath ,
209213 } ;
214+ // The SQLite owner can cheaply prove a clean projection before paying for a
215+ // Worker. Keep the post-worker sweep too, because request-time writers may race.
216+ const needsWorker = await runProjectionWrite (
217+ databaseOptions ,
218+ "sessions.transcript-index.preflight" ,
219+ ( database ) => {
220+ deleteOrphanedTranscriptIndexRowsInTransaction ( database . db ) ;
221+ return listSessionsNeedingTranscriptIndexReconcile ( database . db ) . length > 0 ;
222+ } ,
223+ ) ;
224+ if ( ! needsWorker ) {
225+ return { reconciledSessions : 0 } ;
226+ }
210227 const workerUrl = resolveSessionTranscriptReconcileWorkerUrl ( ) ;
211228 const sourceWorkerExecArgv = workerUrl . pathname . endsWith ( ".ts" ) ? [ "--import" , "tsx" ] : undefined ;
212229 const input : SessionTranscriptReconcileWorkerInput = {
@@ -216,9 +233,12 @@ export function reconcileSessionTranscriptIndexes(
216233 } ;
217234 let worker : Worker ;
218235 try {
219- worker = new Worker ( workerUrl , { workerData : input , execArgv : sourceWorkerExecArgv } ) ;
236+ worker = ( params . createWorker ?? ( ( filename , options ) => new Worker ( filename , options ) ) ) (
237+ workerUrl ,
238+ { workerData : input , execArgv : sourceWorkerExecArgv } ,
239+ ) ;
220240 } catch ( error ) {
221- return Promise . reject ( normalizeReconcileError ( error ) ) ;
241+ throw normalizeReconcileError ( error ) ;
222242 }
223243
224244 return new Promise < SessionTranscriptReconcileResult > ( ( resolve , reject ) => {
0 commit comments