informed-decision/docs/evidence/2026-10-11-purpose-image-release/sqlite_readonly.py
tegwick e0359bcb6c Prepare purpose image release with bounded custody protocol and owner reviews
Assistant: codex
Assistant-Model: gpt-6.1-sol
Assistant-Session: 01a126e1-76f8-7643-8cf3-e93336539ade
2026-10-11 03:54:35 +02:00

39 lines
1.9 KiB
Python

"""Pre-open schema/integrity/count proof; stdlib only, never constructs Store."""
import argparse
import json
from pathlib import Path
import sqlite3
def inspect_readonly(path):
path = Path(path)
if not path.is_file() or path.is_symlink():
raise ValueError('existing nonsymlink private database required')
with sqlite3.connect(path.resolve().as_uri() + '?mode=ro', uri=True, timeout=5) as db:
db.execute('PRAGMA query_only=ON')
db.execute('BEGIN')
version = db.execute('PRAGMA user_version').fetchone()[0]
if version != 5:
raise ValueError('pre-open database is not schema5; refuse migration')
if db.execute('PRAGMA integrity_check').fetchall() != [('ok',)]:
raise ValueError('read-only integrity check refused')
lifecycle = {name: db.execute('SELECT COUNT(*) FROM ' + table).fetchone()[0]
for name, table in (('records', 'lifecycle_records'),
('events', 'lifecycle_events'),
('pending', 'lifecycle_pending'))}
result = {'schema_version': version, 'lifecycle': lifecycle,
'outbox': dict(db.execute('SELECT state,COUNT(*) FROM outbox GROUP BY state')),
'submissions': dict(db.execute('SELECT state,COUNT(*) FROM submissions GROUP BY state')),
'integrity': 'ok', 'read_only_pre_open': True, 'store_constructed': False}
db.rollback()
return result
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--db', type=Path, required=True)
args = parser.parse_args()
try:
print(json.dumps(inspect_readonly(args.db), sort_keys=True))
except Exception:
raise SystemExit('read-only schema5/integrity/count gate refused; no Store/admin open') from None