code wiki / _hdl_build / nx_vizsla_ics_gate.nx

nx_vizsla_ics_gate.nx source

↩ module page · 428 lines · 16224 B

1// nx_vizsla_ics_gate.nx -- VIZSLA R-CAL.4 gate: RFC 5545 .ics EXPORT is gate-proven against 2// construction-known fixtures. GENUINE COMPOSITION: forks the blessed calendar organ to LOAD the 3// store, then the ics organ to EXPORT it -- the gate never fabricates store bytes itself. 4// 5// fixture (loaded via nx_vizsla_calendar, seg-6201): 6// EVENT launch 2026-06-20 10:00 +90 title "pizza,night;launch" (TEXT-escape exercise) 7// EVENT ride 2026-07-03 08:00 +120 title 86-char token (75-octet folding exercise) 8// EVENT far 2026-09-01 08:00 +60 out-of-window (exclusion exercise) 9// RRULE standup 2026-06-01 WEEKLY/2 x6 -> native RRULE passthrough 10// RRULE bday 2024-02-29 YEARLY/1 x50 -> intersects window via last-occurrence (2073) 11// window 2026-06-01..2026-07-31 => events=2 rules=2 => 4 VEVENTs. 12// 13// Rows: 14// 1 export-ok exit 0 + VIZSLA-ICS-VERDICT events=2 rules=2 15// 2 vcalendar-frame BEGIN:VCALENDAR + VERSION:2.0 + PRODID + END:VCALENDAR 16// 3 crlf-strict every LF preceded by CR (RFC5545 line ends), >=10 lines 17// 4 vevent-count exactly 4 BEGIN:VEVENT 18// 5 dtstart-dtend launch DTSTART:20260620T100000 + DTEND:20260620T113000 19// 6 uid-stable UID:launch-20260620@nishi.vizsla 20// 7 rrule-weekly RRULE:FREQ=WEEKLY;INTERVAL=2;COUNT=6 21// 8 rrule-yearly RRULE:FREQ=YEARLY;INTERVAL=1;COUNT=50 22// 9 summary-escaping SUMMARY:pizza\,night\;launch (backslash-escaped , and ;) 23// 10 fold-75 no physical line >75 octets + a CRLF+space continuation + folded=1 24// 11 window-exclusion UID:far- ABSENT (out-of-window event not exported) 25// 12 export-determinism two exports byte-identical (no clocks in the artifact) 26// 13 loud-fail-bad-date export from=2026-13-99 => exit 1 [negative control] 27// 14 loud-fail-unwritable export into a nonexistent dir => exit 1 [negative control] 28// Evidence: VIZSLA-ICS-GATE -> stdout + knowledge/status/vizsla_gate.log; exit 0 iff 14/14. 29// spec: knowledge/research/2026-06-22-vizsla-sclass-roadmap.md license_tier: ORIGINAL 30import "nx_syscalls.nx" 31 32func ig_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 33func ig_p(s: *u8) -> i64 { sys_write(1, s, ig_slen(s)); return 0 } 34 35func ig_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 ig_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 ig_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, ig_slen(content)) 58 sys_close(fd) 59 return 0 60} 61 62func ig_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 ig_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 ig_has(path: *u8, needle: *u8) -> i64 { 110 let szp: *i64 = sys_mmap(16) as *i64 111 let b: *u8 = ig_readall(path, szp) 112 let sz: i64 = szp[0] 113 let n: i64 = ig_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 ig_count(path: *u8, needle: *u8) -> i64 { 129 let szp: *i64 = sys_mmap(16) as *i64 130 let b: *u8 = ig_readall(path, szp) 131 let sz: i64 = szp[0] 132 let n: i64 = ig_slen(needle) 133 var cnt: i64 = 0 134 if sz < n { return 0 } 135 var i: i64 = 0 136 while i + n <= sz { 137 var ok: i64 = 1 138 var j: i64 = 0 139 while j < n { 140 if b[i + j] != needle[j] { ok = 0; j = n } else { j = j + 1 } 141 } 142 if ok == 1 { cnt = cnt + 1; i = i + n } else { i = i + 1 } 143 } 144 return cnt 145} 146 147// every LF preceded by CR; returns line count if strict, else -1 148func ig_crlf_lines(path: *u8) -> i64 { 149 let szp: *i64 = sys_mmap(16) as *i64 150 let b: *u8 = ig_readall(path, szp) 151 let sz: i64 = szp[0] 152 if sz <= 0 { return 0 - 1 } 153 var lines: i64 = 0 154 var i: i64 = 0 155 while i < sz { 156 if b[i] == (10 as u8) { 157 if i == 0 { return 0 - 1 } 158 if b[i - 1] != (13 as u8) { return 0 - 1 } 159 lines = lines + 1 160 } 161 i = i + 1 162 } 163 return lines 164} 165 166// longest physical line in octets EXCLUDING the CRLF terminator 167func ig_maxline(path: *u8) -> i64 { 168 let szp: *i64 = sys_mmap(16) as *i64 169 let b: *u8 = ig_readall(path, szp) 170 let sz: i64 = szp[0] 171 var mx: i64 = 0 172 var cur: i64 = 0 173 var i: i64 = 0 174 while i < sz { 175 if b[i] == (10 as u8) { 176 var l: i64 = cur - 1 177 if l < 0 { l = 0 } 178 if l > mx { mx = l } 179 cur = 0 180 } else { 181 cur = cur + 1 182 } 183 i = i + 1 184 } 185 if cur > mx { mx = cur } 186 return mx 187} 188 189func ig_fileeq(p1: *u8, p2: *u8) -> i64 { 190 let s1: *i64 = sys_mmap(16) as *i64 191 let s2: *i64 = sys_mmap(16) as *i64 192 let b1: *u8 = ig_readall(p1, s1) 193 let b2: *u8 = ig_readall(p2, s2) 194 if s1[0] != s2[0] { return 0 } 195 if s1[0] <= 0 { return 0 } 196 var i: i64 = 0 197 while i < s1[0] { 198 if b1[i] != b2[i] { return 0 } 199 i = i + 1 200 } 201 return 1 202} 203 204func ig_row(name: *u8, pass: i64) -> i64 { 205 ig_p("ROW " as *u8) 206 ig_p(name) 207 if pass == 1 { ig_p(" PASS\n" as *u8) } else { ig_p(" FAIL\n" as *u8) } 208 return pass 209} 210 211func ig_args(a1: *u8, a2: *u8, a3: *u8, a4: *u8, a5: *u8) -> *i64 { 212 let a: *i64 = sys_mmap(64) as *i64 213 a[0] = a1 as i64 214 a[1] = a2 as i64 215 a[2] = a3 as i64 216 a[3] = a4 as i64 217 a[4] = a5 as i64 218 a[5] = 0 219 return a 220} 221 222func ig_ensure(elf: *u8, organ: *u8) -> i64 { 223 let pr: i64 = sys_openat_rd(elf) 224 if pr >= 0 { sys_close(pr); return 0 } 225 ig_p(" instrument missing -> rebuilding via durable runner\n" as *u8) 226 ig_runv("_offc/nx_sov_build_run.elf" as *u8, ig_args(organ, 0 as *u8, 0 as *u8, 0 as *u8, 0 as *u8), "/tmp/vzi_rebuild.out" as *u8) 227 return 0 228} 229 230func main(argc: i64, argv: *i64) -> i64 { 231 ig_p("=== VIZSLA ICS GATE: RFC 5545 export KATs (frame/CRLF/UID/RRULE/escape/fold) ===\n" as *u8) 232 let cal: *u8 = "/tmp/nx_vizsla_calendar.sov.elf" as *u8 233 let ics: *u8 = "/tmp/nx_vizsla_ics.sov.elf" as *u8 234 ig_ensure(cal, "nx_vizsla_calendar" as *u8) 235 ig_ensure(ics, "nx_vizsla_ics" as *u8) 236 237 let pfx: *u8 = sys_mmap(128) 238 var po: i64 = 0 239 po = ig_cat(pfx, po, "/tmp/vziE" as *u8) 240 po = ig_catn(pfx, po, sys_now_us()) 241 po = ig_cat(pfx, po, "-" as *u8) 242 pfx[po] = 0 as u8 243 244 let fx: *u8 = "EVENT launch 2026-06-20 600 90 pizza,night;launch\nEVENT ride 2026-07-03 480 120 the-really-long-annual-friends-camping-trip-planning-weekend-at-the-lake-house-edition\nEVENT far 2026-09-01 480 60 out-of-window\nRRULE standup 2026-06-01 540 30 WEEKLY 2 6 biweekly-standup\nRRULE bday 2024-02-29 540 60 YEARLY 1 50 leap-birthday\n" as *u8 245 ig_write("/tmp/vzi_fx.txt" as *u8, fx) 246 247 var pass: i64 = 0 248 var r: i64 = 0 249 250 let rcl: i64 = ig_runv(cal, ig_args("load" as *u8, "/tmp/vzi_fx.txt" as *u8, pfx, "6201" as *u8, 0 as *u8), "/tmp/vzi_load.txt" as *u8) 251 if rcl != 0 { ig_p("FIXTURE LOAD FAILED -- gate cannot proceed\n" as *u8) } 252 253 // row 1: export ok + verdict counts 254 let outp: *u8 = "/tmp/vzi_out.ics" as *u8 255 let rc1: i64 = ig_runv(ics, ig_args("export" as *u8, pfx, "2026-06-01" as *u8, "2026-07-31" as *u8, outp), "/tmp/vzi_r1.txt" as *u8) 256 r = 0 257 if rc1 == 0 { 258 if ig_has("/tmp/vzi_r1.txt" as *u8, "VIZSLA-ICS-VERDICT events=2 rules=2" as *u8) == 1 { r = 1 } 259 } 260 pass = pass + ig_row("export-ok" as *u8, r) 261 262 // row 2: VCALENDAR frame 263 r = 0 264 if ig_has(outp, "BEGIN:VCALENDAR" as *u8) == 1 { 265 if ig_has(outp, "VERSION:2.0" as *u8) == 1 { 266 if ig_has(outp, "PRODID:-//Nishi//Vizsla Calendar//EN" as *u8) == 1 { 267 if ig_has(outp, "END:VCALENDAR" as *u8) == 1 { r = 1 } 268 } 269 } 270 } 271 pass = pass + ig_row("vcalendar-frame" as *u8, r) 272 273 // row 3: strict CRLF line ends 274 let nl: i64 = ig_crlf_lines(outp) 275 r = 0 276 if nl >= 10 { r = 1 } 277 pass = pass + ig_row("crlf-strict" as *u8, r) 278 279 // row 4: exactly 4 VEVENTs 280 r = 0 281 if ig_count(outp, "BEGIN:VEVENT" as *u8) == 4 { 282 if ig_count(outp, "END:VEVENT" as *u8) == 4 { r = 1 } 283 } 284 pass = pass + ig_row("vevent-count" as *u8, r) 285 286 // row 5: DTSTART/DTEND of launch 287 r = 0 288 if ig_has(outp, "DTSTART:20260620T100000" as *u8) == 1 { 289 if ig_has(outp, "DTEND:20260620T113000" as *u8) == 1 { r = 1 } 290 } 291 pass = pass + ig_row("dtstart-dtend" as *u8, r) 292 293 // row 6: stable UID 294 r = ig_has(outp, "UID:launch-20260620@nishi.vizsla" as *u8) 295 pass = pass + ig_row("uid-stable" as *u8, r) 296 297 // row 7: WEEKLY RRULE passthrough 298 r = ig_has(outp, "RRULE:FREQ=WEEKLY;INTERVAL=2;COUNT=6" as *u8) 299 pass = pass + ig_row("rrule-weekly" as *u8, r) 300 301 // row 8: YEARLY RRULE passthrough (the birthday substrate) 302 r = ig_has(outp, "RRULE:FREQ=YEARLY;INTERVAL=1;COUNT=50" as *u8) 303 pass = pass + ig_row("rrule-yearly" as *u8, r) 304 305 // row 9: TEXT escaping (needle built byte-wise: SUMMARY:pizza\,night\;launch) 306 let esc: *u8 = sys_mmap(64) 307 var eo: i64 = 0 308 eo = ig_cat(esc, eo, "SUMMARY:pizza" as *u8) 309 esc[eo] = 92 as u8; eo = eo + 1 310 eo = ig_cat(esc, eo, ",night" as *u8) 311 esc[eo] = 92 as u8; eo = eo + 1 312 eo = ig_cat(esc, eo, ";launch" as *u8) 313 esc[eo] = 0 as u8 314 r = ig_has(outp, esc) 315 pass = pass + ig_row("summary-escaping" as *u8, r) 316 317 // row 10: 75-octet folding (no long physical line + a CRLF+SPACE continuation + folded=1) 318 let contn: *u8 = sys_mmap(8) 319 contn[0] = 13 as u8 320 contn[1] = 10 as u8 321 contn[2] = 32 as u8 322 contn[3] = 0 as u8 323 r = 0 324 if ig_maxline(outp) <= 75 { 325 if ig_has(outp, contn) == 1 { 326 if ig_has("/tmp/vzi_r1.txt" as *u8, "folded=1" as *u8) == 1 { r = 1 } 327 } 328 } 329 pass = pass + ig_row("fold-75" as *u8, r) 330 331 // row 11: out-of-window event excluded 332 r = 0 333 if ig_has(outp, "UID:far-" as *u8) == 0 { r = 1 } 334 pass = pass + ig_row("window-exclusion" as *u8, r) 335 336 // row 12: determinism (re-export byte-identical) 337 ig_runv(ics, ig_args("export" as *u8, pfx, "2026-06-01" as *u8, "2026-07-31" as *u8, "/tmp/vzi_out2.ics" as *u8), "/tmp/vzi_r12.txt" as *u8) 338 r = ig_fileeq(outp, "/tmp/vzi_out2.ics" as *u8) 339 pass = pass + ig_row("export-determinism" as *u8, r) 340 341 // row 13: loud-fail bad date (negative control) 342 let rc13: i64 = ig_runv(ics, ig_args("export" as *u8, pfx, "2026-13-99" as *u8, "2026-07-31" as *u8, "/tmp/vzi_bad.ics" as *u8), "/tmp/vzi_r13.txt" as *u8) 343 r = 0 344 if rc13 == 1 { r = 1 } 345 pass = pass + ig_row("loud-fail-bad-date" as *u8, r) 346 347 // row 14: loud-fail unwritable out path (negative control) 348 let rc14: i64 = ig_runv(ics, ig_args("export" as *u8, pfx, "2026-06-01" as *u8, "2026-07-31" as *u8, "/tmp/no_such_dir_vzi/out.ics" as *u8), "/tmp/vzi_r14.txt" as *u8) 349 r = 0 350 if rc14 == 1 { r = 1 } 351 pass = pass + ig_row("loud-fail-unwritable" as *u8, r) 352 353 // ===== IMPORT round-trip: the .ics we just exported -> a fresh store the calendar organ reads ===== 354 let pfxI: *u8 = sys_mmap(128) 355 var pio: i64 = 0 356 pio = ig_cat(pfxI, pio, "/tmp/vziI" as *u8) 357 pio = ig_catn(pfxI, pio, sys_now_us()) 358 pio = ig_cat(pfxI, pio, "-" as *u8) 359 pfxI[pio] = 0 as u8 360 361 // row 15: import the exported .ics -> events=2 rules=2 new=4 362 let rc15: i64 = ig_runv(ics, ig_args("import" as *u8, outp, pfxI, "6301" as *u8, 0 as *u8), "/tmp/vzi_r15.txt" as *u8) 363 r = 0 364 if rc15 == 0 { 365 if ig_has("/tmp/vzi_r15.txt" as *u8, "VIZSLA-ICS-IMPORT events=2 rules=2 new=4 dup_instore=0 segment=seg-6301" as *u8) == 1 { r = 1 } 366 } 367 pass = pass + ig_row("import-ok" as *u8, r) 368 369 // row 16: the imported single event is queryable through the real calendar organ 370 let rc16: i64 = ig_runv(cal, ig_args("day" as *u8, pfxI, "2026-07-03" as *u8, 0 as *u8, 0 as *u8), "/tmp/vzi_r16.txt" as *u8) 371 r = 0 372 if rc16 == 0 { 373 if ig_has("/tmp/vzi_r16.txt" as *u8, "VIZSLA-CAL-EVENT date=2026-07-03 start=08:00 end=10:00 dur=120 id=ride" as *u8) == 1 { r = 1 } 374 } 375 pass = pass + ig_row("import-event-queryable" as *u8, r) 376 377 // row 17: the imported RRULEs expand -- YEARLY birthday (Feb clamp) + WEEKLY interval survive 378 let rc17: i64 = ig_runv(cal, ig_args("expand" as *u8, pfxI, "2026-01-01" as *u8, "2026-07-31" as *u8, 0 as *u8), "/tmp/vzi_r17.txt" as *u8) 379 r = 0 380 if rc17 == 0 { 381 if ig_has("/tmp/vzi_r17.txt" as *u8, "VIZSLA-CAL-OCC id=bday date=2026-02-28 start=09:00 end=10:00 freq=YEARLY" as *u8) == 1 { 382 if ig_has("/tmp/vzi_r17.txt" as *u8, "VIZSLA-CAL-OCC id=standup date=2026-06-15 start=09:00 end=09:30 freq=WEEKLY" as *u8) == 1 { r = 1 } 383 } 384 } 385 pass = pass + ig_row("import-rrule-queryable" as *u8, r) 386 387 // row 18: re-import is CID-idempotent (never-lose additive plane) 388 let rc18: i64 = ig_runv(ics, ig_args("import" as *u8, outp, pfxI, "6302" as *u8, 0 as *u8), "/tmp/vzi_r18.txt" as *u8) 389 r = 0 390 if rc18 == 0 { 391 if ig_has("/tmp/vzi_r18.txt" as *u8, "new=0 dup_instore=4 segment=none" as *u8) == 1 { r = 1 } 392 } 393 pass = pass + ig_row("import-idempotent" as *u8, r) 394 395 // row 19: import missing file (negative control) 396 let rc19: i64 = ig_runv(ics, ig_args("import" as *u8, "/tmp/vzi_nope.ics" as *u8, pfxI, "6303" as *u8, 0 as *u8), "/tmp/vzi_r19.txt" as *u8) 397 r = 0 398 if rc19 == 1 { r = 1 } 399 pass = pass + ig_row("import-loud-fail" as *u8, r) 400 401 let permil: i64 = (pass * 1000) / 19 402 let logfd: i64 = sys_openat_append("knowledge/status/vizsla_gate.log" as *u8, 0x1a4) 403 var fdi: i64 = 0 404 while fdi < 2 { 405 var fd: i64 = 1 406 if fdi == 1 { fd = logfd } 407 if fd > 0 { 408 let line: *u8 = sys_mmap(256) 409 var o: i64 = 0 410 o = ig_cat(line, o, "VIZSLA-ICS-GATE epoch=" as *u8) 411 o = ig_catn(line, o, sys_now_realtime_sec()) 412 o = ig_cat(line, o, " rows=19 pass=" as *u8) 413 o = ig_catn(line, o, pass) 414 o = ig_cat(line, o, " permil=" as *u8) 415 o = ig_catn(line, o, permil) 416 if pass == 19 { 417 o = ig_cat(line, o, " verdict=GREEN\n" as *u8) 418 } else { 419 o = ig_cat(line, o, " verdict=RED\n" as *u8) 420 } 421 sys_write(fd, line, o) 422 } 423 fdi = fdi + 1 424 } 425 if logfd > 0 { sys_close(logfd) } 426 if pass == 19 { return 0 } 427 return 1 428}