nx_dialogue_gate.nx source
↩ module page · 157 lines · 8901 B
1// nx_dialogue_gate.nx -- REFEREE for WRITING rung W-DLG-1 (nx_dialogue).
2//
3// [T1] turn + session counting and side identification.
4// [T2] per-turn intensity scoring is exact (benign proxy lexicon, per the seam).
5// [T3] all four cells, one session of each kind -> exact cell id.
6// [T4] THE HEADLINE METRIC on a 4-session corpus: tally = [1,1,1,1], unprompted rate = 250 permil,
7// and the PARTITION SUMS to the session count (a partition is a claim: check the parts).
8// [T5] first-explicit-turn index + NEG-CONTROL (a clean session returns -1, not 0).
9// [T6] escalation slope: rising positive, flat zero, falling NEGATIVE (sign discriminates).
10// [T7] NEG-CONTROL the detector can FAIL TO FIRE: raise the threshold above every score and the same
11// corpus collapses to 4 NEITHER -- proving the meter is not a constant that always accuses.
12// [T8] whole-word: a longer word CONTAINING a lexicon term does not score (heated != overheated).
13// [T9] division guard: rate over an empty corpus is 0, not a trap.
14//
15// Evidence -> stdout + knowledge/status/dialogue_gate.log. Exit 0 GREEN / 1 RED.
16// Sovereign x86_64. license_tier: ORIGINAL expect_exit: 0
17import "nx_syscalls_x86_64.nx"
18import "nx_dialogue.nx"
19
20func gp(logfd: i64, s: *u8) -> i64 {
21 var n: i64 = 0
22 while s[n] != (0 as u8) { n = n + 1 }
23 sys_write(1, s, n)
24 if logfd > 0 { sys_write(logfd, s, n) }
25 return 0
26}
27func gn(logfd: i64, v: i64) -> i64 {
28 var m: i64 = v
29 if m < 0 {
30 sys_write(1, "-\x00" as *u8, 1)
31 if logfd > 0 { sys_write(logfd, "-\x00" as *u8, 1) }
32 m = 0 - m
33 }
34 let t: *u8 = sys_mmap(32)
35 let b: *u8 = sys_mmap(32)
36 var k: i64 = 0
37 if m == 0 { t[0] = 48 as u8; k = 1 }
38 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
39 var i: i64 = 0
40 while i < k { b[i] = t[k - 1 - i]; i = i + 1 }
41 sys_write(1, b, k)
42 if logfd > 0 { sys_write(logfd, b, k) }
43 return 0
44}
45func slen(s: *u8) -> i64 {
46 var n: i64 = 0
47 while s[n] != (0 as u8) { n = n + 1 }
48 return n
49}
50func pr(logfd: i64, label: *u8, got: i64, exp: i64) -> i64 {
51 gp(logfd, label)
52 gp(logfd, " got=\x00" as *u8); gn(logfd, got)
53 gp(logfd, " exp=\x00" as *u8); gn(logfd, exp)
54 if got == exp { gp(logfd, " OK\n\x00" as *u8); return 1 }
55 gp(logfd, " FAIL\n\x00" as *u8)
56 return 0
57}
58
59func main() -> i64 {
60 let logfd: i64 = sys_openat_append("knowledge/status/dialogue_gate.log\x00" as *u8, 0x1a4)
61 gp(logfd, "DIALOGUE-GATE W-DLG-1 (turns / scoring / 4 cells / headline rate / first-turn / slope / neg / whole-word / guard)\n\x00" as *u8)
62 var ok: i64 = 1
63
64 // BENIGN PROXY LEXICON -- the seam: this gate proves the MATH, never authors explicit content.
65 // packed null-terminated terms + parallel weights, exactly the nx_register contract.
66 let terms: *u8 = "calm\x00heated\x00graphic\x00\x00" as *u8
67 let w: *i64 = sys_mmap(64) as *i64
68 w[0] = 0; w[1] = 3; w[2] = 5
69 let NT: i64 = 3
70 let THR: i64 = 5
71
72 // ---- T1: counting ----
73 gp(logfd, " [T1] turn/session counting\n\x00" as *u8)
74 let t1: *u8 = "SESSION\nUSER\nhello there\nBOT\na calm reply\nUSER\nstill calm\n\x00" as *u8
75 let n1: i64 = slen(t1)
76 if pr(logfd, " turns\x00" as *u8, dl_count_turns(t1, n1), 3) == 0 { ok = 0 }
77 if pr(logfd, " sessions\x00" as *u8, dl_count_sessions(t1, n1), 1) == 0 { ok = 0 }
78 if pr(logfd, " marker side at 8 (USER)\x00" as *u8, dl_marker_side(t1, n1, 8), DL_SIDE_USER) == 0 { ok = 0 }
79
80 // ---- T2: per-turn scoring exact ----
81 gp(logfd, " [T2] per-turn intensity (calm=0 heated=3 graphic=5)\n\x00" as *u8)
82 let t2: *u8 = "SESSION\nUSER\ncalm calm\nBOT\nheated graphic\n\x00" as *u8
83 let n2: i64 = slen(t2)
84 if pr(logfd, " user max\x00" as *u8, dl_side_max(t2, n2, 0, n2, DL_SIDE_USER, terms, w, NT), 0) == 0 { ok = 0 }
85 if pr(logfd, " bot max\x00" as *u8, dl_side_max(t2, n2, 0, n2, DL_SIDE_BOT, terms, w, NT), 8) == 0 { ok = 0 }
86
87 // ---- T3: all four cells ----
88 gp(logfd, " [T3] four-cell classification (prose semantics, not the paper's corrupted symbols)\n\x00" as *u8)
89 let cn: *u8 = "SESSION\nUSER\ncalm\nBOT\ncalm\n\x00" as *u8
90 let cu: *u8 = "SESSION\nUSER\ncalm\nBOT\ngraphic\n\x00" as *u8
91 let cd: *u8 = "SESSION\nUSER\ngraphic\nBOT\ncalm\n\x00" as *u8
92 let cm: *u8 = "SESSION\nUSER\ngraphic\nBOT\ngraphic\n\x00" as *u8
93 if pr(logfd, " neither\x00" as *u8, dl_cell(cn, slen(cn), 0, slen(cn), THR, terms, w, NT), DL_CELL_NEITHER) == 0 { ok = 0 }
94 if pr(logfd, " unprompted\x00" as *u8, dl_cell(cu, slen(cu), 0, slen(cu), THR, terms, w, NT), DL_CELL_UNPROMPTED) == 0 { ok = 0 }
95 if pr(logfd, " declined\x00" as *u8, dl_cell(cd, slen(cd), 0, slen(cd), THR, terms, w, NT), DL_CELL_DECLINED) == 0 { ok = 0 }
96 if pr(logfd, " mutual\x00" as *u8, dl_cell(cm, slen(cm), 0, slen(cm), THR, terms, w, NT), DL_CELL_MUTUAL) == 0 { ok = 0 }
97
98 // ---- T4: the headline metric + partition integrity ----
99 gp(logfd, " [T4] corpus tally, unprompted rate, partition sum\n\x00" as *u8)
100 let corp: *u8 = "SESSION\nUSER\ncalm\nBOT\ncalm\nSESSION\nUSER\ncalm\nBOT\ngraphic\nSESSION\nUSER\ngraphic\nBOT\ncalm\nSESSION\nUSER\ngraphic\nBOT\ngraphic\n\x00" as *u8
101 let cnn: i64 = slen(corp)
102 let out: *i64 = sys_mmap(64) as *i64
103 let ns: i64 = dl_tally(corp, cnn, THR, terms, w, NT, out)
104 if pr(logfd, " sessions\x00" as *u8, ns, 4) == 0 { ok = 0 }
105 if pr(logfd, " cell neither\x00" as *u8, out[0], 1) == 0 { ok = 0 }
106 if pr(logfd, " cell unprompted\x00" as *u8, out[1], 1) == 0 { ok = 0 }
107 if pr(logfd, " cell declined\x00" as *u8, out[2], 1) == 0 { ok = 0 }
108 if pr(logfd, " cell mutual\x00" as *u8, out[3], 1) == 0 { ok = 0 }
109 if pr(logfd, " PARTITION SUM == sessions\x00" as *u8, out[0] + out[1] + out[2] + out[3], ns) == 0 { ok = 0 }
110 if pr(logfd, " unprompted rate permil\x00" as *u8, dl_rate_permil(out[1], ns), 250) == 0 { ok = 0 }
111 if pr(logfd, " honoured-decline rate permil\x00" as *u8, dl_rate_permil(out[2], ns), 250) == 0 { ok = 0 }
112
113 // ---- T5: first-explicit-turn + NEG ----
114 gp(logfd, " [T5] first-explicit-turn index (the ruler structurally cannot compute this)\n\x00" as *u8)
115 let esc: *u8 = "SESSION\nUSER\ncalm\nBOT\ncalm\nUSER\ncalm\nBOT\ngraphic\n\x00" as *u8
116 let en: i64 = slen(esc)
117 if pr(logfd, " bot first-explicit turn\x00" as *u8, dl_first_explicit(esc, en, 0, en, DL_SIDE_BOT, THR, terms, w, NT), 1) == 0 { ok = 0 }
118 if pr(logfd, " NEG user never explicit\x00" as *u8, dl_first_explicit(esc, en, 0, en, DL_SIDE_USER, THR, terms, w, NT), 0 - 1) == 0 { ok = 0 }
119
120 // ---- T6: slope sign discriminates ----
121 gp(logfd, " [T6] escalation slope (milli-intensity per turn)\n\x00" as *u8)
122 let rise: *u8 = "SESSION\nBOT\ncalm\nBOT\nheated\nBOT\ngraphic\n\x00" as *u8
123 let flat: *u8 = "SESSION\nBOT\nheated\nBOT\nheated\nBOT\nheated\n\x00" as *u8
124 let fall: *u8 = "SESSION\nBOT\ngraphic\nBOT\nheated\nBOT\ncalm\n\x00" as *u8
125 if pr(logfd, " rising\x00" as *u8, dl_slope(rise, slen(rise), 0, slen(rise), DL_SIDE_BOT, terms, w, NT), 2500) == 0 { ok = 0 }
126 if pr(logfd, " flat\x00" as *u8, dl_slope(flat, slen(flat), 0, slen(flat), DL_SIDE_BOT, terms, w, NT), 0) == 0 { ok = 0 }
127 if pr(logfd, " NEG falling\x00" as *u8, dl_slope(fall, slen(fall), 0, slen(fall), DL_SIDE_BOT, terms, w, NT), 0 - 2500) == 0 { ok = 0 }
128
129 // ---- T7: NEG-CONTROL the meter can fail to fire ----
130 gp(logfd, " [T7] NEG-CONTROL: threshold above every score -> the meter must go quiet, not accuse\n\x00" as *u8)
131 let out2: *i64 = sys_mmap(64) as *i64
132 let ns2: i64 = dl_tally(corp, cnn, 999, terms, w, NT, out2)
133 if pr(logfd, " all-neither\x00" as *u8, out2[0], 4) == 0 { ok = 0 }
134 if pr(logfd, " unprompted now 0\x00" as *u8, out2[1], 0) == 0 { ok = 0 }
135 if pr(logfd, " partition still sums\x00" as *u8, out2[0] + out2[1] + out2[2] + out2[3], ns2) == 0 { ok = 0 }
136
137 // ---- T8: whole-word ----
138 gp(logfd, " [T8] NEG-CONTROL whole-word: a longer word containing a term must not score\n\x00" as *u8)
139 let ww: *u8 = "SESSION\nUSER\noverheated graphically\nBOT\ncalm\n\x00" as *u8
140 let wn: i64 = slen(ww)
141 if pr(logfd, " user max (overheated/graphically ignored)\x00" as *u8, dl_side_max(ww, wn, 0, wn, DL_SIDE_USER, terms, w, NT), 0) == 0 { ok = 0 }
142
143 // ---- T9: division guard ----
144 gp(logfd, " [T9] division guard\n\x00" as *u8)
145 if pr(logfd, " rate over empty corpus\x00" as *u8, dl_rate_permil(0, 0), 0) == 0 { ok = 0 }
146
147 if ok == 1 {
148 gp(logfd, "DIALOGUE-GATE result=ALL-PASS verdict=GREEN\n\x00" as *u8)
149 if logfd > 0 { sys_close(logfd) }
150 sys_exit(0)
151 return 0
152 }
153 gp(logfd, "DIALOGUE-GATE result=FAIL verdict=RED\n\x00" as *u8)
154 if logfd > 0 { sys_close(logfd) }
155 sys_exit(1)
156 return 1
157}