@@ -167,6 +167,220 @@ public void onComplete() {
167167 assertTrue (events .stream ().anyMatch (event -> event instanceof AgentEvent .MessageDelta ));
168168 }
169169
170+ // ------------------------------------------------------------------
171+ // issue #268: interceptor tool selection must gate disclosure AND execution
172+ // ------------------------------------------------------------------
173+
174+ @ Test
175+ void unfilteredRunDisclosesStaticAndDynamicTools () throws Exception {
176+ Tool staticTool = tool ("chosen" , "{}" , (ignored , context ) ->
177+ CompletableFuture .completedFuture (ToolResult .text ("static" )));
178+ Tool dynamicTool = tool ("dyn" , "{}" , (ignored , context ) ->
179+ CompletableFuture .completedFuture (ToolResult .text ("dynamic" )));
180+ ScriptedClient client = new ScriptedClient (List .of (response (ChatMessage .assistant ("answer" ), List .of (), 1 )));
181+ ReActAgent agent = ReActAgent .builder (client ).tools (new ToolRegistry (List .of (staticTool )))
182+ .dynamicTools (state -> new ToolRegistry (List .of (dynamicTool ))).build ();
183+
184+ agent .invoke (request (), context ()).get (3 , TimeUnit .SECONDS );
185+
186+ List <String > disclosed = client .requests .get (0 ).tools ().stream ()
187+ .map (ToolDefinition ::name ).toList ();
188+ assertEquals (List .of ("chosen" , "dyn" ), disclosed );
189+ }
190+
191+ @ Test
192+ void explicitEmptySelectionDisablesDisclosureAndExecution () throws Exception {
193+ ToolCall call = new ToolCall ("call-1" , "secret" , "{}" );
194+ ScriptedClient client = new ScriptedClient (List .of (response (ChatMessage .assistant ("try" , List .of (call )),
195+ List .of (call ), 1 ), response (ChatMessage .assistant ("blocked" ), List .of (), 1 )));
196+ ModelInterceptor interceptor = new ModelInterceptor () {
197+ public String getName () {
198+ return "disable-tools" ;
199+ }
200+
201+ public ModelResponse interceptModel (ModelRequest request , ModelCallHandler next ) {
202+ return next .call (ModelRequest .builder (request ).tools (List .of ()).build ());
203+ }
204+ };
205+ Tool secret = tool ("secret" , "{}" , (ignored , context ) ->
206+ CompletableFuture .completedFuture (ToolResult .text ("leaked" )));
207+ AgentOutcome outcome = ReActAgent .builder (client ).tools (new ToolRegistry (List .of (secret )))
208+ .interceptors (List .of (interceptor )).build ().invoke (request (), context ()).get (3 , TimeUnit .SECONDS );
209+
210+ // The model sees no tools at all.
211+ assertTrue (client .requests .get (0 ).tools ().isEmpty ());
212+ // A tool call for a filtered-out tool surfaces via the unknown-tool error path.
213+ assertEquals ("blocked" , outcome .messages ().get (outcome .messages ().size () - 1 ).text ());
214+ assertTrue (outcome .messages ().stream ()
215+ .anyMatch (message -> message .text ().contains ("Unknown tool: secret" )));
216+ }
217+
218+ @ Test
219+ void subsetSelectionDisclosesAndExecutesOnlyChosenTools () throws Exception {
220+ ToolCall call = new ToolCall ("call-1" , "chosen" , "{}" );
221+ ScriptedClient client = new ScriptedClient (List .of (response (ChatMessage .assistant ("use" , List .of (call )),
222+ List .of (call ), 1 ), response (ChatMessage .assistant ("done" ), List .of (), 1 )));
223+ ModelInterceptor interceptor = new ModelInterceptor () {
224+ public String getName () {
225+ return "subset" ;
226+ }
227+
228+ public ModelResponse interceptModel (ModelRequest request , ModelCallHandler next ) {
229+ return next .call (ModelRequest .builder (request ).tools (List .of ("chosen" )).build ());
230+ }
231+ };
232+ Tool chosen = tool ("chosen" , "{}" , (ignored , context ) ->
233+ CompletableFuture .completedFuture (ToolResult .text ("chosen result" )));
234+ Tool secret = tool ("secret" , "{}" , (ignored , context ) ->
235+ CompletableFuture .completedFuture (ToolResult .text ("leaked" )));
236+ AgentOutcome outcome = ReActAgent .builder (client ).tools (new ToolRegistry (List .of (chosen , secret )))
237+ .interceptors (List .of (interceptor )).build ().invoke (request (), context ()).get (3 , TimeUnit .SECONDS );
238+
239+ List <String > disclosed = client .requests .get (0 ).tools ().stream ()
240+ .map (ToolDefinition ::name ).toList ();
241+ assertEquals (List .of ("chosen" ), disclosed );
242+ assertTrue (outcome .messages ().stream ().anyMatch (message -> "chosen result" .equals (message .text ())));
243+ assertTrue (outcome .messages ().stream ().noneMatch (message -> "leaked" .equals (message .text ())));
244+ }
245+
246+ @ Test
247+ void dynamicToolsParticipateInSelection () throws Exception {
248+ Tool dynamic = tool ("dyn" , "{}" , (ignored , context ) ->
249+ CompletableFuture .completedFuture (ToolResult .text ("dynamic result" )));
250+ ScriptedClient client = new ScriptedClient (List .of (response (ChatMessage .assistant ("answer" ), List .of (), 1 )));
251+ ModelInterceptor interceptor = new ModelInterceptor () {
252+ public String getName () {
253+ return "dynamic-only" ;
254+ }
255+
256+ public ModelResponse interceptModel (ModelRequest request , ModelCallHandler next ) {
257+ return next .call (ModelRequest .builder (request ).tools (List .of ("dyn" )).build ());
258+ }
259+ };
260+ ReActAgent agent = ReActAgent .builder (client ).dynamicTools (state -> new ToolRegistry (List .of (dynamic )))
261+ .interceptors (List .of (interceptor )).build ();
262+
263+ agent .invoke (request (), context ()).get (3 , TimeUnit .SECONDS );
264+
265+ List <String > disclosed = client .requests .get (0 ).tools ().stream ()
266+ .map (ToolDefinition ::name ).toList ();
267+ assertEquals (List .of ("dyn" ), disclosed );
268+ }
269+
270+ @ Test
271+ void staticToolShadowsDynamicToolOnNameConflict () throws Exception {
272+ AtomicInteger staticExecutions = new AtomicInteger ();
273+ AtomicInteger dynamicExecutions = new AtomicInteger ();
274+ Tool staticTool = tool ("lookup" , "{}" , (ignored , context ) -> {
275+ staticExecutions .incrementAndGet ();
276+ return CompletableFuture .completedFuture (ToolResult .text ("original" ));
277+ });
278+ Tool dynamicTool = tool ("lookup" , "{}" , (ignored , context ) -> {
279+ dynamicExecutions .incrementAndGet ();
280+ return CompletableFuture .completedFuture (ToolResult .text ("replacement" ));
281+ });
282+ ToolCall call = new ToolCall ("call-1" , "lookup" , "{}" );
283+ ScriptedClient client = new ScriptedClient (List .of (response (ChatMessage .assistant ("use" , List .of (call )),
284+ List .of (call ), 1 ), response (ChatMessage .assistant ("done" ), List .of (), 1 )));
285+ ReActAgent agent = ReActAgent .builder (client ).tools (new ToolRegistry (List .of (staticTool )))
286+ .dynamicTools (state -> new ToolRegistry (List .of (dynamicTool ))).build ();
287+
288+ agent .invoke (request (), context ()).get (3 , TimeUnit .SECONDS );
289+
290+ // Static bindings shadow same-named dynamic disclosures (getTools() semantics).
291+ assertEquals (1 , staticExecutions .get ());
292+ assertEquals (0 , dynamicExecutions .get ());
293+ }
294+
295+ @ Test
296+ void streamingRunDisclosesTheSameFilteredSetAsNonStreaming () throws Exception {
297+ Tool secret = tool ("secret" , "{}" , (ignored , context ) ->
298+ CompletableFuture .completedFuture (ToolResult .text ("leaked" )));
299+ StreamingClient client = new StreamingClient ();
300+ ModelInterceptor interceptor = new ModelInterceptor () {
301+ public String getName () {
302+ return "disable-tools" ;
303+ }
304+
305+ public ModelResponse interceptModel (ModelRequest request , ModelCallHandler next ) {
306+ return next .call (ModelRequest .builder (request ).tools (List .of ()).build ());
307+ }
308+ };
309+ ReActAgent agent = ReActAgent .builder (client ).streamModel (true ).tools (new ToolRegistry (List .of (secret )))
310+ .interceptors (List .of (interceptor )).build ();
311+
312+ try (AgentExecutionContext context = context ()) {
313+ CompletableFuture <AgentOutcome > outcome = new CompletableFuture <>();
314+ agent .stream (request (), context ).subscribe (new Flow .Subscriber <>() {
315+ public void onSubscribe (Flow .Subscription subscription ) {
316+ subscription .request (Long .MAX_VALUE );
317+ }
318+
319+ public void onNext (AgentEvent event ) {
320+ if (event instanceof AgentEvent .Finished finished ) {
321+ outcome .complete (finished .outcome ());
322+ }
323+ }
324+
325+ public void onError (Throwable error ) {
326+ outcome .completeExceptionally (error );
327+ }
328+
329+ public void onComplete () {
330+ }
331+ });
332+ outcome .get (3 , TimeUnit .SECONDS );
333+ }
334+ assertTrue (client .requests .get (0 ).tools ().isEmpty ());
335+ }
336+
337+ @ Test
338+ void selectionDoesNotLeakAcrossSequentialRuns () throws Exception {
339+ ToolCall chosenCall = new ToolCall ("call-1" , "chosen" , "{}" );
340+ ScriptedClient client = new ScriptedClient (List .of (
341+ response (ChatMessage .assistant ("use" , List .of (chosenCall )), List .of (chosenCall ), 1 ),
342+ response (ChatMessage .assistant ("done" ), List .of (), 1 ),
343+ response (ChatMessage .assistant ("use" , List .of (chosenCall )), List .of (chosenCall ), 1 ),
344+ response (ChatMessage .assistant ("done" ), List .of (), 1 )));
345+ ModelInterceptor interceptor = new ModelInterceptor () {
346+ public String getName () {
347+ return "subset" ;
348+ }
349+
350+ public ModelResponse interceptModel (ModelRequest request , ModelCallHandler next ) {
351+ return next .call (ModelRequest .builder (request ).tools (List .of ("chosen" )).build ());
352+ }
353+ };
354+ Tool chosen = tool ("chosen" , "{}" , (ignored , context ) ->
355+ CompletableFuture .completedFuture (ToolResult .text ("chosen result" )));
356+ Tool secret = tool ("secret" , "{}" , (ignored , context ) ->
357+ CompletableFuture .completedFuture (ToolResult .text ("leaked" )));
358+ ReActAgent agent = ReActAgent .builder (client ).tools (new ToolRegistry (List .of (chosen , secret )))
359+ .interceptors (List .of (interceptor )).build ();
360+
361+ AgentOutcome first = agent .invoke (request (), context ()).get (3 , TimeUnit .SECONDS );
362+ AgentOutcome second = agent .invoke (request (), context ()).get (3 , TimeUnit .SECONDS );
363+
364+ for (AgentOutcome outcome : List .of (first , second )) {
365+ assertTrue (outcome .messages ().stream ().anyMatch (message -> "chosen result" .equals (message .text ())));
366+ assertTrue (outcome .messages ().stream ().noneMatch (message -> "leaked" .equals (message .text ())));
367+ }
368+ }
369+
370+ @ Test
371+ void unfilteredRunWithNoDynamicToolsKeepsRegistryFastPath () throws Exception {
372+ Tool staticTool = tool ("only" , "{}" , (ignored , context ) ->
373+ CompletableFuture .completedFuture (ToolResult .text ("ok" )));
374+ ScriptedClient client = new ScriptedClient (List .of (response (ChatMessage .assistant ("answer" ), List .of (), 1 )));
375+ ReActAgent agent = ReActAgent .builder (client ).tools (new ToolRegistry (List .of (staticTool ))).build ();
376+
377+ agent .invoke (request (), context ()).get (3 , TimeUnit .SECONDS );
378+
379+ List <String > disclosed = client .requests .get (0 ).tools ().stream ()
380+ .map (ToolDefinition ::name ).toList ();
381+ assertEquals (List .of ("only" ), disclosed );
382+ }
383+
170384 @ Test
171385 void deniedToolCanSuspendForHumanApproval () throws Exception {
172386 ToolCall call = new ToolCall ("call-1" , "danger" , "{}" );
0 commit comments