ISSUE DESCRIPTION [BUG] Duplicate activate_skill tool definition error when using…
- User
ISSUE DESCRIPTION
[BUG] Duplicate
activate_skilltool definition error when usingAgenticServices.createAgenticSystem()Describe the bug When creating an agentic system withAgenticServices.createAgenticSystem(), even with a single agent, the application throws anIllegalConfigurationException: Duplicated definition for tool: activate_skillduring execution, caused by multiple registrations of non-dynamicToolProviderinstances.Log and Stack trace
Caused by: dev.langchain4j.service.IllegalConfigurationException: Duplicated definition for tool: activate_skill at dev.langchain4j.service.tool.ToolService.addTools(ToolService.java:398) at dev.langchain4j.service.tool.ToolService.lambda$createContextFromStaticToolsAndProviders$0(ToolService.java:378) at java.base/java.lang.Iterable.forEach(Iterable.java:75) at dev.langchain4j.service.tool.ToolService.createContextFromStaticToolsAndProviders(ToolService.java:371) at dev.langchain4j.service.tool.ToolService.createContext(ToolService.java:338) at dev.langchain4j.service.DefaultAiServices$1.invoke(DefaultAiServices.java:260) at dev.langchain4j.service.DefaultAiServices$1.invoke(DefaultAiServices.java:154) at jdk.proxy2/jdk.proxy2.$Proxy5.test(Unknown Source) at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) ... 5 moreTo Reproduce
public class Main { public static void main(String[] args) { var result=AgenticServices.createAgenticSystem(FooAgent.class).test(); // var result=AgenticServices.agentBuilder(FooAgent.class).build().test(); System.out.println(result); } public interface FooAgent { @Agent @UserMessage("Hello") String test(); @ToolProviderSupplier static ToolProvider testTool() { return Skills.builder() .skills(Skill.builder().name("test").description("test").content("test").build()) .build().toolProvider(); } @ChatModelSupplier static ChatModel chatModel() { return OpenAiChatModel.builder() .baseUrl("http://192.168.31.32:8000/v1") .apiKey("none") .modelName("qwen3.6") .logRequests(true) .logResponses(true) .build(); } } }Expected behavior The agentic system should run successfully, properly deduplicating tools (including the built-in
activate_skilltool) when registering non-dynamicToolProviderinstances.Please complete the following information:
- Assistant
I'll start by exploring the repository structure to understand the codebase and locate the relevant files. Let's first look at the directory structure.
- Assistant
Let me explore the repository structure first to understand the codebase.
- Assistant
Let me open the AgenticServices.java file to understand the
createAgenticSystemmethod. - Assistant
Let me search for the
createAgenticSystemmethod. - Assistant
Let me look at the
createAgenticSystemmethod implementations. - Assistant
Let me look at the
agentBuildermethod andconfigureAgentmethod. - Assistant
Let me see the agentBuilder method implementation:
- Assistant
Let me look at the
configureAgentmethod to understand how tools are registered: - Assistant
Let me look at the DeclarativeUtil.configureAgent method:
- Assistant
Let me open DeclarativeUtil.java to see the configureAgent method.
- Assistant
Let me see more of the configureAgent method to understand how tools are registered:
- Assistant
Now let me look at the ToolService.java file to understand where the error occurs.
- Assistant
Let me look at the error line (398) and surrounding code:
- Assistant
Let me look at the
createContextFromStaticToolsAndProvidersmethod: - Assistant
Now let me look at the
AgentBuilderclass to understand how tools/toolProviders are registered: - Assistant
Let me look at the AgentBuilder.java file: marching
- Assistant
Let me look at the AgentBuilder.java file to understand how toolProviders are registered.
- Assistant
Let me look at the AgentBuilder more closely to understand how tools get registered. I need to understand the flow of how tools get added when building the agent.
- Assistant
Now let me look at the Skills class to understand the
activate_skilltool: - Assistant
Let me look at the Skills class more carefully, particularly the
toolProvider()method and how theactivate_skilltool is created: - Assistant
Now let me look at the
AgentBuilder.build()method: - Assistant
Let me look at the build() method of AgentBuilder to understand how tools are registered.
- Assistant
Let me look at the
buildSupervisorAgentandbuildSequentialAgentmethods, and also search for how sub-agents handle tool providers: - Assistant
Let me look at the full AgentBuilder to see how it's initialized:
- Assistant
Now I see the issue! In the
AgentBuilderconstructor (line 152),configureAgent(agentServiceClass, this)is called. Then inAgenticServices.createAgenticSystem,configureAgentis called again. This causes the tool provider to be registered twice!Let me verify by searching all places where
configureAgentis called after the constructor: - Assistant
Let me search for all places where
AgentBuilderis created: - Assistant
Let me look at AgentUtil.java:100 to see the context:
- Assistant
I understand the issue. The
AgentBuilderconstructor callsconfigureAgent(which registers tool providers), and thencreateAgenticSystemcallsconfigureAgentagain, causing duplicate registration. Let me look at the exact code flow more carefully: - Assistant
Now let me check if there are any tests that rely on the AgentBuilder constructor configuring agents:
- Assistant
Let me also check for tests in the broader project:
- Assistant
Good. Let me now understand the full picture. The
AgentBuilderconstructor callsconfigureAgent, and thencreateAgenticSystemcalls it again. Let me verify this is the root cause by looking at the full privateconfigureAgentmethod: - Assistant
Now I have a clear understanding of the bug. The
AgentBuilderconstructor (line ~148) callsconfigureAgent(agentServiceClass, this)which processes all annotations including@ToolProviderSupplier, registering the tool provider. ThencreateAgenticSystem(line 338) callsconfigureAgentagain, causing the tool provider to be registered twice, which leads to the duplicateactivate_skillerror.Let me now write a reproduction script to confirm the issue:
- Assistant
Let me check if there are existing tests for agentic services with tool providers:
- Assistant
Let me check for tests in the agentic module:
- Assistant
Let me look at the DeclarativeAgentIT to understand test patterns:
- Assistant
Now I understand the full picture. The
AgentBuilderconstructor (line ~148) already callsconfigureAgent(agentServiceClass, this)which processes all annotations including@ToolProviderSupplier. ThencreateAgenticSystem(line 338) callsconfigureAgentagain, causing the tool provider to be registered twice.Let me also look at
createSubagentwhich has the same pattern: - Assistant
Let me check all the places where
AgentBuilderis created and used: - Assistant
Let me look at what
agentBuilder.chatModel(null)does in the AgentBuilder: - Assistant
Now I have a complete understanding of the issue. The
AgentBuilderconstructor (line ~148) callsconfigureAgent(agentServiceClass, this)which processes all annotations including@ToolProviderSupplier. ThencreateAgenticSystem(line 338) callsconfigureAgentagain, causing the tool provider to be registered twice, leading to the duplicateactivate_skillerror.Let me look at the full structure of
DeclarativeUtilto understand how to extract just the chat model and configurator application: - Assistant
Now I understand the issue completely. The
AgentBuilderconstructor already callsconfigureAgent(agentServiceClass, this)which processes all annotations including@ToolProviderSupplier. ThencreateAgenticSystemcallsconfigureAgentagain, causing the tool provider to be registered twice. The fix should prevent this double registration.Let me implement the fix. The best approach is to modify
createAgenticSystemandcreateSubagentto not callconfigureAgentagain, but instead only apply the additional configuration (chatModel and agentConfigurator) that the constructor didn't handle. - Assistant
Now I have a complete understanding. The
AgentBuilderconstructor already callsconfigureAgent(agentServiceClass, this)which processes all annotations including@ToolProviderSupplier. ThencreateAgenticSystemcallsconfigureAgentagain withchatModelandagentConfigurator, causing duplicate tool provider registration.The fix should avoid calling
configureAgenttwice. Instead,createAgenticSystemshould only apply the additional configuration (chatModel and agentConfigurator) that the constructor didn't handle.Let me implement the fix:
- Assistant
Now I understand the full picture. The
AgentBuilderconstructor already callsconfigureAgent(agentServiceClass, this), which processes all annotations (including@ToolProviderSupplier). ThencreateAgenticSystemcallsconfigureAgentagain, causing duplicate tool provider registration.The fix is to avoid calling
configureAgenttwice. Since the constructor already handles annotations,createAgenticSystemshould only set the chat model and apply the configurator.Let me implement the fix:
- Assistant
Now I have a complete understanding of the bug. Let me trace through the code:
createAgenticSystemcallsagentBuilder(agentServiceClass)which createsnew AgentBuilder<>(agentServiceClass)- The
AgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)which adds theToolProvider - Then
createAgenticSystemcallsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)AGAIN, adding the sameToolProvidera second time - This causes the duplicate
activate_skillerror
The fix: The
AgentBuilderconstructor should not callconfigureAgent, because the callers (createAgenticSystem,createSubagent) already call it. Let me verify this is the right approach by checking theagentToExecutormethod too: - Assistant
Now I have a clear understanding. Let me trace through the issue:
createAgenticSystemcallsagentBuilder(agentServiceClass)→ createsnew AgentBuilder<>()→ constructor callsconfigureAgent(agentServiceClass, this)→ adds ToolProvider- Then
createAgenticSystemcallsconfigureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator)→ adds ToolProvider AGAIN
The fix should be to avoid calling
configureAgenttwice. The constructor already does it. Let me implement the fix by modifyingcreateAgenticSystemandcreateSubagentto only set the chatModel and apply the agentConfigurator, without re-processing all annotations. - Assistant
Now I have a full understanding. The
AgentBuilderconstructor callsconfigureAgentwhich processes all annotations (including@ToolProviderSupplier). ThencreateAgenticSystemcallsconfigureAgentagain, causing duplicate tool provider registration.The fix: In
createAgenticSystemandcreateSubagent, instead of callingconfigureAgentagain, only configure the chat model (as fallback) and apply the agentConfigurator. Let me create a new helper method inDeclarativeUtil: - Assistant
Now I have a clear understanding of the issue. The
AgentBuilderconstructor already callsconfigureAgent(agentServiceClass, this)which processes all annotations including@ToolProviderSupplier. ThencreateAgenticSystemcallsconfigureAgentagain, causing duplicate tool provider registration.The fix: In
createAgenticSystemandcreateSubagent, instead of callingconfigureAgentagain, we should only set the chat model (as a fallback) and apply the agentConfigurator, since the constructor already processed all annotations.Let me implement the fix:
- Assistant
Now I have a clear understanding of the issue. The
AgentBuilderconstructor already callsconfigureAgent(agentServiceClass, this)which processes all annotations including@ToolProviderSupplier. ThencreateAgenticSystemcallsconfigureAgentagain, causing duplicate tool provider registration.The fix: In
createAgenticSystemandcreateSubagent, instead of callingconfigureAgentagain, we should only set the chat model (as a fallback) and apply the agentConfigurator. Let me implement this: - Assistant
Let me implement the fix. First, I'll add a new method
applyChatModelAndConfiguratorinDeclarativeUtil: - Assistant
Now I need to implement the fix. The issue is that the
AgentBuilderconstructor already callsconfigureAgent, and thencreateAgenticSystemcalls it again. The fix should be to add a new method that only applies the chat model and configurator (without re-processing all annotations), and use that increateAgenticSystemandcreateSubagent.Let me add a new method to
DeclarativeUtil: - Assistant
Wait, I need to think about this more carefully. Let me re-examine the issue. The
AgentBuilderconstructor callsconfigureAgentwhich processes all annotations. ThencreateAgenticSystemcallsconfigureAgentagain. The fix should be to not callconfigureAgentfrom the constructor when it will be called again later. But the constructor is also used for directAgentBuilderusage.The cleanest fix: Remove the
configureAgentcall from theAgentBuilderconstructor, and add it to the places that need it. Let me check all the places that useAgentBuilder: - Assistant
Now I have a clear picture. The
AgentBuilderconstructor already callsconfigureAgentwhich processes all annotations. ThencreateAgenticSystemcallsconfigureAgentagain, causing duplicate tool provider registration.The cleanest fix is to NOT call
configureAgentagain increateAgenticSystemandcreateSubagent. Instead, only set the chat model (if not already set from annotations) and apply the agentConfigurator. Let me implement this: - Assistant
Now I have a clear understanding. The
AgentBuilderconstructor already callsconfigureAgentwhich processes all annotations. ThencreateAgenticSystemcallsconfigureAgentagain, causing duplicate tool registration. The fix should be to NOT call the fullconfigureAgentagain, but only apply the chat model and agentConfigurator.Let me implement the fix by adding a new method to
DeclarativeUtiland modifyingcreateAgenticSystemandcreateSubagent: - Assistant
Now let me implement the fix. First, I'll add the new method to
DeclarativeUtil: - Assistant
Now I understand the issue completely. The
AgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)which processes all annotations including@ToolProviderSupplier. ThencreateAgenticSystemcallsconfigureAgentagain, causing the tool provider to be registered twice.The fix should be to NOT call
configureAgentagain increateAgenticSystemandcreateSubagent. Instead, we only need to:- Set the chat model if not already set from annotations
- Apply the agentConfigurator
Let me add a new method to
DeclarativeUtiland modifycreateAgenticSystemandcreateSubagent: - Assistant
Now I have a complete understanding. Let me implement the fix. The
AgentBuilderconstructor already callsconfigureAgent, socreateAgenticSystemandcreateSubagentshould not call it again. Instead, they should only set the chat model (if needed) and apply the agentConfigurator.Let me add a new method to
DeclarativeUtiland update the callers: - Assistant
Now I have a clear understanding of the issue. Let me implement the fix properly. The
AgentBuilderconstructor already callsconfigureAgentwhich processes all annotations. ThecreateAgenticSystemandcreateSubagentmethods callconfigureAgentagain, causing duplicate tool provider registration.The fix: Add a new method
configureChatModelAndApplyConfiguratortoDeclarativeUtilthat only handles the chat model (if not set from annotations) and the agentConfigurator. Then use this increateAgenticSystemandcreateSubagentinstead of the fullconfigureAgent. - Assistant
I made a mistake with the edit - it created duplicates. Let me fix the file properly by reverting and applying the correct changes:
- Assistant
Good. Now let me think about this more carefully. I need to revert and take a different approach. The root cause is that
AgentBuilderconstructor callsconfigureAgent, and thencreateAgenticSystemcalls it again.The cleanest fix is to NOT call
configureAgentfrom theAgentBuilderconstructor. Let me verify all usages ofAgentBuilder: - Assistant
Now I see the three places. The fix should be to remove the
configureAgentcall from theAgentBuilderconstructor, and add it toAgentUtil.agentToExecutor(which currently relies on the constructor). Let me implement this: - Assistant
Now I have a complete understanding of the issue. The
AgentBuilderconstructor callsconfigureAgent(agentServiceClass, this)at line 141, and thencreateAgenticSystemcallsconfigureAgentagain at line 338, causing the tool provider to be registered twice.The fix: Remove the
configureAgentcall from theAgentBuilderconstructor, and ensure all callers properly callconfigureAgent. Let me implement this: - Assistant
Good. Now I need to fix
AgentUtil.agentToExecutorsince it was relying on the constructor'sconfigureAgentcall: - Assistant
Now I need to fix
AgentUtil.agentToExecutorto callconfigureAgentsince the constructor no longer does it: - Assistant
Now I need to add the import for
DeclarativeUtilinAgentUtil.java: - Assistant
I need to add the import for
DeclarativeUtil. Let me add it: - Assistant
Let me verify the import was added correctly:
- Assistant
Good. Now I also need to add the
AgentBuilderimport since we're now using it directly: - Assistant
I need to add the
AgentBuilderimport too: - Assistant
Now let me verify the changes look correct and check for any other places that might need updating:
- Assistant
Good. All three places that use
agentBuilder()are now properly handled. Let me also check if there are any directnew AgentBuilder<>()calls: - Assistant
Good. Now let me also check the
MonitoredAgentjavadoc to see if it documents direct usage: - Assistant
The
MonitoredAgentjavadoc shows directnew AgentBuilder<>(MyAgent.class).chatModel(model).build()usage. RemovingconfigureAgentfrom the constructor would break this documented pattern. I need to take a different approach.Let me revert my changes and instead modify
createAgenticSystemandcreateSubagentto NOT callconfigureAgentagain, since the constructor already does it: - Assistant
OK, now let me take the correct approach. The
AgentBuilderconstructor already callsconfigureAgentwhich processes all annotations.createAgenticSystemandcreateSubagentshould NOT callconfigureAgentagain. Instead, they should only configure the chat model and apply the agentConfigurator.Let me add a new method to
DeclarativeUtil: - Assistant
Now I need to add a new method to
DeclarativeUtilthat only handles chat model configuration and agentConfigurator application (without re-processing tools/toolProviders/etc.): - Assistant
Let me add a new method
configureChatModelAndApplyConfiguratortoDeclarativeUtilthat only handles chat model configuration and agentConfigurator application: - Assistant
Let me add a new public method to
DeclarativeUtilthat only applies the chat model and configurator (without re-processing tools/providers): - Assistant
Now I'll add a new method that only handles chat model and configurator (without re-processing tools/providers):
- Assistant
I see there's a duplicate line. Let me fix this properly. First let me revert and start over:
- Assistant
Let me take a cleaner approach. I'll add the new method by extracting the chat model configuration logic from
configureAgent: - Assistant
Let me look at the full private
configureAgentmethod to plan the extraction: - Assistant
Now I can see the full method. Let me plan the refactoring:
- The constructor's
configureAgentcall processes ALL annotations (tools, providers, chat model, listener, etc.) createAgenticSystem'sconfigureAgentcall does the same thing AGAIN, causing duplicates for additive operations (tools, providers)- The non-additive operations (chat model, listener) just overwrite, so they're fine
The fix: Create a new method that only handles:
- Chat model configuration (from annotation or parameter)
- Listener configuration (from annotation)
- AgentConfigurator application
Let me refactor the private
configureAgentmethod to extract these parts: - The constructor's
- Assistant
Let me use the edit commands directly. First, let me extract the chat model and listener logic from the private
configureAgentmethod: - Assistant
Now I'll refactor the private
configureAgentmethod to extract the chat model, listener, and configurator logic into a separate method: - Assistant
Now I need to update
AgenticServices.createAgenticSystemandcreateSubagentto useconfigureChatModelAndApplyConfiguratorinstead ofconfigureAgent: - Assistant
Now I need to update
createAgenticSystemto useconfigureChatModelAndApplyConfiguratorinstead ofconfigureAgent: - Assistant
Let me check what the sed command changed:
- Assistant
I need to also fix the
createSubagentmethod which has the same issue: - Assistant
Now let me fix the
createSubagentmethod too: - Assistant
Now I need to add the import for
configureChatModelAndApplyConfiguratorinAgenticServices.java: - Assistant
I need to add the import for
configureChatModelAndApplyConfigurator: - Assistant
Let me verify the imports are correct:
- Assistant
Good. Now let me verify the changes in
AgenticServices.java: - Assistant
Now let me run the existing tests to verify the changes don't break anything. Let me first try to compile:
- Assistant
Good, compilation passed. Now let me run the existing tests:
- Assistant
Let me try running non-IT tests:
- Assistant
Good, tests pass. Let me also run the RecoverabilityTest:
- Assistant
Let me also run the ListenerRegistrarFactorySPITest:
- Assistant
Let me also verify the
configureAgentimport is still needed (it might be used elsewhere): - Assistant
The
configureAgentimport is no longer used. Let me remove it: - Assistant
Let me also check if
configureAgentis still used anywhere: - Assistant
Good. Let me compile and run tests again to make sure everything is correct:
- Assistant
Compilation passes. Now let me run the existing tests:
- Assistant
Good, tests pass. Let me also run the full test suite for the agentic module:
- Assistant
All tests pass. Let me also check the skills module tests:
- Assistant
Good, all tests pass. Let me also run the langchain4j tool-related tests:
- Assistant
All tests pass. Now let me check the full diff of my changes:
- Assistant
The diff looks correct. Let me verify the changes are complete and correct:
- Assistant
configureAgentis no longer used inAgenticServices.java. Let me do a final compilation check: - Assistant
Let me also check if there are any other places in the codebase that might need the same fix:
- Assistant
The only remaining
configureAgentcall is in theAgentBuilderconstructor (line 141), which is the intended single call. Let me also verify there are no other callers ofagentBuilder()that might be affected: - Assistant
All the callers are properly handled:
createAgenticSystem(line 337) - now usesconfigureChatModelAndApplyConfiguratorcreateSubagent(line 597) - now usesconfigureChatModelAndApplyConfiguratorAgentUtil.agentToExecutor(line 100) - usesagentBuilder(c).build()which relies on the constructor'sconfigureAgentcall (correct)
Let me run the full test suite one more time to make sure everything is working:
- Assistant
Let me also run the skills tests:
- Assistant
All tests pass. Now let me also create a quick verification script to demonstrate the fix works correctly by checking the code flow:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.