Repository navigation
fix(db): reclaim space on delete, and stop listing skills from rewriting the registry - #801
Merged
Merged
Conversation
…ing the registry Three faults behind the reported symptoms, all in how the agent's SQLite database is written. **Deletes never returned space.** `auto_vacuum` was off, so deleting a session moved its pages to the freelist and the file never shrank. It had reached 7.4 GB holding 1.5 GB of live rows — 6.2 M `run_events` in a table plus index of 1.5 GB, and 79% of the file free. New databases enable incremental auto-vacuum, an existing one is compacted once at open, and every delete now runs a bounded incremental drain. **"List the skills" was a writer.** `list_installed` and `catalogue_with_status` both ran `reconcile` on every call, which deletes and rewrites `skill_installations` — so a read-only RPC took the write lock of the same database the session store writes to. The log shows the collision exactly: a burst of `list_installed_skills` / `list_available_skills` from the desktop, then `Session persistence command failed: database is locked`. Listing is now a pure read; reconcile runs once per database (to adopt skill directories with no registry row, the v3 → v4 upgrade) and on every mutation, where its cost is invisible next to the install itself. **Read-modify-write transactions lost the lock race.** A deferred transaction that reads before it writes gets `SQLITE_BUSY_SNAPSHOT` **immediately** when another connection commits in between — `busy_timeout` does not apply, because waiting would break the snapshot those reads came from (measured: 0.00 s with a 5 s timeout). `replace` reads `legacy_imports` before writing, so session persistence failed instantly while the skill registry committed. Write transactions now take the lock at `BEGIN`, with a bounded retry; the retry is around the acquisition rather than the body so large payloads are not cloned per attempt. Finally, the desktop read the two skill lists four times per message — twice as the composer mounted, twice from the recommender — and each read is one of the RPCs above. They now share a cache that is cleared on every mutation, so a message costs one read. Tests pin each fault: the lock race is reproduced by holding the write lock from a second connection (fails in 42 s without the fix, passes in 0.12 s with it), the reclamation by deleting 24 000 events and asserting the file shrinks, the compaction by reopening a database that was left bloated, and the read-only property by listing while the write lock is held (that test reproduces the exact "database is locked" from the field). Plus one desktop test per cache behaviour, including that four concurrent readers cost two RPCs and that an install invalidates rather than serving a stale list. Verified: agent 1868, tui 2049, cli, desktop 1245 (144 files), fmt/clippy/lint clean.
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.
The three faults
All three are in how the agent writes its SQLite database. They share one log:
1. Deletes never returned space
auto_vacuumwas off, so deleting a session moved its pages to the freelist and thefile never shrank.
agent.dbhad reached 7.4 GB holding 1.5 GB of live rows —6.2 M
run_events(1.1 GB table + 0.3 GB index), and 79% of the file was freepages. Nothing ever compacted it: there is no
VACUUMin the agent, and the onlypruning RPC (
prune_run_events) has no caller in any client.Now: a new database enables incremental auto-vacuum, an existing one is compacted
once at open (7.4 GB → 1.5 GB, measured at 2.4 s), and every delete runs a
bounded incremental drain.
2. "List the skills" was a writer
list_installedandcatalogue_with_statusboth ranreconcileon every call,which does
DELETE FROM skill_installationsand re-inserts every row — so aread-only RPC took the write lock of the same database the session store writes to.
That is the collision above: the desktop asks for both lists as it mounts
(
Composerfor mention completion, the recommendation hook for its candidate set),and session persistence failed while those committed.
Listing is now a pure read. Reconcile still runs once per database — needed to adopt
skill directories with no registry row, i.e. the v3 → v4 upgrade and a directory
placed by hand — and on every mutation, where its cost is invisible next to the
install it accompanies.
3. Read-modify-write transactions lost the lock race
A deferred transaction that reads before it writes gets
SQLITE_BUSY_SNAPSHOTimmediately when another connection commits in between —
busy_timeoutdoes notapply, because waiting would break the snapshot those reads came from (measured:
0.00 s with a 5 s timeout).
replacereadslegacy_importsbefore writing, sosession persistence failed instantly while the skill registry committed. Writes now
take the lock at
BEGIN, with a bounded retry. The retry wraps the acquisition,not the body, so a history rewrite is not cloned once per attempt.
4. The desktop read the lists four times per message
Two as the composer mounted, two from the recommender — each one an RPC from #2.
They now share a cache that every mutation clears, so a message costs one read.
Clearing happens on settle, so a read that starts mid-install cannot be cached and
outlive it.
Tests
Each fault is pinned by a test that fails without the fix:
a_session_write_waits_out_a_competing_writer(hold the write lock from a second connection)database is locked, error code 5, after 42 s of busy-timeout waits → passes in 0.12 slisting_skills_does_not_need_the_write_lockdeleting_a_session_returns_its_pages_to_the_filesystempruning_run_events_reclaims_their_pagesopening_compacts_a_database_written_before_this_changeopening_compacts_a_bloated_incremental_databaseshared skill catalogincremental_vacuumfrees one page per call (measured: 3291 calls to drain a13 MB freelist, 298 ms), so the drain loops with a page budget instead of
pretending one call is enough — a 6 GB freelist would otherwise take minutes, which
is why the open-time compaction exists as a second path.
Verified: agent 1868, tui 2049, cli, desktop 1245 / 144 files, plus
fmt, clippy and eslint/tsc clean.
Notes
2.4 s for the 7.4 GB case. It is non-fatal: if
VACUUMfails (no space), the agentstarts as before, logs a warning, and retries on the next start.
agent.dbis compacted by the next agent start on this change, so an alreadyrunning agent keeps its file until restarted.
run_eventshas no retention policy, so the database will growagain with use.
prune_run_eventsexists and works; wiring it to a policy is aseparate decision.