Files
rust-discovery/tools/compact_helper_captures.py
T

61 lines
2.5 KiB
Python

#!/usr/bin/env python3
"""Backfill lossless storage for old helper snapshots on a development guest."""
import argparse
from contextlib import closing
from pathlib import Path
import sqlite3
def compact(db, schema, batch=32):
db.execute('PRAGMA foreign_keys=ON')
db.execute('BEGIN IMMEDIATE')
try:
if db.execute("SELECT type FROM sqlite_schema WHERE name='helper_sync_captures'").fetchone() == ('table',):
statement = ''
for line in schema.splitlines(keepends=True):
statement += line
if sqlite3.complete_statement(statement):
db.execute(statement)
statement = ''
db.commit()
except Exception:
db.rollback()
raise
cursor = -1
total = 0
while True:
db.execute('BEGIN IMMEDIATE')
try:
rows = db.execute(
"SELECT checkpoint,snapshot,fingerprint,captured_at FROM helper_capture_headers "
"WHERE checkpoint>? AND tail_id IS NULL ORDER BY checkpoint LIMIT ?", (cursor, batch)
).fetchall()
for checkpoint, raw, fingerprint, captured_at in rows:
cursor = checkpoint
if b',"attempts":' not in raw:
continue
db.execute("DELETE FROM helper_capture_headers WHERE checkpoint=?", (checkpoint,))
db.execute("INSERT INTO helper_sync_captures VALUES(?,?,?,?)", (checkpoint, raw, fingerprint, captured_at))
restored = db.execute("SELECT snapshot FROM helper_sync_captures WHERE checkpoint=?", (checkpoint,)).fetchone()[0]
if restored != raw:
raise ValueError('snapshot roundtrip failed')
total += 1
db.commit()
except Exception:
db.rollback()
raise
if len(rows) < batch:
return total
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('ledger', type=Path)
parser.add_argument('--schema', type=Path, default=Path(__file__).resolve().parents[1] / 'crates/agent-core/src/helper_capture_storage.sql')
args = parser.parse_args()
with closing(sqlite3.connect(args.ledger.resolve(strict=True).as_uri()+'?mode=rw', uri=True, timeout=30)) as db:
db.execute('PRAGMA max_page_count=262144')
rows = compact(db, args.schema.read_text())
print(f"{rows} snapshots backfilled; {db.execute('PRAGMA freelist_count').fetchone()[0]} free pages")