code wiki / (root) / nx_self_build.nx

nx_self_build.nx

buildroot/runtime/nx_self_build.nx

16119 B365 linesdepth 7pulls 8 transitivereach 1 importersview sourcekind librarytopic self
docsdependenciesstructsconstsfunctions

about

nx_self_build.nx -- the 3-tier SELF-BUILD POLICY + the reversible-modify transaction (snapshot -> write -> re-gate -> AUTO-ROLLBACK). THE NET BEFORE THE TRAPEZE. Before the engine is ever pointed at a real source file, this organ proves -- on a DELIBERATE failed write -- that a reversible modify either lands gated-green OR is rolled back BYTE-IDENTICAL to the pre-write snapshot. A rollback you have not proven on a deliberate failure is not a safety net. ============================================================================ THE OPERATOR'S 3-TIER POLICY (the spec): TIER 1 ADDITIVE : new file / append / soft-flag is_current=0 -> ALLOW autonomously (cannot break existing code). TIER 2 REVERSIBLE : overwrite an existing file -> ALLOW only via SNAPSHOT -> WRITE -> RE-GATE -> AUTO-ROLLBACK on failure. No breaking without a proven, byte-identical rollback. TIER 3 DESTRUCTIVE: delete / drop / overwrite WITHOUT backup / truncate history -> DENY + require explicit operator confirm (Cardinal #13 additive-only). FAIL-CLOSED on unknown. ============================================================================ ADDITIVE-OVER-WARDEN: the classifier never WEAKENS any warden verdict. It can only refuse where warden refuses, or ADD the rollback discipline on top of an overwrite that warden hard-denies. Warden stays the single source of cardinal-truth; this is a pure composition layer (no edit to nx_warden_lib). CRASH-SAFETY (the headline trap defended): every destructive write is done write-temp -> fsync -> renameat2 (atomic on the same fs), NEVER truncate-in- place. The ORIGINAL is never mutated until the new version is published; on a gate-fail we restore from a DURABLE on-disk snapshot (not RAM-only) via the same atomic path, then VERIFY the restored bytes rehash to the snapshot hash. RE-GATE DECOUPLING (avoids the gate-runner's hardcoded /tmp scratch-path collision under concurrent ticks, AND keeps this module free of the heavy fork+exec re-gate dependency). The transaction is TWO-PHASE: nx_self_build_begin(txn) : snapshot (durable) -> journal INTENT -> atomic-write the new bytes. The new file now sits on disk for the caller to GATE. <caller runs its gate on `target`> (prod: nx_gate_runner.gr_gate;

dependencies 4 imports · 1 importers

nx_syscalls.nx nx_blob_store.nx nx_journal_log.nx nx_warden_lib.nx nx_self_build.nx nx_self_build_test.nx

imports: nx_syscalls.nxnx_blob_store.nxnx_journal_log.nxnx_warden_lib.nx

imported by: nx_self_build_test.nx

structs

110struct NxSelfBuild
132struct NxSbTxn

consts

64const SB_TIER_ADDITIVE: i64 = 1
65const SB_TIER_REVERSIBLE: i64 = 2
66const SB_TIER_DESTRUCTIVE: i64 = 3
69const SB_OK: i64 = 0 // write passed gate, new bytes stand
70const SB_ROLLED_BACK: i64 = 1 // write failed gate, restored byte-ident
71const SB_ROLLBACK_FAILED: i64 = 2 // LOUD: restore != snapshot (must NEVER)
72const SB_DENIED: i64 = 3 // tier-3, needs explicit operator confirm
73const SB_BAD_INPUT: i64 = 4 // null ctx / unreadable target / etc.
76const SB_SCHEMA_INTENT: i64 = 7001
77const SB_SCHEMA_COMMIT: i64 = 7002
78const SB_SCHEMA_ROLLBACK: i64 = 7003
79const SB_SCHEMA_ROLLBACK_FAILED: i64 = 7004
81const SB_MODE: i64 = 420 // 0644
99const SB_SYS_RENAMEAT2: i64 = 316
100const SB_SYS_FSYNC: i64 = 74
103const SB_SYS_RENAMEAT2: i64 = 316
104const SB_SYS_FSYNC: i64 = 74

functions

115func nx_self_build_new() -> *NxSelfBuild
122func nx_self_build_is_valid(ctx: *NxSelfBuild) -> i64
145func sb_slen(s: *u8) -> i64
152func sb_concat(base: *u8, suffix: *u8) -> *u8
165func sb_fsync(fd: i64) -> i64
called by 1: sb_atomic_write
170func sb_renameat2(old_path: *u8, new_path: *u8) -> i64
called by 1: sb_atomic_write
177func sb_write_all(fd: i64, buf: *u8, len: i64) -> i64
called by 1: sb_atomic_write calls 1: sys_write
194func sb_atomic_write(target: *u8, buf: *u8, len: i64) -> i64
207func nx_sb_classify(kind: i64, target: *u8, has_backup: i64, has_gate: i64) -> i64
221func nx_self_build_authorize(kind: i64, target: *u8,
called by 1: main calls 2: nx_sb_classifywarden_authorize
238func nx_sb_txn_new(ctx: *NxSelfBuild, target: *u8) -> *NxSbTxn
called by 1: main calls 1: sys_mmap
256func nx_self_build_begin(txn: *NxSbTxn, new_bytes: *u8, new_len: i64) -> i64
298func sb_txn_snap_hash(txn: *NxSbTxn) -> *NxBlobHash
315func nx_self_build_finish(txn: *NxSbTxn, verdict: i64) -> i64