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): adaptive timeout + quiet/stdin efficiency (P2)
Gotcha Contributor21953100b7e1
diff --git a/app/src/main/assets/skills/termux_filesystem.json b/app/src/main/assets/skills/termux_filesystem.jsonindex 12bef20..7a59809 100644--- a/app/src/main/assets/skills/termux_filesystem.json+++ b/app/src/main/assets/skills/termux_filesystem.json@@ -1,9 +1,24 @@{"id": "termux_filesystem",- "targetPackageNames": ["com.termux", "com.gotcha"],+ "targetPackageNames": [+ "com.termux",+ "com.gotcha"+ ],"name": "Termux Filesystem","description": "Three filesystem worlds: Gotcha's working dir, Termux's home, and /sdcard. They are not interchangeable.",- "requiresTools": ["run_termux_command"],- "keywords": ["file", "path", "sdcard", "/sdcard", "storage", "home", "directory", "upload", "download"],- "instructions": "Three filesystem worlds — keep them separate:\n1. Gotcha's working dir (default cwd for `run_command`, `read_file`, `write_file`, `edit`, `glob`, `grep`): `/storage/emulated/0/Gotcha/chats/<slug_id>/`. INVISIBLE to Termux (different UID).\n2. Termux's home (default `working_dir` for `run_termux_command`): `/data/data/com.termux/files/home`. INVISIBLE to Gotcha.\n3. Shared storage: `/storage/emulated/0/Download`, `/storage/emulated/0/Pictures`, `/storage/emulated/0/Music`. Readable by both UIDs, but ONLY AFTER the user has run `termux-setup-storage` in Termux once. Before that, Termux sees `/sdcard/` as empty.\n\nThe only safe bridge between the two worlds is `/sdcard/Download/`:\n1. Gotcha: write_file('/storage/emulated/0/Download/foo.txt', ...). Termux reads: cat /sdcard/Download/foo.txt.\n2. Termux writes /sdcard/Download/result.txt. Gotcha reads: read_file('/storage/emulated/0/Download/result.txt').\n\nInside Termux, use `/sdcard` (which symlinks to `/storage/emulated/0`). Do NOT use `/storage/emulated/0` directly inside Termux for write paths — go through `/sdcard`.\n\n`$PREFIX` is `/data/data/com.termux/files/usr`. The model should not guess `/usr/bin` for Termux — that path is empty.\n\nIf a Gotcha `read_file` on a path that exists in `/sdcard/Download/` fails inside Termux's view, the user has likely not run `termux-setup-storage`. Ask the user to open Termux once and run `termux-setup-storage`, then retry.\n\nTermux's `/data/data/com.termux/files/home` is a real directory, but writing to it from Gotcha via `run_command` would fail with Permission denied — that is a different UID. Stick to the /sdcard bridge.\n\nFor temporary files inside Termux, prefer `/data/data/com.termux/files/home/.tmp/` or `$TMPDIR` — both are private to Termux and survive a single call.\n\ncross-uid file sizes above 100MB are slow across the /sdcard bridge (FUSE + sdcardfs). When transferring large files, prefer streaming via `dd` or `tar` straight through a pipe."-}+ "requiresTools": [+ "run_termux_command"+ ],+ "keywords": [+ "file",+ "path",+ "sdcard",+ "/sdcard",+ "storage",+ "home",+ "directory",+ "upload",+ "download"+ ],+ "instructions": "Three filesystem worlds \u2014 keep them separate:\n1. Gotcha's working dir (default cwd for `run_command`, `read_file`, `write_file`, `edit`, `glob`, `grep`): `/storage/emulated/0/Gotcha/chats/<slug_id>/`. INVISIBLE to Termux (different UID).\n2. Termux's home (default `working_dir` for `run_termux_command`): `/data/data/com.termux/files/home`. INVISIBLE to Gotcha.\n3. Shared storage: `/storage/emulated/0/Download`, `/storage/emulated/0/Pictures`, `/storage/emulated/0/Music`. Readable by both UIDs, but ONLY AFTER the user has run `termux-setup-storage` in Termux once. Before that, Termux sees `/sdcard/` as empty.\n\nThe only safe bridge between the two worlds is `/sdcard/Download/`:\n1. Gotcha: write_file('/storage/emulated/0/Download/foo.txt', ...). Termux reads: cat /sdcard/Download/foo.txt.\n2. Termux writes /sdcard/Download/result.txt. Gotcha reads: read_file('/storage/emulated/0/Download/result.txt').\n\nInside Termux, use `/sdcard` (which symlinks to `/storage/emulated/0`). Do NOT use `/storage/emulated/0` directly inside Termux for write paths \u2014 go through `/sdcard`.\n\n`$PREFIX` is `/data/data/com.termux/files/usr`. The model should not guess `/usr/bin` for Termux \u2014 that path is empty.\n\nIf a Gotcha `read_file` on a path that exists in `/sdcard/Download/` fails inside Termux's view, the user has likely not run `termux-setup-storage`. Ask the user to open Termux once and run `termux-setup-storage`, then retry.\n\nTermux's `/data/data/com.termux/files/home` is a real directory, but writing to it from Gotcha via `run_command` would fail with Permission denied \u2014 that is a different UID. Stick to the /sdcard bridge.\n\nFor small-to-medium scripts and payloads <100KB, prefer heredoc/stdin (python3 - << 'PY' / sh -s << 'EOF' via the run_termux_command stdin param) over the /sdcard bridge \u2014 it avoids FUSE overhead and does not require termux-setup-storage. For temporary files inside Termux, prefer $TMPDIR \u2014 private to Termux and survives a single call; use /data/data/com.termux/files/home/.tmp/ as fallback.\n\ncross-uid file sizes above 100MB are slow across the /sdcard bridge (FUSE + sdcardfs). When transferring large files, prefer streaming via `dd` or `tar` straight through a pipe."+}\ No newline at end of filediff --git a/app/src/main/assets/skills/termux_operations.json b/app/src/main/assets/skills/termux_operations.jsonindex 42f5cbe..2396051 100644--- a/app/src/main/assets/skills/termux_operations.json+++ b/app/src/main/assets/skills/termux_operations.json@@ -1,9 +1,22 @@{"id": "termux_operations",- "targetPackageNames": ["com.termux", "com.gotcha"],+ "targetPackageNames": [+ "com.termux",+ "com.gotcha"+ ],"name": "Termux Operations","description": "How to drive Termux through Gotcha: state isolation, prompts, output caps, and timeouts.",- "requiresTools": ["run_termux_command"],- "keywords": ["termux", "shell", "command", "pkg", "apt", "linux", "run_termux_command"],- "instructions": "1. run_termux_command runs under Termux's UID, not Gotcha's. Gotcha's working directory is invisible to Termux, and Termux's home is invisible to Gotcha. The only cross-uid bridge is /sdcard/Download (after `termux-setup-storage` has been run in Termux once).\n2. Each call is a fresh /bin/sh -c. `cd`, `export`, activated virtualenvs, and `source` do NOT carry over. Chain dependent steps with `&&` in one command, never split across calls.\n3. There is no interactive terminal. Anything that waits for typed input (pkg install without -y, ssh password prompt, read, mysql>) blocks until the 600s timeout. Always pass -y / --yes / -q where the tool supports them.\n4. Output is capped at 32KB by Gotcha and ~100KB by Termux. For larger output, redirect to a file inside Termux (`grep ... > /sdcard/Download/out.txt`) and read it back via Gotcha's read_file.\n5. You cannot kill a command after submission. A timed-out command keeps running under Termux's UID. Before launching anything long, warn the user and check existing processes with `ps -ef` before starting a new one.\n6. Hard ceiling: timeout_seconds <= 600. `pkg install` of ffmpeg, chromium, rust, golang, or texlive can hit this. If it does, split into smaller packages or warn the user.\n7. Concurrency cap: 4 commands in flight, process-wide. Sub-agents share the same semaphore. A 5th call returns an error.\n8. Android 12+ background restriction: if Gotcha is in the background and the assistive ball is off, run_termux_command fails with \"Not allowed to start service\" — Termux exposes a foreground service. Tell the user to bring Gotcha to the foreground (or switch the assistive ball on).\n9. The Play-Store build of Termux has no RUN_COMMAND API and can never work. Only F-Droid or GitHub builds are usable.\n10. The four setup steps are: install Termux from F-Droid, confirm the build exposes Run Commands API, grant com.termux.permission.RUN_COMMAND, and set `allow-external-apps=true` in ~/.termux/termux.properties. Walk the user through Settings -> Termux (Linux shell) if any check fails."-}+ "requiresTools": [+ "run_termux_command"+ ],+ "keywords": [+ "termux",+ "shell",+ "command",+ "pkg",+ "apt",+ "linux",+ "run_termux_command"+ ],+ "instructions": "1. run_termux_command runs under Termux's UID, not Gotcha's. Gotcha's working directory is invisible to Termux, and Termux's home is invisible to Gotcha. The only cross-uid bridge is /sdcard/Download (after `termux-setup-storage` has been run in Termux once).\n2. Each call is a fresh /bin/sh -c. `cd`, `export`, activated virtualenvs, and `source` do NOT carry over. Chain dependent steps with `&&` in one command, never split across calls.\n3. There is no interactive terminal. Anything that waits for typed input (pkg install without -y, ssh password prompt, read, mysql>) blocks until the 600s timeout. Always pass -y / --yes / -q where the tool supports them. Prefer quiet flags for installs (pkg install -y -q, pip install -q, git clone --depth 1 -q) to stay under the output cap and speed up inference.\n4. Output is capped at 32KB by Gotcha and ~100KB by Termux. For larger output, redirect inside Termux to $TMPDIR (e.g. `cmd > $TMPDIR/build.log 2>&1 || tail -n 40 $TMPDIR/build.log`) and only surface the tail; for payloads <100KB prefer heredoc/stdin (python3 - << 'PY' / sh -s << 'EOF' via the stdin param) over write_file to /sdcard/Download which goes through FUSE. Use /sdcard/Download only when a file must persist across calls or termux-setup-storage is missing.\n5. You cannot kill a command after submission. A timed-out command keeps running under Termux's UID. Before launching anything long, warn the user and check existing processes with `ps -ef` before starting a new one.\n6. Hard ceiling: timeout_seconds <= 600; adaptive default is 120s (300s for pkg/apt/pip/npm/cargo/proot-distro when no timeout_seconds is passed). `pkg install` of ffmpeg, chromium, rust, golang, or texlive can still hit this. If it does, split into smaller packages or warn the user.\n7. Concurrency cap: 4 commands in flight, process-wide. Sub-agents share the same semaphore. A 5th call returns an error.\n8. Android 12+ background restriction: if Gotcha is in the background and the assistive ball is off, run_termux_command fails with \"Not allowed to start service\" \u2014 Termux exposes a foreground service. Tell the user to bring Gotcha to the foreground (or switch the assistive ball on).\n9. The Play-Store build of Termux has no RUN_COMMAND API and can never work. Only F-Droid or GitHub builds are usable.\n10. The four setup steps are: install Termux from F-Droid, confirm the build exposes Run Commands API, grant com.termux.permission.RUN_COMMAND, and set `allow-external-apps=true` in ~/.termux/termux.properties. Walk the user through Settings -> Termux (Linux shell) if any check fails."+}\ No newline at end of filediff --git a/app/src/main/java/com/gotcha/tools/TermuxTool.kt b/app/src/main/java/com/gotcha/tools/TermuxTool.ktindex 44223e3..db36339 100644--- a/app/src/main/java/com/gotcha/tools/TermuxTool.kt+++ b/app/src/main/java/com/gotcha/tools/TermuxTool.kt@@ -43,7 +43,7 @@ import kotlin.random.Random*/class TermuxTool(private val context: Context,- private val defaultTimeoutSeconds: Int = 60,+ private val defaultTimeoutSeconds: Int = 120,private val maxOutputBytes: Int = 32 * 1024) {@@ -198,7 +198,12 @@ class TermuxTool(if (!status.pluginApiAvailable) return TermuxMessages.pluginApiMissing(status.versionName)if (!status.permissionGranted) return TermuxMessages.permissionNeeded()- val timeout = (timeoutSeconds ?: defaultTimeoutSeconds).coerceIn(1, MAX_TIMEOUT_SECONDS)+ val adaptiveDefault = if (timeoutSeconds == null && PKG_LIKE_REGEX.containsMatchIn(trimmed)) {+ 300+ } else {+ defaultTimeoutSeconds+ }+ val timeout = (timeoutSeconds ?: adaptiveDefault).coerceIn(1, MAX_TIMEOUT_SECONDS)// A timed-out command is not cancelled — it keeps running under Termux's uid, out of our// reach. Without a ceiling, a model that retries a slow `pkg` a few times would leave a// growing pile of live processes behind it, each still holding Termux's package lock.@@ -415,6 +420,9 @@ class TermuxTool(internal const val ERRNO_MINOR_FAILURES = 1 // Activity.RESULT_FIRST_USERinternal const val ERRNO_FAILED = 2 // Activity.RESULT_FIRST_USER + 1+ /** Package-manager commands that are inherently slow and benefit from a larger default timeout. */+ private val PKG_LIKE_REGEX = Regex("""\b(pkg|apt|apt-get|pip3?|npm|cargo|proot-distro)\b""")+/** Shell separators the deny-list splits on before matching each part. */private val SEGMENT_SEPARATORS = Regex("""[;&|\n]+""")