Skip to content

iOS: a stopped model download resumes from NSURLSession's resume data (#127) - #132

Merged
juancguerrerodev merged 1 commit into
mainfrom
dbejarano820/127-ios-download-resume
Nov 27, 2026
Merged

juancguerrerodev merged 1 commit into
mainfrom
dbejarano820/127-ios-download-resume

Conversation

@dbejarano820

Copy link
Copy Markdown
Contributor

What and why

Closes #127

On iOS a stopped model download started again from zero: expo-file-system resumes only from the base64 resume data NSURLSession produces on a pause (it rejects Android's byte offset), and .part does not exist on iOS until the file is complete. Now the download screen pauses the running download when the app goes to the background or the connection drops, the resume data is saved in <name>.part.resume, and the next attempt resumes from it. A missing, stale or rejected record means one clean restart. Android keeps its byte offset path, and the SHA-256 check on the finished file is unchanged.

Vision fit

The model comes down once over HTTPS and is checked before use, so the phone can work offline. A download that keeps its place on a phone that drops Wi-Fi or leaves the app makes that first step work on iPhones, and keeps the screen's promise ("If it drops, it picks up where it stopped").

Non-negotiables

  • This keeps every non-negotiable in AGENTS.md as it is.
  • Or it changes one, and this is the accepted RFC: #

Checklist

  • It closes one issue, linked above, or it's a small fix (a typo, a broken link, a wrong comment). A feature has an accepted proposal.
  • I wrote the failing test first. It fails without this change and passes with it.
  • These pass: pnpm check:privacy, pnpm typecheck, pnpm lint, pnpm test and node scripts/guard/check.mjs --patterns scripts/guard/patterns.txt.
  • No new network call, telemetry, analytics, ads or crash reporting. (A new request in an app adapter is explained below, with its host, skill and risk.)
  • No new dependency, or the reason for each one is below.
  • Every UI string is in packages/copy, in es-419 and en, with matching keys and placeholders, and no em or en dashes.
  • Tests, fixtures and screenshots use made-up data only.

How it works:

  • apps/mobile/src/download-resume.ts (pure, download-resume.test.ts): resumePlan() picks the path (Android: range from .part; iOS: the saved resume data, or straight to the check when the whole file is already in .part; otherwise clean), resumeRecordText() / readResumeRecord() write and check the record (URL, base64 resume data, bytes so far; a record for another URL, malformed, not base64, an unknown version or longer than the model is ignored), resumableBytes() is what the screen and the update read as kept, and pausesFor() says only iOS pauses, on background (not inactive) or when the connection drops.
  • adapters/device.ts: phoneDownload() follows the plan. The record is used once and deleted before a resume; a resumed task that pauses again saves a fresh one. If NSURLSession rejects the resume data, the same call restarts clean from zero. pauseDownloadWhenLeaving() pauses the running task on iOS; it is a no-op on Android.
  • app/download.tsx subscribes to it while the screen is open. A pause lands as the existing interrupted state, and the existing return and reconnect handlers resume it.
  • adapters/model-update.ts reads the iOS saved bytes for download_failed and recovery, and deletes the record with the staged file.

Known limit: expo-file-system does not hand back resume data when a task fails (only when it is paused), so a failure the app did not pause for (for example a server error) still restarts from zero on iOS. Getting that data would need a Swift change to expo-file-system, which this pull request does not make.

Test-first: download-resume.test.ts was run before download-resume.ts existed and failed with ERR_MODULE_NOT_FOUND; with the module it passes, 9 of 9.

Model path

  • This doesn't touch the model path: packages/model, agent, skills or eval, the prompt, the router, tool descriptions, normalizers, examples or the model file.
  • It does. The eval numbers from before and after are below, run as docs/evals.md says. No held-out case was tuned on.

Screenshots

  • No UI change.
  • UI change. Each changed screen is below, in both themes and both languages, with made-up data.

No copy changed: the screen's lines about keeping progress are now true on iOS for the pauses above.

New dependencies and network calls

n/a. No new dependency. The download is still the one request to the pinned model host (huggingface.co), counted once per attempt through countOutbound in downloadModel() as before; a resume continues that same download.

AI agents

  • No AI agent wrote any part of this.
  • An AI agent helped. Which one, and what it did: Claude Code (Claude Opus 5.5) read the expo-file-system iOS and JS source, wrote the test first, then the pure module and the adapter and screen wiring, and ran every gate. It did not run the change on an iPhone; that check is still to do.
  • I read and ran everything in this pull request, and I can explain it.

…#127)

On iOS, expo-file-system resumes only from the base64 resume data NSURLSession
produces on a pause, and .part does not exist until the file is complete, so
every stopped download started again from zero.

- download-resume.ts (pure, tested): which path each platform takes, the record
  kept next to .part, and when to fall back to a clean restart.
- The download screen pauses the running download on iOS when the app goes to
  the background or the connection drops; the pause saves the resume data in
  <name>.part.resume, and the next attempt resumes from it. A missing, stale or
  rejected record means one clean restart.
- partialModel and the model update read the saved bytes on iOS.
- Android keeps its byte offset path. The SHA-256 check is unchanged.

Signed-off-by: Daniel Bejarano <dbejarano820@gmail.com>
@juancguerrerodev
juancguerrerodev marked this pull request as ready for review November 27, 2026 19:44
@juancguerrerodev
juancguerrerodev self-requested a review as a code owner November 27, 2026 19:44
@juancguerrerodev
juancguerrerodev force-pushed the dbejarano820/127-ios-download-resume branch from 50fbb0d to c8485fc Compare November 27, 2026 19:45
@juancguerrerodev
juancguerrerodev merged commit c8485fc into main Nov 27, 2026
4 checks passed
@juancguerrerodev
juancguerrerodev deleted the dbejarano820/127-ios-download-resume branch November 27, 2026 19:50
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.

iOS: resume a stopped model download instead of starting from zero

2 participants