nx_restage.nx source
↩ module page · 132 lines · 8731 B
1// nx_restage.nx -- EAT THE DEPLOY-TOOLING DEBT (operator 2026-07-17 "eat the debt and build the tools"):
2// the missing primitive that restages a freshly-built elf into a build tree's _offc/. Without it,
3// buildroot/_offc generators go STALE vs runtime source (the F-004 stale-binary class: e.g. the compare
4// @verdict directive existed in nx_swcompare_sota.nx source but the staged _offc elf predated it, so the
5// SSOT verdict silently fell back to a hardcoded string). nx_fs_write refuses _offc (text + deny), nx_shelltool
6// is read-only, /api/build stages to nishihost not _offc -- so this closed gap blocked EVERY generator refresh.
7// nx_restage <target> [broot] broot default "buildroot" ("." = in-place / local test)
8// -> chdir broot; if the PROMOTED twin ../<target>.elf exists, COPY IT (2026-09-05, see WHY below); else
9// fork _offc/nx_sov_build_run.elf <target> --build-only (=> _build/<target>.sov.elf)
10// -> binary-safe copy to _offc/<target>.elf.new; VERIFY ELF magic + nonzero; atomic rename over _offc/<target>.elf
11// -> emits {"action":"RESTAGED","target":..,"path":..,"source":"PROMOTED ../<t>.elf"|"BUILT _build/<t>.sov.elf","bytes":N}
12// WHY PROMOTED-FIRST (2026-09-05): nx_compare_regen's fork-freshness check SHA-256s _offc/<gen>.elf against
13// the PROMOTED ../<gen>.elf and names "nx_restage <gen>" as the remedy. A REBUILD here runs the sovereign
14// lane, which is byte-different from the /api/build lane on identical source (measured: nx_swcompare_matrix
15// restaged 332,217 B against the promoted 368,016 B -- FORK-STALE by construction, a THIRD digest). Copying
16// the promoted bytes is the only refresh that can ever read FRESH; the rebuild stays as the fallback for a
17// target that has never been promoted. The destination is opened 0755, so an inert twin cannot be produced.
18// target sanitized [A-Za-z0-9_] (no path escape). Exit 0 restaged | 1 build/copy fail | 2 usage/bad-target.
19// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0
20import "nx_tool_run.nx"
21const K_MAGIC_262144: i64 = 262144
22const K_MAGIC_65536: i64 = 65536
23
24func rp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
25func rpe(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(2,s,n); return 0 }
26func rn(v: i64) -> i64 { let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m} let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 }
27// append C-string src into dst at off; return new off
28func r_cat(dst: *u8, off: i64, src: *u8) -> i64 { var o: i64=off; var j: i64=0; while src[j]!=(0 as u8){dst[o]=src[j];o=o+1;j=j+1} return o }
29// target must be nonempty [A-Za-z0-9_], <64 chars. returns 1 ok / 0 reject.
30func r_san(name: *u8) -> i64 {
31 var i: i64 = 0
32 while name[i] != (0 as u8) {
33 let c: i64 = name[i] as i64
34 var ok: i64 = 0
35 if c >= 48 { if c <= 57 { ok = 1 } } // 0-9
36 if c >= 65 { if c <= 90 { ok = 1 } } // A-Z
37 if c >= 97 { if c <= 122 { ok = 1 } } // a-z
38 if c == 95 { ok = 1 } // _
39 if ok == 0 { return 0 }
40 i = i + 1
41 if i >= 64 { return 0 }
42 }
43 if i == 0 { return 0 }
44 return 1
45}
46
47func main(argc: i64, argv: *i64) -> i64 {
48 if argc < 2 { rpe("usage: nx_restage <target> [buildroot]\n" as *u8); sys_exit(2); return 2 }
49 let target: *u8 = argv[1] as *u8
50 var broot: *u8 = "buildroot" as *u8
51 if argc >= 3 { broot = argv[2] as *u8 }
52 if r_san(target) == 0 { rpe("nx_restage: REFUSED bad target (need [A-Za-z0-9_], <64)\n" as *u8); sys_exit(2); return 2 }
53 // NAME THE PATH ACTUALLY TRIED, never the word the author had in mind (sibling fix, same hour, same
54 // defect as nx_compare_regen and nx_stale_check): printing a fixed "buildroot" sends the reader to
55 // inspect a healthy build root instead of the argument they actually mistyped.
56 // AND THE FIRST CUT OF THIS COMMENT WAS ITSELF THE HALF-FIX IT WARNS ABOUT: it said "argv[1] is the
57 // BUILD ROOT", copied verbatim from nx_compare_regen where that IS true. Here -- and in
58 // nx_stale_check, where the same wrong sentence was pasted -- argv[1] is the TARGET and argv[2] is the
59 // build root. One explanation was written once and applied to three organs without re-reading any of
60 // their argv contracts, which is precisely how a fix becomes wrong in the organs it was copied into.
61 if sys_chdir(broot) != 0 { rpe("nx_restage: cannot chdir " as *u8); rpe(broot); rpe(" (argv[2] is the build root; argv[1] is the target)\n" as *u8); sys_exit(1); return 1 }
62
63 // ---- source of truth (2026-09-05): the PROMOTED serving-root twin ../<target>.elf, when one exists ----
64 let psrc: *u8 = sys_mmap(256)
65 var pso: i64 = r_cat(psrc, 0, "../" as *u8); pso = r_cat(psrc, pso, target); pso = r_cat(psrc, pso, ".elf" as *u8); psrc[pso] = 0 as u8
66 var from_promoted: i64 = 0
67 let pfd: i64 = sys_openat_rd(psrc)
68 if pfd >= 0 { sys_close(pfd); from_promoted = 1 }
69
70 // ---- build (FALLBACK for a never-promoted target): fork _offc/nx_sov_build_run.elf <target> --build-only ----
71 let av: *i64 = sys_mmap(64) as *i64
72 av[0] = "_offc/nx_sov_build_run.elf" as i64
73 av[1] = target as i64
74 av[2] = "--build-only" as i64
75 av[3] = 0
76 let cap: i64 = K_MAGIC_262144
77 let out: *u8 = sys_mmap(cap)
78 let ol: *i64 = sys_mmap(16) as *i64
79 var rc: i64 = 0
80 if from_promoted == 0 { rc = tr_run_capture("_offc/nx_sov_build_run.elf" as *u8, av, out, cap, ol) }
81 if rc != 0 {
82 rpe("nx_restage: BUILD FAILED rc=" as *u8); let d: *u8=sys_mmap(16); var m: i64=rc; if m<0{sys_write(2,"-" as *u8,1);m=0-m} var k: i64=0; if m==0{d[0]=48 as u8;k=1} while m>0{d[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var z: i64=0; let e: *u8=sys_mmap(16); while z<k{e[z]=d[k-1-z];z=z+1} sys_write(2,e,k); rpe("\n" as *u8)
83 if ol[0] > 0 { sys_write(2, out, ol[0]); rpe("\n" as *u8) }
84 sys_exit(1); return 1
85 }
86
87 // ---- paths ----
88 let src: *u8 = sys_mmap(256)
89 var so: i64 = 0
90 if from_promoted == 1 { so = r_cat(src, 0, psrc) } else {
91 // 2026-07-29 seq1267: runner emits buildroot/_build now (no-/tmp doctrine); we are post-chdir(broot),
92 // so the artifact is _build/<target>.sov.elf relative to the buildroot CWD.
93 so = r_cat(src, 0, "_build/" as *u8); so = r_cat(src, so, target); so = r_cat(src, so, ".sov.elf" as *u8)
94 }
95 src[so] = 0 as u8
96 let dnew: *u8 = sys_mmap(256)
97 var dno: i64 = r_cat(dnew, 0, "_offc/" as *u8); dno = r_cat(dnew, dno, target); dno = r_cat(dnew, dno, ".elf.new" as *u8); dnew[dno] = 0 as u8
98 let dst: *u8 = sys_mmap(256)
99 var dso: i64 = r_cat(dst, 0, "_offc/" as *u8); dso = r_cat(dst, dso, target); dso = r_cat(dst, dso, ".elf" as *u8); dst[dso] = 0 as u8
100
101 // ---- binary-safe copy src -> dnew, verify ELF magic on first bytes ----
102 let fin: i64 = sys_openat_rd(src)
103 if fin < 0 { rpe("nx_restage: source artifact missing: " as *u8); rpe(src); rpe("\n" as *u8); sys_exit(1); return 1 }
104 let fout: i64 = sys_openat_wr(dnew, 493) // 0755 executable
105 if fout < 0 { sys_close(fin); rpe("nx_restage: cannot open _offc/<target>.elf.new (write cap?)\n" as *u8); sys_exit(1); return 1 }
106 let buf: *u8 = sys_mmap(K_MAGIC_65536)
107 var total: i64 = 0
108 var magic_ok: i64 = 0
109 var go: i64 = 1
110 while go == 1 {
111 let r: i64 = sys_read(fin, buf, K_MAGIC_65536)
112 if r <= 0 { go = 0 } else {
113 if total == 0 {
114 if r >= 4 { if buf[0] == (127 as u8) { if buf[1] == (69 as u8) { if buf[2] == (76 as u8) { if buf[3] == (70 as u8) { magic_ok = 1 } } } } }
115 }
116 var w: i64 = 0
117 while w < r { let k: i64 = sys_write(fout, (buf as i64 + w) as *u8, r - w); if k <= 0 { go = 0; w = r } else { w = w + k } }
118 total = total + r
119 }
120 }
121 sys_close(fin)
122 sys_close(fout)
123 if total < 4 { rpe("nx_restage: copied 0 bytes\n" as *u8); sys_exit(1); return 1 }
124 if magic_ok == 0 { rpe("nx_restage: NOT an ELF (magic check failed) -- refusing to stage\n" as *u8); sys_exit(1); return 1 }
125 if sys_renameat(dnew, dst) != 0 { rpe("nx_restage: atomic rename into _offc failed\n" as *u8); sys_exit(1); return 1 }
126
127 rp("{\"action\":\"RESTAGED\",\"target\":\"" as *u8); rp(target); rp("\",\"path\":\"_offc/" as *u8); rp(target); rp(".elf\",\"source\":\"" as *u8)
128 if from_promoted == 1 { rp("PROMOTED " as *u8) } else { rp("BUILT " as *u8) }
129 rp(src); rp("\",\"bytes\":" as *u8); rn(total); rp("}\n" as *u8)
130 sys_exit(0)
131 return 0
132}