exochain-catapult 0.2.0-beta

EXOCHAIN Catapult — franchise business incubator with FM 3-05 operational doctrine
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
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
// Copyright 2026 Exochain Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

//! Heartbeat monitoring — Paperclip concept adapted for ExoChain.
//!
//! Tracks per-agent pulse via HLC timestamps. Timeouts trigger PACE
//! escalation through the ODA command hierarchy.

use std::collections::BTreeMap;

use exo_core::{Did, Hash256, Timestamp};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::error::{CatapultError, Result};

/// Domain tag for canonical heartbeat receipt hashes.
pub const HEARTBEAT_HASH_DOMAIN: &str = "exo.catapult.heartbeat_record.v1";
const HEARTBEAT_HASH_SCHEMA_VERSION: &str = "1.0.0";

/// Status of a heartbeat invocation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum HeartbeatStatus {
    Queued,
    Running,
    Completed,
    Failed,
    TimedOut,
}

/// A single heartbeat record from an agent.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HeartbeatRecord {
    pub id: Uuid,
    pub newco_id: Uuid,
    pub agent_did: Did,
    pub status: HeartbeatStatus,
    pub started: Timestamp,
    pub finished: Option<Timestamp>,
    /// Resource usage counters (token counts, API calls, etc.).
    pub usage: BTreeMap<String, u64>,
    /// Hash of this heartbeat for receipt chaining.
    pub receipt_hash: Hash256,
}

/// Caller-supplied deterministic metadata for creating a heartbeat record.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HeartbeatRecordInput {
    pub id: Uuid,
    pub newco_id: Uuid,
    pub agent_did: Did,
    pub status: HeartbeatStatus,
    pub started: Timestamp,
    pub finished: Option<Timestamp>,
    pub usage: BTreeMap<String, u64>,
}

impl HeartbeatRecord {
    /// Create a heartbeat record with a deterministic canonical receipt hash.
    ///
    /// # Errors
    /// Returns [`CatapultError`] when the input contains placeholder metadata
    /// or canonical hashing fails.
    pub fn new(input: HeartbeatRecordInput) -> Result<Self> {
        validate_heartbeat_input(&input)?;
        let receipt_hash = heartbeat_record_receipt_hash(&input)?;
        Ok(Self {
            id: input.id,
            newco_id: input.newco_id,
            agent_did: input.agent_did,
            status: input.status,
            started: input.started,
            finished: input.finished,
            usage: input.usage,
            receipt_hash,
        })
    }

    /// Validate externally supplied or deserialized heartbeat metadata.
    ///
    /// # Errors
    /// Returns [`CatapultError`] when placeholders are present or the stored
    /// receipt hash does not match the canonical heartbeat payload.
    pub fn validate(&self) -> Result<()> {
        let input = self.input();
        validate_heartbeat_input(&input)?;
        if self.receipt_hash == Hash256::ZERO {
            return Err(CatapultError::InvalidHeartbeat {
                reason: "heartbeat receipt hash must not be zero".into(),
            });
        }
        if !self.verify_receipt_hash()? {
            return Err(CatapultError::InvalidHeartbeat {
                reason: "heartbeat receipt hash does not match canonical payload".into(),
            });
        }
        Ok(())
    }

    /// Verify the stored receipt hash against the canonical payload.
    ///
    /// # Errors
    /// Returns [`CatapultError`] if canonical hashing fails.
    pub fn verify_receipt_hash(&self) -> Result<bool> {
        Ok(heartbeat_record_receipt_hash(&self.input())? == self.receipt_hash)
    }

    fn input(&self) -> HeartbeatRecordInput {
        HeartbeatRecordInput {
            id: self.id,
            newco_id: self.newco_id,
            agent_did: self.agent_did.clone(),
            status: self.status,
            started: self.started,
            finished: self.finished,
            usage: self.usage.clone(),
        }
    }
}

