dwarves-gui/tests/robot_mimir_thin_wire.rs
//! **OVER-THE-WIRE thin test + perf baseline for the Mímir lenses** (closes the
//! wire-audit gap on the merged `nornir-warehouse-embed → Mimir.*` re-point).
//!
//! The re-point moved the QUERY half of every dwarves lens into nornir behind the six
//! `Mimir.*` lens RPCs (ARCHITECTURE L5a); a THIN dwarves host (t14s → oden) asks the
//! server — which OWNS the warehouse — to fold, then renders the returned finding-set.
//! The audit found the seam had **no over-wire thin test and no bench**: nothing proved
//! the lenses actually travel the wire (as opposed to the pane quietly local-folding
//! `Warehouse.Scan`), and there was no perf baseline for the round-trip.
//!
//! This closes both, using ONLY the sanctioned in-process test seam
//! (`nornir_warehouse_embed::testkit::{MockMimir, spawn_mock_mimir}` — a real tonic
//! `Mimir` server on an ephemeral port, NOT a stub) so nothing is twinned (L5):
//!
//! 1. [`each_mimir_lens_rpc_carries_the_folded_findings_back_over_the_wire`] — drives
//! the dwarves THIN client (`nornir_warehouse_embed::mimir::*`) for EACH of the six
//! lens RPCs (GenericCost / ZombieFeatures / ChangeRisk / SimilarCode / WeakTests /
//! BenchGaps) against the in-proc server and asserts the client gets back EXACTLY the
//! JSON the server folded — a sentinel a local fold could never synthesise — proving
//! the finding-set crossed the wire. A dead endpoint maps to the honest typed
//! `Unreachable`, never a silent empty, so the value genuinely traversed the socket.
//! 2. [`the_thin_pane_consumes_the_wire_result_for_all_six_lenses`] — the SHIPPED thin
//! pane (`mimir_lens::lens_pane` / `LensPane::thin`) scanned against the in-proc
//! server emits `thin=true` and carries each lens's wire JSON verbatim in its
//! `state_json` — it renders what the server computed, it does not local-fold.
//! 3. [`live_pane_still_local_folds_warehouse_scan_thin_pane_does_not`] — the honest
//! record of the ONE pane site that still local-folds: `WarehouseLens` /
//! `LensPane::live` (the embedded/fat host path) reads RAW tables over
//! `Warehouse.Scan` and folds LOCALLY (`thin=false`), while the shipped default
//! folds on the server (`thin=true`). Both are driven over the wire so the split is
//! observable, not asserted from a doc comment.
//! 4. [`mimir_thin_perf_baseline`] — a timed bench of a local lens fold vs. the full
//! thin round-trip (client → in-proc server → fold → wire → client), recorded as a
//! bench-history record (printed as JSON + appended to a sidecar
//! `bench_history_mimir_thin.jsonl`) so there is a perf baseline to trend.
//!
//! Every functional check emits a `nornir-testmatrix` `functional_status` cell under
//! `--features testmatrix` (reuse edda; never a hand-rolled ledger).
use nornir_warehouse_embed::mimir as m;
use nornir_warehouse_embed::testkit::{spawn_mock_mimir, MockMimir};
use nornir_warehouse_embed::WarehouseReadError;
use serde_json::json;
#[allow(unused_variables)]
fn matrix(check: &str, ok: bool, detail: &str) {
#[cfg(feature = "testmatrix")]
nornir_testmatrix::functional_status("dwarves-gui", check, ok, detail);
}
/// A per-lens sentinel body the server folds and returns. Every field is a value a
/// LOCAL fold over a warehouse could never synthesise (`__over_wire__`, a per-lens
/// `nonce`), so seeing it back in the client / pane proves it crossed the wire.
fn sentinel(lens: &str, nonce: u64) -> serde_json::Value {
json!({
"__over_wire__": true,
"lens": lens,
"nonce": nonce,
"evidence_note": format!("{lens}: served by the in-proc Mimir over the wire."),
})
}
/// Seed a [`MockMimir`] so each of the six lens RPCs returns its own sentinel, and
/// hand back the `(nonce-per-lens)` map so a caller can assert the exact body returned.
fn seeded_server() -> (
nornir_warehouse_embed::testkit::MockWarehouseServer,
[(&'static str, u64); 6],
) {
let lenses: [(&str, u64); 6] = [
("generic_cost", 1001),
("zombie_features", 1002),
("change_risk", 1003),
("similar_code", 1004),
("weak_tests", 1005),
("bench_gaps", 1006),
];
let srv = spawn_mock_mimir(MockMimir {
generic_cost: sentinel("generic_cost", 1001).to_string(),
zombie_features: sentinel("zombie_features", 1002).to_string(),
change_risk: sentinel("change_risk", 1003).to_string(),
similar_code: sentinel("similar_code", 1004).to_string(),
weak_tests: sentinel("weak_tests", 1005).to_string(),
bench_gaps: sentinel("bench_gaps", 1006).to_string(),
..Default::default()
});
(srv, lenses)
}
/// **1 — EACH lens RPC carries the folded findings BACK OVER THE WIRE.**
///
/// RED-when-broken: if any `mimir::*` client leg stopped speaking the `Mimir` gRPC, or
/// the pane's thin path went back to local-folding, the sentinel body (which no local
/// fold can produce) would not come back, or the dead-port probe would return a body
/// instead of the typed `Unreachable`.
#[test]
fn each_mimir_lens_rpc_carries_the_folded_findings_back_over_the_wire() {
let (srv, lenses) = seeded_server();
let (e, t, w, repo) = (srv.endpoint.as_str(), "", "nordisk", "");
// Every one of the six lens client legs, driven against the in-proc server.
let got: Vec<(&str, serde_json::Value)> = vec![
(
"generic_cost",
m::generic_cost(e, t, w, repo, 0, 0).expect("GenericCost over wire"),
),
(
"zombie_features",
m::zombie_features(e, t, w, repo, 0).expect("ZombieFeatures over wire"),
),
(
"change_risk",
m::change_risk(e, t, w, repo, 0).expect("ChangeRisk over wire"),
),
(
"similar_code",
m::similar_code(e, t, w, repo, 0).expect("SimilarCode over wire"),
),
(
"weak_tests",
m::weak_tests(e, t, w, repo, 0).expect("WeakTests over wire"),
),
(
"bench_gaps",
m::bench_gaps(e, t, w, repo, 0).expect("BenchGaps over wire"),
),
];
let mut all_ok = true;
for ((lens, nonce), (name, body)) in lenses.iter().zip(got.iter()) {
assert_eq!(lens, name, "leg order pins the lens under test");
// The body is EXACTLY what the server folded — the over-wire sentinel, not a
// locally-synthesised report. A local fold has no `__over_wire__` field.
let ok = body == &sentinel(lens, *nonce);
assert!(
ok,
"{lens}: client got the server's folded body back over the wire: {body}"
);
assert_eq!(
body["__over_wire__"], true,
"{lens}: the body is the server's, not a local fold"
);
assert_eq!(
body["nonce"], *nonce,
"{lens}: the exact folded body, byte-for-byte"
);
all_ok &= ok;
}
// The value genuinely traversed the socket: a dead endpoint is the honest typed
// Unreachable, NEVER a silent empty that a local fold could stand in for.
drop(srv);
let dead = m::change_risk("http://127.0.0.1:1", "", "nordisk", "", 0).unwrap_err();
let dead_ok = matches!(dead, WarehouseReadError::Unreachable(_));
assert!(
dead_ok,
"a dead Mimir endpoint ⇒ Unreachable, not a body: {dead:?}"
);
matrix(
"mimir_thin_each_lens_rpc_over_the_wire",
all_ok && dead_ok,
"all SIX Mimir lens RPCs (GenericCost/ZombieFeatures/ChangeRisk/SimilarCode/\
WeakTests/BenchGaps) driven from the dwarves thin client against an in-process \
nornir Mimir gRPC server (testkit::spawn_mock_mimir): each returns the server's \
folded body verbatim over the wire (an __over_wire__ sentinel no local fold can \
make), and a dead endpoint maps to the honest typed Unreachable — proving the \
thin seam carries the lenses over gRPC, not via a local Warehouse.Scan fold.",
);
}
/// **2 — the SHIPPED thin pane consumes the WIRE result for all six lenses.**
///
/// The pane renders what the server folded (`thin=true`) and carries each lens's wire
/// JSON verbatim in its `state_json`; it adds no numbers of its own and does NOT
/// local-fold. RED-when-broken: a pane that reverted to the fat/local path would emit
/// `thin=false` and real report shapes instead of the six sentinels.
#[test]
fn the_thin_pane_consumes_the_wire_result_for_all_six_lenses() {
use dwarves_gui::mimir_lens::{lens_pane, LensPane};
use facett_core::Facet;
let (srv, lenses) = seeded_server();
// The SHIPPED constructor (`lens_pane`) is thin — assert on it directly so the
// default a user gets is the one under test, not just an internal `thin()` seam.
for mut pane in [
lens_pane(&srv.endpoint, "", "nordisk"),
LensPane::thin(&srv.endpoint, "", "nordisk"),
] {
pane.update_json("\"scan\"");
let s = pane.state_json();
assert_eq!(
s["thin"], true,
"the pane took the thin (server-folded) path: {}",
s["status"]
);
assert_eq!(s["scanned"], true);
assert!(
s["source"]
.as_str()
.unwrap_or_default()
.contains("Mimir.* RPCs"),
"the source is the Mimir.* RPC leg, not a local warehouse scan: {}",
s["source"]
);
// The pane state key each lens's wire body lands under.
let key_of = |lens: &str| match lens {
"generic_cost" => "bloat",
"zombie_features" => "zombie",
"change_risk" => "risk",
"similar_code" => "similar_code",
"weak_tests" => "weak_tests",
"bench_gaps" => "bench_gaps",
_ => unreachable!(),
};
for (lens, nonce) in &lenses {
let key = key_of(lens);
assert_eq!(
s[key],
sentinel(lens, *nonce),
"the pane carries the {lens} wire body verbatim under `{key}` (no local fold)"
);
}
}
drop(srv);
matrix(
"mimir_thin_pane_consumes_wire_result",
true,
"the shipped thin Lenses pane (mimir_lens::lens_pane / LensPane::thin), scanned \
against an in-process Mimir server, emits thin=true and carries all six lens \
bodies (bloat/zombie/risk/similar_code/weak_tests/bench_gaps) verbatim from the \
wire in its state_json — it renders the server's finding-set and never local-folds.",
);
}
/// **3 — honest record: `LensPane::live` still local-folds `Warehouse.Scan`; the
/// shipped thin pane does not.**
///
/// The one remaining pane site that local-folds is `WarehouseLens` (the embedded/fat
/// host path, `mimir_lens.rs`: `scan_warehouse_table` per table → local
/// `bloat/zombie/risk::analyze`). It is retained ON PURPOSE for a host that OWNS the
/// warehouse, so this is documented, not converted. Driven over the wire so the split
/// is observable: `live` reads RAW tables via `Warehouse.Scan` and folds LOCALLY
/// (`thin=false`); `thin` gets folded findings via `Mimir.*` (`thin=true`).
#[test]
fn live_pane_still_local_folds_warehouse_scan_thin_pane_does_not() {
use dwarves_gui::mimir_lens::{LensPane, RemoteLensSource, WarehouseLens};
use facett_core::Facet;
use nornir_warehouse_embed::testkit::spawn_mock_warehouse;
// A minimal warehouse the LOCAL fold can chew on (binary_facts × symbol_sizes give
// the bloat lens a non-empty artifact, so load() is Ok, not UNAVAILABLE).
let cols_bin = vec!["content_sha256", "repo", "artifact_path", "total_bytes"];
let cols_sym = vec![
"content_sha256",
"crate_name",
"symbol_demangled_no_generics",
"symbol_demangled",
"size_bytes",
"kind",
"is_monomorphization",
];
let to_s = |v: &[&str]| v.iter().map(|s| s.to_string()).collect::<Vec<_>>();
let wh = spawn_mock_warehouse(vec![
(
"binary_facts".to_string(),
to_s(&cols_bin),
vec![to_s(&["c1", "znippy", "/t/release/znippy", "1000000"])],
),
(
"symbol_sizes".to_string(),
to_s(&cols_sym),
vec![
to_s(&[
"c1",
"znippy",
"znippy::parse_stream",
"znippy::parse_stream::<u8>",
"20000",
"Text",
"true",
]),
to_s(&[
"c1",
"znippy",
"znippy::parse_stream",
"znippy::parse_stream::<u16>",
"20000",
"Text",
"true",
]),
to_s(&[
"c1",
"znippy",
"znippy::parse_stream",
"znippy::parse_stream::<u32>",
"20000",
"Text",
"true",
]),
],
),
]);
// The LIVE (embedded) pane: reads Warehouse.Scan, folds LOCALLY.
let mut live = LensPane::live(&wh.endpoint, "", "nordisk");
live.update_json("\"scan\"");
let ls = live.state_json();
let live_local = ls["thin"] == json!(false) && ls["scanned"] == json!(true);
assert!(
live_local,
"LensPane::live folds LOCALLY over Warehouse.Scan (thin=false): {}",
ls["status"]
);
// A real local fold produced numbers — the bloat report is a live shape, not a sentinel.
assert!(
ls["bloat"]["artifacts"].as_u64().unwrap_or(0) >= 1,
"the local fold produced an artifact: {}",
ls["bloat"]
);
assert_eq!(
ls["bloat"].get("__over_wire__"),
None,
"a LOCAL fold has no over-wire marker"
);
drop(wh);
// The seam-level contract that keeps the two honest: a WarehouseLens has NO remote
// fold (it will call Warehouse.Scan + local analyze), a RemoteLensSource DOES.
let local_src_folds_locally =
dwarves_gui::mimir_lens::LensSource::remote(&WarehouseLens::new("http://x", "", "w"))
.is_none();
let remote_src_folds_on_server =
dwarves_gui::mimir_lens::LensSource::remote(&RemoteLensSource::new("http://x", "", "w"))
.is_some();
assert!(
local_src_folds_locally,
"WarehouseLens is a LOCAL-fold source (Warehouse.Scan)"
);
assert!(
remote_src_folds_on_server,
"RemoteLensSource folds on the server (Mimir.*)"
);
matrix(
"mimir_live_local_fold_documented",
live_local && local_src_folds_locally && remote_src_folds_on_server,
"HONEST local-fold record: LensPane::live (WarehouseLens) still reads raw tables \
over Warehouse.Scan and folds LOCALLY (thin=false) — retained on purpose for a \
host that OWNS the warehouse — while the shipped default (LensPane::thin / \
RemoteLensSource) folds on the server via Mimir.* (thin=true). The split is \
driven over the wire and observable in state_json, not asserted from a comment.",
);
}
// ── 4. perf baseline: local fold vs. the full thin round-trip ───────────────────────
/// One measured bench arm: `results[]`-shaped, mirroring the `nornir bench` record.
fn arm(
name: &str,
iters: u64,
elapsed: std::time::Duration,
extra: serde_json::Value,
) -> serde_json::Value {
let s = elapsed.as_secs_f64();
let mut o = json!({
"name": name,
"iters": iters,
"elapsed_ms": (s * 1000.0 * 1000.0).round() / 1000.0,
"per_op_us": (s / iters as f64 * 1_000_000.0 * 1000.0).round() / 1000.0,
"ops_per_sec": (iters as f64 / s).round(),
});
if let (Some(a), Some(b)) = (o.as_object_mut(), extra.as_object()) {
for (k, v) in b {
a.insert(k.clone(), v.clone());
}
}
o
}
/// **4 — perf baseline for the lens fold + the thin round-trip.**
///
/// Times two arms and records them as a bench-history record so the round-trip has a
/// trendable baseline (the audit's second gap):
/// * `dwarves.mimir_lens_fold` — the pure LOCAL fold (bloat+zombie+risk over the
/// committed fixture), the fat/embedded path's cost.
/// * `dwarves.mimir_thin_round_trip` — the full THIN loop for all six lenses:
/// client → in-proc nornir Mimir server → fold(returned) → wire → client parse.
///
/// The record is printed as one JSON line (like `nornir bench`'s `run_main_json`) AND
/// appended to `bench_history_mimir_thin.jsonl` at the repo root (a NEW sidecar file the
/// human folds into the canonical `bench_history.jsonl` on merge). Iteration counts are
/// modest so the test stays a fast guard, not a long bench.
#[test]
fn mimir_thin_perf_baseline() {
use dwarves::mimir::{bloat, risk, zombie};
use dwarves_gui::mimir_lens::{FixtureLens, LensSource};
use std::time::Instant;
// The fixture warehouse the fat pane folds — the same source of truth the pane uses.
let sources = FixtureLens.load().expect("fixture loads");
let manifests = FixtureLens.manifests();
// Fold once to (a) verify it is non-trivial and (b) get the exact state_jsons the
// thin server will serve — so the round-trip carries realistic payloads.
let fold_once = || {
let b = bloat::analyze(&sources, 2, 25).state_json();
let z = zombie::analyze(&sources, &manifests, 25).state_json();
let mut cov = std::collections::BTreeMap::new();
for t in dwarves::coverage::TABLES {
cov.insert(t.to_string(), sources.get(t).clone());
}
let borrowed: dwarves::coverage::Tables =
cov.iter().map(|(k, v)| (k.as_str(), v)).collect();
let idx = dwarves::coverage::build(
"mimir bench",
&borrowed,
&dwarves::coverage::GraphFeed::not_asked("a local fold: the code graph is not asked"),
&[],
&Default::default(),
0,
1,
1,
);
let r = risk::analyze(&sources, &idx.covered_by_repo, 25).state_json();
(b, z, r)
};
let (bloat_j, zombie_j, risk_j) = fold_once();
assert!(
bloat_j["artifacts"].as_u64().unwrap_or(0) >= 1,
"the fold is non-trivial"
);
// Arm A — local fold.
let fold_iters: u64 = 200;
let t = Instant::now();
for _ in 0..fold_iters {
std::hint::black_box(fold_once());
}
let fold_elapsed = t.elapsed();
// Arm B — the full thin round-trip for all six lenses, over the in-proc server.
let srv = spawn_mock_mimir(MockMimir {
generic_cost: bloat_j.to_string(),
zombie_features: zombie_j.to_string(),
change_risk: risk_j.to_string(),
similar_code: sentinel("similar_code", 1004).to_string(),
weak_tests: sentinel("weak_tests", 1005).to_string(),
bench_gaps: sentinel("bench_gaps", 1006).to_string(),
..Default::default()
});
let (e, w) = (srv.endpoint.as_str(), "nordisk");
let rt_iters: u64 = 60;
let t = Instant::now();
for _ in 0..rt_iters {
std::hint::black_box((
m::generic_cost(e, "", w, "", 0, 0).unwrap(),
m::zombie_features(e, "", w, "", 0).unwrap(),
m::change_risk(e, "", w, "", 0).unwrap(),
m::similar_code(e, "", w, "", 0).unwrap(),
m::weak_tests(e, "", w, "", 0).unwrap(),
m::bench_gaps(e, "", w, "", 0).unwrap(),
));
}
let rt_elapsed = t.elapsed();
drop(srv);
let cores = std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(0);
let machine = std::fs::read_to_string("/proc/sys/kernel/hostname")
.map(|s| s.trim().to_string())
.unwrap_or_else(|_| "unknown".to_string());
let unix_ms = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_millis() as u64)
.unwrap_or(0);
let record = json!({
"suite": "dwarves.mimir_thin",
"version": env!("CARGO_PKG_VERSION"),
"machine": machine,
"cores": cores,
"unix_ms": unix_ms,
"results": [
arm("dwarves.mimir_lens_fold", fold_iters, fold_elapsed, json!({"lenses": 3, "path": "local"})),
arm("dwarves.mimir_thin_round_trip", rt_iters, rt_elapsed, json!({"lenses": 6, "path": "client->server->wire->client"})),
],
});
// Print as one JSON line (mirrors `nornir bench run`'s `run_main_json`)…
println!("BENCH {}", serde_json::to_string(&record).unwrap());
// …and append to a NEW sidecar bench-history file at the repo root (best-effort:
// recording a baseline must never fail the guard). The human folds this into the
// canonical `bench_history.jsonl` on merge.
let sidecar = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.parent()
.map(|p| p.join("bench_history_mimir_thin.jsonl"));
if let Some(path) = sidecar {
use std::io::Write;
if let Ok(mut f) = std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(&path)
{
let _ = writeln!(f, "{}", serde_json::to_string(&record).unwrap());
}
}
let ok = fold_elapsed.as_nanos() > 0 && rt_elapsed.as_nanos() > 0;
assert!(ok, "both arms produced a timing");
matrix(
"mimir_thin_perf_baseline",
ok,
&format!(
"perf baseline recorded (bench_history_mimir_thin.jsonl): local lens fold \
{:.1}us/op ({} iters, 3 lenses) vs. full thin round-trip {:.1}us/op ({} iters, \
6 lenses over client->in-proc Mimir server->wire->client). Trendable baseline \
for the audited thin seam.",
fold_elapsed.as_secs_f64() / fold_iters as f64 * 1e6,
fold_iters,
rt_elapsed.as_secs_f64() / rt_iters as f64 * 1e6,
rt_iters,
),
);
}