#!/usr/bin/env python3 """ Differential hold-time A/B: perturbed vs unperturbed CONTROL from same checkpoint. Save to "." (daemon working dir), find checkpoint by glob. Cancels mass-leak drift — both runs share the same leak, difference isolates perturbation. Mandatory sanity gate: Arm A (density, dead channel) MUST decay faster than Arm B (stress). If density == stress, the measurement is still broken — STOP. """ import zmq, json, time, struct, glob, os import numpy as np # Daemon's working dir is /mnt/d/resonance-engine-active CKPT_DIR = "/mnt/d/resonance-engine-active" def main(): ctx = zmq.Context() cp = ctx.socket(zmq.PUB); cp.connect("tcp://127.0.0.1:5557") ack_sub = ctx.socket(zmq.SUB); ack_sub.connect("tcp://127.0.0.1:5559") ack_sub.setsockopt_string(zmq.SUBSCRIBE, "") coarse_sub = ctx.socket(zmq.SUB); coarse_sub.connect("tcp://127.0.0.1:5561") coarse_sub.setsockopt_string(zmq.SUBSCRIBE, "") time.sleep(1.0) # Drain for s in [coarse_sub, ack_sub]: while True: try: s.recv(flags=zmq.NOBLOCK) except zmq.ZMQError: break def send_cmd(d): cp.send_string(json.dumps(d)) time.sleep(0.05) def wait_ack(cmd_name, timeout=8.0): ack_sub.setsockopt(zmq.RCVTIMEO, int(timeout * 1000)) start = time.time() while time.time() - start < timeout: try: ack = json.loads(ack_sub.recv_string()) if ack.get("ack") == cmd_name: ack_sub.setsockopt(zmq.RCVTIMEO, -1) return ack except zmq.ZMQError: pass ack_sub.setsockopt(zmq.RCVTIMEO, -1) return None def drain_coarse(): while True: try: coarse_sub.recv(flags=zmq.NOBLOCK) except zmq.ZMQError: break def collect_coarse(n_frames): coarse_sub.setsockopt(zmq.RCVTIMEO, 90000) frames = [] while len(frames) < n_frames: try: data = coarse_sub.recv() if len(data) >= 16 + 4 * 6144 and data[:4] == b"KGCF": cycle = struct.unpack_from("= threshold)}/{n}") hold_idx = None for i in range(10, n - 5): if distances[i] < threshold and np.all(distances[i:i + 5] < threshold): hold_idx = i break if hold_idx is not None: hold_cycles = hold_idx * 10 print(f" Returned at frame {hold_idx} (~{hold_cycles} cycles)") else: hold_cycles = n * 10 print(f" Never returned within window ({hold_cycles} cycles)") # Bootstrap CI np.random.seed(42) boot_samples = [] for _ in range(100): idx = np.random.choice(n, n, replace=True) boot_d = np.array([np.mean((perturb_frames[j][1] - control_frames[j][1]) ** 2) for j in idx if j < n]) boot_hold = None for j in range(10, n - 5): if boot_d[j] < threshold and np.all(boot_d[j:j + 5] < threshold): boot_hold = j break boot_samples.append(float(boot_hold * 10) if boot_hold is not None else float(n * 10)) ci_low = np.percentile(boot_samples, 2.5) ci_high = np.percentile(boot_samples, 97.5) print(f" Hold-time: {hold_cycles} cycles, 95% CI: [{ci_low:.0f}, {ci_high:.0f}]") return hold_cycles, ci_low, ci_high ht_a, lo_a, hi_a = compute_hold_time(perturb_a, "Arm A: inject_density") ht_b, lo_b, hi_b = compute_hold_time(perturb_b, "Arm B: perturb_stress") # ========== SANITY GATE ========== print("\n" + "=" * 60) print("SANITY GATE: Does density decay faster than stress?") print("=" * 60) print(f" Arm A (density): {ht_a} cycles, CI [{lo_a:.0f}, {hi_a:.0f}]") print(f" Arm B (stress): {ht_b} cycles, CI [{lo_b:.0f}, {hi_b:.0f}]") if ht_a < ht_b: print(f" GATE PASS: density ({ht_a}) < stress ({ht_b}) — instrument is working.") elif ht_a > ht_b: print(f" ANOMALOUS: density ({ht_a}) > stress ({ht_b}) — unexpected, investigate.") else: print(f" GATE FAIL: density ({ht_a}) == stress ({ht_b}) — instrument STILL broken.") print(f" STOP. Do NOT proceed to sweep engine until this is fixed.") cp.close(); ack_sub.close(); coarse_sub.close(); ctx.term() print("\nDone.") if __name__ == "__main__": main()