code wiki / _hdl_build / nx_transport_policy_gate.nx
nx_transport_policy_gate.nx source
↩ module page · 107 lines · 5281 B
1// nx_transport_policy_gate.nx -- gate for the TIERED TRANSPORT policy (best-of-June-2026 on top,
2// skilled fallbacks, mandatory failure flagging, auto-heal). KATs the whole life-cycle:
3// T1 best-tier selection per capability mask (QUIC>striped>single; nothing->0 hard-down)
4// T2 skilled fallback: failures below threshold KEEP the tier (no flap); the 3rd consecutive
5// failure falls ONE rung (T1->T2->T3, clamped)
6// T3 success resets the failure counter (one blip never accumulates into a fallback)
7// T4 auto-heal: upgrade probe allowed only after the backoff window; backoff DOUBLES per
8// fallback (30->60) and caps; a successful upgrade HALVES it (healing accelerates)
9// T5 flag line: every transition assembles the mandatory TRANSPORT-TIER line (FAIL-DOWN
10// verdict=DEGRADED / HEAL-UP verdict=GREEN) -- parse-checked byte content
11// T6 NEG: no upgrade before the window; T7 NEG: tier never falls below SINGLE.
12// expect_exit: 0 license_tier: ORIGINAL
13import "nx_transport_policy.nx"
14
15func pw2(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
16func pn2(v: i64) -> i64 {
17 let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m}
18 let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}
19 var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 }
20func contains2(hay: *u8, n: i64, needle: *u8) -> i64 {
21 var nl: i64=0; while needle[nl]!=(0 as u8){nl=nl+1}
22 if nl==0 {return 1}
23 var i: i64=0
24 while i+nl<=n { var j: i64=0; var ok: i64=1; while j<nl { if hay[i+j]!=needle[j]{ok=0;j=nl} else {j=j+1} } if ok==1{return 1} i=i+1 }
25 return 0 }
26func chk2(name: *u8, ok: i64, pass: *i64) -> i64 {
27 pw2(" " as *u8)
28 if ok==1 { pw2("PASS " as *u8); pass[0]=pass[0]+1 } else { pw2("FAIL " as *u8) }
29 pw2(name); pw2("\n" as *u8)
30 return 0 }
31
32func main() -> i64 {
33 pw2("=== nx_transport_policy_gate: tier ladder + skilled fallback + flag + auto-heal ===\n" as *u8)
34 let pass: *i64 = sys_mmap(16) as *i64
35 pass[0]=0
36
37 // T1 best-tier per caps
38 var t1: i64 = 1
39 if tp_best(7) != 1 { t1 = 0 } // QUIC up -> top
40 if tp_best(3) != 2 { t1 = 0 } // no QUIC, multi-leg ok -> striped
41 if tp_best(1) != 3 { t1 = 0 } // only single WSS -> single
42 if tp_best(0) != 0 { t1 = 0 } // nothing -> hard-down 0
43 chk2("T1 best-tier selection per capability mask " as *u8, t1, pass)
44
45 // T2 skilled fallback at threshold, not before
46 let st: *i64 = sys_mmap(32) as *i64
47 st[0]=0; st[1]=0; st[2]=0
48 var tier: i64 = 1
49 var t2: i64 = 1
50 tier = tp_on_failure(tier, 1000, st)
51 if tier != 1 { t2 = 0 } // 1st failure: stay
52 tier = tp_on_failure(tier, 1010, st)
53 if tier != 1 { t2 = 0 } // 2nd: stay
54 tier = tp_on_failure(tier, 1020, st)
55 if tier != 2 { t2 = 0 } // 3rd: fall to striped
56 chk2("T2 fallback only on 3rd consecutive failure " as *u8, t2, pass)
57
58 // T3 success resets the counter
59 st[0]=2
60 tp_on_success(st)
61 var t3: i64 = 1
62 if st[0] != 0 { t3 = 0 }
63 chk2("T3 success heartbeat resets the failure counter " as *u8, t3, pass)
64
65 // T4 heal window + backoff doubling + upgrade halving
66 var t4: i64 = 1
67 if st[2] != 30 { t4 = 0 } // first fallback set 30s
68 if tp_should_retry_upgrade(2, 1025, st) != 0 { t4 = 0 } // 5s later: window closed
69 if tp_should_retry_upgrade(2, 1055, st) != 1 { t4 = 0 } // 35s later: window open
70 var tafter: i64 = tier
71 tafter = tp_on_failure(tafter, 2000, st)
72 tafter = tp_on_failure(tafter, 2010, st)
73 tafter = tp_on_failure(tafter, 2020, st) // threshold again -> falls to 3, backoff doubles
74 if tafter != 3 { t4 = 0 }
75 if st[2] != 60 { t4 = 0 }
76 let up: i64 = tp_on_upgrade(tafter, st) // heal probe succeeded -> back to 2, backoff halves
77 if up != 2 { t4 = 0 }
78 if st[2] != 30 { t4 = 0 }
79 chk2("T4 heal window + backoff x2 on fall, /2 on heal " as *u8, t4, pass)
80
81 // T5 flag lines (mandatory, parse-checked)
82 let buf: *u8 = sys_mmap(256)
83 var n: i64 = tp_flag(buf, 3000, 1, 2, 1)
84 var t5: i64 = 1
85 if contains2(buf, n, "TRANSPORT-TIER epoch=3000 from=1 to=2 event=FAIL-DOWN verdict=DEGRADED" as *u8) != 1 { t5 = 0 }
86 n = tp_flag(buf, 3100, 2, 1, 2)
87 if contains2(buf, n, "event=HEAL-UP verdict=GREEN" as *u8) != 1 { t5 = 0 }
88 chk2("T5 mandatory flag lines assemble byte-correct " as *u8, t5, pass)
89
90 // T6 NEG: no upgrade before the window
91 st[1]=5000; st[2]=30
92 var t6: i64 = 0
93 if tp_should_retry_upgrade(2, 5010, st) == 0 { t6 = 1 }
94 chk2("T6 NEG no premature upgrade (window closed) " as *u8, t6, pass)
95
96 // T7 NEG: never below SINGLE
97 st[0]=2
98 let t7t: i64 = tp_on_failure(3, 6000, st)
99 var t7: i64 = 0
100 if t7t == 3 { t7 = 1 }
101 chk2("T7 NEG tier clamps at SINGLE (never disconnects) " as *u8, t7, pass)
102
103 pw2("TRANSPORT-POLICY-GATE pass=" as *u8); pn2(pass[0]); pw2("/7" as *u8)
104 if pass[0]==7 { pw2(" verdict=GREEN -- top-of-line first, skilled fallbacks, flagged + self-healing\n" as *u8); return 0 }
105 pw2(" verdict=RED\n" as *u8)
106 return 1
107}