nx_tone_w_gate.nx source
↩ module page · 127 lines · 5645 B
1// nx_tone_w_gate.nx -- REFEREE for WRITING rung W-TONE-2 (weighting + drift).
2//
3// [T1] unit weights (all 1000) reproduce the unweighted metric EXACTLY.
4// [T2] THE FIX: a candidate that the RAW metric mis-attributes (cadence
5// dominates) is correctly re-attributed once the cadence dim is damped --
6// the SAME candidate flips A<->B purely by changing the weights.
7// [T3] DRIFT report: signed per-dim deviation has the right sign + magnitude,
8// and its total equals the weighted distance.
9// [T4] NEG-CONTROL: drift of a voice vs itself is all-zero / total 0.
10//
11// Synthetic fingerprints (filled directly) so the weighting MATH is exact and
12// hand-verifiable, independent of the text->vector path proven in nx_tone_gate.
13// Evidence -> stdout + knowledge/status/tone_w_gate.log. Exit 0 GREEN / 1 RED.
14// Sovereign x86_64. license_tier: ORIGINAL
15import "nx_syscalls_x86_64.nx"
16import "nx_tone.nx"
17
18func gp(logfd: i64, s: *u8) -> i64 {
19 var n: i64 = 0
20 while s[n] != (0 as u8) { n = n + 1 }
21 sys_write(1, s, n)
22 if logfd > 0 { sys_write(logfd, s, n) }
23 return 0
24}
25func gn(logfd: i64, v: i64) -> i64 {
26 var m: i64 = v
27 if m < 0 {
28 sys_write(1, "-\x00" as *u8, 1)
29 if logfd > 0 { sys_write(logfd, "-\x00" as *u8, 1) }
30 m = 0 - m
31 }
32 let bb: *u8 = sys_mmap(32)
33 let t: *u8 = sys_mmap(32)
34 var k: i64 = 0
35 if m == 0 { t[0] = 48 as u8; k = 1 }
36 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
37 var i: i64 = 0
38 while i < k { bb[i] = t[k - 1 - i]; i = i + 1 }
39 sys_write(1, bb, k)
40 if logfd > 0 { sys_write(logfd, bb, k) }
41 return 0
42}
43func pr_kv(logfd: i64, label: *u8, got: i64, exp: i64) -> i64 {
44 gp(logfd, label)
45 gp(logfd, " got=\x00" as *u8); gn(logfd, got)
46 gp(logfd, " exp=\x00" as *u8); gn(logfd, exp)
47 if got == exp { gp(logfd, " OK\n\x00" as *u8); return 1 }
48 gp(logfd, " FAIL\n\x00" as *u8)
49 return 0
50}
51func pr_cond(logfd: i64, label: *u8, cond: i64) -> i64 {
52 gp(logfd, label)
53 if cond != 0 { gp(logfd, " OK\n\x00" as *u8); return 1 }
54 gp(logfd, " FAIL\n\x00" as *u8)
55 return 0
56}
57// set v[0..k-1] = val
58func fill(v: *i64, k: i64, val: i64) -> i64 {
59 var i: i64 = 0
60 while i < k { v[i] = val; i = i + 1 }
61 return 0
62}
63
64func main() -> i64 {
65 let logfd: i64 = sys_openat_append("knowledge/status/tone_w_gate.log\x00" as *u8, 0x1a4)
66 gp(logfd, "TONE-W-GATE W-TONE-2 (per-dim weighting + drift)\n\x00" as *u8)
67
68 let K: i64 = TN_DIMS
69 let A: *i64 = sys_mmap(128) as *i64
70 let B: *i64 = sys_mmap(128) as *i64
71 let cand: *i64 = sys_mmap(128) as *i64
72 let wun: *i64 = sys_mmap(128) as *i64 // unit weights (all 1000)
73 let wz: *i64 = sys_mmap(128) as *i64 // cadence damped to 0
74 let drf: *i64 = sys_mmap(128) as *i64
75 var ok: i64 = 1
76
77 // voices: A cadence=2000 adverb=200 ; B cadence=2600 adverb=0
78 // cand cadence=2600 adverb=200 (matches B on cadence, A on adverb)
79 fill(A, K, 0); A[0] = 2000; A[4] = 200
80 fill(B, K, 0); B[0] = 2600; B[4] = 0
81 fill(cand, K, 0); cand[0] = 2600; cand[4] = 200
82 fill(wun, K, 1000)
83 fill(wz, K, 1000); wz[0] = 0
84
85 // ---- T1: unit weights == unweighted ----
86 gp(logfd, " [T1] unit weights reproduce tn_dist exactly\n\x00" as *u8)
87 if pr_kv(logfd, " dist_w(A,B,unit)\x00" as *u8, tn_dist_w(A, B, wun, K), tn_dist(A, B, K)) == 0 { ok = 0 }
88 if pr_kv(logfd, " dist_w(cand,A,unit)\x00" as *u8, tn_dist_w(cand, A, wun, K), tn_dist(cand, A, K)) == 0 { ok = 0 }
89
90 // ---- T2: THE FIX -- weights flip a mis-attribution ----
91 gp(logfd, " [T2] same cand flips A<->B by weighting alone\n\x00" as *u8)
92 gp(logfd, " raw distA=\x00" as *u8); gn(logfd, tn_dist(cand, A, K))
93 gp(logfd, " distB=\x00" as *u8); gn(logfd, tn_dist(cand, B, K)); gp(logfd, "\n\x00" as *u8)
94 // raw (cadence dominates): cand mis-attributed to B
95 if pr_kv(logfd, " raw: cand closer to A\x00" as *u8, tn_closer(cand, A, B, K), 0) == 0 { ok = 0 }
96 gp(logfd, " damped distA=\x00" as *u8); gn(logfd, tn_dist_w(cand, A, wz, K))
97 gp(logfd, " distB=\x00" as *u8); gn(logfd, tn_dist_w(cand, B, wz, K)); gp(logfd, "\n\x00" as *u8)
98 // cadence damped: cand correctly attributed to A
99 if pr_kv(logfd, " damped: cand closer to A\x00" as *u8, tn_closer_w(cand, A, B, wz, K), 1) == 0 { ok = 0 }
100
101 // ---- T3: DRIFT report ----
102 gp(logfd, " [T3] drift(cand,A,unit): signed per-dim deviation\n\x00" as *u8)
103 let tot: i64 = tn_drift(cand, A, wun, K, drf)
104 if pr_kv(logfd, " drift cadence dim0 (=2600-2000)\x00" as *u8, drf[0], 600) == 0 { ok = 0 }
105 if pr_kv(logfd, " drift adverb dim4 (=200-200)\x00" as *u8, drf[4], 0) == 0 { ok = 0 }
106 if pr_kv(logfd, " drift total == dist_w\x00" as *u8, tot, tn_dist_w(cand, A, wun, K)) == 0 { ok = 0 }
107 var s1: i64 = 0
108 if drf[0] > 0 { s1 = 1 }
109 if pr_cond(logfd, " cadence drift positive (cand runs longer -> steer down)\x00" as *u8, s1) == 0 { ok = 0 }
110
111 // ---- T4: NEG-CONTROL -- drift vs self is zero ----
112 gp(logfd, " [T4] NEG-CONTROL drift(A,A) all-zero\n\x00" as *u8)
113 let tot0: i64 = tn_drift(A, A, wun, K, drf)
114 if pr_kv(logfd, " drift(A,A) total\x00" as *u8, tot0, 0) == 0 { ok = 0 }
115 if pr_kv(logfd, " drift(A,A) dim0\x00" as *u8, drf[0], 0) == 0 { ok = 0 }
116
117 if ok == 1 {
118 gp(logfd, "TONE-W-GATE result=ALL-PASS verdict=GREEN\n\x00" as *u8)
119 if logfd > 0 { sys_close(logfd) }
120 sys_exit(0)
121 return 0
122 }
123 gp(logfd, "TONE-W-GATE result=FAIL verdict=RED\n\x00" as *u8)
124 if logfd > 0 { sys_close(logfd) }
125 sys_exit(1)
126 return 1
127}