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
fix(ide-browser): show a closed panel window again when the page refuses to close (#805)
Ruben Lütke015a97d6059f
diff --git a/dodaios/ui/browser_embed.py b/dodaios/ui/browser_embed.pyindex 3e9269f6d5..d2893cee3f 100644--- a/dodaios/ui/browser_embed.py+++ b/dodaios/ui/browser_embed.py@@ -83,6 +83,8 @@ WATCH_INTERVAL_MS = 200CALL_TIMEOUT_S = 5.0#: How long a visible placement holds without the page confirming it.LEASE_S = 3.0+#: How long a closed window gets to go away before it is shown again.+CLOSE_GRACE_S = 2.0#: Where the window in use is remembered, so a restarted app takes it back.STATE_FILE_NAME = "ide_browser_window.json"@@ -117,6 +119,7 @@ SWP_ASYNCWINDOWPOS = 0x4000SW_HIDE = 0SW_SHOWNORMAL = 1SW_SHOW = 5+SW_SHOWNA = 8SW_RESTORE = 9GW_HWNDNEXT = 2GW_HWNDPREV = 3@@ -1102,11 +1105,26 @@ class ChromeEmbed:logger.info("Browser panel gave its window back (%s)", "closed" if close else "kept open")if close:user32.PostMessageW(hwnd, WM_CLOSE, 0, 0)+ if not self._closed_within(hwnd, CLOSE_GRACE_S):+ # The page refused to close (a "Leave site?" prompt). A hidden+ # window would stay hidden for good, and the browser keeps+ # sending new windows into it: show it, prompt included.+ logger.info("Browser panel window did not close; shown again")+ user32.ShowWindow(hwnd, SW_SHOWNA)else:user32.ShowWindow(hwnd, SW_SHOW)if focus:user32.SetForegroundWindow(hwnd)+ def _closed_within(self, hwnd: int, seconds: float) -> bool:+ user32 = self._load_api().user32+ deadline = time.monotonic() + seconds+ while user32.IsWindow(hwnd):+ if time.monotonic() >= deadline:+ return False+ time.sleep(0.05)+ return True+_embed: ChromeEmbed | None = None_embed_lock = threading.Lock()diff --git a/tests/unit/ui/test_ide_browser_routes.py b/tests/unit/ui/test_ide_browser_routes.pyindex 36991b9c5a..aecfbbfedd 100644--- a/tests/unit/ui/test_ide_browser_routes.py+++ b/tests/unit/ui/test_ide_browser_routes.py@@ -195,3 +195,53 @@ def test_embed_open_tab_refuses_without_a_shown_window(monkeypatch: pytest.Monkeassert embed.open_tab("https://example.com")["reason"] == "not_embedded"assert embed.status()["state"] == "idle"assert started == []+++class _FakeUser32:+ """Records window calls; the window refuses to close unless told otherwise."""++ def __init__(self, closes: bool) -> None:+ self.closes = closes+ self.alive = True+ self.calls: list[tuple[str, int]] = []++ def IsWindow(self, _hwnd: int) -> bool: # noqa: N802 - Win32 name+ return self.alive++ def IsHungAppWindow(self, _hwnd: int) -> bool: # noqa: N802 - Win32 name+ return False++ def PostMessageW(self, _hwnd: int, msg: int, *_: int) -> bool: # noqa: N802 - Win32 name+ self.calls.append(("post", msg))+ if self.closes:+ self.alive = False+ return True++ def ShowWindow(self, _hwnd: int, cmd: int) -> bool: # noqa: N802 - Win32 name+ self.calls.append(("show", cmd))+ return True++ def SetWindowRgn(self, *_: object) -> int: # noqa: N802 - Win32 name+ return 1++[email protected]("closes", [True, False])+def test_closing_a_window_that_refuses_shows_it_again(+ monkeypatch: pytest.MonkeyPatch, closes: bool,+) -> None:+ """A page that asks "Leave site?" must not leave the user's window hidden for good."""+ from types import SimpleNamespace++ from dodaios.ui import browser_embed++ monkeypatch.setattr(browser_embed, "CLOSE_GRACE_S", 0.05)+ embed = browser_embed.ChromeEmbed()+ user32 = _FakeUser32(closes)+ embed._api = SimpleNamespace(user32=user32) # type: ignore[assignment]+ embed._chrome = 4242+ monkeypatch.setattr(embed, "_forget", lambda: None)++ embed._give_back(close=True, focus=False)++ assert ("post", browser_embed.WM_CLOSE) in user32.calls+ assert (("show", browser_embed.SW_SHOWNA) in user32.calls) is not closes