noracu 0.1.3 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -64,12 +64,12 @@ end tell
64
64
  '''
65
65
 
66
66
 
67
- def _run_osascript(script: str, *, strip: bool = True) -> str:
67
+ def _run_osascript(script: str, *, strip: bool = True, timeout: float = 10) -> str:
68
68
  result = subprocess.run(
69
69
  ["osascript", "-e", script],
70
70
  capture_output=True,
71
71
  text=True,
72
- timeout=10,
72
+ timeout=timeout,
73
73
  )
74
74
  if result.returncode != 0:
75
75
  raise WindowError(f"osascript failed: {result.stderr.strip()}")
@@ -101,7 +101,7 @@ def ensure_running() -> None:
101
101
  raise WindowError("iPhone Mirroring did not start within 10s")
102
102
 
103
103
 
104
- def ensure_frontmost() -> None:
104
+ def ensure_frontmost(timeout: float = 10) -> None:
105
105
  """Bring the iPhone Mirroring window to the front.
106
106
 
107
107
  CGEvents are delivered to the frontmost app; if another window
@@ -109,18 +109,19 @@ def ensure_frontmost() -> None:
109
109
  F31: when the app had to be activated (possibly switching Spaces), wait
110
110
  until the window yields a real frame before returning, so the caller's
111
111
  next capture is not a transition frame refused as capture_failed.
112
+ ``timeout`` bounds each osascript run (a tidy-up passes a short one).
112
113
  """
113
- if _run_osascript(_FRONTMOST_SCRIPT) == "true":
114
+ if _run_osascript(_FRONTMOST_SCRIPT, timeout=timeout) == "true":
114
115
  return
115
116
  subprocess.run(
116
117
  ["osascript", "-e", f'tell application "{APP_NAME}" to activate'],
117
118
  capture_output=True,
118
- timeout=10,
119
+ timeout=timeout,
119
120
  )
120
121
  import time
121
122
 
122
123
  time.sleep(0.4) # let the window server settle
123
- if _run_osascript(_FRONTMOST_SCRIPT) != "true":
124
+ if _run_osascript(_FRONTMOST_SCRIPT, timeout=timeout) != "true":
124
125
  raise WindowError("could not bring iPhone Mirroring to front")
125
126
  _wait_capturable()
126
127
 
@@ -402,16 +403,23 @@ def parse_overlay(raw: str) -> dict:
402
403
  def overlay_snapshot() -> tuple[dict, bool]:
403
404
  """(overlay state, hover) from one probe; the state is what get_overlay() returns.
404
405
 
405
- Fail-open: any osascript failure (app not running, AX hiccup, timeout) returns
406
- live=None so callers can decide; this never raises.
406
+ Fail-open at the probe layer: any osascript failure (app not running, AX hiccup,
407
+ timeout) returns live=None so callers can decide; this never raises. When the app
408
+ is not running or has no window on any Space, ``reason`` is set ("not_running" or
409
+ "no_window") so _require_live() can report that without activating the app.
407
410
  """
408
411
  try:
409
412
  # strip=False: a trailing "<class>\t" line (nameless button) must keep its tab —
410
413
  # str.strip() would eat it and make the last line unparseable.
411
414
  raw = _run_osascript(_OVERLAY_SCRIPT, strip=False)
412
415
  except Exception:
416
+ if not is_running():
417
+ return {"live": None, "overlay": None, "buttons": 0, "reason": "not_running"}, False
413
418
  return {"live": None, "overlay": None, "buttons": 0}, False
414
- return parse_overlay_ex(raw)
419
+ state, hover = parse_overlay_ex(raw)
420
+ if raw.strip() == "NO_WINDOW" and get_window_info() is None:
421
+ state = {**state, "reason": "no_window"}
422
+ return state, hover
415
423
 
416
424
 
417
425
  def get_overlay() -> dict:
@@ -419,6 +427,46 @@ def get_overlay() -> dict:
419
427
  return overlay_snapshot()[0]
420
428
 
421
429
 
430
+ # F37 (2026-11-05, macOS 27.2): the first launch after the macOS upgrade showed a welcome
431
+ # screen ("iPhone on Your Mac …") in a 640×660 window. Its button made it look like the paused
432
+ # overlay: phone_status reported paused=true, phone_launch told the user to lock the iPhone, and
433
+ # resume() clicks an overlay's lone button as if it were Resume. The mirror window itself — live
434
+ # or paused — is phone-shaped: elongated, portrait (measured 236×524, 329×727, 354×781 — about
435
+ # 2.2:1) or, for an app that rotates, landscape. A setup, welcome or permission screen is close to
436
+ # square, so a click is only ever sent on an elongated window. 1.7 keeps the 16:9 iPhone SE
437
+ # (≈1.78) in and a wide dialog (e.g. 800×520 ≈ 1.54) out. Geometry, not text: language-
438
+ # independent like the rest of the overlay probe.
439
+ PHONE_MIN_ASPECT = 1.7
440
+
441
+
442
+ def is_phone_shaped(bounds: WindowBounds | None = None) -> bool | None:
443
+ """True when the mirror window is elongated like a phone, portrait or landscape (F37).
444
+
445
+ The long side must be at least PHONE_MIN_ASPECT times the short side. None when the
446
+ bounds cannot be read.
447
+ """
448
+ try:
449
+ b = bounds if bounds is not None else get_bounds()
450
+ except Exception:
451
+ return None
452
+ if b.width <= 0 or b.height <= 0:
453
+ return None # degenerate bounds say nothing about the shape
454
+ long_side, short_side = max(b.width, b.height), min(b.width, b.height)
455
+ return long_side >= PHONE_MIN_ASPECT * short_side
456
+
457
+
458
+ def is_setup_screen(overlay: dict | None = None,
459
+ bounds: WindowBounds | None = None) -> bool:
460
+ """True when an overlay is up in a window that is not phone-shaped (F37).
461
+
462
+ Such a screen (first-run welcome, setup, permission prompt) is the user's to
463
+ finish: resume() never clicks it and phone_launch says so. Unknown bounds → False.
464
+ """
465
+ if overlay is None:
466
+ overlay = get_overlay()
467
+ return overlay["live"] is False and is_phone_shaped(bounds) is False
468
+
469
+
422
470
  def is_paused(overlay: dict | None = None) -> bool | None:
423
471
  """True when the overlay shows a Resume control; False when live; None when unknown.
424
472
 
@@ -493,6 +541,10 @@ def resume() -> bool:
493
541
  return True
494
542
  if overlay["live"] is not False or overlay["buttons"] != 1:
495
543
  return False
544
+ if is_phone_shaped() is not True:
545
+ # F37: a button in a window that is not phone-shaped belongs to a setup /
546
+ # welcome / permission screen — never ours to click. Unknown bounds: no click either.
547
+ return False
496
548
  try:
497
549
  clicked = _run_osascript(_CLICK_RESUME_SCRIPT)
498
550
  except (WindowError, subprocess.TimeoutExpired):
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "noracu",
3
- "version": "0.1.3",
3
+ "version": "0.2.0",
4
4
  "mcpName": "io.github.sgInnora/noracu",
5
5
  "description": "Local MCP server: macOS apps and a browser by element index, plus iPhone Mirroring (experimental).",
6
6
  "type": "module",
@@ -10,9 +10,10 @@
10
10
  },
11
11
  "scripts": {
12
12
  "start": "node src/server.mjs",
13
- "test": "node test/e2e.mjs",
14
- "test:ci": "NORACU_CI=1 node test/e2e.mjs",
15
- "prepublishOnly": "node --check src/server.mjs"
13
+ "test": "node test/engine-unit.mjs && node test/e2e.mjs",
14
+ "test:ci": "node test/engine-unit.mjs && NORACU_CI=1 node test/e2e.mjs",
15
+ "check": "node --check src/server.mjs && node --check src/mac-ax.js && node --check scripts/gen-tools.mjs && python3 -c \"import ast,sys; [ast.parse(open(f).read(), f) for f in sys.argv[1:]]\" imctl/src/imctl/*.py",
16
+ "prepublishOnly": "npm run check && node scripts/check-pack.mjs && node scripts/gen-tools.mjs && git diff --exit-code docs/tools.md"
16
17
  },
17
18
  "engines": {
18
19
  "node": ">=20"