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(ui): isolate window icon branding by process

Ruben Lütke57f58144cb46
diff --git a/jarvis/ui/icon_utils.py b/jarvis/ui/icon_utils.py
index 49d8be40c5..f2b49fc7a8 100644
--- a/jarvis/ui/icon_utils.py
+++ b/jarvis/ui/icon_utils.py
@@ -927,13 +927,20 @@ def apply_tk_window_icon(root: Any) -> None:
def set_window_icon_by_title(
- title: str, ico_path: Path, *, quiet: bool = False
+ title: str,
+ ico_path: Path,
+ *,
+ quiet: bool = False,
+ expected_pid: int | None = None,
) -> bool:
- """Sets the taskbar and titlebar icon of the window matching ``title``.
+ """Set the icon only when the titled window belongs to ``expected_pid``.
Needed because pywebview doesn't stably expose the HWND. ``FindWindowW``
against the title is a pragmatic way — the Jarvis window title is
- constant ("Personal Jarvis") and unique.
+ constant ("Personal Jarvis"). The title is not globally unique, though: a
+ terminal opened in the repository commonly has the same title. Never stamp
+ a title match until its owning PID is verified, or the terminal receives
+ Jarvis's AUMID/relaunch metadata and gets grouped with the desktop app.
Args:
title: Window title exactly as set by pywebview.
@@ -941,6 +948,8 @@ def set_window_icon_by_title(
quiet: If True, "hwnd not found" notices are logged at debug
instead of warning. For polling loops where the window is
expected to appear only after a few iterations.
+ expected_pid: Required owner of the matched window. Defaults to the
+ current process.
Returns:
True if both icons could be set.
@@ -960,6 +969,10 @@ def set_window_icon_by_title(
user32 = ctypes.windll.user32
user32.FindWindowW.restype = wintypes.HWND
+ user32.GetWindowThreadProcessId.argtypes = [
+ wintypes.HWND,
+ ctypes.POINTER(wintypes.DWORD),
+ ]
hwnd = user32.FindWindowW(None, title)
if not hwnd:
if quiet:
@@ -967,6 +980,17 @@ def set_window_icon_by_title(
else:
logger.warning("Window '{}' not found — icon not set", title)
return False
+ owner_pid = wintypes.DWORD()
+ user32.GetWindowThreadProcessId(hwnd, ctypes.byref(owner_pid))
+ wanted_pid = os.getpid() if expected_pid is None else expected_pid
+ if owner_pid.value != wanted_pid:
+ logger.debug(
+ "Window '{}' belongs to pid={}, not pid={}; icon not set",
+ title,
+ owner_pid.value,
+ wanted_pid,
+ )
+ return False
return _apply_icon_to_hwnd(int(hwnd), ico_path)
diff --git a/tests/unit/ui/test_icon_window_ownership.py b/tests/unit/ui/test_icon_window_ownership.py
new file mode 100644
index 0000000000..12f8c4c354
--- /dev/null
+++ b/tests/unit/ui/test_icon_window_ownership.py
@@ -0,0 +1,62 @@
+"""Regression guards for process-safe desktop window branding."""
+
+from __future__ import annotations
+
+import os
+import sys
+from pathlib import Path
+
+import pytest
+
+from jarvis.ui import icon_utils
+
+
[email protected](sys.platform != "win32", reason="Windows-only window identity")
+def test_title_icon_setter_rejects_a_foreign_same_title_window(
+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path
+) -> None:
+ """A repository terminal may have the exact desktop-app title."""
+
+ import ctypes
+ from ctypes import wintypes
+
+ ico = tmp_path / "app.ico"
+ ico.write_bytes(b"synthetic icon")
+ foreign_pid = os.getpid() + 1
+
+ class _User32:
+ def __init__(self) -> None:
+ self.FindWindowW = _Callable(lambda *_args: 12345)
+ self.GetWindowThreadProcessId = _Callable(self._write_foreign_pid)
+
+ @staticmethod
+ def _write_foreign_pid(_hwnd, pid_pointer) -> int: # noqa: ANN001
+ ctypes.cast(
+ pid_pointer, ctypes.POINTER(wintypes.DWORD)
+ ).contents.value = foreign_pid
+ return 1
+
+ applied: list[int] = []
+ monkeypatch.setattr(ctypes.windll, "user32", _User32())
+ monkeypatch.setattr(
+ icon_utils,
+ "_apply_icon_to_hwnd",
+ lambda hwnd, _path: applied.append(hwnd) or True,
+ )
+
+ assert not icon_utils.set_window_icon_by_title(
+ "Personal Jarvis", ico, expected_pid=os.getpid()
+ )
+ assert applied == []
+
+
+class _Callable:
+ """Callable fake that accepts ctypes ``argtypes``/``restype`` attributes."""
+
+ def __init__(self, callback) -> None: # noqa: ANN001
+ self._callback = callback
+ self.argtypes = None
+ self.restype = None
+
+ def __call__(self, *args): # noqa: ANN002, ANN204
+ return self._callback(*args)