interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / crates / allen-reflex / examples / common / mod.rs

139 lines · 4526 bytes Raw Blame History
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//! Host-side helpers shared by the examples (std allowed here; the core stays no_std).
17#![allow(dead_code)]
18
19use allen_reflex::{exam_turns, Action, Config, Reflex};
20use std::path::PathBuf;
21
22/// One exam case. `id`, `cat`, `action` and `text` are labels: the harness keeps them,
23/// the core only ever sees `turns`.
24#[derive(Clone, Debug)]
25pub struct Case {
26 pub id: String,
27 pub family: String,
28 pub cat: String,
29 pub action: Action,
30 pub text: String,
31 pub turns: String,
32 /// "contract", "train:Dxxx" (extra file) or the split name
33 pub source: String,
34}
35
36pub fn exam_dir() -> PathBuf {
37 PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../docs/native/pi5-8/orchestrator-exam")
38}
39
40/// Read cases-<split>.psv (id|cat|action|text|turns) or the extra file
41/// (id|family|cat|action|text|turns|source).
42pub fn load(split: &str) -> Vec<Case> {
43 if split == "heldout" && std::env::var("ALLEN_REFLEX_HELDOUT_ONCE").ok().as_deref() != Some("1")
44 {
45 panic!(
46 "held-out is sealed: set ALLEN_REFLEX_HELDOUT_ONCE=1 only for the single final scoring"
47 );
48 }
49 let name = if split == "extra" {
50 "cases-reflex-extra.psv".to_string()
51 } else {
52 format!("cases-{split}.psv")
53 };
54 let p = exam_dir().join(name);
55 let s = std::fs::read_to_string(&p).unwrap_or_else(|e| panic!("{}: {e}", p.display()));
56 let mut out = Vec::new();
57 for line in s.lines() {
58 if line.starts_with('#') || line.trim().is_empty() {
59 continue;
60 }
61 let f: Vec<&str> = line
62 .splitn(if split == "extra" { 7 } else { 5 }, '|')
63 .collect();
64 let (id, fam, cat, act, text, turns, src) = if split == "extra" {
65 assert!(f.len() == 7, "bad extra line: {line}");
66 (f[0], f[1], f[2], f[3], f[4], f[5], f[6])
67 } else {
68 assert!(f.len() == 5, "bad line: {line}");
69 (f[0], f[0], f[1], f[2], f[3], f[4], split)
70 };
71 out.push(Case {
72 id: id.into(),
73 family: fam.into(),
74 cat: cat.into(),
75 action: Action::from_name(act).unwrap_or_else(|| panic!("bad action {act}")),
76 text: text.into(),
77 turns: turns.into(),
78 source: src.into(),
79 });
80 }
81 out
82}
83
84/// Train a fresh core on `cases`.
85pub fn build(cfg: Config, cases: &[&Case]) -> Reflex {
86 let mut r = Reflex::new(cfg);
87 for c in cases {
88 let t = exam_turns(&c.turns);
89 let txt = if c.text.is_empty() {
90 None
91 } else {
92 Some(c.text.as_str())
93 };
94 r.train(&t, c.action, txt).expect("train");
95 }
96 r.finish_training();
97 r
98}
99
100/// The training pool: train split + our own extra cases.
101pub fn pool() -> Vec<Case> {
102 let mut v = load("train");
103 v.extend(load("extra"));
104 v
105}
106
107/// Score as score.sh does: (action ok, exact ok).
108pub fn judge(c: &Case, d: &allen_reflex::Decision) -> (bool, bool) {
109 let a = d.action == c.action;
110 if !a {
111 return (false, false);
112 }
113 if c.text.is_empty() {
114 return (true, true);
115 }
116 let n = |s: &str| -> String {
117 let l = s.to_ascii_lowercase();
118 let l = l.split_whitespace().collect::<Vec<_>>().join(" ");
119 l.trim_end_matches(|ch: char| ".!?;,".contains(ch) || ch.is_whitespace())
120 .trim()
121 .to_string()
122 };
123 (true, d.text.as_deref().map(n) == Some(n(&c.text)))
124}
125
126pub fn esc(s: &str) -> String {
127 let mut o = String::with_capacity(s.len() + 8);
128 for ch in s.chars() {
129 match ch {
130 '"' => o.push_str("\\\""),
131 '\\' => o.push_str("\\\\"),
132 '\n' => o.push_str("\\n"),
133 '\t' => o.push_str("\\t"),
134 c if (c as u32) < 0x20 => o.push_str(&format!("\\u{:04x}", c as u32)),
135 c => o.push(c),
136 }
137 }
138 o
139}