nx_compare_ladder_capture.nx source
↩ module page · 92 lines · 4960 B
1// Test-only staged matrix capture. No source edits or index publication.
2// WARNING: matrix HTML rendering can refresh public reference assets via rp_publish.
3// Do not run against the real tree without accepting that existing emitter side effect.
4// args: <unique-output-stem> <capture-bytes> <timeout-ms> <html|json>; all limits are caller supplied.
5import "nx_tool_run.nx"
6import "nx_itoa_lib.nx"
7const CRM_I64_MAX: i64 = 9223372036854775807
8const CRM_PATH_CAP: i64 = 512
9func crm_out(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return sys_write(1, s, n) }
10func crm_equal(a: *u8, b: *u8) -> i64 { var i: i64 = 0; while a[i] != (0 as u8) { if a[i] != b[i] { return 0 }; i = i + 1 }; if b[i] != (0 as u8) { return 0 }; return 1 }
11func crm_decimal(s: *u8) -> i64 {
12 var n: i64 = 0; var i: i64 = 0
13 if s[0] == (0 as u8) { return 0 }
14 while s[i] != (0 as u8) {
15 let c: i64 = s[i] as i64
16 if c < 48 { return 0 }; if c > 57 { return 0 }
17 let d: i64 = c - 48
18 if n > (CRM_I64_MAX - d) / 10 { return 0 }
19 n = n * 10 + d; i = i + 1
20 }
21 return n
22}
23func crm_append(p: *u8, at: i64, s: *u8) -> i64 {
24 var i: i64 = 0; var o: i64 = at
25 while s[i] != (0 as u8) { p[o] = s[i]; o = o + 1; i = i + 1 }
26 p[o] = 0 as u8
27 return o
28}
29func main(argc: i64, argv: *i64) -> i64 {
30 if argc != 5 { crm_out("usage: nx_compare_staged_capture <unique-output-stem> <capture-bytes> <timeout-ms> <html|json>\n" as *u8); return 2 }
31 let capacity: i64 = crm_decimal(argv[2] as *u8)
32 let deadline: i64 = crm_decimal(argv[3] as *u8)
33 if capacity <= 0 { crm_out("REFUSED invalid capture bytes\n" as *u8); return 2 }
34 if deadline <= 0 { crm_out("REFUSED invalid timeout\n" as *u8); return 2 }
35 let path: *u8 = sys_mmap(CRM_PATH_CAP)
36 if (path as i64) <= 0 { return 3 }
37 let prefix_len: i64 = crm_append(path, 0, "knowledge/status/" as *u8)
38 let name: *u8 = argv[1] as *u8
39 var ni: i64 = 0
40 while name[ni] != (0 as u8) {
41 if prefix_len + ni + 6 >= CRM_PATH_CAP { crm_out("REFUSED output stem too long\n" as *u8); return 2 }
42 let c: i64 = name[ni] as i64
43 var ok: i64 = 0
44 if c >= 97 { if c <= 122 { ok = 1 } }
45 if c >= 48 { if c <= 57 { ok = 1 } }
46 if c == 95 { ok = 1 }; if c == 45 { ok = 1 }
47 if ok == 0 { crm_out("REFUSED invalid output stem\n" as *u8); return 2 }
48 ni = ni + 1
49 }
50 if ni == 0 { return 2 }
51 let no: i64 = crm_append(path, prefix_len, name)
52 let mode: *u8 = argv[4] as *u8
53 if crm_equal(mode, "html" as *u8) == 0 { if crm_equal(mode, "json" as *u8) == 0 { return 2 } }
54 if crm_equal(mode, "html" as *u8) == 1 { crm_append(path, no, ".html" as *u8) } else { crm_append(path, no, ".json" as *u8) }
55 if sys_chdir("buildroot" as *u8) != 0 { crm_out("REFUSED cannot chdir buildroot\n" as *u8); return 3 }
56 let prior: i64 = sys_openat_rd(path)
57 if prior >= 0 { sys_close(prior); crm_out("REFUSED output already exists; choose a new test stem\n" as *u8); return 3 }
58 let output: *u8 = sys_mmap(capacity)
59 if (output as i64) <= 0 { crm_out("REFUSED capture allocation failed\n" as *u8); return 3 }
60 let av: *i64 = sys_mmap(32) as *i64
61 av[0] = "_build/nx_swcompare_matrix_ladder_test_20260907.sov.elf" as i64
62 av[1] = "deploy" as i64; av[2] = mode as i64; av[3] = 0
63 let olen: *i64 = sys_mmap(16) as *i64
64 let cut: *i64 = sys_mmap(16) as *i64
65 let rc: i64 = tr_run_capture_tr(av[0] as *u8, av, output, capacity, olen, deadline, cut)
66 crm_out("CAPTURE child_rc=" as *u8); nxi_out(rc); crm_out(" bytes=" as *u8); nxi_out(olen[0]); crm_out(" truncated=" as *u8); nxi_out(cut[0]); crm_out("\n" as *u8)
67 if rc != 0 { return 4 }
68 if cut[0] != TR_FIT { return 4 }
69 if olen[0] <= 0 { return 4 }
70 if crm_equal(mode, "html" as *u8) == 1 {
71 if tr_contains(output, olen[0], "<html" as *u8) != 1 { return 4 }
72 if tr_contains(output, olen[0], "</html>" as *u8) != 1 { return 4 }
73 } else { if output[0] != (123 as u8) { return 4 } }
74 let ofd: i64 = sys_openat_wr(path, 384)
75 if ofd < 0 { crm_out("REFUSED cannot create test output\n" as *u8); return 5 }
76 var done: i64 = 0
77 while done < olen[0] {
78 let wrote: i64 = sys_write(ofd, (output as i64 + done) as *u8, olen[0] - done)
79 if wrote <= 0 { sys_close(ofd); crm_out("FAILED short write; partial test artifact retained\n" as *u8); return 5 }
80 done = done + wrote
81 }
82 if sys_close(ofd) != 0 { return 5 }
83 let vlen: *i64 = sys_mmap(16) as *i64
84 let verify: *u8 = sys_read_file(path, vlen)
85 if (verify as i64) == 0 { return 6 }
86 if vlen[0] != olen[0] { return 6 }
87 var vi: i64 = 0
88 while vi < olen[0] { if verify[vi] != output[vi] { return 6 }; vi = vi + 1 }
89 crm_out("CAPTURE-VERIFIED path=buildroot/" as *u8); crm_out(path); crm_out(" bytes=" as *u8); nxi_out(olen[0]); crm_out(" index-publish-invoked=0\n" as *u8)
90 return 0
91}
92