Add Smart Cache: play-driven downloads with a size budget - #1244
Draft
skalthoff wants to merge 2 commits into
Draft
Add Smart Cache: play-driven downloads with a size budget#1244skalthoff wants to merge 2 commits into
skalthoff wants to merge 2 commits into
Conversation
Implements #1154. Tracks played past the scrobble threshold are admitted into a user-budgeted cache; cold tracks are evicted to make room, and explicit downloads become pins that are never auto-evicted. - src/cache/core: pure policy engine (admission, recency-decayed scoring, watermark eviction, disk reconciliation) with no RN/native imports - src/cache: ledger store (versioned MMKV), nitro storage adapter, and an event-pump service wiring player signals to the engine - downloads pipeline: wire onDownloadStateChange, dedupe completion upserts, recover stuck 'fetching' state via startup reconciliation - replace auto-download with cache admission; migrate the legacy setting to a default 4GB budget once - Storage screen: smart cache toggle, budget presets, usage line, and a confirmation card before destructive budget shrinks - tests: 5 new suites / 66 tests covering scoring, eviction invariants, reconciliation, engine scenarios, and the service against a fake adapter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What is the change
This adds a smart cache: the user sets one size budget, tracks they actually listen to earn their way onto disk, and cold tracks make room for hot ones. Explicit downloads become pins that are never auto-evicted.
How it decides:
How it's built:
src/cache/core/is a pure policy engine — no React/RN/nitro imports. One reducer,decide(ledger, event, now, context) → { ledger, effects }, returns commands (fetch/evict/confirm-eviction) instead of performing side effects, so every policy rule is unit-tested with plain objectssrc/cache/service.tsis a small event pump that serializes events through the engine and executes effects against a storage adapter; the nitroDownloadManagersits behind that adapter interfacecache-ledger-storage, registered inSTORAGE_SCHEMA_VERSIONSfrom day one)fetchingentries with no live task — the stuck-download state — are recoveredDownload pipeline hardening that rode along:
onDownloadStateChangeis now wired, so failures reach the ledger (withstorage_fulltriggering an emergency eviction pass) instead of disappearingonDownloadCompleteupserts by track id instead of blind-appending (no more duplicate entries on re-download)UI: the Storage screen's download settings gain the Smart Cache toggle, budget presets (1–32 GB), a usage line (
X of Y used · N cached · M downloaded), and the eviction confirmation card. Download quality settings are unchanged.Testing: 5 new suites / 66 tests (
jest/functional/Cache/) — scoring decay properties, eviction invariants (never evicts pins, never evicts the queue, deterministic plans, watermark hysteresis), reconciliation scenarios including stuck-state recovery, engine event scenarios, and the full service run against a fake adapter. Full app suite: 18 suites / 168 tests passing;tsc, ESLint, and Prettier clean.Deliberate v1 scope (happy to adjust in review):
unpinwhen the UI wants itWhat does this address
Big library on the server, limited space on the device. Manually curating downloads doesn't scale past a few hundred tracks, and the previous auto-download grew without bound and never cleaned up after itself. This also fixes the class of stuck-download state that previously survived restarts, and replaces the "Unused in 30+ days" guess in storage cleanup with real per-track play history (
lastPlayedAtis now actually tracked).Issue number / link
Tag reviewers
@anultravioletaurora
CI note:
run-jest-test-suite,build-bundle, andpublish-ota-updateare green. Themaestro-testsfailure is pre-existing — that workflow currently fails on every branch includingmain(instrumentation crash on the emulator; see the workflow's recent run history). Probably worth removing or repairingmaestro-test.ymlseparately now that the Jest suite covers CI.