/// An alert generated when an agent misses their heartbeat window.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HeartbeatAlert {
    pub agent_did: Did,
    pub last_seen: Timestamp,
    pub elapsed_ms: u64,
    pub severity: AlertSeverity,
}

/// Severity of a heartbeat alert.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum AlertSeverity {
    /// Agent is late but within tolerance.
    Warning,
    /// Agent has exceeded the timeout — PACE escalation needed.
    Critical,
}

/// Monitors agent heartbeats and generates alerts on timeout.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HeartbeatMonitor {
    /// Last heartbeat timestamp per agent DID.
    last_seen: BTreeMap<Did, Timestamp>,
    /// Full heartbeat history per agent.
    history: BTreeMap<Did, Vec<HeartbeatRecord>>,
    /// Warning threshold in milliseconds.
    warn_ms: u64,
    /// Critical timeout in milliseconds (triggers PACE escalation).
    timeout_ms: u64,
}

impl HeartbeatMonitor {
    /// Default warning threshold: 3 minutes.
    pub const DEFAULT_WARN_MS: u64 = 180_000;
    /// Default critical timeout: 5 minutes.
    pub const DEFAULT_TIMEOUT_MS: u64 = 300_000;

    /// Create a new heartbeat monitor with default thresholds.
    #[must_use]
    pub fn new() -> Self {
        Self {
            last_seen: BTreeMap::new(),
            history: BTreeMap::new(),
            warn_ms: Self::DEFAULT_WARN_MS,
            timeout_ms: Self::DEFAULT_TIMEOUT_MS,
        }
    }

    /// Create a monitor with custom thresholds.
    #[must_use]
    pub fn with_thresholds(warn_ms: u64, timeout_ms: u64) -> Self {
        Self {
            last_seen: BTreeMap::new(),
            history: BTreeMap::new(),
            warn_ms,
            timeout_ms,
        }
    }

    /// Record a heartbeat from an agent.
    pub fn record(&mut self, record: HeartbeatRecord) -> Result<()> {
        record.validate()?;
        let did = record.agent_did.clone();
        let ts = record.started;
        self.last_seen.insert(did.clone(), ts);
        self.history.entry(did).or_default().push(record);
        Ok(())
    }

    /// Check all agents for heartbeat health at the given time.
    #[must_use]
    pub fn check_health(&self, now: &Timestamp) -> Vec<HeartbeatAlert> {
        let mut alerts = Vec::new();
        for (did, last) in &self.last_seen {
            let Some(elapsed_ms) = now.physical_ms.checked_sub(last.physical_ms) else {
                alerts.push(HeartbeatAlert {
                    agent_did: did.clone(),
                    last_seen: *last,
                    elapsed_ms: 0,
                    severity: AlertSeverity::Critical,
                });
                continue;
            };
            if elapsed_ms >= self.timeout_ms {
                alerts.push(HeartbeatAlert {
                    agent_did: did.clone(),
                    last_seen: *last,
                    elapsed_ms,
                    severity: AlertSeverity::Critical,
                });
            } else if elapsed_ms >= self.warn_ms {
                alerts.push(HeartbeatAlert {
                    agent_did: did.clone(),
                    last_seen: *last,
                    elapsed_ms,
                    severity: AlertSeverity::Warning,
                });
            }
        }
        alerts
    }

    /// Get the last-seen timestamp for an agent.
    #[must_use]
    pub fn last_seen(&self, did: &Did) -> Option<&Timestamp> {
        self.last_seen.get(did)
    }

    /// Get the heartbeat history for an agent.
    #[must_use]
    pub fn history(&self, did: &Did) -> Option<&[HeartbeatRecord]> {
        self.history.get(did).map(|v| v.as_slice())
    }

    /// Number of agents being monitored.
    #[must_use]
    pub fn agent_count(&self) -> usize {
        self.last_seen.len()
    }

