nx_craft_resident_morf_gate.nx source
↩ module page · 141 lines · 8881 B
1// ORIGINAL. Build against the reviewed canonical craft candidate, not its dated name.
2import "nx_syscalls.nx"
3import "nx_gate_verdict.nx"
4import "nx_wasm_craft.nx"
5
6func crg_copy(dst: *u8,src: *u8,n: i64) -> i64 {
7 var i: i64 = 0
8 while i < n { dst[i] = src[i]; i = i+1 }
9 return 0
10}
11func crg_run(path: *u8,ctr: *i64) -> i64 {
12 let size: *i64 = sys_mmap(MVC_WORD) as *i64
13 if (size as i64) <= 0 { return 0-1 }
14 let raw: *u8 = sys_read_file(path,size)
15 if (raw as i64) <= 0 { return 0-1 }
16 let flen: i64 = size[0]
17 if flen%MVC_WORD != 0 { return WCR_E_STAGE }
18 let raw_hash: i64 = nxa_check2(1,raw as *i64,flen/MVC_WORD)
19 if flen > wc_resident_stage_cap() { return WCR_E_STAGE }
20 let h: *i64 = raw as *i64
21 let sk: i64 = nxa_counted_section(raw,flen,nxa_tag4("SKEL" as *u8),NA_SKEL_W)
22 let an: i64 = nxa_section_entry(raw,flen,nxa_tag4("ANIM" as *u8))
23 let po: i64 = nxa_section_entry(raw,flen,nxa_tag4("POSE" as *u8))
24 if sk < 0 { return sk }; if an < 0 { return an }
25 let nj: i64 = h[sk]; let naw: i64 = h[an+2]
26 var npw: i64 = 0
27 if po >= 0 { npw = h[po+2] }
28 if po < 0 { if po != 0-1 { return po } }
29 if nj > NXA_NJ_CAP { return NXA_RC_NJ_CAP }
30 if naw > NXA_ANIM_CAP_W { return NXA_RC_NAW_CAP }
31 if npw > NXA_POSE_CAP_W { return NXA_RC_NPW_CAP }
32 let initial: i64 = sys_mmap(CRAFT_EXT_TOTAL) as i64
33 if initial <= 0 { return 0-1 }
34 gv_check_eq("pre-ingest zero token refuses without optional allocation" as *u8,wc_resident_cast(initial,1,0),WCR_E_TOKEN,ctr)
35 crg_copy((initial+O_FB) as *u8,raw,flen)
36 wc_morf_copy((initial+O_NXA_SKEL) as *i64,((raw as i64)+(sk+1)*MVC_WORD) as *i64,nj*NA_SKEL_W)
37 wc_morf_copy((initial+O_NXA_ANIM) as *i64,((raw as i64)+h[an+1]) as *i64,naw)
38 if npw > 0 { wc_morf_copy((initial+O_NXA_POSE) as *i64,((raw as i64)+h[po+1]) as *i64,npw) }
39 let ingested: i64 = wc_nxa_ingest(initial,nj,naw,npw)
40 gv_check_eq("real source clip admitted by existing engine" as *u8,ingested,0,ctr)
41 if ingested < 0 { return ingested }
42 gv_check_eq("inactive token refuses without allocated optional bank" as *u8,wc_resident_cast(initial,1,0),WCR_E_TOKEN,ctr)
43 let pages: i64 = wc_resident_pages(initial,flen)
44 gv_check("real staged asset derives positive page request" as *u8,pages > 0,ctr)
45 if pages < 0 { return pages }
46 let m: *NmiAsset = wc_resident_morph(initial,flen)
47 let needed: i64 = wc_morf_storage_bytes(wc_resident_draw(initial,flen),m.tag_words)
48 let exact: i64 = O_WCM+needed
49 gv_check_eq("page request covers exactly rounded dynamic footprint" as *u8,pages,(exact+WASM_PAGE_BYTES-1)/WASM_PAGE_BYTES,ctr)
50 let base: i64 = sys_mmap(exact+MVC_WORD) as i64
51 if base <= 0 { return 0-1 }
52 crg_copy(base as *u8,initial as *u8,CRAFT_EXT_TOTAL)
53 let sentinel: *i64 = (base+exact) as *i64
54 sentinel[0] = 771
55 gv_check_eq("one-byte-short real allocation refuses" as *u8,wc_resident_bind(base,flen,exact-1,1),WCM_RC_CAPACITY,ctr)
56 gv_check_eq("capacity refusal does not publish token" as *u8,wc_resident_current(base,1),0,ctr)
57 gv_check_eq("relocated arena reconstructs views and binds real asset" as *u8,wc_resident_bind(base,flen,exact,1),0,ctr)
58 gv_check_eq("exact allocation tail remains untouched" as *u8,sentinel[0],771,ctr)
59 gv_check_eq("matching resident token published" as *u8,wc_resident_current(base,1),1,ctr)
60 gv_check_eq("different resident token refuses" as *u8,wc_resident_tag(base,2,0),WCR_E_TOKEN,ctr)
61 let mobs: *i64 = mobp(base)
62 en_init(mobs,MOB_CAP,MOB_NCOMP)
63 gv_check("actual entity spawned" as *u8,en_spawn(mobs) > 0,ctr)
64 gv_check_eq("new bank zero weights retain incumbent float path" as *u8,wc_resident_cast(base,1,0),0,ctr)
65 let d: *WcmDraw = wc_morf_descriptor(base)
66 let a: *MrdAsset = d.asset; let v: *MvcView = a.morph
67 var selected: i64 = 0-1; var c: i64 = 0
68 while c < v.channels {
69 var j: i64 = 0
70 while j < v.face_words*MVC_AXES {
71 if v.delta[c*v.face_words*MVC_AXES+j] != 0 { if selected < 0 { selected = c } }
72 j = j+1
73 }
74 c = c+1
75 }
76 gv_check("real asset contains a nonzero channel" as *u8,selected >= 0,ctr)
77 if selected < 0 { return WCR_E_STAGE }
78 gv_check_eq("actual entity accepts source-unit channel weight" as *u8,wc_resident_weight(base,1,0,selected,v.unit),0,ctr)
79 gv_check("owned real geometry actually moves" as *u8,wc_resident_cast(base,1,0) > 0,ctr)
80 gv_check_eq("actual owner reads prepared position address" as *u8,wc_resident_positions(base,1,0),d.positions as i64,ctr)
81 let ready_before: i64 = d.ready
82 gv_check_eq("same operation retry succeeds without resetting state" as *u8,wc_resident_bind(base,flen,exact,1),0,ctr)
83 gv_check_eq("same operation retry preserves active weight" as *u8,d.weights[selected],v.unit,ctr)
84 gv_check_eq("same operation retry preserves prepared output" as *u8,d.ready,ready_before,ctr)
85 let changed: *MvcView = wc_resident_view(base,flen)
86 let old_delta: i64 = changed.delta[0]
87 changed.delta[0] = old_delta+1
88 let sh: *i64 = (base+O_FB) as *i64
89 // Find the TOC entry directly: mutation intentionally invalidated checksum.
90 var me: i64 = 0-1; var si: i64 = 0
91 while si < sh[2] { if sh[4+si*4] == nxa_tag4("MORF" as *u8) { me = 4+si*4 }; si = si+1 }
92 if me < 0 { return WCR_E_STAGE }
93 sh[me+3] = nxa_check2(1,(base+O_FB+sh[me+1]) as *i64,sh[me+2])
94 sh[3] = nxa_check2(1,(base+O_FB+4*MVC_WORD) as *i64,sh[2]*4)
95 gv_check_eq("changed checksummed MORF remains canonically admitted" as *u8,wc_resident_admit(base,flen),0,ctr)
96 gv_check_eq("same token with changed native operation conflicts" as *u8,wc_resident_bind(base,flen,exact,1),WCR_E_CONFLICT,ctr)
97 gv_check_eq("conflict preserves active weight" as *u8,d.weights[selected],v.unit,ctr)
98 gv_check_eq("conflict preserves prepared output" as *u8,d.ready,ready_before,ctr)
99 crg_copy((base+O_FB) as *u8,raw,flen)
100 // A later framebuffer render cannot change any admitted bank payload.
101 let bank_hash: i64 = nxa_check2(1,(base+O_WCM) as *i64,needed/MVC_WORD)
102 let staged_header: *i64 = (base+O_FB) as *i64
103 staged_header[0] = 0
104 gv_check("malformed overwritten staging refuses rebind" as *u8,wc_resident_bind(base,flen,exact,2) < 0,ctr)
105 gv_check_eq("refused replacement retains old resident" as *u8,wc_resident_current(base,1),1,ctr)
106 gv_check_eq("overwritten staging leaves owned bank identical" as *u8,nxa_check2(1,(base+O_WCM) as *i64,needed/MVC_WORD),bank_hash,ctr)
107 crg_copy((base+O_FB) as *u8,raw,flen)
108 let clip: *i64 = (base+O_NXA_SKEL) as *i64
109 let before: i64 = clip[0]; clip[0] = before+1
110 gv_check_eq("different actual rig bytes refuse binding" as *u8,wc_resident_bind(base,flen,exact,2),WCR_E_CLIP,ctr)
111 clip[0] = before
112 let anim: *i64 = (base+O_NXA_ANIM) as *i64
113 let old_anim: i64 = anim[0]; anim[0] = old_anim+1
114 gv_check_eq("different actual animation bytes refuse binding" as *u8,wc_resident_bind(base,flen,exact,2),WCR_E_CLIP,ctr)
115 anim[0] = old_anim
116 gv_check("fixture has actual POSE payload for identity witness" as *u8,npw > 0,ctr)
117 if npw < 1 { return WCR_E_CLIP }
118 let pose: *i64 = (base+O_NXA_POSE) as *i64
119 let old_pose: i64 = pose[0]; pose[0] = old_pose+1
120 gv_check_eq("different actual pose bytes refuse binding" as *u8,wc_resident_bind(base,flen,exact,2),WCR_E_CLIP,ctr)
121 pose[0] = old_pose
122 gv_check_eq("identity refusals preserve previous optional resident" as *u8,wc_resident_current(base,1),1,ctr)
123 gv_check_eq("restored exact rig allows replacement token" as *u8,wc_resident_bind(base,flen,exact,2),0,ctr)
124 gv_check_eq("old token cannot address replacement" as *u8,wc_resident_cast(base,1,0),WCR_E_TOKEN,ctr)
125 gv_check_eq("replacement clears entity weights" as *u8,wc_resident_cast(base,2,0),0,ctr)
126 gv_check_eq("existing clip reload remains successful" as *u8,wc_nxa_ingest(base,nj,naw,npw),0,ctr)
127 gv_check_eq("every clip reload invalidates optional resident" as *u8,wc_resident_cast(base,2,0),WCR_E_TOKEN,ctr)
128 let final_header: *i64 = nxa_hp(base)
129 gv_check_eq("baseline clip remains loaded" as *u8,final_header[NXA_H_LOADED],1,ctr)
130 gv_check_eq("entire borrowed real asset remains unchanged" as *u8,nxa_check2(1,raw as *i64,flen/MVC_WORD),raw_hash,ctr)
131 return 0
132}
133func main(argc: i64,argv: *i64) -> i64 {
134 let ctr: *i64 = gv_ctr()
135 gv_head("resident MORF staging and exact clip identity" as *u8)
136 var rc: i64 = 0-1
137 if argc == 1 { rc = crg_run("/tmp/nxmorfgate/d1.nxa" as *u8,ctr) }
138 if argc == 2 { rc = crg_run(argv[1] as *u8,ctr) }
139 gv_check_eq("real resident loader lifecycle completes" as *u8,rc,0,ctr)
140 return gv_verdict("NX-CRAFT-RESIDENT-MORF" as *u8,ctr,"native allocation and real asset; browser growth, worker barrier and GPU delivery remain open" as *u8)
141}