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