code wiki / _hdl_build / nx_janitor_scrub.nx
nx_janitor_scrub.nx source
↩ module page · 87 lines · 4748 B
1// nx_janitor_scrub.nx -- the JANITOR's third-party CLEANUP capability = the unbuilt "sprawl half (X-JAN-002)"
2// named in nx_janitor.nx's own header (operator: "get s class capabilities in the nishi auditor and janitor to
3// clean all this up that you fuck up and do"). The auditor (nx_sovereignty_audit) DETECTS the third-party debt
4// and splits it 3 ways (sav_class_of: 1=THIRDPARTY_LANG 2=NONSOV_FORMAT 3=WEB_BOUNDARY) but nothing ACTS -- so
5// we scrub by hand, over and over. This composes the auditor's classes and applies the SAFE action per class,
6// additive-only (#13: never delete production data) + NEVER-BRICK (#26: reversible by construction):
7// NONSOV_FORMAT -> MIGRATE the bytes into the sovereign seg_store, BYTE-VERIFIED, original left INTACT (reversible).
8// THIRDPARTY_LANG-> QUARANTINE: record in a ledger for OPERATOR-GATED retirement (a .py can't be auto-rewritten; FLAG).
9// WEB_BOUNDARY -> REVIEW: flag (Law-1 browser boundary; emitted-vs-vendored is a human call).
10// The janitor never deletes / never overwrites the source -- it MIGRATES (additive) + FLAGS. Sovereign. license_tier: ORIGINAL
11import "nx_seg_store.nx"
12import "nx_syscalls.nx"
13
14// the janitor's SAFE actions, keyed by the AUDITOR's debt class (sav_class_of)
15const JAN_NONE: i64 = 0
16const JAN_MIGRATE: i64 = 1
17const JAN_QUARANTINE: i64 = 2
18const JAN_REVIEW: i64 = 3
19const JAN_QSTORE: *u8 = "knowledge/store/janitor-quarantine"
20
21// THE CLEANUP POLICY: the safe action for an auditor debt class. format->migrate, lang->quarantine, web->review.
22func jan_action(audit_class: i64) -> i64 {
23 if audit_class == 2 { return JAN_MIGRATE } // NONSOV_FORMAT (tsv/conf/toml/json)
24 if audit_class == 1 { return JAN_QUARANTINE } // THIRDPARTY_LANG (py/sh/ts/mjs)
25 if audit_class == 3 { return JAN_REVIEW } // WEB_BOUNDARY (js/css/html)
26 return JAN_NONE
27}
28
29func jan_exists(path: *u8) -> i64 { let fd: i64=sys_openat_rd(path); if fd>=0 { sys_close(fd); return 1 } return 0 }
30
31func jan_key(k: i64, out: *u8) -> i64 {
32 var o: i64=0; let p: *u8="row:" as *u8; var i: i64=0
33 while p[i]!=(0 as u8){ out[o]=p[i]; o=o+1; i=i+1 }
34 if k==0 { out[o]=48 as u8; o=o+1 } else { let t: *u8=sys_mmap(24); var m: i64=k; var n: i64=0; while m>0 { t[n]=(48+(m%10)) as u8; m=m/10; n=n+1 } var j: i64=0; while j<n { out[o]=t[n-1-j]; o=o+1; j=j+1 } }
35 out[o]=0 as u8; return 0
36}
37
38// MIGRATE a non-sovereign data file -> seg_store, ADDITIVELY (original UNTOUCHED) + BYTE-VERIFIED. totp[0]=source
39// data-lines, verp[0]=lines proven byte-exact in the store. NEVER-BRICK: reads src, writes store, ZERO deletes.
40// The general engine the operator wanted ("get us off tsv") -- works for ANY non-sov data file, not just RACI.
41func jan_migrate(src_path: *u8, store_prefix: *u8, totp: *i64, verp: *i64) -> i64 {
42 let szp: *i64 = sys_mmap(16) as *i64
43 let buf: *u8 = ss_readall(src_path, szp)
44 let sz: i64 = szp[0]
45 if sz <= 0 { totp[0]=0; verp[0]=0; return 0 }
46 let lp: *i64 = sys_mmap(8*4096) as *i64; let ll: *i64 = sys_mmap(8*4096) as *i64
47 var k: i64 = 0; var i: i64 = 0; var ls: i64 = 0
48 while i <= sz {
49 var nl: i64 = 0; if i>=sz { nl=1 } else { if buf[i]==(10 as u8) { nl=1 } }
50 if nl==1 {
51 let len: i64 = i - ls
52 if len>0 { if (buf[ls] as i64) != 35 { if k<4096 { lp[k]=(buf as i64)+ls; ll[k]=len; k=k+1 } } } // skip '#'
53 ls = i+1
54 }
55 i=i+1
56 }
57 totp[0]=k
58 let w: *i64 = ss_begin()
59 var j: i64 = 0
60 while j<k { let rk: *u8=sys_mmap(64); jan_key(j, rk); ss_add(w, 1, rk, lp[j] as *u8, ll[j]); j=j+1 }
61 ss_commit(store_prefix, w, sys_now_realtime_sec())
62 let h: *i64 = ss_open(store_prefix)
63 let pq: *i64=sys_mmap(16) as *i64; let lq: *i64=sys_mmap(16) as *i64
64 var ver: i64 = 0; var j2: i64 = 0
65 while j2<k {
66 let rk: *u8=sys_mmap(64); jan_key(j2, rk)
67 if ss_hget(h, rk, pq, lq)==1 { if lq[0]==ll[j2] { let a: *u8=pq[0] as *u8; let b: *u8=lp[j2] as *u8; var eq: i64=1; var x: i64=0; while x<lq[0] { if a[x]!=b[x] { eq=0 } x=x+1 } if eq==1 { ver=ver+1 } } }
68 j2=j2+1
69 }
70 verp[0]=ver
71 return ver
72}
73
74// QUARANTINE: record a third-party-lang file in the ledger for OPERATOR-GATED retirement. NEVER deletes (#13).
75func jan_quarantine_record(path: *u8) -> i64 {
76 let w: *i64 = ss_begin()
77 ss_add(w, 1, path, "QUARANTINE-3RDPARTY-LANG-pending-operator-retire" as *u8, 48)
78 ss_commit(JAN_QSTORE, w, sys_now_realtime_sec())
79 return 1
80}
81func jan_is_quarantined(path: *u8) -> i64 {
82 let h: *i64 = ss_open(JAN_QSTORE)
83 if (h as i64)==0 { return 0 }
84 let pq: *i64=sys_mmap(16) as *i64; let lq: *i64=sys_mmap(16) as *i64
85 if ss_hget(h, path, pq, lq)==1 { return 1 }
86 return 0
87}