@@ -8,7 +8,10 @@ import {
88} from "../config/sessions/session-accessor.js" ;
99import { waitForSessionTranscriptIndexReconcile } from "../config/sessions/session-transcript-reconcile.js" ;
1010import { formatSqliteSessionFileMarker } from "../config/sessions/sqlite-marker.js" ;
11- import { closeOpenClawAgentDatabasesForTest } from "../state/openclaw-agent-db.js" ;
11+ import {
12+ closeOpenClawAgentDatabasesForTest ,
13+ openOpenClawAgentDatabase ,
14+ } from "../state/openclaw-agent-db.js" ;
1215import { closeOpenClawStateDatabaseForTest } from "../state/openclaw-state-db.js" ;
1316import { captureEnv , setTestEnvValue } from "../test-utils/env.js" ;
1417import { readSessionMessagesAroundIdWithStatsAsync } from "./session-transcript-anchor-reader.js" ;
@@ -362,6 +365,40 @@ describe("session transcript reader facade", () => {
362365 ) . resolves . toMatchObject ( { found : true , seq : 1 } ) ;
363366 } ) ;
364367
368+ test ( "waits for an in-flight SQLite projection before counting messages" , async ( ) => {
369+ const sessionId = "reader-sqlite-rebuilding-count" ;
370+ const scope = {
371+ agentId : "main" ,
372+ sessionId,
373+ sessionKey : `agent:main:${ sessionId } ` ,
374+ storePath,
375+ } ;
376+ await persistSessionTranscriptTurn ( scope , {
377+ messages : [
378+ {
379+ eventId : "root" ,
380+ parentId : null ,
381+ message : { role : "user" , content : "cross-client prompt" } ,
382+ } ,
383+ {
384+ eventId : "reply" ,
385+ parentId : "root" ,
386+ message : { role : "assistant" , content : "cross-client reply" } ,
387+ } ,
388+ ] ,
389+ touchSessionEntry : false ,
390+ } ) ;
391+ const database = openOpenClawAgentDatabase ( {
392+ agentId : "main" ,
393+ path : path . join ( tempDir , "openclaw-agent.sqlite" ) ,
394+ } ) ;
395+ database . db
396+ . prepare ( "UPDATE session_transcript_index_state SET needs_rebuild = 1 WHERE session_id = ?" )
397+ . run ( sessionId ) ;
398+
399+ await expect ( readSessionMessageCountAsync ( scope ) ) . resolves . toBe ( 2 ) ;
400+ } ) ;
401+
365402 test ( "projects SQLite transcript reads to the active branch" , async ( ) => {
366403 const sessionId = "reader-sqlite-branch" ;
367404 const scope = {
0 commit comments