Skip to content

fix(vault): trim the changes log in the daily prune pass - #236

Merged
bketelsen merged 2 commits into
mainfrom
irulan/219-prune-changes-log
Nov 14, 2026
Merged

bketelsen merged 2 commits into
mainfrom
irulan/219-prune-changes-log

Conversation

@frostyard-machines

@frostyard-machines frostyard-machines commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Summary

notesd's durable event log, the changes table, grew without bound: store.DB.PruneChanges existed, but nothing called it. Closes #219.

  • Retention rule (ADR-0044, Proposed): the daily prune pass (Manager.Prune) deletes the durable events recorded more than 30 days ago (Config.Keep, the same window as local versions) and all but the newest 100,000 (Config.KeepEvents, about 20 MB). The age rule follows ADR-0021's need to replay moves made while an app was closed. The count cap bounds the table during a burst (a first scan, a bulk move).
  • Cost of a resync: both apps reload the vault list and the open vault on every connection already. A client away longer than the retained log loses only this: its tabs do not follow the moves of that time (ADR-0021). The latest seq survives a prune (AUTOINCREMENT), so a client that saw it resumes even when the log is empty.
  • Only events.subscribe reads the log. store.ChangesSince and store.OldestSeq have one caller, events.Broker.Subscribe. The changes in store.Pending (internal/store/remotes.go) is a CTE alias over files and sync_files, not the table. The sync loop subscribes without sinceSeq (internal/syncer/loop.go), and the hub's log is the revisions table.
  • Race closed in Broker.Subscribe: a prune between the check of the oldest seq and the replay read could give a replay with a gap and no resync. The broker now reads the oldest seq again after the replay. A prune deletes only from the oldest end, so this second read is enough.
  • PruneChanges stays in the store (grooming: dead and duplicated code after the native desktop move #213). It now takes an age cutoff and returns the number of rows it deleted.
  • Docs: a new section, docs/design/vault.md#changes-log; a retention line in docs/specs/rpc-protocol.md#events; the ADR index.

Risk tier

Risk tier: 2 — behaviour change in code: notesd now deletes rows of its own event log, and events.subscribe answers resync to a client older than the retained log. No vault file, version, sync state or API contract changes. No path in .github/risk-paths.json matches.

Docs housekeeping

  • New docs started from their category's TEMPLATE.md
  • Every new doc indexed in docs/README.md
  • Cross-links added in both directions (ADR ↔ design ↔ spec ↔ plan). The ADRs that 0044 builds on are Accepted, so they get no back-link.
  • New significant decision recorded as an ADR first, in this PR
  • New or edited ADRs are fact-only: context, decision and consequences

Verification

  • make check green (host)
  • Revert test for a fix: each new test fails with its fix reverted.
    • With the PruneChanges call removed from Manager.Prune, go test ./internal/vault -run TestPruneTrimsTheChangesLog fails ("want resync").
    • With internal/events/broker.go from main, go test ./internal/events -run TestResyncWhenPrunedDuringTheReplayRead fails ("want resync at seq 4").
    • With internal/store/changes.go from cf96312, go test ./internal/store -run TestPruneChangesByAgeLeavesNoGap fails ("PruneChanges = 1, ; want seqs 1 and 2 deleted").
  • Blocking review point fixed: guard added and 1 file added under .memory/corrections/

Review

Cross-review by openai/gpt-6.1-sol (coder anthropic/claude-opus-5-5), base bac5e976271515126740f6b736eda6ce989ca8d6:

  • At cf96312af19b015d00fd9654bc7f11b654f4c3d8: Not ready, 2 findings.
    1. Medium, blocking: the age rule (at < cutoff) could delete an event between 2 kept ones when the wall clock went back. A subscriber from before the gap then got a replay with a gap and no resync. Fixed in e32b3d0: the age rule deletes a seq prefix (up to the newest event recorded before the cutoff). Guard: store.TestPruneChangesByAgeLeavesNoGap and .memory/corrections/2026-11-13-pr-236-prune-a-seq-prefix.json.
    2. Low, not blocking: the docs said that the 100,000 bound holds whatever the rate. The bound holds after each pass only. Fixed in e32b3d0 (ADR, design, spec).
  • Re-review 1 at e32b3d0bb35e862b263ad9b67819d9e2e9e530f4: Ready, Blocking: none.

The changes table grew without bound: PruneChanges existed, but nothing
called it. The daily prune pass now deletes the durable events older than
Config.Keep (30 days, as for versions) and all but the newest
Config.KeepEvents (100,000). A subscriber older than the retained log gets
resync, as the protocol says.

events.subscribe now reads the oldest retained seq again after it reads
the replay, so a prune between its reads gives resync, not a replay with
a gap.

ADR-0044 (Proposed) records the rule; docs/design/vault.md and
docs/specs/rpc-protocol.md describe it.

Closes #219
@github-actions github-actions Bot added the level:L3 Skim label Nov 14, 2026
An event's time is the wall clock at its commit, and the clock can go
back, so `at < cutoff` could delete an event between two kept ones. A
subscriber from before the gap then got a replay with a gap and no
resync, since the broker checks only the oldest retained seq. The age
rule now deletes up to the newest event recorded before the cutoff.

The docs also say that the limits hold after each pass, not between
passes.

Guard: store.TestPruneChangesByAgeLeavesNoGap, and a record in
.memory/corrections/ (cross-review of #236).
@frostyard-machines frostyard-machines added the ready-for-human Reviewed, CI green: ready for Brian to review and merge label Nov 14, 2026
@bketelsen
bketelsen merged commit 6e415ea into main Nov 14, 2026
12 checks passed
@bketelsen
bketelsen deleted the irulan/219-prune-changes-log branch November 14, 2026 00:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

level:L3 Skim ready-for-human Reviewed, CI green: ready for Brian to review and merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

changes log is never pruned: add a retention step that calls store.PruneChanges

2 participants