diff --git a/apps/evo-server/cellular_bridge.hpp b/apps/evo-server/cellular_bridge.hpp index 54f5156..4a5d430 100644 --- a/apps/evo-server/cellular_bridge.hpp +++ b/apps/evo-server/cellular_bridge.hpp @@ -161,7 +161,12 @@ class CellularBridge { static_cast(event.session), event.account.c_str(), static_cast(event.reason)); } - void replicate(const server::CellularLives& state) { + // `state` is what players see (the session batch's working state between commits); + // mutation offers and life endings come only from `committed`, so a player can act on + // them durably. Without `committed`, both are the same state. + void replicate(const server::CellularLives& state, + const server::CellularLives* committed = nullptr) { + const auto& authority = committed ? *committed : state; const auto tick = state.tick(); if (replicated_tick_ == tick) return; @@ -418,15 +423,20 @@ class CellularBridge { &network::CellularPerceivedProducer::organism); } (void)sessions_->send_cellular_perception(network::AccountId{life.account}, view); - send_offer(state, life, tick); + const auto owned = std::ranges::find( + authority.lives(), life.identity.life, + [](const server::CellularOwnedLife& value) { return value.identity.life; }); + if (owned != authority.lives().end()) + send_offer(authority, *owned, authority.tick()); } // After every owner's view: clear only the sessions that got none this tick. sessions_->clear_cellular_perceptions(tick); std::erase_if(offers_, [&](const SentOffer& sent) { - return std::ranges::find(state.lives(), sent.account, - &server::CellularOwnedLife::account) == state.lives().end(); + return std::ranges::find(authority.lives(), sent.account, + &server::CellularOwnedLife::account) == + authority.lives().end(); }); - announce_endings(state); + announce_endings(authority); // What each deposit held when last seen, for next tick's `drained` flags. deposit_stock_ = std::move(seen_stock_); seen_stock_.clear(); diff --git a/apps/evo-server/main.cpp b/apps/evo-server/main.cpp index 04fdd29..491f805 100644 --- a/apps/evo-server/main.cpp +++ b/apps/evo-server/main.cpp @@ -936,6 +936,11 @@ int serve_cellular(Options& o) { bool ready = false; std::optional phase; server::CellularTickSchedule schedule; + // The open session batch between commits, the actions it carries, and the monotone pace + // 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::vector awaiting_outcomes; // A tick that cannot be built is retried every 50 ms; name each distinct cause once. std::optional> stalled; @@ -1086,13 +1091,56 @@ int serve_cellular(Options& o) { std::chrono::steady_clock::now().time_since_epoch()) .count()); }; - const auto paced = schedule.observe(monotonic_us(), status.epoch, - state ? state->tick() : status.committed_tick, needed, + // Pacing follows the simulated tick (the open batch's working state), never backwards + // within an epoch: a discarded batch must not look like a head regression. + const auto simulated_tick = batch_builder ? batch_builder->candidate().tick() + : state ? state->tick() + : status.committed_tick; + if (status.epoch != pace_epoch) { + pace_epoch = status.epoch; + pace_tick = simulated_tick; + } + pace_tick = std::max(pace_tick, simulated_tick); + const auto paced = schedule.observe(monotonic_us(), status.epoch, pace_tick, needed, status.ready && state, status.ecological_pending_ticks); if (!paced) { std::puts("error cellular-schedule"); return finish(4); } + const auto flush_ticks = std::clamp( + static_cast(o.host.flush_interval.count()) / 50, 1, 60); + const auto flush = [&] { + if (!batch_builder || batch_builder->batch().ticks.empty()) { + batch_builder.reset(); + return; + } + auto published = host.publish(batch_builder->batch(), batch_builder->actions(), + batch_builder->mutation_actions()); + if (published) + host.refuse_births(batch_builder->refused_births()); + if (!published && host.status().phase != server::HostPhase::resolving) { + bridge->dropped(batch_actions, true); + std::erase_if(awaiting_outcomes, [&](const auto& action) { + return std::ranges::find(batch_actions, action) != batch_actions.end(); + }); + } + batch_builder.reset(); + batch_actions.clear(); + deliver_finalized(); + state = host.state(); // Publication replaced the committed state. + }; + // A batch cannot outlive the authority or the raw mode it was built under; when demand + // ends, what it holds is committed. + if (batch_builder && (!status.ready || !state || host.population_mode())) { + bridge->dropped(batch_actions, true); + std::erase_if(awaiting_outcomes, [&](const auto& action) { + return std::ranges::find(batch_actions, action) != batch_actions.end(); + }); + batch_builder.reset(); + batch_actions.clear(); + } else if (batch_builder && !needed) { + flush(); + } if (status.ready && state && (needed || host.population_mode())) { if (host.population_mode()) { if (paced->retry_ready && (paced->due || status.ecological_pending_ticks != 0)) { @@ -1100,103 +1148,123 @@ int serve_cellular(Options& o) { if (!published) schedule.defer(monotonic_us()); } - } else if (paced->due && state->tick() != UINT64_MAX) { - const auto tick = state->tick() + 1; - std::vector baseline; - baseline.reserve(state->lives().size()); - // ADR-0128: an owned life whose account holds no session here is frozen - // (rules 5); the trace records the boundary, and its carried actions are - // refused below. - const bool presence_rules = - state->rules_version() >= server::cellular_lives_presence_rules; - std::vector offline; - for (const auto& life : state->lives()) { - server::CellularLifeIntent intent{life.identity.life, {}}; - network::AccountId account; - account.bytes = life.account; - if (presence_rules && life.account != server::LifeAccount{} && - !bridge->sessions().holder(account)) { - intent.intent.frozen = true; - offline.push_back(life.account); + } else { + // Rules 12 regions hold thousands of deposits, so their committed payload is + // large: commit every flush interval (ADR-0089: 1 s), not every tick. Players + // see the batch's working state at once; outcomes become final at its commit. + // Births and mutation choices commit promptly, in their own fresh batch. + if (batch_builder && (!held_births.empty() || !host.queued_mutations().empty())) + flush(); + const auto* current = batch_builder ? &batch_builder->candidate() : state; + // A failed commit can leave the host without state until it recovers. + if (current && host.committed() && paced->due && needed && + current->tick() != UINT64_MAX) { + const auto tick = current->tick() + 1; + std::vector baseline; + baseline.reserve(current->lives().size()); + // ADR-0128: an owned life whose account holds no session here is frozen + // (rules 5); the trace records the boundary, and its carried actions are + // refused below. + const bool presence_rules = + current->rules_version() >= server::cellular_lives_presence_rules; + std::vector offline; + for (const auto& life : current->lives()) { + server::CellularLifeIntent intent{life.identity.life, {}}; + network::AccountId account; + account.bytes = life.account; + if (presence_rules && life.account != server::LifeAccount{} && + !bridge->sessions().holder(account)) { + intent.intent.frozen = true; + offline.push_back(life.account); + } + baseline.push_back(intent); } - baseline.push_back(intent); - } - std::ranges::sort(baseline, {}, &server::CellularLifeIntent::life_id); - // ADR-0125: wildlife follow the deterministic controller; its intentions - // ride the trace like a player's, and the owner validates them. - if (const auto wild = server::wildlife_intents(*state, tick); wild) { - for (const auto& intent : *wild) { - const auto found = std::ranges::lower_bound( - baseline, intent.life_id, {}, &server::CellularLifeIntent::life_id); - if (found != baseline.end() && found->life_id == intent.life_id) - *found = intent; + std::ranges::sort(baseline, {}, &server::CellularLifeIntent::life_id); + // ADR-0125: wildlife follow the deterministic controller; its intentions + // ride the trace like a player's, and the owner validates them. + if (const auto wild = server::wildlife_intents(*current, tick); wild) { + for (const auto& intent : *wild) { + const auto found = std::ranges::lower_bound( + baseline, intent.life_id, {}, &server::CellularLifeIntent::life_id); + if (found != baseline.end() && found->life_id == intent.life_id) + *found = intent; + } } - } - auto actions = bridge->carry(tick); - if (!offline.empty()) { - std::vector departed; - std::erase_if(actions, [&](const network::CarriedAction& action) { - if (std::ranges::find(offline, action.account.bytes) == offline.end()) - return false; - departed.push_back(action); - return true; - }); - bridge->dropped(departed, true); - } - auto builder = - server::CellularSessionBatchBuilder::create(*state, host.committed()->head); - if (!builder) { - report_stall("create", builder.error(), tick); - bridge->dropped(actions, true); - } else { - auto appended = builder->append_with_birth_refusals( - tick, baseline, actions, held_births, host.queued_mutations()); - if (!appended && !actions.empty()) { - // ADR-0127: one account's unusable action (its life died, or it - // names an unperceived patch) refuses only that action, never the - // tick for every other player. - std::vector usable, refused; - for (const auto& action : actions) - (server::prepare_cellular_session_tick(*state, tick, baseline, - std::span{&action, 1}) - ? usable - : refused) - .push_back(action); - bridge->dropped(refused, true); - actions = std::move(usable); - appended = builder->append_with_birth_refusals( - tick, baseline, actions, held_births, host.queued_mutations()); + auto actions = bridge->carry(tick); + if (!offline.empty()) { + std::vector departed; + std::erase_if(actions, [&](const network::CarriedAction& action) { + if (std::ranges::find(offline, action.account.bytes) == offline.end()) + return false; + departed.push_back(action); + return true; + }); + bridge->dropped(departed, true); } - if (!appended) { - const auto lives = builder->last_lives_error(); - report_stall("append", appended.error(), tick, - lives ? static_cast(*lives) : -1); + if (!batch_builder) { + auto created = server::CellularSessionBatchBuilder::create( + *state, host.committed()->head); + if (created) + batch_builder.emplace(std::move(*created)); + else + report_stall("create", created.error(), tick); + } + if (!batch_builder) { bridge->dropped(actions, true); } else { - stalled.reset(); - awaiting_outcomes.insert(awaiting_outcomes.end(), actions.begin(), + // Births and mutation choices only ever enter a fresh batch. + const bool fresh = batch_builder->batch().ticks.empty(); + const std::span births = + fresh ? std::span{held_births} + : std::span{}; + const std::span mutations = + fresh ? host.queued_mutations() + : std::span{}; + auto appended = batch_builder->append_with_birth_refusals( + tick, baseline, actions, births, mutations); + if (!appended && !actions.empty()) { + // ADR-0127: one account's unusable action (its life died, or it + // names an unperceived patch) refuses only that action, never the + // tick for every other player. + std::vector usable, refused; + for (const auto& action : actions) + (server::prepare_cellular_session_tick(*current, tick, baseline, + std::span{&action, 1}) + ? usable + : refused) + .push_back(action); + bridge->dropped(refused, true); + actions = std::move(usable); + appended = batch_builder->append_with_birth_refusals( + tick, baseline, actions, births, mutations); + } + if (!appended) { + const auto lives = batch_builder->last_lives_error(); + report_stall("append", appended.error(), tick, + lives ? static_cast(*lives) : -1); + bridge->dropped(actions, true); + flush(); // Commit what is good; the next tick starts afresh. + } else { + stalled.reset(); + awaiting_outcomes.insert(awaiting_outcomes.end(), actions.begin(), + actions.end()); + batch_actions.insert(batch_actions.end(), actions.begin(), actions.end()); - auto published = host.publish(builder->batch(), builder->actions(), - builder->mutation_actions()); - if (published) - host.refuse_births(builder->refused_births()); - if (!published && host.status().phase != server::HostPhase::resolving) { - bridge->dropped(actions, true); - std::erase_if(awaiting_outcomes, [&](const auto& action) { - return std::ranges::find(actions, action) != actions.end(); - }); + const bool prompt = !births.empty() || !mutations.empty(); + if (prompt || batch_builder->batch().ticks.size() >= flush_ticks) + flush(); } - deliver_finalized(); } + // Unappended work keeps its pacing debt; retry with bounded frequency. + const auto* after = batch_builder ? &batch_builder->candidate() : host.state(); + if (!after || after->tick() < tick) + schedule.defer(monotonic_us()); } - // Only a confirmed head consumes pacing debt. Retry failures with bounded - // frequency while retaining debt; never reset a deadline to now + quantum. - if (!host.state() || host.state()->tick() < tick) - schedule.defer(monotonic_us()); } } if (const auto* committed_state = host.state(); committed_state) - bridge->replicate(*committed_state); + bridge->replicate(batch_builder ? batch_builder->candidate() : *committed_state, + committed_state); std::fflush(stdout); std::this_thread::sleep_for(2ms); } diff --git a/deploy/kubernetes/region/dev-session-route/listener.yaml b/deploy/kubernetes/region/dev-session-route/listener.yaml index 3a24c5e..5095cc3 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-20261021T234706-05bba866:1792640826"} + - {name: REGION_SESSION_WINDOW, value: "dev-20261022T014554-534aa212:1792647954"} - {name: REGION_M10_MAX_GRANT_LIFETIME, value: "14400"} ports: # QUIC (ADR-0090): UDP only; nothing listens on TCP 7400. diff --git a/src/biology/cellular_habitat.cpp b/src/biology/cellular_habitat.cpp index 59ca880..53d394f 100644 --- a/src/biology/cellular_habitat.cpp +++ b/src/biology/cellular_habitat.cpp @@ -184,7 +184,8 @@ CellularHabitat::perceive(CellularPosition position, std::uint32_t extent_um) co return std::unexpected{CellularHabitatError::outside_area}; std::vector visible; - if (config_.format == cellular_habitat_deposit_format) { + if (config_.format == cellular_habitat_deposit_format || + config_.format == cellular_habitat_liquid_format) { // A deposit is visible when the gap from the footprint to its disc is within range. for (const auto& patch : config_.patches) { const auto reach = std::uint64_t{cellular_trial_perception_radius_um} + patch.radius_um; diff --git a/tests/cellular_deposit_motion_tests.cpp b/tests/cellular_deposit_motion_tests.cpp index d0942a4..e2b11a7 100644 --- a/tests/cellular_deposit_motion_tests.cpp +++ b/tests/cellular_deposit_motion_tests.cpp @@ -95,6 +95,21 @@ void format_three_round_trip() { CHECK(CellularHabitat::create(old)); } +// A format-3 habitat is perceived as pools and bubbles, exactly like format 2. +void liquid_deposits_are_perceived() { + CellularPatch pool{{8'000, 8'000}, 2, energy(500), 2, 500, CellularDepositKind::pool}; + pool.merged = 1; + const auto habitat = CellularHabitat::create(liquid({bubble(1, 5'100, 5'000), pool})); + CHECK(habitat); + const auto seen = (*habitat)->perceive({5'000, 5'000}, 20); + CHECK(seen && seen->size() == 2); + CHECK(std::ranges::any_of(*seen, [](const auto& p) { + return p.id == 1 && p.kind == CellularDepositKind::mote && p.radius_um == 30; + })); + CHECK(std::ranges::any_of( + *seen, [](const auto& p) { return p.id == 2 && p.kind == CellularDepositKind::pool; })); +} + void still_water_keeps_bubbles() { const auto config = liquid({bubble(1, 5'000, 5'000), bubble(2, 9'000, 9'000)}); const auto moved = advect(config, still()); @@ -210,6 +225,7 @@ void refusals() { int main() { try { format_three_round_trip(); + liquid_deposits_are_perceived(); still_water_keeps_bubbles(); passing_cells_push_and_drift_decays(); touching_bubbles_merge_up_to_the_cap();