feat(sync): F2 — origin-gated conflict-detection

Closes the false-positive conflict-toast loop on history-replay. Conflict
notifications now fire only when the local field meta records origin='user'
AND the pull is not an initial hydration round.

Origin source-of-truth:
- shared-ai/field-meta.ts → originFromActor(actor) maps actor.kind onto
  the FieldOrigin enum: user→'user', ai→'agent', system+SYSTEM_MIGRATION
  →'migration', any other system source→'system'.
- Dexie creating/updating hooks call it once per write so every persisted
  field carries the right pipeline tag.
- repair-silent-twin + legacy-avatar wrap their writes in
  runAsAsync(makeSystemActor(SYSTEM_MIGRATION, ...)) so the hook stamps
  origin='migration'. Future replays of those rows from another device
  will not surface as conflicts.

applyServerChanges options:
- New ApplyServerChangesOptions { isInitialHydration?: boolean }.
- Push-response and pull-paged-loop callers compute it from the cursor
  state (`!oldestCursor` / `!cursor`). Pagination resets the flag after
  the first page.
- Conflict-trigger gates on `!options.isInitialHydration && localMeta[k]
  ?.origin === 'user'` in addition to the prior tests.

Tests (sync.test.ts):
- New: replay-burst (10 sequential server updates → 0 conflicts)
- New: agent-origin local write + server overwrite → 0 conflicts
- New: isInitialHydration suppresses everything → 0 conflicts
- New: real user edit + server overwrite → 1 conflict
- All 25 prior tests still pass.

29/29 vitest sync.test.ts cases green; svelte-check 0 errors over 7647
files.

Plan: docs/plans/sync-field-meta-overhaul.md F2 done-criteria met.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Till JS 2026-04-26 21:38:56 +02:00
parent 6c942e3ab2
commit ad5e04a554
9 changed files with 279 additions and 50 deletions

View file

@ -23,6 +23,7 @@
*/
import type { Actor } from './actor';
import { SYSTEM_MIGRATION } from './actor';
/**
* Pipeline that produced a given field value, from the perspective of
@ -67,3 +68,23 @@ export function makeFieldMeta(at: string, actor: Actor, origin: FieldOrigin): Fi
export function isUserOriginatedField(meta: FieldMeta | undefined): boolean {
return meta?.origin === 'user';
}
/**
* Map an actor onto the pipeline-origin we stamp at the Dexie hook.
*
* Rules:
* - `kind: 'user'` `'user'`
* - `kind: 'ai'` `'agent'`
* - `kind: 'system'` with `principalId === SYSTEM_MIGRATION` `'migration'`
* - any other system source `'system'`
*
* Server-replay is set explicitly by `applyServerChanges`, never derived
* here the local hook only sees writes the local code initiated.
*/
export function originFromActor(actor: Actor): FieldOrigin {
if (actor.kind === 'ai') return 'agent';
if (actor.kind === 'system') {
return actor.principalId === SYSTEM_MIGRATION ? 'migration' : 'system';
}
return 'user';
}

View file

@ -38,7 +38,7 @@ export {
} from './actor';
export type { FieldMeta, FieldOrigin } from './field-meta';
export { makeFieldMeta, isUserOriginatedField } from './field-meta';
export { makeFieldMeta, isUserOriginatedField, originFromActor } from './field-meta';
export type {
IterationPhase,