fix(sync): never keep a local tag version on pull

The server has no updatedAt on tags and sends updated_at = created_at, so a
local tag rename always looked newer than the server row: after a sign-out /
sign-in, a stale phone rename overwrote a later web rename. Tags now always
take the server version on pull (an offline tag rename is lost, as before),
locked by a smoke source check. Lists and tasks keep the keepLocal rule.

Refs #149

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
le king fu 2026-10-24 17:15:28 -04:00
commit aaa8cc2a27
3 changed files with 20 additions and 9 deletions

View file

@ -110,7 +110,7 @@ Chaque règle cite sa source. Elles valent quel que soit le niveau : une PR norm
- `initialReset` efface toutes les données locales avant de tirer celles du serveur : un appel ajouté se justifie, et il reste derrière une confirmation de l'utilisateur. — `src/services/syncClient.ts`, `app/(tabs)/settings.tsx`
- Une suppression côté serveur est un soft-delete (`deletedAt`), relayé à l'app par le pull. — `web/src/app/api/sync/route.ts`
- Le pull garde le `createdAt` local d'une ligne existante, et le `calendarEventId` d'une tâche (colonne absente du serveur). — `src/services/syncRules.ts` (`pulled*Values`), #149
- Dans un pull depuis `SYNC_EPOCH` (première sync, ou après une déconnexion qui remet `lastSyncAt` à null), une ligne locale strictement plus récente que le serveur est gardée et remise dans l'outbox (`writeOutboxEntry`, action `update`). — `src/services/syncRules.ts` (`keepLocal`), `src/services/syncClient.ts`, #149
- Dans un pull depuis `SYNC_EPOCH` (première sync, ou après une déconnexion qui remet `lastSyncAt` à null), une liste ou une tâche locale strictement plus récente que le serveur est gardée et remise dans l'outbox (`writeOutboxEntry`, action `update`). Jamais une étiquette : le serveur n'a pas d'`updatedAt` sur les étiquettes et renvoie `updated_at` = `created_at`, donc un renommage hors connexion d'étiquette est perdu (check smoke « applyTagChange never keeps a local tag »). — `src/services/syncRules.ts` (`keepLocal`), `src/services/syncClient.ts`, #149
- Une suppression serveur s'applique toujours, même sur une ligne locale plus récente : le protocole n'a pas de restauration. — `src/services/syncClient.ts`, #149
- `src/services/syncRules.ts` reste sans import, pour que `tests/syncRules.test.mjs` le charge par le type stripping de Node (check smoke `syncRules.ts stays import-free`). — `tests/smoke.test.cjs`, `tests/syncRules.test.mjs`, #149
- Après un push ou un pull qui applique des changements, le widget est rafraîchi (`syncWidgetData`). — PR #66, `src/services/syncClient.ts`

View file

@ -14,7 +14,6 @@ import {
pulledTagValues,
listUpdatePayload,
taskUpdatePayload,
tagUpdatePayload,
} from '@/src/services/syncRules';
const SYNC_API_BASE = 'https://liste.lacompagniemaximus.com';
@ -167,7 +166,7 @@ async function applyChange(change: SyncPullChange, since: string): Promise<void>
await applyTaskChange(entity_id, action, payload, since);
break;
case 'tag':
await applyTagChange(entity_id, action, payload, since);
await applyTagChange(entity_id, action, payload);
break;
case 'task_tag':
await applyTaskTagChange(entity_id, action, payload);
@ -219,7 +218,12 @@ async function applyTaskChange(id: string, action: string, payload: Record<strin
}
}
async function applyTagChange(id: string, action: string, payload: Record<string, unknown>, since: string) {
// Tags never keep a local version (no keepLocal): the server has no updatedAt
// on tags and sends updated_at = created_at, which never moves on a rename.
// Comparing against it would let a stale phone rename overwrite a later web
// rename after a sign-out / sign-in. An offline tag rename is lost, as before
// #149 (decision of Max, 2026-10-24, review of PR #157).
async function applyTagChange(id: string, action: string, payload: Record<string, unknown>) {
if (action === 'delete') {
await db.delete(taskTags).where(eq(taskTags.tagId, id));
await db.delete(tags).where(eq(tags.id, id));
@ -227,11 +231,6 @@ async function applyTagChange(id: string, action: string, payload: Record<string
}
const [local] = await db.select().from(tags).where(eq(tags.id, id));
if (keepLocal(local, payload, since)) {
// Newer offline edit: keep it and push it back to the server
await writeOutboxEntry('tag', id, 'update', tagUpdatePayload(local));
return;
}
const values = pulledTagValues(id, payload, local);
if (local) {

View file

@ -181,6 +181,18 @@ check('syncRules.ts stays import-free (protects type-stripping test path)', () =
);
});
// Tags must not go through keepLocal: the server's tag updated_at is its
// created_at (no updatedAt column), so a local rename would always look newer
// (PR #157 review). syncClient.ts cannot load under node, hence a source check.
check('applyTagChange never keeps a local tag (no keepLocal)', () => {
const src = fs.readFileSync(path.join(__dirname, '..', 'src/services/syncClient.ts'), 'utf8');
const start = src.indexOf('async function applyTagChange(');
assert.ok(start >= 0, 'applyTagChange not found in syncClient.ts');
const end = src.indexOf('\nasync function ', start + 1);
const body = src.slice(start, end === -1 ? undefined : end);
assert.ok(!/keepLocal\s*\(/.test(body), 'applyTagChange must not call keepLocal');
});
// --- Build toolchain guards ---
// A blanket `npm audit fix` (PR #118) once rewrote hundreds of lock entries:
// babel-preset-expo and @expo/metro-config lost their root hoisting and a