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(desktop): stop clobbering the shared user32 prototypes pywebview relies on
rubenluetke10-beep8c988e7035f0
diff --git a/jarvis/cu/indicator/win32.py b/jarvis/cu/indicator/win32.pyindex 3a44a3317d..005fc0c3a0 100644--- a/jarvis/cu/indicator/win32.py+++ b/jarvis/cu/indicator/win32.py@@ -44,7 +44,11 @@ def _user32():import ctypes # noqa: PLC0415from ctypes import wintypes # noqa: PLC0415- user32 = ctypes.windll.user32 # type: ignore[attr-defined]+ # A private WinDLL instance, not ctypes.windll.user32: mutating argtypes+ # on the shared object corrupts every other caller in the process,+ # including pywebview's winforms SetWindowPos calls (see BUG-log 136x/day+ # ArgumentError crash).+ user32 = ctypes.WinDLL("user32", use_last_error=True)_configure_user32(user32, ctypes, wintypes)return user32diff --git a/jarvis/platform/capture_exclusion.py b/jarvis/platform/capture_exclusion.pyindex f7ab98dcc9..28cbc1feae 100644--- a/jarvis/platform/capture_exclusion.py+++ b/jarvis/platform/capture_exclusion.py@@ -39,7 +39,11 @@ def _user32() -> Any:import ctypes # noqa: PLC0415from ctypes import wintypes # noqa: PLC0415- user32 = ctypes.windll.user32 # type: ignore[attr-defined]+ # A private WinDLL instance, not ctypes.windll.user32: mutating argtypes+ # on the shared object corrupts every other caller in the process,+ # including pywebview's winforms SetWindowPos calls (see BUG-log 136x/day+ # ArgumentError crash).+ user32 = ctypes.WinDLL("user32", use_last_error=True)_configure_user32(user32, ctypes, wintypes)return user32diff --git a/jarvis/platform/window_state.py b/jarvis/platform/window_state.pyindex 7e40af32c7..10efb72159 100644--- a/jarvis/platform/window_state.py+++ b/jarvis/platform/window_state.py@@ -71,6 +71,35 @@ class WindowInfo:pid: int | None = None+# Cached, process-private DLL instances. Never ``ctypes.windll.user32`` /+# ``ctypes.windll.kernel32``: this module binds pointer-sized argtypes on+# whatever object it gets, and the shared ``windll`` singleton is used by+# every other library in the process (e.g. pywebview's winforms backend,+# which calls SetWindowPos relying on it staying argtypes-free).+_private_user32_dll = None+_private_kernel32_dll = None+++def _win32_user32():+ """Return the cached private ``user32`` WinDLL, creating it on first use."""+ global _private_user32_dll+ import ctypes # noqa: PLC0415++ if _private_user32_dll is None:+ _private_user32_dll = ctypes.WinDLL("user32", use_last_error=True)+ return _private_user32_dll+++def _win32_kernel32():+ """Return the cached private ``kernel32`` WinDLL, creating it on first use."""+ global _private_kernel32_dll+ import ctypes # noqa: PLC0415++ if _private_kernel32_dll is None:+ _private_kernel32_dll = ctypes.WinDLL("kernel32", use_last_error=True)+ return _private_kernel32_dll++def _configure_window_query_api(user32, ctypes, wintypes) -> None:"""Bind pointer-sized Win32 window-query signatures."""user32.GetForegroundWindow.argtypes = []@@ -176,7 +205,7 @@ def _find_and_focus_windows(title_contains: str) -> tuple[bool, str]:import ctypes # noqa: PLC0415from ctypes import wintypes # noqa: PLC0415- user32 = ctypes.windll.user32+ user32 = _win32_user32()_configure_window_query_api(user32, ctypes, wintypes)EnumWindows = user32.EnumWindows@@ -252,8 +281,8 @@ def _force_foreground_windows(hwnd: int) -> bool:import ctypes # noqa: PLC0415from ctypes import wintypes # noqa: PLC0415- user32 = ctypes.windll.user32- kernel32 = ctypes.windll.kernel32+ user32 = _win32_user32()+ kernel32 = _win32_kernel32()_configure_window_query_api(user32, ctypes, wintypes)kernel32.GetCurrentThreadId.argtypes = []kernel32.GetCurrentThreadId.restype = wintypes.DWORD@@ -315,7 +344,7 @@ def _list_windows_windows() -> list[WindowInfo]:import ctypes # noqa: PLC0415from ctypes import wintypes # noqa: PLC0415- user32 = ctypes.windll.user32+ user32 = _win32_user32()_configure_window_query_api(user32, ctypes, wintypes)EnumWindows = user32.EnumWindowsEnumWindowsProc = ctypes.WINFUNCTYPE(wintypes.BOOL, wintypes.HWND, wintypes.LPARAM)@@ -364,7 +393,7 @@ def _foreground_title_windows() -> str:import ctypes # noqa: PLC0415from ctypes import wintypes # noqa: PLC0415- user32 = ctypes.windll.user32+ user32 = _win32_user32()_configure_window_query_api(user32, ctypes, wintypes)with per_monitor_dpi_context():hwnd = user32.GetForegroundWindow()@@ -1096,7 +1125,7 @@ def _foreground_window_windows() -> WindowInfo | None:import ctypes # noqa: PLC0415from ctypes import wintypes # noqa: PLC0415- user32 = ctypes.windll.user32+ user32 = _win32_user32()_configure_window_query_api(user32, ctypes, wintypes)with per_monitor_dpi_context():hwnd = user32.GetForegroundWindow()@@ -1285,7 +1314,7 @@ def _window_rect_windows(hwnd: int) -> tuple[int, int, int, int] | None:import ctypes # noqa: PLC0415from ctypes import wintypes # noqa: PLC0415- user32 = ctypes.windll.user32+ user32 = _win32_user32()_configure_window_query_api(user32, ctypes, wintypes)with per_monitor_dpi_context():rect = wintypes.RECT()@@ -1304,7 +1333,7 @@ def _window_client_rect_windows(hwnd: int) -> tuple[int, int, int, int] | None:import ctypes # noqa: PLC0415from ctypes import wintypes # noqa: PLC0415- user32 = ctypes.windll.user32+ user32 = _win32_user32()_configure_window_query_api(user32, ctypes, wintypes)with per_monitor_dpi_context():rect = wintypes.RECT()@@ -1327,7 +1356,7 @@ def _move_to_primary_windows(win: WindowInfo, primary: dict) -> tuple[bool, str]import ctypes # noqa: PLC0415from ctypes import wintypes # noqa: PLC0415- user32 = ctypes.windll.user32+ user32 = _win32_user32()_configure_window_query_api(user32, ctypes, wintypes)hwnd = int(win.handle)@@ -1381,7 +1410,7 @@ def _window_class_windows(hwnd: int) -> str:import ctypes # noqa: PLC0415from ctypes import wintypes # noqa: PLC0415- user32 = ctypes.windll.user32+ user32 = _win32_user32()_configure_window_query_api(user32, ctypes, wintypes)buf = ctypes.create_unicode_buffer(256)if not user32.GetClassNameW(hwnd, buf, 256):@@ -1438,7 +1467,7 @@ def window_frame_rect(win: WindowInfo) -> tuple[int, int, int, int] | None:from ctypes import wintypes # noqa: PLC0415hwnd = int(win.handle)- user32 = ctypes.windll.user32+ user32 = _win32_user32()_configure_window_query_api(user32, ctypes, wintypes)with per_monitor_dpi_context():rect = wintypes.RECT()@@ -1644,7 +1673,7 @@ def window_is_maximized(win: WindowInfo) -> bool | None:_SW_SHOWMAXIMIZED = 3wp = _WINDOWPLACEMENT()wp.length = ctypes.sizeof(_WINDOWPLACEMENT)- user32 = ctypes.windll.user32+ user32 = _win32_user32()_configure_window_query_api(user32, ctypes, wintypes)if not user32.GetWindowPlacement(int(win.handle), ctypes.byref(wp)):return None@@ -1712,7 +1741,7 @@ def maximize_window(win: WindowInfo) -> tuple[bool, str]:return False, "no window handle to maximize"import ctypes # noqa: PLC0415- user32 = ctypes.windll.user32+ user32 = _win32_user32()hwnd = int(win.handle)_GWL_STYLE, _WS_MAXIMIZEBOX = -16, 0x00010000_SW_MAXIMIZE = 3diff --git a/jarvis/ui/desktop_app.py b/jarvis/ui/desktop_app.pyindex 993245f619..10f9a71c81 100644--- a/jarvis/ui/desktop_app.py+++ b/jarvis/ui/desktop_app.py@@ -685,7 +685,11 @@ def _bring_window_to_front_by_title(title: str) -> bool:import ctypesfrom ctypes import wintypes- user32 = ctypes.windll.user32+ # A private WinDLL instance, not ctypes.windll.user32: setting+ # pointer-sized argtypes on the shared object corrupts every other+ # caller in the process — including pywebview's own SetWindowPos+ # calls, which rely on it staying argtypes-free.+ user32 = ctypes.WinDLL("user32", use_last_error=True)user32.FindWindowW.restype = wintypes.HWNDuser32.GetForegroundWindow.restype = wintypes.HWNDuser32.GetWindowRect.argtypes = [wintypes.HWND, ctypes.POINTER(wintypes.RECT)]diff --git a/jarvis/ui/icon_utils.py b/jarvis/ui/icon_utils.pyindex d143dd1721..f5e1c6f958 100644--- a/jarvis/ui/icon_utils.py+++ b/jarvis/ui/icon_utils.py@@ -341,7 +341,10 @@ def _replace_exe_icon(exe_path: Path, ico_path: Path) -> bool:return FalseRT_ICON, RT_GROUP_ICON, LANG = 3, 14, 0x0409- k = ctypes.windll.kernel32+ # A private WinDLL instance, not ctypes.windll.kernel32: mutating+ # argtypes on the shared object corrupts every other caller in the+ # process (e.g. pywebview's winforms SetWindowPos calls).+ k = ctypes.WinDLL("kernel32", use_last_error=True)k.BeginUpdateResourceW.restype = wintypes.HANDLEk.BeginUpdateResourceW.argtypes = [wintypes.LPCWSTR, wintypes.BOOL]k.UpdateResourceW.argtypes = [@@ -1218,7 +1221,10 @@ def _apply_icon_to_hwnd(hwnd: int, ico_path: Path) -> bool:logger.opt(exception=exc).warning("ctypes not available")return False- user32 = ctypes.windll.user32+ # A private WinDLL instance, not ctypes.windll.user32: mutating+ # argtypes on the shared object corrupts every other caller in the+ # process (e.g. pywebview's winforms SetWindowPos calls).+ user32 = ctypes.WinDLL("user32", use_last_error=True)user32.LoadImageW.restype = wintypes.HANDLEuser32.SendMessageW.restype = ctypes.c_long# On 64-bit Windows SetClassLongPtrW is the correct variant.@@ -1379,7 +1385,10 @@ def set_window_icon_by_title(logger.opt(exception=exc).warning("ctypes not available")return False- user32 = ctypes.windll.user32+ # A private WinDLL instance, not ctypes.windll.user32: mutating+ # argtypes on the shared object corrupts every other caller in the+ # process (e.g. pywebview's winforms SetWindowPos calls).+ user32 = ctypes.WinDLL("user32", use_last_error=True)user32.FindWindowW.restype = wintypes.HWNDuser32.GetWindowThreadProcessId.argtypes = [wintypes.HWND,@@ -1426,7 +1435,10 @@ def set_window_icon_for_pid(pid: int, ico_path: Path) -> bool:logger.opt(exception=exc).warning("ctypes not available")return False- user32 = ctypes.windll.user32+ # A private WinDLL instance, not ctypes.windll.user32: mutating+ # argtypes on the shared object corrupts every other caller in the+ # process (e.g. pywebview's winforms SetWindowPos calls).+ user32 = ctypes.WinDLL("user32", use_last_error=True)user32.GetWindowThreadProcessId.argtypes = [wintypes.HWND, ctypes.POINTER(wintypes.DWORD)]diff --git a/jarvis/ui/msix_redirection.py b/jarvis/ui/msix_redirection.pyindex 86ce74b512..64d6add6a4 100644--- a/jarvis/ui/msix_redirection.py+++ b/jarvis/ui/msix_redirection.py@@ -77,7 +77,10 @@ def package_family_name() -> str | None:import ctypesfrom ctypes import wintypes- kernel32 = ctypes.windll.kernel32+ # A private WinDLL instance, not ctypes.windll.kernel32: mutating+ # argtypes on the shared object corrupts every other caller in the+ # process (e.g. pywebview's winforms SetWindowPos calls).+ kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)probe = kernel32.GetCurrentPackageFamilyNameprobe.argtypes = [ctypes.POINTER(wintypes.UINT), wintypes.LPWSTR]probe.restype = wintypes.LONGdiff --git a/tests/unit/platform/test_win32_shared_windll_isolation.py b/tests/unit/platform/test_win32_shared_windll_isolation.pynew file mode 100644index 0000000000..1643ba38dd--- /dev/null+++ b/tests/unit/platform/test_win32_shared_windll_isolation.py@@ -0,0 +1,113 @@+"""Regression test for the shared ``ctypes.windll.user32`` corruption bug.++Symptom (136x/day in the desktop app log): "Exception occurred during+on_script_notify" -> pywebview's ``webview/platforms/winforms.py``+``BrowserForm.move()`` calls+``windll.user32.SetWindowPos(hwnd, None, x, y, None, None, flags)`` and+raises ``ctypes.ArgumentError`` because ``None`` can no longer be coerced+to an int argument.++Root cause: several Jarvis modules bound pointer-sized ``.argtypes`` /+``.restype`` on the PROCESS-GLOBAL ``ctypes.windll.user32`` /+``ctypes.windll.kernel32`` singleton. ``ctypes`` caches one function-pointer+object per (dll instance, name), shared by every caller in the process —+pywebview's own ``SetWindowPos`` / ``GetWindowLongW`` / ``SetWindowLongW``+calls pass plain Python ints/None and rely on that object staying+argtypes-free so ctypes can coerce them loosely.++Fix: every Jarvis module binds its prototypes on its OWN private+``ctypes.WinDLL("user32", use_last_error=True)`` instance instead of+mutating ``ctypes.windll.user32`` in place. This test exercises the fixed+call paths and then asserts the shared, process-global object is still+untouched — a canary against this bug class recurring anywhere in the repo,+not just in the specific modules fixed here.+"""+from __future__ import annotations++import sys++import pytest++pytestmark = pytest.mark.skipif(+ sys.platform != "win32", reason="ctypes.windll is Windows-only"+)++_MISSING_WINDOW_TITLE = "Jarvis Regression Test — window that does not exist 42fa9c"+++def _assert_shared_user32_is_clean() -> None:+ import ctypes++ # ``.restype`` on an untouched ctypes function is c_long (c_int and+ # c_long are the SAME type on Windows) even before any assignment — that+ # is just ctypes' own implicit default and proves nothing. ``.argtypes``+ # is the real signal: it genuinely stays None until application code+ # assigns it, and pywebview's winforms.py BrowserForm.move()/resize()+ # call SetWindowPos with plain ints/None on this exact shared object,+ # relying on argtypes staying unset so ctypes coerces them loosely.+ assert ctypes.windll.user32.SetWindowPos.argtypes is None+ # winforms.py also calls these two directly on the shared object.+ assert ctypes.windll.user32.GetWindowLongW.argtypes is None+ assert ctypes.windll.user32.SetWindowLongW.argtypes is None+++def test_window_state_focus_leaves_shared_user32_untouched() -> None:+ """jarvis.platform.window_state binds SetWindowPos et al. on a private DLL."""+ from jarvis.platform import window_state as ws++ ws._find_and_focus_windows(_MISSING_WINDOW_TITLE)+ _assert_shared_user32_is_clean()+++def test_desktop_app_bring_to_front_leaves_shared_user32_untouched() -> None:+ """jarvis.ui.desktop_app binds SetWindowPos et al. on a private DLL."""+ from jarvis.ui import desktop_app++ desktop_app._bring_window_to_front_by_title(_MISSING_WINDOW_TITLE)+ _assert_shared_user32_is_clean()+++def test_cu_indicator_win32_leaves_shared_user32_untouched() -> None:+ """jarvis.cu.indicator.win32 binds GetWindowLongW/SetWindowLongW on a+ private DLL — those two are also called directly by pywebview."""+ from jarvis.cu.indicator import win32 as cu_win32++ cu_win32.harden_window(0)+ _assert_shared_user32_is_clean()+++def test_capture_exclusion_leaves_shared_user32_untouched() -> None:+ """jarvis.platform.capture_exclusion binds GetParent/SetWindowDisplayAffinity+ on a private DLL. hwnd=1 is a deliberately invalid-but-truthy handle: the+ real Win32 call just fails gracefully, it never dereferences memory."""+ from jarvis.platform import capture_exclusion++ capture_exclusion.exclude_hwnd_from_capture(1)+ _assert_shared_user32_is_clean()+++def test_icon_utils_leaves_shared_user32_and_kernel32_untouched(tmp_path) -> None:+ """jarvis.ui.icon_utils binds SetClassLongPtrW/FindWindowW/... on private+ DLLs — set_window_icon_by_title with a nonexistent title exercises the+ configuring code path without needing a real HWND. The file only needs+ to exist; FindWindowW returns 0 for the missing title before any icon+ bytes are ever read."""+ from jarvis.ui import icon_utils++ fake_ico = tmp_path / "fake.ico"+ fake_ico.write_bytes(b"")+ icon_utils.set_window_icon_by_title(_MISSING_WINDOW_TITLE, fake_ico, quiet=True)+ _assert_shared_user32_is_clean()+++def test_msix_redirection_leaves_shared_kernel32_untouched(monkeypatch) -> None:+ """jarvis.ui.msix_redirection binds GetCurrentPackageFamilyName on a+ private DLL. Reset the module's one-shot cache so the probe actually runs."""+ from jarvis.ui import msix_redirection++ monkeypatch.setattr(msix_redirection, "_FAMILY_PROBED", False)+ msix_redirection.package_family_name()++ import ctypes++ assert ctypes.windll.kernel32.GetCurrentPackageFamilyName.argtypes is None