@@ -8,6 +8,7 @@ import { handleGemini } from "./gemini"
88import { handleOpenAIResponses } from "./openai-responses"
99import { runStrategies } from "../core/strategies"
1010import { accumulateGCStats } from "./gc-tracker"
11+ import { trimToolParametersCache } from "../state/tool-cache"
1112
1213export type { FetchHandlerContext , FetchHandlerResult , SynthPrompts } from "./types"
1314
@@ -55,6 +56,9 @@ export function installFetchWrapper(
5556 const inputUrl = typeof input === 'string' ? input : 'URL object'
5657 let modified = false
5758
59+ // Capture tool IDs before handlers run to track what gets cached this request
60+ const toolIdsBefore = new Set ( state . toolParameters . keys ( ) )
61+
5862 // Try each format handler in order
5963 // OpenAI Chat Completions & Anthropic style (body.messages)
6064 if ( body . messages && Array . isArray ( body . messages ) ) {
@@ -80,9 +84,14 @@ export function installFetchWrapper(
8084 }
8185 }
8286
83- // Run strategies after handlers have populated toolParameters cache
87+ // Run strategies when new tools are cached
88+ // We use all tool IDs for deduplication detection (to find duplicates across requests)
89+ // but pruning is session-scoped via state.prunedIds
8490 const sessionId = state . lastSeenSessionId
85- if ( sessionId && state . toolParameters . size > 0 ) {
91+ const toolIdsAfter = Array . from ( state . toolParameters . keys ( ) )
92+ const newToolsCached = toolIdsAfter . filter ( id => ! toolIdsBefore . has ( id ) ) . length > 0
93+
94+ if ( sessionId && newToolsCached && state . toolParameters . size > 0 ) {
8695 const toolIds = Array . from ( state . toolParameters . keys ( ) )
8796 const alreadyPruned = state . prunedIds . get ( sessionId ) ?? [ ]
8897 const alreadyPrunedLower = new Set ( alreadyPruned . map ( id => id . toLowerCase ( ) ) )
@@ -102,6 +111,9 @@ export function installFetchWrapper(
102111 accumulateGCStats ( state , sessionId , result . prunedIds , body , logger )
103112 }
104113 }
114+
115+ // Trim cache to prevent unbounded memory growth
116+ trimToolParametersCache ( state )
105117 }
106118
107119 if ( modified ) {
0 commit comments