refactor(tier_manager): Phase B language-advantage pass — dichotomy wave

Behavior-identical (frozen battery 65+5 + live parity 1720/1720 +
freeze --verify 121/121 + both pins 12/12 re-run green on THIS commit,
against the freshly built release .so):
- tier_get: exhaustive bracket match — the decision inputs (rock,
  wash>=20, parsed last, parsed now) dispatch in one match; the recency
  ladder is scoped inside the both-parse arm and every fall-through
  lands in the oracle's warm default. The tier is computed once and
  allocated once at exit (was: five early-return 'into()' sites).
- TIER_CONFIG closed set hoisted to a const table — config lookup,
  should_search and should_compress all derive from ONE table (mirrors
  the oracle's single dict; three independent encodings would drift).
  Unknown tiers ride the warm row / False exactly like the oracle's
  .get defaults.
- cargo fmt pass over the crate's battery files (formatting only).

Evidence: verification/summary-tier_manager.md + tier-* exits/logs.
This commit is contained in:
Nick committed 2026-10-14 16:02:25 +01:00
1 parent e54360d0b0
commit 04120edd73
4 files changed
+107 -53

No files matched your search

+86 -38
View File
@@ -811,25 +811,33 @@ pub fn tier_get(entry: &Json, hot_days: f64, warm_days: f64, now: &str) -> Json
Some(Json::Str(s)) => s.as_str(),
_ => "washable",
};
if rock == "rock" {
return Json::Str("hot".into());
}
if wash >= 20.0 {
return Json::Str("hot".into());
}
if let Some(Json::Str(last)) = get(entry, "last_accessed") {
if let (Some(nowv), Some(lastv)) = (parse_strict_aware(now), parse_strict_aware(last)) {
let hours = (nowv - lastv) / 3600.0;
if hours < hot_days * 24.0 {
return Json::Str("hot".into());
let last = match get(entry, "last_accessed") {
Some(Json::Str(s)) => Some(s.as_str()),
_ => None,
};
// Exhaustive bracket dispatch: the tier is decided exactly once — the
// recency ladder is scoped inside the both-timestamps-parse arm, and
// every fall-through (missing key, unparsable timestamp, unparsable
// now) lands in the oracle's warm default. Single allocation at exit.
let tier = match (rock, wash >= 20.0, last, parse_strict_aware(now)) {
("rock", _, _, _) => "hot",
(_, true, _, _) => "hot",
(_, _, Some(last), Some(nowv)) => match parse_strict_aware(last) {
Some(lastv) => {
let hours = (nowv - lastv) / 3600.0;
if hours < hot_days * 24.0 {
"hot"
} else if hours < warm_days * 24.0 {
"warm"
} else {
"cold"
}
}
if hours < warm_days * 24.0 {
return Json::Str("warm".into());
}
return Json::Str("cold".into());
}
}
Json::Str("warm".into())
None => "warm",
},
_ => "warm",
};
Json::Str(tier.into())
}
pub fn parse_strict_aware(s: &str) -> Option<f64> {
@@ -943,6 +951,46 @@ pub fn tier_demote(entry: &Json, hot_days: f64, warm_days: f64, now: &str) -> Js
// ── tier_manager gap-close kernels (dichotomy wave) ─────────────────────────
/// The oracle's TIER_CONFIG as ONE closed set — config lookup and the
/// should_search/should_compress projections all derive from this table
/// (the Python module has a single dict; three independent encodings
/// would drift). (name, indexed, cached, compressed, searched, description)
const TIER_CONFIG_TABLE: [(&str, bool, bool, bool, bool, &str); 3] = [
(
"hot",
true,
true,
false,
true,
"Instant access, indexed, cached",
),
(
"warm",
true,
false,
false,
true,
"Fast access, indexed, not cached",
),
(
"cold",
false,
false,
true,
false,
"Slow access, compressed, not searched",
),
];
fn tier_config_row(tier: &str) -> &'static (&'static str, bool, bool, bool, bool, &'static str) {
// Unknown tiers ride the warm row exactly like
// TIER_CONFIG.get(tier, TIER_CONFIG["warm"]).
TIER_CONFIG_TABLE
.iter()
.find(|&&(name, ..)| name == tier)
.unwrap_or(&TIER_CONFIG_TABLE[1])
}
/// demotion_sweep: the sweep operation's named alias of the get_tier
/// decision (oracle docstring: "Same as get_tier but explicitly named
/// for the sweep operation") — thresholds + now arrive as inputs.
@@ -955,38 +1003,38 @@ pub fn tier_demotion_sweep(entry: &Json, hot_days: f64, warm_days: f64, now: &st
/// insertion order (indexed, cached, compressed, searched, description)
/// — the envelope wire order is what the Python facade hands back.
pub fn tier_get_tier_config(tier: &str) -> Json {
let fields = match tier {
"hot" => (true, true, false, true, "Instant access, indexed, cached"),
"cold" => (
false,
false,
true,
false,
"Slow access, compressed, not searched",
),
// warm — and every tier the oracle dict doesn't define, because
// TIER_CONFIG.get(tier, TIER_CONFIG["warm"]) defaults to warm.
_ => (true, false, false, true, "Fast access, indexed, not cached"),
};
let row = tier_config_row(tier);
Json::Obj(vec![
("indexed".to_string(), Json::Bool(fields.0)),
("cached".to_string(), Json::Bool(fields.1)),
("compressed".to_string(), Json::Bool(fields.2)),
("searched".to_string(), Json::Bool(fields.3)),
("description".to_string(), Json::Str(fields.4.into())),
("indexed".to_string(), Json::Bool(row.1)),
("cached".to_string(), Json::Bool(row.2)),
("compressed".to_string(), Json::Bool(row.3)),
("searched".to_string(), Json::Bool(row.4)),
("description".to_string(), Json::Str(row.5.into())),
])
}
/// should_search: TIER_CONFIG["searched"] projection; unknown tiers
/// default the empty dict ({"searched": False}) per the oracle.
pub fn tier_should_search(tier: &str) -> Json {
Json::Bool(matches!(tier, "hot" | "warm"))
Json::Bool(
TIER_CONFIG_TABLE
.iter()
.find(|&&(name, ..)| name == tier)
.map(|&r| r.4)
.unwrap_or(false),
)
}
/// should_compress: TIER_CONFIG["compressed"] projection; unknown tiers
/// default the empty dict ({"compressed": False}) per the oracle.
pub fn tier_should_compress(tier: &str) -> Json {
Json::Bool(tier == "cold")
Json::Bool(
TIER_CONFIG_TABLE
.iter()
.find(|&&(name, ..)| name == tier)
.map(|&r| r.3)
.unwrap_or(false),
)
}
// ── witness kernels (W3.4) ──────────────────────────────────────────────────
@@ -171,10 +171,15 @@ fn llm_payload_battery() {
fn write_store_battery() {
replay_state("write_store", |i| {
Json::Arr(
broker::write_store_argv(&s(i, "py"), &s(i, "store_py"), &s(i, "text"), &s(i, "topic"))
.into_iter()
.map(Json::Str)
.collect(),
broker::write_store_argv(
&s(i, "py"),
&s(i, "store_py"),
&s(i, "text"),
&s(i, "topic"),
)
.into_iter()
.map(Json::Str)
.collect(),
)
});
}
@@ -130,8 +130,13 @@ fn pattern_triples_prefix_lattice() {
}
assert_eq!(
counts,
[("DRIFT", 10), ("GENUINENESS", 4), ("SYCOPHANCY", 18), ("WEAKNESS", 3)]
.into_iter()
.collect()
[
("DRIFT", 10),
("GENUINENESS", 4),
("SYCOPHANCY", 18),
("WEAKNESS", 3)
]
.into_iter()
.collect()
);
}
@@ -49,12 +49,10 @@ mod battery {
fn replay(kernel: &str, args: &[Json]) -> Json {
match kernel {
"safety_keyword_stopwords" => Json::Arr(broker::safety_keyword_stopwords()),
"safety_cc_probe" => Json::Bool(broker::safety_cc_probe(
matches!(args[0], Json::Bool(true)),
)),
"safety_cc_loader_pin" => {
Json::Bool(broker::safety_cc_loader_pin(&s_arg(&args[0])))
"safety_cc_probe" => {
Json::Bool(broker::safety_cc_probe(matches!(args[0], Json::Bool(true))))
}
"safety_cc_loader_pin" => Json::Bool(broker::safety_cc_loader_pin(&s_arg(&args[0]))),
"safety_py_matches" => Json::Bool(broker::safety_py_matches(
&s_arg(&args[0]),
&arr_arg(&args[1]),
@@ -88,9 +86,7 @@ mod battery {
"safety_get_patterns_text" => {
Json::Str(broker::safety_get_patterns_text(&arr_arg(&args[0])))
}
"safety_clear_patterns" => {
Json::Arr(broker::safety_clear_patterns(&arr_arg(&args[0])))
}
"safety_clear_patterns" => Json::Arr(broker::safety_clear_patterns(&arr_arg(&args[0]))),
"safety_self_test" => broker::safety_self_test(&s_arg(&args[0])),
other => panic!("unknown kernel in frozen truth: {other}"),
}