code wiki / _hdl_build / nx_dist_publish_run.nx
nx_dist_publish_run.nx source
↩ module page · 67 lines · 4203 B
1// nx_dist_publish_run.nx -- MAKE IT REAL: a runnable that publishes a REAL file on the NAS into the hub's
2// distribution index (operator: "when we get data on the hub we can pass it to other systems"). Runs ON THE NAS
3// (hashing a 1.25GB shard over 9p would crash WSL). Composes the enabler (nx_dist_publish): SHA-256 the real bytes
4// -> content-id -> BT-v2 magnet + HTTPS url + /api/dist + append a row to /volume1/ai/dist/dist_index.conf (the
5// persistent Napster registry), idempotent (skip if the cid is already present -- the has-cid check is inlined to
6// keep this a single-lib import). Launched by the allowlisted nx_hostctl `distpub` action. expect_exit: 0
7// license_tier: ORIGINAL
8import "nx_dist_publish.nx"
9import "nx_syscalls.nx"
10const K_MAGIC_1048576: i64 = 1048576
11const K_MAGIC_1024: i64 = 1024
12const K_MAGIC_4096: i64 = 4096
13
14func w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
15func wn(v: i64) -> i64 { var m: i64=v; if m<0{w("-" as *u8);m=0-m} let t:*u8=sys_mmap(24); 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; let o:*u8=sys_mmap(24); while i<k{o[i]=t[k-1-i];i=i+1} sys_write(1,o,k); return 0 }
16func dpr_strlen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
17func dpr_contains(hay: *u8, hlen: i64, needle: *u8) -> i64 {
18 let nl: i64 = dpr_strlen(needle); if nl==0 { return 1 }
19 var i: i64=0; while i+nl<=hlen { var ok: i64=1; var j: i64=0; while j<nl { if hay[i+j]!=needle[j] { ok=0 } j=j+1 } if ok==1 { return 1 } i=i+1 }
20 return 0
21}
22// idempotency: is <TAB>cid<TAB> already in the registry buffer?
23func dpr_has_cid(buf: *u8, blen: i64, cid: *u8) -> i64 {
24 let ndl: *u8 = sys_mmap(128); var o: i64=0; ndl[o]=9 as u8; o=o+1
25 var i: i64=0; while cid[i]!=(0 as u8) { ndl[o]=cid[i]; o=o+1; i=i+1 } ndl[o]=9 as u8; o=o+1; ndl[o]=0 as u8
26 return dpr_contains(buf, blen, ndl)
27}
28
29func main() -> i64 {
30 w("=== nx_dist_publish_run: publish a REAL NAS file into the hub distribution index ===\n" as *u8)
31 let target: *u8 = "/volume1/ai/apertus/model.safetensors.index.json" as *u8
32 let name: *u8 = "Apertus-8B-Instruct-2509-model.safetensors.index.json" as *u8
33 let idx: *u8 = "/volume1/ai/dist/dist_index.conf" as *u8
34
35 let cid: *u8 = sys_mmap(128)
36 w(" hashing " as *u8); w(target); w(" ...\n" as *u8)
37 let sz: i64 = dp_content_id(target, cid)
38 if sz < 0 { w(" TARGET NOT FOUND\n" as *u8); sys_exit(1); return 1 }
39
40 let ibuf: *u8 = sys_mmap(K_MAGIC_1048576); var ilen: i64 = 0
41 let rfd: i64 = sys_openat_rd(idx)
42 if rfd >= 0 { ilen = sys_read(rfd, ibuf, K_MAGIC_1048576); sys_close(rfd); if ilen<0 { ilen=0 } }
43 if dpr_has_cid(ibuf, ilen, cid)==1 { w(" ALREADY PUBLISHED (idempotent, cid present)\n cid=" as *u8); w(cid); w("\n" as *u8); sys_exit(0); return 0 }
44
45 let mag: *u8 = sys_mmap(K_MAGIC_1024); dp_magnet(cid, name, mag)
46 let url: *u8 = sys_mmap(K_MAGIC_1024); dp_dl_url(cid, name, url)
47 let api: *u8 = sys_mmap(256); dp_api(cid, api)
48 let row: *u8 = sys_mmap(K_MAGIC_4096); let rl: i64 = dp_index_row(name, cid, sz, mag, url, row)
49
50 var fd: i64 = sys_openat_append(idx, 0x1a4)
51 if fd < 0 { let cf: i64 = sys_openat_wr(idx, 0x1a4); if cf>=0 { sys_close(cf) } fd = sys_openat_append(idx, 0x1a4) }
52 if fd < 0 { w(" INDEX OPEN FAILED\n" as *u8); sys_exit(1); return 1 }
53 // append the internal source path (6th field) so the serve daemon can resolve cid -> bytes; then newline
54 var ro: i64 = rl; row[ro]=9 as u8; ro=ro+1
55 var si: i64=0; while target[si]!=(0 as u8) { row[ro]=target[si]; ro=ro+1; si=si+1 }
56 sys_write(fd, row, ro); let nl: *u8 = sys_mmap(2); nl[0]=10 as u8; sys_write(fd, nl, 1); sys_close(fd)
57
58 w(" PUBLISHED:\n name=" as *u8); w(name)
59 w("\n cid=" as *u8); w(cid)
60 w("\n size=" as *u8); wn(sz); w(" bytes" as *u8)
61 w("\n magnet=" as *u8); w(mag)
62 w("\n download=" as *u8); w(url)
63 w("\n api=" as *u8); w(api)
64 w("\n -> row appended to /volume1/ai/dist/dist_index.conf\n" as *u8)
65 w("=== GREEN (real file published to the hub: magnet + download + api + index) ===\n" as *u8)
66 sys_exit(0); return 0
67}