@@ -149,6 +149,61 @@ describe("installEmbeddedAttemptContextGuards", () => {
149149 expect ( removeLoopHook ) . toHaveBeenCalledOnce ( ) ;
150150 } ) ;
151151
152+ it . each ( [
153+ { name : "attempt configuration is absent" , config : undefined } ,
154+ {
155+ name : "context pruning is not configured" ,
156+ config : { agents : { defaults : { } } } ,
157+ } ,
158+ {
159+ name : "context pruning has no mode" ,
160+ config : { agents : { defaults : { contextPruning : { } } } } ,
161+ } ,
162+ {
163+ name : "context pruning is explicitly off" ,
164+ config : { agents : { defaults : { contextPruning : { mode : "off" } } } } ,
165+ } ,
166+ ] ) ( "does not inspect providers when $name" , ( { config } ) => {
167+ hoisted . isCacheTtlEligibleProvider . mockReturnValue ( true ) ;
168+ const input = createInput ( ) ;
169+ input . attempt = { ...input . attempt , config : config as never } ;
170+
171+ const guards = installEmbeddedAttemptContextGuards ( input as never ) ;
172+
173+ expect ( hoisted . isCacheTtlEligibleProvider ) . not . toHaveBeenCalled ( ) ;
174+ expect ( hoisted . readLastCacheTtlTimestamp ) . not . toHaveBeenCalled ( ) ;
175+ guards . remove ( ) ;
176+ } ) ;
177+
178+ it ( "does not install cache-TTL pruning for an ineligible provider" , async ( ) => {
179+ const input = createInput ( ) ;
180+ input . attempt = {
181+ ...input . attempt ,
182+ config : { agents : { defaults : { contextPruning : { mode : "cache-ttl" } } } } as never ,
183+ } ;
184+ const originalTransform = vi . fn ( async ( messages : AgentMessage [ ] ) => messages ) ;
185+ input . activeSession . agent . transformContext = originalTransform ;
186+
187+ const guards = installEmbeddedAttemptContextGuards ( input as never ) ;
188+
189+ expect ( hoisted . isCacheTtlEligibleProvider ) . toHaveBeenCalledExactlyOnceWith (
190+ "provider-1" ,
191+ "model-1" ,
192+ "anthropic-messages" ,
193+ ) ;
194+ expect ( hoisted . readLastCacheTtlTimestamp ) . not . toHaveBeenCalled ( ) ;
195+ const messages : AgentMessage [ ] = [
196+ { role : "user" , content : [ { type : "text" , text : "hello" } ] , timestamp : 1 } ,
197+ ] ;
198+ expect (
199+ await input . activeSession . agent . transformContext ?.( messages , new AbortController ( ) . signal ) ,
200+ ) . toBe ( messages ) ;
201+ expect ( originalTransform ) . toHaveBeenCalledOnce ( ) ;
202+
203+ guards . remove ( ) ;
204+ expect ( input . activeSession . agent . transformContext ) . toBe ( originalTransform ) ;
205+ } ) ;
206+
152207 it ( "projects expired cache-TTL history before context-engine assembly once per attempt" , async ( ) => {
153208 const now = Date . now ( ) ;
154209 hoisted . isCacheTtlEligibleProvider . mockReturnValue ( true ) ;
@@ -193,6 +248,18 @@ describe("installEmbeddedAttemptContextGuards", () => {
193248 ] as AgentMessage [ ] ;
194249
195250 const guards = installEmbeddedAttemptContextGuards ( input as never ) ;
251+ expect ( hoisted . isCacheTtlEligibleProvider ) . toHaveBeenCalledExactlyOnceWith (
252+ "anthropic" ,
253+ "claude-sonnet-4-6" ,
254+ "anthropic-messages" ,
255+ ) ;
256+ expect ( hoisted . readLastCacheTtlTimestamp ) . toHaveBeenCalledExactlyOnceWith (
257+ input . sessionManager ,
258+ {
259+ provider : "anthropic" ,
260+ modelId : "claude-sonnet-4-6" ,
261+ } ,
262+ ) ;
196263 await input . activeSession . agent . transformContext ?.( messages , new AbortController ( ) . signal ) ;
197264 const firstTool = engineMessages ?. find ( ( message ) => message . role === "toolResult" ) ;
198265 expect ( firstTool ?. content [ 0 ] ) . toMatchObject ( {
0 commit comments