Skip to content

fix(db): open a read-only stale DB without migrating - #4901

Closed
KuSh wants to merge 2 commits into
rtk-ai:fix/schema-version-enumfrom
KuSh:fix/tracking-readonly-open
Closed

KuSh wants to merge 2 commits into
rtk-ai:fix/schema-version-enumfrom
KuSh:fix/tracking-readonly-open

Conversation

@KuSh

@KuSh KuSh commented Oct 31, 2026 •

Copy link
Copy Markdown
Collaborator

Stacked on #4894: this PR's base is a copy of that branch, so the diff shows only this change. It is retargeted to develop once #4894 is merged.

Summary

When a tracking DB or recall store is read-only and stale (a build that bumped the schema version, then a sandboxed agent shell with a read-only data dir, a read-only mount, or a file left root-owned by sudo rtk ...), its open failed on the migration: rtk gain, discover and session printed "Failed to initialize tracking database", and rtk recall said the store was unavailable. Both now open the database as it is, and a query that needs a table or column the old schema lacks says why.

db::open_and_migrate and db::open_existing_and_migrate return the connection and a behind flag. A gated open never migrates a connection SQLite reports read-only (sqlite3_db_readonly: opened read-only, or SQLite's fallback on a file it cannot write, EACCES/EROFS, after claim has tried to make the file writable): it reads the stamp it already has, and the database is behind when the stamp is below the schema's last step. A refusal that only shows on write (a read-only directory) is told by SQLITE_READONLY from a step (any SQLITE_READONLY_*, found anywhere in the error chain; CannotOpen is not one), after the version has been read. Ownership comes first: a read-only file that is another database's, or untagged with a stamp the schema does not own, is refused, not opened behind. rtk init (Always) still fails on a database it cannot migrate, and reports it as a warning.

The Tracker holds a small Db wrapper without Deref, so every query goes through prepare / execute / query_row, which append "tracking database is read-only and was not upgraded" to a "no such table", "no such column" or "has no column named" error (as a plain SQLite error, without the statement text). Because Tracker::new() now succeeds on such a DB, record, record_parse_failure, record_hook_decision, record_attribution and classifier_cache_put return Ok(()) on a behind tracker without touching the DB, so the hook and rtk <cmd> stay quiet. rtk gain --reset is an explicit request and reports the refusal.

The recall store caches the flag with its connection, and an error from a closure run on a behind connection gets the same note, "recall database is read-only and was not upgraded". Its writers need no skip: their failures were already handled as on any store that cannot be written.

  • rtk gain on a read-only stale DB opens and shows what the old schema holds; a query on a newer table fails with no such table: engine_attribution (tracking database is read-only and was not upgraded) instead of "Failed to initialize tracking database".
  • rtk recall --list, rtk recall <hash> and rtk gain --recalls on a read-only stale store read what it holds. One without recall_stats shows Recall store unavailable: no such table: recall_stats (recall database is read-only and was not upgraded) in rtk gain --recalls.
  • The hook, rtk <cmd> and the analytics writers stay quiet on such a DB: no "hook_decisions logging failed" and no write errors.
  • rtk gain --reset on it fails with "Failed to reset token savings: tracking database is read-only and was not upgraded".
  • A writable stale DB, a 0444 backup that can be made writable, a current read-only DB and a corrupt or unreadable DB behave as before.
  • A WAL-mode file SQLite cannot read without writing its -shm (no sidecars, on a read-only mount) still fails on the first read, as on develop.

Test plan

  • A stale read-only DB is kept behind with its stamp untouched and no step applied, even a step that would fail before writing; a writable stale one is migrated, not kept behind; a current read-only one, and one stamped by a newer build, is not behind
  • A kept-behind WAL database has its -wal/-shm restricted to 0600
  • Always mode still fails with SQLITE_READONLY
  • A read-only file tagged as another database's, or untagged with a stamp the schema does not own, is refused; an unversioned one is taken and kept behind
  • A refusal that only shows on write (PRAGMA query_only) keeps the connection behind; in Always mode it fails
  • A locked directory is given back to its owner and the steps run, it does not keep a database behind
  • is_readonly is true for a SQLITE_OPEN_READ_ONLY connection and for Connection::open on a 0444 file (skipped when the process can write it); a 0444 file claim can make writable is migrated, not kept behind
  • Only SQLITE_READONLY counts as read-only, not CannotOpen or a plain error
  • Only SQLite's missing table or column errors count as a missing object, not a syntax error or an error from elsewhere
  • The tracking Db carries the flag from Opened; a behind DB names the cause for a missing table (prepare, execute and query_row; a plain SqliteFailure too), a missing column and an INSERT naming one, and leaves other errors alone; a current tracker never annotates
  • A behind tracker's write methods return Ok and leave every table empty (engine-only writers in #[cfg(feature = "engine")] tests); a current tracker still records
  • reset_all on a behind tracker reports the note
  • A behind recall store serves what it holds; a missing table or column gets the note on a new, a cached and (on Unix) a used-once connection; a current store's error and any other error do not
  • develop, fix(db): version the tracking and recall schema migrations #4894 and this branch run on a read-only bind mount (unshare -rm) against stale, current, empty, WAL and foreign recall stores: rtk recall --list, rtk recall <hash> and rtk gain --recalls work wherever develop's do, except on a store that is another database's file
  • Each assertion fails when the code it pins is reverted
  • cargo fmt, cargo clippy --all-targets, cargo test --all, the same with --no-default-features, and a x86_64-pc-windows-gnu check

🤖 Generated with Claude Code

@rtk-wshm-sync-bot rtk-wshm-sync-bot Bot added bug Something isn't working tracking database labels Oct 31, 2026
@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟡 Risk medium

Summary

Adds open_and_migrate_or_stay_behind to core::db so the tracking database, when read-only and behind the current schema, opens as-is instead of failing migration. Queries hitting missing tables/columns now surface a clear 'read-only and was not upgraded' message, and tracking writes become silent no-ops on a behind database instead of erroring; rtk init and the generic open_and_migrate path are unaffected.

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Analyzed automatically by wshm · This is an automated analysis, not a human review.

KuSh and others added 2 commits November 4, 2026 14:48
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.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A gated open of a read-only database keeps the connection at the schema it has,
for tracking and recall; queries that need the missing table or column say the
database was not upgraded.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@KuSh
KuSh force-pushed the fix/schema-version-enum branch from f8f8180 to d3a5497 Compare November 4, 2026 18:27
@KuSh
KuSh force-pushed the fix/tracking-readonly-open branch from 6ccfa6f to a5a9ff1 Compare November 4, 2026 18:27
@KuSh KuSh changed the title fix(tracking): open a read-only stale DB without migrating fix(db): open a read-only stale DB without migrating Nov 4, 2026
@KuSh
KuSh force-pushed the fix/schema-version-enum branch from d3a5497 to 5c66e2a Compare November 6, 2026 18:18
@KuSh

KuSh commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator Author

Closing: #4894 now covers this and goes further. A step that fails on a normal open keeps the database usable whatever the cause, for tracking and recall, which replaces the read-only handling here.

@KuSh KuSh closed this Nov 6, 2026
@KuSh
KuSh deleted the fix/tracking-readonly-open branch November 19, 2026 00:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working database tracking

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant