oxios-kernel 0.2.0

Oxios kernel: supervisor, event bus, state store
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
//! Core capability types for the Oxios capability system.
//!
//! Capabilities are unforgeable tokens that encode authority over specific
//! resources. An agent's capability space (CSpace) is the complete set of
//! capabilities it holds.
//!
//! # Design
//!
//! Inspired by capability-based security (seL4, Capsicum), each capability
//! binds a set of rights to a specific resource. Capabilities cannot be
//! forged — they are issued by the kernel or by agents with DELEGATE rights.
//!
//! ```
//! use oxios_kernel::capability::types::*;
//! use oxios_kernel::capability::template::CapabilityTemplate;
//!
//! let cspace = CapabilityTemplate::worker().build();
//! assert!(!cspace.is_empty());
//! ```

use std::collections::HashMap;
use std::fmt;

use serde::{Deserialize, Serialize};

use crate::space::SpaceId;
use crate::types::AgentId;

/// Unique identifier for a capability.
///
/// Each capability receives a random UUID at creation time, making
/// capabilities unforgeable — an agent cannot guess a valid CapabilityId.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct CapabilityId(pub uuid::Uuid);

impl CapabilityId {
    /// Generate a new random capability ID.
    pub fn new() -> Self {
        Self(uuid::Uuid::new_v4())
    }
}

impl fmt::Display for CapabilityId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "cap:{}", self.0)
    }
}

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

/// Who issued a capability.
///
/// The kernel is the root authority. Agents with DELEGATE rights can
/// issue derived capabilities scoped to their own authority.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(tag = "issuer", content = "id")]
pub enum Issuer {
    /// The Oxios kernel — root authority.
    Kernel,
    /// An agent that delegated a subset of its own authority.
    Agent(AgentId),
}

impl fmt::Display for Issuer {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Issuer::Kernel => write!(f, "kernel"),
            Issuer::Agent(id) => write!(f, "agent:{}", id),
        }
    }
}

/// Bit-flag rights encoded in a capability.
///
/// Rights are represented as a `u8` bitmask. Standard combinations are
/// provided as associated constants.
///
/// | Flag     | Value |
/// |----------|-------|
/// | NONE     | 0x00  |
/// | READ     | 0x01  |
/// | WRITE    | 0x02  |
/// | EXECUTE  | 0x04  |
/// | DELEGATE | 0x08  |
/// | ALL      | 0x0F  |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Rights(pub u8);

impl Rights {
    /// No rights at all.
    pub const NONE: Rights = Rights(0x00);
    /// Read access to a resource.
    pub const READ: Rights = Rights(0x01);
    /// Write / mutate access to a resource.
    pub const WRITE: Rights = Rights(0x02);
    /// Execute a resource (run a program, invoke a tool).
    pub const EXECUTE: Rights = Rights(0x04);
    /// Right to delegate a subset of this capability to another agent.
    pub const DELEGATE: Rights = Rights(0x08);
    /// All rights combined.
    pub const ALL: Rights = Rights(0x0F);

    /// Returns `true` if this set contains all the given rights.
    pub fn contains(self, other: Rights) -> bool {
        (self.0 & other.0) == other.0
    }

    /// Returns a new Rights set with the given flags added.
    pub fn union(self, other: Rights) -> Rights {
        Rights(self.0 | other.0)
    }

    /// Returns a new Rights set with only the flags present in both.
    pub fn intersect(self, other: Rights) -> Rights {
        Rights(self.0 & other.0)
    }

    /// Returns true if no rights are set.
    pub fn is_empty(self) -> bool {
        self.0 == 0
    }
}

impl fmt::Display for Rights {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.0 == Rights::ALL.0 {
            return write!(f, "ALL");
        }
        if self.0 == Rights::NONE.0 {
            return write!(f, "NONE");
        }
        let mut parts = Vec::new();
        if self.contains(Rights::READ) {
            parts.push("R");
        }
        if self.contains(Rights::WRITE) {
            parts.push("W");
        }
        if self.contains(Rights::EXECUTE) {
            parts.push("X");
        }
        if self.contains(Rights::DELEGATE) {
            parts.push("D");
        }
        write!(f, "{}", parts.join("|"))
    }
}

