refactor(identity_anchors): Phase B language-advantage pass — dichotomy wave
broker: the five behavior lattices parse once per process (OnceLock caches behind a cached_lattice! macro — drift checks run per response, re-parsing 3KB of static JSON every call was pure waste); the [pattern, message, prefix] row construction folds into one prefixed() combinator shared by _pattern_triples and check_genuineness (the duplicated pair-unpacking loops are gone); _pattern_triples' table mutation becomes an array+chain+flat_map pipeline; the show renderers write through std::fmt::Write directly into the output String (no temp format! allocations), show_pronouns' two mark-loops become one [(key, mark)] walk. Behavior identical: battery 60 green (44 frozen cases replay VERBATIM), clippy -D warnings 0, live parity 88/88 both pins, pytest 48/48 both pins.
This commit is contained in:
1 parent
62eb2bf90e
commit
9964e01e56
1 file changed
+93
-84
@@ -414,89 +414,95 @@ fn lattice(raw: &str) -> Json {
|
||||
crate::pyjson::parse(raw).expect("embedded lattice must parse")
|
||||
}
|
||||
|
||||
/// _announcement_patterns: the announcement lattice (identity-announcement
|
||||
/// anti-patterns with their corrective messages).
|
||||
pub fn _announcement_patterns() -> Json {
|
||||
lattice(ANNOUNCEMENT_PATTERNS_JSON)
|
||||
/// The lattices are static data — parse once per process (drift checks run
|
||||
/// per response; re-parsing 3KB of JSON every call is pure waste).
|
||||
macro_rules! cached_lattice {
|
||||
($(#[$meta:meta])* $name:ident, $raw:ident) => {
|
||||
$(#[$meta])*
|
||||
pub fn $name() -> Json {
|
||||
static CACHE: std::sync::OnceLock<Json> = std::sync::OnceLock::new();
|
||||
CACHE.get_or_init(|| lattice($raw)).clone()
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// _pronoun_patterns: the pronoun-confusion lattice.
|
||||
pub fn _pronoun_patterns() -> Json {
|
||||
lattice(PRONOUN_PATTERNS_JSON)
|
||||
}
|
||||
cached_lattice!(
|
||||
/// _announcement_patterns: the announcement lattice (identity-announcement
|
||||
/// anti-patterns with their corrective messages).
|
||||
_announcement_patterns, ANNOUNCEMENT_PATTERNS_JSON);
|
||||
|
||||
/// _hedge_patterns: the evidence-free hedging lattice.
|
||||
pub fn _hedge_patterns() -> Json {
|
||||
lattice(HEDGE_PATTERNS_JSON)
|
||||
}
|
||||
cached_lattice!(
|
||||
/// _pronoun_patterns: the pronoun-confusion lattice.
|
||||
_pronoun_patterns, PRONOUN_PATTERNS_JSON);
|
||||
|
||||
/// _sycophancy_patterns: the sycophancy lattice (agreement, flattery,
|
||||
/// self-deprecation, validation-seeking, disagreement-avoidance).
|
||||
pub fn _sycophancy_patterns() -> Json {
|
||||
lattice(SYCOPHANCY_PATTERNS_JSON)
|
||||
}
|
||||
cached_lattice!(
|
||||
/// _hedge_patterns: the evidence-free hedging lattice.
|
||||
_hedge_patterns, HEDGE_PATTERNS_JSON);
|
||||
|
||||
/// PERFORMATIVE_PATTERNS (module data): the genuineness lattice.
|
||||
fn performative_patterns() -> Json {
|
||||
lattice(PERFORMATIVE_PATTERNS_JSON)
|
||||
cached_lattice!(
|
||||
/// _sycophancy_patterns: the sycophancy lattice (agreement, flattery,
|
||||
/// self-deprecation, validation-seeking, disagreement-avoidance).
|
||||
_sycophancy_patterns, SYCOPHANCY_PATTERNS_JSON);
|
||||
|
||||
cached_lattice!(
|
||||
/// PERFORMATIVE_PATTERNS (module data): the genuineness lattice.
|
||||
performative_patterns, PERFORMATIVE_PATTERNS_JSON);
|
||||
|
||||
/// One [pattern, message, prefix] row (pairs truncate-and-tag; the Python
|
||||
/// comprehension takes exactly the first two fields).
|
||||
fn prefixed(table: &Json, prefix: &str) -> Vec<Json> {
|
||||
match table {
|
||||
Json::Arr(rows) => rows
|
||||
.iter()
|
||||
.filter_map(|row| {
|
||||
if let Json::Arr(pair) = row {
|
||||
if pair.len() >= 2 {
|
||||
return Some(Json::Arr(vec![
|
||||
pair[0].clone(),
|
||||
pair[1].clone(),
|
||||
Json::Str(prefix.to_string()),
|
||||
]));
|
||||
}
|
||||
}
|
||||
None
|
||||
})
|
||||
.collect(),
|
||||
_ => vec![],
|
||||
}
|
||||
}
|
||||
|
||||
/// _pattern_triples: the [pattern, message, prefix] triples — the data
|
||||
/// authority the drift kernel matches over (batch 124: the GENUINENESS table
|
||||
/// rides here too, so kernel and fallback see one lattice).
|
||||
pub fn _pattern_triples() -> Json {
|
||||
let mut tables: Vec<(String, Json)> = vec![
|
||||
("DRIFT".into(), _announcement_patterns()),
|
||||
("WEAKNESS".into(), _hedge_patterns()),
|
||||
("SYCOPHANCY".into(), _sycophancy_patterns()),
|
||||
("GENUINENESS".into(), performative_patterns()),
|
||||
];
|
||||
// DRIFT = announcement + pronoun, in that order (the fallback's loop
|
||||
// order — the frozen truth freezes exactly this sequence).
|
||||
if let Some(drift) = tables.first_mut() {
|
||||
if let Json::Arr(rows) = &mut drift.1 {
|
||||
if let Json::Arr(pronoun_rows) = _pronoun_patterns() {
|
||||
rows.extend(pronoun_rows);
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut triples = vec![];
|
||||
for (prefix, table) in &tables {
|
||||
if let Json::Arr(rows) = table {
|
||||
for row in rows {
|
||||
if let Json::Arr(pair) = row {
|
||||
if pair.len() >= 2 {
|
||||
triples.push(Json::Arr(vec![
|
||||
pair[0].clone(),
|
||||
pair[1].clone(),
|
||||
Json::Str(prefix.clone()),
|
||||
]));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Json::Arr(triples)
|
||||
let drift: Vec<Json> = match (
|
||||
_announcement_patterns(),
|
||||
_pronoun_patterns(),
|
||||
) {
|
||||
(Json::Arr(a), Json::Arr(p)) => a.into_iter().chain(p).collect(),
|
||||
(Json::Arr(a), _) => a,
|
||||
(_, Json::Arr(p)) => p,
|
||||
_ => vec![],
|
||||
};
|
||||
Json::Arr(
|
||||
[
|
||||
(Json::Arr(drift), "DRIFT"),
|
||||
(_hedge_patterns(), "WEAKNESS"),
|
||||
(_sycophancy_patterns(), "SYCOPHANCY"),
|
||||
(performative_patterns(), "GENUINENESS"),
|
||||
]
|
||||
.into_iter()
|
||||
.flat_map(|(table, prefix)| prefixed(&table, prefix))
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
|
||||
/// check_genuineness: performative-language matching over the embedded
|
||||
/// genuineness lattice (the same matching drift_check applies to triples).
|
||||
pub fn check_genuineness(text: &str) -> Vec<String> {
|
||||
let mut triples = vec![];
|
||||
if let Json::Arr(rows) = performative_patterns() {
|
||||
for row in rows {
|
||||
if let Json::Arr(pair) = row {
|
||||
if pair.len() >= 2 {
|
||||
triples.push(Json::Arr(vec![
|
||||
pair[0].clone(),
|
||||
pair[1].clone(),
|
||||
Json::Str("GENUINENESS".into()),
|
||||
]));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
drift_check(text, &triples)
|
||||
drift_check(text, &prefixed(&performative_patterns(), "GENUINENESS"))
|
||||
}
|
||||
|
||||
/// _cc_core: the kernel mirror of the facade's availability probe — the pin
|
||||
@@ -514,14 +520,15 @@ pub fn _cc_core(pin: Option<&str>) -> Option<Json> {
|
||||
/// show_anchors: renders the ANCHORS display (category headers upper-cased,
|
||||
/// bulleted examples — the exact print-bytes of the oracle).
|
||||
pub fn show_anchors(anchors: &Json) -> String {
|
||||
use std::fmt::Write;
|
||||
let mut out = String::new();
|
||||
if let Json::Obj(pairs) = anchors {
|
||||
for (category, examples) in pairs {
|
||||
out.push_str(&format!("\n {}:\n", category.to_uppercase()));
|
||||
let _ = write!(out, "\n {}:\n", category.to_uppercase());
|
||||
if let Json::Arr(items) = examples {
|
||||
for ex in items {
|
||||
if let Json::Str(s) = ex {
|
||||
out.push_str(&format!(" • {s}\n"));
|
||||
let _ = writeln!(out, " • {s}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -533,13 +540,18 @@ pub fn show_anchors(anchors: &Json) -> String {
|
||||
/// show_rules: renders the embodiment rules (anti-pattern ✗, pattern ✓,
|
||||
/// reason → per rule).
|
||||
pub fn show_rules(rules: &Json) -> String {
|
||||
use std::fmt::Write;
|
||||
let mut out = String::new();
|
||||
if let Json::Arr(items) = rules {
|
||||
for rule in items {
|
||||
out.push_str(&format!("\n RULE: {}\n", str_or(get(rule, "rule"), "")));
|
||||
out.push_str(&format!(" ✗ {}\n", str_or(get(rule, "anti_pattern"), "")));
|
||||
out.push_str(&format!(" ✓ {}\n", str_or(get(rule, "pattern"), "")));
|
||||
out.push_str(&format!(" → {}\n", str_or(get(rule, "reason"), "")));
|
||||
let _ = write!(
|
||||
out,
|
||||
"\n RULE: {}\n ✗ {}\n ✓ {}\n → {}\n",
|
||||
str_or(get(rule, "rule"), ""),
|
||||
str_or(get(rule, "anti_pattern"), ""),
|
||||
str_or(get(rule, "pattern"), ""),
|
||||
str_or(get(rule, "reason"), "")
|
||||
);
|
||||
}
|
||||
}
|
||||
out
|
||||
@@ -548,25 +560,22 @@ pub fn show_rules(rules: &Json) -> String {
|
||||
/// show_pronouns: renders the pronoun disambiguation rules (examples ✓,
|
||||
/// anti-patterns ✗ when present — 'it' carries none).
|
||||
pub fn show_pronouns(pronouns: &Json) -> String {
|
||||
use std::fmt::Write;
|
||||
let mut out = String::new();
|
||||
if let Json::Obj(pairs) = pronouns {
|
||||
for (pronoun, info) in pairs {
|
||||
out.push_str(&format!(
|
||||
let _ = write!(
|
||||
out,
|
||||
"\n '{}' → {}\n",
|
||||
pronoun,
|
||||
str_or(get(info, "refers_to"), "")
|
||||
));
|
||||
if let Some(Json::Arr(examples)) = get(info, "examples") {
|
||||
for ex in examples {
|
||||
if let Json::Str(s) = ex {
|
||||
out.push_str(&format!(" ✓ {s}\n"));
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(Json::Arr(anti)) = get(info, "anti_patterns") {
|
||||
for ap in anti {
|
||||
if let Json::Str(s) = ap {
|
||||
out.push_str(&format!(" ✗ {s}\n"));
|
||||
);
|
||||
for (key, mark) in [("examples", "✓"), ("anti_patterns", "✗")] {
|
||||
if let Some(Json::Arr(rows)) = get(info, key) {
|
||||
for row in rows {
|
||||
if let Json::Str(s) = row {
|
||||
let _ = writeln!(out, " {mark} {s}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user