code wiki / _hdl_build / nx_memroot.nx
nx_memroot.nx source
↩ module page · 447 lines · 17940 B
1// nx_memroot.nx -- FROM-GOD ROOTING FOR THE MEMORY PLANE. node -> parent -> ... -> ORIGIN.
2//
3// CONVERTED TO nx_memplane_lib 2026-08-07. It now owns only the rooting: the BFS, the declared-edge
4// authority, the lineage emitter and the parent proposals. Walk, name table, link scanner, read,
5// join and the derived-artefact test come from the lib.
6//
7// THE MISS THIS ORGAN CORRECTS (operator)
8// ---------------------------------------
9// "it should be a logical from god does this already exist ... to give feedback on parents and
10// descendents to give the ability to triangulate and get context and understand what is happening
11// vs a flat directory where naming and guessing are key"
12//
13// A whole session of lexical instruments -- slug similarity, rarity weighting, BM25 -- all COMPENSATE
14// FOR A FLAT DIRECTORY by getting better at guessing names. The estate had already written the
15// correction 22 days earlier, in a memory whose filename is the problem:
16// feedback-sprawl-orphans-use-fromgod-rooting-systems -- "USE the operator's established FROM-GOD +
17// DEPENDENCY-GRAPH + NO-ORPHANS systems FIRST ... do NOT propose a NEW ladder when the system exists."
18// ★★★★★★ THE ANTI-PATTERN HAS A MEMORY FILE, AND I REPRODUCED IT ANYWAY -- WHICH IS THE ADOPTION GAP
19// THE FILE DESCRIBES: A DOCTRINE THAT IS WRITTEN BUT UNROOTED IS NOT REACHABLE WHEN IT WOULD MATTER.
20//
21// MEASURED GAP: 1,514 files declare `node_type: memory` -- graph nodes BY DECLARATION -- and the
22// parent edge was absent from all of them. Declared and unwired, so the corpus behaves as a flat
23// directory of 2,181 files. That is the root cause under every symptom measured this session.
24//
25// MATCHED KNOWN GOOD: nx_build_rooted_gate, the NO-ORPHANS enforcer for organs -- root every node to
26// ORIGIN, census the FLOATING ones as the OPERATOR-PACED worklist; floating is not dead, it is
27// unrooted. Same shape, applied to the memory plane, rather than a new ladder.
28//
29// DECLARED EDGES ARE THE AUTHORITY; citation edges are the fallback. A node's derived parent is the
30// file that first cites it on a breadth-first walk from ORIGIN, so DEPTH is the true distance from
31// the front door. ⚠The declared pass runs AFTER the walk: applied before it, a declared parent has no
32// depth yet, so every declared node still received an accidental citation parent.
33// ★ AN AUTHORITY APPLIED BEFORE THE DATA IT DEPENDS ON EXISTS IS NOT AN AUTHORITY.
34//
35// usage: nx_memroot [--dir D] [--apply] [--floating] [--suggest]
36// exit: 0 = every node rooted (GREEN); 1 = floating nodes exist (the worklist)
37// Sovereign: imports nx_memplane_lib. license_tier: ORIGINAL
38import "nx_memplane_lib.nx"
39
40const MR_DIR: *u8 = "/mnt/c/Users/elder/.claude/projects/C--Users-elder/memory"
41const MR_ORIG: *u8 = "MEMORY.md"
42const MR_OUT: *u8 = "reference-memory-lineage.md"
43const MR_FBUF: i64 = 1048576
44const MR_OBUF: i64 = 4194304
45const MR_MSG: i64 = 16384
46const MR_HUBS: i64 = 96
47const MR_TOK: i64 = 8192
48
49// ---- topic fingerprints, used only by --suggest ------------------------------------------------
50
51func mr_tokput(set: *i64, h: i64) -> i64 {
52 var p: i64 = h & (MR_TOK - 1)
53 var r: i64 = 0
54 var go: i64 = 1
55 while go == 1 {
56 if set[p] == 0 { set[p] = h; r = 1; go = 0 } else {
57 if set[p] == h { go = 0 } else { p = (p + 1) & (MR_TOK - 1) }
58 }
59 }
60 return r
61}
62
63func mr_tokhas(set: *i64, h: i64) -> i64 {
64 var p: i64 = h & (MR_TOK - 1)
65 var r: i64 = 0
66 var go: i64 = 1
67 while go == 1 {
68 if set[p] == 0 { go = 0 } else {
69 if set[p] == h { r = 1; go = 0 } else { p = (p + 1) & (MR_TOK - 1) }
70 }
71 }
72 return r
73}
74
75func mr_tokset(buf: *u8, n: i64, set: *i64) -> i64 {
76 var h: i64 = 2166136261
77 var ln: i64 = 0
78 var i: i64 = 0
79 var c2: i64 = 0
80 while i <= n {
81 var c: i64 = 0 - 1
82 if i < n {
83 let b: u8 = buf[i]
84 if b >= (65 as u8) { if b <= (90 as u8) { c = (b as i64) + 32 } }
85 if b >= (97 as u8) { if b <= (122 as u8) { c = b as i64 } }
86 if b >= (48 as u8) { if b <= (57 as u8) { c = b as i64 } }
87 }
88 if c >= 0 {
89 h = h ^ c
90 h = (h * 16777619) & 1073741823
91 ln = ln + 1
92 } else {
93 if ln >= 6 { if h != 0 { c2 = c2 + mr_tokput(set, h) } }
94 h = 2166136261
95 ln = 0
96 }
97 i = i + 1
98 }
99 return c2
100}
101
102func mr_score(buf: *u8, n: i64, hub: *i64) -> i64 {
103 let seen: *i64 = sys_mmap(8 * MR_TOK) as *i64
104 var z: i64 = 0
105 while z < MR_TOK { seen[z] = 0; z = z + 1 }
106 var h: i64 = 2166136261
107 var ln: i64 = 0
108 var i: i64 = 0
109 var sc: i64 = 0
110 while i <= n {
111 var c: i64 = 0 - 1
112 if i < n {
113 let b: u8 = buf[i]
114 if b >= (65 as u8) { if b <= (90 as u8) { c = (b as i64) + 32 } }
115 if b >= (97 as u8) { if b <= (122 as u8) { c = b as i64 } }
116 if b >= (48 as u8) { if b <= (57 as u8) { c = b as i64 } }
117 }
118 if c >= 0 {
119 h = h ^ c
120 h = (h * 16777619) & 1073741823
121 ln = ln + 1
122 } else {
123 if ln >= 6 { if h != 0 { if mr_tokput(seen, h) == 1 { if mr_tokhas(hub, h) == 1 { sc = sc + 1 } } } }
124 h = 2166136261
125 ln = 0
126 }
127 i = i + 1
128 }
129 return sc
130}
131
132func main(argc: i64, argv: *i64) -> i64 {
133 var dir: *u8 = MR_DIR
134 var apply: i64 = 0
135 var listfloat: i64 = 0
136 var suggest: i64 = 0
137 var ai: i64 = 1
138 while ai < argc {
139 let a: *u8 = argv[ai] as *u8
140 if mp_streq(a, "--apply" as *u8) == 1 { apply = 1 }
141 if mp_streq(a, "--floating" as *u8) == 1 { listfloat = 1 }
142 if mp_streq(a, "--suggest" as *u8) == 1 { suggest = 1 }
143 if mp_streq(a, "--dir" as *u8) == 1 { if ai + 1 < argc { dir = argv[ai + 1] as *u8; ai = ai + 1 } }
144 ai = ai + 1
145 }
146
147 let msg: *u8 = sys_mmap(MR_MSG)
148 let names: *u8 = sys_mmap(MP_MAXF * MP_SLOT)
149 let tbl: *i64 = sys_mmap(8 * MP_HASH) as *i64
150 let parent: *i64 = sys_mmap(8 * MP_MAXF) as *i64
151 let depth: *i64 = sys_mmap(8 * MP_MAXF) as *i64
152 let kids: *i64 = sys_mmap(8 * MP_MAXF) as *i64
153 let queue: *i64 = sys_mmap(8 * MP_MAXF) as *i64
154 let decl: *i64 = sys_mmap(8 * MP_MAXF) as *i64
155 let fbuf: *u8 = sys_mmap(MR_FBUF)
156 let obuf: *u8 = sys_mmap(MR_OBUF)
157 let path: *u8 = sys_mmap(4096)
158 let probe: *u8 = sys_mmap(1024)
159 let slug: *u8 = sys_mmap(MP_SLOT * 2)
160 let out: *i64 = sys_mmap(64) as *i64
161
162 let cnt: i64 = mp_scan(dir, names, tbl, MP_MAXF)
163 if cnt < 0 {
164 var e: i64 = mp_cat(msg, 0, "nx_memroot: REFUSED -- cannot open the memory dir.\n" as *u8)
165 mp_say(msg, e)
166 return 1
167 }
168 var i: i64 = 0
169 while i < cnt { parent[i] = 0 - 1; depth[i] = 0 - 1; kids[i] = 0; decl[i] = 0 - 1; i = i + 1 }
170
171 // ---- PASS A: read DECLARED edges. The registry is the authority for organs; frontmatter is the
172 // authority here, because the edge belongs ON the node, not in a side-car (and not in a TSV).
173 var declared: i64 = 0
174 i = 0
175 while i < cnt {
176 mp_join(path, dir, mp_nameptr(names, i))
177 let tl: i64 = mp_readf(path, fbuf, MR_FBUF)
178 if tl > 0 {
179 let sl: i64 = mp_declparent(fbuf, tl, slug)
180 if sl > 0 {
181 var hit: i64 = mp_get(tbl, names, slug, sl)
182 if hit < 0 {
183 slug[sl] = 46 as u8
184 slug[sl + 1] = 109 as u8
185 slug[sl + 2] = 100 as u8
186 slug[sl + 3] = 0 as u8
187 hit = mp_get(tbl, names, slug, sl + 3)
188 }
189 if hit >= 0 { if hit != i { decl[i] = hit; declared = declared + 1 } }
190 }
191 }
192 i = i + 1
193 }
194
195 let orig: i64 = mp_get(tbl, names, MR_ORIG, mp_len(MR_ORIG))
196 if orig < 0 {
197 var e2: i64 = mp_cat(msg, 0, "nx_memroot: REFUSED -- no MEMORY.md; the memory plane has no ORIGIN to root to.\n" as *u8)
198 mp_say(msg, e2)
199 return 1
200 }
201 var qh: i64 = 0
202 var qt: i64 = 0
203 depth[orig] = 0
204 parent[orig] = orig
205 queue[qt] = orig
206 qt = qt + 1
207 var rooted: i64 = 1
208 var maxd: i64 = 0
209 var declrooted: i64 = 0
210
211 // ---- BFS. The parent is the SHALLOWEST citer, so depth is the true distance from the front door.
212 while qh < qt {
213 let cur: i64 = queue[qh]
214 qh = qh + 1
215 let cname: *u8 = mp_nameptr(names, cur)
216 var total: i64 = 0
217 if mp_is_dump(cname) == 0 {
218 mp_join(path, dir, cname)
219 total = mp_readf(path, fbuf, MR_FBUF)
220 // ⚠ DO NOT WALK THROUGH A DERIVED ARTEFACT. Asking the FILE, not a name list: this organ's
221 // own output silenced the orphan metric in three separate organs before the marker existed.
222 if mp_is_derived(fbuf, total) == 1 { total = 0 }
223 }
224 var from: i64 = 0
225 var go: i64 = 1
226 while go == 1 {
227 if total <= 0 { go = 0 } else {
228 if mp_link_next(fbuf, total, from, out) == 1 {
229 from = out[3]
230 let hit: i64 = mp_resolve(fbuf, out, names, tbl, probe)
231 if hit >= 0 {
232 if depth[hit] < 0 {
233 depth[hit] = depth[cur] + 1
234 parent[hit] = cur
235 kids[cur] = kids[cur] + 1
236 if depth[hit] > maxd { maxd = depth[hit] }
237 queue[qt] = hit
238 qt = qt + 1
239 rooted = rooted + 1
240 }
241 }
242 } else { go = 0 }
243 }
244 }
245 }
246
247 // ---- DECLARED EDGES WIN, applied AFTER the walk (see header) ----
248 var pass: i64 = 0
249 while pass < 64 {
250 var changed: i64 = 0
251 i = 0
252 while i < cnt {
253 if decl[i] >= 0 {
254 if parent[i] != decl[i] {
255 if depth[decl[i]] >= 0 {
256 if i != orig {
257 if parent[i] >= 0 { if kids[parent[i]] > 0 { kids[parent[i]] = kids[parent[i]] - 1 } }
258 if depth[i] < 0 { rooted = rooted + 1 }
259 parent[i] = decl[i]
260 depth[i] = depth[decl[i]] + 1
261 kids[decl[i]] = kids[decl[i]] + 1
262 if depth[i] > maxd { maxd = depth[i] }
263 declrooted = declrooted + 1
264 changed = 1
265 }
266 }
267 }
268 }
269 i = i + 1
270 }
271 if changed == 0 { pass = 64 } else { pass = pass + 1 }
272 }
273
274 let floating: i64 = cnt - rooted
275
276 // ---- emit the lineage registry ----
277 if apply == 1 {
278 var o: i64 = 0
279 o = mp_cat(obuf, o, "# Memory lineage -- GENERATED by nx_memroot, do not hand-edit\n" as *u8)
280 o = mp_cat(obuf, o, MP_DERIVED_MARK)
281 o = mp_cat(obuf, o, "\n\n" as *u8)
282 o = mp_cat(obuf, o, "Every ROOTED memory below carries `node -> parent -> ... -> ORIGIN(MEMORY.md)`. The parent is the\nSHALLOWEST file that cites it, so DEPTH is the true distance from the front door: how many hops a\nsession must make before it can reach that memory at all.\n\n" as *u8)
283 o = mp_cat(obuf, o, "FLOATING nodes have no citer anywhere. Floating is NOT dead -- it is UNROOTED, and rooting is\noperator-paced, exactly as nx_build_rooted_gate treats floating organs.\n\n## ROOTED\n\n" as *u8)
284 var d: i64 = 0
285 while d <= maxd {
286 var wrote: i64 = 0
287 i = 0
288 while i < cnt {
289 if depth[i] == d {
290 if wrote == 0 {
291 o = mp_cat(obuf, o, "\n### depth " as *u8)
292 o = mp_catn(obuf, o, d)
293 o = mp_cat(obuf, o, "\n" as *u8)
294 wrote = 1
295 }
296 o = mp_cat(obuf, o, "- [" as *u8)
297 o = mp_cat(obuf, o, mp_nameptr(names, i))
298 o = mp_cat(obuf, o, "](" as *u8)
299 o = mp_cat(obuf, o, mp_nameptr(names, i))
300 o = mp_cat(obuf, o, ") parent=" as *u8)
301 o = mp_cat(obuf, o, mp_nameptr(names, parent[i]))
302 if kids[i] > 0 {
303 o = mp_cat(obuf, o, " children=" as *u8)
304 o = mp_catn(obuf, o, kids[i])
305 }
306 o = mp_cat(obuf, o, "\n" as *u8)
307 }
308 i = i + 1
309 }
310 d = d + 1
311 }
312 o = mp_cat(obuf, o, "\n## FLOATING -- unrooted, the operator-paced worklist\n\n" as *u8)
313 i = 0
314 while i < cnt {
315 if depth[i] < 0 {
316 o = mp_cat(obuf, o, "- [" as *u8)
317 o = mp_cat(obuf, o, mp_nameptr(names, i))
318 o = mp_cat(obuf, o, "](" as *u8)
319 o = mp_cat(obuf, o, mp_nameptr(names, i))
320 o = mp_cat(obuf, o, ")\n" as *u8)
321 }
322 i = i + 1
323 }
324 o = mp_cat(obuf, o, "\nROOTED " as *u8)
325 o = mp_catn(obuf, o, rooted)
326 o = mp_cat(obuf, o, " / FLOATING " as *u8)
327 o = mp_catn(obuf, o, floating)
328 o = mp_cat(obuf, o, " / max depth " as *u8)
329 o = mp_catn(obuf, o, maxd)
330 o = mp_cat(obuf, o, "\n" as *u8)
331
332 mp_join(path, dir, MR_OUT)
333 let tmp: *u8 = ((path as i64) + 2048) as *u8
334 var t2: i64 = mp_cat(tmp, 0, path)
335 t2 = mp_cat(tmp, t2, ".mrtmp" as *u8)
336 tmp[t2] = 0 as u8
337 let wfd: i64 = sys_openat_wr(tmp, 420)
338 if wfd < 0 { return 1 }
339 let w2: i64 = mp_write_all(wfd, obuf, o)
340 sys_close(wfd)
341 if w2 != o { return 1 }
342 if sys_renameat(tmp, path) != 0 { return 1 }
343 }
344
345 // ---- --suggest: PROPOSE a parent for every floating node; never assign one ----
346 if suggest == 1 {
347 let hubs: *i64 = sys_mmap(8 * MR_HUBS) as *i64
348 var nh: i64 = 0
349 var need: i64 = 3
350 while need >= 1 {
351 i = 0
352 while i < cnt {
353 if nh < MR_HUBS {
354 if kids[i] >= need {
355 var dup: i64 = 0
356 var z: i64 = 0
357 while z < nh { if hubs[z] == i { dup = 1 } z = z + 1 }
358 if dup == 0 { hubs[nh] = i; nh = nh + 1 }
359 }
360 }
361 i = i + 1
362 }
363 need = need - 1
364 }
365 let hsets: *i64 = sys_mmap(8 * MR_HUBS * MR_TOK) as *i64
366 var h: i64 = 0
367 while h < nh {
368 let base: i64 = h * MR_TOK
369 var z2: i64 = 0
370 while z2 < MR_TOK { hsets[base + z2] = 0; z2 = z2 + 1 }
371 mp_join(path, dir, mp_nameptr(names, hubs[h]))
372 let hl: i64 = mp_readf(path, fbuf, MR_FBUF)
373 mr_tokset(fbuf, hl, ((hsets as i64) + base * 8) as *i64)
374 h = h + 1
375 }
376 var proposed: i64 = 0
377 i = 0
378 while i < cnt {
379 if depth[i] < 0 {
380 mp_join(path, dir, mp_nameptr(names, i))
381 let fl: i64 = mp_readf(path, fbuf, MR_FBUF)
382 var best: i64 = 0 - 1
383 var bs: i64 = 0
384 h = 0
385 while h < nh {
386 let sc: i64 = mr_score(fbuf, fl, ((hsets as i64) + (h * MR_TOK) * 8) as *i64)
387 if sc > bs { bs = sc; best = h }
388 h = h + 1
389 }
390 if best >= 0 { if bs >= 3 {
391 var d2: i64 = mp_cat(msg, 0, " parent: " as *u8)
392 d2 = mp_cat(msg, d2, mp_nameptr(names, hubs[best]))
393 d2 = mp_cat(msg, d2, " <- " as *u8)
394 d2 = mp_cat(msg, d2, mp_nameptr(names, i))
395 d2 = mp_cat(msg, d2, " (shared=" as *u8)
396 d2 = mp_catn(msg, d2, bs)
397 d2 = mp_cat(msg, d2, ")\n" as *u8)
398 mp_say(msg, d2)
399 proposed = proposed + 1
400 } }
401 }
402 i = i + 1
403 }
404 var sm: i64 = mp_cat(msg, 0, " -- " as *u8)
405 sm = mp_catn(msg, sm, proposed)
406 sm = mp_cat(msg, sm, " proposals from " as *u8)
407 sm = mp_catn(msg, sm, nh)
408 sm = mp_cat(msg, sm, " hubs. PROPOSALS ONLY -- nothing was written. Approve by adding `parent: <slug>` to the node's frontmatter.\n" as *u8)
409 mp_say(msg, sm)
410 }
411
412 if listfloat == 1 {
413 var shown: i64 = 0
414 i = 0
415 while i < cnt {
416 if depth[i] < 0 {
417 if shown < 25 {
418 var l: i64 = mp_cat(msg, 0, " FLOATING " as *u8)
419 l = mp_cat(msg, l, mp_nameptr(names, i))
420 l = mp_cat(msg, l, "\n" as *u8)
421 mp_say(msg, l)
422 shown = shown + 1
423 }
424 }
425 i = i + 1
426 }
427 }
428
429 var m: i64 = mp_cat(msg, 0, "nx_memroot: " as *u8)
430 m = mp_catn(msg, m, cnt)
431 m = mp_cat(msg, m, " memory nodes; DECLARED parents " as *u8)
432 m = mp_catn(msg, m, declared)
433 m = mp_cat(msg, m, " (rooted via declaration " as *u8)
434 m = mp_catn(msg, m, declrooted)
435 m = mp_cat(msg, m, "), ROOTED to ORIGIN " as *u8)
436 m = mp_catn(msg, m, rooted)
437 m = mp_cat(msg, m, ", FLOATING " as *u8)
438 m = mp_catn(msg, m, floating)
439 m = mp_cat(msg, m, ", max depth " as *u8)
440 m = mp_catn(msg, m, maxd)
441 if floating == 0 { m = mp_cat(msg, m, " GREEN: every node roots to god.\n" as *u8) } else {
442 m = mp_cat(msg, m, " -- floating is UNROOTED, not dead; rooting is operator-paced.\n" as *u8)
443 }
444 mp_say(msg, m)
445 if floating > 0 { return 1 }
446 return 0
447}