From 9ffe37cab7275546a0bfe2b9458108a990e11854 Mon Sep 17 00:00:00 2001 From: adlee-was-taken Date: Sat, 17 Oct 2026 21:41:30 -0400 Subject: [PATCH 1/3] fix(shadow_report): window is days, not seconds --- scripts/shadow_report.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/scripts/shadow_report.py b/scripts/shadow_report.py index 85dcbe1..0ee032f 100644 --- a/scripts/shadow_report.py +++ b/scripts/shadow_report.py @@ -19,14 +19,14 @@ def run_report(db_path: str, days: int): sys.exit(1) # Window filter - use AND so it can be appended after an existing WHERE - where_clause = "AND julianday(observed_at) >= julianday('now') - ? / 86400.0" + where_clause = "AND julianday(observed_at) >= julianday('now') - ?" params = (days,) # Empty table guard try: has_rows = conn.execute( "SELECT 1 FROM classifier_shadow " - "WHERE julianday(observed_at) >= julianday('now') - ? / 86400.0 " + "WHERE julianday(observed_at) >= julianday('now') - ? " "LIMIT 1", params ).fetchone() except sqlite3.OperationalError as e: @@ -51,7 +51,7 @@ def run_report(db_path: str, days: int): shadow_count = conn.execute( "SELECT COUNT(*) as n FROM classifier_shadow " - "WHERE julianday(observed_at) >= julianday('now') - ? / 86400.0", + "WHERE julianday(observed_at) >= julianday('now') - ?", params, ).fetchone()["n"] @@ -60,7 +60,7 @@ def run_report(db_path: str, days: int): SELECT COUNT(DISTINCT s.decision_id) as n FROM classifier_shadow s JOIN route_decisions d ON s.decision_id = d.id - WHERE d.kind = 'chat' AND julianday(s.observed_at) >= julianday('now') - ? / 86400.0 + WHERE d.kind = 'chat' AND julianday(s.observed_at) >= julianday('now') - ? """, params ).fetchone()["n"] @@ -130,7 +130,7 @@ def run_report(db_path: str, days: int): JOIN route_decisions d ON s.decision_id = d.id WHERE s.abstained = 0 AND d.classification_source = 'classifier' - AND julianday(s.observed_at) >= julianday('now') - ? / 86400.0 + AND julianday(s.observed_at) >= julianday('now') - ? """, params ).fetchall() @@ -162,7 +162,7 @@ def run_report(db_path: str, days: int): FROM classifier_shadow s JOIN route_decisions d ON s.decision_id = d.id WHERE d.classification_source IN ('session_history', 'fallback') - AND julianday(s.observed_at) >= julianday('now') - ? / 86400.0 + AND julianday(s.observed_at) >= julianday('now') - ? """, params ).fetchall() @@ -194,7 +194,7 @@ def run_report(db_path: str, days: int): JOIN route_decisions d ON s.decision_id = d.id JOIN verifications v ON d.request_id = v.request_id WHERE v.kind = 'client_outcome' - AND julianday(s.observed_at) >= julianday('now') - ? / 86400.0 + AND julianday(s.observed_at) >= julianday('now') - ? """, params ).fetchall() @@ -231,7 +231,7 @@ def run_report(db_path: str, days: int): r["confidence"] for r in conn.execute( "SELECT confidence FROM classifier_shadow " - "WHERE julianday(observed_at) >= julianday('now') - ? / 86400.0", + "WHERE julianday(observed_at) >= julianday('now') - ?", params, ) if r["confidence"] is not None @@ -256,7 +256,7 @@ def run_report(db_path: str, days: int): SUM(CASE WHEN s.task_category = d.task_category THEN 1 ELSE 0 END) as matches FROM classifier_shadow s JOIN route_decisions d ON s.decision_id = d.id - WHERE s.abstained = 0 AND {cond} AND julianday(s.observed_at) >= julianday('now') - ? / 86400.0 + WHERE s.abstained = 0 AND {cond} AND julianday(s.observed_at) >= julianday('now') - ? """, params ).fetchone() if res and res["total"] > 0: @@ -267,7 +267,7 @@ def run_report(db_path: str, days: int): # Sessions stats sess_data = conn.execute( "SELECT session_key, COUNT(*) as n FROM classifier_shadow " - "WHERE julianday(observed_at) >= julianday('now') - ? / 86400.0 " + "WHERE julianday(observed_at) >= julianday('now') - ? " "GROUP BY session_key", params ).fetchall() if sess_data: -- 2.49.1 From 5897aea901996f8de6b5f3c9a834125a19a5fd25 Mon Sep 17 00:00:00 2001 From: adlee-was-taken Date: Sat, 17 Oct 2026 21:45:00 -0400 Subject: [PATCH 2/3] test(shadow_report): window honours days --- tests/test_shadow_encoder.py | 114 ++++++++++++++++++++++++++++++++++- 1 file changed, 113 insertions(+), 1 deletion(-) diff --git a/tests/test_shadow_encoder.py b/tests/test_shadow_encoder.py index 61a0552..399ef89 100644 --- a/tests/test_shadow_encoder.py +++ b/tests/test_shadow_encoder.py @@ -800,4 +800,116 @@ def test_chat_flag_on_schedules_shadow_task(tmp_path, monkeypatch): assert resp.status_code == 200 assert len(calls) == 1 assert calls[0][0] is not None - assert isinstance(calls[0][0], int) + + +# --------------------------------------------------------------------------- +# 17. shadow_report.run_report honours the --days window +# --------------------------------------------------------------------------- + + +def _write_shadow_row(conn, observed_at, decision_id=1, **extra): + """Insert a classifier_shadow row and return its decision_id.""" + conn.execute( + """ + INSERT INTO classifier_shadow ( + decision_id, session_key, labeler, task_category, + confidence, abstained, reject_reason, latency_ms, observed_at + ) VALUES (?, 'sess-x', 'local_encoder', 'coding_refactor', + 0.9, 0, NULL, 50, ?) + """, + (decision_id, observed_at), + ) + conn.commit() + + +def test_report_days_window_2h_row_with_days_1(db_path, capsys): + """A row observed 2 hours ago is counted when days=1.""" + two_hours_ago = ( + datetime.now(timezone.utc) + ).isoformat() + conn = sqlite3.connect(str(db_path)) + conn.execute( + """ + INSERT INTO route_decisions ( + id, observed_at, kind, task_category, task_tier, + required_context_tokens, confidence, classifier_ms, + classification_source, selected_model, selected_provider + ) VALUES (1, ?, 'chat', 'coding_refactor', 2, + 500, 0.9, 100, 'classifier', 'mock-1', 'neuralwatt') + """, + (two_hours_ago,), + ) + _write_shadow_row(conn, two_hours_ago) + conn.close() + + import contextlib + import io + f = io.StringIO() + with contextlib.redirect_stdout(f): + try: + shadow_report.run_report(str(db_path), 1) + except SystemExit: + pass + out = f.getvalue() + assert "Shadow rows: 1" in out + + +def test_report_days_window_3d_row_excluded_days_1_included_days_7(db_path, capsys): + """A row 3 days ago is excluded with days=1 but included with days=7.""" + three_days_ago = ( + datetime.now(timezone.utc).replace( + year=datetime.now(timezone.utc).year, + month=datetime.now(timezone.utc).month, + day=datetime.now(timezone.utc).day - 3, + ) + ).isoformat() + + conn = sqlite3.connect(str(db_path)) + now_iso = datetime.now(timezone.utc).isoformat() + conn.execute( + """ + INSERT INTO route_decisions ( + id, observed_at, kind, task_category, task_tier, + required_context_tokens, confidence, classifier_ms, + classification_source, selected_model, selected_provider + ) VALUES (1, ?, 'chat', 'coding_refactor', 2, + 500, 0.9, 100, 'classifier', 'mock-1', 'neuralwatt') + """, + (three_days_ago,), + ) + conn.execute( + """ + INSERT INTO route_decisions ( + id, observed_at, kind, task_category, task_tier, + required_context_tokens, confidence, classifier_ms, + classification_source, selected_model, selected_provider + ) VALUES (2, ?, 'chat', 'coding_refactor', 2, + 500, 0.9, 100, 'classifier', 'mock-1', 'neuralwatt') + """, + (now_iso,), + ) + _write_shadow_row(conn, three_days_ago, decision_id=1) + _write_shadow_row(conn, now_iso, decision_id=2) + conn.commit() + conn.close() + + import contextlib + import io as _io + + f = _io.StringIO() + with contextlib.redirect_stdout(f): + try: + shadow_report.run_report(str(db_path), 1) + except SystemExit: + pass + out1 = f.getvalue() + assert "Shadow rows: 1" in out1, f"Expected 1 shadow row with days=1, got:\n{out1}" + + f2 = _io.StringIO() + with contextlib.redirect_stdout(f2): + try: + shadow_report.run_report(str(db_path), 7) + except SystemExit: + pass + out2 = f2.getvalue() + assert "Shadow rows: 2" in out2, f"Expected 2 shadow rows with days=7, got:\n{out2}" -- 2.49.1 From 9c9cdcbb31e6f73383da034d224dd2831f266fa3 Mon Sep 17 00:00:00 2001 From: adlee-was-taken Date: Sat, 17 Oct 2026 22:00:02 -0400 Subject: [PATCH 3/3] test(shadow_report): fix window tests (real 2h offset, restore assertion) --- tests/test_shadow_encoder.py | 128 +++++++++++++---------------------- 1 file changed, 48 insertions(+), 80 deletions(-) diff --git a/tests/test_shadow_encoder.py b/tests/test_shadow_encoder.py index 399ef89..a6c6676 100644 --- a/tests/test_shadow_encoder.py +++ b/tests/test_shadow_encoder.py @@ -17,6 +17,7 @@ import json import sqlite3 import sys from datetime import datetime, timezone +from datetime import timedelta from pathlib import Path import pytest @@ -800,6 +801,7 @@ def test_chat_flag_on_schedules_shadow_task(tmp_path, monkeypatch): assert resp.status_code == 200 assert len(calls) == 1 assert calls[0][0] is not None + assert isinstance(calls[0][0], int) # --------------------------------------------------------------------------- @@ -808,7 +810,10 @@ def test_chat_flag_on_schedules_shadow_task(tmp_path, monkeypatch): def _write_shadow_row(conn, observed_at, decision_id=1, **extra): - """Insert a classifier_shadow row and return its decision_id.""" + """Insert a classifier_shadow row. + observed_at is a timedelta offset from now (e.g. timedelta(hours=2)). + """ + ts = (datetime.now(timezone.utc) - observed_at).isoformat() conn.execute( """ INSERT INTO classifier_shadow ( @@ -817,99 +822,62 @@ def _write_shadow_row(conn, observed_at, decision_id=1, **extra): ) VALUES (?, 'sess-x', 'local_encoder', 'coding_refactor', 0.9, 0, NULL, 50, ?) """, - (decision_id, observed_at), + (decision_id, ts), ) conn.commit() +def _route_decision(conn, id_, offset, **extra): + """Insert a route_decision row at ``now - offset``. + + *offset* is a ``datetime.timedelta`` (e.g. ``timedelta(hours=2)``). + """ + ts = (datetime.now(timezone.utc) - offset).isoformat() + conn.execute( + """ + INSERT INTO route_decisions ( + id, observed_at, kind, task_category, task_tier, + required_context_tokens, confidence, classifier_ms, + classification_source, selected_model, selected_provider + ) VALUES (?, ?, 'chat', 'coding_refactor', 2, + 500, 0.9, 100, 'classifier', 'mock-1', 'neuralwatt') + """, + (id_, ts), + ) + + def test_report_days_window_2h_row_with_days_1(db_path, capsys): """A row observed 2 hours ago is counted when days=1.""" - two_hours_ago = ( - datetime.now(timezone.utc) - ).isoformat() conn = sqlite3.connect(str(db_path)) - conn.execute( - """ - INSERT INTO route_decisions ( - id, observed_at, kind, task_category, task_tier, - required_context_tokens, confidence, classifier_ms, - classification_source, selected_model, selected_provider - ) VALUES (1, ?, 'chat', 'coding_refactor', 2, - 500, 0.9, 100, 'classifier', 'mock-1', 'neuralwatt') - """, - (two_hours_ago,), - ) - _write_shadow_row(conn, two_hours_ago) + _route_decision(conn, 1, timedelta(hours=2)) + _write_shadow_row(conn, timedelta(hours=2)) conn.close() - import contextlib - import io - f = io.StringIO() - with contextlib.redirect_stdout(f): - try: - shadow_report.run_report(str(db_path), 1) - except SystemExit: - pass - out = f.getvalue() - assert "Shadow rows: 1" in out + try: + shadow_report.run_report(str(db_path), 1) + except SystemExit: + pass + captured = capsys.readouterr() + assert "Shadow rows: 1" in captured.out def test_report_days_window_3d_row_excluded_days_1_included_days_7(db_path, capsys): """A row 3 days ago is excluded with days=1 but included with days=7.""" - three_days_ago = ( - datetime.now(timezone.utc).replace( - year=datetime.now(timezone.utc).year, - month=datetime.now(timezone.utc).month, - day=datetime.now(timezone.utc).day - 3, - ) - ).isoformat() - conn = sqlite3.connect(str(db_path)) - now_iso = datetime.now(timezone.utc).isoformat() - conn.execute( - """ - INSERT INTO route_decisions ( - id, observed_at, kind, task_category, task_tier, - required_context_tokens, confidence, classifier_ms, - classification_source, selected_model, selected_provider - ) VALUES (1, ?, 'chat', 'coding_refactor', 2, - 500, 0.9, 100, 'classifier', 'mock-1', 'neuralwatt') - """, - (three_days_ago,), - ) - conn.execute( - """ - INSERT INTO route_decisions ( - id, observed_at, kind, task_category, task_tier, - required_context_tokens, confidence, classifier_ms, - classification_source, selected_model, selected_provider - ) VALUES (2, ?, 'chat', 'coding_refactor', 2, - 500, 0.9, 100, 'classifier', 'mock-1', 'neuralwatt') - """, - (now_iso,), - ) - _write_shadow_row(conn, three_days_ago, decision_id=1) - _write_shadow_row(conn, now_iso, decision_id=2) - conn.commit() + _route_decision(conn, 1, timedelta(days=3)) + _route_decision(conn, 2, timedelta(seconds=0)) + _write_shadow_row(conn, timedelta(days=3), decision_id=1) + _write_shadow_row(conn, timedelta(seconds=0), decision_id=2) conn.close() - import contextlib - import io as _io + try: + shadow_report.run_report(str(db_path), 1) + except SystemExit: + pass + assert "Shadow rows: 1" in capsys.readouterr().out - f = _io.StringIO() - with contextlib.redirect_stdout(f): - try: - shadow_report.run_report(str(db_path), 1) - except SystemExit: - pass - out1 = f.getvalue() - assert "Shadow rows: 1" in out1, f"Expected 1 shadow row with days=1, got:\n{out1}" - - f2 = _io.StringIO() - with contextlib.redirect_stdout(f2): - try: - shadow_report.run_report(str(db_path), 7) - except SystemExit: - pass - out2 = f2.getvalue() - assert "Shadow rows: 2" in out2, f"Expected 2 shadow rows with days=7, got:\n{out2}" + try: + shadow_report.run_report(str(db_path), 7) + except SystemExit: + pass + assert "Shadow rows: 2" in capsys.readouterr().out -- 2.49.1