nx_writecraft_gate.nx source
↩ module page · 145 lines · 6664 B
1// nx_writecraft_gate.nx -- REFEREE for WRITING rung W-R0 (nx_writecraft).
2//
3// Proves the prose-measurement bedrock with no mocks:
4// [T1] exact word/sentence/syllable/letter/complex counts on a known sentence
5// [T2] exact syllable heuristic on hand-verified words (incl. silent-e + "Cle")
6// [T3] exact readability milli-values (FRE / FKGL / FOG) on T1
7// [T5] div-by-zero guard: text with no end punctuation still scores (S->1)
8// [T4] NEGATIVE CONTROL: graduate-jargon text MUST score a higher grade and a
9// lower reading-ease than a simple sentence -- the metric discriminates,
10// it does not return a constant. A constant-returning organ fails here.
11//
12// Evidence -> stdout + knowledge/status/writecraft_gate.log. Exit 0 GREEN / 1 RED.
13// Sovereign x86_64 throughout. license_tier: ORIGINAL
14import "nx_syscalls_x86_64.nx"
15import "nx_writecraft.nx"
16
17func gp(logfd: i64, s: *u8) -> i64 {
18 var n: i64 = 0
19 while s[n] != (0 as u8) { n = n + 1 }
20 sys_write(1, s, n)
21 if logfd > 0 { sys_write(logfd, s, n) }
22 return 0
23}
24func gn(logfd: i64, v: i64) -> i64 {
25 var m: i64 = v
26 if m < 0 {
27 sys_write(1, "-\x00" as *u8, 1)
28 if logfd > 0 { sys_write(logfd, "-\x00" as *u8, 1) }
29 m = 0 - m
30 }
31 let bb: *u8 = sys_mmap(32)
32 let t: *u8 = sys_mmap(32)
33 var k: i64 = 0
34 if m == 0 { t[0] = 48 as u8; k = 1 }
35 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
36 var i: i64 = 0
37 while i < k { bb[i] = t[k - 1 - i]; i = i + 1 }
38 sys_write(1, bb, k)
39 if logfd > 0 { sys_write(logfd, bb, k) }
40 return 0
41}
42func slen(s: *u8) -> i64 {
43 var n: i64 = 0
44 while s[n] != (0 as u8) { n = n + 1 }
45 return n
46}
47// print "label got=G exp=E [OK|FAIL]"; return 1 if G==E
48func pr_kv(logfd: i64, label: *u8, got: i64, exp: i64) -> i64 {
49 gp(logfd, label)
50 gp(logfd, " got=\x00" as *u8); gn(logfd, got)
51 gp(logfd, " exp=\x00" as *u8); gn(logfd, exp)
52 if got == exp { gp(logfd, " OK\n\x00" as *u8); return 1 }
53 gp(logfd, " FAIL\n\x00" as *u8)
54 return 0
55}
56// print "label [OK|FAIL]"; return cond
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/writecraft_gate.log\x00" as *u8, 0x1a4)
66 gp(logfd, "WRITECRAFT-GATE W-R0 (prose metrics: counts + readability)\n\x00" as *u8)
67
68 let out: *i64 = sys_mmap(64) as *i64
69 var ok: i64 = 1
70
71 // ---- T1: exact counts ----
72 gp(logfd, " [T1] counts of \"The cat sat on the mat.\"\n\x00" as *u8)
73 let t1: *u8 = "The cat sat on the mat.\x00" as *u8
74 wc_counts(t1, slen(t1), out)
75 if pr_kv(logfd, " letters\x00" as *u8, out[0], 17) == 0 { ok = 0 }
76 if pr_kv(logfd, " words\x00" as *u8, out[1], 6) == 0 { ok = 0 }
77 if pr_kv(logfd, " sentences\x00" as *u8, out[2], 1) == 0 { ok = 0 }
78 if pr_kv(logfd, " syllables\x00" as *u8, out[3], 6) == 0 { ok = 0 }
79 if pr_kv(logfd, " complex\x00" as *u8, out[4], 0) == 0 { ok = 0 }
80
81 // ---- T3: exact readability on T1 (out still holds T1 counts) ----
82 gp(logfd, " [T3] readability milli-units of T1\n\x00" as *u8)
83 if pr_kv(logfd, " FRE_milli\x00" as *u8, wc_flesch_reading_ease(out), 116145) == 0 { ok = 0 }
84 if pr_kv(logfd, " FKGL_milli\x00" as *u8, wc_fk_grade(out), 0 - 1450) == 0 { ok = 0 }
85 if pr_kv(logfd, " FOG_milli\x00" as *u8, wc_gunning_fog(out), 2400) == 0 { ok = 0 }
86
87 // ---- T2: syllable heuristic, hand-verified ----
88 gp(logfd, " [T2] syllable heuristic\n\x00" as *u8)
89 let wa: *u8 = "cat\x00" as *u8
90 if pr_kv(logfd, " cat\x00" as *u8, wc_syllables(wa, 0, slen(wa)), 1) == 0 { ok = 0 }
91 let wb: *u8 = "apple\x00" as *u8
92 if pr_kv(logfd, " apple\x00" as *u8, wc_syllables(wb, 0, slen(wb)), 2) == 0 { ok = 0 }
93 let wc: *u8 = "table\x00" as *u8
94 if pr_kv(logfd, " table\x00" as *u8, wc_syllables(wc, 0, slen(wc)), 2) == 0 { ok = 0 }
95 let wd: *u8 = "make\x00" as *u8
96 if pr_kv(logfd, " make\x00" as *u8, wc_syllables(wd, 0, slen(wd)), 1) == 0 { ok = 0 }
97 let we: *u8 = "readability\x00" as *u8
98 if pr_kv(logfd, " readability\x00" as *u8, wc_syllables(we, 0, slen(we)), 5) == 0 { ok = 0 }
99
100 // ---- T5: guard against div-by-zero when no end punctuation ----
101 gp(logfd, " [T5] guard (no end punctuation -> S treated as 1)\n\x00" as *u8)
102 let t5: *u8 = "hello world\x00" as *u8
103 wc_counts(t5, slen(t5), out)
104 if pr_kv(logfd, " words\x00" as *u8, out[1], 2) == 0 { ok = 0 }
105 if pr_kv(logfd, " sentences\x00" as *u8, out[2], 0) == 0 { ok = 0 }
106 if pr_kv(logfd, " FKGL_milli(guarded)\x00" as *u8, wc_fk_grade(out), 2890) == 0 { ok = 0 }
107
108 // ---- T4: NEGATIVE CONTROL -- jargon must score harder than simple ----
109 gp(logfd, " [T4] NEG-CONTROL discrimination (jargon vs simple)\n\x00" as *u8)
110 let simp: *u8 = "The cat sat on the mat.\x00" as *u8
111 wc_counts(simp, slen(simp), out)
112 let fk_s: i64 = wc_fk_grade(out)
113 let fre_s: i64 = wc_flesch_reading_ease(out)
114 let cx: *u8 = "The unconscionable bureaucratic methodology necessitated extraordinarily complicated documentation procedures.\x00" as *u8
115 wc_counts(cx, slen(cx), out)
116 let fk_c: i64 = wc_fk_grade(out)
117 let fre_c: i64 = wc_flesch_reading_ease(out)
118 gp(logfd, " simple FKGL_milli=\x00" as *u8); gn(logfd, fk_s)
119 gp(logfd, " FRE_milli=\x00" as *u8); gn(logfd, fre_s); gp(logfd, "\n\x00" as *u8)
120 gp(logfd, " jargon FKGL_milli=\x00" as *u8); gn(logfd, fk_c)
121 gp(logfd, " FRE_milli=\x00" as *u8); gn(logfd, fre_c); gp(logfd, "\n\x00" as *u8)
122 var c1: i64 = 0
123 if fk_c > fk_s { c1 = 1 }
124 if pr_cond(logfd, " jargon grade > simple grade\x00" as *u8, c1) == 0 { ok = 0 }
125 var c2: i64 = 0
126 if fk_c > 12000 { c2 = 1 }
127 if pr_cond(logfd, " jargon grade > 12 (college+)\x00" as *u8, c2) == 0 { ok = 0 }
128 var c3: i64 = 0
129 if fre_c < fre_s { c3 = 1 }
130 if pr_cond(logfd, " jargon reading-ease < simple\x00" as *u8, c3) == 0 { ok = 0 }
131 var c4: i64 = 0
132 if fre_c < 0 { c4 = 1 }
133 if pr_cond(logfd, " jargon reading-ease < 0 (very hard)\x00" as *u8, c4) == 0 { ok = 0 }
134
135 if ok == 1 {
136 gp(logfd, "WRITECRAFT-GATE result=ALL-PASS verdict=GREEN\n\x00" as *u8)
137 if logfd > 0 { sys_close(logfd) }
138 sys_exit(0)
139 return 0
140 }
141 gp(logfd, "WRITECRAFT-GATE result=FAIL verdict=RED\n\x00" as *u8)
142 if logfd > 0 { sys_close(logfd) }
143 sys_exit(1)
144 return 1
145}