code wiki / (root) / nx_model_enumerate.nx

nx_model_enumerate.nx source

↩ module page · 74 lines · 4074 B

1// nx_model_enumerate.nx -- the STANDARD, reusable model-port ENUMERATE stage: fetch a HuggingFace repo's tree API 2// over sovereign TLS and report reachability + the model's shape (how many .safetensors shards, config present). 3// File-DRIVEN (reads the tree-API URL from data/mp_target_url.txt) so it ports ANY model with no code edit -- unlike 4// the per-model hardcoded probes (nx_hf_enumerate_probe pins Apertus). Self-contained substring parse (no dependency 5// on the currently-drifted nx_hf_tree_parse; a substring count survives chunked framing). This is stage-1 of the 6// generic port driver the model-port registry dispatches. license_tier: ORIGINAL expect_exit: 0 7import "nx_syscalls.nx" 8import "nx_x509_trust_store.nx" 9import "nx_trust_store_load_from_certdata.nx" 10import "nx_https_fetch_follow.nx" 11const K_MAGIC_2048: i64 = 2048 12const K_MAGIC_2047: i64 = 2047 13const K_MAGIC_4194304: i64 = 4194304 14const K_MAGIC_4000: i64 = 4000 15 16func w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 17func 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 } 18 19// non-overlapping substring count of needle (NUL-terminated) in buf[0..n). 20func me_count(buf: *u8, n: i64, needle: *u8) -> i64 { 21 var m: i64 = 0; while needle[m] != (0 as u8) { m = m + 1 } 22 if m == 0 { return 0 } 23 var cnt: i64 = 0 24 var i: i64 = 0 25 while i + m <= n { 26 var j: i64 = 0; var ok: i64 = 1 27 while j < m { if buf[i+j] != needle[j] { ok = 0; j = m } else { j = j + 1 } } 28 if ok == 1 { cnt = cnt + 1; i = i + m } else { i = i + 1 } 29 } 30 return cnt 31} 32 33func main() -> i64 { 34 w("=== nx_model_enumerate: sovereign HF tree enumerate (file-driven, standard port stage-1) ===\n" as *u8) 35 36 // read the tree-API URL from data/mp_target_url.txt (strip trailing whitespace/newline) 37 let fd: i64 = sys_openat_rd("data/mp_target_url.txt" as *u8) 38 if fd < 0 { w(" FAIL: cannot open data/mp_target_url.txt\n" as *u8); sys_exit(1); return 1 } 39 let urlbuf: *u8 = sys_mmap(K_MAGIC_2048) 40 var ul: i64 = sys_read(fd, urlbuf, K_MAGIC_2047) 41 sys_close(fd) 42 while ul > 0 { if urlbuf[ul-1] <= (32 as u8) { ul = ul - 1 } else { ul = 0 - ul } } 43 if ul < 0 { ul = 0 - ul } 44 urlbuf[ul] = 0 as u8 45 w(" url: " as *u8); w(urlbuf); w("\n" as *u8) 46 47 let r: i64 = nx_trust_store_load_from_certdata("data/mozilla_certdata.txt" as *u8, 512, K_MAGIC_4194304) 48 if r <= 0 { w(" FAIL: certdata load (data/mozilla_certdata.txt)\n" as *u8); sys_exit(1); return 1 } 49 let store: *TrustStore = r as *TrustStore 50 51 let cap: i64 = K_MAGIC_4194304 52 let out: *u8 = sys_mmap(cap) 53 let status: *i64 = sys_mmap(8) as *i64 54 let tn: i64 = nx_https_fetch_follow(urlbuf, store, out, cap, 6, status) 55 w(" fetch status=" as *u8); wn(status[0]); w(" raw-bytes=" as *u8); wn(tn); w("\n" as *u8) 56 if tn <= 0 { w(" FAIL: tree fetch returned no body\n" as *u8); sys_exit(1); return 1 } 57 58 let st: i64 = me_count(out, tn, ".safetensors" as *u8) 59 let idx: i64 = me_count(out, tn, ".safetensors.index.json" as *u8) 60 let cfg: i64 = me_count(out, tn, "config.json" as *u8) 61 let lfs: i64 = me_count(out, tn, "\"lfs\"" as *u8) 62 w(" .safetensors mentions=" as *u8); wn(st); w(" index.json=" as *u8); wn(idx); w(" config.json=" as *u8); wn(cfg); w(" lfs-blocks=" as *u8); wn(lfs); w("\n" as *u8) 63 64 // print the full tree body (small repos fit) so the .safetensors path + size + lfs.oid are visible 65 w(" body: " as *u8) 66 var p: i64 = 0 67 while p < K_MAGIC_4000 { if p < tn { sys_write(1, ((out as i64) + p) as *u8, 1) } p = p + 1 } 68 w("\n" as *u8) 69 70 if st >= 1 { w("REACHABLE + safetensors present -> resolvable target\n" as *u8); sys_exit(0); return 0 } 71 w("REACHABLE but no .safetensors found (wrong repo id or non-safetensors model)\n" as *u8) 72 sys_exit(0) 73 return 0 74}