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.
Python agent-orchestrationai-agentai-coding-assistantclaude-codecoding-agentcomputer-usedesktop-applocal-firstmcppersonal-jarvisvoice-assistantvoice-coding
feat(desktop): log the JavaScript a hung window is stuck in before recovering it
rubenluetke10-beep00ee687d5e1c
diff --git a/jarvis/ui/desktop_app.py b/jarvis/ui/desktop_app.pyindex 7b8441b577..78e2e0a638 100644--- a/jarvis/ui/desktop_app.py+++ b/jarvis/ui/desktop_app.py@@ -5592,6 +5592,7 @@ class DesktopApp:the tray's reopen), so both are covered by one guard with one policy."""try:+ from jarvis.ui.webview_hang_probe import HangStackProbefrom jarvis.ui.window_watchdog import BlankWindowWatchdogif self._blank_watchdog is not None:@@ -5605,6 +5606,7 @@ class DesktopApp:warming_probe=self._backend_warming,failure_detail=self._blank_window_detail,theme=self._resolved_theme,+ stack_probe=HangStackProbe(webview_storage_dir(), self._url()),)self._blank_watchdog.start()except Exception: # noqa: BLE001@@ -5694,6 +5696,12 @@ class DesktopApp:)enable_webview_accessibility_tree()+ # A local DevTools endpoint, so a hung page can be asked where its+ # script is stuck before the watchdog ends the renderer (see+ # jarvis/ui/webview_hang_probe.py). WebView2 only; a no-op elsewhere.+ from jarvis.ui.webview_hang_probe import enable_webview_remote_debugging++ enable_webview_remote_debugging()try:import webview # type: ignore[import-not-found]except ModuleNotFoundError as exc:diff --git a/jarvis/ui/webview_hang_probe.py b/jarvis/ui/webview_hang_probe.pynew file mode 100644index 0000000000..84a65a407d--- /dev/null+++ b/jarvis/ui/webview_hang_probe.py@@ -0,0 +1,269 @@+"""Name the JavaScript that hung the desktop window, before it is recovered.++The blank-window watchdog (``jarvis/ui/window_watchdog.py``) heals a hung page+by ending its renderer and reloading. That brings the window back, but it+destroys the only evidence of WHY it hung: a renderer spinning one core in a+script cannot answer ``evaluate_js``, cannot report its own long task, and the+desktop app has no dev tools. Live 2026-10-02 the window hung three times in+one afternoon and every trace was lost with the renderer.++What still reaches a page whose main thread never yields is the DevTools+protocol's ``Debugger.pause``: Chromium delivers it on the IO thread and V8+interrupts the running script, wherever it is. So the probe keeps one+DevTools session on the main page with the debugger enabled (that part needs+the main thread, so it happens while the page is healthy), and on a hang+pauses the page a few times, logs the call frames with a slice of the source+around each, and lets it go again. The renderer is ended right after anyway.++Windows/WebView2 only: the session goes through Chromium's remote-debugging+endpoint, opened on a random local port (``--remote-debugging-port=0``) whose+number Chromium writes into the profile directory. Elsewhere — WKWebView,+WebKitGTK, Qt — nothing is opened and :meth:`HangStackProbe.capture` returns+an empty list. The endpoint listens on loopback only and is reachable only by+someone who can read this user's profile directory, which already holds the+app's own session state.+"""++from __future__ import annotations++import json+import os+import sys+import threading+import time+import urllib.request+from collections.abc import MutableMapping+from pathlib import Path+from typing import Any++from loguru import logger++from jarvis.platform.webview_accessibility import WEBVIEW2_ARGS_ENV++#: Asks WebView2 for a DevTools endpoint on a free loopback port.+REMOTE_DEBUGGING_FLAG = "--remote-debugging-port=0"++#: How many times the hung page is paused; several samples show the loop.+SAMPLES = 3++#: Frames logged per sample — the loop sits at the top, its caller below.+MAX_FRAMES = 12++#: Characters of source shown on each side of a frame's position.+SNIPPET_RADIUS = 90+++def enable_webview_remote_debugging(+ *, env: MutableMapping[str, str] | None = None, platform: str | None = None+) -> bool:+ """Open the DevTools endpoint the probe needs, before the WebView starts.++ Appended to the shared WebView2 argument variable (never assigned), like+ the accessibility flag. Returns whether the flag is in place.+ """+ if (platform or sys.platform) != "win32":+ return False+ target = os.environ if env is None else env+ existing = target.get(WEBVIEW2_ARGS_ENV, "")+ if "--remote-debugging-port" in existing:+ return True+ target[WEBVIEW2_ARGS_ENV] = f"{existing} {REMOTE_DEBUGGING_FLAG}".strip()+ return True+++def _active_port(profile_dir: Path) -> int | None:+ """The port Chromium chose, from ``DevToolsActivePort`` in the profile."""+ for candidate in (+ profile_dir / "DevToolsActivePort",+ profile_dir / "EBWebView" / "DevToolsActivePort",+ ):+ try:+ first = candidate.read_text(encoding="utf-8").splitlines()[0].strip()+ return int(first)+ except (OSError, IndexError, ValueError):+ continue+ return None+++class _Session:+ """One DevTools websocket to a page, with a reader thread."""++ def __init__(self, ws_url: str) -> None:+ from websockets.sync.client import connect++ self._ws = connect(ws_url, open_timeout=5, max_size=None, origin=None)+ self._next_id = 0+ self._send_lock = threading.Lock()+ self._cv = threading.Condition()+ self._replies: dict[int, dict[str, Any]] = {}+ self._paused: list[dict[str, Any]] = []+ self._scripts: dict[str, str] = {}+ self.alive = True+ threading.Thread(target=self._read, name="webview-hang-probe", daemon=True).start()++ def _read(self) -> None:+ try:+ for raw in self._ws:+ msg = json.loads(raw)+ with self._cv:+ if "id" in msg:+ self._replies[msg["id"]] = msg+ elif msg.get("method") == "Debugger.scriptParsed":+ params = msg.get("params", {})+ self._scripts[params.get("scriptId", "")] = params.get("url", "")+ elif msg.get("method") == "Debugger.paused":+ self._paused.append(msg.get("params", {}))+ self._cv.notify_all()+ except Exception as exc: # noqa: BLE001 — a closed page ends the session+ logger.debug("webview hang probe: session closed ({})", exc)+ finally:+ with self._cv:+ self.alive = False+ self._cv.notify_all()++ def send(self, method: str, params: dict[str, Any] | None = None) -> int:+ with self._send_lock:+ self._next_id += 1+ mid = self._next_id+ self._ws.send(json.dumps({"id": mid, "method": method, "params": params or {}}))+ return mid++ def call(+ self, method: str, params: dict[str, Any] | None = None, *, wait: float = 5.0+ ) -> dict[str, Any] | None:+ mid = self.send(method, params)+ deadline = time.monotonic() + wait+ with self._cv:+ while mid not in self._replies and self.alive:+ left = deadline - time.monotonic()+ if left <= 0:+ return None+ self._cv.wait(left)+ return self._replies.pop(mid, None)++ def wait_paused(self, wait: float) -> dict[str, Any] | None:+ deadline = time.monotonic() + wait+ with self._cv:+ while not self._paused and self.alive:+ left = deadline - time.monotonic()+ if left <= 0:+ return None+ self._cv.wait(left)+ return self._paused.pop(0) if self._paused else None++ def script_url(self, script_id: str) -> str:+ with self._cv:+ return self._scripts.get(script_id, "")++ def close(self) -> None:+ self.alive = False+ try:+ self._ws.close()+ except Exception as exc: # noqa: BLE001 — closing a dead socket is best effort+ logger.debug("webview hang probe: close failed ({})", exc)+++class HangStackProbe:+ """Keeps the debugger armed on the main page; names the hung code on demand."""++ def __init__(self, profile_dir: Path | None, page_url: str) -> None:+ self._profile_dir = profile_dir+ self._page_prefix = page_url.split("?", 1)[0].rstrip("/")+ self._session: _Session | None = None+ self._lock = threading.Lock()+ self._warned = False++ def arm(self) -> None:+ """Attach and enable the debugger while the page is healthy. Never raises."""+ with self._lock:+ if self._session is not None and self._session.alive:+ return+ try:+ self._session = self._attach()+ except Exception as exc: # noqa: BLE001 — diagnostics must never cost the window+ self._session = None+ if not self._warned:+ self._warned = True+ logger.info("webview hang probe unavailable: {}", exc)++ def _attach(self) -> _Session | None:+ if self._profile_dir is None:+ return None+ port = _active_port(self._profile_dir)+ if port is None:+ return None+ with urllib.request.urlopen(f"http://127.0.0.1:{port}/json/list", timeout=3) as resp:+ targets = json.load(resp)+ page = next(+ (+ t+ for t in targets+ if t.get("type") == "page"+ and str(t.get("url", "")).split("?", 1)[0].rstrip("/") == self._page_prefix+ ),+ None,+ )+ if page is None:+ return None+ session = _Session(page["webSocketDebuggerUrl"])+ if session.call("Debugger.enable", wait=10) is None:+ session.close()+ return None+ return session++ def capture(self) -> list[str]:+ """Pause the hung page a few times and describe where it was. Never raises."""+ with self._lock:+ session = self._session+ if session is None or not session.alive:+ return []+ lines: list[str] = []+ try:+ for sample in range(SAMPLES):+ session.send("Debugger.pause")+ paused = session.wait_paused(5.0)+ if paused is None:+ lines.append(f"sample {sample + 1}: the page did not pause")+ break+ frames = paused.get("callFrames", [])[:MAX_FRAMES]+ lines.append(f"sample {sample + 1}: {len(frames)} frame(s)")+ for index, frame in enumerate(frames):+ lines.append(+ " " + self._describe(session, index, frame, with_source=sample == 0)+ )+ session.call("Debugger.resume", wait=5)+ time.sleep(0.3)+ except Exception as exc: # noqa: BLE001+ lines.append(f"capture stopped: {exc}")+ return lines++ @staticmethod+ def _describe(+ session: _Session, index: int, frame: dict[str, Any], *, with_source: bool+ ) -> str:+ loc = frame.get("location", {})+ script_id = loc.get("scriptId", "")+ line = int(loc.get("lineNumber", 0))+ col = int(loc.get("columnNumber", 0))+ url = session.script_url(script_id) or frame.get("url", "")+ name = frame.get("functionName") or "<anonymous>"+ text = f"#{index} {name} {url.rsplit('/', 1)[-1]}:{line + 1}:{col + 1}"+ if not with_source or index > 3:+ return text+ reply = session.call("Debugger.getScriptSource", {"scriptId": script_id}, wait=5)+ source = ((reply or {}).get("result") or {}).get("scriptSource", "")+ rows = source.split("\n")+ if line < len(rows):+ row = rows[line]+ snippet = row[max(0, col - SNIPPET_RADIUS) : col + SNIPPET_RADIUS]+ text += f"\n {snippet!r}"+ return text++ def close(self) -> None:+ with self._lock:+ if self._session is not None:+ self._session.close()+ self._session = None+++__all__ = ["HangStackProbe", "REMOTE_DEBUGGING_FLAG", "enable_webview_remote_debugging"]diff --git a/jarvis/ui/window_watchdog.py b/jarvis/ui/window_watchdog.pyindex 8bef1c9468..38b7829072 100644--- a/jarvis/ui/window_watchdog.py+++ b/jarvis/ui/window_watchdog.py@@ -673,6 +673,7 @@ class BlankWindowWatchdog:caller: _WindowCaller | None = None,action_caller: _WindowCaller | None = None,end_hung_renderer: Callable[[], list[int]] | None = None,+ stack_probe: Any = None,) -> None:self._window = windowself._url = url@@ -694,6 +695,9 @@ class BlankWindowWatchdog:self._caller = caller or _WindowCaller()self._action_caller = action_caller or _WindowCaller()self._end_hung_renderer = end_hung_renderer or end_busy_renderers+ # Names the script a hung page is stuck in before the renderer, and+ # with it the evidence, is ended (``jarvis/ui/webview_hang_probe.py``).+ self._stack_probe = stack_probeself._stop = threading.Event()self._thread: threading.Thread | None = None@@ -732,9 +736,12 @@ class BlankWindowWatchdog:if window is None:continuetry:+ page = self._page_state(window)+ if page == "up" and self._stack_probe is not None:+ self._stack_probe.arm()verdict = self._policy.decide(Observation(- page=self._page_state(window),+ page=page,backend_alive=self._safe(self._backend_alive, default=True),server_healthy=self._safe(self._health_probe, default=False),now=time.monotonic(),@@ -818,6 +825,14 @@ class BlankWindowWatchdog:"Desktop window stopped answering while the server is healthy — ""its page is hung; ending its renderer and reloading.")+ if self._stack_probe is not None:+ stack = self._safe(self._stack_probe.capture, default=[])+ if stack:+ logger.warning(+ "Hung window: the page's JavaScript was stuck here:\n{}", "\n".join(stack)+ )+ else:+ logger.warning("Hung window: no JavaScript stack could be captured.")self._safe(self._end_hung_renderer, default=[])self._caller = _WindowCaller()diff --git a/tests/unit/ui/test_blank_window_watchdog.py b/tests/unit/ui/test_blank_window_watchdog.pyindex 18f90b0958..c94b4069ff 100644--- a/tests/unit/ui/test_blank_window_watchdog.py+++ b/tests/unit/ui/test_blank_window_watchdog.py@@ -572,3 +572,26 @@ def test_attaching_a_reopened_window_starts_a_fresh_incident() -> None:wd.attach(second)assert wd._window is secondassert wd._policy.decide(_obs(0.0)).action is Action.WAIT+++def test_a_hung_page_is_asked_for_its_stack_before_the_renderer_ends() -> None:+ from jarvis.ui.window_watchdog import Verdict++ order: list[str] = []++ class _Probe:+ def arm(self) -> None:+ order.append("arm")++ def capture(self) -> list[str]:+ order.append("capture")+ return ["sample 1: 1 frame(s)", " #0 spin index.js:1:2"]++ window = _FakeWindow()+ wd = _watchdog(+ window,+ stack_probe=_Probe(),+ end_hung_renderer=lambda: order.append("end") or [],+ )+ assert wd._apply(window, Verdict(Action.RELOAD, after_up=True, hung=True))+ assert order == ["capture", "end"]diff --git a/tests/unit/ui/test_webview_hang_probe.py b/tests/unit/ui/test_webview_hang_probe.pynew file mode 100644index 0000000000..d7779e0802--- /dev/null+++ b/tests/unit/ui/test_webview_hang_probe.py@@ -0,0 +1,52 @@+"""The hang probe names a hung page's script; these pin its quiet edges.++The live path (a real WebView2 page in ``while(true)``, paused over the+DevTools protocol) was verified end to end on Windows; it needs a browser and+cannot run here. What runs everywhere is what must never break the boot.+"""++from __future__ import annotations++from pathlib import Path++from jarvis.ui.webview_hang_probe import (+ REMOTE_DEBUGGING_FLAG,+ HangStackProbe,+ _active_port,+ enable_webview_remote_debugging,+)+++def test_the_flag_is_appended_on_windows_only() -> None:+ env: dict[str, str] = {+ "WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS": "--force-renderer-accessibility"+ }+ assert enable_webview_remote_debugging(env=env, platform="win32") is True+ assert env["WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS"] == (+ f"--force-renderer-accessibility {REMOTE_DEBUGGING_FLAG}"+ )+ other: dict[str, str] = {}+ assert enable_webview_remote_debugging(env=other, platform="linux") is False+ assert other == {}+++def test_a_port_someone_already_chose_is_kept() -> None:+ env = {"WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS": "--remote-debugging-port=9333"}+ assert enable_webview_remote_debugging(env=env, platform="win32") is True+ assert env["WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS"] == "--remote-debugging-port=9333"+++def test_the_active_port_is_read_from_the_profile(tmp_path: Path) -> None:+ assert _active_port(tmp_path) is None+ (tmp_path / "EBWebView").mkdir()+ (tmp_path / "EBWebView" / "DevToolsActivePort").write_text(+ "51234\n/devtools/browser/abc\n", encoding="utf-8"+ )+ assert _active_port(tmp_path) == 51234+++def test_without_an_endpoint_the_probe_is_a_quiet_no_op(tmp_path: Path) -> None:+ probe = HangStackProbe(tmp_path, "http://127.0.0.1:47821/")+ probe.arm()+ assert probe.capture() == []+ HangStackProbe(None, "http://127.0.0.1:47821/").arm()