@@ -14,6 +14,7 @@ import {
1414 readSessionTranscriptActiveLeafEvents ,
1515 readSessionTranscriptActiveStats ,
1616 readSessionTranscriptBoundedMessageTailPage ,
17+ readSessionTranscriptContextByteSize ,
1718 readSessionTranscriptMessageAnchorPage ,
1819 readSessionTranscriptMessageEventById ,
1920 readSessionTranscriptMessageEventCount ,
@@ -226,6 +227,59 @@ describe("SQLite active transcript event projection", () => {
226227 expect ( page . events . map ( ( { event } ) => ( event as { id ?: unknown } ) . id ) ) . toEqual ( [ "small" ] ) ;
227228 } ) ;
228229
230+ it ( "sizes only the reset-bounded replay window while retaining oversized history" , async ( ) => {
231+ await persistSessionTranscriptTurn ( scope , {
232+ messages : [
233+ {
234+ eventId : "oversized-history" ,
235+ parentId : null ,
236+ message : { role : "user" , content : "x" . repeat ( 4_300_000 ) } ,
237+ } ,
238+ {
239+ eventId : "kept-user" ,
240+ parentId : "oversized-history" ,
241+ message : { role : "user" , content : "kept question" } ,
242+ } ,
243+ {
244+ eventId : "kept-assistant" ,
245+ parentId : "kept-user" ,
246+ message : { role : "assistant" , content : "kept answer" } ,
247+ } ,
248+ ] ,
249+ touchSessionEntry : false ,
250+ } ) ;
251+ await appendTranscriptEvent ( scope , {
252+ type : "reset" ,
253+ id : "reset-boundary" ,
254+ parentId : "kept-assistant" ,
255+ timestamp : "2026-08-06T14:20:01.969Z" ,
256+ reason : "new" ,
257+ firstKeptEntryId : "kept-user" ,
258+ } ) ;
259+ await persistSessionTranscriptTurn ( scope , {
260+ messages : [
261+ {
262+ eventId : "post-reset" ,
263+ parentId : "reset-boundary" ,
264+ message : { role : "user" , content : "ordinary next turn" } ,
265+ } ,
266+ ] ,
267+ touchSessionEntry : false ,
268+ } ) ;
269+
270+ expect ( readSessionTranscriptActiveStats ( scope ) . sizeBytes ) . toBeGreaterThan ( 4 * 1024 * 1024 ) ;
271+ expect ( readSessionTranscriptContextByteSize ( scope ) ) . toBeLessThan ( 4 * 1024 ) ;
272+ expect ( readSessionTranscriptMessageEventCount ( scope ) ) . toBe ( 3 ) ;
273+ expect ( readSessionTranscriptMessageEventById ( scope , "oversized-history" ) ) . toBeUndefined ( ) ;
274+
275+ const database = openOpenClawAgentDatabase ( { agentId : scope . agentId , env : scope . env } ) ;
276+ expect (
277+ database . db
278+ . prepare ( "SELECT COUNT(*) AS count FROM transcript_events WHERE session_id = ?" )
279+ . get ( scope . sessionId ) ,
280+ ) . toEqual ( { count : 6 } ) ;
281+ } ) ;
282+
229283 it ( "fails fast and schedules maintenance when out-of-band state is dirty" , async ( ) => {
230284 await persistSessionTranscriptTurn ( scope , {
231285 messages : [
0 commit comments