Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion screenmind/engine/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand Down
10 changes: 10 additions & 0 deletions screenmind/platform_support/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<content> - <App>". Adapters whose OS reports a clean,
user-facing app name override this to True.
"""
return False
6 changes: 6 additions & 0 deletions screenmind/platform_support/macos.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
68 changes: 68 additions & 0 deletions tests/test_app_reconciliation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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."""

Expand Down