Skip to content

[issue-217] Precache libdedx WASM + ASR worker after model download; offline SW cache fallback - #218

Open
grzanka wants to merge 2 commits into
mainfrom
issue-217-precache-offline-assets
Open

[issue-217] Precache libdedx WASM + ASR worker after model download; offline SW cache fallback#218
grzanka wants to merge 2 commits into
mainfrom
issue-217-precache-offline-assets

Conversation

@grzanka

@grzanka grzanka commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

What changed

Fixes #217 (verifying/refining #163 §5.4): the "download models" consent
flow never touched wasm/libdedx.mjs or the ASR worker script — both are
dynamic-loaded lazily, only on a query's first use — so a user who went
offline right after finishing that flow hit an uncached fetch and failed,
even though they'd just been told they were ready.

Implements both parts of the issue's suggested fix:

  1. Precache the compute/ASR code paths as part of the download flow.
    model-status.svelte.ts's startDownload() now awaits getService()
    (libdedx WASM) and calls a new asrStatus.prewarm() (fire-and-forget,
    reuses the same singleton worker client start() later uses) right
    after downloadModelWeights() succeeds, before flipping to "ready".
    Failures from either are logged only, never fatal — the model weights
    are already safely cached at that point, so a WASM/ASR hiccup shouldn't
    revert the whole flow to "fresh" and force a re-download.

  2. Give coi-serviceworker.js's fetch handler a caches.match()
    fallback.
    Every successful same-origin GET response it proxies is now
    also written into a new aidedx-coi-runtime-v1 Cache Storage bucket. On
    a failed live fetch (offline), it now checks Cache Storage — every
    bucket, so it also benefits from transformers.js's own
    "transformers-cache" entries for ONNX Runtime Web's wasm/mjs — before
    re-throwing. Kept same-origin-only so it never duplicates the
    multi-hundred-MB cross-origin model weight downloads (Cyfronet S3
    mirror) into a second cache.

Test plan

  • pnpm run format:check — scoped to the changed files; a repo-wide
    run trips on pre-existing untracked .android-asr-cache/ and
    .venv-asr-bench/ directories unrelated to this change (confirmed
    the same failure exists on main)
  • pnpm run lint — scoped to the changed files, same pre-existing
    caveat
  • pnpm run check — 0 errors
  • pnpm run validate:eval
  • pnpm test — 982 passed, including new coverage for
    asrStatus.prewarm() and modelStatus's post-download precache
    (success path + both failure paths staying non-fatal)
  • pnpm build
  • Live-browser re-verification of the literal §5.4 repro (download
    models → disconnect networking → no prior online query → submit a
    query) — not done as part of this PR; see [issue-163] §5.4: offline claim fails on first use — libdedx WASM + ASR worker never precached #217's own repro steps

🤖 Generated with Claude Code

…offline SW cache fallback

Fixes the offline claim failing on first use (#217, verifying/refining #163
§5.4): `wasm/libdedx.mjs` and the ASR worker script are both dynamic-loaded
lazily on first query, so the "download models" consent flow never fetched
(or cached) either one — a user who went straight offline after finishing
that flow hit an uncached dynamic import/Worker fetch and failed, even
though they'd just been told they were ready.

- `asr-status.svelte.ts`: add `prewarm()`, a fire-and-forget call that
  triggers the ASR worker's pipeline load via the same singleton worker
  client `start()` uses, so a later real transcription reuses it instead of
  spinning up a second worker.
- `model-status.svelte.ts`: after `downloadModelWeights()` succeeds, await
  `getService()` (libdedx WASM) and call `asrStatus.prewarm()` before
  flipping to "ready". Both failures are logged, not fatal — the model
  weights are already safely cached at that point, so a WASM/ASR hiccup
  here shouldn't revert the whole flow to "fresh".
- `static/coi-serviceworker.js`: give the vendored fetch handler a Cache
  Storage fallback. Successful same-origin GET responses it proxies are
  written into a new `aidedx-coi-runtime-v1` bucket; a failed live fetch
  (offline) now checks Cache Storage (every bucket, so it also picks up
  transformers.js's own "transformers-cache" entries for ONNX Runtime
  Web's wasm/mjs) before re-throwing. Kept same-origin-only so it never
  duplicates the multi-hundred-MB cross-origin model weight downloads.

Test plan:
- [x] pnpm run format:check (scoped to changed files — repo-wide run trips
      on pre-existing untracked `.android-asr-cache/`/`.venv-asr-bench/`
      directories unrelated to this change)
- [x] pnpm run lint (scoped to changed files, same pre-existing caveat)
- [x] pnpm run check
- [x] pnpm run validate:eval
- [x] pnpm test (982 passed, incl. new precache/prewarm coverage)
- [x] pnpm build

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses issue #217 by making the “download models” consent flow actually prepare the app for offline first use: it proactively loads the libdedx WASM compute path and warms the ASR worker, and it enhances the COI service worker to persist and serve cached responses when live fetches fail.

Changes:

  • Add a runtime Cache Storage bucket + offline fallback behavior to coi-serviceworker.js for proxied fetches.
  • After downloadModelWeights() succeeds, precache libdedx (getService()) and prewarm ASR (asrStatus.prewarm()) before marking the model state as "ready" (non-fatal on failure).
  • Add Vitest coverage for the new post-download precache behavior and the ASR prewarm() path.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
static/coi-serviceworker.js Adds runtime caching for successful GETs and a cache fallback on fetch failure to improve offline resilience.
src/lib/models/model-status.svelte.ts Preloads compute WASM and warms ASR right after a successful model download to support offline-first-use.
src/lib/models/model-status.test.ts Tests that post-download precaching occurs and that precache failures remain non-fatal.
src/lib/asr/asr-status.svelte.ts Adds prewarm() to trigger worker pipeline load without changing ASR phase/recording state.
src/lib/asr/asr-status.test.ts Tests prewarm() behavior and verifies the warmed worker client is reused by start()/stop().

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread static/coi-serviceworker.js Outdated
…T, not just same-origin

Addresses Copilot review feedback on #218: the read-side fallback in
coi-serviceworker.js's catch block was gated by the same isCacheableGet
flag as the write side, so it only ever ran for same-origin GETs. That
meant a cross-origin GET (the Cyfronet S3 model-weight mirror in
particular) could never fall back to Cache Storage on a failed live fetch,
even though transformers.js's own "transformers-cache" bucket might
already hold a cached response for it — contradicting both the inline
comment and the PR description's "search every bucket" claim.

Split the single flag into `isSameOriginGet` (still gates the cache
*write*, so this SW never duplicates the multi-hundred-MB cross-origin
weight downloads into a second bucket) and `isGet` (gates the *read*
fallback, any origin).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
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.

[issue-163] §5.4: offline claim fails on first use — libdedx WASM + ASR worker never precached

2 participants