Skip to content

feat(store): keep the spectrum history across restarts - #17

Merged
mrcsin merged 13 commits into
masterfrom
persistent-history
Nov 7, 2026
Merged

mrcsin merged 13 commits into
masterfrom
persistent-history

Conversation

@mrcsin

@mrcsin mrcsin commented Nov 7, 2026

Copy link
Copy Markdown
Member

The spectrum history was wiped at every start, so an accidental close lost up to a day of trend.

  • One history file %LOCALAPPDATA%\SemiSpec\Spectra\spectra.db is kept across restarts; the 24 h retention runs at open and every minute, with a one-step path when every row has expired (78 ms instead of 909 ms on a full day). Store files of older builds are deleted at start.
  • The file carries a schema version; a file of another version is replaced with a fresh one (one Warning in the log).
  • A file SQLite cannot read is set aside as spectra.broken-<time>.db (at most 2 kept) and reported in the message panel; damage found later by a write, read or retention pass leaves a marker so the next start sets the file aside the same way.
  • At start the trend loads the stored day once the store is open; the periods the app was closed show as "Нет данных" breaks, now drawn from gaps over 10 s between points.

Verified: 1045 tests (992 before), Release build 0 warnings, format and terse clean; warm benchmark within targets (whole-day re-read 752 ms against 2000 ms). Reviewed by a second model with no blocking findings. Left for the operator PC: the cold read of a full day after a reboot (budget 10 s) and the smoke checklist in the plan.

🤖 Generated with Claude Code

mrcsin and others added 13 commits November 6, 2026 18:06
The spectrum history is wiped at every start, so an accidental close
loses up to a day of trend. The plan keeps one history file across
restarts with the existing 24 h retention, loads it into the trend at
start, and sets a damaged or outdated file aside.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Each start wiped its own spectra-<pid>.db and swept the files of other
instances, so the 24 h history died with every restart. The store now
writes spectra.db in the store folder and keeps it. Prepare deletes
the spectra-<pid>.db files and sidecars of older builds instead of
sweeping each hour. The writer runs one retention pass right after the
open, so a file closed for days loses its expired rows before it is
read. The schema statements skip existing tables until the version gate
of a later change replaces them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The history file now survives restarts, so a file closed for a day or
more holds only expired rows at the next start. The row-wise retention
delete visits every row and index entry: 908.5 ms on a 762 MB day, and
it runs before the trend can read.

DeleteBefore reads max(received_at) of both tables inside its
transaction. When both are below the cutoff or empty, it runs DELETE
without WHERE, which takes SQLite's truncate optimisation: 77.9 ms on
the same file. Otherwise it keeps the row-wise statements.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The history file now outlives the process, so a build with another
schema can meet a file it cannot use. Task 1 papered over the reopen
with CREATE TABLE IF NOT EXISTS, which accepts any file that happens to
hold tables of those names.

SpectrumDatabase.Open now reads PRAGMA user_version and the table count
in one IMMEDIATE transaction: a table-less file gets the schema and
version 1, version 1 is used, anything else fails with
StoreSchemaMismatchError. StoreOpening, the writer's new open sequence,
deletes a mismatched file with its sidecars, logs one Warning naming
both versions and opens a fresh file; no panel entry, since an app
update causes it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The history file now lives across restarts, so a file SQLite reports as
not a database (26) or corrupt (11) would end history for every later
run with a StoreOpenError.

SpectrumDatabase.Open turns those two codes into StoreFileUnreadableError.
StoreOpening then renames the file to spectra.broken-<local stamp>.db with
its WAL, keeps the two newest set-aside files, opens a fresh file and
reports StoreFileReplacedError as a Warning. A failed rename or reopen
still ends in StoreOpenError.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The open reads only the header and the schema page, so damage deeper in
the history file shows later, as a failed write or read, and the next
run would keep using the damaged file.

StoreDamage.IsDamage tells SQLite codes 11 and 26 from other failures.
After such a failed batch, retention pass, checkpoint or history read,
the failing party creates the empty marker spectra.db.damaged; a held
lock marks nothing. The next open finds the marker before it opens the
file and sets the file aside with the same report as an unreadable one.
SetAside and DeleteWithSidecars delete the marker.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A spectra.db.damaged marker whose spectra.db was deleted by hand made the
open try to set aside a missing file, so the store failed with
StoreOpenError at every start until someone deleted the marker.

Prepare now deletes a marker whose history file is gone. The marker beside
an existing file stays, so the next open still sets that file aside.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The trend's start read must not open spectra.db while the writer may still
delete, rename or create it during the open, the set-aside or the schema
replace.

SpectrumStore.Opened completes true once the writer opened the file and ran
the retention at open, and false when the open fails, a dispose cancels the
writer first, or a stop comes before the start. A reader that waits on it
never meets a file the writer is about to replace.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
FrameContinuity raised a NoSpectra break only between frames of the
current run, so the time the app was closed and the closed periods
inside a loaded stored day drew as a continuous line.

The fold now adds a NoSpectra break at each point more than the silence
limit after the point before it, also the one just before the range.
Every spectrum, live or stored, is one point (NaN when uncovered), so one
rule covers a live silence and the gaps in stored history. FrameContinuity
keeps the seq reset, Loss breaks and failure marks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
After a restart the trend stayed empty until a key change, although the
history file now keeps the last 24 h across runs.

The cache records its run start and asks for the stored day before it,
at the live key or else at the key of the last stored spectrum. The
feed waits for the store's ready signal with the token its subscription
cancels, reads that spectrum, and the existing throttled history read
lands the day. FoldCurrent drops expired points without new frames, and
every finished history read logs its landing times.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The plan's edge case, a key change while the start read lands the stored
day, had no test of its own; only the generic second-key-change test
covered the Switch it relies on.

The new test holds the start read's first chunk, feeds a live spectrum
at another wavelength, and checks that the held read is cancelled and
the new key is read. The plan records the acceptance run: 1045 of 1045
tests, the gates, and the warm benchmark trend rows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The architecture docs still described one store file per process, wiped
at start and swept hourly, and NoSpectra breaks from FrameContinuity.

They now describe spectra.db kept across restarts, the legacy cleanup,
the schema version gate, the damage marker and set-aside, retention at
open with its fast path, the Opened signal, the trend's start read and
NoSpectra breaks from point gaps, with the matching test coverage.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The plan said no reader opens the history file while the writer may
delete or rename it, but only the start read waits for the open. It
now says so and names the outcome of the rare race: the file is kept
and the session reports a store open error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mrcsin
mrcsin merged commit 2e2714b into master Nov 7, 2026
1 check passed
@mrcsin
mrcsin deleted the persistent-history branch November 7, 2026 06:26
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