Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

fix(platform): let the macOS AX raise succeed on Sonoma and Cocoa apps

rubenluetke10-beepecb552ead9ac
diff --git a/jarvis/platform/window_state.py b/jarvis/platform/window_state.py
index c4cea72eec..7e40af32c7 100644
--- a/jarvis/platform/window_state.py
+++ b/jarvis/platform/window_state.py
@@ -581,7 +581,15 @@ def _find_and_focus_macos(title_contains: str) -> tuple[bool, str]:
return False, "The matching macOS application exited before it could be focused."
options = NSApplicationActivateIgnoringOtherApps | NSApplicationActivateAllWindows
if not bool(app.activateWithOptions_(options)):
- return False, "macOS refused to activate the matching application."
+ # macOS 14+ "cooperative activation" refuses a background app (which a
+ # tray-launched Jarvis always is during a voice command). The AX raise
+ # below works regardless with the Accessibility grant, so a refusal
+ # here is advisory — aborting turned every Sonoma focus into a failure
+ # for a window the AX calls would have raised anyway.
+ log.debug(
+ "NSRunningApplication activation refused (cooperative activation); "
+ "continuing via the AX raise path"
+ )
root = AXUIElementCreateApplication(pid)
_macos_bound_ax_messaging(root)
@@ -609,12 +617,19 @@ def _find_and_focus_macos(title_contains: str) -> tuple[bool, str]:
else 0
)
raise_error = AXUIElementPerformAction(target, "AXRaise")
- focus_error = AXUIElementSetAttributeValue(root, "AXFocusedWindow", target)
+ # AXFocusedWindow on the APPLICATION element is read-only in AppKit — the
+ # setter returned kAXErrorAttributeUnsupported for every Cocoa app, which
+ # the old all-must-be-zero rule turned into "refused to focus" for windows
+ # that were already standing in front. The settable spelling of the same
+ # intent is AXMain on the WINDOW element; it stays advisory.
+ main_error = AXUIElementSetAttributeValue(target, "AXMain", True)
front_error = AXUIElementSetAttributeValue(root, "AXFrontmost", True)
- if any(
- error != 0
- for error in (front_error, focus_error, restore_error, raise_error)
- ):
+ if main_error != 0:
+ log.debug("AXMain could not be set on the target window: %s", main_error)
+ # Decisive: a window that stayed minimized, or an app that could neither
+ # raise the window nor come frontmost. One of raise/frontmost succeeding
+ # puts the window on top in practice; requiring both was the bug above.
+ if restore_error != 0 or (raise_error != 0 and front_error != 0):
return False, "macOS Accessibility refused to focus the matching window."
return True, target_title
diff --git a/tests/unit/plugins/tool/test_switch_window.py b/tests/unit/plugins/tool/test_switch_window.py
index b10a05d767..b8991d2e33 100644
--- a/tests/unit/plugins/tool/test_switch_window.py
+++ b/tests/unit/plugins/tool/test_switch_window.py
@@ -33,7 +33,14 @@ def _ctx() -> ExecutionContext:
# --- macOS (Quartz/AppKit/AX) ----------------------------------------------
-def _install_fake_macos_apis(monkeypatch, *, trusted=True, minimized=False):
+def _install_fake_macos_apis(
+ monkeypatch,
+ *,
+ trusted=True,
+ minimized=False,
+ activate_result=True,
+ set_attr_error=None,
+):
from jarvis.platform.permissions import PermissionState
ax_window = object()
@@ -43,7 +50,7 @@ def _install_fake_macos_apis(monkeypatch, *, trusted=True, minimized=False):
class _App:
def activateWithOptions_(self, options):
calls.append(("activate", options))
- return True
+ return activate_result
appkit = types.SimpleNamespace(
NSApplicationActivateAllWindows=1,
@@ -71,7 +78,7 @@ def _install_fake_macos_apis(monkeypatch, *, trusted=True, minimized=False):
) or 0,
AXUIElementSetAttributeValue=lambda element, attr, value: calls.append(
("set", attr)
- ) or 0,
+ ) or (set_attr_error or {}).get(attr, 0),
)
monkeypatch.setitem(sys.modules, "AppKit", appkit)
monkeypatch.setitem(sys.modules, "ApplicationServices", services)
@@ -110,8 +117,13 @@ def test_macos_focus_success(monkeypatch):
found, msg = _find_and_focus_macos("Editor")
assert found is True
assert msg == "My Editor"
- assert ("set", "AXFocusedWindow") in calls
+ assert ("set", "AXMain") in calls
+ assert ("set", "AXFrontmost") in calls
assert ("perform", "AXRaise") in calls
+ # AXFocusedWindow on the app element is read-only in AppKit — setting it
+ # returned attribute-unsupported for every Cocoa app and failed the whole
+ # focus. The settable spelling is AXMain on the window (see the fix).
+ assert ("set", "AXFocusedWindow") not in calls
def test_macos_focus_restores_minimized_window_from_all_window_catalog(monkeypatch):
@@ -134,7 +146,47 @@ def test_macos_focus_restores_minimized_window_from_all_window_catalog(monkeypat
assert ("set", "AXMinimized") in calls
assert ("perform", "AXRaise") in calls
assert calls.index(("set", "AXMinimized")) < calls.index(("perform", "AXRaise"))
- assert calls.index(("perform", "AXRaise")) < calls.index(("set", "AXFocusedWindow"))
+ assert calls.index(("perform", "AXRaise")) < calls.index(("set", "AXMain"))
+
+
+def test_macos_focus_survives_cooperative_activation_refusal(monkeypatch):
+ """macOS 14+ refuses activation from a background app; AX must still raise."""
+ calls = _install_fake_macos_apis(monkeypatch, activate_result=False)
+ monkeypatch.setattr(
+ "jarvis.platform.window_state._quartz_window_list",
+ lambda **_kwargs: [_mac_window()],
+ )
+ found, msg = _find_and_focus_macos("Editor")
+ assert found is True
+ assert msg == "My Editor"
+ assert ("perform", "AXRaise") in calls
+
+
+def test_macos_focus_tolerates_a_read_only_main_attribute(monkeypatch):
+ """An advisory AXMain refusal must not fail a raise that succeeded."""
+ _install_fake_macos_apis(monkeypatch, set_attr_error={"AXMain": -25205})
+ monkeypatch.setattr(
+ "jarvis.platform.window_state._quartz_window_list",
+ lambda **_kwargs: [_mac_window()],
+ )
+ found, msg = _find_and_focus_macos("Editor")
+ assert found is True
+ assert msg == "My Editor"
+
+
+def test_macos_focus_fails_when_raise_and_frontmost_both_fail(monkeypatch):
+ _install_fake_macos_apis(
+ monkeypatch, set_attr_error={"AXFrontmost": -25204, "AXMain": -25204}
+ )
+ services = sys.modules["ApplicationServices"]
+ services.AXUIElementPerformAction = lambda element, action: -25204
+ monkeypatch.setattr(
+ "jarvis.platform.window_state._quartz_window_list",
+ lambda **_kwargs: [_mac_window()],
+ )
+ found, msg = _find_and_focus_macos("Editor")
+ assert found is False
+ assert "refused to focus" in msg
def test_macos_accessibility_denied_message(monkeypatch):