This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 18ee5f04bd43 CAMEL-26932: camel-jbang - the history of a camel ask 
chat stays bounded, and a question that fails leaves no trace in it (#30018)
18ee5f04bd43 is described below

commit 18ee5f04bd43dd6a9100d9c9ad91b3cc2bf468c0
Author: Claus Ibsen <[email protected]>
AuthorDate: Sun Nov 15 16:05:44 2026 +0100

    CAMEL-26932: camel-jbang - the history of a camel ask chat stays bounded, 
and a question that fails leaves no trace in it (#30018)
    
    …ded, and a question that fails leaves no trace in it
    
    camel ask kept every tool call and result of a chat in its history, in full 
(cli_exec returns up to 64 KB), and sent
    the whole history with every question; once it was larger than the context 
window of the model every later question
    failed, and the failed turn stayed in the history. The chat now bounds its 
history as the AI panel of the TUI does:
    a tool result is cut at 18,000 characters, after an answer the tool results 
of the questions before the previous one
    are shrunk, and the chat keeps the last 20 questions. A question that fails 
(the model is not reachable, or there is no
    answer within --max-iterations) is removed from the history, and the note 
on the mistakes of the answer before is
    kept for the next question.
    
    The history code of the TUI moved to camel-jbang-core (ChatHistory), which 
camel ask and the AI panel share.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../modules/ROOT/pages/camel-jbang-ai.adoc         |   5 +
 .../apache/camel/dsl/jbang/core/commands/Ask.java  |  30 ++++-
 .../camel/dsl/jbang/core/commands/ChatHistory.java | 127 +++++++++++++++++++++
 .../camel/dsl/jbang/core/commands/AskLoopTest.java |  56 +++++++++
 .../camel/dsl/jbang/core/commands/tui/AiPanel.java |  85 ++------------
 .../jbang/core/commands/tui/SourceSizeTest.java    |   2 +-
 6 files changed, 227 insertions(+), 78 deletions(-)

diff --git a/docs/user-manual/modules/ROOT/pages/camel-jbang-ai.adoc 
b/docs/user-manual/modules/ROOT/pages/camel-jbang-ai.adoc
index b86891d11c63..869f1828d9ba 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-jbang-ai.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-jbang-ai.adoc
@@ -29,6 +29,11 @@ camel ask
 This opens a `ask>` prompt where you can have a multi-turn conversation,
 with the AI maintaining context across questions.
 
+The history of the chat is sent with every question, so it is kept bounded, as 
in the AI panel of the TUI: a tool
+result is cut at 18,000 characters, once a question is answered the tool 
results of the questions before the previous
+one are shrunk to their start, and the chat keeps the last 20 questions. A 
question that fails (the model cannot be
+reached, or gives no answer within `--max-iterations`) leaves no trace in the 
history, so you can ask again.
+
 === What the AI can do
 
 The agent has access to tools that let it:
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Ask.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Ask.java
index 0373e1749d71..4c18c9dfd6b2 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Ask.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Ask.java
@@ -129,7 +129,7 @@ public class Ask extends CamelCommand {
     private String pendingNote;
 
     private long targetPid;
-    private AskTools askTools;
+    AskTools askTools;
     // the chat's reader, or one made on the terminal for the first call to 
confirm
     private LineReader confirmReader;
 
@@ -241,7 +241,31 @@ public class Ask extends CamelCommand {
         return 0;
     }
 
-    private int runAgentLoop(
+    /**
+     * Answers a question, with the history of the chat so far. The history 
stays bounded, as it is sent with every
+     * request: tool results are cut, and after an answer the older ones are 
shrunk and the oldest turns dropped (as in
+     * the AI panel of the TUI). A question that fails leaves no trace in the 
history: else the next request repeats the
+     * failed turn, and once the history is larger than the context window of 
the model every later question fails
+     * (CAMEL-26932).
+     */
+    int runAgentLoop(
+            LlmClient client, String systemPrompt,
+            List<LlmClient.ToolDef> tools, List<LlmClient.Message> messages,
+            String userQuestion) {
+        int start = messages.size();
+        String note = pendingNote;
+        int answer = askWithHistory(client, systemPrompt, tools, messages, 
userQuestion);
+        if (answer == 0) {
+            ChatHistory.compact(messages, ChatHistory.MAX_HISTORY_TURNS, 
ChatHistory.COMPACT_TOOL_RESULT_CHARS);
+        } else {
+            messages.subList(start, messages.size()).clear();
+            // the mistakes of the answer before are told with the next 
question
+            pendingNote = note;
+        }
+        return answer;
+    }
+
+    private int askWithHistory(
             LlmClient client, String systemPrompt,
             List<LlmClient.ToolDef> tools, List<LlmClient.Message> messages,
             String userQuestion) {
@@ -275,7 +299,7 @@ public class Ask extends CamelCommand {
                     if (showTools) {
                         printer().printDiagnostic("[result] " + 
truncate(result, 200));
                     }
-                    results.add(new LlmClient.ToolResult(toolCall.id(), 
result));
+                    results.add(new LlmClient.ToolResult(toolCall.id(), 
ChatHistory.truncateToolResult(result)));
                 }
                 messages.add(LlmClient.Message.toolResults(results));
             } else {
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ChatHistory.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ChatHistory.java
new file mode 100644
index 000000000000..b97144d96e6a
--- /dev/null
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ChatHistory.java
@@ -0,0 +1,127 @@
+/*
+ * 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.util.ArrayList;
+import java.util.List;
+
+import org.apache.camel.dsl.jbang.core.commands.ai.CatalogDocs;
+
+/**
+ * Keeps the history of a chat with a model bounded, as the whole history is 
sent with every request: camel ask and the
+ * AI panel of the TUI. A history larger than the context window of the model 
makes every later question fail.
+ */
+public final class ChatHistory {
+
+    /**
+     * A tool result is cut to this many characters before it enters the 
history, because a single log or table dump can
+     * otherwise be larger than the whole system prompt. It is the longest 
answer of camel_catalog_doc, so no catalog
+     * answer is cut: a long one names at its end the options and headers it 
leaves out, which the model needs to ask
+     * for one of them (16_000 cut that end off, once the catalog answers grew 
to 18,000 characters).
+     */
+    public static final int MAX_TOOL_RESULT_CHARS = 
CatalogDocs.MAX_ANSWER_LENGTH;
+
+    /**
+     * Tool results from turns before the previous one are shrunk to this many 
characters once the turn is answered. The
+     * model's own answer already summarises them, and the whole history is 
re-sent (and re-processed by a local model)
+     * on every request.
+     */
+    public static final int COMPACT_TOOL_RESULT_CHARS = 400;
+
+    /**
+     * Oldest turns are dropped beyond this many user questions in one 
conversation.
+     */
+    public static final int MAX_HISTORY_TURNS = 20;
+
+    private ChatHistory() {
+    }
+
+    /**
+     * Caps a tool result before it enters the model history.
+     */
+    public static String truncateToolResult(String result) {
+        if (result == null || result.length() <= MAX_TOOL_RESULT_CHARS) {
+            return result;
+        }
+        return result.substring(0, MAX_TOOL_RESULT_CHARS)
+               + "\n... [truncated, " + (result.length() - 
MAX_TOOL_RESULT_CHARS)
+               + " more characters; narrow the request (filter, limit, 
section) to see the rest]";
+    }
+
+    /**
+     * Keeps the model history bounded after a turn is answered: tool results 
from turns before the previous one are
+     * shrunk to their head, and the oldest turns are dropped beyond {@code 
maxTurns} user questions. The previous turn
+     * is kept intact so an immediate follow-up can still refer to what was 
just fetched. Whole turns are removed (user
+     * message through the final answer) so assistant tool calls never lose 
their matching results.
+     */
+    public static void compact(List<LlmClient.Message> history, int maxTurns, 
int compactChars) {
+        compact(history, maxTurns, compactChars, true);
+    }
+
+    /**
+     * As {@link #compact(List, int, int)}; with {@code keepPreviousTurn} 
false the most recent answered turn is
+     * compacted as well (used by {@code /compact} of the TUI).
+     */
+    public static void compact(
+            List<LlmClient.Message> history, int maxTurns, int compactChars,
+            boolean keepPreviousTurn) {
+        if (history == null || history.isEmpty()) {
+            return;
+        }
+        List<Integer> userIndexes = new ArrayList<>();
+        for (int i = 0; i < history.size(); i++) {
+            LlmClient.Message m = history.get(i);
+            if ("user".equals(m.role()) && m.toolCalls() == null && 
m.toolResults() == null) {
+                userIndexes.add(i);
+            }
+        }
+        if (userIndexes.size() > maxTurns) {
+            int keepFrom = userIndexes.get(userIndexes.size() - maxTurns);
+            history.subList(0, keepFrom).clear();
+            int dropped = userIndexes.size() - maxTurns;
+            userIndexes = userIndexes.subList(dropped, 
userIndexes.size()).stream()
+                    .map(index -> index - keepFrom).toList();
+        }
+        // everything before the previous turn (i.e. before the second-last 
user message) is compacted; /compact
+        // also compacts the previous turn itself
+        int keep = keepPreviousTurn ? 2 : 1;
+        if (userIndexes.size() < keep) {
+            return;
+        }
+        int compactBefore = keepPreviousTurn ? 
userIndexes.get(userIndexes.size() - 2) : history.size();
+        for (int i = 0; i < compactBefore; i++) {
+            LlmClient.Message m = history.get(i);
+            if (m.toolResults() == null || m.toolResults().isEmpty()) {
+                continue;
+            }
+            boolean changed = false;
+            List<LlmClient.ToolResult> compacted = new 
ArrayList<>(m.toolResults().size());
+            for (LlmClient.ToolResult tr : m.toolResults()) {
+                String content = tr.content();
+                if (content != null && content.length() > compactChars) {
+                    content = content.substring(0, compactChars)
+                              + "\n... [earlier result compacted; call the 
tool again for the full data]";
+                    changed = true;
+                }
+                compacted.add(new LlmClient.ToolResult(tr.toolCallId(), 
content));
+            }
+            if (changed) {
+                history.set(i, LlmClient.Message.toolResults(compacted));
+            }
+        }
+    }
+}
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/AskLoopTest.java
 
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/AskLoopTest.java
index d686ffa9e5ae..eff5bb9408c1 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/AskLoopTest.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/AskLoopTest.java
@@ -17,6 +17,7 @@
 package org.apache.camel.dsl.jbang.core.commands;
 
 import java.nio.file.Path;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
@@ -131,6 +132,61 @@ class AskLoopTest extends CamelCommandBaseTestSupport {
         assertThat(printer.getStderr()).contains("10 input / 5 output / 15 
total tokens");
     }
 
+    @Test
+    void aQuestionOfAChatThatFailsLeavesNoTraceInTheHistory() throws Exception 
{
+        // CAMEL-26932: the failed turn stayed in the history, and was sent 
again with every later question
+        server.reply("/api/chat", 500, "{\"error\":\"model runner has 
crashed\"}")
+                .thenReply("/api/chat", 200, answer("Two routes"));
+        Ask command = command();
+        command.askTools = new AskTools(-1);
+        List<LlmClient.Message> history = new ArrayList<>();
+
+        assertThat(command.runAgentLoop(client(), "system", List.of(), 
history, "first question")).isEqualTo(1);
+        assertThat(history).isEmpty();
+
+        assertThat(command.runAgentLoop(client(), "system", List.of(), 
history, "second question")).as(printer.getStderr())
+                .isZero();
+        JsonArray messages = (JsonArray) server.lastRequest().get("messages");
+        assertThat(messages.stream().map(m -> ((JsonObject) 
m).getString("content")))
+                .containsExactly("system", "second question");
+    }
+
+    @Test
+    void theToolResultsOfAChatStayBounded() throws Exception {
+        // CAMEL-26932: every tool result of the chat stayed in the history in 
full, until it was larger than the
+        // context window of the model
+        server.reply("/api/chat", 200, toolCall("big"), answer("one"), 
toolCall("big"), answer("two"),
+                toolCall("big"), answer("three"));
+        Ask command = command();
+        String big = "x".repeat(ChatHistory.MAX_TOOL_RESULT_CHARS * 2);
+        command.askTools = new AskTools(-1) {
+            @Override
+            public String executeTool(String name, JsonObject args) {
+                return big;
+            }
+        };
+        List<LlmClient.Message> history = new ArrayList<>();
+
+        for (String question : List.of("first", "second", "third")) {
+            assertThat(command.runAgentLoop(client(), "system", List.of(), 
history, question)).as(printer.getStderr()).isZero();
+        }
+
+        List<Integer> results = history.stream().filter(m -> m.toolResults() 
!= null)
+                .map(m -> m.toolResults().get(0).content().length()).toList();
+        assertThat(results).hasSize(3);
+        // the result of the first question is shrunk, the one of the previous 
question is kept, all are cut
+        
assertThat(results.get(0)).isLessThan(ChatHistory.COMPACT_TOOL_RESULT_CHARS + 
100);
+        assertThat(results.subList(1, 3)).allMatch(length -> length > 
ChatHistory.MAX_TOOL_RESULT_CHARS
+                && length < ChatHistory.MAX_TOOL_RESULT_CHARS + 200);
+    }
+
+    private LlmClient client() {
+        LlmClient client = 
LlmClient.create().withUrl(server.url()).withApiType(LlmClient.ApiType.ollama)
+                
.withExplicitModel("test").withStream(true).withTimeout(30).withPrinter(printer);
+        assertThat(client.detectEndpoint()).isTrue();
+        return client;
+    }
+
     private Ask command(String... args) {
         Ask command = new Ask(new CamelJBangMain().withPrinter(printer));
         CommandLineTestHelper.parse(command, Stream.concat(
diff --git 
a/dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/AiPanel.java
 
b/dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/AiPanel.java
index 95d9339fc509..a97b99228f98 100644
--- 
a/dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/AiPanel.java
+++ 
b/dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/AiPanel.java
@@ -63,10 +63,10 @@ import dev.tamboui.widgets.paragraph.Paragraph;
 import dev.tamboui.widgets.spinner.Spinner;
 import dev.tamboui.widgets.spinner.SpinnerState;
 import dev.tamboui.widgets.spinner.SpinnerStyle;
+import org.apache.camel.dsl.jbang.core.commands.ChatHistory;
 import org.apache.camel.dsl.jbang.core.commands.LlmClient;
 import org.apache.camel.dsl.jbang.core.commands.ai.AnswerChecks;
 import org.apache.camel.dsl.jbang.core.commands.ai.AppFeatures;
-import org.apache.camel.dsl.jbang.core.commands.ai.CatalogDocs;
 import org.apache.camel.dsl.jbang.core.commands.ai.ToolConfirms;
 import org.apache.camel.dsl.jbang.core.common.ExampleHelper;
 import org.apache.camel.dsl.jbang.run.common.Printer;
@@ -90,24 +90,12 @@ class AiPanel {
      * note instead, so a stuck loop ends with an explanation rather than at 
the iteration limit.
      */
     static final int MAX_IDENTICAL_TOOL_CALLS = 3;
-    /**
-     * Longest tool result handed to the model. The AI log keeps the full 
text; the model gets the head plus a note,
-     * because a single log or table dump can otherwise be larger than the 
whole system prompt. It is the longest answer
-     * of camel_catalog_doc, so no catalog answer is cut: a long one names at 
its end the options and headers it leaves
-     * out, which the model needs to ask for one of them (16_000 cut that end 
off, once the catalog answers grew to
-     * 18,000 characters).
-     */
-    static final int MAX_TOOL_RESULT_CHARS = CatalogDocs.MAX_ANSWER_LENGTH;
-    /**
-     * Tool results from turns before the previous one are shrunk to this many 
characters once the turn is answered. The
-     * model's own answer already summarises them, and the whole history is 
re-sent (and re-processed by a local model)
-     * on every request.
-     */
-    static final int COMPACT_TOOL_RESULT_CHARS = 400;
-    /**
-     * Oldest turns are dropped beyond this many user questions in one 
conversation.
-     */
-    static final int MAX_HISTORY_TURNS = 20;
+    /** A tool result is cut to this many characters before it enters the 
model history, see {@link ChatHistory}. */
+    static final int MAX_TOOL_RESULT_CHARS = ChatHistory.MAX_TOOL_RESULT_CHARS;
+    /** Tool results from turns before the previous one are shrunk to this 
many characters, see {@link ChatHistory}. */
+    static final int COMPACT_TOOL_RESULT_CHARS = 
ChatHistory.COMPACT_TOOL_RESULT_CHARS;
+    /** Oldest turns are dropped beyond this many user questions, see {@link 
ChatHistory}. */
+    static final int MAX_HISTORY_TURNS = ChatHistory.MAX_HISTORY_TURNS;
     /**
      * With a local endpoint the history is left untouched until the prompt 
exceeds a budget. A local server (Ollama)
      * keeps the KV cache of the previous request, so a request that merely 
extends the conversation only pays for its
@@ -2423,22 +2411,14 @@ class AiPanel {
      * Caps a tool result before it enters the model history; the AI log keeps 
the full text.
      */
     static String truncateToolResult(String result) {
-        if (result == null || result.length() <= MAX_TOOL_RESULT_CHARS) {
-            return result;
-        }
-        return result.substring(0, MAX_TOOL_RESULT_CHARS)
-               + "\n... [truncated, " + (result.length() - 
MAX_TOOL_RESULT_CHARS)
-               + " more characters; narrow the request (filter, limit, 
section) to see the rest]";
+        return ChatHistory.truncateToolResult(result);
     }
 
     /**
-     * Keeps the model history bounded after a turn is answered: tool results 
from turns before the previous one are
-     * shrunk to their head, and the oldest turns are dropped beyond {@code 
maxTurns} user questions. The previous turn
-     * is kept intact so an immediate follow-up can still refer to what was 
just fetched. Whole turns are removed (user
-     * message through the final answer) so assistant tool calls never lose 
their matching results.
+     * Keeps the model history bounded after a turn is answered, see {@link 
ChatHistory#compact(List, int, int)}.
      */
     static void compactHistory(List<LlmClient.Message> history, int maxTurns, 
int compactChars) {
-        compactHistory(history, maxTurns, compactChars, true);
+        ChatHistory.compact(history, maxTurns, compactChars);
     }
 
     /**
@@ -2448,50 +2428,7 @@ class AiPanel {
     static void compactHistory(
             List<LlmClient.Message> history, int maxTurns, int compactChars,
             boolean keepPreviousTurn) {
-        if (history == null || history.isEmpty()) {
-            return;
-        }
-        List<Integer> userIndexes = new ArrayList<>();
-        for (int i = 0; i < history.size(); i++) {
-            LlmClient.Message m = history.get(i);
-            if ("user".equals(m.role()) && m.toolCalls() == null && 
m.toolResults() == null) {
-                userIndexes.add(i);
-            }
-        }
-        if (userIndexes.size() > maxTurns) {
-            int keepFrom = userIndexes.get(userIndexes.size() - maxTurns);
-            history.subList(0, keepFrom).clear();
-            int dropped = userIndexes.size() - maxTurns;
-            userIndexes = userIndexes.subList(dropped, 
userIndexes.size()).stream()
-                    .map(index -> index - keepFrom).toList();
-        }
-        // everything before the previous turn (i.e. before the second-last 
user message) is compacted; /compact
-        // also compacts the previous turn itself
-        int keep = keepPreviousTurn ? 2 : 1;
-        if (userIndexes.size() < keep) {
-            return;
-        }
-        int compactBefore = keepPreviousTurn ? 
userIndexes.get(userIndexes.size() - 2) : history.size();
-        for (int i = 0; i < compactBefore; i++) {
-            LlmClient.Message m = history.get(i);
-            if (m.toolResults() == null || m.toolResults().isEmpty()) {
-                continue;
-            }
-            boolean changed = false;
-            List<LlmClient.ToolResult> compacted = new 
ArrayList<>(m.toolResults().size());
-            for (LlmClient.ToolResult tr : m.toolResults()) {
-                String content = tr.content();
-                if (content != null && content.length() > compactChars) {
-                    content = content.substring(0, compactChars)
-                              + "\n... [earlier result compacted; call the 
tool again for the full data]";
-                    changed = true;
-                }
-                compacted.add(new LlmClient.ToolResult(tr.toolCallId(), 
content));
-            }
-            if (changed) {
-                history.set(i, LlmClient.Message.toolResults(compacted));
-            }
-        }
+        ChatHistory.compact(history, maxTurns, compactChars, keepPreviousTurn);
     }
 
     /**
diff --git 
a/dsl/camel-jbang/camel-jbang-plugin-tui/src/test/java/org/apache/camel/dsl/jbang/core/commands/tui/SourceSizeTest.java
 
b/dsl/camel-jbang/camel-jbang-plugin-tui/src/test/java/org/apache/camel/dsl/jbang/core/commands/tui/SourceSizeTest.java
index b2750b61d8a7..f4bb1bde10d5 100644
--- 
a/dsl/camel-jbang/camel-jbang-plugin-tui/src/test/java/org/apache/camel/dsl/jbang/core/commands/tui/SourceSizeTest.java
+++ 
b/dsl/camel-jbang/camel-jbang-plugin-tui/src/test/java/org/apache/camel/dsl/jbang/core/commands/tui/SourceSizeTest.java
@@ -56,7 +56,7 @@ class SourceSizeTest {
 
     /** The files and the methods (class.method) over the limits, and the most 
lines each may have. */
     private static final String OVER_THE_LIMITS = """
-            AiPanel.java 3197
+            AiPanel.java 3093
             AiPanel.runAgentLoop 156
             AiUsageStats.render 166
             CamelMonitor.java 2632

Reply via email to