nx_microbiome.nx source
↩ module page · 194 lines · 6621 B
1// nx_microbiome.nx -- loose colony of cells with collective function.
2//
3// Per [[feedback-unified-immune-architecture-three-tier]] table row:
4// "soil microbial community / gut+skin+oral microbiome." THE third-
5// tier ecosystem primitive that bridges digital substrate, land
6// stewardship, and human-health domains under a single biology-honest
7// abstraction.
8//
9// DIFFERENT from nx_organism: organism is a single multi-pathway
10// entity with tight coordination. Microbiome is a LOOSE COLLECTION
11// of independent organisms (or cells) that work together statistically
12// without central coordination -- like the gut microbiome's hundreds
13// of bacterial species, or the soil microbiome's thousands.
14//
15// THE DIVERSITY INSIGHT: a healthy microbiome has many distinct cell
16// kinds; a stressed/diseased one is dominated by one or two opportu-
17// nists. Substrate measures kind-diversity as a health metric --
18// >=4 distinct cell kinds = HEALTHY; 1-3 = STRESSED; 0 distinct =
19// COLLAPSED (sterile).
20//
21// Composes:
22// nx_cell -- microbiome holds many cell pointers
23// nx_organism -- distinct sibling primitive at a different scale
24// nx_immune -- colony-level immune monitors microbiome health
25// nx_aerobic -- stagnant microbiome = anaerobic decomposition
26// trigger
27// nx_attention_class -- diverse attention classes in microbiome =
28// healthy "ecology" of work
29//
30// V1 ships:
31// - struct NxMicrobiome with cells ring (cell pointers + kind tags)
32// - add_cell / remove_cell / find_cell
33// - kind_diversity (count of distinct attention classes present)
34// - health verdict (sealed: HEALTHY / STRESSED / COLLAPSED)
35//
36// Gap list (V1 honest perf verdict):
37// - kind = attention_class in V1; V2 may add per-cell taxonomic tag
38// - no per-cell weight (Shannon entropy ignored; just count distinct)
39// - no temporal-trend tracking (V2 adds rolling-window health)
40
41import "nx_syscalls.nx"
42import "nx_tier.nx"
43import "nx_attention_class.nx"
44import "nx_cell.nx"
45
46// ===== Sealed enum: NxMicrobiomeHealth ============================
47
48const NX_MB_HEALTHY: nx_int = 0 // >=4 distinct kinds
49const NX_MB_STRESSED: nx_int = 1 // 1-3 distinct kinds
50const NX_MB_COLLAPSED: nx_int = 2 // 0 cells (sterile)
51const NX_MB_N_HEALTHS: nx_int = 3
52
53// ===== Sealed enum: NxMicrobiomeVerdict ===========================
54
55const NX_MB_OK: nx_int = 0
56const NX_MB_ERR_FULL: nx_int = 1
57const NX_MB_ERR_NOT_FOUND: nx_int = 2
58const NX_MB_ERR_DUPLICATE: nx_int = 3
59
60// ===== Struct: NxMicrobiomeMember =================================
61
62struct NxMicrobiomeMember {
63 cell_id: nx_int,
64 cell_ptr: *NxCell,
65 kind_tag: nx_int, // == cell.attention_class in V1
66 joined_us: nx_size,
67}
68
69// ===== Struct: NxMicrobiome =======================================
70
71struct NxMicrobiome {
72 members: *NxMicrobiomeMember,
73 capacity: nx_size,
74 count: nx_size,
75}
76
77const NX_MB_MEMBER_BYTES: nx_size = 32
78
79const NX_MB_HEALTHY_DIVERSITY_THRESHOLD: nx_int = 4
80
81func nx_mb_health_is_valid(h: nx_int) -> nx_int {
82 if h < 0 { return 0 }
83 if h >= NX_MB_N_HEALTHS { return 0 }
84 return 1
85}
86
87func nx_microbiome_new(capacity: nx_size) -> *NxMicrobiome {
88 let m: *NxMicrobiome = (sys_mmap(24)) as *NxMicrobiome
89 let bytes: nx_size = capacity * NX_MB_MEMBER_BYTES
90 m.members = (sys_mmap(bytes)) as *NxMicrobiomeMember
91 m.capacity = capacity
92 m.count = 0
93 return m
94}
95
96func _mb_at(m: *NxMicrobiome, idx: nx_size) -> *NxMicrobiomeMember {
97 return (m.members as i64 + (idx as i64) * NX_MB_MEMBER_BYTES) as *NxMicrobiomeMember
98}
99
100func _mb_find(m: *NxMicrobiome, cell_id: nx_int) -> nx_int {
101 var i: nx_size = 0
102 while i < m.count {
103 let r: *NxMicrobiomeMember = _mb_at(m, i)
104 if r.cell_id == cell_id { return i as i64 }
105 i = i + 1
106 }
107 return -1
108}
109
110func nx_microbiome_add(m: *NxMicrobiome,
111 cell: *NxCell,
112 now_us: nx_size) -> nx_int {
113 if (cell as i64) == 0 { return NX_MB_ERR_NOT_FOUND }
114 if _mb_find(m, cell.cell_id) >= 0 { return NX_MB_ERR_DUPLICATE }
115 if m.count >= m.capacity { return NX_MB_ERR_FULL }
116 let r: *NxMicrobiomeMember = _mb_at(m, m.count)
117 r.cell_id = cell.cell_id
118 r.cell_ptr = cell
119 r.kind_tag = cell.attention_class
120 r.joined_us = now_us
121 m.count = m.count + 1
122 return NX_MB_OK
123}
124
125func nx_microbiome_remove(m: *NxMicrobiome, cell_id: nx_int) -> nx_int {
126 let idx: nx_int = _mb_find(m, cell_id)
127 if idx < 0 { return NX_MB_ERR_NOT_FOUND }
128 // Compact: shift remaining down
129 var i: nx_size = idx as nx_size
130 while i < m.count - 1 {
131 let dst: *NxMicrobiomeMember = _mb_at(m, i)
132 let src: *NxMicrobiomeMember = _mb_at(m, i + 1)
133 dst.cell_id = src.cell_id
134 dst.cell_ptr = src.cell_ptr
135 dst.kind_tag = src.kind_tag
136 dst.joined_us = src.joined_us
137 i = i + 1
138 }
139 m.count = m.count - 1
140 return NX_MB_OK
141}
142
143func nx_microbiome_contains(m: *NxMicrobiome, cell_id: nx_int) -> nx_int {
144 if _mb_find(m, cell_id) >= 0 { return 1 }
145 return 0
146}
147
148// ===== nx_microbiome_kind_diversity ===============================
149//
150// How many DISTINCT kind_tag values appear in the colony? Used by
151// health verdict.
152
153func nx_microbiome_kind_diversity(m: *NxMicrobiome) -> nx_int {
154 if m.count == 0 { return 0 }
155 var distinct: nx_int = 0
156 var i: nx_size = 0
157 while i < m.count {
158 let r_i: *NxMicrobiomeMember = _mb_at(m, i)
159 var seen_before: nx_int = 0
160 var j: nx_size = 0
161 while j < i {
162 let r_j: *NxMicrobiomeMember = _mb_at(m, j)
163 if r_j.kind_tag == r_i.kind_tag { seen_before = 1; break }
164 j = j + 1
165 }
166 if seen_before == 0 { distinct = distinct + 1 }
167 i = i + 1
168 }
169 return distinct
170}
171
172// ===== nx_microbiome_health =======================================
173
174func nx_microbiome_health(m: *NxMicrobiome) -> nx_int {
175 if m.count == 0 { return NX_MB_COLLAPSED }
176 let div: nx_int = nx_microbiome_kind_diversity(m)
177 if div >= NX_MB_HEALTHY_DIVERSITY_THRESHOLD { return NX_MB_HEALTHY }
178 return NX_MB_STRESSED
179}
180
181func nx_microbiome_count(m: *NxMicrobiome) -> nx_size {
182 return m.count
183}
184
185func nx_microbiome_count_by_kind(m: *NxMicrobiome, kind: nx_int) -> nx_int {
186 var hits: nx_int = 0
187 var i: nx_size = 0
188 while i < m.count {
189 let r: *NxMicrobiomeMember = _mb_at(m, i)
190 if r.kind_tag == kind { hits = hits + 1 }
191 i = i + 1
192 }
193 return hits
194}