nx_self_audit.nx source
↩ module page · 184 lines · 8916 B
1// nx_self_audit.nx -- substrate-native self-awareness primitive.
2//
3// Per user directive 2026-05-14: "and nishi lang should be self aware
4// of all these things allowing the user to improve it as it goes".
5//
6// Reads every substrate state file (algorithm_cards / ontology /
7// patent_table / source_allowlist / displacement_targets /
8// perf_baselines / competition_history) plus runtime/ inventory,
9// and emits an ACTIONABLE per-domain coverage + per-status report.
10//
11// Output convention: each line either announces a gap with a named
12// next-action OR confirms a covered area with a number. No raw
13// counts without context.
14//
15// genealogy_id: substrate_self_awareness_2026_05_14
16// lineage_id: user_improvable_introspection
17
18// nx_safety_envelope:
19// intended_use: AUTO_APPLIED -- primitive-specific tuning queued
20// sil_target: SIL1
21// evidence: [bulk_applied_2026-05-16, see-file-comment-for-detail]
22// verdict: NOT_YET_EVALUATED
23
24import "nx_syscalls.nx"
25import "nx_runtime.nx"
26import "nx_tier.nx"
27import "nx_str.nx"
28import "nx_provenance.nx"
29import "nx_patent_check.nx"
30import "nx_ontology.nx"
31
32// Count non-comment / non-blank lines in a TSV / TXT file. Returns
33// -1 on read failure. Strategy: scan once, track line_first_char
34// per newline; count if not '#' / '\n' / '\0'.
35func nx_audit_count_lines(path: *u8) -> nx_int {
36 let len_p: *nx_int = (sys_mmap(8)) as *nx_int
37 len_p[0] = 0
38 let buf: *u8 = sys_read_file(path, len_p)
39 if (buf as nx_int) == 0 { return -1 }
40 let n: nx_int = len_p[0]
41 var count: nx_int = 0
42 var i: nx_int = 0
43 while i < n {
44 let c: nx_int = buf[i] as nx_int
45 // line_first_char condition: i==0 OR previous byte was newline
46 var is_first: nx_int = 0
47 if i == 0 { is_first = 1 }
48 if i > 0 {
49 if buf[i - 1] == 10 { is_first = 1 }
50 }
51 if is_first == 1 {
52 if c != 35 { // not '#'
53 if c != 10 { // not blank
54 if c != 0 {
55 count = count + 1
56 }
57 }
58 }
59 }
60 i = i + 1
61 }
62 return count
63}
64
65func nx_audit_print_count(label: *u8, n: nx_int) {
66 print(label)
67 print(": " as *u8)
68 if n < 0 { print("(missing)" as *u8) }
69 if n >= 0 { print_i64(n) }
70 println("" as *u8)
71}
72
73func main() -> nx_exit {
74 println("==================================================================" as *u8)
75 println("NISHILANG SELF-AUDIT" as *u8)
76 println("(per self-aware-substrate cardinal: actionable beats encyclopedic)" as *u8)
77 println("==================================================================" as *u8)
78
79 // ----- Layer A: substrate state file counts -----
80 println("" as *u8)
81 println("[A] SUBSTRATE STATE (raw counts):" as *u8)
82 let n_cards: nx_int = nx_audit_count_lines("nxc2/specs/algorithm_cards.jsonl" as *u8)
83 let n_ontology: nx_int = nx_audit_count_lines("nxc2/specs/nx_ontology.txt" as *u8)
84 let n_patents: nx_int = nx_audit_count_lines("nxc2/specs/nx_patent_table.txt" as *u8)
85 let n_sources: nx_int = nx_audit_count_lines("nxc2/specs/nx_source_allowlist.txt" as *u8)
86 let n_targets: nx_int = nx_audit_count_lines("nxc2/specs/nx_displacement_targets.txt" as *u8)
87 let n_baseline: nx_int = nx_audit_count_lines("nxc2/specs/nx_perf_baselines.txt" as *u8)
88 let n_compete: nx_int = nx_audit_count_lines("nxc2/specs/nx_competition_history.txt" as *u8)
89
90 nx_audit_print_count(" algorithm_cards" as *u8, n_cards)
91 nx_audit_print_count(" ontology_triples" as *u8, n_ontology)
92 nx_audit_print_count(" patent_table_rows" as *u8, n_patents)
93 nx_audit_print_count(" source_allowlist_rows" as *u8, n_sources)
94 nx_audit_print_count(" displacement_targets" as *u8, n_targets)
95 nx_audit_print_count(" perf_baselines" as *u8, n_baseline)
96 nx_audit_print_count(" competition_history_rows" as *u8, n_compete)
97
98 // ----- Layer B: ontology domain coverage -----
99 println("" as *u8)
100 println("[B] ONTOLOGY DOMAIN COVERAGE (in_domain triples):" as *u8)
101 let o: *NxOntology = nx_ontology_load("nxc2/specs/nx_ontology.txt" as *u8)
102 if (o as nx_int) == 0 {
103 println(" (ontology load failed -- file missing or unreadable)" as *u8)
104 }
105 if (o as nx_int) != 0 {
106 nx_audit_print_count(" competitive_programming" as *u8,
107 nx_ontology_count_with_po(o, "in_domain" as *u8, "competitive_programming" as *u8))
108 nx_audit_print_count(" cryptography " as *u8,
109 nx_ontology_count_with_po(o, "in_domain" as *u8, "cryptography" as *u8))
110 nx_audit_print_count(" numerical_analysis " as *u8,
111 nx_ontology_count_with_po(o, "in_domain" as *u8, "numerical_analysis" as *u8))
112 nx_audit_print_count(" graph_theory " as *u8,
113 nx_ontology_count_with_po(o, "in_domain" as *u8, "graph_theory" as *u8))
114 nx_audit_print_count(" machine_learning " as *u8,
115 nx_ontology_count_with_po(o, "in_domain" as *u8, "machine_learning" as *u8))
116 nx_audit_print_count(" computational_geometry " as *u8,
117 nx_ontology_count_with_po(o, "in_domain" as *u8, "computational_geometry" as *u8))
118 nx_audit_print_count(" information_theory " as *u8,
119 nx_ontology_count_with_po(o, "in_domain" as *u8, "information_theory" as *u8))
120 nx_audit_print_count(" string_processing " as *u8,
121 nx_ontology_count_with_po(o, "in_domain" as *u8, "string_processing" as *u8))
122 nx_audit_print_count(" data_structures " as *u8,
123 nx_ontology_count_with_po(o, "in_domain" as *u8, "data_structures" as *u8))
124 }
125
126 // ----- Layer C: patent posture -----
127 println("" as *u8)
128 println("[C] PATENT POSTURE (from nx_patent_table.txt):" as *u8)
129 let t: *NxPatentTable = nx_patent_load("nxc2/specs/nx_patent_table.txt" as *u8)
130 if (t as nx_int) == 0 {
131 println(" (patent table load failed)" as *u8)
132 }
133 if (t as nx_int) != 0 {
134 nx_audit_print_count(" PD " as *u8, nx_patent_n_by_status(t, NX_PAT_PD))
135 nx_audit_print_count(" EXPIRED " as *u8, nx_patent_n_by_status(t, NX_PAT_EXPIRED))
136 nx_audit_print_count(" ACTIVE " as *u8, nx_patent_n_by_status(t, NX_PAT_ACTIVE))
137 nx_audit_print_count(" UNKNOWN " as *u8, nx_patent_n_by_status(t, NX_PAT_UNKNOWN))
138 nx_audit_print_count(" total rows " as *u8, nx_patent_n_records(t))
139 }
140
141 // ----- Layer D: ACTIONABLE next moves -----
142 println("" as *u8)
143 println("[D] ACTIONABLE NEXT MOVES (per 1pct-minimum + self-aware cardinals):" as *u8)
144
145 if (o as nx_int) != 0 {
146 let n_crypto: nx_int = nx_ontology_count_with_po(o, "in_domain" as *u8, "cryptography" as *u8)
147 if n_crypto == 0 {
148 println(" - cryptography domain has 0 ontology triples but 16 PD" as *u8)
149 println(" crypto algos in nx_patent_table (aes/sha/chacha20/curve25519" as *u8)
150 println(" /ed25519/argon2/bcrypt/scrypt etc.). Next: add ontology" as *u8)
151 println(" rows for those + start runtime/nx_crypto_chacha20.nx." as *u8)
152 }
153 let n_geom: nx_int = nx_ontology_count_with_po(o, "in_domain" as *u8, "computational_geometry" as *u8)
154 if n_geom == 0 {
155 println(" - computational_geometry has 0 ontology triples; CP canon" as *u8)
156 println(" needs convex_hull / sweep_line / closest_pair / kd_tree." as *u8)
157 println(" Next: add patent rows + ontology rows + runtime files." as *u8)
158 }
159 }
160
161 if (t as nx_int) != 0 {
162 let n_unknown: nx_int = nx_patent_n_by_status(t, NX_PAT_UNKNOWN)
163 if n_unknown > 0 {
164 println(" - patent_table has UNKNOWN-status rows (default-deny). Audit" as *u8)
165 println(" each: convert to PD / EXPIRED / ACTIVE based on research." as *u8)
166 }
167 }
168
169 println(" - HLL bench LOSE rows pending fix:" as *u8)
170 println(" * throughput_native: needs nxc2 native x86_64 target" as *u8)
171 println(" * parity_max_precision: extend nx_sketch_hll.nx lg_k cap to 21" as *u8)
172 println(" * serialized_size: implement nx_hll_serialize" as *u8)
173 println(" * memory_at_parity: now LOSE_BY_PARITY per 1pct cardinal" as *u8)
174 println(" (find 1% byte savings via header tightening)" as *u8)
175
176 println(" - Bench coverage: 1/4 Apache DataSketches families done (HLL)" as *u8)
177 println(" Still need: KLL (quantile) / Theta (set ops) / CMS (frequency)" as *u8)
178
179 println("" as *u8)
180 println("==================================================================" as *u8)
181 println("Run again any time to see what to improve next." as *u8)
182 println("==================================================================" as *u8)
183 return 0
184}