# How WeekDone Syncs Offline-First Across Desktop, Web, and Mobile

- Canonical URL: https://blog.weekdone.app/offline-first-sqlite-sync
- Language: en
- Category: engineering
- Author: Gang Wang
- Published: 2026-11-29
- Updated: 2026-11-29

> TL;DR: Every WeekDone mutation writes locally first—to SQLite WAL on Desktop or IndexedDB/localStorage on Web—and syncs incrementally via monotonic server cursors over HTTPS + SSE without blocking offline work or overwriting local edits silently.

## Why build Local-First instead of a pure cloud web app?

When you press `⌘K` to capture an idea or click a checkbox in Darkroom, waiting 300ms for a remote API round-trip breaks focus. Worse, airplane mode or unstable hotel Wi-Fi turns cloud-only task apps into read-only placeholders.

WeekDone reverses the dependency: **local storage is the primary source of truth for the UI, and the Go backend acts as an asynchronous delta sync relay.**

## How does the 6-table SQLite WAL + Pull-Merge-Push protocol work?

On macOS and Windows (Tauri v2), the desktop host persists state across six core tables (`tasks`, `task_steps`, `pomodoro_logs`, `attachments`, `weekly_reviews`, `sync_meta`) in SQLite **WAL mode** (`PRAGMA journal_mode=WAL; PRAGMA synchronous=NORMAL;`).

Each sync cycle follows a predictable **Pull-Merge-Push** sequence:

1. **Pull**: Client calls `GET /api/v1/sync/pull?cursor={last_sync_cursor}&limit=100` to receive all records with `server_updated_at > cursor`.
2. **Merge & Conflict Safety**: Incoming server records update clean local entities. If a local entity is `dirty` and the server also changed it, the client writes a conflict copy (`conflicts/{uuid}_{ts}.json`) before adopting the server state.
3. **Push**: Client pushes remaining local `dirty` mutations via `POST /api/v1/sync/push` and advances its local `last_sync_cursor`.
4. **Live Wakeup**: Connected clients subscribe to Server-Sent Events (`GET /api/v1/sync/events`) to pull immediately when another device pushes changes.

```text
Device A (Offline Edit) ──▶ Local SQLite WAL (dirty)
                                  │
                          Network Restored
                                  ▼
      Pull (?cursor=41) ──▶ Merge + Dump Conflicts ──▶ Push Dirty Batch ──▶ SSE Broadcast to Device B
```

## How are attachments and user data secured?

| Layer | Mechanism | Purpose |
| :--- | :--- | :--- |
| Authentication | Google OAuth 2.0 (`openid email profile`) + HMAC-signed JWTs | 1-hour access token + 30-day refresh token |
| Envelope Encryption | Optional L2 **AES-256-GCM** per-user DEK wrapped by Master KEK | Protects synced task titles & notes at rest |
| Attachment Storage | Direct-to-R2 SigV4 presigned URLs + backend proxy fallback | Zero public bucket exposure; 100 MB free quota |
| Telemetry | Zero third-party ad or behavioral tracking SDKs | Complete user privacy across all platforms |

> [!IMPORTANT]
> If you delete your account (`DELETE /api/v1/auth/me`), WeekDone removes your account record, synced tasks, and cloud attachment metadata permanently while leaving your local offline workspace untouched until you choose to clear it.


## FAQ

### Can I use WeekDone completely offline without signing in?

Yes. All core features—The Stage, Garden, Darkroom timer, local attachments, and Energy Map—work offline without an account.

### How does WeekDone handle sync conflicts between two offline devices?

WeekDone uses a deterministic Server-Wins reconciliation strategy while dumping the overwritten local version to a local conflicts/{uuid}_{timestamp}.json file on Desktop so no edit is ever lost.

### How is cloud-synced data protected at rest?

Passwords use Argon2id/HMAC verification, and user task payloads support optional L2 AES-256-GCM envelope encryption with per-user Data Encryption Keys (DEKs).
