You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by "davsclaus (via GitHub)" <gi...@apache.org> on 2026/10/16 03:00:27 UTC
[PR] CAMEL-25398: camel-jbang - camel ask, MCP tools and the monitor surface CLI failures to agents [camel]
davsclaus opened a new pull request, #27931:
URL: https://github.com/apache/camel/pull/27931
Step 10 of [CAMEL-25388](https://issues.apache.org/jira/browse/CAMEL-25388): an AI agent that drives the Camel CLI, through `camel ask`, `camel mcp` or the monitor's MCP server, learns that something failed from the exit code or `isError`, not by reading prose.
### CliRunner (one way to run a command in this JVM)
- `CliRunner.run(args[, timeout])` returns `CliResult(exitCode, stdout, stderr, elapsedMs, timedOut)`: the printer of the CLI and picocli's out and err are captured apart, and restored afterwards. A command that does not finish in time is interrupted and gets `124`.
- One lock (`CliRunner.LOCK`, the `CamelJBangMain.class` monitor the runners already used): `camel ask`, the AI panel of the monitor, and now also each command of the camel shell (`CamelShellCommandRegistry` wraps its commands), as they share the printer of the CLI.
- The AI panel's executor uses it, so a command's help and usage errors are captured too (picocli out went to the real stdout under the monitor).
### camel ask `cli_exec`
- Returns `{"exitCode", "stdout", "stderr", "elapsedMs", "timedOut"}` with a 60 second timeout. Before, the exit code was dropped whenever the command printed something.
- Refuses a command that runs until it is stopped, with the form that returns: `run`/`dev` without `--background` or a `--max-*` limit, `infra run` without `--background`, `log`/`infra log` (and `trace`/`cmd receive --action=dump`) without `--follow=false` or `--duration`, `--watch` without `--duration`, `debug`, `shell`, `tui`, `nano`, `edit`, `ask`, `hawtio`; `export --clean-dir` needs `--yes` (its prompt cannot be answered there). A command that changes something is still asked first (CAMEL-25676), and without a terminal it needs `camel ask --yes`.
### Tool failures are errors
- `camel_run` with `status: failed` throws, so MCP answers `isError` (the JSON with the exit code and output is the error text).
- `IntegrationLauncher`: a restart that fails or cannot start, and `kill` of a process that is gone, throw.
- `ToolContext.executeAction` throws when the integration answers with an error (the 4.24 connector answers failures since CAMEL-25390) or does not answer; before, the timeout text came back as a normal answer.
- New `ToolContext.sendAction` (and `ActionClient.send(pid, ...)`) for actions without a result: route stop/start/suspend/resume and `camel_control stop-routes/start-routes/reset-stats/reload` wait for the confirmation, and an integration before 4.24 takes the action unconfirmed, as `camel cmd` does.
- `camel_runtime_ai_trace` no longer turns a failed query into "no AI components".
- The monitor: `executeShared` and the summary write rethrow tool errors, so its MCP server sets `isError`; only an unknown tool is "Unknown tool" (any `IllegalArgumentException` inside a tool was reported as an unknown tool, also in the AI panel). A failed `tui_run_example` is a tool error.
### The monitor's launches
- A restarted Camel Main integration is watched like a launch: a failed restart shows why, and quitting the monitor stops it (the process was dropped, and its log went to the public temp dir). `camel_control restart` waits up to 30 seconds for the outcome; a failed start is a tool error with the end of the log.
- A launch counts as started when the status file of the integration, or of a process it started (the app of a Maven launch), says Camel has started. The log is still read for a start that fails while the JVM lives on (Spring Boot `APPLICATION FAILED TO START`, a Maven build failure, Quarkus dev mode) and for an app without the CLI connector, which writes no status file, so log scraping is not removed entirely.
### External commands
- `docker info` / `podman info` (`camel doctor`, the monitor's doctor and launches) and the Ollama monitor's `ps`/`nvidia-smi`/`ioreg` read the output to the end and then waited, so the timeout never applied (`camel doctor` had none). `ExternalCommand` discards the output or reads it while the command runs, and destroys a command that does not finish in time.
### Docs
- 4.24 upgrade guide: "the AI tools report failures as errors".
- `camel-jbang-ai.adoc`: what the model gets from a `camel` command, and which commands it does not run.
### Tests
- `CliRunnerTest`: output and exit code, not found, picocli usage error and help, a command interrupted after the timeout (124), the JSON.
- `AskToolsTest`: the blocking commands and their hints, and the refusal through `cli_exec`.
- `ExternalCommandTest`: exit codes, a hanging command destroyed in time, output larger than a pipe holds.
- `TuiMcpServerToolErrorTest`: a tool error and an unknown tool answer `isError`, a normal answer does not.
- `LaunchManagerOutcomeTest`: started from the status file, failed from the exit code with the log in the restart error, the phases of a started Camel.
- `TuiToolRegistryEvalExpressionTest` expects the tool error instead of the `Error:` text.
- `mvn test` of camel-jbang-core, camel-jbang-plugin-tui and camel-jbang-mcp: all tests pass.
_Claude Code on behalf of davsclaus_
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_01DDRLJAyf8cDXSg8iX785My
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] CAMEL-25398: camel-jbang - camel ask, MCP tools and the monitor surface CLI failures to agents [camel]
Posted by "davsclaus (via GitHub)" <gi...@apache.org>.
davsclaus commented on code in PR #27931:
URL: https://github.com/apache/camel/pull/27931#discussion_r4290651433
##########
dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/AiCliCommandExecutor.java:
##########
@@ -110,7 +107,7 @@ private void execute(ActiveCommand command) {
int exitCode = 1;
boolean interrupted = false;
try {
- synchronized (CamelJBangMain.class) {
+ synchronized (CliRunner.LOCK) {
Review Comment:
Keeping it: the outer lock is not only around `CliRunner.run`. The default invoker (`invoke`) first switches the selected process (`EnvironmentHelper.setSelectedProcess`, static state that `camel cmd send` reads) and restores it afterwards. Holding the lock across the switch, the command and the restore makes them one unit; without it, a command of `camel ask` or the camel shell could run in between with the selected process of the AI panel. The inner acquisition in `CliRunner` is reentrant. It is also what `AiCliCommandExecutorTest` relies on when it holds the lock to keep a command waiting.
(For the record, `stillRunning()` and `STOP_GRACE_MILLIS` are in ee4b692129e5, with the test of a command that ignores the interrupt.)
_Claude Code on behalf of davsclaus_
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] CAMEL-25398: camel-jbang - camel ask, MCP tools and the monitor surface CLI failures to agents [camel]
Posted by "gnodet-bot (via GitHub)" <gi...@apache.org>.
gnodet-bot commented on code in PR #27931:
URL: https://github.com/apache/camel/pull/27931#discussion_r4290546661
##########
dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CliRunner.java:
##########
@@ -0,0 +1,187 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.dsl.jbang.core.commands;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.util.List;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.FutureTask;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+import org.apache.camel.dsl.jbang.core.common.CliExitCode;
+import org.apache.camel.dsl.jbang.core.common.Printer;
+import org.apache.camel.dsl.jbang.core.common.StringPrinter;
+import org.apache.camel.util.json.JsonObject;
+import picocli.CommandLine;
+
+/**
+ * Runs a Camel CLI command in this JVM, as camel ask and the TUI do, and gives what a script running camel gets: the
+ * exit code, and stdout and stderr apart. The commands share the printer of {@link CamelJBangMain} and the picocli
+ * command tree, so one command runs at a time: the camel shell takes the same {@link #LOCK}.
+ */
+public final class CliRunner {
+
+ /**
+ * The lock of the commands that run in this JVM.
+ */
+ public static final Object LOCK = CamelJBangMain.class;
+
+ private CliRunner() {
+ }
+
+ /**
+ * What a command did.
+ *
+ * @param exitCode the exit code, as camel exits with it ({@link CliExitCode#TIMEOUT} when it did not finish in
+ * time)
+ * @param stdout the output of the command
+ * @param stderr the errors, warnings and tips
+ * @param elapsedMs how long the command ran
+ * @param timedOut whether the command did not finish in time, and was interrupted
+ */
+ public record CliResult(int exitCode, String stdout, String stderr, long elapsedMs, boolean timedOut) {
+
+ public boolean isSuccess() {
+ return exitCode == CliExitCode.OK;
+ }
+
+ /**
+ * The result as JSON, for an AI agent: exitCode, stdout, stderr, elapsedMs and timedOut, where stdout and
+ * stderr are cut at the given number of characters.
+ */
+ public JsonObject toJson(int maxChars) {
+ JsonObject answer = new JsonObject();
+ answer.put("exitCode", exitCode);
+ answer.put("stdout", cut(stdout, maxChars));
+ answer.put("stderr", cut(stderr, maxChars));
+ answer.put("elapsedMs", elapsedMs);
+ answer.put("timedOut", timedOut);
+ return answer;
+ }
+
+ private static String cut(String text, int maxChars) {
+ return text.length() > maxChars ? text.substring(0, maxChars) + "\n... (truncated)" : text;
+ }
+ }
+
+ /**
+ * Runs the command, and waits until it has finished.
+ *
+ * @param args the command line without camel, such as <tt>get route --json</tt>
+ */
+ public static CliResult run(List<String> args) {
+ return run(args, 0);
+ }
+
+ /**
+ * Runs the command, and interrupts it when it does not finish in time.
+ *
+ * @param args the command line without camel, such as <tt>get route --json</tt>
+ * @param timeoutMillis how long the command may run, or 0 to wait until it has finished
+ */
+ public static CliResult run(List<String> args, long timeoutMillis) {
+ CommandLine commandLine = CamelJBangMain.getCommandLine();
+ if (commandLine == null) {
+ throw new IllegalStateException("The Camel CLI is not initialized");
+ }
+ Output output = new Output();
+ long start = System.nanoTime();
+ if (timeoutMillis <= 0) {
+ int code = execute(commandLine, args, output);
+ return output.result(code, elapsed(start), false);
+ }
+
+ FutureTask<Integer> task = new FutureTask<>(() -> execute(commandLine, args, output));
+ Thread thread = new Thread(task, "camel-cli-runner");
+ thread.setDaemon(true);
+ thread.start();
+ try {
+ int code = task.get(timeoutMillis, TimeUnit.MILLISECONDS);
+ return output.result(code, elapsed(start), false);
+ } catch (TimeoutException e) {
+ task.cancel(true);
Review Comment:
⚠️ **Potential lock starvation on timeout**
`task.cancel(true)` sends an interrupt, but `execute()` holds `synchronized (LOCK)` for the entire duration of `commandLine.execute(...)`. If the picocli command is blocked on non-interruptible I/O or a native call that ignores interrupts, the daemon thread keeps holding `LOCK` until the I/O unblocks — which could be never. Every subsequent CLI command (e.g. from the TUI) calling `execute()` will block waiting on `LOCK`, effectively hanging the shell.
Consider adding a `thread.join(someGraceMillis)` after the cancel and logging a warning if the thread is still alive, so operators can detect a stuck lock.
##########
dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/TuiToolRegistry.java:
##########
@@ -1868,6 +1874,8 @@ String callRunExample(Map<String, Object> args) throws Exception {
if (outcome.log() != null) {
result.put("log", outcome.log());
}
+ // a tool error (MCP isError), which still has the log that tells why
+ throw new ToolExecutionException(Jsoner.serialize(result));
Review Comment:
⚠️ **`callRunExample` throws JSON inside `ToolExecutionException`, which gets `"Error: "` prepended**
`throw new ToolExecutionException(Jsoner.serialize(result))` stores a serialized JSON object as the exception message. `TuiMcpServer.handleToolsCall` catches this as `Exception e` at line 552–553 and sets `text = "Error: " + e.getMessage()`, producing a string like `Error: {"status":"failed",...}`. That is not valid JSON, so any MCP client that tries to parse the tool error content as JSON (e.g. to extract `log`) will fail.
The intent seems to be that `ToolExecutionException` should be caught separately and its message used directly as `text` (with `isError = true`) — the same way `UnknownToolException` is caught at line 548. Add a `catch (ToolExecutionException e)` before the generic `catch (Exception e)` block in `handleToolsCall` that sets `text = e.getMessage()` without the prefix.
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] CAMEL-25398: camel-jbang - camel ask, MCP tools and the monitor surface CLI failures to agents [camel]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #27931:
URL: https://github.com/apache/camel/pull/27931#issuecomment-6207623503
<!-- ci-tested-modules -->
:test_tube: **CI tested the following changed modules:**
- `docs`
- `dsl/camel-jbang/camel-jbang-core`
- `dsl/camel-jbang/camel-jbang-mcp`
- `dsl/camel-jbang/camel-jbang-plugin-tui`
---
<details><summary>:microscope: Scalpel shadow comparison — Scalpel: 5 of 705 tested, 8 compile-only — current: 5 all tested</summary>
[Maveniverse Scalpel](https://github.com/maveniverse/scalpel) detected **5 affected modules** (current approach: 5).
**Skip-tests mode would test 5 modules** (4 direct + 3 downstream), **skip tests for 8** (generated code, meta-modules)
<details><summary>Modules Scalpel would test (5)</summary>
- `camel-jbang-mcp` ← dsl/camel-jbang/camel-jbang-mcp/src/main/java/org/apache/camel/dsl/jbang/core/commands/mcp/AiTraceTools.java
- `camel-jbang-plugin-mcp` ← downstream of org.apache.camel:camel-jbang-core
- `camel-jbang-plugin-tui` ← dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/AiCliCommandExecutor.java, dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/AiPanel.java, dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/DoctorPopup.java, dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/LaunchManager.java, dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/McpFacade.java, dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/OllamaMonitor.java, dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/ProcessControl.java, dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/TuiFileTools.java, dsl/camel-jbang/camel-jba
ng-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/TuiMcpServer.java, dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/TuiToolRegistry.java, dsl/camel-jbang/camel-jbang-plugin-tui/src/test/java/org/apache/camel/dsl/jbang/core/commands/tui/LaunchManagerOutcomeTest.java, dsl/camel-jbang/camel-jbang-plugin-tui/src/test/java/org/apache/camel/dsl/jbang/core/commands/tui/TuiMcpServerToolErrorTest.java, dsl/camel-jbang/camel-jbang-plugin-tui/src/test/java/org/apache/camel/dsl/jbang/core/commands/tui/TuiToolRegistryEvalExpressionTest.java
- `camel-jbang-plugin-validate` ← downstream of org.apache.camel:camel-jbang-core
- `camel-launcher-container` ← downstream of org.apache.camel:camel-launcher
</details>
<details><summary>Modules with tests skipped (8)</summary>
- `camel-jbang-it`
- `camel-jbang-main`
- `camel-jbang-plugin-edit`
- `camel-jbang-plugin-generate`
- `camel-jbang-plugin-kubernetes`
- `camel-jbang-plugin-test`
- `camel-launcher`
- `coverage`
</details>
> :information_source: Shadow mode — Scalpel observes but does not affect test execution. [Learn more](https://github.com/maveniverse/scalpel)
</details>
:warning: **Some tests are disabled on GitHub Actions** (`@DisabledIfSystemProperty(named = "ci.env.name")`) and require manual verification:
- `dsl/camel-jbang/camel-jbang-core`: 2 test(s) disabled on GitHub Actions
- `dsl/camel-jbang/camel-jbang-mcp`: 2 test(s) disabled on GitHub Actions
:bulb: **Manual integration tests recommended:**
> You modified `dsl/camel-jbang/camel-jbang-core`. The related integration tests in `dsl/camel-jbang/camel-jbang-it` are excluded from CI. Consider running them manually:
> ```
> mvn verify -f dsl/camel-jbang/camel-jbang-it -Djbang-it-test
> ```
<details><summary>All tested modules (15 modules, 3m 19s total)</summary>
**Total reactor time:** 3m 19s
| Module | Duration | Status |
| --- | --- | --- |
| Camel :: JBang :: Plugin :: TUI | 59.4s | SUCCESS |
| Camel :: Launcher | 36.3s | SUCCESS |
| Camel :: JBang :: MCP | 31.9s | SUCCESS |
| Camel :: JBang :: Plugin :: Validate | 29.4s | SUCCESS |
| Camel :: Docs | 15.1s | SUCCESS |
| Camel :: JBang :: Plugin :: Kubernetes | 12.9s | SUCCESS |
| Camel :: JBang :: Plugin :: Testing | 6.7s | SUCCESS |
| Camel :: JBang :: Plugin :: MCP | 1.4s | SUCCESS |
| Camel :: JBang :: Plugin :: Generate | 1.3s | SUCCESS |
| Camel :: JBang :: Plugin :: Edit | 1.1s | SUCCESS |
| Camel :: Coverage | 1.0s | SUCCESS |
| Camel :: JBang :: Integration tests | 0.8s | SUCCESS |
| Camel :: JBang :: Main | 0.7s | SUCCESS |
| Camel :: Launcher :: Container | 0.6s | SUCCESS |
| Camel :: JBang :: Core | n/a | |
**Top 20 slowest modules:**
- `Camel :: JBang :: Plugin :: TUI` (59.4s)
- `Camel :: Launcher` (36.3s)
- `Camel :: JBang :: MCP` (31.9s)
- `Camel :: JBang :: Plugin :: Validate` (29.4s)
- `Camel :: Docs` (15.1s)
- `Camel :: JBang :: Plugin :: Kubernetes` (12.9s)
- `Camel :: JBang :: Plugin :: Testing` (6.7s)
- `Camel :: JBang :: Plugin :: MCP` (1.4s)
- `Camel :: JBang :: Plugin :: Generate` (1.3s)
- `Camel :: JBang :: Plugin :: Edit` (1.1s)
- `Camel :: Coverage` (1.0s)
- `Camel :: JBang :: Integration tests` (0.8s)
- `Camel :: JBang :: Main` (0.7s)
- `Camel :: Launcher :: Container` (0.6s)
</details>
---
:gear: [View full build and test results](https://github.com/apache/camel/actions/runs/38843321332)
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] CAMEL-25398: camel-jbang - camel ask, MCP tools and the monitor surface CLI failures to agents [camel]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #27931:
URL: https://github.com/apache/camel/pull/27931#issuecomment-6207053591
:star2: Thank you for your contribution to the Apache Camel project! :star2:
:robot: CI automation will test this PR automatically.
:camel: Apache Camel Committers, please review the following items:
* First-time contributors **require MANUAL approval** for the GitHub Actions to run
* You can use the command `/component-test (camel-)component-name1 (camel-)component-name2..` to request a test from the test bot although they are normally detected and executed by CI.
* You can label PRs using `skip-tests` and `test-dependents` to fine-tune the checks executed by this PR.
* Build and test logs are available in the summary page. **Only** [Apache Camel committers](https://camel.apache.org/community/team/#committers) have access to the summary.
:warning: Be careful when sharing logs. Review their contents before sharing them publicly.
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] CAMEL-25398: camel-jbang - camel ask, MCP tools and the monitor surface CLI failures to agents [camel]
Posted by "gnodet-bot (via GitHub)" <gi...@apache.org>.
gnodet-bot commented on code in PR #27931:
URL: https://github.com/apache/camel/pull/27931#discussion_r4290646170
##########
dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/AiCliCommandExecutor.java:
##########
@@ -110,7 +107,7 @@ private void execute(ActiveCommand command) {
int exitCode = 1;
boolean interrupted = false;
try {
- synchronized (CamelJBangMain.class) {
+ synchronized (CliRunner.LOCK) {
Review Comment:
⚠️ **Redundant outer lock — `CliRunner.run()` already acquires `LOCK` internally**
`invoke()` now calls `CliRunner.run(argv)` (the zero-timeout overload), which calls `execute(commandLine, args, output)` on the same thread — and that method opens `synchronized (LOCK)` itself. Java's intrinsic monitors are reentrant, so this compiles and runs correctly, but the outer `synchronized (CliRunner.LOCK)` here does not protect anything that `CliRunner` doesn't already protect: by the time this block exits, `CliRunner.run()` has already acquired and released the lock for the entire command execution.
The net effect is two nested lock acquisitions (reentrant, harmless) but zero added protection. Remove the outer `synchronized` to make the intent clear: the locking contract belongs to `CliRunner`, not to its callers.
```suggestion
exitCode = invoker.execute(command.request.argv(), new CapturePrinter(output));
```
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] CAMEL-25398: camel-jbang - camel ask, MCP tools and the monitor surface CLI failures to agents [camel]
Posted by "davsclaus (via GitHub)" <gi...@apache.org>.
davsclaus merged PR #27931:
URL: https://github.com/apache/camel/pull/27931
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] CAMEL-25398: camel-jbang - camel ask, MCP tools and the monitor surface CLI failures to agents [camel]
Posted by "davsclaus (via GitHub)" <gi...@apache.org>.
davsclaus commented on code in PR #27931:
URL: https://github.com/apache/camel/pull/27931#discussion_r4290590786
##########
dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CliRunner.java:
##########
@@ -0,0 +1,187 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.dsl.jbang.core.commands;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.util.List;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.FutureTask;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+import org.apache.camel.dsl.jbang.core.common.CliExitCode;
+import org.apache.camel.dsl.jbang.core.common.Printer;
+import org.apache.camel.dsl.jbang.core.common.StringPrinter;
+import org.apache.camel.util.json.JsonObject;
+import picocli.CommandLine;
+
+/**
+ * Runs a Camel CLI command in this JVM, as camel ask and the TUI do, and gives what a script running camel gets: the
+ * exit code, and stdout and stderr apart. The commands share the printer of {@link CamelJBangMain} and the picocli
+ * command tree, so one command runs at a time: the camel shell takes the same {@link #LOCK}.
+ */
+public final class CliRunner {
+
+ /**
+ * The lock of the commands that run in this JVM.
+ */
+ public static final Object LOCK = CamelJBangMain.class;
+
+ private CliRunner() {
+ }
+
+ /**
+ * What a command did.
+ *
+ * @param exitCode the exit code, as camel exits with it ({@link CliExitCode#TIMEOUT} when it did not finish in
+ * time)
+ * @param stdout the output of the command
+ * @param stderr the errors, warnings and tips
+ * @param elapsedMs how long the command ran
+ * @param timedOut whether the command did not finish in time, and was interrupted
+ */
+ public record CliResult(int exitCode, String stdout, String stderr, long elapsedMs, boolean timedOut) {
+
+ public boolean isSuccess() {
+ return exitCode == CliExitCode.OK;
+ }
+
+ /**
+ * The result as JSON, for an AI agent: exitCode, stdout, stderr, elapsedMs and timedOut, where stdout and
+ * stderr are cut at the given number of characters.
+ */
+ public JsonObject toJson(int maxChars) {
+ JsonObject answer = new JsonObject();
+ answer.put("exitCode", exitCode);
+ answer.put("stdout", cut(stdout, maxChars));
+ answer.put("stderr", cut(stderr, maxChars));
+ answer.put("elapsedMs", elapsedMs);
+ answer.put("timedOut", timedOut);
+ return answer;
+ }
+
+ private static String cut(String text, int maxChars) {
+ return text.length() > maxChars ? text.substring(0, maxChars) + "\n... (truncated)" : text;
+ }
+ }
+
+ /**
+ * Runs the command, and waits until it has finished.
+ *
+ * @param args the command line without camel, such as <tt>get route --json</tt>
+ */
+ public static CliResult run(List<String> args) {
+ return run(args, 0);
+ }
+
+ /**
+ * Runs the command, and interrupts it when it does not finish in time.
+ *
+ * @param args the command line without camel, such as <tt>get route --json</tt>
+ * @param timeoutMillis how long the command may run, or 0 to wait until it has finished
+ */
+ public static CliResult run(List<String> args, long timeoutMillis) {
+ CommandLine commandLine = CamelJBangMain.getCommandLine();
+ if (commandLine == null) {
+ throw new IllegalStateException("The Camel CLI is not initialized");
+ }
+ Output output = new Output();
+ long start = System.nanoTime();
+ if (timeoutMillis <= 0) {
+ int code = execute(commandLine, args, output);
+ return output.result(code, elapsed(start), false);
+ }
+
+ FutureTask<Integer> task = new FutureTask<>(() -> execute(commandLine, args, output));
+ Thread thread = new Thread(task, "camel-cli-runner");
+ thread.setDaemon(true);
+ thread.start();
+ try {
+ int code = task.get(timeoutMillis, TimeUnit.MILLISECONDS);
+ return output.result(code, elapsed(start), false);
+ } catch (TimeoutException e) {
+ task.cancel(true);
Review Comment:
Thanks, it was worse than detection: `synchronized` does not answer an interrupt either, so a command that timed out while it waited for the lock still ran once the other command ended. Fixed in the next commit:
- The caller marks the command as abandoned when it gives up; once the command gets the lock it returns without running (test: `config set` that timed out waiting for the lock sets nothing afterwards; it did before).
- After the interrupt the runner waits up to 2 seconds for the command to end, and the result says what is left: `it waited for another command that is still running, and does not run`, or `it has not stopped yet, and the next command waits until it has` (test: a command that ignores the interrupt).
The lock stays the `CamelJBangMain.class` monitor shared with the camel shell and the AI panel; a command that never ends still holds it, but it is no longer silent.
_Claude Code on behalf of davsclaus_
##########
dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/TuiToolRegistry.java:
##########
@@ -1868,6 +1874,8 @@ String callRunExample(Map<String, Object> args) throws Exception {
if (outcome.log() != null) {
result.put("log", outcome.log());
}
+ // a tool error (MCP isError), which still has the log that tells why
+ throw new ToolExecutionException(Jsoner.serialize(result));
Review Comment:
Fixed in the next commit: `handleToolsCall` catches `ToolExecutionException` before the generic `Exception` and answers with its message as is (`isError: true`), so the JSON of a failed `tui_run_example` stays JSON. Other exceptions keep the `Error: ` prefix. `TuiMcpServerToolErrorTest` now expects `expression is required` without the prefix.
_Claude Code on behalf of davsclaus_
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org