From 4b3444997f61b76da0c04e86447b8880b85dacd5 Mon Sep 17 00:00:00 2001 From: Sarcastic-Soul Date: Sat, 15 Aug 2026 23:02:46 +0530 Subject: [PATCH] fix(enrich): skip events whose timestamp will not parse enrich_events guards frame rows against unparseable timestamps, with a comment saying why, but the event loop below had no equivalent check. parse_epoch returns 0.0 on garbage, and nearest_index(frame_epochs, 0.0) resolves to index 0 -- so the event was attributed to the window's FIRST frame. The SQL window is a string comparison, so a partly-malformed stamp ("2026-07-04T99:99:99", or a truncated write) passes it and reaches the parser. Observed: a click labeled "Merge" made in Chrome attributed to Mail, with frame_dt_ms of 56 years and confidence still reported as high because the event carried a native label. Skip those rows instead. An event that cannot be placed in time has no honest frame to belong to, and the package's contract is that unresolvable data stays unresolved rather than being invented. Fixes #53 Co-Authored-By: Claude Opus 5 --- src/activity_frames/enrich.py | 8 ++++++ tests/test_robustness.py | 52 +++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/src/activity_frames/enrich.py b/src/activity_frames/enrich.py index 94f4178..d4f229d 100644 --- a/src/activity_frames/enrich.py +++ b/src/activity_frames/enrich.py @@ -188,6 +188,14 @@ def enrich_events( for ts, etype, x, y, elem_name, elem_role, text_content, event_app in event_rows: epoch = parse_epoch(ts or "") + if epoch <= 0: + # Same guard the frame rows get above. The SQL window is a string + # comparison, so a partly-malformed stamp ("2026-07-04T99:99:99") + # passes it and lands here as 0.0 -- and nearest_index would then + # attribute the event to the window's FIRST frame: a real frame, + # wrong answer, reported at whatever confidence the label implies. + # An event we cannot place in time has no honest home. + continue etype = etype or "" # 1. Authoritative context from the nearest frame. diff --git a/tests/test_robustness.py b/tests/test_robustness.py index 4d358b2..9acaf84 100644 --- a/tests/test_robustness.py +++ b/tests/test_robustness.py @@ -105,6 +105,58 @@ def test_malformed_timestamps_are_skipped(tmp_path): assert all(f.app != "App" or f.start != "?" for f in doc.frames) +def test_unparseable_event_timestamps_are_not_attributed(tmp_path): + """An event whose timestamp will not parse is dropped, not pinned to the + window's first frame. + + The SQL window is a string comparison, so a stamp like + "2026-07-04T99:99:99" is inside the range but yields epoch 0.0 -- and + nearest_index(frame_epochs, 0.0) resolves to index 0. Left unguarded, a + click made in one app is attributed to whichever app happened to be open + first that day, at high confidence if it carried a native label. + """ + from activity_frames.enrich import enrich_events + + path = tmp_path / "badevent.sqlite" + conn = sqlite3.connect(path) + conn.executescript( + """ + CREATE TABLE frames (id INTEGER PRIMARY KEY, timestamp TIMESTAMP, + app_name TEXT, window_name TEXT, browser_url TEXT, focused BOOLEAN); + CREATE TABLE ui_events (id INTEGER PRIMARY KEY, timestamp DATETIME, + event_type TEXT, x INT, y INT, text_content TEXT, app_name TEXT, + element_name TEXT, element_role TEXT); + """ + ) + for ts, app in (("09:00:00", "Mail"), ("15:00:00", "Chrome")): + conn.execute( + "INSERT INTO frames (timestamp, app_name, window_name, focused) " + "VALUES (?, ?, 'W', 1)", + (f"2026-07-04T{ts}.000000+00:00", app), + ) + for bad in ("2026-07-04T99:99:99", "2026-07-04T10:00"): + conn.execute( + "INSERT INTO ui_events (timestamp, event_type, app_name, " + "element_name, element_role) VALUES (?, 'click', 'Chrome', " + "'Merge', 'AXButton')", + (bad,), + ) + conn.execute( + "INSERT INTO ui_events (timestamp, event_type, app_name, element_name, " + "element_role) VALUES ('2026-07-04T15:00:01.000000+00:00', 'click', " + "'Chrome', 'Good', 'AXButton')" + ) + conn.commit() + conn.close() + + events = enrich_events(Database(str(path)), "2026-07-04T00:00:00", + "2026-07-05T00:00:00") + assert [e.label for e in events] == ["Good"] + assert all(e.epoch > 0 for e in events) + # nothing was parked on the first frame of the day + assert not any(e.app == "Mail" for e in events) + + def test_parse_epoch_never_raises(): for junk in ["", "x", "2026", "not-a-date", "2026-99-99T00:00:00", None or ""]: assert parse_epoch(junk) == 0.0