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-agent-state / src / id.rs

140 lines · 4421 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//! Sovereign cryptographic identities for agents and reasoning branches.
17
18use aienos_kernel::crypto::sha256;
19use core::fmt;
20use serde::{Deserialize, Serialize};
21
22/// Sovereign Logical Agent Identifier (32-byte cryptographic key or UUIDv7 digest).
23#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
24pub struct LogicalAgentId(pub [u8; 32]);
25
26impl LogicalAgentId {
27 /// Construct a LogicalAgentId directly from 32 bytes.
28 pub const fn from_bytes(bytes: [u8; 32]) -> Self {
29 Self(bytes)
30 }
31
32 /// Deterministically derive an agent ID from a canonical seed string.
33 pub fn from_seed(seed: &str) -> Self {
34 let prefix = b"AIENOS_AGENT_GENESIS_v1:";
35 let seed_bytes = seed.as_bytes();
36 let mut hasher = sha256::Sha256::new();
37 hasher.update(prefix);
38 hasher.update(seed_bytes);
39 Self(hasher.finalize())
40 }
41
42 /// Return raw bytes.
43 pub const fn as_bytes(&self) -> &[u8; 32] {
44 &self.0
45 }
46}
47
48impl fmt::Debug for LogicalAgentId {
49 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50 write!(f, "AgentId({}", &self.to_string()[..12])?;
51 write!(f, "...)")
52 }
53}
54
55impl fmt::Display for LogicalAgentId {
56 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57 for b in &self.0 {
58 write!(f, "{:02x}", b)?;
59 }
60 Ok(())
61 }
62}
63
64/// Deterministic Logical Branch Identifier derived from parent branch + branch index.
65#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
66pub struct LogicalBranchId(pub [u8; 32]);
67
68impl LogicalBranchId {
69 /// Construct directly from 32 bytes.
70 pub const fn from_bytes(bytes: [u8; 32]) -> Self {
71 Self(bytes)
72 }
73
74 /// Create root branch for an agent.
75 pub fn root_branch(agent_id: LogicalAgentId) -> Self {
76 let mut hasher = sha256::Sha256::new();
77 hasher.update(b"AIENOS_ROOT_BRANCH_v1:");
78 hasher.update(agent_id.as_bytes());
79 Self(hasher.finalize())
80 }
81
82 /// Deterministically derive a child branch ID from a parent branch ID and a child index.
83 pub fn derive(parent: LogicalBranchId, branch_index: u64) -> Self {
84 let mut hasher = sha256::Sha256::new();
85 hasher.update(b"AIENOS_CHILD_BRANCH_v1:");
86 hasher.update(&parent.0);
87 hasher.update(&branch_index.to_be_bytes());
88 Self(hasher.finalize())
89 }
90
91 /// Return raw bytes.
92 pub const fn as_bytes(&self) -> &[u8; 32] {
93 &self.0
94 }
95}
96
97impl fmt::Debug for LogicalBranchId {
98 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
99 write!(f, "BranchId({}", &self.to_string()[..12])?;
100 write!(f, "...)")
101 }
102}
103
104impl fmt::Display for LogicalBranchId {
105 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
106 for b in &self.0 {
107 write!(f, "{:02x}", b)?;
108 }
109 Ok(())
110 }
111}
112
113#[cfg(test)]
114mod tests {
115 use super::*;
116
117 #[test]
118 fn test_agent_id_determinism() {
119 let id1 = LogicalAgentId::from_seed("agent-alpha");
120 let id2 = LogicalAgentId::from_seed("agent-alpha");
121 let id3 = LogicalAgentId::from_seed("agent-beta");
122
123 assert_eq!(id1, id2);
124 assert_ne!(id1, id3);
125 assert_eq!(id1.to_string().len(), 64);
126 }
127
128 #[test]
129 fn test_branch_id_derivation() {
130 let agent = LogicalAgentId::from_seed("spark-agent");
131 let root = LogicalBranchId::root_branch(agent);
132 let child0 = LogicalBranchId::derive(root, 0);
133 let child1 = LogicalBranchId::derive(root, 1);
134 let child0_repeat = LogicalBranchId::derive(root, 0);
135
136 assert_eq!(child0, child0_repeat);
137 assert_ne!(child0, child1);
138 assert_ne!(root, child0);
139 }
140}