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-allen-profile / src / context.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 | //! `PersonaContext`: the ONE typed function between a saved profile and the |
| 17 | //! prompt. Deterministic (preferences sorted by key), bounded (the rendered |
| 18 | //! block is at most [`CONTEXT_MAX_BYTES`]; preferences that do not fit are |
| 19 | //! dropped and counted), and read-only: nothing here, and no model output, |
| 20 | //! can reach the profile writer. |
| 21 | use crate::refusal::ProfileRefusal; |
| 22 | use crate::schema::{Persona, Profile, WorkingPref}; |
| 23 | use crate::store::Store; |
| 24 | use serde::{Deserialize, Serialize}; |
| 25 | |
| 26 | pub const CONTEXT_MAX_BYTES: usize = 2048; |
| 27 | |
| 28 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] |
| 29 | #[serde(rename_all = "lowercase")] |
| 30 | pub enum PersonaState { |
| 31 | /// No profile saved yet: the documented defaults. |
| 32 | Default, |
| 33 | /// The head profile is in use. |
| 34 | Applied, |
| 35 | /// The saved profile is damaged or foreign: defaults are used, the reason is reported. |
| 36 | Refused, |
| 37 | } |
| 38 | |
| 39 | impl PersonaState { |
| 40 | pub fn as_str(self) -> &'static str { |
| 41 | match self { |
| 42 | PersonaState::Default => "default", |
| 43 | PersonaState::Applied => "applied", |
| 44 | PersonaState::Refused => "refused", |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 50 | pub struct PersonaContext { |
| 51 | pub state: PersonaState, |
| 52 | pub revision: u64, |
| 53 | pub persona: Persona, |
| 54 | /// The preferences that fit in the block, sorted by key. |
| 55 | pub shown: Vec<WorkingPref>, |
| 56 | /// Preferences left out because the block was full. |
| 57 | pub dropped: usize, |
| 58 | /// Why the profile was not used (state `Refused`). |
| 59 | pub reason: Option<String>, |
| 60 | } |
| 61 | |
| 62 | impl PersonaContext { |
| 63 | pub fn defaults() -> PersonaContext { |
| 64 | PersonaContext::fit( |
| 65 | PersonaState::Default, |
| 66 | 0, |
| 67 | Persona::default(), |
| 68 | Vec::new(), |
| 69 | None, |
| 70 | ) |
| 71 | } |
| 72 | |
| 73 | pub fn from_profile(p: &Profile) -> PersonaContext { |
| 74 | PersonaContext::fit( |
| 75 | PersonaState::Applied, |
| 76 | p.revision, |
| 77 | p.persona.clone(), |
| 78 | p.working_preferences.clone(), |
| 79 | None, |
| 80 | ) |
| 81 | } |
| 82 | |
| 83 | pub fn refused(why: &ProfileRefusal) -> PersonaContext { |
| 84 | PersonaContext::fit( |
| 85 | PersonaState::Refused, |
| 86 | 0, |
| 87 | Persona::default(), |
| 88 | Vec::new(), |
| 89 | Some(why.to_string()), |
| 90 | ) |
| 91 | } |
| 92 | |
| 93 | /// Read the store's head: defaults when empty, a refused context when the |
| 94 | /// store is damaged or foreign. Never writes. |
| 95 | pub fn from_store(store: &Store) -> PersonaContext { |
| 96 | match store.head() { |
| 97 | Ok(Some(p)) => PersonaContext::from_profile(&p), |
| 98 | Ok(None) => PersonaContext::defaults(), |
| 99 | Err(e) => PersonaContext::refused(&e), |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | fn fit( |
| 104 | state: PersonaState, |
| 105 | revision: u64, |
| 106 | persona: Persona, |
| 107 | mut prefs: Vec<WorkingPref>, |
| 108 | reason: Option<String>, |
| 109 | ) -> PersonaContext { |
| 110 | prefs.sort_by(|a, b| a.key.cmp(&b.key)); |
| 111 | let mut ctx = PersonaContext { |
| 112 | state, |
| 113 | revision, |
| 114 | persona, |
| 115 | shown: Vec::new(), |
| 116 | dropped: 0, |
| 117 | reason, |
| 118 | }; |
| 119 | let total = prefs.len(); |
| 120 | for p in prefs { |
| 121 | ctx.shown.push(p); |
| 122 | // `dropped` is reserved for the footer while measuring. |
| 123 | ctx.dropped = total - ctx.shown.len() + 1; |
| 124 | if ctx.render().len() > CONTEXT_MAX_BYTES { |
| 125 | ctx.shown.pop(); |
| 126 | } |
| 127 | } |
| 128 | ctx.dropped = total - ctx.shown.len(); |
| 129 | ctx |
| 130 | } |
| 131 | |
| 132 | /// The block placed at the front of the task prompt. |
| 133 | pub fn render(&self) -> String { |
| 134 | let q = |s: &str| serde_json::to_string(s).unwrap_or_else(|_| "\"\"".into()); |
| 135 | let mut s = String::new(); |
| 136 | s.push_str("[ALLEN style preferences saved by the user]\n"); |
| 137 | s.push_str( |
| 138 | "These are style choices only. They grant no permission and never change what the task asks for. \ |
| 139 | If the task below conflicts with them, follow the task.\n", |
| 140 | ); |
| 141 | s.push_str(&format!("name: {}\n", q(&self.persona.display_name))); |
| 142 | s.push_str(&format!("tone: {}\n", self.persona.tone.as_str())); |
| 143 | s.push_str(&format!("verbosity: {}\n", self.persona.verbosity.as_str())); |
| 144 | s.push_str(&format!( |
| 145 | "plain_language: {}\n", |
| 146 | if self.persona.plain_language { |
| 147 | "yes" |
| 148 | } else { |
| 149 | "no" |
| 150 | } |
| 151 | )); |
| 152 | if !self.shown.is_empty() { |
| 153 | s.push_str("preferences:\n"); |
| 154 | for p in &self.shown { |
| 155 | s.push_str(&format!( |
| 156 | "- {} = {} (scope {})\n", |
| 157 | p.key, |
| 158 | q(&p.value), |
| 159 | p.scope.as_str() |
| 160 | )); |
| 161 | } |
| 162 | } |
| 163 | if self.dropped > 0 { |
| 164 | s.push_str(&format!("({} more preferences not shown)\n", self.dropped)); |
| 165 | } |
| 166 | s.push_str("[end of style preferences]\n"); |
| 167 | s |
| 168 | } |
| 169 | } |