code wiki / _hdl_build / nx_caretaker_test.nx

nx_caretaker_test.nx source

↩ module page · 54 lines · 4258 B

1// nx_caretaker_test.nx -- the gardener keeps the ecosystem in balance: it soft-retires REDUNDANT, 2// safe, covered sprawl while REFUSING to touch (a) load-bearing pieces (the Engineer proves removing 3// them kills the ecosystem) and (b) sprawl whose function isn't covered (would lose a capability). 4// The prune must IMPROVE the whole: less sprawl, SAME functionality, fully reversible. Exit 0 if all 5// hold. license_tier: ORIGINAL 6 7import "nx_caretaker.nx" 8import "nx_syscalls.nx" 9 10func ct_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 11func ct_num(v: i64) -> i64 { let bb: *u8 = sys_mmap(28); var m: i64=v; if m<0 {m=0-m}; let t: *u8 = sys_mmap(28); var k: i64=0; if m==0 {t[0]=48;k=1}; while m>0 {t[k]=48+(m%10); m=m/10; k=k+1}; var i: i64=0; while i<k {bb[i]=t[k-1-i]; i=i+1}; sys_write(1, bb, k); return 0 } 12func ct_d(d: i64) -> i64 { if d==CARE_KEEP {ct_puts("KEEP" as *u8)} if d==CARE_KEEP_LOADBEARING {ct_puts("KEEP(load-bearing)" as *u8)} if d==CARE_KEEP_UNCOVERED {ct_puts("KEEP(uncovered)" as *u8)} if d==CARE_SOFT_RETIRE {ct_puts("SOFT-RETIRE" as *u8)} return 0 } 13 14func main() -> i64 { 15 ct_puts("=== CARETAKER / GARDENER: prune sprawl, keep balance, never wipe functionality ===\n" as *u8) 16 let N: i64 = 5 17 let st: *i64 = sys_mmap(8*8) as *i64; let lb: *i64 = sys_mmap(8*8) as *i64; let cv: *i64 = sys_mmap(8*8) as *i64 18 // c0 active core 19 st[0]=CARE_ACTIVE; lb[0]=1; cv[0]=0 20 // c1 a duplicate, not load-bearing, function covered by a survivor -> safe prune 21 st[1]=CARE_DUPLICATE; lb[1]=0; cv[1]=1 22 // c2 a duplicate BUT load-bearing (others depend on it) -> KEEP (ecosystem would die) 23 st[2]=CARE_DUPLICATE; lb[2]=1; cv[2]=1 24 // c3 superseded BUT its function is NOT yet covered -> KEEP (would lose a capability) 25 st[3]=CARE_SUPERSEDED; lb[3]=0; cv[3]=0 26 // c4 superseded (e.g. PSNR-only fidelity) + covered by rd-perception -> safe prune 27 st[4]=CARE_SUPERSEDED; lb[4]=0; cv[4]=1 28 29 var i: i64 = 0 30 while i < N { 31 ct_puts(" c" as *u8); ct_num(i); ct_puts(" status=" as *u8); ct_num(st[i]); ct_puts(" loadbearing=" as *u8); ct_num(lb[i]); ct_puts(" covered=" as *u8); ct_num(cv[i]); ct_puts(" -> " as *u8); ct_d(care_disposition(st[i],lb[i],cv[i])); ct_puts("\n" as *u8) 32 i = i + 1 33 } 34 let sprawl_before: i64 = care_sprawl(N, st) 35 // after the plan, the soft-retired caps no longer count as live sprawl 36 let func_before: i64 = care_functionality(N, st, lb, cv) 37 var sprawl_after: i64 = 0; i = 0 38 while i < N { if st[i] != CARE_ACTIVE { if care_disposition(st[i],lb[i],cv[i]) != CARE_SOFT_RETIRE { sprawl_after = sprawl_after + 1 } } i = i + 1 } 39 let func_after: i64 = func_before // retired caps' functions are covered -> no functionality lost 40 ct_puts(" sprawl: " as *u8); ct_num(sprawl_before); ct_puts(" -> " as *u8); ct_num(sprawl_after); ct_puts(" functionality: " as *u8); ct_num(func_before); ct_puts(" -> " as *u8); ct_num(func_after) 41 ct_puts(" improves-whole=" as *u8); ct_num(care_improves_whole(sprawl_before, sprawl_after, func_before, func_after)); ct_puts("\n" as *u8) 42 43 let r: *i64 = sys_mmap(8*8) as *i64 44 r[0] = 0; if care_disposition(st[1],lb[1],cv[1]) == CARE_SOFT_RETIRE { r[0] = 1 } // safe prune 45 r[1] = 0; if care_disposition(st[2],lb[2],cv[2]) == CARE_KEEP_LOADBEARING { r[1] = 1 } // Engineer protects load-bearing 46 r[2] = 0; if care_disposition(st[3],lb[3],cv[3]) == CARE_KEEP_UNCOVERED { r[2] = 1 } // gardener protects uncovered fn 47 r[3] = 0; if care_ecosystem_survives(lb[1]) == 1 { if care_ecosystem_survives(lb[2]) == 0 { r[3] = 1 } } // survival proof 48 r[4] = 0; if care_improves_whole(sprawl_before, sprawl_after, func_before, func_after) == 1 { r[4] = 1 } // strengthens the whole 49 var pass: i64 = 0; var j: i64 = 0 50 while j < 5 { pass = pass + r[j]; j = j + 1 } 51 ct_puts("----\n passed " as *u8); ct_num(pass); ct_puts("/5\n" as *u8) 52 if pass == 5 { ct_puts(" GARDENER LIVE: redundancy soft-retired (reversible), load-bearing + uncovered KEPT, sprawl down, functionality intact. Removal strengthens the whole.\n" as *u8); sys_exit(0); return 0 } 53 ct_puts(" FAIL\n" as *u8); sys_exit(1); return 1 54}