code wiki / _hdl_build / nx_vsretract.nx
nx_vsretract.nx source
↩ module page · 116 lines · 5329 B
1// nx_vsretract.nx -- withdraw a comparative claim that turned out to be wrong.
2//
3// argv: <domain> <scope> <reason>
4//
5// Appends a VSRETRACT line to the same ledger nx_vsbest writes. nx_vsroll_lib walks that ledger in
6// order, so a retraction clears the (domain, scope) back to UNMEASURED, and a later nx_vsbest claim
7// after a clean re-run legitimately re-establishes it.
8//
9// WHY THIS EXISTS: I admitted search|latency=behind on a benchmark that had starved our own engine
10// (the harness held a 892MB FTS5 index it had just spent 483s building, and I timed our search under
11// that contention). Re-timed clean, our search answers in 1-6ms. The claim was wrong and there was
12// NO WAY TO WITHDRAW IT -- nx_vsbest only appends. Five routes to a fabricated WIN were closed by
13// construction; there was no route to correct an honest MISTAKE.
14// ★★★★★AN AXIS THAT CANNOT BE UN-CLAIMED CAN BE POISONED BY EXACTLY ONE BAD MEASUREMENT.
15//
16// RULE 13: this DELETES NOTHING. The original claim stays in the log as history -- the retraction is
17// a later row that supersedes it. A reader can still see what was claimed, when, and why it was
18// withdrawn, which is the whole point of an append-only ledger.
19//
20// A REASON IS MANDATORY. A retraction with no stated cause is indistinguishable from someone quietly
21// deleting an inconvenient loss, and this axis exists to make that impossible.
22// license_tier: ORIGINAL No hw writes (Rule 26).
23import "nx_vsbest_lib.nx"
24
25const VX_LOG: *u8 = "knowledge/status/vsbest_claims.log"
26const VX_MODE: i64 = 0x1a4
27const VX_LINE_CAP: i64 = 2048
28const VX_MIN_REASON: i64 = 24
29
30func vx_apps(b: *u8, cap: i64, o: i64, s: *u8) -> i64 {
31 if o < 0 { return 0 - 1 }
32 var n: i64 = 0
33 while s[n] != (0 as u8) { n = n + 1 }
34 if o + n > cap { return 0 - 1 }
35 var p: i64 = o
36 var i: i64 = 0
37 while i < n { b[p] = s[i]; p = p + 1; i = i + 1 }
38 return p
39}
40
41func vx_appn(b: *u8, cap: i64, o: i64, v: i64) -> i64 {
42 if o < 0 { return 0 - 1 }
43 let t: *u8 = sys_mmap(32)
44 var m: i64 = v
45 var k: i64 = 0
46 if m == 0 { t[0] = 48 as u8; k = 1 }
47 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
48 if o + k > cap { return 0 - 1 }
49 var p: i64 = o
50 var i: i64 = 0
51 while i < k { b[p] = t[k - 1 - i]; p = p + 1; i = i + 1 }
52 return p
53}
54
55func main(argc: i64, argv: *i64) -> i64 {
56 if argc < 4 {
57 _p("usage: nx_vsretract <domain> <scope> <reason>\n" as *u8)
58 _p(" withdraws a comparative claim. The original row STAYS in the ledger as history;\n" as *u8)
59 _p(" this appends a superseding VSRETRACT row. A reason of real substance is REQUIRED.\n" as *u8)
60 sys_exit(1)
61 return 1
62 }
63 let domain: *u8 = argv[1] as *u8
64 let scope: *u8 = argv[2] as *u8
65 let reason: *u8 = argv[3] as *u8
66
67 _p("=== NX-VSRETRACT: withdraw a comparative claim ===\n" as *u8)
68 if domain[0] == (0 as u8) { _p("REFUSED: empty domain\n" as *u8); sys_exit(2); return 2 }
69 if scope[0] == (0 as u8) { _p("REFUSED: empty scope\n" as *u8); sys_exit(2); return 2 }
70 if el_len(reason) < VX_MIN_REASON {
71 _p("REFUSED: a retraction needs a REASON, not a shrug -- state what was wrong with the\n" as *u8)
72 _p(" measurement. Without one this is indistinguishable from deleting an inconvenient loss.\n" as *u8)
73 sys_exit(3)
74 return 3
75 }
76
77 // The claim being withdrawn must actually exist, or this is noise.
78 let szp: *i64 = sys_mmap(16) as *i64
79 let cb: *u8 = ss_readall(VX_LOG, szp)
80 var cn: i64 = szp[0]
81 if cn < 0 { cn = 0 }
82 let dt: *u8 = sys_mmap(256)
83 var o: i64 = ss_cat(dt, 0, " domain=" as *u8)
84 o = ss_cat(dt, o, domain); dt[o] = 32 as u8; dt[o+1] = 0 as u8
85 if cn == 0 {
86 _p("REFUSED: the claims ledger is empty -- nothing to retract\n" as *u8)
87 sys_exit(4)
88 return 4
89 }
90 if el_contains(cb, cn, dt) == 0 {
91 _p("REFUSED: no claim recorded for domain=" as *u8); _p(domain)
92 _p(" -- retracting something never claimed would itself be a false record\n" as *u8)
93 sys_exit(4)
94 return 4
95 }
96
97 let fd: i64 = sys_openat_append(VX_LOG, VX_MODE)
98 if fd < 0 { _p("NX-VSRETRACT RED reason=log-unwritable\n" as *u8); sys_exit(5); return 5 }
99 let lb: *u8 = sys_mmap(VX_LINE_CAP)
100 var q: i64 = vx_apps(lb, VX_LINE_CAP, 0, "VSRETRACT epoch=" as *u8)
101 q = vx_appn(lb, VX_LINE_CAP, q, sys_now_realtime_sec())
102 q = vx_apps(lb, VX_LINE_CAP, q, " domain=" as *u8); q = vx_apps(lb, VX_LINE_CAP, q, domain)
103 q = vx_apps(lb, VX_LINE_CAP, q, " scope=" as *u8); q = vx_apps(lb, VX_LINE_CAP, q, scope)
104 q = vx_apps(lb, VX_LINE_CAP, q, " reason=" as *u8); q = vx_apps(lb, VX_LINE_CAP, q, reason)
105 q = vx_apps(lb, VX_LINE_CAP, q, "\n" as *u8)
106 if q < 0 { sys_close(fd); _p("NX-VSRETRACT RED reason=line-too-long\n" as *u8); sys_exit(6); return 6 }
107 sys_write(fd, lb, q)
108 sys_close(fd)
109
110 _p(" domain=" as *u8); _p(domain); _p(" scope=" as *u8); _p(scope); _p("\n" as *u8)
111 _p(" RETRACTED -- this scope reads UNMEASURED again. The original claim remains in the ledger\n" as *u8)
112 _p(" as history (rule 13); a clean re-run may claim it again.\n" as *u8)
113 _p("NX-VSRETRACT verdict=GREEN (claim withdrawn)\n" as *u8)
114 sys_exit(0)
115 return 0
116}