"""The database recorder and the configured checkpointer.""" import sqlite3 import pytest from langgraph.checkpoint.memory import InMemorySaver from langgraph.checkpoint.sqlite.aio import AsyncSqliteSaver from execution_engine.checkpointing import open_checkpointer from execution_engine.config import get_settings from execution_engine.database import DatabaseManager from execution_engine.recorder import DatabaseRecorder from execution_engine.states.ledger import LedgerEvent @pytest.fixture def sqlite_db(tmp_path, monkeypatch): path = tmp_path / "records.db" monkeypatch.setattr(get_settings(), "SQLITE_FALLBACK_DB", str(path)) return DatabaseManager(db_url="sqlite:///unused"), path async def test_database_recorder_writes_events_submissions_and_status(sqlite_db): manager, path = sqlite_db recorder = DatabaseRecorder(manager) await recorder.record_events( "exec-1", [ LedgerEvent( 1, "submitted", "requirements", "requirements-analyst", "attempt 1" ) ], ) await recorder.record_submission( "exec-1", "deployment_guidance", "# Guide", {"terraform/main.tf": "resource {}"} ) await recorder.record_status( "exec-1", session_id="session-1", workflow_request="Ingestion", status="awaiting_user", current_phase="solution_architecture", details={"revisions": 2}, ) with sqlite3.connect(path) as conn: logs = conn.execute( "SELECT iteration, review_status, reviewer_agent, feedback " "FROM orchestrator_review_logs WHERE execution_id = 'exec-1'" ).fetchall() artifacts = conn.execute( "SELECT artifact_type, file_path FROM adk_artifacts WHERE execution_id = 'exec-1' " "ORDER BY file_path" ).fetchall() execution = conn.execute( "SELECT status, current_phase, loop_count FROM workflow_executions " "WHERE execution_id = 'exec-1'" ).fetchone() session = conn.execute( "SELECT agent_name, state_data FROM adk_sessions WHERE session_id = 'session-1'" ).fetchone() assert logs == [(1, "submitted", "requirements-analyst", "requirements: attempt 1")] assert artifacts == [ ("deployment_guidance", "deliverables/deployment_guidance.md"), ("deployment_guidance", "terraform/main.tf"), ] assert execution == ("awaiting_user", "solution_architecture", 2) assert session[0] == "solution-architect" assert "exec-1" in session[1] async def test_checkpointer_follows_settings(tmp_path, monkeypatch): settings = get_settings() monkeypatch.setattr(settings, "CHECKPOINTER", "memory") async with open_checkpointer(settings) as saver: assert isinstance(saver, InMemorySaver) monkeypatch.setattr(settings, "CHECKPOINTER", "sqlite") monkeypatch.setattr(settings, "CHECKPOINT_DB_PATH", str(tmp_path / "cp.db")) async with open_checkpointer(settings) as saver: assert isinstance(saver, AsyncSqliteSaver) monkeypatch.setattr(settings, "CHECKPOINTER", "redis") with pytest.raises(ValueError, match="redis"): async with open_checkpointer(settings): pass