code wiki / _hdl_build / nx_consol_apply.nx
nx_consol_apply.nx source
↩ module page · 99 lines · 4767 B
1// nx_consol_apply.nx -- GATED CONSOLIDATION APPLIER (2026-07-20, closes the atlas-consolidation loop:
2// detect[nx_consolidate] -> classify[reachability] -> APPLY[here], behaviour-preserving BY CONSTRUCTION).
3// The never-brick proof for retiring a dup shadow: build the affected target (BEFORE), retire the shadow,
4// rebuild (AFTER). If AFTER == BEFORE byte-for-byte, the shadow was NEVER in the build -> retiring it is
5// PROVEN behaviour-neutral -> CONSOLIDATED. If it differs (or the build changes/breaks), the shadow WAS
6// live -> RESTORE it and REFUSE. Retire = MOVE (rule 13 additive: the shadow is relocated, never deleted;
7// rollback = move it back). No source is edited; a seat/PM points this at one proposal from the consolq- plane.
8// argv: <shadow-path> <retire-path> <buildelf> <buildarg> <artifact-path>
9// exit: 0 CONSOLIDATED | 1 REFUSED-live-shadow (restored) | 2 usage/shadow-absent | 3 build/move error (restored)
10// license_tier: ORIGINAL No hw writes (Rule 26).
11import "nx_seat_drive_lib.nx"
12import "nx_seg_store.nx"
13import "nx_deploy_lib.nx"
14import "nx_syscalls.nx"
15
16const CA_CAP: i64 = 2097152
17
18func ca_read(path: *u8, buf: *u8, cap: i64) -> i64 {
19 let fd: i64 = sys_openat_rd(path)
20 if fd < 0 { return 0 - 1 }
21 var n: i64 = 0
22 var r: i64 = sys_read(fd, buf, cap - 1)
23 while r > 0 { n = n + r; if n >= cap - 1 { r = 0 } else { r = sys_read(fd, buf + n, cap - 1 - n) } }
24 sys_close(fd)
25 return n
26}
27func ca_exists(path: *u8) -> i64 { let fd: i64 = sys_openat_rd(path); if fd >= 0 { sys_close(fd); return 1 } return 0 }
28func ca_eq(a: *u8, an: i64, b: *u8, bn: i64) -> i64 { if an != bn { return 0 } var i: i64 = 0; while i < an { if a[i] != b[i] { return 0 } i = i + 1 } return 1 }
29
30func main(argc: i64, argv: *i64) -> i64 {
31 if argc < 6 { sd_w("usage: nx_consol_apply <shadow> <retire> <buildelf> <buildarg> <artifact>\n" as *u8); sys_exit(2); return 2 }
32 let shadow: *u8 = argv[1] as *u8
33 let retire: *u8 = argv[2] as *u8
34 let buildelf: *u8 = argv[3] as *u8
35 let buildarg: *u8 = argv[4] as *u8
36 let artifact: *u8 = argv[5] as *u8
37
38 if ca_exists(shadow) == 0 {
39 sd_w("NX-CONSOL-APPLY verdict=ABORTED reason=shadow-file-not-found:" as *u8); sd_w(shadow)
40 sd_w(" fix=verify-the-path;-it-may-already-be-consolidated-(check-runtime/_retired_shadow/)\n" as *u8)
41 sys_exit(2); return 2
42 }
43
44 let av: *i64 = sys_mmap(32) as *i64
45 av[0] = buildarg as i64
46
47 // BEFORE build -> snapshot the artifact
48 dep_run_capture(buildelf, av, 1, "/tmp/ca_build.out" as *u8)
49 let before: *u8 = sys_mmap(CA_CAP)
50 let bn: i64 = ca_read(artifact, before, CA_CAP)
51 if bn <= 0 {
52 sd_w("NX-CONSOL-APPLY verdict=ABORTED reason=before-build-produced-no-artifact(target-does-not-build-cleanly)" as *u8)
53 sd_w(" fix=the-build-target-may-be-wrong-or-already-broken;-build-it-manually-first-to-see-the-compiler-error. shadow-UNTOUCHED\n" as *u8)
54 sys_exit(3); return 3
55 }
56
57 // RETIRE the shadow (move, additive)
58 if sys_renameat(shadow, retire) != 0 {
59 sd_w("NX-CONSOL-APPLY verdict=ABORTED reason=retire-move-failed(filesystem-error)" as *u8)
60 sd_w(" fix=ensure-the-retire-directory-exists-and-is-writable(mkdir-p-its-parent). shadow-UNTOUCHED\n" as *u8)
61 sys_exit(3); return 3
62 }
63
64 // AFTER build -> re-snapshot
65 dep_run_capture(buildelf, av, 1, "/tmp/ca_build.out" as *u8)
66 let after: *u8 = sys_mmap(CA_CAP)
67 let an: i64 = ca_read(artifact, after, CA_CAP)
68
69 if an > 0 { if ca_eq(before, bn, after, an) == 1 {
70 sd_w("NX-CONSOL-APPLY verdict=CONSOLIDATED (build byte-identical; shadow was dead) retired=" as *u8)
71 sd_w(retire)
72 sd_w("\n" as *u8)
73 sys_exit(0)
74 return 0
75 } }
76
77 // build changed or broke -> the shadow WAS live -> RESTORE + REFUSE (never-brick)
78 if sys_renameat(retire, shadow) != 0 {
79 sd_w("NX-CONSOL-APPLY verdict=REFUSED-AND-RESTORE-FAILED reason=build-changed-AND-the-restore-move-failed shadow=" as *u8)
80 sd_w(shadow)
81 sd_w(" fix=MANUAL-RESTORE-NEEDED-move-" as *u8)
82 sd_w(retire)
83 sd_w("-back-to-" as *u8)
84 sd_w(shadow)
85 sd_w("-immediately\n" as *u8)
86 sys_exit(3)
87 return 3
88 }
89 sd_w("NX-CONSOL-APPLY verdict=REFUSED reason=retiring-the-shadow-CHANGED-the-target-build(the-shadow-is-LIVE,-not-dead-code) bytesBefore=" as *u8)
90 let nb: *u8 = sys_mmap(32)
91 sd_num(nb, 0, bn)
92 sd_w(nb)
93 sd_w(" bytesAfter=" as *u8)
94 sd_num(nb, 0, an)
95 sd_w(nb)
96 sd_w(" fix=this-copy-is-the-one-the-build-actually-uses;-do-NOT-retire-it;-if-consolidating-check-which-tree-is-canonical-and-retire-the-OTHER-copy-instead. shadow-RESTORED(safe)\n" as *u8)
97 sys_exit(1)
98 return 1
99}