code wiki / _hdl_build / nx_train_triage_sweep_gate.nx

nx_train_triage_sweep_gate.nx source

↩ module page · 131 lines · 6419 B

1// nx_train_triage_sweep_gate.nx -- ENGINEER's gate for the sweep organ. Proves on scratch 2// lanes (argv overrides, never the durable logs): R1 first sweep escalates ONLY the sick 3// report; R2 second sweep escalates NOTHING (idempotent, law 10); R3 a verdict CHANGE 4// escalates exactly once more; R4 a BROKEN report turns the sweep exit nonzero (loud). 5// license_tier: ORIGINAL 6import "nx_train_triage.nx" 7 8func tsg_wreport(path: *u8, preds: *i64, right: i64, total: i64, losses: *i64, nloss: i64) -> i64 { 9 let fd: i64 = sys_openat_wr(path, 0x1a4) 10 if fd < 0 { return 0 } 11 let cls: *i64 = sys_mmap(128) as *i64 12 cls[0] = 88; cls[1] = 5; cls[2] = 20; cls[3] = 6; cls[4] = 5; cls[5] = 2; cls[6] = 5; cls[7] = 18 13 var c: i64 = 0 14 while c < 8 { 15 tt_w(fd, "TRIAGE-CLASS id=" as *u8); tt_wn(fd, c) 16 tt_w(fd, " count=" as *u8); tt_wn(fd, cls[c]); tt_w(fd, "\n" as *u8) 17 c = c + 1 18 } 19 c = 0 20 while c < 8 { 21 tt_w(fd, "TRIAGE-PRED id=" as *u8); tt_wn(fd, c) 22 tt_w(fd, " count=" as *u8); tt_wn(fd, preds[c]); tt_w(fd, "\n" as *u8) 23 c = c + 1 24 } 25 tt_w(fd, "TRIAGE-ACC right=" as *u8); tt_wn(fd, right) 26 tt_w(fd, " total=" as *u8); tt_wn(fd, total); tt_w(fd, "\n" as *u8) 27 var i: i64 = 0 28 while i < nloss { 29 tt_w(fd, "TRIAGE-LOSS ep=" as *u8); tt_wn(fd, i * 100) 30 tt_w(fd, " micro=" as *u8); tt_wn(fd, losses[i]); tt_w(fd, "\n" as *u8) 31 i = i + 1 32 } 33 sys_close(fd) 34 return 1 35} 36 37// run the sweep elf with conf/tlog/pmlog args; returns its exit code 38func tsg_run(conf: *u8, tlog: *u8, pmlog: *u8) -> i64 { 39 let pid: i64 = sys_fork() 40 if pid == 0 { 41 let dn: i64 = sys_openat_wr("/dev/null" as *u8, 0x1a4) 42 if dn >= 0 { sys_dup3(dn, 1, 0); sys_dup3(dn, 2, 0) } 43 let elf: *u8 = "/tmp/nx_train_triage_sweep.sov.elf" as *u8 44 let argv: *i64 = sys_mmap(64) as *i64 45 argv[0] = elf as i64; argv[1] = conf as i64; argv[2] = tlog as i64; argv[3] = pmlog as i64; argv[4] = 0 46 let envp: *i64 = sys_mmap(16) as *i64; envp[0] = 0 47 sys_execve(elf, argv, envp); sys_exit(127) 48 } 49 let st: *i64 = sys_mmap(16) as *i64 50 sys_wait4(pid, st, 0) 51 if (st[0] % 128) != 0 { return 0 - 1 } 52 return (st[0] >> 8) & 0xff 53} 54func tsg_count(path: *u8, marker: *u8) -> i64 { 55 let lenp: *i64 = sys_mmap(16) as *i64 56 let b: *u8 = sys_read_file(path, lenp) 57 let n: i64 = lenp[0] 58 if n <= 0 { return 0 } 59 var cnt: i64 = 0 60 var i: i64 = 0 61 while i < n { 62 if tt_match(b, i, n, marker) == 1 { cnt = cnt + 1 } 63 i = i + 1 64 } 65 return cnt 66} 67func tsg_row(label: *u8, got: i64, want: i64, failp: *i64) -> i64 { 68 tt_w(1, " row " as *u8); tt_w(1, label) 69 tt_w(1, " want=" as *u8); tt_wn(1, want) 70 tt_w(1, " got=" as *u8); tt_wn(1, got) 71 if got == want { tt_w(1, " PASS\n" as *u8) } else { tt_w(1, " FAIL\n" as *u8); failp[0] = failp[0] + 1 } 72 return 0 73} 74 75func main() -> i64 { 76 tt_w(1, "=== TRAIN-TRIAGE-SWEEP GATE (escalate-on-change, idempotent, loud-broken; scratch lanes) ===\n" as *u8) 77 let failp: *i64 = sys_mmap(16) as *i64 78 failp[0] = 0 79 let p: *i64 = sys_mmap(128) as *i64 80 let l: *i64 = sys_mmap(256) as *i64 81 // report A = healthy; report B = majority collapse 82 p[0]=88; p[1]=5; p[2]=20; p[3]=6; p[4]=5; p[5]=2; p[6]=5; p[7]=18 83 l[0]=1491; l[1]=121; l[2]=76; l[3]=14; l[4]=7; l[5]=4 84 tsg_wreport("/tmp/_tsg_report_a.log" as *u8, p, 149, 149, l, 6) 85 p[0]=149; p[1]=0; p[2]=0; p[3]=0; p[4]=0; p[5]=0; p[6]=0; p[7]=0 86 l[0]=88000; l[1]=84000; l[2]=84000; l[3]=84000; l[4]=84000; l[5]=84000 87 tsg_wreport("/tmp/_tsg_report_b.log" as *u8, p, 88, 149, l, 6) 88 // scratch conf + empty scratch logs 89 let cfd: i64 = sys_openat_wr("/tmp/_tsg_conf.txt" as *u8, 0x1a4) 90 if cfd >= 0 { 91 tt_w(cfd, "R /tmp/_tsg_report_a.log\n" as *u8) 92 tt_w(cfd, "R /tmp/_tsg_report_b.log\n" as *u8) 93 sys_close(cfd) 94 } 95 let t1: i64 = sys_openat_wr("/tmp/_tsg_triage.log" as *u8, 0x1a4) 96 if t1 >= 0 { tt_w(t1, "# scratch\n" as *u8); sys_close(t1) } 97 let p1: i64 = sys_openat_wr("/tmp/_tsg_pm.log" as *u8, 0x1a4) 98 if p1 >= 0 { tt_w(p1, "# scratch\n" as *u8); sys_close(p1) } 99 100 // R1: first sweep -- rc 0, exactly ONE escalation (the collapse), none for healthy 101 let rc1: i64 = tsg_run("/tmp/_tsg_conf.txt" as *u8, "/tmp/_tsg_triage.log" as *u8, "/tmp/_tsg_pm.log" as *u8) 102 tsg_row("R1-rc" as *u8, rc1, 0, failp) 103 tsg_row("R1-escalations" as *u8, tsg_count("/tmp/_tsg_pm.log" as *u8, "PM-ESCALATION source=train-triage" as *u8), 1, failp) 104 // R2: second sweep -- verdicts steady, escalations unchanged (idempotent, law 10) 105 let rc2: i64 = tsg_run("/tmp/_tsg_conf.txt" as *u8, "/tmp/_tsg_triage.log" as *u8, "/tmp/_tsg_pm.log" as *u8) 106 tsg_row("R2-rc" as *u8, rc2, 0, failp) 107 tsg_row("R2-idempotent" as *u8, tsg_count("/tmp/_tsg_pm.log" as *u8, "PM-ESCALATION source=train-triage" as *u8), 1, failp) 108 // R3: report A regresses healthy -> blowup; exactly one MORE escalation 109 p[0]=88; p[1]=5; p[2]=20; p[3]=11; p[4]=5; p[5]=2; p[6]=0; p[7]=18 110 l[0]=1491; l[1]=121; l[2]=76; l[3]=14; l[4]=7; l[5]=5; l[6]=0; l[7]=1; l[8]=224; l[9]=104; l[10]=104 111 tsg_wreport("/tmp/_tsg_report_a.log" as *u8, p, 144, 149, l, 11) 112 let rc3: i64 = tsg_run("/tmp/_tsg_conf.txt" as *u8, "/tmp/_tsg_triage.log" as *u8, "/tmp/_tsg_pm.log" as *u8) 113 tsg_row("R3-rc" as *u8, rc3, 0, failp) 114 tsg_row("R3-change-escalates" as *u8, tsg_count("/tmp/_tsg_pm.log" as *u8, "PM-ESCALATION source=train-triage" as *u8), 2, failp) 115 // R4: a BROKEN report (no ACC/LOSS) -> sweep exits NONZERO (loud) 116 let bfd: i64 = sys_openat_wr("/tmp/_tsg_report_c.log" as *u8, 0x1a4) 117 if bfd >= 0 { tt_w(bfd, "TRIAGE-CLASS id=0 count=88\n" as *u8); sys_close(bfd) } 118 let c2fd: i64 = sys_openat_wr("/tmp/_tsg_conf2.txt" as *u8, 0x1a4) 119 if c2fd >= 0 { tt_w(c2fd, "R /tmp/_tsg_report_c.log\n" as *u8); sys_close(c2fd) } 120 let rc4: i64 = tsg_run("/tmp/_tsg_conf2.txt" as *u8, "/tmp/_tsg_triage.log" as *u8, "/tmp/_tsg_pm.log" as *u8) 121 tsg_row("R4-broken-loud" as *u8, rc4, 1, failp) 122 123 if failp[0] == 0 { 124 tt_w(1, " SWEEP GATE: PASS 7/7 (training failures now auto-file backlog work, exactly once)\n" as *u8) 125 sys_exit(0) 126 return 0 127 } 128 tt_w(1, " SWEEP GATE: FAIL rows=" as *u8); tt_wn(1, failp[0]); tt_w(1, "\n" as *u8) 129 sys_exit(1) 130 return 1 131}