Skip to content

fix(app): stop auto-retrying a recording the server refuses for its audio - #14337

Merged
Git-on-my-level merged 1 commit into
mainfrom
fix/wal-sync-permanent-failure-retry-loop
Sep 17, 2026
Merged

Git-on-my-level merged 1 commit into
mainfrom
fix/wal-sync-permanent-failure-retry-loop

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Sep 17, 2026 •

Copy link
Copy Markdown
Member

What

A recording the server refuses because of its own audio now stops being auto-uploaded. It is retained on disk and presented in the sync UI as failed with a manual Retry, instead of being re-sent on every recovery wake forever.

Why (the measured loop)

An iOS client (1.0.551+1217) re-uploaded the same 2.2 KB WAL to POST /v2/sync-local-files 39 times in ~21 hours. Every attempt was accepted with 202, and every job then failed identically:

INFO:utils.sync.files:No PCM data was decoded.
WARNING:utils.sync.files:Sync decode dropped 1 of 1 unreadable files
ERROR:utils.sync.pipeline:event=sync_transcription_job outcome=invalid_input status=failed
  provider=unknown model=unknown lane=backfill reason_code=sync_invalid_audio

The backend is behaving correctly. decode_files_to_wav drops unreadable files individually and only raises once the batch has nothing left (backend/utils/sync/files.py:289); the pipeline turns that into a terminal failed with reason_code=sync_invalid_audio (backend/utils/sync/pipeline.py:1786-1800). A file that decodes to zero PCM will decode to zero PCM again — the verdict is final.

The client never read that. Root cause, on origin/main:

  1. syncJobTerminalPolicy (app/lib/services/wals/local_wal_sync.dart:42-45) returns retry for every terminal status but completed.
  2. reconcileUploadedWals (local_wal_sync.dart:1213-1233) therefore puts each member WAL back to WalStatus.miss and does w.retryCount += 1 (:1219).
  3. Nothing downstream consults retryCount. The automatic drain filters only on status == miss && storage == disk (local_wal_sync.dart:685, and the same predicate in the initial snapshot), so the WAL is immediately eligible again.
  4. RecordingTransferCoordinator._runPass reconciles and then drains in the same pass (recording_transfer_coordinator.dart:246 then :256), and re-arms on foreground, connectivity, device-connect, cooldown and startup. That is the loop that produced the 39 uploads — one upload per wake, indefinitely.
  5. walMaxAutoRetries existed but only coloured the row (wal.dart:52, read solely by syncDisplayState at :168). getOrphanedWals had its own hardcoded retryCount < 3, which no upload path went through.

app/test/unit/wal_upload_resilience_test.dart already carried a test literally named KNOWN GAP: syncAll picks up miss WAL regardless of retryCount, whose comment prescribed this fix.

Changes

Client-only. The backend already sends everything needed: reason_code is in the job-status payload (backend/routers/sync.py:1489) and SyncJobStatusResponse already parses it (app/lib/backend/schema/conversation.dart:823). No wire change, so old clients are unaffected.

  • Classify the verdict — syncJobFailureIsPermanent treats a whole-job failed carrying an input-caused reason (sync_invalid_audio, stt_invalid_input) as unchangeable by another upload. partial_failure is deliberately excluded: segments that landed prove the batch is not wholly bad. Combined with the decoder dropping unreadable files individually, a permanent verdict can never strand a good sibling — a 1-of-5 bad file still lets the other four through, and the job only fails when nothing decoded.
  • Spend the budget on that verdict instead of one attempt per pass, and gate every automatic loop on it: isAutoUploadEligible now applies walMaxAutoRetries in _syncAll and getOrphanedWals. So an unclassified future permanent failure also stops after the budget rather than looping — the bound does not depend on knowing the reason code.
  • Never drop audio. The recording stays miss on disk and renders as WalSyncDisplayState.failed, keeping the manual Retry and the swipe-to-delete the sync pages already offer. No new state was invented.
  • Manual retry still works. A deliberate single-recording retry restores the budget — including when SyncProvider.syncWal coalesces that tap into a running drain, which would otherwise have silently dropped it. One tap buys exactly one upload.
  • Transient failures are untouched: capacity/paced/backend-busy keep skipping the budget entirely, and upstream/timeout/decode-failed keep their per-attempt increment.

Tests

app/test/unit/capture_recovery_replay_scenarios_test.dart — two behavioural tests through the deterministic replay world (real LocalWalSyncImpl + RecordingTransferCoordinator, scripted upload boundary and clock):

  • the incident: a failed / sync_invalid_audio job, then a wake of every WakeTrigger. Against unpatched code this produced 7 uploads for 7 wakes; with the fix, 1. It also asserts the file is retained byte-identical, the row reads failed, and a manual retry still reaches the server exactly once.
  • a non-regression twin: stt_upstream_error keeps costing one attempt per pass and stays retrying.

app/test/unit/sync_job_terminal_policy_test.dart — classification unit tests (permanent vs transient vs partial_failure, and the budget predicate against syncDisplayState). These fail to compile against unpatched code.

app/test/unit/wal_upload_resilience_test.dart — the KNOWN GAP test is rewritten to assert the closed behaviour, plus a twin proving a WAL with budget left is still uploaded. Per the repo's rule on same-PR test rewrites: the external source is the test's own prescribed fix (its comment says "syncAll() should skip WALs with retryCount >= N") and the measured incident above.

