nx_comparewatch_lib.nx source
↩ module page · 154 lines · 6472 B
1// nx_comparewatch_lib.nx -- PURE parsers for the /compare watch-plane census (2026-08-29).
2//
3// WHY THIS EXISTS. The question "how far is /compare from 100 percent?" had NO sovereign answer: the
4// only correct count ever taken (945 rows = 826 OPEN + 119 LANDED, 2026-08-29) was a HAND TALLY of a
5// fully-read plane dump, because no organ publishes an OPEN/LANDED count and nx_shelltool grep is
6// literal-substring only -- structurally unable to anchor on the status FIELD. That matters because at
7// least one LANDED row (legalpractice_of_import) carries the literal token "OPEN:" inside its label
8// prose, so any substring count is inflated by construction. ★A NUMBER THAT CAN ONLY BE PRODUCED BY A
9// HAND TALLY CANNOT RIDE A BEAT, CANNOT BE RATCHETED, AND IS RE-DERIVED FROM SCRATCH BY EVERY SEAT
10// THAT NEEDS IT.
11//
12// LIB+PROGRAM SPLIT ON PURPOSE: everything here is a pure function of caller-owned buffers -- no
13// filesystem, no clock, no seg-store -- so nx_comparewatch_census_gate composes THESE EXACT functions
14// in-process on planted fixtures, and a mutation to any rule is reachable by the gate.
15//
16// ROW GRAMMAR (the comparewatch- plane, written by nx_compare_regen):
17// id <TAB> domain <TAB> label <TAB> organ-path <TAB> symbol <TAB> status
18// STATUS IS THE LAST TAB-DELIMITED FIELD AND IS COMPARED EXACT-LENGTH: "OPEN" and "LANDED" only.
19// Any other final field is CW_OTHER -- an unrecognised status must NEVER fall into a known bucket,
20// because the bucket it lands in becomes the number somebody plans against. A row with no tab at all
21// is CW_MALFORMED, its own bucket for the same reason.
22// license_tier: ORIGINAL No hw writes (Rule 26).
23import "nx_syscalls.nx"
24
25const CW_UNSET: i64 = 0
26const CW_OPEN: i64 = 1
27const CW_LANDED: i64 = 2
28const CW_OTHER: i64 = 3
29const CW_MALFORMED: i64 = 4
30const CW_TOMB: i64 = 5
31
32// key parse sentinels: "q:n" is the plane's own declared-count key.
33const CW_KEY_COUNT: i64 = 0 - 2
34const CW_KEY_FOREIGN: i64 = 0 - 1
35
36const CW_TABC: i64 = 9
37const CW_CR: i64 = 13
38const CW_NL: i64 = 10
39
40// final tab-delimited field of v[0..n), trailing CR/NL trimmed. out2[0]=offset out2[1]=len.
41// Returns 1, or 0 when the row carries no tab at all (malformed -- there IS no final field).
42func cw_lastfield(v: *u8, n: i64, out2: *i64) -> i64 {
43 var last: i64 = 0 - 1
44 var i: i64 = 0
45 while i < n { if (v[i] as i64) == CW_TABC { last = i } i = i + 1 }
46 if last < 0 { return 0 }
47 var s: i64 = last + 1
48 var e: i64 = n
49 var trim: i64 = 1
50 while trim == 1 {
51 trim = 0
52 if e > s {
53 let c: i64 = v[e - 1] as i64
54 if c == CW_CR { e = e - 1; trim = 1 } else { if c == CW_NL { e = e - 1; trim = 1 } }
55 }
56 }
57 out2[0] = s
58 out2[1] = e - s
59 return 1
60}
61
62// exact-length compare of v[o..o+l) against NUL-terminated lit. Exactness is LOAD-BEARING: "OPENX",
63// "OPEN " and a label that merely CONTAINS the token must all fail, and length-first makes that so.
64func cw_field_eq(v: *u8, o: i64, l: i64, lit: *u8) -> i64 {
65 var ll: i64 = 0
66 while lit[ll] != (0 as u8) { ll = ll + 1 }
67 if l != ll { return 0 }
68 var i: i64 = 0
69 while i < l { if v[o + i] != lit[i] { return 0 } i = i + 1 }
70 return 1
71}
72
73// status of one row VALUE. POSITIONAL (last field), never substring -- see the header for the measured
74// row that makes the difference between a right and an inflated count.
75func cw_status_code(v: *u8, n: i64) -> i64 {
76 let o2: *i64 = sys_mmap(16) as *i64
77 if cw_lastfield(v, n, o2) == 0 { return CW_MALFORMED }
78 if cw_field_eq(v, o2[0], o2[1], "OPEN" as *u8) == 1 { return CW_OPEN }
79 if cw_field_eq(v, o2[0], o2[1], "LANDED" as *u8) == 1 { return CW_LANDED }
80 return CW_OTHER
81}
82
83// 0-based tab field `want` of v[0..n). out2 offset/len. 1 found, 0 row has too few fields.
84func cw_field(v: *u8, n: i64, want: i64, out2: *i64) -> i64 {
85 var f: i64 = 0
86 var s: i64 = 0
87 var i: i64 = 0
88 while i <= n {
89 var atend: i64 = 0
90 if i == n { atend = 1 } else { if (v[i] as i64) == CW_TABC { atend = 1 } }
91 if atend == 1 {
92 if f == want { out2[0] = s; out2[1] = i - s; return 1 }
93 f = f + 1
94 s = i + 1
95 }
96 i = i + 1
97 }
98 return 0
99}
100
101// seg-store key -> row index. "q:n" -> CW_KEY_COUNT (the plane's declared total). "q:<digits>" -> the
102// index, digits STRICT (any other byte refuses). Everything else -> CW_KEY_FOREIGN, counted and
103// reported by the caller, never silently folded into the row set.
104func cw_key_idx(k: *u8, kl: i64) -> i64 {
105 if kl < 3 { return CW_KEY_FOREIGN }
106 if (k[0] as i64) != 113 { return CW_KEY_FOREIGN } // 'q'
107 if (k[1] as i64) != 58 { return CW_KEY_FOREIGN } // ':'
108 if kl == 3 { if (k[2] as i64) == 110 { return CW_KEY_COUNT } } // 'n'
109 var v: i64 = 0
110 var i: i64 = 2
111 while i < kl {
112 let c: i64 = k[i] as i64
113 if c < 48 { return CW_KEY_FOREIGN }
114 if c > 57 { return CW_KEY_FOREIGN }
115 v = v * 10 + (c - 48)
116 i = i + 1
117 }
118 return v
119}
120
121// strict decimal of v[0..n) (the q:n VALUE). -1 on any non-digit or empty -- a count that cannot be
122// parsed must be UNAVAILABLE, never zero.
123func cw_parse_count(v: *u8, n: i64) -> i64 {
124 if n <= 0 { return 0 - 1 }
125 var val: i64 = 0
126 var i: i64 = 0
127 while i < n {
128 let c: i64 = v[i] as i64
129 if c == CW_CR { return val }
130 if c == CW_NL { return val }
131 if c < 48 { return 0 - 1 }
132 if c > 57 { return 0 - 1 }
133 val = val * 10 + (c - 48)
134 i = i + 1
135 }
136 return val
137}
138
139// LAST-VERSION-WINS APPLY. The cursor streams records chronologically, so overwriting the slot IS the
140// versioning rule ss_get applies -- and putting the overwrite HERE makes that semantic gate-reachable
141// instead of an unstated array assignment. Copies the domain (truncated to domw-1, NUL-terminated)
142// because the cursor's value pointer is only valid until the next record.
143// Returns 1 applied, 0 refused (idx out of range -- the caller counts and REPORTS the refusal).
144func cw_apply(stArr: *i64, domBuf: *u8, maxrows: i64, domw: i64, idx: i64, st: i64, d: *u8, dlen: i64) -> i64 {
145 if idx < 0 { return 0 }
146 if idx >= maxrows { return 0 }
147 stArr[idx] = st
148 var n: i64 = dlen
149 if n > domw - 1 { n = domw - 1 }
150 var i: i64 = 0
151 while i < n { domBuf[idx * domw + i] = d[i]; i = i + 1 }
152 domBuf[idx * domw + n] = 0 as u8
153 return 1
154}