← All essays

NO. 06 Builder Notes

What people actually building with local AI and phone agents learned this year

Four hard engineering lessons from shipping on-device Gemma, 53 Android tools, prompt caching, and a rootless Debian PTY workspace.

Gestu Build running an interactive coding agent inside a real PTY session on Android—real resize, real mouse reporting, no mangled ANSI grid.
Real capture · Gestu on Android

Gestu Build running an interactive coding agent inside a real PTY session on Android—real resize, real mouse reporting, no mangled ANSI grid.

Spend time on r/LocalLLaMA, r/termux, or Hacker News and you quickly spot two camps talking about AI agents. One draws twelve-box autonomy diagrams. The other ships code onto real phones and desktops—and discovers why naive demos collapse the moment they meet a 4B quantized model, a 32K context window, or an untrusted email header.

Building Gestu through v2.6.6 taught us four concrete engineering lessons that separate a demo from an assistant you can live with.

1. Never hand a 4B local model 53 tool schemas

Advertising every tool on every turn looks neat on a feature matrix. On a 2B–4B on-device LiteRT/Gemma model, dumping 53 JSON schemas plus a dynamic-UI catalogue into the system prompt crushes prefill latency, blows KV cache, and triggers malformed tool calls.

In Gestu, ChatSystemPromptBuilder compiles two distinct variants: CHAT_REMOTE and CHAT_LOCAL. On-device turns scope the allowlist tightly, cap injected memories at a hard 2,000-character boundary (never slicing mid-entry), and omit the heavy UI schema. On Windows GGUF runs (llama-server), compaction queries the live tokenizer—counting the actual system prompt, history, attachments, and tool schemas—and compacts proactively at 85% of context using the local engine itself so private turns never leak to a remote summarizer.

2. Prompt-prefix order is a caching contract

In multi-turn tool loops against remote providers, prefix caching only hits when leading bytes stay bit-for-bit identical turn over turn. Putting live counters—unread email counts or a ticking clock—at the top of the prompt silently invalidates the entire cache every message.

We enforce prompt order as a strict contract:

[ CACHED INVARIANT PREFIX ]
  Soul & Persona → Honesty / Tool Use / Acting rules
  → Memory & Automation instructions → Dynamic UI schema
  → Runtime Context (no clock; timestamp rides on newest user turn)

[ MUTABLE SESSION TAIL ]
  Heartbeat Additions → Per-conversation-frozen Memories
  → Active /slash Skill (zero bytes on every other turn)

Live counters moved to on-demand tools (list_tasks, get_email_status), and newly saved memories freeze for the active thread unless deleted or edited.

3. Unattended background runs are a prompt-injection surface

An unattended Heartbeat prompt is built from untrusted strings written by strangers—email subjects, SMS previews, notification text. Giving that loop unrestricted access to compose_email, send_sms_direct, Accessibility screen taps, or schedule_task invites a spam message to hijack your device in your pocket.

ToolExecutor enforces an allowlist (ToolPolicy.UNATTENDED_ALLOWED_TOOLS) at one chokepoint—never a denylist, so new tools fail closed automatically:

  • Allowed unattended: reading opt-in mail/SMS/notifications, web lookup, memory, workspace sandbox commands, and SMS drafting (stages a draft for human tap).
  • Blocked unattended: sending email/SMS, placing calls, Accessibility taps, deleting mail/calendar items, Soul promotion, and creating/cancelling schedules (preventing persistent trigger planting).

4. Real terminal agents need real PTY plumbing—and pinned installers

Running CLI coding agents (Claude Code, OpenCode, Grok, DeepSeek Harness) inside Gestu Pro's Android proot Debian 12 sandbox or Windows WSL2 Gestu Build workspace breaks immediately on guessed font metrics.

Gestu Build measures real character cell advances, sends genuine TIOCSWINSZ resizes (stty rows R cols C), translates desktop mouse-wheel events into DECSET 1006 SGR sequences only in alternate-screen mode, and bridges Windows host local LLM ports (Ollama 11434, LM Studio 1234, llama.cpp 8080, vLLM 8000) into WSL2 via $WINDOWS_HOST_IP—while unloading idle LiteRT weights from RAM/VRAM before a coding session starts. We also pin tested installers (keeping OpenCode on 1.x after verifying 2.x died with EIO on our PTY) while letting users opt into updates explicitly.

The builders pulling ahead with local agents aren't chasing the loudest demo. They're respecting memory budgets, protecting caches, and drawing hard lines around untrusted input—which is why our changelog reads like engineering notes instead of marketing confetti.

Inspect the real thing

Built for people who read changelogs.

Apache-2.0 app shell, signed FOSS APK with published SHA-256, 31 BYOK providers + LAN Ollama on Free—and Gestu Pro unlocking Debian 12, Gestu Build, Host Root, and MCP servers.