Your phone, on your command. Gotcha is world's first fully on-device AI copilot for Android that turns natural language into real device actions — safely.
fix: implement anti-loop guard in AgentEngine to abort after 3 consecutive identical tool-call signatures
Rishabh Bajpaid3150779452c
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 0664631..757665d 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -51,3 +51,15 @@ Studio, OpenRouter, any OpenAI-compatible server) are unchanged.(runs Google sign-in with the Activity context, persists the JWT, refreshes theViewModel) and `onSamosaSignOut`. The initial route already opens Settings whenthe active provider is unconfigured, satisfying the "prompt to sign in" flow.++### Fixed++- **agent/AgentEngine.kt**: added an anti-loop guard to the shared agent tool+ loop. When consecutive tool rounds produce a byte-identical signature (tool-call+ names + tool result text) — e.g. the accessibility service repeatedly returning+ "enabled but not running", or `webfetch` retrying a dead URL that keeps 404-ing —+ the loop now bails out after 3 identical rounds with a clear message instead of+ spinning until `maxToolRounds`. Provider-agnostic; benefits both OpenAI-compatible+ and Samosa AI. (Root cause of the apparent "reply re-sent in a loop" during+ tool-heavy Samosa tasks: the model kept retrying a failing tool with no new+ information; not a re-injection of assistant replies as user turns.)diff --git a/app/src/main/java/com/gotcha/agent/AgentEngine.kt b/app/src/main/java/com/gotcha/agent/AgentEngine.ktindex 3a85208..38c01ad 100644--- a/app/src/main/java/com/gotcha/agent/AgentEngine.kt+++ b/app/src/main/java/com/gotcha/agent/AgentEngine.kt@@ -278,6 +278,12 @@ class AgentEngine(// tools (FileResolver already accepts one); deferred for now.FileResolver.WORKING_DIR_BASE = workingDir().absolutePathcheckAndCompactHistory(llm)+ // Anti-loop guard: if consecutive tool rounds produce the byte-identical+ // set of tool-call names + results (e.g. a tool that keeps returning the+ // same "service not running" error), the model is stuck retrying with no+ // new information. Break after [MAX_REPEATED_TOOL_ROUNDS] such rounds.+ var lastRoundSignature: String? = null+ var repeatedRoundCount = 0repeat(settings.maxToolRounds) { iteration ->if (iteration > 0) delay(INTER_CALL_DELAY_MS)events.onActivity("Thinking…")@@ -395,10 +401,34 @@ class AgentEngine(}}}+ val historySizeBeforeTools = history.sizeexecuteToolCalls()saveCurrentSession()// Re-assert after each round; a concurrent engine may have moved it.FileResolver.WORKING_DIR_BASE = workingDir().absolutePath++ // Anti-loop guard: signature = tool-call names + the tool result text+ // appended this round. Identical signatures across consecutive rounds+ // mean the model is retrying the same failing action with no progress.+ val roundToolResults = history.drop(historySizeBeforeTools)+ .filter { it.role == "tool" }+ .joinToString("\n") { it.textContent }+ val roundSignature = toolCalls.joinToString(",") { it.function.name } + "|" + roundToolResults+ if (roundSignature == lastRoundSignature) {+ repeatedRoundCount+++ if (repeatedRoundCount >= MAX_REPEATED_TOOL_ROUNDS) {+ events.onUi(+ MessageKind.ERROR,+ "Stopped: the same tool action kept returning the same result " ++ "($repeatedRoundCount times in a row) with no progress. " ++ "Check that the required service/permission is available, then try again."+ )+ return+ }+ } else {+ repeatedRoundCount = 0+ lastRoundSignature = roundSignature+ }}events.onUi(MessageKind.ERROR,@@ -961,6 +991,13 @@ class AgentEngine(private const val TAG = "Gotcha"private const val INTER_CALL_DELAY_MS = 400L+ /**+ * How many consecutive tool rounds with a byte-identical+ * (tool names + results) signature are tolerated before the agent loop+ * bails out. Catches "service not running" / dead-URL retry storms.+ */+ private const val MAX_REPEATED_TOOL_ROUNDS = 3+/*** Parses the body of a QUESTION: tool result into a [PendingQuestion].* Format: <question>\n--OPTIONS--\nopt1\nopt2\n--ALLOW_CUSTOM--\ntrue