code wiki / (root) / nx_twinreport_lib.nx

nx_twinreport_lib.nx source

↩ module page · 296 lines · 14858 B

1// nx_twinreport_lib.nx -- FROM A CARD TO A VISIBLE RESULT IN ONE CALL (aesthetictwin AT42, 2026-09-17). 2// Operator 2026-09-17: "just get all of it to mcp and api and workflow and agent powered its better to see what you have 3// rendered". A reference card's axes go in; the estate's fit loop solves the genes, the fitted face is rendered from three 4// views, and the result rows are APPENDED to the board's results file in the grammar nx_compare_results renders 5// (result|slug|kind|date|class|title|report|image|headline), so the next publish shows them. Nothing here is specific to 6// one person: any card the fit can read is a slug. The seeds verb does the same for synthetic people drawn from the gene 7// ranges. Rows are slug-keyed, so a re-run supersedes its own earlier rows instead of duplicating them. 8// license_tier: ORIGINAL No hw writes (Rule 26). 9import "nx_syscalls.nx" 10import "nx_civil_date.nx" 11import "nx_twinfit_lib.nx" 12 13const TWR_YAW_THREEQUARTER: i64 = 2145 // thirty degrees in the renderer's angle unit (radians times 4096) 14const TWR_CAMZ_CLOSE: i64 = 2 15const TWR_PATHB: i64 = 1024 16const TWR_ROWB: i64 = 4096 17const TWR_VIEWS: i64 = 3 18const TWR_MODE644: i64 = 420 19const TWR_DATE_FIELDS: i64 = 8 20const TWR_DATE_YEAR: i64 = 0 21const TWR_DATE_MONTH: i64 = 1 22const TWR_DATE_DAY: i64 = 2 23const TWR_TEN: i64 = 10 24const TWR_CLASS: *u8 = "face-synthetic" 25const TWR_E_NOTARGET: i64 = 0 - 3 26const TWR_E_RENDER: i64 = 0 - 4 27const TWR_E_APPEND: i64 = 0 - 5 28const TWR_E_RANGE: i64 = 0 - 6 29// the receipt 30const TWR_R_EVALS: i64 = 0 31const TWR_R_REACHED: i64 = 1 32const TWR_R_UNREACHED: i64 = 2 33const TWR_R_UNSTATED: i64 = 3 34const TWR_R_CONFIRMED: i64 = 4 35const TWR_R_RENDERS: i64 = 5 36const TWR_R_ROWS: i64 = 6 37const TWR_R_ROW_BYTES: i64 = 7 38const TWR_R_PNG_BYTES: i64 = 8 39const TWR_R_UNCHANGED: i64 = 9 40const TWR_R_N: i64 = 10 41 42func twr_cat2(out: *u8, o: i64, n: i64) -> i64 { // a two-digit field, zero padded 43 var p: i64 = o 44 if n < TWR_TEN { p = ri_cat(out, p, "0" as *u8) } 45 return ri_catn(out, p, n) 46} 47// today's UTC date as YYYY-MM-DD, from the clock through the estate's civil date 48func twr_today(out: *u8) -> i64 { 49 let cd: *i64 = sys_mmap(TWR_DATE_FIELDS * TW_I64) as *i64 50 civil_from_unix(sys_now_realtime_sec(), cd as *CivilDate) 51 var o: i64 = ri_catn(out, 0, cd[TWR_DATE_YEAR]) 52 o = ri_cat(out, o, "-" as *u8) 53 o = twr_cat2(out, o, cd[TWR_DATE_MONTH]) 54 o = ri_cat(out, o, "-" as *u8) 55 o = twr_cat2(out, o, cd[TWR_DATE_DAY]) 56 out[o] = 0 as u8 57 return o 58} 59func twr_view_name(v: i64) -> *u8 { 60 if v == 0 { return "close" as *u8 } 61 if v == 1 { return "threequarter" as *u8 } 62 return "head" as *u8 63} 64func twr_view_words(v: i64) -> *u8 { 65 if v == 0 { return "close-up" as *u8 } 66 if v == 1 { return "three-quarter view" as *u8 } 67 return "whole head" as *u8 68} 69func twr_view_yaw(v: i64) -> i64 { if v == 1 { return TWR_YAW_THREEQUARTER } return TW_YAW_FRONTAL } 70func twr_view_camz(v: i64) -> i64 { if v == 2 { return TW_CAMZ_UNITS } return TWR_CAMZ_CLOSE } 71func twr_append(path: *u8, buf: *u8, n: i64) -> i64 { 72 let fd: i64 = sys_openat_append(path, TWR_MODE644) 73 if fd < 0 { return TWR_E_APPEND } 74 let k: i64 = sys_write(fd, buf, n) // one write: the append is whole or absent 75 sys_close(fd) 76 if k != n { return TWR_E_APPEND } 77 return n 78} 79// IDEMPOTENT APPEND: a standing plan runs this report every day, and a report that says what the file's current row for 80// that slug already says (the date apart) is not a new result. Only rows that differ from the file's LAST row of the same 81// slug are appended, so the results file grows with changes and not with the calendar 82const TWR_PIPE: i64 = 124 83const TWR_BYTE: i64 = 255 84const TWR_NL: i64 = 10 85const TWR_DATE_FIELD: i64 = 3 86// offset just past the k-th pipe of the line [s, e), or e when the line has fewer 87func twr_after_pipe(buf: *u8, s: i64, e: i64, k: i64) -> i64 { 88 var seen: i64 = 0 89 var i: i64 = s 90 while i < e { 91 if ((buf[i] as i64) & TWR_BYTE) == TWR_PIPE { seen = seen + 1; if seen == k { return i + 1 } } 92 i = i + 1 93 } 94 return e 95} 96func twr_span_eq(a: *u8, as0: i64, ae: i64, b: *u8, bs: i64, be: i64) -> i64 { 97 if ae - as0 != be - bs { return 0 } 98 var i: i64 = 0 99 while as0 + i < ae { if a[as0 + i] != b[bs + i] { return 0 } i = i + 1 } 100 return 1 101} 102// are two result rows the same result, the date field apart 103func twr_same_but_date(a: *u8, as0: i64, ae: i64, b: *u8, bs: i64, be: i64) -> i64 { 104 let a_d0: i64 = twr_after_pipe(a, as0, ae, TWR_DATE_FIELD) 105 let a_d1: i64 = twr_after_pipe(a, as0, ae, TWR_DATE_FIELD + 1) 106 let b_d0: i64 = twr_after_pipe(b, bs, be, TWR_DATE_FIELD) 107 let b_d1: i64 = twr_after_pipe(b, bs, be, TWR_DATE_FIELD + 1) 108 if twr_span_eq(a, as0, a_d0, b, bs, b_d0) == 0 { return 0 } 109 return twr_span_eq(a, a_d1, ae, b, b_d1, be) 110} 111// the file's LAST line that starts with the same result|<slug>| head as the row [rs, re): its start, or -1; its end in out[0] 112func twr_last_row(file: *u8, fn: i64, row: *u8, rs: i64, re: i64, out: *i64) -> i64 { 113 let head_end: i64 = twr_after_pipe(row, rs, re, 2) 114 var found: i64 = 0 - 1 115 var i: i64 = 0 116 while i < fn { 117 var e: i64 = i 118 var going: i64 = 1 119 while going == 1 { if e >= fn { going = 0 } else { if ((file[e] as i64) & TWR_BYTE) == TWR_NL { going = 0 } else { e = e + 1 } } } 120 if e - i >= head_end - rs { if twr_span_eq(file, i, i + (head_end - rs), row, rs, head_end) == 1 { found = i; out[0] = e } } 121 i = e + 1 122 } 123 return found 124} 125// append only the rows of [rows, 0..ro) that change the file's current answer; rep gets the appended and unchanged counts 126func twr_append_changed(path: *u8, rows: *u8, ro: i64, rep: *i64) -> i64 { 127 let fl: *i64 = sys_mmap(2 * TW_I64) as *i64 128 fl[0] = 0 129 let file: *u8 = sys_read_file(path, fl) 130 var fn: i64 = 0 131 if (file as i64) != 0 { fn = fl[0] } 132 let outb: *u8 = sys_mmap(ro + 1) 133 let le: *i64 = sys_mmap(TW_I64) as *i64 134 var oo: i64 = 0 135 var appended: i64 = 0 136 var unchanged: i64 = 0 137 var i: i64 = 0 138 while i < ro { 139 var e: i64 = i 140 var going: i64 = 1 141 while going == 1 { if e >= ro { going = 0 } else { if ((rows[e] as i64) & TWR_BYTE) == TWR_NL { going = 0 } else { e = e + 1 } } } 142 var same: i64 = 0 143 if fn > 0 { 144 let at: i64 = twr_last_row(file, fn, rows, i, e, le) 145 if at >= 0 { same = twr_same_but_date(file, at, le[0], rows, i, e) } 146 } 147 if same == 1 { unchanged = unchanged + 1 } else { 148 var k: i64 = i 149 while k < e { outb[oo] = rows[k]; oo = oo + 1; k = k + 1 } 150 outb[oo] = TWR_NL as u8 151 oo = oo + 1 152 appended = appended + 1 153 } 154 i = e + 1 155 } 156 rep[TWR_R_ROWS] = appended 157 rep[TWR_R_UNCHANGED] = unchanged 158 rep[TWR_R_ROW_BYTES] = oo 159 if oo == 0 { return 0 } 160 return twr_append(path, outb, oo) 161} 162// the head of a result row up to and including the image href: result|<slug>-<view>|<kind>|<date>|class|<title>|-|<href>| 163func twr_row_head(row: *u8, o: i64, slug: *u8, suffix: *u8, kind: *u8, date: *u8, title_a: *u8, title_b: *u8, title_c: *u8, hrefpfx: *u8, file: *u8) -> i64 { 164 var p: i64 = ri_cat(row, o, "result|" as *u8) 165 p = ri_cat(row, p, slug); p = ri_cat(row, p, suffix) 166 p = ri_cat(row, p, "|" as *u8); p = ri_cat(row, p, kind) 167 p = ri_cat(row, p, "|" as *u8); p = ri_cat(row, p, date) 168 p = ri_cat(row, p, "|" as *u8); p = ri_cat(row, p, TWR_CLASS) 169 p = ri_cat(row, p, "|" as *u8); p = ri_cat(row, p, title_a); p = ri_cat(row, p, title_b); p = ri_cat(row, p, title_c) 170 p = ri_cat(row, p, "|-|" as *u8); p = ri_cat(row, p, hrefpfx); p = ri_cat(row, p, file) 171 p = ri_cat(row, p, "|" as *u8) 172 return p 173} 174// FIT REPORT: card in, three renders and three result rows out. Returns 0, or a TWR_E_ code 175func twr_fit_report(base: i64, slug: *u8, target_path: *u8, outdir: *u8, hrefpfx: *u8, results_path: *u8, rep: *i64) -> i64 { 176 var z: i64 = 0 177 while z < TWR_R_N { rep[z] = 0; z = z + 1 } 178 let tv: *i64 = sys_mmap(TW_AXES * TW_I64) as *i64 179 let te: *i64 = sys_mmap(TW_AXES * TW_I64) as *i64 180 if tw_load_target(target_path, tv, te) == 0 { return TWR_E_NOTARGET } 181 let ga: *i64 = sys_mmap(TW_ALL_GENES * TW_I64) as *i64 182 let ax: *i64 = sys_mmap(TW_AXES * TW_I64) as *i64 183 let cost: *i64 = sys_mmap(TW_I64) as *i64 184 tw_all_canon_fill(ga) 185 rep[TWR_R_EVALS] = tw_fit(base, tv, te, ga, ax, cost) 186 var a: i64 = 0 187 while a < TW_AXES { 188 let st: i64 = tw_axis_state(ax, tv, te, a) 189 if st == TW_AXIS_REACHED { rep[TWR_R_REACHED] = rep[TWR_R_REACHED] + 1 } 190 if st == TW_AXIS_UNREACHED { rep[TWR_R_UNREACHED] = rep[TWR_R_UNREACHED] + 1 } 191 if st == TW_AXIS_UNSTATED { rep[TWR_R_UNSTATED] = rep[TWR_R_UNSTATED] + 1 } 192 a = a + 1 193 } 194 tw_build_all(base, ga) 195 let tt: *i64 = sys_mmap(TW_T_N * TW_I64) as *i64 196 rep[TWR_R_CONFIRMED] = tw_truth_landmarks(base, TW_YAW_FRONTAL, tt) 197 // the headline every view of this fit carries: the axes beside the card, the verdict, the genome 198 let head: *u8 = sys_mmap(TWR_ROWB) 199 var h: i64 = ri_cat(head, 0, "Fitted by the estate's own fit loop from the card's stated axes: " as *u8) 200 a = 0 201 while a < TW_AXES { 202 if tv[a] != TW_UNSET { 203 h = ri_cat(head, h, tw_axis_name(a)); h = ri_cat(head, h, " " as *u8) 204 if ax[a] != TW_UNSET { h = ri_catn(head, h, ax[a]) } else { h = ri_cat(head, h, "unstated" as *u8) } 205 h = ri_cat(head, h, " of " as *u8); h = ri_catn(head, h, tv[a]) 206 h = ri_cat(head, h, " " as *u8); h = ri_cat(head, h, tw_axis_state_name(tw_axis_state(ax, tv, te, a))); h = ri_cat(head, h, "; " as *u8) 207 } 208 a = a + 1 209 } 210 h = ri_cat(head, h, "verdict " as *u8) 211 if rep[TWR_R_UNREACHED] + rep[TWR_R_UNSTATED] == 0 { h = ri_cat(head, h, "FIT" as *u8) } else { h = ri_cat(head, h, "PARTIAL" as *u8) } 212 h = ri_cat(head, h, "; genome " as *u8) 213 var k: i64 = 0 214 while k < TW_ALL_GENES { if k > 0 { h = ri_cat(head, h, "," as *u8) } h = ri_catn(head, h, ga[k]); k = k + 1 } 215 h = ri_cat(head, h, "; " as *u8); h = ri_catn(head, h, rep[TWR_R_EVALS]); h = ri_cat(head, h, " genomes evaluated. Only the axes the generator can state are fitted; the rest of the face is the canon, so this is not yet a likeness." as *u8) 216 head[h] = 0 as u8 217 let date: *u8 = sys_mmap(TWR_PATHB) 218 twr_today(date) 219 let skin: *i64 = sys_mmap(TW_SLOTS * TW_I64) as *i64 220 tw_skin_default(skin) 221 let rows: *u8 = sys_mmap(TWR_ROWB * TWR_VIEWS) 222 var ro: i64 = 0 223 var v: i64 = 0 224 while v < TWR_VIEWS { 225 let file: *u8 = sys_mmap(TWR_PATHB) 226 var f: i64 = ri_cat(file, 0, slug); f = ri_cat(file, f, "_" as *u8); f = ri_cat(file, f, twr_view_name(v)); f = ri_cat(file, f, ".png" as *u8); file[f] = 0 as u8 227 let path: *u8 = sys_mmap(TWR_PATHB) 228 var q: i64 = ri_cat(path, 0, outdir); q = ri_cat(path, q, "/" as *u8); q = ri_cat(path, q, file); path[q] = 0 as u8 229 let nb: i64 = tw_render_all_png(base, ga, skin, twr_view_yaw(v), twr_view_camz(v), path) 230 if nb <= 0 { return TWR_E_RENDER } 231 rep[TWR_R_RENDERS] = rep[TWR_R_RENDERS] + 1 232 rep[TWR_R_PNG_BYTES] = rep[TWR_R_PNG_BYTES] + nb 233 let sfx: *u8 = sys_mmap(TWR_PATHB) 234 var s2: i64 = ri_cat(sfx, 0, "-" as *u8); s2 = ri_cat(sfx, s2, twr_view_name(v)); sfx[s2] = 0 as u8 235 let tc: *u8 = sys_mmap(TWR_PATHB) 236 var t3: i64 = ri_cat(tc, 0, ", " as *u8); t3 = ri_cat(tc, t3, twr_view_words(v)); tc[t3] = 0 as u8 237 ro = twr_row_head(rows, ro, slug, sfx, "twin-fit" as *u8, date, "Generator fitted to the card " as *u8, slug, tc, hrefpfx, file) 238 ro = ri_cat(rows, ro, head) 239 ro = ri_cat(rows, ro, "\n" as *u8) 240 rep[TWR_R_ROWS] = rep[TWR_R_ROWS] + 1 241 v = v + 1 242 } 243 rows[ro] = 0 as u8 244 rep[TWR_R_ROW_BYTES] = ro 245 if twr_append_changed(results_path, rows, ro, rep) < 0 { return TWR_E_APPEND } 246 return 0 247} 248// SEED REPORT: synthetic people seed_lo..seed_hi, one whole-head render and one result row each 249func twr_seed_report(base: i64, seed_lo: i64, seed_hi: i64, outdir: *u8, hrefpfx: *u8, results_path: *u8, rep: *i64) -> i64 { 250 var z: i64 = 0 251 while z < TWR_R_N { rep[z] = 0; z = z + 1 } 252 if seed_hi < seed_lo { return TWR_E_RANGE } 253 let date: *u8 = sys_mmap(TWR_PATHB) 254 twr_today(date) 255 let skin: *i64 = sys_mmap(TW_SLOTS * TW_I64) as *i64 256 let idv: *i64 = sys_mmap(TW_SLOTS * TW_I64) as *i64 257 let eg: *i64 = sys_mmap(TW_SLOTS * TW_I64) as *i64 258 let rows: *u8 = sys_mmap(TWR_ROWB * (seed_hi - seed_lo + 1)) 259 var ro: i64 = 0 260 var seed: i64 = seed_lo 261 while seed <= seed_hi { 262 let num: *u8 = sys_mmap(TWR_PATHB) 263 var nn: i64 = ri_catn(num, 0, seed); num[nn] = 0 as u8 264 let file: *u8 = sys_mmap(TWR_PATHB) 265 var f: i64 = ri_cat(file, 0, "seed_" as *u8); f = ri_cat(file, f, num); f = ri_cat(file, f, ".png" as *u8); file[f] = 0 as u8 266 let path: *u8 = sys_mmap(TWR_PATHB) 267 var q: i64 = ri_cat(path, 0, outdir); q = ri_cat(path, q, "/" as *u8); q = ri_cat(path, q, file); path[q] = 0 as u8 268 persongen_build_eye(base, seed, skin, idv, eg) 269 sdfmt_render(base, TW_YAW_FRONTAL, TW_CAMZ_UNITS, skin[TW_SKIN_SLOT_R], skin[TW_SKIN_SLOT_G], skin[TW_SKIN_SLOT_B], TW_RENDER_WORKERS) 270 let fb: *i64 = (base + fb_off()) as *i64 271 let nb: i64 = png_publish(fb, ww(), hh(), path, TW_PNG_ANNOUNCE) 272 if nb <= 0 { return TWR_E_RENDER } 273 rep[TWR_R_RENDERS] = rep[TWR_R_RENDERS] + 1 274 rep[TWR_R_PNG_BYTES] = rep[TWR_R_PNG_BYTES] + nb 275 ro = twr_row_head(rows, ro, "seed-" as *u8, num, "synth-seed" as *u8, date, "Synthetic person, seed " as *u8, num, "" as *u8, hrefpfx, file) 276 ro = ri_cat(rows, ro, "Drawn from the gene ranges by the person generator: identity " as *u8) 277 var k: i64 = 0 278 while k < TW_GENES { if k > 0 { ro = ri_cat(rows, ro, "," as *u8) } ro = ri_catn(rows, ro, idv[k]); k = k + 1 } 279 ro = ri_cat(rows, ro, "; skin " as *u8) 280 ro = ri_catn(rows, ro, skin[TW_SKIN_SLOT_R]); ro = ri_cat(rows, ro, "," as *u8); ro = ri_catn(rows, ro, skin[TW_SKIN_SLOT_G]); ro = ri_cat(rows, ro, "," as *u8); ro = ri_catn(rows, ro, skin[TW_SKIN_SLOT_B]) 281 ro = ri_cat(rows, ro, "; eye genes " as *u8) 282 k = 0 283 while k < FA_EG_N { 284 if k > 0 { ro = ri_cat(rows, ro, "," as *u8) } 285 ro = ri_catn(rows, ro, eg[k]) 286 k = k + 1 287 } 288 ro = ri_cat(rows, ro, ".\n" as *u8) 289 rep[TWR_R_ROWS] = rep[TWR_R_ROWS] + 1 290 seed = seed + 1 291 } 292 rows[ro] = 0 as u8 293 rep[TWR_R_ROW_BYTES] = ro 294 if twr_append_changed(results_path, rows, ro, rep) < 0 { return TWR_E_APPEND } 295 return 0 296}