nx_browser_census.nx source
↩ module page · 129 lines · 7345 B
1// nx_browser_census.nx -- SOVEREIGN browser-parity census scoreboard organ (MCP tool + API payload).
2// Operator directive 2026-07-23: "mcp and api and object oriented and agentic and workflow ready ... it needs
3// to scale." Replaces the one-off PowerShell -> static-JSON pipeline (the scattering root) with a data-driven
4// sovereign organ: sites + per-site results live in the SEG-STORE PLANE knowledge/store/browsercensus- (NOT
5// hardcoded, NOT a tsv per the sovereign-formats law) -> ADD SITES WITHOUT TOUCHING CODE = scales to N.
6// Reads the plane, parses each site RECORD, aggregates a SCOREBOARD (mean reading-recall vs Chrome, class
7// counts, mean height-ratio), emits structured JSON to stdout (the MCP + API payload) AND colocates it to
8// knowledge/status/browser_census.log (evidence). Fail-closed: absent/empty plane -> RED exit 1 (no scoreboard
9// without data). Row schema, 6 tab fields: site \t class \t reading_recall_pm \t ratio_pct \t chrome_h \t nishi_h.
10// reading_recall_pm/ratio_pct = "-" or non-digit means UNMEASURED (excluded from the mean, never a silent 0).
11// Workflow-ready: one plane row per site, one JSON record per site -> composable/fan-out. license_tier: ORIGINAL
12// expect_exit: 0
13import "nx_store_seed_lib.nx"
14import "nx_seg_store.nx"
15import "nx_syscalls.nx"
16const BC_MAGIC_4096: i64 = 4096
17
18const BC_PLANE_CAP: i64 = 1048576
19const BC_JSON_CAP: i64 = 262144
20
21// ---- emit helpers (single-responsibility string builders) ----
22func bc_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
23func bc_cat(o: *u8, at: i64, s: *u8) -> i64 { var i: i64=0; var a: i64=at; while s[i]!=(0 as u8){o[a]=s[i]; a=a+1; i=i+1} return a }
24func bc_catf(o: *u8, at: i64, p: *u8, n: i64) -> i64 { var i: i64=0; var a: i64=at; while i<n { o[a]=p[i]; a=a+1; i=i+1 } return a }
25func bc_catn(o: *u8, at: i64, v: i64) -> i64 {
26 var a: i64=at; var x: i64=v
27 if x<0 { o[a]=45 as u8; a=a+1; x=0-x }
28 let tm: *u8=sys_mmap(24); var k: i64=0
29 if x==0 { tm[0]=48 as u8; k=1 }
30 while x>0 { tm[k]=(48+x%10) as u8; x=x/10; k=k+1 }
31 var j: i64=0; while j<k { o[a]=tm[k-1-j]; a=a+1; j=j+1 }
32 return a
33}
34// does field p[0..n) contain zero-terminated sub? (no-break style: sentinel exits)
35func bc_field_has(p: *u8, n: i64, sub: *u8) -> i64 {
36 var sl: i64=0; while sub[sl]!=(0 as u8){sl=sl+1}
37 if sl==0 { return 0 }
38 var i: i64=0; var found: i64=0
39 while i+sl<=n {
40 var jj: i64=0; var ok: i64=1
41 while jj<sl { if p[i+jj]!=sub[jj] { ok=0; jj=sl } else { jj=jj+1 } }
42 if ok==1 { found=1; i=n } else { i=i+1 }
43 }
44 return found
45}
46// parse field p[0..n) as a non-negative int; -1 if the first byte is not a digit ("-" = UNMEASURED)
47func bc_field_int(p: *u8, n: i64) -> i64 {
48 if n<=0 { return 0-1 }
49 let c0: i64=p[0] as i64
50 if c0<48 { return 0-1 }
51 if c0>57 { return 0-1 }
52 var v: i64=0; var i: i64=0
53 while i<n { let c: i64=p[i] as i64; if c>=48 { if c<=57 { v=v*10+(c-48) } } i=i+1 }
54 return v
55}
56
57func main() -> i64 {
58 let mb: *u8=sys_mmap(BC_PLANE_CAP)
59 let mn: i64=sts_load("knowledge/store/browsercensus-\x00" as *u8, mb, BC_PLANE_CAP-BC_MAGIC_4096)
60 if mn<=0 {
61 bc_puts("BROWSER-CENSUS RED -- native plane knowledge/store/browsercensus- ABSENT or EMPTY (fail-closed, no scoreboard without data)\n" as *u8)
62 sys_exit(1)
63 return 1
64 }
65 let jb: *u8=sys_mmap(BC_JSON_CAP)
66 var j: i64=0
67 j=bc_cat(jb, j, "{\x22v\x22:1,\x22kind\x22:\x22browser-census\x22,\x22oracle\x22:\x22chrome-headless + Windows.Media.Ocr\x22,\x22sites\x22:[" as *u8)
68 // ---- parse the data-driven plane: one RECORD per site row (6 tab fields) ----
69 let fs: *i64=sys_mmap(64) as *i64
70 let fl: *i64=sys_mmap(64) as *i64
71 var nsites: i64=0; var content_ok: i64=0; var blank: i64=0; var fetch_empty: i64=0; var chrome_blocked: i64=0
72 var rsum: i64=0; var rcnt: i64=0; var ratsum: i64=0; var ratcnt: i64=0
73 var i: i64=0; var first: i64=1
74 while i<mn {
75 let ls: i64=i
76 var le: i64=ls; var scan: i64=1
77 while scan==1 { if le>=mn { scan=0 } else { if mb[le]==(10 as u8) { scan=0 } else { le=le+1 } } }
78 let lend: i64=le
79 i=le+1
80 let llen: i64=lend-ls
81 if llen>3 { if mb[ls]!=(35 as u8) {
82 var nf: i64=0; var p: i64=ls; var fstart: i64=ls
83 while p<lend {
84 if mb[p]==(9 as u8) { if nf<6 { fs[nf]=fstart; fl[nf]=p-fstart; nf=nf+1 } fstart=p+1 }
85 p=p+1
86 }
87 if nf<6 { fs[nf]=fstart; fl[nf]=lend-fstart; nf=nf+1 }
88 if nf>=2 {
89 // 0=site 1=class 2=reading_recall_pm 3=ratio_pct 4=chrome_h 5=nishi_h
90 let clsp: *u8=(mb as i64+fs[1]) as *u8
91 let clsn: i64=fl[1]
92 if bc_field_has(clsp, clsn, "CONTENT-OK\x00" as *u8)==1 { content_ok=content_ok+1 }
93 if bc_field_has(clsp, clsn, "BLANK\x00" as *u8)==1 { blank=blank+1 }
94 if bc_field_has(clsp, clsn, "FETCH-EMPTY\x00" as *u8)==1 { fetch_empty=fetch_empty+1 }
95 if bc_field_has(clsp, clsn, "CHROME-BLOCKED\x00" as *u8)==1 { chrome_blocked=chrome_blocked+1 }
96 var rec: i64=0-1; var rat: i64=0-1
97 if nf>=3 { rec=bc_field_int((mb as i64+fs[2]) as *u8, fl[2]) }
98 if nf>=4 { rat=bc_field_int((mb as i64+fs[3]) as *u8, fl[3]) }
99 if rec>=0 { rsum=rsum+rec; rcnt=rcnt+1 }
100 if rat>=0 { ratsum=ratsum+rat; ratcnt=ratcnt+1 }
101 nsites=nsites+1
102 if first==0 { j=bc_cat(jb, j, "," as *u8) }
103 first=0
104 j=bc_cat(jb, j, "{\x22site\x22:\x22" as *u8); j=bc_catf(jb, j, (mb as i64+fs[0]) as *u8, fl[0])
105 j=bc_cat(jb, j, "\x22,\x22class\x22:\x22" as *u8); j=bc_catf(jb, j, clsp, clsn)
106 j=bc_cat(jb, j, "\x22,\x22reading_recall_pm\x22:" as *u8); j=bc_catn(jb, j, rec)
107 j=bc_cat(jb, j, ",\x22ratio_pct\x22:" as *u8); j=bc_catn(jb, j, rat)
108 j=bc_cat(jb, j, "}" as *u8)
109 }
110 } }
111 }
112 var meanr: i64=0; if rcnt>0 { meanr=rsum/rcnt }
113 var meanrat: i64=0; if ratcnt>0 { meanrat=ratsum/ratcnt }
114 j=bc_cat(jb, j, "],\x22n_sites\x22:" as *u8); j=bc_catn(jb, j, nsites)
115 j=bc_cat(jb, j, ",\x22content_ok\x22:" as *u8); j=bc_catn(jb, j, content_ok)
116 j=bc_cat(jb, j, ",\x22blank\x22:" as *u8); j=bc_catn(jb, j, blank)
117 j=bc_cat(jb, j, ",\x22fetch_empty\x22:" as *u8); j=bc_catn(jb, j, fetch_empty)
118 j=bc_cat(jb, j, ",\x22chrome_blocked\x22:" as *u8); j=bc_catn(jb, j, chrome_blocked)
119 j=bc_cat(jb, j, ",\x22mean_reading_recall_pm\x22:" as *u8); j=bc_catn(jb, j, meanr)
120 j=bc_cat(jb, j, ",\x22mean_height_ratio_pct\x22:" as *u8); j=bc_catn(jb, j, meanrat)
121 j=bc_cat(jb, j, ",\x22recall_measured_sites\x22:" as *u8); j=bc_catn(jb, j, rcnt)
122 j=bc_cat(jb, j, ",\x22note\x22:\x22reading_recall vs Chrome (1000=parity); -1 = UNMEASURED (excluded from the mean); data-driven SEG-STORE plane, add sites without code\x22}" as *u8)
123 // colocate evidence (0x1a4 = 0644)
124 let fd: i64=sys_openat_wr("knowledge/status/browser_census.log\x00" as *u8, 0x1a4)
125 if fd>=0 { sys_write(fd, jb, j); sys_write(fd, "\n\x00" as *u8, 1); sys_close(fd) }
126 sys_write(1, jb, j)
127 sys_write(1, "\n\x00" as *u8, 1)
128 return 0
129}