From 5ed6650cddf747cc9dd8bf8aa8796ec4075b5770 Mon Sep 17 00:00:00 2001 From: Pavel Smirnov Date: Tue, 6 Oct 2026 10:06:13 +0200 Subject: [PATCH] fix(worker): pass the OS-detected app to backfilled captures _backfill_skipped() read the app_name column to build the CaptureResult. Rows it picks up were never analyzed, so app_name is NULL and the analyzer got no OS hint. The final app name then fell back to title parsing or Gemma's visual guess (e.g. a Chrome tab became "ScreenMind"). Read detected_app first and fall back to app_name. Co-Authored-By: Claude Opus 5.5 --- screenmind/workers/analysis_worker.py | 2 +- tests/test_workers.py | 30 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/screenmind/workers/analysis_worker.py b/screenmind/workers/analysis_worker.py index 59477af..b0544da 100644 --- a/screenmind/workers/analysis_worker.py +++ b/screenmind/workers/analysis_worker.py @@ -647,7 +647,7 @@ async def _backfill_skipped(self): try: conn = self._db._get_conn() row = conn.execute( - """SELECT id, screenshot_path, window_title, app_name, ocr_text, ocr_boxes + """SELECT id, screenshot_path, window_title, COALESCE(detected_app, app_name), ocr_text, ocr_boxes FROM activities WHERE status IN ('pending', 'skipped', 'failed') AND DATE(timestamp) = DATE('now', 'localtime') diff --git a/tests/test_workers.py b/tests/test_workers.py index 8a7a396..933812e 100644 --- a/tests/test_workers.py +++ b/tests/test_workers.py @@ -174,3 +174,33 @@ def test_extract_url_strips_punctuation(self): from screenmind.workers.analysis_worker import _extract_all_urls urls = _extract_all_urls("See https://example.com/page.") assert urls[0] == "https://example.com/page" + + +class TestAnalysisWorkerBackfill: + """Backfill re-analyzes rows that were never analyzed.""" + + async def test_backfill_passes_detected_app_as_hint(self, db, tmp_path): + """Unanalyzed rows have app_name NULL; the OS-detected app must be used.""" + from PIL import Image + from screenmind.storage.models import ScreenshotEntry + from screenmind.workers.analysis_worker import AnalysisWorker + + shot = tmp_path / "shot.jpg" + Image.new("RGB", (64, 64), "white").save(shot) + db.insert_activity(ScreenshotEntry( + timestamp=datetime.now(), + screenshot_path=str(shot), + window_title="Chats", + detected_app_name="Telegram", + bookmarked=False, + analyzed=False, + )) + + worker = AnalysisWorker(queue=asyncio.Queue(maxsize=100), database=db) + worker._process = AsyncMock() + await worker._backfill_skipped() + + worker._process.assert_awaited_once() + capture = worker._process.await_args.args[0] + assert capture.app_name == "Telegram" + assert capture.window_title == "Chats"