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: correct two error paths proven wrong by testing them on a device
Durganshu Mishra56b7ec760791
diff --git a/app/src/main/java/com/gotcha/tools/TermuxMessages.kt b/app/src/main/java/com/gotcha/tools/TermuxMessages.ktindex 2cc9dd9..afb6405 100644--- a/app/src/main/java/com/gotcha/tools/TermuxMessages.kt+++ b/app/src/main/java/com/gotcha/tools/TermuxMessages.kt@@ -37,9 +37,18 @@ internal object TermuxMessages {"Termux was installed after Gotcha — reinstall or update Gotcha so Android can grant it.)")+ /**+ * Seen on-device as `Not allowed to start service … app is in background`: from Android 12,+ * an app in the background cannot start another app's foreground service, and Termux's+ * RunCommandService is one. Gotcha's assistive ball normally holds a foreground service that+ * exempts us, so this shows up when it is switched off and the chat UI has been backgrounded.+ */fun startFailed(cause: Throwable) = ToolResult.error(- "Could not reach Termux's RUN_COMMAND service: ${cause.message}. Termux may have been disabled or stopped " +- "by the system. You may open Termux once and ask again, or use run_command for the app's own sandbox."+ "Could not reach Termux's RUN_COMMAND service: ${cause.message}. If that says Gotcha is in the " ++ "background, Android is refusing the start — from Android 12 a backgrounded app cannot launch " ++ "another app's foreground service. Ask the user to bring Gotcha to the foreground (or switch the " ++ "assistive ball on, which keeps it exempt) and try again. Otherwise Termux may have been disabled " ++ "or force-stopped; use run_command for the app's own sandbox meanwhile.")fun tooManyInFlight(limit: Int) = ToolResult.error(@@ -49,20 +58,22 @@ internal object TermuxMessages {)/**- * The causes are indistinguishable from here — Termux sends nothing until a command finishes- * — so this lists them rather than picking one and sounding certain.+ * A timeout means the command was accepted and is simply taking too long — a *rejected*+ * command comes back promptly with an errno instead. Notably `allow-external-apps` being+ * unset is **not** a cause here, despite what the plugin docs imply: on-device it returns+ * ERRNO_FAILED with an explanatory message within a second. Listing it would send the model+ * off fixing the one thing that is already known not to be wrong.*/fun timedOut(command: String, timeout: Int, hadStdin: Boolean) = ToolResult.error(buildString {- append("No result from Termux after ${timeout}s for: $command. I cannot tell these apart:")+ append("No result from Termux after ${timeout}s for: $command. Termux accepted the command, ")+ append("so it is running — it is just not finished. Either:")if (!hadStdin) {- append("\n- the command may be waiting for typed input that will never come — there is no ")- append("terminal here. Retry with a non-interactive flag (e.g. -y) or pass the answer in stdin.")+ append("\n- it is waiting for typed input that will never come, since there is no terminal ")+ append("here. Retry with a non-interactive flag (e.g. -y) or pass the answer in stdin.")}- append("\n- it may simply still be running. I cannot kill another app's process, so it keeps going; ")- append("check Termux itself, and retry with a larger timeout_seconds if it was just slow.")- append("\n- `allow-external-apps=true` may be missing from `~/.termux/termux.properties`, in which ")- append("case Termux silently ignored the request and no command ever ran.")+ append("\n- or it is genuinely slow. Retry with a larger timeout_seconds. Note it keeps running ")+ append("either way — I cannot kill another app's process — so check Termux before starting again.")})diff --git a/app/src/main/java/com/gotcha/tools/TermuxTool.kt b/app/src/main/java/com/gotcha/tools/TermuxTool.ktindex f9b57d0..b548676 100644--- a/app/src/main/java/com/gotcha/tools/TermuxTool.kt+++ b/app/src/main/java/com/gotcha/tools/TermuxTool.kt@@ -23,16 +23,17 @@ import java.util.concurrent.atomic.AtomicInteger* `com.termux.app.RunCommandService`: we send it a command, it runs it in the background (no* terminal pop-up) and returns stdout/stderr/exit code through a [PendingIntent].*- * Two things must be true before that works, and only the first is detectable:+ * Two things must be true before that works:* 1. `com.termux.permission.RUN_COMMAND` is granted. It is a **`dangerous`** permission that* Termux itself declares, so listing it in the manifest is not enough — it needs a runtime* grant, raised through [ToolResult.TERMUX_ACCESS]. A caveat worth knowing: a custom* dangerous permission whose defining app was installed *after* Gotcha may not be* grantable until Gotcha is updated or reinstalled.- * 2. `allow-external-apps=true` is set in Termux's `~/.termux/termux.properties`. That file- * lives in Termux's private storage, so we cannot read it. When it is false Termux drops- * the intent silently and no result ever arrives — indistinguishable from here from a slow- * command, which is why the timeout message names both causes.+ * 2. `allow-external-apps=true` is set in Termux's `~/.termux/termux.properties`. That file is+ * in Termux's private storage so we cannot read it ahead of time, but we do not need to:+ * verified on-device against Termux 0.118.3, running with it unset comes back promptly as+ * `err = ERRNO_FAILED` with an `errmsg` naming the property, which [formatResult] passes+ * through verbatim. It is a clear failure, not the silent hang the API docs imply.** Availability is gated on the Termux package being *installed* ([Capability.TERMUX]), not on* the grant — gating on the grant would hide the only tool that can raise the prompt. Thediff --git a/app/src/test/java/com/gotcha/tools/TermuxToolTest.kt b/app/src/test/java/com/gotcha/tools/TermuxToolTest.ktindex 17275dc..fa2a451 100644--- a/app/src/test/java/com/gotcha/tools/TermuxToolTest.kt+++ b/app/src/test/java/com/gotcha/tools/TermuxToolTest.kt@@ -146,13 +146,15 @@ class TermuxToolTest {val result = tool.runCommand("echo hello", timeoutSeconds = 1)- // No Termux exists to answer the PendingIntent, so this is the timeout path — and it has- // to name both indistinguishable causes.+ // No Termux exists to answer the PendingIntent, so this is the timeout path.assertFalse(result.success)assertNull(result.needsPermission)- assertTrue(result.message.contains("still be running"))- assertTrue(result.message.contains("allow-external-apps"))+ assertTrue(result.message.contains("genuinely slow"))assertTrue("must name the interactive-prompt cause too", result.message.contains("waiting for typed input"))+ assertFalse(+ "allow-external-apps comes back as an errno, not a timeout — verified on-device",+ result.message.contains("allow-external-apps")+ )}@Test@@ -167,7 +169,7 @@ class TermuxToolTest {"stdin was given, so a missing prompt answer cannot be the cause",result.message.contains("waiting for typed input"))- assertTrue(result.message.contains("still be running"))+ assertTrue(result.message.contains("genuinely slow"))}@Test@@ -380,6 +382,29 @@ class TermuxToolTest {assertTrue(result.message.contains("cancelled"))}+ @Test+ fun `allow-external-apps being unset comes back as a readable error, not a hang`() {+ // The exact bundle Termux 0.118.3 sent on a Nothing Phone 3a with the property removed.+ // The plugin docs imply the request is dropped silently; it is not, and the message it+ // returns already tells the user precisely what to fix — so it must be passed through+ // rather than flattened into a generic failure.+ val bundle = Bundle().apply {+ putInt(TermuxTool.RESULT_ERR, 2)+ putString(+ TermuxTool.RESULT_ERRMSG,+ "Error Code: `2`\nError Message:\n```\nRunCommandService requires `allow-external-apps` " ++ "property to be set to `true` in `~/.termux/termux.properties` file.\n```"+ )+ }++ val result = tool.formatResult(bundle)++ assertFalse(result.success)+ assertTrue(result.message.contains("failed"))+ assertTrue("the actionable part must survive", result.message.contains("allow-external-apps"))+ assertTrue(result.message.contains("termux.properties"))+ }+@Testfun `a termux-side failure surfaces its message and fails`() {val bundle = resultBundle(exitCode = 0).apply {