agentmux_srv_core/backend/vault/sync_activity.rs
1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The Vault's activity as sync records (docs/specs/SPEC_VAULT_SHARED_ACROSS_MACHINES_LAN_AND_CLOUD_2026_11_01.md
5//! section 6.7): append-only per machine, so lines from different machines never conflict.
6//!
7//! A line is named by its machine and its sequence number there, and never changes. A machine keeps its own hash chain
8//! over its own lines (`storage::vault::activity_hash`); the lines of other machines are kept beside them, without that
9//! chain, and shown with a Machine column. A line holds no value, but its label and target are sealed like any record.
10
11use serde::{Deserialize, Serialize};
12
13use super::sync_merge::Decision;
14use crate::backend::storage::vault::VaultActivityRow;
15
16/// An activity line as a sync record carries it. It leaves out what is the machine's own bookkeeping: the order in its
17/// table and its place in its hash chain.
18#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
19pub struct ActivityBody {
20 pub at: i64,
21 /// In a record, the record the entry is written under (its link id), the same on every machine; in a row, the id the entry
22 /// has here.
23 pub entry_id: String,
24 pub entry_label: String,
25 pub actor: String,
26 pub action: String,
27 pub target: String,
28 pub outcome: String,
29 pub version: i64,
30 pub actor_kind: String,
31 pub host: String,
32 pub reason: String,
33 pub refs: String,
34}
35
36impl ActivityBody {
37 pub fn of_row(r: &VaultActivityRow) -> Self {
38 Self {
39 at: r.at,
40 entry_id: r.entry_id.clone(),
41 entry_label: r.entry_label.clone(),
42 actor: r.actor.clone(),
43 action: r.action.clone(),
44 target: r.target.clone(),
45 outcome: r.outcome.clone(),
46 version: r.version,
47 actor_kind: r.actor_kind.clone(),
48 host: r.host.clone(),
49 reason: r.reason.clone(),
50 refs: r.refs.clone(),
51 }
52 }
53
54 /// The row a machine shows for a line another machine made. It has no place in this machine's chain: `seq` is the
55 /// sequence number on the machine that made it, and the hashes are empty.
56 pub fn into_row(&self, seq: i64) -> VaultActivityRow {
57 VaultActivityRow {
58 seq,
59 at: self.at,
60 entry_id: self.entry_id.clone(),
61 entry_label: self.entry_label.clone(),
62 actor: self.actor.clone(),
63 action: self.action.clone(),
64 target: self.target.clone(),
65 outcome: self.outcome.clone(),
66 version: self.version,
67 actor_kind: self.actor_kind.clone(),
68 host: self.host.clone(),
69 reason: self.reason.clone(),
70 refs: self.refs.clone(),
71 prev_hash: String::new(),
72 hash: String::new(),
73 }
74 }
75}
76
77/// The id of the line `seq` made on `machine`: `a.<machine>.<seq>`, letters, digits and `._-` as a sealed record's id may
78/// hold. The sequence has no dot, so the id reads back one way only.
79pub fn record_id(machine: &str, seq: i64) -> String {
80 format!("a.{machine}.{seq}")
81}
82
83/// The machine and sequence an activity record id names, or `None` for any other id.
84pub fn parse_record_id(id: &str) -> Option<(&str, i64)> {
85 let rest = id.strip_prefix("a.")?;
86 let (machine, seq) = rest.rsplit_once('.')?;
87 let seq: i64 = seq.parse().ok().filter(|n| *n > 0)?;
88 (!machine.is_empty()).then_some((machine, seq))
89}
90
91/// What to do with a line that arrives: a line this machine already has changes nothing, and any other is added. A line
92/// never conflicts, because no two machines make the same (machine, sequence).
93pub fn merge(already_have: bool) -> Decision {
94 if already_have {
95 Decision::KeptLocal
96 } else {
97 Decision::Applied
98 }
99}
100
101#[cfg(test)]
102#[path = "sync_activity_tests.rs"]
103mod tests;