nx_meristem.nx source
↩ module page · 194 lines · 6598 B
1// nx_meristem.nx -- undifferentiated growth-tip substrate for spawning new cells.
2//
3// Biology: meristematic tissue at root tips, shoot apices, and cambium is
4// the SOURCE of all plant growth. Cells there are undifferentiated and
5// retain the ability to become root / leaf / stem / flower depending on
6// hormonal signals and position. Without meristem, no growth.
7//
8// In Nishi: spawn-new-cell substrate that retains differentiation choice
9// until commitment. Composes with [[nx_spore]] (the spore germinates INTO
10// a meristem) + [[nx_seed]] (seed contains apical + radicle meristem) +
11// [[feedback-substrate-builds-z-image-class-ai-from-math-up]] (cells
12// differentiate into image / video / LLM / game roles at runtime under
13// resource pressure from the conductor cardinal).
14//
15// V1 ships sealed enum of differentiation roles + spawn slot + commit
16// gate + lineage tracking.
17
18import "nx_syscalls.nx"
19import "nx_tier.nx"
20
21// ===== Sealed enum: NxCellRole ====================================
22//
23// What an undifferentiated cell can commit to becoming.
24
25const NX_ME_ROLE_UNDIFFERENTIATED: nx_int = 0 // not yet committed
26const NX_ME_ROLE_LLM_INFERENCE: nx_int = 1
27const NX_ME_ROLE_IMAGE_GEN: nx_int = 2
28const NX_ME_ROLE_VIDEO_GEN: nx_int = 3
29const NX_ME_ROLE_AUDIO_TTS: nx_int = 4
30const NX_ME_ROLE_GAME_LOGIC: nx_int = 5
31const NX_ME_ROLE_PHYSICS_SIM: nx_int = 6
32const NX_ME_ROLE_STORAGE_TIER: nx_int = 7
33const NX_ME_ROLE_NETWORK_PEER: nx_int = 8
34const NX_ME_ROLE_AUDIT_SCAN: nx_int = 9
35const NX_ME_ROLE_N: nx_int = 10
36
37// ===== Sealed enum: NxMeristemVerdict =============================
38
39const NX_ME_V_OK: nx_int = 0
40const NX_ME_V_ALREADY_DIFFERENTIATED: nx_int = 1
41const NX_ME_V_DIFFERENTIATION_REFUSED: nx_int = 2 // no resources for role
42const NX_ME_V_INVALID: nx_int = 3
43const NX_ME_V_NULL: nx_int = 4
44const NX_ME_V_N: nx_int = 5
45
46// ===== Struct: NxCell =============================================
47
48struct NxCell {
49 cell_id: nx_int,
50 role: nx_int,
51 parent_meristem_id: nx_int,
52 spawned_at_us: nx_size,
53 committed_at_us: nx_size, // 0 until commit
54 lineage_depth: nx_int,
55}
56
57const NX_ME_C_BYTES: nx_int = 48
58
59struct NxMeristem {
60 meristem_id: nx_int,
61 cells: *u8,
62 n_cells: nx_int,
63 capacity: nx_int,
64 parent_seed_id: nx_int, // 0 if root meristem
65 next_cell_id: nx_int,
66 formed_at_us: nx_size,
67}
68
69const NX_ME_BYTES: nx_int = 56
70
71// ===== Validators =================================================
72
73func nx_me_role_is_valid(r: nx_int) -> nx_int {
74 if r < 0 { return 0 }
75 if r >= NX_ME_ROLE_N { return 0 }
76 return 1
77}
78
79func nx_me_v_is_valid(v: nx_int) -> nx_int {
80 if v < 0 { return 0 }
81 if v >= NX_ME_V_N { return 0 }
82 return 1
83}
84
85func nx_me_role_is_compute_heavy(r: nx_int) -> nx_int {
86 if r == NX_ME_ROLE_LLM_INFERENCE { return 1 }
87 if r == NX_ME_ROLE_IMAGE_GEN { return 1 }
88 if r == NX_ME_ROLE_VIDEO_GEN { return 1 }
89 if r == NX_ME_ROLE_PHYSICS_SIM { return 1 }
90 return 0
91}
92
93// ===== Constructor ================================================
94
95func nx_me_new(meristem_id: nx_int,
96 capacity: nx_int,
97 parent_seed_id: nx_int,
98 now_us: nx_size) -> *NxMeristem {
99 if capacity <= 0 { return 0 as *NxMeristem }
100 if meristem_id == 0 { return 0 as *NxMeristem }
101 let raw: *u8 = sys_mmap(NX_ME_BYTES)
102 let m: *NxMeristem = raw as *NxMeristem
103 m.meristem_id = meristem_id
104 m.cells = sys_mmap(capacity * NX_ME_C_BYTES)
105 m.n_cells = 0
106 m.capacity = capacity
107 m.parent_seed_id = parent_seed_id
108 m.next_cell_id = 1
109 m.formed_at_us = now_us
110 return m
111}
112
113func _me_cell_at(m: *NxMeristem, idx: nx_int) -> *NxCell {
114 if idx < 0 { return 0 as *NxCell }
115 if idx >= m.n_cells { return 0 as *NxCell }
116 let off: nx_int = idx * NX_ME_C_BYTES
117 return (m.cells + off) as *NxCell
118}
119
120// ===== Spawn undifferentiated cell ================================
121
122func nx_me_spawn(m: *NxMeristem, lineage_depth: nx_int, now_us: nx_size) -> nx_int {
123 if (m as i64) == 0 { return 0 }
124 if m.n_cells >= m.capacity { return 0 }
125 let off: nx_int = m.n_cells * NX_ME_C_BYTES
126 let c: *NxCell = (m.cells + off) as *NxCell
127 c.cell_id = m.next_cell_id
128 c.role = NX_ME_ROLE_UNDIFFERENTIATED
129 c.parent_meristem_id = m.meristem_id
130 c.spawned_at_us = now_us
131 c.committed_at_us = 0
132 c.lineage_depth = lineage_depth
133 m.n_cells = m.n_cells + 1
134 m.next_cell_id = m.next_cell_id + 1
135 return c.cell_id
136}
137
138func nx_me_find_cell(m: *NxMeristem, cell_id: nx_int) -> *NxCell {
139 if (m as i64) == 0 { return 0 as *NxCell }
140 var i: nx_int = 0
141 while i < m.n_cells {
142 let c: *NxCell = _me_cell_at(m, i)
143 if c.cell_id == cell_id { return c }
144 i = i + 1
145 }
146 return 0 as *NxCell
147}
148
149// ===== Commit cell to role ========================================
150
151func nx_me_differentiate(m: *NxMeristem,
152 cell_id: nx_int,
153 role: nx_int,
154 now_us: nx_size) -> nx_int {
155 if (m as i64) == 0 { return NX_ME_V_NULL }
156 if nx_me_role_is_valid(role) == 0 { return NX_ME_V_INVALID }
157 if role == NX_ME_ROLE_UNDIFFERENTIATED { return NX_ME_V_INVALID }
158 let c: *NxCell = nx_me_find_cell(m, cell_id)
159 if (c as i64) == 0 { return NX_ME_V_INVALID }
160 if c.role != NX_ME_ROLE_UNDIFFERENTIATED { return NX_ME_V_ALREADY_DIFFERENTIATED }
161 c.role = role
162 c.committed_at_us = now_us
163 return NX_ME_V_OK
164}
165
166// ===== Aggregations ===============================================
167
168func nx_me_count_by_role(m: *NxMeristem, role: nx_int) -> nx_int {
169 if (m as i64) == 0 { return 0 }
170 var count: nx_int = 0
171 var i: nx_int = 0
172 while i < m.n_cells {
173 let c: *NxCell = _me_cell_at(m, i)
174 if c.role == role { count = count + 1 }
175 i = i + 1
176 }
177 return count
178}
179
180func nx_me_undifferentiated_count(m: *NxMeristem) -> nx_int {
181 return nx_me_count_by_role(m, NX_ME_ROLE_UNDIFFERENTIATED)
182}
183
184func nx_me_max_lineage_depth(m: *NxMeristem) -> nx_int {
185 if (m as i64) == 0 { return 0 }
186 var max_d: nx_int = 0
187 var i: nx_int = 0
188 while i < m.n_cells {
189 let c: *NxCell = _me_cell_at(m, i)
190 if c.lineage_depth > max_d { max_d = c.lineage_depth }
191 i = i + 1
192 }
193 return max_d
194}