"""Raw sample pruning per spec §3.4, ST-5. Raw samples are pruned opportunistically to 14 days. Hour observations and day aggregates are retained indefinitely. """ import sqlite3 from datetime import datetime, timedelta, timezone # Spec §3.4: Raw-sample retention RAW_SAMPLE_RETENTION_DAYS = 14 def prune_old_samples( conn: sqlite3.Connection, now: datetime, retention_days: int = RAW_SAMPLE_RETENTION_DAYS, ) -> int: """Remove raw samples older than retention_days. Args: conn: Connection to the observation store. now: Current UTC time. retention_days: Number of days to retain (default 14). Returns: Number of samples removed. """ cutoff = (now - timedelta(days=retention_days)).isoformat() cursor = conn.execute("DELETE FROM samples WHERE ts < ?", (cutoff,)) conn.commit() return cursor.rowcount