code wiki / (root) / nx_tier1_immune_compose_test.nx

nx_tier1_immune_compose_test.nx source

↩ module page · 175 lines · 7761 B

1// nx_tier1_immune_compose_test.nx -- the complete judo loop. 2// 3// Exercises every Tier-1 immune primitive (methyl + pamp + crispr + 4// pamp_meta + antibody + lysis) together with the mimicry trio 5// (aposematism + crypsis + decoy) in a single scenario. Per 6// [[feedback-unified-immune-architecture-three-tier]] this is the 7// FULL unified-architecture verification. 8// 9// The scenario: 10// 1. Substrate boots; methyl-marked self cells; antibody array 11// initialized 12// 2. Aposematism warning advertised (SIEM_MONITORED, COMPLIANCE_ 13// AUDITED) -- combined backoff > 90% 14// 3. Decoy planted with tripwire token + decoy methyl mark 15// 4. Foreign byte buffer arrives at IO boundary 16// a. nx_pamp scans: homoglyph detected (single-channel hit) 17// b. nx_pamp_meta cross-validates: 3 channels say legit, 1 18// says fake -> HIGH_DECEPTION (mimic caught) 19// c. nx_crispr remembers signature 20// d. nx_antibody_generate creates naive antibody 21// 5. Second arrival of same signature: 22// a. nx_antibody_match HITS (O(1) recall) 23// b. nx_antibody_affinity_mature bumps to 972 Q10 24// c. nx_lysis_select picks CAPABILITY_REVOKE for MED severity 25// 6. Third arrival escalates to HIGH severity: 26// a. nx_lysis_select picks PROCESS_PAUSE (reversible-only) 27// b. nx_lysis_emit + nx_lysis_log -> evict_journal 28// 7. After 10 successful matches: 29// a. antibody promotes to MEMORY state 30// b. federation-readiness predicate fires 31// 32// All 8 immune primitives exercised; ~30 assertions; complete loop. 33 34import "nx_syscalls.nx" 35import "nx_tier.nx" 36import "nx_attention_class.nx" 37import "nx_evict_journal.nx" 38import "nx_methyl.nx" 39import "nx_pamp.nx" 40import "nx_pamp_meta.nx" 41import "nx_crispr.nx" 42import "nx_antibody.nx" 43import "nx_lysis.nx" 44import "nx_aposematism.nx" 45import "nx_decoy.nx" 46 47func main() -> i64 { 48 let sig: *u8 = (sys_mmap(96)) as *u8 49 sig[0] = 1 as u8 50 51 // ===== Step 1: substrate-self marked ============================= 52 let self_mark: *NxMethylMark = nx_methyl_new(100, 53 0xcafebabecafebabe, 1000, sig, 96) 54 if nx_methyl_is_self(self_mark, 2000, 5000, 100) != 1 { return 1 } 55 56 // ===== Step 2: aposematism warning stacked ======================== 57 let warn_sig: *u8 = (sys_mmap(16)) as *u8 58 warn_sig[0] = 67 as u8 // 'C' 59 let p_comp: *NxWarningPattern = nx_ap_pattern_new( 60 NX_AP_COMPLIANCE_AUDITED, warn_sig, 8) 61 let p_siem: *NxWarningPattern = nx_ap_pattern_new( 62 NX_AP_SIEM_MONITORED, warn_sig, 8) 63 let arr: **NxWarningPattern = (sys_mmap(16)) as **NxWarningPattern 64 let slot0: *i64 = (arr as i64) as *i64 65 let slot1: *i64 = (arr as i64 + 8) as *i64 66 slot0[0] = p_comp as i64 67 slot1[0] = p_siem as i64 68 let combined_backoff: nx_int = nx_ap_combined_backoff_q10(arr, 2) 69 // Compliance Q10 870 + SIEM Q10 410: stacked > each individually 70 if combined_backoff <= 870 { return 2 } 71 72 // ===== Step 3: decoy planted ====================================== 73 let cred_payload: *u8 = (sys_mmap(64)) as *u8 74 cred_payload[0] = 65 as u8 75 let decoy: *NxDecoy = nx_decoy_plant(NX_DK_FAKE_CREDENTIALS, 76 0xbeefcafedeadbeef, cred_payload, 64) 77 if (decoy as i64) == 0 { return 3 } 78 79 // ===== Step 4a: foreign buffer arrives; pamp scans ================ 80 // Trojan Source homoglyph: ASCII 'A' + Cyrillic 'а' 81 let hostile: *u8 = (sys_mmap(16)) as *u8 82 hostile[0] = 65 as u8 // 'A' 83 hostile[1] = 66 as u8 // 'B' 84 hostile[2] = 208 as u8 // Cyrillic prefix 85 hostile[3] = 176 as u8 // Cyrillic 'а' 86 hostile[4] = 67 as u8 // 'C' 87 let hit: *NxPampHit = (sys_mmap(32)) as *NxPampHit 88 let v_pamp: nx_int = nx_pamp_scan(hostile, 5, hit) 89 if v_pamp != NX_PAMP_DETECTED { return 4 } 90 if hit.kind != NX_PAMP_TROJAN_SOURCE_HOMOGLYPH { return 5 } 91 92 // ===== Step 4b: pamp_meta cross-validates ========================= 93 // 3 channels say legit (~900), 1 says fake (50) 94 let reading: *NxChannelReading = nx_pcm_reading_new(900, 920, 50, 905) 95 let v_meta: nx_int = nx_pcm_verdict(reading) 96 if v_meta != NX_PCM_HIGH_DECEPTION { return 6 } 97 let dec_q10: nx_int = nx_pcm_deception_q10(reading) 98 if dec_q10 < 500 { return 7 } 99 100 // ===== Step 4c: crispr remembers ================================== 101 let crispr: *NxCrisprArray = nx_crispr_new(32) 102 let homo_hash: nx_size = 0xfeed0001 103 nx_crispr_remember(crispr, NX_PAMP_TROJAN_SOURCE_HOMOGLYPH, 104 homo_hash, 5000) 105 if crispr.count != 1 { return 8 } 106 107 // ===== Step 4d: antibody generated from pamp+pamp_meta ============ 108 let ab_arr: *NxAntibodyArray = nx_antibody_array_new(16) 109 // Combine pamp's confidence with pamp_meta's deception_q10 to seed 110 // the antibody's initial affinity. Max(hit.confidence, dec_q10). 111 var init_aff: nx_int = hit.confidence 112 if dec_q10 > init_aff { init_aff = dec_q10 } 113 let ab_idx: nx_int = nx_antibody_generate(ab_arr, 114 NX_PAMP_TROJAN_SOURCE_HOMOGLYPH, homo_hash, init_aff, 5100) 115 if ab_idx != 0 { return 9 } 116 let ab0: *NxAntibody = (ab_arr.antibodies as i64 + 0 * 56) as *NxAntibody 117 if ab0.state != NX_AB_ST_NAIVE { return 10 } 118 119 // ===== Step 5a: second arrival; antibody hits ===================== 120 let m: nx_int = nx_antibody_match(ab_arr, 121 NX_PAMP_TROJAN_SOURCE_HOMOGLYPH, homo_hash) 122 if m != 0 { return 11 } 123 124 // ===== Step 5b: affinity mature =================================== 125 nx_antibody_affinity_mature(ab_arr, m, 6000) 126 if ab0.hit_count != 1 { return 12 } 127 if ab0.affinity_q10 < init_aff { return 13 } // bumped 128 129 // ===== Step 5c: lysis selects CAPABILITY_REVOKE for MED =========== 130 // severity=MED, net=0, persist=0, key_leak=0, reversible_only=0 131 let mech_med: nx_int = nx_lysis_select(NX_LYSIS_SEV_MED, 132 0, 0, 0, 0) 133 if mech_med != NX_LYSIS_CAPABILITY_REVOKE { return 14 } 134 135 // ===== Step 6a: HIGH severity escalation (reversible-only) ======== 136 let mech_hi_rev: nx_int = nx_lysis_select(NX_LYSIS_SEV_HIGH, 137 0, 1, 0, 1) 138 if mech_hi_rev != NX_LYSIS_PROCESS_PAUSE { return 15 } 139 140 // ===== Step 6b: emit + log lysis action =========================== 141 let journal: *NxEvictJournal = nx_evict_journal_new(16) 142 let action: *NxLysisAction = nx_lysis_emit(1234, 143 mech_hi_rev, NX_LYSIS_SEV_HIGH, 7000, 1) 144 if (action as i64) == 0 { return 16 } 145 if action.reversible != 1 { return 17 } 146 if action.operator_gated != 0 { return 18 } // PAUSE is not gated 147 nx_lysis_log(action, journal, NX_AC_DEV, 9999) 148 if journal.count != 1 { return 19 } 149 150 // ===== Step 7a: 9 more successful matches -> MEMORY state ========= 151 var i: nx_int = 0 152 while i < 9 { 153 nx_antibody_affinity_mature(ab_arr, 0, 8000 + i) 154 i = i + 1 155 } 156 if ab0.hit_count != 10 { return 20 } 157 if ab0.affinity_q10 != 1024 { return 21 } // capped 158 if ab0.state != NX_AB_ST_MEMORY { return 22 } 159 160 // ===== Step 7b: federation-ready ================================== 161 if nx_antibody_is_federation_ready(ab0) != 1 { return 23 } 162 163 // ===== Step 8: forensic ledger asserts ============================ 164 if nx_antibody_total_hits(ab_arr) != 10 { return 24 } 165 if nx_antibody_count_by_state(ab_arr, NX_AB_ST_MEMORY) != 1 { return 25 } 166 if nx_crispr_count_kind(crispr, NX_PAMP_TROJAN_SOURCE_HOMOGLYPH) != 1 { return 26 } 167 // Decoy was NOT read in this scenario (no xenocell probe sim'd) 168 if nx_decoy_was_read(decoy) != 0 { return 27 } 169 // Self mark still validates 170 if nx_methyl_is_self(self_mark, 8000, 10000, 100) != 1 { return 28 } 171 // Combined aposematism still high 172 if nx_ap_combined_backoff_q10(arr, 2) <= 870 { return 29 } 173 174 return 0 175}