nx_metric_ledger.nx source
↩ module page · 120 lines · 5973 B
1// nx_metric_ledger.nx -- RETURN-AND-REPORT primitive (operator 2026-07-03: "on everything we do on the
2// nishi ecosystem teams apis etc it should be a feedback loop with return and report as a key part so
3// that we always know if we are moving toward more or less performance and quality").
4// Every organ that measures ANYTHING calls ml_report(name, value, dir, unit, src):
5// 1. appends `METRIC <name> t_us=<t> value=<v> unit=<u> src=<s>` to knowledge/status/nx_metric_ledger.log
6// 2. scans backward for the PREVIOUS row of the same metric
7// 3. prints + returns the TREND: BETTER / WORSE / FLAT / NEW (dir: +1 higher-is-better, -1 lower-is-better)
8// So every run of every measuring organ answers "are we moving toward more or less performance/quality"
9// mechanically -- no one has to remember to compare. Append-only (rule #13); the ledger IS the history.
10// Returns: 2=BETTER 1=FLAT 0=NEW -1=WORSE (callers may gate on WORSE = regression trap).
11// No main logic beyond a compile smoke; the gate is nx_metric_ledger_gate. license_tier: ORIGINAL
12import "nx_syscalls.nx"
13const ML_MAGIC_4611686018427387904: i64 = 4611686018427387904
14
15const ML_LOG: *u8 = "knowledge/status/nx_metric_ledger.log"
16const ML_CAP: i64 = 4194304
17
18func ml_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
19func ml_wn(v: i64) -> i64 {
20 let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m}
21 let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}
22 var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 }
23func ml_fw(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 }
24func ml_fn(fd: i64, v: i64) -> i64 {
25 let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(fd,"-" as *u8,1);m=0-m}
26 let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}
27 var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(fd,b,k); return 0 }
28func ml_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
29
30// parse a signed decimal at buf[p..]; returns value (stops at first non-digit)
31func ml_parse(buf: *u8, p0: i64, n: i64) -> i64 {
32 var p: i64 = p0
33 var neg: i64 = 0
34 if p < n { if (buf[p] as i64) == 45 { neg = 1; p = p + 1 } }
35 var v: i64 = 0
36 var g: i64 = 1
37 while g == 1 {
38 if p < n { let c: i64 = buf[p] as i64; if c >= 48 { if c <= 57 { v = v*10 + (c-48); p = p + 1 } else { g = 0 } } else { g = 0 } } else { g = 0 }
39 }
40 if neg == 1 { return 0 - v }
41 return v
42}
43
44// find the LAST prior `METRIC <name> ... value=<v>` row; returns v, or i64-min-sentinel if none
45func ml_prev(name: *u8) -> i64 {
46 let none: i64 = 0 - ML_MAGIC_4611686018427387904
47 let buf: *u8 = sys_mmap(ML_CAP)
48 let fd: i64 = sys_openat_rd(ML_LOG)
49 if fd < 0 { return none }
50 var tot: i64 = 0
51 var r: i64 = 1
52 while r > 0 { let dst: *u8 = ((buf as i64)+tot) as *u8; r = sys_read(fd, dst, ML_CAP - tot); if r > 0 { tot = tot + r } }
53 sys_close(fd)
54 let nl: i64 = ml_slen(name)
55 var best: i64 = none
56 var pos: i64 = 0
57 while pos < tot {
58 var le: i64 = pos
59 var g: i64 = 1
60 while g == 1 { if le < tot { if (buf[le] as i64) == 10 { g = 0 } else { le = le + 1 } } else { g = 0 } }
61 // row must start "METRIC <name> "
62 if le - pos > 7 + nl {
63 var okrow: i64 = 1
64 let pfx: *u8 = "METRIC " as *u8
65 var i: i64 = 0
66 while i < 7 { if (buf[pos+i] as i64) != (pfx[i] as i64) { okrow = 0; i = 7 } else { i = i + 1 } }
67 if okrow == 1 {
68 i = 0
69 while i < nl { if (buf[pos+7+i] as i64) != (name[i] as i64) { okrow = 0; i = nl } else { i = i + 1 } }
70 if okrow == 1 { if (buf[pos+7+nl] as i64) != 32 { okrow = 0 } }
71 }
72 if okrow == 1 {
73 // find " value=" within the row
74 var q: i64 = pos
75 while q < le - 7 {
76 if (buf[q] as i64) == 32 { if (buf[q+1] as i64) == 118 { if (buf[q+2] as i64) == 97 { if (buf[q+3] as i64) == 108 { if (buf[q+4] as i64) == 117 { if (buf[q+5] as i64) == 101 { if (buf[q+6] as i64) == 61 {
77 best = ml_parse(buf, q + 7, le)
78 q = le
79 } } } } } } }
80 q = q + 1
81 }
82 }
83 }
84 pos = le + 1
85 }
86 return best
87}
88
89// THE return-and-report call. dir: +1 higher-is-better (fps), -1 lower-is-better (latency).
90// Prints `TREND <name> prev=<p> now=<v> -> BETTER|WORSE|FLAT|NEW` and appends the new row.
91// Returns 2=BETTER 1=FLAT 0=NEW -1=WORSE.
92func ml_report(name: *u8, value: i64, dir: i64, unit: *u8, src: *u8) -> i64 {
93 let none: i64 = 0 - ML_MAGIC_4611686018427387904
94 let prev: i64 = ml_prev(name)
95 let fd: i64 = sys_openat_append(ML_LOG, 420)
96 if fd >= 0 {
97 ml_fw(fd, "METRIC " as *u8); ml_fw(fd, name)
98 ml_fw(fd, " t_us=" as *u8); ml_fn(fd, sys_now_us())
99 ml_fw(fd, " value=" as *u8); ml_fn(fd, value)
100 ml_fw(fd, " unit=" as *u8); ml_fw(fd, unit)
101 ml_fw(fd, " src=" as *u8); ml_fw(fd, src)
102 ml_fw(fd, "\n" as *u8)
103 sys_close(fd)
104 }
105 ml_w(" TREND " as *u8); ml_w(name)
106 if prev == none {
107 ml_w(" now=" as *u8); ml_wn(value); ml_w(" " as *u8); ml_w(unit); ml_w(" -> NEW (baseline recorded)\n" as *u8)
108 return 0
109 }
110 ml_w(" prev=" as *u8); ml_wn(prev); ml_w(" now=" as *u8); ml_wn(value); ml_w(" " as *u8); ml_w(unit)
111 if value == prev { ml_w(" -> FLAT\n" as *u8); return 1 }
112 var better: i64 = 0
113 if dir > 0 { if value > prev { better = 1 } }
114 if dir < 0 { if value < prev { better = 1 } }
115 if better == 1 { ml_w(" -> BETTER\n" as *u8); return 2 }
116 ml_w(" -> WORSE\n" as *u8)
117 return 0 - 1
118}
119
120func main() -> i64 { return 0 }