code wiki / (root) / nx_doc_catalog_gate.nx

nx_doc_catalog_gate.nx source

↩ module page · 83 lines · 4734 B

1// nx_doc_catalog_gate.nx -- R2 GATE for the doc-intelligence LIBRARIAN: proves documents are cataloged as 2// content-addressed structured records, retrievable + decodable, deduped by content (same doc -> same CID), 3// and additive (distinct docs coexist; re-catalog is idempotent). Hermetic /tmp prefix. Exits 0 iff ALL 4// pass. license_tier: ORIGINAL 5import "nx_syscalls.nx" 6import "nx_asset_record.nx" 7import "nx_doc_catalog.nx" 8 9func g_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 10func g_putn(v: i64) -> i64 { 11 if v == 0 { sys_write(1, "0" as *u8, 1); return 0 } 12 var m: i64 = v; if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m } 13 let d: *u8 = sys_mmap(24); var k: i64 = 0 14 while m > 0 { d[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 15 var j: i64 = k - 1 16 while j >= 0 { sys_write(1, ((d as i64)+j) as *u8, 1); j = j - 1 } 17 return 0 18} 19func chk(name: *u8, got: i64, want: i64, st: *i64) -> i64 { 20 if got == want { st[0] = st[0] + 1; g_puts(" PASS "); g_puts(name); g_puts("\n") } 21 else { st[1] = st[1] + 1; g_puts(" FAIL "); g_puts(name); g_puts(" got="); g_putn(got); g_puts(" want="); g_putn(want); g_puts("\n") } 22 return 0 23} 24 25func main() -> i64 { 26 let st: *i64 = sys_mmap(16) as *i64 27 st[0] = 0; st[1] = 0 28 29 // hermetic prefix "/tmp/doccat_<us>-" 30 let pfx: *u8 = sys_mmap(64) 31 var pp: i64 = 0 32 let pre: *u8 = "/tmp/doccat_\x00" as *u8 33 while pre[pp] != (0 as u8) { pfx[pp] = pre[pp]; pp = pp + 1 } 34 let us: i64 = sys_now_us() 35 let tmp: *u8 = sys_mmap(32); var m: i64 = us; var kk: i64 = 0 36 if m == 0 { tmp[0] = 0x30 as u8; kk = 1 } 37 while m > 0 { tmp[kk] = (0x30 + (m % 10)) as u8; m = m / 10; kk = kk + 1 } 38 var z: i64 = kk - 1 39 while z >= 0 { pfx[pp] = tmp[z]; pp = pp + 1; z = z - 1 } 40 pfx[pp] = 0x2d as u8; pp = pp + 1; pfx[pp] = 0 as u8 41 42 let cid1: *u8 = sys_mmap(96) 43 let cid1b: *u8 = sys_mmap(96) 44 let cid2: *u8 = sys_mmap(96) 45 46 chk("catalog doc1 (Memorial bill)", dc_catalog_doc(pfx, "Memorial Hospital Itemized Statement\x00" as *u8, "Memorial Hospital\x00" as *u8, "2025-03-10\x00" as *u8, "ACCT-12345\x00" as *u8, "5 line-items; 4 audit flags; $2356 fair-price overcharge\x00" as *u8, cid1), 0, st) 47 48 // retrieve + decode doc1 49 let pq: *i64 = sys_mmap(16) as *i64 50 let lq: *i64 = sys_mmap(16) as *i64 51 chk("doc1 retrievable by CID", dc_get(pfx, cid1, pq, lq), 1, st) 52 let keys: *i64 = sys_mmap(8 * 64) as *i64 53 let vals: *i64 = sys_mmap(8 * 64) as *i64 54 let nf: i64 = ar_decode(pq[0] as *u8, lq[0], keys, vals, 64) 55 var dok: i64 = 0 56 if nf > 0 { dok = 1 } 57 chk("doc1 decodes", dok, 1, st) 58 chk("doc1 type = doc", ar_streq(ar_get(keys, vals, nf, "type\x00" as *u8), "doc\x00" as *u8), 1, st) 59 chk("doc1 title intact", ar_streq(ar_get(keys, vals, nf, "title\x00" as *u8), "Memorial Hospital Itemized Statement\x00" as *u8), 1, st) 60 chk("doc1 is_current = 1", ar_streq(ar_get(keys, vals, nf, "is_current\x00" as *u8), "1\x00" as *u8), 1, st) 61 chk("doc1 carries structured extract summary", ar_streq(ar_get(keys, vals, nf, "extract_summary\x00" as *u8), "5 line-items; 4 audit flags; $2356 fair-price overcharge\x00" as *u8), 1, st) 62 chk("doc1 classification = private", ar_streq(ar_get(keys, vals, nf, "classification\x00" as *u8), "private\x00" as *u8), 1, st) 63 64 // a DIFFERENT document -> different CID 65 chk("catalog doc2 (different statement)", dc_catalog_doc(pfx, "City Clinic Statement\x00" as *u8, "City Clinic\x00" as *u8, "2025-04-02\x00" as *u8, "ACCT-99\x00" as *u8, "2 line-items; 0 flags\x00" as *u8, cid2), 0, st) 66 chk("doc2 CID differs from doc1 (content-addressed)", ar_streq(cid1, cid2), 0, st) 67 68 // re-catalog doc1 (same content) -> same CID (idempotent dedup) 69 chk("re-catalog doc1 (dedup)", dc_catalog_doc(pfx, "Memorial Hospital Itemized Statement\x00" as *u8, "Memorial Hospital\x00" as *u8, "2025-03-10\x00" as *u8, "ACCT-12345\x00" as *u8, "5 line-items; 4 audit flags; $2356 fair-price overcharge\x00" as *u8, cid1b), 0, st) 70 chk("dedup: same content -> same CID", ar_streq(cid1, cid1b), 1, st) 71 72 // additive: both documents still coexist + retrievable 73 chk("doc1 STILL retrievable (additive)", dc_get(pfx, cid1, pq, lq), 1, st) 74 chk("doc2 retrievable (additive)", dc_get(pfx, cid2, pq, lq), 1, st) 75 76 // negative control 77 chk("bogus CID not found (-1)", dc_get(pfx, "nxc1-deadbeef\x00" as *u8, pq, lq), 0 - 1, st) 78 79 g_puts("nx_doc_catalog_gate: PASS="); g_putn(st[0]); g_puts(" FAIL="); g_putn(st[1]); g_puts("\n") 80 if st[1] == 0 { g_puts("DOC-INTEL R2 nx_doc_catalog: GREEN\n"); return 0 } 81 g_puts("DOC-INTEL R2 nx_doc_catalog: RED\n") 82 return 1 83}