Repository navigation
fix(agent): take the write lock up front for read-then-write session writes - #890
Merged
Merged
Conversation
…writes
Every live session owns its own SQLite connection to the same agent.db, so the
three inline transactions in `Manager` -- the run-boundary append, the metadata
update, and fork materialization -- read before they write on a connection that
another connection can commit against. In WAL mode such a deferred
read-to-write upgrade fails *immediately* with SQLITE_BUSY_SNAPSHOT
("database is locked"), bypassing both `busy_timeout` and the retry ladder that
`database::begin_immediate` exists to provide. That is the hazard already fixed
for the `SqliteStore` writes, still open on these paths.
A field session hit it at startup: the hydration metadata sync
(`set_model` -> `update_info`) recorded "database is locked" in the session's
`last_error` while its caller only logs that failure, so every later prompt was
rejected with "session persistence is unavailable: database is locked" for
hours -- and with no active run, the manual recovery path had nothing to key
off -- until the agent restarted.
Switch those transactions, and the legacy importer with the same
read-then-write shape, to `database::begin_immediate`. The new test holds the
write lock from a second connection and fails with "database is locked" without
the fix.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Symptom
A workspace session (
agent.db 体积膨胀排查) rejected every prompt withsession persistence is unavailable: database is locked— for 1h45m after the agent's last restart, while its three most recent runs each failed in ~350 ms (i.e. the error was already recorded, not re-detected per attempt). Restarting the agent cleared it.Root cause
Every live session owns its own
Manager→Database→ SQLite connection on the sameagent.db, so a different connection can commit while one session's transaction is open. ThreeManagertransactions read before they write and were opened withdb.transaction()(BEGIN DEFERRED):append_run_start— reads the existing run markers, then appends the boundaryupdate_session_info_fields— reads the current metadata, then rewrites itcreate_fork— reads the parent's entries, then inserts the childIn WAL mode, when another connection commits between the read and the first write, SQLite fails the read→write upgrade immediately with
SQLITE_BUSY_SNAPSHOT— whose message is exactlydatabase is locked— and neitherbusy_timeoutnor the retry ladder applies.database::begin_immediateexists precisely for this (its doc comment records the same failure seen when the skill registry wrote the file), but it was only wired into theSqliteStorewrites;legacy_import's destination check has the same shape.Who recorded it: the session's startup metadata sync. The agent started at 15:41:54, hydrated the thread, and its
set_model→update_info("model")ran at 15:41:56.038 (the exactsessions.updated_at_msof the session row). That hydration path swallows the error (let _ = self.set_model(..).inspect_err(..), warning only) — butSessionPersistence::record_resulthad already stored the message inlast_error, and the prompt guard reads that first.Why it never recovered: with no active run,
recover_persistence_degradedbails withsession has no active run, so the manual recovery path had nothing to key off and the only way out was an agent restart.Changes
Those four transactions now go through
database::begin_immediate: the write lock is taken atBEGIN, so a competing commit makes us wait (5 sbusy_timeout+ the retry ladder) instead of losing the upgrade instantly.Test
metadata_update_survives_a_concurrent_commit_on_another_connectionholdsBEGIN IMMEDIATEon a second connection to the same file and commits ~200 ms later. Without the fix it fails with exactlyError code 5: database is locked; with it, the update waits for the lock and lands.Checks
cargo fmt -p future-agent --check,cargo clippy -p future-agent --all-targets -- -D warnings,cargo test -p future-agent.Not in this PR
A metadata write that fails for any other reason still poisons prompting until restart when no run is active (the swallowed-error path above). Worth a separate look — this PR removes the trigger that hit the field, not the poison mechanism.