Skip to content

Clear deleted history out of the database files, not just the tables - #324

Merged
parawanderer merged 1 commit into
mainfrom
feat/compact-after-history-prune
Nov 28, 2026
Merged

parawanderer merged 1 commit into
mainfrom
feat/compact-after-history-prune

Conversation

@parawanderer

Copy link
Copy Markdown
Owner

Follow-up to #321 (location history retention) and #315 (removing a tag's data), both by @lazzurs. Deleted reports were still readable in the database files after either.

Cause

Android's SQLite has secure_delete and auto_vacuum on (measured on the managed device: secure_delete=1, auto_vacuum=1), so a delete zeroes the row. In WAL mode that happens in the log: until a checkpoint, the main file keeps the pages from before the delete and -wal keeps the frames that inserted the rows.

#315's compactBestEffort ran VACUUM and then sqlite.query("PRAGMA wal_checkpoint(TRUNCATE)").close(). An Android cursor runs when first read, so that checkpoint never executed. After VACUUM, the deleted text was still in the files; after a checkpoint whose row is read, in neither. #321's prune did no checkpoint at all.

Change

  • db/repo/DatabaseCompaction: checkpoint reads the pragma's row, and logs when a reader kept it from finishing; now runs VACUUM then checkpoint. Replaces BeaconRepository.compactBestEffort.
  • LocationHistoryRetention.prune (Settings, shorter period): now when something was deleted.
  • LocationHistoryRetention.pruneAtStart (every app start): checkpoint only, which clears the rows and writes only the changed pages, instead of rewriting the whole file at every start. OpenAirTagApplication calls it.
  • Tag removal keeps VACUUM plus the now-working checkpoint.
  • AGENTS.md rule 24 records the WAL remnants and the unread cursor.

Tests

  • CompactingAfterDeletingHistoryTest (new, instrumented): a database file of its own, 300 padded reports carrying a marker, prune, then searches the main file and -wal byte for byte. Asserts the marker was there before, is gone after, and that the kept report's marker is still found. One test per path (prune, pruneAtStart).
  • HistoryRetentionSettingTest: the fake pruner recorded the stored period inside Single.fromCallable, i.e. at subscription, so save.andThen(prune) passed the ordering test as well as RxFlows.onlyAfter did. It now records when prune() is called. New aFailedSaveDeletesNothingAndPutsThePeriodBack hosts the row on TestHostActivity with a repository whose save fails, and asserts the pruner is never called and the subtitle returns to Forever.

Negative controls, each run on the managed device and reverted:

Change Result
Checkpoint closed unread, as before both compaction tests fail
No checkpoint in pruneAtStart the start test fails
RxFlows.onlyAfter replaced with save.andThen(prune) the ordering test and the failed-save test fail

Verified

  • Managed device (arm64 aosp-atd API 34): CompactingAfterDeletingHistoryTest, PruningOldLocationHistoryTest, HistoryRetentionSettingTest, RemovingATagForgetsItsKeysTest, 31 tests, all pass.
  • :app:testDebugUnitTest and check_java_imports.py pass. The full emulator suite was not run locally; CI runs it.

Not verified

  • A phone whose SQLite lacks secure_delete. VACUUM stays on the user-initiated paths for that case; the app-start checkpoint alone would leave deleted rows in free space there until the next Settings change or tag removal.
  • How long VACUUM takes on a large real history.

🤖 Generated with Claude Code

A delete left the rows readable in the files. Android's SQLite zeroes a
deleted row, but in WAL mode that happens in the log: until a checkpoint
the main file keeps the old pages and the -wal keeps the frames that
inserted the rows. The checkpoint #315 added after VACUUM never ran,
because a PRAGMA query closed unread is never executed.

DatabaseCompaction now holds both steps, with the checkpoint's row read
so it runs. A period chosen in Settings and a removed tag get VACUUM and
the checkpoint; the prune at every app start gets the checkpoint alone,
which is what clears the rows and writes only the changed pages.

CompactingAfterDeletingHistoryTest reads both files byte for byte for
text only the deleted reports carried; both tests fail with the
checkpoint unread, and the start test fails with it removed.

HistoryRetentionSettingTest's fake now records when prune() is called
rather than when it is subscribed, so save.andThen(prune) fails the
ordering test, and a new test covers a failed save: nothing is deleted
and the old period is shown again. Both fail with onlyAfter replaced by
andThen.

AGENTS.md rule 24 records the WAL remnants and the unread cursor.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@parawanderer
parawanderer merged commit dc6c41e into main Nov 28, 2026
4 checks passed

This branch was previously deployed

1 inactive deployment
Android Build — ad5f7e20 Deployed Nov 28, 2026 by parawanderer via Instrumented tests (emulator) #436
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