code wiki / _hdl_build / nx_media_record.nx

nx_media_record.nx source

↩ module page · 75 lines · 3721 B

1// nx_media_record.nx -- MLIB-003 v1 (CALLOUT-012). The uniform MEDIA RECORD = a PROFILE of 2// the X-CAP-001 meta schema (nx_meta_schema), made CONTENT-ADDRESSED via the canonical CID 3// (nx_canon_cid). Composes proven organs (no rebuild, no parallel substrate): 4// record (modality/cid/origin/gen_modality/source_model/consent/license/attr.codec/dims/phash) 5// -> ms_valid (consent hard line) + ms_roundtrip (lossless, X-CAP-001) 6// -> canon_encode + cid_of -> content fingerprint "nxc1-<64hex>" = the store key lib:<fp>. 7// The fingerprint is DETERMINISTIC (same record -> same fp = dedup/provenance) and CONTENT- 8// SENSITIVE (one byte changes -> different fp = integrity/tamper). Persisting+versioning the 9// record on the seg store (nx_seg_store, time-travel PROVEN by nx_infomgmt_gate) = the wiring 10// slice MLIB-003b. Self-validating gate (MEDIARECGATE) for nx_reconcile. license_tier: ORIGINAL 11import "nx_syscalls.nx" 12import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc) 13import "nx_meta_schema.nx" 14import "nx_canon_cid.nx" 15const K_MAGIC_16384: i64 = 16384 16 17func mr_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 18// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer 19// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the 20// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls). 21// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign. 22func mr_putn(v: i64) -> i64 { nxi_out(v); return 0 } 23 24// content fingerprint of a record (keys/vals as pointers-i64): canonical-encode then CID. 25func mr_fingerprint(keys: *i64, vals: *i64, n: i64, fpout: *u8) -> i64 { 26 let cbuf: *u8 = sys_mmap(K_MAGIC_16384) 27 let clen: i64 = canon_encode(keys, vals, n, cbuf) 28 return cid_of(cbuf, clen, fpout) 29} 30 31func main() -> i64 { 32 let keys: *i64 = sys_mmap(8 * 64) as *i64 33 let vals: *i64 = sys_mmap(8 * 64) as *i64 34 35 ms_set(keys, vals, 0, "modality" as *u8, "image" as *u8) 36 ms_set(keys, vals, 1, "cid" as *u8, "img-7f3a" as *u8) 37 ms_set(keys, vals, 2, "origin" as *u8, "handmade" as *u8) 38 ms_set(keys, vals, 3, "gen_modality" as *u8, "none" as *u8) 39 ms_set(keys, vals, 4, "source_model" as *u8, "none" as *u8) 40 ms_set(keys, vals, 5, "consent" as *u8, "self" as *u8) 41 ms_set(keys, vals, 6, "license" as *u8, "owned" as *u8) 42 ms_set(keys, vals, 7, "attr.codec" as *u8, "png" as *u8) 43 ms_set(keys, vals, 8, "attr.dims" as *u8, "1920x1080" as *u8) 44 ms_set(keys, vals, 9, "attr.phash" as *u8, "ab12cd34" as *u8) 45 46 let valid: i64 = ms_valid(keys, vals, 10) 47 let rt: i64 = ms_roundtrip(keys, vals, 10) 48 49 let fp1: *u8 = sys_mmap(80) 50 mr_fingerprint(keys, vals, 10, fp1) 51 let fp1b: *u8 = sys_mmap(80) 52 mr_fingerprint(keys, vals, 10, fp1b) 53 let det: i64 = ms_streq(fp1, fp1b) 54 55 // content-sensitivity: flip one attribute -> fingerprint MUST differ (dedup/integrity) 56 ms_set(keys, vals, 9, "attr.phash" as *u8, "ZZ999999" as *u8) 57 let fp2: *u8 = sys_mmap(80) 58 mr_fingerprint(keys, vals, 10, fp2) 59 let sens: i64 = 1 - ms_streq(fp1, fp2) 60 61 mr_puts("MEDIARECGATE valid=" as *u8); mr_putn(valid) 62 mr_puts(" lossless_roundtrip=" as *u8); mr_putn(rt) 63 mr_puts(" cid_deterministic=" as *u8); mr_putn(det) 64 mr_puts(" cid_content_sensitive=" as *u8); mr_putn(sens) 65 mr_puts(" fp=" as *u8); mr_puts(fp1) 66 67 var ok: i64 = 1 68 if valid != 1 { ok = 0 } 69 if rt != 1 { ok = 0 } 70 if det != 1 { ok = 0 } 71 if sens != 1 { ok = 0 } 72 if ok == 1 { mr_puts(" verdict=GREEN\n" as *u8); return 0 } 73 mr_puts(" verdict=RED\n" as *u8) 74 return 1 75}