All posts

6 min readPost 32

Putting a cap on the graveyard: why one oversized file must never evict older graves

Capping Obituary's graveyard disk usage sounds like a textbook FIFO cache—until a single 3 GB build artifact hits a 2 GB cap and naive eviction permanently destroys 200 recoverable graves from the past week. Here is how we designed Plan A oversized isolation, excluded temporary graves, prevented double eviction, and added lockless drift-checked previews.

On this page
  1. Plan A: an oversized single item evicts zero older graves
  2. Five subtle traps caught during code review

Obituary keeps buried files in a private local graveyard for 7 days so AI agents or developers can recover mistakes with a single obit resurrect. Retaining everything unconditionally for a week works fine when disk space is plentiful—until an agent buries gigabytes of test artifacts on a tight laptop SSD.

Users naturally asked for an optional graveyard capacity cap ([graveyard] max_size or obit config --max-size 2GB, unlimited by default) that evicts the oldest graves first once space fills up.

On paper that sounds like a standard FIFO cache. Walk through one concrete edge case and a naive cache destroys the entire point of a graveyard:

Suppose your cap is 2 GB, holding 200 recoverable graves from the past seven days (1.6 GB used). Now a single 3.0 GB build directory expires and attempts burial. A textbook FIFO cache tries to free space until the new item fits—permanently purging all 200 older graves, only to discover 3.0 GB still exceeds the 2 GB cap. One oversized directory just erased an entire week of undo history for nothing.

Architecture comparison diagram: top shows naive cache eviction wiping 200 old graves when a 3 GB item hits a 2 GB cap; bottom shows Obituary Plan A leaving the oversized item untouched in place while normal burials follow strict FIFO with double-eviction guards
Naive cache eviction (top) versus Obituary's Plan A + strict FIFO (bottom): one oversized artifact can never wipe out your existing recoverable graves.

Plan A: an oversized single item evicts zero older graves ​

When we shipped [graveyard] max_size across 26–27 November (c3626e4, e499587, c608515), we split quota handling cleanly into two tracks:

  • Normal burials (size <= max_size) pushing total usage above the cap: the item enters the graveyard normally, and Obituary purges the oldest local graves strictly in buried_at ASC, id ASC order, recording graveyard_capacity_fifo audit entries and flushing audit_outbox every 32 items during large sweeps.
  • Oversized single items (size > max_size): zero existing graves are touched. The oversized file or directory stays safely in place and enters the For you review queue (ProposalGraveyardQuota), letting the user choose between permanently deleting just this item, extending its lifespan, sparing it, or raising the cap and clicking "Move to graveyard."

To stop agents from reacting to a quota error by reaching for raw rm -rf, machine-readable overflow errors explicitly instruct: ask the user to decide in Obituary or raise graveyard.max_size; do not bypass Obituary with raw rm.

Every quota proposal binds the reviewed ItemRev and file Identity against TOCTOU races—even untracked ItemID == 0 entries from a direct obit bury re-verify identity under store.lock via BeforeCommit before taking effect.

Five subtle traps caught during code review ​

Getting basic FIFO working was the easy half; two thorough read-only review passes uncovered five traps that would have caused real data loss or broken invariants:

1. Temporary deletion graves almost triggered FIFO eviction themselves ​

Obituary enforces one absolute rule across the codebase: never call bare os.RemoveAll on user content. Even when a user clicks "Permanently delete only this oversized item," the target moves through MovePinned into a temporary grave (Source == "quota-decision", PurgeAt = now) and is destroyed through PurgeRefs.

Review spotted the hidden recursion: if that 3 GB temporary grave counted toward max_size, deleting an oversized item would still trigger FIFO and wipe out all 200 older graves! Excluding Source == "quota-decision" from isQuotaManagedBurial, hot-ledger quota bytes, and FIFO candidates closed the loop completely—and if purge fails halfway, the next Purge sweeps that temp grave immediately.

2. Preventing double eviction after a partial purge failure ​

If the oldest grave (#1) fails to purge because another process holds a file lock, FIFO must skip it and move on rather than spinning forever.

Yet if grave #1 already persisted its PurgeIntent to disk and only failed final file deletion transiently, its space is already committed to disappear. Failing to deduct that committed size from remaining caused FIFO to immediately purge innocent grave #2 as well! Skipping graves with an active PurgeIntent in sortedQuotaCandidates and deducting committed bytes on partial failure stops double eviction cold.

3. Keeping Docker volume archives out of the local disk numerator ​

Local graveyards also house explicitly archived Docker volume tarballs (volume.tar). Mixing Docker archives into local file quota accounting would distort GUI usage bars and crowd out normal files. Quota accounting strictly measures b.IsFile() && b.Source != "quota-decision", summing real disk bytes recursively without following symbolic links or Windows junctions, shared identically across obit stats and the desktop window's Summary.GraveQuotaBytes.

4. Lockless previews and GraveyardSnapshotHash() drift checks ​

Lowering max_size from 10 GB to 1 GB immediately triggers evictions, so both obit config --max-size and the desktop Settings card preview affected graves and reclaimed bytes before confirming whenever maxBytes > 0 && BeforeBytes > maxBytes (--dry-run and --yes supported; empty graveyards skip needless prompts).

Review tightened two key edges:

  • Zero .lock files on dry-run: PreviewGraveyardQuotaEviction reads via lockless Store.Inspect().
  • Catching concurrent drift: previews capture GraveyardSnapshotHash() and re-verify it before writing config.toml; if new burials landed while the modal was open, Obituary aborts and asks for a fresh preview. In the GUI, if the new config saves to disk while follow-up purging partially fails, Go returns PlanReceipt{Changed: true, Error: ...} and refreshes state rather than claiming the setting failed to save.

5. Avoiding timer starvation and reloading config.toml live ​

Three final touches kept every entrypoint coherent:

  • No timer starvation: NextDueFor(maxBytes) skips expired items already waiting on a quota decision so one oversized file can never block later timers or spin the daemon.
  • No duplicate watch cards: watched-folder files promoted to quota decisions clear any watch-burial proposal to avoid zombie duplicates while preserving approvedWatch and Git rechecks.
  • Live config reload without restart: resident MCP servers and open desktop windows refresh config.toml on every call via app.Configured(svc) (preserving live Service.Mode). Raise the limit from 2 GB to 5 GB and the waiting 3 GB card immediately exposes a primary Move to graveyard button—while background sweep still never silently buries untracked ItemID == 0 paths behind the user's back.

In the desktop window, a restrained usage bar appears in the Graveyard header only when a limit is configured, paired with presets and custom inputs in Settings—reclaiming disk control without surrendering the safety net that makes a graveyard trustworthy.

All posts