code wiki / (root) / nx_citator_lib.nx

nx_citator_lib.nx source

↩ module page · 217 lines · 9826 B

1// nx_citator_lib.nx -- F995: CITATION TREATMENT SIGNALS (Shepard's / KeyCite analog), on the CID plane. 2// 3// Verifying that a quote is REAL (nx_cite) is necessary but not sufficient: a perfectly real quote from 4// a case that has since been OVERRULED is bad law, and relying on it is malpractice. This layer records 5// how later authorities TREAT an earlier one, and answers the question a citator exists to answer: is 6// this still good law? 7// 8// Treatment vocabulary (a DECLARED data contract; severity = how much it undercuts reliance): 9// followed severity 0 positive, reinforces 10// distinguished severity 1 caution, limited to its facts 11// questioned severity 2 caution, reliability doubted 12// overruled severity 3 NEGATIVE -- no longer good law 13// 14// ★WORST-WINS: a case's status is the MAX severity across every treatment of it. One overruling kills 15// reliance no matter how many later courts followed it -- you cannot average away dead law. This is the 16// bright line: good_law == (worst severity < overruled). 17// 18// ★COMPOSES nx_cite: cit_safe_to_cite passes ONLY if the quote verifies (real span of a real source) 19// AND the cited authority is still good law. So a real quote from overruled law is UNSAFE -- the exact 20// trap a citator prevents and a chatbot walks straight into. 21// 22// SCALE ENVELOPE (declared): cit_status scans the treatment index for the cited case's key-prefix 23// (mirrors the proven trust-ledger per-key scan) -- O(total-treatments); a per-case index is the rung. 24// DRY: composes nx_cite_lib (cite_ok) + nx_matter_lib (reg_*/mt_*/canon_encode). license_tier: ORIGINAL LIB. 25 26import "nx_cite_lib.nx" 27 28const CIT_COLON: i64 = 58 29 30// map a treatment string to its severity (unknown -> 0, a declared-vocabulary data error, not a crash). 31func cit_sev(t: *u8) -> i64 { 32 if mt_streq(t, "overruled" as *u8) == 1 { return 3 } 33 if mt_streq(t, "questioned" as *u8) == 1 { return 2 } 34 if mt_streq(t, "distinguished" as *u8) == 1 { return 1 } 35 if mt_streq(t, "followed" as *u8) == 1 { return 0 } 36 return 0 37} 38 39// record that `citing` treats `cited` with `treatment`. key = "<cited>:<citing>" so all treatments of a 40// cited case share its key-prefix and can be swept. 41func cit_treat_put(prefix: *u8, cited: *u8, citing: *u8, treatment: *u8) -> i64 { 42 let id: *u8 = sys_mmap(256) 43 var o: i64 = mt_catcopy(id, 0, cited) 44 id[o] = CIT_COLON as u8 45 o = o + 1 46 o = mt_catcopy(id, o, citing) 47 id[o] = 0 as u8 48 let k: *i64 = sys_mmap(8 * 2) as *i64 49 let v: *i64 = sys_mmap(8 * 2) as *i64 50 k[0] = ("treatment" as *u8) as i64 51 v[0] = treatment as i64 52 k[1] = ("citing" as *u8) as i64 53 v[1] = citing as i64 54 let rec: *u8 = sys_mmap(512) 55 let rl: i64 = canon_encode(k, v, 2, rec) 56 return reg_put(prefix, "cit:" as *u8, "cit:__idx__" as *u8, id, rec, rl) 57} 58 59// ★F996 PROVENANCED INGEST (/compare/legal LG2: cit_treat_ingest): rows `cited|citing|treatment` 60// (pipe = byte 124, one row per line) from a MIRRORED treatment file. REFUSES the whole file when 61// src_url or asof is EMPTY -- an unprovenanced treatment is a claim nobody can trace, and ingesting it 62// would poison worst-wins with unauditable severity. Each record carries src+asof fields; cit_status 63// reads only `treatment`, so worst-wins is untouched BY CONSTRUCTION. A malformed row, or one whose 64// treatment is OUTSIDE the declared vocabulary, is DROPPED AND COUNTED via dropped[0], never silently 65// ingested at severity 0 -- an unknown token defaulting to the permissive value is the pr_mode class. 66// Per-row allocations MIRROR cit_treat_put exactly: reg_put's retention semantics are its own contract, 67// and reusing a buffer it may retain would corrupt every prior record to save bytes a treatment file 68// does not need. Returns ingested-count, or 0-1 = provenance refusal, 0-2 = unreadable file. 69func cit_treat_ingest(prefix: *u8, src_path: *u8, src_url: *u8, asof: *u8, dropped: *i64) -> i64 { 70 dropped[0] = 0 71 if src_url[0] == (0 as u8) { return 0 - 1 } 72 if asof[0] == (0 as u8) { return 0 - 1 } 73 let lp: *i64 = sys_mmap(8) as *i64 74 let buf: *u8 = sys_read_file(src_path, lp) 75 let len: i64 = lp[0] 76 if len <= 0 { return 0 - 2 } 77 let cited: *u8 = sys_mmap(256) 78 let citing: *u8 = sys_mmap(256) 79 let treat: *u8 = sys_mmap(64) 80 var n: i64 = 0 81 var i: i64 = 0 82 var ls: i64 = 0 83 while i <= len { 84 var eol: i64 = 0 85 if i == len { eol = 1 } 86 if i < len { if buf[i] == 10 as u8 { eol = 1 } } 87 if eol == 1 { 88 if i > ls { 89 var f: i64 = 0 90 var w: i64 = 0 91 var ok: i64 = 1 92 var p: i64 = ls 93 while p < i { 94 if buf[p] == 124 as u8 { 95 if f == 0 { cited[w] = 0 as u8 } 96 if f == 1 { citing[w] = 0 as u8 } 97 f = f + 1 98 w = 0 99 } else { 100 if f == 0 { if w < 255 { cited[w] = buf[p]; w = w + 1 } } 101 if f == 1 { if w < 255 { citing[w] = buf[p]; w = w + 1 } } 102 if f == 2 { if w < 63 { treat[w] = buf[p]; w = w + 1 } } 103 if f > 2 { ok = 0 } 104 } 105 p = p + 1 106 } 107 treat[w] = 0 as u8 108 if f != 2 { ok = 0 } 109 if ok == 1 { if cited[0] == (0 as u8) { ok = 0 } } 110 if ok == 1 { if citing[0] == (0 as u8) { ok = 0 } } 111 if ok == 1 { if treat[0] == (0 as u8) { ok = 0 } } 112 if ok == 1 { 113 if cit_sev(treat) == 0 { 114 if mt_streq(treat, "followed" as *u8) == 0 { ok = 0 } 115 } 116 } 117 if ok == 1 { 118 let id: *u8 = sys_mmap(256) 119 var o: i64 = mt_catcopy(id, 0, cited) 120 id[o] = CIT_COLON as u8 121 o = o + 1 122 o = mt_catcopy(id, o, citing) 123 id[o] = 0 as u8 124 let k: *i64 = sys_mmap(8 * 4) as *i64 125 let v: *i64 = sys_mmap(8 * 4) as *i64 126 k[0] = ("treatment" as *u8) as i64 127 v[0] = treat as i64 128 k[1] = ("citing" as *u8) as i64 129 v[1] = citing as i64 130 k[2] = ("src" as *u8) as i64 131 v[2] = src_url as i64 132 k[3] = ("asof" as *u8) as i64 133 v[3] = asof as i64 134 let rec: *u8 = sys_mmap(1024) 135 let rl: i64 = canon_encode(k, v, 4, rec) 136 let rr: i64 = reg_put(prefix, "cit:" as *u8, "cit:__idx__" as *u8, id, rec, rl) 137 if rr >= 0 { n = n + 1 } else { dropped[0] = dropped[0] + 1 } 138 } else { 139 dropped[0] = dropped[0] + 1 140 } 141 } 142 ls = i + 1 143 } 144 i = i + 1 145 } 146 return n 147} 148 149// ★the WORST treatment severity recorded against `cited` (0 if none). worst-wins. 150func cit_status(prefix: *u8, cited: *u8) -> i64 { 151 // SIZE-TO-NEED: buffer derived from the index itself. The pair this replaces 152 // returned -1 once the citation index passed 1 MiB, and -1 skips the walk 153 // below -- a citator would have reported a case as GOOD LAW because the index 154 // outgrew its ceiling, which is the one answer it must never get wrong. 155 let idxbox: *i64 = sys_mmap(16) as *i64 156 let ilen: i64 = reg_index_read(prefix, "cit:__idx__" as *u8, idxbox) 157 let idx: *u8 = idxbox[0] as *u8 158 let cp: *u8 = sys_mmap(256) 159 var cpl: i64 = mt_catcopy(cp, 0, cited) 160 cp[cpl] = CIT_COLON as u8 161 cpl = cpl + 1 162 cp[cpl] = 0 as u8 163 let idb: *u8 = sys_mmap(256) 164 let po: *i64 = sys_mmap(16) as *i64 165 let lo: *i64 = sys_mmap(16) as *i64 166 let tf: *u8 = sys_mmap(64) 167 var worst: i64 = 0 168 var ls: i64 = 0 169 var i: i64 = 0 170 while i <= ilen { 171 var eol: i64 = 0 172 if i == ilen { eol = 1 } 173 if i < ilen { if idx[i] == 10 as u8 { eol = 1 } } 174 if eol == 1 { 175 if i > ls { 176 var c: i64 = 0 177 while ls + c < i { idb[c] = idx[ls + c]; c = c + 1 } 178 idb[c] = 0 as u8 179 var mtch: i64 = 1 180 var x: i64 = 0 181 while cp[x] != (0 as u8) { if idb[x] != cp[x] { mtch = 0 } x = x + 1 } 182 if mtch == 1 { 183 if reg_get(prefix, "cit:" as *u8, idb, po, lo) == 1 { 184 mt_field(po[0] as *u8, lo[0], "treatment" as *u8, 9, tf) 185 let s: i64 = cit_sev(tf) 186 if s > worst { worst = s } 187 } 188 } 189 } 190 ls = i + 1 191 } 192 i = i + 1 193 } 194 return worst 195} 196 197// 1 if `cited` is still good law (no overruling recorded); 0 if overruled. 198func cit_is_good_law(prefix: *u8, cited: *u8) -> i64 { 199 if cit_status(prefix, cited) >= 3 { return 0 } 200 return 1 201} 202 203// 1 if a citation carries CAUTION (distinguished/questioned) but is not dead -- a yellow flag. 204func cit_is_caution(prefix: *u8, cited: *u8) -> i64 { 205 let s: i64 = cit_status(prefix, cited) 206 if s >= 1 { if s < 3 { return 1 } } 207 return 0 208} 209 210// ★THE COMPOSED GATE: safe to cite ONLY if the quote verifies (nx_cite) AND the cited authority is 211// still good law. A real quote from overruled law returns 0. Same prefix holds both the cite: sources 212// and the cit: treatments (distinct keyprefixes, one plane). 213func cit_safe_to_cite(prefix: *u8, source_id: *u8, quoted: *u8) -> i64 { 214 if cite_ok(prefix, source_id, quoted) == 0 { return 0 } 215 if cit_is_good_law(prefix, source_id) == 0 { return 0 } 216 return 1 217}