code wiki / _hdl_build / nx_frontier_put.nx

nx_frontier_put.nx source

↩ module page · 275 lines · 13494 B

1// nx_frontier_put.nx -- STORE-NATIVE edit verb for the frontier plane (operator 2026-07-18: "we are 2// supposed to use the nishi information management formats and system"). The seg-store IS the edit 3// surface; the flat TSV becomes a GENERATED VIEW re-exported on every put, byte-consistent, so the 4// cron seed-before-emit and legacy readers stay true during the transition (F-201b retires it). 5// nx_frontier_put set <id> <T|D|X> [prefix] [exportpath] 6// nx_frontier_put add <tab-row-9col> [prefix] [exportpath] 7// Fail-closed: unknown id / duplicate id / bad status commit NOTHING. flock on <prefix>plock. 8// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0 9import "nx_store_seed_lib.nx" 10import "nx_seg_store.nx" 11import "nx_syscalls.nx" 12 13const FP_CAP: i64 = 1048576 14const FP_TAB: i64 = 9 15const FP_NL: i64 = 10 16const FP_STDERR: i64 = 2 17const FP_LOCK_EX: i64 = 2 18const FP_MODE: i64 = 420 19const FP_SPAN: i64 = 16 20const FP_PATHCAP: i64 = 256 21const FP_HDRCAP: i64 = 512 22const FP_EXIT_USAGE: i64 = 2 23 24func fp_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 25func fp_werr(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(FP_STDERR, s, n); return 0 } 26func fp_vlen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 27func fp_cat(d: *u8, o: i64, s: *u8) -> i64 { var i: i64 = 0; while s[i] != (0 as u8) { d[o] = s[i]; o = o + 1; i = i + 1 } return o } 28func fp_catn(d: *u8, o: i64, v: i64) -> i64 { let t: *u8 = sys_mmap(28); var m: i64 = v; if m < 0 { m = 0 } var k: i64 = 0; if m == 0 { t[0] = 48 as u8; k = 1 } while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } var i: i64 = 0; while i < k { d[o] = t[k-1-i]; o = o + 1; i = i + 1 } return o } 29func fp_le(q: *u8, i: i64, n: i64) -> i64 { var e: i64 = i; var s: i64 = 1; while s == 1 { if e >= n { s = 0 } else { if q[e] == (FP_NL as u8) { s = 0 } else { e = e + 1 } } } return e } 30func fp_col(q: *u8, ls: i64, le: i64, c: i64, out: *i64) -> i64 { 31 var col: i64 = 0 32 var p: i64 = ls 33 while col < c { 34 var s: i64 = 1 35 while s == 1 { if p >= le { return 0 } if q[p] == (FP_TAB as u8) { s = 0 } else { p = p + 1 } } 36 p = p + 1 37 col = col + 1 38 } 39 var e: i64 = p 40 var s2: i64 = 1 41 while s2 == 1 { if e >= le { s2 = 0 } else { if q[e] == (FP_TAB as u8) { s2 = 0 } else { e = e + 1 } } } 42 out[0] = p 43 out[1] = e 44 return 1 45} 46func fp_span_lit(q: *u8, s: i64, e: i64, lit: *u8) -> i64 { 47 var i: i64 = 0 48 while s + i < e { if lit[i] == (0 as u8) { return 0 } if q[s+i] != lit[i] { return 0 } i = i + 1 } 49 if lit[i] != (0 as u8) { return 0 } 50 return 1 51} 52// AUTO-ID (eats debt seq40: concurrent sessions raced hand-picked F-ids). Scan col0 for every 53// <letter><digits> id, return the max numeric part (0 if none). Computed UNDER the flock and used 54// to assign <letter><max+1> atomically in the same critical section = no read-then-write race gap. 55// Suffixed ids (F103a) are skipped for the max (variants of a base number), harmless. 56func fp_max_fid(q: *u8, n: i64, letter: i64) -> i64 { 57 let c0: *i64 = sys_mmap(FP_SPAN) as *i64 58 var mx: i64 = 0 59 var i: i64 = 0 60 while i < n { 61 let le: i64 = fp_le(q, i, n) 62 if fp_col(q, i, le, 0, c0) == 1 { 63 let s: i64 = c0[0] 64 let e: i64 = c0[1] 65 if e - s >= 2 { if (q[s] as i64) == letter { 66 var allnum: i64 = 1 67 var v: i64 = 0 68 var j: i64 = s + 1 69 while j < e { let c: i64 = q[j] as i64; if c < 48 { allnum = 0; j = e } else { if c > 57 { allnum = 0; j = e } else { v = v * 10 + (c - 48); j = j + 1 } } } 70 if allnum == 1 { if v > mx { mx = v } } 71 } } 72 } 73 i = le + 1 74 } 75 return mx 76} 77// take the flock: open <prefix>plock append-create, LOCK_EX. returns fd or -1. 78func fp_lock(prefix: *u8) -> i64 { 79 let p: *u8 = sys_mmap(FP_PATHCAP) 80 var o: i64 = fp_cat(p, 0, prefix) 81 o = fp_cat(p, o, "plock" as *u8) 82 p[o] = 0 as u8 83 let fd: i64 = sys_openat_append(p, FP_MODE) 84 if fd < 0 { return 0 - 1 } 85 sys_flock(fd, FP_LOCK_EX) 86 return fd 87} 88// export the generated view: header + rows. returns 0 ok. 89func fp_export(path: *u8, rows: *u8, n: i64) -> i64 { 90 let fd: i64 = sys_openat_wr(path, FP_MODE) 91 if fd < 0 { return 0 - 1 } 92 let h: *u8 = sys_mmap(FP_HDRCAP) 93 h[0] = 35 as u8 94 var o: i64 = fp_cat(h, 1, " GENERATED VIEW (do not hand-edit): SSOT = the seg-store; edit via nx_frontier_put set/add (MCP). Cron re-seeds the store from this view transitionally, so it stays byte-consistent.\n" as *u8) 95 sys_write(fd, h, o) 96 sys_write(fd, rows, n) 97 sys_close(fd) 98 return 0 99} 100func main(argc: i64, argv: *i64) -> i64 { 101 if argc < 3 { fp_werr("usage: nx_frontier_put {set <id> <T|D|X> | add <tab-row-9col> | addauto <tab-row-8col-no-id>} [prefix] [exportpath]\n" as *u8); sys_exit(FP_EXIT_USAGE); return FP_EXIT_USAGE } 102 let verb: *u8 = argv[1] as *u8 103 var is_set: i64 = 0 104 var is_add: i64 = 0 105 var is_auto: i64 = 0 106 if fp_span_lit(verb, 0, fp_vlen(verb), "set" as *u8) == 1 { is_set = 1 } 107 if fp_span_lit(verb, 0, fp_vlen(verb), "add" as *u8) == 1 { is_add = 1 } 108 if fp_span_lit(verb, 0, fp_vlen(verb), "addauto" as *u8) == 1 { is_auto = 1 } 109 if is_set == 0 { if is_add == 0 { if is_auto == 0 { fp_werr("PUT-FAIL unknown verb\n" as *u8); sys_exit(FP_EXIT_USAGE); return FP_EXIT_USAGE } } } 110 var prefix: *u8 = "knowledge/store/frontier-" as *u8 111 var expath: *u8 = "knowledge/registry/frontier_queue.tsv" as *u8 112 if is_set == 1 { 113 if argc < 4 { fp_werr("PUT-FAIL set needs <id> <T|D|X>\n" as *u8); sys_exit(FP_EXIT_USAGE); return FP_EXIT_USAGE } 114 if argc > 4 { prefix = argv[4] as *u8 } 115 if argc > 5 { expath = argv[5] as *u8 } 116 let id: *u8 = argv[2] as *u8 117 let st: *u8 = argv[3] as *u8 118 if fp_vlen(st) != 1 { fp_werr("PUT-FAIL status must be one of T D X\n" as *u8); sys_exit(1); return 1 } 119 var stok: i64 = 0 120 if st[0] == (84 as u8) { stok = 1 } 121 if st[0] == (68 as u8) { stok = 1 } 122 if st[0] == (88 as u8) { stok = 1 } 123 if stok == 0 { fp_werr("PUT-FAIL status must be one of T D X\n" as *u8); sys_exit(1); return 1 } 124 let lk: i64 = fp_lock(prefix) 125 if lk < 0 { fp_werr("PUT-FAIL cannot lock\n" as *u8); sys_exit(1); return 1 } 126 let q: *u8 = sys_mmap(FP_CAP) 127 let n: i64 = sts_load(prefix, q, FP_CAP) 128 if n <= 0 { fp_werr("PUT-FAIL store empty or unseeded\n" as *u8); sys_exit(1); return 1 } 129 let out: *u8 = sys_mmap(FP_CAP) 130 let c0: *i64 = sys_mmap(FP_SPAN) as *i64 131 let c5: *i64 = sys_mmap(FP_SPAN) as *i64 132 var o: i64 = 0 133 var found: i64 = 0 134 var i: i64 = 0 135 while i < n { 136 let le: i64 = fp_le(q, i, n) 137 var mt: i64 = 0 138 if fp_col(q, i, le, 0, c0) == 1 { if fp_span_lit(q, c0[0], c0[1], id) == 1 { mt = 1 } } 139 if mt == 1 { 140 if fp_col(q, i, le, 5, c5) == 1 { 141 var t: i64 = i 142 while t < c5[0] { out[o] = q[t]; o = o + 1; t = t + 1 } 143 out[o] = st[0] 144 o = o + 1 145 var t2: i64 = c5[1] 146 while t2 < le { out[o] = q[t2]; o = o + 1; t2 = t2 + 1 } 147 found = found + 1 148 } else { mt = 0 } 149 } 150 if mt == 0 { var t3: i64 = i; while t3 < le { out[o] = q[t3]; o = o + 1; t3 = t3 + 1 } } 151 out[o] = FP_NL as u8 152 o = o + 1 153 i = le + 1 154 } 155 if found != 1 { fp_werr("PUT-FAIL id not found (nothing committed)\n" as *u8); sys_exit(1); return 1 } 156 let cnt: i64 = sts_seed(prefix, out, o) 157 if cnt < 0 { fp_werr("PUT-FAIL commit error\n" as *u8); sys_exit(1); return 1 } 158 fp_export(expath, out, o) 159 let msg: *u8 = sys_mmap(FP_HDRCAP) 160 var m: i64 = fp_cat(msg, 0, "PUT OK set id=" as *u8) 161 m = fp_cat(msg, m, id) 162 m = fp_cat(msg, m, " status=" as *u8) 163 msg[m] = st[0] 164 m = m + 1 165 m = fp_cat(msg, m, " rows=" as *u8) 166 m = fp_catn(msg, m, cnt) 167 m = fp_cat(msg, m, " exported=1\n" as *u8) 168 sys_write(1, msg, m) 169 sys_exit(0) 170 return 0 171 } 172 // addauto (seq40): id auto-assigned as F<max+1> ATOMICALLY under the flock. Row = 8 cols 173 // (title sev prio owner status deps extra lane) -- the id column is prepended by the organ. 174 if is_auto == 1 { 175 if argc < 3 { fp_werr("PUT-FAIL addauto needs <row-8col-no-id>\n" as *u8); sys_exit(FP_EXIT_USAGE); return FP_EXIT_USAGE } 176 var aprefix: *u8 = "knowledge/store/frontier-" as *u8 177 var aexpath: *u8 = "knowledge/registry/frontier_queue.tsv" as *u8 178 if argc > 3 { aprefix = argv[3] as *u8 } 179 if argc > 4 { aexpath = argv[4] as *u8 } 180 let arow: *u8 = argv[2] as *u8 181 let arl: i64 = fp_vlen(arow) 182 var atabs: i64 = 0 183 var ati: i64 = 0 184 while ati < arl { if arow[ati] == (FP_TAB as u8) { atabs = atabs + 1 } ati = ati + 1 } 185 if atabs != 7 { fp_werr("PUT-FAIL addauto row must have 8 tab-separated cols (id auto-assigned)\n" as *u8); sys_exit(1); return 1 } 186 let alk: i64 = fp_lock(aprefix) 187 if alk < 0 { fp_werr("PUT-FAIL cannot lock\n" as *u8); sys_exit(1); return 1 } 188 let aq: *u8 = sys_mmap(FP_CAP) 189 let an: i64 = sts_load(aprefix, aq, FP_CAP) 190 if an <= 0 { fp_werr("PUT-FAIL store empty or unseeded\n" as *u8); sys_exit(1); return 1 } 191 let nextid: i64 = fp_max_fid(aq, an, 70) + 1 192 let aout: *u8 = sys_mmap(FP_CAP) 193 var ao: i64 = 0 194 var aci: i64 = 0 195 while aci < an { aout[ao] = aq[aci]; ao = ao + 1; aci = aci + 1 } 196 aout[ao] = 70 as u8 197 ao = ao + 1 198 ao = fp_catn(aout, ao, nextid) 199 aout[ao] = FP_TAB as u8 200 ao = ao + 1 201 var ari: i64 = 0 202 while ari < arl { aout[ao] = arow[ari]; ao = ao + 1; ari = ari + 1 } 203 aout[ao] = FP_NL as u8 204 ao = ao + 1 205 // O(1) APPEND 2026-07-30: addauto is a PURE APPEND (every existing row is copied VERBATIM, 206 // then one new row is added), so re-seeding the whole plane was O(rows x segments) write 207 // amplification on the hottest frontier path. aout must STILL be built in full because 208 // fp_export below writes the complete flat export -- so we keep the buffer and hand 209 // sts_append_fast only the NEW ROW SLICE: it starts at offset an (after the copied rows) 210 // and ao-an-1 drops the trailing newline the bare-row contract does not want. 211 // The put/close paths above are MODIFY, not append -- they correctly keep sts_seed. 212 var acnt: i64 = sts_append_fast(aprefix, (aout as i64 + an) as *u8, ao - an - 1) 213 if acnt < 0 { acnt = sts_seed(aprefix, aout, ao) } 214 if acnt < 0 { fp_werr("PUT-FAIL commit error\n" as *u8); sys_exit(1); return 1 } 215 fp_export(aexpath, aout, ao) 216 let amsg: *u8 = sys_mmap(FP_HDRCAP) 217 var am: i64 = fp_cat(amsg, 0, "PUT OK addauto id=F" as *u8) 218 am = fp_catn(amsg, am, nextid) 219 am = fp_cat(amsg, am, " rows=" as *u8) 220 am = fp_catn(amsg, am, acnt) 221 am = fp_cat(amsg, am, " exported=1\n" as *u8) 222 sys_write(1, amsg, am) 223 sys_exit(0) 224 return 0 225 } 226 // add 227 if argc > 3 { prefix = argv[3] as *u8 } 228 if argc > 4 { expath = argv[4] as *u8 } 229 let row: *u8 = argv[2] as *u8 230 let rl: i64 = fp_vlen(row) 231 var tabs: i64 = 0 232 var ti: i64 = 0 233 while ti < rl { if row[ti] == (FP_TAB as u8) { tabs = tabs + 1 } ti = ti + 1 } 234 if tabs != 8 { fp_werr("PUT-FAIL row must have 9 tab-separated cols\n" as *u8); sys_exit(1); return 1 } 235 var ide: i64 = 0 236 var s: i64 = 1 237 while s == 1 { if ide >= rl { s = 0 } else { if row[ide] == (FP_TAB as u8) { s = 0 } else { ide = ide + 1 } } } 238 if ide == 0 { fp_werr("PUT-FAIL empty id\n" as *u8); sys_exit(1); return 1 } 239 let lk2: i64 = fp_lock(prefix) 240 if lk2 < 0 { fp_werr("PUT-FAIL cannot lock\n" as *u8); sys_exit(1); return 1 } 241 let q2: *u8 = sys_mmap(FP_CAP) 242 let n2: i64 = sts_load(prefix, q2, FP_CAP) 243 let c0b: *i64 = sys_mmap(FP_SPAN) as *i64 244 var j: i64 = 0 245 while j < n2 { 246 let le2: i64 = fp_le(q2, j, n2) 247 if fp_col(q2, j, le2, 0, c0b) == 1 { 248 if c0b[1] - c0b[0] == ide { 249 var eq: i64 = 1 250 var k: i64 = 0 251 while k < ide { if q2[c0b[0]+k] != row[k] { eq = 0 } k = k + 1 } 252 if eq == 1 { fp_werr("PUT-FAIL id already exists (nothing committed)\n" as *u8); sys_exit(1); return 1 } 253 } 254 } 255 j = le2 + 1 256 } 257 let out2: *u8 = sys_mmap(FP_CAP) 258 var o2: i64 = 0 259 var ci: i64 = 0 260 while ci < n2 { out2[o2] = q2[ci]; o2 = o2 + 1; ci = ci + 1 } 261 var ri: i64 = 0 262 while ri < rl { out2[o2] = row[ri]; o2 = o2 + 1; ri = ri + 1 } 263 out2[o2] = FP_NL as u8 264 o2 = o2 + 1 265 let cnt2: i64 = sts_seed(prefix, out2, o2) 266 if cnt2 < 0 { fp_werr("PUT-FAIL commit error\n" as *u8); sys_exit(1); return 1 } 267 fp_export(expath, out2, o2) 268 let msg2: *u8 = sys_mmap(FP_HDRCAP) 269 var m2: i64 = fp_cat(msg2, 0, "PUT OK add rows=" as *u8) 270 m2 = fp_catn(msg2, m2, cnt2) 271 m2 = fp_cat(msg2, m2, " exported=1\n" as *u8) 272 sys_write(1, msg2, m2) 273 sys_exit(0) 274 return 0 275}