stateset-a2a 1.22.0

Agent-to-Agent commerce service layer: splits, subscriptions, escrow, webhooks, event streaming
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//! Escrow release condition types and evaluation.
//!
//! Four condition types are supported:
//!
//! - **`SellerFulfilled`**: Linked quote status must be `fulfilled`.
//! - **`BuyerConfirmed`**: Buyer explicitly confirms receipt.
//! - **`TimeLock`**: Release after a specified timestamp.
//! - **`Milestone`**: An arbitrary milestone is marked as completed.

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

/// The type of an escrow release condition.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum ConditionType {
    /// The linked quote must have status `fulfilled`.
    SellerFulfilled,
    /// The buyer must explicitly confirm delivery/receipt.
    BuyerConfirmed,
    /// Funds are released after a specified timestamp.
    TimeLock,
    /// An arbitrary milestone must be marked as completed.
    Milestone,
}

impl std::fmt::Display for ConditionType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::SellerFulfilled => write!(f, "seller_fulfilled"),
            Self::BuyerConfirmed => write!(f, "buyer_confirmed"),
            Self::TimeLock => write!(f, "time_lock"),
            Self::Milestone => write!(f, "milestone"),
        }
    }
}

/// A single escrow release condition.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Condition {
    /// The type of condition.
    #[serde(rename = "type")]
    pub condition_type: ConditionType,

    /// Whether this condition has been manually completed/confirmed.
    #[serde(default)]
    pub completed: bool,

    /// Associated quote ID (for `SellerFulfilled`).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub quote_id: Option<Uuid>,

    /// Release-after timestamp (for `TimeLock`).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub release_after: Option<DateTime<Utc>>,

    /// Human-readable description (for `Milestone`).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
}

impl Condition {
    /// Create a `SellerFulfilled` condition.
    #[must_use]
    pub const fn seller_fulfilled(quote_id: Option<Uuid>) -> Self {
        Self {
            condition_type: ConditionType::SellerFulfilled,
            completed: false,
            quote_id,
            release_after: None,
            description: None,
        }
    }

    /// Create a `BuyerConfirmed` condition.
    #[must_use]
    pub const fn buyer_confirmed() -> Self {
        Self {
            condition_type: ConditionType::BuyerConfirmed,
            completed: false,
            quote_id: None,
            release_after: None,
            description: None,
        }
    }

    /// Create a `TimeLock` condition.
    #[must_use]
    pub const fn time_lock(release_after: DateTime<Utc>) -> Self {
        Self {
            condition_type: ConditionType::TimeLock,
            completed: false,
            quote_id: None,
            release_after: Some(release_after),
            description: None,
        }
    }

    /// Create a `Milestone` condition.
    #[must_use]
    pub fn milestone(description: impl Into<String>) -> Self {
        Self {
            condition_type: ConditionType::Milestone,
            completed: false,
            quote_id: None,
            release_after: None,
            description: Some(description.into()),
        }
    }

    /// Mark this condition as completed.
    pub const fn confirm(&mut self) {
        self.completed = true;
    }
}

/// Result of evaluating a single condition.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConditionEvaluation {
    /// The condition that was evaluated.
    pub condition: Condition,
    /// Whether the condition is met.
    pub met: bool,
}

/// Evaluate a `BuyerConfirmed` condition.
///
/// Met if and only if `completed` is `true`.
#[must_use]
pub const fn evaluate_buyer_confirmed(condition: &Condition) -> bool {
    condition.completed
}

/// Evaluate a `TimeLock` condition.
///
/// Met if the current time is at or after `release_after`.
#[must_use]
pub fn evaluate_time_lock(condition: &Condition, now: DateTime<Utc>) -> bool {
    condition.release_after.is_some_and(|release_after| now >= release_after)
}

/// Evaluate a `Milestone` condition.
///
/// Met if and only if `completed` is `true`.
#[must_use]
pub const fn evaluate_milestone(condition: &Condition) -> bool {
    condition.completed
}

/// Evaluate a `SellerFulfilled` condition.
///
/// Met if the provided `quote_status` is `"fulfilled"`.
/// The caller is responsible for looking up the quote status.
#[must_use]
pub fn evaluate_seller_fulfilled(quote_status: Option<&str>) -> bool {
    quote_status == Some("fulfilled")
}

