# Season sync and encrypted backup

The repository's architectural decision, rationale and consequences.

# Season sync and encrypted backup

The account stays optional (ADR-0016). Signing in from the mobile settings (Better Auth, the template's auth screens) enables two things, each behind its own switch: **Back up my Archive** and **Show my season on the web**. Both are off after sign-in; nothing leaves the device until the Athlete turns one on.

**Season sync** sends only public plan data to Postgres: planned event id, checklist ticks, the latest Estimate (range, currency, date) and nights. It lands in `season_plan`, a tenant table (`pgTable.withRLS`, one row per User, Organization and event, readable only by its owner) behind the API feature `season` (`season.upsertPlans`, `season.list`, `season.clear`; Effect per ADR-0010). The web season dashboard (US-38) reads `season.list`. The mobile serializer accepts only the public plan type, so a Break, injury or weight cannot be passed to it without a type error (ADR-0017, type-level test).

**Backup** is one encrypted blob per User in R2 (EU jurisdiction, ADR-0018), under `backups/<user>/`, outside the Organization prefixes the files gateway serves. The device encrypts it before upload: AES-256-GCM (`expo-crypto`) with a random data key kept in the device keychain; that key is also wrapped with a key derived from a 128-bit recovery code that is shown once and never stored by us. Restoring on a new phone needs the code. The blob goes up through a presigned PUT from the storage platform (`DirectStorage.signPut`, behind the database-backed session) and comes back for a restore as ciphertext through `backup.download`; the server never sees plaintext or any key. API feature `backup`: `prepare`, `commit`, `latest`, `download`, `remove`. Each upload uses a new object key; after it lands the server deletes the older ones, so one backup remains.

**Delete my data** wipes the local Archive, its key, the backup key and the certificate photos. When signed in it first deletes the User's `season_plan` rows and backup objects, then the account through Better Auth; the UI promises the backup is gone within 30 days, which also covers provider-side copies.

Chosen over syncing the whole Archive (private records would need per-field rules and a server copy of health-adjacent data) and over server-side encryption (the server would hold the key). The cost: a lost recovery code with a lost phone means the backup cannot be read.

## Consequences [#consequences]

- An account deleted from the web does not yet delete the backup objects; a sweep of `backups/<user>/` for deleted Users is a follow-up.
- Photos are not in the backup (RF08); they are in the full export only.

## Amendments [#amendments]

- 2026-11-16 (US-38 review, owner decision): the season sync also carries the Athlete's home city (name, latitude, longitude; the academy's city when the catalogue knows it, else the departure city they picked) and the Estimate's per-night items (hotel and food, low/central/high per night). ADR-0017 makes only Breaks, injuries and weight private, so neither is a private record; both are public plan data. The home is stored on every `season_plan` row of the User (column `home`), and the latest sync writes all rows. The web uses the synced home for distances and keeps a city typed in the browser only as a fallback when nothing is synced.

