code wiki / _hdl_build / nx_vizsla_calendar_conflict_gate.nx
nx_vizsla_calendar_conflict_gate.nx source
↩ module page · 263 lines · 10730 B
1// nx_vizsla_calendar_conflict_gate.nx -- VIZSLA R-CAL.2 gate: conflict detection (overlap scan across the
2// expanded occurrences + single events) is gate-proven against construction-known fixtures with HAND-COMPUTED
3// overlaps (oracle in THIS header). 100% sovereign: self-writes fixtures via the organ's syscalls, forks the
4// organ, greps stdout.
5//
6// fixture (seg-7001), single EVENTs + one DAILY RRULE:
7// meeting 06-22 09:00 +60 -> [540,600)
8// call 06-22 09:30 +30 -> [570,600)
9// after 06-22 10:00 +30 -> [600,630) (back-to-back with meeting/call -- MUST NOT conflict)
10// lunch 06-22 12:00 +60 -> [720,780)
11// solo 06-23 09:00 +60 -> [540,600) (different day)
12// standup 06-22 09:15 +30 DAILY x1 -> [555,585) (a RECURRING occurrence that overlaps meeting AND call)
13// W1 conflicts 06-22..06-22 (sorted meeting/standup/call/after/lunch) =>
14// meeting x standup [555,585)=09:15-09:45 ; meeting x call [570,600)=09:30-10:00 ;
15// standup x call [570,585)=09:30-09:45 (after is adjacent -> NO clash) => occurrences=5 conflicts=3
16// W2 conflicts 06-23..06-23 => only solo => occurrences=1 conflicts=0 (no false positive)
17//
18// Rows:
19// 1 conflict-load 6 records -> new=6 seg-7001
20// 2 conflict-overlap-events meeting x call line exact (two single events)
21// 3 conflict-recurring meeting x standup line exact (the RRULE occurrence participates)
22// 4 conflict-three-way standup x call line exact
23// 5 conflict-verdict occurrences=5 conflicts=3 (adjacency 'after' counted but NOT a clash)
24// 6 no-conflict-other-day W2 occurrences=1 conflicts=0 (day isolation, no false positive)
25// 7 conflict-determinism W1 byte-identical twice
26// 8 loud-fail-bad-date conflicts from=2026-13-99 => exit 1 [negative control]
27// Evidence: VIZSLA-CAL-CONFLICT-GATE -> stdout + knowledge/status/vizsla_gate.log; exit 0 iff 8/8.
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 CONFLICT GATE: overlap-scan KATs (hand-computed clashes) ===\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/vzcC" 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 cf: *u8 = "EVENT meeting 2026-06-22 540 60 morning-meeting\nEVENT call 2026-06-22 570 30 client-call\nEVENT after 2026-06-22 600 30 right-after\nEVENT lunch 2026-06-22 720 60 lunch\nEVENT solo 2026-06-23 540 60 solo-day\nRRULE standup 2026-06-22 555 30 DAILY 1 1 daily-standup\n" as *u8
180 og_write("/tmp/vzc_cf.txt" as *u8, cf)
181 let fcf: *u8 = "/tmp/vzc_cf.txt" as *u8
182 var pass: i64 = 0
183 var r: i64 = 0
184
185 // row 1: load 6 records
186 let rc1: i64 = og_runv(ob, og_args("load" as *u8, fcf, pfx, "7001" as *u8, 0 as *u8), "/tmp/vzc_c1.txt" as *u8)
187 r = 0
188 if rc1 == 0 {
189 if og_has("/tmp/vzc_c1.txt" as *u8, "VIZSLA-CAL-LOAD scanned=6 new=6 dup_infile=0 dup_instore=0 segment=seg-7001" as *u8) == 1 { r = 1 }
190 }
191 pass = pass + og_row("conflict-load" as *u8, r)
192
193 // rows 2-5: W1 conflicts on 2026-06-22
194 let rc2: i64 = og_runv(ob, og_args("conflicts" as *u8, pfx, "2026-06-22" as *u8, "2026-06-22" as *u8, 0 as *u8), "/tmp/vzc_c2.txt" as *u8)
195 r = 0
196 if rc2 == 0 {
197 if og_has("/tmp/vzc_c2.txt" as *u8, "VIZSLA-CAL-CONFLICT date=2026-06-22 a=meeting@09:00-10:00 b=call@09:30-10:00 overlap=09:30-10:00" as *u8) == 1 { r = 1 }
198 }
199 pass = pass + og_row("conflict-overlap-events" as *u8, r)
200
201 r = og_has("/tmp/vzc_c2.txt" as *u8, "VIZSLA-CAL-CONFLICT date=2026-06-22 a=meeting@09:00-10:00 b=standup@09:15-09:45 overlap=09:15-09:45" as *u8)
202 pass = pass + og_row("conflict-recurring-participates" as *u8, r)
203
204 r = og_has("/tmp/vzc_c2.txt" as *u8, "VIZSLA-CAL-CONFLICT date=2026-06-22 a=standup@09:15-09:45 b=call@09:30-10:00 overlap=09:30-09:45" as *u8)
205 pass = pass + og_row("conflict-three-way" as *u8, r)
206
207 r = og_has("/tmp/vzc_c2.txt" as *u8, "VIZSLA-CAL-CONFLICT-VERDICT occurrences=5 conflicts=3" as *u8)
208 pass = pass + og_row("conflict-verdict" as *u8, r)
209
210 // row 6: a day with no overlap (single event) -> no false positive
211 let rc6: i64 = og_runv(ob, og_args("conflicts" as *u8, pfx, "2026-06-23" as *u8, "2026-06-23" as *u8, 0 as *u8), "/tmp/vzc_c6.txt" as *u8)
212 r = 0
213 if rc6 == 0 {
214 if og_has("/tmp/vzc_c6.txt" as *u8, "VIZSLA-CAL-CONFLICT-VERDICT occurrences=1 conflicts=0" as *u8) == 1 { r = 1 }
215 }
216 pass = pass + og_row("no-conflict-other-day" as *u8, r)
217
218 // row 7: determinism
219 og_runv(ob, og_args("conflicts" as *u8, pfx, "2026-06-22" as *u8, "2026-06-22" as *u8, 0 as *u8), "/tmp/vzc_c7.txt" as *u8)
220 r = og_fileeq("/tmp/vzc_c2.txt" as *u8, "/tmp/vzc_c7.txt" as *u8)
221 pass = pass + og_row("conflict-determinism" as *u8, r)
222
223 // row 8: loud-fail on bad date (negative control)
224 let rc8: i64 = og_runv(ob, og_args("conflicts" as *u8, pfx, "2026-13-99" as *u8, "2026-06-22" as *u8, 0 as *u8), "/tmp/vzc_c8.txt" as *u8)
225 r = 0
226 if rc8 == 1 { r = 1 }
227 pass = pass + og_row("loud-fail-bad-date" as *u8, r)
228
229 let permil: i64 = (pass * 1000) / 8
230 let logfd: i64 = sys_openat_append("knowledge/status/vizsla_gate.log" as *u8, 0x1a4)
231 var fdi: i64 = 0
232 while fdi < 2 {
233 var fd: i64 = 1
234 if fdi == 1 { fd = logfd }
235 if fd > 0 {
236 let line: *u8 = sys_mmap(256)
237 var o: i64 = 0
238 o = og_cat(line, o, "VIZSLA-CAL-CONFLICT-GATE epoch=" as *u8)
239 o = og_catn(line, o, sys_now_realtime_sec())
240 o = og_cat(line, o, " rows=8 pass=" as *u8)
241 o = og_catn(line, o, pass)
242 o = og_cat(line, o, " permil=" as *u8)
243 o = og_catn(line, o, permil)
244 if pass == 8 {
245 o = og_cat(line, o, " verdict=GREEN\n" as *u8)
246 } else {
247 o = og_cat(line, o, " verdict=RED\n" as *u8)
248 }
249 sys_write(fd, line, o)
250 }
251 fdi = fdi + 1
252 }
253 if logfd > 0 { sys_close(logfd) }
254 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
255 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
256 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
257 let ctr__dry: *i64 = gv_ctr()
258 ctr__dry[0] = pass
259 ctr__dry[1] = 8
260 let rc__dry: i64 = gv_verdict("VIZSLA-CALENDAR-CONFLICT-GATE" as *u8, ctr__dry, "teeth unchanged; verdict emission migrated onto the shared base class" as *u8)
261 sys_exit(rc__dry)
262 return rc__dry
263}