code wiki / _hdl_build / nx_eco_graph_store_gate.nx
nx_eco_graph_store_gate.nx source
↩ module page · 71 lines · 4257 B
1// nx_eco_graph_store_gate.nx -- proves the R1 graph STORE (build-once persist / query-fast load): a finalized
2// synthetic graph SAVEs, LOADs back zero-copy, and answers the SAME queries (roots-to-god / children / coupling
3// / name-lookup), with a NEG-CONTROL (a missing name resolves to -1). This is the SSOT the API/MCP/wiki read.
4// license_tier: ORIGINAL expect_exit: 0
5import "nx_syscalls.nx"
6import "nx_eco_graph.nx"
7
8func hw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
9func pn(v: i64) -> i64 { let b: *u8=sys_mmap(24); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m} let t: *u8=sys_mmap(24); var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var j: i64=0; while j<k{b[j]=t[k-1-j];j=j+1} sys_write(1,b,k); return 0 }
10func slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
11func gi(g: *EcoGraph, s: *u8) -> i64 { return eg_intern(g, s, slen(s)) }
12func zero(v: *u8, n: i64) -> i64 { var i: i64=0; while i<n { v[i]=0 as u8; i=i+1 } return 0 }
13
14func main() -> i64 {
15 hw("=== nx_eco_graph_store_gate -- build-once persist / query-fast load via SOVEREIGN seg_store ===\n" as *u8)
16 let g: *EcoGraph = eg_new(64, 256, 4096, 128)
17 let god: i64 = gi(g, "nx_god.nx" as *u8)
18 let a: i64 = gi(g, "nx_a.nx" as *u8)
19 let b: i64 = gi(g, "nx_b.nx" as *u8)
20 let top: i64 = gi(g, "nx_top.nx" as *u8)
21 let orph: i64 = gi(g, "nx_orph.nx" as *u8)
22 let isl: i64 = gi(g, "nx_island.nx" as *u8)
23 eg_add_edge(g, a, god); eg_add_edge(g, b, god); eg_add_edge(g, top, a); eg_add_edge(g, top, b); eg_add_edge(g, orph, god)
24 eg_finalize(g)
25 let sr: i64 = eg_save(g, "knowledge/store/ecogtest" as *u8) // SOVEREIGN seg_store, not a raw file
26
27 let g2: *EcoGraph = eg_load("knowledge/store/ecogtest" as *u8)
28 var fails: i64 = 0
29
30 // T1: save ok + load ok + counts survive
31 var t1: i64 = 0
32 if sr == 0 { if (g2 as i64) != 0 { if g2.node_count == 6 { if g2.edge_count == 5 { t1 = 1 } } } }
33 if t1 == 1 { hw("T1 PASS save+load round-trip (nodes=6 edges=5 from disk)\n" as *u8) } else { fails=fails+1; hw("T1 FAIL save/load (sr="); pn(sr); hw(")\n" as *u8) }
34
35 if (g2 as i64) == 0 { hw("NX-ECO-STORE RED load failed\n" as *u8); sys_exit(1); return 1 }
36
37 // T2: name lookup on the loaded store
38 let gd: i64 = eg_find(g2, "nx_god.nx" as *u8, 9)
39 let td: i64 = eg_find(g2, "nx_top.nx" as *u8, 9)
40 var t2: i64 = 0
41 if gd >= 0 { if td >= 0 { t2 = 1 } }
42 if t2 == 1 { hw("T2 PASS eg_find on the store (god + top located)\n" as *u8) } else { fails=fails+1; hw("T2 FAIL find gd="); pn(gd); hw(" td="); pn(td); hw("\n" as *u8) }
43
44 // T3: coupling from the loaded store
45 var t3: i64 = 0
46 if eg_ca(g2, gd) == 3 { if eg_ce(g2, td) == 2 { t3 = 1 } }
47 if t3 == 1 { hw("T3 PASS coupling from disk: Ca(god)=3 Ce(top)=2\n" as *u8) } else { fails=fails+1; hw("T3 FAIL Ca(god)="); pn(eg_ca(g2,gd)); hw(" Ce(top)="); pn(eg_ce(g2,td)); hw("\n" as *u8) }
48
49 // T4: roots-to-god(top)=3 and children(god)=4 traversed on the LOADED store
50 let vis: *u8 = sys_mmap(64)
51 let out: *i64 = sys_mmap(64*8) as *i64
52 let on: *i64 = sys_mmap(8) as *i64
53 zero(vis, 64); on[0] = 0; vis[td] = 1 as u8
54 eg_ancestors(g2, td, vis, out, on, 64)
55 let anc: i64 = on[0]
56 zero(vis, 64); on[0] = 0; vis[gd] = 1 as u8
57 eg_descendants(g2, gd, vis, out, on, 64)
58 let desc: i64 = on[0]
59 var t4: i64 = 0
60 if anc == 3 { if desc == 4 { t4 = 1 } }
61 if t4 == 1 { hw("T4 PASS queries on the store: roots-to-god(top)=3, children(god)=4\n" as *u8) } else { fails=fails+1; hw("T4 FAIL anc="); pn(anc); hw(" desc="); pn(desc); hw("\n" as *u8) }
62
63 // T5 NEG-CONTROL: a missing name -> -1 (find is real, not always-hit)
64 var t5: i64 = 0
65 if eg_find(g2, "nx_nope.nx" as *u8, 10) == (0 - 1) { t5 = 1 }
66 if t5 == 1 { hw("T5 PASS neg-control: missing name resolves to -1\n" as *u8) } else { fails=fails+1; hw("T5 FAIL missing name found\n" as *u8) }
67
68 if fails == 0 { hw("NX-ECO-STORE GREEN -- build-once persist + query-fast load round-trips (the API/MCP/wiki SSOT)\n" as *u8); sys_exit(0); return 0 }
69 hw("NX-ECO-STORE RED fails=" as *u8); pn(fails); hw("\n" as *u8)
70 sys_exit(1); return 1
71}