From a57f5b03c1d327fe69f81f121fee2886b9559839 Mon Sep 17 00:00:00 2001 From: Pavel Smirnov Date: Tue, 6 Oct 2026 10:06:13 +0200 Subject: [PATCH 1/2] fix(analyzer): prefer the OS window owner over title parsing on macOS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit _normalize() resolves the app name from the " - " title convention first (L1), then the OS process name (L2). That order fits Windows, where titles end with the app name and the process name is an exe like "chrome.exe". On macOS titles rarely end with the app name: Terminal's title is "dir — command — 120×30", so the app became "120×30"; a Chrome tab "ScreenMind — Your AI Memory, 100% Local" became the app. On macOS the window owner name from Quartz is ground truth, so use it first (still skipping generic wrappers). Title parsing remains the fallback when there is no owner name. Windows and Linux are unchanged. Existing reconciliation scenarios use Windows-style inputs, so they now pin sys.platform to win32; new macOS scenarios cover the darwin path. Co-Authored-By: Claude Opus 5.5 --- screenmind/engine/analyzer.py | 9 ++++++++- tests/test_app_reconciliation.py | 34 ++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/screenmind/engine/analyzer.py b/screenmind/engine/analyzer.py index f11d59b..b723350 100644 --- a/screenmind/engine/analyzer.py +++ b/screenmind/engine/analyzer.py @@ -17,6 +17,7 @@ import io import json import re +import sys import time from pathlib import Path from typing import Optional @@ -735,8 +736,14 @@ def _normalize( # ── Phase 1: Walk the hierarchy to find best app name ──────── resolved_name = None + # macOS: the window owner from Quartz is ground truth, and titles rarely + # end with the app name (Terminal: "dir — cmd — 120×30"), so use it first. + if sys.platform == "darwin" and app_name_hint: + if app_name_hint.lower().strip() not in GENERIC_PROCESS_NAMES: + resolved_name = app_name_hint + # L1: Title " - " extraction ("main.py - Visual Studio Code" → "Visual Studio Code") - title_app = _extract_app_from_title(window_title) + title_app = None if resolved_name else _extract_app_from_title(window_title) if title_app: resolved_name = title_app diff --git a/tests/test_app_reconciliation.py b/tests/test_app_reconciliation.py index 4ffdf18..36fee8a 100644 --- a/tests/test_app_reconciliation.py +++ b/tests/test_app_reconciliation.py @@ -291,6 +291,12 @@ def _make_record(**kwargs): class TestReconcileAppName: """Test the 4-level hierarchy for app name resolution.""" + @pytest.fixture(autouse=True) + def _non_macos(self, monkeypatch): + # These scenarios use Windows/Linux-style process names and titles. + # macOS resolves the OS owner first; see TestReconcileAppNameMacOS. + monkeypatch.setattr("screenmind.engine.analyzer.sys.platform", "win32") + def test_scenario_1_alacritty_misidentified(self, analyzer): """Alacritty (terminal) misidentified as VS Code.""" record = _make_record(app_name="VS Code", activity_category="coding") @@ -437,6 +443,34 @@ def test_scenario_19_browser_process_shadows_title(self, analyzer): assert result.activity_category == "browsing" # Browser: trusts Gemma +class TestReconcileAppNameMacOS: + """On macOS the Quartz window owner wins over title parsing.""" + + @pytest.fixture(autouse=True) + def _macos(self, monkeypatch): + monkeypatch.setattr("screenmind.engine.analyzer.sys.platform", "darwin") + + def test_terminal_title_suffix_ignored(self, analyzer): + """Terminal titles end in the window size, not the app name.""" + record = _make_record(app_name="Terminal", activity_category="terminal") + result = analyzer._normalize(record, app_name_hint="Terminal", + window_title="ScreenMind — python — 120×30") + assert result.app_name == "Terminal" + assert result.activity_category == "terminal" + + def test_owner_beats_title_segment(self, analyzer): + record = _make_record(app_name="Slack", activity_category="communication") + result = analyzer._normalize(record, app_name_hint="Slack", + window_title="general (Channel) - TripleTen - Slack") + assert result.app_name == "Slack" + + def test_no_owner_falls_back_to_title(self, analyzer): + record = _make_record(app_name="Code", activity_category="coding") + result = analyzer._normalize(record, app_name_hint=None, + window_title="main.py - Visual Studio Code") + assert result.app_name == "Visual Studio Code" + + class TestBackwardCompatibility: """Ensure old calling convention (no hints) works unchanged.""" From b14b7ef7d8054e6a2996b9c77c49674b4455972c Mon Sep 17 00:00:00 2001 From: Pavel Smirnov Date: Tue, 6 Oct 2026 11:53:03 +0200 Subject: [PATCH 2/2] refactor(analyzer): ask the platform adapter whether to trust the OS app name Replace the sys.platform == "darwin" check in _normalize() with a property on the platform layer, following the platform_support pattern: - PlatformAdapter.trusts_os_app_name is False by default (Windows/Linux keep title-first resolution). - MacOSAdapter returns True: the window owner name is user-facing, while titles rarely end with the app name. - analyzer._os_app_name_is_trusted() reads it and treats any adapter error as "not trusted". Tests now patch that helper instead of sys.platform, and check the default/override on each adapter. Co-Authored-By: Claude Opus 5.5 --- screenmind/engine/analyzer.py | 16 +++++++--- screenmind/platform_support/base.py | 10 +++++++ screenmind/platform_support/macos.py | 6 ++++ tests/test_app_reconciliation.py | 44 ++++++++++++++++++++++++---- 4 files changed, 67 insertions(+), 9 deletions(-) diff --git a/screenmind/engine/analyzer.py b/screenmind/engine/analyzer.py index b723350..98297f9 100644 --- a/screenmind/engine/analyzer.py +++ b/screenmind/engine/analyzer.py @@ -17,7 +17,6 @@ import io import json import re -import sys import time from pathlib import Path from typing import Optional @@ -26,6 +25,7 @@ from screenmind.config import settings from screenmind.engine import llm_client +from screenmind.platform_support import adapter as platform_adapter from screenmind.storage.models import ActivityRecord logger = logging.getLogger("screenmind.engine.analyzer") @@ -137,6 +137,14 @@ }) +def _os_app_name_is_trusted() -> bool: + """Ask the platform adapter whether the OS app name beats title parsing.""" + try: + return platform_adapter().trusts_os_app_name + except Exception: + return False + + def _extract_app_from_title(window_title: Optional[str]) -> Optional[str]: """Extract app name from the 'content - AppName' title convention. @@ -715,6 +723,7 @@ def _normalize( """Normalize fields and reconcile app identity using three signals. Hierarchy for app name (first non-empty wins, then compared with Gemma): + L0: OS process name, only where the platform adapter trusts it (macOS) L1: Title " - " extraction (most reliable pattern) L2: OS process name (ground truth, skip generic wrappers) L3: Title simple extraction (short clean title, for OS=None/generic) @@ -736,9 +745,8 @@ def _normalize( # ── Phase 1: Walk the hierarchy to find best app name ──────── resolved_name = None - # macOS: the window owner from Quartz is ground truth, and titles rarely - # end with the app name (Terminal: "dir — cmd — 120×30"), so use it first. - if sys.platform == "darwin" and app_name_hint: + # L0: platforms with a clean OS app name (macOS) use it before title parsing + if app_name_hint and _os_app_name_is_trusted(): if app_name_hint.lower().strip() not in GENERIC_PROCESS_NAMES: resolved_name = app_name_hint diff --git a/screenmind/platform_support/base.py b/screenmind/platform_support/base.py index ce44dfe..552ddcc 100644 --- a/screenmind/platform_support/base.py +++ b/screenmind/platform_support/base.py @@ -52,3 +52,13 @@ def is_a11y_available(self) -> bool: def platform_name(self) -> str: """Human-readable platform name.""" return "Unknown" + + @property + def trusts_os_app_name(self) -> bool: + """Whether get_active_app_name() is a better app identity than the window title. + + False by default: on Windows the process name is an exe like "chrome.exe", + and titles follow " - ". Adapters whose OS reports a clean, + user-facing app name override this to True. + """ + return False diff --git a/screenmind/platform_support/macos.py b/screenmind/platform_support/macos.py index 9c3b3bd..19fbb5a 100644 --- a/screenmind/platform_support/macos.py +++ b/screenmind/platform_support/macos.py @@ -104,6 +104,12 @@ def get_active_app_name(self) -> Optional[str]: def is_a11y_available(self) -> bool: return self._ax_available + @property + def trusts_os_app_name(self) -> bool: + """The window owner name ("Terminal", "Google Chrome") is user-facing, while + titles rarely end with the app name (Terminal: "dir — cmd — 120×30").""" + return True + def extract_a11y_text(self, hwnd: Optional[int] = None) -> Tuple[Optional[str], str]: """ Extract accessible text using macOS Accessibility API (AXUIElement). diff --git a/tests/test_app_reconciliation.py b/tests/test_app_reconciliation.py index 36fee8a..efbd2ed 100644 --- a/tests/test_app_reconciliation.py +++ b/tests/test_app_reconciliation.py @@ -292,10 +292,10 @@ class TestReconcileAppName: """Test the 4-level hierarchy for app name resolution.""" @pytest.fixture(autouse=True) - def _non_macos(self, monkeypatch): + def _os_name_not_trusted(self, monkeypatch): # These scenarios use Windows/Linux-style process names and titles. # macOS resolves the OS owner first; see TestReconcileAppNameMacOS. - monkeypatch.setattr("screenmind.engine.analyzer.sys.platform", "win32") + monkeypatch.setattr("screenmind.engine.analyzer._os_app_name_is_trusted", lambda: False) def test_scenario_1_alacritty_misidentified(self, analyzer): """Alacritty (terminal) misidentified as VS Code.""" @@ -444,11 +444,11 @@ def test_scenario_19_browser_process_shadows_title(self, analyzer): class TestReconcileAppNameMacOS: - """On macOS the Quartz window owner wins over title parsing.""" + """Where the adapter trusts the OS app name (macOS), it wins over title parsing.""" @pytest.fixture(autouse=True) - def _macos(self, monkeypatch): - monkeypatch.setattr("screenmind.engine.analyzer.sys.platform", "darwin") + def _os_name_trusted(self, monkeypatch): + monkeypatch.setattr("screenmind.engine.analyzer._os_app_name_is_trusted", lambda: True) def test_terminal_title_suffix_ignored(self, analyzer): """Terminal titles end in the window size, not the app name.""" @@ -471,6 +471,40 @@ def test_no_owner_falls_back_to_title(self, analyzer): assert result.app_name == "Visual Studio Code" +class TestTrustsOsAppName: + """The platform layer decides whether the OS app name is trusted.""" + + def test_base_default_false(self): + from screenmind.platform_support.base import PlatformAdapter + assert PlatformAdapter.trusts_os_app_name.fget(object()) is False + + def test_macos_true(self): + from screenmind.platform_support.macos import MacOSAdapter + assert MacOSAdapter.trusts_os_app_name.fget(object()) is True + + def test_windows_and_linux_inherit_false(self): + from screenmind.platform_support.windows import WindowsAdapter + from screenmind.platform_support.linux import LinuxAdapter + from screenmind.platform_support.base import PlatformAdapter + assert WindowsAdapter.trusts_os_app_name is PlatformAdapter.trusts_os_app_name + assert LinuxAdapter.trusts_os_app_name is PlatformAdapter.trusts_os_app_name + + def test_analyzer_reads_adapter(self, monkeypatch): + import screenmind.engine.analyzer as analyzer + fake = type("A", (), {"trusts_os_app_name": True})() + monkeypatch.setattr(analyzer, "platform_adapter", lambda: fake) + assert analyzer._os_app_name_is_trusted() is True + fake.trusts_os_app_name = False + assert analyzer._os_app_name_is_trusted() is False + + def test_analyzer_adapter_error_means_untrusted(self, monkeypatch): + import screenmind.engine.analyzer as analyzer + def boom(): + raise RuntimeError("no adapter") + monkeypatch.setattr(analyzer, "platform_adapter", boom) + assert analyzer._os_app_name_is_trusted() is False + + class TestBackwardCompatibility: """Ensure old calling convention (no hints) works unchanged."""