code wiki / _hdl_build / nx_teacher_coordinate_gate.nx
nx_teacher_coordinate_gate.nx source
↩ module page · 124 lines · 6509 B
1// nx_teacher_coordinate_gate.nx -- proves the TEACHER + TRAINER + PUBLISHER coordinate: the published
2// page is a JOINT artifact whose content depends on BOTH the teacher's store AND the trainer's live state.
3//
4// Hermetic /tmp. Seed 3 insights (teacher), practice one (trainer), emit the page (publisher), read it:
5// T1 emit produced bytes
6// T2 page carries the teacher's insight COUNT (INSIGHTS=3)
7// T3 page carries the trainer's COVERED meter (COVERED=1 after one practice)
8// T4 page renders the teacher's insight rules (rule one / two / three) -- data-driven, all of them
9// T5 page carries source+id (the unified record shape)
10// T6[neg] no-fabrication: a rule never seeded is ABSENT from the page
11// T7 COORDINATION: practice a 2nd insight -> re-emit -> the page now shows COVERED=2 (the publisher
12// reflects the trainer's UPDATED state -- proof the meter is live, not a constant)
13// GREEN iff all. Sovereign: imports nx_teacher_publish + nx_syscalls. license_tier: ORIGINAL
14import "nx_teacher_publish.nx"
15import "nx_syscalls.nx"
16
17func cg_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != 0 as u8 { n = n + 1 } sys_write(1, s, n); return 0 }
18func cg_putn(v: i64) -> i64 {
19 if v == 0 { sys_write(1, "0" as *u8, 1); return 0 }
20 var m: i64 = v; if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m }
21 let d: *u8 = sys_mmap(24); var k: i64 = 0
22 while m > 0 { d[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
23 var j: i64 = k - 1
24 while j >= 0 { sys_write(1, ((d as i64)+j) as *u8, 1); j = j - 1 }
25 return 0
26}
27func cg_cat(dst: *u8, off: i64, s: *u8) -> i64 { var i: i64 = 0; while s[i] != 0 as u8 { dst[off + i] = s[i]; i = i + 1 } return off + i }
28func cg_catn(dst: *u8, off: i64, v: i64) -> i64 {
29 var m: i64 = v; var o: i64 = off
30 let t: *u8 = sys_mmap(28); var k: i64 = 0
31 if m == 0 { t[0] = 48 as u8; k = 1 }
32 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
33 var i: i64 = 0; while i < k { dst[o + i] = t[k - 1 - i]; i = i + 1 }
34 return o + k
35}
36func cg_join(out: *u8, a: *u8, b: *u8) -> i64 { var o: i64 = cg_cat(out, 0, a); o = cg_cat(out, o, b); out[o] = 0 as u8; return o }
37func cg_mkdir(path: *u8) -> i64 { return sys_mkdir(path, 0x1ed) }
38func cg_write(path: *u8, content: *u8) -> i64 {
39 let fd: i64 = sys_openat_wr(path, 0x1a4)
40 if fd < 0 { return 0 - 1 }
41 var n: i64 = 0; while content[n] != 0 as u8 { n = n + 1 }
42 sys_write(fd, content, n); sys_close(fd); return 0
43}
44func cg_read(path: *u8, buf: *u8, cap: i64) -> i64 {
45 let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 }
46 var n: i64 = 0; var go: i64 = 1
47 while go == 1 { let r: i64 = sys_read(fd, ((buf as i64) + n) as *u8, cap - n); if r <= 0 { go = 0 } else { n = n + r } if n >= cap { go = 0 } }
48 sys_close(fd); return n
49}
50func cg_contains(buf: *u8, n: i64, needle: *u8) -> i64 {
51 var pl: i64 = 0; while needle[pl] != (0 as u8) { pl = pl + 1 }
52 if pl == 0 { return 0 }
53 var i: i64 = 0
54 while i + pl <= n {
55 var j: i64 = 0; var ok: i64 = 1
56 while j < pl { if buf[i + j] != needle[j] { ok = 0; j = pl } else { j = j + 1 } }
57 if ok == 1 { return 1 }
58 i = i + 1
59 }
60 return 0
61}
62func cg_assert(label: *u8, cond: i64) -> i64 {
63 if cond != 0 { cg_puts(" [PASS] "); cg_puts(label); cg_puts("\n"); return 1 }
64 cg_puts(" [FAIL] "); cg_puts(label); cg_puts("\n"); return 0
65}
66
67func main() -> i64 {
68 cg_puts("=== nx_teacher_coordinate_gate: teacher+trainer+publisher joint artifact (neg-control) ===\n")
69 let epoch: i64 = sys_now_realtime_sec()
70 let root: *u8 = sys_mmap(512)
71 var ro: i64 = cg_cat(root, 0, "/tmp/tcg-" as *u8); ro = cg_catn(root, ro, epoch); root[ro] = 0 as u8
72 cg_mkdir(root)
73 let led: *u8 = sys_mmap(512); cg_join(led, root, "/cap.tsv\x00" as *u8)
74 let cap: *u8 = sys_mmap(512); cg_join(cap, root, "/cap-\x00" as *u8)
75 let state: *u8 = sys_mmap(512); cg_join(state, root, "/state-\x00" as *u8)
76 let page: *u8 = sys_mmap(512); cg_join(page, root, "/curriculum.html\x00" as *u8)
77 cg_write(led, "# cap\nI1\tC\trule one\tx\nI2\tC\trule two\tx\nI3\tC\trule three\tx\n\x00" as *u8)
78
79 let now: i64 = 1000000000
80 // TEACHER
81 tsyn_merge_cols(led, "fix\x00" as *u8, cap, 0, 2)
82 // TRAINER: practice I1
83 trn_practice(state, "I1\x00" as *u8, now)
84 // PUBLISHER: emit
85 let bytes: i64 = tpub_emit(cap, state, page, now)
86 let buf: *u8 = sys_mmap(65536)
87 let n: i64 = cg_read(page, buf, 65535)
88
89 var pass: i64 = 0
90 var total: i64 = 0
91 var c: i64 = 0
92
93 c = 0; if bytes > 0 { if n > 0 { c = 1 } }
94 total = total + 1; pass = pass + cg_assert("T1 publisher emitted a page", c)
95
96 c = 0; if cg_contains(buf, n, "INSIGHTS=3\x00" as *u8) == 1 { c = 1 }
97 total = total + 1; pass = pass + cg_assert("T2 page carries teacher insight count (INSIGHTS=3)", c)
98
99 c = 0; if cg_contains(buf, n, "COVERED=1\x00" as *u8) == 1 { c = 1 }
100 total = total + 1; pass = pass + cg_assert("T3 page carries trainer meter (COVERED=1)", c)
101
102 c = 0; if cg_contains(buf, n, "rule one\x00" as *u8) == 1 { c = 1 }
103 total = total + 1; pass = pass + cg_assert("T4a page renders insight rule one", c)
104
105 c = 0; if cg_contains(buf, n, "rule two\x00" as *u8) == 1 { if cg_contains(buf, n, "rule three\x00" as *u8) == 1 { c = 1 } }
106 total = total + 1; pass = pass + cg_assert("T4b page renders ALL insight rules (two + three)", c)
107
108 c = 0; if cg_contains(buf, n, "I1\x00" as *u8) == 1 { if cg_contains(buf, n, "fix\x00" as *u8) == 1 { c = 1 } }
109 total = total + 1; pass = pass + cg_assert("T5 page carries source + id", c)
110
111 c = 0; if cg_contains(buf, n, "PHANTOM rule never seeded\x00" as *u8) == 0 { c = 1 }
112 total = total + 1; pass = pass + cg_assert("T6[neg] no-fabrication: a rule never seeded is ABSENT from the page", c)
113
114 // COORDINATION: trainer state change must flow through to the published page.
115 trn_practice(state, "I2\x00" as *u8, now)
116 tpub_emit(cap, state, page, now)
117 let n2: i64 = cg_read(page, buf, 65535)
118 c = 0; if cg_contains(buf, n2, "COVERED=2\x00" as *u8) == 1 { c = 1 }
119 total = total + 1; pass = pass + cg_assert("T7 COORDINATION: practice 2nd -> re-emit -> page shows COVERED=2 (live trainer->publisher)", c)
120
121 cg_puts("\n---- nx_teacher_coordinate gate: passed "); cg_putn(pass); cg_puts(" / "); cg_putn(total); cg_puts(" ----\n")
122 if pass == total { cg_puts("verdict=GREEN\n"); sys_exit(0); return 0 }
123 cg_puts("verdict=RED\n"); sys_exit(1); return 1
124}