nx_transport_policy.nx source
↩ module page · 102 lines · 5171 B
1// nx_transport_policy.nx -- TIERED TRANSPORT policy for the comms stack (lib, no main).
2// Operator 2026-07-02: "the most s class exceed june 2026 capability with the worse ones as skilled
3// fallbacks in case of issues and flagging of the failure on the top of the line so they can auto heal."
4//
5// THE LADDER (best first; each lower tier is a proven, skilled fallback -- rule 14 graceful degradation):
6// TIER 1 QUIC/WebTransport datagrams + core RS-FEC + L4S-aware (June-2026 frontier; needs the
7// sovereign HTTP/3+QUIC server -- the registered frontier build)
8// TIER 2 multi-socket striped WSS + core RS-FEC (k=8,m=2 across legs; one leg's TCP stall becomes
9// a recoverable erasure -- the measured intl-h2h win, buildable now)
10// TIER 3 single WSS/TCP (today's LIVE path -- proven, survives everything, worst intl behavior)
11//
12// CONTRACT (what the client core + daemon both follow):
13// - always run the HIGHEST tier the measured capabilities allow (tp_best)
14// - a tier must fail CONSECUTIVELY (threshold, default 3) before falling back -- skilled, not flappy
15// - EVERY transition (fail-down or heal-up) EMITS A FLAG LINE (tp_flag) -> the status log the
16// keep-up/heal loop consumes; a silent fallback is a doctrine violation the gate kills
17// - fallback is never final: tp_should_retry_upgrade opens a heal window (exponential backoff,
18// 30s..600s cap) to probe the higher tier again -- the AUTO-HEAL path back to top-of-line
19// license_tier: ORIGINAL
20import "nx_syscalls.nx"
21
22const TP_TIER_TOP: i64 = 1
23const TP_TIER_STRIPED: i64 = 2
24const TP_TIER_SINGLE: i64 = 3
25const TP_FAIL_THRESHOLD: i64 = 3
26const TP_BACKOFF_MIN_S: i64 = 30
27const TP_BACKOFF_CAP_S: i64 = 600
28
29// capability mask: bit0 = single WSS reachable, bit1 = multi-leg allowed (server slots), bit2 = QUIC/WT up
30func tp_best(caps: i64) -> i64 {
31 if (caps & 4) != 0 { return TP_TIER_TOP }
32 if (caps & 2) != 0 { return TP_TIER_STRIPED }
33 if (caps & 1) != 0 { return TP_TIER_SINGLE }
34 return 0 // nothing reachable: caller surfaces hard-down
35}
36
37// one more failure observed on `tier`. st[0]=consec_fails st[1]=last_fail_epoch st[2]=backoff_s
38// returns the tier to RUN NEXT (same tier until threshold, then one rung down, clamped at single).
39func tp_on_failure(tier: i64, now_s: i64, st: *i64) -> i64 {
40 st[0] = st[0] + 1
41 st[1] = now_s
42 if st[0] < TP_FAIL_THRESHOLD { return tier }
43 st[0] = 0
44 if st[2] < TP_BACKOFF_MIN_S { st[2] = TP_BACKOFF_MIN_S } else { st[2] = st[2] * 2 }
45 if st[2] > TP_BACKOFF_CAP_S { st[2] = TP_BACKOFF_CAP_S }
46 if tier < TP_TIER_SINGLE { return tier + 1 }
47 return TP_TIER_SINGLE
48}
49
50// success heartbeat on the running tier: resets the consecutive-failure counter.
51func tp_on_success(st: *i64) -> i64 { st[0] = 0; return 0 }
52
53// heal window: 1 when it is time to probe one tier UP again (auto-heal toward top-of-line).
54func tp_should_retry_upgrade(tier: i64, now_s: i64, st: *i64) -> i64 {
55 if tier <= TP_TIER_TOP { return 0 } // already at the top
56 if st[2] < TP_BACKOFF_MIN_S { st[2] = TP_BACKOFF_MIN_S }
57 if now_s - st[1] >= st[2] { return 1 }
58 return 0
59}
60
61// a successful upgrade probe: move one rung UP and shrink the backoff (healing accelerates).
62func tp_on_upgrade(tier: i64, st: *i64) -> i64 {
63 st[0] = 0
64 st[2] = st[2] / 2
65 if st[2] < TP_BACKOFF_MIN_S { st[2] = TP_BACKOFF_MIN_S }
66 if tier > TP_TIER_TOP { return tier - 1 }
67 return TP_TIER_TOP
68}
69
70func tp_apps(b: *u8, n: i64, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8){b[n+i]=s[i];i=i+1} return n+i }
71func tp_appn(b: *u8, n: i64, v: i64) -> i64 {
72 let t: *u8=sys_mmap(28); var m: i64=v; 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}
73 var i: i64=0; var p: i64=n; while i<k{b[p]=t[k-1-i];p=p+1;i=i+1} return p }
74
75// assemble the MANDATORY flag line for a transition. event: 1=FAIL-DOWN 2=HEAL-UP 3=HARD-DOWN.
76// One line -> knowledge/status/transport_tier.log; the keep-up loop + censuses consume the LAST line.
77func tp_flag(buf: *u8, now_s: i64, from_tier: i64, to_tier: i64, event: i64) -> i64 {
78 var n: i64 = 0
79 n = tp_apps(buf, n, "TRANSPORT-TIER epoch=" as *u8)
80 n = tp_appn(buf, n, now_s)
81 n = tp_apps(buf, n, " from=" as *u8)
82 n = tp_appn(buf, n, from_tier)
83 n = tp_apps(buf, n, " to=" as *u8)
84 n = tp_appn(buf, n, to_tier)
85 if event == 1 { n = tp_apps(buf, n, " event=FAIL-DOWN verdict=DEGRADED" as *u8) }
86 if event == 2 { n = tp_apps(buf, n, " event=HEAL-UP verdict=GREEN" as *u8) }
87 if event == 3 { n = tp_apps(buf, n, " event=HARD-DOWN verdict=RED" as *u8) }
88 buf[n] = 10 as u8
89 n = n + 1
90 return n
91}
92
93// convenience: append a transition flag to the status log (single O_APPEND write, no tearing).
94func tp_flag_emit(now_s: i64, from_tier: i64, to_tier: i64, event: i64) -> i64 {
95 let buf: *u8 = sys_mmap(256)
96 let n: i64 = tp_flag(buf, now_s, from_tier, to_tier, event)
97 let fd: i64 = sys_openat_append("knowledge/status/transport_tier.log" as *u8, 0x1a4)
98 if fd < 0 { return 0 - 1 }
99 sys_write(fd, buf, n)
100 sys_close(fd)
101 return 0
102}