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: stabilize chat dir mid-run and prefer Termux for complex serve
Gotcha Contributor2f030c87a8fe
diff --git a/app/src/main/assets/skills/local_serve.json b/app/src/main/assets/skills/local_serve.jsonindex d137838..9ed172e 100644--- a/app/src/main/assets/skills/local_serve.json+++ b/app/src/main/assets/skills/local_serve.json@@ -1,9 +1,14 @@{"id": "local_serve",- "targetPackageNames": ["com.gotcha"],+ "targetPackageNames": [+ "com.gotcha"+ ],"title": "Serve Local HTML on Device",- "description": "Build an HTML app in Gotcha chats and serve it on a local port via toybox netcat (Operator mode only).",- "requiresTools": ["write_file", "run_command"],+ "description": "Build an HTML app in Gotcha chats and serve it on a local port \u2014 Termux-first when available, toybox fallback otherwise (Operator mode only).",+ "requiresTools": [+ "write_file",+ "run_command"+ ],"keywords": [],- "instructions": "Purpose: When the user asks to build/develop/create an app, game, page, or tool and does not specify how to run it, make it immediately usable on this phone as a self-contained HTML page served on a local port. This is the smart default when local execution is possible. If the request is genuinely ambiguous (could be Android APK vs web app vs Python script vs Termux), ask one clarifying question: 'Do you want this runnable on this phone as a web app (HTML on a local port), or as an Android app / Python script / Termux program?' Then act on the answer — do not default to asking when the intent is clearly a web-playable game or page.\n1. Write a single self-contained index.html (inline CSS+JS, CDN via https if needed, no local asset dependencies) to /storage/emulated/0/Gotcha/chats/<Slug>_<shortId>/index.html using write_file. Keep it under 100KB. This is the Gotcha world — do NOT use run_termux_command or Termux's $HOME for this file.\n2. Pick a free port: try 8000, then 7777, 8080, 9000. Test with run_command: `toybox nc -z 127.0.0.1 8000 || echo free` — first free wins. Use timeout_seconds=5 for these probes.\n3. Serve it with run_command (Operator mode only; Monitor cannot run this). Do NOT use run_termux_command for Gotcha-world files. Use a persistent loop with HTTP headers so every connection gets text/html:\n `nohup sh -c 'while true; do { printf \"HTTP/1.1 200 OK\\r\\nContent-Type: text/html; charset=utf-8\\r\\nContent-Length: $(wc -c < \"/storage/emulated/0/Gotcha/chats/<Slug>_<id>/index.html\")\\r\\nCache-Control: no-cache\\r\\nAccess-Control-Allow-Origin: *\\r\\nConnection: close\\r\\n\\r\\n\"; cat \"/storage/emulated/0/Gotcha/chats/<Slug>_<id>/index.html\"; } | toybox nc -L -p <PORT> -q 1; done' > /data/local/tmp/serve_<PORT>.log 2>&1 & echo $!`\n If toybox nc -L is single-connection, the while-true loop keeps it alive for the browser and for a verification curl. If nohup is missing, fallback to `sh -c 'while true; do ...; done' &`.\n4. Verify with run_command: `sleep 1; curl -sI http://127.0.0.1:<PORT> | head -n 20` must show HTTP/1.1 200 and Content-Type: text/html. Use timeout_seconds=5. If it fails, `ps | grep toybox` and retry with next free port.\n5. Return to the user immediately: `http://127.0.0.1:<PORT>` and `http://localhost:<PORT>` (both are secure contexts for camera/mic on Android Chrome) plus the file path. Do not wait for a follow-up 'run it at ...' message. Note: server lives until reboot or `pkill -f \"toybox nc.*<PORT>\"` via run_command.\n6. If the user explicitly asked for Termux/Python/APK (e.g., 'run in Termux', 'make an APK'), do not force the web default — honor that and use the appropriate Termux/proot skill instead.\n"-}+ "instructions": "Purpose: When the user asks to build/develop/create an app, game, page, or tool and does not specify how to run it, make it immediately usable on this phone as a self-contained HTML page served on a local port. This is the smart default when local execution is possible. If the request is genuinely ambiguous (could be Android APK vs web app vs Python script vs Termux), ask one clarifying question: 'Do you want this runnable on this phone as a web app (HTML on a local port), or as an Android app / Python script / Termux program?' Then act on the answer \u2014 do not default to asking when the intent is clearly a web-playable game or page.\n1. Write a single self-contained index.html (inline CSS+JS, CDN via https if needed, no local asset dependencies) to index.html via write_file (relative \u2014 resolves to Gotcha chats working dir). Keep it under 100KB. Use relative path so slug renames mid-run do not break it. Gotcha world = /storage/emulated/0/Gotcha/chats/<Slug>_<shortId>/, Termux world = $HOME \u2014 never hardcode an absolute Gotcha path you received earlier in the run; re-resolve via working directory or read_file.\n2. Pick a free port: try 8000, then 7777, 8080, 9000, 8765. Test with run_command when Termux not available, or run_termux_command when it is: `toybox nc -z 127.0.0.1 8000 || echo free` / `ss -tulpn` may be permission denied \u2014 treat Permission denied as free. First free wins. Use timeout_seconds=5.\n3. Serve it \u2014 Termux-first for complex/long-running (user says most commands only work with run_termux_command): Check <env> Termux status. If Termux installed: yes and RUN_COMMAND granted: yes (Operator mode), prefer Termux: `mkdir -p $HOME/<Slug> && cat > $HOME/<Slug>/index.html` via stdin piping the HTML you just wrote (read_file the Gotcha file first, then pipe via stdin to run_termux_command), then `termux-wake-lock; nohup python3 -m http.server <PORT> --directory $HOME/<Slug> > $HOME/<Slug>.log 2>&1 & echo $!` (use $HOME not /tmp, and timeout_seconds=10). If python3 missing, `pkg install python -y -q` first (adaptive 300s). Else fallback to Gotcha toybox: `nohup sh -c 'while true; do { printf \"HTTP/1.1 200 OK\\r\\nContent-Type: text/html; charset=utf-8\\r\\nContent-Length: $(wc -c < \"index.html\")\\r\\nCache-Control: no-cache\\r\\nConnection: close\\r\\n\\r\\n\"; cat \"index.html\"; } | toybox nc -L -p <PORT> -q 1; done' > /data/local/tmp/serve_<PORT>.log 2>&1 & echo $!` via run_command. Do not waste time probing run_command toybox/httpd when Termux is available and the task is serve/build \u2014 go straight to Termux.\n4. Verify with the same world you served from: if Termux, `run_termux_command: curl -sI http://127.0.0.1:<PORT> | head -n 20` must show HTTP/1.1 200; if toybox, `run_command: sleep 1; curl -sI http://127.0.0.1:<PORT>`. Use timeout_seconds=5. If it fails, `ps | grep -E \"python|toybox\"` and retry next free port. Do not fall back to pkill/fuser via run_termux_command to kill \u2014 use notification Exit only for stuck Termux.\n5. Return immediately: `http://127.0.0.1:<PORT>` and `http://localhost:<PORT>` (secure contexts for camera) plus file path. Do not wait for follow-up 'run it at ...'. Server lives until reboot or `pkill -f \"http.server <PORT>\"`/`\"toybox nc.*<PORT>\"` in its own world, or notification Exit for Termux.\n6. If the user explicitly asked for Termux/Python/APK (e.g., 'run in Termux', 'make an APK'), honor that and use the appropriate Termux/proot skill instead of forcing web."+}\ No newline at end of filediff --git a/app/src/main/java/com/gotcha/agent/AgentEngine.kt b/app/src/main/java/com/gotcha/agent/AgentEngine.ktindex ce766a6..3299d05 100644--- a/app/src/main/java/com/gotcha/agent/AgentEngine.kt+++ b/app/src/main/java/com/gotcha/agent/AgentEngine.kt@@ -200,8 +200,13 @@ class AgentEngine(val id = sessionId ?: "unknown"return if (workingDirRoot == GotchaStorage.chatsRoot().absolutePath) {// Chat dirs are named "Slug_shortId" and renamed in place as the- // title becomes known; call working dirs (below) have no title.- GotchaStorage.ensureChatDir(id, currentTitle(), create)+ // title becomes known. During an active run the system prompt already+ // told the agent the working directory, so renaming mid-run would+ // invalidate absolute paths the agent just received (issue in+ // 3923757b Sudoku: Create-a-Sudoku-game vs Sudoku-Game-Creation-Request).+ // Use findOrCreate mid-run to keep the dir stable; real rename happens+ // only in saveCurrentSession when the run is done.+ GotchaStorage.findOrCreateChatDir(id, currentTitle(), create)} else {java.io.File(workingDirRoot, id).also { if (create) it.mkdirs() }}@@ -303,7 +308,16 @@ class AgentEngine())// Rename the chat dir in place now that the real title is known.- setupWorkingDir()+ // Use ensureChatDir (with rename) here, not the mid-run findOrCreate+ // path in setupWorkingDir, so the rename is deferred to session save+ // and does not invalidate absolute paths the agent received mid-run.+ if (workingDirRoot == GotchaStorage.chatsRoot().absolutePath) {+ val dir = GotchaStorage.ensureChatDir(id, title, create = true)+ FileResolver.WORKING_DIR_BASE = dir.absolutePath+ GotchaLog.d(TAG) { "saveCurrentSession: renamed working dir to ${dir.absolutePath}" }+ } else {+ setupWorkingDir()+ }}/** Replaces the persisted run-summary list when switching to another session. */@@ -1077,17 +1091,23 @@ class AgentEngine(// Default to HTML-on-port when the user wants something built but did not// say how to run it — make it runnable on this phone without a follow-up.// Operator-only and suppressed in voice-call brevity mode. The skill- // local_serve carries the exact run_command/toybox recipe; this nudge- // is the cheap index-0 default that causes the agent to fetch it.+ // local_serve carries the exact recipe; this nudge is the cheap index-0+ // default that causes the agent to fetch it. Most complex/long-running+ // commands only work reliably with run_termux_command, so prefer Termux+ // when available — do not waste turns probing run_command toybox when the+ // task needs python/build/serve and Termux is installed.val serveDirective = if (agent == AgentMode.OPERATOR && !callMode) {"\n\nWhen the user asks you to build, develop, or create an app, game, page, or tool " +"and does not specify how to run it, default to a self-contained HTML page " +- "in the Gotcha chats folder and serve it on a free local port with run_command " +- "(toybox nc) so it is immediately usable on this phone — do not wait for a " +- "follow-up 'run it'. If the request is genuinely ambiguous (could be an Android " +- "APK vs a web app vs a Python script vs a Termux program), ask one clarifying " +- "question whether they want it runnable on this phone as a web app or elsewhere " +- "before choosing."+ "in the Gotcha chats folder and serve it on a free local port so it is " ++ "immediately usable on this phone — do not wait for a follow-up 'run it'. " ++ "If Termux is available (check <env> Termux installed: yes), prefer " ++ "run_termux_command: copy the HTML to \$HOME via stdin and serve with " ++ "termux-wake-lock + python3 -m http.server; otherwise use run_command " ++ "toybox nc loop. If the request is genuinely ambiguous (could be an " ++ "Android APK vs a web app vs a Python script vs a Termux program), ask one " ++ "clarifying question whether they want it runnable on this phone as a web app " ++ "or elsewhere before choosing."} else {""}diff --git a/app/src/main/java/com/gotcha/data/GotchaStorage.kt b/app/src/main/java/com/gotcha/data/GotchaStorage.ktindex 1756a55..a629e67 100644--- a/app/src/main/java/com/gotcha/data/GotchaStorage.kt+++ b/app/src/main/java/com/gotcha/data/GotchaStorage.kt@@ -88,6 +88,22 @@ object GotchaStorage {return dir}+ /**+ * Like [ensureChatDir] but never renames — returns the existing dir if any,+ * otherwise creates/returns the desired dir. Used mid-run to avoid+ * invalidating an absolute path the agent already received in the system+ * prompt (`Working directory: .../Slug_id`) when the title is generated+ * mid-run.+ */+ fun findOrCreateChatDir(sessionId: String, title: String, create: Boolean = true): File {+ val existing = findChatDir(sessionId)+ if (existing != null) {+ if (create) existing.mkdirs()+ return existing+ }+ return ensureChatDir(sessionId, title, create)+ }+fun subdir(chatDir: File, kind: Kind): File {val dir = File(chatDir, kind.dirName)dir.mkdirs()