@@ -18,10 +18,12 @@ import type {
1818 TranscriptMessageAppendOptions ,
1919} from "./session-accessor.sqlite-contract.js" ;
2020import {
21+ createTranscriptIdentityReader ,
2122 findTranscriptEventInDatabase ,
2223 loadTranscriptEventsFromDatabase ,
2324 readTranscriptEventId ,
2425 readTranscriptEventMessage ,
26+ readTranscriptIdentityByEventId ,
2527} from "./session-accessor.sqlite-read.js" ;
2628import { getSessionKysely , type ResolvedTranscriptScope } from "./session-accessor.sqlite-scope.js" ;
2729import {
@@ -60,6 +62,7 @@ type TranscriptAppendOptions = {
6062
6163type TranscriptAppendCursor = {
6264 initialized ?: boolean ;
65+ readIdentity ?: ReturnType < typeof createTranscriptIdentityReader > ;
6366 nextSeq ?: number ;
6467 appendToIndex ?: ReturnType < typeof createTranscriptIndexAppenderInTransaction > ;
6568 updateWindow ?: ( createdAt : number ) => unknown ;
@@ -118,8 +121,12 @@ function appendTranscriptEvent(
118121 cursor . initialized = true ;
119122 }
120123 const identity = readTranscriptEventIdentity ( persistedEvent ) ;
121- if ( identity && readTranscriptIdentityByEventId ( database , scope . sessionId , identity . eventId ) ) {
122- return false ;
124+ if ( identity ) {
125+ // Reuse compilation within this batch, but read fresh rows after every append.
126+ cursor . readIdentity ??= createTranscriptIdentityReader ( database , scope . sessionId ) ;
127+ if ( cursor . readIdentity ( identity . eventId ) ) {
128+ return false ;
129+ }
123130 }
124131 const idempotencyKeyOwner = identity ?. messageIdempotencyKey
125132 ? readIdempotencyKeyOwner ( database , scope . sessionId , identity . messageIdempotencyKey )
@@ -575,23 +582,6 @@ export function updateSqliteTranscriptEventJsonInTransaction(
575582 scheduleTranscriptProjectionReconcile ( database , sessionId , ! rebuildSynchronously , { } ) ;
576583}
577584
578- export function readTranscriptIdentityByEventId (
579- database : OpenClawAgentDatabase ,
580- sessionId : string ,
581- eventId : string ,
582- ) : { eventId : string ; parentId : string | null ; seq : number } | undefined {
583- const db = getSessionKysely ( database . db ) ;
584- const row = executeSqliteQueryTakeFirstSync (
585- database . db ,
586- db
587- . selectFrom ( "transcript_event_identities" )
588- . select ( [ "event_id" , "parent_id" , "seq" ] )
589- . where ( "session_id" , "=" , sessionId )
590- . where ( "event_id" , "=" , eventId ) ,
591- ) ;
592- return row ? { eventId : row . event_id , parentId : row . parent_id , seq : row . seq } : undefined ;
593- }
594-
595585function readIdempotencyKeyOwner (
596586 database : OpenClawAgentDatabase ,
597587 sessionId : string ,
0 commit comments