@@ -27,11 +27,14 @@ function historyEventId(entry: { event: unknown } | undefined): unknown {
2727 return event && typeof event === "object" && "id" in event ? event . id : undefined ;
2828}
2929
30- function enforceSqliteVariableLimit ( database : OpenClawAgentDatabase ) : void {
30+ function enforceSqliteVariableLimit (
31+ database : OpenClawAgentDatabase ,
32+ limit = REGRESSION_SQLITE_VARIABLE_LIMIT ,
33+ ) : void {
3134 const prepare = database . db . prepare . bind ( database . db ) ;
3235 vi . spyOn ( database . db , "prepare" ) . mockImplementation ( ( source ) => {
3336 const variableCount = source . match ( / \? / gu) ?. length ?? 0 ;
34- if ( variableCount > REGRESSION_SQLITE_VARIABLE_LIMIT ) {
37+ if ( variableCount > limit ) {
3538 throw new Error ( "too many SQL variables" ) ;
3639 }
3740 return prepare ( source ) ;
@@ -318,30 +321,84 @@ describe("SQLite transcript history events", () => {
318321 expect ( events . map ( historyEventId ) ) . toEqual ( [ "seed" , "active-boundary-2" , "active-boundary-4" ] ) ;
319322 } ) ;
320323
321- it ( "bounds metadata bindings when the raw history window exceeds SQLite's limit" , async ( ) => {
324+ it . each ( [ REGRESSION_MAX_MESSAGES , REGRESSION_SQLITE_VARIABLE_LIMIT + 1 ] ) (
325+ "reads %s recent messages with bounded metadata bindings" ,
326+ async ( maxMessages ) => {
327+ await persistSessionTranscriptTurn ( scope , {
328+ messages : [ { eventId : "seed" , parentId : null , message : { role : "user" , content : "seed" } } ] ,
329+ touchSessionEntry : false ,
330+ } ) ;
331+ const database = openOpenClawAgentDatabase ( { agentId : scope . agentId , env : scope . env } ) ;
332+ const bindingCount = Math . max ( REGRESSION_SQLITE_VARIABLE_LIMIT , maxMessages ) ;
333+ insertSyntheticHistory ( database , scope . sessionId , bindingCount ) ;
334+ enforceSqliteVariableLimit ( database ) ;
335+
336+ const page = readRecentSessionTranscriptHistoryEvents ( scope , {
337+ maxBytes : 1_000_000 ,
338+ maxLines : bindingCount + 1 ,
339+ maxMessages,
340+ } ) ;
341+
342+ expect ( page . totalMessages ) . toBe ( bindingCount + 1 ) ;
343+ expect ( page . events ) . toHaveLength ( maxMessages ) ;
344+ expect ( historyEventId ( page . events [ 0 ] ) ) . toBe (
345+ `synthetic-message-${ String ( bindingCount - maxMessages + 2 ) } ` ,
346+ ) ;
347+ expect ( historyEventId ( page . events . at ( - 1 ) ) ) . toBe (
348+ `synthetic-message-${ String ( bindingCount + 1 ) } ` ,
349+ ) ;
350+ } ,
351+ ) ;
352+
353+ it ( "batches sparse reset history without reviving discarded tool results" , async ( ) => {
354+ const keptIds = Array . from ( { length : 1_001 } , ( _ , index ) => `kept-${ index } ` ) ;
322355 await persistSessionTranscriptTurn ( scope , {
323- messages : [ { eventId : "seed" , parentId : null , message : { role : "user" , content : "seed" } } ] ,
356+ messages : keptIds . flatMap ( ( eventId , index ) => [
357+ {
358+ eventId,
359+ parentId : index === 0 ? null : `tool-${ index - 1 } ` ,
360+ message : { role : "user" , content : eventId } ,
361+ } ,
362+ {
363+ eventId : `tool-${ index } ` ,
364+ parentId : eventId ,
365+ message : { role : "toolResult" , content : "discarded orphan result" } ,
366+ } ,
367+ ] ) ,
368+ touchSessionEntry : false ,
369+ } ) ;
370+ await appendTranscriptEvent ( scope , {
371+ type : "reset" ,
372+ id : "reset" ,
373+ parentId : "tool-1000" ,
374+ timestamp : "2026-08-30T00:00:00.000Z" ,
375+ reason : "new" ,
376+ firstKeptEntryId : keptIds [ 0 ] ,
377+ } ) ;
378+ await persistSessionTranscriptTurn ( scope , {
379+ messages : [
380+ { eventId : "fresh" , parentId : "reset" , message : { role : "user" , content : "fresh" } } ,
381+ ] ,
324382 touchSessionEntry : false ,
325383 } ) ;
326384 const database = openOpenClawAgentDatabase ( { agentId : scope . agentId , env : scope . env } ) ;
327- const bindingCount = REGRESSION_SQLITE_VARIABLE_LIMIT ;
328- insertSyntheticHistory ( database , scope . sessionId , bindingCount ) ;
329- enforceSqliteVariableLimit ( database ) ;
385+ enforceSqliteVariableLimit ( database , 999 ) ;
330386
331387 const page = readRecentSessionTranscriptHistoryEvents ( scope , {
332388 maxBytes : 1_000_000 ,
333- maxLines : bindingCount + 1 ,
334- maxMessages : REGRESSION_MAX_MESSAGES ,
389+ maxLines : 2_010 ,
390+ maxMessages : keptIds . length + 2 ,
335391 } ) ;
336-
337- expect ( page . totalMessages ) . toBe ( bindingCount + 1 ) ;
338- expect ( page . events ) . toHaveLength ( REGRESSION_MAX_MESSAGES ) ;
339- expect ( historyEventId ( page . events [ 0 ] ) ) . toBe (
340- `synthetic-message-${ String ( bindingCount - REGRESSION_MAX_MESSAGES + 2 ) } ` ,
341- ) ;
342- expect ( historyEventId ( page . events . at ( - 1 ) ) ) . toBe (
343- `synthetic-message-${ String ( bindingCount + 1 ) } ` ,
392+ expect ( page . totalMessages ) . toBe ( keptIds . length + 2 ) ;
393+ expect ( page . events . map ( historyEventId ) ) . toEqual ( [ ...keptIds , "reset" , "fresh" ] ) ;
394+ expect ( page . events . map ( ( { seq } ) => seq ) ) . toEqual (
395+ Array . from ( { length : keptIds . length + 2 } , ( _ , index ) => index + 1 ) ,
344396 ) ;
397+ expect (
398+ readSessionTranscriptHistoryEventPage ( scope , { offset : 500 , maxMessages : 2 } ) . events . map (
399+ historyEventId ,
400+ ) ,
401+ ) . toEqual ( [ "kept-501" , "kept-502" ] ) ;
345402 } ) ;
346403
347404 it ( "reads more boundaries than SQLite permits as statement bindings" , async ( ) => {
0 commit comments