diff --git a/screenmind/engine/analyzer.py b/screenmind/engine/analyzer.py index f11d59b..98297f9 100644 --- a/screenmind/engine/analyzer.py +++ b/screenmind/engine/analyzer.py @@ -25,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") @@ -136,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. @@ -714,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) @@ -735,8 +745,13 @@ def _normalize( # ── Phase 1: Walk the hierarchy to find best app name ──────── resolved_name = None + # 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 + # 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/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 4ffdf18..efbd2ed 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 _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._os_app_name_is_trusted", lambda: False) + 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,68 @@ def test_scenario_19_browser_process_shadows_title(self, analyzer): assert result.activity_category == "browsing" # Browser: trusts Gemma +class TestReconcileAppNameMacOS: + """Where the adapter trusts the OS app name (macOS), it wins over title parsing.""" + + @pytest.fixture(autouse=True) + 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.""" + 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 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."""