diff --git a/apps/evo-server/main.cpp b/apps/evo-server/main.cpp index baf5d88..4863833 100644 --- a/apps/evo-server/main.cpp +++ b/apps/evo-server/main.cpp @@ -940,7 +940,7 @@ int serve_cellular(Options& o) { // head per epoch (rules-12 regions commit every flush interval, not every tick). std::optional batch_builder; std::vector batch_actions; - std::uint64_t pace_epoch = 0, pace_tick = 0; + std::uint64_t pace_epoch = 0, pace_tick = 0, flushes_logged = 0; std::vector awaiting_outcomes; // A tick that cannot be built is retried every 50 ms; name each distinct cause once. std::optional> stalled; @@ -1120,8 +1120,17 @@ int serve_cellular(Options& o) { batch_builder.reset(); return; } + const auto flush_start = std::chrono::steady_clock::now(); + const auto flush_ticks_held = batch_builder->batch().ticks.size(); auto published = host.publish(batch_builder->batch(), batch_builder->actions(), batch_builder->mutation_actions()); + // Operator evidence for commit stalls: every slow commit, and one in 60 otherwise. + const auto flush_ms = std::chrono::duration_cast( + std::chrono::steady_clock::now() - flush_start) + .count(); + if (flush_ms > 50 || ++flushes_logged % 60 == 0) + std::printf("cellular commit ticks=%zu ms=%lld ok=%d\n", flush_ticks_held, + static_cast(flush_ms), published ? 1 : 0); if (published) host.refuse_births(batch_builder->refused_births()); if (!published && host.status().phase != server::HostPhase::resolving) { diff --git a/deploy/kubernetes/region/dev-session-route/listener.yaml b/deploy/kubernetes/region/dev-session-route/listener.yaml index 8392319..80516cc 100644 --- a/deploy/kubernetes/region/dev-session-route/listener.yaml +++ b/deploy/kubernetes/region/dev-session-route/listener.yaml @@ -12,7 +12,7 @@ spec: env: - {name: REGION_SESSION, value: "dev"} - {name: REGION_SESSION_LISTEN, value: "0.0.0.0:7400"} - - {name: REGION_SESSION_WINDOW, value: "dev-20261022T024258-2c04acc3:1792651378"} + - {name: REGION_SESSION_WINDOW, value: "dev-20261022T104000-45da298d:1792680000"} - {name: REGION_M10_MAX_GRANT_LIFETIME, value: "14400"} ports: # QUIC (ADR-0090): UDP only; nothing listens on TCP 7400. diff --git a/deploy/kubernetes/region/dev/tuning/cellular.tuning b/deploy/kubernetes/region/dev/tuning/cellular.tuning index 5662397..8912e93 100644 --- a/deploy/kubernetes/region/dev/tuning/cellular.tuning +++ b/deploy/kubernetes/region/dev/tuning/cellular.tuning @@ -20,6 +20,7 @@ founder.reverse_thrust_um_per_s2 = 400 # braking and reversing founder.drag_ppm_per_tick = 95_163 # k = 2 /s: 0.5 s time constant founder.lateral_drag_ppm_per_tick = 95_163 founder.turn_heading_per_s = 65_536 # 65,536 = one full turn per second +founder.thrust_cost_per_mille = 250 # thrust energy: a quarter of the full-speed metabolic cost # Currents: 0.3x-1.5x founder speed, so the strongest cannot be swum against. currents.min_count = 2 diff --git a/include/evo/biology/cellular_tuning.hpp b/include/evo/biology/cellular_tuning.hpp index 65cf3f6..549ea74 100644 --- a/include/evo/biology/cellular_tuning.hpp +++ b/include/evo/biology/cellular_tuning.hpp @@ -27,6 +27,8 @@ struct CellularTuning { std::int64_t founder_thrust_um_per_s2{}, founder_reverse_thrust_um_per_s2{}; std::int64_t founder_drag_ppm_per_tick{}, founder_lateral_drag_ppm_per_tick{}; std::int64_t founder_turn_heading_per_s{}; // 65,536 heading units per turn. + // Thrust energy as a share of the metabolic full-speed movement cost, per thousand. + std::int64_t founder_thrust_cost_per_mille{}; // Currents. std::int64_t currents_min_count{}, currents_max_count{}; std::int64_t currents_min_width_um{}, currents_max_width_um{}; diff --git a/include/evo/server/lifecycle/cellular_liquid.hpp b/include/evo/server/lifecycle/cellular_liquid.hpp index 7c9159d..fae5185 100644 --- a/include/evo/server/lifecycle/cellular_liquid.hpp +++ b/include/evo/server/lifecycle/cellular_liquid.hpp @@ -16,6 +16,8 @@ struct CellularLiquidRules { biology::CellularCurrentConfig currents{}; // Its area is the owner's area. biology::CellularHandling founder{}; // Per-tick founder handling; costs unused. biology::CellularDepositMotionRules deposits{}; + // Thrust energy as a share of the metabolic full-speed movement cost, 1..10,000 per mille. + std::uint32_t thrust_cost_per_mille{1'000}; auto operator<=>(const CellularLiquidRules&) const = default; }; diff --git a/schemas/cellular-lives-v10.md b/schemas/cellular-lives-v10.md index c680f92..7199ed6 100644 --- a/schemas/cellular-lives-v10.md +++ b/schemas/cellular-lives-v10.md @@ -11,7 +11,7 @@ record, length rule and validation is format 9's. | 364 | 4 each | Current width, height (the owner's area), grid pitch, minimum and maximum count, minimum and maximum route width (um), minimum and maximum speed (um/s) | | 400 | 4 each | Founder handling per 50 ms tick: thrust (Q10 um/s), reverse thrust (Q10 um/s), forward drag (ppm), lateral drag (ppm), turn (heading units, at most 32,768) | | 420 | 4 each | Deposit motion: drift radius (um), maximum drift (Q10 um/s), drift decay (ppm per tick), merged cap (at most 65,535), bubble radius (um) | -| 440 | 4 | Reserved zero | +| 440 | 4 | Thrust cost per mille of the metabolic full-speed movement cost (1..10,000); 1,000 is written as zero | | 444 | 8 each | Sorted spawn sites, then lives, corpses and the rest as in format 9 | Each life record appends its motion state after the offspring genome: velocity relative to diff --git a/src/biology/cellular_tuning.cpp b/src/biology/cellular_tuning.cpp index 55b4c05..9222e75 100644 --- a/src/biology/cellular_tuning.cpp +++ b/src/biology/cellular_tuning.cpp @@ -35,6 +35,8 @@ constexpr std::array fields{ 0, 999'999}, Field{"founder.turn_heading_per_s", &CellularTuning::founder_turn_heading_per_s, 0, 10 * std::int64_t{cellular_heading_turn}}, + Field{"founder.thrust_cost_per_mille", &CellularTuning::founder_thrust_cost_per_mille, 1, + 10'000}, Field{"currents.min_count", &CellularTuning::currents_min_count, 0, 16}, Field{"currents.max_count", &CellularTuning::currents_max_count, 0, 16}, Field{"currents.min_width_um", &CellularTuning::currents_min_width_um, 100, axis_limit}, diff --git a/src/server/lifecycle/cellular_liquid.cpp b/src/server/lifecycle/cellular_liquid.cpp index 4f9d210..9dbd625 100644 --- a/src/server/lifecycle/cellular_liquid.cpp +++ b/src/server/lifecycle/cellular_liquid.cpp @@ -17,7 +17,8 @@ bool cellular_liquid_valid(const CellularLiquidRules& rules, biology::CellularAr f.reverse_cost.value == 0 && d.merged_cap != 0 && d.bubble_radius_um != 0 && d.bubble_radius_um <= biology::max_cellular_deposit_radius_um && d.max_drift_q10 >= 0 && d.max_drift_q10 <= biology::cellular_velocity_limit_q10 && - d.drift_decay_ppm < biology::cellular_drag_ppm_limit; + d.drift_decay_ppm < biology::cellular_drag_ppm_limit && + rules.thrust_cost_per_mille != 0 && rules.thrust_cost_per_mille <= 10'000; } CellularLiquidRules cellular_liquid_rules(const biology::CellularTuning& t, @@ -36,12 +37,22 @@ CellularLiquidRules cellular_liquid_rules(const biology::CellularTuning& t, biology::cellular_velocity_q10_per_um_s), .drift_decay_ppm = u32(t.food_induced_drift_decay_ppm_per_tick), .merged_cap = static_cast(t.food_merged_bubble_cap_multiple), - .bubble_radius_um = biology::cellular_mote_radius_um}}; + .bubble_radius_um = biology::cellular_mote_radius_um}, + .thrust_cost_per_mille = u32(t.founder_thrust_cost_per_mille)}; } std::expected cellular_liquid_handling(const biology::Genome& genome, const biology::MetabolicBudget& budget, const CellularLiquidRules& rules) { - return biology::derive_cellular_handling(genome, budget, rules.founder); + auto handling = biology::derive_cellular_handling(genome, budget, rules.founder); + if (!handling) + return handling; + const auto scaled = [&rules](accounting::Picojoules cost) { + const auto product = cost.value * rules.thrust_cost_per_mille; + return accounting::Picojoules{product / 1'000 + (product % 1'000 != 0 ? 1 : 0)}; + }; + handling->thrust_cost = scaled(handling->thrust_cost); + handling->reverse_cost = scaled(handling->reverse_cost); + return handling; } } // namespace evo::server diff --git a/src/server/lifecycle/cellular_lives.cpp b/src/server/lifecycle/cellular_lives.cpp index 2c77f54..899178b 100644 --- a/src/server/lifecycle/cellular_lives.cpp +++ b/src/server/lifecycle/cellular_lives.cpp @@ -1,6 +1,8 @@ #include "evo/server/lifecycle/cellular_lives.hpp" + #include "evo/biology/budding.hpp" #include "evo/biology/founder.hpp" +#include "evo/core/random.hpp" #include #include @@ -1115,7 +1117,9 @@ std::vector CellularLives::snapshot() const { out.put(d.drift_decay_ppm, 4); out.put(d.merged_cap, 4); out.put(d.bubble_radius_um, 4); - out.put(0, 4); + // Thrust cost share; zero stands for the original 1,000 per mille. + const auto cost = state_->liquid.thrust_cost_per_mille; + out.put(cost == 1'000 ? 0 : cost, 4); } for (const auto site : state_->nursery_config.sites) out.site(site); @@ -1338,12 +1342,32 @@ CellularLives::birth_candidate(LifeRequest request, LifeAllocation allocation) c if (!budget || budget->body != endowed->body) return std::unexpected{CellularLivesError::biology}; - std::optional position; + // Rules 12: a new founder starts at a seeded random spot anywhere in the region (1 mm + // clear of its edges), so starts spread out; the provisioned sites remain the fallback. + // Seeded by world, region and the life's identity, so replay places it identically. + std::vector candidates; + if (state_->rules_version >= cellular_lives_liquid_rules) { + constexpr std::uint64_t margin = 1'000; + const auto& area = state_->config.area; + if (area.width_um > 2 * margin + budget->phenotype.extent && + area.height_um > 2 * margin + budget->phenotype.extent) { + Random random{ + mutation_seed(state_->world, state_->region, allocation.life, 0x5350574EU)}; + const auto span_x = area.width_um - 2 * margin - budget->phenotype.extent; + const auto span_y = area.height_um - 2 * margin - budget->phenotype.extent; + for (int attempt = 0; attempt < 16; ++attempt) + candidates.push_back( + {margin + random.next() % span_x, margin + random.next() % span_y}); + } + } for (const auto site : state_->nursery_config.sites) { if (site.x_um < 0 || site.y_um < 0) return std::unexpected{CellularLivesError::invalid_config}; - const biology::CellularPosition candidate{static_cast(site.x_um), - static_cast(site.y_um)}; + candidates.push_back( + {static_cast(site.x_um), static_cast(site.y_um)}); + } + std::optional position; + for (const auto candidate : candidates) { bool occupied = false; for (const auto& living : state_->lives) { if (living.presence == CellularPresence::suspended) @@ -1582,7 +1606,9 @@ CellularLives::restore(std::span bytes, std::uint64_t world, const auto cap = u32(); d.merged_cap = static_cast(cap); d.bubble_radius_um = u32(); - if (in.get(4) != 0 || turn > 0xFFFFU || cap > 0xFFFFU || + const auto cost = u32(); + liquid.thrust_cost_per_mille = cost == 0 ? 1'000 : cost; + if (cost == 1'000 || turn > 0xFFFFU || cap > 0xFFFFU || !cellular_liquid_valid(liquid, config.area)) return std::unexpected{CellularLivesError::format}; auto generated = biology::CellularFlowField::shared(liquid.currents); diff --git a/tests/cellular_liquid_owner_tests.cpp b/tests/cellular_liquid_owner_tests.cpp index 78281fe..ffaa3b0 100644 --- a/tests/cellular_liquid_owner_tests.cpp +++ b/tests/cellular_liquid_owner_tests.cpp @@ -2,6 +2,7 @@ #include "fixtures/cellular_ecology.hpp" #include +#include #include #include @@ -166,6 +167,44 @@ void trace_replays_brakes() { require(replayed->snapshot() == live->snapshot(), "replay diverged from the live run"); } +// Founders start at a seeded random spot, not a provisioned site, identically on replay. +void founders_spawn_at_random() { + const auto state = liquid_region(); + const auto first = state->lives().front().position; + const auto sites = std::array{1'000ULL, 4'000ULL}; + require(!(std::ranges::find(sites, first.x_um) != sites.end() && + std::ranges::find(sites, first.y_um) != sites.end()), + "a rules-12 founder started on a provisioned site"); + require(first.x_um >= 1'000 && first.y_um >= 1'000 && first.x_um < 47'000 && + first.y_um < 47'000, + "a founder started too close to the region edge"); + // The same identity starts at the same spot (the fixture funds one founder). + auto again = liquid_region(); + require(again->lives().front().position == first, "spawning is not deterministic"); +} + +// The declared thrust cost share scales every thrust and braking cost. +void thrust_cost_is_tunable() { + const auto state = liquid_region(); + const auto& life = state->lives().front(); + const auto genome = state->archive().find(life.identity.genome); + require(genome.has_value(), "founder genome missing"); + const auto budget = biology::metabolic_budget(*genome, life.growth, {293'150, {}}); + require(budget.has_value(), "founder budget"); + auto full = rules(); + auto quarter = rules(); + quarter.thrust_cost_per_mille = 250; + const auto a = checked(cellular_liquid_handling(*genome, *budget, full), "full cost"); + const auto b = checked(cellular_liquid_handling(*genome, *budget, quarter), "quarter cost"); + require(a.thrust_cost.value > 0 && + b.thrust_cost.value == (a.thrust_cost.value * 250 + 999) / 1'000 && + b.thrust_q10 == a.thrust_q10, + "thrust cost did not scale"); + auto zero = rules(); + zero.thrust_cost_per_mille = 0; + require(!cellular_liquid_valid(zero, area), "a zero thrust cost share was accepted"); +} + void older_rules_refuse_brake() { auto state = opening(7, true, 1); require(!state->advance_tick(swim(*state, {}, true)), "rules 8 accepted a brake"); @@ -179,6 +218,8 @@ int main() { food_moves_and_merges_without_loss(); restore_continues_exactly(); trace_replays_brakes(); + founders_spawn_at_random(); + thrust_cost_is_tunable(); older_rules_refuse_brake(); } catch (const std::exception& error) { std::fprintf(stderr, "%s\n", error.what()); diff --git a/tests/cellular_tuning_tests.cpp b/tests/cellular_tuning_tests.cpp index 9762e8c..7625d6f 100644 --- a/tests/cellular_tuning_tests.cpp +++ b/tests/cellular_tuning_tests.cpp @@ -29,6 +29,7 @@ CellularTuning v0() { .founder_drag_ppm_per_tick = 95'163, .founder_lateral_drag_ppm_per_tick = 95'163, .founder_turn_heading_per_s = 65'536, + .founder_thrust_cost_per_mille = 250, .currents_min_count = 2, .currents_max_count = 4, .currents_min_width_um = 3'000, @@ -114,7 +115,7 @@ void refusals() { CHECK(!parsed && parsed.error().kind == CellularTuningErrorKind::missing_key && parsed.error().key == "mutation.offer_options"); const auto line = parse_cellular_tuning(base + "\nbad\n"); - CHECK(!line && line.error().line == 41); + CHECK(!line && line.error().line == 42); CHECK(error_of(std::string(max_cellular_tuning_bytes + 1, '#')) == CellularTuningErrorKind::too_large); } diff --git a/tests/region_store_tests.cpp b/tests/region_store_tests.cpp index e5d6194..774939e 100644 --- a/tests/region_store_tests.cpp +++ b/tests/region_store_tests.cpp @@ -769,6 +769,7 @@ void cellular_v9_provisioning_declares_liquid() { "region.perception_radius_um = 5000\nfounder.thrust_um_per_s2 = 400\n" "founder.reverse_thrust_um_per_s2 = 400\nfounder.drag_ppm_per_tick = 95163\n" "founder.lateral_drag_ppm_per_tick = 95163\nfounder.turn_heading_per_s = 65536\n" + "founder.thrust_cost_per_mille = 250\n" "currents.min_count = 2\ncurrents.max_count = 4\ncurrents.min_width_um = 3000\n" "currents.max_width_um = 6000\ncurrents.min_speed_um_per_s = 60\n" "currents.max_speed_um_per_s = 300\ncurrents.grid_pitch_um = 250\n"