Skip to content
This repository was archived by the owner on Nov 15, 2026. It is now read-only.

feat: a daily mark-and-scan sweep removes orphaned blobs after a grace period - #12

Merged
bketelsen merged 1 commit into
mainfrom
feat/blob-sweep
Oct 22, 2026
Merged

bketelsen merged 1 commit into
mainfrom
feat/blob-sweep

Conversation

@bketelsen

Copy link
Copy Markdown
Contributor

Today readd never removes a blob: the originals and images of items whose source is removed stay on disk. This adds a sweep, as ADR-0007 (Proposed) decides.

The decision

  • Mark-and-scan, not a reference count. References go by SQLite cascade where no Go code runs, and originals have no blobs row; a count would need triggers and could drift. The sweep derives what is in use from items.original_hash and item_blobs.hash each time, and also finds files no row ever named.
  • Grace period: blob_orphans (hash, since) (migration 2) records when the sweep first found a file unnamed. A file goes only when since and its modification time are both older than blob_grace_days (default 7, 1 to 365).
  • Pending fetches and concurrent saves: Put of an existing blob touches its modification time. blobs.Store has a read-write lock: Put holds it shared, a removal alone, and the removal re-checks the file time and the database under it. The blobs row is deleted only when no item_blobs row names the hash again.
  • Saved notes hold no blob (images become links); their items keep their blobs.
  • How it runs: in readd, 10 minutes after start, then every 24 hours. blob_sweep: on (default), dry-run (the schedule only logs), off. readctl sweep [-dry-run] runs it now through blobs.sweep, caller local only (a session gets methodNotFound).
  • Dry run removes nothing and writes nothing; it logs "would remove an orphaned blob" per blob. A real removal logs "removed an orphaned blob" with the hash, size and since.

Tests

  • internal/engine/sweep_test.go: blobs in use stay (image, original, saved note, pending item, starred, archived, shared); orphans past the grace go with their rows; orphans inside the grace stay (new, recent mtime, recent since); a removed source's blobs wait, then go, and a shared one stays; the dry run removes and writes nothing; a save during the sweep (Put then reference, Put alone, reference alone) keeps its blob; and a sweep running beside 60 saves under the race detector.
  • internal/blobs, internal/store, internal/config, internal/readapi, cmd/readd, cmd/readctl: List, RemoveIf, the touch, the queries, the settings, local-only, the CLI.

Checks

make check, make race and make cross pass locally. make contract-update changes no golden; the web bundle does not change.

Notes for review

  • since and the cutoff use the engine clock; the modification time is the file system's. In production they are the same clock.
  • blob_grace_days: 0 reads as the default, 7.
  • The docs follow: the spec (method, result, readctl, configuration, state, rules), the design overview, and roadmap Phase 8. The ADR is Proposed: please accept it or ask for changes.

Humans merge.

…s after a grace period

ADR-0007 (Proposed). readd marks the hashes the database names, scans
blobs/, records in blob_orphans when it first finds an unnamed file,
and removes a file once that record and its modification time are both
past blob_grace_days (7). Put touches a blob that exists, and a removal
checks the file's time and the database again under the blob store's
lock, so a save beside a sweep keeps its blob. The sweep runs 10
minutes after start, then every 24 hours (blob_sweep: on, dry-run or
off), and readctl sweep [-dry-run] runs it now through blobs.sweep,
caller local only. A dry run removes and writes nothing.
@bketelsen
bketelsen merged commit 2decfa3 into main Oct 22, 2026
3 checks passed
@frostyard-machines
frostyard-machines deleted the feat/blob-sweep branch October 26, 2026 00:21
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant