code wiki / _hdl_build / nx_organ_registry_gate.nx
nx_organ_registry_gate.nx source
↩ module page · 139 lines · 5993 B
1// nx_organ_registry_gate.nx -- POINTER-INTEGRITY GUARD for consolidation.
2//
3// The durable fix for "an organ vanished because consolidation deleted it and
4// left the pointers dangling" (caught live: nx_fetch, the merged research
5// fetcher, was dedup-deleted -> SOURCE-NOT-FOUND days later). Per
6// feedback-no-tool-proliferation-consolidate-or-justify + Cardinal 13
7// (additive-only: consolidation SUPERSEDES-AND-PRESERVES, never deletes a
8// superseder), this guard reads the canonical-organ registry
9// (knowledge/registry/organ_supersession.tsv) and asserts EVERY listed
10// canonical organ's source still resolves on disk. RED the instant one is
11// missing -> the consolidation breakage surfaces LOUDLY (REACTIVE pillar)
12// instead of as a mysterious build failure later.
13//
14// Liar-kill: a hardcoded BOGUS path must read as MISSING (proves the checker
15// actually stats the filesystem and isn't rubber-stamping GREEN).
16//
17// Evidence -> knowledge/status/organ_registry.log
18// Composes: nx_syscalls (file probe). license_tier: ORIGINAL
19import "nx_syscalls.nx"
20import "nx_gate_verdict.nx"
21
22const REG_PATH: *u8 = "knowledge/registry/organ_supersession.tsv"
23const ORG_LOG: *u8 = "knowledge/status/organ_registry.log"
24
25func ew(fd: i64, s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(fd, s, n); return 0 }
26func ewn(fd: i64, v: i64) -> i64 {
27 let bb: *u8 = sys_mmap(28); var m: i64 = v
28 if m < 0 { m = 0 - m; sys_write(fd, "-" as *u8, 1) }
29 let t: *u8 = sys_mmap(28); var k: i64 = 0
30 if m == 0 { t[0] = 48; k = 1 }
31 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
32 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 }
33 sys_write(fd, bb, k); return 0
34}
35
36// does this source path resolve on disk?
37func org_exists(relpath: *u8) -> i64 {
38 let fd: i64 = sys_openat_rd(relpath)
39 if fd < 0 { return 0 }
40 sys_close(fd)
41 return 1
42}
43
44// copy src[a..b) into dst, NUL-terminate; return length
45func org_copy(dst: *u8, src: *u8, a: i64, b: i64) -> i64 {
46 var i: i64 = 0
47 while a + i < b { dst[i] = src[a + i]; i = i + 1 }
48 dst[i] = 0 as u8
49 return i
50}
51
52func main() -> i64 {
53 var ok: i64 = 1
54
55 // multi-target writer: stdout + the durable log
56 let namebuf: *u8 = sys_mmap(512)
57 let pathbuf: *u8 = sys_mmap(1024)
58
59 // ---- liar-kill self-test: a bogus path MUST be missing ----
60 var selftest_ok: i64 = 1
61 if org_exists("runtime/nx__bogus_sentinel_zzz__.nx" as *u8) != 0 { selftest_ok = 0; ok = 0 }
62
63 // ---- read the registry ----
64 let lb: *i64 = sys_mmap(16) as *i64
65 let data: *u8 = sys_read_file(REG_PATH, lb)
66 var rows: i64 = 0
67 var present: i64 = 0
68 var missing: i64 = 0
69 var registry_ok: i64 = 1
70 if (data as i64) == 0 { registry_ok = 0; ok = 0 }
71
72 // open the log once for the per-missing detail lines
73 let logfd: i64 = sys_openat_append(ORG_LOG, 420)
74
75 if registry_ok == 1 {
76 let n: i64 = lb[0]
77 var pos: i64 = 0
78 while pos < n {
79 // find end of line
80 var eol: i64 = pos
81 var fe: i64 = 0
82 while fe == 0 { if eol >= n { fe = 1 } else { if (data[eol] & 0xff) == 10 { fe = 1 } else { eol = eol + 1 } } }
83 // skip blank lines and comments (#...)
84 var skip: i64 = 0
85 if eol <= pos { skip = 1 }
86 if skip == 0 { if (data[pos] & 0xff) == 35 { skip = 1 } } // '#'
87 if skip == 0 {
88 // field 1 = name [pos, tab1)
89 var tab1: i64 = pos
90 var f1: i64 = 0
91 while f1 == 0 { if tab1 >= eol { f1 = 1 } else { if (data[tab1] & 0xff) == 9 { f1 = 1 } else { tab1 = tab1 + 1 } } }
92 if tab1 < eol {
93 // field 2 = relpath [tab1+1, tab2)
94 var tab2: i64 = tab1 + 1
95 var f2: i64 = 0
96 while f2 == 0 { if tab2 >= eol { f2 = 1 } else { if (data[tab2] & 0xff) == 9 { f2 = 1 } else { tab2 = tab2 + 1 } } }
97 org_copy(namebuf, data, pos, tab1)
98 org_copy(pathbuf, data, tab1 + 1, tab2)
99 rows = rows + 1
100 if org_exists(pathbuf) == 1 {
101 present = present + 1
102 } else {
103 missing = missing + 1
104 ok = 0
105 if logfd >= 1 { ew(logfd, " MISSING-CANONICAL name=" as *u8); ew(logfd, namebuf); ew(logfd, " path=" as *u8); ew(logfd, pathbuf); ew(logfd, "\n" as *u8) }
106 ew(1, " MISSING-CANONICAL name=" as *u8); ew(1, namebuf); ew(1, " path=" as *u8); ew(1, pathbuf); ew(1, "\n" as *u8)
107 }
108 }
109 }
110 pos = eol + 1
111 }
112 }
113
114 // ---- verdict to stdout + log ----
115 var fd: i64 = 1
116 while fd >= 1 {
117 ew(fd, "ORGANREGISTRYGATE authored=organ selftest_bogus_detected=" as *u8); ewn(fd, selftest_ok)
118 ew(fd, " registry_readable=" as *u8); ewn(fd, registry_ok)
119 ew(fd, " canonical_rows=" as *u8); ewn(fd, rows)
120 ew(fd, " present=" as *u8); ewn(fd, present)
121 ew(fd, " missing=" as *u8); ewn(fd, missing)
122 if ok == 1 { ew(fd, " verdict=GREEN\n" as *u8) } else { ew(fd, " verdict=RED\n" as *u8) }
123 if fd == 1 {
124 if logfd >= 1 { fd = logfd } else { fd = 0 }
125 } else {
126 sys_close(fd); fd = 0
127 }
128 }
129
130 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
131 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
132 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
133 let ctr__dry: *i64 = gv_ctr()
134 ctr__dry[0] = ok
135 ctr__dry[1] = 1
136 let rc__dry: i64 = gv_verdict("ORGAN-REGISTRY-GATE" as *u8, ctr__dry, "teeth unchanged; verdict emission migrated onto the shared base class" as *u8)
137 sys_exit(rc__dry)
138 return rc__dry
139}