@@ -491,14 +491,68 @@ describe("createTransform nudge cache handling", () => {
491491 } ) ;
492492 } ) ;
493493
494- it ( "clears sticky turn reminder when messages contain a recent ctx_reduce call" , async ( ) => {
495- //#given
496- useTempDataHome ( "context-transform-sticky-suppress-by-message-" ) ;
497- const scheduler : Scheduler = { shouldExecute : mock ( ( ) => "defer" as const ) } ;
494+ it ( "keeps sticky turn reminder on cache-safe defer pass even when ctx_reduce is in recent messages" , async ( ) => {
495+ //#given — scheduler defers, so this is a cache-safe pass
496+ useTempDataHome ( "context-transform-sticky-keep-on-defer-" ) ;
498497 const db = openDatabase ( ) ;
499498 const transform = createTransform ( {
500499 tagger : createTagger ( ) ,
501- scheduler,
500+ scheduler : { shouldExecute : mock ( ( ) => "defer" as const ) } ,
501+ contextUsageMap : new Map < string , { usage : ContextUsage ; updatedAt : number } > ( [
502+ [
503+ "ses-1" ,
504+ { usage : { percentage : 41 , inputTokens : 80_000 } , updatedAt : Date . now ( ) } ,
505+ ] ,
506+ ] ) ,
507+ nudger : ( ) => null ,
508+ db,
509+ nudgePlacements : createNudgePlacementStore ( db ) ,
510+ flushedSessions : new Set < string > ( ) ,
511+ lastHeuristicsTurnId : new Map < string , string > ( ) ,
512+ clearReasoningAge : 50 ,
513+ protectedTags : 0 ,
514+ autoDropToolAge : 1000 ,
515+ } ) ;
516+
517+ setPersistedStickyTurnReminder (
518+ db ,
519+ "ses-1" ,
520+ '\n\n<instruction name="ctx_reduce_turn_cleanup">sticky reminder</instruction>' ,
521+ ) ;
522+
523+ const messages : TestMessage [ ] = [
524+ {
525+ info : { id : "m-user" , role : "user" , sessionID : "ses-1" } ,
526+ parts : [ { type : "text" , text : "user prompt" } ] ,
527+ } ,
528+ {
529+ info : { id : "m-assistant" , role : "assistant" } ,
530+ parts : [
531+ { type : "text" , text : "assistant response" } ,
532+ {
533+ type : "tool-invocation" as "text" ,
534+ callID : "reduce-call" as unknown as undefined ,
535+ toolName : "ctx_reduce" ,
536+ } as unknown as TestMessage [ "parts" ] [ 0 ] ,
537+ ] ,
538+ } ,
539+ ] ;
540+
541+ //#when
542+ await transform ( { } , { messages } ) ;
543+
544+ //#then — reminder stays in DB and keeps being reinjected (cache-safe: no removal)
545+ expect ( firstText ( messages [ 0 ] ! ) ) . toContain ( "sticky reminder" ) ;
546+ expect ( getPersistedStickyTurnReminder ( db , "ses-1" ) ) . not . toBeNull ( ) ;
547+ } ) ;
548+
549+ it ( "clears sticky turn reminder on cache-busting execute pass when ctx_reduce is in recent messages" , async ( ) => {
550+ //#given — scheduler executes, so this pass already busts cache
551+ useTempDataHome ( "context-transform-sticky-clear-on-execute-" ) ;
552+ const db = openDatabase ( ) ;
553+ const transform = createTransform ( {
554+ tagger : createTagger ( ) ,
555+ scheduler : { shouldExecute : mock ( ( ) => "execute" as const ) } ,
502556 contextUsageMap : new Map < string , { usage : ContextUsage ; updatedAt : number } > ( [
503557 [
504558 "ses-1" ,
@@ -521,7 +575,6 @@ describe("createTransform nudge cache handling", () => {
521575 '\n\n<instruction name="ctx_reduce_turn_cleanup">sticky reminder</instruction>' ,
522576 ) ;
523577
524- // Messages include a ctx_reduce tool call — agent already reduced
525578 const messages : TestMessage [ ] = [
526579 {
527580 info : { id : "m-user" , role : "user" , sessionID : "ses-1" } ,
@@ -543,7 +596,7 @@ describe("createTransform nudge cache handling", () => {
543596 //#when
544597 await transform ( { } , { messages } ) ;
545598
546- //#then — reminder should be cleared from DB and NOT injected
599+ //#then — reminder cleared from DB and NOT injected (cache already busting)
547600 expect ( firstText ( messages [ 0 ] ! ) ) . not . toContain ( "sticky reminder" ) ;
548601 expect ( getPersistedStickyTurnReminder ( db , "ses-1" ) ) . toBeNull ( ) ;
549602 } ) ;
0 commit comments