let your agent control your phone
fix: detect a dead session structurally, not by matching phrases
ShawnPanafba7008633f5
diff --git a/src/phone_harness/background.py b/src/phone_harness/background.pyindex 5e35938..99959ae 100644--- a/src/phone_harness/background.py+++ b/src/phone_harness/background.py@@ -71,6 +71,7 @@ _LMOUSE_DOWN, _LMOUSE_UP, _LMOUSE_DRAGGED = 1, 2, 6find_window = mirror.find_windowrunning_app = mirror.running_app+window_ax_content = mirror.window_ax_content# Shared with the mirror backend. Nothing here needs focus, but screen_info()diff --git a/src/phone_harness/helpers.py b/src/phone_harness/helpers.pyindex 3e17ae8..768ead4 100644--- a/src/phone_harness/helpers.py+++ b/src/phone_harness/helpers.py@@ -48,19 +48,39 @@ AGENT_WORKSPACE = Path(# 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.+# Fallback only. The primary check is structural (see connection_state), and+# a phrase list cannot be complete: it missed "Connection Paused" and the Mac+# login screen in English, and misses every interstitial on a non-English+# system._BLOCKED_MARKERS = ("iphone in use", "lock your iphone", "mirroring ended",- "to connect")+ "to connect", "connection paused", "connection interrupted",+ "is locked", "enter the mac login", "try again")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."""+ 'blocked' means an interstitial is up — iPhone in Use, paused, ended,+ connect, or the Mac login screen — and nothing should be tapped or typed+ until the user clears it.++ Detected structurally: the live phone image is a video stream that+ accessibility cannot see into, so a working session exposes no UI inside+ the window, while every interstitial is an ordinary Mac view with labels+ and a button. That holds in any language and for screens Apple has not+ shipped yet, where matching known phrases does neither — the old list+ missed both "Connection Paused" and the Mac login prompt, and reported+ 'ready' for a password field.++ The phrase list is kept as a fallback in case an interstitial exposes no+ accessibility content.+ """if mirror.running_app() is None:return "not-running"if mirror.find_window() is None:return "no-window"+ if mirror.window_ax_content():+ return "blocked"path, win = mirror.capture() # window exists, so this won't launch anythingtexts = " ".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"@@ -87,11 +107,18 @@ def ensure_mirroring():raise RuntimeError("iPhone Mirroring is open but no phone is connected. Please connect ""your phone in the app, then retry.")+ # Quote the interstitial itself rather than guessing which one it is.+ # Button titles are unreliable — the same screen reported 'Connect' once+ # and an empty title a minute later — so only static text is used.+ said = " ".join(t for role, t in mirror.window_ax_content()+ if role == "AXStaticText" and t)+ detail = f" It says: {said}" if said else ""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.")+ "iPhone Mirroring is not connected — an interstitial is on screen."+ + detail+ + " This needs you: clear it on the Mac, 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 cea2be8..ef660cf 100644--- a/src/phone_harness/mirror.py+++ b/src/phone_harness/mirror.py@@ -115,6 +115,82 @@ def running_app():return apps[0] if apps else None+def window_ax_content():+ """Native UI drawn inside the mirroring window, as [(role, text)].++ A connected session returns []. The phone image is a video stream and+ accessibility cannot see into it — that is the same property that forces+ the harness to read the screen with OCR.++ The interstitials macOS draws in that window when the session is not+ usable (iPhone in Use, paused, ended, locked, connect) are ordinary Mac+ views, so they come back with their labels and buttons. Detecting them by+ the presence of real UI rather than by searching for known phrases is what+ makes this work on a non-English system, and on whatever screen Apple adds+ next.++ Observed: 0 elements live, 4 on "iPhone in Use". Button titles are not+ dependable — the same screen reported 'Connect' once and an empty title a+ minute later — so callers should read AXStaticText for anything shown to+ a user.+ """+ app = running_app()+ if app is None:+ return []+ win = find_window()+ if win is None:+ return []+ err, ax_wins = _AS.AXUIElementCopyAttributeValue(+ _AS.AXUIElementCreateApplication(app.processIdentifier()),+ "AXWindows", None)+ if err or not ax_wins:+ return []++ def geom(node):+ e1, pos = _AS.AXUIElementCopyAttributeValue(node, "AXPosition", None)+ e2, size = _AS.AXUIElementCopyAttributeValue(node, "AXSize", None)+ if e1 or e2 or pos is None or size is None:+ return None+ ok, p = _AS.AXValueGetValue(pos, _AS.kAXValueCGPointType, None)+ ok2, sz = _AS.AXValueGetValue(size, _AS.kAXValueCGSizeType, None)+ return (p.x, p.y, sz.width, sz.height) if ok and ok2 else None++ # Match the phone window by geometry. The app also owns a Settings sheet+ # and a Welcome dialog; counting their contents would report a healthy+ # session as blocked.+ target = None+ for w in ax_wins:+ g = geom(w)+ if g and abs(g[0] - win["x"]) < 4 and abs(g[1] - win["y"]) < 4 \+ and abs(g[2] - win["w"]) < 4 and abs(g[3] - win["h"]) < 4:+ target = w+ break+ if target is None:+ return []++ out = []++ def walk(node, depth=0):+ if depth > 6 or len(out) > 40:+ return+ e, role = _AS.AXUIElementCopyAttributeValue(node, "AXRole", None)+ role = "" if e else str(role)+ if role in ("AXStaticText", "AXButton", "AXTextField",+ "AXSecureTextField", "AXImage"):+ parts = []+ for attr in ("AXTitle", "AXValue", "AXDescription"):+ ea, v = _AS.AXUIElementCopyAttributeValue(node, attr, None)+ if not ea and isinstance(v, str) and v.strip():+ parts.append(v.strip())+ out.append((role, " ".join(parts)))+ ek, kids = _AS.AXUIElementCopyAttributeValue(node, "AXChildren", None)+ for k in (kids or []):+ walk(k, depth + 1)++ walk(target)+ return out++def frontmost_window():"""The frontmost normal-layer window, or None — i.e. *which* app is infront. Queried live from the window list, which needs no run loop."""