V3 S6 : confiance (révocation, quota, appareils, conflits) + import + onboarding
Backend : token_version (000022, claim tv, 401 si révoqué, bump au password — fin limite V1), quota (000023 idempotente, colonne héritée de 000001, 413 QUOTA_EXCEEDED, GET /users/me/usage), devices (GET + DELETE révocation, EnforceDeviceExists en prod), renames concurrents transparents (baseUpdatedAt + conflicts[] non-fataux, tolérance 2 s). Tests service + handlers + migrations + auth. Mobile : pipeline conflits (DTO + SyncConflictStore + worker + dialogue), baseUpdatedAt prêt, liens 7j par défaut, écran Appareils (+ quota), import classeur unifié (scan/note/dossier), onboarding cercle Famille. Contrat docs/api-v1.md, V3.md (S6 livré).
This commit is contained in:
+80
-13
@@ -53,10 +53,22 @@ type FailedOperation struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
// SyncResult is the outbox response: applied = index of the next op to send.
|
||||
// ConflictInfo reports a last-writer-wins rename applied over a concurrent
|
||||
// change (V3 S6 A4). Non-fatal : le batch continue, le client affiche
|
||||
// « renommé ailleurs » avec le nom serveur gagnant.
|
||||
type ConflictInfo struct {
|
||||
OperationID string `json:"operation_id"`
|
||||
ResourceID string `json:"resource_id"`
|
||||
ServerName string `json:"serverName"`
|
||||
ServerUpdatedAt int64 `json:"serverUpdatedAt"`
|
||||
}
|
||||
|
||||
// SyncResult is the outbox response: applied = index of the next op to send,
|
||||
// conflicts = renames concurrents appliqués quand même (non-fataux, S6 A4).
|
||||
type SyncResult struct {
|
||||
Applied int `json:"applied"`
|
||||
Failed *FailedOperation `json:"failed,omitempty"`
|
||||
Applied int `json:"applied"`
|
||||
Failed *FailedOperation `json:"failed,omitempty"`
|
||||
Conflicts []ConflictInfo `json:"conflicts,omitempty"`
|
||||
}
|
||||
|
||||
type createResourcePayload struct {
|
||||
@@ -71,6 +83,11 @@ type createResourcePayload struct {
|
||||
|
||||
type renamePayload struct {
|
||||
Name string `json:"name"`
|
||||
// V3 S6 A4 : `updated_at` (ms epoch) vu par le client au moment du
|
||||
// rename. Si la ressource a bougé depuis (concurrent), le rename
|
||||
// s'applique quand même (last-writer-wins) mais est signalé en
|
||||
// `conflicts` (transparence, pas de CRDT).
|
||||
BaseUpdatedAt *int64 `json:"baseUpdatedAt"`
|
||||
}
|
||||
|
||||
type movePayload struct {
|
||||
@@ -156,33 +173,39 @@ var errCycle = errors.New("cannot move a resource into its own subtree")
|
||||
// explicite avec code INTERNAL, jamais en erreur Go nue (500 nu) — le client
|
||||
// sait exactement où reprendre.
|
||||
func (s *Resources) ApplyBatch(userID, deviceID string, ops []SyncOperation) (SyncResult, error) {
|
||||
// Conflits de rename (S6 A4) : non-fataux, collectés tout au long du
|
||||
// batch et renvoyés avec le résultat (le batch continue).
|
||||
var conflicts []ConflictInfo
|
||||
fail := func(i int, op *SyncOperation, code, message string) (SyncResult, error) {
|
||||
return SyncResult{Applied: i, Failed: &FailedOperation{OperationID: op.OperationID, Code: code, Message: message}, Conflicts: conflicts}, nil
|
||||
}
|
||||
for i := range ops {
|
||||
op := &ops[i]
|
||||
if err := validateSyncOp(op); err != nil {
|
||||
return SyncResult{Applied: i, Failed: &FailedOperation{OperationID: op.OperationID, Code: "INVALID_REQUEST", Message: err.Error()}}, nil
|
||||
return fail(i, op, "INVALID_REQUEST", err.Error())
|
||||
}
|
||||
claimed, err := s.Repository.Operations.Claim(deviceID, op.OperationID, op.Operation, derefString(op.RefType), nullableInt64(derefInt64(op.RefID)), derefString(op.ResourceID), op.Payload)
|
||||
if err != nil {
|
||||
// Faute client (clé ou payload malformé) → INVALID_REQUEST halt,
|
||||
// message statique sûr ; panne persistance → INTERNAL explicite.
|
||||
if errors.Is(err, repository.ErrInvalidOperationID) {
|
||||
return SyncResult{Applied: i, Failed: &FailedOperation{OperationID: op.OperationID, Code: "INVALID_REQUEST", Message: "operation_id must be 32 lowercase hex chars"}}, nil
|
||||
return fail(i, op, "INVALID_REQUEST", "operation_id must be 32 lowercase hex chars")
|
||||
}
|
||||
if errors.Is(err, repository.ErrInvalidPayload) {
|
||||
return SyncResult{Applied: i, Failed: &FailedOperation{OperationID: op.OperationID, Code: "INVALID_REQUEST", Message: "invalid payload JSON"}}, nil
|
||||
return fail(i, op, "INVALID_REQUEST", "invalid payload JSON")
|
||||
}
|
||||
log.Printf("sync: claim device=%s op=%s: %v", deviceID, op.OperationID, err)
|
||||
return SyncResult{Applied: i, Failed: &FailedOperation{OperationID: op.OperationID, Code: "INTERNAL", Message: "failed to persist operation state"}}, nil
|
||||
return fail(i, op, "INTERNAL", "failed to persist operation state")
|
||||
}
|
||||
if !claimed {
|
||||
// Rejeu : même (device, op) déjà tracé — comparer (A9).
|
||||
stored, err := s.Repository.Operations.Get(deviceID, op.OperationID)
|
||||
if err != nil {
|
||||
log.Printf("sync: replay read device=%s op=%s: %v", deviceID, op.OperationID, err)
|
||||
return SyncResult{Applied: i, Failed: &FailedOperation{OperationID: op.OperationID, Code: "INTERNAL", Message: "failed to read operation state"}}, nil
|
||||
return fail(i, op, "INTERNAL", "failed to read operation state")
|
||||
}
|
||||
if err := stored.MatchesReplay(op.Operation, derefString(op.ResourceID), op.Payload); err != nil {
|
||||
return SyncResult{Applied: i, Failed: &FailedOperation{OperationID: op.OperationID, Code: "INVALID_REQUEST", Message: "operation replayed with different payload"}}, nil
|
||||
return fail(i, op, "INVALID_REQUEST", "operation replayed with different payload")
|
||||
}
|
||||
if stored.Status == repository.OpStatusApplied {
|
||||
continue // déjà appliquée à l'identique → skip idempotent
|
||||
@@ -190,16 +213,18 @@ func (s *Resources) ApplyBatch(userID, deviceID string, ops []SyncOperation) (Sy
|
||||
// Trace 'failed' à payload identique : la première tentative est
|
||||
// morte avant MarkApplied (crash) → on ré-applique (recovery).
|
||||
}
|
||||
if err := s.applySyncOp(userID, deviceID, op); err != nil {
|
||||
if conflict, err := s.applySyncOpWithConflict(userID, deviceID, op); err != nil {
|
||||
code, message := classifySyncError(err)
|
||||
return SyncResult{Applied: i, Failed: &FailedOperation{OperationID: op.OperationID, Code: code, Message: message}}, nil
|
||||
return fail(i, op, code, message)
|
||||
} else if conflict != nil {
|
||||
conflicts = append(conflicts, *conflict)
|
||||
}
|
||||
if err := s.Repository.Operations.MarkApplied(deviceID, op.OperationID); err != nil {
|
||||
log.Printf("sync: promote device=%s op=%s: %v", deviceID, op.OperationID, err)
|
||||
return SyncResult{Applied: i, Failed: &FailedOperation{OperationID: op.OperationID, Code: "INTERNAL", Message: "operation applied but trace update failed; safe to retry"}}, nil
|
||||
return fail(i, op, "INTERNAL", "operation applied but trace update failed; safe to retry")
|
||||
}
|
||||
}
|
||||
return SyncResult{Applied: len(ops)}, nil
|
||||
return SyncResult{Applied: len(ops), Conflicts: conflicts}, nil
|
||||
}
|
||||
|
||||
func validateSyncOp(op *SyncOperation) error {
|
||||
@@ -226,6 +251,48 @@ func validateSyncOp(op *SyncOperation) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// conflictToleranceMs absorbe le skew d'horloge client/serveur ET la
|
||||
// troncation à la seconde des `updatedAt` RFC3339 lus par le client : un
|
||||
// écart ≤ 2 s n'est pas un conflit (ex. create suivi d'un rename immédiat
|
||||
// par le même device). Au-delà, quelqu'un d'autre a écrit entre-temps.
|
||||
const conflictToleranceMs = 2000
|
||||
|
||||
// applySyncOpWithConflict applique une op et détecte les renames concurrents
|
||||
// (S6 A4) : si `baseUpdatedAt` est fourni et que la ressource a bougé depuis
|
||||
// (au-delà de la tolérance), le rename s'applique quand même (LWW) mais est
|
||||
// signalé. Sans base (vieux clients) : aucun signalement, comportement V1.
|
||||
func (s *Resources) applySyncOpWithConflict(ownerID, deviceID string, op *SyncOperation) (*ConflictInfo, error) {
|
||||
var base *int64
|
||||
resID := derefString(op.ResourceID)
|
||||
if op.Operation == OpUpdateMetadata {
|
||||
var p renamePayload
|
||||
if err := json.Unmarshal(op.Payload, &p); err == nil && p.BaseUpdatedAt != nil {
|
||||
base = p.BaseUpdatedAt
|
||||
}
|
||||
}
|
||||
var preMs int64
|
||||
havePre := false
|
||||
if base != nil {
|
||||
if _, ms, err := s.Repo.LiveNameAndUpdatedAt(resID); err == nil {
|
||||
preMs, havePre = ms, true
|
||||
}
|
||||
}
|
||||
if err := s.applySyncOp(ownerID, deviceID, op); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if base != nil && havePre && preMs > *base+conflictToleranceMs {
|
||||
name, ms, err := s.Repo.LiveNameAndUpdatedAt(resID)
|
||||
if err != nil {
|
||||
return nil, nil // renommé puis supprimé dans le même batch : rien à signaler.
|
||||
}
|
||||
return &ConflictInfo{
|
||||
OperationID: op.OperationID, ResourceID: resID,
|
||||
ServerName: name, ServerUpdatedAt: ms,
|
||||
}, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *Resources) applySyncOp(ownerID, deviceID string, op *SyncOperation) error {
|
||||
resourceID := derefString(op.ResourceID)
|
||||
switch op.Operation {
|
||||
|
||||
Reference in New Issue
Block a user