code wiki / _hdl_build / nx_doctor_apply_gate.nx

nx_doctor_apply_gate.nx source

↩ module page · 67 lines · 4431 B

1// nx_doctor_apply_gate.nx -- SOVEREIGN gate for the Doctor's graceful-apply core. 2// T1 APPLY -- region present exactly once -> applied, file rewritten correctly. 3// T2 CONFLICT -- region absent (already-applied / changed by another) -> REFUSED, file UNCHANGED (no clobber). 4// T3 AMBIGUOUS -- region matches >1 -> REFUSED, file UNCHANGED (won't guess). 5// T4 WITHOUT-COMPETITION (the point) -- a concurrent edit ELSEWHERE in the file is PRESERVED while my 6// non-overlapping change still applies against the CURRENT file = a clean merge, no clobber, no race-loss. 7// license_tier: ORIGINAL 8import "nx_syscalls.nx" 9import "nx_doctor_apply.nx" // doc_apply_mem / da_read / DA_* / da_slen 10 11const GT_TGT: *u8 = "/tmp/da_gate_target" 12 13func gt_uw(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 14func gt_wn(v: i64) -> i64 { let b: *u8 = sys_mmap(24); var m: i64 = v; var k: i64 = 0; if m==0 {b[0]=48 as u8;k=1} let t: *u8=sys_mmap(24); while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var o: *u8=sys_mmap(24); var w: i64=0; var q: i64=k-1; while q>=0{o[w]=t[q];w=w+1;q=q-1} sys_write(1,o,w); return 0 } 15func gt_write(path: *u8, s: *u8, len: i64) -> i64 { let fd: i64 = sys_openat_wr(path, 0x1a4); if fd < 0 { return 0-1 } sys_write(fd, s, len); sys_close(fd); return 0 } 16// read GT_TGT and compare to expected[0..elen). 1 = equal. 17func gt_file_eq(path: *u8, expected: *u8, elen: i64) -> i64 { 18 let buf: *u8 = sys_mmap(DA_CAP); let n: i64 = da_read(path, buf, DA_CAP) 19 if n != elen { return 0 } 20 var i: i64 = 0; while i < elen { if buf[i] != expected[i] { return 0 } i = i + 1 } 21 return 1 22} 23 24func main(argc: i64, argv: *i64) -> i64 { 25 var pass: i64 = 0; var total: i64 = 0 26 27 // ---- T1: clean apply (region present once) ---- 28 gt_write(GT_TGT, "hello FOO world" as *u8, 15) 29 let c1: i64 = doc_apply_mem(GT_TGT, "FOO" as *u8, 3, "BAR" as *u8, 3) 30 total = total + 1 31 var ok1: i64 = 0 32 if c1 == DA_APPLIED { if gt_file_eq(GT_TGT, "hello BAR world" as *u8, 15) == 1 { ok1 = 1 } } 33 if ok1 == 1 { pass = pass + 1; gt_uw("[PASS] " as *u8) } else { gt_uw("[FAIL] " as *u8) } 34 gt_uw("T1 apply: code=" as *u8); gt_wn(c1); gt_uw(" file=>'hello BAR world' (want APPLIED=0)\n" as *u8) 35 36 // ---- T2: conflict / no-match -> refuse, no clobber (file still 'hello BAR world') ---- 37 let c2: i64 = doc_apply_mem(GT_TGT, "FOO" as *u8, 3, "BAR" as *u8, 3) 38 total = total + 1 39 var ok2: i64 = 0 40 if c2 == DA_CONFLICT { if gt_file_eq(GT_TGT, "hello BAR world" as *u8, 15) == 1 { ok2 = 1 } } 41 if ok2 == 1 { pass = pass + 1; gt_uw("[PASS] " as *u8) } else { gt_uw("[FAIL] " as *u8) } 42 gt_uw("T2 conflict(no-match): code=" as *u8); gt_wn(c2); gt_uw(" file UNCHANGED (want CONFLICT=1, no clobber)\n" as *u8) 43 44 // ---- T3: ambiguous (>1 match) -> refuse, no clobber ---- 45 gt_write(GT_TGT, "FOO and FOO" as *u8, 11) 46 let c3: i64 = doc_apply_mem(GT_TGT, "FOO" as *u8, 3, "BAR" as *u8, 3) 47 total = total + 1 48 var ok3: i64 = 0 49 if c3 == DA_AMBIG { if gt_file_eq(GT_TGT, "FOO and FOO" as *u8, 11) == 1 { ok3 = 1 } } 50 if ok3 == 1 { pass = pass + 1; gt_uw("[PASS] " as *u8) } else { gt_uw("[FAIL] " as *u8) } 51 gt_uw("T3 ambiguous(>1): code=" as *u8); gt_wn(c3); gt_uw(" file UNCHANGED (want AMBIGUOUS=2, no clobber)\n" as *u8) 52 53 // ---- T4: WITHOUT-COMPETITION. base had 'AAA mid FOO end'; a concurrent edit elsewhere made it 54 // 'QQQ mid FOO end'; my non-overlapping FOO->BAR must apply against CURRENT, preserving QQQ. ---- 55 gt_write(GT_TGT, "AAA mid FOO end" as *u8, 15) // the base I "read" 56 gt_write(GT_TGT, "QQQ mid FOO end" as *u8, 15) // another workstream's concurrent edit (AAA->QQQ) 57 let c4: i64 = doc_apply_mem(GT_TGT, "FOO" as *u8, 3, "BAR" as *u8, 3) 58 total = total + 1 59 var ok4: i64 = 0 60 if c4 == DA_APPLIED { if gt_file_eq(GT_TGT, "QQQ mid BAR end" as *u8, 15) == 1 { ok4 = 1 } } 61 if ok4 == 1 { pass = pass + 1; gt_uw("[PASS] " as *u8) } else { gt_uw("[FAIL] " as *u8) } 62 gt_uw("T4 without-competition: code=" as *u8); gt_wn(c4); gt_uw(" file=>'QQQ mid BAR end' (concurrent QQQ PRESERVED + my FOO->BAR merged; want APPLIED=0)\n" as *u8) 63 64 gt_uw("=== nx_doctor_apply_gate " as *u8); gt_wn(pass); gt_uw("/" as *u8); gt_wn(total) 65 if pass == total { gt_uw(" GREEN\n" as *u8); return 0 } 66 gt_uw(" RED\n" as *u8); return 1 67}