code wiki / _hdl_build / nx_known_issue_store.nx
nx_known_issue_store.nx source
↩ module page · 144 lines · 6639 B
1// nx_known_issue_store.nx -- LIB: the team's window onto the KNOWN-ISSUE catalogue in the
2// SOVEREIGN INFORMATION STORE (knowledge/store/ki-*, via nx_seg_store). The pure-Nishi home for
3// "have we seen this before?" -- NO TSV, NO SQL. The machine-matchable twin of the human-narrative
4// landmines / workaround_audit registries: those are prose for a reader; THIS is bytes an organ
5// matches a live failure against, so the Engineer/Doctor can RECALL a remedy instead of giving up.
6//
7// This is the structural twin of the "no-hang" error-emit guarantee (nx_netscope_dns recv-timed
8// keystone): there, every error PATH must EMIT a verdict within a deadline (never hang). Here, every
9// SEEN issue must be RECALLED + ROUTED to its remedy (never a silent "can't identify/fix this"). The
10// _recall_or_red_gate locks it the way T2 locks no-hang.
11//
12// Record schema (all keys under prefix knowledge/store/ki-), TAB-separated value fields:
13// ki:ids -> index: TAB-separated list of catalogue ids (the recall walk order)
14// ki:<id> -> id <tab> class <tab> status <tab> signature <tab> remedy
15// class = DIALECT|ENV|TOOLCHAIN|MATH (mirrors landmines scope)
16// status = AUTO|ADAPT|DISCIPLINE|OPEN (the RESPONSE class)
17// signature = exact substring to match in an Engineer diagnostic / crash
18// artifact, or "-" when the issue has no crisp text signature
19// (recall-by-id: a documented adaptation/discipline, not auto)
20// remedy = organ:func for AUTO (e.g. nx_doctor_fix:doc_heal_token), else
21// a response token (durable-runner-rearm, mask-after-shift, ...)
22// ki:provenance -> authorship breadcrumb (staging-vs-verified is machine-visible in the store)
23// license_tier: ORIGINAL
24import "nx_seg_store.nx"
25import "nx_syscalls.nx"
26const KI_MAGIC_1024: i64 = 1024
27
28const KI_PREFIX: *u8 = "knowledge/store/ki-"
29
30// recall verdicts
31const KI_HIT: i64 = 1 // a seen issue's signature matched the diagnostic -> remedy routed
32const KI_UNKNOWN: i64 = 0 // no seen signature matched -> genuinely novel (UNFIXABLE stays legal)
33
34func ki_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
35
36// latest record for `key` under `prefix`: 1=found (ptr/len set), 0=tombstoned, -1=absent.
37func ki_get_p(prefix: *u8, key: *u8, ptrout: *i64, lenout: *i64) -> i64 {
38 return ss_get(prefix, key, ptrout, lenout)
39}
40func ki_get(key: *u8, ptrout: *i64, lenout: *i64) -> i64 {
41 return ss_get(KI_PREFIX, key, ptrout, lenout)
42}
43
44// next segment id for a commit under `prefix` = 1 + current segment count.
45func ki_seg_next_p(prefix: *u8) -> i64 {
46 let segs: *i64 = sys_mmap(8 * 260) as *i64
47 let nseg: i64 = ss_manifest(prefix, segs)
48 if nseg < 0 { return 1 }
49 return 1 + nseg
50}
51
52// extract the f-th (0-based) TAB-separated field of rec[0..rlen) into out (NUL-terminated);
53// returns the field length (0 if absent). One parser for signature/remedy/status by construction.
54func ki_field(rec: *u8, rlen: i64, f: i64, out: *u8) -> i64 {
55 var cur: i64 = 0
56 var i: i64 = 0
57 var o: i64 = 0
58 while cur < f {
59 if i >= rlen { out[0] = 0 as u8; return 0 }
60 if rec[i] == (9 as u8) { cur = cur + 1 }
61 i = i + 1
62 }
63 var go: i64 = 1
64 while go == 1 {
65 if i >= rlen { go = 0 } else {
66 if rec[i] == (9 as u8) { go = 0 } else { out[o] = rec[i]; o = o + 1; i = i + 1 }
67 }
68 }
69 out[o] = 0 as u8
70 return o
71}
72
73// first index of needle[0..nlen) in hay[0..hlen), or -1. The match engine: a recalled signature
74// is a real substring of the live diagnostic (NOT a constant -- the tamper test proves this).
75func ki_substr(hay: *u8, hlen: i64, needle: *u8, nlen: i64) -> i64 {
76 if nlen <= 0 { return 0 - 1 }
77 var i: i64 = 0
78 while i + nlen <= hlen {
79 var m: i64 = 1
80 var j: i64 = 0
81 while j < nlen {
82 if hay[i + j] != needle[j] { m = 0; j = nlen } else { j = j + 1 }
83 }
84 if m == 1 { return i }
85 i = i + 1
86 }
87 return 0 - 1
88}
89
90// THE RECALL: walk the catalogue index under `prefix`; if any record's signature (field 3, != "-")
91// occurs as a substring of diag[0..dlen), copy that record's id/remedy/status into the outs and
92// return KI_HIT. If no seen signature matches, return KI_UNKNOWN (honest "genuinely new"). Pure
93// store reads -- the matcher is DATA-driven: adding a new known issue is a seed edit, not new code.
94func ki_recall(prefix: *u8, diag: *u8, dlen: i64, outid: *u8, outrem: *u8, outstat: *u8) -> i64 {
95 let pq: *i64 = sys_mmap(16) as *i64
96 let lq: *i64 = sys_mmap(16) as *i64
97 if ss_get(prefix, "ki:ids" as *u8, pq, lq) != 1 { return KI_UNKNOWN }
98 let ids: *u8 = pq[0] as *u8
99 let idn: i64 = lq[0]
100 let idbuf: *u8 = sys_mmap(64)
101 let keybuf: *u8 = sys_mmap(128)
102 let sig: *u8 = sys_mmap(KI_MAGIC_1024)
103 let rq: *i64 = sys_mmap(16) as *i64
104 let rl: *i64 = sys_mmap(16) as *i64
105 var i: i64 = 0
106 while i < idn {
107 // pull one TAB-separated id out of the index
108 var o: i64 = 0
109 var go: i64 = 1
110 while go == 1 {
111 if i >= idn { go = 0 } else {
112 if ids[i] == (9 as u8) { i = i + 1; go = 0 } else { idbuf[o] = ids[i]; o = o + 1; i = i + 1 }
113 }
114 }
115 idbuf[o] = 0 as u8
116 if o > 0 {
117 // key = "ki:" + id
118 keybuf[0] = 107 as u8 // 'k'
119 keybuf[1] = 105 as u8 // 'i'
120 keybuf[2] = 58 as u8 // ':'
121 var t: i64 = 0
122 while t < o { keybuf[3 + t] = idbuf[t]; t = t + 1 }
123 keybuf[3 + o] = 0 as u8
124 if ss_get(prefix, keybuf, rq, rl) == 1 {
125 let rec: *u8 = rq[0] as *u8
126 let rln: i64 = rl[0]
127 let slen: i64 = ki_field(rec, rln, 3, sig) // signature field
128 var has: i64 = 1
129 if slen == 0 { has = 0 }
130 if slen == 1 { if sig[0] == (45 as u8) { has = 0 } } // "-" = no signature
131 if has == 1 {
132 if ki_substr(diag, dlen, sig, slen) >= 0 {
133 var z: i64 = 0
134 while z <= o { outid[z] = idbuf[z]; z = z + 1 }
135 ki_field(rec, rln, 4, outrem) // remedy
136 ki_field(rec, rln, 2, outstat) // status
137 return KI_HIT
138 }
139 }
140 }
141 }
142 }
143 return KI_UNKNOWN
144}