Skip to main content

mur_common/
commander.rs

1use serde::{Deserialize, Serialize};
2
3/// Key under which a governance directive is placed in a `ChannelEvent.payload`.
4pub const COMMANDER_DIRECTIVE_KEY: &str = "commander_directive";
5
6/// `<mur_home>/commander`: the commander's identity, pinned keys and config.
7/// Here rather than in `mur-core` because the agent sandbox names files in it
8/// and `mur-agent-runtime` must not depend on `mur-core`.
9pub const COMMANDER_DIR: &str = "commander";
10/// The pinned commander public key, inside [`COMMANDER_DIR`].
11pub const COMMANDER_PUB: &str = "identity.pub";
12/// The previous pinned key, kept for one rotation, inside [`COMMANDER_DIR`].
13pub const COMMANDER_PREV_PUB: &str = "identity.prev.pub";
14
15/// A cryptographically-signed instruction from the Commander governance plane.
16///
17/// Serialised as a JSON object and placed under [`COMMANDER_DIRECTIVE_KEY`]
18/// in a `ChannelEvent.payload` appended to the `fleet-<name>` channel. The
19/// signing layer lives in `mur-core`; this crate only owns the payload shape.
20/// Cross-network A2A-envelope delivery is Phase 2.
21#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
22pub struct CommanderDirective {
23    /// Directive kind, e.g. `"kill"`, `"budget_ceiling"`.
24    pub kind: String,
25
26    /// Target fleet name.
27    pub fleet: String,
28
29    /// Optional budget ceiling in USD; `>=0` is honoured, `<0`/`NaN` ignored.
30    pub budget_usd: Option<f64>,
31
32    /// Replay-prevention nonce (UUID v4 recommended).
33    pub nonce: String,
34
35    /// Wall-clock milliseconds since Unix epoch when the directive was issued.
36    pub issued_at_ms: u64,
37}
38
39/// Derived governance state for a running fleet loop.
40///
41/// Built by reducing the stream of [`CommanderDirective`]s received so far.
42#[derive(Debug, Clone, Default, PartialEq)]
43pub struct GovernanceState {
44    /// If `true`, the loop must stop immediately.
45    pub killed: bool,
46
47    /// Active budget ceiling (USD), if any has been applied.
48    pub budget_ceiling: Option<f64>,
49
50    /// Nonce of the directive that produced `killed == true` (audit binding).
51    /// `None` when not killed, or after a resume clears the kill.
52    pub kill_nonce: Option<String>,
53
54    /// Nonce of the directive that set `budget_ceiling` (audit binding).
55    pub budget_nonce: Option<String>,
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn directive_roundtrips_under_the_marker_key() {
64        let d = CommanderDirective {
65            kind: "kill".into(),
66            fleet: "dev".into(),
67            budget_usd: None,
68            nonce: "n1".into(),
69            issued_at_ms: 1_750_000_000_000,
70        };
71        let wrapped = serde_json::json!({ COMMANDER_DIRECTIVE_KEY: &d });
72        let got: CommanderDirective =
73            serde_json::from_value(wrapped[COMMANDER_DIRECTIVE_KEY].clone()).unwrap();
74        assert_eq!(got.kind, "kill");
75        assert_eq!(got.fleet, "dev");
76        assert_eq!(got.issued_at_ms, 1_750_000_000_000);
77    }
78
79    #[test]
80    fn governance_state_default_is_inert() {
81        let g = GovernanceState::default();
82        assert!(!g.killed && g.budget_ceiling.is_none());
83    }
84}