@@ -393,6 +393,35 @@ describe('FullCompaction', () => {
393393 expect ( messageText ( compactionCall ?. history [ 2 ] ) ) . toBe ( '[Old tool result content cleared]' ) ;
394394 expect ( messageText ( compactionCall ?. history [ 5 ] ) ) . toBe ( 'lookup result' ) ;
395395 } ) ;
396+ it ( 'uses global history offsets when projecting a from compaction range' , async ( ) => {
397+ enableMicroCompactionFlag ( ) ;
398+ const ctx = testAgent ( {
399+ microCompaction : {
400+ keepRecentMessages : 0 ,
401+ minContentTokens : 1 ,
402+ } ,
403+ } ) ;
404+ ctx . configure ( {
405+ provider : CATALOGUED_PROVIDER ,
406+ modelCapabilities : CATALOGUED_MODEL_CAPABILITIES ,
407+ } ) ;
408+ ctx . appendExchange ( 1 , 'old user one' , 'old assistant one' , 20 ) ;
409+ ctx . appendToolExchange ( ) ;
410+ const longToolResult = 'lookup result ' . repeat ( 100 ) ;
411+ const toolResult = ctx . agent . context . history . at ( - 1 ) ;
412+ if ( toolResult ?. role !== 'tool' ) throw new Error ( 'Expected a tool result.' ) ;
413+ toolResult . content [ 0 ] = { type : 'text' , text : longToolResult } ;
414+ ctx . appendExchange ( 3 , 'recent user two' , 'recent assistant two' , 40 ) ;
415+ ctx . agent . microCompaction . apply ( 3 ) ;
416+ const compacted = ctx . once ( 'context.apply_compaction' ) ;
417+
418+ ctx . mockNextResponse ( { type : 'text' , text : 'Compacted summary.' } ) ;
419+ await ctx . rpc . beginCompaction ( { promptFromEnd : 2 , direction : 'from' } as never ) ;
420+ await compacted ;
421+
422+ const [ compactionCall ] = ctx . llmCalls ;
423+ expect ( messageText ( compactionCall ?. history [ 2 ] ) ) . toBe ( longToolResult ) ;
424+ } ) ;
396425
397426 it ( 'fires PreCompact and PostCompact hooks from the compaction module' , async ( ) => {
398427 const dir = mkdtempSync ( join ( tmpdir ( ) , 'pythinker-compact-hooks-' ) ) ;
0 commit comments