code wiki / _hdl_build / nx_vizsla_graph_gate.nx

nx_vizsla_graph_gate.nx source

↩ module page · 217 lines · 9511 B

1// nx_vizsla_graph_gate.nx -- VIZSLA intro-graph gate. Construction-known oracle. 2// Graph (undirected): me--alice alice--bob bob--carol me--dave dave--carol 3// me->carol: me>dave>carol (len 2) BEATS me>alice>bob>carol (len 3) => BFS shortest = via dave 4// alice->carol: alice>bob>carol (len 2) 5// me->eve: eve not in graph => no path 6// link a b and link b a = the SAME canonical edge (undirected dedup) 7// Rows: 8// 1 link link me alice -> new=1 9// 2 undirected-dedup link alice me (reversed) -> new=0 (same canonical edge) 10// 3 neighbors neighbors me -> count=2, alice + dave present 11// 4 shortest-path path me carol -> length=2 via=dave path=me>dave>carol (3-hop NOT taken) 12// 5 symmetric path carol me -> length=2 (undirected) 13// 6 multi-hop path alice carol -> length=2 via=bob path=alice>bob>carol 14// 7 no-path path me eve -> length=-1 via=NONE path=NONE 15// 8 self-path path me me -> length=0 16// 9 neg-self-loop link bob bob -> fail loud exit 1 17// Evidence: VIZSLA-GRAPH-GATE -> stdout + knowledge/status/vizsla_gate.log; exit 0 iff 9/9. 18// license_tier: ORIGINAL 19import "nx_syscalls.nx" 20 21func og_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 22func og_p(s: *u8) -> i64 { sys_write(1, s, og_slen(s)); return 0 } 23 24func og_cat(dst: *u8, off: i64, s: *u8) -> i64 { 25 var i: i64 = 0 26 while s[i] != (0 as u8) { dst[off + i] = s[i]; i = i + 1 } 27 return off + i 28} 29 30func og_catn(dst: *u8, off: i64, v: i64) -> i64 { 31 var o: i64 = off 32 var m: i64 = v 33 if m < 0 { dst[o] = 45 as u8; o = o + 1; m = 0 - m } 34 let t: *u8 = sys_mmap(28) 35 var k: i64 = 0 36 if m == 0 { t[0] = 48 as u8; k = 1 } 37 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 38 var i: i64 = 0 39 while i < k { dst[o + i] = t[k - 1 - i]; i = i + 1 } 40 return o + k 41} 42 43func og_readall(path: *u8, szout: *i64) -> *u8 { 44 let fd: i64 = sys_openat_rd(path) 45 if fd < 0 { szout[0] = 0 - 1; return 0 as *u8 } 46 let sz: i64 = sys_lseek(fd, 0, 2) 47 sys_lseek(fd, 0, 0) 48 let buf: *u8 = sys_mmap(sz + 64) 49 var got: i64 = 0 50 var n: i64 = 1 51 while n > 0 { n = sys_read(fd, (buf as i64 + got) as *u8, 65536); if n > 0 { got = got + n } } 52 sys_close(fd) 53 szout[0] = got 54 return buf 55} 56 57func og_runv(elf: *u8, args: *i64, outpath: *u8) -> i64 { 58 let pid: i64 = sys_fork() 59 if pid == 0 { 60 if (outpath as i64) != 0 { 61 let ofd: i64 = sys_openat_wr(outpath, 0x1a4) 62 if ofd >= 0 { sys_dup3(ofd, 1, 0); sys_dup3(ofd, 2, 0) } 63 } 64 let argv: *i64 = sys_mmap(128) as *i64 65 argv[0] = elf as i64 66 var i: i64 = 0 67 var go: i64 = 1 68 while go == 1 { if args[i] == 0 { go = 0 } else { argv[i + 1] = args[i]; i = i + 1 } } 69 argv[i + 1] = 0 70 let envp: *i64 = sys_mmap(16) as *i64 71 envp[0] = 0 72 sys_execve(elf, argv, envp) 73 sys_exit(127) 74 } 75 let st: *i64 = sys_mmap(16) as *i64 76 sys_wait4(pid, st, 0) 77 let sig: i64 = st[0] & 0x7f 78 if sig != 0 { return 128 + sig } 79 return (st[0] >> 8) & 0xff 80} 81 82func og_idx(buf: *u8, sz: i64, needle: *u8) -> i64 { 83 let n: i64 = og_slen(needle) 84 if n == 0 { return 0 - 1 } 85 var i: i64 = 0 86 while i + n <= sz { 87 var ok: i64 = 1 88 var j: i64 = 0 89 while j < n { if buf[i + j] != needle[j] { ok = 0; j = n } else { j = j + 1 } } 90 if ok == 1 { return i } 91 i = i + 1 92 } 93 return 0 - 1 94} 95 96func og_has(path: *u8, needle: *u8) -> i64 { 97 let szp: *i64 = sys_mmap(16) as *i64 98 let b: *u8 = og_readall(path, szp) 99 if og_idx(b, szp[0], needle) >= 0 { return 1 } 100 return 0 101} 102 103func og_row(name: *u8, pass: i64) -> i64 { 104 og_p("ROW " as *u8); og_p(name) 105 if pass == 1 { og_p(" PASS\n" as *u8) } else { og_p(" FAIL\n" as *u8) } 106 return pass 107} 108 109func og_args(a1: *u8, a2: *u8, a3: *u8, a4: *u8, a5: *u8, a6: *u8) -> *i64 { 110 let a: *i64 = sys_mmap(64) as *i64 111 a[0] = a1 as i64; a[1] = a2 as i64; a[2] = a3 as i64; a[3] = a4 as i64; a[4] = a5 as i64; a[5] = a6 as i64; a[6] = 0 112 return a 113} 114 115func main(argc: i64, argv: *i64) -> i64 { 116 og_p("=== VIZSLA GRAPH GATE: intro-graph / who-knows-whom (BFS warm-intro path) ===\n" as *u8) 117 var ob: *u8 = "/tmp/nx_vizsla_graph.sov.elf" as *u8 118 if argc > 1 { ob = argv[1] as *u8 } 119 let pr: i64 = sys_openat_rd(ob) 120 if pr >= 0 { sys_close(pr) } 121 else { 122 og_p(" instrument missing -> rebuilding via durable runner\n" as *u8) 123 og_runv("_offc/nx_sov_build_run.elf" as *u8, og_args("nx_vizsla_graph" as *u8, 0 as *u8, 0 as *u8, 0 as *u8, 0 as *u8, 0 as *u8), "/tmp/vxg_rebuild.out" as *u8) 124 } 125 126 let pfx: *u8 = sys_mmap(128) 127 var po: i64 = 0 128 po = og_cat(pfx, po, "/tmp/vxgG" as *u8); po = og_catn(pfx, po, sys_now_us()); po = og_cat(pfx, po, "-" as *u8) 129 pfx[po] = 0 as u8 130 131 var pass: i64 = 0 132 var r: i64 = 0 133 134 // 1 link me--alice 135 let rc1: i64 = og_runv(ob, og_args("link" as *u8, pfx, "me" as *u8, "alice" as *u8, "2026-01-01" as *u8, 0 as *u8), "/tmp/vxg_l1.txt" as *u8) 136 r = 0 137 if rc1 == 0 { if og_has("/tmp/vxg_l1.txt" as *u8, "VIZSLA-GRAPH-LINK a=me b=alice new=1" as *u8) == 1 { r = 1 } } 138 pass = pass + og_row("link" as *u8, r) 139 140 // 2 reversed link -> same canonical edge -> new=0 141 let rc2: i64 = og_runv(ob, og_args("link" as *u8, pfx, "alice" as *u8, "me" as *u8, "2026-01-01" as *u8, 0 as *u8), "/tmp/vxg_l2.txt" as *u8) 142 r = 0 143 if rc2 == 0 { if og_has("/tmp/vxg_l2.txt" as *u8, "VIZSLA-GRAPH-LINK a=alice b=me new=0" as *u8) == 1 { r = 1 } } 144 pass = pass + og_row("undirected-dedup" as *u8, r) 145 146 // setup the rest of the graph 147 og_runv(ob, og_args("link" as *u8, pfx, "alice" as *u8, "bob" as *u8, "2026-01-01" as *u8, 0 as *u8), "/tmp/vxg_s1.txt" as *u8) 148 og_runv(ob, og_args("link" as *u8, pfx, "bob" as *u8, "carol" as *u8, "2026-01-01" as *u8, 0 as *u8), "/tmp/vxg_s2.txt" as *u8) 149 og_runv(ob, og_args("link" as *u8, pfx, "me" as *u8, "dave" as *u8, "2026-01-01" as *u8, 0 as *u8), "/tmp/vxg_s3.txt" as *u8) 150 og_runv(ob, og_args("link" as *u8, pfx, "dave" as *u8, "carol" as *u8, "2026-01-01" as *u8, 0 as *u8), "/tmp/vxg_s4.txt" as *u8) 151 152 // 3 neighbors me -> alice + dave, count=2 153 og_runv(ob, og_args("neighbors" as *u8, pfx, "me" as *u8, 0 as *u8, 0 as *u8, 0 as *u8), "/tmp/vxg_n.txt" as *u8) 154 r = 0 155 if og_has("/tmp/vxg_n.txt" as *u8, "count=2" as *u8) == 1 { 156 if og_has("/tmp/vxg_n.txt" as *u8, "alice" as *u8) == 1 { if og_has("/tmp/vxg_n.txt" as *u8, "dave" as *u8) == 1 { r = 1 } } 157 } 158 pass = pass + og_row("neighbors" as *u8, r) 159 160 // 4 shortest path me->carol = via dave (len 2), NOT the 3-hop via alice/bob 161 og_runv(ob, og_args("path" as *u8, pfx, "me" as *u8, "carol" as *u8, 0 as *u8, 0 as *u8), "/tmp/vxg_p1.txt" as *u8) 162 r = 0 163 if og_has("/tmp/vxg_p1.txt" as *u8, "VIZSLA-GRAPH-PATH from=me to=carol length=2 via=dave path=me>dave>carol" as *u8) == 1 { 164 if og_has("/tmp/vxg_p1.txt" as *u8, "me>alice>bob>carol" as *u8) == 0 { r = 1 } 165 } 166 pass = pass + og_row("shortest-path" as *u8, r) 167 168 // 5 symmetric: carol->me also length 2 169 og_runv(ob, og_args("path" as *u8, pfx, "carol" as *u8, "me" as *u8, 0 as *u8, 0 as *u8), "/tmp/vxg_p2.txt" as *u8) 170 r = og_has("/tmp/vxg_p2.txt" as *u8, "from=carol to=me length=2" as *u8) 171 pass = pass + og_row("symmetric" as *u8, r) 172 173 // 6 multi-hop alice->carol = via bob 174 og_runv(ob, og_args("path" as *u8, pfx, "alice" as *u8, "carol" as *u8, 0 as *u8, 0 as *u8), "/tmp/vxg_p3.txt" as *u8) 175 r = og_has("/tmp/vxg_p3.txt" as *u8, "VIZSLA-GRAPH-PATH from=alice to=carol length=2 via=bob path=alice>bob>carol" as *u8) 176 pass = pass + og_row("multi-hop" as *u8, r) 177 178 // 7 no path to a stranger 179 og_runv(ob, og_args("path" as *u8, pfx, "me" as *u8, "eve" as *u8, 0 as *u8, 0 as *u8), "/tmp/vxg_p4.txt" as *u8) 180 r = og_has("/tmp/vxg_p4.txt" as *u8, "from=me to=eve length=-1 via=NONE path=NONE" as *u8) 181 pass = pass + og_row("no-path" as *u8, r) 182 183 // 8 self path 184 og_runv(ob, og_args("path" as *u8, pfx, "me" as *u8, "me" as *u8, 0 as *u8, 0 as *u8), "/tmp/vxg_p5.txt" as *u8) 185 r = og_has("/tmp/vxg_p5.txt" as *u8, "from=me to=me length=0" as *u8) 186 pass = pass + og_row("self-path" as *u8, r) 187 188 // 9 neg self-loop link 189 let rc9: i64 = og_runv(ob, og_args("link" as *u8, pfx, "bob" as *u8, "bob" as *u8, "2026-01-01" as *u8, 0 as *u8), "/tmp/vxg_neg.txt" as *u8) 190 r = 0 191 if rc9 == 1 { r = 1 } 192 pass = pass + og_row("neg-self-loop" as *u8, r) 193 194 let permil: i64 = (pass * 1000) / 9 195 let logfd: i64 = sys_openat_append("knowledge/status/vizsla_gate.log" as *u8, 0x1a4) 196 var fdi: i64 = 0 197 while fdi < 2 { 198 var fd: i64 = 1 199 if fdi == 1 { fd = logfd } 200 if fd > 0 { 201 let line: *u8 = sys_mmap(256) 202 var o: i64 = 0 203 o = og_cat(line, o, "VIZSLA-GRAPH-GATE epoch=" as *u8) 204 o = og_catn(line, o, sys_now_realtime_sec()) 205 o = og_cat(line, o, " rows=9 pass=" as *u8) 206 o = og_catn(line, o, pass) 207 o = og_cat(line, o, " permil=" as *u8) 208 o = og_catn(line, o, permil) 209 if pass == 9 { o = og_cat(line, o, " verdict=GREEN\n" as *u8) } else { o = og_cat(line, o, " verdict=RED\n" as *u8) } 210 sys_write(fd, line, o) 211 } 212 fdi = fdi + 1 213 } 214 if logfd > 0 { sys_close(logfd) } 215 if pass == 9 { return 0 } 216 return 1 217}