# External downloader integration

The shared `Aria2RpcClient` implements the documented HTTP JSON-RPC protocol:
token authorization, caller-selected GIDs, URI admission, bounded batched progress
queries, pause/resume/cancel and removal of completed results. It validates response
identity, bounds streamed responses to 1 MiB, preserves cancellation, rejects header
injection and unsafe output filenames, and never issues global queue commands.
Remote error messages are not exposed, since they can contain source URLs/secrets.
Six shared protocol tests passed on desktop and Kotlin/Wasm, covering request
options, identity matching, batched errors, bounded responses and cancellation.
The adapter compiled for Android and iOS alongside those targets.

## User behavior

Settings → Downloads provides the preferred downloader, aria2 JSON-RPC URL, secret
token, remote directory and a connection test. Manual manga and direct-video
downloads use aria2 when selected. Explicit local-folder requests, novels and HLS
playlists keep using the built-in downloader. Download-ahead and automatic release
downloads also remain in managed local storage. Image processing settings apply to
the built-in downloader.

Remote jobs appear in both Downloads settings and the download manager, with file
progress, their selectable server directory, pause/resume/cancel, refresh, retry and
receipt removal. Removing a receipt requires confirmation and neither cancels a
remote transfer nor deletes server files. Remote completion never writes a local
archive path or download row, so downloaded-only views and local cleanup stay correct.

## Recovery and ownership

Each file has a profile-owned GID, persisted and flushed before RPC submission.
The intermediate `Submitting` state survives a lost reply or process restart.
Recovery queries that same GID; missing/unverified work requires attention and is
never blindly admitted again. A user retry verifies the old task is stopped before
creating a new GID. Identical resource URLs/headers can resume their existing output;
changed resources receive fresh filenames to avoid mixing an old partial file with
new content. Old partial files can remain on the server.

Jobs retain the title/source/chapter identity so restoring a library with reused
database IDs cannot redirect an old retry to unrelated content. The endpoint and a
SHA-256 credential binding prevent old controls from targeting a different account.
Restoring the original account settings permits reconciliation. Rotated credentials
are treated as a new account binding; old receipts can be removed locally while the
server's own management interface remains available.

All work uses captured profile HTTP/scope and raw RPC requests. Retirement joins
source/network work; it leaves already accepted server transfers running. Source
URLs and headers are cleared after acceptance. RPC secrets, jobs and operational
credentials are removed from portable backups, including sensitive exports. The
preferences and receipts are excluded from sync; restoring a backup preserves the
current installation's feature settings.

## Performance and limits

The queue observes configuration changes without comparing the potentially large
job payload. Accepted remote work is polled only while the queue is observed;
pending admissions and control requests continue independently. The UI observes
with lifecycle awareness. Idle queues issue no requests, and progress persistence
does not wake another poll. Connection failures back off to at most one retry per
minute. RPC timeouts become ordinary failures, preserving the queue owner's job.

Each pass visits up to four jobs, queries at most 64 file statuses per job in one
batch, and admits at most four new files per job. Progress-only checkpoints are
limited to once per 15 seconds; admission/state transitions are durable. Queue
limits are 100 unresolved jobs, 2,000 files per chapter and a 2 MiB serialized receipt
budget, retaining up to 155 finished receipts alongside active work. Settings
read/transform/write is batched so simultaneous UI edits cannot overwrite receipts.

Integration checks passed for durable admission, serialized restart,
ambiguous acknowledgements, missing tasks, source changes, pause/resume/cancel,
safe retries, profile retirement/isolation, private backup/sync settings, manual
routing and reactive polling. The final broader run passed 270 desktop tests and
60 Kotlin/Wasm browser tests, with compilation on Android, iOS, desktop and web.

The HTTP API is the equivalent integration for Android, iOS, desktop and web. Users
provide their own aria2 server and token. Web deployments require a browser-reachable
HTTPS endpoint and appropriate CORS configuration. No external account or live aria2
server has been supplied or used for live verification. Protocol behavior is
verified against deterministic RPC fixtures; no external account verification is
implied.

Protocol reference: [official aria2 RPC manual](https://aria2.github.io/manual/en/html/aria2c.html#rpc-interface).
