code wiki / _hdl_build / nx_work_retro_gate.nx
nx_work_retro_gate.nx source
↩ module page · 100 lines · 6033 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"
13
14const G_ROOT: *u8 = "/tmp/nx_wretro_gate"
15const G_MEM: *u8 = "/tmp/nx_wretro_gate/mem"
16const G_SWEEP: *u8 = "/tmp/nx_wretro_gate/sweep"
17const G_WLOG: *u8 = "/tmp/nx_wretro_gate/worklog.tsv"
18
19func gp(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
20func gstrlen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
21func gwrite(path: *u8, content: *u8) -> i64 {
22 let fd: i64 = sys_openat_wr(path, 420)
23 if fd < 0 { return 0 - 1 }
24 sys_write(fd, content, gstrlen(content))
25 sys_close(fd)
26 return 0
27}
28func gfind(buf: *u8, n: i64, pat: *u8) -> i64 {
29 var pl: i64 = 0; while pat[pl] != (0 as u8) { pl = pl + 1 }
30 if pl == 0 { return 0 - 1 }
31 var i: i64 = 0
32 while i + pl <= n {
33 var j: i64 = 0; var ok: i64 = 1
34 while j < pl { if buf[i + j] != pat[j] { ok = 0; j = pl } else { j = j + 1 } }
35 if ok == 1 { return i }
36 i = i + 1
37 }
38 return 0 - 1
39}
40
41func main(argc: i64, argv: *i64) -> i64 {
42 gp("=== nx_work_retro_gate (PAST sweep: flag uncaptured work) ===\n" as *u8)
43
44 var pass: i64 = 0
45 var fail: i64 = 0
46
47 // ---- classifier unit tests (pure, no file I/O) ----
48 let corpus: *u8 = "the arc references nx_known_organ and the worklog mentions nx_logged_organ here" as *u8
49 let clen: i64 = gstrlen(corpus)
50 let NOW: i64 = 1800000000
51 let WIN: i64 = 604800 // 7 days
52
53 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) }
54
55 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) }
56
57 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) }
58
59 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) }
60
61 // ---- end-to-end sweep over a temp dir ----
62 sys_mkdir(G_ROOT, 511)
63 sys_mkdir(G_MEM, 511)
64 sys_mkdir(G_SWEEP, 511)
65 // a memory doc that RECORDS nx_acct_organ
66 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)
67 gwrite(G_WLOG, "1800000000\tingest\tWrite\tnx_acct_organ.nx\n" as *u8)
68 // sweep dir: RECORDED primary + LOST primary + LOST helper (_gate) -- all freshly created => recent
69 gwrite("/tmp/nx_wretro_gate/sweep/nx_acct_organ.nx" as *u8, "// recorded\n" as *u8)
70 gwrite("/tmp/nx_wretro_gate/sweep/nx_lost_organ.nx" as *u8, "// nowhere in the record\n" as *u8)
71 gwrite("/tmp/nx_wretro_gate/sweep/nx_lost_gate.nx" as *u8, "// unrecorded but a helper (gate)\n" as *u8)
72
73 let cbuf: *u8 = sys_mmap(WR_CORPUSCAP)
74 let cl2: i64 = wr_load_corpus(G_MEM, G_WLOG, cbuf, WR_CORPUSCAP)
75 let flagged: *u8 = sys_mmap(65536)
76 let stats: *i64 = sys_mmap(24) as *i64
77 // huge window so the freshly-created files are unambiguously "recent"
78 let nprim: i64 = wr_sweep(G_SWEEP, cbuf, cl2, NOW, 1000000000000, flagged, 65536, stats)
79 let fl: i64 = gstrlen(flagged)
80
81 // T5: PRIMARY shortlist = exactly the lost primary (helper + recorded excluded).
82 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) }
83
84 // T6: recorded organ never flagged.
85 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) }
86
87 // T7: RAW flag still counts the helper (nothing hidden) -> raw==2, but it is NOT in the shortlist.
88 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) }
89
90 // T8: wr_is_helper unit -- suffixes + leading underscore classify, plain names do not.
91 var t8: i64 = 1
92 if wr_is_helper("nx_x_gate" as *u8) != 1 { t8 = 0 }
93 if wr_is_helper("_probe_thing" as *u8) != 1 { t8 = 0 }
94 if wr_is_helper("nx_real_organ" as *u8) != 0 { t8 = 0 }
95 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) }
96
97 gp("RESULT pass=" as *u8); gn(pass); gp(" fail=" as *u8); gn(fail)
98 if fail == 0 { gp(" verdict=GREEN\n" as *u8); sys_exit(0); return 0 }
99 gp(" verdict=RED\n" as *u8); sys_exit(1); return 1
100}