code wiki / (root) / nx_substrate_compose_test.nx

nx_substrate_compose_test.nx source

↩ module page · 204 lines · 8364 B

1// nx_substrate_compose_test.nx -- end-to-end compose demo. 2// 3// Exercises ALL 11 optimization-layer primitives together to verify 4// they compose. Per cardinal real-playtest-loop, the per-primitive 5// smokes don't catch composition bugs (one primitive can pass its 6// own test while violating an invariant another primitive depends 7// on). This smoke is the substrate's whole-cloth verification. 8// 9// Scenario: an "iridium voxel world" cell hosts a 32x32x32 tissue 10// at 3 bits per voxel (12 KiB = ESP32-feasible). The cell declares: 11// - foreground game attention class (frame budget pressure) 12// - LOCAL_GPU tropism preference 13// - PTX ribosome target 14// - 16 KiB RAM ceiling (tissue + some scratch) 15// 16// We then simulate the lifecycle: 17// 1. Construct the tissue, fill it half-air half-stone 18// 2. Wrap the cell in a pathway 19// 3. Profile a "raycast" call site through metabolism 20// 4. Apply budget pressure 21// 5. Ask homeostasis for a signal 22// 6. Resolve tropism for the signal 23// 7. Pick ribosome target for the destination tier 24// 8. Log eviction journal entry for the migration 25// 26// Every step asserts the expected outcome from a foreground-game cell 27// under realistic resource pressure. 28 29import "nx_syscalls.nx" 30import "nx_tier.nx" 31import "nx_budget.nx" 32import "nx_attention_class.nx" 33import "nx_yield.nx" 34import "nx_evict_journal.nx" 35import "nx_metabolism.nx" 36import "nx_homeostasis.nx" 37import "nx_palette.nx" 38import "nx_tropism.nx" 39import "nx_ribosome.nx" 40import "nx_pathway.nx" 41import "nx_tissue.nx" 42 43// Helper -- materialize the cell-spec on the heap. 44func _spec(cell_id: nx_int, ac: nx_int, tr: nx_int, rb: nx_int, 45 b: *NxBudget, site: nx_int, tier: nx_int) -> *NxCellSpec { 46 let s: *NxCellSpec = (sys_mmap(64)) as *NxCellSpec 47 s.cell_id = cell_id 48 s.attention_class = ac 49 s.tropism_prefer = tr 50 s.ribosome_target = rb 51 s.budget = b 52 s.site_id_hint = site 53 s.initial_tier = tier 54 return s 55} 56 57func main() -> i64 { 58 // ========= 1. Construct the tissue (32x32x32 @ 3bpp = 12 KiB) ===== 59 let tissue: *NxTissue = nx_tissue_new(32, 32, 32, NX_BPP_3) 60 if (tissue as i64) == 0 { return 1 } 61 if nx_tissue_storage_bytes(tissue) != 12288 { return 2 } 62 63 // Fill bottom half (z < 16) with stone (idx 1), leave top half air. 64 var z: nx_size = 0 65 while z < 16 { 66 var y: nx_size = 0 67 while y < 32 { 68 var x: nx_size = 0 69 while x < 32 { 70 nx_tissue_set(tissue, x, y, z, 1) 71 x = x + 1 72 } 73 y = y + 1 74 } 75 z = z + 1 76 } 77 // 16*32*32 = 16384 voxels stone 78 if nx_tissue_count_nonzero(tissue) != 16384 { return 3 } 79 80 // ========= 2. Cell budget: 16 KiB RAM, no VRAM disk net =========== 81 // cell_id=7777, ram_max=16384, vram_max=0, cpu_us_max=16000, disk_max=0, net_bps_max=0 82 let budget: *NxBudget = nx_budget_new(7777, 83 16384, 0, 84 16000, 0, 0) 85 86 // Account for tissue storage in the budget (12288 bytes). 87 if nx_budget_request(budget, NX_RES_RAM, 12288) != NX_BUDGET_OK { return 4 } 88 if nx_budget_remaining(budget, NX_RES_RAM) != 4096 { return 5 } // 16K - 12K 89 90 // ========= 3. Wrap cell in a pathway ============================== 91 let pw: *NxPathway = nx_pathway_new(4, 8) 92 // cell_id=7777, attention=FOREGROUND_GAME, tropism=LOCAL_GPU, ribosome=PTX, 93 // budget=budget, site_id=4242, tier=WORKSTATION 94 let spec: *NxCellSpec = _spec(7777, 95 NX_AC_INTERACTIVE_FOREGROUND_GAME, 96 NX_TROP_LOCAL_GPU, 97 NX_RBT_PTX, budget, 98 4242, 99 NX_TIER_WORKSTATION) 100 if nx_pathway_add_cell(pw, spec) != NX_PW_OK { return 6 } 101 102 let total_ram: nx_size = nx_pathway_aggregate_ram_max(pw) 103 if total_ram != 16384 { return 7 } 104 105 // ========= 4. Profile a "raycast" call site over many hits ======= 106 let metab: *NxMetabolism = nx_metab_new(16) 107 var i: nx_int = 0 108 while i < 5000 { // crosses VECTOR threshold -> SERVER suggested 109 // site=4242, cycles=200, ram=256, attention=FOREGROUND_GAME 110 nx_metab_record(metab, 4242, 200, 256, 111 NX_AC_INTERACTIVE_FOREGROUND_GAME) 112 i = i + 1 113 } 114 if nx_metab_hit_count(metab, 4242) != 5000 { return 8 } 115 if nx_metab_avg_cycles(metab, 4242) != 200 { return 9 } 116 if nx_metab_suggest_target(metab, 4242) != NX_TIER_SERVER { return 10 } 117 118 // ========= 5. Apply CPU pressure -> homeostasis fires THERMAL ===== 119 nx_budget_request(budget, NX_RES_CPU, 15500) // 96.875% -> over 90% 120 let cpu_q: nx_int = nx_budget_pressure_q10(budget, NX_RES_CPU) 121 if cpu_q < 921 { return 11 } // confirm we're above threshold 122 123 let journal: *NxEvictJournal = nx_evict_journal_new(8) 124 let sig: nx_int = nx_homeo_check(budget, journal, metab, 7777, 4242) 125 if sig != NX_HOMEO_MIGRATE_THERMAL { return 12 } 126 127 // ========= 6. Resolve tropism for the THERMAL signal ============== 128 let target_trop: nx_int = nx_tropism_resolve(sig, 129 NX_TROP_LOCAL_CPU, 130 NX_AC_INTERACTIVE_FOREGROUND_GAME, 131 NX_TROP_LOCAL_GPU) 132 // Foreground thermal -> LOCAL_GPU 133 if target_trop != NX_TROP_LOCAL_GPU { return 13 } 134 if nx_tropism_is_local(target_trop) != 1 { return 14 } 135 136 // ========= 7. Pick ribosome target for the destination ============ 137 // Foreground stays on workstation tier (where the GPU lives), but 138 // metabolism suggests promote-to-SERVER for this hot site. Picker 139 // honors the explicit pathway preference (PTX, GPU family). 140 let rb_target: nx_int = nx_pathway_resolve_target(pw, 7777, metab) 141 if rb_target != NX_RBT_PTX { return 15 } 142 if nx_rbt_is_gpu(rb_target) != 1 { return 16 } 143 144 // ========= 8. Log the migration into the eviction journal ======== 145 let now_us: nx_size = 100000 146 // cell_id=7777, reason=MIGRATED, resource=CPU, class=FOREGROUND_GAME, displaced_by=0 147 let logrc: nx_int = nx_evict_log(journal, now_us, 148 7777, 149 NX_EVR_MIGRATED, 150 NX_RES_CPU, 151 NX_AC_INTERACTIVE_FOREGROUND_GAME, 152 0) 153 if logrc != 0 { return 17 } 154 if journal.count != 1 { return 18 } 155 156 let entry: *NxEvictEntry = nx_evict_at(journal, 0) 157 if entry.cell_id != 7777 { return 19 } 158 if entry.reason != NX_EVR_MIGRATED { return 20 } 159 if entry.resource_kind != NX_RES_CPU { return 21 } 160 161 // ========= 9. After migration, simulate budget release ============ 162 nx_budget_release(budget, NX_RES_CPU, 15500) 163 if nx_budget_pressure_q10(budget, NX_RES_CPU) != 0 { return 22 } 164 165 // Homeostasis should now report STEADY. 166 let sig2: nx_int = nx_homeo_check(budget, journal, metab, 7777, 4242) 167 // metabolism still suggests promote, so signal could be PROMOTE_TIER 168 // not STEADY -- accept either (composition allows both depending on 169 // whether metabolism heat is checked after pressure clears). 170 if sig2 != NX_HOMEO_STEADY { 171 if sig2 != NX_HOMEO_PROMOTE_TIER { return 23 } 172 } 173 174 // ========= 10. Yield contract -- foreground holder, background asks == 175 let now_yield: nx_size = 0 176 // cell_id=7777, attention=FOREGROUND_GAME, quantum=16000us, now=now_yield 177 let holder: *NxYieldContract = nx_yield_contract_new( 178 7777, 179 NX_AC_INTERACTIVE_FOREGROUND_GAME, 180 16000, now_yield) 181 // Background inference requests -> DENIED (foreground out-ranks). 182 let v: nx_int = nx_yield_request(holder, 183 NX_AC_BACKGROUND_INFERENCE, 1000) 184 if v != NX_YIELD_DENIED { return 24 } 185 186 // ========= 11. Tissue + palette: write voxel, read back =========== 187 // After all the resource arbitration, the actual gameplay still 188 // works: tissue stays consistent. 189 nx_tissue_set(tissue, 5, 5, 20, 7) 190 if nx_tissue_get(tissue, 5, 5, 20) != 7 { return 25 } 191 // Untouched region stays as filled (1 below z=16, 0 above) 192 if nx_tissue_get(tissue, 5, 5, 5) != 1 { return 26 } 193 if nx_tissue_get(tissue, 5, 5, 18) != 0 { return 27 } 194 195 // ========= 12. Demo summary: assert the cell stayed alive ========= 196 // Budget RAM still has the tissue allocation, attention class is 197 // intact, pathway still holds the cell. 198 if budget.ram_used != 12288 { return 28 } 199 let cell: *NxCellSpec = nx_pathway_get_cell(pw, 7777) 200 if (cell as i64) == 0 { return 29 } 201 if cell.attention_class != NX_AC_INTERACTIVE_FOREGROUND_GAME { return 30 } 202 203 return 0 204}