impl std::ops::BitOr for Rights {
    type Output = Self;
    fn bitor(self, rhs: Self) -> Self {
        Rights(self.0 | rhs.0)
    }
}

impl std::ops::BitAnd for Rights {
    type Output = Self;
    fn bitand(self, rhs: Self) -> Self {
        Rights(self.0 & rhs.0)
    }
}

/// A reference to a protected resource.
///
/// Resources are the objects that capabilities govern. Each variant
/// identifies a distinct resource class in the Oxios kernel.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(tag = "kind", content = "ref")]
pub enum ResourceRef {
    /// A kernel domain (e.g., "memory", "scheduler").
    KernelDomain {
        /// Domain name within the kernel.
        domain: String,
    },
    /// An installed program.
    Program {
        /// Program name as registered in the program store.
        name: String,
    },
    /// A workspace space.
    Space {
        /// Space identifier.
        id: SpaceId,
    },
    /// Another agent (for inter-agent communication).
    Agent {
        /// Agent identifier.
        id: AgentId,
    },
    /// Command execution (shell or structured).
    Exec {
        /// Execution mode: "shell" or "structured".
        mode: String,
    },
    /// Headless browser access.
    Browser,
    /// Agent-to-agent communication channel.
    A2a,
    /// MCP (Model Context Protocol) server bridge.
    Mcp {
        /// MCP server name.
        server: String,
    },
}

impl fmt::Display for ResourceRef {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ResourceRef::KernelDomain { domain } => write!(f, "kernel:{}", domain),
            ResourceRef::Program { name } => write!(f, "program:{}", name),
            ResourceRef::Space { id } => write!(f, "space:{}", id),
            ResourceRef::Agent { id } => write!(f, "agent:{}", id),
            ResourceRef::Exec { mode } => write!(f, "exec:{}", mode),
            ResourceRef::Browser => write!(f, "browser"),
            ResourceRef::A2a => write!(f, "a2a"),
            ResourceRef::Mcp { server } => write!(f, "mcp:{}", server),
        }
    }
}

/// An unforgeable token encoding authority over a specific resource.
///
/// A capability binds a set of `Rights` to a `ResourceRef`, and records
/// who issued it. Agents present capabilities to the `AccessManager` to
/// prove they are authorised to perform an action.
///
/// # Immutability
///
/// Capabilities are immutable once created. To change rights, create a
/// new capability and replace the old one in the CSpace.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Capability {
    /// Unique identifier for this capability.
    pub id: CapabilityId,
    /// The resource this capability governs.
    pub resource: ResourceRef,
    /// The rights granted over the resource.
    pub rights: Rights,
    /// Who issued this capability.
    pub issuer: Issuer,
}

impl Capability {
    /// Creates a new kernel-issued capability with the given resource and rights.
    pub fn kernel(resource: ResourceRef, rights: Rights) -> Self {
        Self {
            id: CapabilityId::new(),
            resource,
            rights,
            issuer: Issuer::Kernel,
        }
    }

    /// Creates a new agent-issued capability (delegation).
    pub fn delegated(resource: ResourceRef, rights: Rights, issuer: AgentId) -> Self {
        Self {
            id: CapabilityId::new(),
            resource,
            rights,
            issuer: Issuer::Agent(issuer),
        }
    }

    /// Returns true if this capability grants the requested rights.
    pub fn grants(&self, required: Rights) -> bool {
        self.rights.contains(required)
    }
}

/// An agent's **capability space**: the complete set of capabilities it holds.
///
/// The CSpace is a `HashMap<CapabilityId, Capability>`. When the
/// `AccessManager` checks whether an agent may perform an action, it
/// scans the agent's CSpace for a capability matching the target
/// resource with sufficient rights.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CSpace {
    /// The agent that owns this capability space.
    pub agent_id: AgentId,
    /// Map from capability ID to capability.
    caps: HashMap<CapabilityId, Capability>,
}

