"""Inspect real local SQLite joins over an authored synthetic event log.""" import argparse import hashlib import json from pathlib import Path from history import History, minute ROOT = Path(__file__).resolve().parent KEY = 'user_events_1d@v1' CUTOFF = minute('2026-01-08T12:00:00Z') FIRST = minute('2026-01-08T12:30:00Z') CORRECTION = minute('2026-01-08T14:00:00Z') def events(): return json.loads((ROOT / 'scenario.json').read_text())['events'] def observations(): return [dict(id=id, entity=entity, predict_at='2026-01-08T'+time+'Z') for id, entity, time in [('before-job','user-01','12:15:00'), ('first-read','user-01','13:00:00'), ('after-correction','user-01','15:00:00'), ('unknown-user','user-99','15:00:00')]] def record(): db = History() try: db.materialize(events(), CUTOFF, FIRST) before = db.join(observations(), KEY) db.materialize(events(), CUTOFF, CORRECTION, 'corrected') after = db.join(observations(), KEY) return {'mode': 'local SQLite; authored synthetic data; no model', 'input_sha256': hashlib.sha256((ROOT/'scenario.json').read_bytes()).hexdigest(), 'cutoff': CUTOFF, 'first_published_at': FIRST, 'corrected_published_at': CORRECTION, 'before_backfill': before, 'after_backfill': after, 'historical_read_unchanged': before[1] == after[1], 'retained_vintages_user_01': db.db.execute('SELECT COUNT(*) FROM history ' 'WHERE entity=?', ('user-01',)).fetchone()[0]} finally: db.close() def main(): p = argparse.ArgumentParser(); p.add_argument('--check', action='store_true'); p.add_argument('--save', type=Path) args=p.parse_args(); result=record() if args.check and result != json.loads((ROOT/'join-result.json').read_text()): raise SystemExit('joined rows differ from the bundled record') if args.save: args.save.write_text(json.dumps(result, indent=2)+'\n') print('Feature window ends at Jan 08 12:00 UTC; first publication 12:30; correction 14:00') for row in result['after_backfill']: feature=row['feature']; value='missing' if feature is None else feature['value'] print(f"{row['id']}: predict_at={row['predict_at']}; feature={value}") print('13:00 historical read unchanged after correction:', result['historical_read_unchanged']) print('Retained user-01 vintages:', result['retained_vintages_user_01']) if __name__=='__main__': main()