code wiki / _hdl_build / nx_memplane_gate.nx
nx_memplane_gate.nx source
↩ module page · 181 lines · 10074 B
1// nx_memplane_gate.nx -- SOVEREIGN gate for the memory-plane primitives. No shell, no fixtures.
2//
3// WHAT THIS REPLACES AND WHY
4// --------------------------
5// Every gate written for the memory-plane organs this session was a `sh` script executed by WSL from
6// PowerShell: Linux shell + Windows host + third-party runtime, sitting directly on the program path,
7// in an estate whose own doctrine is bits-up sovereignty and whose own gates are ELF organs.
8// I matched the shape of a test script instead of the shape of the estate's gates.
9//
10// The shell dependency was not merely impure, it CORRUPTED WORK TWICE IN ONE SESSION:
11// * a generated .sh written with CRLF made `2>&1` a syntax error in dash;
12// * a PowerShell single-quoted Replace put a LITERAL backtick-n into nx_memroot.nx, producing
13// "UNRESOLVED identifier 'n'" -- source and deployed binary silently diverged until a build failed.
14// Neither failure mode exists inside an organ.
15//
16// A gate could not be an organ while each organ owned `main()` -- there was nothing to import. With
17// the primitives in nx_memplane_lib, the gate becomes what the estate's gates are: an organ that
18// imports the lib and asserts IN-PROCESS. Every tooth below runs on string literals in memory, so
19// this gate touches no filesystem at all and cannot be broken by a quoting or line-ending bug.
20//
21// Each tooth pins a defence that was learned ONCE and then MISSING from the next organ written --
22// which is precisely why the primitives now live in one place.
23//
24// exit: 0 = all teeth green; 1 = a defence regressed
25// Sovereign: imports nx_memplane_lib (which imports only nx_syscalls). license_tier: ORIGINAL
26import "nx_memplane_lib.nx"
27
28func main(argc: i64, argv: *i64) -> i64 {
29 let msg: *u8 = sys_mmap(8192)
30 let out: *i64 = sys_mmap(64) as *i64
31 let scratch: *u8 = sys_mmap(1024)
32 var pass: i64 = 0
33 var fail: i64 = 0
34
35 // -- tiny in-process assert helpers (printing, not exiting: report EVERY regression, not the first)
36 var m: i64 = mp_cat(msg, 0, "nx_memplane_gate: in-process, no shell\n" as *u8)
37 mp_say(msg, m)
38
39 // TOOTH 1 -- the false-GREEN defence: aggregate dumps are recognised in ONE place
40 var t: i64 = 1
41 var ok: i64 = 1
42 if mp_is_dump("reference-orphan-catalogue.md" as *u8) != 1 { ok = 0 }
43 if mp_is_dump("MEMORY.overflow.md" as *u8) != 1 { ok = 0 }
44 if mp_is_dump("reference-index-recent-refs-dormant.md" as *u8) != 1 { ok = 0 }
45 if mp_is_dump("reference-memory-lineage.md" as *u8) != 1 { ok = 0 }
46 if mp_is_dump("feedback-ordinary-memory.md" as *u8) != 0 { ok = 0 }
47 if ok == 1 { pass = pass + 1; m = mp_cat(msg, 0, " PASS T1 dumps recognised, ordinary names are not\n" as *u8) } else { fail = fail + 1; m = mp_cat(msg, 0, " FAIL T1 dump recognition\n" as *u8) }
48 mp_say(msg, m)
49
50 // TOOTH 2 -- a markdown link is found and delimited
51 let s2: *u8 = "see [x](feedback-alpha.md) here" as *u8
52 ok = 0
53 if mp_link_next(s2, mp_len(s2), 0, out) == 1 {
54 let l2: i64 = out[1] - out[0]
55 if l2 == 17 { ok = 1 } // "feedback-alpha.md"
56 }
57 if ok == 1 { pass = pass + 1; m = mp_cat(msg, 0, " PASS T2 markdown link delimited\n" as *u8) } else { fail = fail + 1; m = mp_cat(msg, 0, " FAIL T2 markdown link\n" as *u8) }
58 mp_say(msg, m)
59
60 // TOOTH 3 -- a wiki link is found and flagged as wiki
61 let s3: *u8 = "cites [[feedback-beta]] end" as *u8
62 ok = 0
63 if mp_link_next(s3, mp_len(s3), 0, out) == 1 { if out[2] == 1 { ok = 1 } }
64 if ok == 1 { pass = pass + 1; m = mp_cat(msg, 0, " PASS T3 wiki link flagged\n" as *u8) } else { fail = fail + 1; m = mp_cat(msg, 0, " FAIL T3 wiki link\n" as *u8) }
65 mp_say(msg, m)
66
67 // TOOTH 4 -- A NESTED ARRAY LITERAL IS NOT A WIKI LINK.
68 // 25 of memhealth's first 427 "dangling" links were code blocks like [[433,94,2],[434,90,2]],
69 // visible only because an independent oracle disagreed by a systematic amount.
70 let s4: *u8 = "code: [[433,94,2],[434,90,2],[0,0,0]] done" as *u8
71 ok = 1
72 if mp_link_next(s4, mp_len(s4), 0, out) == 1 { ok = 0 }
73 if ok == 1 { pass = pass + 1; m = mp_cat(msg, 0, " PASS T4 nested array literal rejected\n" as *u8) } else { fail = fail + 1; m = mp_cat(msg, 0, " FAIL T4 array literal read as a link\n" as *u8) }
74 mp_say(msg, m)
75
76 // TOOTH 5 -- a path-bearing target points outside the corpus, so it is not a sibling edge
77 let s5: *u8 = "see [x](../../elsewhere/thing.md) here" as *u8
78 ok = 1
79 if mp_link_next(s5, mp_len(s5), 0, out) == 1 { ok = 0 }
80 if ok == 1 { pass = pass + 1; m = mp_cat(msg, 0, " PASS T5 external path rejected\n" as *u8) } else { fail = fail + 1; m = mp_cat(msg, 0, " FAIL T5 external path accepted\n" as *u8) }
81 mp_say(msg, m)
82
83 // TOOTH 6 -- a markdown link that names no .md is not a memory edge
84 let s6: *u8 = "see [x](https:something) here" as *u8
85 ok = 1
86 if mp_link_next(s6, mp_len(s6), 0, out) == 1 { ok = 0 }
87 if ok == 1 { pass = pass + 1; m = mp_cat(msg, 0, " PASS T6 non-.md target rejected\n" as *u8) } else { fail = fail + 1; m = mp_cat(msg, 0, " FAIL T6 non-.md target accepted\n" as *u8) }
88 mp_say(msg, m)
89
90 // TOOTH 7 -- a DECLARED parent is read from frontmatter
91 let s7: *u8 = "---\nname: stub\nparent: canon.md\n---\nbody\n" as *u8
92 ok = 0
93 if mp_declparent(s7, mp_len(s7), scratch) > 0 {
94 if mp_streq(scratch, "canon.md" as *u8) == 1 { ok = 1 }
95 }
96 if ok == 1 { pass = pass + 1; m = mp_cat(msg, 0, " PASS T7 frontmatter parent read\n" as *u8) } else { fail = fail + 1; m = mp_cat(msg, 0, " FAIL T7 frontmatter parent\n" as *u8) }
97 mp_say(msg, m)
98
99 // TOOTH 8 -- A 'parent:' IN PROSE IS NOT AN EDGE.
100 // A grep counting prose hits reported 9 declared edges in the live corpus; the truth was 0.
101 let s8: *u8 = "---\nname: p\n---\nsee [[canon]] (parent: 3-way same-day pattern) and kill children before parent:\n" as *u8
102 ok = 0
103 if mp_declparent(s8, mp_len(s8), scratch) == 0 { ok = 1 }
104 if ok == 1 { pass = pass + 1; m = mp_cat(msg, 0, " PASS T8 prose parent ignored\n" as *u8) } else { fail = fail + 1; m = mp_cat(msg, 0, " FAIL T8 prose counted as an edge\n" as *u8) }
105 mp_say(msg, m)
106
107 // TOOTH 9 -- .md discrimination
108 ok = 1
109 if mp_is_md("a.md" as *u8) != 1 { ok = 0 }
110 if mp_is_md("a.txt" as *u8) != 0 { ok = 0 }
111 if mp_is_md("md" as *u8) != 0 { ok = 0 }
112 if ok == 1 { pass = pass + 1; m = mp_cat(msg, 0, " PASS T9 .md discrimination\n" as *u8) } else { fail = fail + 1; m = mp_cat(msg, 0, " FAIL T9 .md discrimination\n" as *u8) }
113 mp_say(msg, m)
114
115 // TOOTH 10 -- the name index round-trips, and a miss is a miss
116 let names: *u8 = sys_mmap(MP_SLOT * 8)
117 let tbl: *i64 = sys_mmap(8 * MP_HASH) as *i64
118 var i: i64 = 0
119 while i < MP_HASH { tbl[i] = 0; i = i + 1 }
120 let n0: *u8 = mp_nameptr(names, 0)
121 let e0: i64 = mp_cat(n0, 0, "feedback-alpha.md" as *u8)
122 n0[e0] = 0 as u8
123 let n1: *u8 = mp_nameptr(names, 1)
124 let e1: i64 = mp_cat(n1, 0, "project-beta.md" as *u8)
125 n1[e1] = 0 as u8
126 mp_put(tbl, names, 0)
127 mp_put(tbl, names, 1)
128 let miss: i64 = 0 - 1
129 ok = 1
130 if mp_get(tbl, names, "feedback-alpha.md" as *u8, 17) != 0 { ok = 0 }
131 if mp_get(tbl, names, "project-beta.md" as *u8, 15) != 1 { ok = 0 }
132 if mp_get(tbl, names, "absent.md" as *u8, 9) != miss { ok = 0 }
133 if ok == 1 { pass = pass + 1; m = mp_cat(msg, 0, " PASS T10 name index round-trips, miss returns -1\n" as *u8) } else { fail = fail + 1; m = mp_cat(msg, 0, " FAIL T10 name index\n" as *u8) }
134 mp_say(msg, m)
135
136 // TOOTH 11 -- the scanner ADVANCES: two links in one buffer are both found
137 let s11: *u8 = "[[alpha]] then [x](beta.md) end" as *u8
138 var found: i64 = 0
139 var from: i64 = 0
140 var g: i64 = 1
141 while g == 1 {
142 if mp_link_next(s11, mp_len(s11), from, out) == 1 { found = found + 1; from = out[3] } else { g = 0 }
143 }
144 if found == 2 { pass = pass + 1; m = mp_cat(msg, 0, " PASS T11 scanner advances past a match (2 links)\n" as *u8) } else { fail = fail + 1; m = mp_cat(msg, 0, " FAIL T11 scanner did not advance\n" as *u8) }
145 mp_say(msg, m)
146
147 // TOOTH 12 -- SELF-DECLARATION beats a name list.
148 // The same dump silenced the orphan metric in three organs because each guarded only against the
149 // artefacts it already knew. A generated file now declares itself in its HEAD.
150 let d1: *u8 = "# Orphan catalogue\n<!-- NX-DERIVED: regenerated artefact, not authored memory -->\n\n- [x](x.md)\n" as *u8
151 let d2: *u8 = "# An ordinary authored memory\n\nbody body body\n" as *u8
152 ok = 1
153 if mp_is_derived(d1, mp_len(d1)) != 1 { ok = 0 }
154 if mp_is_derived(d2, mp_len(d2)) != 0 { ok = 0 }
155 if ok == 1 { pass = pass + 1; m = mp_cat(msg, 0, " PASS T12 derived marker detected, authored file is not\n" as *u8) } else { fail = fail + 1; m = mp_cat(msg, 0, " FAIL T12 derived marker\n" as *u8) }
156 mp_say(msg, m)
157
158 // TOOTH 13 -- a MENTION deep in prose is not a declaration.
159 // Otherwise a memory that DISCUSSES the marker (this session wrote several) would exclude itself.
160 let pad: *u8 = "x" as *u8
161 let big: *u8 = sys_mmap(4096)
162 var bo: i64 = mp_cat(big, 0, "# An authored memory about derived artefacts\n\n" as *u8)
163 var q: i64 = 0
164 while q < 700 { bo = mp_cat(big, bo, pad); q = q + 1 }
165 bo = mp_cat(big, bo, "\n<!-- NX-DERIVED: regenerated artefact, not authored memory -->\n" as *u8)
166 big[bo] = 0 as u8
167 ok = 0
168 if mp_is_derived(big, bo) == 0 { ok = 1 }
169 if ok == 1 { pass = pass + 1; m = mp_cat(msg, 0, " PASS T13 deep mention is not a declaration\n" as *u8) } else { fail = fail + 1; m = mp_cat(msg, 0, " FAIL T13 deep mention counted as derived\n" as *u8) }
170 mp_say(msg, m)
171
172 var r: i64 = mp_cat(msg, 0, "RESULT: " as *u8)
173 r = mp_catn(msg, r, pass)
174 r = mp_cat(msg, r, " passed, " as *u8)
175 r = mp_catn(msg, r, fail)
176 r = mp_cat(msg, r, " failed\n" as *u8)
177 mp_say(msg, r)
178 if fail > 0 { sys_exit(1); return 1 }
179 sys_exit(0)
180 return 0
181}