nx_sites_intel.nx source
↩ module page · 197 lines · 7538 B
1// nx_sites_intel.nx -- the IMPROVE layer of the self-improving sites
2// system. A sovereign, operator-run analyzer that reads the additive
3// telemetry log (the SENSE layer's append-only output) and emits a
4// structured intelligence report.
5//
6// It UNDERSTANDS the additive history: connection volume, success vs
7// failure (by verdict), LAN/internet access mix, per-vhost mix, and the
8// TLS handshake latency distribution (min/avg/max + a histogram). This
9// is the quantified S-class scorecard -- re-run it as the log grows to
10// track improvement over time (the additive log is the training data).
11//
12// PRIVACY (no-tracking cardinal): the telemetry log it reads contains
13// NO PII -- only a coarse LAN/internet bit, timings, byte counts, and
14// outcome codes. No IPs, no UA/Referer, no cross-session correlation.
15//
16// Usage: nx_sites_intel.elf (reads the hardcoded telemetry path)
17//
18// license_tier: ORIGINAL
19
20import "nx_syscalls.nx"
21
22const NX_INTEL_LOG_PATH: *u8 = "/volume1/homes/elderwesto/nishihost/sites_telemetry.log" as *u8
23
24func ai_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != 0 { n = n + 1 } return n }
25func ai_puts(s: *u8) -> i64 { sys_write(1, s, ai_slen(s)); return 0 }
26
27func ai_putdec(v: i64) -> i64 {
28 var x: i64 = v
29 let buf: *u8 = sys_mmap(32)
30 var o: i64 = 0
31 if x < 0 { buf[0] = 0x2d as u8; o = 1; x = 0 - x }
32 var nd: i64 = 1
33 var t: i64 = x
34 while t >= 10 { t = t / 10; nd = nd + 1 }
35 var i: i64 = nd - 1
36 while i >= 0 {
37 var d: i64 = x
38 var k: i64 = 0
39 while k < i { d = d / 10; k = k + 1 }
40 buf[o + (nd - 1 - i)] = (0x30 + (d % 10)) as u8
41 i = i - 1
42 }
43 sys_write(1, buf, o + nd)
44 return 0
45}
46
47func ai_kv(label: *u8, v: i64) -> i64 { ai_puts(label); ai_putdec(v); ai_puts("\n" as *u8); return 0 }
48
49// Find substring `key` (klen) within buf[ls..le); return index AFTER the
50// match, or -1 if not present.
51func ai_find(buf: *u8, ls: i64, le: i64, key: *u8, klen: i64) -> i64 {
52 var i: i64 = ls
53 let last: i64 = le - klen
54 while i <= last {
55 var j: i64 = 0
56 var ok: i64 = 1
57 while j < klen {
58 if buf[i + j] != key[j] { ok = 0; j = klen } else { j = j + 1 }
59 }
60 if ok == 1 { return i + klen }
61 i = i + 1
62 }
63 return 0 - 1
64}
65
66// Parse an optional-sign integer at buf[pos..le). pos<0 -> returns 0.
67func ai_pint(buf: *u8, pos: i64, le: i64) -> i64 {
68 if pos < 0 { return 0 }
69 var p: i64 = pos
70 var neg: i64 = 0
71 if p < le { if (buf[p] & 0xff) == 0x2d { neg = 1; p = p + 1 } }
72 var v: i64 = 0
73 var go: i64 = 1
74 while go == 1 {
75 if p >= le { go = 0 } else {
76 let c: i64 = (buf[p] as i64) & 0xff
77 if c >= 0x30 { if c <= 0x39 { v = v * 10 + (c - 0x30); p = p + 1 } else { go = 0 } } else { go = 0 }
78 }
79 }
80 if neg == 1 { return 0 - v }
81 return v
82}
83
84func main() -> i64 {
85 let len_box: *i64 = (sys_mmap(8)) as *i64
86 len_box[0] = 0
87 let buf: *u8 = sys_read_file(NX_INTEL_LOG_PATH, len_box)
88 if (buf as i64) == 0 { ai_puts("intel: no telemetry log yet\n" as *u8); return 1 }
89 let n: i64 = len_box[0]
90
91 var total: i64 = 0
92 var ok_n: i64 = 0
93 var fail_n: i64 = 0
94 var accL: i64 = 0
95 var accN: i64 = 0
96 var vh0: i64 = 0
97 var vh1: i64 = 0
98 var vh2: i64 = 0
99 var hs_min: i64 = 0
100 var hs_max: i64 = 0
101 var hs_sum: i64 = 0
102 var by_sum: i64 = 0
103 // hs histogram (over OK handshakes), microsecond bounds.
104 var b_lt50: i64 = 0
105 var b_50_200: i64 = 0
106 var b_200_500: i64 = 0
107 var b_500_1k: i64 = 0
108 var b_1k_2k: i64 = 0
109 var b_ge2k: i64 = 0
110 // failure verdicts of interest.
111 var rc_read_ch: i64 = 0 // -2
112 var rc_recv_ch: i64 = 0 // -3
113 var rc_proto: i64 = 0 // -4
114 var rc_other: i64 = 0
115
116 var ls: i64 = 0
117 var i: i64 = 0
118 while i < n {
119 if (buf[i] & 0xff) == 0x0a {
120 let le: i64 = i
121 if le > ls + 4 {
122 total = total + 1
123 let rc: i64 = ai_pint(buf, ai_find(buf, ls, le, " rc=" as *u8, 4), le)
124 let hs: i64 = ai_pint(buf, ai_find(buf, ls, le, " hs=" as *u8, 4), le)
125 let by: i64 = ai_pint(buf, ai_find(buf, ls, le, " by=" as *u8, 4), le)
126 let vh: i64 = ai_pint(buf, ai_find(buf, ls, le, " vh=" as *u8, 4), le)
127 let acc_pos: i64 = ai_find(buf, ls, le, " acc=" as *u8, 5)
128 if acc_pos >= 0 {
129 if (buf[acc_pos] & 0xff) == 0x4c { accL = accL + 1 } else { accN = accN + 1 }
130 }
131 if rc == 0 {
132 ok_n = ok_n + 1
133 by_sum = by_sum + by
134 if vh == 0 { vh0 = vh0 + 1 }
135 if vh == 1 { vh1 = vh1 + 1 }
136 if vh == 2 { vh2 = vh2 + 1 }
137 hs_sum = hs_sum + hs
138 if ok_n == 1 { hs_min = hs; hs_max = hs } else {
139 if hs < hs_min { hs_min = hs }
140 if hs > hs_max { hs_max = hs }
141 }
142 if hs < 50000 { b_lt50 = b_lt50 + 1 } else {
143 if hs < 200000 { b_50_200 = b_50_200 + 1 } else {
144 if hs < 500000 { b_200_500 = b_200_500 + 1 } else {
145 if hs < 1000000 { b_500_1k = b_500_1k + 1 } else {
146 if hs < 2000000 { b_1k_2k = b_1k_2k + 1 } else { b_ge2k = b_ge2k + 1 }
147 }
148 }
149 }
150 }
151 } else {
152 fail_n = fail_n + 1
153 if rc == (0 - 2) { rc_read_ch = rc_read_ch + 1 } else {
154 if rc == (0 - 3) { rc_recv_ch = rc_recv_ch + 1 } else {
155 if rc == (0 - 4) { rc_proto = rc_proto + 1 } else { rc_other = rc_other + 1 }
156 }
157 }
158 }
159 }
160 ls = i + 1
161 }
162 i = i + 1
163 }
164
165 var hs_avg: i64 = 0
166 if ok_n > 0 { hs_avg = hs_sum / ok_n }
167
168 ai_puts("=== Nishi Sites Intelligence (self-improving / additive) ===\n" as *u8)
169 ai_kv("connections_total: " as *u8, total)
170 ai_kv(" handshake_ok: " as *u8, ok_n)
171 ai_kv(" handshake_fail: " as *u8, fail_n)
172 ai_puts("access_mix:\n" as *u8)
173 ai_kv(" lan: " as *u8, accL)
174 ai_kv(" internet: " as *u8, accN)
175 ai_puts("vhost_mix(ok):\n" as *u8)
176 ai_kv(" family(0): " as *u8, vh0)
177 ai_kv(" andelinwest(1): " as *u8, vh1)
178 ai_kv(" wiki(2): " as *u8, vh2)
179 ai_puts("handshake_latency_us (ok):\n" as *u8)
180 ai_kv(" min: " as *u8, hs_min)
181 ai_kv(" avg: " as *u8, hs_avg)
182 ai_kv(" max: " as *u8, hs_max)
183 ai_puts("handshake_histogram (ok):\n" as *u8)
184 ai_kv(" <50ms: " as *u8, b_lt50)
185 ai_kv(" 50-200ms: " as *u8, b_50_200)
186 ai_kv(" 200-500ms: " as *u8, b_200_500)
187 ai_kv(" 500ms-1s: " as *u8, b_500_1k)
188 ai_kv(" 1-2s: " as *u8, b_1k_2k)
189 ai_kv(" >=2s: " as *u8, b_ge2k)
190 ai_puts("failures_by_verdict:\n" as *u8)
191 ai_kv(" read_ch(-2): " as *u8, rc_read_ch)
192 ai_kv(" recv_ch(-3): " as *u8, rc_recv_ch)
193 ai_kv(" protocol(-4): " as *u8, rc_proto)
194 ai_kv(" other: " as *u8, rc_other)
195 ai_kv("bytes_served_total: " as *u8, by_sum)
196 return 0
197}