nx_forkgrade_gate.nx source
↩ module page · 188 lines · 11848 B
1// nx_forkgrade_gate.nx -- CE8 (codeeffectiveness fg_trend): does the fork census leave a TRAJECTORY, and only where it should?
2//
3// END-TO-END: forks the promoted nx_forkgrade (or the <subject_elf> given as argv[1], absolutised against the CWD) over a
4// THREE-FILE fixture pair it plants under /tmp -- identical, A-superset, bidirectional -- so the census has known answers,
5// then asserts the ONE new capability: every successful census appends one row to a spine, announced (spine= fd= wrote=
6// of=), and two censuses in time leave two rows. The spine path is DERIVED from dirA, so the fixture writes a SIDECAR
7// beside its own root and the PRODUCTION spine (knowledge/status/forkgrade.spine) is asserted UNCHANGED by size: a trial
8// run can never forge the production trajectory. Every expected count below was decided from the fixture before the run.
9// RED against the pre-fg_trend binary by construction (no spine line, no sidecar): the RED-before-GREEN this gate owes.
10// The subject forks nx_srcdiff.elf by BARE NAME from its CWD, so the run is started in the directory where that ruler
11// resolves (CWD or its parent) -- derived from data, never a pinned absolute path; absent ruler = SKIP, never RED.
12// exit: 0 GREEN . 1 RED . 3 SKIP license_tier: ORIGINAL No hw writes.
13import "nx_syscalls.nx"
14import "nx_gate_verdict.nx"
15import "nx_tool_run.nx"
16
17const FGG_PATH: i64 = 1024
18const FGG_CAP: i64 = 1048576
19const FGG_TMO: i64 = 60000
20const FGG_ARGV: i64 = 8
21const FGG_WORD: i64 = 8
22const FGG_MODE_RWX: i64 = 493
23const FGG_MODE_RW: i64 = 420
24const FGG_ROOT: *u8 = "/tmp/nx_forkgrade_gate"
25const FGG_DIRA: *u8 = "/tmp/nx_forkgrade_gate/A"
26const FGG_DIRB: *u8 = "/tmp/nx_forkgrade_gate/B"
27const FGG_LIST: *u8 = "/tmp/nx_forkgrade_gate/list.txt"
28const FGG_SIDECAR: *u8 = "/tmp/nx_forkgrade_gate/A.forkgrade.spine"
29const FGG_PROD_SPINE: *u8 = "knowledge/status/forkgrade.spine"
30const FGG_RULER: *u8 = "/nx_srcdiff.elf"
31const FGG_NFILES: i64 = 3
32const FGG_EXPECT_ADOPTABLE_PERMIL: i64 = 666 // (identical 1 + a_superset 1) * 1000 / 3 pairs, hand-computed from the fixture
33const FGG_ZERO: i64 = 48
34const FGG_NINE: i64 = 57
35const FGG_NL: i64 = 10
36const FGG_SLASH: i64 = 47
37
38func fgg_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
39func fgg_cat(dst: *u8, off: i64, s: *u8) -> i64 { var o: i64 = off; var i: i64 = 0; while s[i] != (0 as u8) { dst[o] = s[i]; o = o + 1; i = i + 1 } dst[o] = 0 as u8; return o }
40func fgg_find(buf: *u8, n: i64, needle: *u8) -> i64 {
41 let m: i64 = fgg_slen(needle)
42 if m == 0 { return 0 - 1 }
43 var i: i64 = 0
44 while i + m <= n { var j: i64 = 0; var ok: i64 = 1; while j < m { if buf[i + j] != needle[j] { ok = 0; j = m } else { j = j + 1 } } if ok == 1 { return i } i = i + 1 }
45 return 0 - 1
46}
47func fgg_has(buf: *u8, n: i64, needle: *u8) -> i64 { if fgg_find(buf, n, needle) >= 0 { return 1 } return 0 }
48func fgg_num_after(buf: *u8, n: i64, key: *u8) -> i64 {
49 let at: i64 = fgg_find(buf, n, key)
50 if at < 0 { return 0 - 1 }
51 var i: i64 = at + fgg_slen(key)
52 var v: i64 = 0
53 var any: i64 = 0
54 while i < n { let c: i64 = buf[i] as i64; if c < FGG_ZERO { break } if c > FGG_NINE { break } v = v * 10 + (c - FGG_ZERO); any = 1; i = i + 1 }
55 if any == 0 { return 0 - 1 }
56 return v
57}
58func fgg_write(path: *u8, text: *u8) -> i64 {
59 sys_unlinkat(path)
60 let fd: i64 = sys_openat_wr(path, FGG_MODE_RW)
61 if fd < 0 { return 0 - 1 }
62 let n: i64 = fgg_slen(text)
63 var off: i64 = 0
64 while off < n { let r: i64 = sys_write(fd, (text as i64 + off) as *u8, n - off); if r <= 0 { sys_close(fd); return 0 - 2 } off = off + r }
65 sys_close(fd)
66 return 0
67}
68func fgg_exists(path: *u8) -> i64 { let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 } sys_close(fd); return 1 }
69// file size by lseek END, -1 when absent -- the artifact witness for "the production spine did not move"
70func fgg_size(path: *u8) -> i64 { let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 - 1 } let s: i64 = sys_lseek(fd, 0, 2); sys_close(fd); return s }
71// newline count in a file, -1 when absent. The buffer is not released: the sidecar is a few hundred bytes read twice
72// and the process exits at the verdict -- bounded and named.
73func fgg_lines(path: *u8) -> i64 {
74 let ln: *i64 = sys_mmap(16) as *i64
75 let b: *u8 = sys_read_file(path, ln)
76 if (b as i64) == 0 { return 0 - 1 }
77 var c: i64 = 0
78 var i: i64 = 0
79 while i < ln[0] { if b[i] == (FGG_NL as u8) { c = c + 1 } i = i + 1 }
80 return c
81}
82func fgg_subject_from_argv(a1: *u8, out: *u8) -> i64 {
83 var o: i64 = 0
84 if a1[0] != (FGG_SLASH as u8) { let cwd: *u8 = sys_mmap(FGG_PATH); if sys_getcwd(cwd, FGG_PATH) <= 0 { out[0] = 0 as u8; return 0 } o = fgg_cat(out, 0, cwd); o = fgg_cat(out, o, "/" as *u8) }
85 o = fgg_cat(out, o, a1)
86 return fgg_exists(out)
87}
88func fgg_resolve(out: *u8) -> i64 {
89 let cwd: *u8 = sys_mmap(FGG_PATH)
90 if sys_getcwd(cwd, FGG_PATH) <= 0 { return 0 }
91 var k: i64 = 0
92 while k < 3 {
93 var o: i64 = fgg_cat(out, 0, cwd)
94 if k == 0 { o = fgg_cat(out, o, "/nx_forkgrade.elf" as *u8) }
95 if k == 1 { o = fgg_cat(out, o, "/../nx_forkgrade.elf" as *u8) }
96 if k == 2 { o = fgg_cat(out, o, "/buildroot/_build/nx_forkgrade.sov.elf" as *u8) }
97 if fgg_exists(out) == 1 { return 1 }
98 k = k + 1
99 }
100 out[0] = 0 as u8
101 return 0
102}
103// the directory the subject's ruler resolves in (CWD, then its parent) -- the run must start there
104func fgg_ruler_root(out: *u8) -> i64 {
105 let cwd: *u8 = sys_mmap(FGG_PATH)
106 if sys_getcwd(cwd, FGG_PATH) <= 0 { return 0 }
107 let probe: *u8 = sys_mmap(FGG_PATH)
108 var k: i64 = 0
109 while k < 2 {
110 var o: i64 = fgg_cat(out, 0, cwd)
111 if k == 1 { o = fgg_cat(out, o, "/.." as *u8) }
112 var p: i64 = fgg_cat(probe, 0, out)
113 p = fgg_cat(probe, p, FGG_RULER)
114 if fgg_exists(probe) == 1 { return 1 }
115 k = k + 1
116 }
117 out[0] = 0 as u8
118 return 0
119}
120func fgg_run(gen: *u8, root: *u8, out: *u8, outlen: *i64) -> i64 {
121 let av: *i64 = sys_mmap(FGG_ARGV * FGG_WORD) as *i64
122 av[0] = gen as i64; av[1] = FGG_LIST as i64; av[2] = FGG_DIRA as i64; av[3] = FGG_DIRB as i64; av[4] = 0
123 return tr_run_capture_cwd(gen, av, out, FGG_CAP, outlen, FGG_TMO, root)
124}
125
126func main(argc: i64, argv: *i64) -> i64 {
127 let ctr: *i64 = gv_ctr()
128 gv_head("NX-FORKGRADE-GATE: the fork census leaves a trajectory, in a sidecar beside its own root, never the production spine" as *u8)
129 let gen: *u8 = sys_mmap(FGG_PATH)
130 var have: i64 = 0
131 var src: *u8 = "resolved" as *u8
132 if argc >= 2 { have = fgg_subject_from_argv(argv[1] as *u8, gen); src = "argv-override" as *u8 } else { have = fgg_resolve(gen) }
133 gv_puts(" subject=" as *u8); if have == 1 { gv_puts(gen) } else { gv_puts("ABSENT" as *u8) } gv_puts(" (" as *u8); gv_puts(src); gv_puts(")\n" as *u8)
134 if gv_need("nx_forkgrade binary (serving root, its parent, _build, or argv subject)" as *u8, have, ctr) == 0 { return gv_verdict("nx_forkgrade_gate" as *u8, ctr, "no subject to drive" as *u8) }
135 let root: *u8 = sys_mmap(FGG_PATH)
136 let have_root: i64 = fgg_ruler_root(root)
137 gv_puts(" ruler_root=" as *u8); if have_root == 1 { gv_puts(root) } else { gv_puts("ABSENT" as *u8) } gv_puts("\n" as *u8)
138 if gv_need("nx_srcdiff.elf in the CWD or its parent (the subject forks it by bare name)" as *u8, have_root, ctr) == 0 { return gv_verdict("nx_forkgrade_gate" as *u8, ctr, "the subject's ruler does not resolve from here" as *u8) }
139 // ---- fixture: three pairs with known answers; the sidecar is unlinked first so the gate is idempotent ----
140 sys_mkdir(FGG_ROOT, FGG_MODE_RWX); sys_mkdir(FGG_DIRA, FGG_MODE_RWX); sys_mkdir(FGG_DIRB, FGG_MODE_RWX)
141 fgg_write("/tmp/nx_forkgrade_gate/A/f_same.nx" as *u8, "func t() -> i64 { return 0 }\nfunc u() -> i64 { return 1 }\n" as *u8)
142 fgg_write("/tmp/nx_forkgrade_gate/B/f_same.nx" as *u8, "func t() -> i64 { return 0 }\nfunc u() -> i64 { return 1 }\n" as *u8)
143 fgg_write("/tmp/nx_forkgrade_gate/A/f_asup.nx" as *u8, "func t() -> i64 { return 0 }\nfunc extra() -> i64 { return 2 }\n" as *u8)
144 fgg_write("/tmp/nx_forkgrade_gate/B/f_asup.nx" as *u8, "func t() -> i64 { return 0 }\n" as *u8)
145 fgg_write("/tmp/nx_forkgrade_gate/A/f_bidi.nx" as *u8, "func t() -> i64 { return 0 }\nfunc onlyA() -> i64 { return 3 }\n" as *u8)
146 fgg_write("/tmp/nx_forkgrade_gate/B/f_bidi.nx" as *u8, "func t() -> i64 { return 0 }\nfunc onlyB() -> i64 { return 4 }\n" as *u8)
147 fgg_write(FGG_LIST, "f_same.nx\nf_asup.nx\nf_bidi.nx\n" as *u8)
148 sys_unlinkat(FGG_SIDECAR)
149 if gv_need("fixture written under /tmp" as *u8, fgg_exists(FGG_LIST), ctr) == 0 { return gv_verdict("nx_forkgrade_gate" as *u8, ctr, "fixture tree unwritable" as *u8) }
150 let prod_before: i64 = fgg_size(FGG_PROD_SPINE)
151 let out: *u8 = sys_mmap(FGG_CAP)
152 let ol: *i64 = sys_mmap(16) as *i64
153 // ---- run 1 ----
154 let rc1: i64 = fgg_run(gen, root, out, ol)
155 let n1: i64 = ol[0]
156 gv_puts(" run1 rc=" as *u8); gv_num(rc1); gv_puts(" bytes=" as *u8); gv_num(n1); gv_puts("\n" as *u8)
157 let tot1: i64 = fgg_num_after(out, n1, "# FORKGRADE total=" as *u8)
158 gv_check_eq("run1-census-graded-all-three-fixture-pairs" as *u8, tot1, FGG_NFILES, ctr)
159 gv_check("run1-census-verdict-GREEN" as *u8, fgg_has(out, n1, "NX-FORKGRADE verdict=GREEN" as *u8), ctr)
160 gv_check("run1-announces-the-spine-it-wrote" as *u8, fgg_has(out, n1, "# FORKGRADE spine=" as *u8), ctr)
161 gv_check("run1-spine-is-the-SIDECAR-beside-dirA-derived-from-the-argument" as *u8, fgg_has(out, n1, "# FORKGRADE spine=/tmp/nx_forkgrade_gate/A.forkgrade.spine" as *u8), ctr)
162 let wrote1: i64 = fgg_num_after(out, n1, " wrote=" as *u8)
163 gv_check("run1-spine-write-announced-positive-bytes" as *u8, (wrote1 > 0) as i64, ctr)
164 gv_check_eq("run1-sidecar-holds-exactly-one-row" as *u8, fgg_lines(FGG_SIDECAR), 1, ctr)
165 // the spine row and the census line must carry the SAME total: one figure, two readers
166 let sb: *i64 = sys_mmap(16) as *i64
167 let sbuf: *u8 = sys_read_file(FGG_SIDECAR, sb)
168 var sp_total: i64 = 0 - 1
169 var sp_permil: i64 = 0 - 1
170 if (sbuf as i64) != 0 { sp_total = fgg_num_after(sbuf, sb[0], "|total=" as *u8); sp_permil = fgg_num_after(sbuf, sb[0], "|adoptable_permil=" as *u8) }
171 gv_check_eq("spine-row-total-equals-the-census-total" as *u8, sp_total, tot1, ctr)
172 gv_check_eq("spine-row-adoptable-permil-hand-computed-2-of-3" as *u8, sp_permil, FGG_EXPECT_ADOPTABLE_PERMIL, ctr)
173 // ---- run 2: a second census in time leaves a second row -- the trend ----
174 let rc2: i64 = fgg_run(gen, root, out, ol)
175 gv_check("run2-exit-0" as *u8, (rc2 == 0) as i64, ctr)
176 gv_check_eq("two-censuses-in-time-leave-two-rows-a-trajectory-not-a-level" as *u8, fgg_lines(FGG_SIDECAR), 2, ctr)
177 // ---- neg-control: the PRODUCTION spine did not move -- a fixture run can never forge the trajectory ----
178 let prod_after: i64 = fgg_size(FGG_PROD_SPINE)
179 gv_check("neg-control-production-spine-unchanged-by-the-fixture-run-size-before-equals-after" as *u8, (prod_before == prod_after) as i64, ctr)
180 gv_bite("spine-written-beside-the-fixture-and-NOT-into-production" as *u8, (fgg_exists(FGG_SIDECAR) == 1) as i64, (prod_before != prod_after) as i64, ctr)
181 gv_kv("run1_total" as *u8, tot1)
182 gv_kv("spine_total" as *u8, sp_total)
183 gv_kv("spine_adoptable_permil" as *u8, sp_permil)
184 gv_kv("sidecar_rows_after_two_runs" as *u8, fgg_lines(FGG_SIDECAR))
185 gv_kv("prod_spine_size_before" as *u8, prod_before)
186 gv_kv("prod_spine_size_after" as *u8, prod_after)
187 return gv_verdict("nx_forkgrade_gate" as *u8, ctr, "three known pairs graded; one spine row per census in a sidecar derived from dirA; two runs leave two rows; the production spine is asserted unchanged" as *u8)
188}