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(ide-browser): watch the panel's Chrome window a previous app run left behind (#1114)

Ruben Lütke450b5189e40d
diff --git a/dodaios/ui/browser_embed.py b/dodaios/ui/browser_embed.py
index 38af6c8d73..bec6ed9ae0 100644
--- a/dodaios/ui/browser_embed.py
+++ b/dodaios/ui/browser_embed.py
@@ -77,7 +77,9 @@ from dodaios.ui.browser_profiles import (
logger = logging.getLogger(__name__)
-__all__ = ["ChromeEmbed", "get_embed", "release_host_if_any", "shutdown_embed"]
+__all__ = [
+ "ChromeEmbed", "get_embed", "park_left_over_window", "release_host_if_any", "shutdown_embed",
+]
T = TypeVar("T")
@@ -553,6 +555,62 @@ class ChromeEmbed:
# -- a window left by the previous app run ---------------------------------
+ def park_left_over(self) -> bool:
+ """Watch the window a previous app run left behind, hidden, until the panel wants it.
+
+ Unwatched, that window stays a hidden, borderless window of the user's
+ browser: the browser keeps sending links and new windows into it, and
+ the user sees nothing. Parked, it is hidden like a closed panel, and a
+ link that arrives in it hands it back as a normal window.
+ """
+ if not self.supported():
+ return False
+ with self._lock:
+ if self._closing or self._chrome:
+ return False
+ try:
+ record = self._read_state()
+ spec = browser_by_id(str(record.get("browser", ""))) if record else None
+ if record is None or spec is None:
+ return False
+ self._ensure_thread()
+ parked = self._call(lambda: self._park(spec, str(record.get("profile", ""))))
+ except Exception as exc: # noqa: BLE001 - a left-over window is never load-bearing
+ logger.warning("Browser panel could not park the left-over window: %s", exc)
+ return False
+ if parked:
+ logger.info("Browser panel parked the window of the previous run")
+ return bool(parked)
+
+ def _park(self, spec: BrowserSpec, profile: str) -> bool:
+ """Adopt the left-over window with no panel (native thread).
+
+ A window on screen right now is in use: it becomes a normal window
+ instead of vanishing.
+ """
+ if self._chrome:
+ return False
+ record = self._left_over(spec)
+ if record is None:
+ return False
+ on_screen = bool(self._load_api().user32.IsWindowVisible(int(record["hwnd"])))
+ if not self._adopt_left_over(spec, profile, 0):
+ return False
+ if on_screen:
+ self._give_back(close=False, focus=False)
+ return True
+
+ def _read_state(self) -> dict[str, Any] | None:
+ path = self._state_path()
+ if path is None or not path.is_file():
+ return None
+ try:
+ record = json.loads(path.read_text(encoding="utf-8"))
+ except (OSError, ValueError) as exc:
+ logger.info("Browser panel window record unreadable, nothing parked: %s", exc)
+ return None
+ return record if isinstance(record, dict) else None
+
@staticmethod
def _state_path() -> Path | None:
try:
@@ -1199,6 +1257,14 @@ def get_embed() -> ChromeEmbed:
return _embed
+def park_left_over_window() -> None:
+ """At app start: watch the browser window a previous run left behind. Never raises."""
+ try:
+ get_embed().park_left_over()
+ except Exception: # noqa: BLE001
+ logger.debug("Browser panel left-over window not parked", exc_info=True)
+
+
def release_host_if_any(host: int) -> None:
"""Close the panel's browser window if it sits in ``host``; no-op otherwise."""
embed = _embed
diff --git a/dodaios/ui/desktop_app.py b/dodaios/ui/desktop_app.py
index f02de64fcc..ef15fed4d5 100644
--- a/dodaios/ui/desktop_app.py
+++ b/dodaios/ui/desktop_app.py
@@ -5873,6 +5873,17 @@ class DesktopApp:
# page load, and the UI reloads itself after each frontend build.
self._window.events.loaded += lambda w=self._window: self._register_native_drop(w)
self._arm_window_frame(self._window)
+ if sys.platform == "win32":
+ self._window.events.shown += self._park_browser_panel_window
+
+ @staticmethod
+ def _park_browser_panel_window() -> None:
+ """Take charge of a browser panel window the previous app run left behind."""
+ from dodaios.ui.browser_embed import park_left_over_window # noqa: PLC0415
+
+ threading.Thread(
+ target=park_left_over_window, name="browser-panel-park", daemon=True,
+ ).start()
@staticmethod
def _register_native_drop(window: Any) -> None:
diff --git a/tests/unit/ui/test_browser_embed_external_links.py b/tests/unit/ui/test_browser_embed_external_links.py
index 663abf45b8..a5d71b600a 100644
--- a/tests/unit/ui/test_browser_embed_external_links.py
+++ b/tests/unit/ui/test_browser_embed_external_links.py
@@ -105,3 +105,35 @@ def test_window_the_browser_showed_by_itself_is_handed_back(
assert (embed.handed == ["released"]) is handed_back
assert (("show", browser_embed.SW_HIDE) in user32.calls) is not handed_back
+
+
[email protected](("on_screen", "state"), [(False, "embedded"), (True, "released")])
+def test_left_over_window_is_parked_or_handed_back(
+ monkeypatch: pytest.MonkeyPatch, on_screen: bool, state: str,
+) -> None:
+ """A window a crashed app left behind gets watched (hidden) or becomes a normal window."""
+ embed = browser_embed.ChromeEmbed()
+ record = {"hwnd": CHROME, "pid": 1, "browser": "chrome", "profile": "Profile 3"}
+ embed._api = SimpleNamespace(user32=SimpleNamespace(IsWindowVisible=lambda _h: on_screen))
+ monkeypatch.setattr(embed, "_left_over", lambda _spec: record)
+
+ def adopt(_spec: Any, _profile: str, _host: int) -> bool:
+ embed._chrome, embed._state = CHROME, "embedded"
+ return True
+
+ def give_back(**_kw: Any) -> None:
+ embed._chrome, embed._state = 0, "released"
+
+ monkeypatch.setattr(embed, "_adopt_left_over", adopt)
+ monkeypatch.setattr(embed, "_give_back", give_back)
+
+ assert embed._park(SimpleNamespace(), "Profile 3") is True
+ assert embed._state == state
+
+
+def test_nothing_to_park_without_a_record(monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setattr(browser_embed, "_is_windows", lambda: True)
+ embed = browser_embed.ChromeEmbed()
+ monkeypatch.setattr(embed, "_read_state", lambda: None)
+ assert embed.park_left_over() is False
+ assert embed._thread is None