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.

perf(macos): bound AX messaging and the open_app focus probe — kill the 15s already-running stall

Personal Jarvis Maintainerc6935559f33f
diff --git a/jarvis/platform/window_state.py b/jarvis/platform/window_state.py
index f9ca5193e7..daee1b304b 100644
--- a/jarvis/platform/window_state.py
+++ b/jarvis/platform/window_state.py
@@ -357,6 +357,33 @@ def _select_macos_ax_window(
return None
+#: Per-call ceiling for AX messages to another app, in seconds. The macOS
+#: default is ~6 s PER CALL, so a busy target (Safari mid-page-load) could
+#: hold a raise/focus sequence of 4-5 AX calls for well over the CU action
+#: timeout — live 2026-07-21: open_app('Safari') burned the full 15 s
+#: _ACT_TIMEOUT_S in exactly this path while Safari was already open.
+_AX_MESSAGING_TIMEOUT_S = 1.5
+
+
+def _macos_bound_ax_messaging(
+ element, timeout_s: float = _AX_MESSAGING_TIMEOUT_S,
+) -> None:
+ """Cap the AX messaging timeout for calls through ``element`` (best-effort).
+
+ Applies to the element it is set on (setting it on the app root bounds the
+ calls made through that root). A missing symbol or native error is
+ ignored — the call sequence then simply keeps the OS default.
+ """
+ try:
+ from ApplicationServices import ( # type: ignore[import-not-found] # noqa: PLC0415
+ AXUIElementSetMessagingTimeout,
+ )
+
+ AXUIElementSetMessagingTimeout(element, float(timeout_s))
+ except Exception: # noqa: BLE001 — advisory bound only
+ log.debug("AXUIElementSetMessagingTimeout unavailable", exc_info=True)
+
+
def _find_and_focus_macos(title_contains: str) -> tuple[bool, str]:
"""Activate and AX-raise a matching macOS window without AppleScript.
@@ -426,6 +453,7 @@ def _find_and_focus_macos(title_contains: str) -> tuple[bool, str]:
return False, "macOS refused to activate the matching application."
root = AXUIElementCreateApplication(pid)
+ _macos_bound_ax_messaging(root)
windows = list(_macos_ax_attr(root, "AXWindows") or [])
owner_name = str(matched_entry.get("kCGWindowOwnerName") or "")
target_title = _macos_window_title(matched_entry)
@@ -439,6 +467,8 @@ def _find_and_focus_macos(title_contains: str) -> tuple[bool, str]:
"The application activated, but its matching AX window was "
"unavailable or ambiguous."
)
+ # The timeout is per-element: bound the window element too, not only root.
+ _macos_bound_ax_messaging(target)
target_title = str(_macos_ax_attr(target, "AXTitle") or target_title)
minimized = bool(_macos_ax_attr(target, "AXMinimized") or False)
diff --git a/jarvis/plugins/tool/open_app.py b/jarvis/plugins/tool/open_app.py
index 98e593f7a1..4ba0486357 100644
--- a/jarvis/plugins/tool/open_app.py
+++ b/jarvis/plugins/tool/open_app.py
@@ -10,6 +10,7 @@ land directly in os.startfile.
from __future__ import annotations
import asyncio
+import logging
import os
import re
import shutil
@@ -28,6 +29,8 @@ from jarvis.voice.action_phrases import action_phrase, resolve_phrase_language
# Apps for which a second window is normal/expected — never short-circuit these
# to "already running -> focus"; the user almost always wants a fresh instance.
+logger = logging.getLogger(__name__)
+
_MULTI_INSTANCE_APPS: frozenset[str] = frozenset({
"explorer", "cmd", "powershell", "pwsh", "wt", "windowsterminal",
"terminal", "conhost", "gnome-terminal", "konsole", "xterm",
@@ -308,19 +311,38 @@ class OpenAppTool:
or app_name.startswith((".", "\\", "/"))
)
if reuse_existing and not is_url and not is_path and low not in _MULTI_INSTANCE_APPS:
- running = window_state.is_app_running(app_name)
- if running is not None:
- # Raise by the known window handle through the HARDENED path —
- # a plain focus is refused under the Windows foreground-lock, so
- # an already-open-but-backgrounded app (the WhatsApp report)
- # stayed behind everything. raise_window defeats that lock.
- focused, _msg = window_state.raise_window(running)
- if focused:
- return ToolResult(
- success=True,
- output=f"{app_name} is already running — brought it to the front.",
+ # Off the event loop AND hard-bounded: the macOS raise path talks
+ # AX to the target app, and a busy target once held this for the
+ # CU dispatcher's entire 15 s action timeout (live 2026-07-21,
+ # Safari mid-load). On a timeout we simply fall through to the
+ # normal launch — `open -a` focuses a running app anyway.
+ try:
+ async with asyncio.timeout(4.0):
+ running = await asyncio.to_thread(
+ window_state.is_app_running, app_name,
)
- # focus failed (e.g. foreground-lock) -> fall through to launch
+ focused = False
+ if running is not None:
+ # Raise by the known window handle through the
+ # HARDENED path — a plain focus is refused under the
+ # Windows foreground-lock, so an already-open-but-
+ # backgrounded app (the WhatsApp report) stayed
+ # behind everything. raise_window defeats that lock.
+ focused, _msg = await asyncio.to_thread(
+ window_state.raise_window, running,
+ )
+ except TimeoutError:
+ focused = False
+ logger.warning(
+ "[open_app] already-running focus probe for %r exceeded "
+ "4s — falling through to a normal launch", app_name,
+ )
+ if focused:
+ return ToolResult(
+ success=True,
+ output=f"{app_name} is already running — brought it to the front.",
+ )
+ # focus failed (e.g. foreground-lock) -> fall through to launch
try:
launch_target = resolve_app_launch_target(app_name)
diff --git a/jarvis/vision/ax_tree.py b/jarvis/vision/ax_tree.py
index ccaef67a94..65151589a4 100644
--- a/jarvis/vision/ax_tree.py
+++ b/jarvis/vision/ax_tree.py
@@ -311,6 +311,18 @@ class AXTreeSource:
logger.debug("AXUIElementCreateApplication failed", exc_info=True)
return (window_title, pid, [])
+ # Bound each AX round trip: the OS default is ~6 s PER CALL, so one
+ # busy app could blow straight through the walk's 1.2 s wall-clock
+ # budget (the deadline is only checked BETWEEN calls). Best-effort.
+ try:
+ from ApplicationServices import ( # type: ignore[import-not-found] # noqa: PLC0415
+ AXUIElementSetMessagingTimeout,
+ )
+
+ AXUIElementSetMessagingTimeout(root, 0.5)
+ except Exception: # noqa: BLE001 — advisory bound only
+ logger.debug("AXUIElementSetMessagingTimeout unavailable", exc_info=True)
+
# The engine layer decides whether to discard a non-matching window —
# hard-filtering here would be more brittle (mirrors the Windows source).
_ = window_title_filter