Add durable state store and boot-time reconciliation (A2/A3)
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>
This commit is contained in:
1 parent
5962aaee57
commit
4319882f35
5 files changed
+516
No files matched your search
@@ -0,0 +1,68 @@
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from nisse_dev.reconcile import reconcile_on_boot
|
||||
from nisse_dev.state import Intent, StateStore
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def store(tmp_path: Path) -> StateStore:
|
||||
return StateStore(tmp_path / "state.db")
|
||||
|
||||
|
||||
def test_no_pending_intents_resumes(store: StateStore) -> None:
|
||||
report = reconcile_on_boot(store, {})
|
||||
assert report.action == "resume"
|
||||
assert report.notes == []
|
||||
|
||||
|
||||
def test_verified_effect_commits_intent_and_resumes(store: StateStore) -> None:
|
||||
store.begin_intent("push_branch", {"branch": "m2"})
|
||||
report = reconcile_on_boot(store, {"push_branch": lambda intent: True})
|
||||
assert report.action == "resume"
|
||||
assert store.pending_intents() == []
|
||||
|
||||
|
||||
def test_verified_no_effect_aborts_intent_and_resumes(store: StateStore) -> None:
|
||||
store.begin_intent("push_branch", {"branch": "m2"})
|
||||
report = reconcile_on_boot(store, {"push_branch": lambda intent: False})
|
||||
assert report.action == "resume"
|
||||
assert store.pending_intents() == []
|
||||
|
||||
|
||||
def test_unverifiable_intent_gates_to_await_input(store: StateStore) -> None:
|
||||
store.begin_intent("push_branch", {"branch": "m2"})
|
||||
report = reconcile_on_boot(store, {"push_branch": lambda intent: None})
|
||||
assert report.action == "await_input"
|
||||
assert [i.kind for i in store.pending_intents()] == ["push_branch"]
|
||||
|
||||
|
||||
def test_unknown_intent_kind_gates_to_await_input(store: StateStore) -> None:
|
||||
store.begin_intent("mystery", {})
|
||||
report = reconcile_on_boot(store, {})
|
||||
assert report.action == "await_input"
|
||||
assert "no reconciler registered" in report.notes[0]
|
||||
|
||||
|
||||
def test_reconciler_receives_the_intent(store: StateStore) -> None:
|
||||
store.begin_intent("push_branch", {"branch": "m2"})
|
||||
seen: list[Intent] = []
|
||||
|
||||
def check(intent: Intent) -> bool:
|
||||
seen.append(intent)
|
||||
return True
|
||||
|
||||
reconcile_on_boot(store, {"push_branch": check})
|
||||
assert seen[0].payload == {"branch": "m2"}
|
||||
|
||||
|
||||
def test_mixed_verdicts_still_gate(store: StateStore) -> None:
|
||||
store.begin_intent("push_branch", {})
|
||||
store.begin_intent("open_pr", {})
|
||||
report = reconcile_on_boot(
|
||||
store,
|
||||
{"push_branch": lambda intent: True, "open_pr": lambda intent: None},
|
||||
)
|
||||
assert report.action == "await_input"
|
||||
assert [i.kind for i in store.pending_intents()] == ["open_pr"]
|
||||
@@ -0,0 +1,118 @@
|
||||
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)
|
||||
Reference in new issue
Block a user