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(appshot): capture the app window, not the mascot or bar overlay

rubenluetke10-beep9acdad62d956
diff --git a/docs/BUGS.md b/docs/BUGS.md
index 360f9b7160..2a6a29f161 100644
--- a/docs/BUGS.md
+++ b/docs/BUGS.md
@@ -15544,3 +15544,25 @@ custom pane names the same way (positions stay exact) and, when a name is no
pane but a Jarvis agent, says so in `jarvis_agent`. Guards:
`tests/unit/core/test_spoken_names.py`, `tests/unit/society/test_agent_names.py`,
`tests/unit/plugins/tool/test_delegate_to_agent.py`.
+
+## BUG-228: an appshot photographed only the mascot or the Jarvis bar, not the app window (MEDIUM, FIXED 2026-10-02)
+
+**Symptom.** "Take an appshot" during a voice call sometimes sent the model a
+small square of the mascot instead of the window the person was working in.
+The log showed `appshot: active window 288x288 via tool -> turn` right after
+a normal 1280x696 one.
+
+**Cause.** An appshot captures the foreground window. A click on the mascot or
+the bar (to talk, mute or hang up) gives that small topmost `JarvisOrb` Tk
+overlay the Windows focus, so `GetForegroundWindow` returned the overlay and
+the capture targeted its 288x288 rectangle.
+
+**Fix.** `window_state.foreground_app_window()` looks past this process's own
+overlays (tool, non-activating or capture-excluded windows) and walks down
+the Z-order to the first real app window under them — the window the person
+used last. It steps over the taskbar, which sits between the overlays and the
+apps, and returns `None` at the wallpaper so the capture falls back to the
+monitor. The Screen Context window probe reads this instead of the raw
+foreground window. macOS and X11 need no walk: their probes never report the
+floating overlays. Guards: `tests/unit/platform/test_foreground_app_window.py`,
+`tests/unit/screen_context/test_ports.py`.
diff --git a/jarvis/platform/window_state.py b/jarvis/platform/window_state.py
index 10efb72159..f6cd17366b 100644
--- a/jarvis/platform/window_state.py
+++ b/jarvis/platform/window_state.py
@@ -32,6 +32,7 @@ import re
import shutil
import subprocess
import time
+from collections.abc import Callable
from dataclasses import dataclass
from jarvis.core.process_utils import NO_WINDOW_CREATIONFLAGS
@@ -1141,6 +1142,182 @@ def _foreground_window_windows() -> WindowInfo | None:
)
+# Win32 constants for telling Jarvis's own floating overlays (the bar, the
+# mascot, the transcription bubble) apart from the app window under them.
+_GWL_EXSTYLE = -20
+_GW_HWNDNEXT = 2
+_WS_EX_TOOLWINDOW = 0x00000080
+_WS_EX_NOACTIVATE = 0x08000000
+_DWMWA_CLOAKED = 14
+#: A window smaller than this in either direction is no app window a person
+#: works in (helper and message windows are often 0x0 or 1x1).
+_MIN_APP_WINDOW_PX = 64
+#: Hard stop for the Z-order walk; a real desktop has far fewer top-level windows.
+_MAX_Z_ORDER_STEPS = 512
+#: The wallpaper windows sit at the bottom of the Z-order: reaching one means no
+#: app window is open under the overlay. The taskbar is NOT here — it is a
+#: topmost tool window that sits between the overlays and the apps, and the
+#: walk steps over it like any other non-app window.
+_DESKTOP_WINDOW_CLASSES = frozenset({"Progman", "WorkerW"})
+
+
+def foreground_app_window() -> WindowInfo | None:
+ """The app window the person is working in, looking past Jarvis's overlays.
+
+ Clicking the Jarvis bar or the mascot gives that small topmost overlay the
+ Windows focus, so the plain foreground window is the overlay itself — and a
+ "front window" capture photographs a square of mascot instead of the app.
+ When the foreground window is one of this process's overlays, this walks
+ down the Z-order to the first real app window under it. That is the window
+ the person used last, because activating a window raises it to the top of
+ the normal band.
+
+ Other platforms return :func:`foreground_window` unchanged: the macOS probe
+ already skips windows above the normal layer, where the overlays float, and
+ X11 never hands focus to the override-redirect overlay windows. Returns
+ ``None`` when no app window sits under the overlay (only the desktop), so
+ callers degrade to monitor scope. Best-effort; never raises.
+ """
+ try:
+ if detect_platform() == "win32":
+ return _foreground_app_window_windows()
+ return foreground_window()
+ except Exception: # noqa: BLE001
+ log.debug("foreground_app_window failed", exc_info=True)
+ return None
+
+
+def _pick_app_window(
+ foreground: int,
+ *,
+ next_below: Callable[[int], int],
+ is_own_overlay: Callable[[int], bool],
+ is_app_window: Callable[[int], bool],
+ is_desktop: Callable[[int], bool],
+) -> int | None:
+ """Pure Z-order walk behind :func:`foreground_app_window`.
+
+ The foreground window wins unless it is one of our overlays. Otherwise the
+ first app window below it wins; reaching the wallpaper (or the end of the
+ list) means there is no app window, which is ``None``.
+ """
+ if not is_own_overlay(foreground):
+ return foreground
+ below = next_below(foreground)
+ for _ in range(_MAX_Z_ORDER_STEPS):
+ if not below:
+ return None
+ if is_desktop(below):
+ return None
+ if not is_own_overlay(below) and is_app_window(below):
+ return below
+ below = next_below(below)
+ return None
+
+
+def _foreground_app_window_windows() -> WindowInfo | None:
+ if os.name != "nt":
+ return None
+ import ctypes # noqa: PLC0415
+ from ctypes import wintypes # noqa: PLC0415
+
+ user32 = _win32_user32()
+ _configure_window_query_api(user32, ctypes, wintypes)
+ user32.GetWindow.argtypes = [wintypes.HWND, wintypes.UINT]
+ user32.GetWindow.restype = wintypes.HWND
+ user32.GetWindowDisplayAffinity.argtypes = [
+ wintypes.HWND,
+ ctypes.POINTER(wintypes.DWORD),
+ ]
+ user32.GetWindowDisplayAffinity.restype = wintypes.BOOL
+ own_pid = os.getpid()
+
+ def _pid_of(hwnd: int) -> int:
+ pid = wintypes.DWORD()
+ user32.GetWindowThreadProcessId(hwnd, ctypes.byref(pid))
+ return int(pid.value)
+
+ def _is_own_process(pid: int) -> bool:
+ if pid == own_pid:
+ return True
+ # The bar can run in a child process (subprocess overlay host).
+ try:
+ import psutil # noqa: PLC0415
+
+ return int(psutil.Process(pid).ppid()) == own_pid
+ except Exception: # noqa: BLE001 - no psutil / dead pid: not ours
+ return False
+
+ def _ex_style(hwnd: int) -> int:
+ return int(user32.GetWindowLongW(hwnd, _GWL_EXSTYLE)) & 0xFFFFFFFF
+
+ def _is_own_overlay(hwnd: int) -> bool:
+ ex_style = _ex_style(hwnd)
+ floating = bool(ex_style & (_WS_EX_TOOLWINDOW | _WS_EX_NOACTIVATE))
+ if not floating:
+ affinity = wintypes.DWORD()
+ floating = bool(
+ user32.GetWindowDisplayAffinity(hwnd, ctypes.byref(affinity))
+ and affinity.value
+ )
+ return floating and _is_own_process(_pid_of(hwnd))
+
+ def _is_cloaked(hwnd: int) -> bool:
+ cloaked = wintypes.DWORD()
+ try:
+ get_attribute = ctypes.windll.dwmapi.DwmGetWindowAttribute
+ get_attribute.argtypes = [
+ wintypes.HWND,
+ wintypes.DWORD,
+ ctypes.c_void_p,
+ wintypes.DWORD,
+ ]
+ get_attribute.restype = ctypes.c_long
+ res = get_attribute(
+ wintypes.HWND(hwnd),
+ wintypes.DWORD(_DWMWA_CLOAKED),
+ ctypes.byref(cloaked),
+ ctypes.sizeof(cloaked),
+ )
+ except (OSError, AttributeError):
+ return False # no DWM: nothing can be cloaked
+ return res == 0 and bool(cloaked.value)
+
+ def _is_app_window(hwnd: int) -> bool:
+ if not user32.IsWindowVisible(hwnd) or user32.IsIconic(hwnd):
+ return False
+ if _ex_style(hwnd) & _WS_EX_TOOLWINDOW or _is_cloaked(hwnd):
+ return False
+ rect = wintypes.RECT()
+ if not user32.GetWindowRect(hwnd, ctypes.byref(rect)):
+ return False
+ return (
+ rect.right - rect.left >= _MIN_APP_WINDOW_PX
+ and rect.bottom - rect.top >= _MIN_APP_WINDOW_PX
+ )
+
+ with per_monitor_dpi_context():
+ foreground = user32.GetForegroundWindow()
+ if not foreground:
+ return None
+ chosen = _pick_app_window(
+ int(foreground),
+ next_below=lambda hwnd: int(user32.GetWindow(hwnd, _GW_HWNDNEXT) or 0),
+ is_own_overlay=_is_own_overlay,
+ is_app_window=_is_app_window,
+ is_desktop=lambda hwnd: _window_class_windows(hwnd) in _DESKTOP_WINDOW_CLASSES,
+ )
+ if chosen is None:
+ log.info("foreground is a Jarvis overlay with no app window under it")
+ return None
+ length = int(user32.GetWindowTextLengthW(chosen))
+ buf = ctypes.create_unicode_buffer(length + 1)
+ user32.GetWindowTextW(chosen, buf, length + 1)
+ return WindowInfo(
+ title=buf.value or "", handle=int(chosen), pid=_pid_of(chosen) or None
+ )
+
+
def _quartz_window_list(*, on_screen_only: bool = True) -> list:
"""Quartz window info dicts, front-to-back, or ``[]`` when unavailable.
diff --git a/jarvis/screen_context/ports.py b/jarvis/screen_context/ports.py
index c5decb7f61..eefb9cfbf8 100644
--- a/jarvis/screen_context/ports.py
+++ b/jarvis/screen_context/ports.py
@@ -256,12 +256,17 @@ class PlatformWindowProbe:
name = "platform-window"
def foreground_snapshot(self) -> WindowSnapshot | None:
- """Sample facts, frame and native handle atomically."""
+ """Sample facts, frame and native handle atomically.
+
+ Reads the app window, not the raw foreground: a click on the Jarvis
+ bar or the mascot makes that overlay the foreground window, and an
+ appshot of it is a picture of the mascot (BUG-228).
+ """
try:
from jarvis.platform import window_state as ws # noqa: PLC0415
with _input_space():
- win = ws.foreground_window()
+ win = ws.foreground_app_window()
if win is None:
return None
rect = ws.window_frame_rect(win) or ws.window_rect(win)
diff --git a/tests/unit/platform/test_foreground_app_window.py b/tests/unit/platform/test_foreground_app_window.py
new file mode 100644
index 0000000000..af4222fdc9
--- /dev/null
+++ b/tests/unit/platform/test_foreground_app_window.py
@@ -0,0 +1,85 @@
+"""BUG-228: an appshot taken right after a click on the Jarvis bar or the mascot
+photographed the overlay itself (a 288x288 square) instead of the app window.
+
+The Z-order walk is pure and tested here with plain fakes; the Win32 glue is
+exercised on a real desktop, not in CI.
+"""
+from __future__ import annotations
+
+from jarvis.platform import window_state as ws
+from jarvis.platform.window_state import WindowInfo
+
+# Z-order, top to bottom, as measured live: mascot overlay, bar overlay, the
+# taskbar (a topmost tool window), a minimized window, the editor, the wallpaper.
+_MASCOT, _BAR, _TASKBAR, _MINIMIZED, _EDITOR, _DESKTOP = 10, 11, 12, 13, 14, 15
+_ORDER = [_MASCOT, _BAR, _TASKBAR, _MINIMIZED, _EDITOR, _DESKTOP]
+
+
+def _pick(foreground: int, *, overlays=(_MASCOT, _BAR), app_windows=(_EDITOR,)):
+ def next_below(hwnd: int) -> int:
+ index = _ORDER.index(hwnd)
+ return _ORDER[index + 1] if index + 1 < len(_ORDER) else 0
+
+ return ws._pick_app_window(
+ foreground,
+ next_below=next_below,
+ is_own_overlay=lambda hwnd: hwnd in overlays,
+ is_app_window=lambda hwnd: hwnd in app_windows,
+ is_desktop=lambda hwnd: hwnd == _DESKTOP,
+ )
+
+
+def test_normal_foreground_window_is_kept() -> None:
+ assert _pick(_EDITOR) == _EDITOR
+
+
+def test_foreground_overlay_falls_through_to_the_app_window_under_it() -> None:
+ assert _pick(_MASCOT) == _EDITOR
+
+
+def test_the_taskbar_between_overlay_and_app_does_not_stop_the_walk() -> None:
+ assert _pick(_BAR) == _EDITOR
+
+
+def test_only_the_desktop_under_the_overlay_means_no_app_window() -> None:
+ assert _pick(_MASCOT, app_windows=()) is None
+
+
+def test_end_of_the_window_list_means_no_app_window() -> None:
+ def next_below(hwnd: int) -> int:
+ return {_MASCOT: _EDITOR}.get(hwnd, 0)
+
+ assert ws._pick_app_window(
+ _MASCOT,
+ next_below=next_below,
+ is_own_overlay=lambda hwnd: hwnd == _MASCOT,
+ is_app_window=lambda _hwnd: False,
+ is_desktop=lambda _hwnd: False,
+ ) is None
+
+
+def test_foreground_app_window_dispatches_to_win32(monkeypatch) -> None:
+ editor = WindowInfo(title="Editor", handle=_EDITOR, pid=7)
+ monkeypatch.setattr(ws, "detect_platform", lambda: "win32")
+ monkeypatch.setattr(ws, "_foreground_app_window_windows", lambda: editor)
+
+ assert ws.foreground_app_window() == editor
+
+
+def test_foreground_app_window_is_the_foreground_window_elsewhere(monkeypatch) -> None:
+ front = WindowInfo(title="Terminal", handle=99, pid=8)
+ for platform in ("darwin", "linux"):
+ monkeypatch.setattr(ws, "detect_platform", lambda platform=platform: platform)
+ monkeypatch.setattr(ws, "foreground_window", lambda: front)
+
+ assert ws.foreground_app_window() == front
+
+
+def test_foreground_app_window_never_raises(monkeypatch) -> None:
+ def boom():
+ raise OSError("user32 gone")
+
+ monkeypatch.setattr(ws, "detect_platform", lambda: "win32")
+ monkeypatch.setattr(ws, "_foreground_app_window_windows", boom)
+
+ assert ws.foreground_app_window() is None
diff --git a/tests/unit/screen_context/test_ports.py b/tests/unit/screen_context/test_ports.py
index 644c87da28..35a212071a 100644
--- a/tests/unit/screen_context/test_ports.py
+++ b/tests/unit/screen_context/test_ports.py
@@ -81,3 +81,30 @@ def test_visible_windows_retains_untitled_password_manager_candidate(
assert len(visible) == 1
assert visible[0].app_name == "1Password.exe"
assert visible[0].title == ""
+
+
+def test_foreground_snapshot_reads_the_app_window_not_an_overlay(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """BUG-228: a click on the mascot must not make it the appshot's window."""
+ monkeypatch.setattr(ports, "_input_space", nullcontext)
+ monkeypatch.setattr(
+ "jarvis.platform.window_state.foreground_window",
+ lambda: WindowInfo(title="JarvisOrb", handle=1, pid=11),
+ )
+ monkeypatch.setattr(
+ "jarvis.platform.window_state.foreground_app_window",
+ lambda: WindowInfo(title="Editor", handle=2, pid=22),
+ )
+ monkeypatch.setattr(
+ "jarvis.platform.window_state.window_frame_rect",
+ lambda _window: (0, 0, 1200, 800),
+ )
+ monkeypatch.setattr(ports, "_app_name_for_pid", lambda _pid: "Code.exe")
+
+ snapshot = ports.PlatformWindowProbe().foreground_snapshot()
+
+ assert snapshot is not None
+ assert snapshot.handle == 2
+ assert snapshot.facts.title == "Editor"
+ assert snapshot.facts.frame_rect == (0, 0, 1200, 800)