@@ -327,13 +327,14 @@ describe("installToolResultContextGuard", () => {
327327 expectOpenClawTruncation ( getToolResultText ( transformed [ 0 ] ) ) ;
328328 } ) ;
329329
330- it ( "raises a structured mid-turn precheck signal after a new tool result overflows " , async ( ) => {
330+ it ( "raises a structured mid-turn precheck signal after projected tool results overflow " , async ( ) => {
331331 // The signal carries route metadata so the run loop can compact/truncate
332332 // without guessing from a generic overflow error.
333333 const agent = makeGuardableAgent ( ) ;
334334 const contextForNextCall = [
335335 makeUser ( "prompt already in history" ) ,
336336 makeToolResult ( "call_big" , "x" . repeat ( 80_000 ) ) ,
337+ makeToolResult ( "call_big_2" , "y" . repeat ( 80_000 ) ) ,
337338 ] ;
338339
339340 try {
@@ -349,12 +350,48 @@ describe("installToolResultContextGuard", () => {
349350 expect ( err ) . toBeInstanceOf ( MidTurnPrecheckSignal ) ;
350351 const signal = err as MidTurnPrecheckSignal ;
351352 expect ( signal . name ) . toBe ( "MidTurnPrecheckSignal" ) ;
352- expect ( signal . request . route ) . toBe ( "compact_then_truncate " ) ;
353+ expect ( signal . request . route ) . toBe ( "compact_only " ) ;
353354 expect ( typeof signal . request . overflowTokens ) . toBe ( "number" ) ;
354355 expect ( typeof signal . request . toolResultReducibleChars ) . toBe ( "number" ) ;
355356 }
356357 } ) ;
357358
359+ it ( "does not fire mid-turn precheck for tool-heavy history that fits after boundary projection" , async ( ) => {
360+ const agent = makeGuardableAgent ( ) ;
361+ const contextForNextCall = [ makeUser ( "prompt already in history" ) ] ;
362+ for ( let index = 0 ; index < 20 ; index += 1 ) {
363+ contextForNextCall . push ( makeToolResult ( `call_${ index } ` , "x" . repeat ( 12_500 ) ) ) ;
364+ }
365+
366+ const transformed = await applyMidTurnPrecheckGuardToContext ( agent , contextForNextCall , {
367+ contextWindowTokens : 200_000 ,
368+ contextTokenBudget : 200_000 ,
369+ reserveTokens : 50_000 ,
370+ toolResultMaxChars : 16_000 ,
371+ prePromptMessageCount : 1 ,
372+ } ) ;
373+
374+ expect ( transformed ) . toBe ( contextForNextCall ) ;
375+ } ) ;
376+
377+ it ( "uses the context window, not the pre-reserve budget, for mid-turn projection" , async ( ) => {
378+ const agent = makeGuardableAgent ( ) ;
379+ const contextForNextCall = [ makeUser ( "prompt already in history" ) ] ;
380+ for ( let index = 0 ; index < 40 ; index += 1 ) {
381+ contextForNextCall . push ( makeToolResult ( `call_${ index } ` , "x" . repeat ( 22_500 ) ) ) ;
382+ }
383+
384+ await expect (
385+ applyMidTurnPrecheckGuardToContext ( agent , contextForNextCall , {
386+ contextWindowTokens : 200_000 ,
387+ contextTokenBudget : 120_000 ,
388+ reserveTokens : 20_000 ,
389+ toolResultMaxChars : 16_000 ,
390+ prePromptMessageCount : 1 ,
391+ } ) ,
392+ ) . rejects . toBeInstanceOf ( MidTurnPrecheckSignal ) ;
393+ } ) ;
394+
358395 it ( "does not run mid-turn precheck when no new tool result was appended" , async ( ) => {
359396 const agent = makeGuardableAgent ( ) ;
360397 const contextForNextCall = [ makeUser ( "u" . repeat ( 80_000 ) ) ] ;
@@ -604,7 +641,11 @@ describe("installContextEngineLoopHook", () => {
604641 toolResultMaxChars : 16_000 ,
605642 } ) ;
606643
607- const messages = [ makeUser ( "first" ) , makeToolResult ( "call_1" , "x" . repeat ( 80_000 ) ) ] ;
644+ const messages = [
645+ makeUser ( "first" ) ,
646+ makeToolResult ( "call_1" , "x" . repeat ( 80_000 ) ) ,
647+ makeToolResult ( "call_2" , "y" . repeat ( 80_000 ) ) ,
648+ ] ;
608649
609650 await expect ( callTransform ( agent , messages ) ) . rejects . toBeInstanceOf ( MidTurnPrecheckSignal ) ;
610651 expect ( engine . afterTurn ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -625,7 +666,11 @@ describe("installContextEngineLoopHook", () => {
625666 toolResultMaxChars : 16_000 ,
626667 } ) ;
627668
628- const messages = [ makeUser ( "first" ) , makeToolResult ( "call_1" , "x" . repeat ( 80_000 ) ) ] ;
669+ const messages = [
670+ makeUser ( "first" ) ,
671+ makeToolResult ( "call_1" , "x" . repeat ( 80_000 ) ) ,
672+ makeToolResult ( "call_2" , "y" . repeat ( 80_000 ) ) ,
673+ ] ;
629674 const transformed = await callTransform ( agent , messages ) ;
630675
631676 expect ( transformed ) . toBe ( compactedView ) ;
0 commit comments