Skip to content

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: treat a null startService return as an immediate failure

Durganshu Mishra4a5767633826
diff --git a/app/src/main/java/com/gotcha/tools/TermuxMessages.kt b/app/src/main/java/com/gotcha/tools/TermuxMessages.kt
index afb6405..3e27465 100644
--- a/app/src/main/java/com/gotcha/tools/TermuxMessages.kt
+++ b/app/src/main/java/com/gotcha/tools/TermuxMessages.kt
@@ -51,6 +51,18 @@ internal object TermuxMessages {
"or force-stopped; use run_command for the app's own sandbox meanwhile."
)
+ /**
+ * `startService` returning null rather than throwing. Distinct from [startFailed]: nothing
+ * refused us, the service simply was not there to start — usually Termux force-stopped, or
+ * uninstalled between the availability probe and the call.
+ */
+ fun serviceDidNotStart() = ToolResult.error(
+ "Termux did not start the command: its RUN_COMMAND service could not be reached. Termux has most " +
+ "likely been force-stopped (by the user or a battery manager), or was uninstalled just now. Ask " +
+ "the user to open Termux once so it is running again, then retry. No command was started, so " +
+ "nothing is pending."
+ )
+
fun tooManyInFlight(limit: Int) = ToolResult.error(
"$limit Termux commands are already running and none has finished yet. Earlier commands keep running " +
"even after they time out — I cannot kill them. Wait for them to finish (use the sleep tool), or " +
diff --git a/app/src/main/java/com/gotcha/tools/TermuxTool.kt b/app/src/main/java/com/gotcha/tools/TermuxTool.kt
index b548676..2329364 100644
--- a/app/src/main/java/com/gotcha/tools/TermuxTool.kt
+++ b/app/src/main/java/com/gotcha/tools/TermuxTool.kt
@@ -156,9 +156,15 @@ class TermuxTool(
TermuxResultReceiver.resultIntent(context, requestCode),
pendingIntentFlags()
)
- runCatching { context.startService(commandIntent(trimmed, workingDir, stdin, pendingIntent)) }
- .exceptionOrNull()
- ?.let { return TermuxMessages.startFailed(it) }
+ val started = runCatching {
+ context.startService(commandIntent(trimmed, workingDir, stdin, pendingIntent))
+ }
+ started.exceptionOrNull()?.let { return TermuxMessages.startFailed(it) }
+ // startService reports "no such service" by returning null, not by throwing. Android
+ // also excludes force-stopped packages from intent resolution, so a Termux the user
+ // (or an OEM battery manager) has stopped lands here. Waiting on the deferred would
+ // burn the whole timeout and then claim the command was accepted and still running.
+ if (started.getOrNull() == null) return TermuxMessages.serviceDidNotStart()
val bundle = withTimeoutOrNull(timeout * 1000L) { deferred.await() }
?: return TermuxMessages.timedOut(trimmed, timeout, hadStdin = stdin != null)
diff --git a/app/src/test/java/com/gotcha/tools/TermuxToolTest.kt b/app/src/test/java/com/gotcha/tools/TermuxToolTest.kt
index fa2a451..2a59211 100644
--- a/app/src/test/java/com/gotcha/tools/TermuxToolTest.kt
+++ b/app/src/test/java/com/gotcha/tools/TermuxToolTest.kt
@@ -172,6 +172,25 @@ class TermuxToolTest {
assertTrue(result.message.contains("genuinely slow"))
}
+ @Test
+ fun `a service that does not start is reported at once, not as a timeout`() = runTest {
+ // startService signals "no such service" by returning null rather than throwing, which
+ // is what a force-stopped Termux produces: Android excludes stopped packages from intent
+ // resolution, while the package and its service still resolve for the availability
+ // probe. Robolectric always returns a component, so the null is injected here.
+ installTermux()
+ grantRunCommand()
+ val nullStartingContext = object : android.content.ContextWrapper(context) {
+ override fun startService(service: android.content.Intent?): android.content.ComponentName? = null
+ }
+
+ val result = TermuxTool(nullStartingContext).runCommand("echo hello", timeoutSeconds = 600)
+
+ assertFalse(result.success)
+ assertTrue("must not be reported as still running: ${result.message}", result.message.contains("force-stopped"))
+ assertTrue("must say nothing is pending", result.message.contains("nothing is pending"))
+ }
+
@Test
fun `command and stdin share the size budget`() = runTest {
installTermux()