code wiki / _hdl_build / nx_flip_gate.nx

nx_flip_gate.nx source

↩ module page · 211 lines · 12685 B

1// nx_flip_gate.nx -- THE REGRESSION GATE for the flip lane (organs 1-4). 2// Until now every one of the lane's ~26 proofs was a ONE-TIME hand verification: correct when made, 3// and completely unable to stop the next session silently breaking it. This makes them re-runnable. 4// Forks each live organ via tr_run_capture and asserts BOTH the exit code AND a required substring of 5// its output -- so a verdict that flips, a number that drifts, or a fail-closed path that stops firing 6// all turn this RED. 7// D001: emits a `verdict=` anchor so the ecosystem judge can read it. 8// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0 9import "nx_gate_lib.nx" 10import "nx_flip_lib.nx" 11import "nx_tool_run.nx" 12import "nx_syscalls.nx" 13 14const FG_OUT: i64 = 1 15const FG_OUTCAP: i64 = 262144 16const FG_AVCAP: i64 = 192 17const FG_TMPCAP: i64 = 32 18const FG_D0: i64 = 48 19const FG_BASE10: i64 = 10 20const FG_NL: i64 = 10 21const FG_BUF: i64 = 262144 22const FG_SCRATCH: i64 = 32 23const FG_EXIT_RED: i64 = 1 24 25// u26a0NO `const <name>: *u8 = "..."` HERE. The first gate run scored exactly 6/16 -- precisely the six 26// nx_flip_score cases -- while all TEN cases that used a DIFFERENT path const failed, even though every 27// one of those organs was independently re-verified GREEN via nx_plan_run at the same moment. That is 28// the known const-pointer trap (reference-nishilang-nx-cc-gotchas: a const *u8 can compile clean and 29// then resolve to garbage). Paths are now plain `let` bindings inside main(). 30 31// CONVERTED: the four generic printers now DELEGATE to the shared base (fx_*). Kept as thin 32// gate-local wrappers so the ~40 fg_ call sites below stay untouched -- the surgical, low-risk way 33// to fold this last organ onto nx_flip_lib.nx. The base's fx_bcatn ALSO handles the buffered case. 34func fg_puts(s: *u8) -> i64 { return fx_puts(s) } 35func fg_putn(n: i64) -> i64 { return fx_putn(n) } 36func fg_bcat(b: *u8, o: i64, s: *u8) -> i64 { return fx_bcat(b, o, s) } 37func fg_bcatn(b: *u8, o: i64, n: i64) -> i64 { return fx_bcatn(b, o, n) } 38// does haystack[0..n) contain the NUL-terminated needle? 39func fg_contains(h: *u8, n: i64, needle: *u8) -> i64 { 40 var m: i64 = 0 41 while needle[m] != (0 as u8) { m = m + 1 } 42 if m == 0 { return 1 } 43 if m > n { return 0 } 44 var i: i64 = 0 45 var found: i64 = 0 46 while i <= n - m { 47 if found == 0 { 48 var j: i64 = 0 49 var ok: i64 = 1 50 while j < m { 51 if ok == 1 { if h[i+j] != needle[j] { ok = 0 } } 52 j = j + 1 53 } 54 if ok == 1 { found = 1 } 55 } 56 i = i + 1 57 } 58 return found 59} 60// build an argv vector; "" marks an absent slot (avoids null-pointer literals) 61func fg_av(p: *u8, a1: *u8, a2: *u8, a3: *u8, a4: *u8, a5: *u8, a6: *u8, a7: *u8, a8: *u8, a9: *u8) -> *i64 { 62 let av: *i64 = sts_mm(FG_AVCAP) as *i64 63 av[0] = p as i64 64 var n: i64 = 1 65 // skip marker is the NON-EMPTY literal "@" (0x40) -- a real, definitely-NUL-terminated string 66 if a1[0] != (64 as u8) { av[n] = a1 as i64; n = n + 1 } 67 if a2[0] != (64 as u8) { av[n] = a2 as i64; n = n + 1 } 68 if a3[0] != (64 as u8) { av[n] = a3 as i64; n = n + 1 } 69 if a4[0] != (64 as u8) { av[n] = a4 as i64; n = n + 1 } 70 if a5[0] != (64 as u8) { av[n] = a5 as i64; n = n + 1 } 71 if a6[0] != (64 as u8) { av[n] = a6 as i64; n = n + 1 } 72 if a7[0] != (64 as u8) { av[n] = a7 as i64; n = n + 1 } 73 if a8[0] != (64 as u8) { av[n] = a8 as i64; n = n + 1 } 74 if a9[0] != (64 as u8) { av[n] = a9 as i64; n = n + 1 } 75 av[n] = 0 76 return av 77} 78// run one case; assert exit code AND required substring. counts[0]=total counts[1]=pass 79func fg_case(nm: *u8, path: *u8, av: *i64, want_exit: i64, want_sub: *u8, counts: *i64, pb: *u8, po: i64) -> i64 { 80 let out: *u8 = sts_mm(FG_OUTCAP) 81 let ol: *i64 = sts_mm(FG_SCRATCH) as *i64 82 let rc: i64 = tr_run_capture(path, av, out, FG_OUTCAP, ol) 83 var ok: i64 = 1 84 if rc != want_exit { ok = 0 } 85 if fg_contains(out, ol[0], want_sub) == 0 { ok = 0 } 86 counts[0] = counts[0] + 1 87 if ok == 1 { counts[1] = counts[1] + 1 } 88 // FAILURES ONLY: a passing case is noise, and the plan-runner snippet cap (200B) means a long 89 // roster of PASS lines would hide the very failures this gate exists to surface. 90 if ok == 1 { return po } 91 var o: i64 = po 92 o = fg_bcat(pb, o, "FAIL " as *u8) 93 o = fg_bcat(pb, o, nm) 94 o = fg_bcat(pb, o, " exit=" as *u8); o = fg_bcatn(pb, o, rc) 95 o = fg_bcat(pb, o, " want_exit=" as *u8); o = fg_bcatn(pb, o, want_exit) 96 o = fg_bcat(pb, o, " want=" as *u8); o = fg_bcat(pb, o, want_sub) 97 o = fg_bcat(pb, o, "\n" as *u8) 98 return o 99} 100 101func main(argc: i64, argv: *i64) -> i64 { 102 let counts: *i64 = sts_mm(FG_SCRATCH) as *i64 103 counts[0] = 0 104 counts[1] = 0 105 let pb: *u8 = sts_mm(FG_BUF) 106 var po: i64 = 0 107 // u26a0NOT "" -- an EMPTY literal cannot be relied on to read back as a NUL first byte, and when the 108 // skip-test failed the sentinels were appended as REAL argv, so argv[n] became a plane-prefix root 109 // of "" -> planes resolved to `class-` instead of `flip-class-` -> every organ correctly REFUSED 110 // (exit=4). That, not any const-pointer issue, is why the first run scored 6/16: the six score 111 // cases pass NINE real args and use no sentinel at all. 112 let E: *u8 = "@" as *u8 113 let FG_P_SCORE: *u8 = "/volume1/homes/elderwesto/nishihost/nx_flip_score.elf" as *u8 114 let FG_P_REAL: *u8 = "/volume1/homes/elderwesto/nishihost/nx_flip_real.elf" as *u8 115 let FG_P_DEFL: *u8 = "/volume1/homes/elderwesto/nishihost/nx_flip_defl.elf" as *u8 116 let FG_P_WORK: *u8 = "/volume1/homes/elderwesto/nishihost/nx_flip_worklist.elf" as *u8 117 118 // ---- organ 1: nx_flip_score (part-by-part + coverage gate) ---- 119 po = fg_case("score-car-coverage-refuses" as *u8, FG_P_SCORE, 120 fg_av(FG_P_SCORE, "CAR" as *u8, "FOCUS13A" as *u8, "4200" as *u8, "7800" as *u8, "7000" as *u8, "150" as *u8, "0" as *u8, "250" as *u8, "30" as *u8), 121 0, "uninspected_mandatory=5" as *u8, counts, pb, po) 122 po = fg_case("score-car-profit-arithmetic" as *u8, FG_P_SCORE, 123 fg_av(FG_P_SCORE, "CAR" as *u8, "FOCUS13A" as *u8, "4200" as *u8, "7800" as *u8, "7000" as *u8, "150" as *u8, "0" as *u8, "250" as *u8, "30" as *u8), 124 0, "profit_p10=1390" as *u8, counts, pb, po) 125 po = fg_case("score-car-dps6-is-LEAVE" as *u8, FG_P_SCORE, 126 fg_av(FG_P_SCORE, "CAR" as *u8, "FOCUS13A" as *u8, "4200" as *u8, "7800" as *u8, "7000" as *u8, "150" as *u8, "0" as *u8, "250" as *u8, "30" as *u8), 127 0, "CAR-TRANS cond=30 rep_typ=2200 rep_high=3800 lift_typ=2000 lift_low=1200 margin_typ=-200 margin_p10=-2600 action=LEAVE" as *u8, counts, pb, po) 128 po = fg_case("score-realestate-coverage-refuses" as *u8, FG_P_SCORE, 129 fg_av(FG_P_SCORE, "REALESTATE" as *u8, "123MAIN" as *u8, "180000" as *u8, "265000" as *u8, "245000" as *u8, "3000" as *u8, "0" as *u8, "4000" as *u8, "90" as *u8), 130 0, "uninspected_mandatory=6" as *u8, counts, pb, po) 131 po = fg_case("score-no-findings-refused" as *u8, FG_P_SCORE, 132 fg_av(FG_P_SCORE, "CAR" as *u8, "NEVER-INSPECTED" as *u8, "4200" as *u8, "7800" as *u8, "7000" as *u8, "150" as *u8, "0" as *u8, "250" as *u8, "30" as *u8), 133 4, "absent-inspection-is-not-a-clean-inspection" as *u8, counts, pb, po) 134 po = fg_case("score-unknown-class-refused" as *u8, FG_P_SCORE, 135 fg_av(FG_P_SCORE, "CRYPTO-MOONBOY" as *u8, "FOCUS13A" as *u8, "4200" as *u8, "7800" as *u8, "7000" as *u8, "150" as *u8, "0" as *u8, "250" as *u8, "30" as *u8), 136 4, "unknown-asset-class" as *u8, counts, pb, po) 137 138 // ---- organ 2: nx_flip_real (real value + manipulation + balance-sheet coverage) ---- 139 po = fg_case("real-aapl-backing-287bps" as *u8, FG_P_REAL, 140 fg_av(FG_P_REAL, "EQUITY" as *u8, "AAPL" as *u8, "3500000000000" as *u8, "365" as *u8, E, E, E, E, E), 141 0, "backing_bps=287" as *u8, counts, pb, po) 142 po = fg_case("real-manipulation-outranks-value" as *u8, FG_P_REAL, 143 fg_av(FG_P_REAL, "EQUITY" as *u8, "TEST-SPOOF-MECHANISM" as *u8, "1000000000" as *u8, "365" as *u8, E, E, E, E, E), 144 0, "triangulated-manipulation-blocks-trade" as *u8, counts, pb, po) 145 po = fg_case("real-gross-only-refused" as *u8, FG_P_REAL, 146 fg_av(FG_P_REAL, "EQUITY" as *u8, "TEST-GROSS-ONLY" as *u8, "100000000" as *u8, "365" as *u8, E, E, E, E, E), 147 0, "incomplete-balance-sheet" as *u8, counts, pb, po) 148 po = fg_case("real-no-components-refused" as *u8, FG_P_REAL, 149 fg_av(FG_P_REAL, "EQUITY" as *u8, "NEVER-ANALYSED" as *u8, "1000000000" as *u8, "365" as *u8, E, E, E, E, E), 150 4, "absent-analysis-is-not-clean-analysis" as *u8, counts, pb, po) 151 152 // ---- organ 3: nx_flip_defl (real gain vs debasement) ---- 153 po = fg_case("defl-illusory-gain" as *u8, FG_P_DEFL, 154 fg_av(FG_P_DEFL, "M2SA" as *u8, "2011-10" as *u8, "2013-09" as *u8, "100000" as *u8, "112000" as *u8, E, E, E, E), 155 0, "verdict=ILLUSORY nominal_gain_bps=1200 real_gain_bps=-112" as *u8, counts, pb, po) 156 po = fg_case("defl-real-gain" as *u8, FG_P_DEFL, 157 fg_av(FG_P_DEFL, "M2SA" as *u8, "2011-10" as *u8, "2013-09" as *u8, "100000" as *u8, "130000" as *u8, E, E, E, E), 158 0, "verdict=REAL-GAIN nominal_gain_bps=3000 real_gain_bps=1476" as *u8, counts, pb, po) 159 po = fg_case("defl-absent-period-refused" as *u8, FG_P_DEFL, 160 fg_av(FG_P_DEFL, "M2SA" as *u8, "2011-10" as *u8, "2099-12" as *u8, "100000" as *u8, "130000" as *u8, E, E, E, E), 161 4, "absent-deflator-would-fake-a-real-gain" as *u8, counts, pb, po) 162 po = fg_case("defl-ecb-current-illusory" as *u8, FG_P_DEFL, 163 fg_av(FG_P_DEFL, "ECBM2EA" as *u8, "2023-12" as *u8, "2026-05" as *u8, "100000" as *u8, "108000" as *u8, E, E, E, E), 164 0, "verdict=ILLUSORY nominal_gain_bps=800 real_gain_bps=-37" as *u8, counts, pb, po) 165 po = fg_case("defl-ecb-current-realgain" as *u8, FG_P_DEFL, 166 fg_av(FG_P_DEFL, "ECBM2EA" as *u8, "2023-12" as *u8, "2026-05" as *u8, "100000" as *u8, "130000" as *u8, E, E, E, E), 167 0, "debasement_bps=1008" as *u8, counts, pb, po) 168 po = fg_case("defl-fred-13yr-illusory" as *u8, FG_P_DEFL, 169 fg_av(FG_P_DEFL, "M2SL" as *u8, "2011-10" as *u8, "2026-05" as *u8, "100000" as *u8, "240000" as *u8, E, E, E, E), 170 0, "verdict=ILLUSORY nominal_gain_bps=14000 real_gain_bps=-21" as *u8, counts, pb, po) 171 po = fg_case("defl-wheat-real-loss" as *u8, FG_P_DEFL, 172 fg_av(FG_P_DEFL, "M2SL" as *u8, "2023-12" as *u8, "2026-05" as *u8, "22963" as *u8, "22088" as *u8, E, E, E, E), 173 0, "verdict=REAL-LOSS nominal_gain_bps=-381 real_gain_bps=-1330" as *u8, counts, pb, po) 174 po = fg_case("defl-swapped-periods-refused" as *u8, FG_P_DEFL, 175 fg_av(FG_P_DEFL, "M2SA" as *u8, "2013-09" as *u8, "2011-10" as *u8, "100000" as *u8, "112000" as *u8, E, E, E, E), 176 4, "period-order-invalid" as *u8, counts, pb, po) 177 178 // ---- organ 4: nx_flip_worklist (intake) ---- 179 po = fg_case("worklist-car-missing5" as *u8, FG_P_WORK, 180 fg_av(FG_P_WORK, "CAR" as *u8, "FOCUS13A" as *u8, E, E, E, E, E, E, E), 181 0, "missing_mandatory=5" as *u8, counts, pb, po) 182 po = fg_case("worklist-hardasset-catalog-live" as *u8, FG_P_WORK, 183 fg_av(FG_P_WORK, "HARDASSET" as *u8, E, E, E, E, E, E, E, E), 184 0, "missing_mandatory=6" as *u8, counts, pb, po) 185 po = fg_case("worklist-commodity-names-datadebt" as *u8, FG_P_WORK, 186 fg_av(FG_P_WORK, "COMMODITY" as *u8, E, E, E, E, E, E, E, E), 187 0, "missing_mandatory=5" as *u8, counts, pb, po) 188 po = fg_case("worklist-forex-names-datadebt" as *u8, FG_P_WORK, 189 fg_av(FG_P_WORK, "FOREX" as *u8, E, E, E, E, E, E, E, E), 190 0, "missing_mandatory=3" as *u8, counts, pb, po) 191 po = fg_case("worklist-truly-empty-class-refused" as *u8, FG_P_WORK, 192 fg_av(FG_P_WORK, "NO-SUCH-CLASS" as *u8, E, E, E, E, E, E, E, E), 193 4, "unknown-asset-class" as *u8, counts, pb, po) 194 195 // DURABLE EVIDENCE (seq986): the clock dispatcher records only THAT a job ran, never its exit code, 196 197 // and discards stdout -- so without this line an hourly RED is invisible. Written for nx_gate_rollup. 198 199 gl_log("knowledge/status/flip_gate.log" as *u8, "NX-FLIP-GATE" as *u8, counts) 200 201 fg_puts("NX-FLIP-GATE\n" as *u8) 202 fg_puts("verdict=" as *u8) 203 if counts[1] == counts[0] { fg_puts("GREEN" as *u8) } else { fg_puts("RED" as *u8) } 204 fg_puts(" pass=" as *u8); fg_putn(counts[1]) 205 fg_puts("/" as *u8); fg_putn(counts[0]) 206 fg_puts("\n" as *u8) 207 sys_write(FG_OUT, pb, po) 208 if counts[1] == counts[0] { sys_exit(0); return 0 } 209 sys_exit(FG_EXIT_RED) 210 return FG_EXIT_RED 211}