aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / aien-runtime / src / persona.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 | //! ALLEN persona profile glue (arch#159): turns the engaged identity plus the |
| 17 | //! profile store into (a) the prompt prefix and report of a compose task and |
| 18 | //! (b) the answers to the `Allen*` control commands. |
| 19 | //! |
| 20 | //! The profile store is written ONLY by `aien-allen-profile`, and only from |
| 21 | //! the operator commands handled here. Nothing in the compose task path |
| 22 | //! (model text, proposals, workspace files) calls a writer. |
| 23 | use crate::control::{ |
| 24 | AllenHistoryReport, AllenProfileReport, AllenRefusalReport, AllenStatusReport, ControlCommand, |
| 25 | ControlResponse, DeploymentSummary, PersonaReport, |
| 26 | }; |
| 27 | use aien_allen::Resolved; |
| 28 | use aien_allen_profile::{Identity, Persona, PersonaContext, ProfileRefusal, Store}; |
| 29 | use std::path::Path; |
| 30 | |
| 31 | /// Facilities in the plan that v1 does not have. Reported, never given a control. |
| 32 | pub const UNSUPPORTED: [&str; 2] = ["avatar", "voice"]; |
| 33 | |
| 34 | /// `None` = ALLEN is not engaged: no persona text, no change in behaviour. |
| 35 | pub fn context_for(dir: &Path, id: Option<&Resolved>) -> Option<PersonaContext> { |
| 36 | let r = id?; |
| 37 | let store = Store::new(dir, Identity::from(r)); |
| 38 | let ctx = PersonaContext::from_store(&store); |
| 39 | if let Some(why) = &ctx.reason { |
| 40 | tracing::warn!("ALLEN persona profile not used (defaults apply): {why}"); |
| 41 | } |
| 42 | Some(ctx) |
| 43 | } |
| 44 | |
| 45 | pub fn report_for(ctx: Option<&PersonaContext>) -> PersonaReport { |
| 46 | match ctx { |
| 47 | None => PersonaReport { |
| 48 | state: "not_engaged".into(), |
| 49 | display_name: String::new(), |
| 50 | revision: 0, |
| 51 | reason: None, |
| 52 | }, |
| 53 | Some(c) => PersonaReport { |
| 54 | state: c.state.as_str().into(), |
| 55 | display_name: c.persona.display_name.clone(), |
| 56 | revision: c.revision, |
| 57 | reason: c.reason.clone(), |
| 58 | }, |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /// The persona block in front of the task prompt; the prompt itself is |
| 63 | /// unchanged when there is no context. |
| 64 | pub fn prefix_prompt(prompt: &str, ctx: Option<&PersonaContext>) -> String { |
| 65 | match ctx { |
| 66 | None => prompt.to_string(), |
| 67 | Some(c) => format!("{}\n{prompt}", c.render()), |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | fn refused(e: &ProfileRefusal) -> ControlResponse { |
| 72 | let current_revision = match e { |
| 73 | ProfileRefusal::StaleUpdate { current, .. } => Some(*current), |
| 74 | _ => None, |
| 75 | }; |
| 76 | ControlResponse::AllenRefused(Box::new(AllenRefusalReport { |
| 77 | code: e.code().into(), |
| 78 | message: e.to_string(), |
| 79 | current_revision, |
| 80 | })) |
| 81 | } |
| 82 | |
| 83 | fn profile_report(store: &Store) -> Result<ControlResponse, ProfileRefusal> { |
| 84 | let head = store.head()?; |
| 85 | Ok(ControlResponse::AllenProfile(Box::new(match head { |
| 86 | Some(p) => AllenProfileReport { |
| 87 | revision: p.revision, |
| 88 | state: "applied".into(), |
| 89 | profile: Some(p), |
| 90 | defaults: None, |
| 91 | }, |
| 92 | None => AllenProfileReport { |
| 93 | revision: 0, |
| 94 | state: "default".into(), |
| 95 | profile: None, |
| 96 | defaults: Some(Persona::default()), |
| 97 | }, |
| 98 | }))) |
| 99 | } |
| 100 | |
| 101 | fn written(p: aien_allen_profile::Profile) -> ControlResponse { |
| 102 | ControlResponse::AllenProfile(Box::new(AllenProfileReport { |
| 103 | revision: p.revision, |
| 104 | state: "applied".into(), |
| 105 | profile: Some(p), |
| 106 | defaults: None, |
| 107 | })) |
| 108 | } |
| 109 | |
| 110 | /// Answer one `Allen*` command. `id` is the identity this daemon resolved |
| 111 | /// (`None` = not engaged); `label` is the proposer label the daemon runs. |
| 112 | pub fn handle_allen_command( |
| 113 | dir: &Path, |
| 114 | id: Option<&Resolved>, |
| 115 | label: &str, |
| 116 | cmd: &ControlCommand, |
| 117 | ) -> ControlResponse { |
| 118 | if let ControlCommand::AllenStatus = cmd { |
| 119 | return status(dir, id, label); |
| 120 | } |
| 121 | let Some(r) = id else { |
| 122 | return refused(&ProfileRefusal::NotEngaged); |
| 123 | }; |
| 124 | let store = Store::new(dir, Identity::from(r)); |
| 125 | let out = match cmd { |
| 126 | ControlCommand::AllenProfileShow => profile_report(&store), |
| 127 | ControlCommand::AllenProfileSet { |
| 128 | expected_revision, |
| 129 | changes, |
| 130 | } => store.set(*expected_revision, changes).map(written), |
| 131 | ControlCommand::AllenProfileHistory => store |
| 132 | .history() |
| 133 | .map(|entries| ControlResponse::AllenHistory(Box::new(AllenHistoryReport { entries }))), |
| 134 | ControlCommand::AllenProfileRevert { |
| 135 | expected_revision, |
| 136 | to, |
| 137 | } => store.revert(*expected_revision, *to).map(written), |
| 138 | ControlCommand::AllenProfileReset { expected_revision } => { |
| 139 | store.reset(*expected_revision).map(written) |
| 140 | } |
| 141 | _ => { |
| 142 | return ControlResponse::Error("not an ALLEN profile command".into()); |
| 143 | } |
| 144 | }; |
| 145 | out.unwrap_or_else(|e| refused(&e)) |
| 146 | } |
| 147 | |
| 148 | fn status(dir: &Path, id: Option<&Resolved>, label: &str) -> ControlResponse { |
| 149 | let ctx = context_for(dir, id); |
| 150 | let deployment = id.and_then(|r| { |
| 151 | aien_allen::deployment::verify(&aien_allen::deployment::deployments_path(dir), &r.agent) |
| 152 | .ok() |
| 153 | .and_then(|v| v.last().cloned()) |
| 154 | .map(|e| DeploymentSummary { |
| 155 | seq: e.seq, |
| 156 | candidate_id: e.candidate_id, |
| 157 | placeholder: e.placeholder, |
| 158 | }) |
| 159 | }); |
| 160 | ControlResponse::AllenStatusReport(Box::new(AllenStatusReport { |
| 161 | identity: if id.is_some() { |
| 162 | "engaged" |
| 163 | } else { |
| 164 | "not_engaged" |
| 165 | } |
| 166 | .into(), |
| 167 | fingerprint: id.map(|r| Identity::from(r).fingerprint()), |
| 168 | head_sequence: id.map(|r| r.head_seq), |
| 169 | chain_verified: id.map(|r| r.chain_verified), |
| 170 | persona: report_for(ctx.as_ref()), |
| 171 | model: label.to_string(), |
| 172 | execution_mode: if label.starts_with("model:") { |
| 173 | "local_model" |
| 174 | } else { |
| 175 | "stub" |
| 176 | } |
| 177 | .into(), |
| 178 | deployment, |
| 179 | unsupported: UNSUPPORTED.iter().map(|s| s.to_string()).collect(), |
| 180 | })) |
| 181 | } |