code wiki / _hdl_build / nx_sponsor_core.nx
nx_sponsor_core.nx source
↩ module page · 253 lines · 9310 B
1// nx_sponsor_core.nx -- EXECUTIVE-SPONSOR decision core (mainless; operator
2// doctrine 2026-06-11): the sponsor is NOT a gate on objective work -- parity
3// rungs, kernels, conformance counts flow TODO->DONE on evidence with no
4// human in the path. The sponsor's viewpoint enters at exactly TWO points:
5// spec time: NOVEL rows tagged ||ASK=spec (subjective feature choices --
6// "the novel needs us both", sponsor + tutor co-spec batch)
7// result time: rows tagged ||ASK=result reach VIEW (flipped by reconcile
8// when their objective evidence is green) and wait for a
9// VIEWPOINT: APPROVE -> DONE; DENY+feedback -> NOVEL (iterate)
10// with the feedback appended to planning_prefs.tsv -- the
11// registry future planning READS (preference learning as DATA).
12// Decisions arrive via knowledge/registry/sponsor_decisions.tsv (hand rows
13// today; the nishifamily intake form lands the same contract later):
14// APPROVE <id>
15// DENY <id> <feedback...>
16// Transitions are a strict state machine (only VIEW moves); anything else =
17// loud REFUSED row, repeated decisions = silent no-ops (idempotent).
18// license_tier: ORIGINAL
19
20import "nx_wd_core.nx"
21const K_MAGIC_65536: i64 = 65536
22const K_MAGIC_65535: i64 = 65535
23
24// status byte of id in queue (fresh load each call; flips rewrite the file)
25func sr_status(qp: *u8, id: *u8) -> i64 {
26 let cx: *i64 = an_newcx()
27 if an_load(qp, cx) <= 0 { return 0 - 1 }
28 let r: i64 = an_find(cx, id)
29 if r < 0 { return 0 - 1 }
30 let st: *i64 = cx[4] as *i64
31 return st[r]
32}
33
34// every decision's reason is TRAINING DATA (operator 2026-06-11: "capture
35// feedback on all decisions so we can use it to train and improve the team
36// system including the ai pieces") -- PREF rows carry the verb too.
37func sr_pref_append(prefs: *u8, id: *u8, action: *u8, fb: *u8) -> i64 {
38 let fd: i64 = sys_openat_append(prefs, 0x1a4)
39 if fd < 0 { return 0 }
40 wd_w(fd, "PREF id=" as *u8)
41 wd_w(fd, id)
42 wd_w(fd, " action=" as *u8)
43 wd_w(fd, action)
44 wd_w(fd, " feedback=" as *u8)
45 wd_w(fd, fb)
46 wd_w(fd, " epoch=" as *u8)
47 wd_wn(fd, sys_now_realtime_sec())
48 wd_w(fd, "\n" as *u8)
49 sys_close(fd)
50 return 1
51}
52
53// apply one decision; returns 1 applied, 0 no-op, -1 refused (loud).
54// verbs: 1=APPROVE (VIEW->DONE, ship), 2=REWORK (VIEW->NOVEL, refine the
55// deliverable), 3=RESTART (VIEW->NOVEL, re-spec from zero -- the distinction
56// from REWORK lives in the learned action), 4=DENY (VIEW->KILLED, terminal
57// business kill: duplicative / no ROI; never resurrected by evidence).
58func sr_apply(qp: *u8, prefs: *u8, verb: i64, id: *u8, fb: *u8) -> i64 {
59 let st: i64 = sr_status(qp, id)
60 if st < 0 { return 0 - 1 }
61 if verb == 1 {
62 if st == 86 {
63 wd_flip_status(qp, id, "DONE" as *u8)
64 sr_pref_append(prefs, id, "APPROVE" as *u8, fb)
65 return 1
66 }
67 if st == 68 { return 0 }
68 return 0 - 1
69 }
70 if verb == 2 {
71 if st == 86 {
72 wd_flip_status(qp, id, "NOVEL" as *u8)
73 sr_pref_append(prefs, id, "REWORK" as *u8, fb)
74 return 1
75 }
76 if st == 78 { return 0 }
77 return 0 - 1
78 }
79 if verb == 3 {
80 if st == 86 {
81 wd_flip_status(qp, id, "NOVEL" as *u8)
82 sr_pref_append(prefs, id, "RESTART" as *u8, fb)
83 return 1
84 }
85 if st == 78 { return 0 }
86 return 0 - 1
87 }
88 if st == 86 {
89 wd_flip_status(qp, id, "KILLED" as *u8)
90 sr_pref_append(prefs, id, "DENY" as *u8, fb)
91 return 1
92 }
93 // DENY is ALSO legal on NOVEL: the sponsor flags a raised hand as
94 // duplicative/no-ROI BEFORE tutor time burns ("flagging the novel",
95 // operator 2026-06-11). Reason captured like every verb.
96 if st == 78 {
97 wd_flip_status(qp, id, "KILLED" as *u8)
98 sr_pref_append(prefs, id, "DENY" as *u8, fb)
99 return 1
100 }
101 if st == 75 { return 0 }
102 return 0 - 1
103}
104
105// census: NOVEL rows tagged ||ASK=spec = the sponsor+tutor co-spec batch
106func sr_askspec(qp: *u8, lfd: i64) -> i64 {
107 let cx: *i64 = an_newcx()
108 if an_load(qp, cx) <= 0 { return 0 }
109 let st: *i64 = cx[4] as *i64
110 var cnt: i64 = 0
111 var r: i64 = 0
112 while r < cx[0] {
113 if st[r] == 78 {
114 let g: *u8 = an_gate_at(cx, r)
115 var gl: i64 = 0
116 while g[gl] != (0 as u8) { gl = gl + 1 }
117 var j: i64 = 0
118 var hit: i64 = 0
119 while j < gl {
120 if wd_at(g, gl, j, "||ASK=spec" as *u8, 10) == 1 { hit = 1; j = gl } else { j = j + 1 }
121 }
122 if hit == 1 {
123 cnt = cnt + 1
124 wd_w(1, "SPONSOR-SPEC id=" as *u8)
125 wd_w(1, an_id_at(cx, r))
126 wd_w(1, " (needs sponsor+tutor co-spec)\n" as *u8)
127 if lfd >= 0 {
128 wd_w(lfd, "SPONSOR-SPEC id=" as *u8)
129 wd_w(lfd, an_id_at(cx, r))
130 wd_w(lfd, "\n" as *u8)
131 }
132 }
133 }
134 r = r + 1
135 }
136 return cnt
137}
138
139// skip spaces/tabs from p (bounded by le); returns new p
140func sr_skipws(buf: *u8, p: i64, le: i64) -> i64 {
141 var q: i64 = p
142 var sk: i64 = 1
143 while sk == 1 {
144 if q >= le { sk = 0 } else {
145 var sp: i64 = 0
146 if buf[q] == (32 as u8) { sp = 1 }
147 if buf[q] == (9 as u8) { sp = 1 }
148 if sp == 1 { q = q + 1 } else { sk = 0 }
149 }
150 }
151 return q
152}
153
154// scx block: scx[0]=qp scx[1]=prefs scx[2]=applied-counter scx[3]=refused
155// scx[4]=lfd. Process one decision line [ls, le) -- hoisted out of sr_run
156// to keep the if-nesting flat (the 4-deep miscompile law).
157func sr_line(scx: *i64, buf: *u8, n: i64, ls: i64, le: i64) -> i64 {
158 if le <= ls { return 0 }
159 if buf[ls] == (35 as u8) { return 0 }
160 var verb: i64 = 0 - 1
161 var p: i64 = ls
162 if wd_at(buf, n, p, "APPROVE" as *u8, 7) == 1 { verb = 1; p = p + 7 }
163 if verb < 0 { if wd_at(buf, n, p, "REWORK" as *u8, 6) == 1 { verb = 2; p = p + 6 } }
164 if verb < 0 { if wd_at(buf, n, p, "RESTART" as *u8, 7) == 1 { verb = 3; p = p + 7 } }
165 if verb < 0 { if wd_at(buf, n, p, "DENY" as *u8, 4) == 1 { verb = 4; p = p + 4 } }
166 if verb < 0 { return 0 }
167 p = sr_skipws(buf, p, le)
168 let id: *u8 = sys_mmap(64)
169 var k: i64 = 0
170 var go: i64 = 1
171 while go == 1 {
172 if p >= le { go = 0 } else {
173 var sp: i64 = 0
174 if buf[p] == (32 as u8) { sp = 1 }
175 if buf[p] == (9 as u8) { sp = 1 }
176 if sp == 1 { go = 0 } else {
177 if k < 63 { id[k] = buf[p]; k = k + 1 }
178 p = p + 1
179 }
180 }
181 }
182 id[k] = 0 as u8
183 if k == 0 { return 0 }
184 p = sr_skipws(buf, p, le)
185 let fb: *u8 = sys_mmap(512)
186 var f: i64 = 0
187 while p < le { if f < 511 { fb[f] = buf[p]; f = f + 1 } p = p + 1 }
188 fb[f] = 0 as u8
189 let rv: i64 = sr_apply(scx[0] as *u8, scx[1] as *u8, verb, id, fb)
190 let lfd: i64 = scx[4]
191 if rv == 1 {
192 scx[2] = scx[2] + 1
193 wd_w(1, "SPONSOR applied id=" as *u8); wd_w(1, id)
194 if verb == 1 { wd_w(1, " -> DONE\n" as *u8) }
195 if verb == 2 { wd_w(1, " -> NOVEL (rework w/ feedback)\n" as *u8) }
196 if verb == 3 { wd_w(1, " -> NOVEL (restart from spec)\n" as *u8) }
197 if verb == 4 { wd_w(1, " -> KILLED (business kill)\n" as *u8) }
198 if lfd >= 0 { wd_w(lfd, "SPONSOR applied id=" as *u8); wd_w(lfd, id); wd_w(lfd, "\n" as *u8) }
199 }
200 if rv < 0 {
201 scx[3] = scx[3] + 1
202 wd_w(1, "SPONSOR REFUSED id=" as *u8); wd_w(1, id)
203 wd_w(1, " (unknown id or illegal transition)\n" as *u8)
204 if lfd >= 0 { wd_w(lfd, "SPONSOR REFUSED id=" as *u8); wd_w(lfd, id); wd_w(lfd, "\n" as *u8) }
205 }
206 return 0
207}
208
209// the full review pass; returns 0 (loud rows carry the news, beat stays green)
210func sr_run(qp: *u8, dec: *u8, prefs: *u8, logpath: *u8) -> i64 {
211 let lfd: i64 = sys_openat_append(logpath, 0x1a4)
212 let buf: *u8 = sys_mmap(K_MAGIC_65536)
213 var n: i64 = 0
214 let fd: i64 = sys_openat_rd(dec)
215 if fd >= 0 {
216 var rr: i64 = sys_read(fd, buf, K_MAGIC_65535)
217 while rr > 0 { n = n + rr; rr = sys_read(fd, buf + n, K_MAGIC_65535 - n) }
218 sys_close(fd)
219 }
220 let scx: *i64 = sys_mmap(64) as *i64
221 scx[0] = qp as i64
222 scx[1] = prefs as i64
223 scx[2] = 0
224 scx[3] = 0
225 scx[4] = lfd
226 var ls: i64 = 0
227 var i: i64 = 0
228 while i <= n {
229 var eol: i64 = 0
230 if i == n { eol = 1 } else { if buf[i] == (10 as u8) { eol = 1 } }
231 if eol == 1 {
232 sr_line(scx, buf, n, ls, i)
233 ls = i + 1
234 }
235 i = i + 1
236 }
237 let ask: i64 = sr_askspec(qp, lfd)
238 var p2: i64 = 0
239 while p2 < 2 {
240 var ofd: i64 = 1
241 if p2 == 1 { ofd = lfd }
242 if ofd >= 0 {
243 wd_w(ofd, "SPONSOR applied=" as *u8); wd_wn(ofd, scx[2])
244 wd_w(ofd, " refused=" as *u8); wd_wn(ofd, scx[3])
245 wd_w(ofd, " askspec=" as *u8); wd_wn(ofd, ask)
246 wd_w(ofd, " epoch=" as *u8); wd_wn(ofd, sys_now_realtime_sec())
247 wd_w(ofd, " verdict=GREEN\n" as *u8)
248 }
249 p2 = p2 + 1
250 }
251 if lfd >= 0 { sys_close(lfd) }
252 return 0
253}