code wiki / _hdl_build / nx_meal_source_gate.nx

nx_meal_source_gate.nx source

↩ module page · 110 lines · 5968 B

1// nx_meal_source_gate.nx -- proves the meal planner's LINK-ROT defence is real rather than promised, on the 2// nx_gate_verdict base class (L009/D001: gates inherit, never hand-roll). Fixture paths are per-run 3// epoch-suffixed /tmp so the gate can never measure its own history (the office-intake T1 state-poisoning 4// lesson). Two cells are BITE-PROVEN (gv_bite): a detector that has never fired on a crafted defect is 5// vacuous, and vacuous green is how "we archive everything" ships while archiving nothing. 6// license_tier: ORIGINAL No hw writes (Rule 26). 7import "nx_syscalls.nx" 8import "nx_meal_source.nx" 9import "nx_gate_verdict.nx" 10 11const MG_WARC: i64 = 65536 12const MG_BODY: i64 = 2048 13const MG_FLD: i64 = 1024 14const MG_PFX: i64 = 160 15const MG_DAY: i64 = 20671 16 17func main(argc: i64, argv: *i64) -> i64 { 18 let ctr: *i64 = gv_ctr() 19 gv_head("nx_meal_source -- provenance + WARC preservation: a citation that outlives the origin host" as *u8) 20 21 let ep: i64 = sys_now_realtime_sec() 22 let prefix: *u8 = sys_mmap(MG_PFX) 23 var po: i64 = as_append(prefix, 0, "/tmp/msgate_" as *u8) 24 po = fd_apnum(prefix, po, ep) 25 po = as_append(prefix, po, "-" as *u8) 26 prefix[po] = 0 as u8 27 28 let url: *u8 = "https://example.org/recipes/short-ribs" as *u8 29 let url2: *u8 = "https://example.org/recipes/short-ribs-2" as *u8 30 31 // A payload carrying BOTH embedded CRLF and a COUNTERFEIT WARC header. Content-Length delimiting must 32 // survive body text that looks exactly like record framing -- this is where a line-scanning archive 33 // silently truncates someone's recipe and nobody notices until the origin is gone. 34 let body: *u8 = sys_mmap(MG_BODY) 35 var bl: i64 = 0 36 bl = as_append(body, bl, "<h1>Gochujang Braised Short Ribs</h1>\r\n" as *u8) 37 bl = as_append(body, bl, "\r\nWARC/1.0\r\nWARC-Type: resource\r\nContent-Length: 99\r\n\r\n" as *u8) 38 bl = as_append(body, bl, "<p>Serves 6. Braise 3 hours at 150C.</p>" as *u8) 39 body[bl] = 0 as u8 40 41 let warc: *u8 = sys_mmap(MG_WARC) 42 var wl: i64 = 0 43 wl = ms_capture(warc, wl, url, "2026-08-06T00:00:00Z" as *u8, "text/html" as *u8, body, bl) 44 45 // ---- T1 provenance round-trip ---- 46 ms_record(prefix, "shortribs" as *u8, "Gochujang Braised Short Ribs" as *u8, "Example Kitchen" as *u8, url, "CC-BY-4.0" as *u8, MG_DAY, bl) 47 let f: *u8 = sys_mmap(MG_FLD) 48 var t1: i64 = 1 49 if ms_field(prefix, "shortribs" as *u8, MS_F_TITLE, f) == 0 { t1 = 0 } 50 if fd_streq(f, "Gochujang Braised Short Ribs" as *u8) == 0 { t1 = 0 } 51 ms_field(prefix, "shortribs" as *u8, MS_F_PUBLISHER, f) 52 if fd_streq(f, "Example Kitchen" as *u8) == 0 { t1 = 0 } 53 ms_field(prefix, "shortribs" as *u8, MS_F_URL, f) 54 if fd_streq(f, url) == 0 { t1 = 0 } 55 ms_field(prefix, "shortribs" as *u8, MS_F_LICENSE, f) 56 if fd_streq(f, "CC-BY-4.0" as *u8) == 0 { t1 = 0 } 57 if ms_field_num(prefix, "shortribs" as *u8, MS_F_BYTES) != bl { t1 = 0 } 58 if ms_field_num(prefix, "shortribs" as *u8, MS_F_DAY) != MG_DAY { t1 = 0 } 59 gv_check("T1 provenance round-trips (title, publisher, url, license, day, bytes)" as *u8, t1, ctr) 60 61 // ---- T2 the preserved copy is byte-exact ---- 62 gv_check("T2 preserved bytes return BYTE-IDENTICAL through embedded CRLF + a counterfeit WARC header" as *u8, ms_capture_verify(warc, wl, url, body, bl), ctr) 63 64 // ---- T3 exact-URI: a near-miss must NOT be satisfied by our copy ---- 65 var t3: i64 = 1 66 if ms_is_archived(warc, wl, url) != 1 { t3 = 0 } 67 if ms_is_archived(warc, wl, url2) != 0 { t3 = 0 } 68 if ms_archive_bytes(warc, wl, url) != bl { t3 = 0 } 69 if ms_archive_bytes(warc, wl, url2) != (0 - 1) { t3 = 0 } 70 gv_check("T3 lookup is EXACT-URI -- a longer near-miss url is NOT answered by our archived copy" as *u8, t3, ctr) 71 72 // ---- T4 BITE the rot detector ---- 73 ms_record(prefix, "norot" as *u8, "Unpreserved Dish" as *u8, "Other Kitchen" as *u8, "https://example.org/recipes/gone" as *u8, "CC-BY-4.0" as *u8, 0, 0) 74 var rot_bad: i64 = 0 75 if ms_rot_state(prefix, "norot" as *u8, warc, wl) == MS_ROT_RISK { rot_bad = 1 } 76 var rot_good: i64 = 0 77 if ms_rot_state(prefix, "shortribs" as *u8, warc, wl) == MS_ROT_RISK { rot_good = 1 } 78 gv_bite("T4 rot detector flags the cited-but-unpreserved source, stays silent on the preserved one" as *u8, rot_bad, rot_good, ctr) 79 80 // ---- T5 an unsourced recipe renders NO credit strip ---- 81 var t5: i64 = 0 82 if ms_rot_state(prefix, "nosuchrecipe" as *u8, warc, wl) == MS_ROT_NONE { t5 = 1 } 83 if ms_is_citable(prefix, "nosuchrecipe" as *u8) != 0 { t5 = 0 } 84 if ms_is_citable(prefix, "shortribs" as *u8) != 1 { t5 = 0 } 85 gv_check("T5 an unsourced recipe reports ROT_NONE and is NOT citable (no empty brought-to-you-by)" as *u8, t5, ctr) 86 87 // ---- T6 BITE the byte-verifier with a one-byte tamper ---- 88 let aoff: i64 = ms_archive_off(warc, wl, url) 89 let orig: u8 = warc[aoff + 5] 90 var repl: i64 = 88 91 if (orig as i64) == 88 { repl = 89 } 92 warc[aoff + 5] = repl as u8 93 var tam_bad: i64 = 0 94 if ms_capture_verify(warc, wl, url, body, bl) == 0 { tam_bad = 1 } 95 warc[aoff + 5] = orig 96 var tam_good: i64 = 0 97 if ms_capture_verify(warc, wl, url, body, bl) == 0 { tam_good = 1 } 98 gv_bite("T6 byte-verifier rejects a ONE-BYTE tampered archive and accepts the restored one" as *u8, tam_bad, tam_good, ctr) 99 100 // ---- T7 idempotency ---- 101 var t7: i64 = 1 102 if ms_record(prefix, "shortribs" as *u8, "Gochujang Braised Short Ribs" as *u8, "Example Kitchen" as *u8, url, "CC-BY-4.0" as *u8, MG_DAY, bl) != 0 { t7 = 0 } 103 let toks: *i64 = sys_mmap(8 * 64) as *i64 104 if ms_list(prefix, toks) != 2 { t7 = 0 } 105 gv_check("T7 re-recording identical provenance writes NOTHING and never duplicates the id list" as *u8, t7, ctr) 106 107 let rc: i64 = gv_verdict("MEAL-SOURCE" as *u8, ctr, "provenance recorded; preservation MEASURED not promised; exact-URI; tamper-evident" as *u8) 108 sys_exit(rc) 109 return 0 110}