Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/crates/allen-reflex-kernel/src/runner.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / crates / allen-reflex-kernel / src / runner.rs` 171 lines ยท 5349 bytes Raw Blame History 1 //! One trained core and one decision line per case. Shared by the kernel entry points and the 2 //! host harness (`examples/host_exam.rs`), so both print the same bytes for the same case. 3 //! 4 //! The core gets the turns only. Learning is switched off before the first decision (scoring 5 //! rule of `docs/native/PI5-8-ALLEN-REFLEX.md`), and `decide` takes the core by shared 6 //! reference, so no decision can change the next one. 7 8 use allen_reflex::{exam_turns, Config, Mode, Reflex}; 9 use alloc::string::String; 10 11 /// Line prefix of a decision. 12 pub const LINE_TAG: &str = "ALLEN_D"; 13 14 /// Mode for a number on the C side (0 reflex, 1 nearest, 2 perceptron). 15 pub fn mode_of(n: u32) -> Option { 16 match n { 17 0 => Some(Mode::Reflex), 18 1 => Some(Mode::Nearest), 19 2 => Some(Mode::Perceptron), 20 _ => None, 21 } 22 } 23 24 /// A trained core for one mode. 25 pub struct Session { 26 core: Reflex, 27 examples: usize, 28 } 29 30 impl Session { 31 /// Train a fresh core with the frozen configuration of `mode` on the compiled-in pool, 32 /// then switch learning off. 33 pub fn train(mode: Mode) -> Result { 34 let mut core = Reflex::new(Config::frozen(mode)); 35 let mut err = None; 36 let examples = crate::pool::for_each(|ex| { 37 if err.is_some() { 38 return; 39 } 40 let t = exam_turns(ex.turns); 41 let text = if ex.text.is_empty() { 42 None 43 } else { 44 Some(ex.text) 45 }; 46 if core.train(&t, ex.action, text).is_err() { 47 err = Some("training refused an example"); 48 } 49 })?; 50 if let Some(e) = err { 51 return Err(e); 52 } 53 core.finish_training(); 54 core.set_learning(false); 55 Ok(Session { core, examples }) 56 } 57 58 /// Mode name. 59 pub fn mode(&self) -> Mode { 60 self.core.config().mode 61 } 62 63 /// Training examples stored. 64 pub fn examples(&self) -> usize { 65 self.examples 66 } 67 68 /// Learned-state bytes reported by the core (capacity based). 69 pub fn memory_bytes(&self) -> usize { 70 self.core.memory_bytes() 71 } 72 73 /// Exemplars kept by the core. 74 pub fn exemplars(&self) -> usize { 75 self.core.exemplars() 76 } 77 78 /// The decision JSON for `turns`. Nothing else of the core's output leaves this function. 79 pub fn decide_json(&self, turns: &str) -> String { 80 let t = exam_turns(turns); 81 self.core.decide(&t).0.json() 82 } 83 } 84 85 /// `ALLEN_D ` into `out` (cleared first); returns the byte offset of the JSON. 86 pub fn line(out: &mut String, mode: Mode, id: &str, json: &str) -> usize { 87 out.clear(); 88 out.push_str(LINE_TAG); 89 out.push(' '); 90 out.push_str(mode.name()); 91 out.push(' '); 92 out.push_str(id); 93 out.push(' '); 94 let off = out.len(); 95 out.push_str(json); 96 off 97 } 98 99 /// Line prefix of an interactive route proposal (aienos#399). 100 pub const ROUTE_TAG: &str = "ALLEN_P"; 101 102 /// What the C side checks about one proposal (route, label, speaker, readiness). 103 #[derive(Clone, Copy, Debug, PartialEq, Eq)] 104 pub struct ProposalFacts { 105 /// `Route` index 106 pub route: u32, 107 /// the core's `Action` index 108 pub label: u32, 109 /// 1 owner, 0 guest 110 pub owner: u32, 111 /// 1 ready, 0 not ready 112 pub ready: u32, 113 } 114 115 impl Session { 116 /// The proposal JSON for one route card request in this image's context. `caps` are the 117 /// image's real capabilities (bits of [`allen_reflex::route::Caps::from_bits`]); the card 118 /// can only say who speaks, the consent and the pending state, never what the image has. 119 pub fn propose_json( 120 &self, 121 r: &crate::card::RouteRef, 122 turns_escaped: &str, 123 request_id: u64, 124 caps: u32, 125 ) -> (String, ProposalFacts) { 126 use allen_reflex::route::{self, Calibration, Caps, RouteCtx, RouteRequest, Speaker}; 127 let decoded = route::pct_decode(turns_escaped); 128 let text = decoded.as_deref().unwrap_or(""); 129 let t = exam_turns(text); 130 let ctx = RouteCtx { 131 speaker: if r.owner { 132 Speaker::Owner 133 } else { 134 Speaker::Guest 135 }, 136 memory_consent: r.consent, 137 pending: r.pending, 138 caps: Caps::from_bits(caps), 139 installed_model: None, 140 subject_id: [0u8; 32], 141 cal: Calibration::frozen(self.mode()), 142 }; 143 let req = RouteRequest { 144 request_id, 145 session_id: r.session, 146 turns: if decoded.is_some() { &t } else { &[] }, 147 }; 148 let p = route::propose(&self.core, &req, &ctx); 149 let facts = ProposalFacts { 150 route: p.route as u32, 151 label: p.label.idx() as u32, 152 owner: r.owner as u32, 153 ready: p.not_ready.is_none() as u32, 154 }; 155 (p.json(), facts) 156 } 157 } 158 159 /// `ALLEN_P ` into `out` (cleared first); returns the byte offset of the JSON. 160 pub fn route_line(out: &mut String, mode: Mode, id: &str, json: &str) -> usize { 161 out.clear(); 162 out.push_str(ROUTE_TAG); 163 out.push(' '); 164 out.push_str(mode.name()); 165 out.push(' '); 166 out.push_str(id); 167 out.push(' '); 168 let off = out.len(); 169 out.push_str(json); 170 off 171 }