nx_tier2_immune_compose_test.nx source
↩ module page · 175 lines · 7821 B
1// nx_tier2_immune_compose_test.nx -- colony immunity + federation.
2//
3// The Tier-2 unified demonstration: multi-cell host where one cell
4// detects + matures an antibody, immune system orchestrates clonal
5// expansion to peer cells, pollinate federates the federation-ready
6// antibody to remote peer hosts, vitals catches Intel ME activity
7// invisible at OS layer (power draw during reported idle).
8//
9// Per [[feedback-unified-immune-architecture-three-tier]] Tier-2 +
10// federation closure.
11//
12// Lifecycle simulated:
13// 1. Host has 3 cells (voxel-world / image-grader / reader-audio)
14// 2. Each cell has its own nx_antibody_array; all registered with
15// nx_immune as HEALTHY initially
16// 3. Vitals baseline captured during clean idle (pkg 5000 mW)
17// 4. Intel ME activity: vitals sample shows pkg 5800 mW during
18// OS-reported idle -- 16% above baseline -- ANOMALY fires
19// 5. Image-grader cell encounters a malicious classifier-poison
20// payload; pamp_meta cross-validates HIGH_DECEPTION; antibody
21// generated; matures through 10 hits to MEMORY state
22// 6. nx_immune_clonal_expand broadcasts the antibody to voxel-world
23// and reader-audio (the other cells), so they detect the same
24// attack on first encounter
25// 7. Antibody is federation-ready (affinity 870+); operator approves
26// via nx_pollinate_consent; payload queued for distribution to
27// 3 known peer hosts
28// 8. Final state: 3 cells immunized with same signature; vitals
29// anomaly count > 0; pollinator outbox has the antibody
30// payload awaiting transport
31
32import "nx_syscalls.nx"
33import "nx_tier.nx"
34import "nx_evict_journal.nx"
35import "nx_pamp.nx"
36import "nx_pamp_meta.nx"
37import "nx_antibody.nx"
38import "nx_lysis.nx"
39import "nx_vitals.nx"
40import "nx_immune.nx"
41import "nx_pollinate.nx"
42
43func main() -> i64 {
44 // ===== Step 1: build the colony (3 cells, each w/ antibody array)
45 let journal: *NxEvictJournal = nx_evict_journal_new(32)
46 let immune: *NxImmune = nx_immune_new(8, journal)
47
48 let ab_voxel: *NxAntibodyArray = nx_antibody_array_new(8)
49 let ab_grader: *NxAntibodyArray = nx_antibody_array_new(8)
50 let ab_reader: *NxAntibodyArray = nx_antibody_array_new(8)
51
52 nx_immune_register_cell(immune, 100, ab_voxel)
53 nx_immune_register_cell(immune, 200, ab_grader)
54 nx_immune_register_cell(immune, 300, ab_reader)
55 if immune.n_cells != 3 { return 1 }
56
57 // ===== Step 2: colony starts HEALTHY ============================
58 if nx_immune_global_status(immune) != NX_COL_HEALTHY { return 2 }
59
60 // ===== Step 3: vitals baseline -- 5000mW clean idle ==============
61 // source, state, min_value=4800, max_value=5200,
62 // low_dev=100, high_dev=300, critical_dev=600
63 let baseline: *NxVitalsEnvelope = nx_vs_envelope_new(
64 NX_VS_DC_RAIL, NX_VS_STATE_IDLE, 4800, 5200,
65 100, 300, 600)
66 if baseline.min_value != 4800 { return 3 }
67
68 // ===== Step 4: Intel ME activity -- power 5800mW during idle ====
69 // Within envelope expected_min=4800, expected_max=5200; 5800 is
70 // OUTSIDE envelope -> nx_vs_classify returns deviation.
71 let reading: *NxVitalsReading = nx_vs_reading_new(
72 NX_VS_DC_RAIL, NX_VS_STATE_IDLE, 5800, 1000)
73 let v: nx_int = nx_vs_classify(reading, baseline)
74 if v == NX_VV_WITHIN_ENVELOPE { return 4 }
75 if nx_vv_is_concerning(v) != 1 { return 5 }
76
77 // ===== Step 5: also use the dedicated ME helper ==================
78 // 100mA drain during OFF state with system-asleep is anomalous.
79 // Envelope is 0-5 mA; 100 mA is far above critical_dev=100.
80 let me_drain_verdict: nx_int = nx_vs_classify_off_state_active_ME(100)
81 if nx_vv_is_concerning(me_drain_verdict) != 1 { return 6 }
82
83 // ===== Step 6: grader encounters poisoned classifier payload =====
84 // pamp single-channel detects homoglyph
85 let poison: *u8 = (sys_mmap(16)) as *u8
86 poison[0] = 65 as u8 // 'A'
87 poison[1] = 208 as u8 // Cyrillic prefix
88 poison[2] = 176 as u8 // 0xB0 -- Cyrillic 'а'
89 poison[3] = 66 as u8 // 'B'
90 let hit: *NxPampHit = (sys_mmap(32)) as *NxPampHit
91 if nx_pamp_scan(poison, 4, hit) != NX_PAMP_DETECTED { return 7 }
92
93 // pamp_meta cross-validates (3 channels say legit, 1 says fake)
94 let reading2: *NxChannelReading = nx_pcm_reading_new(900, 920, 50, 905)
95 if nx_pcm_verdict(reading2) != NX_PCM_HIGH_DECEPTION { return 8 }
96
97 // grader cell -> ALERTED (it observed the threat)
98 nx_immune_set_cell_status(immune, 200, NX_COL_ALERTED, 2000)
99 if nx_immune_global_status(immune) != NX_COL_ALERTED { return 9 }
100
101 // grader generates antibody
102 let ab_idx: nx_int = nx_antibody_generate(ab_grader,
103 NX_PAMP_TROJAN_SOURCE_HOMOGLYPH, 0x1ee00001, 900, 2100)
104 if ab_idx < 0 { return 10 }
105
106 // ===== Step 7: matures through 10 hits to MEMORY ================
107 var i: nx_int = 0
108 while i < 10 {
109 nx_antibody_affinity_mature(ab_grader, ab_idx, 3000 + i * 100)
110 i = i + 1
111 }
112 let grader_ab: *NxAntibody = (ab_grader.antibodies as i64 +
113 (ab_idx as i64) * 56) as *NxAntibody
114 if grader_ab.state != NX_AB_ST_MEMORY { return 11 }
115 if nx_antibody_is_federation_ready(grader_ab) != 1 { return 12 }
116
117 // ===== Step 8: clonal expansion -- voxel+reader receive antibody =
118 let distributed: nx_int = nx_immune_clonal_expand(immune, 200,
119 NX_PAMP_TROJAN_SOURCE_HOMOGLYPH, 0x1ee00001,
120 grader_ab.affinity_q10, 4500)
121 if distributed != 2 { return 13 }
122 if ab_voxel.count != 1 { return 14 }
123 if ab_reader.count != 1 { return 15 }
124
125 // ===== Step 9: voxel encounter is now O(1) HIT (no full pamp scan)
126 let voxel_match: nx_int = nx_antibody_match(ab_voxel,
127 NX_PAMP_TROJAN_SOURCE_HOMOGLYPH, 0x1ee00001)
128 if voxel_match < 0 { return 16 }
129
130 // ===== Step 10: pollinate -- federate antibody to peer hosts ====
131 let pollinator: *NxPollinator = nx_pollinator_new(8, 8, 1, journal)
132 nx_pollinate_add_peer(pollinator, 1001, 768) // family-laptop
133 nx_pollinate_add_peer(pollinator, 1002, 614) // homelab
134 nx_pollinate_add_peer(pollinator, 1003, 410) // newer peer
135 if pollinator.n_peers != 3 { return 17 }
136
137 let body: *u8 = (sys_mmap(64)) as *u8
138 body[0] = 65 as u8
139 let sig: *u8 = (sys_mmap(96)) as *u8
140 sig[0] = 42 as u8
141 let payload: *NxPollinationPayload = (sys_mmap(72)) as *NxPollinationPayload
142 payload.kind = NX_PK_ANTIBODY
143 payload.originator_id = 7 // this host
144 payload.content_hash = 0x1ee00001
145 payload.body_ptr = body
146 payload.body_len = 64
147 payload.sig_ptr = sig
148 payload.sig_len = 96
149 payload.ts_us = 5000
150
151 if nx_pollinate_propose(pollinator, payload) != NX_PL_OK { return 18 }
152 if nx_pollinate_outbox_count(pollinator) != 1 { return 19 }
153
154 // ===== Step 11: operator consents to federation =================
155 if nx_pollinate_consent(pollinator, 0, 5100) != NX_PL_OK { return 20 }
156
157 // ===== Step 12: lysis selects MED CAPABILITY_REVOKE ==============
158 let mech: nx_int = nx_lysis_select(NX_LYSIS_SEV_MED, 0, 0, 0, 0)
159 if mech != NX_LYSIS_CAPABILITY_REVOKE { return 21 }
160
161 // ===== Step 13: grader returns to HEALTHY after lysis ===========
162 nx_immune_set_cell_status(immune, 200, NX_COL_HEALTHY, 6000)
163 if nx_immune_global_status(immune) != NX_COL_HEALTHY { return 22 }
164
165 // ===== Step 14: quarantine NOT recommended (all 3 cells healthy)
166 if nx_immune_should_quarantine_host(immune) != 0 { return 23 }
167
168 // ===== Step 15: forensic asserts ================================
169 if nx_immune_alert_count(immune, 200) != 1 { return 24 }
170 if nx_antibody_count_by_state(ab_grader, NX_AB_ST_MEMORY) != 1 { return 25 }
171 // 1 journal entry from the consent log
172 if journal.count < 1 { return 26 }
173
174 return 0
175}