code wiki / _hdl_build / nx_geo_census.nx
nx_geo_census.nx source
↩ module page · 165 lines · 8829 B
1// nx_geo_census.nx -- GATE + CENSUS for GEO-004: the MEASURED-exceed scoreboard (no-wave). Twin of
2// nx_competitive_census, for geo. Seeds an incumbent feature refset (Google-Maps/PostGIS/Turf/S2/H3
3// killer features) into the SOVEREIGN STORE (knowledge/store/geo-*, NOT tsv), then for each feature
4// marks PRESENT *only* if a real Nishi gate log is GREEN -- never asserted. Computes reach permil.
5// Honest by design: today we are BEHIND (a handful present of ~28); the number RATCHETS UP as each
6// GEO rung lands. This is how "superior to google" is proven, not claimed.
7//
8// The GATE proves the COUNTING LOGIC (a known-GREEN log -> present; "-" or missing/non-green -> not
9// present), so the reach figure is trustworthy. Evidence -> knowledge/status/geo_census.log.
10// license_tier: ORIGINAL
11import "nx_seg_store.nx"
12import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc)
13import "nx_syscalls.nx"
14const GC_MAGIC_262144: i64 = 262144
15
16const GC_PREFIX: *u8 = "knowledge/store/geo-"
17const GC_KEY: *u8 = "geocensus:refset"
18const GC_LOG: *u8 = "knowledge/status/geo_census.log"
19
20// the incumbent benchmark set: feature<tab>incumbent<tab>nishi_gate_log (or "-" if none yet).
21// PRESENT rows point at a real gate log; the census re-checks each is actually GREEN.
22const GC_REFSET: *u8 = "geofence-polygon turf/shapely/postgis knowledge/status/geo_gate.log\ngeofence-radius turf/postgis knowledge/status/geo_gate.log\nbbox-prefilter rtree/postgis knowledge/status/geo_gate.log\ngeohash-index geohash/redis-geo knowledge/status/geohash_gate.log\nproximity-prefix geohash knowledge/status/geohash_gate.log\ndistance-meters turf/postgis/google knowledge/status/geo_distance.log\nhaversine-precise turf/postgis knowledge/status/geo_haversine.log\nbearing-heading turf knowledge/status/geo_query.log\nnearest-knn postgis/turf knowledge/status/geo_query.log\ngeocoding google/nominatim knowledge/status/geo_geocode.log\nreverse-geocoding google/nominatim knowledge/status/geo_geocode.log\nrouting-shortest-path google/osrm/valhalla knowledge/status/geo_route.log\nisochrones google/valhalla knowledge/status/geo_route.log\nvector-tiles mapbox/google knowledge/status/geo_tiles.log\nraster-render mapbox/leaflet knowledge/status/geo_raster.log\ns2-cells google-s2 knowledge/status/geo_s2.log\nh3-hex uber-h3 knowledge/status/geo_h3.log\npolygon-area turf/postgis knowledge/status/geo_area.log\npolygon-centroid turf/postgis knowledge/status/geo_centroid.log\nconvex-hull turf/postgis knowledge/status/geo_hull.log\nline-simplify turf/mapbox knowledge/status/geo_simplify.log\npolygon-intersection turf/postgis knowledge/status/geo_clip.log\npolygon-union turf/postgis knowledge/status/geo_union.log\nbuffer-offset turf/postgis knowledge/status/geo_buffer.log\nspatial-join postgis knowledge/status/geo_join.log\nline-of-sight cesium knowledge/status/geo_los.log\nplaces-search google/osm knowledge/status/geo_places.log\nmap-matching osrm/google knowledge/status/geo_match.log\n"
23
24func gc_w(fd: i64, s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(fd, s, n); return 0 }
25// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer
26// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the
27// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls).
28// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign.
29func gc_wn(fd: i64, v: i64) -> i64 { nxi_fd(fd, v); return 0 }
30func gc_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
31
32func gc_read(path: *u8, buf: *u8, cap: i64) -> i64 {
33 let fd: i64 = sys_openat_rd(path)
34 if fd < 0 { return 0 }
35 var n: i64 = 0
36 var r: i64 = sys_read(fd, buf, cap - 1)
37 while r > 0 { n = n + r; if n >= cap - 1 { r = 0 } else { r = sys_read(fd, buf + n, cap - 1 - n) } }
38 sys_close(fd)
39 return n
40}
41
42// substring present in buf[0..n]?
43func gc_has(buf: *u8, n: i64, needle: *u8) -> i64 {
44 let nl: i64 = gc_len(needle)
45 if nl == 0 { return 1 }
46 var i: i64 = 0
47 while i + nl <= n {
48 var k: i64 = 0
49 var mt: i64 = 1
50 while k < nl { if buf[i + k] != needle[k] { mt = 0; k = nl } else { k = k + 1 } }
51 if mt == 1 { return 1 }
52 i = i + 1
53 }
54 return 0
55}
56
57// is a gate-log path GREEN? path "-" (length 1) -> 0. else read it + scan "verdict=GREEN".
58func gc_gate_green(path: *u8, plen: i64) -> i64 {
59 if plen <= 1 { return 0 }
60 let pb: *u8 = sys_mmap(plen + 1)
61 var i: i64 = 0
62 while i < plen { pb[i] = path[i]; i = i + 1 }
63 pb[plen] = 0 as u8
64 let buf: *u8 = sys_mmap(GC_MAGIC_262144)
65 let n: i64 = gc_read(pb, buf, GC_MAGIC_262144)
66 if n <= 0 { return 0 }
67 return gc_has(buf, n, "verdict=GREEN" as *u8)
68}
69
70// store value byte-equals lit?
71func gc_streq_store(val: *u8, vlen: i64) -> i64 {
72 let pq: *i64 = sys_mmap(16) as *i64
73 let lq: *i64 = sys_mmap(16) as *i64
74 if ss_get(GC_PREFIX, GC_KEY, pq, lq) != 1 { return 0 }
75 if lq[0] != vlen { return 0 }
76 let b: *u8 = pq[0] as *u8
77 var i: i64 = 0
78 while i < vlen { if b[i] != val[i] { return 0 } i = i + 1 }
79 return 1
80}
81
82// count PRESENT + total over the refset bytes: each line feature<tab>incumbent<tab>gatelog; PRESENT
83// if the gatelog (field 2) is GREEN. writes total to *outtot; returns present count.
84func gc_measure(buf: *u8, n: i64, outtot: *i64) -> i64 {
85 var present: i64 = 0
86 var total: i64 = 0
87 var i: i64 = 0
88 while i < n {
89 var le: i64 = i
90 var go: i64 = 1
91 while go == 1 { if le >= n { go = 0 } else { if buf[le] == (10 as u8) { go = 0 } else { le = le + 1 } } }
92 if le > i {
93 // find 3rd field (after 2 tabs) within [i, le)
94 var f: i64 = 0
95 var p: i64 = i
96 while p < le { if buf[p] == (9 as u8) { f = f + 1; if f == 2 { p = le } else { p = p + 1 } } else { p = p + 1 } }
97 // p is now at the tab before field2 (or le); advance past it to field2 start
98 var gstart: i64 = i
99 f = 0
100 var q: i64 = i
101 while q < le { if buf[q] == (9 as u8) { f = f + 1; if f == 2 { gstart = q + 1; q = le } else { q = q + 1 } } else { q = q + 1 } }
102 let glen: i64 = le - gstart
103 total = total + 1
104 if gc_gate_green((buf + gstart) as *u8, glen) == 1 { present = present + 1 }
105 }
106 i = le + 1
107 }
108 outtot[0] = total
109 return present
110}
111
112func gc_emit(fd: i64, present: i64, total: i64, reach: i64, ok: i64) -> i64 {
113 gc_w(fd, "GEOCENSUS authored=organ source=store measured-not-asserted present=" as *u8); gc_wn(fd, present)
114 gc_w(fd, " total=" as *u8); gc_wn(fd, total)
115 gc_w(fd, " reach_permil=" as *u8); gc_wn(fd, reach)
116 if reach >= 500 { gc_w(fd, " standing=AHEAD" as *u8) } else { gc_w(fd, " standing=BEHIND(honest-baseline-ratchets-up)" as *u8) }
117 if ok == 1 { gc_w(fd, " verdict=GREEN\n" as *u8) } else { gc_w(fd, " verdict=RED\n" as *u8) }
118 return 0
119}
120
121func main() -> i64 {
122 let rlen: i64 = gc_len(GC_REFSET)
123 // seed refset to the sovereign store (idempotent: only if absent/changed).
124 if gc_streq_store(GC_REFSET, rlen) == 0 {
125 let segs: *i64 = sys_mmap(8 * 260) as *i64
126 let nseg: i64 = ss_manifest(GC_PREFIX, segs)
127 var segid: i64 = 1
128 if nseg >= 0 { segid = 1 + nseg }
129 let w: *i64 = ss_begin()
130 ss_add(w, 1, GC_KEY, GC_REFSET, rlen)
131 ss_commit(GC_PREFIX, w, segid)
132 }
133
134 // read refset back from the store + measure.
135 let pq: *i64 = sys_mmap(16) as *i64
136 let lq: *i64 = sys_mmap(16) as *i64
137 var present: i64 = 0
138 var total: i64 = 0
139 let tot: *i64 = sys_mmap(8) as *i64
140 if ss_get(GC_PREFIX, GC_KEY, pq, lq) == 1 {
141 present = gc_measure(pq[0] as *u8, lq[0], tot)
142 total = tot[0]
143 }
144 var reach: i64 = 0
145 if total > 0 { reach = present * 1000 / total }
146
147 // GATE: prove the counting LOGIC (a known-GREEN log -> present; "-" and a missing log -> not).
148 let ctl_green: i64 = gc_gate_green("knowledge/status/geo_gate.log" as *u8, 30) // expect 1
149 let ctl_dash: i64 = gc_gate_green("-" as *u8, 1) // expect 0
150 let ctl_miss: i64 = gc_gate_green("knowledge/status/_nope_nope.log" as *u8, 31) // expect 0
151
152 var ok: i64 = 1
153 if ctl_green != 1 { ok = 0 }
154 if ctl_dash != 0 { ok = 0 }
155 if ctl_miss != 0 { ok = 0 }
156 if total != 28 { ok = 0 }
157 if present < 6 { ok = 0 } // the 6 gated rungs (geofence x3 + geohash x2 + distance) at minimum
158
159 gc_emit(1, present, total, reach, ok)
160 let lf: i64 = sys_openat_append(GC_LOG, 420)
161 if lf >= 0 { gc_emit(lf, present, total, reach, ok); sys_close(lf) }
162
163 if ok == 1 { return 0 }
164 return 1
165}