# Download automation

Shared controls live under Settings → Downloads on Android, iOS, desktop and web.
All new automatic actions default off. Existing delete-after-reading preferences
are honored with the new protections.

## Behavior

- Library updates can queue newly discovered releases. Include/exclude categories,
  unread-only and filler rules apply; exclusions win. Existing queue rows, including
  stopped, failed and paused rows, are preserved. Release priority and duplicate
  selection match the reader.
- Opening a chapter/episode can download a configurable number of upcoming unread
  releases, independently for manga, anime and novels. Existing download rows count
  toward the window. Incognito sessions do not download ahead.
- Cleanup after consumption and cleanup after a manual read mark are independent.
  Completion is explicitly reported by each reader/player; a progress save on an
  already-read chapter does not invent a new completion event.
- Cleanup can retain the latest N eligible read releases and exclude categories.
  Bookmarks are protected by default. Local-library originals and open chapters
  are always protected. Continuous readers retain leases for earlier visible
  chapters until their stream closes. Queued/running/paused transfers are not
  removed by automatic cleanup.
- Preview cleanup lists read downloads before explicit deletion. Confirmation
  authorizes only the previewed IDs. Read state, retention, bookmarks, category
  exclusions, active readers and task state are checked again before deletion.

Automatic requests use the existing persistent queue, destination, Wi-Fi policy,
source concurrency, retry/recovery and speed controls. Anime resolves video
streams with source/video headers; novels store cleaned chapter HTML. Readers
prefer downloaded images, video and novel HTML before source extraction.

## Recovery and file safety

New-release IDs are recorded in the same database batch as the updater's chapter
changes. Queue admissions precede receipt acknowledgement; interrupted handoffs
can replay without duplicating persisted queue rows. Receipts are acknowledged in
chunks. Fixing destination preferences triggers another attempt after admission
failure. Consumption receipts exist only for downloads subject to enabled cleanup
and are removed after cleanup; ordinary reading with cleanup off adds no ledger.

Managed download deletion runs outside the UI thread. It rejects local originals,
malformed root/title paths, traversal and redirected chapter paths. A task is
reserved against retry through both file deletion and the database commit.
Manual removal joins a transfer before deleting its files. Combined library/download removal awaits filesystem work before deleting database parents; failures preserve titles for retry. Failure/cancellation
preserves database markers for retry. Profile retirement joins owned cleanup and
pending filesystem writes before closing storage.

New settings are nested under `features` because `DbSettings` already reaches the
JVM constructor/copy method parameter limit. Released settings and the prerelease
flat `home` field remain readable. Credentials and automatic-work receipts are
excluded from exported backups. Feature preferences remain installation-local
through hub sync and profile restore, avoiding activation on another device.

## Browser storage

Web downloads use the origin-private filesystem (OPFS). The bridge streams chunks
with backpressure and keeps metadata indexes in memory; downloaded binaries are
not stored in localStorage. Legacy backup storage/listings remain compatible, and deleting a closed profile removes its OPFS files while preserving explicit backup exports. The additional-profile HTTP transport streams through a bounded channel rather than buffering a response or imposing the old 64 MiB body cap. Browser
file preparation finishes before bootstrap or opening another profile.

A service worker serves local media with byte ranges and relative HLS resources.
Downloaded HTML receives a sandbox CSP. This requires a secure context and a
browser supporting OPFS and service workers. Source requests remain subject to
the browser's normal CORS/header restrictions and the player's codec support.
Browser archive import/CBZ creation and broader local-library management are
tracked separately in the parity ledger.

API references: [OPFS](https://developer.mozilla.org/en-US/docs/Web/API/File_System_API/Origin_private_file_system),
[writable file streams](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileHandle/createWritable).

## Verification

- Shared policy, automation, updater, filesystem failure/path safety, offline
  content, settings migration, backup/sync isolation and profile lifecycle tests.
- Kotlin/Wasm OPFS interop tests (stream/append/promote/read/bounds and interrupted
  writes, legacy backups and profile deletion), plus streaming HTTP tests above 64 MiB with backpressure and cancellation checks. Asynchronous failure propagation was verified using a temporary negative control. Use `--tests '*BrowserStorageInteropTest*'`; JS filters include methods.
  Empty suites now fail instead of producing a misleading green result.
- `scripts/test-browser-storage.mjs` exercises real Chrome OPFS: 12 MiB streaming,
  reload recovery, partial resume, byte ranges, protected deletion, path/profile
  isolation and downloaded-HTML sandbox headers. It accepts an optional path to
  an installed `playwright-core` module.

Final target checks and any remaining limitations are recorded in
`../parity/NON_UI_IMPLEMENTATION.md`.