/// Evaluate a single condition, given the current time and an optional quote status.
///
/// This is the primary dispatch function that routes to the specific evaluator
/// for each condition type.
#[must_use]
pub fn evaluate_condition(
    condition: &Condition,
    now: DateTime<Utc>,
    quote_status: Option<&str>,
) -> bool {
    match condition.condition_type {
        ConditionType::SellerFulfilled => evaluate_seller_fulfilled(quote_status),
        ConditionType::BuyerConfirmed => evaluate_buyer_confirmed(condition),
        ConditionType::TimeLock => evaluate_time_lock(condition, now),
        ConditionType::Milestone => evaluate_milestone(condition),
    }
}

/// Evaluate all conditions and return whether all are met.
///
/// An empty conditions list is considered "all met" (unconditional release).
///
/// The `quote_status_fn` closure is called for each `SellerFulfilled` condition
/// to look up the quote status by ID.
pub fn evaluate_all_conditions<F>(
    conditions: &[Condition],
    now: DateTime<Utc>,
    quote_status_fn: F,
) -> (bool, Vec<ConditionEvaluation>)
where
    F: Fn(Option<&Uuid>) -> Option<String>,
{
    if conditions.is_empty() {
        return (true, Vec::new());
    }

    let mut evaluations = Vec::with_capacity(conditions.len());

    for condition in conditions {
        let quote_status = if condition.condition_type == ConditionType::SellerFulfilled {
            quote_status_fn(condition.quote_id.as_ref())
        } else {
            None
        };

        let met = evaluate_condition(condition, now, quote_status.as_deref());

        evaluations.push(ConditionEvaluation { condition: condition.clone(), met });
    }

    let all_met = evaluations.iter().all(|e| e.met);
    (all_met, evaluations)
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::TimeZone;

    fn now() -> DateTime<Utc> {
        Utc.with_ymd_and_hms(2026, 2, 22, 12, 0, 0).unwrap()
    }

    fn past() -> DateTime<Utc> {
        Utc.with_ymd_and_hms(2026, 2, 20, 12, 0, 0).unwrap()
    }

    fn future() -> DateTime<Utc> {
        Utc.with_ymd_and_hms(2026, 3, 1, 12, 0, 0).unwrap()
    }

    // ===== SellerFulfilled =====

    #[test]
    fn seller_fulfilled_met_when_quote_fulfilled() {
        assert!(evaluate_seller_fulfilled(Some("fulfilled")));
    }

    #[test]
    fn seller_fulfilled_not_met_when_quote_pending() {
        assert!(!evaluate_seller_fulfilled(Some("pending")));
    }

    #[test]
    fn seller_fulfilled_not_met_when_no_quote() {
        assert!(!evaluate_seller_fulfilled(None));
    }

    // ===== BuyerConfirmed =====

    #[test]
    fn buyer_confirmed_met_when_completed() {
        let mut c = Condition::buyer_confirmed();
        c.confirm();
        assert!(evaluate_buyer_confirmed(&c));
    }

    #[test]
    fn buyer_confirmed_not_met_by_default() {
        let c = Condition::buyer_confirmed();
        assert!(!evaluate_buyer_confirmed(&c));
    }

    // ===== TimeLock =====

    #[test]
    fn time_lock_met_when_past_release() {
        let c = Condition::time_lock(past());
        assert!(evaluate_time_lock(&c, now()));
    }

    #[test]
    fn time_lock_met_when_exactly_at_release() {
        let t = now();
        let c = Condition::time_lock(t);
        assert!(evaluate_time_lock(&c, t));
    }

    #[test]
    fn time_lock_not_met_when_before_release() {
        let c = Condition::time_lock(future());
        assert!(!evaluate_time_lock(&c, now()));
    }

    #[test]
    fn time_lock_not_met_without_release_after() {
        let c = Condition {
            condition_type: ConditionType::TimeLock,
            completed: false,
            quote_id: None,
            release_after: None,
            description: None,
        };
        assert!(!evaluate_time_lock(&c, now()));
    }

    // ===== Milestone =====

    #[test]
    fn milestone_met_when_completed() {
        let mut c = Condition::milestone("Design review");
        c.confirm();
        assert!(evaluate_milestone(&c));
    }

    #[test]
    fn milestone_not_met_by_default() {
        let c = Condition::milestone("Design review");
        assert!(!evaluate_milestone(&c));
    }

    // ===== evaluate_condition dispatch =====

    #[test]
    fn evaluate_condition_dispatches_buyer_confirmed() {
        let mut c = Condition::buyer_confirmed();
        c.confirm();
        assert!(evaluate_condition(&c, now(), None));
    }

    #[test]
    fn evaluate_condition_dispatches_time_lock() {
        let c = Condition::time_lock(past());
        assert!(evaluate_condition(&c, now(), None));
    }

    #[test]
    fn evaluate_condition_dispatches_seller_fulfilled() {
        let c = Condition::seller_fulfilled(None);
        assert!(evaluate_condition(&c, now(), Some("fulfilled")));
        assert!(!evaluate_condition(&c, now(), Some("pending")));
    }

    #[test]
    fn evaluate_condition_dispatches_milestone() {
        let c = Condition::milestone("Ship prototype");
        assert!(!evaluate_condition(&c, now(), None));
    }

    // ===== evaluate_all_conditions =====

    #[test]
    fn all_conditions_met_empty_list() {
        let (all_met, evals) = evaluate_all_conditions(&[], now(), |_| None);
        assert!(all_met);
        assert!(evals.is_empty());
    }

    #[test]
    fn all_conditions_met_single_confirmed() {
        let mut c = Condition::buyer_confirmed();
        c.confirm();
        let (all_met, evals) = evaluate_all_conditions(&[c], now(), |_| None);
        assert!(all_met);
        assert_eq!(evals.len(), 1);
        assert!(evals[0].met);
    }

    #[test]
    fn all_conditions_not_met_one_unconfirmed() {
        let c1 = Condition::buyer_confirmed(); // not confirmed
        let c2 = Condition::time_lock(past()); // met
        let (all_met, evals) = evaluate_all_conditions(&[c1, c2], now(), |_| None);
        assert!(!all_met);
        assert!(!evals[0].met);
        assert!(evals[1].met);
    }

    #[test]
    fn all_conditions_met_mixed_types() {
        let mut c1 = Condition::buyer_confirmed();
        c1.confirm();
        let c2 = Condition::time_lock(past());
        let mut c3 = Condition::milestone("Ship it");
        c3.confirm();

        let (all_met, evals) = evaluate_all_conditions(&[c1, c2, c3], now(), |_| None);
        assert!(all_met);
        assert_eq!(evals.len(), 3);
    }

    #[test]
    fn seller_fulfilled_with_quote_lookup() {
        let quote_id = Uuid::new_v4();
        let c = Condition::seller_fulfilled(Some(quote_id));

        let (all_met, _) = evaluate_all_conditions(std::slice::from_ref(&c), now(), |id| {
            if id == Some(&quote_id) { Some("fulfilled".into()) } else { None }
        });
        assert!(all_met);

        let (all_met, _) = evaluate_all_conditions(&[c], now(), |id| {
            if id == Some(&quote_id) { Some("pending".into()) } else { None }
        });
        assert!(!all_met);
    }

    // ===== Condition constructors =====

    #[test]
    fn condition_seller_fulfilled_defaults() {
        let c = Condition::seller_fulfilled(None);
        assert_eq!(c.condition_type, ConditionType::SellerFulfilled);
        assert!(!c.completed);
        assert!(c.quote_id.is_none());
    }

    #[test]
    fn condition_time_lock_has_release_after() {
        let t = now();
        let c = Condition::time_lock(t);
        assert_eq!(c.condition_type, ConditionType::TimeLock);
        assert_eq!(c.release_after, Some(t));
    }

    #[test]
    fn condition_milestone_has_description() {
        let c = Condition::milestone("Phase 1 complete");
        assert_eq!(c.condition_type, ConditionType::Milestone);
        assert_eq!(c.description, Some("Phase 1 complete".into()));
    }

    #[test]
    fn condition_type_display() {
        assert_eq!(ConditionType::SellerFulfilled.to_string(), "seller_fulfilled");
        assert_eq!(ConditionType::BuyerConfirmed.to_string(), "buyer_confirmed");
        assert_eq!(ConditionType::TimeLock.to_string(), "time_lock");
        assert_eq!(ConditionType::Milestone.to_string(), "milestone");
    }

    // ===== Serialization =====

    #[test]
    fn condition_serializes_to_json() {
        let c = Condition::buyer_confirmed();
        let json = serde_json::to_string(&c).unwrap();
        assert!(json.contains("\"type\":\"buyer_confirmed\""));
        assert!(json.contains("\"completed\":false"));
    }

    #[test]
    fn condition_round_trips_json() {
        let original = Condition::milestone("Test milestone");
        let json = serde_json::to_string(&original).unwrap();
        let deserialized: Condition = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.condition_type, ConditionType::Milestone);
        assert_eq!(deserialized.description, Some("Test milestone".into()));
    }
}