nx_tone_gate.nx source
↩ module page · 133 lines · 6305 B
1// nx_tone_gate.nx -- REFEREE for WRITING rung W-TONE-1 (nx_tone).
2//
3// [T1] EXACT fingerprint math: a controlled text -> hand-computed dims
4// (cadence tf[0], avg word length tf[1]) prove the counting+normalization.
5// [T2] DISCRIMINATION: a terse voice vs an ornate voice differ in the EXPECTED
6// DIRECTION on independent dims (cadence + adverb ratio) -- the fingerprint
7// carries real signal, it is not a constant.
8// [T3] THE CAPABILITY ("write in my tone"): given a target voice A and a
9// contrast voice B, a NEW terse draft matches A (tn_closer==1) and a NEW
10// ornate draft does NOT (tn_closer==0). Correct voice attribution.
11// [T4] NEG-CONTROL / metric sanity: dist(A,A)==0 (identical -> zero); a voice
12// matches itself over a contrast.
13//
14// Evidence -> stdout + knowledge/status/tone_gate.log. Exit 0 GREEN / 1 RED.
15// Sovereign x86_64. license_tier: ORIGINAL
16import "nx_syscalls_x86_64.nx"
17import "nx_tone.nx"
18
19func gp(logfd: i64, s: *u8) -> i64 {
20 var n: i64 = 0
21 while s[n] != (0 as u8) { n = n + 1 }
22 sys_write(1, s, n)
23 if logfd > 0 { sys_write(logfd, s, n) }
24 return 0
25}
26func gn(logfd: i64, v: i64) -> i64 {
27 var m: i64 = v
28 if m < 0 {
29 sys_write(1, "-\x00" as *u8, 1)
30 if logfd > 0 { sys_write(logfd, "-\x00" as *u8, 1) }
31 m = 0 - m
32 }
33 let bb: *u8 = sys_mmap(32)
34 let t: *u8 = sys_mmap(32)
35 var k: i64 = 0
36 if m == 0 { t[0] = 48 as u8; k = 1 }
37 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
38 var i: i64 = 0
39 while i < k { bb[i] = t[k - 1 - i]; i = i + 1 }
40 sys_write(1, bb, k)
41 if logfd > 0 { sys_write(logfd, bb, k) }
42 return 0
43}
44func slen(s: *u8) -> i64 {
45 var n: i64 = 0
46 while s[n] != (0 as u8) { n = n + 1 }
47 return n
48}
49func pr_kv(logfd: i64, label: *u8, got: i64, exp: i64) -> i64 {
50 gp(logfd, label)
51 gp(logfd, " got=\x00" as *u8); gn(logfd, got)
52 gp(logfd, " exp=\x00" as *u8); gn(logfd, exp)
53 if got == exp { gp(logfd, " OK\n\x00" as *u8); return 1 }
54 gp(logfd, " FAIL\n\x00" as *u8)
55 return 0
56}
57func pr_cond(logfd: i64, label: *u8, cond: i64) -> i64 {
58 gp(logfd, label)
59 if cond != 0 { gp(logfd, " OK\n\x00" as *u8); return 1 }
60 gp(logfd, " FAIL\n\x00" as *u8)
61 return 0
62}
63
64func main() -> i64 {
65 let logfd: i64 = sys_openat_append("knowledge/status/tone_gate.log\x00" as *u8, 0x1a4)
66 gp(logfd, "TONE-GATE W-TONE-1 (voice fingerprint: cadence/diction/signals/punctuation)\n\x00" as *u8)
67
68 let sc: *i64 = sys_mmap(64) as *i64
69 let fpA: *i64 = sys_mmap(128) as *i64
70 let fpB: *i64 = sys_mmap(128) as *i64
71 let fcT: *i64 = sys_mmap(128) as *i64
72 let fcO: *i64 = sys_mmap(128) as *i64
73 var ok: i64 = 1
74
75 // ---- T1: exact fingerprint math ----
76 // "I run. You hide." -> words=4 sentences=2 letters=11
77 // tf[0]=4*1000/2=2000 ; tf[1]=11*1000/4=2750
78 gp(logfd, " [T1] exact dims on \"I run. You hide.\"\n\x00" as *u8)
79 let t1: *u8 = "I run. You hide.\x00" as *u8
80 tn_fingerprint(t1, slen(t1), fpA, sc)
81 if pr_kv(logfd, " cadence tf[0]\x00" as *u8, fpA[0], 2000) == 0 { ok = 0 }
82 if pr_kv(logfd, " wordlen tf[1]\x00" as *u8, fpA[1], 2750) == 0 { ok = 0 }
83
84 // ---- T2: discrimination (terse vs ornate), expected DIRECTION ----
85 gp(logfd, " [T2] NEG-CONTROL discrimination: terse vs ornate\n\x00" as *u8)
86 let terse: *u8 = "He ran. She fell. Night came. They slept.\x00" as *u8
87 let ornate: *u8 = "Gently, almost imperceptibly, the luminous, shimmering twilight descended upon the ancient, sprawling, magnificent city, and everyone marveled quietly.\x00" as *u8
88 tn_fingerprint(terse, slen(terse), fpA, sc)
89 tn_fingerprint(ornate, slen(ornate), fpB, sc)
90 gp(logfd, " terse cadence=\x00" as *u8); gn(logfd, fpA[0])
91 gp(logfd, " adv=\x00" as *u8); gn(logfd, fpA[4]); gp(logfd, "\n\x00" as *u8)
92 gp(logfd, " ornate cadence=\x00" as *u8); gn(logfd, fpB[0])
93 gp(logfd, " adv=\x00" as *u8); gn(logfd, fpB[4]); gp(logfd, "\n\x00" as *u8)
94 var c1: i64 = 0
95 if fpB[0] > fpA[0] { c1 = 1 }
96 if pr_cond(logfd, " ornate cadence > terse cadence\x00" as *u8, c1) == 0 { ok = 0 }
97 var c2: i64 = 0
98 if fpB[4] > fpA[4] { c2 = 1 }
99 if pr_cond(logfd, " ornate adverb-ratio > terse adverb-ratio\x00" as *u8, c2) == 0 { ok = 0 }
100 var c3: i64 = 0
101 if tn_dist(fpA, fpB, TN_DIMS) > 0 { c3 = 1 }
102 if pr_cond(logfd, " dist(terse,ornate) > 0 (not constant)\x00" as *u8, c3) == 0 { ok = 0 }
103
104 // ---- T3: THE CAPABILITY -- match a NEW draft to the target voice ----
105 // target voice A = terse ; contrast voice B = ornate (from T2)
106 gp(logfd, " [T3] match new drafts to target voice A(terse) vs B(ornate)\n\x00" as *u8)
107 let candT: *u8 = "Dawn broke. Birds sang. He woke.\x00" as *u8
108 let candO: *u8 = "Slowly and rather beautifully, the gentle, golden morning light gradually filled the vast, silent, peaceful room.\x00" as *u8
109 tn_fingerprint(candT, slen(candT), fcT, sc)
110 tn_fingerprint(candO, slen(candO), fcO, sc)
111 gp(logfd, " distT->A=\x00" as *u8); gn(logfd, tn_dist(fcT, fpA, TN_DIMS))
112 gp(logfd, " distT->B=\x00" as *u8); gn(logfd, tn_dist(fcT, fpB, TN_DIMS)); gp(logfd, "\n\x00" as *u8)
113 gp(logfd, " distO->A=\x00" as *u8); gn(logfd, tn_dist(fcO, fpA, TN_DIMS))
114 gp(logfd, " distO->B=\x00" as *u8); gn(logfd, tn_dist(fcO, fpB, TN_DIMS)); gp(logfd, "\n\x00" as *u8)
115 if pr_kv(logfd, " terse draft matches A\x00" as *u8, tn_closer(fcT, fpA, fpB, TN_DIMS), 1) == 0 { ok = 0 }
116 if pr_kv(logfd, " ornate draft NOT matches A\x00" as *u8, tn_closer(fcO, fpA, fpB, TN_DIMS), 0) == 0 { ok = 0 }
117
118 // ---- T4: metric sanity / neg-control ----
119 gp(logfd, " [T4] metric sanity: dist(A,A)==0, A matches itself\n\x00" as *u8)
120 if pr_kv(logfd, " dist(A,A)\x00" as *u8, tn_dist(fpA, fpA, TN_DIMS), 0) == 0 { ok = 0 }
121 if pr_kv(logfd, " A closer to A than B\x00" as *u8, tn_closer(fpA, fpA, fpB, TN_DIMS), 1) == 0 { ok = 0 }
122
123 if ok == 1 {
124 gp(logfd, "TONE-GATE result=ALL-PASS verdict=GREEN\n\x00" as *u8)
125 if logfd > 0 { sys_close(logfd) }
126 sys_exit(0)
127 return 0
128 }
129 gp(logfd, "TONE-GATE result=FAIL verdict=RED\n\x00" as *u8)
130 if logfd > 0 { sys_close(logfd) }
131 sys_exit(1)
132 return 1
133}