15 Commits

Author SHA1 Message Date
yoniebans
be3d6affc8 refactor(state)!: remove the old session store
hermes_state.py becomes the stand-in plugins import, the rest of the legacy store and its test folder go, and the per-process modules the backend imported from main move under persistence/sqlite/_state/. The backend checks PID namespaces and lease holders through process_identity and its WAL setup through sqlite_support, and its error module takes the shared predicates and classification from storage_errors, so neither helper set has a second copy. The backend, sqlite_support.py and the tests that pin those modules reach their final imports, and the backend-import guard allows no file outside persistence/. The modules no caller imports any more go with it: the agent's onboarding rules (now in session_rules) and gateway/session_admission.py.

The stand-in's SessionDB releases its store on close() and, used again afterwards, opens it again with a warning, as the old class reopened its connection, unless the file at its path was replaced or the store is latched corrupt; a read-only one raises. A close during a call releases the store when the call returns. A named source wins over a source list in its session listing and count.

An agent given a writable stand-in SessionDB as session_db= and no persistence= takes the store handle that SessionDB forwards to as its persistence, so its turn flush, token counts, compression and session row writes go through the storage interface, as an agent given the old SessionDB wrote through it.

The backend-import guard also holds the backend's remaining reads of the process or task home, each with its reason, and refuses any other and any import of the stand-in inside persistence/sqlite/.

Plugin context engines get the agent's store as persistence= beside the deprecated session_db=, in each hook whose signature accepts that keyword, including the rebind after /new, /resume and /branch, and the context engine guide and the plugin guide's replacement table name it. A test fails once the running release reaches the stand-in's removal release, listing what goes with the stand-in.

release_unheld_media and collect_unheld_api_images take a store only: their SessionDB branches, which no caller reaches, go with the gateway's own copies of the admission and transcript queries.
2026-11-17 19:10:57 +01:00
yoniebans
da53c34592 test(gateway): move the newer authority tests onto the persistence interface
Ten tests written on main after the old base still built the authority on a SessionDB and read admissions through authority.db, SessionAuthority(db=), SessionStore._db and _admit_native_write(authorize=). They now build it with persistence= (storage_handle or open_persistence) and read through the interface the gateway uses: admissions.list/claim/settle/admit, runtime.begin_epoch, sessions.get, bindings.receipt, admission_row, AuthorityRefusal and the vocabulary's ADMITTED_REQUEST_CONFLICT. Setup only: every assertion keeps its subject and expected value.

The resumable_subagents schema test pins main's table statement as a literal instead of reading it from the legacy schema module.
2026-11-17 17:51:23 +01:00
yoniebans
dd869572db test(persistence): ledger reconciles state.db's schema once per file
The delegation ledger reaches state.db through the process-shared store, whose schema reconcile runs when the profile's StoreHolder opens. While the process holds the store, as the TUI gateway does for its lifetime, repeated orphan sweeps, reads and writes borrow that one holder and do not reconcile again, and each profile's file is reconciled on its own. The legacy test of the tool's own per-connection schema cache is retired: that cache no longer exists.
2026-11-17 17:51:23 +01:00
yoniebans
ebcc56e80a test(persistence/sqlite): backend and contract cases that drive the switched callers
These cases exercise the backend through callers that now use the interface, so they land after them. The legacy tests/hermes_state copies of the same cases go with them.
2026-11-17 17:51:21 +01:00
yoniebans
8e00ecfadc clean: the lease and ledger-setup cases describe the rule they pin
The two test modules added for the lease holder and the ledger setup described the code before the fix and the other implementation's writes. Their docstrings now state the rule each case pins. Docstrings only; the syntax trees with docstrings blanked are identical.
2026-11-17 17:49:49 +01:00
yoniebans
3f76db78f6 F22: a ledger's table setup waits for its locks no longer than the ledgers' lock wait
_ledger_setup took the process-wide setup lock and the holder's writer lock with bare with blocks, and its deadline covered only the retry loop: a long write elsewhere held the setup, and every ledger verb behind it, with no bound. Both locks are now taken against that deadline, the writer lock through writer_mutex_until as every write takes it, so a held lock refuses the setup with StoreContention, which the existing retry loop and callers already handle. A case holds the writer lock from another thread and asserts the setup refuses within the wait and runs nothing; it hung before the fix.

Reported-by: Greptile
Reported-by: unsupportedpastels
2026-11-17 17:49:49 +01:00
yoniebans
5f9465f2ec F21: the turn-lease verbs resolve the store holder once, before the write
The four turn-lease verbs called self._store.holder() inside their write transaction to find the conversation key. After a profile sweep (close_all_under) cleared the holder's registry ownership, that call reacquired from the registry, which waits on the sweep's teardown, which waits on the write lock the transaction holds: a deadlock. Main's lease writes resolve nothing inside the transaction. Each verb now resolves the holder once before the write and the closure uses that object, so a sweep during the write completes after it. A case starts a profile sweep from inside the write of try_acquire, refresh and release, and asserts both finish; it hung before the fix (stopped by its recorded pid after 150 s) and passes after. The only other in-closure holder calls in the backend are the topic reads' lambdas in gateway/routing.py, which run outside any transaction.