    /// Validate a deserialized monitor and all recorded heartbeat history.
    ///
    /// # Errors
    /// Returns [`CatapultError`] when heartbeat history contains placeholders
    /// or a last-seen timestamp disagrees with recorded history.
    pub fn validate(&self) -> Result<()> {
        for (did, history) in &self.history {
            let mut latest = None;
            for record in history {
                if record.agent_did != *did {
                    return Err(CatapultError::InvalidHeartbeat {
                        reason: format!(
                            "heartbeat history key {did} does not match record DID {}",
                            record.agent_did
                        ),
                    });
                }
                record.validate()?;
                latest = Some(latest.map_or(record.started, |current: Timestamp| {
                    current.max(record.started)
                }));
            }
            match (self.last_seen.get(did), latest) {
                (Some(last_seen), Some(latest_started)) if *last_seen == latest_started => {}
                (Some(_), Some(latest_started)) => {
                    return Err(CatapultError::InvalidHeartbeat {
                        reason: format!(
                            "heartbeat last_seen for {did} does not match latest record {latest_started}"
                        ),
                    });
                }
                _ => {
                    return Err(CatapultError::InvalidHeartbeat {
                        reason: format!("heartbeat history for {did} has no matching last_seen"),
                    });
                }
            }
        }

        for did in self.last_seen.keys() {
            if !self.history.contains_key(did) {
                return Err(CatapultError::InvalidHeartbeat {
                    reason: format!("heartbeat last_seen for {did} has no history"),
                });
            }
        }
        Ok(())
    }
}

impl Default for HeartbeatMonitor {
    fn default() -> Self {
        Self::new()
    }
}

/// Compute the canonical receipt hash for a heartbeat record.
///
/// # Errors
/// Returns [`CatapultError`] if canonical CBOR hashing fails.
pub fn heartbeat_record_receipt_hash(input: &HeartbeatRecordInput) -> Result<Hash256> {
    validate_heartbeat_input(input)?;
    exo_core::hash::hash_structured(&HeartbeatHashPayload::from_input(input)).map_err(|e| {
        CatapultError::InvalidHeartbeat {
            reason: format!("heartbeat canonical hash failed: {e}"),
        }
    })
}

#[derive(Serialize)]
struct HeartbeatHashPayload<'a> {
    domain: &'static str,
    schema_version: &'static str,
    id: Uuid,
    newco_id: Uuid,
    agent_did: &'a Did,
    status: HeartbeatStatus,
    started: Timestamp,
    finished: Option<Timestamp>,
    usage: &'a BTreeMap<String, u64>,
}

impl<'a> HeartbeatHashPayload<'a> {
    fn from_input(input: &'a HeartbeatRecordInput) -> Self {
        Self {
            domain: HEARTBEAT_HASH_DOMAIN,
            schema_version: HEARTBEAT_HASH_SCHEMA_VERSION,
            id: input.id,
            newco_id: input.newco_id,
            agent_did: &input.agent_did,
            status: input.status,
            started: input.started,
            finished: input.finished,
            usage: &input.usage,
        }
    }
}

