code wiki / _hdl_build / nx_fitloop.nx
nx_fitloop.nx source
↩ module page · 255 lines · 12087 B
1// nx_fitloop.nx -- THE KEYSTONE: METROLOGY IN THE AUTHORING LOOP (debt 1785967773; operator 2026-08-05:
2// crafting and measuring must grow each other). A generator is driven by a judge in a CLOSED LOOP with
3// an auditable trial trace -- the capability this session proved manually (hand 241->358->258->191->181mm
4// across five hand-run pose trials) and the gap measured in the industry: authoring tools have no
5// metrology in the loop, metrology tools do not author.
6//
7// v1 searches the 2-parameter pose space measured tonight: symmetric shoulder raise s and elbow raise e
8// (degrees, clamped 0..90; left rows get -deg, right +deg; elbow pivot approximation declared valid for
9// small s). Generator = nx_meshpose apply. Judge = nx_meshdist parts; OBJECTIVE = the SUM of all per-part
10// mean distances (pose moves only arms, torso terms are constant, so minimising the sum minimises the
11// arm gap without wrecking anything else -- and a whole-body objective cannot be gamed by improving one
12// part at another's expense).
13//
14// u2605THE VACUOUS-OBJECTIVE GUARD IS THE ORGAN'S FIRST TOOTH: a failed judge run yields an empty capture
15// whose sum is 0 = "perfect". Any trial parsing fewer than 10 part rows is INVALID and scores WORST,
16// never best -- an optimiser that can reward a broken ruler will find the broken ruler immediately.
17//
18// nx_fitloop pose2 <base.nxmesh> <oracle_parts.nxmesh> <out_best.nxmesh> [budget]
19// nx_fitloop selftest
20// Trial trace: one TRIAL row per evaluation (s, e, objective um-sum, parts parsed) -- the optimisation
21// is EVIDENCE, not a black box. license_tier: ORIGINAL expect_exit: 0
22import "nx_syscalls.nx"
23import "nx_tool_run.nx"
24const FL_MAGIC_4096: i64 = 4096
25
26const FL_CAP: i64 = 262144
27const FL_MAXTRIAL: i64 = 40
28const FL_BAD: i64 = 4611686018427387903
29const FL_MINPARTS: i64 = 10
30
31func hw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
32func pn(v: i64) -> i64 { let b: *u8=sys_mmap(32) as *u8; var x: i64=v; var ng: i64=0; if x<0{ng=1;x=0-x} var i: i64=31; if x==0{b[i]=48 as u8;i=i-1} while x>0{b[i]=(48+x%10) as u8;x=x/10;i=i-1} if ng==1{b[i]=45 as u8;i=i-1} sys_write(1,(b as i64+i+1) as *u8,31-i); return 0 }
33func fl_refuse(reason: *u8) -> i64 { hw("FITLOOP REFUSED: " as *u8); hw(reason); hw("\n" as *u8); return 0 }
34func fl_cat(b: *u8, off: i64, s: *u8) -> i64 {
35 var o: i64 = off
36 var i: i64 = 0
37 while s[i] != (0 as u8) { b[o] = s[i]; o = o + 1; i = i + 1 }
38 return o
39}
40func fl_int(b: *u8, off: i64, v: i64) -> i64 {
41 var o: i64 = off
42 var x: i64 = v
43 if x < 0 { b[o] = 45 as u8; o = o + 1; x = 0 - x }
44 let t: *u8 = sys_mmap(32)
45 var n: i64 = 0
46 if x == 0 { b[o] = 48 as u8; return o + 1 }
47 while x > 0 { t[n] = (48 + x%10) as u8; x = x/10; n = n + 1 }
48 while n > 0 { n = n - 1; b[o] = t[n]; o = o + 1 }
49 return o
50}
51func fl_wrfile(path: *u8, b: *u8, n: i64) -> i64 {
52 let fd: i64 = sys_openat_wr(path, 420)
53 if fd < 0 { return 0 - 1 }
54 sys_write(fd, b, n)
55 sys_close(fd)
56 return 0
57}
58func fl_exists(path: *u8) -> i64 {
59 let fd: i64 = sys_openat_rd(path)
60 if fd < 0 { return 0 }
61 sys_close(fd)
62 return 1
63}
64// parse the judge capture: sum every "mean_um":<int> and count them
65func fl_objective(cap: *u8, n: i64, cnt: *i64) -> i64 {
66 let key: *u8 = "\x22mean_um\x22:" as *u8
67 var kl: i64 = 0
68 while key[kl] != (0 as u8) { kl = kl + 1 }
69 var sum: i64 = 0
70 var c: i64 = 0
71 var i: i64 = 0
72 while i + kl < n {
73 var hit: i64 = 1
74 var k: i64 = 0
75 while k < kl { if cap[i+k] != key[k] { hit = 0; k = kl } else { k = k + 1 } }
76 if hit == 1 {
77 var j: i64 = i + kl
78 var v: i64 = 0
79 var any: i64 = 0
80 var go: i64 = 1
81 while go == 1 {
82 if j >= n { go = 0 } else {
83 let ch: i64 = cap[j] as i64
84 if ch >= 48 { if ch <= 57 { v = v*10 + (ch-48); any = 1; j = j + 1 } else { go = 0 } } else { go = 0 }
85 }
86 }
87 if any == 1 { sum = sum + v; c = c + 1 }
88 i = j
89 } else { i = i + 1 }
90 }
91 cnt[0] = c
92 return sum
93}
94// build the 4-row symmetric pose file for (s, e)
95func fl_posefile(path: *u8, s: i64, e: i64) -> i64 {
96 let b: *u8 = sys_mmap(FL_MAGIC_4096)
97 var o: i64 = 0
98 o = fl_cat(b, o, "rot\t0\t240\t300\t800\t208\t802\t" as *u8)
99 o = fl_int(b, o, 0 - s)
100 o = fl_cat(b, o, "\nrot\t760\t1000\t300\t800\t792\t802\t" as *u8)
101 o = fl_int(b, o, s)
102 o = fl_cat(b, o, "\nrot\t0\t240\t300\t645\t139\t645\t" as *u8)
103 o = fl_int(b, o, 0 - e)
104 o = fl_cat(b, o, "\nrot\t760\t1000\t300\t645\t861\t645\t" as *u8)
105 o = fl_int(b, o, e)
106 o = fl_cat(b, o, "\n" as *u8)
107 return fl_wrfile(path, b, o)
108}
109// one trial: pose -> generate -> judge -> objective. FL_BAD on any failure or vacuous judge.
110func fl_trial(base: *u8, oracle: *u8, s: i64, e: i64, nparts: *i64) -> i64 {
111 if fl_posefile("/tmp/fl_pose.txt" as *u8, s, e) != 0 { return FL_BAD }
112 let av: *i64 = sys_mmap(16*8) as *i64
113 av[0] = "/volume1/homes/elderwesto/nishihost/nx_meshpose.elf" as *u8 as i64
114 av[1] = "apply" as *u8 as i64
115 av[2] = base as i64
116 av[3] = "/tmp/fl_cand.nxmesh" as *u8 as i64
117 av[4] = "/tmp/fl_pose.txt" as *u8 as i64
118 av[5] = 0
119 let cap1: *u8 = sys_mmap(FL_CAP)
120 let ol: *i64 = sys_mmap(16) as *i64
121 // s==0 and e==0 would REFUSE in meshpose (zero-degree rows still move 0 verts? no -- 0-deg rotation
122 // moves vertices by identity, matched>0) -- identity rotation still counts vertices as moved.
123 let rc1: i64 = tr_run_capture("/volume1/homes/elderwesto/nishihost/nx_meshpose.elf" as *u8, av, cap1, FL_CAP, ol)
124 if rc1 != 0 { return FL_BAD }
125 let av2: *i64 = sys_mmap(16*8) as *i64
126 av2[0] = "/volume1/homes/elderwesto/nishihost/nx_meshdist.elf" as *u8 as i64
127 av2[1] = "parts" as *u8 as i64
128 av2[2] = "/tmp/fl_cand.nxmesh" as *u8 as i64
129 av2[3] = oracle as i64
130 av2[4] = "128" as *u8 as i64
131 av2[5] = 0
132 let cap2: *u8 = sys_mmap(FL_CAP)
133 let ol2: *i64 = sys_mmap(16) as *i64
134 let rc2: i64 = tr_run_capture("/volume1/homes/elderwesto/nishihost/nx_meshdist.elf" as *u8, av2, cap2, FL_CAP, ol2)
135 if rc2 != 0 { return FL_BAD }
136 let cnt: *i64 = sys_mmap(16) as *i64
137 let obj: i64 = fl_objective(cap2, ol2[0], cnt)
138 nparts[0] = cnt[0]
139 if cnt[0] < FL_MINPARTS { return FL_BAD }
140 return obj
141}
142func fl_pose2(base: *u8, oracle: *u8, outp: *u8, budget: i64) -> i64 {
143 if fl_exists(base) == 0 { fl_refuse("base mesh unreadable" as *u8); return 3 }
144 if fl_exists(oracle) == 0 { fl_refuse("oracle mesh unreadable" as *u8); return 3 }
145 var s: i64 = 0
146 var e: i64 = 0
147 var step: i64 = 16
148 var trials: i64 = 0
149 let np: *i64 = sys_mmap(16) as *i64
150 hw("FITLOOP pose2 start budget=" as *u8); pn(budget); hw(" objective=sum(mean_um over parts)\n" as *u8)
151 var best: i64 = fl_trial(base, oracle, s, e, np)
152 trials = trials + 1
153 hw("TRIAL " as *u8); pn(trials); hw(" s=" as *u8); pn(s); hw(" e=" as *u8); pn(e); hw(" obj=" as *u8); pn(best); hw(" parts=" as *u8); pn(np[0]); hw("\n" as *u8)
154 if best == FL_BAD { fl_refuse("baseline trial failed or judge vacuous -- nothing to optimise" as *u8); return 4 }
155 var go: i64 = 1
156 while go == 1 {
157 var improved: i64 = 0
158 // coordinate descent: each param, both directions
159 var pi: i64 = 0
160 while pi < 2 {
161 var dir: i64 = 0
162 while dir < 2 {
163 var ns: i64 = s
164 var ne: i64 = e
165 var d: i64 = step
166 if dir == 1 { d = 0 - step }
167 if pi == 0 { ns = s + d } else { ne = e + d }
168 if ns < 0 { ns = 0 }
169 if ns > 90 { ns = 90 }
170 if ne < 0 { ne = 0 }
171 if ne > 90 { ne = 90 }
172 var same: i64 = 0
173 if ns == s { if ne == e { same = 1 } }
174 if same == 0 { if trials < budget {
175 let o2: i64 = fl_trial(base, oracle, ns, ne, np)
176 trials = trials + 1
177 hw("TRIAL " as *u8); pn(trials); hw(" s=" as *u8); pn(ns); hw(" e=" as *u8); pn(ne); hw(" obj=" as *u8); pn(o2); hw(" parts=" as *u8); pn(np[0]); hw("\n" as *u8)
178 if o2 < best { best = o2; s = ns; e = ne; improved = 1 }
179 } }
180 dir = dir + 1
181 }
182 pi = pi + 1
183 }
184 if improved == 0 {
185 step = step / 2
186 if step < 2 { go = 0 }
187 }
188 if trials >= budget { go = 0 }
189 }
190 // re-generate the best candidate to the requested output
191 fl_posefile("/tmp/fl_pose.txt" as *u8, s, e)
192 let av3: *i64 = sys_mmap(16*8) as *i64
193 av3[0] = "/volume1/homes/elderwesto/nishihost/nx_meshpose.elf" as *u8 as i64
194 av3[1] = "apply" as *u8 as i64
195 av3[2] = base as i64
196 av3[3] = outp as i64
197 av3[4] = "/tmp/fl_pose.txt" as *u8 as i64
198 av3[5] = 0
199 let cap3: *u8 = sys_mmap(FL_CAP)
200 let ol3: *i64 = sys_mmap(16) as *i64
201 tr_run_capture("/volume1/homes/elderwesto/nishihost/nx_meshpose.elf" as *u8, av3, cap3, FL_CAP, ol3)
202 hw("{\x22organ\x22:\x22nx_fitloop\x22,\x22verb\x22:\x22pose2\x22,\x22best_s_deg\x22:" as *u8); pn(s)
203 hw(",\x22best_e_deg\x22:" as *u8); pn(e)
204 hw(",\x22best_objective_um_sum\x22:" as *u8); pn(best)
205 hw(",\x22trials\x22:" as *u8); pn(trials)
206 hw(",\x22out\x22:\x22" as *u8); hw(outp)
207 hw("\x22,\x22note\x22:\x22every trial above is the audit trace; vacuous judge output scores WORST by construction\x22}\n" as *u8)
208 return 0
209}
210func fl_selftest() -> i64 {
211 var fails: i64 = 0
212 hw("T0 absent base must REFUSE:\n" as *u8)
213 if fl_pose2("/tmp/fl_absent_zz.nxmesh" as *u8, "/tmp/fl_absent_zz2.nxmesh" as *u8, "/tmp/fl_x.nxmesh" as *u8, 4) == 0 { fails = fails + 1; hw("T0 FAIL\n" as *u8) } else { hw("T0 PASS\n" as *u8) }
214 hw("T1 vacuous-objective guard: fl_objective on an empty buffer must count 0 parts:\n" as *u8)
215 let b: *u8 = sys_mmap(64)
216 let c: *i64 = sys_mmap(16) as *i64
217 let v: i64 = fl_objective(b, 0, c)
218 if c[0] == 0 { if v == 0 { hw("T1 PASS (0 parts parsed -> trial scores FL_BAD upstream)\n" as *u8) } else { fails = fails + 1; hw("T1 FAIL\n" as *u8) } } else { fails = fails + 1; hw("T1 FAIL\n" as *u8) }
219 hw("T2 objective parser on a real fragment:\n" as *u8)
220 let f: *u8 = "{\x22part\x22:\x22a\x22,\x22mean_um\x22:100},{\x22part\x22:\x22b\x22,\x22mean_um\x22:250}" as *u8
221 var fn: i64 = 0
222 while f[fn] != (0 as u8) { fn = fn + 1 }
223 let v2: i64 = fl_objective(f, fn, c)
224 if v2 == 350 { if c[0] == 2 { hw("T2 PASS sum=350 parts=2\n" as *u8) } else { fails = fails + 1; hw("T2 FAIL count\n" as *u8) } } else { fails = fails + 1; hw("T2 FAIL sum=" as *u8); pn(v2); hw("\n" as *u8) }
225 if fails == 0 { hw("FITLOOP-SELFTEST GREEN 3/3 (positive control = the live pose2 run)\n" as *u8); return 0 }
226 hw("FITLOOP-SELFTEST RED fails=" as *u8); pn(fails); hw("\n" as *u8)
227 return 1
228}
229func main(argc: i64, argv: *i64) -> i64 {
230 if argc < 2 {
231 hw("usage: nx_fitloop pose2 <base.nxmesh> <oracle_parts.nxmesh> <out_best.nxmesh> [budget] | selftest\n" as *u8)
232 sys_exit(2)
233 return 2
234 }
235 let a1: *u8 = argv[1] as *u8
236 if a1[0] == (115 as u8) { let rc: i64 = fl_selftest(); sys_exit(rc); return rc }
237 if a1[0] == (112 as u8) {
238 if argc < 5 { hw("usage: nx_fitloop pose2 <base> <oracle> <out> [budget]\n" as *u8); sys_exit(2); return 2 }
239 var budget: i64 = 24
240 if argc >= 6 {
241 let a5: *u8 = argv[5] as *u8
242 budget = 0
243 var q: i64 = 0
244 while a5[q] != (0 as u8) { budget = budget*10 + ((a5[q] as i64) - 48); q = q + 1 }
245 if budget < 4 { budget = 4 }
246 if budget > FL_MAXTRIAL { budget = FL_MAXTRIAL }
247 }
248 let rc2: i64 = fl_pose2(argv[2] as *u8, argv[3] as *u8, argv[4] as *u8, budget)
249 sys_exit(rc2)
250 return rc2
251 }
252 hw("unknown verb\n" as *u8)
253 sys_exit(2)
254 return 2
255}