This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch q4g-repeat in repository https://gitbox.apache.org/repos/asf/camel.git
commit 2b408c3b0eeeb24fc772e9b2adbd339cfc5480cf Author: Claus Ibsen <[email protected]> AuthorDate: Sun Dec 6 08:50:41 2026 +0100 chore: camel-jbang - a repeated doc read gets its YAML examples, then a tool error A note that is the same text each time did not stop a small model: in the local-model benchmark (qwen4, Dec 2026) 16 of 21 steps that ended at 25 tool calls without a write got the repeated note 5 to 24 times in a row, the identical call about once a second. - The first note of a doc answer with YAML examples gives those examples again (at most 3, each at most 2000 characters), to adapt and write. - From the next identical catalog read (camel_catalog_doc, camel_catalog_find, camel_catalog_sample), the note is refused: the MCP server answers it as a tool error that names the next call to make. Other deterministic tools keep the note. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y4dDi5iQNgtHrDdz2NpwF --- .../jbang/core/commands/ai/RepeatedToolCalls.java | 70 +++++++++++++++++++++ .../core/commands/ai/RepeatedToolCallsTest.java | 73 ++++++++++++++++++++++ .../jbang/core/commands/mcp/AuthoringTools.java | 6 ++ .../core/commands/mcp/AuthoringToolsTest.java | 21 +++++++ 4 files changed, 170 insertions(+) diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/RepeatedToolCalls.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/RepeatedToolCalls.java index 7f2b03dee99d..efafeb8956b7 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/RepeatedToolCalls.java +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/RepeatedToolCalls.java @@ -19,13 +19,18 @@ package org.apache.camel.dsl.jbang.core.commands.ai; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.HexFormat; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeMap; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.apache.camel.util.json.JsonArray; import org.apache.camel.util.json.JsonObject; /** @@ -42,6 +47,12 @@ import org.apache.camel.util.json.JsonObject; * with the next step: the {@link ToolDescriptor#repeatHint() hint} of the tool, such as writing the file, whose checks * say what to change, or else to go on with the task. * <p> + * A note that is the same text each time did not stop a small model either. In the local-model benchmark (qwen4, Dec + * 2026) 16 of 21 steps that ended at their 25 tool calls without a write got the note 5 to 24 times in a row: the model + * sent the identical call about once a second. So the first note of a doc answer with YAML examples gives those + * examples again, short and ready to adapt, and from the next identical catalog read the note is {@link #REFUSED + * refused}: a client that can, answers it as a tool error, which looks different from every answer before it. + * <p> * One instance tracks one session: a client connection, or a conversation of an AI panel. Call {@link #reset()} when a * new session starts on the same instance. */ @@ -56,6 +67,21 @@ public final class RepeatedToolCalls { /** The next step the note starts with when the tool has no {@link ToolDescriptor#repeatHint() hint} of its own. */ static final String NEXT_STEP = "Go on with the next step of your task."; + /** + * The key of a note for a catalog read that was asked again after the note with its YAML examples, or without them: + * the call is refused, and the MCP server answers it as a tool error. + */ + public static final String REFUSED = "refused"; + + /** The YAML examples of a doc answer that the first note gives again, at most this many. */ + static final int MAX_EXAMPLES = 3; + + /** A YAML example longer than this is left out of the note: it is not the short example a model can adapt. */ + static final int MAX_EXAMPLE_LENGTH = 2000; + + private static final Pattern YAML_EXAMPLE = Pattern.compile("\\[source,yaml]\\s*\\R(-{4,})\\R(.*?)\\R\\1(?=\\R|$)", + Pattern.DOTALL); + /** Guards against unbounded growth over a very long session; the oldest counts are simply forgotten. */ private static final int MAX_TRACKED = 1000; @@ -82,6 +108,9 @@ public final class RepeatedToolCalls { /** The calls whose answer was an error given as data, such as a name the catalog has not got, with that answer. */ private final Map<String, JsonObject> misses = new HashMap<>(); + /** The YAML examples of the doc answers given in full, by call. */ + private final Map<String, List<String>> examples = new HashMap<>(); + /** * Returns the short note to answer with instead of running the tool, or null when the tool should run and answer in * full: a tool that is not deterministic, or one of the first {@value #FULL_ANSWERS} identical calls. When this @@ -95,6 +124,7 @@ public final class RepeatedToolCalls { if (counts.size() >= MAX_TRACKED) { counts.clear(); misses.clear(); + examples.clear(); } // Peek at the current count without incrementing. The increment happens only after a successful answer // (recordSuccessfulAnswer), so a call that throws an exception does not consume the retry budget. @@ -131,6 +161,22 @@ public final class RepeatedToolCalls { } return note; } + List<String> yaml = examples.get(key); + if (times == FULL_ANSWERS + 1 && yaml != null) { + note.put("yamlExamples", new JsonArray(yaml)); + note.put("note", "The answer is the same as before, so here are only its YAML examples: adapt one of them and" + + " write the file with camel_write_file, which checks the options against the catalog." + + " You asked this " + times + " times now with the same arguments."); + return note; + } + if (times > FULL_ANSWERS + 1 && CATALOG_READS.contains(tool.name())) { + note.put(REFUSED, true); + note.put("note", "Not answered again: you asked " + tool.name() + " this " + times + " times with the same" + + " arguments, and the answer does not change. Your next call should be camel_write_file" + + " or camel_edit_file with the file of your task: the write checks the options against" + + " the catalog, and its answer says what to change."); + return note; + } // the next step first, then the fact; the count is timesAsked, this call included (CAMEL-26875) note.put("note", (tool.repeatHint() != null ? tool.repeatHint() : NEXT_STEP) + " You asked this " + times + " times now with the same arguments, and the answer is the same each time: it is in your" @@ -161,6 +207,7 @@ public final class RepeatedToolCalls { if (counts.size() >= MAX_TRACKED) { counts.clear(); misses.clear(); + examples.clear(); } String key = key(tool.name(), args, tool.deterministicWhen()); counts.merge(key, 1, Integer::sum); @@ -169,6 +216,12 @@ public final class RepeatedToolCalls { } else { misses.remove(key); } + List<String> yaml = answer != null && answer.get("doc") instanceof String doc ? yamlExamples(doc) : List.of(); + if (yaml.isEmpty()) { + examples.remove(key); + } else { + examples.put(key, yaml); + } } /** @@ -201,9 +254,26 @@ public final class RepeatedToolCalls { public synchronized void reset() { counts.clear(); misses.clear(); + examples.clear(); readsSinceWrite = 0; } + /** + * The YAML examples of an AsciiDoc text ({@code [source,yaml]} blocks), the first {@value #MAX_EXAMPLES} that are + * not longer than {@value #MAX_EXAMPLE_LENGTH} characters. + */ + static List<String> yamlExamples(String doc) { + List<String> answer = new ArrayList<>(); + Matcher m = YAML_EXAMPLE.matcher(doc); + while (m.find() && answer.size() < MAX_EXAMPLES) { + String yaml = m.group(2).strip(); + if (!yaml.isEmpty() && yaml.length() <= MAX_EXAMPLE_LENGTH) { + answer.add(yaml); + } + } + return answer; + } + /** * The tool name with its arguments in name order, leaving out the empty ones: a model that sends an optional * argument as an empty string is asking the same question as one that leaves it out. diff --git a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ai/RepeatedToolCallsTest.java b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ai/RepeatedToolCallsTest.java index e8d5e362df7c..35637e24e8b2 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ai/RepeatedToolCallsTest.java +++ b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ai/RepeatedToolCallsTest.java @@ -59,8 +59,81 @@ class RepeatedToolCallsTest { // a page that was cut was not answered in full: the note says how to get the sections it left out assertThat(note.getString("note")).doesNotContain("in full").contains("docPage=<section title>"); + // from the fourth, a catalog read is refused: the MCP server answers it as a tool error JsonObject fourth = calls.repeatOf(catalogDoc, sql); assertThat(fourth.getInteger("timesAsked")).isEqualTo(4); + assertThat(fourth.getBoolean(RepeatedToolCalls.REFUSED)).isTrue(); + assertThat(fourth.getString("note")) + .startsWith("Not answered again: you asked camel_catalog_doc this 4 times with the same arguments") + .contains("Your next call should be camel_write_file or camel_edit_file"); + } + + @Test + void theFirstNoteOfADocAnswerGivesItsYamlExamplesAgain() { + // qwen4 (Dec 2026) asked one Rest DSL section 18 times in a row: the note gives what it can adapt and write + Map<String, String> section = Map.of("name", "rest-dsl", "docPage", "Binding to POJOs using data formats"); + JsonObject answer = new JsonObject(); + answer.put("doc", """ + == Binding to POJOs using data formats + + [tabs] + ==== + Java:: + + + [source,java] + ---- + rest("/users/").post().type(UserPojo.class); + ---- + + YAML:: + + + [source,yaml] + ---- + - rest: + post: + - type: com.foo.UserPojo + ---- + ==== + """); + for (int i = 0; i < 2; i++) { + assertThat(calls.repeatOf(catalogDoc, section)).isNull(); + calls.recordSuccessfulAnswer(catalogDoc, section, answer); + } + + JsonObject note = calls.repeatOf(catalogDoc, section); + assertThat(note.getBoolean("repeated")).isTrue(); + assertThat(note.get(RepeatedToolCalls.REFUSED)).isNull(); + assertThat((List<Object>) note.get("yamlExamples")) + .containsExactly("- rest:\n post:\n - type: com.foo.UserPojo"); + assertThat(note.getString("note")).startsWith("The answer is the same as before, so here are only its YAML" + + " examples: adapt one of them and write the file"); + + JsonObject fourth = calls.repeatOf(catalogDoc, section); + assertThat(fourth.getBoolean(RepeatedToolCalls.REFUSED)).isTrue(); + assertThat(fourth.get("yamlExamples")).isNull(); + } + + @Test + void onlyShortYamlExamplesAreKeptAndAtMostThree() { + String block = "[source,yaml]\n----\n- route: %s\n----\n"; + String doc = "[source,java]\n----\nfrom(\"a\");\n----\n" + block.formatted("one") + + block.formatted("x".repeat(RepeatedToolCalls.MAX_EXAMPLE_LENGTH)) + block.formatted("two") + + block.formatted("three") + block.formatted("four"); + assertThat(RepeatedToolCalls.yamlExamples(doc)) + .containsExactly("- route: one", "- route: two", "- route: three"); + assertThat(RepeatedToolCalls.yamlExamples("no examples here")).isEmpty(); + } + + @Test + void aToolThatIsNotACatalogReadIsNotRefused() { + ToolDescriptor eval = ToolRegistry.findTool("camel_eval_expression"); + Map<String, String> expr = Map.of("expression", "${body}", "body", "hello"); + assertThat(eval.isDeterministic(expr)).isTrue(); + successfulCall(eval, expr); + successfulCall(eval, expr); + calls.repeatOf(eval, expr); + JsonObject fourth = calls.repeatOf(eval, expr); + assertThat(fourth.get(RepeatedToolCalls.REFUSED)).isNull(); assertThat(fourth.getString("note")).contains("You asked this 4 times now"); } diff --git a/dsl/camel-jbang/camel-jbang-mcp/src/main/java/org/apache/camel/dsl/jbang/core/commands/mcp/AuthoringTools.java b/dsl/camel-jbang/camel-jbang-mcp/src/main/java/org/apache/camel/dsl/jbang/core/commands/mcp/AuthoringTools.java index 61faf7080797..1e52cb20f267 100644 --- a/dsl/camel-jbang/camel-jbang-mcp/src/main/java/org/apache/camel/dsl/jbang/core/commands/mcp/AuthoringTools.java +++ b/dsl/camel-jbang/camel-jbang-mcp/src/main/java/org/apache/camel/dsl/jbang/core/commands/mcp/AuthoringTools.java @@ -39,6 +39,7 @@ import io.quarkiverse.mcp.server.ToolCallException; import io.quarkiverse.mcp.server.ToolGuardrails; import org.apache.camel.catalog.CamelCatalog; import org.apache.camel.dsl.jbang.core.commands.ai.CatalogDocs; +import org.apache.camel.dsl.jbang.core.commands.ai.RepeatedToolCalls; import org.apache.camel.dsl.jbang.core.commands.ai.ToolContext; import org.apache.camel.dsl.jbang.core.commands.ai.ToolExecutionException; import org.apache.camel.dsl.jbang.core.commands.ai.ToolRegistry; @@ -559,6 +560,11 @@ public class AuthoringTools { String readNote = repeatedCalls != null ? repeatedCalls.readNote(connection, tool) : null; JsonObject repeat = repeatedCalls != null ? repeatedCalls.repeatOf(connection, tool, args) : null; if (repeat != null) { + // a catalog read asked again after its note: a tool error, which looks different from every answer before + // it (a note that is the same text each time did not stop a small model) + if (Boolean.TRUE.equals(repeat.get(RepeatedToolCalls.REFUSED))) { + throw new ToolCallException(repeat.getString("note")); + } return repeat; } JsonObject result = call(tool, args, catalogService, configure); diff --git a/dsl/camel-jbang/camel-jbang-mcp/src/test/java/org/apache/camel/dsl/jbang/core/commands/mcp/AuthoringToolsTest.java b/dsl/camel-jbang/camel-jbang-mcp/src/test/java/org/apache/camel/dsl/jbang/core/commands/mcp/AuthoringToolsTest.java index 8f409793f5ed..fd100d979d47 100644 --- a/dsl/camel-jbang/camel-jbang-mcp/src/test/java/org/apache/camel/dsl/jbang/core/commands/mcp/AuthoringToolsTest.java +++ b/dsl/camel-jbang/camel-jbang-mcp/src/test/java/org/apache/camel/dsl/jbang/core/commands/mcp/AuthoringToolsTest.java @@ -318,6 +318,27 @@ class AuthoringToolsTest { .doesNotContain("camel_write_file"); } + @Test + void aFourthIdenticalCatalogCallOfAConnectionIsAToolError() { + // a note that is the same text each time did not stop a small model (qwen4, Dec 2026): from the fourth + // identical catalog read, the answer is a tool error, which looks different from every answer before it + AuthoringTools counted = new AuthoringTools(); + counted.repeatedCalls = new RepeatedCallSessions(); + McpConnection agent = connection("agent-1"); + for (int i = 0; i < 3; i++) { + counted.camel_catalog_doc(agent, "sql", null, null, null, null, null, null, null, null, null); + } + + assertThatThrownBy( + () -> counted.camel_catalog_doc(agent, "sql", null, null, null, null, null, null, null, null, null)) + .isInstanceOf(ToolCallException.class) + .hasMessageStartingWith("Not answered again: you asked camel_catalog_doc this 4 times") + .hasMessageContaining("Your next call should be camel_write_file or camel_edit_file"); + // another question is answered in full + assertThat(counted.camel_catalog_doc(agent, "timer", null, null, null, null, null, null, null, null, null) + .get("repeated")).isNull(); + } + @Test void aThirdIdenticalCatalogCallOfAConnectionGetsAShortNote() { AuthoringTools counted = new AuthoringTools();
