9 Commits

Author SHA1 Message Date
yoniebans
68bb692467 fix(storage): a browse read checks schema drift on its own connection
Every Desktop and dashboard session read opened state.db twice: an admin handle ran the declared-schema probe on a fresh connection, then the reader the read uses opened another. The read-only SessionDB browse the storage interface replaced ran that probe on its one reader connection, so each open and page read became about a third slower at the median.

open_persistence_reader gains check_drift, passed through the backend to the read-only holder the way patience_s is. With it, the holder's open runs the probe on its own connection and raises the core DeclarativeDrift with the reason StoreAdmin.schema_drift_reason gives; both now share one helper, declared_schema_drift. The browse open binds its reader with the check, and on drift runs the existing one-time catch-up and opens again, so a store that lags the declared schema is still caught up or refused by name exactly as before. A browse open's failures are reported in its 503 only and no longer reach the process's last session-store init error; main's browse wrote it only when its reader failed after the separate probe had passed.
2026-11-18 15:12:51 +01:00
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
55dc43ef57 F24: the SQLite store answers serves_same_store by holder identity
Two SQLite handles serve one store while they share one holder: the registry hands every handle on a profile the same holder for the file's current generation, and a handle opened after state.db was replaced takes a new one while the older handle keeps the generation it held, as main's registry identity check compares its objects. SQLiteStore.serves_same_store compares holders, and the assembly's Persistence forwards it between the stores the two handles are bound to. A handle opened after the file was replaced reads the replacement and does not serve the same store as the owner; red without the method (the case raises AttributeError), green with it.

Reported-by: Greptile
Reported-by: BearHuddleston
2026-11-17 17:50:49 +01:00
yoniebans
07bc10c39f fix(persistence): bind only the store's home with push_state when opening and dispatching
The store open's _home_bound and the on_session_delete dispatch in session_retention bound the store's profile home through the legacy set_hermes_home_override, which under a bound scope moves the whole scope (secrets and terminal policy with it) or refuses in a hosted one. Both now push the home alone with hermes_scope.push_state(home=...), keeping the bind condition and resetting the token afterwards, so the caller's scope stays its own.
2026-11-17 17:49:49 +01:00
yoniebans
891473d2d4 fix(persistence/sqlite): a store in a folder with no settings of its own opens without setting the folder up as a home
Binding the store's folder as the Hermes home while it opens makes the configuration read run under that folder, and reading the configuration sets the bound home up first: sessions/, cron/, logs/, memories/, skills/, SOUL.md and the rest appear in any folder a store is opened on, where main creates them only for the current home. A folder without a config.yaml has no database settings to read, so the backend now binds it only when it holds one; a profile with its own settings still opens with them. A backend case opens a store in a folder without a config.yaml and checks none of the home's folders appear.
2026-11-17 17:49:48 +01:00
yoniebans
6ca27d12f1 fix(persistence/sqlite): a store opens with its own profile's database settings
The connection helpers read database.* (synchronous, cache_size and the rest) through load_config_readonly(), so they follow the Hermes home bound when the connection opens. Main's gateway acquires a served profile's state.db at once, under that profile's bound scope, so its writer takes that profile's settings. A store handle here opens its file on first use, and a store is named by its profile, so a handle opened under the served profile's scope but first used after the scope was reset, or a profile's store named from another profile, opened with the settings of whichever profile the caller ran as: a caller with synchronous OFF could open another profile's writer with OFF.

The backend now binds the store's own home while it acquires the holder, so the writer, the read-only holder's connection and the schema setup at open read that profile's configuration, as main's open under the served profile's scope does. Read connections the holder opens later from another thread take the settings of the home bound on that thread, as main's pooled readers do. A backend case writes different database settings for the caller's home and the store's profile and checks that the writer and a read-only holder open with the profile's.
2026-11-17 17:49:48 +01:00
yoniebans
99539fe12f fix(persistence/sqlite): a hosted-room repository refuses both placements once its backend closes
A repository taken from a handle and kept after the backend's last handle closed refused on placement='profile', through the store holder, as the persistence handle documents. On placement='shared' it still opened shared-state.db and wrote and read rooms, and probe_hosted_room still opened the profile's state.db directly. Every hosted-room verb names its file through HostedFiles.path, which now checks first that the store's backend is open and raises the same RuntimeError a closed backend raises elsewhere, without opening any file. Main's hosted-room functions take a path and have no handle to close, so no caller of main's code relies on a write after close. A placement case keeps a room repository past the backend's close and checks that a write, a read and a probe refuse on both placements and that neither file is created.
2026-11-17 17:49:48 +01:00
yoniebans
855591dc56 fix(persistence/sqlite): a handle opened after its file is replaced uses the new file
Every handle on a profile shares one backend, and the backend kept the holder it first acquired for as long as any handle stayed open. Once state.db was replaced (a snapshot restore, a recovery swap), a handle opened afterwards was given that fenced holder: its writes raised StoreReplaced and its reads answered the old file until every handle had closed. On main a new acquire after the replace gets the registry's new generation, and only the instance opened before it stays fenced.

Each store handle now keeps the holder it first used. A handle without one asks the registry for the current generation; when that is a new one, the backend serves it to new handles and keeps the superseded holder for the handles that opened it, so they stay fenced, and releases both at close. A sweep and the read-only backend behave as before. A backend case replaces the file under an open handle and checks that a new handle writes the replacement, the old handle's write raises StoreReplaced, and close releases every generation.
2026-11-17 17:49:48 +01:00
yoniebans
7b70fdf001 feat(persistence/sqlite): the backend, its services and the assembly door
SQLiteBackend assembles the repositories over a holder; services.py answers without a healthy store (the corruption latch and the readiness probe); persistence_assembly.py is the one door application code opens persistence through. Nothing in production constructs the backend yet.
2026-11-17 17:49:46 +01:00