code wiki / _hdl_build / nx_toolatency_legacy_gate.nx
nx_toolatency_legacy_gate.nx source
↩ module page · 133 lines · 8437 B
1// Isolated incumbent/staged default-CLI parity; no observations branch invocation.
2import "nx_toolatency_observation_lib.nx"
3import "nx_tool_run.nx"
4import "nx_gate_verdict.nx"
5const OC_FILE_MODE: i64 = 384
6const LG_DIR_MODE: i64 = 448
7const LG_EXIT_OVER: i64 = 6
8static lg_input: *u8
9static lg_conf: *u8
10static lg_lane: *u8
11func oc_copy(b: *u8, p: i64, s: *u8) -> i64 {
12 var i: i64=0; while s[i] != (0 as u8) { b[p+i]=s[i]; i=i+1 }; b[p+i]=0 as u8; return p+i
13}
14
15func oc_path_ok(s: *u8, directory: i64) -> i64 {
16 let n: i64=to_len(s)
17 if n < 2 || n >= SYS_PATH_MAX/2 || s[0] != (47 as u8) { return 0 }
18 var p: i64=0; while p < n { if s[p] < (33 as u8) || s[p] > (126 as u8) { return 0 }; p=p+1 }
19 if directory == 1 {
20 if n <= 5 || to_eq(s,0,5,"/tmp/" as *u8) != 1 { return 0 }
21 p=5
22 while p < n {
23 let c: i64=s[p] as i64
24 if (c < 48 || c > 57) && (c < 65 || c > 90) && (c < 97 || c > 122) && c != 45 && c != 95 { return 0 }; p=p+1
25 }
26 }; return 1
27}
28
29func oc_put(path: *u8, b: *u8, n: i64) -> i64 {
30 let fd: i64=sys_openat_exclusive(path,OC_FILE_MODE); if fd < 0 { return 0 }
31 var done: i64=0; var ok: i64=1
32 while done < n && ok == 1 {
33 let w: i64=sys_write(fd,((b as i64)+done) as *u8,n-done)
34 if w <= 0 { ok=0 } else { done=done+w }
35 }
36 if sys_fsync(fd) != 0 { ok=0 }; if sys_close(fd) != 0 { ok=0 }; return ok
37}
38
39func oc_read(path: *u8, b: *u8, cap: i64) -> i64 {
40 let fd: i64=sys_openat_rd(path); if fd < 0 { return 0-1 }
41 let n: i64=sys_lseek(fd,0,2); var ok: i64=1; var done: i64=0
42 if n < 0 || n > cap { ok=0 }
43 if sys_lseek(fd,0,0) != 0 { ok=0 }
44 while ok == 1 && done < n {
45 let got: i64=sys_read(fd,((b as i64)+done) as *u8,n-done)
46 if got <= 0 { ok=0 } else { done=done+got }
47 }
48 if sys_close(fd) != 0 { ok=0 }; if ok == 0 { return 0-1 }; return n
49}
50
51func oc_same(path: *u8, expected: *u8, n: i64, scratch: *u8, cap: i64) -> i64 {
52 if oc_read(path,scratch,cap) != n { return 0 }
53 var i: i64=0; while i < n { if scratch[i] != expected[i] { return 0 }; i=i+1 }; return 1
54}
55
56func lg_setup(name: *u8) -> i64 {
57 if sys_mkdir(name,LG_DIR_MODE) != 0 { return 0 }
58 if sys_chdir(name) != 0 { return 0 }
59 if sys_mkdir("knowledge" as *u8,LG_DIR_MODE) != 0 { return 0 }
60 if sys_mkdir("knowledge/status" as *u8,LG_DIR_MODE) != 0 { return 0 }
61 if oc_put("knowledge/status/actlog.jrnl" as *u8,lg_input,to_len(lg_input)) != 1 { return 0 }
62 if oc_put("knowledge/toolatency.conf" as *u8,lg_conf,to_len(lg_conf)) != 1 { return 0 }
63 if oc_put("async_only_tools.conf" as *u8,lg_lane,to_len(lg_lane)) != 1 { return 0 }
64 if sys_chdir(".." as *u8) != 0 { return 0 }; return 1
65}
66func lg_run(subject: *u8, name: *u8, b: *u8, cap: i64, timeout: i64, ctr: *i64) -> i64 {
67 let av: *i64=sys_mmap(2*TO_WORD_BYTES) as *i64; let len: *i64=sys_mmap(TO_WORD_BYTES) as *i64
68 if (av as i64) <= 0 || (len as i64) <= 0 { return 0-1 }
69 av[0]=subject as i64; av[1]=0; len[0]=0
70 let ec: i64=tr_run_capture_cwd(subject,av,b,cap,len,timeout,name); let n: i64=len[0]
71 gv_puts("subject=" as *u8); gv_puts(subject); gv_puts(" exit=" as *u8); gv_num(ec); gv_puts(" bytes=" as *u8); gv_num(n); gv_puts("\n" as *u8)
72 gv_check("legacy repeated-breach exit retained" as *u8,ec == LG_EXIT_OVER,ctr)
73 gv_check("complete default stdout fits capture" as *u8,n > 0 && n < cap,ctr)
74 if n <= 0 || n >= cap { return 0-1 }
75 let path: *u8=sys_mmap(SYS_PATH_MAX); if (path as i64) <= 0 { return 0-1 }
76 let p: i64=oc_copy(path,0,name); oc_copy(path,p,"/stdout.txt" as *u8)
77 gv_check("default stdout retained exclusively" as *u8,oc_put(path,b,n),ctr)
78 gv_check("phase row excluded from measured calls" as *u8,to_has(b,0,n,"rows=4 measured=3 tools=2" as *u8),ctr)
79 gv_check("original verdict and route-off behavior retained" as *u8,to_has(b,0,n,"tools=2 over_budget=1 at_risk=0 auto_route=0 auto_routed=0" as *u8),ctr)
80 sys_munmap(path,SYS_PATH_MAX); sys_munmap(av as *u8,2*TO_WORD_BYTES); sys_munmap(len as *u8,TO_WORD_BYTES); return n
81}
82func lg_unchanged(name: *u8, scratch: *u8, cap: i64, ctr: *i64) -> i64 {
83 if sys_chdir(name) != 0 { gv_check("fixture cwd accessible" as *u8,0,ctr); return 0 }
84 gv_check("legacy input unchanged" as *u8,oc_same("knowledge/status/actlog.jrnl" as *u8,lg_input,to_len(lg_input),scratch,cap),ctr)
85 gv_check("legacy config unchanged" as *u8,oc_same("knowledge/toolatency.conf" as *u8,lg_conf,to_len(lg_conf),scratch,cap),ctr)
86 gv_check("route-off lane file unchanged" as *u8,oc_same("async_only_tools.conf" as *u8,lg_lane,to_len(lg_lane),scratch,cap),ctr)
87 if sys_chdir(".." as *u8) != 0 { return 0 }; return 1
88}
89func main(argc: i64, argv: *i64) -> i64 {
90 if argc != 6 { gv_puts("usage: legacy-gate absolute-incumbent absolute-staged /tmp/fresh-dir capture_capacity timeout_ms\n" as *u8); return 3 }
91 let old: *u8=argv[1] as *u8; let staged: *u8=argv[2] as *u8; let dir: *u8=argv[3] as *u8
92 if oc_path_ok(old,0) != 1 || oc_path_ok(staged,0) != 1 || oc_path_ok(dir,1) != 1 { return 3 }
93 let params: *i64=sys_mmap(2*TO_WORD_BYTES) as *i64; if (params as i64) <= 0 { return 3 }
94 if to_int(argv[4] as *u8,0,to_len(argv[4] as *u8),params) != 1 { return 3 }
95 if to_int(argv[5] as *u8,0,to_len(argv[5] as *u8),((params as i64)+TO_WORD_BYTES) as *i64) != 1 { return 3 }
96 let cap: i64=params[0]; let timeout: i64=params[1]
97 if cap <= 0 || cap >= TO_MAX/2 || timeout <= 0 || timeout >= TO_MAX/2 { return 3 }
98 lg_input="1\tmcp\tfixture_fast\tcall\tok\tdur_ms=10\n2\tmcp\tfixture_slow\tcall\tok\tdur_ms=15000\n3\tmcp\tfixture_slow\tcall\tok\tdur_ms=15000\n4\tmcp\tfixture_phase\tobserve\trequest_read_return\ttools/timing timing_v=1 lane=server job=-1 request_pid=-1 request_begin_us=10 clock=monotonic_us clock_scope=same_process_tree begin_us=10 end_us=11 elapsed_us=1 code=0 bytes=-1 client_first_byte=unobserved result_first_byte=unobserved\n" as *u8
99 lg_conf="act_permil=40\nact_avg_ms=2000\nauto_route=0\n" as *u8
100 lg_lane="fixture_already_async\n" as *u8
101 if cap < to_len(lg_input) { return 3 }
102 if sys_mkdir(dir,LG_DIR_MODE) != 0 { return 3 }; if sys_chdir(dir) != 0 { return 3 }
103 if lg_setup("incumbent" as *u8) != 1 || lg_setup("staged" as *u8) != 1 { return 3 }
104 let ctr: *i64=gv_ctr(); gv_head("TOOLATENCY-LEGACY / isolated exact default parity" as *u8)
105 let a: *u8=sys_mmap(cap); let b: *u8=sys_mmap(cap); let scratch: *u8=sys_mmap(cap)
106 if (a as i64) <= 0 || (b as i64) <= 0 || (scratch as i64) <= 0 { return 3 }
107 let an: i64=lg_run(old,"incumbent" as *u8,a,cap,timeout,ctr)
108 let bn: i64=lg_run(staged,"staged" as *u8,b,cap,timeout,ctr)
109 var same: i64=1; if an <= 0 || an != bn { same=0 }; var i: i64=0
110 while same == 1 && i < an { if a[i] != b[i] { same=0 }; i=i+1 }
111 gv_check("incumbent and staged stdout byte-identical" as *u8,same,ctr)
112 let aj: i64=oc_read("incumbent/knowledge/status/toolatency.log" as *u8,a,cap)
113 let bj: i64=oc_read("staged/knowledge/status/toolatency.log" as *u8,b,cap)
114 // The sole intentionally variable field is each invocation's realtime timestamp.
115 var ap: i64=0; var bp: i64=0
116 while ap < aj && a[ap] != (32 as u8) { ap=ap+1 }
117 while bp < bj && b[bp] != (32 as u8) { bp=bp+1 }
118 var stamps: i64=0
119 if ap > 3 && bp > 3 {
120 if to_eq(a,0,3,"ts=" as *u8) == 1 && to_eq(b,0,3,"ts=" as *u8) == 1 {
121 if to_int(a,3,ap,params) == 1 && params[0] > 0 { if to_int(b,3,bp,params) == 1 && params[0] > 0 { stamps=1 } }
122 }
123 }
124 gv_check("only valid invocation timestamps normalized" as *u8,stamps,ctr)
125 same=1
126 if aj <= 0 || bj <= 0 || ap == aj || bp == bj || aj-ap != bj-bp { same=0 }
127 i=0; while same == 1 && i < aj-ap { if a[ap+i] != b[bp+i] { same=0 }; i=i+1 }
128 gv_check("legacy journal payload same after independent timestamps" as *u8,same,ctr)
129 gv_check("journal contains original RED denominator" as *u8,to_has(b,0,bj,"rows=4 measured=3 tools=2 over_budget=1 at_risk=0" as *u8) && to_has(b,0,bj,"verdict=RED" as *u8),ctr)
130 lg_unchanged("incumbent" as *u8,scratch,cap,ctr); lg_unchanged("staged" as *u8,scratch,cap,ctr)
131 gv_puts("retained_fixture_directory=" as *u8); gv_puts(dir); gv_puts("\n" as *u8)
132 return gv_verdict("TOOLATENCY-LEGACY" as *u8,ctr,"one representative default differential; auto-route mutation and wider legacy policy branches not asserted" as *u8)
133}