Build-order step 1: the critical path everything else sits on. - StateStore: SQLite in WAL mode with synchronous=FULL, so every state transition and intent-log write is committed and fsynced before returning; a power cut cannot tear state - Durable intent log: begin_intent records a unit of work before it starts; commit/abort resolve it; pending intents survive restart - schema_version stamped from the first commit, with a sequential migration hook; a newer state file than the code supports is refused rather than misread - reconcile_on_boot (A3 first cut): pending intents are checked against external truth by registered reconcilers (observable checks, never model self-assessment); verified effects are committed, verified no-ops aborted, anything unverifiable gates to AWAIT_INPUT — never blind-resume - Lives in nisse_dev per spec 0.1: the library is extracted after the dev agent works, not designed up front - Integration test kills a subprocess mid-work with os._exit and verifies state integrity and intent visibility on reopen Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
119 lines
3.8 KiB
Python
119 lines
3.8 KiB
Python
import sqlite3
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from nisse_dev.state import StateSchemaError, StateStore
|
|
|
|
|
|
@pytest.fixture
|
|
def db(tmp_path: Path) -> Path:
|
|
return tmp_path / "state.db"
|
|
|
|
|
|
def test_wal_mode_and_full_sync(db: Path) -> None:
|
|
with StateStore(db) as store:
|
|
journal = store._conn.execute("PRAGMA journal_mode").fetchone()[0]
|
|
sync = store._conn.execute("PRAGMA synchronous").fetchone()[0]
|
|
assert journal == "wal"
|
|
assert sync == 2 # FULL
|
|
|
|
|
|
def test_fresh_store_stamps_schema_version(db: Path) -> None:
|
|
with StateStore(db, schema_version=1) as store:
|
|
assert store.schema_version == 1
|
|
|
|
|
|
def test_transition_persists_across_reopen(db: Path) -> None:
|
|
with StateStore(db) as store:
|
|
store.transition("EXECUTING", project="demo", data={"milestone": 2})
|
|
with StateStore(db) as store:
|
|
state = store.get_state()
|
|
assert state is not None
|
|
assert state.state == "EXECUTING"
|
|
assert state.project == "demo"
|
|
assert state.data == {"milestone": 2}
|
|
|
|
|
|
def test_get_state_on_fresh_store_is_none(db: Path) -> None:
|
|
with StateStore(db) as store:
|
|
assert store.get_state() is None
|
|
|
|
|
|
def test_intent_lifecycle(db: Path) -> None:
|
|
with StateStore(db) as store:
|
|
intent_id = store.begin_intent("push_branch", {"branch": "m2"})
|
|
[pending] = store.pending_intents()
|
|
assert pending.id == intent_id
|
|
assert pending.kind == "push_branch"
|
|
assert pending.payload == {"branch": "m2"}
|
|
store.commit_intent(intent_id)
|
|
assert store.pending_intents() == []
|
|
|
|
|
|
def test_pending_intent_survives_reopen(db: Path) -> None:
|
|
with StateStore(db) as store:
|
|
store.begin_intent("open_pr", {"title": "hello"})
|
|
with StateStore(db) as store:
|
|
[pending] = store.pending_intents()
|
|
assert pending.kind == "open_pr"
|
|
|
|
|
|
def test_aborted_intent_is_not_pending(db: Path) -> None:
|
|
with StateStore(db) as store:
|
|
intent_id = store.begin_intent("push_branch", {})
|
|
store.abort_intent(intent_id)
|
|
assert store.pending_intents() == []
|
|
|
|
|
|
def test_resolving_nonpending_intent_raises(db: Path) -> None:
|
|
with StateStore(db) as store:
|
|
intent_id = store.begin_intent("push_branch", {})
|
|
store.commit_intent(intent_id)
|
|
with pytest.raises(KeyError):
|
|
store.commit_intent(intent_id)
|
|
with pytest.raises(KeyError):
|
|
store.abort_intent(9999)
|
|
|
|
|
|
def test_migration_runs_and_bumps_version(db: Path) -> None:
|
|
with StateStore(db, schema_version=1):
|
|
pass
|
|
|
|
def add_notes_table(conn: sqlite3.Connection) -> None:
|
|
conn.execute("CREATE TABLE notes (id INTEGER PRIMARY KEY, body TEXT)")
|
|
|
|
with StateStore(db, schema_version=2, migrations={1: add_notes_table}) as store:
|
|
assert store.schema_version == 2
|
|
store._conn.execute("SELECT * FROM notes")
|
|
|
|
|
|
def test_missing_migration_raises(db: Path) -> None:
|
|
with StateStore(db, schema_version=1):
|
|
pass
|
|
with pytest.raises(StateSchemaError, match="no migration"):
|
|
StateStore(db, schema_version=2, migrations={})
|
|
|
|
|
|
def test_failed_migration_leaves_version_unchanged(db: Path) -> None:
|
|
with StateStore(db, schema_version=1):
|
|
pass
|
|
|
|
def broken(conn: sqlite3.Connection) -> None:
|
|
conn.execute("CREATE TABLE notes (id INTEGER PRIMARY KEY)")
|
|
raise RuntimeError("boom")
|
|
|
|
with pytest.raises(RuntimeError, match="boom"):
|
|
StateStore(db, schema_version=2, migrations={1: broken})
|
|
with StateStore(db, schema_version=1) as store:
|
|
assert store.schema_version == 1
|
|
with pytest.raises(sqlite3.OperationalError):
|
|
store._conn.execute("SELECT * FROM notes")
|
|
|
|
|
|
def test_newer_schema_is_refused(db: Path) -> None:
|
|
with StateStore(db, schema_version=3):
|
|
pass
|
|
with pytest.raises(StateSchemaError, match="newer"):
|
|
StateStore(db, schema_version=1)
|