x0x 0.17.2

Agent-to-agent gossip network for AI systems — no winners, no losers, just cooperation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! Trust evaluation for (identity, machine) pairs.
//!
//! The `TrustEvaluator` combines an agent's trust level with its
//! identity type and machine records to produce a `TrustDecision`.
//!
//! # Machine Pinning
//!
//! When an agent's identity type is `Pinned`, only messages
//! originating from machine IDs that appear in the contact's machine list with
//! `pinned: true` are accepted. Any other machine identity results in
//! `TrustDecision::RejectMachineMismatch`.
//!
//! # Trust Decision Flow
//!
//! ```text
//! blocked?       → RejectBlocked
//! pinned + wrong machine → RejectMachineMismatch
//! pinned + right machine → Accept
//! Trusted level  → Accept
//! Known level    → AcceptWithFlag
//! Unknown level  → Unknown
//! ```
//!
//! # Example
//!
//! ```rust
//! use x0x::trust::{TrustContext, TrustDecision, TrustEvaluator};
//! use x0x::contacts::ContactStore;
//! use std::path::PathBuf;
//!
//! let store = ContactStore::new(PathBuf::from("/tmp/test-contacts.json"));
//! let evaluator = TrustEvaluator::new(&store);
//! ```

use crate::contacts::{ContactStore, IdentityType, TrustLevel};
use crate::identity::{AgentId, MachineId};

/// The outcome of a trust evaluation for a `(AgentId, MachineId)` pair.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]
#[serde(rename_all = "snake_case")]
pub enum TrustDecision {
    /// Accept the message — identity and machine are trusted.
    Accept,
    /// Accept but flag — identity is known/trusted, but machine is not pinned
    /// (or we have no machine constraint for this contact).
    AcceptWithFlag,
    /// Reject — the contact is pinned to specific machines and this one is not in the list.
    RejectMachineMismatch,
    /// Reject — the identity is explicitly blocked.
    RejectBlocked,
    /// Unknown sender — deliver with an unknown tag; the consumer decides.
    Unknown,
}

impl std::fmt::Display for TrustDecision {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Accept => write!(f, "accept"),
            Self::AcceptWithFlag => write!(f, "accept_with_flag"),
            Self::RejectMachineMismatch => write!(f, "reject_machine_mismatch"),
            Self::RejectBlocked => write!(f, "reject_blocked"),
            Self::Unknown => write!(f, "unknown"),
        }
    }
}

/// Context for a trust evaluation.
///
/// Carries the agent and machine identities extracted from an incoming
/// [`crate::IdentityAnnouncement`] or message.
#[derive(Debug, Clone, Copy)]
pub struct TrustContext<'a> {
    /// The portable agent identity of the sender.
    pub agent_id: &'a AgentId,
    /// The machine identity of the sending daemon.
    pub machine_id: &'a MachineId,
}

/// Evaluates trust for `(AgentId, MachineId)` pairs against a `ContactStore`.
///
/// The evaluator is cheap to construct — it borrows the store for the duration
/// of the evaluation.
pub struct TrustEvaluator<'a> {
    store: &'a ContactStore,
}

impl<'a> TrustEvaluator<'a> {
    /// Create a new evaluator backed by the given contact store.
    #[must_use]
    pub fn new(store: &'a ContactStore) -> Self {
        Self { store }
    }

