code wiki / _hdl_build / nx_wflow_branch_gate.nx

nx_wflow_branch_gate.nx source

↩ module page · 95 lines · 4764 B

1// nx_wflow_branch_gate.nx -- R9 MID-FLOW BRANCHING gate: optional 7th step field `k=v` evaluated against 2// the run VARIABLE plane at execution. Match -> step runs; mismatch OR missing variable -> status=SKIP 3// recorded in the ledger and the flow continues (Zapier-filter semantics, event-sourced). Dash or absent 4// field = always run (every pre-R9 definition stays valid). Proves: the SAME flow branches on event data; 5// var-derived conditions (set-var then branch); load refuses garbage conditions. ZERO forks. 6// license_tier: ORIGINAL expect_exit: 0 7import "nx_wflow_engine.nx" 8 9func bg_n(led: *u8, ridtok: *u8, st: *u8) -> i64 { 10 let szp: *i64 = sys_mmap(8) as *i64 11 let b: *u8 = wf_readall(led, szp) 12 if (b as i64) == 0 { return 0 - 1 } 13 return wf_lines_with2(b, szp[0], ridtok, st) 14} 15 16func main() -> i64 { 17 p("=== NX-WFLOW-BRANCH GATE (R9: per-step k=v conditions vs the variable plane; SKIP recorded) ===\n" as *u8) 18 var ok: i64 = 1 19 let now: i64 = wf_now() 20 let led: *u8 = sys_mmap(128) 21 var lo: i64 = 0 22 lo = wf_cat(led, lo, "/tmp/wfbr_" as *u8) 23 lo = wf_catn(led, lo, now) 24 lo = wf_cat(led, lo, ".log" as *u8) 25 led[lo] = 0 as u8 26 27 let flows: *i64 = sys_mmap(8 * 4) as *i64 28 flows[0] = "fb1|order|-" as *u8 as i64 29 flows[1] = "fb2|deal|-" as *u8 as i64 30 let steps: *i64 = sys_mmap(8 * 8) as *i64 31 steps[0] = "fb1|1|set-var|-|tier=gold|1|-" as *u8 as i64 32 steps[1] = "fb1|2|notify|-|Gold perks for {name}|1|tier=gold" as *u8 as i64 33 steps[2] = "fb1|3|notify|-|Standard path|1|tier=silver" as *u8 as i64 34 steps[3] = "fb1|4|notify|-|Wrap up for {name}|1" as *u8 as i64 35 steps[4] = "fb2|1|notify|-|Big deal path|1|size=big" as *u8 as i64 36 steps[5] = "fb2|2|notify|-|Every deal path|1" as *u8 as i64 37 let cx: *i64 = sys_mmap(32) as *i64 38 cx[0] = steps as i64 39 cx[1] = 6 40 cx[2] = led as i64 41 cx[3] = 0 42 43 // T0 load: 7-field rows valid; garbage condition refused 44 let l0: i64 = wf_load(steps, 6) 45 let bad: *i64 = sys_mmap(16) as *i64 46 bad[0] = "f9|1|notify|-|x|1|garbage" as *u8 as i64 47 let lb: i64 = wf_load(bad, 1) 48 p(" T0 load=" as *u8); pn(l0); p(" badCond=" as *u8); pn(lb); p("\n" as *u8) 49 if l0 != 6 { ok = 0 } 50 if lb != (0 - 1) { ok = 0 } 51 52 // T1 var-derived branch: set-var tier=gold -> gold step RUNS, silver step SKIPs, unconditional runs 53 wf_fire(cx, flows, 2, "order~name=Rose~id=B1" as *u8) 54 let d1: i64 = bg_n(led, " rid=B1.fb1 " as *u8, "status=DONE" as *u8) 55 let o1: i64 = bg_n(led, " rid=B1.fb1 " as *u8, "status=OK" as *u8) 56 let s3: i64 = bg_n(led, " rid=B1.fb1 " as *u8, " step=3 status=SKIP" as *u8) 57 let s2: i64 = bg_n(led, " rid=B1.fb1 " as *u8, " step=2 status=SKIP" as *u8) 58 p(" T1 goldrun done=" as *u8); pn(d1); p(" okSteps=" as *u8); pn(o1); p(" silverSkipped=" as *u8); pn(s3); p(" goldSkipped=" as *u8); pn(s2); p("\n" as *u8) 59 if d1 != 1 { ok = 0 } 60 if o1 != 3 { ok = 0 } 61 if s3 != 1 { ok = 0 } 62 if s2 != 0 { ok = 0 } 63 64 // T2 the SAME flow branches on EVENT data: size=big runs the branch, size=small skips it 65 wf_fire(cx, flows, 2, "deal~size=big~id=B3" as *u8) 66 wf_fire(cx, flows, 2, "deal~size=small~id=B4" as *u8) 67 let bigOk: i64 = bg_n(led, " rid=B3.fb2 " as *u8, " step=1 status=OK" as *u8) 68 let smallSkip: i64 = bg_n(led, " rid=B4.fb2 " as *u8, " step=1 status=SKIP" as *u8) 69 let d3: i64 = bg_n(led, " rid=B3.fb2 " as *u8, "status=DONE" as *u8) 70 let d4: i64 = bg_n(led, " rid=B4.fb2 " as *u8, "status=DONE" as *u8) 71 p(" T2 branch bigRan=" as *u8); pn(bigOk); p(" smallSkipped=" as *u8); pn(smallSkip); p(" bothDone=" as *u8); pn(d3 + d4); p("\n" as *u8) 72 if bigOk != 1 { ok = 0 } 73 if smallSkip != 1 { ok = 0 } 74 if d3 + d4 != 2 { ok = 0 } 75 76 // T3 missing variable -> SKIP recorded, run still completes (data-dependent, never a crash) 77 wf_fire(cx, flows, 2, "deal~id=B5" as *u8) 78 let s5: i64 = bg_n(led, " rid=B5.fb2 " as *u8, " step=1 status=SKIP" as *u8) 79 let d5: i64 = bg_n(led, " rid=B5.fb2 " as *u8, "status=DONE" as *u8) 80 p(" T3 missingVar skipped=" as *u8); pn(s5); p(" done=" as *u8); pn(d5); p("\n" as *u8) 81 if s5 != 1 { ok = 0 } 82 if d5 != 1 { ok = 0 } 83 84 // T4 negctl: nonsense status absent; unconditional step never skipped 85 let ban: i64 = bg_n(led, "WFRUN" as *u8, "status=BANANA" as *u8) 86 let s4u: i64 = bg_n(led, " rid=B1.fb1 " as *u8, " step=4 status=SKIP" as *u8) 87 p(" T4 negctl nonsense=" as *u8); pn(ban); p(" wrapSkipped=" as *u8); pn(s4u); p("\n" as *u8) 88 if ban != 0 { ok = 0 } 89 if s4u != 0 { ok = 0 } 90 91 p("NX-WFLOW-BRANCH-GATE ledger=" as *u8); p(led); p(" forks=0 " as *u8) 92 if ok == 1 { p("verdict=GREEN\n" as *u8); return 0 } 93 p("verdict=RED\n" as *u8) 94 return 1 95}