impl CSpace {
    /// Creates an empty CSpace for the given agent.
    pub fn new(agent_id: AgentId) -> Self {
        Self {
            agent_id,
            caps: HashMap::new(),
        }
    }

    /// Inserts a capability into this space, returning the old one if replaced.
    pub fn insert(&mut self, cap: Capability) -> Option<Capability> {
        self.caps.insert(cap.id, cap)
    }

    /// Removes a capability by ID.
    pub fn remove(&mut self, id: &CapabilityId) -> Option<Capability> {
        self.caps.remove(id)
    }

    /// Looks up a capability by ID.
    pub fn get(&self, id: &CapabilityId) -> Option<&Capability> {
        self.caps.get(id)
    }

    /// Returns true if any capability in this space matches the resource
    /// and grants the requested rights.
    pub fn can(&self, resource: &ResourceRef, required: Rights) -> bool {
        self.caps
            .values()
            .any(|cap| &cap.resource == resource && cap.grants(required))
    }

    /// Returns an iterator over all capabilities in this space.
    pub fn iter(&self) -> impl Iterator<Item = &Capability> {
        self.caps.values()
    }

    /// Returns the number of capabilities.
    pub fn len(&self) -> usize {
        self.caps.len()
    }

    /// Returns true if there are no capabilities.
    pub fn is_empty(&self) -> bool {
        self.caps.is_empty()
    }

    /// Retains only capabilities for which `pred` returns true.
    pub fn retain(&mut self, pred: impl Fn(&Capability) -> bool) {
        self.caps.retain(|_, cap| pred(cap));
    }

    /// Returns capabilities matching a specific resource type filter.
    pub fn filter_resource(&self, f: impl Fn(&ResourceRef) -> bool) -> Vec<&Capability> {
        self.caps.values().filter(|c| f(&c.resource)).collect()
    }

    /// Returns the unique kernel domain names active in this CSpace.
    ///
    /// Useful for building a kernel manifest for the system prompt.
    pub fn active_domains(&self) -> Vec<&str> {
        let mut domains: Vec<&str> = self
            .caps
            .values()
            .filter_map(|cap| match &cap.resource {
                ResourceRef::KernelDomain { domain } => Some(domain.as_str()),
                ResourceRef::Exec { .. } => Some("exec"),
                ResourceRef::Browser => Some("browser"),
                _ => None,
            })
            .collect();
        domains.sort();
        domains.dedup();
        domains
    }
}

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

    #[test]
    fn rights_bit_ops() {
        let rw = Rights::READ | Rights::WRITE;
        assert!(rw.contains(Rights::READ));
        assert!(rw.contains(Rights::WRITE));
        assert!(!rw.contains(Rights::EXECUTE));

        let r = rw.intersect(Rights::READ);
        assert_eq!(r, Rights::READ);
    }

    #[test]
    fn rights_display() {
        assert_eq!(format!("{}", Rights::ALL), "ALL");
        assert_eq!(format!("{}", Rights::NONE), "NONE");
        assert_eq!(format!("{}", Rights::READ | Rights::EXECUTE), "R|X");
    }

    #[test]
    fn cspace_can_check() {
        let agent = AgentId::new_v4();
        let mut cs = CSpace::new(agent);
        let cap = Capability::kernel(
            ResourceRef::Exec {
                mode: "shell".into(),
            },
            Rights::READ | Rights::EXECUTE,
        );
        cs.insert(cap);

        assert!(cs.can(
            &ResourceRef::Exec {
                mode: "shell".into()
            },
            Rights::EXECUTE
        ));
        assert!(!cs.can(
            &ResourceRef::Exec {
                mode: "shell".into()
            },
            Rights::WRITE
        ));
        assert!(!cs.can(&ResourceRef::Browser, Rights::READ));
    }

    #[test]
    fn capability_delegation() {
        let issuer = AgentId::new_v4();
        let cap = Capability::delegated(
            ResourceRef::Agent { id: issuer },
            Rights::READ | Rights::DELEGATE,
            issuer,
        );
        assert!(matches!(cap.issuer, Issuer::Agent(_)));
        assert!(cap.grants(Rights::DELEGATE));
    }
}