Reported-by: Greptile
Reported-by: unsupportedpastels
2026-11-17 17:49:49 +01:00
yoniebans
0ad4d10151 refactor(persistence/sqlite): filter live delegations by state='running' only
The SQLite ledger matches the contract: every in-flight filter on `async_delegations` (prune counts and deletes, the orphan sweep and its reads, the finished read, the in-flight read) and the worker delivery claim test `state='running'` or `state!='running'`, since no writer stores `finalizing`. The abandoned-delegation recovery parity case now expects the one running row recovered.
2026-11-17 17:49:49 +01:00
yoniebans
775eedb68c fix(persistence): the SQLite begin_epoch refuses a bump while another process owns the profile
The backend's begin_runtime_epoch now runs main's owner guard before the bump: while another process holds the profile's gateway.lock, the bump raises AuthorityRefusal('ownership_conflict', field='runtime_epoch') and the owner's epoch stays where it was. A reservation this process holds grants the bump, the flock decides rather than the lock record, and a store with no directory of its own never refuses.

Main's ownership cases run against the backend's runtime.begin_epoch.
2026-11-17 17:49:49 +01:00
yoniebans
c44e6bb31b feat(persistence): latest_admission on SQLite, newest first per status
The session's newest admission carrying a top-level payload marker: one SELECT per status in the table's CHECK order, each walking the (target, status, seq) index newest first, merged by seq with heapq.merge and stopped at the first row whose decoded payload holds the marker. Every cursor is closed before the read connection goes back.

A SQLite-only test counts the rows read from the cursors: an early match reads two rows of a thirty-two row history, a miss reads all of them, and no cursor is left open at the end of the read context.
2026-11-17 17:49:48 +01:00
yoniebans
fbf4aeaf98 test(persistence/sqlite): spell the schema-statement match with re.DOTALL
The repository's ruff rule FURB167 refuses the re.S alias in the resumable_subagents schema case.
2026-11-17 17:49:48 +01:00
yoniebans
382cd6d02a feat(persistence/sqlite): durable subagent records on the delegation ledger
The SubagentLedger verbs on the SQLite coordination repository, in a new persistence/sqlite/authority/subagents.py mixed into SQLiteCoordinationRepository. Each verb carries main's statements from tools/delegate_tool_resumable.py and tools/delegate_tool_auto_resume.py with their binding order: the upsert and the limits pass, the checkpoint, the gateway-shutdown mark, the continue claim and hand-back, the note, the expired and manual-claim cancels, the automatic claim, start, confirm and refusal settlement, the give-up with its failed completion row, the supersession of a session's children and the dismissal. Policy and owner identity arrive as parameters.

A write computed from a record the caller read (a run record, a dismissal, an automatic run's start) compares the stored row with that record inside its own write and answers a lost write with nothing written. The parent-supersession check walks the compression chain on the verb's connection. A give-up whose unit row changed since the caller built its completion rolls back and answers route_moved.

recover_abandoned_delegations takes the subagent recoveries and limits and writes them first in its own transaction, each only while the record is still running or claimed by its dead owner.

A verb for a profile whose store was removed answers as a write that did nothing and creates nothing, since the store's opener refuses a missing home. The worktree sweep's pinned read and the owed probe read through their own read-only connection, as main does, and never create or write the store.
2026-11-17 17:49:48 +01:00
yoniebans
7acb807daf feat(persistence-sqlite): resumable_subagents table in the store schema
The SQLite schema carries main's resumable_subagents table (the durable subagent records of the delegation ledger), created with the rest of SCHEMA_SQL on every writable open, so a store opened before the table existed gains it.
2026-11-17 17:49:48 +01:00
yoniebans
ffed216232 fix(persistence/sqlite): pin the float spelling and chat id refusals in the SQLite backend's own tests
The shared contract asks every backend only for neutral properties of float content and unbindable chat ids, so the exact behaviour main has, and the SQLite backend keeps, is pinned here instead. Float content reads back as the text a SQLite TEXT column stores for it, for the eight values whose spelling differs between SQLite builds. A chat id outside SQLite's 64-bit range raises the driver's own OverflowError ("Python int too large to convert to SQLite INTEGER") with no cause; a list or dict raises PermanentStoreError chained to the driver's ProgrammingError ("Error binding parameter 6: ..."). Each refusal leaves no session row and no routing entry.

The removed case compared the reference backend's float spelling with the installed SQLite, which the reference now asks for that spelling, so it compared the library with itself and tested no backend.
2026-11-17 17:49:47 +01:00
yoniebans
f2fd4a7559 test(persistence/sqlite): authority and coordination cases
The SQLite backend's session authority and coordination cases.
2026-11-17 17:49:47 +01:00