code wiki / _hdl_build / _drv_f3_gate.nx
_drv_f3_gate.nx source
↩ module page · 104 lines · 6062 B
1// _drv_f3_gate.nx -- gate for LOCAL-ONLY DRIVER SELF-TUNE (X-DRV-F3). NO mocks.
2//
3// (1) TUNED -- runs the REAL nx_self_tune; it sweeps the candidate configs on the local emu and
4// writes a tuned local registry whose chosen config is the FASTEST (lean=80 < verbose=128) --
5// a measurable local improvement over the naive/worst config, with no external input.
6// (2) NO-PHONE-HOME -- the shared telemetry outbox (telemetry_shared.log) is truncated before the
7// run and MUST remain empty after: self-tuning shares NOTHING (privacy-preserving).
8//
9// Evidence -> knowledge/status/self_tune.log (DRVF3GATE row). Sovereign. license_tier: ORIGINAL
10import "nx_syscalls.nx"
11
12const F_SELFTUNE: *u8 = "_offc/nx_self_tune.elf"
13const F_TUNED: *u8 = "knowledge/registry/driver_registry_tuned_local.tsv"
14const F_SHARED: *u8 = "knowledge/status/telemetry_shared.log"
15
16func g_p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
17func g_fp(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 }
18func g_fn(fd: i64, v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m}; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48;k=1}; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(fd,bb,k); return 0 }
19
20func g_run(prog: *u8, outpath: *u8) -> i64 {
21 let pid: i64 = sys_fork()
22 if pid == 0 {
23 if outpath != (0 as *u8) { let ofd: i64 = sys_openat_wr(outpath, 0x1a4); if ofd >= 0 { sys_dup3(ofd, 1, 0); sys_dup3(ofd, 2, 0) } }
24 let argv: *i64 = sys_mmap(16) as *i64
25 argv[0] = prog as i64; argv[1] = 0
26 let envp: *i64 = sys_mmap(16) as *i64
27 envp[0] = "PATH=/usr/bin:/bin" as *u8 as i64; envp[1] = 0
28 sys_execve(prog, argv, envp)
29 sys_exit(127)
30 }
31 let st: *i64 = sys_mmap(16) as *i64
32 sys_wait4(pid, st, 0)
33 let sg: i64 = st[0] & 0x7f
34 if sg != 0 { return 128 + sg }
35 return (st[0] >> 8) & 0xff
36}
37
38func g_read(path: *u8, buf: *u8, cap: i64) -> i64 {
39 let fd: i64 = sys_openat_rd(path)
40 if fd < 0 { return 0 }
41 var n: i64 = 0
42 var go: i64 = 1
43 while go == 1 { let r: i64 = sys_read(fd, (buf as i64 + n) as *u8, cap - 1 - n); if r <= 0 { go = 0 } else { n = n + r } if n >= cap - 1 { go = 0 } }
44 sys_close(fd)
45 return n
46}
47func g_trunc(path: *u8) -> i64 { let fd: i64 = sys_openat_wr(path, 0x1a4); if fd >= 0 { sys_close(fd) } return 0 }
48
49func g_has(buf: *u8, n: i64, pat: *u8) -> i64 { var pl: i64=0; while pat[pl]!=(0 as u8){pl=pl+1} if pl<=0 { return 0 } var i: i64=0; while i+pl<=n { var k: i64=0; var hit: i64=1; while k<pl { if buf[i+k]!=pat[k]{hit=0;k=pl}else{k=k+1} } if hit==1 { return 1 } i=i+1 } return 0 }
50
51// parse the steps field (3rd col) of the "2\t..." line in the tuned registry; -1 if absent.
52func g_tuned_steps(buf: *u8, n: i64) -> i64 {
53 var i: i64 = 0
54 while i < n {
55 var at_line: i64 = 0
56 if i == 0 { at_line = 1 } else { if buf[i-1] == (10 as u8) { at_line = 1 } }
57 if at_line == 1 { if i+2 <= n { if buf[i]==(50 as u8) { if buf[i+1]==(9 as u8) {
58 var p: i64 = i; var tabs: i64 = 0
59 while p < n { if buf[p]==(10 as u8) { p=n } else { if buf[p]==(9 as u8) { tabs=tabs+1; if tabs==2 { p=p+1; var v: i64=0; var any: i64=0; while p<n { let c: i64=buf[p] as i64; if c>=48 { if c<=57 { v=v*10+(c-48); any=1; p=p+1 } else { p=n } } else { p=n } } if any==1 { return v } return 0-1 } else { p=p+1 } } else { p=p+1 } } }
60 }}}}
61 i = i + 1
62 }
63 return 0 - 1
64}
65// does the tuned "2\t" line name the lean config?
66func g_tuned_is_lean(buf: *u8, n: i64) -> i64 { return g_has(buf, n, "2\tknowledge/specs/drv_proto_nndp_lean.spec" as *u8) }
67
68func main() -> i64 {
69 g_p("=== local-only self-tune gate (X-DRV-F3: tune on own hardware, no phone-home) ===\n" as *u8)
70 let lfd: i64 = sys_openat_append("knowledge/status/self_tune.log" as *u8, 0x1a4)
71
72 // pre-state: truncate the shared outbox so we can prove self-tune writes NOTHING to it.
73 g_trunc(F_SHARED)
74
75 let rc: i64 = g_run(F_SELFTUNE, "/tmp/_f3_run.out" as *u8)
76
77 let tb: *u8 = sys_mmap(8192); let tn: i64 = g_read(F_TUNED, tb, 8192)
78 let tuned_steps: i64 = g_tuned_steps(tb, tn)
79 let is_lean: i64 = g_tuned_is_lean(tb, tn)
80 // tuned must be the lean config (80) -- the local minimum, better than verbose (128).
81 var tuned_ok: i64 = 0
82 if rc == 0 { if is_lean == 1 { if tuned_steps == 80 { tuned_ok = 1 } } }
83
84 // NO-PHONE-HOME: the shared outbox stayed empty (self-tune shared nothing).
85 let sb: *u8 = sys_mmap(8192); let sn: i64 = g_read(F_SHARED, sb, 8192)
86 var no_phone_home: i64 = 0
87 if g_has(sb, sn, "SHARED " as *u8) == 0 { no_phone_home = 1 }
88
89 g_p(" tuned_config=lean? " as *u8); g_fn(1, is_lean); g_p(" tuned_steps=" as *u8); g_fn(1, tuned_steps); g_p(" (vs verbose 128)" as *u8)
90 g_p(" no_phone_home=" as *u8); g_fn(1, no_phone_home); g_p("\n" as *u8)
91
92 var pass: i64 = 0
93 if tuned_ok == 1 { if no_phone_home == 1 { pass = 1 } }
94
95 if pass == 1 {
96 g_p("DRVF3GATE verdict=GREEN (local-only self-tune: swept its own configs on its own emu, kept the fastest [lean 80 steps < verbose 128] as the tuned local registry -- a measurable local improvement -- and shared NOTHING [shared outbox empty, no phone-home]: privacy-preserving, still improves)\n" as *u8)
97 if lfd >= 0 { g_fp(lfd, "DRVF3GATE verdict=GREEN keystone=local-only-self-tune tuned=lean(80)<verbose(128) no-phone-home=yes(shared-outbox-empty) epoch=" as *u8); g_fn(lfd, sys_now_realtime_sec()); g_fp(lfd, "\n" as *u8); sys_close(lfd) }
98 sys_exit(0); return 0
99 }
100 g_p("DRVF3GATE verdict=RED (tuned/no-phone-home not all green)\n" as *u8)
101 if lfd >= 0 { g_fp(lfd, "DRVF3GATE verdict=RED tuned_ok=" as *u8); g_fn(lfd, tuned_ok); g_fp(lfd, " no_phone_home=" as *u8); g_fn(lfd, no_phone_home); g_fp(lfd, " tuned_steps=" as *u8); g_fn(lfd, tuned_steps); g_fp(lfd, "\n" as *u8); sys_close(lfd) }
102 sys_exit(1)
103 return 1
104}