Repository navigation
fix(db): version the tracking and recall schema migrations - #4894
Conversation
📊 Automated PR Analysis
SummaryFixes SQLite schema migration bugs in rtk's tracking and recall databases, where a stale user_version could skip migrations or merge conflicts could silently drop one. Introduces a shared core::db module with explicit version enums, ordered per-version migration steps, application_id file tagging to prevent cross-database path mixups, and propagates previously-swallowed migration errors. Also updates rtk init/gain commands to handle a failing or unusable recall store gracefully instead of hard-… Review Checklist
Analyzed automatically by wshm · This is an automated analysis, not a human review. |
aeppling
left a comment
There was a problem hiding this comment.
Two regressions against develop, both in src/core/db.rs.
1. Tracking DB directory not writable. develop chmodded the parent before opening; the PR reads the identity first, and on a WAL file that read has to create -shm.
D=$(mktemp -d); export RTK_DB_PATH=$D/history.db
rtk git status
chmod 0555 $D
rtk gain
# develop: works, directory repaired to 0700
# PR: Failed to read the tracking DB version: … SQLITE_READONLY_DIRECTORY; `rtk init` fails the same waySame with no DB yet (mkdir -m 0555): develop creates it, the PR fails in pre_create.
2. Read-only tracking DB behind a symlink. SQLite creates -wal/-shm next to the resolved target; restrict_sidecars chmods <link>-wal/<link>-shm, which never exist, so the reopened connection stays read-only.
D=$(mktemp -d); export RTK_DB_PATH=$D/history.db.real
rtk git status; sqlite3 $D/history.db.real 'PRAGMA user_version = 2'
chmod 0444 $D/history.db.real; ln -s history.db.real $D/history.db
RTK_DB_PATH=$D/history.db rtk gain
# develop: works
# PR: Failed to start migration InitialRecheck of the tracking DB: attempt to write a readonly database
# ls -la $D: history.db.real-shm is still 0444; chmod u+w on it makes the PR succeedOn a DB already at stamp 3 the symptom is silent: inserts fail where develop recorded.
f8f8180 to
d3a5497
Compare
|
Thanks, both reproduced and fixed. Reading the file before repairing anything was the cause of the first one, and it went further than your repro: a DB at To look for others of the same kind, I ran develop's binary and this one on 450 tracking states (directory mode, file mode, journal mode or leftover sidecars, symlink) and 189 recall states. Nothing develop handles fails now. One difference stays on this PR alone, with its fix in #4901: a tracking DB or recall store that is really unwritable (read-only mount, another user's file), behind on its stamp and not in WAL mode, still fails to open here because a step has to write. #4901 opens it as it is, for both; I checked that on a read-only bind mount. In WAL mode it fails on develop too. |
aeppling
left a comment
There was a problem hiding this comment.
Both fixed on d3a5497b. Two cases still fail here and work on develop; both were already there at f8f81805.
1. Unwritable DB behind on its stamp. chflags uchg stands in for a read-only mount or another user's file.
D=$(mktemp -d); export RTK_DB_PATH=$D/history.db
rtk git status; sqlite3 $D/history.db 'PRAGMA user_version = 2'
chmod 0444 $D/history.db; chflags uchg $D/history.db
rtk gain
# develop: works
# PR: Migration InitialRecheck of the tracking DB failed: Failed to backfill commands.project_path: attempt to write a readonly database2. DB reached through a symlink into an unwritable directory.
D=$(mktemp -d); mkdir $D/store $D/data
RTK_DB_PATH=$D/store/history.db rtk git status
sqlite3 $D/store/history.db 'PRAGMA user_version = 2; PRAGMA journal_mode = DELETE'
ln -s ../store/history.db $D/data/history.db; chmod 0555 $D/store
RTK_DB_PATH=$D/data/history.db rtk gain
# develop: works
# PR: Failed to stamp the tracking DB at InitialRecheck: attempt to write a readonly database: SQLITE_READONLY_DIRECTORYSame cause: a stamp-2 DB has to take the InitialRecheck write before it opens, and a failed step fails the open. Suggestion: a failed step rolls back, keeps the stamp, is reported by gain/init, and the DB is used as it is, as on develop. Refusing a foreign file stays fatal. That covers both without needing #4901.
d3a5497 to
5c66e2a
Compare
|
Thanks, both reproduced on Linux (a read-only bind mount in place of It also covers a case the read-only handling in #4901 did not: a stamp-2 database whose write lock another process holds failed after the 5 s busy timeout, and now works after it. Two more changes came with it:
The develop comparison now includes your symlink into another directory: 12 of those states failed at Two consequences to weigh:
This makes #4901 unnecessary, so I am closing it. #4909 is rebased on this. |
5c66e2a to
9690e12
Compare
SCHEMA_VERSION is now derived from a SchemaVersion enum, and each variant has its own migration step, applied in order from the database's user_version and stamped in its own transaction. Two changes adding a schema version now conflict instead of merging into one shared bump. The open and the migration engine live in core::db, which the recall database now uses with its own versions, replacing its swallowed ALTER. Each file carries an application_id naming its database, and a file tagged as another database's is refused before rtk changes anything on it. A step that fails rolls back and keeps the stamp, so the next open tries again, and the database is used as it is. rtk gain says why it was not upgraded, a query that needs what is missing says so, rtk gain --reset gives the migration another chance, and rtk init reports the failure. A file that cannot be opened normally is read without its sidecars before anything is repaired, so one that is not rtk's keeps its permissions, and a WAL-mode file on a read-only mount opens when its -wal holds nothing. Co-Authored-By: Claude Opus 5.5 <[email protected]>
9690e12 to
94aa52e
Compare
|
Hey @KuSh LGTM, On the two consequences, both are fine:
|
* Merge pull request rtk-ai#4894 from KuSh/fix/schema-version-enum fix(db): version the tracking and recall schema migrations Ported from upstream rtk-ai/rtk 76be19c, with 24ff226 (rtk-ai#5180, clippy on Windows FileId). tracking.rs keeps the fork's seen_by_agent cap helpers; retriever.rs keeps a test-only open_existing for the tee and posthook tests. Co-authored-by: Nicolas Le Cam <[email protected]> Co-authored-by: LeviReisJs <[email protected]> * test(db): databases from the fork's last release migrate, and a failed step leaves them usable * fix(db): build against the fork's rusqlite 0.41 (MAIN_DB, busy_handler takes &mut) * style(db): rustfmt * test(db): a failed recall step and a step failing at its last statement leave the release's files usable --------- Co-authored-by: Nicolas Le Cam <[email protected]> Co-authored-by: LeviReisJs <[email protected]>
Summary
The tracking DB's
SCHEMA_VERSIONwas a plainconst, and a staleuser_versionre-ran every migration. Two PRs that each add a migration make the same2 → 3edit, so git merges them silently, and a database already stamped 3 by a build with only the first one never runs the second one's migration. The recall DB had no version at all and re-ran its schema on every open.This moves the SQLite open and migration engine into a new
core::db, used by both databases:Nextsentinel (tracking:Initial = 1,EngineTables = 2,InitialRecheck = 3; recall:Initial = 1,Recalled = 2), one step per version inMIGRATIONS, and a const assertion that fails the build unless they match. A new schema change adds a variant above the marker line and its step; two PRs doing that at once conflict.IMMEDIATEtransaction that re-reads the stamp under the lock and stamps its own version, so a failed step leaves the previous version and the next open tries again, and concurrent openers apply each step once. A current database costs one unlocked read.rtk initre-applies every step (tracking always, recall when its store exists and recall is enabled) and leaves each database at this build's version, as develop does.application_id = 0x5254_4B00 | kind("RTK" plus a database number from aDbKindenum). The tag is read with the stamp and checked before anything is written, so pointing one database's path at the other's file (or at another application's) is refused instead of silently skipping steps, and a refused file that could be read is left as it was.commandsALTERs, the NULL backfill, the project index, recall'srecalledALTER) now fail their step, throughadd_column_if_missing. Releases that stamped 1 or 2 could have skipped part of the Initial schema that way, soInitialRecheckre-applies it once on those databases.rtk gainreport says why on stderr, a query that needs what is missing says so,rtk gain --resetreplays the steps first, andrtk initstill warns. Refusing a file that is not this database's stays fatal.immutable=1, which needs no sidecar: one that is not a database or not this one is refused with its modes kept. Otherwise a directory or file its owner cannot use is given back (0700,0600) and the open is tried again, so everything develop repaired before opening still opens. A file that only this made readable and that is then refused gets its modes back. The sidecars are found through a symlink, and repaired before a step writes to them.-shm, so it failed on develop. It now opens with thatimmutable=1read, unless a-walor a hot-journalholds data the read would miss.The parent-directory chmod of a configured database path is unchanged (#4280).
Behaviour changes:
rtk gainprintsrtk: tracking database was not upgraded: <cause>on stderr, and a query that needs what is missing fails withno such column: x (tracking database was not upgraded: <cause>). Writes are still attempted. A reset replays the steps, then clears what it can reach: a table a failed step left out stops it, with the cause.rtk discoverno longer suggestsrtk initfor a failed step.rtk: recall database looks corrupted (…). Runrtk initto recreate it., as tracking does, instead of being recreated on open.… is the rtk recall database, not the tracking one; set RTK_DB_PATH or [tracking] database_path to another file.rtk initalso repairs an existing recall store; a failure prints[warn] Failed to prepare recall database: …and init carries on.rtk gain --resetstill resets tracking when the recall store is unusable, says so on stdout and exits 1 withrtk: recall stats were not reset: ….rtk gain --recallsfails on it as on develop, withrtk: Recall store unavailable: ….rtk initandrtk discover --verboseprint the whole error chain, so the SQLite cause follows the step that failed.rtk recallsaysstore unavailableonly when the store cannot be opened, and prints other failures as they are.Test plan
commands.project_path), unversioned recall stores; a fresh DB of each kind; steps at or below the stamp never replayed (a probe step that fails when run twice)rtk init: replays, never lowers a stamp mid-run, hands a newer build's DB back at this build's version, leaves a missing or disabled recall store alonertk initfails, a file tagged since the first read is still refused0555directory, a write lock held by another process, a step that always fails; everyrtk gainreport,--reset,rtk discoverand the hook on them0000or0200or in a directory at0000or0300, which get their modes back; a path that is not UTF-8; a-walor-journalholding data keeps the failure; a path that needs escaping or starts with two slashes; a step never writes to a loose sidecar-wal,-shmand directory under umask 022; recall's connection cache never serves another path, a removed store or a recreated one0555/0500/0000with an existing or missing file, file at0000/0200, both at once, a read-only file behind a symlink, a missing store left alone8bb6096b):rtk ls,rtk gainandrtk initon 600 tracking states (directory mode, file mode, journal mode or leftover sidecars, symlink, including one into another directory) andrtk recall --list,rtk gain --recallsandrtk initon 189 recall states; nothing develop handles fails here (rtk lsandrtk gainwork on develop in 549 of the 600 tracking states, and the same 51 fail on both). Tracking's final directory and file modes afterrtk lsandrtk gainare the same on every state develop handles. For recall, the only directory-mode difference is the documented one: a directory at0555,0500or0300comes out at0700rtk gain --reset/--recallsandrtk init's recall warning through the real binarygit merge-file; one more variant added to both DBs leaves the suite greencargo fmt,cargo clippy --all-targets,cargo test --all, the same with--no-default-features, and ax86_64-pc-windows-gnucheck🤖 Generated with Claude Code