@@ -154,6 +154,162 @@ describe("SQLite active transcript event projection", () => {
154154 } ) ;
155155 } ) ;
156156
157+ it ( "stops counting discarded transcript bytes after a reset" , async ( ) => {
158+ await persistSessionTranscriptTurn ( scope , {
159+ messages : [
160+ {
161+ eventId : "discarded-old" ,
162+ parentId : null ,
163+ message : { role : "user" , content : `discarded ${ "x" . repeat ( 20_000 ) } ` } ,
164+ } ,
165+ ] ,
166+ touchSessionEntry : false ,
167+ } ) ;
168+ await appendTranscriptEvent ( scope , {
169+ type : "reset" ,
170+ id : "reset-boundary" ,
171+ parentId : "discarded-old" ,
172+ timestamp : "2026-08-15T00:00:00.000Z" ,
173+ reason : "new" ,
174+ } ) ;
175+ await persistSessionTranscriptTurn ( scope , {
176+ messages : [
177+ {
178+ eventId : "post-reset" ,
179+ parentId : "reset-boundary" ,
180+ message : { role : "user" , content : "fresh turn" } ,
181+ } ,
182+ ] ,
183+ touchSessionEntry : false ,
184+ } ) ;
185+
186+ expect ( readSessionTranscriptActiveStats ( scope ) ) . toMatchObject ( { eventCount : 1 } ) ;
187+ expect ( readSessionTranscriptActiveStats ( scope ) . sizeBytes ) . toBeLessThan ( 1_000 ) ;
188+ } ) ;
189+
190+ it ( "counts paired reset tool results without counting discarded orphan results" , async ( ) => {
191+ const assistantMessage = {
192+ role : "assistant" as const ,
193+ api : "openai-responses" as const ,
194+ provider : "openai" ,
195+ model : "gpt-5.6-sol" ,
196+ content : [ { type : "toolCall" as const , id : "call-1" , name : "read" , arguments : { } } ] ,
197+ stopReason : "toolUse" as const ,
198+ timestamp : Date . parse ( "2026-08-15T00:00:00.000Z" ) ,
199+ usage : {
200+ input : 1 ,
201+ output : 1 ,
202+ cacheRead : 0 ,
203+ cacheWrite : 0 ,
204+ totalTokens : 2 ,
205+ cost : { input : 0 , output : 0 , cacheRead : 0 , cacheWrite : 0 , total : 0 } ,
206+ } ,
207+ } ;
208+ await persistSessionTranscriptTurn ( scope , {
209+ messages : [
210+ {
211+ eventId : "discarded-old" ,
212+ parentId : null ,
213+ message : { role : "user" , content : `discarded ${ "x" . repeat ( 12_000 ) } ` } ,
214+ } ,
215+ {
216+ eventId : "kept-user" ,
217+ parentId : "discarded-old" ,
218+ message : { role : "user" , content : "kept question" } ,
219+ } ,
220+ { eventId : "kept-assistant" , parentId : "kept-user" , message : assistantMessage } ,
221+ {
222+ eventId : "kept-result" ,
223+ parentId : "kept-assistant" ,
224+ message : {
225+ role : "toolResult" ,
226+ toolCallId : "call-1" ,
227+ toolName : "read" ,
228+ content : [ { type : "text" , text : `paired ${ "p" . repeat ( 3_000 ) } ` } ] ,
229+ isError : false ,
230+ timestamp : Date . parse ( "2026-08-15T00:00:01.000Z" ) ,
231+ } ,
232+ } ,
233+ {
234+ eventId : "discarded-orphan" ,
235+ parentId : "kept-result" ,
236+ message : {
237+ role : "toolResult" ,
238+ toolCallId : "orphan-call" ,
239+ toolName : "read" ,
240+ content : [ { type : "text" , text : `orphan ${ "o" . repeat ( 20_000 ) } ` } ] ,
241+ isError : false ,
242+ timestamp : Date . parse ( "2026-08-15T00:00:02.000Z" ) ,
243+ } ,
244+ } ,
245+ ] ,
246+ touchSessionEntry : false ,
247+ } ) ;
248+ await appendTranscriptEvent ( scope , {
249+ type : "reset" ,
250+ id : "reset-boundary" ,
251+ parentId : "discarded-orphan" ,
252+ timestamp : "2026-08-15T00:00:03.000Z" ,
253+ reason : "new" ,
254+ firstKeptEntryId : "kept-user" ,
255+ } ) ;
256+ await persistSessionTranscriptTurn ( scope , {
257+ messages : [
258+ {
259+ eventId : "post-reset" ,
260+ parentId : "reset-boundary" ,
261+ message : { role : "user" , content : "fresh turn" } ,
262+ } ,
263+ ] ,
264+ touchSessionEntry : false ,
265+ } ) ;
266+
267+ const stats = readSessionTranscriptActiveStats ( scope ) ;
268+ expect ( stats . eventCount ) . toBe ( 4 ) ;
269+ expect ( stats . sizeBytes ) . toBeGreaterThan ( 3_000 ) ;
270+ expect ( stats . sizeBytes ) . toBeLessThan ( 8_000 ) ;
271+
272+ await persistSessionTranscriptTurn ( scope , {
273+ messages : [
274+ {
275+ eventId : "second-post-reset" ,
276+ parentId : "post-reset" ,
277+ message : { role : "assistant" , content : "fresh answer" } ,
278+ } ,
279+ ] ,
280+ touchSessionEntry : false ,
281+ } ) ;
282+ const parseSpy = vi . spyOn ( JSON , "parse" ) ;
283+ try {
284+ expect ( readSessionTranscriptActiveStats ( scope ) . eventCount ) . toBe ( 5 ) ;
285+ expect ( parseSpy ) . not . toHaveBeenCalled ( ) ;
286+ } finally {
287+ parseSpy . mockRestore ( ) ;
288+ }
289+ } ) ;
290+
291+ it ( "keeps counting genuinely oversized post-reset events" , async ( ) => {
292+ await appendTranscriptEvent ( scope , {
293+ type : "reset" ,
294+ id : "reset-boundary" ,
295+ parentId : null ,
296+ timestamp : "2026-08-15T00:00:00.000Z" ,
297+ reason : "new" ,
298+ } ) ;
299+ await persistSessionTranscriptTurn ( scope , {
300+ messages : [
301+ {
302+ eventId : "post-reset" ,
303+ parentId : "reset-boundary" ,
304+ message : { role : "user" , content : "x" . repeat ( 20_000 ) } ,
305+ } ,
306+ ] ,
307+ touchSessionEntry : false ,
308+ } ) ;
309+
310+ expect ( readSessionTranscriptActiveStats ( scope ) . sizeBytes ) . toBeGreaterThan ( 20_000 ) ;
311+ } ) ;
312+
157313 it ( "defers mixed legacy and canonical rebuilds off request stacks" , async ( ) => {
158314 await persistSessionTranscriptTurn ( scope , {
159315 messages : [
0 commit comments