Sync Engine
The Sync Engine (lib/sync-engine.ts) receives delta payloads from the Convex backend and atomically upserts them into the local SQLite cache.Delta Payload
Freshness Guard
The Freshness Guard prevents background sync from overwriting fresh user edits with stale cloud data. It operates at two levels: Task-level: Compares updated_at timestamps. If local is newer, the task AND its child instances are skipped. Instance-level: Checks the is_manual_edit flag. If the local instance has been manually edited but the incoming payload does not reflect that, the incoming data is discarded.Atomic Transaction
All writes are wrapped in db.withTransactionAsync(). The sync token is written outside the transaction, after success. If the transaction fails, the token is not updated, guaranteeing at-least-once delivery.Corruption Recovery
If the transaction throws malformed or disk I/O errors, the engine:- Purges all data records
- Clears the sync token
- Alerts the user
- Force-exits the app
- On next launch, Amnesia Mode triggers a full re-sync