From db3d3131ea4fe73624cb49361a1f20f6cb1dc2c6 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Mon, 16 Nov 2026 23:36:39 -0800 Subject: [PATCH 1/8] ADFA-7186 AI-Core: give a run that repeats a successful call one turn to answer A model that re-issued the call that had just succeeded ended the run at once with nothing said, which on a read-only question left the user with no answer. The first such repeat now adds a turn telling the model its earlier result is above and to answer; the repeated batch is not run again. A second repeat ends the run as before, now with a notice saying the Agent repeated the same step. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- .../plugins/aicore/tool/AgentLoop.kt | 31 ++++++- .../aicore/viewmodel/AgentRunReporter.kt | 8 ++ .../plugins/aicore/viewmodel/ChatViewModel.kt | 4 + .../plugins/aicore/tool/AgentLoopTest.kt | 86 +++++++++++++++++++ 4 files changed, 125 insertions(+), 4 deletions(-) diff --git a/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoop.kt b/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoop.kt index 0fe84a910..11351e3a6 100644 --- a/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoop.kt +++ b/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoop.kt @@ -13,6 +13,9 @@ import com.itsaky.androidide.plugins.services.LlmInferenceService.ChatMessage.Ro * @param formatToolResults words each batch's results as the next user turn. * @param unfinishedTurn words the turn sent when a run that has used tools ends a reply without * [terminalTool]; null ends the run on that reply instead. + * @param repeatedAfterSuccessTurn words the turn sent once when a run re-issues the batch it has just + * run successfully, asking it to answer the user instead. Null sends none, so such a repeat ends + * the run at once, through [Events.onRepeatAfterSuccess]. * @param requiredToolTurn words the turn sent when a run told to call a tool first tries to finish * without it; receives the tool's name. Null never asks. * @param unappliedWriteTurn words the turn sent when a run answers while a file change it attempted @@ -29,6 +32,7 @@ class AgentLoop( ToolCallExtractor::diagnoseUnparsedReply, private val terminalTool: String? = null, private val unfinishedTurn: (suspend () -> String)? = null, + private val repeatedAfterSuccessTurn: (suspend () -> String)? = null, private val requiredToolTurn: (suspend (String) -> String)? = null, private val unappliedWriteTurn: (suspend () -> String)? = null, ) { @@ -120,16 +124,24 @@ class AgentLoop( suspend fun onNoProgressCycle(turns: Int, staleLimit: Int) {} /** - * The model re-issued the batch it had just run successfully, which the loop reads as the - * work being finished rather than as a repeat to abort on. + * The model re-issued the batch it had just run successfully a second time in a run (or the + * loop has no turn to ask it to answer), so the run ends without the terminal tool. * - * The only completion that reaches the user with nothing said: it fires instead of - * [onFinalAnswer], because the model never called the terminal tool. + * Fires instead of [onFinalAnswer]; the model said nothing, so the reporter must tell the + * user why the run stopped. * * @param turn 1-based turn index. */ suspend fun onRepeatAfterSuccess(turn: Int) {} + /** + * The model re-issued the batch it had just run successfully. The batch is not run again; + * the run is given one more turn, told its earlier result is above and to answer the user. + * + * @param turn 1-based turn index. + */ + suspend fun onRepeatAfterSuccessRetry(turn: Int) {} + /** * The model called the terminal tool to finish. * @param turn 1-based turn index. @@ -271,6 +283,7 @@ class AgentLoop( // Once a tool has run, only the terminal tool finishes the run; see [unfinishedTurn]. var toolsRan = false var askedToFinish = false + var askedToAnswerAfterRepeat = false val unapplied = UnappliedWrites(progress) // Cleared once the tool runs or has been asked for, so a model that refuses it still stops. var requiredPending = requiredTool != null && requiredToolTurn != null @@ -351,6 +364,16 @@ class AgentLoop( // A repeated look is not completion while the change the user asked for failed. if (unapplied.warn(turn, history, events)) continue if (unapplied.outstanding) return unapplied.stop(turn, StopReason.ABANDONED, events) + // A read-only question not yet answered looks the same as a finished write, so the + // run gets one turn to answer before it is ended; the repeated batch is not run. + if (!askedToAnswerAfterRepeat && repeatedAfterSuccessTurn != null) { + askedToAnswerAfterRepeat = true + // The answer may be prose, which then ends the run without another prompt. + askedToFinish = true + events.onRepeatAfterSuccessRetry(turn) + history.add(ChatMessage(Role.USER, repeatedAfterSuccessTurn.invoke())) + continue + } events.onRepeatAfterSuccess(turn) return Result(turn, StopReason.COMPLETED) } diff --git a/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/viewmodel/AgentRunReporter.kt b/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/viewmodel/AgentRunReporter.kt index 5b5ac5f68..29ae9ce78 100644 --- a/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/viewmodel/AgentRunReporter.kt +++ b/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/viewmodel/AgentRunReporter.kt @@ -42,6 +42,9 @@ internal class AgentRunReporter( /** The model kept circling actions it had already taken. */ suspend fun noProgress() + + /** The run ended because the model repeated a step that had worked, and answered nothing. */ + suspend fun repeatedStep() } override suspend fun onTurnStarted(turn: Int, maxTurns: Int) { @@ -79,6 +82,11 @@ internal class AgentRunReporter( override suspend fun onRepeatAfterSuccess(turn: Int) { AgentTrace.stage("LOOP", "turn=$turn assumed-complete=repeat-after-success") + notices.repeatedStep() + } + + override suspend fun onRepeatAfterSuccessRetry(turn: Int) { + AgentTrace.stage("LOOP", "turn=$turn asked-to-answer=repeat-after-success") } // No notice: AgentReplyRenderer already puts this same advice in the turn's own bubble. diff --git a/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/viewmodel/ChatViewModel.kt b/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/viewmodel/ChatViewModel.kt index 0a9c83fac..564699037 100644 --- a/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/viewmodel/ChatViewModel.kt +++ b/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/viewmodel/ChatViewModel.kt @@ -227,6 +227,7 @@ class ChatViewModel( formatToolResults = toolResultsPrompt, terminalTool = RESPOND_TOOL, unfinishedTurn = toolResultsPrompt::unfinished, + repeatedAfterSuccessTurn = toolResultsPrompt::repeatedAfterSuccess, requiredToolTurn = toolResultsPrompt::requiredTool, unappliedWriteTurn = toolResultsPrompt::unappliedWrite, ) @@ -2065,6 +2066,9 @@ class ChatViewModel( override suspend fun noProgress() = addSystemMessage(str(R.string.agent_no_progress), MessageStatus.SENT) + + override suspend fun repeatedStep() = + addSystemMessage(str(R.string.agent_repeated_step), MessageStatus.SENT) } /** Whether the user message [messageId] is shown unfolded. Main only. */ diff --git a/plugins/AI-Core/src/test/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoopTest.kt b/plugins/AI-Core/src/test/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoopTest.kt index f81139755..b3d5875d0 100644 --- a/plugins/AI-Core/src/test/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoopTest.kt +++ b/plugins/AI-Core/src/test/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoopTest.kt @@ -1033,6 +1033,7 @@ class AgentLoopTest { override suspend fun onChangeNotMade(turn: Int) { events += "not-made" } override suspend fun onUnfinishedReply(turn: Int) { events += "unfinished" } override suspend fun onRepeatAfterSuccess(turn: Int) { events += "assumed-complete" } + override suspend fun onRepeatAfterSuccessRetry(turn: Int) { events += "repeat-retry" } override suspend fun onMaxIterationsReached(turns: Int) { events += "max" } override suspend fun onUnparsedReply(turn: Int, reason: ToolCallExtractor.UnparsedReply) { events += "unparsed" @@ -1557,7 +1558,92 @@ class AgentLoopTest { assertEquals(listOf("unparsed", "not-made"), events) } + /** [finishingLoop], also giving a run that repeats a successful batch one turn to answer. */ + private fun repeatAwareLoop() = AgentLoop( + formatToolResults = ToolResultsPrompt({ shippedConfig }, "respond"), + terminalTool = "respond", + unfinishedTurn = { UNFINISHED }, + repeatedAfterSuccessTurn = { REPEATED }, + ) + + @Test + fun givenAReadRepeatedAfterSuccess_whenTheLoopRuns_thenItIsToldToAnswerWithoutRerunningAndTheAnswerIsFinal() = runTest { + val (result, history, events) = runWriting( + listOf(toolCall("read_file", "a"), toolCall("read_file", "a"), respond), + mapOf("read_file" to listOf(ToolResult.success("lines"))), + loop = repeatAwareLoop(), + ) + + assertTrue(result.completed) + assertEquals(3, result.turns) + assertEquals(listOf("repeat-retry", "final"), events) + assertEquals(REPEATED, history.last { it.role == Role.USER }.content) + // One result block only: the repeated batch was not run again. + assertEquals(1, history.count { it.role == Role.USER && it.content.contains("[read_file]") }) + } + + @Test + fun givenAReadRepeatedAfterSuccess_whenTheNextTurnAnswersInProse_thenThatProseEndsTheRun() = runTest { + val (result, history, events) = runWriting( + listOf(toolCall("read_file", "a"), toolCall("read_file", "a"), "Two databases."), + mapOf("read_file" to listOf(ToolResult.success("lines"))), + loop = repeatAwareLoop(), + ) + + assertTrue(result.completed) + assertEquals(3, result.turns) + assertEquals(listOf("repeat-retry"), events) + assertEquals("Two databases.", history.last().content) + } + + @Test + fun givenAReadRepeatedTwice_whenTheLoopRuns_thenItEndsAfterTheExtraTurnWithTheRepeatEvent() = runTest { + val (result, _, events) = runWriting( + listOf(toolCall("read_file", "a"), toolCall("read_file", "a"), toolCall("read_file", "a")), + mapOf("read_file" to listOf(ToolResult.success("lines"))), + loop = repeatAwareLoop(), + ) + + assertTrue(result.completed) + assertEquals(3, result.turns) + assertEquals(listOf("repeat-retry", "assumed-complete"), events) + } + + @Test + fun givenAWriteRepeatedAfterSuccess_whenTheModelRepeatsAgain_thenItEndsAfterOneExtraTurnRatherThanLooping() = runTest { + val (result, _, events) = runWriting( + listOf(edit("a", "x"), edit("a", "x"), edit("a", "x"), edit("a", "x")), + mapOf("edit_file" to listOf(appliedEdit)), + loop = repeatAwareLoop(), + ) + + assertTrue(result.completed) + assertEquals(3, result.turns) + assertEquals(listOf("repeat-retry", "assumed-complete"), events) + } + + @Test + fun givenNoRepeatTurn_whenAReadIsRepeatedAfterSuccess_thenItEndsAtOnceWithTheRepeatEvent() = runTest { + val (result, _, events) = runWriting( + listOf(toolCall("read_file", "a"), toolCall("read_file", "a")), + mapOf("read_file" to listOf(ToolResult.success("lines"))), + loop = finishingLoop(), + ) + + assertTrue(result.completed) + assertEquals(2, result.turns) + assertEquals(listOf("assumed-complete"), events) + } + + @Test + fun theShippedRepeatTurnNamesTheTerminalToolAndRendersCleanly() { + val text = ToolResultsPrompt.renderRepeatedAfterSuccess(shippedConfig, "respond") + assertTrue(text, text.contains("\"respond\"")) + assertTrue(text, text.contains("already succeeded")) + } + private companion object { + const val REPEATED = "you repeated it, answer now" const val UNFINISHED = "finish or carry on" const val UNAPPLIED = "the change was not made" const val REQUIRED = "call first:" From 23394bbc632f808a95cf773576b133a891cb0a62 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Mon, 16 Nov 2026 23:36:48 -0800 Subject: [PATCH 2/8] ADFA-7186 AI-Core: add the repeat-answer prompt, notice string and config wiring The previous commit was missing these files: the repeated_after_success turn in agent_loop.yml, its config field, parser line and renderer, the user notice string, and the README mention. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- plugins/AI-Core/README.md | 2 +- .../src/main/assets/prompts/agent_loop.yml | 6 ++++++ .../plugins/aicore/prompt/ToolResultsPrompt.kt | 17 +++++++++++++++++ .../aicore/prompt/config/AgentPromptConfig.kt | 3 +++ .../prompt/config/AgentPromptConfigParser.kt | 1 + plugins/AI-Core/src/main/res/values/strings.xml | 1 + 6 files changed, 29 insertions(+), 1 deletion(-) diff --git a/plugins/AI-Core/README.md b/plugins/AI-Core/README.md index adb01699e..a434c3115 100644 --- a/plugins/AI-Core/README.md +++ b/plugins/AI-Core/README.md @@ -161,7 +161,7 @@ into one document: | `rules.yml` | `rules` | Priority groups, highest first; each has a `heading` (`CRITICAL`, `IMPORTANT`, `MANDATORY`, `OPTIONAL`) and its `items`. **Adding a rule is adding an item.** | | `tools.yml` | `tools`, `tool_call_format` | What introduces the tool list, and how to write a call as text (sent only under the text protocol). The list itself is the tools the run offers (`PromptToolCatalog`). | | `ide_context.yml` | `ide_context`, `session` | One line per fact the IDE can state: open files, module paths. `session` states the device's date and time on every prompt, and that the web tools are there. | -| `agent_loop.yml` | `agent_loop`, `approval` | What the agent is told after each tool batch: the `FAILED:` marker, the truncation notice, what to do next after a success or a failure, the `unfinished` turn sent once when a run that has used tools replies without `respond`, the `unapplied_write` turn sent once when a run tries to finish while a file change it tried failed and was not redone, and the `required_tool` turn sent once when a run that had to search first answers without searching. `approval` is what it is told when the user denies a call, asks for a revision, or leaves the dialog unanswered. | +| `agent_loop.yml` | `agent_loop`, `approval` | What the agent is told after each tool batch: the `FAILED:` marker, the truncation notice, what to do next after a success or a failure, the `unfinished` turn sent once when a run that has used tools replies without `respond`, the `unapplied_write` turn sent once when a run tries to finish while a file change it tried failed and was not redone, and the `repeated_after_success` turn sent once when a run re-issues the call that just succeeded, asking it to answer instead, and the `required_tool` turn sent once when a run that had to search first answers without searching. `approval` is what it is told when the user denies a call, asks for a revision, or leaves the dialog unanswered. | | `context_files.yml` | `context_files` | The heading over the files the user attached to a message, and the notice inside one cut to the size limit. | | `chat_title.yml` | `chat_title` | The system prompt of the one-off request that names a chat from its first message, before the agent replies. | | `web_search.yml` | `web_search` | The system prompt of the one-off request the `web_search` tool makes through the active backend. It may use `CURRENT_TIME`, so "latest" is read as of the device's date. | diff --git a/plugins/AI-Core/src/main/assets/prompts/agent_loop.yml b/plugins/AI-Core/src/main/assets/prompts/agent_loop.yml index d8a998137..f57d5e0b5 100644 --- a/plugins/AI-Core/src/main/assets/prompts/agent_loop.yml +++ b/plugins/AI-Core/src/main/assets/prompts/agent_loop.yml @@ -39,6 +39,12 @@ agent_loop: the file already exists, use update_file; if update_file said the file does not exist, use create_file; otherwise fix what the error said. If you cannot, call the "{{TERMINAL_TOOL}}" tool and say plainly that the change was not made. + # Sent once when a run re-issues the batch it has just run successfully. The batch is not run + # again; the earlier result is already in the transcript. + repeated_after_success: >- + You just repeated your previous tool call, which already succeeded; its result is above, and + the call was not run again. Do not call that tool again. Answer the user now, from that + result: write the answer, or call the "{{TERMINAL_TOOL}}" tool with it. # Sent once when a run that had to check the web first answered without searching; TOOL is the # search tool. A backend that forces the call never sends it (see VerificationPolicy). required_tool: >- diff --git a/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/prompt/ToolResultsPrompt.kt b/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/prompt/ToolResultsPrompt.kt index fb0ebabe6..5a80f0d94 100644 --- a/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/prompt/ToolResultsPrompt.kt +++ b/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/prompt/ToolResultsPrompt.kt @@ -34,6 +34,9 @@ class ToolResultsPrompt( /** The turn sent after a reply that tried to finish while a failed file change was not redone. */ suspend fun unappliedWrite(): String = renderUnappliedWrite(config.config(), terminalTool) + /** The turn sent after a reply that re-issued the batch that had just succeeded. */ + suspend fun repeatedAfterSuccess(): String = renderRepeatedAfterSuccess(config.config(), terminalTool) + /** The turn sent after a reply that answered before calling [tool], which the run had to call first. */ suspend fun requiredTool(tool: String): String = renderRequiredTool(config.config(), terminalTool, tool) @@ -109,6 +112,19 @@ class ToolResultsPrompt( mapOf(PromptVariables.TERMINAL_TOOL to terminalTool), ) + /** + * Renders the turn that asks a run which repeated a successful batch to answer instead. + * + * @param config the loaded prompt config. + * @param terminalTool the name of the tool that ends a run by answering the user. + * @return the turn to add to the transcript. + */ + fun renderRepeatedAfterSuccess(config: AgentPromptConfig, terminalTool: String): String = + PromptTemplateEngine.render( + config.agentLoop.repeatedAfterSuccess, + mapOf(PromptVariables.TERMINAL_TOOL to terminalTool), + ) + /** * Renders the turn that asks a run which answered too early to call [tool] first. * @@ -134,6 +150,7 @@ class ToolResultsPrompt( { render(config, CHECK_TERMINAL_TOOL, CHECK_CHAR_LIMIT, calls, results) } } + { renderUnfinished(config, CHECK_TERMINAL_TOOL) } + { renderUnappliedWrite(config, CHECK_TERMINAL_TOOL) } + + { renderRepeatedAfterSuccess(config, CHECK_TERMINAL_TOOL) } + { renderRequiredTool(config, CHECK_TERMINAL_TOOL, CHECK_REQUIRED_TOOL) } return renders.mapNotNull { check -> try { diff --git a/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/prompt/config/AgentPromptConfig.kt b/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/prompt/config/AgentPromptConfig.kt index c37848505..bcc5e3694 100644 --- a/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/prompt/config/AgentPromptConfig.kt +++ b/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/prompt/config/AgentPromptConfig.kt @@ -113,6 +113,8 @@ data class AgentPromptConfig( * terminal tool. * @property unappliedWrite the turn after a reply that tried to finish while a file change had * failed and no later write to that file had succeeded. + * @property repeatedAfterSuccess the turn after a reply that re-issued the batch that had just + * succeeded, asking the run to answer instead. * @property requiredTool the turn after a reply that answered before calling the tool the run * had to call first, such as a web search before a code review. */ @@ -124,6 +126,7 @@ data class AgentPromptConfig( val afterFailure: PromptText, val unfinished: PromptText, val unappliedWrite: PromptText, + val repeatedAfterSuccess: PromptText, val requiredTool: PromptText, ) diff --git a/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/prompt/config/AgentPromptConfigParser.kt b/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/prompt/config/AgentPromptConfigParser.kt index 848c9f3d4..0d134050f 100644 --- a/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/prompt/config/AgentPromptConfigParser.kt +++ b/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/prompt/config/AgentPromptConfigParser.kt @@ -75,6 +75,7 @@ object AgentPromptConfigParser : PromptConfigParser { afterFailure = text("after_failure"), unfinished = text("unfinished"), unappliedWrite = text("unapplied_write"), + repeatedAfterSuccess = text("repeated_after_success"), requiredTool = text("required_tool"), ) }, diff --git a/plugins/AI-Core/src/main/res/values/strings.xml b/plugins/AI-Core/src/main/res/values/strings.xml index adc219a40..c9aeabaa5 100644 --- a/plugins/AI-Core/src/main/res/values/strings.xml +++ b/plugins/AI-Core/src/main/res/values/strings.xml @@ -221,6 +221,7 @@ Stopped: the model kept requesting the same action. Try rephrasing your request. A change was not made: a file change failed and the assistant did not manage to redo it, whatever its last message says. Try again, or ask for a smaller change. Stopped: the model kept redoing actions it had already taken instead of making progress. Try rephrasing your request, or asking for a smaller step. + Stopped: the Agent repeated the same step instead of answering. Try asking again, or rephrasing your question. Earlier messages were summarized to stay within the model\'s limit. Earlier messages could not be summarized, so only the most recent ones were kept to stay within the model\'s limit. Only the most recent messages were kept to stay within the on-device model\'s limit. From 1b5b431c10c16312eb739056484ea09c55eee095 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Tue, 17 Nov 2026 08:39:44 -0800 Subject: [PATCH 3/8] ADFA-7186 AI-Core: reword the repeat-answer prompt to allow a different call The prompt said "answer from that result", which pushes thin answers when a different call was needed, and "that tool" was ambiguous for a batch. It now says the call(s) succeeded and were not rerun, to answer if the results are enough, else make a different call. Also drops a doubled "and" in the README. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- plugins/AI-Core/README.md | 2 +- plugins/AI-Core/src/main/assets/prompts/agent_loop.yml | 6 +++--- .../itsaky/androidide/plugins/aicore/tool/AgentLoopTest.kt | 3 +++ 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/plugins/AI-Core/README.md b/plugins/AI-Core/README.md index a434c3115..a4ca7a557 100644 --- a/plugins/AI-Core/README.md +++ b/plugins/AI-Core/README.md @@ -161,7 +161,7 @@ into one document: | `rules.yml` | `rules` | Priority groups, highest first; each has a `heading` (`CRITICAL`, `IMPORTANT`, `MANDATORY`, `OPTIONAL`) and its `items`. **Adding a rule is adding an item.** | | `tools.yml` | `tools`, `tool_call_format` | What introduces the tool list, and how to write a call as text (sent only under the text protocol). The list itself is the tools the run offers (`PromptToolCatalog`). | | `ide_context.yml` | `ide_context`, `session` | One line per fact the IDE can state: open files, module paths. `session` states the device's date and time on every prompt, and that the web tools are there. | -| `agent_loop.yml` | `agent_loop`, `approval` | What the agent is told after each tool batch: the `FAILED:` marker, the truncation notice, what to do next after a success or a failure, the `unfinished` turn sent once when a run that has used tools replies without `respond`, the `unapplied_write` turn sent once when a run tries to finish while a file change it tried failed and was not redone, and the `repeated_after_success` turn sent once when a run re-issues the call that just succeeded, asking it to answer instead, and the `required_tool` turn sent once when a run that had to search first answers without searching. `approval` is what it is told when the user denies a call, asks for a revision, or leaves the dialog unanswered. | +| `agent_loop.yml` | `agent_loop`, `approval` | What the agent is told after each tool batch: the `FAILED:` marker, the truncation notice, what to do next after a success or a failure, the `unfinished` turn sent once when a run that has used tools replies without `respond`, the `unapplied_write` turn sent once when a run tries to finish while a file change it tried failed and was not redone, the `repeated_after_success` turn sent once when a run re-issues the call that just succeeded, asking it to answer or make a different call, and the `required_tool` turn sent once when a run that had to search first answers without searching. `approval` is what it is told when the user denies a call, asks for a revision, or leaves the dialog unanswered. | | `context_files.yml` | `context_files` | The heading over the files the user attached to a message, and the notice inside one cut to the size limit. | | `chat_title.yml` | `chat_title` | The system prompt of the one-off request that names a chat from its first message, before the agent replies. | | `web_search.yml` | `web_search` | The system prompt of the one-off request the `web_search` tool makes through the active backend. It may use `CURRENT_TIME`, so "latest" is read as of the device's date. | diff --git a/plugins/AI-Core/src/main/assets/prompts/agent_loop.yml b/plugins/AI-Core/src/main/assets/prompts/agent_loop.yml index f57d5e0b5..86d52abcf 100644 --- a/plugins/AI-Core/src/main/assets/prompts/agent_loop.yml +++ b/plugins/AI-Core/src/main/assets/prompts/agent_loop.yml @@ -42,9 +42,9 @@ agent_loop: # Sent once when a run re-issues the batch it has just run successfully. The batch is not run # again; the earlier result is already in the transcript. repeated_after_success: >- - You just repeated your previous tool call, which already succeeded; its result is above, and - the call was not run again. Do not call that tool again. Answer the user now, from that - result: write the answer, or call the "{{TERMINAL_TOOL}}" tool with it. + Your previous tool call(s) already succeeded and were not run again; their results are above. + If those results are enough, answer the user now: write the answer, or call the + "{{TERMINAL_TOOL}}" tool with it. Otherwise make a different call, not the same one again. # Sent once when a run that had to check the web first answered without searching; TOOL is the # search tool. A backend that forces the call never sends it (see VerificationPolicy). required_tool: >- diff --git a/plugins/AI-Core/src/test/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoopTest.kt b/plugins/AI-Core/src/test/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoopTest.kt index b3d5875d0..4fa509097 100644 --- a/plugins/AI-Core/src/test/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoopTest.kt +++ b/plugins/AI-Core/src/test/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoopTest.kt @@ -1640,6 +1640,9 @@ class AgentLoopTest { val text = ToolResultsPrompt.renderRepeatedAfterSuccess(shippedConfig, "respond") assertTrue(text, text.contains("\"respond\"")) assertTrue(text, text.contains("already succeeded")) + assertTrue(text, text.contains("were not run again")) + assertTrue(text, text.contains("make a different call, not the same one again")) + assertFalse(text, text.contains("Do not call that tool again")) } private companion object { From ed17f8d8d5d93c74ed0f13cd4eb176397f4ec256 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Tue, 17 Nov 2026 08:40:57 -0800 Subject: [PATCH 4/8] ADFA-7186 AI-Core: take a respond sent with the repeated call as the answer A turn that repeated a successful call and also carried a terminal respond had the respond dropped, then the run asked for an answer or ended with the "did not answer" notice. The respond is now the run's answer; the repeated call is still not run. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- .../androidide/plugins/aicore/tool/AgentLoop.kt | 5 +++++ .../plugins/aicore/tool/AgentLoopTest.kt | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoop.kt b/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoop.kt index 11351e3a6..c12141876 100644 --- a/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoop.kt +++ b/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoop.kt @@ -364,6 +364,11 @@ class AgentLoop( // A repeated look is not completion while the change the user asked for failed. if (unapplied.warn(turn, history, events)) continue if (unapplied.outstanding) return unapplied.stop(turn, StopReason.ABANDONED, events) + // The turn that repeats also answered: that is the answer, so nothing is asked. + if (terminal != null) { + events.onFinalAnswer(turn, respondMessageOf(terminal.args).orEmpty()) + return Result(turn, StopReason.COMPLETED) + } // A read-only question not yet answered looks the same as a finished write, so the // run gets one turn to answer before it is ended; the repeated batch is not run. if (!askedToAnswerAfterRepeat && repeatedAfterSuccessTurn != null) { diff --git a/plugins/AI-Core/src/test/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoopTest.kt b/plugins/AI-Core/src/test/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoopTest.kt index 4fa509097..602aae85a 100644 --- a/plugins/AI-Core/src/test/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoopTest.kt +++ b/plugins/AI-Core/src/test/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoopTest.kt @@ -1596,6 +1596,23 @@ class AgentLoopTest { assertEquals("Two databases.", history.last().content) } + @Test + fun givenARepeatedReadSentWithRespond_whenTheLoopRuns_thenTheRespondIsTheAnswerAndTheRepeatIsNotRun() = runTest { + val (result, history, events) = runWriting( + listOf(toolCall("read_file", "a"), toolCall("read_file", "a") + respond), + mapOf("read_file" to listOf(ToolResult.success("lines"))), + loop = repeatAwareLoop(), + ) + + assertTrue(result.completed) + assertEquals(AgentLoop.StopReason.COMPLETED, result.reason) + assertEquals(2, result.turns) + // The answer ends the run: no repeat turn is sent, and no "repeated" notice is raised. + assertEquals(listOf("final"), events) + assertEquals(0, history.count { it.content == REPEATED }) + assertEquals(1, history.count { it.role == Role.USER && it.content.contains("[read_file]") }) + } + @Test fun givenAReadRepeatedTwice_whenTheLoopRuns_thenItEndsAfterTheExtraTurnWithTheRepeatEvent() = runTest { val (result, _, events) = runWriting( From cb11c7d8f9a4aba07d36fb72d9ea692cb24583f6 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Tue, 17 Nov 2026 08:40:57 -0800 Subject: [PATCH 5/8] ADFA-7186 AI-Core: say the repeated step had already succeeded in the stop notice The notice did not say the step worked, which invites running it twice. It now says the repeated step had already succeeded and was not run again, then that the Agent stopped. No test: a string resource with no logic. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- plugins/AI-Core/src/main/res/values/strings.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/AI-Core/src/main/res/values/strings.xml b/plugins/AI-Core/src/main/res/values/strings.xml index c9aeabaa5..ccc254e43 100644 --- a/plugins/AI-Core/src/main/res/values/strings.xml +++ b/plugins/AI-Core/src/main/res/values/strings.xml @@ -221,7 +221,7 @@ Stopped: the model kept requesting the same action. Try rephrasing your request. A change was not made: a file change failed and the assistant did not manage to redo it, whatever its last message says. Try again, or ask for a smaller change. Stopped: the model kept redoing actions it had already taken instead of making progress. Try rephrasing your request, or asking for a smaller step. - Stopped: the Agent repeated the same step instead of answering. Try asking again, or rephrasing your question. + The step the Agent repeated had already succeeded and was not run again. The Agent stopped without answering. Try asking again, or rephrasing your question. Earlier messages were summarized to stay within the model\'s limit. Earlier messages could not be summarized, so only the most recent ones were kept to stay within the model\'s limit. Only the most recent messages were kept to stay within the on-device model\'s limit. From 3cc82576fdc86fcd7619c9fb57eb9f115ae5d5d5 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Tue, 17 Nov 2026 10:17:28 -0800 Subject: [PATCH 6/8] ADFA-7186 AI-Core: keep the required-tool check when a repeat is sent with respond A repeated call sent together with respond finished the run without asking for the required tool, so a web_search could be skipped. Redirect it to the tool as a plain respond is. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- .../plugins/aicore/tool/AgentLoop.kt | 6 +++++ .../plugins/aicore/tool/AgentLoopTest.kt | 25 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoop.kt b/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoop.kt index c12141876..37ea037c2 100644 --- a/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoop.kt +++ b/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoop.kt @@ -361,6 +361,12 @@ class AgentLoop( return unapplied.stop(turn, StopReason.CYCLING, events) } ToolCallProgressGuard.Verdict.ASSUME_COMPLETE -> { + // An answer sent with the repeat owes the required tool as much as a plain one. + if (terminal != null && requiredPending) { + requiredPending = false + askForRequiredTool(turn, requiredTool!!, history, events) + continue + } // A repeated look is not completion while the change the user asked for failed. if (unapplied.warn(turn, history, events)) continue if (unapplied.outstanding) return unapplied.stop(turn, StopReason.ABANDONED, events) diff --git a/plugins/AI-Core/src/test/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoopTest.kt b/plugins/AI-Core/src/test/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoopTest.kt index 602aae85a..a3de520d5 100644 --- a/plugins/AI-Core/src/test/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoopTest.kt +++ b/plugins/AI-Core/src/test/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoopTest.kt @@ -1005,6 +1005,7 @@ class AgentLoopTest { writtenFilesOf: ((ToolCall) -> Set)? = null, changeOf: (ToolCall) -> Any = { it }, resolvedFileOf: (ToolCall) -> String? = { it.args["path"]?.toString() }, + requiredTool: String? = null, ): Triple, List> { val model = ScriptedModel(replies) val used = mutableMapOf() @@ -1023,6 +1024,7 @@ class AgentLoopTest { } }, pathsOf = pathsOf, + requiredTool = requiredTool, changesPaths = { it.name == "edit_file" || it.name == "run_shell_command" }, writesFiles = { it.name == "edit_file" }, writtenFilesOf = writtenFilesOf ?: pathsOf, @@ -1564,6 +1566,7 @@ class AgentLoopTest { terminalTool = "respond", unfinishedTurn = { UNFINISHED }, repeatedAfterSuccessTurn = { REPEATED }, + requiredToolTurn = { tool -> "$REQUIRED $tool" }, ) @Test @@ -1652,6 +1655,28 @@ class AgentLoopTest { assertEquals(listOf("assumed-complete"), events) } + @Test + fun givenARequiredToolPending_whenARepeatIsSentWithRespond_thenTheRunIsRedirectedToTheToolInsteadOfFinishing() = runTest { + val (result, history, events) = runWriting( + listOf( + toolCall("read_file", "a"), + toolCall("read_file", "a") + respond, + toolCall("web_search"), + respond, + ), + mapOf("read_file" to listOf(ToolResult.success("lines")), "web_search" to listOf(ToolResult.success("hits"))), + loop = repeatAwareLoop(), + requiredTool = "web_search", + ) + + assertTrue(result.completed) + assertEquals(4, result.turns) + assertEquals(1, history.count { it.content == "$REQUIRED web_search" }) + assertEquals(1, history.count { it.role == Role.USER && it.content.contains("[web_search]") }) + // Only the final respond ends the run; the repeat's respond did not. + assertEquals(listOf("final"), events) + } + @Test fun theShippedRepeatTurnNamesTheTerminalToolAndRendersCleanly() { val text = ToolResultsPrompt.renderRepeatedAfterSuccess(shippedConfig, "respond") From 07fe53a87b2a10468a8d9d9b1644c0480ec0cb47 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Tue, 17 Nov 2026 10:19:26 -0800 Subject: [PATCH 7/8] ADFA-7186 AI-Core: give a later repeat the extra turn again after a different call worked The repeat prompt invites a different call, but the extra turn was spent for the run. Re-arm it once a successful batch has run; the progress guard still ends a run that alternates between calls. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- .../plugins/aicore/tool/AgentLoop.kt | 4 +++ .../plugins/aicore/tool/AgentLoopTest.kt | 33 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoop.kt b/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoop.kt index 37ea037c2..7b4919618 100644 --- a/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoop.kt +++ b/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoop.kt @@ -399,6 +399,10 @@ class AgentLoop( if (realCalls.any { it.name == requiredTool }) requiredPending = false toolsRan = true askedToFinish = false + // The extra turn invited a different call: once one has worked, a later repeat has the + // run's progress behind it again and is asked too. Alternating between calls cannot make + // this loop for ever, as the progress guard ends a run that keeps reissuing known batches. + if (results.isNotEmpty() && results.all { it.success }) askedToAnswerAfterRepeat = false progress.recordResults(results) unapplied.record(realCalls.zip(results), writesFiles, changesPaths, writtenFilesOf, changeOf) events.onToolResults(turn, realCalls, results) diff --git a/plugins/AI-Core/src/test/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoopTest.kt b/plugins/AI-Core/src/test/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoopTest.kt index a3de520d5..c102c73be 100644 --- a/plugins/AI-Core/src/test/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoopTest.kt +++ b/plugins/AI-Core/src/test/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoopTest.kt @@ -1677,6 +1677,39 @@ class AgentLoopTest { assertEquals(listOf("final"), events) } + @Test + fun givenARepeatAnsweredByADifferentSuccessfulCall_whenItIsRepeatedAgain_thenItGetsTheExtraTurnAgain() = runTest { + val (result, history, events) = runWriting( + listOf( + toolCall("read_file", "a"), toolCall("read_file", "a"), + toolCall("read_file", "b"), toolCall("read_file", "b"), + respond, + ), + mapOf("read_file" to listOf(ToolResult.success("lines"))), + loop = repeatAwareLoop(), + ) + + assertTrue(result.completed) + assertEquals(5, result.turns) + assertEquals(listOf("repeat-retry", "repeat-retry", "final"), events) + assertEquals(2, history.count { it.content == REPEATED }) + } + + @Test + fun givenAModelAlternatingBetweenTwoRepeatedCalls_whenTheExtraTurnIsRearmed_thenTheRunStillEndsAsCycling() = runTest { + val a = toolCall("read_file", "a") + val b = toolCall("read_file", "b") + val (result, _, events) = runWriting( + listOf(a, a, b, b, a, a, b, b, a, a, b, b), + mapOf("read_file" to listOf(ToolResult.success("lines"))), + loop = repeatAwareLoop(), + ) + + assertEquals(AgentLoop.StopReason.CYCLING, result.reason) + assertTrue(result.turns < 12) + assertEquals(listOf("repeat-retry", "repeat-retry"), events.filter { it == "repeat-retry" }) + } + @Test fun theShippedRepeatTurnNamesTheTerminalToolAndRendersCleanly() { val text = ToolResultsPrompt.renderRepeatedAfterSuccess(shippedConfig, "respond") From 1aba2f5d5d7bab04a3ff5148cda58a7909b02111 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Tue, 17 Nov 2026 12:42:04 -0800 Subject: [PATCH 8/8] ADFA-7186 AI-Core: say the repeat turn can come again after a different call worked 07fe53a8 re-arms the repeated_after_success turn once a different batch has succeeded, so a run can get it more than once. The AgentLoop KDoc, the agent_loop.yml comment and the README row still said it is sent once. They now say it is sent at most once until a different call succeeds. Text only. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn --- plugins/AI-Core/README.md | 2 +- plugins/AI-Core/src/main/assets/prompts/agent_loop.yml | 5 +++-- .../com/itsaky/androidide/plugins/aicore/tool/AgentLoop.kt | 7 ++++--- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/plugins/AI-Core/README.md b/plugins/AI-Core/README.md index a4ca7a557..49ddca3c2 100644 --- a/plugins/AI-Core/README.md +++ b/plugins/AI-Core/README.md @@ -161,7 +161,7 @@ into one document: | `rules.yml` | `rules` | Priority groups, highest first; each has a `heading` (`CRITICAL`, `IMPORTANT`, `MANDATORY`, `OPTIONAL`) and its `items`. **Adding a rule is adding an item.** | | `tools.yml` | `tools`, `tool_call_format` | What introduces the tool list, and how to write a call as text (sent only under the text protocol). The list itself is the tools the run offers (`PromptToolCatalog`). | | `ide_context.yml` | `ide_context`, `session` | One line per fact the IDE can state: open files, module paths. `session` states the device's date and time on every prompt, and that the web tools are there. | -| `agent_loop.yml` | `agent_loop`, `approval` | What the agent is told after each tool batch: the `FAILED:` marker, the truncation notice, what to do next after a success or a failure, the `unfinished` turn sent once when a run that has used tools replies without `respond`, the `unapplied_write` turn sent once when a run tries to finish while a file change it tried failed and was not redone, the `repeated_after_success` turn sent once when a run re-issues the call that just succeeded, asking it to answer or make a different call, and the `required_tool` turn sent once when a run that had to search first answers without searching. `approval` is what it is told when the user denies a call, asks for a revision, or leaves the dialog unanswered. | +| `agent_loop.yml` | `agent_loop`, `approval` | What the agent is told after each tool batch: the `FAILED:` marker, the truncation notice, what to do next after a success or a failure, the `unfinished` turn sent once when a run that has used tools replies without `respond`, the `unapplied_write` turn sent once when a run tries to finish while a file change it tried failed and was not redone, the `repeated_after_success` turn sent when a run re-issues the call that just succeeded, asking it to answer or make a different call (at most once until a different call succeeds), and the `required_tool` turn sent once when a run that had to search first answers without searching. `approval` is what it is told when the user denies a call, asks for a revision, or leaves the dialog unanswered. | | `context_files.yml` | `context_files` | The heading over the files the user attached to a message, and the notice inside one cut to the size limit. | | `chat_title.yml` | `chat_title` | The system prompt of the one-off request that names a chat from its first message, before the agent replies. | | `web_search.yml` | `web_search` | The system prompt of the one-off request the `web_search` tool makes through the active backend. It may use `CURRENT_TIME`, so "latest" is read as of the device's date. | diff --git a/plugins/AI-Core/src/main/assets/prompts/agent_loop.yml b/plugins/AI-Core/src/main/assets/prompts/agent_loop.yml index 86d52abcf..ed69313ed 100644 --- a/plugins/AI-Core/src/main/assets/prompts/agent_loop.yml +++ b/plugins/AI-Core/src/main/assets/prompts/agent_loop.yml @@ -39,8 +39,9 @@ agent_loop: the file already exists, use update_file; if update_file said the file does not exist, use create_file; otherwise fix what the error said. If you cannot, call the "{{TERMINAL_TOOL}}" tool and say plainly that the change was not made. - # Sent once when a run re-issues the batch it has just run successfully. The batch is not run - # again; the earlier result is already in the transcript. + # Sent when a run re-issues the batch it has just run successfully: at most once until a + # different batch succeeds, then again for a later repeat. The batch is not run again; the + # earlier result is already in the transcript. repeated_after_success: >- Your previous tool call(s) already succeeded and were not run again; their results are above. If those results are enough, answer the user now: write the answer, or call the diff --git a/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoop.kt b/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoop.kt index 7b4919618..ed9b25a13 100644 --- a/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoop.kt +++ b/plugins/AI-Core/src/main/kotlin/com/itsaky/androidide/plugins/aicore/tool/AgentLoop.kt @@ -13,9 +13,10 @@ import com.itsaky.androidide.plugins.services.LlmInferenceService.ChatMessage.Ro * @param formatToolResults words each batch's results as the next user turn. * @param unfinishedTurn words the turn sent when a run that has used tools ends a reply without * [terminalTool]; null ends the run on that reply instead. - * @param repeatedAfterSuccessTurn words the turn sent once when a run re-issues the batch it has just - * run successfully, asking it to answer the user instead. Null sends none, so such a repeat ends - * the run at once, through [Events.onRepeatAfterSuccess]. + * @param repeatedAfterSuccessTurn words the turn sent when a run re-issues the batch it has just + * run successfully, asking it to answer the user instead. Sent at most once until a different + * batch succeeds, then again for a later repeat. Null sends none, so such a repeat ends the run + * at once, through [Events.onRepeatAfterSuccess]. * @param requiredToolTurn words the turn sent when a run told to call a tool first tries to finish * without it; receives the tool's name. Null never asks. * @param unappliedWriteTurn words the turn sent when a run answers while a file change it attempted