aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / crates / aienos-agent-state / src / incarnation.rs
| 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 | //! Ephemeral execution incarnations and decoupled physical inference states. |
| 17 | |
| 18 | use crate::history::TokenHistory; |
| 19 | use crate::id::{LogicalAgentId, LogicalBranchId}; |
| 20 | use crate::lineage::{EpistemicRefs, Lineage}; |
| 21 | use serde::{Deserialize, Serialize}; |
| 22 | |
| 23 | /// Monotonic boot / process incarnation counter. |
| 24 | pub type IncarnationId = u64; |
| 25 | |
| 26 | /// Ephemeral scheduler slot identifier. |
| 27 | pub type SequenceId = u32; |
| 28 | |
| 29 | /// State of accelerator/hardware inference pipeline. |
| 30 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 31 | pub enum ComputeState { |
| 32 | Idle, |
| 33 | Prefilling, |
| 34 | Decoding, |
| 35 | Suspended, |
| 36 | Evicted, |
| 37 | } |
| 38 | |
| 39 | /// Ephemeral physical inference allocation. |
| 40 | /// |
| 41 | /// Losing this state costs computation (recompute / cache reload), NEVER identity. |
| 42 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 43 | pub struct PhysicalInferenceState { |
| 44 | pub kv_page_table_ref: Option<usize>, |
| 45 | pub accelerator_buffer_ref: Option<usize>, |
| 46 | pub compute_state: ComputeState, |
| 47 | pub allocated_tokens_in_kv: usize, |
| 48 | } |
| 49 | |
| 50 | impl Default for PhysicalInferenceState { |
| 51 | fn default() -> Self { |
| 52 | Self { |
| 53 | kv_page_table_ref: None, |
| 54 | accelerator_buffer_ref: None, |
| 55 | compute_state: ComputeState::Idle, |
| 56 | allocated_tokens_in_kv: 0, |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /// Ephemeral per-boot/process execution instance. |
| 62 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 63 | pub struct ExecutionIncarnation { |
| 64 | pub incarnation_id: IncarnationId, |
| 65 | pub sequence_id: SequenceId, |
| 66 | pub physical_state: PhysicalInferenceState, |
| 67 | } |
| 68 | |
| 69 | impl ExecutionIncarnation { |
| 70 | /// Create new active execution incarnation. |
| 71 | pub fn new(incarnation_id: IncarnationId, sequence_id: SequenceId) -> Self { |
| 72 | Self { |
| 73 | incarnation_id, |
| 74 | sequence_id, |
| 75 | physical_state: PhysicalInferenceState::default(), |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /// Mark incarnation as evicted (hardware resources reclaimed). |
| 80 | pub fn evict(&mut self) { |
| 81 | self.physical_state.kv_page_table_ref = None; |
| 82 | self.physical_state.accelerator_buffer_ref = None; |
| 83 | self.physical_state.allocated_tokens_in_kv = 0; |
| 84 | self.physical_state.compute_state = ComputeState::Evicted; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /// Persistent Logical Branch entity holding complete semantic identity. |
| 89 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 90 | pub struct LogicalBranch { |
| 91 | pub branch_id: LogicalBranchId, |
| 92 | pub parent_branch_id: Option<LogicalBranchId>, |
| 93 | pub agent_id: LogicalAgentId, |
| 94 | pub token_history: TokenHistory, |
| 95 | pub lineage: Lineage, |
| 96 | pub epistemic_refs: EpistemicRefs, |
| 97 | pub created_at_utc: u64, |
| 98 | } |
| 99 | |
| 100 | impl LogicalBranch { |
| 101 | /// Initialize a new root branch for an agent. |
| 102 | pub fn new_root(agent_id: LogicalAgentId, timestamp: u64) -> Self { |
| 103 | let branch_id = LogicalBranchId::root_branch(agent_id); |
| 104 | Self { |
| 105 | branch_id, |
| 106 | parent_branch_id: None, |
| 107 | agent_id, |
| 108 | token_history: TokenHistory::new(), |
| 109 | lineage: Lineage::new_root(agent_id), |
| 110 | epistemic_refs: EpistemicRefs::new(), |
| 111 | created_at_utc: timestamp, |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /// Fork this branch into a child branch, inheriting token history and extending lineage. |
| 116 | pub fn fork(&self, child_id: LogicalBranchId, timestamp: u64) -> Self { |
| 117 | Self { |
| 118 | branch_id: child_id, |
| 119 | parent_branch_id: Some(self.branch_id), |
| 120 | agent_id: self.agent_id, |
| 121 | token_history: self.token_history.fork(), |
| 122 | lineage: self.lineage.extend(child_id), |
| 123 | epistemic_refs: self.epistemic_refs.clone(), |
| 124 | created_at_utc: timestamp, |
| 125 | } |
| 126 | } |
| 127 | } |