Skip to main content

agentmux_srv_core/backend/vault/
sync_grant.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! A site grant ("Always on this site" for an entry) as a sync record (docs/specs/SPEC_VAULT_SHARED_ACROSS_MACHINES_LAN_AND_CLOUD_2026_11_01.md
5//! section 6.6). Grants to an agent (`signin:<agent>`, a command's first-use grant) name an agent of one machine and
6//! never sync: this module has no way to build a record from one, and the capture writes only a grant whose key is a site.
7//!
8//! A grant holds no value, and two machines that make the same grant make the same record, so a grant has no keep-both:
9//! the later version, a grant or its revoking, is the one every machine follows.
10
11use serde::{Deserialize, Serialize};
12use sha2::{Digest, Sha256};
13
14use super::sync_merge::Decision;
15use super::sync_seal::Version;
16
17/// A site grant as a sync record carries it, sealed: which entry and which site stay out of the clear header.
18#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
19pub struct GrantBody {
20    /// The record the entry is written under (its link id), the same on every machine; not the id it has on one.
21    pub entry_id: String,
22    pub origin: String,
23    pub granted_at: i64,
24    /// When the entry was made. A grant only applies to the entry the person was asked about: a re-made entry of the same
25    /// name has the same id and a new `created_at`, and does not inherit the site (the same check the local store makes).
26    pub entry_created_at: i64,
27}
28
29/// Whether `id` has the shape of a grant's record id (`record_id`): `g-` and 32 lowercase hex characters.
30pub fn is_record_id(id: &str) -> bool {
31    id.strip_prefix("g-").is_some_and(|h| h.len() == 32 && h.bytes().all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)))
32}
33
34/// A grant's record id: `g-` and 32 hex characters of a hash over the entry and the site, so the id says neither and is
35/// the same on every machine for the same grant. Revoking is a tombstone of this id.
36pub fn record_id(entry_id: &str, origin: &str) -> String {
37    let mut h = Sha256::new();
38    for part in [entry_id, origin] {
39        h.update((part.len() as u64).to_le_bytes());
40        h.update(part.as_bytes());
41    }
42    let digest = h.finalize();
43    let hex: String = digest.iter().take(16).map(|b| format!("{b:02x}")).collect();
44    format!("g-{hex}")
45}
46
47impl GrantBody {
48    pub fn new(entry_id: &str, origin: &str, granted_at: i64, entry_created_at: i64) -> Self {
49        Self { entry_id: entry_id.to_string(), origin: origin.to_string(), granted_at, entry_created_at }
50    }
51
52    pub fn record_id(&self) -> String {
53        record_id(&self.entry_id, &self.origin)
54    }
55
56    /// Whether a machine whose copy of the entry was made at `local_created_at` (`None` if it has no such entry yet) may
57    /// apply this grant. With no entry yet, the grant waits until the entry arrives.
58    pub fn applies_to(&self, local_created_at: Option<i64>) -> bool {
59        local_created_at == Some(self.entry_created_at)
60    }
61}
62
63/// What to do with a grant or a revoke that arrives, given the current version this machine holds of that record: the
64/// later version wins, and a version this machine has had changes nothing.
65pub fn merge(current: Option<&Version>, had: &[Version], incoming: &Version) -> Decision {
66    if had.contains(incoming) {
67        return Decision::KeptLocal;
68    }
69    match current {
70        Some(c) if incoming <= c => Decision::KeptLocal,
71        _ => Decision::Applied,
72    }
73}
74
75#[cfg(test)]
76#[path = "sync_grant_tests.rs"]
77mod tests;