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.

perf(termux): programmatic recovery + wake-lock for background tasks (P4)

Gotcha Contributor6e1b83200ef4
diff --git a/app/src/main/assets/skills/termux_background.json b/app/src/main/assets/skills/termux_background.json
index 9c5a1f6..3a1e5b6 100644
--- a/app/src/main/assets/skills/termux_background.json
+++ b/app/src/main/assets/skills/termux_background.json
@@ -1,9 +1,24 @@
{
"id": "termux_background",
- "targetPackageNames": ["com.termux", "com.gotcha"],
+ "targetPackageNames": [
+ "com.termux",
+ "com.gotcha"
+ ],
"name": "Termux Background Processes",
"description": "How to keep a Termux process alive across calls and past Doze / battery savers.",
- "requiresTools": ["run_termux_command"],
- "keywords": ["background", "nohup", "wake", "sleep", "doze", "long", "running", "disown", "service"],
- "instructions": "1. A foreground process started by run_termux_command is bound to the lifecycle of that one call. The moment Gotcha returns from awaited completion, the process is orphaned and at the mercy of Android's lmkd, Doze, and OEM battery managers.\n2. Persistence pattern (the safe one):\n - `nohup myserver > /sdcard/Download/myserver.log 2>&1 < /dev/null & echo $! > /sdcard/Download/myserver.pid; disown`\n - Then `exit 0` immediately so the call returns success.\n - Next call: read the pid with `cat /sdcard/Download/myserver.pid`, then `kill $(cat /sdcard/Download/myserver.pid)` to stop it.\n - The `< /dev/null` is mandatory — without it, the process inherits the pipe and gets a SIGPIPE the moment the wrapping shell exits.\n3. Wake lock: `termux-wake-lock` (Termux-side; auto-grants WAKE_LOCK on first use). `termux-wake-unlock` to release. Required for any task longer than ~30s that does not write its own progress to disk.\n4. Android 12+ background restriction: if Gotcha is in the background and the assistive ball is off, runs fail with `Not allowed to start service` because Termux exposes a foreground service. Tell the user to bring Gotcha to the foreground (or switch the assistive ball on).\n5. Doze / battery saver: long-running `npm install`, `cargo build`, `pip install -r requirements.txt`, or `pkg install` of a large package can be suspended mid-flight. There is no in-Termux workaround beyond asking the user to add Gotcha to the battery-saver whitelist in Android Settings.\n6. Do not promise scheduled tasks. termux-cron / termux-job-scheduler exist but run inside Termux's own alarms, which Android's job scheduler can throttle. They are best-effort, not real cron.\n7. What you CANNOT do in Termux: run a process as a system service (no systemd), have it auto-restart on boot, or have it survive `force-stop` of Termux. The model should not promise any of these.\n8. Health probe pattern: when waiting for a server to come up, prefer `until curl -sSf http://127.0.0.1:PORT/healthz >/dev/null 2>&1; do sleep 1; done` inside a single run_termux_command call, with a tight timeout, rather than polling from Gotcha.\n9. Process hygiene: before starting a new long-running service, check `ps -ef | grep <name>` and offer to kill the existing one. The model cannot kill a process after timeout, but a new run_termux_command call can.\n10. Consequence: when the assistive ball is on, Gotcha holds a foreground service that exempts it from the Android 12+ backgrounded-start restriction. Without the ball, the same call fails with the \"Not allowed to start service\" error."
-}
+ "requiresTools": [
+ "run_termux_command"
+ ],
+ "keywords": [
+ "background",
+ "nohup",
+ "wake",
+ "sleep",
+ "doze",
+ "long",
+ "running",
+ "disown",
+ "service"
+ ],
+ "instructions": "1. A foreground process started by run_termux_command is bound to the lifecycle of that one call. The moment Gotcha returns from awaited completion, the process is orphaned and at the mercy of Android's lmkd, Doze, and OEM battery managers.\n2. Persistence pattern (the safe one):\n - `nohup myserver > /sdcard/Download/myserver.log 2>&1 < /dev/null & echo $! > /sdcard/Download/myserver.pid; disown`\n - Then `exit 0` immediately so the call returns success.\n - Next call: read the pid with `cat /sdcard/Download/myserver.pid`, then `kill $(cat /sdcard/Download/myserver.pid)` to stop it.\n - The `< /dev/null` is mandatory \u2014 without it, the process inherits the pipe and gets a SIGPIPE the moment the wrapping shell exits.\n3. Wake lock: `termux-wake-lock` (Termux-side; auto-grants WAKE_LOCK on first use). `termux-wake-unlock` to release. Required for any task longer than ~30s that does not write its own progress to disk.\n4. Android 12+ background restriction: if Gotcha is in the background and the assistive ball is off, runs fail with `Not allowed to start service` because Termux exposes a foreground service. Tell the user to bring Gotcha to the foreground (or switch the assistive ball on).\n5. Doze / battery saver: long-running `npm install`, `cargo build`, `pip install -r requirements.txt`, or `pkg install` of a large package can be suspended mid-flight. There is no in-Termux workaround beyond asking the user to add Gotcha to the battery-saver whitelist in Android Settings.\n6. Do not promise scheduled tasks. termux-cron / termux-job-scheduler exist but run inside Termux's own alarms, which Android's job scheduler can throttle. They are best-effort, not real cron.\n7. What you CANNOT do in Termux: run a process as a system service (no systemd), have it auto-restart on boot, or have it survive `force-stop` of Termux. The model should not promise any of these.\n8. Health probe pattern: when waiting for a server to come up, prefer `until curl -sSf http://127.0.0.1:PORT/healthz >/dev/null 2>&1; do sleep 1; done` inside a single run_termux_command call, with a tight timeout, rather than polling from Gotcha.\n9. Process hygiene and recovery: before starting a new long-running service, check `ps -ef | grep <name>` and offer to kill the existing one. If a previous command timed out and holds the dpkg lock, recover programmatically with `fuser $PREFIX/var/lib/dpkg/lock-frontend || dpkg --configure -a` or `pgrep -fl <name> && pkill -9 -f <name>` via a new run_termux_command call before falling back to the notification-shade Exit (which kills every Termux session). The model cannot kill via the timed-out call itself, but a new call can.\n9b. Wake-lock wrapping for long tasks: for any task >30s (pkg install of large package, npm/pip/cargo build), wrap with `termux-wake-lock && { <cmd>; } ; termux-wake-unlock` inside the same run_termux_command payload. This prevents Doze CPU throttling that turns a 30s task into a 10-minute hang.\n10. Consequence: when the assistive ball is on, Gotcha holds a foreground service that exempts it from the Android 12+ backgrounded-start restriction. Without the ball, the same call fails with the \"Not allowed to start service\" error."
+}
\ No newline at end of file
diff --git a/app/src/main/java/com/gotcha/tools/TermuxMessages.kt b/app/src/main/java/com/gotcha/tools/TermuxMessages.kt
index 5d9e615..5a98f27 100644
--- a/app/src/main/java/com/gotcha/tools/TermuxMessages.kt
+++ b/app/src/main/java/com/gotcha/tools/TermuxMessages.kt
@@ -66,7 +66,9 @@ internal object TermuxMessages {
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 — a run_termux_command cannot kill them. To stop them, open the " +
+ "even after they time out — a run_termux_command cannot kill them from the timed-out call itself, " +
+ "but a new call can try `pgrep -fl <name> && pkill -9 -f <name>` or " +
+ "`fuser \$PREFIX/var/lib/dpkg/lock-frontend || dpkg --configure -a` first. To stop them, open the " +
"notifications shade (global_action or press_key 'notifications') and tap the Exit action on the " +
"Termux notification, which ends every Termux session. If you cannot reach it, ask the user to tap " +
"Exit on the Termux notification. Otherwise wait for them to finish (use the sleep tool) rather " +
@@ -89,7 +91,9 @@ internal object TermuxMessages {
append("here. Retry with a non-interactive flag (e.g. -y) or pass the answer in stdin.")
}
append("\n- or it is genuinely slow. Retry with a larger timeout_seconds (the hard ceiling is 600). ")
- append("Note it keeps running either way — a run_termux_command cannot stop it — so check Termux ")
+ append("Note it keeps running either way — a run_termux_command cannot stop it from the timed-out ")
+ append("call itself, but a new call can try `pgrep -fl <name> && pkill -9 -f <name>` or ")
+ append("`fuser \$PREFIX/var/lib/dpkg/lock-frontend || dpkg --configure -a` first — so check Termux ")
append("before starting again. To stop a stuck one, open the notifications shade ")
append("(global_action or press_key 'notifications') and tap the Exit action on the Termux ")
append("notification, which ends every Termux session. If you cannot reach it, ask the user to tap ")
@@ -99,7 +103,8 @@ internal object TermuxMessages {
append("Ask the user to open Termux, run `termux-change-repo` once, pick a closer mirror, and retry.")
append("\n- or, if this was a long-running process (server, watcher, build) rather than a one-shot ")
append("command, it should be backgrounded with `nohup ... > log 2>&1 < /dev/null & echo $! > pid; ")
- append("disown` and the call should `exit 0` immediately; `termux-wake-lock` locks Doze off.")
+ append("disown` and the call should `exit 0` immediately; wrap long tasks with ")
+ append("`termux-wake-lock && { <cmd>; } ; termux-wake-unlock` to prevent Doze throttling.")
append("\nFor the full list of pitfalls, call search_skills('termux_operations').")
}
)
diff --git a/app/src/test/java/com/gotcha/agent/skills/TermuxSkillsBundledTest.kt b/app/src/test/java/com/gotcha/agent/skills/TermuxSkillsBundledTest.kt
index e5af670..e39f4ea 100644
--- a/app/src/test/java/com/gotcha/agent/skills/TermuxSkillsBundledTest.kt
+++ b/app/src/test/java/com/gotcha/agent/skills/TermuxSkillsBundledTest.kt
@@ -1,7 +1,6 @@
package com.gotcha.agent.skills
import androidx.test.core.app.ApplicationProvider
-import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Before