code wiki / _hdl_build / nx_vizsla_calendar_gate.nx

nx_vizsla_calendar_gate.nx source

↩ module page · 284 lines · 11536 B

1// nx_vizsla_calendar_gate.nx -- VIZSLA R-CAL.0 gate: the calendar substrate (events + FREE/BUSY) is 2// gate-proven against construction-known fixtures with HAND-COMPUTED interval arithmetic (the oracle lives 3// in THIS header, not the module under test). 100% sovereign: self-writes fixtures via the organ's own 4// syscalls, forks the organ elf, greps stdout -- no bash, no foreign tooling. 5// 6// fixture events (seg-5001), all on 2026-06-22: 7// standup 09:00 +30 -> [540,570) 8// review 09:15 +45 -> [555,600) (overlaps standup -> merged busy [540,600) = 09:00-10:00) 9// lunch 12:00 +60 -> [720,780) = 12:00-13:00 10// hand math: sorted standup(540) review(555) lunch(720); 11// merged BUSY = [540,600) + [720,780) => 2 blocks, busy_min = 60+60 = 120; 12// FREE complement of [0,1440) = [0,540)+[600,720)+[780,1440) => 3 blocks, free_min = 540+120+660 = 1320; 13// busy_min+free_min = 1440 (invariant). empty day 2026-06-23 => 0 events, FREE 00:00-24:00, free_min=1440. 14// 15// Rows: 16// 1 loud-fail-missing-events load on absent file => exit 1 [negative control] 17// 2 cal-load 3 EVENT -> new=3 committed seg-5001 18// 3 idempotent-reload new=0 dup_instore=3 segment=none (law 10) 19// 4 day-event-sorted standup line exact (sort + HH:MM) 20// 5 day-overlap-end review end=10:00 (start+dur math) 21// 6 busy-merge BUSY 09:00-10:00 (overlap merged into one block) 22// 7 free-gap FREE 10:00-12:00 (the complement between busy blocks) 23// 8 day-verdict events=3 busy_min=120 free_min=1320 busy_blocks=2 free_blocks=3 24// 9 empty-day-filter 2026-06-23 => FREE 00:00-24:00 + events=0 free_min=1440 free_blocks=1 (date filter) 25// 10 day-determinism byte-identical twice 26// 11 loud-fail-bad-date day on 2026-13-99 => exit 1 [negative control] 27// Evidence: VIZSLA-CAL-GATE line -> stdout + knowledge/status/vizsla_gate.log; exit 0 iff 11/11. 28// spec: knowledge/research/2026-06-22-vizsla-sclass-roadmap.md license_tier: ORIGINAL 29import "nx_syscalls.nx" 30import "nx_gate_verdict.nx" 31 32func og_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 33func og_p(s: *u8) -> i64 { sys_write(1, s, og_slen(s)); return 0 } 34 35func og_cat(dst: *u8, off: i64, s: *u8) -> i64 { 36 var i: i64 = 0 37 while s[i] != (0 as u8) { dst[off + i] = s[i]; i = i + 1 } 38 return off + i 39} 40 41func og_catn(dst: *u8, off: i64, v: i64) -> i64 { 42 var o: i64 = off 43 var m: i64 = v 44 if m < 0 { dst[o] = 45 as u8; o = o + 1; m = 0 - m } 45 let t: *u8 = sys_mmap(28) 46 var k: i64 = 0 47 if m == 0 { t[0] = 48 as u8; k = 1 } 48 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 49 var i: i64 = 0 50 while i < k { dst[o + i] = t[k - 1 - i]; i = i + 1 } 51 return o + k 52} 53 54func og_write(path: *u8, content: *u8) -> i64 { 55 let fd: i64 = sys_openat_wr(path, 0x1a4) 56 if fd < 0 { return 0 - 1 } 57 sys_write(fd, content, og_slen(content)) 58 sys_close(fd) 59 return 0 60} 61 62func og_readall(path: *u8, szout: *i64) -> *u8 { 63 let fd: i64 = sys_openat_rd(path) 64 if fd < 0 { szout[0] = 0 - 1; return 0 as *u8 } 65 let sz: i64 = sys_lseek(fd, 0, 2) 66 sys_lseek(fd, 0, 0) 67 let buf: *u8 = sys_mmap(sz + 64) 68 var got: i64 = 0 69 var n: i64 = 1 70 while n > 0 { 71 n = sys_read(fd, (buf as i64 + got) as *u8, 65536) 72 if n > 0 { got = got + n } 73 } 74 sys_close(fd) 75 szout[0] = got 76 return buf 77} 78 79func og_runv(elf: *u8, args: *i64, outpath: *u8) -> i64 { 80 let pid: i64 = sys_fork() 81 if pid == 0 { 82 if (outpath as i64) != 0 { 83 let ofd: i64 = sys_openat_wr(outpath, 0x1a4) 84 if ofd >= 0 { sys_dup3(ofd, 1, 0); sys_dup3(ofd, 2, 0) } 85 } 86 let argv: *i64 = sys_mmap(128) as *i64 87 argv[0] = elf as i64 88 var i: i64 = 0 89 var go: i64 = 1 90 while go == 1 { 91 if args[i] == 0 { go = 0 } else { 92 argv[i + 1] = args[i] 93 i = i + 1 94 } 95 } 96 argv[i + 1] = 0 97 let envp: *i64 = sys_mmap(16) as *i64 98 envp[0] = 0 99 sys_execve(elf, argv, envp) 100 sys_exit(127) 101 } 102 let st: *i64 = sys_mmap(16) as *i64 103 sys_wait4(pid, st, 0) 104 let sig: i64 = st[0] & 0x7f 105 if sig != 0 { return 128 + sig } 106 return (st[0] >> 8) & 0xff 107} 108 109func og_has(path: *u8, needle: *u8) -> i64 { 110 let szp: *i64 = sys_mmap(16) as *i64 111 let b: *u8 = og_readall(path, szp) 112 let sz: i64 = szp[0] 113 let n: i64 = og_slen(needle) 114 if sz < n { return 0 } 115 var i: i64 = 0 116 while i + n <= sz { 117 var ok: i64 = 1 118 var j: i64 = 0 119 while j < n { 120 if b[i + j] != needle[j] { ok = 0; j = n } else { j = j + 1 } 121 } 122 if ok == 1 { return 1 } 123 i = i + 1 124 } 125 return 0 126} 127 128func og_fileeq(p1: *u8, p2: *u8) -> i64 { 129 let s1: *i64 = sys_mmap(16) as *i64 130 let s2: *i64 = sys_mmap(16) as *i64 131 let b1: *u8 = og_readall(p1, s1) 132 let b2: *u8 = og_readall(p2, s2) 133 if s1[0] != s2[0] { return 0 } 134 if s1[0] <= 0 { return 0 } 135 var i: i64 = 0 136 while i < s1[0] { 137 if b1[i] != b2[i] { return 0 } 138 i = i + 1 139 } 140 return 1 141} 142 143func og_row(name: *u8, pass: i64) -> i64 { 144 og_p("ROW " as *u8) 145 og_p(name) 146 if pass == 1 { og_p(" PASS\n" as *u8) } else { og_p(" FAIL\n" as *u8) } 147 return pass 148} 149 150func og_args(a1: *u8, a2: *u8, a3: *u8, a4: *u8, a5: *u8) -> *i64 { 151 let a: *i64 = sys_mmap(64) as *i64 152 a[0] = a1 as i64 153 a[1] = a2 as i64 154 a[2] = a3 as i64 155 a[3] = a4 as i64 156 a[4] = a5 as i64 157 a[5] = 0 158 return a 159} 160 161func main(argc: i64, argv: *i64) -> i64 { 162 og_p("=== VIZSLA CALENDAR GATE: events + FREE/BUSY KATs (hand-computed intervals) ===\n" as *u8) 163 var ob: *u8 = "buildroot/_build/nx_vizsla_calendar.sov.elf" as *u8 164 if argc > 1 { ob = argv[1] as *u8 } 165 let pr: i64 = sys_openat_rd(ob) 166 if pr >= 0 { sys_close(pr) } 167 else { 168 og_p(" instrument missing -> rebuilding via durable runner\n" as *u8) 169 og_runv("_offc/nx_sov_build_run.elf" as *u8, og_args("nx_vizsla_calendar" as *u8, 0 as *u8, 0 as *u8, 0 as *u8, 0 as *u8), "/tmp/vzc_rebuild.out" as *u8) 170 } 171 172 let pfx: *u8 = sys_mmap(128) 173 var po: i64 = 0 174 po = og_cat(pfx, po, "/tmp/vzcG" as *u8) 175 po = og_catn(pfx, po, sys_now_us()) 176 po = og_cat(pfx, po, "-" as *u8) 177 pfx[po] = 0 as u8 178 179 let ev: *u8 = "EVENT standup 2026-06-22 540 30 daily-sync\nEVENT review 2026-06-22 555 45 design-review\nEVENT lunch 2026-06-22 720 60 team-lunch\n" as *u8 180 og_write("/tmp/vzc_ev.txt" as *u8, ev) 181 let fev: *u8 = "/tmp/vzc_ev.txt" as *u8 182 let d22: *u8 = "2026-06-22" as *u8 183 let d23: *u8 = "2026-06-23" as *u8 184 var pass: i64 = 0 185 var r: i64 = 0 186 187 // row 1: loud-fail on missing events file 188 let rc1: i64 = og_runv(ob, og_args("load" as *u8, "/tmp/vzc_NOPE.txt" as *u8, pfx, "5001" as *u8, 0 as *u8), "/tmp/vzc_out1.txt" as *u8) 189 r = 0 190 if rc1 == 1 { r = 1 } 191 pass = pass + og_row("loud-fail-missing-events" as *u8, r) 192 193 // row 2: load 3 events 194 let rc2: i64 = og_runv(ob, og_args("load" as *u8, fev, pfx, "5001" as *u8, 0 as *u8), "/tmp/vzc_out2.txt" as *u8) 195 r = 0 196 if rc2 == 0 { 197 if og_has("/tmp/vzc_out2.txt" as *u8, "VIZSLA-CAL-LOAD scanned=3 new=3 dup_infile=0 dup_instore=0 segment=seg-5001" as *u8) == 1 { r = 1 } 198 } 199 pass = pass + og_row("cal-load" as *u8, r) 200 201 // row 3: idempotent reload (law 10) 202 let rc3: i64 = og_runv(ob, og_args("load" as *u8, fev, pfx, "5009" as *u8, 0 as *u8), "/tmp/vzc_out3.txt" as *u8) 203 r = 0 204 if rc3 == 0 { 205 if og_has("/tmp/vzc_out3.txt" as *u8, "VIZSLA-CAL-LOAD scanned=3 new=0 dup_infile=0 dup_instore=3 segment=none" as *u8) == 1 { r = 1 } 206 } 207 pass = pass + og_row("idempotent-reload" as *u8, r) 208 209 // rows 4-8: the day dashboard 210 let rc4: i64 = og_runv(ob, og_args("day" as *u8, pfx, d22, 0 as *u8, 0 as *u8), "/tmp/vzc_out4.txt" as *u8) 211 r = 0 212 if rc4 == 0 { 213 if og_has("/tmp/vzc_out4.txt" as *u8, "VIZSLA-CAL-EVENT date=2026-06-22 start=09:00 end=09:30 dur=30 id=standup title=daily-sync" as *u8) == 1 { r = 1 } 214 } 215 pass = pass + og_row("day-event-sorted" as *u8, r) 216 217 r = og_has("/tmp/vzc_out4.txt" as *u8, "VIZSLA-CAL-EVENT date=2026-06-22 start=09:15 end=10:00 dur=45 id=review title=design-review" as *u8) 218 pass = pass + og_row("day-overlap-end" as *u8, r) 219 220 r = og_has("/tmp/vzc_out4.txt" as *u8, "VIZSLA-CAL-BUSY start=09:00 end=10:00" as *u8) 221 pass = pass + og_row("busy-merge" as *u8, r) 222 223 r = og_has("/tmp/vzc_out4.txt" as *u8, "VIZSLA-CAL-FREE start=10:00 end=12:00" as *u8) 224 pass = pass + og_row("free-gap" as *u8, r) 225 226 r = og_has("/tmp/vzc_out4.txt" as *u8, "VIZSLA-CAL-DAY-VERDICT date=2026-06-22 events=3 busy_min=120 free_min=1320 busy_blocks=2 free_blocks=3" as *u8) 227 pass = pass + og_row("day-verdict" as *u8, r) 228 229 // row 9: empty-day date filter (proves the day filter is not vacuous) 230 let rc9: i64 = og_runv(ob, og_args("day" as *u8, pfx, d23, 0 as *u8, 0 as *u8), "/tmp/vzc_out9.txt" as *u8) 231 r = 0 232 if rc9 == 0 { 233 if og_has("/tmp/vzc_out9.txt" as *u8, "VIZSLA-CAL-FREE start=00:00 end=24:00" as *u8) == 1 { 234 if og_has("/tmp/vzc_out9.txt" as *u8, "VIZSLA-CAL-DAY-VERDICT date=2026-06-23 events=0 busy_min=0 free_min=1440 busy_blocks=0 free_blocks=1" as *u8) == 1 { r = 1 } 235 } 236 } 237 pass = pass + og_row("empty-day-filter" as *u8, r) 238 239 // row 10: determinism 240 og_runv(ob, og_args("day" as *u8, pfx, d22, 0 as *u8, 0 as *u8), "/tmp/vzc_out10.txt" as *u8) 241 r = og_fileeq("/tmp/vzc_out4.txt" as *u8, "/tmp/vzc_out10.txt" as *u8) 242 pass = pass + og_row("day-determinism" as *u8, r) 243 244 // row 11: loud-fail on bad date 245 let rc11: i64 = og_runv(ob, og_args("day" as *u8, pfx, "2026-13-99" as *u8, 0 as *u8, 0 as *u8), "/tmp/vzc_out11.txt" as *u8) 246 r = 0 247 if rc11 == 1 { r = 1 } 248 pass = pass + og_row("loud-fail-bad-date" as *u8, r) 249 250 let permil: i64 = (pass * 1000) / 11 251 let logfd: i64 = sys_openat_append("knowledge/status/vizsla_gate.log" as *u8, 0x1a4) 252 var fdi: i64 = 0 253 while fdi < 2 { 254 var fd: i64 = 1 255 if fdi == 1 { fd = logfd } 256 if fd > 0 { 257 let line: *u8 = sys_mmap(256) 258 var o: i64 = 0 259 o = og_cat(line, o, "VIZSLA-CAL-GATE epoch=" as *u8) 260 o = og_catn(line, o, sys_now_realtime_sec()) 261 o = og_cat(line, o, " rows=11 pass=" as *u8) 262 o = og_catn(line, o, pass) 263 o = og_cat(line, o, " permil=" as *u8) 264 o = og_catn(line, o, permil) 265 if pass == 11 { 266 o = og_cat(line, o, " verdict=GREEN\n" as *u8) 267 } else { 268 o = og_cat(line, o, " verdict=RED\n" as *u8) 269 } 270 sys_write(fd, line, o) 271 } 272 fdi = fdi + 1 273 } 274 if logfd > 0 { sys_close(logfd) } 275 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check 276 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled 277 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify. 278 let ctr__dry: *i64 = gv_ctr() 279 ctr__dry[0] = pass 280 ctr__dry[1] = 11 281 let rc__dry: i64 = gv_verdict("VIZSLA-CALENDAR-GATE" as *u8, ctr__dry, "teeth unchanged; verdict emission migrated onto the shared base class" as *u8) 282 sys_exit(rc__dry) 283 return rc__dry 284}