code wiki / (root) / nx_medbill_codes.nx

nx_medbill_codes.nx source

↩ module page · 197 lines · 7246 B

1// nx_medbill_codes.nx -- M1 of the MEDICAL BILLING & PATIENT ADVOCACY ladder (see /compare/medbilling): 2// the sovereign CODE-PACK STORE + DECODE library. Billing codes (ICD-10-CM, HCPCS, CPT) map to their 3// OFFICIAL descriptors, stored as DATA in the sovereign seg-store (#6/#11/#25) -- the engine has ZERO 4// hardcoded descriptors. Descriptors are INGESTED from banked NLM ClinicalTables API responses 5// (clinicaltables.nlm.nih.gov, US-gov, fetched sovereignly by the researcher into knowledge/fetched/ 6// mbc_*.raw) so nothing is fabricated: a code the store does not know returns -1, NEVER a made-up 7// description (the liar-kill invariant). CPT descriptors are AMA-licensed and cannot be bulk-fetched; 8// curated entries carry an explicit "curated:" cite so provenance is always visible. 9// Record layout under key "mbcode:<code>": system|descriptor|cite (pipe-delimited, NUL-terminated). 10// Commit segid = sys_now_us() (see reference-segstore-segid-use-now-us). No floats, no hardware writes. 11// license_tier: ORIGINAL 12import "nx_syscalls.nx" 13import "nx_seg_store.nx" 14const K_MAGIC_2048: i64 = 2048 15const K_MAGIC_65536: i64 = 65536 16const K_MAGIC_1024: i64 = 1024 17 18// -------- small string helpers (mbc_-prefixed to avoid cross-organ symbol collisions) -------- 19func mbc_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 20 21// append NUL-terminated src to out at off; returns new off (no NUL written) 22func mbc_app(out: *u8, off: i64, src: *u8) -> i64 { 23 var o: i64 = off 24 var i: i64 = 0 25 while src[i] != (0 as u8) { out[o] = src[i]; o = o + 1; i = i + 1 } 26 return o 27} 28 29// first index of NUL-terminated needle in hay[from..n), or -1 30func mbc_find(hay: *u8, n: i64, needle: *u8, from: i64) -> i64 { 31 var m: i64 = mbc_slen(needle) 32 if m == 0 { return from } 33 var i: i64 = from 34 while i + m <= n { 35 var j: i64 = 0 36 var ok: i64 = 1 37 while j < m { if hay[i + j] != needle[j] { ok = 0; j = m } else { j = j + 1 } } 38 if ok == 1 { return i } 39 i = i + 1 40 } 41 return 0 - 1 42} 43 44// does NUL-terminated hay contain NUL-terminated needle? 1/0 45func mbc_contains(hay: *u8, needle: *u8) -> i64 { 46 let n: i64 = mbc_slen(hay) 47 if mbc_find(hay, n, needle, 0) >= 0 { return 1 } 48 return 0 49} 50 51func mbc_streq(a: *u8, b: *u8) -> i64 { 52 var i: i64 = 0 53 while a[i] != (0 as u8) { if a[i] != b[i] { return 0 } i = i + 1 } 54 if b[i] == (0 as u8) { return 1 } 55 return 0 56} 57 58// read whole file into buf[0..cap); returns bytes or -1 59func mbc_read_all(path: *u8, buf: *u8, cap: i64) -> i64 { 60 let fd: i64 = sys_openat_rd(path) 61 if fd < 0 { return 0 - 1 } 62 var total: i64 = 0 63 var go: i64 = 1 64 while go == 1 { 65 let dst: *u8 = ((buf as i64) + total) as *u8 66 let n: i64 = sys_read(fd, dst, cap - total) 67 if n <= 0 { go = 0 } else { total = total + n; if total >= cap { go = 0 } } 68 } 69 sys_close(fd) 70 return total 71} 72 73// -------- store -------- 74// build key "mbcode:<code>" into out (NUL-terminated) 75func mbc_key(code: *u8, out: *u8) -> i64 { 76 var o: i64 = 0 77 let p: *u8 = "mbcode:\x00" as *u8 78 o = mbc_app(out, o, p) 79 o = mbc_app(out, o, code) 80 out[o] = 0 as u8 81 return o 82} 83 84// STORE one code record: value = system|descriptor|cite NUL-terminated. 0 ok, negative on error. 85func mbc_put(prefix: *u8, code: *u8, system: *u8, desc: *u8, cite: *u8) -> i64 { 86 let key: *u8 = sys_mmap(256) 87 mbc_key(code, key) 88 let val: *u8 = sys_mmap(K_MAGIC_2048) 89 var o: i64 = 0 90 o = mbc_app(val, o, system) 91 val[o] = 0x7c as u8 92 o = o + 1 93 o = mbc_app(val, o, desc) 94 val[o] = 0x7c as u8 95 o = o + 1 96 o = mbc_app(val, o, cite) 97 val[o] = 0 as u8 98 let w: *i64 = ss_begin() 99 if ss_add(w, 1, key, val, o + 1) != 0 { return 0 - 10 } 100 if ss_commit(prefix, w, sys_now_us()) != 0 { return 0 - 11 } 101 return 0 102} 103 104// LOAD the record for code into out[0..cap) (NUL-terminated). Returns byte length, or -1 if UNKNOWN. 105// An unknown code is -1 by construction -- the decoder never fabricates a descriptor. 106func mbc_get(prefix: *u8, code: *u8, out: *u8, cap: i64) -> i64 { 107 let key: *u8 = sys_mmap(256) 108 mbc_key(code, key) 109 let h: *i64 = ss_open(prefix) 110 if (h as i64) == 0 { return 0 - 1 } 111 let pp: *i64 = sys_mmap(16) as *i64 112 let ll: *i64 = sys_mmap(16) as *i64 113 if ss_hget(h, key, pp, ll) != 1 { return 0 - 1 } 114 let b: *u8 = pp[0] as *u8 115 var n: i64 = ll[0] 116 if n > cap - 1 { n = cap - 1 } 117 var i: i64 = 0 118 while i < n { out[i] = b[i]; i = i + 1 } 119 out[n] = 0 as u8 120 var l: i64 = 0 121 while out[l] != (0 as u8) { l = l + 1 } 122 return l 123} 124 125// extract pipe-field idx (0=system,1=descriptor,2=cite) from a record into out; returns field length. 126func mbc_field(rec: *u8, idx: i64, out: *u8, cap: i64) -> i64 { 127 var i: i64 = 0 128 var f: i64 = 0 129 while f < idx { 130 while rec[i] != (0x7c as u8) { if rec[i] == (0 as u8) { out[0] = 0 as u8; return 0 } i = i + 1 } 131 i = i + 1 132 f = f + 1 133 } 134 var o: i64 = 0 135 while rec[i] != (0x7c as u8) { 136 if rec[i] == (0 as u8) { out[o] = 0 as u8; return o } 137 if o < cap - 1 { out[o] = rec[i]; o = o + 1 } 138 i = i + 1 139 } 140 out[o] = 0 as u8 141 return o 142} 143 144// -------- ingest from a banked NLM ClinicalTables response -------- 145// The display pair in the JSON body looks like ["<CODE>","<Official descriptor>"]. 146// The codes-only array can also match the needle when several codes are returned, so a candidate is 147// accepted ONLY if it contains a space (every official descriptor does; a code never does). 148// Returns 1 = ingested, 0 = code not present in the response, negative = read/store error. 149func mbc_ingest_nlm(prefix: *u8, rawpath: *u8, code: *u8, system: *u8) -> i64 { 150 let cap: i64 = K_MAGIC_65536 151 let raw: *u8 = sys_mmap(cap) 152 let n: i64 = mbc_read_all(rawpath, raw, cap) 153 if n <= 0 { return 0 - 1 } 154 // needle = ["<code>"," built byte-wise (double-quote cannot live in a nx string literal) 155 let needle: *u8 = sys_mmap(128) 156 var o: i64 = 0 157 needle[o] = 0x5b as u8 158 o = o + 1 159 needle[o] = 0x22 as u8 160 o = o + 1 161 o = mbc_app(needle, o, code) 162 needle[o] = 0x22 as u8 163 o = o + 1 164 needle[o] = 0x2c as u8 165 o = o + 1 166 needle[o] = 0x22 as u8 167 o = o + 1 168 needle[o] = 0 as u8 169 let nl: i64 = o 170 let desc: *u8 = sys_mmap(K_MAGIC_1024) 171 var from: i64 = 0 172 var going: i64 = 1 173 var got: i64 = 0 174 while going == 1 { 175 let pos: i64 = mbc_find(raw, n, needle, from) 176 if pos < 0 { going = 0 } else { 177 var s: i64 = pos + nl 178 var d: i64 = 0 179 var hasspace: i64 = 0 180 while s < n { 181 if raw[s] == (0x22 as u8) { s = n } else { 182 if raw[s] == (0x20 as u8) { hasspace = 1 } 183 if d < 1000 { desc[d] = raw[s]; d = d + 1 } 184 s = s + 1 185 } 186 } 187 desc[d] = 0 as u8 188 if hasspace == 1 { 189 if d > 0 { got = 1; going = 0 } 190 } 191 if going == 1 { from = pos + 1 } 192 } 193 } 194 if got == 0 { return 0 } 195 if mbc_put(prefix, code, system, desc, rawpath) != 0 { return 0 - 2 } 196 return 1 197}