nx_gendiff.nx source
↩ module page · 177 lines · 8465 B
1// nx_gendiff.nx -- the GENERATION DIFF that nx_plane_repair names and does not have.
2//
3// WHY. knowledge/store/debt- reads declared_qn=3696 beyond_qn=1: ONE key is reachable past the declared
4// count, and that single key fails every nx_debt add/eat CLOSED, because add/eat re-seed the whole plane
5// from the loaded buffer and committing would orphan it. nx_plane_repair refuses for exactly the right
6// reason -- "reseeding would revive eaten rows and duplicate live ones -- that needs a generation diff,
7// not a reconcile." Both organs failing closed is the system working. What is missing is the ability to
8// SAY WHAT THE EXTRA ROW ACTUALLY IS.
9// (STAR)A REFUSAL THAT NAMES THE MISSING CAPABILITY IS A SPECIFICATION -- BUILD THE THING IT NAMED, DO NOT
10// WEAKEN THE REFUSAL.
11//
12// THE AMBIGUITY, STATED PRECISELY. A key past q:n is one of three things, needing opposite actions:
13// SUPERSEDED byte-identical to a loaded row -> already represented, safe to leave behind
14// REVIVED an id an earlier eat deliberately closed -> must NOT come back
15// LOST-APPEND a DISTINCT row never counted -> real filed work; discarding it is silent data loss
16// The estate's loader documents only the SUPERSEDED story because it was reasoned about for a plane that
17// SHRANK. This plane GREW (3,621 -> 3,696 today), which makes LOST-APPEND the leading hypothesis and the
18// exact case where reconciling q:n downward destroys evidence.
19// (STAR)A STORE'S DOCUMENTED FAILURE MODE IS THE ONE ITS AUTHOR HAPPENED TO MEET; THE OPPOSITE DIRECTION
20// GETS THE SAME SYMPTOM AND THE OPPOSITE REMEDY.
21//
22// THIS ORGAN WRITES NOTHING. It classifies and prints; deciding is a separate deliberate act.
23// license_tier: ORIGINAL No hw writes. Read-only.
24import "nx_syscalls.nx"
25import "nx_store_seed_lib.nx"
26import "nx_seg_store.nx"
27
28const GD_CAP: i64 = 16777216
29const GD_PROBE: i64 = 4096
30const GD_MISSRUN: i64 = 512
31const GD_SHOW: i64 = 240
32
33// A DIAGNOSTIC THAT CAN ONLY LOOK AT ONE PLANE IS A SCRIPT, NOT AN INSTRUMENT. v1 hardcoded the debt
34// plane, which meant it could never be pointed at a plane whose generation split I already understood --
35// and therefore could never be shown to FIRE. The estate has ~1,100 planes; the argument costs one line.
36// (STAR)A GREEN GATE THAT HAS ONLY EVER SEEN CORRECT DATA HAS NOT BEEN SHOWN TO FIRE, AND AN INSTRUMENT
37// WELDED TO A SINGLE SUBJECT CAN NEVER BE SHOWN ANYTHING ELSE.
38func main(argc: i64, argv: *i64) -> i64 {
39 var pfx: *u8 = "knowledge/store/debt-" as *u8
40 if argc > 1 { pfx = argv[1] as *u8 }
41 gd_puts("plane="); gd_puts(pfx); gd_puts("\n")
42 gd_puts("=== nx_gendiff: classify every key past the declared count. WRITES NOTHING. ===\n")
43 let buf: *u8 = sys_mmap(GD_CAP)
44 let fl: *i64 = sys_mmap(64) as *i64
45 let n: i64 = sts_load_honest(pfx, buf, GD_CAP, fl)
46 gd_puts("declared_qn="); gd_putn(fl[0])
47 gd_puts(" loaded_rows="); gd_putn(fl[1])
48 gd_puts(" beyond_qn="); gd_putn(fl[2])
49 gd_puts(" bytes="); gd_putn(n); gd_puts("\n")
50 if fl[2] <= 0 {
51 gd_puts("NX-GENDIFF passed 1/1 verdict=GREEN (nothing past the declared count)\n")
52 sys_exit(0); return 0
53 }
54 let hh: *i64 = ss_open_cached(pfx)
55 if (hh as i64) == 0 { gd_puts("NX-GENDIFF cannot open plane verdict=RED\n"); sys_exit(1); return 1 }
56 let key: *u8 = sys_mmap(256)
57 let pr: *i64 = sys_mmap(64) as *i64
58 let lr: *i64 = sys_mmap(64) as *i64
59 var superseded: i64 = 0
60 var needdec: i64 = 0
61 var found: i64 = 0
62 var miss: i64 = 0
63 var seq: i64 = fl[0]
64 let lim: i64 = fl[0] + GD_PROBE
65 while seq < lim {
66 if miss < GD_MISSRUN {
67 gd_rowkey(seq, key)
68 let hit: i64 = ss_hget(hh, key, pr, lr)
69 if hit == 1 {
70 miss = 0
71 found = found + 1
72 let p: *u8 = pr[0] as *u8
73 let ln: i64 = lr[0]
74 let dupe: i64 = gd_line_present(buf, n, p, ln)
75 let idp: i64 = gd_id_present(buf, n, p, ln)
76 gd_puts("-- key "); gd_puts(key); gd_puts(" len="); gd_putn(ln); gd_puts("\n ")
77 var sh: i64 = ln
78 if sh > GD_SHOW { sh = GD_SHOW }
79 sys_write(1, p, sh)
80 gd_puts("\n")
81 if dupe == 1 {
82 superseded = superseded + 1
83 gd_puts(" CLASS=SUPERSEDED byte-identical to a loaded row -- already represented, safe to leave behind\n")
84 }
85 if dupe == 0 {
86 needdec = needdec + 1
87 if idp == 1 { gd_puts(" CLASS=OLDER-VERSION id IS live but bytes differ -- a prior version of a row since edited\n") }
88 if idp == 0 { gd_puts(" CLASS=DISTINCT-AND-NOT-LIVE a LOST APPEND never counted, or a row an eat closed. Check the eat record: never eaten = filed work that was written and never counted, and dropping it is silent data loss; eaten = raising q:n resurrects it.\n") }
89 if idp < 0 { gd_puts(" CLASS=UNPARSEABLE no first field -- do not act mechanically\n") }
90 }
91 }
92 if hit != 1 { miss = miss + 1 }
93 }
94 seq = seq + 1
95 }
96 gd_puts("\nfound_past_qn="); gd_putn(found)
97 gd_puts(" superseded="); gd_putn(superseded)
98 gd_puts(" needing_a_decision="); gd_putn(needdec); gd_puts("\n")
99 if found == 0 {
100 gd_puts("NX-GENDIFF the loader reports rows past the count but probing found NONE -- contradictory, verdict=RED\n")
101 sys_exit(1); return 1
102 }
103 if needdec == 0 {
104 gd_puts("NX-GENDIFF passed "); gd_putn(found); gd_puts("/"); gd_putn(found)
105 gd_puts(" verdict=GREEN (every key past the count is byte-identical to a live row: SAFE to reconcile q:n)\n")
106 sys_exit(0); return 0
107 }
108 gd_puts("NX-GENDIFF verdict=RED ("); gd_putn(needdec); gd_puts(" keys need a decision -- do NOT reconcile blindly)\n")
109 sys_exit(1); return 1
110}
111
112func gd_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
113func gd_putn(v: i64) -> i64 {
114 if v == 0 { sys_write(1, "0" as *u8, 1); return 0 }
115 var mv: i64 = v
116 if mv < 0 { sys_write(1, "-" as *u8, 1); mv = 0 - mv }
117 let t: *u8 = sys_mmap(32); var k: i64 = 0
118 while mv > 0 { t[k] = (0x30 + (mv - (mv/10)*10)) as u8; mv = mv/10; k = k + 1 }
119 while k > 0 { k = k - 1; sys_write(1, (((t as i64)+k) as *u8), 1) }
120 return 0
121}
122// Does the FIRST TAB-DELIMITED FIELD of p appear as the first field of any loaded line? Returns -1 when the
123// row has no first field at all -- which must never be reported the same as a clean "absent".
124func gd_id_present(buf: *u8, n: i64, p: *u8, ln: i64) -> i64 {
125 var flen: i64 = 0 - 1
126 var q: i64 = 0
127 while q < ln { if p[q] == (9 as u8) { flen = q; q = ln } else { q = q + 1 } }
128 if flen <= 0 { return 0 - 1 }
129 var i: i64 = 0
130 while i < n {
131 var e: i64 = i
132 var nl: i64 = 0 - 1
133 while e < n { if buf[e] == (10 as u8) { nl = e; e = n } else { e = e + 1 } }
134 var end: i64 = n
135 if nl >= 0 { end = nl }
136 if end - i > flen {
137 if buf[i+flen] == (9 as u8) {
138 var k: i64 = 0
139 var same: i64 = 1
140 while k < flen { if buf[i+k] != p[k] { same = 0; k = flen } else { k = k + 1 } }
141 if same == 1 { return 1 }
142 }
143 }
144 i = end + 1
145 }
146 return 0
147}
148func gd_line_present(buf: *u8, n: i64, p: *u8, ln: i64) -> i64 {
149 if ln <= 0 { return 0 }
150 var i: i64 = 0
151 while i < n {
152 var e: i64 = i
153 var nl: i64 = 0 - 1
154 while e < n { if buf[e] == (10 as u8) { nl = e; e = n } else { e = e + 1 } }
155 var end: i64 = n
156 if nl >= 0 { end = nl }
157 if end - i == ln {
158 var k: i64 = 0
159 var same: i64 = 1
160 while k < ln { if buf[i+k] != p[k] { same = 0; k = ln } else { k = k + 1 } }
161 if same == 1 { return 1 }
162 }
163 i = end + 1
164 }
165 return 0
166}
167func gd_rowkey(seq: i64, out: *u8) -> i64 {
168 out[0] = 113 as u8
169 out[1] = 58 as u8
170 var o: i64 = 2
171 if seq == 0 { out[o] = 0x30 as u8; out[o+1] = 0 as u8; return 0 }
172 var mv: i64 = seq
173 let t: *u8 = sys_mmap(32); var k: i64 = 0
174 while mv > 0 { t[k] = (0x30 + (mv - (mv/10)*10)) as u8; mv = mv/10; k = k + 1 }
175 while k > 0 { k = k - 1; out[o] = t[k]; o = o + 1 }
176 out[o] = 0 as u8
177 return 0
178}