Skip to content

feat(mobile): add versioned offline sync for subscription edits - #2321

Merged
github-actions[bot] merged 1 commit into
Smartdevs17:mainfrom
tesleem-28:feat/offline-subscription-sync-1987
Nov 24, 2026
Merged

github-actions[bot] merged 1 commit into
Smartdevs17:mainfrom
tesleem-28:feat/offline-subscription-sync-1987

Conversation

@tesleem-28

@tesleem-28 tesleem-28 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds the client-side core for offline-first subscription edits under mobile/src/sync/:

  • A persisted outbox. Each edit carries the server version it was made against (baseVersion).
  • A conflict policy: versioned optimistic concurrency with a server-authoritative winner. A version mismatch holds the local edit for the user to discard or re-apply. Nothing is overwritten silently.
  • A push engine: FIFO, single-flight, timeouts, bounded retries, and per-record holds, so a conflict or rejection does not block other records.
  • Identifier-only telemetry and warn logs for user-visible sync state.
  • Tests and mobile/src/sync/README.md.

The change is dependency-free. It reuses isRetryableQueryError, queryErrorMessage, SubscriptionListItem and QueryHttpError from mobile/src/queries/.

What breaks if this is wrong

  • Server contract. The engine assumes the server returns a per-record version, applies a write only when baseVersion matches, and answers a mismatch with a conflict payload. The backend has none of this today. Against the current server, every push fails, and edits stay queued. Nothing is lost, but nothing syncs.
  • Storage must throw. asyncStorageAdapter in src/utils/storage.ts returns null on read failure. Wiring it in as-is would make an unreadable outbox look empty, and the next write would overwrite it. The README calls this out. A throwing adapter is needed before wiring.
  • Rebase and coalescing. Pending edits for one record merge and keep the earliest base version. After an applied push, same-base entries are rebased to the applied version. If the server version semantics differ from "each applied write advances the version by at least one", the rebase is wrong.
  • Head-of-line blocking. A transient failure pauses the whole queue in FIFO order. This is deliberate and simple, but a single stuck record delays all other records until it succeeds or is held after maxAttempts.
  • In-flight restart. The in-flight marker is not persisted. If the app dies after the server applies a write and before the ack is stored, the same edit is re-pushed. The server then reports a conflict against the same content, and the user decides.

Scope

Implemented (under mobile/src/sync/):

  • types.ts, conflictPolicy.ts, outbox.ts, engine.ts, index.ts, README.md
  • Tests: __tests__/conflictPolicy.test.ts, __tests__/outbox.test.ts, __tests__/engine.test.ts

Acceptance criteria:

  • Implementation under the technical scope path with no unrelated churn: met. Only new files under mobile/src/sync/.
  • Tests pin every documented behaviour: written, not executed. See Verification.
  • Observability hooks where user-visible: telemetry events and logger.warn are emitted from the engine. There is no UI, so engine.status() and subscribe() are the only user-facing surface.
  • Unit tests pass locally and in CI: not verified. Tests were not run.
  • Documentation: mobile/src/sync/README.md added. docs/API.md is not updated, because there is no API for this yet. docs/offline-architecture.md is not updated. It describes a different design (a field-level LWW CRDT in src/services/cache/crdt.ts and src/store/subscriptionStore.ts), and reconciling the two is a follow-up.

Not implemented, which remains for follow-up work:

  • The HTTP write adapter. createHttpSubscriptionApi in mobile/src/queries/subscriptions.ts is GET-only.
  • The server version and 409 conflict contract. Backend subscription mutation handlers have no per-record version.
  • Wiring to network status and app lifecycle. The useOfflineSync hook lives in src/hooks/, outside mobile/, and is not connected here.
  • Conflict-resolution UI.
  • A throwing SyncStorage adapter (see above).
  • mobile/package.json is not present in this repository, so no dependency manifest was checked for mobile/. The new code imports nothing outside existing modules.

The acceptance criteria are not all met. The remaining items are listed above.

Verification

Verified by reading only:

  • Every import resolves to an existing export: ../queries/queryClient (isRetryableQueryError, queryErrorMessage) and ../queries/subscriptions (SubscriptionListItem, QueryHttpError).
  • Every method called on SyncOutbox, SyncEngine, and SyncTransport matches its declared signature.
  • Each test was traced against the implementation, including the in-flight coalescing and rebase ordering, the conflict and protocol-violation paths, the timeout abort path, and the single-flight flush().
  • Repo conventions followed: Jest __tests__ layout matched by the root jest.config.js testMatch, and Prettier settings from the repo .prettierrc (printWidth 100, single quotes, trailing commas).
  • Safety screen: the root package.json has only "prepare": "husky" as a lifecycle script, with no install-time download or execution. The changed files contain no eval or base64-decoded code. None of the flagged file names (temp_auto_push.bat, config.bat, dist/setup.js) exist in the repository.

Not run:

  • Jest, tsc, ESLint, Prettier, the app, and CI. The branch was created from upstream main at 091a12368e2936377b8eb87586104cd84dc382d9. Upstream moved one commit ahead of the fork's main (9d75e5488af0802bc85010987c68a787987ad762) during the run. That commit touches only backend/ and workers/ billingShift files, not mobile/.

Closes #1987

- Add the mobile/src/sync outbox, conflict policy and push engine. Reason: issue Smartdevs17#1987 requires subscription edits made offline to survive restarts and be reconciled without silent overwrite. Consequence: each queued edit carries the server version it was made against and is applied only while that version is still current.
- Use versioned optimistic concurrency with a server-authoritative winner for conflicts. Reason: device-clock last-writer-wins is unreliable after offline periods and drops the other device's change without a trace. Consequence: a version mismatch holds the local edit for the user to discard or re-apply, and edits to different fields of one record also conflict when the version moved.
- Persist the outbox as one JSON document with copy-on-write commits. Reason: a failed write must not leave memory and storage disagreeing. Consequence: unreadable stored data throws OutboxCorruptError and is never overwritten.
- Retry transient failures using the existing isRetryableQueryError, and hold rejected or exhausted edits. Reason: one bad record must not block the queue indefinitely. Consequence: a transient failure pauses the queue in FIFO order, while a conflict or rejection affects only its own record.
- Emit identifier-only sync telemetry and warn logs. Reason: sync failures are user-visible and need observability without leaking subscription values. Consequence: patch values never appear in telemetry or logs.
- Add unit tests for the policy, outbox persistence and rebase rules, and engine failure paths. Consequence: these tests were verified by reading only, not run.
- Add mobile/src/sync/README.md covering the policy, behaviour, and what is not implemented.

Not included: the HTTP write adapter, the server version and 409 contract, network and lifecycle wiring, and conflict-resolution UI. See the README for details.

Refs Smartdevs17#1987
@drips-wave

drips-wave Bot commented Nov 24, 2026

Copy link
Copy Markdown

@tesleem-28 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@github-actions
github-actions Bot merged commit 9b45699 into Smartdevs17:main Nov 24, 2026
2 checks passed
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.

Implement offline-first subscription edits with conflict-aware sync

1 participant