Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugins/AI-Core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
7 changes: 7 additions & 0 deletions plugins/AI-Core/src/main/assets/prompts/agent_loop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: >-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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.
*
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -124,6 +126,7 @@ data class AgentPromptConfig(
val afterFailure: PromptText,
val unfinished: PromptText,
val unappliedWrite: PromptText,
val repeatedAfterSuccess: PromptText,
val requiredTool: PromptText,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ object AgentPromptConfigParser : PromptConfigParser<AgentPromptConfig> {
afterFailure = text("after_failure"),
unfinished = text("unfinished"),
unappliedWrite = text("unapplied_write"),
repeatedAfterSuccess = text("repeated_after_success"),
requiredTool = text("required_tool"),
)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
) {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Comment thread
davidschachterADFA marked this conversation as resolved.
Resolved
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) {
Comment thread
davidschachterADFA marked this conversation as resolved.
Resolved
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)
Comment thread
davidschachterADFA marked this conversation as resolved.
Resolved
return Result(turn, StopReason.COMPLETED)
}
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ class ChatViewModel(
formatToolResults = toolResultsPrompt,
terminalTool = RESPOND_TOOL,
unfinishedTurn = toolResultsPrompt::unfinished,
repeatedAfterSuccessTurn = toolResultsPrompt::repeatedAfterSuccess,
requiredToolTurn = toolResultsPrompt::requiredTool,
unappliedWriteTurn = toolResultsPrompt::unappliedWrite,
)
Expand Down Expand Up @@ -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. */
Expand Down
1 change: 1 addition & 0 deletions plugins/AI-Core/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@
<string name="agent_repeated_calls">Stopped: the model kept requesting the same action. Try rephrasing your request.</string>
<string name="agent_change_not_made">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.</string>
<string name="agent_no_progress">Stopped: the model kept redoing actions it had already taken instead of making progress. Try rephrasing your request, or asking for a smaller step.</string>
<string name="agent_repeated_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.</string>
<string name="chat_compacted">Earlier messages were summarized to stay within the model\'s limit.</string>
<string name="chat_compaction_failed">Earlier messages could not be summarized, so only the most recent ones were kept to stay within the model\'s limit.</string>
<string name="chat_compaction_trimmed">Only the most recent messages were kept to stay within the on-device model\'s limit.</string>
Expand Down
Loading
Loading