nx_ecosystem_compose_test.nx source
↩ module page · 195 lines · 8818 B
1// nx_ecosystem_compose_test.nx -- silicon up to Steam partnership.
2//
3// Per user 2026-05-19: "think of it like playing nms via steam, where
4// can we build from the silicon up to get the best experience on the
5// nishi ecosystem and then as it moves off our ecosystem into steam
6// partnering with steam to improve things like vram usage, disk space
7// usage."
8//
9// The concrete scenario this smoke validates:
10//
11// HOST: laptop with 16 GB RAM + 12 GB VRAM (RTX 4080-class)
12//
13// NATIVE NISHI ECOSYSTEM:
14// - voxel-world cell (foreground game) -- 4 GB RAM, 2 GB VRAM
15// - reader-audio cell (background) -- 2 GB RAM, 1 GB VRAM
16// - image-grader cell (background) -- 1 GB RAM, 1 GB VRAM
17//
18// FOREIGN SYMBIOTE:
19// - Steam-hosted NMS (foreground game) -- 8 GB RAM, 6 GB VRAM
20// declared; observed
21// varies as user plays
22//
23// TOTAL DECLARED: 15 GB RAM, 10 GB VRAM -- fits in host with slack
24//
25// Lifecycle simulated by the smoke:
26// 1. Build the ecosystem: organism holds the pathway + symbiote
27// 2. Propose treaty: NMS-Steam + voxel-world share VRAM 60/40
28// 3. NMS pushes VRAM observed to 7 GB (within share)
29// 4. NMS overshoots to 9 GB -> treaty BREACHED
30// 5. Renegotiate to 50/50; NMS comes back to 5.5 GB
31// 6. Host pressure low again; ecosystem steady
32// 7. Spawn an opportunistic background cell, force host pressure
33// to 95%, hypothalamus (organism arbiter) picks idle cell to
34// displace (NOT the foreground NMS, NOT the foreground voxel)
35// 8. Idle cell yields; pressure returns to steady
36//
37// This proves the WHOLE optimization stack runs across native +
38// foreign in the actual NMS-via-Steam user scenario.
39
40import "nx_syscalls.nx"
41import "nx_tier.nx"
42import "nx_budget.nx"
43import "nx_attention_class.nx"
44import "nx_yield.nx"
45import "nx_evict_journal.nx"
46import "nx_metabolism.nx"
47import "nx_homeostasis.nx"
48import "nx_palette.nx"
49import "nx_tropism.nx"
50import "nx_ribosome.nx"
51import "nx_pathway.nx"
52import "nx_tissue.nx"
53import "nx_symbiote.nx"
54import "nx_organism.nx"
55import "nx_treaty.nx"
56
57func _add_cell(p: *NxPathway, cell_id: nx_int, ac: nx_int,
58 ram_max: nx_size, vram_max: nx_size,
59 tier: nx_int) -> nx_int {
60 let b: *NxBudget = nx_budget_new(cell_id, ram_max, vram_max, 0, 0, 0)
61 let s: *NxCellSpec = (sys_mmap(64)) as *NxCellSpec
62 s.cell_id = cell_id
63 s.attention_class = ac
64 s.tropism_prefer = NX_TROP_LOCAL_GPU
65 s.ribosome_target = NX_RBT_PTX
66 s.budget = b
67 s.site_id_hint = 0
68 s.initial_tier = tier
69 return nx_pathway_add_cell(p, s)
70}
71
72func main() -> i64 {
73 // ===== Step 1: build the ecosystem =================================
74 // Host: 16 GB RAM, 12 GB VRAM
75 let host: *NxBudget = nx_budget_new(0,
76 16000000000, 12000000000, 0, 500000000000, 0)
77 let journal: *NxEvictJournal = nx_evict_journal_new(32)
78 let org: *NxOrganism = nx_organism_new(host, 4, 4, journal)
79
80 // Native pathway: voxel + reader + grader cells
81 let nishi: *NxPathway = nx_pathway_new(8, 16)
82 if _add_cell(nishi, 100, NX_AC_INTERACTIVE_FOREGROUND_GAME,
83 4000000000, 2000000000, NX_TIER_WORKSTATION) != NX_PW_OK { return 1 }
84 if _add_cell(nishi, 101, NX_AC_BACKGROUND_INFERENCE,
85 2000000000, 1000000000, NX_TIER_WORKSTATION) != NX_PW_OK { return 2 }
86 if _add_cell(nishi, 102, NX_AC_BACKGROUND_BUILD,
87 1000000000, 1000000000, NX_TIER_WORKSTATION) != NX_PW_OK { return 3 }
88 if nx_organism_add_pathway(org, nishi) != NX_ORG_OK { return 4 }
89
90 // Steam-NMS symbiote: pid 12345, 8 GB RAM 6 GB VRAM declared
91 let nms_budget: *NxBudget = nx_budget_new(12345,
92 8000000000, 6000000000, 0, 80000000000, 0)
93 let nms: *NxSymbiote = nx_symbiote_new(12345,
94 (0 as i64) as *u8,
95 NX_AC_INTERACTIVE_FOREGROUND_GAME, nms_budget, 0)
96 if nx_organism_add_symbiote(org, nms) != NX_ORG_OK { return 5 }
97
98 // ===== Step 2: aggregate budgets fit the host ======================
99 if nx_organism_total_ram_max(org) != 15000000000 { return 6 } // 7G nishi + 8G NMS
100 if nx_organism_total_vram_max(org) != 10000000000 { return 7 } // 4G nishi + 6G NMS
101
102 // ===== Step 3: propose VRAM treaty (NMS 60% / voxel 40%) ==========
103 // Both are foreground class -- valid (no priority inversion).
104 // tid=1, a_id=12345 (NMS pid, FOREGROUND_GAME), b_id=100 (voxel, FOREGROUND_GAME),
105 // ram_share_a=512 (50/50), vram_share_a=614 (60/40), now=1000
106 let treaty: *NxTreaty = nx_treaty_propose(1,
107 12345, NX_AC_INTERACTIVE_FOREGROUND_GAME,
108 100, NX_AC_INTERACTIVE_FOREGROUND_GAME,
109 512, 614, 1000)
110 if (treaty as i64) == 0 { return 8 }
111 nx_treaty_accept(treaty, 1100)
112 if nx_treaty_is_active(treaty) != 1 { return 9 }
113
114 // ===== Step 4: NMS within-share consumption -- no breach ==========
115 // Pool = host VRAM = 12 GB. NMS gets 60% = 7.2 GB. NMS observed 7 GB.
116 nx_symbiote_update(nms, 5500000000, 7000000000, 0, 0, 2000)
117 let breach1: nx_int = nx_treaty_check_breach(treaty,
118 12000000000, 7000000000, 2000000000,
119 NX_RES_VRAM, 2100)
120 if breach1 != 0 { return 10 }
121 if nx_treaty_is_active(treaty) != 1 { return 11 }
122
123 // ===== Step 5: NMS overshoots -- treaty BREACHED ==================
124 nx_symbiote_update(nms, 5500000000, 9000000000, 0, 0, 3000)
125 let breach2: nx_int = nx_treaty_check_breach(treaty,
126 12000000000, 9000000000, 2000000000,
127 NX_RES_VRAM, 3100)
128 if breach2 != 1 { return 12 }
129 if treaty.state != NX_TR_BREACHED { return 13 }
130 if treaty.breach_count != 1 { return 14 }
131
132 // ===== Step 6: renegotiate to 50/50; both come back to share ======
133 nx_treaty_renegotiate_shares(treaty, 512, 512, 4000)
134 if treaty.state != NX_TR_PROPOSED { return 15 }
135 nx_treaty_accept(treaty, 4100)
136 if nx_treaty_is_active(treaty) != 1 { return 16 }
137
138 // NMS texture quality dropped from Ultra to High; observed 5.5 GB
139 nx_symbiote_update(nms, 5500000000, 5500000000, 0, 0, 5000)
140 let breach3: nx_int = nx_treaty_check_breach(treaty,
141 12000000000, 5500000000, 4000000000,
142 NX_RES_VRAM, 5100)
143 if breach3 != 0 { return 17 }
144
145 // ===== Step 7: spawn opportunistic idle cell; force host pressure ==
146 // After NMS comes back, all is well. Now an additional cell spawns
147 // and starts hot on the GPU. Together they push host VRAM past 90%.
148 if _add_cell(nishi, 103, NX_AC_IDLE_OPPORTUNISTIC,
149 1000000000, 4000000000, NX_TIER_WORKSTATION) != NX_PW_OK { return 18 }
150 // Now declared VRAM aggregate = 4G voxel + 1G inf + 1G grader + 4G idle + 6G NMS = 16G
151 if nx_organism_total_vram_max(org) != 14000000000 { return 19 }
152
153 // Observed VRAM: pathway sees aggregate max (2+1+1+4 = 8 GB native)
154 // + NMS observed 5.5 GB = 13.5 GB observed (> 12 GB host -> overcommit)
155 let vram_obs: nx_size = nx_organism_total_observed_vram(org)
156 if vram_obs < 13000000000 { return 20 }
157
158 let host_q: nx_int = nx_organism_host_pressure_q10(org, NX_RES_VRAM)
159 if host_q < 921 { return 21 } // > 90% pressure
160
161 // ===== Step 8: hypothalamus picks idle cell for displacement =======
162 let out_id: *i64 = (sys_mmap(8)) as *i64
163 out_id[0] = 0
164 let pick: nx_int = nx_organism_pick_displacement(org, NX_RES_VRAM, out_id)
165 if pick != NX_DISP_NATIVE_CELL { return 22 }
166 if out_id[0] != 103 { return 23 } // idle cell, NOT NMS, NOT voxel
167
168 // ===== Step 9: log the displacement ===============================
169 nx_evict_log(journal, 6000, 103,
170 NX_EVR_MIGRATED, NX_RES_VRAM,
171 NX_AC_IDLE_OPPORTUNISTIC, 100)
172 if journal.count != 1 { return 24 }
173 let e: *NxEvictEntry = nx_evict_at(journal, 0)
174 if e.cell_id != 103 { return 25 }
175 if e.attention_class != NX_AC_IDLE_OPPORTUNISTIC { return 26 }
176
177 // ===== Step 10: confirm NMS is responsive (we did NOT throttle it) ==
178 if nx_symbiote_is_responsive(nms) != 1 { return 27 }
179 // And the voxel-world cell's data is untouched (no migration of
180 // foreground game cell)
181 let voxel_cell: *NxCellSpec = nx_pathway_get_cell(nishi, 100)
182 if (voxel_cell as i64) == 0 { return 28 }
183 if voxel_cell.attention_class != NX_AC_INTERACTIVE_FOREGROUND_GAME { return 29 }
184
185 // ===== Step 11: pathway-wide remote count -- nothing remote yet ====
186 if nx_pathway_count_remote_cells(nishi) != 0 { return 30 }
187
188 // ===== Step 12: organism still has 4 members (3 cells + 1 symbiote) =
189 // 1 pathway + 1 symbiote = 2 top-level members; the pathway
190 // happens to have 4 cells inside but member count is at the
191 // pathway/symbiote granularity.
192 if nx_organism_count_members(org) != 2 { return 31 }
193
194 return 0
195}