File size: 8,437 Bytes
ddd3b6f 0a0791f ddd3b6f 0a0791f ddd3b6f 0a0791f ddd3b6f 0a0791f ddd3b6f 0a0791f ddd3b6f 0a0791f ddd3b6f 0a0791f ddd3b6f e1a6941 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | """Local repository boundary. Cloud implementations must add per-user isolation."""
from contextlib import contextmanager
from typing import Protocol
from pathlib import Path
import sqlite3
import json
import threading
import uuid
from datetime import datetime, timezone
def now(): return datetime.now(timezone.utc).isoformat()
# Files whose schema this process already created. Rerunning the DDL on every request
# took a write lock and caused "database is locked" when batch requests overlapped.
_ready=set()
_ready_lock=threading.Lock()
class InspectionRepository(Protocol):
def save(self, record:dict)->dict: ...
def get(self, identifier:str)->dict|None: ...
def list(self,category='',verdict='',batch='',limit=100,offset=0)->list[dict]: ...
class SQLiteRepository:
def __init__(self,path:Path):
self.path=path
path.parent.mkdir(parents=True,exist_ok=True)
key=str(path.resolve())
with _ready_lock:
if key in _ready and path.exists():return
self._create_schema()
_ready.add(key)
def _create_schema(self):
with self.connect() as db:
db.executescript('''
PRAGMA journal_mode=WAL;
CREATE TABLE IF NOT EXISTS inspections(id TEXT PRIMARY KEY,created_at TEXT NOT NULL,category TEXT NOT NULL,verdict TEXT NOT NULL,batch TEXT NOT NULL,payload TEXT NOT NULL);
CREATE INDEX IF NOT EXISTS inspection_filter ON inspections(category,verdict,batch,created_at);
CREATE TABLE IF NOT EXISTS messages(id INTEGER PRIMARY KEY,inspection_id TEXT NOT NULL,created_at TEXT NOT NULL,role TEXT NOT NULL,payload TEXT NOT NULL);
CREATE TABLE IF NOT EXISTS alerts(id TEXT PRIMARY KEY,inspection_id TEXT NOT NULL,created_at TEXT NOT NULL,kind TEXT NOT NULL,acknowledged INTEGER NOT NULL DEFAULT 0);
CREATE TABLE IF NOT EXISTS failures(id TEXT PRIMARY KEY,created_at TEXT NOT NULL,category TEXT,error TEXT NOT NULL);
CREATE TABLE IF NOT EXISTS inspection_archives(id TEXT PRIMARY KEY,archived_at TEXT NOT NULL,archive_batch TEXT NOT NULL);
CREATE TABLE IF NOT EXISTS audit_events(id TEXT PRIMARY KEY,created_at TEXT NOT NULL,action TEXT NOT NULL,payload TEXT NOT NULL);
''')
@contextmanager
def connect(self):
# Commit on success, roll back on error, and always close the file.
connection=sqlite3.connect(self.path,timeout=20)
connection.row_factory=sqlite3.Row
try:
with connection:
yield connection
finally:
connection.close()
def save(self,record):
with self.connect() as db:
db.execute('INSERT INTO inspections VALUES(?,?,?,?,?,?)',(record['id'],record['created_at'],record['category'],record['verdict'],record.get('batch',''),json.dumps(record)))
if record['verdict']!='Good':
db.execute('INSERT INTO alerts(id,inspection_id,created_at,kind) VALUES(?,?,?,?)',(uuid.uuid4().hex,record['id'],record['created_at'],record['verdict']))
return record
def get(self,identifier):
with self.connect() as db:
row=db.execute('SELECT payload FROM inspections WHERE id=? AND id NOT IN (SELECT id FROM inspection_archives)',(identifier,)).fetchone()
return json.loads(row['payload']) if row else None
def list(self,category='',verdict='',batch='',limit=100,offset=0,archived=False):
clauses,params=['id '+('IN' if archived else 'NOT IN')+' (SELECT id FROM inspection_archives)'],[]
for key,value in [('category',category),('verdict',verdict),('batch',batch)]:
if value: clauses.append(key+'=?'); params.append(value)
query='SELECT payload FROM inspections'+(' WHERE '+' AND '.join(clauses) if clauses else '')+' ORDER BY created_at DESC LIMIT ? OFFSET ?'
with self.connect() as db:
rows=db.execute(query,[*params,limit,offset]).fetchall()
return [json.loads(r['payload']) for r in rows]
def stats(self):
with self.connect() as db:
counts={r['verdict']:r['n'] for r in db.execute('SELECT verdict,count(*) n FROM inspections WHERE id NOT IN (SELECT id FROM inspection_archives) GROUP BY verdict')}
batches=[dict(r) for r in db.execute('SELECT batch,count(*) total,sum(verdict="Anomalous") anomalous,sum(verdict="Uncertain") uncertain FROM inspections WHERE id NOT IN (SELECT id FROM inspection_archives) GROUP BY batch ORDER BY max(created_at) DESC')]
failures=db.execute('SELECT count(*) FROM failures').fetchone()[0]
archived=db.execute('SELECT count(*) FROM inspection_archives').fetchone()[0]
return {'counts':counts,'total':sum(counts.values()),'batches':batches,'failed_requests':failures,'archived_total':archived}
def alerts(self):
with self.connect() as db:
return [dict(r) for r in db.execute('SELECT * FROM alerts WHERE inspection_id NOT IN (SELECT id FROM inspection_archives) ORDER BY created_at DESC LIMIT 200')]
def archive(self,category='',verdict='',batch=''):
clauses=['id NOT IN (SELECT id FROM inspection_archives)'];params=[]
for key,value in [('category',category),('verdict',verdict),('batch',batch)]:
if value:clauses.append(key+'=?');params.append(value)
identifier=uuid.uuid4().hex;timestamp=now()
with self.connect() as db:
changed=db.execute('INSERT INTO inspection_archives SELECT id,?,? FROM inspections WHERE '+' AND '.join(clauses),[timestamp,identifier,*params]).rowcount
db.execute('INSERT INTO audit_events VALUES(?,?,?,?)',(uuid.uuid4().hex,timestamp,'archive',json.dumps({'archive_batch':identifier,'count':changed,'filters':dict(category=category,verdict=verdict,batch=batch)})))
return {'archived':changed,'archive_batch':identifier}
def restore(self,identifiers):
placeholders=','.join('?' for _ in identifiers)
with self.connect() as db:
changed=db.execute('DELETE FROM inspection_archives WHERE id IN ('+placeholders+')',identifiers).rowcount
db.execute('INSERT INTO audit_events VALUES(?,?,?,?)',(uuid.uuid4().hex,now(),'restore',json.dumps({'ids':identifiers,'count':changed})))
return {'restored':changed}
def acknowledge(self,identifier):
with self.connect() as db:
return db.execute('UPDATE alerts SET acknowledged=1 WHERE id=?',(identifier,)).rowcount>0
def add_message(self,identifier,role,payload):
with self.connect() as db:
db.execute('INSERT INTO messages(inspection_id,created_at,role,payload) VALUES(?,?,?,?)',(identifier,now(),role,json.dumps(payload)))
def add_conversation(self,identifier,question,answer):
with self.connect() as db:
db.executemany('INSERT INTO messages(inspection_id,created_at,role,payload) VALUES(?,?,?,?)',[
(identifier,now(),'user',json.dumps({'question':question})),
(identifier,now(),'assistant',json.dumps(answer))])
def messages(self,identifier):
with self.connect() as db:
rows=db.execute('SELECT * FROM (SELECT * FROM messages WHERE inspection_id=? ORDER BY id DESC LIMIT 20) ORDER BY id',(identifier,)).fetchall()
return [{'role':r['role'],'created_at':r['created_at'],**json.loads(r['payload'])} for r in rows]
def failure(self,category,error):
with self.connect() as db:
db.execute('INSERT INTO failures VALUES(?,?,?,?)',(uuid.uuid4().hex,now(),category,error))
def add_review(self,identifier,status,decision,note=''):
from .reviews import validate_review
review={**validate_review(status,decision,note),'inspection_id':identifier,
'reviewer':'Local operator (single-user workspace)','created_at':now()}
with self.connect() as db:
db.execute('INSERT INTO audit_events VALUES(?,?,?,?)',
(uuid.uuid4().hex,review['created_at'],'human_review',json.dumps(review)))
return review
def review_history(self,identifier):
with self.connect() as db:
rows=db.execute("SELECT payload FROM audit_events WHERE action='human_review' AND json_extract(payload,'$.inspection_id')=? ORDER BY created_at",(identifier,)).fetchall()
return [json.loads(row['payload']) for row in rows]
def review_notes(self,identifier):return []
|