gnodet opened a new pull request, #24992:
URL: https://github.com/apache/camel/pull/24992

   ## Summary
   
   Fix for [CAMEL-23944](https://issues.apache.org/jira/browse/CAMEL-23944): 
`ToolExecutionErrorHandler` and `compensateOnToolErrors` are never invoked when 
a Camel route tool fails in `camel-langchain4j-agent`.
   
   _Claude Code on behalf of gnodet_
   
   ### Root cause
   
   The `ToolExecutor` in `LangChain4jAgentProducer.createCamelToolProvider` had 
three bugs:
   
   1. **Missing exchange exception check** — after `process(exchange)`, the 
code did not check `exchange.getException()`. In Camel, route processors store 
exceptions on the exchange instead of rethrowing, so the exception was silently 
ignored and LangChain4j's error handlers never fired.
   
   2. **Exception swallowed as string** — the `catch` block returned an error 
string (`"Error executing tool '...': ..."`) instead of rethrowing, making 
LangChain4j treat the failure as a successful tool result.
   
   3. **Shared exchange** — the `ToolExecutor` reused the live producer 
exchange, leaking tool side-effects (headers, body, exceptions) into the 
calling exchange. This is inconsistent with `LangChain4jToolsProducer` which 
correctly creates `ExchangeHelper.createCopy()`.
   
   ### Fix
   
   - Create an isolated exchange copy (`ExchangeHelper.createCopy()`) for each 
tool invocation
   - Check `toolExchange.getException()` after processing and rethrow if present
   - Rethrow exceptions as `RuntimeException` instead of returning error 
strings, so LangChain4j's `ToolExecutionErrorHandler` / 
`compensateOnToolErrors` can fire
   
   ### Test
   
   Added `LangChain4jAgentToolExecutorErrorHandlingTest` with 4 test cases:
   - **`toolExecutorShouldThrowWhenCamelRouteToolFails`** — CAMEL-23944 
reproducer: verifies the ToolExecutor throws when a route tool fails
   - **`toolExecutorShouldReturnResultWhenCamelRouteToolSucceeds`** — 
regression: verifies success path still works
   - **`toolExecutorShouldPropagateExceptionMessageFromFailingRoute`** — 
verifies the original exception message is preserved
   - **`toolExecutionShouldNotLeakStateIntoCallingExchange`** — verifies 
exchange isolation
   
   ## Test plan
   
   - [x] New unit tests pass (4/4)
   - [x] Full module test suite passes (30/30)
   - [x] Code formatted with `formatter:format` and `impsort:sort`
   - [ ] CI pipeline green
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to