code wiki / _hdl_build / nx_vizsla_health.nx

nx_vizsla_health.nx source

↩ module page · 332 lines · 13638 B

1// nx_vizsla_health.nx -- NISHI VIZSLA: RELATIONSHIP INTELLIGENCE -- the S-class DEPTH a real PRM/CRM has and a 2// cadence-only tool lacks. Operator 2026-06-23: "i want our prm crm professional relationship management engine 3// to be s class exceeds and im sure we have none of the depth of real system yet." This is the depth core that 4// Clay / Affinity / Monica / Salesforce charge for: a computed HEALTH SCORE per relationship that synthesizes 5// the WHOLE interaction history into actionable intelligence -- which relationships are thriving vs quietly 6// dying -- instead of just "due/fresh". Reads the V4 relate store (touches + obligations) on the sovereign 7// CID/seg_store plane; computed LOCALLY (no cloud scoring, no data sale). Integer-only NO-FLOAT. 8// score <prefix> <contacts> <today> per contact: recency + frequency + reciprocity -> 0-100 health + 9// THRIVING/STEADY/COOLING/AT-RISK/NEW; verdict = counts + the at-risk list. 10// MODEL AS DATA (refine = edit, law 11): recency bands (1x/2x/4x cadence -> 100/70/40/10), frequency 11// (touches*25 cap 100), reciprocity (100 - open_owe*25), weights recency50/freq30/balance20, status thresholds. 12// license_tier: ORIGINAL 13import "nx_syscalls.nx" 14import "nx_seg_store.nx" 15const K_MAGIC_1970: i64 = 1970 16const K_MAGIC_146097: i64 = 146097 17const K_MAGIC_719468: i64 = 719468 18const K_MAGIC_131072: i64 = 131072 19 20func vh_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 21func vh_p(s: *u8) -> i64 { sys_write(1, s, vh_slen(s)); return 0 } 22 23func vh_eq(a: *u8, b: *u8) -> i64 { 24 var i: i64 = 0 25 var go: i64 = 1 26 while go == 1 { if a[i] != b[i] { return 0 } if a[i] == (0 as u8) { return 1 } i = i + 1 } 27 return 0 28} 29 30func vh_dup(s: *u8) -> *u8 { 31 let n: i64 = vh_slen(s) 32 let d: *u8 = sys_mmap(n + 2) 33 var i: i64 = 0 34 while i <= n { d[i] = s[i]; i = i + 1 } 35 return d 36} 37 38func vh_atoi(s: *u8) -> i64 { 39 var v: i64 = 0 40 var i: i64 = 0 41 while s[i] != (0 as u8) { let d: i64 = (s[i] as i64) - 48; if d >= 0 { if d <= 9 { v = v * 10 + d } } i = i + 1 } 42 return v 43} 44 45func vh_isws(c: i64) -> i64 { if c == 32 { return 1 } if c == 13 { return 1 } if c == 9 { return 1 } return 0 } 46 47func vh_tok(b: *u8, off: i64, lend: i64, dst: *u8, cap: i64) -> i64 { 48 var p: i64 = off 49 var go: i64 = 1 50 while go == 1 { if p >= lend { go = 0 } else { if vh_isws(b[p] as i64) == 1 { p = p + 1 } else { go = 0 } } } 51 var t: i64 = 0 52 go = 1 53 while go == 1 { 54 if p >= lend { go = 0 } else { 55 if vh_isws(b[p] as i64) == 1 { go = 0 } else { if t < cap - 1 { dst[t] = b[p]; t = t + 1 } p = p + 1 } 56 } 57 } 58 dst[t] = 0 as u8 59 return p 60} 61 62func vh_cat(dst: *u8, off: i64, s: *u8) -> i64 { 63 var i: i64 = 0 64 while s[i] != (0 as u8) { dst[off + i] = s[i]; i = i + 1 } 65 return off + i 66} 67 68func vh_catn(dst: *u8, off: i64, v: i64) -> i64 { 69 var o: i64 = off 70 var m: i64 = v 71 if m < 0 { dst[o] = 45 as u8; o = o + 1; m = 0 - m } 72 let t: *u8 = sys_mmap(28) 73 var k: i64 = 0 74 if m == 0 { t[0] = 48 as u8; k = 1 } 75 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 76 var i: i64 = 0 77 while i < k { dst[o + i] = t[k - 1 - i]; i = i + 1 } 78 return o + k 79} 80 81func vh_find(list: *i64, n: i64, s: *u8) -> i64 { 82 var i: i64 = 0 83 while i < n { if vh_eq(list[i] as *u8, s) == 1 { return i } i = i + 1 } 84 return 0 - 1 85} 86 87func vh_kmemeq(b: *u8, off: i64, n: i64, s: *u8) -> i64 { 88 if vh_slen(s) != n { return 0 } 89 var i: i64 = 0 90 while i < n { if b[off + i] != s[i] { return 0 } i = i + 1 } 91 return 1 92} 93 94func vh_isrel(b: *u8, koff: i64, kl: i64) -> i64 { 95 if kl <= 4 { return 0 } 96 return vh_kmemeq(b, koff, 4, "rel:" as *u8) 97} 98 99func vh_field(rec: *u8, rl: i64, want: *u8, out: *u8, cap: i64) -> i64 { 100 if rl < 8 { return 0 } 101 let nf: i64 = ss_r32(rec, 4) 102 var off: i64 = 8 103 var fi: i64 = 0 104 while fi < nf { 105 if off + 8 > rl { return 0 } 106 let kl: i64 = ss_r32(rec, off) 107 let koff: i64 = off + 4 108 let vl: i64 = ss_r32(rec, koff + kl) 109 let voff: i64 = koff + kl + 4 110 if vh_kmemeq(rec, koff, kl, want) == 1 { 111 var t: i64 = 0 112 while t < vl { if t < cap - 1 { out[t] = rec[voff + t] } t = t + 1 } 113 if t > cap - 1 { t = cap - 1 } 114 out[t] = 0 as u8 115 return 1 116 } 117 off = voff + vl 118 fi = fi + 1 119 } 120 return 0 121} 122 123func vh_datedays(s: *u8) -> i64 { 124 let nums: *i64 = sys_mmap(8 * 4) as *i64 125 var nn: i64 = 0 126 var cur: i64 = 0 127 var indig: i64 = 0 128 var i: i64 = 0 129 var go: i64 = 1 130 while go == 1 { 131 let c: i64 = s[i] as i64 132 var isd: i64 = 0 133 if c >= 48 { if c <= 57 { isd = 1 } } 134 if isd == 1 { cur = cur * 10 + (c - 48); indig = 1 } 135 if isd == 0 { if indig == 1 { if nn < 4 { nums[nn] = cur; nn = nn + 1 } cur = 0; indig = 0 } } 136 if c == 0 { go = 0 } 137 i = i + 1 138 } 139 if nn != 3 { return 0 - 1 } 140 var y: i64 = nums[0] 141 let m: i64 = nums[1] 142 let d: i64 = nums[2] 143 if y < K_MAGIC_1970 { return 0 - 1 } 144 if m < 1 { return 0 - 1 } 145 if m > 12 { return 0 - 1 } 146 if d < 1 { return 0 - 1 } 147 if d > 31 { return 0 - 1 } 148 if m <= 2 { y = y - 1 } 149 let era: i64 = y / 400 150 let yoe: i64 = y - era * 400 151 var mp: i64 = m - 3 152 if m <= 2 { mp = m + 9 } 153 let doy: i64 = (153 * mp + 2) / 5 + d - 1 154 let doe: i64 = yoe * 365 + yoe / 4 - yoe / 100 + doy 155 return era * K_MAGIC_146097 + doe - K_MAGIC_719468 156} 157 158// --- the relationship-intelligence model (data-driven bands) --- 159func vh_recency(days_since: i64, cad: i64) -> i64 { 160 if days_since < 0 { return 0 } 161 if days_since <= cad { return 100 } 162 if days_since <= cad * 2 { return 70 } 163 if days_since <= cad * 4 { return 40 } 164 return 10 165} 166func vh_freq(count: i64) -> i64 { var f: i64 = count * 25; if f > 100 { f = 100 } return f } 167func vh_bal(owe: i64) -> i64 { var b: i64 = 100 - owe * 25; if b < 0 { b = 0 } return b } 168 169func main(argc: i64, argv: *i64) -> i64 { 170 if argc < 5 { vh_p("VIZSLA-HEALTH usage: score <prefix> <contacts> <today> -- fail loud\n" as *u8); return 1 } 171 let cmd: *u8 = argv[1] as *u8 172 if vh_eq(cmd, "score" as *u8) == 0 { vh_p("VIZSLA-HEALTH unknown command (score) -- fail loud\n" as *u8); return 1 } 173 let prefix: *u8 = argv[2] as *u8 174 let cpath: *u8 = argv[3] as *u8 175 let today: i64 = vh_datedays(argv[4] as *u8) 176 if today < 0 { vh_p("VIZSLA-HEALTH bad today date (YYYY-MM-DD) -- fail loud\n" as *u8); return 1 } 177 178 // load contacts -> c_id, c_cad (tier cadence) 179 let szp: *i64 = sys_mmap(16) as *i64 180 let cb: *u8 = ss_readall(cpath, szp) 181 let csz: i64 = szp[0] 182 if csz <= 0 { vh_p("VIZSLA-HEALTH contacts MISSING/EMPTY -- fail loud\n" as *u8); return 1 } 183 let tr_name: *i64 = sys_mmap(8 * 32) as *i64 184 let tr_days: *i64 = sys_mmap(8 * 32) as *i64 185 var ntr: i64 = 0 186 let c_id: *i64 = sys_mmap(8 * 256) as *i64 187 let c_cad: *i64 = sys_mmap(8 * 256) as *i64 188 let c_tch: *i64 = sys_mmap(8 * 256) as *i64 189 let c_last: *i64 = sys_mmap(8 * 256) as *i64 190 let c_owe: *i64 = sys_mmap(8 * 256) as *i64 191 var nc: i64 = 0 192 let t0: *u8 = sys_mmap(128) 193 let t1: *u8 = sys_mmap(128) 194 let t2: *u8 = sys_mmap(128) 195 let t3: *u8 = sys_mmap(128) 196 var i: i64 = 0 197 while i < csz { 198 var e: i64 = i 199 var go: i64 = 1 200 while go == 1 { if e >= csz { go = 0 } else { if cb[e] == (10 as u8) { go = 0 } else { e = e + 1 } } } 201 var p: i64 = vh_tok(cb, i, e, t0, 128) 202 if t0[0] == (35 as u8) { t0[0] = 0 as u8 } 203 if vh_eq(t0, "TIER" as *u8) == 1 { 204 p = vh_tok(cb, p, e, t1, 128); p = vh_tok(cb, p, e, t2, 128) 205 if ntr < 32 { tr_name[ntr] = vh_dup(t1) as i64; tr_days[ntr] = vh_atoi(t2); ntr = ntr + 1 } 206 } 207 if vh_eq(t0, "CONTACT" as *u8) == 1 { 208 p = vh_tok(cb, p, e, t1, 128); p = vh_tok(cb, p, e, t2, 128); p = vh_tok(cb, p, e, t3, 128) 209 let ti: i64 = vh_find(tr_name, ntr, t3) 210 var cad: i64 = 60 211 if ti >= 0 { cad = tr_days[ti] } 212 if nc < 256 { c_id[nc] = vh_dup(t1) as i64; c_cad[nc] = cad; c_tch[nc] = 0; c_last[nc] = 0 - 1; c_owe[nc] = 0; nc = nc + 1 } 213 } 214 i = e + 1 215 } 216 if nc < 1 { vh_p("VIZSLA-HEALTH no CONTACT rows -- fail loud\n" as *u8); return 1 } 217 218 // walk the relate store: touches (count+latest per contact) + obligations (open OWE per contact) 219 let o_dir: *i64 = sys_mmap(8 * 512) as *i64 220 let o_ct: *i64 = sys_mmap(8 * 512) as *i64 221 let o_ref: *i64 = sys_mmap(8 * 512) as *i64 222 var no: i64 = 0 223 let settled: *i64 = sys_mmap(8 * 512) as *i64 224 var nset: i64 = 0 225 let segs: *i64 = sys_mmap(8 * 260) as *i64 226 let nseg: i64 = ss_manifest(prefix, segs) 227 let fk: *u8 = sys_mmap(64) 228 let fa: *u8 = sys_mmap(128) 229 let fb: *u8 = sys_mmap(128) 230 let fc: *u8 = sys_mmap(128) 231 let fdt: *u8 = sys_mmap(128) 232 var s: i64 = 0 233 while s < nseg { 234 let path: *u8 = sys_mmap(512) 235 var po: i64 = 0 236 po = vh_cat(path, po, prefix); po = vh_cat(path, po, segs[s] as *u8); po = vh_cat(path, po, ".docs" as *u8) 237 path[po] = 0 as u8 238 let szp2: *i64 = sys_mmap(16) as *i64 239 let b: *u8 = ss_readall(path, szp2) 240 let sz: i64 = szp2[0] 241 var j: i64 = 0 242 while j + 9 <= sz { 243 let kind: i64 = b[j] 244 let kl: i64 = ss_r32(b, j + 1) 245 let koff: i64 = j + 5 246 let vl: i64 = ss_r32(b, koff + kl) 247 let voff: i64 = koff + kl + 4 248 var take: i64 = 0 249 if kind == 1 { take = vh_isrel(b, koff, kl) } 250 if take == 1 { 251 let rec: *u8 = (b as i64 + voff) as *u8 252 if vh_field(rec, vl, "k" as *u8, fk, 64) == 1 { 253 if vh_eq(fk, "touch" as *u8) == 1 { 254 if vh_field(rec, vl, "a" as *u8, fa, 128) == 1 { 255 let ci: i64 = vh_find(c_id, nc, fa) 256 if ci >= 0 { 257 c_tch[ci] = c_tch[ci] + 1 258 if vh_field(rec, vl, "date" as *u8, fdt, 128) == 1 { let dd: i64 = vh_datedays(fdt); if dd > c_last[ci] { c_last[ci] = dd } } 259 } 260 } 261 } 262 if vh_eq(fk, "oblig" as *u8) == 1 { 263 vh_field(rec, vl, "a" as *u8, fa, 128) 264 vh_field(rec, vl, "b" as *u8, fb, 128) 265 vh_field(rec, vl, "c" as *u8, fc, 128) 266 if no < 512 { o_dir[no] = vh_dup(fa) as i64; o_ct[no] = vh_dup(fb) as i64; o_ref[no] = vh_dup(fc) as i64; no = no + 1 } 267 } 268 if vh_eq(fk, "settle" as *u8) == 1 { 269 if vh_field(rec, vl, "a" as *u8, fa, 128) == 1 { if nset < 512 { settled[nset] = vh_dup(fa) as i64; nset = nset + 1 } } 270 } 271 } 272 } 273 j = voff + vl 274 } 275 s = s + 1 276 } 277 // open OWE per contact (obligation whose ref is not settled, dir=OWE) 278 var k2: i64 = 0 279 while k2 < no { 280 if vh_find(settled, nset, o_ref[k2] as *u8) < 0 { 281 if vh_eq(o_dir[k2] as *u8, "OWE" as *u8) == 1 { 282 let ci: i64 = vh_find(c_id, nc, o_ct[k2] as *u8) 283 if ci >= 0 { c_owe[ci] = c_owe[ci] + 1 } 284 } 285 } 286 k2 = k2 + 1 287 } 288 289 // score + classify 290 let rep: *u8 = sys_mmap(K_MAGIC_131072) 291 var o: i64 = 0 292 var nthr: i64 = 0 293 var nsteady: i64 = 0 294 var ncool: i64 = 0 295 var nrisk: i64 = 0 296 var nnew: i64 = 0 297 var ci2: i64 = 0 298 while ci2 < nc { 299 var dsince: i64 = 0 - 1 300 if c_last[ci2] >= 0 { dsince = today - c_last[ci2] } 301 let rec_s: i64 = vh_recency(dsince, c_cad[ci2]) 302 let frq_s: i64 = vh_freq(c_tch[ci2]) 303 let bal_s: i64 = vh_bal(c_owe[ci2]) 304 let health: i64 = (rec_s * 50 + frq_s * 30 + bal_s * 20) / 100 305 var status: *u8 = "AT-RISK" as *u8 306 if c_last[ci2] < 0 { status = "NEW" as *u8; nnew = nnew + 1 } 307 else { 308 if health >= 75 { status = "THRIVING" as *u8; nthr = nthr + 1 } 309 else { if health >= 50 { status = "STEADY" as *u8; nsteady = nsteady + 1 } 310 else { if health >= 30 { status = "COOLING" as *u8; ncool = ncool + 1 } 311 else { nrisk = nrisk + 1 } } } 312 } 313 o = vh_cat(rep, o, "VIZSLA-HEALTH contact=" as *u8); o = vh_cat(rep, o, c_id[ci2] as *u8) 314 o = vh_cat(rep, o, " health=" as *u8); o = vh_catn(rep, o, health) 315 o = vh_cat(rep, o, " status=" as *u8); o = vh_cat(rep, o, status) 316 o = vh_cat(rep, o, " recency=" as *u8); o = vh_catn(rep, o, rec_s) 317 o = vh_cat(rep, o, " freq=" as *u8); o = vh_catn(rep, o, frq_s) 318 o = vh_cat(rep, o, " balance=" as *u8); o = vh_catn(rep, o, bal_s) 319 o = vh_cat(rep, o, " touches=" as *u8); o = vh_catn(rep, o, c_tch[ci2]) 320 o = vh_cat(rep, o, "\n" as *u8) 321 ci2 = ci2 + 1 322 } 323 o = vh_cat(rep, o, "VIZSLA-HEALTH-VERDICT contacts=" as *u8); o = vh_catn(rep, o, nc) 324 o = vh_cat(rep, o, " thriving=" as *u8); o = vh_catn(rep, o, nthr) 325 o = vh_cat(rep, o, " steady=" as *u8); o = vh_catn(rep, o, nsteady) 326 o = vh_cat(rep, o, " cooling=" as *u8); o = vh_catn(rep, o, ncool) 327 o = vh_cat(rep, o, " at_risk=" as *u8); o = vh_catn(rep, o, nrisk) 328 o = vh_cat(rep, o, " new=" as *u8); o = vh_catn(rep, o, nnew) 329 o = vh_cat(rep, o, "\n" as *u8) 330 sys_write(1, rep, o) 331 return 0 332}