Commands run:

  • flutter analyze on the touched files — no new diagnostics (9 pre-existing baseline infos on untouched lines).
  • flutter test test/unit/capture_recovery_replay_scenarios_test.dart test/unit/sync_job_terminal_policy_test.dart test/unit/local_wal_sync_test.dart test/unit/wal_sync_display_state_test.dart test/unit/wal_sync_upload_test.dart test/unit/wal_upload_resilience_test.dart test/unit/sync_response_handling_test.dart test/unit/wal_delete_cascade_test.dart test/unit/session_wal_sync_test.dart test/unit/sync_manifest_claim_drain_test.dart test/unit/wal_recovery_test.dart — 162 passed.
  • bash test.sh — 2031 passed, 1 failure in test/unit/search_rank_grouping_test.dart, which reproduces identically on unmodified origin/main (pre-existing, unrelated to this diff).

Not exercised on a device: this is the offline-sync policy seam, covered by the replay world. A live run would need an undecodable WAL on a real phone.

Risk

Low and bounded to the offline-upload drain.

  • The only behaviour that becomes less aggressive is automatic re-upload after three terminal job failures (or one input-caused one). Audio is never deleted and the row is never silently hidden — WalSyncDisplayState.failed is an existing state the sync pages already render with a Retry button.
  • retryCount is only incremented by genuine terminal job failures and job-not-found reverts; transient network/5xx paths never touch it, so a flaky connection cannot exhaust the budget.
  • The bulk "Sync" button now skips budget-exhausted rows; those rows keep their own per-row Retry, which restores the budget. Worth a look in review if you'd prefer bulk sync to also reset.

Failure-Class

Failure-Class: FC-permanent-write-rejection-retried-forever

The registry entry is written around the backend's Firestore instance, but the violated contract is the same one: a rejection no retry of the same payload can satisfy, replayed by an automatic sweep, leaving the row non-terminal for good. This is the second instance in this subsystem — #10995 fixed the 422 backfill_lookback_exceeded channel by adding outsideRecoveryWindow; this is the same class arriving through the async job's reason_code. Per the bug-fix guidance the reusable guard surface lands here rather than another call-site exception: one shared isAutoUploadEligible predicate that every automatic loop reads, and one classifier for terminal verdicts. A client-scoped class definition would describe it more precisely; adding a .github/failure-classes/ entry was out of scope for this PR.

Invariants

scripts/pr-preflight --suggest reports no affected product invariants for this diff.

🤖 Generated with Claude Code

Review in cubic

…udio

An iOS client re-uploaded the same 2.2 KB WAL to /v2/sync-local-files 39
times in ~21 hours. Every attempt was accepted with 202 and every job then
failed identically: the decoder produced no PCM, so decode_files_to_wav
raised and the pipeline finalized the job `failed` with
reason_code=sync_invalid_audio.

The client treated that verdict as retryable. syncJobTerminalPolicy returns
`retry` for every terminal status but `completed`, so reconcileUploadedWals
put each member WAL back to `miss` and bumped retryCount — and nothing
consulted retryCount afterwards. The automatic drain filtered only on
`status == miss && storage == disk`, so RecordingTransferCoordinator's
reconcile-then-drain pass re-sent the same bytes on every wake, forever.
walMaxAutoRetries existed but only coloured the sync row.

- Classify the failure: `syncJobFailureIsPermanent` treats a whole-job
  `failed` carrying an input-caused reason (sync_invalid_audio,
  stt_invalid_input) as unchangeable by another upload. `partial_failure`
  is excluded — segments that landed prove the batch is not wholly bad, and
  the backend drops individually unreadable files rather than failing their
  siblings, so a permanent verdict cannot strand a good recording.
- Spend the budget on that verdict instead of one attempt per pass, and
  gate every automatic loop on it: `isAutoUploadEligible` now applies
  walMaxAutoRetries in _syncAll and getOrphanedWals, so an unclassified
  permanent failure also stops after the budget rather than looping.
- Never drop audio: the recording stays `miss` on disk and renders as
  WalSyncDisplayState.failed, keeping its manual Retry and Delete. A
  deliberate single-recording retry restores the budget, including when
  SyncProvider coalesces that tap into a running drain.
- Transient failures (capacity, paced, backend-busy, upstream, timeout)
  keep their existing per-attempt accounting untouched.

Verified:
- flutter analyze on the touched files: no new diagnostics (9 pre-existing
  baseline infos on untouched lines).
- flutter test test/unit/capture_recovery_replay_scenarios_test.dart
  test/unit/sync_job_terminal_policy_test.dart
  test/unit/local_wal_sync_test.dart test/unit/wal_sync_display_state_test.dart
  test/unit/wal_sync_upload_test.dart test/unit/wal_upload_resilience_test.dart
  test/unit/sync_response_handling_test.dart test/unit/wal_delete_cascade_test.dart
  test/unit/session_wal_sync_test.dart test/unit/sync_manifest_claim_drain_test.dart
  test/unit/wal_recovery_test.dart — 162 passed.
- The new replay test reproduces the incident against unpatched code: 7
  wakes produced 7 uploads; with the fix, 1.
- bash test.sh — 2031 passed, 1 pre-existing failure
  (test/unit/search_rank_grouping_test.dart), reproduced on unmodified
  origin/main.

Not exercised on a device: this is the offline-sync policy seam, covered by
the deterministic replay world; a live run needs an undecodable WAL on a
real phone.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@Git-on-my-level
Git-on-my-level merged commit a42a474 into main Sep 17, 2026
41 checks passed
@Git-on-my-level
Git-on-my-level deleted the fix/wal-sync-permanent-failure-retry-loop branch September 17, 2026 11:30
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.

1 participant