diff --git a/plugins/AI-Core/README.md b/plugins/AI-Core/README.md index adb01699e..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, 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 d8a998137..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,6 +39,13 @@ 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 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 + "{{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/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/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..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,6 +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 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 @@ -29,6 +33,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 +125,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 +284,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 @@ -348,9 +362,30 @@ 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) + // 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) { + 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) } @@ -365,6 +400,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/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/main/res/values/strings.xml b/plugins/AI-Core/src/main/res/values/strings.xml index adc219a40..ccc254e43 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. + 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. 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..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 @@ -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, @@ -1033,6 +1035,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 +1560,168 @@ 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 }, + requiredToolTurn = { tool -> "$REQUIRED $tool" }, + ) + + @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 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( + 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 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 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") + 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 { + 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:"