Skip to content

feat(sync): automatic offline batch uploads on stable network (#4822) - #17656

Open
TheRWX wants to merge 1 commit into
BasedHardware:mainfrom
TheRWX:feat/offline-sync-batch-uploads-4822
Open

TheRWX wants to merge 1 commit into
BasedHardware:mainfrom
TheRWX:feat/offline-sync-batch-uploads-4822

Conversation

@TheRWX

@TheRWX TheRWX commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #4822

Implements automatic offline batch upload resumption upon stable network connection with failsafe file preservation, ensuring that offline/batch recordings captured on Android and mobile devices are reliably drained without data loss or permanent retry lockouts.

Key Changes

  1. Network Connectivity Gating:
    • Updated canAutoUploadPhoneRecordings in app/lib/utils/batch_recording.dart with hasNetwork gating, ensuring automatic upload attempts only begin when active network connectivity is established.
    • Connected ConnectivityService().isConnected check in LocalRecordingsProvider._maybeAutoUpload() to avoid attempting uploads into disconnected networks.
  2. Transient Network Error Handling & Disconnection Resilience:
    • Added LocalUploadOutcome.networkDisconnected to LocalUploadOutcome in app/lib/providers/local_recordings_provider.dart.
    • Updated _uploadFile to catch transient network failures (isTransientNetworkError(e)) and classify them as networkDisconnected.
    • Updated _recordUploadOutcome: transient network drops preserve the file on disk without incrementing the permanent per-file failure cap (autoPhoneUploadMaxFailures).
    • Extended recording_detail_sheet.dart to handle LocalUploadOutcome.networkDisconnected with noInternetConnection user feedback.
  3. Automatic Batch Upload on Reconnect:
    • Added _onNetworkRestored() in LocalRecordingsProvider triggered by ConnectivityService().onConnectionChange.
    • When a stable network is detected (connected == true), clears transient failure backoff (_autoFailures.clear()) and triggers _maybeAutoUpload() to automatically drain queued recordings.
    • Added resetAutoPhoneUploadFailures() in batch_recording.dart to support deterministic reset across test and runtime surfaces.
  4. Failsafe File Preservation:
    • Ensured that local files are deleted strictly after confirmed server completion (result.completed != null or reconciler job completed).
    • On network drop or disconnect, the file remains 100% intact on disk in pending state, ready for automatic batch upload when connectivity resumes.
  5. Hermetic Testing:
    • Added comprehensive test coverage in app/test/unit/local_recordings_auto_upload_test.dart for hasNetwork gating, failure reset on reconnect, and sequential disconnect-then-reconnect simulation with failsafe file preservation.

Verification & Testing

  • Unit Tests (test/unit/local_recordings_auto_upload_test.dart):
    • canAutoUploadPhoneRecordings blocks when hasNetwork is false: PASSED
    • canAutoUploadPhoneRecordings allows when hasNetwork is true: PASSED
    • resetAutoPhoneUploadFailures clears all failure counts to restore eligibility: PASSED
    • network restoration and failsafe batch upload simulation: transient disconnect preserves file and reconnect drains preserved files: PASSED
    • All 18 tests in local_recordings_auto_upload_test.dart: PASSED
  • Related Test Suites:
    • test/unit/local_recordings_owner_test.dart: 5/5 PASSED
    • test/services/wals/recording_transfer_coordinator_test.dart: 21/21 PASSED
    • Combined 44/44 tests passed cleanly.
  • Static Analysis & Formatting:
    • dart analyze: No issues found (0 errors).
    • dart format: 0 changed.

Failure-Class: none
PayPal: [email protected]

Review in cubic

…ardware#4822)

Implements automatic offline batch upload resumption upon stable network connection with failsafe file preservation, ensuring that offline/batch recordings captured on Android and mobile devices are reliably drained without data loss or permanent retry lockouts.

Closes BasedHardware#4822

Failure-Class: none

PayPal: [email protected]
@Git-on-my-level

Copy link
Copy Markdown
Member

Thanks @TheRWX — this addresses the core of #4822 (drain offline batch recordings once connectivity returns, without data loss), and it reuses the right primitives rather than inventing new ones. I reviewed all four changed files against main.

app/lib/utils/batch_recording.dart

  • canAutoUploadPhoneRecordings gaining an optional hasNetwork (default true) is backward-compatible, and the single production call site in _maybeAutoUpload now passes ConnectivityService().isConnected explicitly. Clean gate composition.
  • resetAutoPhoneUploadFailures() is only referenced from the test file — LocalRecordingsProvider._onNetworkRestored() clears _autoFailures directly. Consider routing the runtime path through it or dropping it from lib/; a test-only helper in production code adds surface without behavior.

app/lib/providers/local_recordings_provider.dart

  • The transient-error classification is genuinely reachable: I traced SyncUploadGate.upload → uploadLocalFilesV2 → makeMultipartApiCall, and makeMultipartApiCall rethrows network errors after logging (backend/http/shared.dart), so the networkDisconnected branch is live rather than dead code.
  • _recordUploadOutcome's early return for networkDisconnected correctly keeps disconnects out of the per-file failure cap, and deletion in _uploadFile still happens only on server-confirmed completion — the failsafe ordering the issue asked for is preserved.
  • The three new @visibleForTesting hooks (onNetworkRestoredForTesting, maybeAutoUploadForTesting, autoFailuresForTesting) are not used by any test, even though they are exactly what a provider-level reconnect test would need.
  • One conscious decision to make: _onNetworkRestored() clears all of _autoFailures, including counts accumulated for non-network failures (e.g. repeated 400s). Any blip-then-restore cycle wipes that backoff. The backoff is in-memory-only, so this is low-stakes, but it should be intentional.

app/test/unit/local_recordings_auto_upload_test.dart

  • The hasNetwork gate tests are direct and useful.
  • The "failsafe simulation" re-implements the drain loop with local sets, so expect(preservedOnDisk.contains(auto1), isTrue) asserts the test's own bookkeeping rather than provider behavior — the preservation property actually lives in _uploadFile's result.completed path, which the simulation never exercises. Since the visibleForTesting hooks are already added, a provider-level test that stubs the uploader to throw a SocketException, calls maybeAutoUploadForTesting(), then onNetworkRestoredForTesting() and asserts the drain would verify the central claim for real. Tagging needs-tests for that.

app/lib/pages/conversations/recording_detail/recording_detail_sheet.dart

  • Exhaustive switch extended with the new enum case; the noInternetConnection key already exists in the l10n catalog, so no missing-translation risk.

One product question for maintainers: the issue asks for uploads on a "stable" network. ConnectivityService.onConnectionChange fires once per verified reachability transition, so every reconnect edge — even on a flappy connection — immediately starts a drain pass. The pass self-throttles (it breaks on the first networkDisconnected outcome and disconnects don't count against the cap), so this is battery/traffic churn rather than a correctness problem, but whether "stable" should mean a short debounced window is a product call I'm leaving for maintainer review together with merge sign-off.

Nice work keeping the change tight and reusing isTransientNetworkError instead of adding new error-sniffing.

—
Automated maintainer-assist review on behalf of the Omi maintainers; merge sign-off is left to a human maintainer.


by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.

@Git-on-my-level Git-on-my-level added needs-tests PR introduces logic that should be covered by tests flutter flutter work labels Sep 23, 2026

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Feature (automatic offline batch upload resumption) — approve only per policy; DIRTY, needs-tests label.

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Feature (auto-resume offline batch uploads on reconnect) — approve-only per policy.

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Feature-shaped implementation (titled feat, new auto-resume capability) referencing #4822; ambiguous bug/feature treated as feature per policy default — approve-only. needs-tests label, mergeStateStatus DIRTY, not merging.

@TheRWX

TheRWX commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the approval. This branch now conflicts with main. Happy to rebase it onto current main to clear the conflicts (no behavior changes, tests re-run) — want me to go ahead?

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Feature (automatic offline batch upload resumption) — approve-only per policy; DIRTY so not mergeable regardless.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

flutter flutter work needs-tests PR introduces logic that should be covered by tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Better offline sync: automatic batch uploads when there is a stable network

3 participants