nx_writer_run.nx source
↩ module page · 197 lines · 9288 B
1// nx_writer_run.nx -- WRITING arc, rung W-RUN-2: the UNIFYING file-driven runner.
2// One tool, every mode. Reads the chosen mode + the mode registry, ENFORCES the
3// seam (refuses if the registry routes an explicit mode off the local lane),
4// resolves register/lane/structure, and dispatches to the right organ -- emitting
5// the structured artifact the generation SEAT then expands (SFW=tutor seat,
6// explicit=operator local-LLM seat; this tool authors no prose, per the seam).
7//
8// Does NOT touch the existing nx_write_run (its scene-ingestion path is unchanged).
9// Staging: knowledge/staging/writer/{mode,scenario,manga,voice,draft}.txt ;
10// registry SSOT: knowledge/registry/write_modes.tsv . Build+run:
11// ./_offc/nx_sov_build_run.elf nx_writer_run
12//
13// license_tier: ORIGINAL
14// module: nishi-core.write.writerrun
15// depends: nishi-core.write.mode/scenario/manga/tone
16// capability: WRITE_RUN_UNIFIED
17import "nx_syscalls_x86_64.nx"
18import "nx_mode.nx"
19import "nx_scenario.nx"
20import "nx_manga.nx"
21import "nx_tone.nx"
22
23func wstr(outfd: i64, s: *u8) -> i64 {
24 var n: i64 = 0
25 while s[n] != (0 as u8) { n = n + 1 }
26 sys_write(1, s, n)
27 if outfd > 0 { sys_write(outfd, s, n) }
28 return 0
29}
30func wnum(outfd: i64, v: i64) -> i64 {
31 var m: i64 = v
32 if m < 0 { wstr(outfd, "-\x00" as *u8); m = 0 - m }
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 bb[k] = 0 as u8
41 wstr(outfd, bb)
42 return 0
43}
44// null-terminate buf at the first newline; return length
45func wr_firstline(buf: *u8, n: i64) -> i64 {
46 var i: i64 = 0
47 var done: i64 = 0
48 while i < n {
49 if done == 0 {
50 if buf[i] == (10 as u8) { buf[i] = 0 as u8; done = 1 }
51 else { if buf[i] == (13 as u8) { buf[i] = 0 as u8; done = 1 } else { i = i + 1 } }
52 } else { i = n }
53 }
54 if done == 0 { buf[n] = 0 as u8 }
55 var ln: i64 = 0
56 while buf[ln] != (0 as u8) { ln = ln + 1 }
57 return ln
58}
59
60func wr_do_scenario(outfd: i64, lanb: *u8) -> i64 {
61 let lenp: *i64 = sys_mmap(16) as *i64
62 let sb: *u8 = sys_read_file("knowledge/staging/writer/scenario.txt\x00" as *u8, lenp)
63 if sb == (0 as *u8) { wstr(outfd, " (provide scenario.txt: kind/setting/roles/goal lines)\n\x00" as *u8); return 0 }
64 let sn: i64 = lenp[0]
65 let miss: i64 = sc_validate(sb, sn)
66 if miss > 0 { wstr(outfd, " REJECT incomplete scenario; required fields missing: \x00" as *u8); wnum(outfd, miss); wstr(outfd, "\n\x00" as *u8); return 0 }
67 let brief: *u8 = sys_mmap(1024)
68 sc_emit_brief(sb, sn, brief, 1024)
69 wstr(outfd, " BRIEF: \x00" as *u8); wstr(outfd, brief)
70 wstr(outfd, "\n -> hand to the \x00" as *u8); wstr(outfd, lanb); wstr(outfd, " seat to expand into prose.\n\x00" as *u8)
71 return 0
72}
73
74func wr_do_convo(outfd: i64, lanb: *u8) -> i64 {
75 let lenp: *i64 = sys_mmap(16) as *i64
76 let sb: *u8 = sys_read_file("knowledge/staging/writer/scenario.txt\x00" as *u8, lenp)
77 if sb == (0 as *u8) { wstr(outfd, " (provide scenario.txt as the situation to practise)\n\x00" as *u8); return 0 }
78 let sn: i64 = lenp[0]
79 if sc_validate(sb, sn) > 0 { wstr(outfd, " REJECT incomplete situation\n\x00" as *u8); return 0 }
80 let brief: *u8 = sys_mmap(1024)
81 sc_emit_brief(sb, sn, brief, 1024)
82 wstr(outfd, " SITUATION: \x00" as *u8); wstr(outfd, brief)
83 wstr(outfd, "\n -> \x00" as *u8); wstr(outfd, lanb)
84 wstr(outfd, " seat keeps topics going (nx_convo cv_next_topic) + scores attempts respect-gated (cv_rubric).\n\x00" as *u8)
85 return 0
86}
87
88func wr_do_manga(outfd: i64, lanb: *u8) -> i64 {
89 let lenp: *i64 = sys_mmap(16) as *i64
90 let mb: *u8 = sys_read_file("knowledge/staging/writer/manga.txt\x00" as *u8, lenp)
91 if mb == (0 as *u8) { wstr(outfd, " (provide manga.txt: PAGE/PANEL + shot/char/say/sfx/caption)\n\x00" as *u8); return 0 }
92 let mn: i64 = lenp[0]
93 wstr(outfd, " pages: \x00" as *u8); wnum(outfd, mg_count_pages(mb, mn))
94 wstr(outfd, " panels: \x00" as *u8); wnum(outfd, mg_count_panels(mb, mn))
95 wstr(outfd, " missing-shot: \x00" as *u8); wnum(outfd, mg_validate(mb, mn)); wstr(outfd, "\n\x00" as *u8)
96 let pr: *u8 = sys_mmap(512)
97 mg_emit_panel_prompt(mb, mn, 0, pr, 512)
98 wstr(outfd, " panel-0 img: \x00" as *u8); wstr(outfd, pr)
99 wstr(outfd, "\n -> image-gen + \x00" as *u8); wstr(outfd, lanb); wstr(outfd, " seat for dialogue.\n\x00" as *u8)
100 return 0
101}
102
103func wr_do_tone(outfd: i64, lanb: *u8) -> i64 {
104 let lenp: *i64 = sys_mmap(16) as *i64
105 let vb: *u8 = sys_read_file("knowledge/staging/writer/voice.txt\x00" as *u8, lenp)
106 if vb == (0 as *u8) { wstr(outfd, " (provide voice.txt: a sample of your own writing)\n\x00" as *u8); return 0 }
107 let vn: i64 = lenp[0]
108 let sc: *i64 = sys_mmap(64) as *i64
109 let tfV: *i64 = sys_mmap(128) as *i64
110 tn_fingerprint(vb, vn, tfV, sc)
111 wstr(outfd, " voice fingerprinted (cadence dim0=\x00" as *u8); wnum(outfd, tfV[0]); wstr(outfd, ")\n\x00" as *u8)
112 let lenp2: *i64 = sys_mmap(16) as *i64
113 let db: *u8 = sys_read_file("knowledge/staging/writer/draft.txt\x00" as *u8, lenp2)
114 if db == (0 as *u8) { wstr(outfd, " (add draft.txt to score a draft against your voice)\n\x00" as *u8); return 0 }
115 let dn: i64 = lenp2[0]
116 let tfD: *i64 = sys_mmap(128) as *i64
117 tn_fingerprint(db, dn, tfD, sc)
118 let wun: *i64 = sys_mmap(128) as *i64
119 var wi: i64 = 0
120 while wi < TN_DIMS { wun[wi] = 1000; wi = wi + 1 }
121 let drf: *i64 = sys_mmap(128) as *i64
122 let dist: i64 = tn_drift(tfD, tfV, wun, TN_DIMS, drf)
123 wstr(outfd, " draft voice-distance: \x00" as *u8); wnum(outfd, dist); wstr(outfd, "\n\x00" as *u8)
124 var bi: i64 = 0
125 var bv: i64 = 0
126 var di: i64 = 0
127 while di < TN_DIMS {
128 var a: i64 = drf[di]
129 if a < 0 { a = 0 - a }
130 if a > bv { bv = a; bi = di }
131 di = di + 1
132 }
133 wstr(outfd, " largest drift on dim \x00" as *u8); wnum(outfd, bi)
134 wstr(outfd, " (signed \x00" as *u8); wnum(outfd, drf[bi]); wstr(outfd, ")\n -> \x00" as *u8); wstr(outfd, lanb)
135 wstr(outfd, " seat: steer that dim toward 0 to match your voice.\n\x00" as *u8)
136 return 0
137}
138
139func main() -> i64 {
140 let lenp: *i64 = sys_mmap(16) as *i64
141 let modeb: *u8 = sys_read_file("knowledge/staging/writer/mode.txt\x00" as *u8, lenp)
142 if modeb == (0 as *u8) {
143 wstr(0, "no mode.txt -- put one of correspondence/manga/erotica/paper/scenario/language/social in knowledge/staging/writer/mode.txt\n\x00" as *u8)
144 sys_exit(0); return 0
145 }
146 wr_firstline(modeb, lenp[0])
147
148 let regb: *u8 = sys_read_file("knowledge/registry/write_modes.tsv\x00" as *u8, lenp)
149 if regb == (0 as *u8) { wstr(0, "no write_modes.tsv registry\n\x00" as *u8); sys_exit(1); return 1 }
150 let rn: i64 = lenp[0]
151
152 let outfd: i64 = sys_openat_wr("knowledge/staging/writer/out.txt\x00" as *u8, 0x1a4)
153 let scratch: *u8 = sys_mmap(256)
154
155 wstr(outfd, "== nishi-writer ==\nmode: \x00" as *u8); wstr(outfd, modeb); wstr(outfd, "\n\x00" as *u8)
156
157 // GOVERNANCE: the seam audit is a HARD gate
158 let viol: i64 = md_seam_audit(regb, rn, scratch, 256)
159 wstr(outfd, "seam-audit violations: \x00" as *u8); wnum(outfd, viol); wstr(outfd, "\n\x00" as *u8)
160 if viol > 0 {
161 wstr(outfd, "REFUSE: registry routes an explicit mode to a non-local lane (seam violation)\n\x00" as *u8)
162 if outfd > 0 { sys_close(outfd) }
163 sys_exit(2); return 2
164 }
165
166 let lanb: *u8 = sys_mmap(64)
167 let rgb: *u8 = sys_mmap(64)
168 let stb: *u8 = sys_mmap(64)
169 if md_lane(regb, rn, modeb, lanb, 64) < 0 {
170 wstr(outfd, "unknown mode (not in write_modes.tsv)\n\x00" as *u8)
171 if outfd > 0 { sys_close(outfd) }
172 sys_exit(0); return 0
173 }
174 md_register(regb, rn, modeb, rgb, 64)
175 md_structure(regb, rn, modeb, stb, 64)
176 wstr(outfd, "register: \x00" as *u8); wstr(outfd, rgb)
177 wstr(outfd, " lane: \x00" as *u8); wstr(outfd, lanb)
178 wstr(outfd, " structure: \x00" as *u8); wstr(outfd, stb); wstr(outfd, "\n\x00" as *u8)
179
180 var handled: i64 = 0
181 if md_streq(stb, "scenario\x00" as *u8) == 1 { wr_do_scenario(outfd, lanb); handled = 1 }
182 if md_streq(stb, "convo\x00" as *u8) == 1 { wr_do_convo(outfd, lanb); handled = 1 }
183 if md_streq(stb, "panel\x00" as *u8) == 1 { wr_do_manga(outfd, lanb); handled = 1 }
184 if md_streq(stb, "tone\x00" as *u8) == 1 { wr_do_tone(outfd, lanb); handled = 1 }
185 if md_streq(stb, "scene\x00" as *u8) == 1 {
186 wstr(outfd, " register=explicit -> routed to the LOCAL seat (operator hardware). Engine structures via nx_ingest/nx_register (nx_write_run); Claude authors no explicit prose.\n\x00" as *u8)
187 handled = 1
188 }
189 if md_streq(stb, "imrad\x00" as *u8) == 1 {
190 wstr(outfd, " paper structure -> use nx_cite for references; hand the outline to the \x00" as *u8); wstr(outfd, lanb); wstr(outfd, " seat.\n\x00" as *u8)
191 handled = 1
192 }
193 if handled == 0 { wstr(outfd, " structure not yet dispatched; hand the DATA to the seat.\n\x00" as *u8) }
194
195 if outfd > 0 { sys_close(outfd) }
196 sys_exit(0); return 0
197}