code wiki / _hdl_build / nx_sprawl_ratchet.nx
nx_sprawl_ratchet.nx source
↩ module page · 55 lines · 3618 B
1// nx_sprawl_ratchet.nx -- the MULTI-AXIS keep-it-gone ratchet: generalizes nx_sovaudit_ratchet (debt only) to
2// ALL three cleanliness axes from the scorecard -- third-party DEBT, shadow COLLISIONS, SCRATCH accumulation.
3// Each axis has its OWN locked floor in the sovereign seg_store; on each run an axis may HOLD or FALL (ratchets
4// its floor down + locks it), but any RISE is a REGRESSION on that axis = the composite verdict goes RED. Run
5// standing (conductor pulse) and NO new sprawl can creep in on ANY axis without tripping it -- the comprehensive
6// "stop fucking up" guard. Composes the read-only detectors; the only write is the 3 floors. license_tier: ORIGINAL
7import "nx_sovereignty_scorecard.nx" // ssc_debt, ssc_collisions, ssc_scratch
8import "nx_seg_store.nx"
9import "nx_syscalls.nx"
10
11const SPR_STORE: *u8 = "knowledge/store/sprawl-ratchet"
12const SPR_NOFLOOR: i64 = 1000000000
13
14func spr_atoi(s: *u8, n: i64) -> i64 { var v: i64=0; var i: i64=0; while i<n { v = v*10 + ((s[i] as i64)-48); i=i+1 } return v }
15func spr_itoa(v: i64, out: *u8) -> i64 { var m: i64=v; var k: i64=0; if m==0 { out[0]=48 as u8; return 1 } let t: *u8=sys_mmap(24); var nn: i64=0; while m>0 { t[nn]=(48+(m%10)) as u8; m=m/10; nn=nn+1 } while k<nn { out[k]=t[nn-1-k]; k=k+1 } return nn }
16
17func spr_floor_get(axis: *u8) -> i64 {
18 let h: *i64 = ss_open(SPR_STORE)
19 if (h as i64)==0 { return SPR_NOFLOOR }
20 let pq: *i64=sys_mmap(16) as *i64; let lq: *i64=sys_mmap(16) as *i64
21 if ss_hget(h, axis, pq, lq)!=1 { return SPR_NOFLOOR }
22 return spr_atoi(pq[0] as *u8, lq[0])
23}
24// write all three floors in ONE commit (seg_store commit overwrites, so every floor is re-written each time).
25func spr_floor_set_all(d: i64, c: i64, s: i64) -> i64 {
26 let w: *i64 = ss_begin()
27 let bd: *u8=sys_mmap(24); let ld: i64=spr_itoa(d, bd); ss_add(w, 1, "floor:debt" as *u8, bd, ld)
28 let bc: *u8=sys_mmap(24); let lc: i64=spr_itoa(c, bc); ss_add(w, 1, "floor:coll" as *u8, bc, lc)
29 let bs: *u8=sys_mmap(24); let ls: i64=spr_itoa(s, bs); ss_add(w, 1, "floor:scratch" as *u8, bs, ls)
30 ss_commit(SPR_STORE, w, sys_now_realtime_sec())
31 return 0
32}
33
34// PURE per-axis ratchet: returns the new floor; sets reg_out[0]=1 if `current` REGRESSED past the locked floor.
35func spr_one(floor: i64, current: i64, reg_out: *i64) -> i64 {
36 if floor == SPR_NOFLOOR { reg_out[0]=0; return current } // first run: adopt current as the floor
37 if current > floor { reg_out[0]=1; return floor } // REGRESSION: grew past the locked best -> floor holds
38 reg_out[0]=0
39 if current < floor { return current } // improvement: ratchet the floor down + lock
40 return floor // held exactly
41}
42
43// check all three axes against their locked floors; ratchet each; write the new floors. reg_out[0] = bitmask
44// (bit0=debt bit1=coll bit2=scratch). returns 1 = no regression on any axis | 0 = at least one regressed.
45func spr_check_all(debt: i64, coll: i64, scratch: i64, reg_out: *i64) -> i64 {
46 let fd: i64=spr_floor_get("floor:debt" as *u8); let fc: i64=spr_floor_get("floor:coll" as *u8); let fs: i64=spr_floor_get("floor:scratch" as *u8)
47 let rd: *i64=sys_mmap(16) as *i64; let rc: *i64=sys_mmap(16) as *i64; let rs: *i64=sys_mmap(16) as *i64
48 let nd: i64=spr_one(fd, debt, rd); let nc: i64=spr_one(fc, coll, rc); let ns: i64=spr_one(fs, scratch, rs)
49 spr_floor_set_all(nd, nc, ns)
50 var reg: i64=0
51 if rd[0]==1 { reg=reg|1 } if rc[0]==1 { reg=reg|2 } if rs[0]==1 { reg=reg|4 }
52 reg_out[0]=reg
53 if reg==0 { return 1 }
54 return 0
55}