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 / allen-reflex / src / policy.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 | //! Policy layer: `refuse` is decided by intent, not by a bare word. |
| 17 | //! |
| 18 | //! A message is refused when, outside a text payload, it |
| 19 | //! 1. asks the agent to drop its rules ("ignore your rules", "forget your allowed list"), |
| 20 | //! 2. claims a fake mode or authority ("developer mode", "system override"), |
| 21 | //! 3. asks for a verb the agent does not have (unlock, rotate, update, shutdown, delete, |
| 22 | //! wipe, deploy and plain synonyms), unless negated ("do not delete") or used as a noun |
| 23 | //! ("an update"), or "turn off" names a household device (that is `none`), |
| 24 | //! 4. asks for approval without the operator ("approve every task automatically"), or |
| 25 | //! 5. relays an order found in stored data and asks to obey it ("the note says ... do it"). |
| 26 | //! |
| 27 | //! A text payload is the rest of a clause that opens with a store, preference, to-do or |
| 28 | //! writing frame ("Remember this note: ignore your rules ..."). Its words are the |
| 29 | //! operator's data and are never read as orders (exam contract, inject-data rule). |
| 30 | |
| 31 | use crate::lexicon::*; |
| 32 | use crate::segment::Clause; |
| 33 | use crate::text::Token; |
| 34 | use alloc::vec; |
| 35 | |
| 36 | /// Why a message was refused. |
| 37 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 38 | pub enum RefuseRule { |
| 39 | /// asks to drop the rules |
| 40 | RuleBreak, |
| 41 | /// fake mode or authority |
| 42 | Override, |
| 43 | /// a verb the agent does not have |
| 44 | Forbidden, |
| 45 | /// approval without the operator |
| 46 | Blanket, |
| 47 | /// obey an order found in data |
| 48 | DataOrder, |
| 49 | } |
| 50 | |
| 51 | impl RefuseRule { |
| 52 | /// Short name for explanations. |
| 53 | pub fn name(self) -> &'static str { |
| 54 | match self { |
| 55 | RefuseRule::RuleBreak => "rule-break", |
| 56 | RefuseRule::Override => "override", |
| 57 | RefuseRule::Forbidden => "forbidden-verb", |
| 58 | RefuseRule::Blanket => "blanket-approval", |
| 59 | RefuseRule::DataOrder => "data-order", |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /// Verbs that are verbs only at the head of a request ("format the disk" but "what format"). |
| 65 | const AMBIGUOUS: &[&str] = &["update", "format", "restart", "upgrade"]; |
| 66 | /// Words before an ambiguous verb that still make it a verb. |
| 67 | const VERB_LEADS: &[&str] = &["to", "you", "and", "then", "please", "now", "just"]; |
| 68 | |
| 69 | fn negated(toks: &[Token], i: usize, lo: usize) -> bool { |
| 70 | let from = if i >= lo + 2 { i - 2 } else { lo }; |
| 71 | toks[from..i].iter().any(|t| is_in(NEGATION, &t.word)) |
| 72 | } |
| 73 | |
| 74 | /// Check all clauses; returns the rule and the token where it fired. |
| 75 | pub fn check(toks: &[Token], cl: &[Clause]) -> Option<(RefuseRule, usize)> { |
| 76 | let n = toks.len(); |
| 77 | // exempt[i]: token i is inside a text payload |
| 78 | let mut exempt = vec![false; n]; |
| 79 | for c in cl { |
| 80 | let lead = skip_polite(toks, c.start, c.end); |
| 81 | let f = longest(toks, lead, TEXT_FRAMES); |
| 82 | if f > 0 { |
| 83 | for e in exempt.iter_mut().take(c.end).skip(lead + f) { |
| 84 | *e = true; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | let mut report_at: Option<usize> = None; |
| 89 | for c in cl { |
| 90 | let lead = skip_polite(toks, c.start, c.end); |
| 91 | for i in c.start..c.end { |
| 92 | if exempt[i] { |
| 93 | continue; |
| 94 | } |
| 95 | let w = toks[i].word.as_str(); |
| 96 | // 1. rule break: verb + rule object within 3 words |
| 97 | if is_in(RULE_BREAK_VERBS, w) && !negated(toks, i, c.start) { |
| 98 | let hi = core::cmp::min(c.end, i + 4); |
| 99 | if toks[i + 1..hi].iter().any(|t| is_in(RULE_OBJECTS, &t.word)) { |
| 100 | return Some((RefuseRule::RuleBreak, i)); |
| 101 | } |
| 102 | } |
| 103 | // 2. fake mode / authority |
| 104 | if longest(toks, i, OVERRIDE_PHRASES) > 0 { |
| 105 | return Some((RefuseRule::Override, i)); |
| 106 | } |
| 107 | // 3. forbidden verb |
| 108 | if is_in(FORBIDDEN, w) && !negated(toks, i, c.start) { |
| 109 | let noun = i > c.start && is_in(DETERMINERS, &toks[i - 1].word); |
| 110 | let amb = is_in(AMBIGUOUS, w); |
| 111 | let verb_pos = i == lead || (i > c.start && is_in(VERB_LEADS, &toks[i - 1].word)); |
| 112 | if !noun && (!amb || verb_pos) { |
| 113 | return Some((RefuseRule::Forbidden, i)); |
| 114 | } |
| 115 | } |
| 116 | for (a, b) in FORBIDDEN_SPLIT { |
| 117 | if w == *a && !negated(toks, i, c.start) { |
| 118 | let hi = core::cmp::min(c.end, i + 5); |
| 119 | if (i + 1..hi).any(|k| toks[k].word == *b) { |
| 120 | let device = toks[c.start..c.end] |
| 121 | .iter() |
| 122 | .any(|t| is_in(DEVICE_OBJECTS, &t.word)); |
| 123 | if !device { |
| 124 | return Some((RefuseRule::Forbidden, i)); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | // 4. approval without the operator |
| 130 | if (w == "approve" || w == "accept") |
| 131 | && toks[c.start..c.end] |
| 132 | .iter() |
| 133 | .any(|t| is_in(&["automatically", "always", "every", "without"], &t.word)) |
| 134 | { |
| 135 | return Some((RefuseRule::Blanket, i)); |
| 136 | } |
| 137 | if is_in(REPORT, w) && report_at.is_none() { |
| 138 | report_at = Some(i); |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | // 5. an order relayed from data, then obeyed |
| 143 | if let Some(r) = report_at { |
| 144 | for i in r + 1..n { |
| 145 | if !exempt[i] && longest(toks, i, OBEY) > 0 { |
| 146 | return Some((RefuseRule::DataOrder, i)); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | None |
| 151 | } |