cron-service-anti-pattern.md 5.35 KB

Cron Service Anti-Pattern: "Build a VisualAgent Backend"

The Problem

When a user asks you to "build a VisualAgent backend service" or "create a monitoring daemon" in cron mode, the natural instinct is to:

  1. Write a Python script with execute_code that loops, polls, and executes
  2. Use subprocess to run shell commands
  3. Create a monolithic service that monitors directories and processes commands

This is blocked in cron mode because execute_code is denied for safety (no user present to approve arbitrary Python execution).

The Anti-Pattern

# ANTI-PATTERN — This will be BLOCKED in cron mode
execute_code: {
  "code": """
import subprocess, json, os, time

COMMANDS_DIR = "/sdcard/VisualAgent/commands/"
RESULTS_DIR = "/sdcard/VisualAgent/results/"

def run_su(cmd):
    return subprocess.run(f"su -c '{cmd}'", shell=True, capture_output=True, text=True)

while True:
    files = list_command_files()
    for f in files:
        cmd = read_command_file(f)
        result = execute_command(cmd)
        save_result(result, f)
    time.sleep(1)
"""
}

Result: BLOCKED: execute_code runs arbitrary local Python...

The Correct Decision Tree

User asks to "build a VisualAgent service" in cron mode
│
├─→ Step 1: Check for pre-existing infrastructure
│   ├─→ `ls /data/data/com.termux/files/home/visual_agent*.sh`
│   ├─→ `ls /data/data/com.termux/files/home/visual_agent*.py`
│   ├─→ `ls /sdcard/VisualAgent/` (check if already set up)
│   └─→ If found: USE the existing infrastructure
│       ├─→ Run the existing executor script
│       ├─→ Start the existing daemon if needed
│       └─→ Report on the existing service's status
│
└─→ Step 2: No pre-existing infrastructure found
    ├─→ Use DIRECT MCP tool calls for each operation
    │   ├─→ `mcp_android_phone_screenshot` for screenshots
    │   ├─→ `mcp_android_phone_tap` for taps
    │   ├─→ `mcp_android_phone_swipe` for swipes
    │   ├─→ `mcp_android_phone_input_text` for text input
    │   └─→ `mcp_android_phone_open_app` for app launch
    │
    └─→ Use `terminal` with `su -c` for file I/O
        ├─→ List command files: `su -c 'ls /sdcard/VisualAgent/commands/'`
        ├─→ Read commands: `su -c 'cat /sdcard/VisualAgent/commands/cmd.json'`
        └─→ Write results: `su -c 'echo "..." > /sdcard/VisualAgent/results/result.json'`

Why This Works

  1. No execute_code needed — Each operation is a separate tool call
  2. MCP tools are safe — They are pre-approved, bounded operations
  3. Shell commands are approved — terminal with explicit su -c strings go through the shell-string approval mechanism
  4. Cron-safe — No arbitrary Python execution, no user approval needed

Example: Correct VisualAgent Processing in Cron Mode

# Step 1: Check for existing infrastructure
terminal: "su -c 'ls -la /data/data/com.termux/files/home/visual_agent* 2>/dev/null || echo NO_EXISTING'"
terminal: "su -c 'ls -la /sdcard/VisualAgent/ 2>/dev/null || echo NO_DIR'"

# Step 2: If no infrastructure, check for commands directly
terminal: "su -c 'ls -la /sdcard/VisualAgent/commands/'"

# Step 3: If commands found, read and execute via MCP
terminal: "su -c 'cat /sdcard/VisualAgent/commands/cmd_screenshot.json'"
# → {"type": "screenshot", "id": "test"}

mcp_android_phone_screenshot: {path: "/sdcard/VisualAgent/results/result_screenshot.png"}
# → {"ok": true, "path": "/sdcard/VisualAgent/results/result_screenshot.png", "size_bytes": 919971}

# Step 4: Write result
terminal: "su -c 'echo \"{\\"status\\":\\"success\\",\\"size\\":919971}\" > /sdcard/VisualAgent/results/result.json'"

# Step 5: Archive processed command
terminal: "su -c 'mv /sdcard/VisualAgent/commands/cmd_screenshot.json /sdcard/VisualAgent/processed/'"

Key Insight

You are the service. In cron mode, the Hermes agent itself IS the VisualAgent backend. Each cron run is one iteration of the service loop. You don't need to build a separate daemon — you ARE the daemon. Your job is to:

  1. Check for commands (one check per run)
  2. Execute any found commands (via MCP tools)
  3. Save results (via terminal with su -c)
  4. Archive processed commands (via terminal with su -c)
  5. Report results or [SILENT] if nothing to do

Important: Even terminal with complex commands may be blocked in cron. If a terminal call with su -c 'python3 -c "..."' or a long shell string is rejected, simplify: use shorter commands, avoid nested quotes, and prefer MCP tools over shell-based execution. The most reliable pattern is: terminal for simple ls/cat/mv/echo operations, and MCP tools for everything else.

What NOT to Do

  • ❌ Don't try to write a Python service script with execute_code
  • ❌ Don't try to start a background daemon process
  • ❌ Don't create a polling loop within the cron run
  • ❌ Don't try to bypass the execute_code block with creative workarounds
  • ❌ Don't reimplement what already exists on the device
  • references/cron-execute-code-blocked.md — Detailed explanation of the block and workaround patterns
  • references/pre-existing-visualagent-infrastructure.md — How to discover and use existing VisualAgent installations
  • references/cron-polling-anti-patterns.md — Why repeated directory checks are wasteful