nx_mode_gate.nx source
↩ module page · 116 lines · 4709 B
1// nx_mode_gate.nx -- REFEREE for WRITING rung W-MODE-1 (nx_mode).
2//
3// [T1] mode lookup: lane(erotica)=local, lane(correspondence)=tutor,
4// structure(paper)=imrad, structure(social)=convo.
5// [T2] unknown mode -> -1.
6// [T3] SEAM AUDIT: a well-formed registry has 0 violations (every explicit mode
7// routes to the local lane).
8// [T4] NEG-CONTROL: a tampered registry (erotica -> tutor lane) is CAUGHT
9// (audit == 1) -- the seam is load-bearing, not asserted.
10//
11// Evidence -> stdout + knowledge/status/mode_gate.log. Exit 0 GREEN / 1 RED.
12// Sovereign x86_64. license_tier: ORIGINAL
13import "nx_syscalls_x86_64.nx"
14import "nx_mode.nx"
15
16func gp(logfd: i64, s: *u8) -> i64 {
17 var n: i64 = 0
18 while s[n] != (0 as u8) { n = n + 1 }
19 sys_write(1, s, n)
20 if logfd > 0 { sys_write(logfd, s, n) }
21 return 0
22}
23func gn(logfd: i64, v: i64) -> i64 {
24 var m: i64 = v
25 if m < 0 {
26 sys_write(1, "-\x00" as *u8, 1)
27 if logfd > 0 { sys_write(logfd, "-\x00" as *u8, 1) }
28 m = 0 - m
29 }
30 let bb: *u8 = sys_mmap(32)
31 let t: *u8 = sys_mmap(32)
32 var k: i64 = 0
33 if m == 0 { t[0] = 48 as u8; k = 1 }
34 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
35 var i: i64 = 0
36 while i < k { bb[i] = t[k - 1 - i]; i = i + 1 }
37 sys_write(1, bb, k)
38 if logfd > 0 { sys_write(logfd, bb, k) }
39 return 0
40}
41func slen(s: *u8) -> i64 {
42 var n: i64 = 0
43 while s[n] != (0 as u8) { n = n + 1 }
44 return n
45}
46func streq(a: *u8, b: *u8) -> i64 {
47 var i: i64 = 0
48 while a[i] != (0 as u8) {
49 if a[i] != b[i] { return 0 }
50 i = i + 1
51 }
52 if b[i] != (0 as u8) { return 0 }
53 return 1
54}
55func pr_streq(logfd: i64, label: *u8, got: *u8, exp: *u8) -> i64 {
56 gp(logfd, label)
57 gp(logfd, " got=\x00" as *u8); gp(logfd, got)
58 if streq(got, exp) == 1 { gp(logfd, " OK\n\x00" as *u8); return 1 }
59 gp(logfd, " exp=\x00" as *u8); gp(logfd, exp); gp(logfd, " FAIL\n\x00" as *u8)
60 return 0
61}
62func pr_kv(logfd: i64, label: *u8, got: i64, exp: i64) -> i64 {
63 gp(logfd, label)
64 gp(logfd, " got=\x00" as *u8); gn(logfd, got)
65 gp(logfd, " exp=\x00" as *u8); gn(logfd, exp)
66 if got == exp { gp(logfd, " OK\n\x00" as *u8); return 1 }
67 gp(logfd, " FAIL\n\x00" as *u8)
68 return 0
69}
70
71func main() -> i64 {
72 let logfd: i64 = sys_openat_append("knowledge/status/mode_gate.log\x00" as *u8, 0x1a4)
73 gp(logfd, "MODE-GATE W-MODE-1 (router + mechanical seam audit)\n\x00" as *u8)
74
75 let buf: *u8 = sys_mmap(256)
76 let sc: *u8 = sys_mmap(256)
77 var ok: i64 = 1
78
79 let reg: *u8 = "correspondence\x09sfw\x09tutor\x09tone\nmanga\x09mixed\x09tutor\x09panel\nerotica\x09explicit\x09local\x09scene\npaper\x09sfw\x09tutor\x09imrad\nscenario\x09sfw\x09tutor\x09scenario\nlanguage\x09sfw\x09tutor\x09convo\nsocial\x09sfw\x09tutor\x09convo\n\x00" as *u8
80 let rn: i64 = slen(reg)
81
82 // ---- T1: lookups ----
83 gp(logfd, " [T1] mode lookups\n\x00" as *u8)
84 md_lane(reg, rn, "erotica\x00" as *u8, buf, 256)
85 if pr_streq(logfd, " lane(erotica)\x00" as *u8, buf, "local\x00" as *u8) == 0 { ok = 0 }
86 md_lane(reg, rn, "correspondence\x00" as *u8, buf, 256)
87 if pr_streq(logfd, " lane(correspondence)\x00" as *u8, buf, "tutor\x00" as *u8) == 0 { ok = 0 }
88 md_structure(reg, rn, "paper\x00" as *u8, buf, 256)
89 if pr_streq(logfd, " structure(paper)\x00" as *u8, buf, "imrad\x00" as *u8) == 0 { ok = 0 }
90 md_structure(reg, rn, "social\x00" as *u8, buf, 256)
91 if pr_streq(logfd, " structure(social)\x00" as *u8, buf, "convo\x00" as *u8) == 0 { ok = 0 }
92
93 // ---- T2: unknown mode ----
94 gp(logfd, " [T2] unknown mode\n\x00" as *u8)
95 if pr_kv(logfd, " lookup(nope)\x00" as *u8, md_lane(reg, rn, "nope\x00" as *u8, buf, 256), 0 - 1) == 0 { ok = 0 }
96
97 // ---- T3: seam audit on a well-formed registry ----
98 gp(logfd, " [T3] seam audit (well-formed)\n\x00" as *u8)
99 if pr_kv(logfd, " violations\x00" as *u8, md_seam_audit(reg, rn, sc, 256), 0) == 0 { ok = 0 }
100
101 // ---- T4: NEG-CONTROL tampered registry (erotica -> tutor) ----
102 gp(logfd, " [T4] NEG-CONTROL: explicit mode on tutor lane is caught\n\x00" as *u8)
103 let bad: *u8 = "correspondence\x09sfw\x09tutor\x09tone\nerotica\x09explicit\x09tutor\x09scene\n\x00" as *u8
104 if pr_kv(logfd, " violations\x00" as *u8, md_seam_audit(bad, slen(bad), sc, 256), 1) == 0 { ok = 0 }
105
106 if ok == 1 {
107 gp(logfd, "MODE-GATE result=ALL-PASS verdict=GREEN\n\x00" as *u8)
108 if logfd > 0 { sys_close(logfd) }
109 sys_exit(0)
110 return 0
111 }
112 gp(logfd, "MODE-GATE result=FAIL verdict=RED\n\x00" as *u8)
113 if logfd > 0 { sys_close(logfd) }
114 sys_exit(1)
115 return 1
116}