code wiki / _hdl_build / nx_work_retro_gate.nx
nx_work_retro_gate.nx source
↩ module page · 108 lines · 6493 B
1import "nx_gate_gn.nx"
2// nx_work_retro_gate.nx -- hermetic gate for the PAST sweep. Proves the classifier and the end-to-end
3// sweep flag EXACTLY the uncaptured-recent class and nothing else:
4// T1 referenced name -> ACCOUNTED.
5// T2 (LOAD-BEARING) unreferenced + recent -> FLAGGED (uncaptured work is never silently dropped).
6// T3 unreferenced + OLD -> STALE (settled helpers don't drown the signal).
7// T4 referenced + OLD -> ACCOUNTED (being recorded beats age).
8// T5 end-to-end sweep over a temp dir: flags the lost organ, NOT the recorded one (count==1).
9// T6 (NEG) the recorded organ name never appears in the flagged output.
10// Sovereign: imports nx_syscalls + the retro lib only. license_tier: ORIGINAL
11import "nx_syscalls.nx"
12import "nx_work_retro_lib.nx"
13import "nx_gate_verdict.nx"
14
15const G_ROOT: *u8 = "/tmp/nx_wretro_gate"
16const G_MEM: *u8 = "/tmp/nx_wretro_gate/mem"
17const G_SWEEP: *u8 = "/tmp/nx_wretro_gate/sweep"
18const G_WLOG: *u8 = "/tmp/nx_wretro_gate/worklog.tsv"
19
20func gp(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
21func gstrlen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
22func gwrite(path: *u8, content: *u8) -> i64 {
23 let fd: i64 = sys_openat_wr(path, 420)
24 if fd < 0 { return 0 - 1 }
25 sys_write(fd, content, gstrlen(content))
26 sys_close(fd)
27 return 0
28}
29func gfind(buf: *u8, n: i64, pat: *u8) -> i64 {
30 var pl: i64 = 0; while pat[pl] != (0 as u8) { pl = pl + 1 }
31 if pl == 0 { return 0 - 1 }
32 var i: i64 = 0
33 while i + pl <= n {
34 var j: i64 = 0; var ok: i64 = 1
35 while j < pl { if buf[i + j] != pat[j] { ok = 0; j = pl } else { j = j + 1 } }
36 if ok == 1 { return i }
37 i = i + 1
38 }
39 return 0 - 1
40}
41
42func main(argc: i64, argv: *i64) -> i64 {
43 gp("=== nx_work_retro_gate (PAST sweep: flag uncaptured work) ===\n" as *u8)
44
45 var pass: i64 = 0
46 var fail: i64 = 0
47
48 // ---- classifier unit tests (pure, no file I/O) ----
49 let corpus: *u8 = "the arc references nx_known_organ and the worklog mentions nx_logged_organ here" as *u8
50 let clen: i64 = gstrlen(corpus)
51 let NOW: i64 = 1800000000
52 let WIN: i64 = 604800 // 7 days
53
54 if wr_classify("nx_known_organ" as *u8, NOW - 10, corpus, clen, NOW, WIN) == WR_ACCOUNTED { pass = pass + 1; gp(" T1 referenced->ACCOUNTED PASS\n" as *u8) } else { fail = fail + 1; gp(" T1 FAIL\n" as *u8) }
55
56 if wr_classify("nx_ghost_recent" as *u8, NOW - 10, corpus, clen, NOW, WIN) == WR_FLAGGED { pass = pass + 1; gp(" T2 unrecorded+recent->FLAGGED PASS\n" as *u8) } else { fail = fail + 1; gp(" T2 FAIL (uncaptured work would be lost)\n" as *u8) }
57
58 if wr_classify("nx_ghost_old" as *u8, NOW - 9999999, corpus, clen, NOW, WIN) == WR_STALE { pass = pass + 1; gp(" T3 unrecorded+old->STALE PASS\n" as *u8) } else { fail = fail + 1; gp(" T3 FAIL\n" as *u8) }
59
60 if wr_classify("nx_known_organ" as *u8, NOW - 9999999, corpus, clen, NOW, WIN) == WR_ACCOUNTED { pass = pass + 1; gp(" T4 referenced+old->ACCOUNTED PASS\n" as *u8) } else { fail = fail + 1; gp(" T4 FAIL\n" as *u8) }
61
62 // ---- end-to-end sweep over a temp dir ----
63 sys_mkdir(G_ROOT, 511)
64 sys_mkdir(G_MEM, 511)
65 sys_mkdir(G_SWEEP, 511)
66 // a memory doc that RECORDS nx_acct_organ
67 gwrite("/tmp/nx_wretro_gate/mem/project-fixture-2026-06-19.md" as *u8, "this arc built nx_acct_organ and gated it\n" as *u8)
68 gwrite(G_WLOG, "1800000000\tingest\tWrite\tnx_acct_organ.nx\n" as *u8)
69 // sweep dir: RECORDED primary + LOST primary + LOST helper (_gate) -- all freshly created => recent
70 gwrite("/tmp/nx_wretro_gate/sweep/nx_acct_organ.nx" as *u8, "// recorded\n" as *u8)
71 gwrite("/tmp/nx_wretro_gate/sweep/nx_lost_organ.nx" as *u8, "// nowhere in the record\n" as *u8)
72 gwrite("/tmp/nx_wretro_gate/sweep/nx_lost_gate.nx" as *u8, "// unrecorded but a helper (gate)\n" as *u8)
73
74 let cbuf: *u8 = sys_mmap(WR_CORPUSCAP)
75 let cl2: i64 = wr_load_corpus(G_MEM, G_WLOG, cbuf, WR_CORPUSCAP)
76 let flagged: *u8 = sys_mmap(65536)
77 let stats: *i64 = sys_mmap(24) as *i64
78 // huge window so the freshly-created files are unambiguously "recent"
79 let nprim: i64 = wr_sweep(G_SWEEP, cbuf, cl2, NOW, 1000000000000, flagged, 65536, stats)
80 let fl: i64 = gstrlen(flagged)
81
82 // T5: PRIMARY shortlist = exactly the lost primary (helper + recorded excluded).
83 if nprim == 1 { if gfind(flagged, fl, "nx_lost_organ" as *u8) >= 0 { pass = pass + 1; gp(" T5 primary-shortlist==[lost_organ] PASS\n" as *u8) } else { fail = fail + 1; gp(" T5 lost-not-in-shortlist FAIL\n" as *u8) } } else { fail = fail + 1; gp(" T5 primary-count FAIL got=" as *u8); gn(nprim); gp("\n" as *u8) }
84
85 // T6: recorded organ never flagged.
86 if gfind(flagged, fl, "nx_acct_organ" as *u8) < 0 { pass = pass + 1; gp(" T6 recorded-never-flagged PASS\n" as *u8) } else { fail = fail + 1; gp(" T6 recorded-LEAKED FAIL\n" as *u8) }
87
88 // T7: RAW flag still counts the helper (nothing hidden) -> raw==2, but it is NOT in the shortlist.
89 if stats[1] == 2 { if gfind(flagged, fl, "nx_lost_gate" as *u8) < 0 { pass = pass + 1; gp(" T7 helper-raw-counted-not-shortlisted PASS\n" as *u8) } else { fail = fail + 1; gp(" T7 helper-leaked-to-shortlist FAIL\n" as *u8) } } else { fail = fail + 1; gp(" T7 raw-count FAIL got=" as *u8); gn(stats[1]); gp("\n" as *u8) }
90
91 // T8: wr_is_helper unit -- suffixes + leading underscore classify, plain names do not.
92 var t8: i64 = 1
93 if wr_is_helper("nx_x_gate" as *u8) != 1 { t8 = 0 }
94 if wr_is_helper("_probe_thing" as *u8) != 1 { t8 = 0 }
95 if wr_is_helper("nx_real_organ" as *u8) != 0 { t8 = 0 }
96 if t8 == 1 { pass = pass + 1; gp(" T8 wr_is_helper-classifies PASS\n" as *u8) } else { fail = fail + 1; gp(" T8 wr_is_helper FAIL\n" as *u8) }
97
98 gp("RESULT pass=" as *u8); gn(pass); gp(" fail=" as *u8); gn(fail)
99 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
100 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
101 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
102 let ctr__dry: *i64 = gv_ctr()
103 ctr__dry[0] = pass
104 ctr__dry[1] = pass + fail
105 let rc__dry: i64 = gv_verdict("WORK-RETRO-GATE" as *u8, ctr__dry, "teeth unchanged; verdict emission migrated onto the shared base class" as *u8)
106 sys_exit(rc__dry)
107 return rc__dry
108}