interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / crates / aienos-aegis / src / effect.rs

159 lines · 4994 bytes Raw Blame History
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! Effect Intents and Operator Grants conforming to AEGIS (ยง5).
17
18use aienos_agent_state::LogicalAgentId;
19use aienos_kernel::crypto::sha256;
20use serde::{Deserialize, Serialize};
21use std::collections::BTreeMap;
22
23/// Effect Intent proposed by an agent reasoning loop.
24///
25/// Intelligence proposes; deterministic authority disposes.
26#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
27pub struct EffectIntent {
28 pub id: [u8; 16],
29 pub agent_id: LogicalAgentId,
30 pub action: String,
31 pub parameters: BTreeMap<String, String>,
32 pub claimed_capability_id: [u8; 16],
33 pub is_reversible: bool,
34 pub world_id: Option<[u8; 16]>,
35}
36
37impl EffectIntent {
38 fn hash_field(hasher: &mut sha256::Sha256, field: &[u8]) {
39 hasher.update(&(field.len() as u64).to_be_bytes());
40 hasher.update(field);
41 }
42
43 /// Create a new effect intent.
44 pub fn new(
45 agent_id: LogicalAgentId,
46 action: impl Into<String>,
47 parameters: BTreeMap<String, String>,
48 claimed_capability_id: [u8; 16],
49 is_reversible: bool,
50 world_id: Option<[u8; 16]>,
51 ) -> Self {
52 let action_str = action.into();
53 let mut hasher = sha256::Sha256::new();
54 hasher.update(b"AIENOS_INTENT_V1");
55 hasher.update(agent_id.as_bytes());
56 Self::hash_field(&mut hasher, action_str.as_bytes());
57 hasher.update(&claimed_capability_id);
58 hasher.update(&[is_reversible as u8]);
59 if let Some(wid) = &world_id {
60 hasher.update(&[1]);
61 hasher.update(wid);
62 } else {
63 hasher.update(&[0]);
64 }
65 hasher.update(&(parameters.len() as u64).to_be_bytes());
66 for (k, v) in &parameters {
67 Self::hash_field(&mut hasher, k.as_bytes());
68 Self::hash_field(&mut hasher, v.as_bytes());
69 }
70 let digest = hasher.finalize();
71 let mut id = [0u8; 16];
72 id.copy_from_slice(&digest[..16]);
73
74 Self {
75 id,
76 agent_id,
77 action: action_str,
78 parameters,
79 claimed_capability_id,
80 is_reversible,
81 world_id,
82 }
83 }
84}
85
86#[cfg(test)]
87mod tests {
88 use super::*;
89
90 #[test]
91 fn intent_id_distinguishes_parameter_boundaries() {
92 let agent = LogicalAgentId::from_seed("intent-boundaries");
93 let mut first = BTreeMap::new();
94 first.insert("ab".into(), "c".into());
95 let mut second = BTreeMap::new();
96 second.insert("a".into(), "bc".into());
97 let first = EffectIntent::new(agent, "fs.write", first, [1; 16], false, None);
98 let second = EffectIntent::new(agent, "fs.write", second, [1; 16], false, None);
99 assert_ne!(first.id, second.id);
100 }
101}
102
103/// Explicit Operator Grant required for any irreversible action crossing boundaries.
104#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
105pub struct OperatorGrant {
106 pub intent_id: [u8; 16],
107 pub operator_id: String,
108 pub granted_at_utc: u64,
109 pub signature: [u8; 32],
110}
111
112impl OperatorGrant {
113 /// Issue an operator grant.
114 pub fn issue(
115 intent_id: [u8; 16],
116 operator_id: &str,
117 operator_secret: &[u8; 32],
118 granted_at_utc: u64,
119 ) -> Self {
120 let signature =
121 Self::signature_for(&intent_id, operator_id, granted_at_utc, operator_secret);
122
123 Self {
124 intent_id,
125 operator_id: operator_id.to_string(),
126 granted_at_utc,
127 signature,
128 }
129 }
130
131 /// Verify operator grant signature.
132 pub fn verify(&self, operator_secret: &[u8; 32]) -> bool {
133 Self::signature_for(
134 &self.intent_id,
135 &self.operator_id,
136 self.granted_at_utc,
137 operator_secret,
138 ) == self.signature
139 }
140
141 fn signature_for(
142 intent_id: &[u8; 16],
143 operator_id: &str,
144 granted_at_utc: u64,
145 operator_secret: &[u8; 32],
146 ) -> [u8; 32] {
147 let operator_len = (operator_id.len() as u64).to_be_bytes();
148 sha256::hmac_sha256(
149 operator_secret,
150 &[
151 b"AIENOS_GRANT_V1",
152 intent_id,
153 &operator_len,
154 operator_id.as_bytes(),
155 &granted_at_utc.to_be_bytes(),
156 ],
157 )
158 }
159}