fn validate_heartbeat_input(input: &HeartbeatRecordInput) -> Result<()> {
    if input.id.is_nil() {
        return Err(CatapultError::InvalidHeartbeat {
            reason: "heartbeat id must be caller-supplied and non-nil".into(),
        });
    }
    if input.newco_id.is_nil() {
        return Err(CatapultError::InvalidHeartbeat {
            reason: "heartbeat newco id must be non-nil".into(),
        });
    }
    if input.started == Timestamp::ZERO {
        return Err(CatapultError::InvalidHeartbeat {
            reason: "heartbeat started timestamp must be caller-supplied HLC".into(),
        });
    }
    if let Some(finished) = input.finished {
        if finished < input.started {
            return Err(CatapultError::InvalidHeartbeat {
                reason: "heartbeat finished timestamp must not precede started timestamp".into(),
            });
        }
    }
    Ok(())
}

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

    fn test_did(name: &str) -> Did {
        Did::new(&format!("did:exo:test-{name}")).unwrap()
    }

    fn make_heartbeat(did: Did, time_ms: u64) -> HeartbeatRecord {
        let mut bytes = [0u8; 16];
        bytes[..8].copy_from_slice(&time_ms.to_le_bytes());
        HeartbeatRecord::new(HeartbeatRecordInput {
            id: Uuid::from_bytes(bytes),
            newco_id: test_uuid(3),
            agent_did: did,
            status: HeartbeatStatus::Completed,
            started: Timestamp::new(time_ms, 0),
            finished: Some(Timestamp::new(time_ms + 100, 0)),
            usage: BTreeMap::new(),
        })
        .unwrap()
    }

    fn test_uuid(byte: u8) -> Uuid {
        Uuid::from_bytes([byte; 16])
    }

    fn valid_heartbeat_input(did: Did, time_ms: u64) -> HeartbeatRecordInput {
        HeartbeatRecordInput {
            id: test_uuid(1),
            newco_id: test_uuid(2),
            agent_did: did,
            status: HeartbeatStatus::Completed,
            started: Timestamp::new(time_ms, 0),
            finished: Some(Timestamp::new(time_ms + 100, 0)),
            usage: BTreeMap::new(),
        }
    }

    #[test]
    fn heartbeat_record_new_requires_caller_supplied_provenance() {
        let record = HeartbeatRecord::new(valid_heartbeat_input(test_did("agent1"), 1000)).unwrap();

        assert_ne!(record.id, Uuid::nil());
        assert_ne!(record.newco_id, Uuid::nil());
        assert_ne!(record.started, Timestamp::ZERO);
        assert_ne!(record.receipt_hash, Hash256::ZERO);
        assert!(record.verify_receipt_hash().unwrap());
    }

    #[test]
    fn monitor_rejects_placeholder_or_tampered_heartbeat_records() {
        let mut monitor = HeartbeatMonitor::new();

        let mut record =
            HeartbeatRecord::new(valid_heartbeat_input(test_did("agent1"), 1000)).unwrap();
        record.receipt_hash = Hash256::ZERO;
        assert!(monitor.record(record).is_err());

        let mut record =
            HeartbeatRecord::new(valid_heartbeat_input(test_did("agent1"), 1000)).unwrap();
        record.status = HeartbeatStatus::Failed;
        assert!(!record.verify_receipt_hash().unwrap());
        assert!(monitor.record(record).is_err());
    }

    #[test]
    fn heartbeat_input_validation_rejects_placeholder_and_regressive_time() {
        let mut input = valid_heartbeat_input(test_did("agent1"), 1000);
        input.id = Uuid::nil();
        assert!(HeartbeatRecord::new(input).is_err());

        let mut input = valid_heartbeat_input(test_did("agent1"), 1000);
        input.newco_id = Uuid::nil();
        assert!(HeartbeatRecord::new(input).is_err());

        let mut input = valid_heartbeat_input(test_did("agent1"), 1000);
        input.started = Timestamp::ZERO;
        assert!(HeartbeatRecord::new(input).is_err());

        let mut input = valid_heartbeat_input(test_did("agent1"), 1000);
        input.finished = Some(Timestamp::new(999, 0));
        assert!(HeartbeatRecord::new(input).is_err());
    }

    #[test]
    fn monitor_validate_rejects_deserialized_inconsistencies() {
        let did = test_did("agent1");
        let record = make_heartbeat(did.clone(), 1000);
        let mut monitor = HeartbeatMonitor::new();
        monitor.record(record.clone()).unwrap();
        assert!(monitor.validate().is_ok());

        let mut mismatched_key = HeartbeatMonitor::new();
        let other = test_did("other");
        mismatched_key
            .last_seen
            .insert(other.clone(), record.started);
        mismatched_key.history.insert(other, vec![record.clone()]);
        assert!(mismatched_key.validate().is_err());

        let mut stale_last_seen = monitor.clone();
        stale_last_seen
            .last_seen
            .insert(did.clone(), Timestamp::new(1, 0));
        assert!(stale_last_seen.validate().is_err());

        let mut missing_history = HeartbeatMonitor::new();
        missing_history
            .last_seen
            .insert(did, Timestamp::new(1000, 0));
        assert!(missing_history.validate().is_err());
    }

    #[test]
    fn record_and_last_seen() {
        let mut monitor = HeartbeatMonitor::new();
        let did = test_did("agent1");
        monitor.record(make_heartbeat(did.clone(), 1000)).unwrap();
        assert_eq!(monitor.last_seen(&did).unwrap().physical_ms, 1000);
        assert_eq!(monitor.agent_count(), 1);
    }

    #[test]
    fn no_alerts_when_healthy() {
        let mut monitor = HeartbeatMonitor::new();
        let did = test_did("agent1");
        monitor.record(make_heartbeat(did, 1000)).unwrap();

        let now = Timestamp {
            physical_ms: 1500,
            logical: 0,
        };
        assert!(monitor.check_health(&now).is_empty());
    }

    #[test]
    fn future_dated_heartbeat_alerts_critical() {
        let mut monitor = HeartbeatMonitor::with_thresholds(100, 200);
        let did = test_did("agent1");
        monitor.record(make_heartbeat(did.clone(), 1_500)).unwrap();

        let alerts = monitor.check_health(&Timestamp::new(1_000, 0));

        assert_eq!(alerts.len(), 1);
        assert_eq!(alerts[0].agent_did, did);
        assert_eq!(alerts[0].severity, AlertSeverity::Critical);
    }

    #[test]
    fn warning_alert() {
        let mut monitor = HeartbeatMonitor::with_thresholds(100, 200);
        let did = test_did("agent1");
        monitor.record(make_heartbeat(did, 1000)).unwrap();

        let now = Timestamp {
            physical_ms: 1150,
            logical: 0,
        };
        let alerts = monitor.check_health(&now);
        assert_eq!(alerts.len(), 1);
        assert_eq!(alerts[0].severity, AlertSeverity::Warning);
    }

    #[test]
    fn critical_alert() {
        let mut monitor = HeartbeatMonitor::with_thresholds(100, 200);
        let did = test_did("agent1");
        monitor.record(make_heartbeat(did, 1000)).unwrap();

        let now = Timestamp {
            physical_ms: 1250,
            logical: 0,
        };
        let alerts = monitor.check_health(&now);
        assert_eq!(alerts.len(), 1);
        assert_eq!(alerts[0].severity, AlertSeverity::Critical);
    }

    #[test]
    fn multiple_agents() {
        let mut monitor = HeartbeatMonitor::with_thresholds(100, 200);
        monitor.record(make_heartbeat(test_did("a"), 1000)).unwrap();
        monitor.record(make_heartbeat(test_did("b"), 900)).unwrap();

        let now = Timestamp {
            physical_ms: 1150,
            logical: 0,
        };
        let alerts = monitor.check_health(&now);
        // Agent "a" is at 150ms (warning), "b" is at 250ms (critical)
        assert_eq!(alerts.len(), 2);
    }

    #[test]
    fn history_tracking() {
        let mut monitor = HeartbeatMonitor::new();
        let did = test_did("agent1");
        monitor.record(make_heartbeat(did.clone(), 1000)).unwrap();
        monitor.record(make_heartbeat(did.clone(), 2000)).unwrap();
        assert_eq!(monitor.history(&did).unwrap().len(), 2);
    }

    #[test]
    fn status_serde() {
        let statuses = [
            HeartbeatStatus::Queued,
            HeartbeatStatus::Running,
            HeartbeatStatus::Completed,
            HeartbeatStatus::Failed,
            HeartbeatStatus::TimedOut,
        ];
        for s in &statuses {
            let j = serde_json::to_string(s).unwrap();
            let rt: HeartbeatStatus = serde_json::from_str(&j).unwrap();
            assert_eq!(&rt, s);
        }
    }
}