code wiki / _hdl_build / nx_magic.nx
nx_magic.nx
buildroot/runtime/_hdl_build/nx_magic.nx
about
nx_magic.nx -- the MISSING FIXER for law L001 (CLAUDE rule 11, debt seq274 sev8).
nx_law_warden DETECTS the breach (2348 inline literals over 600 organs) and cap-autonomy FILES it,
but nothing turned a breach into a one-step SAFE edit -- so the operator kept watching workstreams
"hit these as bugs and just give them a NEW NUMBER". This organ closes that half.
nx_magic map <file.nx> [threshold] -> JSON: every offending line+col+value+context, UNCAPPED
nx_magic propose <file.nx> [threshold] -> JSON: the const block + per-value site counts
nx_magic apply <file.nx> [threshold] -> ATOMIC rewrite: hoist each distinct value to a named
const inserted AFTER the last import and BEFORE the
first func (the forward-ref law: a const must be
declared above every reader), then replace the sites.
SEMANTIC-NEUTRALITY IS PROVABLE, NOT ASSERTED: hoisting a literal to a const must compile to the
SAME code, so the caller's proof is a BYTE-IDENTICAL elf on rebuild. Not identical => the transform
is NOT neutral => revert. (nx_magic never builds; it stays a pure, reviewable text transform.)
SKIPPED BY CONSTRUCTION (false-positive control): comments -- BOTH whole-line and TRAILING-after-code
(the trailing case was unhandled until 2026-08-14; this line said "comment lines" and meant it),
const/static DECLARATION lines,
bytes inside string literals, digits that are part of an identifier (x2048), and any value below
the threshold. BOTH BASES SINCE 2026-08-25: decimal AND 0x/0X hex. This line used to read "Decimal only
-- hex is DECLARED out of envelope (nearly all hex here is < 1024)", and a declared blind spot is still
a blind spot when the number it produces is published as a rule-11 count. MEASURED on
buildroot/runtime/_hdl_build/nx_activities_gate.nx at floor 2: 41 sites / 26 distinct INCLUDING a
decimal 420 -- and NOT the four `0x1ed` literals four lines away in the same file, which are the same
number. The scanner itself now lives in nx_magic_lib.nx so it can be mutation-proven in-process
(nx_magic_gate); its header carries the mechanism and the scope of what this does NOT change.
IDEMPOTENT: a value whose const already exists is not re-hoisted, so a second apply is a no-op.
license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0
dependencies 2 imports · 0 importers
imports: nx_syscalls.nxnx_magic_lib.nx
imported by: nobody (leaf or entry point)
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| none |
consts
| 29 | const MG_MAGIC_1024: i64 = 1024 |
| 30 | const MG_MAGIC_4096: i64 = 4096 |
| 32 | const MG_OUT: i64 = 1048576 |
| 33 | const MG_COMMA: i64 = 44 |
| 34 | const MG_COLON: i64 = 58 |
| 35 | const MG_LB: i64 = 123 |
| 36 | const MG_RB: i64 = 125 |
| 37 | const MG_DEF_THRESHOLD: i64 = 1024 |
| 44 | const MG_STDERR: i64 = 2 |
| 45 | const MG_EXIT_USAGE: i64 = 2 |
| 46 | const MG_EXIT_ABSENT: i64 = 4 |
| 47 | const MG_FMODE: i64 = MODE_0644 |
functions
| 51 | func mg_werr(s: *u8) -> i64 { sys_write(MG_STDERR, s, mg_slen(s)); return 0 } |
| 52 | func mg_eqs(a: *u8, b: *u8) -> i64 called by 1: main |
| 58 | func mg_b(d: *u8, o: i64, c: i64) -> i64 { d[o] = c as u8; return o + 1 } called by 1: main |
| 59 | func mg_cat(d: *u8, o: i64, s: *u8) -> i64 |
| 65 | func mg_catn(d: *u8, o: i64, v: i64) -> i64 |
| 77 | func mg_atoi(s: *u8) -> i64 called by 1: main |
| 91 | func mg_detect_prefix(q: *u8, n: i64, out: *u8) -> i64 |
| 121 | func mg_name_for(pfx: *u8, v: i64, out: *u8) -> i64 |
| 129 | func mg_has_const(q: *u8, n: i64, name: *u8) -> i64 |
| 150 | func mg_cat_json(d: *u8, o: i64, q: *u8, a: i64, b: i64) -> i64 called by 1: main |
| 169 | func main(argc: i64, argv: *i64) -> i64 |