code wiki / _hdl_build / nx_vizsla_calendar_rec_gate.nx
nx_vizsla_calendar_rec_gate.nx source
↩ module page · 314 lines · 13489 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"
35import "nx_gate_verdict.nx"
36
37func og_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
38func og_p(s: *u8) -> i64 { sys_write(1, s, og_slen(s)); return 0 }
39
40func og_cat(dst: *u8, off: i64, s: *u8) -> i64 {
41 var i: i64 = 0
42 while s[i] != (0 as u8) { dst[off + i] = s[i]; i = i + 1 }
43 return off + i
44}
45
46func og_catn(dst: *u8, off: i64, v: i64) -> i64 {
47 var o: i64 = off
48 var m: i64 = v
49 if m < 0 { dst[o] = 45 as u8; o = o + 1; m = 0 - m }
50 let t: *u8 = sys_mmap(28)
51 var k: i64 = 0
52 if m == 0 { t[0] = 48 as u8; k = 1 }
53 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
54 var i: i64 = 0
55 while i < k { dst[o + i] = t[k - 1 - i]; i = i + 1 }
56 return o + k
57}
58
59func og_write(path: *u8, content: *u8) -> i64 {
60 let fd: i64 = sys_openat_wr(path, 0x1a4)
61 if fd < 0 { return 0 - 1 }
62 sys_write(fd, content, og_slen(content))
63 sys_close(fd)
64 return 0
65}
66
67func og_readall(path: *u8, szout: *i64) -> *u8 {
68 let fd: i64 = sys_openat_rd(path)
69 if fd < 0 { szout[0] = 0 - 1; return 0 as *u8 }
70 let sz: i64 = sys_lseek(fd, 0, 2)
71 sys_lseek(fd, 0, 0)
72 let buf: *u8 = sys_mmap(sz + 64)
73 var got: i64 = 0
74 var n: i64 = 1
75 while n > 0 {
76 n = sys_read(fd, (buf as i64 + got) as *u8, 65536)
77 if n > 0 { got = got + n }
78 }
79 sys_close(fd)
80 szout[0] = got
81 return buf
82}
83
84func og_runv(elf: *u8, args: *i64, outpath: *u8) -> i64 {
85 let pid: i64 = sys_fork()
86 if pid == 0 {
87 if (outpath as i64) != 0 {
88 let ofd: i64 = sys_openat_wr(outpath, 0x1a4)
89 if ofd >= 0 { sys_dup3(ofd, 1, 0); sys_dup3(ofd, 2, 0) }
90 }
91 let argv: *i64 = sys_mmap(128) as *i64
92 argv[0] = elf as i64
93 var i: i64 = 0
94 var go: i64 = 1
95 while go == 1 {
96 if args[i] == 0 { go = 0 } else {
97 argv[i + 1] = args[i]
98 i = i + 1
99 }
100 }
101 argv[i + 1] = 0
102 let envp: *i64 = sys_mmap(16) as *i64
103 envp[0] = 0
104 sys_execve(elf, argv, envp)
105 sys_exit(127)
106 }
107 let st: *i64 = sys_mmap(16) as *i64
108 sys_wait4(pid, st, 0)
109 let sig: i64 = st[0] & 0x7f
110 if sig != 0 { return 128 + sig }
111 return (st[0] >> 8) & 0xff
112}
113
114func og_has(path: *u8, needle: *u8) -> i64 {
115 let szp: *i64 = sys_mmap(16) as *i64
116 let b: *u8 = og_readall(path, szp)
117 let sz: i64 = szp[0]
118 let n: i64 = og_slen(needle)
119 if sz < n { return 0 }
120 var i: i64 = 0
121 while i + n <= sz {
122 var ok: i64 = 1
123 var j: i64 = 0
124 while j < n {
125 if b[i + j] != needle[j] { ok = 0; j = n } else { j = j + 1 }
126 }
127 if ok == 1 { return 1 }
128 i = i + 1
129 }
130 return 0
131}
132
133func og_fileeq(p1: *u8, p2: *u8) -> i64 {
134 let s1: *i64 = sys_mmap(16) as *i64
135 let s2: *i64 = sys_mmap(16) as *i64
136 let b1: *u8 = og_readall(p1, s1)
137 let b2: *u8 = og_readall(p2, s2)
138 if s1[0] != s2[0] { return 0 }
139 if s1[0] <= 0 { return 0 }
140 var i: i64 = 0
141 while i < s1[0] {
142 if b1[i] != b2[i] { return 0 }
143 i = i + 1
144 }
145 return 1
146}
147
148func og_row(name: *u8, pass: i64) -> i64 {
149 og_p("ROW " as *u8)
150 og_p(name)
151 if pass == 1 { og_p(" PASS\n" as *u8) } else { og_p(" FAIL\n" as *u8) }
152 return pass
153}
154
155func og_args(a1: *u8, a2: *u8, a3: *u8, a4: *u8, a5: *u8) -> *i64 {
156 let a: *i64 = sys_mmap(64) as *i64
157 a[0] = a1 as i64
158 a[1] = a2 as i64
159 a[2] = a3 as i64
160 a[3] = a4 as i64
161 a[4] = a5 as i64
162 a[5] = 0
163 return a
164}
165
166func main(argc: i64, argv: *i64) -> i64 {
167 og_p("=== VIZSLA CALENDAR RECURRENCE GATE: RRULE expansion KATs (hand-computed occurrences) ===\n" as *u8)
168 var ob: *u8 = "buildroot/_build/nx_vizsla_calendar.sov.elf" as *u8
169 if argc > 1 { ob = argv[1] as *u8 }
170 let pr: i64 = sys_openat_rd(ob)
171 if pr >= 0 { sys_close(pr) }
172 else {
173 og_p(" instrument missing -> rebuilding via durable runner\n" as *u8)
174 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)
175 }
176
177 let pfx: *u8 = sys_mmap(128)
178 var po: i64 = 0
179 po = og_cat(pfx, po, "/tmp/vzcR" as *u8)
180 po = og_catn(pfx, po, sys_now_us())
181 po = og_cat(pfx, po, "-" as *u8)
182 pfx[po] = 0 as u8
183
184 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
185 og_write("/tmp/vzc_rr.txt" as *u8, rr)
186 let frr: *u8 = "/tmp/vzc_rr.txt" as *u8
187 var pass: i64 = 0
188 var r: i64 = 0
189
190 // row 1: load 3 rules
191 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)
192 r = 0
193 if rc1 == 0 {
194 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 }
195 }
196 pass = pass + og_row("rrule-load" as *u8, r)
197
198 // row 2: idempotent reload
199 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)
200 r = 0
201 if rc2 == 0 {
202 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 }
203 }
204 pass = pass + og_row("idempotent-reload" as *u8, r)
205
206 // rows 3-4: DAILY window W1
207 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)
208 r = 0
209 if rc3 == 0 {
210 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 }
211 }
212 pass = pass + og_row("daily-occurrence" as *u8, r)
213
214 r = og_has("/tmp/vzc_r3.txt" as *u8, "VIZSLA-CAL-EXPAND-VERDICT rules=4 occurrences=3" as *u8)
215 pass = pass + og_row("daily-verdict" as *u8, r)
216
217 // rows 5-6: WEEKLY window W2
218 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)
219 r = 0
220 if rc5 == 0 {
221 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 }
222 }
223 pass = pass + og_row("weekly-occurrence" as *u8, r)
224
225 r = og_has("/tmp/vzc_r5.txt" as *u8, "VIZSLA-CAL-EXPAND-VERDICT rules=4 occurrences=8" as *u8)
226 pass = pass + og_row("weekly-verdict" as *u8, r)
227
228 // rows 7-9: MONTHLY window W3 (with Feb clamp)
229 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)
230 r = 0
231 if rc7 == 0 {
232 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 }
233 }
234 pass = pass + og_row("monthly-occurrence" as *u8, r)
235
236 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)
237 pass = pass + og_row("monthly-clamp-feb" as *u8, r)
238
239 r = og_has("/tmp/vzc_r7.txt" as *u8, "VIZSLA-CAL-EXPAND-VERDICT rules=4 occurrences=5" as *u8)
240 pass = pass + og_row("monthly-verdict" as *u8, r)
241
242 // rows 10-12: YEARLY window W4 (leap-day start + non-leap clamp)
243 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)
244 r = 0
245 if rc10 == 0 {
246 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 }
247 }
248 pass = pass + og_row("yearly-occurrence" as *u8, r)
249
250 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)
251 pass = pass + og_row("yearly-leap-clamp" as *u8, r)
252
253 r = og_has("/tmp/vzc_r10y.txt" as *u8, "VIZSLA-CAL-EXPAND-VERDICT rules=4 occurrences=2" as *u8)
254 pass = pass + og_row("yearly-verdict" as *u8, r)
255
256 // row 13: determinism
257 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)
258 r = og_fileeq("/tmp/vzc_r5.txt" as *u8, "/tmp/vzc_r10.txt" as *u8)
259 pass = pass + og_row("expand-determinism" as *u8, r)
260
261 // row 14: loud-fail on bad freq (negative control)
262 og_write("/tmp/vzc_bad.txt" as *u8, "RRULE x 2026-01-01 0 1 HOURLY 1 3 bad\n" as *u8)
263 let pfx2: *u8 = sys_mmap(128)
264 var po2: i64 = 0
265 po2 = og_cat(pfx2, po2, "/tmp/vzcRB" as *u8)
266 po2 = og_catn(pfx2, po2, sys_now_us())
267 po2 = og_cat(pfx2, po2, "-" as *u8)
268 pfx2[po2] = 0 as u8
269 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)
270 r = 0
271 if rc11 == 1 { r = 1 }
272 pass = pass + og_row("loud-fail-bad-freq" as *u8, r)
273
274 // row 15: loud-fail on bad expand date (negative control)
275 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)
276 r = 0
277 if rc12 == 1 { r = 1 }
278 pass = pass + og_row("loud-fail-bad-date" as *u8, r)
279
280 let permil: i64 = (pass * 1000) / 15
281 let logfd: i64 = sys_openat_append("knowledge/status/vizsla_gate.log" as *u8, 0x1a4)
282 var fdi: i64 = 0
283 while fdi < 2 {
284 var fd: i64 = 1
285 if fdi == 1 { fd = logfd }
286 if fd > 0 {
287 let line: *u8 = sys_mmap(256)
288 var o: i64 = 0
289 o = og_cat(line, o, "VIZSLA-CAL-REC-GATE epoch=" as *u8)
290 o = og_catn(line, o, sys_now_realtime_sec())
291 o = og_cat(line, o, " rows=15 pass=" as *u8)
292 o = og_catn(line, o, pass)
293 o = og_cat(line, o, " permil=" as *u8)
294 o = og_catn(line, o, permil)
295 if pass == 15 {
296 o = og_cat(line, o, " verdict=GREEN\n" as *u8)
297 } else {
298 o = og_cat(line, o, " verdict=RED\n" as *u8)
299 }
300 sys_write(fd, line, o)
301 }
302 fdi = fdi + 1
303 }
304 if logfd > 0 { sys_close(logfd) }
305 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
306 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
307 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
308 let ctr__dry: *i64 = gv_ctr()
309 ctr__dry[0] = pass
310 ctr__dry[1] = 15
311 let rc__dry: i64 = gv_verdict("VIZSLA-CALENDAR-REC-GATE" as *u8, ctr__dry, "teeth unchanged; verdict emission migrated onto the shared base class" as *u8)
312 sys_exit(rc__dry)
313 return rc__dry
314}