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

Op::run — the enum owns its execution: every variant's semantics live
next to its name and the exhaustive match moved into the type (adding an
op without semantics stays a compile error, now enforced where the
behavior lives, not at the call site). _write_snapshot_doc as a
filter_map-collect iterator chain over the transfer array (no mutable
accumulator; the entry-key projection is one declarative pipeline).

Behavior unchanged: battery 7/7, live parity 60/60, clippy -D warnings
clean, module tests 5/5 both pins. Evidence: verification/to-phaseb-parity.exit.
This commit is contained in:
Nick committed 2026-10-14 14:43:43 +01:00
1 parent 19c062e15c
commit 35add00493
2 files changed
+40 -25

No files matched your search

+39 -25
View File
@@ -307,12 +307,13 @@ fn snapshot_doc(st: &State) -> Value {
/// JSON object keyed by entry key). The makedirs/tmp-file/`os.replace`
/// write stays facade (I/O law).
fn _write_snapshot_doc(snap: &Value) -> Value {
let mut cache = Map::new();
for e in snap.get("cache").and_then(Value::as_array).into_iter().flatten() {
if let Some(k) = e.get("key").and_then(Value::as_str) {
cache.insert(k.to_string(), e.clone());
}
}
let cache: Map<String, Value> = snap
.get("cache")
.and_then(Value::as_array)
.into_iter()
.flatten()
.filter_map(|e| Some((e.get("key")?.as_str()?.to_string(), e.clone())))
.collect();
json!({"cache": cache, "stats": snap.get("stats").cloned().unwrap_or(Value::Null)})
}
@@ -505,6 +506,37 @@ impl Op {
other => Err(format!("unknown op: {other}")),
}
}
/// Phase B: the enum owns its execution — every variant's behavior
/// lives next to its name and the match stays exhaustive (adding an op
/// without semantics is a compile error, not a silent fallthrough).
fn run(
self,
st: &mut State,
args: &Value,
now_iso: &str,
now: f64,
snap: &mut Option<Value>,
) -> Result<Value, String> {
let a = Args::from_value(args);
Ok(match self {
Op::GetOptimizer => {
let text = args.get("storage_text").and_then(Value::as_str);
*st = get_optimizer(text);
st._to_state()
}
Op::GetStaticCache => Value::String(get_static_cache(st, a.persona, now_iso, now, snap)),
Op::GetDynamicCache => Value::String(get_dynamic_cache(st, a.context, now_iso, now, snap)),
Op::OptimizePrompt => optimize_prompt(st, a.text, a.context, a.persona, now_iso, now, snap),
Op::GetStatus => get_status(st),
Op::GetCacheStats => get_cache_stats(st),
Op::MarkDirty => {
mark_dirty(st);
Value::Null
}
Op::SaveIfDirty => json!({"save": maybe_save(st, now, snap)}),
})
}
}
/// One borrowed argument view over the wire doc (Phase B: zero-copy —
@@ -538,25 +570,7 @@ fn _run_kernel(
now: f64,
snap: &mut Option<Value>,
) -> Result<Value, String> {
let op = Op::parse(op)?;
let a = Args::from_value(args);
Ok(match op {
Op::GetOptimizer => {
let text = args.get("storage_text").and_then(Value::as_str);
*st = get_optimizer(text);
st._to_state()
}
Op::GetStaticCache => Value::String(get_static_cache(st, a.persona, now_iso, now, snap)),
Op::GetDynamicCache => Value::String(get_dynamic_cache(st, a.context, now_iso, now, snap)),
Op::OptimizePrompt => optimize_prompt(st, a.text, a.context, a.persona, now_iso, now, snap),
Op::GetStatus => get_status(st),
Op::GetCacheStats => get_cache_stats(st),
Op::MarkDirty => {
mark_dirty(st);
Value::Null
}
Op::SaveIfDirty => json!({"save": maybe_save(st, now, snap)}),
})
Op::parse(op)?.run(st, args, now_iso, now, snap)
}
// ── FFI (doc-envelope law) ──────────────────────────────────────────────
+1
View File
@@ -0,0 +1 @@
0