agentmux_srv_core/backend/user_sync/adapter.rs
1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3//
4//! What a kind of collection says about merging (SPEC_USER_DATA_SYNC_LAYER ยง3.3): the layer calls its adapter
5//! with the record's current header and the incoming one, and the adapter answers. The layer never decides what a
6//! conflict means.
7
8use super::record::Header;
9
10/// An adapter's answer for an incoming record.
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub enum Merge {
13 /// Take the incoming record as the record's current state.
14 Applied,
15 /// Keep what is here.
16 KeptLocal,
17 /// Neither wins: both rows stay in the log for the person (a Vault's secrets), and the record's current state
18 /// is the side the adapter names, so every machine shows the same one as current.
19 Conflict { current: Side },
20}
21
22/// Which side of a conflict is the record's current state.
23#[derive(Debug, Clone, Copy, PartialEq, Eq)]
24pub enum Side {
25 Local,
26 Incoming,
27}
28
29/// One kind of collection's merge rule.
30pub trait Adapter {
31 fn merge(&self, local: Option<&Header>, incoming: &Header) -> Merge;
32}
33
34/// The newest version wins, for a record whose whole content is one value.
35pub struct NewestWins;
36
37impl Adapter for NewestWins {
38 fn merge(&self, local: Option<&Header>, incoming: &Header) -> Merge {
39 match local {
40 Some(l) if l.version >= incoming.version => Merge::KeptLocal,
41 _ => Merge::Applied,
42 }
43 }
44}