nx_crashresume_census_gate.nx source
↩ module page · 130 lines · 5702 B
1// nx_crashresume_census_gate.nx -- liar-killed GATE for the crash-resume census core (CR-R0).
2// Proves the cells DISCRIMINATE and never fabricate: fresh evidence -> GREEN, stale -> RED, missing
3// -> UNKNOWN (not GREEN, not RED-from-nothing), garbage tolerated, thresholds are PARAMETERS
4// (neg-control: same stale fixture flips GREEN under a huge window -- no hardcoded verdicts), mtime
5// cells driven by forged deterministic mtimes (sys_utimensat), dir walk honors the extension filter.
6// Fixtures live under /tmp (ext4, wiped-safe: everything recreated per run). Exit 0 only on 9/9.
7// license_tier: ORIGINAL expect_exit: 0
8import "nx_syscalls.nx"
9import "nx_crashresume_census_core.nx"
10
11func g_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
12
13func g_putn(v: i64) -> i64 {
14 let b: *u8 = sys_mmap(32)
15 let e: i64 = ccz_cat_num(b, 0, v)
16 sys_write(1, b, e)
17 return 0
18}
19
20func g_write(path: *u8, s: *u8) -> i64 {
21 let fd: i64 = sys_openat_wr(path, 420)
22 if fd < 0 { return 0 - 1 }
23 sys_write(fd, s, ccz_slen(s))
24 sys_close(fd)
25 return 0
26}
27
28// forge atime+mtime = e (deterministic staleness)
29func g_touch(path: *u8, e: i64) -> i64 {
30 let t: *i64 = sys_mmap(32) as *i64
31 t[0] = e
32 t[1] = 0
33 t[2] = e
34 t[3] = 0
35 return sys_utimensat(path, t)
36}
37
38func g_check(name: *u8, got: i64, want: i64, passp: *i64) -> i64 {
39 g_puts("T " as *u8)
40 g_puts(name)
41 g_puts(" got=" as *u8)
42 g_putn(got)
43 g_puts(" want=" as *u8)
44 g_putn(want)
45 if got == want { g_puts(" PASS\n" as *u8); passp[0] = passp[0] + 1 } else { g_puts(" FAIL\n" as *u8) }
46 return 0
47}
48
49func main(argc: i64, argv: *i64) -> i64 {
50 let pass: *i64 = sys_mmap(16) as *i64
51 pass[0] = 0
52 let now: i64 = sys_now_realtime_sec()
53 sys_mkdir("/tmp/crz_gate_fix" as *u8, 0x1ed)
54 sys_mkdir("/tmp/crz_gate_fix/dw" as *u8, 0x1ed)
55 let buf: *u8 = sys_mmap(65536)
56 let lb: *u8 = sys_mmap(1024)
57
58 // T1 fresh epoch -> GREEN
59 var o: i64 = 0
60 o = ccz_cat_str(lb, o, "HBX ws=T epoch=" as *u8)
61 o = ccz_cat_num(lb, o, now - 10)
62 o = ccz_cat_str(lb, o, " seq=1 actor=1 END\n" as *u8)
63 g_write("/tmp/crz_gate_fix/hb_fresh.log" as *u8, lb)
64 var n: i64 = ccz_read("/tmp/crz_gate_fix/hb_fresh.log" as *u8, buf, 65535)
65 var ts: i64 = ccz_max_epoch(buf, n)
66 g_check("fresh-epoch-GREEN" as *u8, ccz_stale_verdict(ts, now, 86400), CR_GREEN, pass)
67
68 // T2 stale epoch -> RED
69 o = 0
70 o = ccz_cat_str(lb, o, "HBX ws=T epoch=" as *u8)
71 o = ccz_cat_num(lb, o, now - 200000)
72 o = ccz_cat_str(lb, o, " seq=1 actor=1 END\n" as *u8)
73 g_write("/tmp/crz_gate_fix/hb_stale.log" as *u8, lb)
74 n = ccz_read("/tmp/crz_gate_fix/hb_stale.log" as *u8, buf, 65535)
75 let ts_stale: i64 = ccz_max_epoch(buf, n)
76 g_check("stale-epoch-RED" as *u8, ccz_stale_verdict(ts_stale, now, 86400), CR_RED, pass)
77
78 // T3 missing log -> UNKNOWN (never fabricated)
79 n = ccz_read("/tmp/crz_gate_fix/absent.log" as *u8, buf, 65535)
80 var ts3: i64 = 0
81 if n > 0 { ts3 = ccz_max_epoch(buf, n) }
82 g_check("missing-log-UNKNOWN" as *u8, ccz_stale_verdict(ts3, now, 86400), CR_UNKNOWN, pass)
83
84 // T4 garbage tolerated, valid old epoch still parsed -> RED (not crash, not GREEN)
85 o = 0
86 o = ccz_cat_str(lb, o, "epoch= epoch=zz junk\nHBX ws=T epoch=" as *u8)
87 o = ccz_cat_num(lb, o, now - 200000)
88 o = ccz_cat_str(lb, o, " END\ntrailing epoch=\n" as *u8)
89 g_write("/tmp/crz_gate_fix/hb_garbage.log" as *u8, lb)
90 n = ccz_read("/tmp/crz_gate_fix/hb_garbage.log" as *u8, buf, 65535)
91 let ts4: i64 = ccz_max_epoch(buf, n)
92 g_check("garbage-tolerated-RED" as *u8, ccz_stale_verdict(ts4, now, 86400), CR_RED, pass)
93
94 // T5 NEG-CONTROL: the SAME stale fixture flips GREEN under a huge window (threshold is a
95 // parameter, not a hardcoded verdict -- the conf lever is real)
96 g_check("negctl-window-param-GREEN" as *u8, ccz_stale_verdict(ts_stale, now, 10000000), CR_GREEN, pass)
97
98 // T6 mtime stale -> RED (forged mtime)
99 g_write("/tmp/crz_gate_fix/reg.txt" as *u8, "seg-1\n" as *u8)
100 g_touch("/tmp/crz_gate_fix/reg.txt" as *u8, now - 500000)
101 g_check("mtime-stale-RED" as *u8, ccz_stale_verdict(ccz_mtime("/tmp/crz_gate_fix/reg.txt" as *u8), now, 86400), CR_RED, pass)
102
103 // T7 mtime fresh -> GREEN
104 g_touch("/tmp/crz_gate_fix/reg.txt" as *u8, now - 10)
105 g_check("mtime-fresh-GREEN" as *u8, ccz_stale_verdict(ccz_mtime("/tmp/crz_gate_fix/reg.txt" as *u8), now, 86400), CR_GREEN, pass)
106
107 // T8 lag verdicts: capture behind -> RED; capture keeping up -> GREEN; no activity -> UNKNOWN
108 var ok8: i64 = 1
109 if ccz_lag_verdict(now - 10, now - 100000, 3600) != CR_RED { ok8 = 0 }
110 if ccz_lag_verdict(now - 10, now - 20, 3600) != CR_GREEN { ok8 = 0 }
111 if ccz_lag_verdict(0 - 1, now - 20, 3600) != CR_UNKNOWN { ok8 = 0 }
112 if ccz_lag_verdict(now - 10, 0 - 1, 3600) != CR_RED { ok8 = 0 }
113 g_check("lag-verdicts" as *u8, ok8, 1, pass)
114
115 // T9 dir walk honors the ext filter: a.jsonl older than b.txt; newest .jsonl must be a's mtime
116 g_write("/tmp/crz_gate_fix/dw/a.jsonl" as *u8, "x\n" as *u8)
117 g_write("/tmp/crz_gate_fix/dw/b.txt" as *u8, "y\n" as *u8)
118 g_touch("/tmp/crz_gate_fix/dw/a.jsonl" as *u8, now - 50)
119 g_touch("/tmp/crz_gate_fix/dw/b.txt" as *u8, now - 5)
120 var ok9: i64 = 0
121 if ccz_newest_mtime_ext("/tmp/crz_gate_fix/dw" as *u8, ".jsonl" as *u8) == now - 50 { ok9 = 1 }
122 g_check("dirwalk-ext-filter" as *u8, ok9, 1, pass)
123
124 g_puts("CRZ-GATE pass=" as *u8)
125 g_putn(pass[0])
126 g_puts("/9 verdict=" as *u8)
127 if pass[0] == 9 { g_puts("GREEN\n" as *u8); return 0 }
128 g_puts("RED\n" as *u8)
129 return 1
130}