code wiki / (root) / nx_compare_ladder_capture_test_20260907.nx

nx_compare_ladder_capture_test_20260907.nx source

↩ module page · 121 lines · 6898 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> [<target-basename> <domain> <scope-id>]. 5// Four arguments preserve the original fixed target/deploy behavior. Extended calls require a reviewed 6// Compare-owned scope row binding the exact target/domain/mode/SHA; html scope includes reference-asset refresh. 7// No arbitrary path, arbitrary argv, caller policy path, or publishing verb is accepted. 8import "nx_tool_run.nx" 9import "nx_itoa_lib.nx" 10import "nx_compare_capture_scope_lib.nx" 11const CRM_I64_MAX: i64 = 9223372036854775807 12const CRM_PATH_CAP: i64 = 512 13func crm_out(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return sys_write(1, s, n) } 14func 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 } 15func crm_decimal(s: *u8) -> i64 { 16 var n: i64 = 0; var i: i64 = 0 17 if s[0] == (0 as u8) { return 0 } 18 while s[i] != (0 as u8) { 19 let c: i64 = s[i] as i64 20 if c < 48 { return 0 }; if c > 57 { return 0 } 21 let d: i64 = c - 48 22 if n > (CRM_I64_MAX - d) / 10 { return 0 } 23 n = n * 10 + d; i = i + 1 24 } 25 return n 26} 27func crm_append(p: *u8, at: i64, s: *u8) -> i64 { 28 var i: i64 = 0; var o: i64 = at 29 while s[i] != (0 as u8) { p[o] = s[i]; o = o + 1; i = i + 1 } 30 p[o] = 0 as u8 31 return o 32} 33func main(argc: i64, argv: *i64) -> i64 { 34 if argc != 5 { if argc != 8 { crm_out("usage: nx_compare_ladder_capture <unique-output-stem> <capture-bytes> <timeout-ms> <html|json> [<target-basename> <domain> <scope-id>]\n" as *u8); return 2 } } 35 let capacity: i64 = crm_decimal(argv[2] as *u8) 36 let deadline: i64 = crm_decimal(argv[3] as *u8) 37 if capacity <= 0 { crm_out("REFUSED invalid capture bytes\n" as *u8); return 2 } 38 if deadline <= 0 { crm_out("REFUSED invalid timeout\n" as *u8); return 2 } 39 let path: *u8 = sys_mmap(CRM_PATH_CAP) 40 if (path as i64) <= 0 { return 3 } 41 let prefix_len: i64 = crm_append(path, 0, "knowledge/status/" as *u8) 42 let name: *u8 = argv[1] as *u8 43 var ni: i64 = 0 44 while name[ni] != (0 as u8) { 45 if prefix_len + ni + 6 >= CRM_PATH_CAP { crm_out("REFUSED output stem too long\n" as *u8); return 2 } 46 let c: i64 = name[ni] as i64 47 var ok: i64 = 0 48 if c >= 97 { if c <= 122 { ok = 1 } } 49 if c >= 48 { if c <= 57 { ok = 1 } } 50 if c == 95 { ok = 1 }; if c == 45 { ok = 1 } 51 if ok == 0 { crm_out("REFUSED invalid output stem\n" as *u8); return 2 } 52 ni = ni + 1 53 } 54 if ni == 0 { return 2 } 55 let no: i64 = crm_append(path, prefix_len, name) 56 let mode: *u8 = argv[4] as *u8 57 if crm_equal(mode, "html" as *u8) == 0 { if crm_equal(mode, "json" as *u8) == 0 { return 2 } } 58 if crm_equal(mode, "html" as *u8) == 1 { crm_append(path, no, ".html" as *u8) } else { crm_append(path, no, ".json" as *u8) } 59 var target: *u8="nx_swcompare_matrix_ladder_test_20260907" as *u8 60 var domain: *u8="deploy" as *u8 61 var scope: *u8="legacy-default" as *u8 62 if argc==8 { 63 target=argv[5] as *u8; domain=argv[6] as *u8; scope=argv[7] as *u8 64 if ccs_target(target)!=1 { crm_out("REFUSED target must be a Compare-family basename\n" as *u8); return 2 } 65 if ccs_identifier(domain,CRM_PATH_CAP-1)!=1 { crm_out("REFUSED invalid domain\n" as *u8); return 2 } 66 if ccs_identifier(scope,CRM_PATH_CAP-1)!=1 { crm_out("REFUSED invalid scope ID\n" as *u8); return 2 } 67 } 68 let executable: *u8=sys_mmap(CRM_PATH_CAP) 69 var ep: i64=crm_append(executable,0,"_build/" as *u8) 70 ep=crm_append(executable,ep,target); crm_append(executable,ep,".sov.elf" as *u8) 71 let digest: *u8=sys_mmap(CCS_HEX_BYTES+1) 72 if sys_chdir("buildroot" as *u8) != 0 { crm_out("REFUSED cannot chdir buildroot\n" as *u8); return 3 } 73 if argc==8 { 74 if ccs_authorize(scope,target,domain,mode,executable,digest)!=1 { 75 crm_out("REFUSED missing, ambiguous, mismatched or hash-changed Compare capture scope\n" as *u8); return 7 76 } 77 } 78 crm_out("CAPTURE-SCOPE target=" as *u8); crm_out(target); crm_out(" domain=" as *u8); crm_out(domain) 79 crm_out(" mode=" as *u8); crm_out(mode); crm_out(" scope=" as *u8); crm_out(scope) 80 if argc==8 { crm_out(" sha256=" as *u8); crm_out(digest) } else { crm_out(" authority=legacy-fixed-target" as *u8) } 81 if crm_equal(mode,"html" as *u8)==1 { crm_out(" reference-assets=may-refresh" as *u8) } 82 else { crm_out(" reference-assets=not-requested-by-wrapper" as *u8) } 83 crm_out(" index-publish-invoked=0\n" as *u8) 84 let prior: i64 = sys_openat_rd(path) 85 if prior >= 0 { sys_close(prior); crm_out("REFUSED output already exists; choose a new test stem\n" as *u8); return 3 } 86 let output: *u8 = sys_mmap(capacity) 87 if (output as i64) <= 0 { crm_out("REFUSED capture allocation failed\n" as *u8); return 3 } 88 let av: *i64 = sys_mmap(32) as *i64 89 av[0] = executable as i64 90 av[1] = domain as i64; av[2] = mode as i64; av[3] = 0 91 let olen: *i64 = sys_mmap(16) as *i64 92 let cut: *i64 = sys_mmap(16) as *i64 93 let rc: i64 = tr_run_capture_tr(av[0] as *u8, av, output, capacity, olen, deadline, cut) 94 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) 95 if rc != 0 { return 4 } 96 if cut[0] != TR_FIT { return 4 } 97 if olen[0] <= 0 { return 4 } 98 if crm_equal(mode, "html" as *u8) == 1 { 99 if tr_contains(output, olen[0], "<html" as *u8) != 1 { return 4 } 100 if tr_contains(output, olen[0], "</html>" as *u8) != 1 { return 4 } 101 } else { if output[0] != (123 as u8) { return 4 } } 102 let ofd: i64 = sys_openat_wr(path, 384) 103 if ofd < 0 { crm_out("REFUSED cannot create test output\n" as *u8); return 5 } 104 var done: i64 = 0 105 while done < olen[0] { 106 let wrote: i64 = sys_write(ofd, (output as i64 + done) as *u8, olen[0] - done) 107 if wrote <= 0 { sys_close(ofd); crm_out("FAILED short write; partial test artifact retained\n" as *u8); return 5 } 108 done = done + wrote 109 } 110 if sys_close(ofd) != 0 { return 5 } 111 let vlen: *i64 = sys_mmap(16) as *i64 112 let verify: *u8 = sys_read_file(path, vlen) 113 if (verify as i64) == 0 { return 6 } 114 if vlen[0] != olen[0] { return 6 } 115 var vi: i64 = 0 116 while vi < olen[0] { if verify[vi] != output[vi] { return 6 }; vi = vi + 1 } 117 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) 118 return 0 119} 120 121