"""Record the store a reference daemon writes, for compatibility tests. Runs two turns on the reference build that test/reference-daemon.sh pins: one that calls Python and one that answers, each reporting usage. It then changes AGENTS.md and reloads, so the session pins the prompt its turns used, and leaves three inputs waiting in a second session's inbox (the first carrying an image) by refusing their transcript rows while they are submitted. Then it stops the daemon and writes a compacted copy of its store, without that refusal, to apps/albedo/test/fixtures/reference-store/albedo.sqlite. Tests read that copy as a store an older release left behind, so current code is never what wrote it. Re-record only when the reference pin moves, and say why in the commit. Run from the repository root inside `nix develop`: `python3 test/manual/record_reference_store.py`. """ import base64 import json import shutil import sqlite3 import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[2] OUT = ROOT / "apps/albedo/test/fixtures/reference-store/albedo.sqlite" def main() -> None: sys.path.insert(0, str(ROOT / "e2e")) from harness import ( Albedo, Daemon, Provider, Reply, latest_user, on_daemon, operation_id, text, ) from image_limits_test import png from reference_support import run_reference daemon = Daemon("reference-store") daemon.env["ALBEDO_STANDBY"] = "0" run_reference(daemon) usage = { "first question": {"input_tokens": 100, "output_tokens": 20}, "second question": {"input_tokens": 300, "output_tokens": 40}, } def reply(request): items = request.get("input", []) asked = latest_user(request) if items and items[-1].get("type") == "function_call_output": return text("ran it", usage=usage["first question"]) if asked == "first question": return Reply("python", "print(1)", usage=usage["first question"]) return text("answered", usage=usage.get(asked)) provider = Provider(reply) daemon.boot() try: with on_daemon(daemon), Albedo(provider, protocol="responses") as app: agents = app.workspace / "AGENTS.md" agents.write_text("FIRST_VERSION\n") session = app.session() for prompt in ("first question", "second question"): app.prompt(session, prompt).close() app.idle(session) # The session's pinned prompt, beside the known one its turns saved. agents.write_text("SECOND_VERSION\n") app.api(f"/sessions/{session}/reload", {"target": "session"}).close() waiting = app.session() database = app.home / "albedo.sqlite" with sqlite3.connect(database) as db: db.execute( "CREATE TRIGGER hold_inputs BEFORE INSERT ON transcript" f" WHEN NEW.row_class='user' AND NEW.session='{waiting}'" " BEGIN SELECT RAISE(ABORT,'held for the fixture'); END" ) # Operation ids sort opposite to the order the inputs arrive in. inputs = sorted((operation_id() for _ in range(3)), reverse=True) image = base64.b64encode(png(2, 2)).decode() for input_id, intent in zip( inputs, ( { "kind": "message", "text": "waited in the old table", "images": [{"mime_type": "image/png", "data": image}], }, {"kind": "message", "text": "second old input"}, {"kind": "message", "text": "third old input"}, ), ): path = f"/sessions/{waiting}/inputs/{input_id}" with app.api(path, intent, method="PUT") as response: assert json.load(response)["delivery"] == "pending" def copy(_app): OUT.parent.mkdir(parents=True, exist_ok=True) OUT.unlink(missing_ok=True) with sqlite3.connect(app.home / "albedo.sqlite") as db: db.execute("VACUUM INTO ?", (str(OUT),)) # One small page per table keeps the committed copy small. copied = sqlite3.connect(OUT) copied.execute("DROP TRIGGER hold_inputs") copied.execute("PRAGMA journal_mode=delete") copied.execute("PRAGMA page_size=512") copied.execute("VACUUM") copied.close() app.restart(prepare=copy) finally: provider.close() daemon.shutdown() daemon.discard() shutil.rmtree(OUT.parent / "__pycache__", ignore_errors=True) print( f"{OUT} ({OUT.stat().st_size} bytes), session {session}," f" waiting session {waiting}, inputs {inputs}" ) if __name__ == "__main__": main()