    /// Evaluate trust for the given `(agent_id, machine_id)` pair.
    ///
    /// # Decision Rules
    ///
    /// 1. If the agent is blocked → [`TrustDecision::RejectBlocked`]
    /// 2. If `IdentityType::Pinned` and machine is NOT in the pinned list
    ///    → [`TrustDecision::RejectMachineMismatch`]
    /// 3. If `IdentityType::Pinned` and machine IS in the pinned list
    ///    → [`TrustDecision::Accept`]
    /// 4. If `TrustLevel::Trusted` → [`TrustDecision::Accept`]
    /// 5. If `TrustLevel::Known` → [`TrustDecision::AcceptWithFlag`]
    /// 6. Agent not in contact store → [`TrustDecision::Unknown`]
    pub fn evaluate(&self, ctx: &TrustContext<'_>) -> TrustDecision {
        let contact = match self.store.get(ctx.agent_id) {
            Some(c) => c,
            None => return TrustDecision::Unknown,
        };

        // Rule 1: blocked
        if contact.trust_level == TrustLevel::Blocked {
            return TrustDecision::RejectBlocked;
        }

        // Rules 2-3: machine pinning
        if contact.identity_type == IdentityType::Pinned {
            let is_pinned_machine = contact
                .machines
                .iter()
                .any(|m| m.machine_id == *ctx.machine_id && m.pinned);

            if is_pinned_machine {
                return TrustDecision::Accept;
            } else {
                return TrustDecision::RejectMachineMismatch;
            }
        }

        // Rule 4: trusted
        if contact.trust_level == TrustLevel::Trusted {
            return TrustDecision::Accept;
        }

        // Rule 5: known
        if contact.trust_level == TrustLevel::Known {
            return TrustDecision::AcceptWithFlag;
        }

        // Rule 6: unknown trust level (shouldn't reach here since Unknown is default,
        // but handle it for completeness)
        TrustDecision::Unknown
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::contacts::{Contact, ContactStore, IdentityType, MachineRecord, TrustLevel};
    use crate::identity::{AgentKeypair, MachineKeypair};

    fn agent_id() -> AgentId {
        AgentKeypair::generate().expect("keygen").agent_id()
    }

    fn machine_id() -> MachineId {
        MachineKeypair::generate().expect("keygen").machine_id()
    }

    fn store_with_contact(trust: TrustLevel, id_type: IdentityType) -> (ContactStore, AgentId) {
        let dir = tempfile::tempdir().expect("tmpdir");
        let mut store = ContactStore::new(dir.path().join("contacts.json"));
        let aid = agent_id();
        store.add(Contact {
            agent_id: aid,
            trust_level: trust,
            label: None,
            added_at: 0,
            last_seen: None,
            identity_type: id_type,
            machines: Vec::new(),
        });
        (store, aid)
    }

    // ── basic trust level tests ────────────────────────────────────────────

    #[test]
    fn unknown_agent_returns_unknown() {
        let dir = tempfile::tempdir().expect("tmpdir");
        let store = ContactStore::new(dir.path().join("contacts.json"));
        let evaluator = TrustEvaluator::new(&store);
        let aid = agent_id();
        let mid = machine_id();
        let decision = evaluator.evaluate(&TrustContext {
            agent_id: &aid,
            machine_id: &mid,
        });
        assert_eq!(decision, TrustDecision::Unknown);
    }

    #[test]
    fn blocked_agent_returns_reject_blocked() {
        let (store, aid) = store_with_contact(TrustLevel::Blocked, IdentityType::Anonymous);
        let evaluator = TrustEvaluator::new(&store);
        let mid = machine_id();
        let decision = evaluator.evaluate(&TrustContext {
            agent_id: &aid,
            machine_id: &mid,
        });
        assert_eq!(decision, TrustDecision::RejectBlocked);
    }

    #[test]
    fn trusted_non_pinned_returns_accept() {
        let (store, aid) = store_with_contact(TrustLevel::Trusted, IdentityType::Trusted);
        let evaluator = TrustEvaluator::new(&store);
        let mid = machine_id();
        let decision = evaluator.evaluate(&TrustContext {
            agent_id: &aid,
            machine_id: &mid,
        });
        assert_eq!(decision, TrustDecision::Accept);
    }

    #[test]
    fn known_agent_returns_accept_with_flag() {
        let (store, aid) = store_with_contact(TrustLevel::Known, IdentityType::Known);
        let evaluator = TrustEvaluator::new(&store);
        let mid = machine_id();
        let decision = evaluator.evaluate(&TrustContext {
            agent_id: &aid,
            machine_id: &mid,
        });
        assert_eq!(decision, TrustDecision::AcceptWithFlag);
    }

    #[test]
    fn unknown_trust_level_returns_unknown() {
        let (store, aid) = store_with_contact(TrustLevel::Unknown, IdentityType::Anonymous);
        let evaluator = TrustEvaluator::new(&store);
        let mid = machine_id();
        let decision = evaluator.evaluate(&TrustContext {
            agent_id: &aid,
            machine_id: &mid,
        });
        assert_eq!(decision, TrustDecision::Unknown);
    }

    // ── machine pinning tests ─────────────────────────────────────────────

    #[test]
    fn pinned_correct_machine_returns_accept() {
        let dir = tempfile::tempdir().expect("tmpdir");
        let mut store = ContactStore::new(dir.path().join("contacts.json"));
        let aid = agent_id();
        let mid = machine_id();

        store.add(Contact {
            agent_id: aid,
            trust_level: TrustLevel::Trusted,
            label: None,
            added_at: 0,
            last_seen: None,
            identity_type: IdentityType::Anonymous,
            machines: Vec::new(),
        });
        store.add_machine(&aid, MachineRecord::new(mid, None));
        store.pin_machine(&aid, &mid);

        let evaluator = TrustEvaluator::new(&store);
        let decision = evaluator.evaluate(&TrustContext {
            agent_id: &aid,
            machine_id: &mid,
        });
        assert_eq!(decision, TrustDecision::Accept);
    }

    #[test]
    fn pinned_wrong_machine_returns_reject_mismatch() {
        let dir = tempfile::tempdir().expect("tmpdir");
        let mut store = ContactStore::new(dir.path().join("contacts.json"));
        let aid = agent_id();
        let mid = machine_id();
        let other_mid = machine_id();

        store.add(Contact {
            agent_id: aid,
            trust_level: TrustLevel::Trusted,
            label: None,
            added_at: 0,
            last_seen: None,
            identity_type: IdentityType::Anonymous,
            machines: Vec::new(),
        });
        store.add_machine(&aid, MachineRecord::new(mid, None));
        store.pin_machine(&aid, &mid);

        let evaluator = TrustEvaluator::new(&store);
        let decision = evaluator.evaluate(&TrustContext {
            agent_id: &aid,
            machine_id: &other_mid,
        });
        assert_eq!(decision, TrustDecision::RejectMachineMismatch);
    }

    #[test]
    fn blocked_pinned_agent_returns_reject_blocked_not_machine_mismatch() {
        // Blocked check happens before machine pinning check
        let dir = tempfile::tempdir().expect("tmpdir");
        let mut store = ContactStore::new(dir.path().join("contacts.json"));
        let aid = agent_id();
        let mid = machine_id();
        let other_mid = machine_id();

        store.add(Contact {
            agent_id: aid,
            trust_level: TrustLevel::Blocked,
            label: None,
            added_at: 0,
            last_seen: None,
            identity_type: IdentityType::Anonymous,
            machines: Vec::new(),
        });
        store.add_machine(&aid, MachineRecord::new(mid, None));
        store.pin_machine(&aid, &mid);

        let evaluator = TrustEvaluator::new(&store);
        // Even though machine doesn't match, blocked takes priority
        let decision = evaluator.evaluate(&TrustContext {
            agent_id: &aid,
            machine_id: &other_mid,
        });
        assert_eq!(decision, TrustDecision::RejectBlocked);
    }

    // ── integration round-trip ────────────────────────────────────────────

    #[test]
    fn full_trust_round_trip() {
        let dir = tempfile::tempdir().expect("tmpdir");
        let mut store = ContactStore::new(dir.path().join("contacts.json"));
        let aid = agent_id();
        let mid = machine_id();
        let other_mid = machine_id();

        // 1. Add trusted contact
        store.set_trust(&aid, TrustLevel::Trusted);

        // 2. Add machine record with pinned: true
        store.add_machine(&aid, MachineRecord::new(mid, Some("laptop".into())));
        store.pin_machine(&aid, &mid);

        let evaluator = TrustEvaluator::new(&store);

        // 3. Evaluate — expect Accept for the pinned machine
        assert_eq!(
            evaluator.evaluate(&TrustContext {
                agent_id: &aid,
                machine_id: &mid,
            }),
            TrustDecision::Accept
        );

        // 4. Evaluate with a different machine — expect RejectMachineMismatch
        assert_eq!(
            evaluator.evaluate(&TrustContext {
                agent_id: &aid,
                machine_id: &other_mid,
            }),
            TrustDecision::RejectMachineMismatch
        );

        // 5. Block the contact and re-evaluate — expect RejectBlocked
        store.set_trust(&aid, TrustLevel::Blocked);
        let evaluator = TrustEvaluator::new(&store);
        assert_eq!(
            evaluator.evaluate(&TrustContext {
                agent_id: &aid,
                machine_id: &mid,
            }),
            TrustDecision::RejectBlocked
        );

        // 6. Evaluate an entirely unknown agent — expect Unknown
        let unknown_aid = agent_id();
        let unknown_mid = machine_id();
        let evaluator = TrustEvaluator::new(&store);
        assert_eq!(
            evaluator.evaluate(&TrustContext {
                agent_id: &unknown_aid,
                machine_id: &unknown_mid,
            }),
            TrustDecision::Unknown
        );
    }

    #[test]
    fn trust_decision_display() {
        assert_eq!(TrustDecision::Accept.to_string(), "accept");
        assert_eq!(
            TrustDecision::AcceptWithFlag.to_string(),
            "accept_with_flag"
        );
        assert_eq!(
            TrustDecision::RejectMachineMismatch.to_string(),
            "reject_machine_mismatch"
        );
        assert_eq!(TrustDecision::RejectBlocked.to_string(), "reject_blocked");
        assert_eq!(TrustDecision::Unknown.to_string(), "unknown");
    }
}