@@ -180,25 +180,25 @@ def _find_best(self, tool: str, text: str) -> Tuple[Optional[_Entry], float]:
180180
181181 vec = _vectorize (text )
182182 query_tokens = set (vec .keys ())
183-
183+
184184 # Fast path: use inverted index to find candidate entries
185185 # Only consider entries that share at least one token with query
186186 tool_index = self ._index .get (tool , {})
187187 candidate_indices : Set [int ] = set ()
188-
188+
189189 for token in query_tokens :
190190 if token in tool_index :
191191 candidate_indices .update (tool_index [token ])
192-
192+
193193 # If no candidates found via index, fall back to empty result
194194 if not candidate_indices :
195195 return None , 0.0
196-
196+
197197 # Compute similarity only for candidates
198198 best : Optional [_Entry ] = None
199199 best_sim = 0.0
200200 now = time .time ()
201-
201+
202202 for idx in candidate_indices :
203203 if idx >= len (bucket ):
204204 continue
@@ -209,7 +209,7 @@ def _find_best(self, tool: str, text: str) -> Tuple[Optional[_Entry], float]:
209209 if sim > best_sim :
210210 best = e
211211 best_sim = sim
212-
212+
213213 return best , best_sim
214214
215215 async def tool_pre_invoke (self , payload : ToolPreInvokePayload , context : PluginContext ) -> ToolPreInvokeResult :
@@ -267,34 +267,34 @@ async def tool_post_invoke(self, payload: ToolPostInvokePayload, context: Plugin
267267 vec = _vectorize (text )
268268 tokens = set (vec .keys ())
269269 entry = _Entry (text = text , vec = vec , value = payload .result , expires_at = time .time () + max (1 , int (self ._cfg .ttl )), tokens = tokens )
270-
270+
271271 bucket = self ._cache .setdefault (tool , [])
272272 entry_idx = len (bucket )
273273 bucket .append (entry )
274-
274+
275275 # Update inverted index for new entry
276276 tool_index = self ._index [tool ]
277277 for token in tokens :
278278 tool_index [token ].add (entry_idx )
279-
279+
280280 # Evict expired entries and rebuild index
281281 now = time .time ()
282282 # Filter out expired entries
283283 valid_entries = [e for e in bucket if e .expires_at > now ]
284-
284+
285285 # Cap size if needed
286286 if len (valid_entries ) > self ._cfg .max_entries :
287287 valid_entries = valid_entries [- self ._cfg .max_entries :]
288-
288+
289289 # Rebuild bucket and index if we removed or modified entries
290290 if len (valid_entries ) != len (bucket ):
291291 bucket .clear ()
292292 bucket .extend (valid_entries )
293-
293+
294294 # Rebuild inverted index for this tool
295295 self ._index [tool ].clear ()
296296 for new_idx , entry in enumerate (bucket ):
297297 for token in entry .tokens :
298298 self ._index [tool ][token ].add (new_idx )
299-
299+
300300 return ToolPostInvokeResult (metadata = {"approx_cache_stored" : True })
0 commit comments