let your agent control your phone
connection: detect not-connected states and hand off to the user, never auto-connect
ShawnPana720eaeb7b888
diff --git a/SKILL.md b/SKILL.mdindex 633b3de..a3f19ce 100644--- a/SKILL.md+++ b/SKILL.md@@ -65,6 +65,22 @@ hard to reverse: sending a message, posting, purchasing, deleting, changingsettings. Navigating and reading for the user's own task is fine, but don'tlinger in personal content (Messages, Photos, Mail) beyond what the task needs.+## Connection is the user's job++The harness never connects the phone for you. Connecting or resuming mirroring+is a physical action — opening the app, approving the prompt, and (crucially)+**locking the iPhone when it says "iPhone in Use"** — that only the user can do.++`ensure_mirroring()` gates every task on this: if the phone isn't connected it+raises a clear message (call `connection_state()` yourself to check —+`ready` / `blocked` / `no-window` / `not-running`). When you hit that:++- **STOP and relay the message. Ask the user to connect the phone themselves.**+- **Never** tap `Connect` / `Continue`, and **never** loop-poll waiting for the+ connection. Tapping Connect while the phone is unlocked does nothing, and+ polling just burns time — the only fix is the user locking/connecting the+ phone. Retry once *after they confirm they've done it*, not before.+## Gotchas- **Unfocused input is swallowed silently.** The window must be frontmost;@@ -73,9 +89,9 @@ linger in personal content (Messages, Photos, Mail) beyond what the task needs.it; AppleScript `click at` fails silently. Only HID-level CGEvents work.- **The window moves.** Never cache coordinates across calls; `ocr()` and`swipe()` re-query bounds every time.-- **Unlocking the physical phone pauses the session.** The window then shows a- resume interstitial — `ocr()` will show its text; tap through it or ask the- user to lock the phone.+- **Unlocking the physical phone pauses the session** ("iPhone in Use"). Do not+ tap through the resume screen — stop and ask the user to lock/connect the+ phone (see "Connection is the user's job").- **`type_text` needs an iOS text field focused first** — tap the field, waitfor the keyboard, then type.- **Home-Screen labels are not tap targets.** `tap_text("Weather")` hits thediff --git a/src/phone_harness/helpers.py b/src/phone_harness/helpers.pyindex c40c441..ab95c51 100644--- a/src/phone_harness/helpers.py+++ b/src/phone_harness/helpers.py@@ -21,11 +21,53 @@ AGENT_WORKSPACE = Path(# --- session / state ---+# Distinctive text on the not-connected interstitials. Reconnecting past any of+# these is a PHYSICAL action only the user can do (open the app, and if it says+# "iPhone in Use", LOCK the phone). The agent must never tap through them.+_BLOCKED_MARKERS = ("iphone in use", "lock your iphone", "mirroring ended",+ "to connect")+++def connection_state():+ """'ready' | 'blocked' | 'no-window' | 'not-running'.++ 'blocked' means a connect / 'iPhone in Use' / paused interstitial is on+ screen. Cheap to call; use it to decide whether to proceed."""+ if mirror.running_app() is None:+ return "not-running"+ if mirror.find_window() is None:+ return "no-window"+ path, win = mirror.capture() # window exists, so this won't launch anything+ texts = " ".join(o["text"] for o in _ocr.recognize(path, win)).lower()+ return "blocked" if any(m in texts for m in _BLOCKED_MARKERS) else "ready"++def ensure_mirroring():- """Launch + focus iPhone Mirroring; return window bounds {x, y, w, h, id}."""- win = mirror.ensure_window()- mirror.activate()- return win+ """Return the mirroring window if the phone is connected and ready.++ Never launches the app, taps Connect/Continue, or polls to reconnect —+ resuming mirroring is physical and only the user can do it. If the session+ isn't ready, raises with instructions for the user. STOP and relay that+ message; do not try to tap through the connect screen yourself.+ """+ state = connection_state()+ if state == "ready":+ mirror.activate()+ return mirror.find_window()+ if state == "not-running":+ raise RuntimeError(+ "iPhone Mirroring isn't running. Please open the iPhone Mirroring "+ "app and connect your phone, then retry — reconnecting is physical, "+ "so I can't do it for you.")+ if state == "no-window":+ raise RuntimeError(+ "iPhone Mirroring is open but no phone is connected. Please connect "+ "your phone in the app, then retry.")+ raise RuntimeError(+ "iPhone Mirroring is not connected — it's showing a connect / 'iPhone "+ "in Use' screen. This needs you: open iPhone Mirroring and connect the "+ "phone, and if it says 'iPhone in Use', LOCK your iPhone so mirroring "+ "can resume. Then retry. I will not tap Connect for you.")def screen_info():diff --git a/src/phone_harness/mirror.py b/src/phone_harness/mirror.pyindex 15f2530..57d01d1 100644--- a/src/phone_harness/mirror.py+++ b/src/phone_harness/mirror.py@@ -46,36 +46,36 @@ def is_frontmost():def activate():+ """Bring iPhone Mirroring frontmost. Does NOT launch it — opening the app+ and connecting the phone is the user's job, not the agent's."""app = running_app()if app is None:- subprocess.run(["open", "-a", APP_NAME], check=True)- deadline = time.time() + 15- while app is None and time.time() < deadline:- time.sleep(0.5)- app = running_app()- if app is None:- raise RuntimeError(f"{APP_NAME} did not launch")+ raise RuntimeError(+ f"{APP_NAME} isn't running — open it and connect your phone.")if not is_frontmost():app.activateWithOptions_(1 << 1) # NSApplicationActivateIgnoringOtherAppstime.sleep(0.5)-def ensure_window(timeout=15.0):- """Launch + focus the mirroring app if needed; return window bounds.+def ensure_window(timeout=5.0):+ """Return the mirroring window bounds. Does not launch or connect anything —+ if the app isn't running or has no phone window, raises so the user can+ connect it themselves.The window exists even while the session shows a paused/connect- interstitial — detecting those is the caller's job (OCR the capture).+ interstitial — detecting those is connection_state()'s job."""win = find_window()if win is None:- activate()+ activate() # frontmost if running; raises if not runningdeadline = time.time() + timeoutwhile win is None and time.time() < deadline:time.sleep(0.5)win = find_window()if win is None:raise RuntimeError(- f"{APP_NAME} has no window — is the phone paired and in range?")+ f"{APP_NAME} has no phone window — connect your phone in the "+ "app, then retry.")return win