#!/usr/bin/env python3 """ Write batch summaries back to oracle.db. Reads /tmp/athena_summarize_batch.json (array of entry dicts with 'summary' key added by sub-agent), writes each summary JSON to entries.summary column. Usage: python3 write_summaries.py /tmp/athena_summarize_batch_result.json """ import json import sqlite3 import sys def main(): if len(sys.argv) < 2: print("Usage: python3 write_summaries.py ", file=sys.stderr) sys.exit(1) result_path = sys.argv[1] try: with open(result_path) as f: results = json.load(f) except (FileNotFoundError, json.JSONDecodeError) as e: print(f"Error reading {result_path}: {e}", file=sys.stderr) sys.exit(1) db_path = '/home/vpsadmin/oracle/oracle.db' conn = sqlite3.connect(db_path) cur = conn.cursor() written = 0 skipped = 0 errors = 0 for item in results: eid = item.get('id') summary = item.get('summary') if not eid or not summary: skipped += 1 continue # Validate summary has expected keys if not all(k in summary for k in ('one_liner', 'key_technical_point', 'potential_use_case', 'confidence')): print(f" ⚠ ID {eid}: missing required keys, skipping", file=sys.stderr) skipped += 1 continue # Quality gate: reject low-confidence or generic summaries ol = summary.get('one_liner', '') if len(ol) < 20: print(f" ⚠ ID {eid}: one_liner too short ({len(ol)} chars), skipping", file=sys.stderr) skipped += 1 continue if any(generic in ol.lower() for generic in ('this article discusses', 'this paper presents', 'see full')): print(f" ⚠ ID {eid}: generic one_liner, skipping", file=sys.stderr) skipped += 1 continue try: cur.execute("UPDATE entries SET summary = ? WHERE id = ?", (json.dumps(summary), eid)) written += 1 print(f" ✓ ID {eid}: {ol[:70]}...") except Exception as e: print(f" ✗ ID {eid}: {e}", file=sys.stderr) errors += 1 conn.commit() # Verify cur.execute("SELECT COUNT(*) FROM entries WHERE summary IS NOT NULL") total = cur.fetchone()[0] conn.close() print(f"\nResults: {written} written, {skipped} skipped, {errors} errors") print(f"Total entries with summary: {total}") return 0 if errors == 0 else 1 if __name__ == "__main__": sys.exit(main())