2026-11-25 · The job database protected: copies, a check, a way back (#275)

2026-11-25 · The job database protected: copies, a check, a way back (#275)

Context

Every correction, review mark, project, dictionary decision and paid answer lives in one file, jobs.sqlite. The review of 25 November (safety nets) found nothing between that file and one bad migration or one damaged page: migrations ran on the live file with no copy taken; quick_check ran on project files only; a database that could not be opened ended the launch with “may be damaged”; no test migrated a database an earlier release had written; one job record this version could not decode failed the whole job list; and a frozen text cut short by a power cut was refused as changed for ever, since replaced files were renamed without being flushed and a frozen file was never written again.

Decision

Where the copies live. In a folder of their own, backups, in the app’s data folder beside the database (on Windows %LOCALAPPDATA%\Kalamu Data\backups, on macOS ~/Library/Application Support/Kalamu/backups, inside the package or container for the store builds). Never beside the user’s documents, never sent anywhere. Each file’s name says what it is and when it was made, in UTC: jobs-daily-2026-11-25-143210.sqlite, jobs-before-update-…, jobs-set-aside-…. The names, not a separate record, are what the app reads back, so a copy moved in or out by hand is understood.

What is copied, and how many are kept. The database can be large (paid answers, every page’s text), so few copies, each made with VACUUM INTO (a consistent, compacted copy that holds what the write-ahead log holds), flushed to the disk, and given its name only once whole:

So the folder holds at most five copies besides what was set aside: for a 2 GB database, up to 10 GB. That is the cost of not losing paid work; Clear unused results shrinks every later copy.

Never filling the disk silently. Before each copy the free space of the folder’s disk is asked (fs4, already in the tree); a copy is made only if, once made, at least 1 GB stays free beyond it. A copy skipped is logged and said in Settings (how much was free). The update still runs when its copy was skipped: refusing to start for want of space would cost the researcher more than the copy protects, and each migration is still a transaction.

The check. At launch, before anything changes the file, the database is probed cheaply (its header and table list read on a connection of its own). A file SQLite calls damaged or not a database leads to the question below. SQLite’s quick_check, which reads every page and takes seconds on a large file, runs after launch, off the window’s thread, on a connection of its own. When it finds damage the interface offers the newest copy at once (and Settings says so until it is dealt with); otherwise the day’s copy is made and old copies pruned. integrity_check (indexes matched against tables, many times slower) is not run.

A damaged file at launch. The interface does not exist yet, so the shell asks in a native message, in English or French as the start-up messages are, naming the folder the file will be kept in and the logs folder: Open the copy (the newest daily or before-update copy, dated) or, when there is none, Start with an empty list; or Quit, which changes nothing. Either way forward the file is set aside first. Errors that are not the file’s (a newer version’s database, no permission, a disk gone) are reported as before and nothing is moved.

Damage the probe does not reach. A file whose header and table list are whole but a later page is not passes the probe; opening it (the pragmas, a migration) can then fail with SQLite’s corrupt or not-a-database code. That failure takes the same path as damage the probe found: asked once, the file set aside, the newest copy offered (often the one just made before the update). The store keeps SQLite’s own error for this (open_telling); every other caller still sees the error as before.

Never without a database. Opening a copy stages it first: copied beside the database (jobs.sqlite.restoring, with its log when it has one) and flushed; only then is the database set aside and the staged copy renamed in. When the copy cannot be read, nothing is moved; when setting aside or the last rename fails, what was staged is removed and what was set aside put back, so a failed restore leaves the database and its jobs as they were.

Opening a copy later. Settings › General › Safety copies lists the copies and opens one after asking. The database is in use, so the choice is written into the folder (open-at-next-launch.txt) and the app restarts; the next launch sets the database aside and puts the copy in its place before opening it. Refused while a job runs. The restart (request_restart) ends the app without the window’s close request, so it does not pass through the guard that keeps a window from closing over an unsaved correction, prompt or workflow (#271): the interface asks about a draft not saved first, in the same words, before asking about the copy. Setting aside is tried again for a few seconds while the ending process still holds the file. The request is forgotten whatever happens, so a copy that cannot be opened is not tried at every launch.

Fixtures. crates/store/fixtures holds a database written by 1.0.0 and by each minor release since: every one changed the schema (1.1, 1.2, 1.3, 1.6, 1.7, 1.8, 1.9) or a record the store keeps as JSON (1.4, 1.5, 1.10); the patch releases changed neither. Each was written by that release’s own core and store crates, taken out of git into a scratch workspace by fixtures/make.sh, with fixtures/generator.rs appended to that release’s store tests so its own helpers make the records: one job with two answers and a correction, and from 1.1.0 a project with a collection, a document, a link and a chosen result. All eleven tags built. The store’s tests migrate each and read everything back; a list in the test must match the folder. A release that changes the schema or a stored record runs make.sh <tag> and adds the version.

One bad row. list_jobs leaves out a job whose record or status this version cannot read, and unreadable_jobs names it (id, name, why); the interface names it once at start-up and the log records it. The record is kept unchanged, for the newer version that may read it.

Files replaced. write_atomic flushes the temporary file (sync_all) before the rename. A frozen text whose file is missing or no longer matches its fingerprint is written again from the store when the result it was frozen from, as corrected now, still gives exactly the same text: when the same text is frozen again, and when a job that reads it finds it changed. Otherwise it stays refused, as before.

Consequences