Skip to main content

aios_core/governance/
mod.rs

1//! 动作治理凭证与 dispatch 抽象
2//!
3//! `AuthorizedAction` 是 Action Bus 上唯一可被执行的凭证。它字段私有,构造器
4//! `pub(crate) seal` 只允许同 crate 的 `ActionLifecycle` 在策略通过后调用,从而
5//! 在编译期保证:任何外部 crate(包括 `aios-action`)无法伪造或反序列化出一个
6//! 可执行动作。
7
8use aios_spec::governance::{
9    ActionCoord, ActionOutcome, ActionProposal, AdapterError, EffectClass,
10};
11use aios_spec::intent::SuggestedAction;
12
13/// 经 `ActionLifecycle` 审查通过、允许交给 adapter 执行的动作凭证。
14///
15/// - 字段私有,外部 crate 无法 struct-literal 构造。
16/// - 不实现 `Deserialize`,防止反序列化伪造。
17/// - 只暴露 getter,adapter 只能读取不能修改。
18#[derive(Debug, Clone, serde::Serialize)]
19pub struct AuthorizedAction {
20    intent_id: String,
21    coord: ActionCoord,
22    action: SuggestedAction,
23    effect: EffectClass,
24    authorized_at_ms: i64,
25}
26
27impl AuthorizedAction {
28    /// 唯一构造器。仅 `crate::action_lifecycle` 可在 `PolicyChecked` 后调用。
29    pub(crate) fn seal(proposal: &ActionProposal, authorized_at_ms: i64) -> Self {
30        Self {
31            intent_id: proposal.intent_id.clone(),
32            coord: proposal.coord,
33            action: proposal.action.clone(),
34            effect: proposal.effect,
35            authorized_at_ms,
36        }
37    }
38
39    pub fn intent_id(&self) -> &str {
40        &self.intent_id
41    }
42
43    pub fn coord(&self) -> ActionCoord {
44        self.coord
45    }
46
47    pub fn action(&self) -> &SuggestedAction {
48        &self.action
49    }
50
51    pub fn effect(&self) -> EffectClass {
52        self.effect
53    }
54
55    pub fn authorized_at_ms(&self) -> i64 {
56        self.authorized_at_ms
57    }
58}
59
60/// 动作 dispatch 抽象。
61///
62/// `DefaultActionExecutor`(Android bridge 转发 + 本地 stub)与
63/// `OfflineAdapter`(纯离线确定性模拟)都实现此 trait,由 `ActionLifecycle`
64/// 在运行时二选一注入。
65pub trait ActionAdapter {
66    fn name(&self) -> &'static str;
67    fn execute(&self, authorized: &AuthorizedAction) -> Result<ActionOutcome, AdapterError>;
68}
69
70#[cfg(test)]
71mod tests {
72    use super::*;
73    use aios_spec::governance::ActionCoord;
74    use aios_spec::intent::{ActionType, ActionUrgency};
75
76    #[test]
77    fn seal_copies_proposal_fields() {
78        let proposal = ActionProposal {
79            intent_id: "intent-1".into(),
80            coord: ActionCoord {
81                window_ordinal: 1,
82                intent_ordinal: 2,
83                action_ordinal: 3,
84            },
85            action: SuggestedAction {
86                action_type: ActionType::NoOp,
87                target: None,
88                urgency: ActionUrgency::Immediate,
89            },
90            effect: EffectClass::PureRead,
91            proposed_at_ms: 1000,
92        };
93
94        let authorized = AuthorizedAction::seal(&proposal, 2000);
95        assert_eq!(authorized.intent_id(), "intent-1");
96        assert_eq!(authorized.coord(), proposal.coord);
97        assert!(matches!(authorized.action().action_type, ActionType::NoOp));
98        assert_eq!(authorized.effect(), EffectClass::PureRead);
99        assert_eq!(authorized.authorized_at_ms(), 2000);
100    }
101}