Skip to content

fix(db): reclaim space on delete, and stop listing skills from rewriting the registry - #801

Merged
huichen merged 2 commits into
mainfrom
fix/db-reclaim
Sep 23, 2026
Merged

huichen merged 2 commits into
mainfrom
fix/db-reclaim

Conversation

@huichen

@huichen huichen commented Sep 23, 2026

Copy link
Copy Markdown
Collaborator

The three faults

All three are in how the agent writes its SQLite database. They share one log:

DEBUG [grpc] list_installed_skills / list_available_skills   ← 8 calls in 20 ms
ERROR Session persistence command failed: database is locked
WARN  [session] could not sync agent loop model during hydration: database is locked

1. Deletes never returned space

auto_vacuum was off, so deleting a session moved its pages to the freelist and the
file never shrank. agent.db had 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 free
pages
. Nothing ever compacted it: there is no VACUUM in the agent, and the only
pruning 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_installed and catalogue_with_status both ran reconcile on every call,
which does DELETE FROM skill_installations and re-inserts every row — so a
read-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
(Composer for 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_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. 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:

test without the fix
a_session_write_waits_out_a_competing_writer (hold the write lock from a second connection) fails with database is locked, error code 5, after 42 s of busy-timeout waits → passes in 0.12 s
listing_skills_does_not_need_the_write_lock reproduces the exact field error
deleting_a_session_returns_its_pages_to_the_filesystem freelist 3290 pages, file unchanged
pruning_run_events_reclaims_their_pages same
opening_compacts_a_database_written_before_this_change 7.4 GB stays 7.4 GB
opening_compacts_a_bloated_incremental_database the ratio guard, not just the migration
5 × desktop shared skill catalog four RPCs per burst instead of two

incremental_vacuum frees one page per call (measured: 3291 calls to drain a
13 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

  • The one-time compaction needs roughly 2× the database size in free space and about
    2.4 s for the 7.4 GB case. It is non-fatal: if VACUUM fails (no space), the agent
    starts as before, logs a warning, and retries on the next start.
  • agent.db is compacted by the next agent start on this change, so an already
    running agent keeps its file until restarted.
  • Not addressed here: run_events has no retention policy, so the database will grow
    again with use. prune_run_events exists and works; wiring it to a policy is a
    separate decision.

…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.
@huichen
huichen merged commit e8e80cf into main Sep 23, 2026
12 checks passed
@huichen
huichen deleted the fix/db-reclaim branch September 23, 2026 10:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant