code wiki / _hdl_build / nx_vizsla_calendar_rec_gate.nx

nx_vizsla_calendar_rec_gate.nx source

↩ module page · 306 lines · 12932 B

1// nx_vizsla_calendar_rec_gate.nx -- VIZSLA R-CAL.1 gate: recurrence (iCalendar RFC5545 RRULE) is gate-proven 2// against construction-known fixtures with HAND-COMPUTED occurrence dates (oracle in THIS header). 100% 3// sovereign: self-writes fixtures via the organ's syscalls, forks the organ, greps stdout. 4// 5// rules (seg-6001): 6// pills 2026-06-20 08:00 +5 DAILY 1 x5 -> 06-20,21,22,23,24 7// standup 2026-06-01 09:00 +30 WEEKLY 2 x6 -> 06-01,15,29 / 07-13,27 / 08-10 8// report 2026-01-31 10:00 +30 MONTHLY 1 x4 -> 01-31, 02-28(clamp from 31), 03-31, 04-30(clamp) 9// bday 2024-02-29 09:00 +60 YEARLY 1 x4 -> 2024-02-29, 2025-02-28(leap clamp), 2026-02-28, 2027-02-28 10// windows: 11// W1 expand 2026-06-20..2026-06-22 => pills 06-20/21/22 only => rules=4 occurrences=3 12// W2 expand 2026-06-01..2026-07-01 => pills x5 + standup 06-01/15/29 x3 => rules=4 occurrences=8 13// W3 expand 2026-01-01..2026-04-30 => report x4 + bday 2026-02-28 => rules=4 occurrences=5 14// W4 expand 2024-01-01..2025-12-31 => bday 2024-02-29 + 2025-02-28 only => rules=4 occurrences=2 15// 16// Rows: 17// 1 rrule-load 4 RRULE -> new=4 committed seg-6001 18// 2 idempotent-reload new=0 dup_instore=4 (law 10) 19// 3 daily-occurrence pills 2026-06-20 n=1 (DAILY step + HH:MM) 20// 4 daily-verdict W1 rules=4 occurrences=3 (window cutoff drops 06-23/24) 21// 5 weekly-occurrence standup 2026-06-15 n=2 (WEEKLY interval=2 step) 22// 6 weekly-verdict W2 rules=4 occurrences=8 (cross-rule window union) 23// 7 monthly-occurrence report 2026-01-31 n=1 (MONTHLY step) 24// 8 monthly-clamp-feb report 2026-02-28 n=2 (31 -> Feb-clamp, leap-aware days-in-month) 25// 9 monthly-verdict W3 rules=4 occurrences=5 (report x4 + the bday 2026 instance) 26// 10 yearly-occurrence bday 2024-02-29 n=1 (YEARLY start lands on the true leap day) 27// 11 yearly-leap-clamp bday 2025-02-28 n=2 (Feb-29 birthday clamps in a non-leap year) 28// 12 yearly-verdict W4 rules=4 occurrences=2 29// 13 expand-determinism W2 byte-identical twice 30// 14 loud-fail-bad-freq RRULE freq=HOURLY => load exit 1 [negative control] 31// 15 loud-fail-bad-date expand from=2026-13-99 => exit 1 [negative control] 32// Evidence: VIZSLA-CAL-REC-GATE -> stdout + knowledge/status/vizsla_gate.log; exit 0 iff 15/15. 33// spec: knowledge/research/2026-06-22-vizsla-sclass-roadmap.md license_tier: ORIGINAL 34import "nx_syscalls.nx" 35 36func og_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 37func og_p(s: *u8) -> i64 { sys_write(1, s, og_slen(s)); return 0 } 38 39func og_cat(dst: *u8, off: i64, s: *u8) -> i64 { 40 var i: i64 = 0 41 while s[i] != (0 as u8) { dst[off + i] = s[i]; i = i + 1 } 42 return off + i 43} 44 45func og_catn(dst: *u8, off: i64, v: i64) -> i64 { 46 var o: i64 = off 47 var m: i64 = v 48 if m < 0 { dst[o] = 45 as u8; o = o + 1; m = 0 - m } 49 let t: *u8 = sys_mmap(28) 50 var k: i64 = 0 51 if m == 0 { t[0] = 48 as u8; k = 1 } 52 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 53 var i: i64 = 0 54 while i < k { dst[o + i] = t[k - 1 - i]; i = i + 1 } 55 return o + k 56} 57 58func og_write(path: *u8, content: *u8) -> i64 { 59 let fd: i64 = sys_openat_wr(path, 0x1a4) 60 if fd < 0 { return 0 - 1 } 61 sys_write(fd, content, og_slen(content)) 62 sys_close(fd) 63 return 0 64} 65 66func og_readall(path: *u8, szout: *i64) -> *u8 { 67 let fd: i64 = sys_openat_rd(path) 68 if fd < 0 { szout[0] = 0 - 1; return 0 as *u8 } 69 let sz: i64 = sys_lseek(fd, 0, 2) 70 sys_lseek(fd, 0, 0) 71 let buf: *u8 = sys_mmap(sz + 64) 72 var got: i64 = 0 73 var n: i64 = 1 74 while n > 0 { 75 n = sys_read(fd, (buf as i64 + got) as *u8, 65536) 76 if n > 0 { got = got + n } 77 } 78 sys_close(fd) 79 szout[0] = got 80 return buf 81} 82 83func og_runv(elf: *u8, args: *i64, outpath: *u8) -> i64 { 84 let pid: i64 = sys_fork() 85 if pid == 0 { 86 if (outpath as i64) != 0 { 87 let ofd: i64 = sys_openat_wr(outpath, 0x1a4) 88 if ofd >= 0 { sys_dup3(ofd, 1, 0); sys_dup3(ofd, 2, 0) } 89 } 90 let argv: *i64 = sys_mmap(128) as *i64 91 argv[0] = elf as i64 92 var i: i64 = 0 93 var go: i64 = 1 94 while go == 1 { 95 if args[i] == 0 { go = 0 } else { 96 argv[i + 1] = args[i] 97 i = i + 1 98 } 99 } 100 argv[i + 1] = 0 101 let envp: *i64 = sys_mmap(16) as *i64 102 envp[0] = 0 103 sys_execve(elf, argv, envp) 104 sys_exit(127) 105 } 106 let st: *i64 = sys_mmap(16) as *i64 107 sys_wait4(pid, st, 0) 108 let sig: i64 = st[0] & 0x7f 109 if sig != 0 { return 128 + sig } 110 return (st[0] >> 8) & 0xff 111} 112 113func og_has(path: *u8, needle: *u8) -> i64 { 114 let szp: *i64 = sys_mmap(16) as *i64 115 let b: *u8 = og_readall(path, szp) 116 let sz: i64 = szp[0] 117 let n: i64 = og_slen(needle) 118 if sz < n { return 0 } 119 var i: i64 = 0 120 while i + n <= sz { 121 var ok: i64 = 1 122 var j: i64 = 0 123 while j < n { 124 if b[i + j] != needle[j] { ok = 0; j = n } else { j = j + 1 } 125 } 126 if ok == 1 { return 1 } 127 i = i + 1 128 } 129 return 0 130} 131 132func og_fileeq(p1: *u8, p2: *u8) -> i64 { 133 let s1: *i64 = sys_mmap(16) as *i64 134 let s2: *i64 = sys_mmap(16) as *i64 135 let b1: *u8 = og_readall(p1, s1) 136 let b2: *u8 = og_readall(p2, s2) 137 if s1[0] != s2[0] { return 0 } 138 if s1[0] <= 0 { return 0 } 139 var i: i64 = 0 140 while i < s1[0] { 141 if b1[i] != b2[i] { return 0 } 142 i = i + 1 143 } 144 return 1 145} 146 147func og_row(name: *u8, pass: i64) -> i64 { 148 og_p("ROW " as *u8) 149 og_p(name) 150 if pass == 1 { og_p(" PASS\n" as *u8) } else { og_p(" FAIL\n" as *u8) } 151 return pass 152} 153 154func og_args(a1: *u8, a2: *u8, a3: *u8, a4: *u8, a5: *u8) -> *i64 { 155 let a: *i64 = sys_mmap(64) as *i64 156 a[0] = a1 as i64 157 a[1] = a2 as i64 158 a[2] = a3 as i64 159 a[3] = a4 as i64 160 a[4] = a5 as i64 161 a[5] = 0 162 return a 163} 164 165func main(argc: i64, argv: *i64) -> i64 { 166 og_p("=== VIZSLA CALENDAR RECURRENCE GATE: RRULE expansion KATs (hand-computed occurrences) ===\n" as *u8) 167 var ob: *u8 = "/tmp/nx_vizsla_calendar.sov.elf" as *u8 168 if argc > 1 { ob = argv[1] as *u8 } 169 let pr: i64 = sys_openat_rd(ob) 170 if pr >= 0 { sys_close(pr) } 171 else { 172 og_p(" instrument missing -> rebuilding via durable runner\n" as *u8) 173 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) 174 } 175 176 let pfx: *u8 = sys_mmap(128) 177 var po: i64 = 0 178 po = og_cat(pfx, po, "/tmp/vzcR" as *u8) 179 po = og_catn(pfx, po, sys_now_us()) 180 po = og_cat(pfx, po, "-" as *u8) 181 pfx[po] = 0 as u8 182 183 let rr: *u8 = "RRULE pills 2026-06-20 480 5 DAILY 1 5 morning-pills\nRRULE standup 2026-06-01 540 30 WEEKLY 2 6 biweekly-standup\nRRULE report 2026-01-31 600 30 MONTHLY 1 4 month-end-report\nRRULE bday 2024-02-29 540 60 YEARLY 1 4 leap-birthday\n" as *u8 184 og_write("/tmp/vzc_rr.txt" as *u8, rr) 185 let frr: *u8 = "/tmp/vzc_rr.txt" as *u8 186 var pass: i64 = 0 187 var r: i64 = 0 188 189 // row 1: load 3 rules 190 let rc1: i64 = og_runv(ob, og_args("load" as *u8, frr, pfx, "6001" as *u8, 0 as *u8), "/tmp/vzc_r1.txt" as *u8) 191 r = 0 192 if rc1 == 0 { 193 if og_has("/tmp/vzc_r1.txt" as *u8, "VIZSLA-CAL-LOAD scanned=4 new=4 dup_infile=0 dup_instore=0 segment=seg-6001" as *u8) == 1 { r = 1 } 194 } 195 pass = pass + og_row("rrule-load" as *u8, r) 196 197 // row 2: idempotent reload 198 let rc2: i64 = og_runv(ob, og_args("load" as *u8, frr, pfx, "6009" as *u8, 0 as *u8), "/tmp/vzc_r2.txt" as *u8) 199 r = 0 200 if rc2 == 0 { 201 if og_has("/tmp/vzc_r2.txt" as *u8, "VIZSLA-CAL-LOAD scanned=4 new=0 dup_infile=0 dup_instore=4 segment=none" as *u8) == 1 { r = 1 } 202 } 203 pass = pass + og_row("idempotent-reload" as *u8, r) 204 205 // rows 3-4: DAILY window W1 206 let rc3: i64 = og_runv(ob, og_args("expand" as *u8, pfx, "2026-06-20" as *u8, "2026-06-22" as *u8, 0 as *u8), "/tmp/vzc_r3.txt" as *u8) 207 r = 0 208 if rc3 == 0 { 209 if og_has("/tmp/vzc_r3.txt" as *u8, "VIZSLA-CAL-OCC id=pills date=2026-06-20 start=08:00 end=08:05 freq=DAILY n=1 title=morning-pills" as *u8) == 1 { r = 1 } 210 } 211 pass = pass + og_row("daily-occurrence" as *u8, r) 212 213 r = og_has("/tmp/vzc_r3.txt" as *u8, "VIZSLA-CAL-EXPAND-VERDICT rules=4 occurrences=3" as *u8) 214 pass = pass + og_row("daily-verdict" as *u8, r) 215 216 // rows 5-6: WEEKLY window W2 217 let rc5: i64 = og_runv(ob, og_args("expand" as *u8, pfx, "2026-06-01" as *u8, "2026-07-01" as *u8, 0 as *u8), "/tmp/vzc_r5.txt" as *u8) 218 r = 0 219 if rc5 == 0 { 220 if og_has("/tmp/vzc_r5.txt" as *u8, "VIZSLA-CAL-OCC id=standup date=2026-06-15 start=09:00 end=09:30 freq=WEEKLY n=2 title=biweekly-standup" as *u8) == 1 { r = 1 } 221 } 222 pass = pass + og_row("weekly-occurrence" as *u8, r) 223 224 r = og_has("/tmp/vzc_r5.txt" as *u8, "VIZSLA-CAL-EXPAND-VERDICT rules=4 occurrences=8" as *u8) 225 pass = pass + og_row("weekly-verdict" as *u8, r) 226 227 // rows 7-9: MONTHLY window W3 (with Feb clamp) 228 let rc7: i64 = og_runv(ob, og_args("expand" as *u8, pfx, "2026-01-01" as *u8, "2026-04-30" as *u8, 0 as *u8), "/tmp/vzc_r7.txt" as *u8) 229 r = 0 230 if rc7 == 0 { 231 if og_has("/tmp/vzc_r7.txt" as *u8, "VIZSLA-CAL-OCC id=report date=2026-01-31 start=10:00 end=10:30 freq=MONTHLY n=1 title=month-end-report" as *u8) == 1 { r = 1 } 232 } 233 pass = pass + og_row("monthly-occurrence" as *u8, r) 234 235 r = og_has("/tmp/vzc_r7.txt" as *u8, "VIZSLA-CAL-OCC id=report date=2026-02-28 start=10:00 end=10:30 freq=MONTHLY n=2 title=month-end-report" as *u8) 236 pass = pass + og_row("monthly-clamp-feb" as *u8, r) 237 238 r = og_has("/tmp/vzc_r7.txt" as *u8, "VIZSLA-CAL-EXPAND-VERDICT rules=4 occurrences=5" as *u8) 239 pass = pass + og_row("monthly-verdict" as *u8, r) 240 241 // rows 10-12: YEARLY window W4 (leap-day start + non-leap clamp) 242 let rc10: i64 = og_runv(ob, og_args("expand" as *u8, pfx, "2024-01-01" as *u8, "2025-12-31" as *u8, 0 as *u8), "/tmp/vzc_r10y.txt" as *u8) 243 r = 0 244 if rc10 == 0 { 245 if og_has("/tmp/vzc_r10y.txt" as *u8, "VIZSLA-CAL-OCC id=bday date=2024-02-29 start=09:00 end=10:00 freq=YEARLY n=1 title=leap-birthday" as *u8) == 1 { r = 1 } 246 } 247 pass = pass + og_row("yearly-occurrence" as *u8, r) 248 249 r = og_has("/tmp/vzc_r10y.txt" as *u8, "VIZSLA-CAL-OCC id=bday date=2025-02-28 start=09:00 end=10:00 freq=YEARLY n=2 title=leap-birthday" as *u8) 250 pass = pass + og_row("yearly-leap-clamp" as *u8, r) 251 252 r = og_has("/tmp/vzc_r10y.txt" as *u8, "VIZSLA-CAL-EXPAND-VERDICT rules=4 occurrences=2" as *u8) 253 pass = pass + og_row("yearly-verdict" as *u8, r) 254 255 // row 13: determinism 256 og_runv(ob, og_args("expand" as *u8, pfx, "2026-06-01" as *u8, "2026-07-01" as *u8, 0 as *u8), "/tmp/vzc_r10.txt" as *u8) 257 r = og_fileeq("/tmp/vzc_r5.txt" as *u8, "/tmp/vzc_r10.txt" as *u8) 258 pass = pass + og_row("expand-determinism" as *u8, r) 259 260 // row 14: loud-fail on bad freq (negative control) 261 og_write("/tmp/vzc_bad.txt" as *u8, "RRULE x 2026-01-01 0 1 HOURLY 1 3 bad\n" as *u8) 262 let pfx2: *u8 = sys_mmap(128) 263 var po2: i64 = 0 264 po2 = og_cat(pfx2, po2, "/tmp/vzcRB" as *u8) 265 po2 = og_catn(pfx2, po2, sys_now_us()) 266 po2 = og_cat(pfx2, po2, "-" as *u8) 267 pfx2[po2] = 0 as u8 268 let rc11: i64 = og_runv(ob, og_args("load" as *u8, "/tmp/vzc_bad.txt" as *u8, pfx2, "6100" as *u8, 0 as *u8), "/tmp/vzc_r11.txt" as *u8) 269 r = 0 270 if rc11 == 1 { r = 1 } 271 pass = pass + og_row("loud-fail-bad-freq" as *u8, r) 272 273 // row 15: loud-fail on bad expand date (negative control) 274 let rc12: i64 = og_runv(ob, og_args("expand" as *u8, pfx, "2026-13-99" as *u8, "2026-07-01" as *u8, 0 as *u8), "/tmp/vzc_r12.txt" as *u8) 275 r = 0 276 if rc12 == 1 { r = 1 } 277 pass = pass + og_row("loud-fail-bad-date" as *u8, r) 278 279 let permil: i64 = (pass * 1000) / 15 280 let logfd: i64 = sys_openat_append("knowledge/status/vizsla_gate.log" as *u8, 0x1a4) 281 var fdi: i64 = 0 282 while fdi < 2 { 283 var fd: i64 = 1 284 if fdi == 1 { fd = logfd } 285 if fd > 0 { 286 let line: *u8 = sys_mmap(256) 287 var o: i64 = 0 288 o = og_cat(line, o, "VIZSLA-CAL-REC-GATE epoch=" as *u8) 289 o = og_catn(line, o, sys_now_realtime_sec()) 290 o = og_cat(line, o, " rows=15 pass=" as *u8) 291 o = og_catn(line, o, pass) 292 o = og_cat(line, o, " permil=" as *u8) 293 o = og_catn(line, o, permil) 294 if pass == 15 { 295 o = og_cat(line, o, " verdict=GREEN\n" as *u8) 296 } else { 297 o = og_cat(line, o, " verdict=RED\n" as *u8) 298 } 299 sys_write(fd, line, o) 300 } 301 fdi = fdi + 1 302 } 303 if logfd > 0 { sys_close(logfd) } 304 if pass == 15 { return 0 } 305 return 1 306}