code wiki / _hdl_build / nx_converge.nx
nx_converge.nx source
↩ module page · 197 lines · 7608 B
1// nx_converge.nx -- CLI/MCP surface for the retention convergence controller.
2// All control logic lives in nx_converge_lib.nx, which the GATE imports too, so the thing that ships
3// and the thing that is graded are the same code.
4//
5// VERBS
6// plan <target> <tol>
7// -> the FIRST denoise probe to render, plus the step budget. Costs zero GPU.
8// next <target> <tol> <lo> <hi> <probe> <measured> <steps_used>
9// -> the next probe (or DONE) given what the ruler measured for the last render.
10// STATELESS: the bracket travels in the arguments, so an agent/workflow can drive one
11// render at a time and the whole search is replayable from its transcript.
12// simulate <target> <tol>
13// -> run the controller to completion against the oracle built from REAL measured retention,
14// printing the full trajectory. This is how you see the step count before spending GPU.
15// selftest
16// expect_exit: 0 license_tier: ORIGINAL
17import "nx_converge_lib.nx"
18const K_MAGIC_1200: i64 = 1200
19
20func cw(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
21func cn(v: i64) -> i64 {
22 let b: *u8 = sys_mmap(28)
23 var m: i64 = v
24 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m }
25 let t: *u8 = sys_mmap(28)
26 var k: i64 = 0
27 if m == 0 { t[0] = (48 as u8); k = 1 }
28 while m > 0 { t[k] = ((48 + (m % 10)) as u8); m = m / 10; k = k + 1 }
29 var i: i64 = 0
30 while i < k { b[i] = t[k - 1 - i]; i = i + 1 }
31 sys_write(1, b, k)
32 return 0
33}
34
35func c_streq(a: *u8, b: *u8) -> i64 {
36 var i: i64 = 0
37 while a[i] != (0 as u8) { if a[i] != b[i] { return 0 } i = i + 1 }
38 if b[i] != (0 as u8) { return 0 }
39 return 1
40}
41
42func c_atoi(s: *u8) -> i64 {
43 var v: i64 = 0
44 var i: i64 = 0
45 var neg: i64 = 0
46 if s[0] == (45 as u8) { neg = 1; i = 1 }
47 while s[i] != (0 as u8) {
48 let c: i64 = (s[i] as i64) & 255
49 if c >= 48 { if c <= 57 { v = v * 10 + (c - 48) } }
50 i = i + 1
51 }
52 if neg == 1 { return 0 - v }
53 return v
54}
55
56func c_status_name(st: i64) -> i64 {
57 if st == CV_OK { cw("CONVERGED" as *u8); return 0 }
58 if st == CV_CONTINUE { cw("CONTINUE" as *u8); return 0 }
59 if st == CV_EXHAUSTED { cw("EXHAUSTED" as *u8); return 0 }
60 cw("UNREACHABLE" as *u8)
61 return 0
62}
63
64func c_simulate(target: i64, tol: i64) -> i64 {
65 cw("{\"mode\":\"simulate\",\"target_permil\":" as *u8); cn(target)
66 cw(",\"tolerance\":" as *u8); cn(tol)
67 cw(",\"oracle\":\"interpolated from REAL measured retention (350->859, 550->647, 750->359)\"}\n" as *u8)
68
69 var lo: i64 = CV_MIN
70 var hi: i64 = CV_MAX
71 var probe: i64 = cv_first_probe()
72 var steps: i64 = 0
73 let st: *i64 = sys_mmap(8)
74 let nlo: *i64 = sys_mmap(8)
75 let nhi: *i64 = sys_mmap(8)
76 st[0] = CV_CONTINUE
77 var measured: i64 = 0
78
79 if cv_reachable(target) == 0 {
80 cw(" UNREACHABLE: target outside [0,1000]\n" as *u8)
81 return 3
82 }
83 while st[0] == CV_CONTINUE {
84 measured = cv_oracle(probe)
85 steps = steps + 1
86 cw(" step " as *u8); cn(steps)
87 cw(": denoise=" as *u8); cn(probe)
88 cw(" -> retention=" as *u8); cn(measured)
89 cw(" bracket=[" as *u8); cn(lo); cw("," as *u8); cn(hi); cw("]\n" as *u8)
90 let nxt: i64 = cv_step(target, tol, lo, hi, probe, measured, steps, st, nlo, nhi)
91 lo = nlo[0]
92 hi = nhi[0]
93 if nxt != CV_DONE { probe = nxt }
94 }
95 cw(" RESULT status=" as *u8); c_status_name(st[0])
96 cw(" denoise=" as *u8); cn(probe)
97 cw(" retention=" as *u8); cn(measured)
98 cw(" renders=" as *u8); cn(steps)
99 cw("\n" as *u8)
100 return 0
101}
102
103// The teeth in brief; the full battery is nx_converge_gate.
104func c_selftest() -> i64 {
105 var pass: i64 = 0
106 var total: i64 = 0
107 let d: *i64 = sys_mmap(8)
108 let r: *i64 = sys_mmap(8)
109 let s: *i64 = sys_mmap(8)
110
111 // converges on a mid-range target
112 total = total + 1
113 let st1: i64 = cv_run(700, 15, d, r, s)
114 var e1: i64 = r[0] - 700
115 if e1 < 0 { e1 = 0 - e1 }
116 if st1 == CV_OK { if e1 <= 15 { pass = pass + 1 } }
117 cw("T1 converge target=700 tol=15 -> status=" as *u8); c_status_name(st1)
118 cw(" denoise=" as *u8); cn(d[0]); cw(" retention=" as *u8); cn(r[0])
119 cw(" renders=" as *u8); cn(s[0]); cw("\n" as *u8)
120
121 // an impossible target must refuse, not spin
122 total = total + 1
123 let st2: i64 = cv_run(K_MAGIC_1200, 10, d, r, s)
124 if st2 == CV_UNREACHABLE { if s[0] == 0 { pass = pass + 1 } }
125 cw("T2 unreachable target=1200 -> status=" as *u8); c_status_name(st2)
126 cw(" renders=" as *u8); cn(s[0]); cw(" (must be 0 -- refuse BEFORE spending GPU)\n" as *u8)
127
128 cw("SELFTEST pass=" as *u8); cn(pass)
129 cw("/" as *u8); cn(total)
130 if pass == total { cw(" verdict=GREEN\n" as *u8); return 0 }
131 cw(" verdict=RED\n" as *u8)
132 return 1
133}
134
135func main(argc: i64, argv: *i64) -> i64 {
136 if argc < 2 {
137 cw("usage: nx_converge plan <target> <tol>\n" as *u8)
138 cw(" nx_converge next <target> <tol> <lo> <hi> <probe> <measured> <steps_used>\n" as *u8)
139 cw(" nx_converge simulate <target> <tol>\n" as *u8)
140 cw(" nx_converge selftest\n" as *u8)
141 return 2
142 }
143 let verb: *u8 = argv[1] as *u8
144
145 if c_streq(verb, "selftest" as *u8) == 1 { return c_selftest() }
146
147 if c_streq(verb, "plan" as *u8) == 1 {
148 if argc < 4 { cw("{\"error\":\"plan needs <target> <tol>\"}\n" as *u8); return 2 }
149 let target: i64 = c_atoi(argv[2] as *u8)
150 let tol: i64 = c_atoi(argv[3] as *u8)
151 if cv_reachable(target) == 0 {
152 cw("{\"status\":\"UNREACHABLE\",\"why\":\"target outside [0,1000]\"}\n" as *u8)
153 return 3
154 }
155 cw("{\"status\":\"CONTINUE\",\"probe_denoise_permil\":" as *u8); cn(cv_first_probe())
156 cw(",\"lo\":" as *u8); cn(CV_MIN)
157 cw(",\"hi\":" as *u8); cn(CV_MAX)
158 cw(",\"steps_used\":0,\"max_renders\":" as *u8); cn(CV_MAX_STEPS)
159 cw(",\"note\":\"render at this denoise, score with nx_refbench, then call next\"}\n" as *u8)
160 return 0
161 }
162
163 if c_streq(verb, "next" as *u8) == 1 {
164 if argc < 9 { cw("{\"error\":\"next needs <target> <tol> <lo> <hi> <probe> <measured> <steps_used>\"}\n" as *u8); return 2 }
165 let target: i64 = c_atoi(argv[2] as *u8)
166 let tol: i64 = c_atoi(argv[3] as *u8)
167 let lo: i64 = c_atoi(argv[4] as *u8)
168 let hi: i64 = c_atoi(argv[5] as *u8)
169 let probe: i64 = c_atoi(argv[6] as *u8)
170 let measured: i64 = c_atoi(argv[7] as *u8)
171 let used: i64 = c_atoi(argv[8] as *u8)
172 let st: *i64 = sys_mmap(8)
173 let nlo: *i64 = sys_mmap(8)
174 let nhi: *i64 = sys_mmap(8)
175 let nxt: i64 = cv_step(target, tol, lo, hi, probe, measured, used, st, nlo, nhi)
176 cw("{\"status\":\"" as *u8); c_status_name(st[0]); cw("\"" as *u8)
177 if nxt != CV_DONE {
178 cw(",\"probe_denoise_permil\":" as *u8); cn(nxt)
179 } else {
180 cw(",\"final_denoise_permil\":" as *u8); cn(probe)
181 cw(",\"final_retention_permil\":" as *u8); cn(measured)
182 }
183 cw(",\"lo\":" as *u8); cn(nlo[0])
184 cw(",\"hi\":" as *u8); cn(nhi[0])
185 cw(",\"steps_used\":" as *u8); cn(used)
186 cw("}\n" as *u8)
187 return 0
188 }
189
190 if c_streq(verb, "simulate" as *u8) == 1 {
191 if argc < 4 { cw("{\"error\":\"simulate needs <target> <tol>\"}\n" as *u8); return 2 }
192 return c_simulate(c_atoi(argv[2] as *u8), c_atoi(argv[3] as *u8))
193 }
194
195 cw("{\"error\":\"unknown verb\"}\n" as *u8)
196 return 2
197}