code wiki / _hdl_build / nx_hf_tree_parse_gate.nx
nx_hf_tree_parse_gate.nx source
↩ module page · 62 lines · 4218 B
1// nx_hf_tree_parse_gate.nx -- GATE for nx_hf_tree_parse (M2b: per-shard path/size/sha256 for a verified pull).
2// Over a real-shaped HF tree fixture (a non-LFS config.json + one LFS .safetensors shard whose top-level "size"
3// is the pointer size 99 but whose lfs "size" is the REAL 16060 -> proves we read the LFS size, not the pointer):
4// T1 shard count hft_count(".safetensors") == 1
5// T2 lfs count hft_count("\"lfs\":{\"oid\":\"") == 1
6// T3 lfs sha256 hft_lfs_oid -> the exact 64-hex digest (= what the orchestrator fail-closed verifies against)
7// T4 real file size hft_num_field from the oid -> 16060 (the lfs size, NOT the 99 pointer size)
8// T5 paths in order path[0]="config.json", path[1]="model-00001-of-00002.safetensors"
9// GREEN iff T1-T5 pass. Sovereign nx_cc->nxasm. expect_exit: 0 license_tier: ORIGINAL
10import "nx_hf_tree_parse.nx"
11import "nx_syscalls.nx"
12
13func w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
14func 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 }
15func streq(a: *u8, b: *u8) -> i64 { var i: i64=0; while 1==1 { if a[i]!=b[i] {return 0} if a[i]==(0 as u8) {return 1} i=i+1 } return 1 }
16
17func main() -> i64 {
18 w("=== nx_hf_tree_parse_gate: per-shard path/size/sha256 from HF tree JSON (M2b) ===\n" as *u8)
19 var pass: i64=0; var total: i64=0
20
21 let json: *u8 = "[{\"type\":\"file\",\"oid\":\"deadbeef\",\"size\":900,\"path\":\"config.json\"},{\"type\":\"file\",\"oid\":\"cafef00d\",\"size\":99,\"path\":\"model-00001-of-00002.safetensors\",\"lfs\":{\"oid\":\"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\",\"size\":16060,\"pointerSize\":135}}]" as *u8
22 var n: i64=0; while json[n]!=(0 as u8){n=n+1}
23 let known_sha: *u8 = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" as *u8
24
25 // ---- T1: shard count ----
26 let shards: i64 = hft_count(json, n, ".safetensors" as *u8)
27 var t1: i64=1; if shards != 1 { t1=0 }
28 total=total+1; if t1==1 { pass=pass+1; w(" [PASS] " as *u8) } else { w(" [FAIL] " as *u8) }
29 w("T1 shard count=" as *u8); wn(shards); w(" (expect 1)\n" as *u8)
30
31 // ---- T2: lfs entry count ----
32 let lfs: i64 = hft_count(json, n, "\"lfs\":{\"oid\":\"" as *u8)
33 var t2: i64=1; if lfs != 1 { t2=0 }
34 total=total+1; if t2==1 { pass=pass+1; w(" [PASS] " as *u8) } else { w(" [FAIL] " as *u8) }
35 w("T2 lfs count=" as *u8); wn(lfs); w(" (expect 1)\n" as *u8)
36
37 // ---- T3: lfs sha256 ----
38 let sha: *u8 = sys_mmap(80)
39 let oid_start: i64 = hft_lfs_oid(json, n, 0, sha)
40 var t3: i64=1; if oid_start < 0 { t3=0 } else { if streq(sha, known_sha)!=1 { t3=0 } }
41 total=total+1; if t3==1 { pass=pass+1; w(" [PASS] " as *u8) } else { w(" [FAIL] " as *u8) }
42 w("T3 lfs sha256=" as *u8); w(sha); w("\n" as *u8)
43
44 // ---- T4: real (lfs) file size, not the pointer size ----
45 let sz: i64 = hft_num_field(json, n, oid_start, "\"size\":" as *u8)
46 var t4: i64=1; if sz != 16060 { t4=0 }
47 total=total+1; if t4==1 { pass=pass+1; w(" [PASS] " as *u8) } else { w(" [FAIL] " as *u8) }
48 w("T4 lfs size=" as *u8); wn(sz); w(" (expect 16060, not the 99 pointer size)\n" as *u8)
49
50 // ---- T5: paths in order ----
51 let p1: *u8 = sys_mmap(128); let s1: i64 = hft_path(json, n, 0, p1)
52 let p2: *u8 = sys_mmap(128); hft_path(json, n, s1 + 1, p2)
53 var t5: i64=1
54 if streq(p1, "config.json" as *u8)!=1 { t5=0 }
55 if streq(p2, "model-00001-of-00002.safetensors" as *u8)!=1 { t5=0 }
56 total=total+1; if t5==1 { pass=pass+1; w(" [PASS] " as *u8) } else { w(" [FAIL] " as *u8) }
57 w("T5 path[0]=" as *u8); w(p1); w(" path[1]=" as *u8); w(p2); w("\n" as *u8)
58
59 w("\n=== nx_hf_tree_parse_gate " as *u8); wn(pass); w("/" as *u8); wn(total)
60 if pass==total { w(" GREEN (HF tree -> per-shard path+size+sha256; the per-file digests a fail-closed weights pull verifies against)\n" as *u8); sys_exit(0); return 0 }
61 w(" RED\n" as *u8); sys_exit(1); return 1
62}