code wiki / _hdl_build / nx_labsci_gate.nx
nx_labsci_gate.nx source
↩ module page · 190 lines · 9724 B
1// nx_labsci_gate.nx -- THE MISSING EVIDENCE ORGAN for the lab-science lane.
2//
3// WHY THIS EXISTS: the ecosystem carries a real chemistry/biochemistry stack (nx_peptide + _ext/_msms/
4// _deconv/_denovo/_identify/_isotope, nx_supplement_screen, nx_thermal_process, nx_water_activity,
5// nx_shelf_life, nx_stability, nx_stability_rh, nx_icecream) exposed through nx_labsci_svc.
6//
7// The lane IS gated -- 43 gates / 497 assertions -- but by `_scratch/labsci_lane_gate.sh`: a shell
8// script, outside any deployable tree, hardcoding one laptop's absolute path (debt seq1352). So its
9// evidence can only be produced by hand, on one machine, into the AUTHORING root -- while the maturity
10// rollup reads the NAS root (181 logs there vs 1164 locally, debt seq1339). The domain is therefore
11// ungradeable not for want of measurement but because the witness cannot run where the reader looks.
12//
13// This organ is the sovereign, deployable replacement path: it builds and runs on the NAS, so the
14// evidence is PRODUCED where the ruler reads instead of copied there (a copied log is a stale claim).
15//
16// WHAT IT MEASURES: the library against PUBLISHED EXTERNAL REFERENCE VALUES, not against itself.
17// Self-agreement is not evidence -- these are standard MS calibration peptides and established
18// regulatory thresholds that were never inputs to our derivations:
19// Angiotensin II DRVYIHPF monoisotopic 1045.5345 Da
20// Bradykinin RPPGFSPFR monoisotopic 1059.5614 Da
21// Substance P RPKPQQFFGLM free acid 1347.7121 Da
22// HTST pasteurisation 72 C / 15 s = the legal PMO schedule
23// pH 4.6 = the C. botulinum high-acid regulatory line
24// Every positive has a NEGATIVE CONTROL so the gate can go RED.
25//
26// It writes knowledge/status/labsci_gate.log with a VERDICT= line, which is exactly the shape the
27// ecomat evkind-2 gate-liveness deriver (el_last_green) reads -- so admitting a `lab-science` domain
28// pointing here yields a MEASURED grade, never a stored assertion.
29// license_tier: ORIGINAL expect_exit: 0
30import "nx_syscalls.nx"
31import "nx_peptide.nx"
32import "nx_thermal_process.nx"
33import "nx_water_activity.nx"
34
35const LG_STDOUT: i64 = 1
36const LG_MODE: i64 = 420
37const LG_LOG: *u8 = "knowledge/status/labsci_gate.log"
38
39// Mass tolerance in q4 units (1e-4 Da). 100 = 0.01 Da -- tight enough that a wrong residue table or a
40// missing water of hydrolysis FAILS, loose enough to absorb fixed-point rounding.
41const LG_MASS_TOL_Q4: i64 = 100
42
43// Published monoisotopic masses x 10^4.
44const LG_ANGIOTENSIN2_Q4: i64 = 10455345
45const LG_BRADYKININ_Q4: i64 = 10595614
46// Substance P as the PLAIN sequence (free acid): sum of residue monoisotopic masses + H2O = 1347.7121.
47// NOT 1346.7281 -- that is the C-terminally AMIDATED physiological form, and amidation (OH -> NH2,
48// -0.9840 Da) is a modification the sequence alone does not declare. Feeding the amidated figure to a
49// sequence-only calculator is a ~1 Da category error; the first run of this gate caught exactly that,
50// and the library was right. Kept as a distinct constant so the distinction stays documented.
51const LG_SUBSTANCEP_Q4: i64 = 13477121
52
53// Monoisotopic proton, x 10^4 (1.007276 Da).
54const LG_PROTON_Q4: i64 = 10073
55
56// PMO HTST: 72 C (milli-C) held 15 s (ms). pH in milli-units; 4.6 is the botulinum line.
57const LG_HTST_TEMP_MILLI_C: i64 = 72000
58const LG_HTST_HOLD_MS: i64 = 15000
59const LG_WEAK_TEMP_MILLI_C: i64 = 60000
60const LG_WEAK_HOLD_MS: i64 = 1000
61const LG_PH_HIGH_ACID_MILLI: i64 = 4000
62const LG_PH_LOW_ACID_MILLI: i64 = 5000
63
64func lg_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(LG_STDOUT, s, n); return 0 }
65func lg_num(fd: i64, v: i64) -> i64 {
66 var m: i64 = v
67 if m < 0 { m = 0 - m; sys_write(fd, "-" as *u8, 1) }
68 let t: *u8 = sys_mmap(28)
69 var k: i64 = 0
70 if m == 0 { t[0] = 48 as u8; k = 1 }
71 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
72 let o: *u8 = sys_mmap(28)
73 var i: i64 = 0
74 while i < k { o[i] = t[k - 1 - i]; i = i + 1 }
75 sys_write(fd, o, k)
76 return 0
77}
78func lg_abs(v: i64) -> i64 { if v < 0 { return 0 - v } return v }
79
80// one check: report PASS/FAIL with the measured number so a RED is diagnosable, never just a count.
81func lg_check(ok: i64, label: *u8, got: i64, want: i64) -> i64 {
82 if ok == 1 { lg_puts(" PASS " as *u8) } else { lg_puts(" FAIL " as *u8) }
83 lg_puts(label)
84 lg_puts(" got=" as *u8); lg_num(LG_STDOUT, got)
85 lg_puts(" want=" as *u8); lg_num(LG_STDOUT, want)
86 lg_puts("\n" as *u8)
87 if ok == 1 { return 0 }
88 return 1
89}
90
91// mass within tolerance of the published value
92func lg_mass_ok(seq: *u8, want: i64, label: *u8) -> i64 {
93 let got: i64 = pep_mass_mono_q4(seq)
94 var ok: i64 = 0
95 if lg_abs(got - want) <= LG_MASS_TOL_Q4 { ok = 1 }
96 return lg_check(ok, label, got, want)
97}
98
99func main() -> i64 {
100 lg_puts("=== NISHI LAB-SCIENCE GATE -- chemistry measured vs PUBLISHED external references ===\n" as *u8)
101 var fails: i64 = 0
102 var checks: i64 = 0
103
104 lg_puts("-- peptide: monoisotopic mass vs standard MS calibration peptides --\n" as *u8)
105 fails = fails + lg_mass_ok("DRVYIHPF" as *u8, LG_ANGIOTENSIN2_Q4, "angiotensin-II mono q4" as *u8); checks = checks + 1
106 fails = fails + lg_mass_ok("RPPGFSPFR" as *u8, LG_BRADYKININ_Q4, "bradykinin mono q4" as *u8); checks = checks + 1
107 fails = fails + lg_mass_ok("RPKPQQFFGLM" as *u8, LG_SUBSTANCEP_Q4, "substance-P mono q4" as *u8); checks = checks + 1
108
109 lg_puts("-- peptide: structural invariants + validity neg-controls --\n" as *u8)
110 let l8: i64 = pep_len("DRVYIHPF" as *u8)
111 var ok: i64 = 0; if l8 == 8 { ok = 1 }
112 fails = fails + lg_check(ok, "angiotensin-II length" as *u8, l8, 8); checks = checks + 1
113
114 let v1: i64 = pep_seq_valid("DRVYIHPF" as *u8)
115 ok = 0; if v1 == 1 { ok = 1 }
116 fails = fails + lg_check(ok, "valid sequence accepted" as *u8, v1, 1); checks = checks + 1
117
118 // NEG-CONTROL: X is not a standard residue -- must be REFUSED, not silently approximated.
119 let v2: i64 = pep_seq_valid("DRVYIHPX" as *u8)
120 ok = 0; if v2 != 1 { ok = 1 }
121 fails = fails + lg_check(ok, "neg-ctrl non-standard residue refused" as *u8, v2, 0); checks = checks + 1
122
123 // average mass >= monoisotopic for any real peptide (isotope-weighted mean sits above the light peak)
124 let mono: i64 = pep_mass_mono_q4("DRVYIHPF" as *u8)
125 let avg: i64 = pep_mass_avg_q4("DRVYIHPF" as *u8)
126 ok = 0; if avg > mono { ok = 1 }
127 fails = fails + lg_check(ok, "average mass exceeds monoisotopic" as *u8, avg, mono); checks = checks + 1
128
129 // Charge-state arithmetic. [M+2H]2+ is NOT half of [M+H]+ -- it is M/2 + proton, while half of
130 // [M+H]+ is M/2 + proton/2, so they differ by half a proton BY CONSTRUCTION. The exact physical
131 // invariant is 2*mz2 - mz1 = one proton, which pins the charge-carrier mass itself.
132 let mz1: i64 = pep_mz_q4(mono, 1)
133 let mz2: i64 = pep_mz_q4(mono, 2)
134 let carrier: i64 = (2 * mz2) - mz1
135 ok = 0; if lg_abs(carrier - LG_PROTON_Q4) <= LG_MASS_TOL_Q4 { ok = 1 }
136 fails = fails + lg_check(ok, "2*[M+2H]2+ - [M+H]+ = proton" as *u8, carrier, LG_PROTON_Q4); checks = checks + 1
137
138 lg_puts("-- thermal: PMO pasteurisation schedule + neg-control --\n" as *u8)
139 let c1: i64 = tp_pasteurization_compliant(LG_HTST_TEMP_MILLI_C, LG_HTST_HOLD_MS, 0)
140 ok = 0; if c1 == 1 { ok = 1 }
141 fails = fails + lg_check(ok, "HTST 72C/15s compliant" as *u8, c1, 1); checks = checks + 1
142
143 // NEG-CONTROL: 60 C for 1 s is nowhere near lethal -- must NOT pass.
144 let c2: i64 = tp_pasteurization_compliant(LG_WEAK_TEMP_MILLI_C, LG_WEAK_HOLD_MS, 0)
145 ok = 0; if c2 != 1 { ok = 1 }
146 fails = fails + lg_check(ok, "neg-ctrl 60C/1s not compliant" as *u8, c2, 0); checks = checks + 1
147
148 // lethality must be monotone in hold time
149 let e1: i64 = tp_pasteurization_equiv_ms(LG_HTST_TEMP_MILLI_C, LG_HTST_HOLD_MS, 0)
150 let e2: i64 = tp_pasteurization_equiv_ms(LG_HTST_TEMP_MILLI_C, LG_HTST_HOLD_MS * 2, 0)
151 ok = 0; if e2 > e1 { ok = 1 }
152 fails = fails + lg_check(ok, "lethality monotone in hold time" as *u8, e2, e1); checks = checks + 1
153
154 lg_puts("-- acid barrier: the pH 4.6 C. botulinum regulatory line --\n" as *u8)
155 let a1: i64 = cs_is_high_acid(LG_PH_HIGH_ACID_MILLI)
156 ok = 0; if a1 == 1 { ok = 1 }
157 fails = fails + lg_check(ok, "pH 4.0 is high-acid" as *u8, a1, 1); checks = checks + 1
158
159 // NEG-CONTROL: pH 5.0 is above the line -- must NOT be called high-acid.
160 let a2: i64 = cs_is_high_acid(LG_PH_LOW_ACID_MILLI)
161 ok = 0; if a2 != 1 { ok = 1 }
162 fails = fails + lg_check(ok, "neg-ctrl pH 5.0 not high-acid" as *u8, a2, 0); checks = checks + 1
163
164 // ---- verdict + the evidence line the ecomat deriver reads ----
165 let passed: i64 = checks - fails
166 var permil: i64 = 0
167 if checks > 0 { permil = (passed * 1000) / checks }
168
169 lg_puts("LABSCI-GATE checks=" as *u8); lg_num(LG_STDOUT, checks)
170 lg_puts(" passed=" as *u8); lg_num(LG_STDOUT, passed)
171 lg_puts(" permil=" as *u8); lg_num(LG_STDOUT, permil)
172 if fails == 0 { lg_puts(" VERDICT=GREEN\n" as *u8) } else { lg_puts(" VERDICT=RED\n" as *u8) }
173
174 let fd: i64 = sys_openat_append(LG_LOG, LG_MODE)
175 if fd >= 0 {
176 lg_num(fd, sys_now_realtime_sec())
177 sys_write(fd, " nx_labsci_gate checks=" as *u8, 23)
178 lg_num(fd, checks)
179 sys_write(fd, " passed=" as *u8, 8)
180 lg_num(fd, passed)
181 sys_write(fd, " permil=" as *u8, 8)
182 lg_num(fd, permil)
183 if fails == 0 { sys_write(fd, " VERDICT=GREEN\n" as *u8, 15) } else { sys_write(fd, " VERDICT=RED\n" as *u8, 13) }
184 sys_close(fd)
185 }
186
187 if fails == 0 { sys_exit(0); return 0 }
188 sys_exit(1)
189 return 1
190}