From 10b042309edbada11f51a12e3a6a628a7ba1f92b Mon Sep 17 00:00:00 2001 From: dingo Date: Sun, 11 Oct 2026 01:03:05 -0400 Subject: [PATCH] Add cellular profile region serving --- CMakeLists.txt | 3 +- apps/evo-server/cellular_bridge.hpp | 199 +++++++++++++++++ apps/evo-server/main.cpp | 200 +++++++++++++++++- docs/engineering/M11_3_DELIVERY_PLAN.md | 7 + docs/engineering/VALIDATION.md | 12 ++ .../evo/server/lifecycle/cellular_lives.hpp | 1 + src/server/lifecycle/cellular_lives.cpp | 3 + tests/cellular_lives_tests.cpp | 1 + 8 files changed, 423 insertions(+), 3 deletions(-) create mode 100644 apps/evo-server/cellular_bridge.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index ccebc27..d3ff22f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -263,7 +263,8 @@ if(EVO_BUILD_POSTGRES) endif() if(EVO_BUILD_QUIC) # M10.4c QUIC sessions inside the region process (ADR-0091/0092); opt-in by flags. - target_link_libraries(evo-server PRIVATE evo_session_server evo_control_ledger_postgres) + target_link_libraries(evo-server PRIVATE evo_session_server evo_control_ledger_postgres + evo_cellular_session_adapter) target_compile_definitions(evo-server PRIVATE EVO_SERVER_M10=1) endif() evo_owned_target(evo-server) diff --git a/apps/evo-server/cellular_bridge.hpp b/apps/evo-server/cellular_bridge.hpp new file mode 100644 index 0000000..7fbb6f3 --- /dev/null +++ b/apps/evo-server/cellular_bridge.hpp @@ -0,0 +1,199 @@ +#pragma once + +#include "evo/client/organism_view.hpp" +#include "evo/network/session_grant.hpp" +#include "evo/server/cellular_region_host.hpp" +#include "evo/server/pg_slot.hpp" +#include "evo/server/postgres_action_ledger.hpp" +#include "evo/server/postgres_control_ledger.hpp" +#include "evo/server/region_sessions.hpp" +#include "evo/server/session_window.hpp" +#include "m10_bridge.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace evo::server_app { +// Profile-4 network boundary. Biology and persistence remain owned by the cellular host; +// this bridge handles admission, v7 action receipts and read-only replicated cell views. +class CellularBridge { + public: + static std::optional + open(const M10Options& options, server::CellularRegionHost& host, + persistence::RegionKey key, std::uint64_t frame, const persistence::PgOptions& pg) { + if (!options.complete()) + return fail("error cellular-session-config"); + CellularBridge bridge; + const auto window = server::SessionWindow::open(options.session_window, wall_seconds()); + if (!window) + return fail("error cellular-session-window"); + const auto colon = options.listen.rfind(':'); + std::uint16_t port{}; + if (colon == std::string::npos || + std::from_chars(options.listen.data() + colon + 1, + options.listen.data() + options.listen.size(), port) + .ec != std::errc{}) + return fail("error cellular-session-listen"); + const auto issuer = read(options.issuer); + if (issuer.size() != network::grant_public_key_bytes) + return fail("error cellular-session-issuer"); + + server::RegionSessionsConfig config; + config.transport.listen_address = options.listen.substr(0, colon); + config.transport.port = port; + config.transport.certificate_pem_file = options.certificate; + config.transport.key_pem_file = options.key; + config.control.environment = options.environment == "dev" + ? network::DeploymentEnvironment::dev + : options.environment == "stage" + ? network::DeploymentEnvironment::stage + : network::DeploymentEnvironment::world0; + config.control.world = {key.world, frame}; + config.control.region = key.region; + config.control.epoch = 1; + config.control.max_sessions = 512; + config.transport.limits.max_connections = 576; + network::GrantPublicKey public_key{}; + std::copy(issuer.begin(), issuer.end(), public_key.begin()); + config.issuer_keys.push_back({1, public_key}); + + bridge.window_ = *window; + bridge.slot_ = std::make_unique(pg, std::chrono::seconds{2}); + const auto lease = [&host] { return host.authority(); }; + bridge.control_ = + std::make_unique(*bridge.slot_, key, lease); + bridge.actions_ = + std::make_unique(*bridge.slot_, key, lease); + config.ledger = bridge.control_.get(); + config.actions = bridge.actions_.get(); + config.accept_cellular_actions = true; + config.create_primitive_life = [&host](const network::AccountId& account, + std::uint64_t generation, + std::uint64_t request) { + return host.create_primitive_life(account.bytes, generation, request); + }; + config.query_primitive_life = [&host](const network::AccountId& account, + std::uint64_t generation, + std::uint64_t request) { + return host.query_primitive_life(account.bytes, generation, request); + }; + auto sessions = server::RegionSessions::create(std::move(config)); + if (!sessions) + return fail(sessions.error() == server::RegionSessionsError::transport + ? "error cellular-session-listen" + : "error cellular-session-config"); + bridge.sessions_ = std::move(*sessions); + bridge.sessions_->set_default_interest({0, 0, 0, 7}); + std::printf("cellular listening %u\n", static_cast(bridge.sessions_->port())); + return std::optional{std::move(bridge)}; + } + + [[nodiscard]] bool accepting(std::uint64_t now_seconds) { + const bool open = window_ && window_->accepting(now_seconds); + if (!open && !shutdown_) { + sessions_->shutdown(std::chrono::steady_clock::now()); + shutdown_ = true; + } + return open; + } + [[nodiscard]] server::RegionSessions& sessions() noexcept { + return *sessions_; + } + [[nodiscard]] std::vector carry(std::uint64_t tick) { + return sessions_->carry(tick); + } + void committed(std::span actions, std::uint64_t sequence) { + sessions_->committed(actions, sequence); + } + void dropped(std::span actions, bool final) { + sessions_->dropped(actions, final); + } + void poll(const server::HostStatus& status, std::uint64_t now_seconds) { + sessions_->poll({status.ready, status.epoch, status.simulated_tick}, now_seconds, + std::chrono::steady_clock::now()); + for (const auto& event : sessions_->take_events()) + std::printf("cellular %s session=%llu account=%s reason=%u\n", + event.kind == server::RegionSessionEvent::Kind::opened ? "opened" + : event.kind == server::RegionSessionEvent::Kind::closed ? "closed" + : "refused", + static_cast(event.session), event.account.c_str(), + static_cast(event.reason)); + } + void replicate(const server::CellularLives& state) { + const auto tick = state.tick(); + if (replicated_tick_ == tick) + return; + const auto source = [&](const network::SessionInterest& interest) { + network::ReplicationSource result; + result.identity = {state.world(), state.frame()}; + result.organism_revision = tick; + for (const auto& life : state.lives()) { + const auto voxel_x = life.position.x_um / 1'000'000; + const auto voxel_y = life.position.y_um / 1'000'000; + if (voxel_x > + static_cast(std::numeric_limits::max()) || + voxel_y > static_cast(std::numeric_limits::max())) + continue; + const world::VoxelAddress voxel{static_cast(voxel_x), + static_cast(voxel_y), 0}; + if (voxel.x < interest.x - interest.radius || + voxel.x > interest.x + interest.radius || + voxel.y < interest.y - interest.radius || + voxel.y > interest.y + interest.radius) + continue; + const auto genome = state.archive().find(life.identity.genome); + if (!genome) + continue; + const biology::EnvironmentState environment{ + state.ambient_temperature_millikelvin(), life.reserve}; + auto cell = client::present_organism( + life.identity.organism, voxel, + {life.position.x_um % 1'000'000}, {life.position.y_um % 1'000'000}, *genome, + life.growth, environment, client::OrganismState::alive); + if (cell) + result.organisms.push_back(*cell); + } + std::ranges::sort(result.organisms, {}, &client::OrganismCell::id); + return result; + }; + sessions_->replicate(tick, source); + replicated_tick_ = tick; + } + void shutdown() { + sessions_->shutdown(std::chrono::steady_clock::now()); + } + + private: + static std::uint64_t wall_seconds() { + return static_cast(std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()) + .count()); + } + static std::string read(const std::string& path) { + std::ifstream file{path, std::ios::binary}; + return {std::istreambuf_iterator{file}, std::istreambuf_iterator{}}; + } + static std::optional fail(const char* message) { + std::puts(message); + return std::nullopt; + } + + std::unique_ptr slot_; + std::unique_ptr control_; + std::unique_ptr actions_; + std::unique_ptr sessions_; + std::optional window_; + std::optional replicated_tick_; + bool shutdown_{}; +}; +} // namespace evo::server_app diff --git a/apps/evo-server/main.cpp b/apps/evo-server/main.cpp index b4e481e..9f0c468 100644 --- a/apps/evo-server/main.cpp +++ b/apps/evo-server/main.cpp @@ -54,6 +54,7 @@ // provisioning remain disabled. #include "evo/biology/founder.hpp" #include "evo/processes/development_opening.hpp" +#include "evo/server/cellular_session_batch.hpp" #include "evo/server/lifecycle/primitive_lives.hpp" #include "evo/server/ops_http.hpp" #include "evo/server/primitive_region_host.hpp" @@ -61,6 +62,7 @@ #include "m10_bridge.hpp" #include "session_bridge.hpp" #ifdef EVO_SERVER_M10 +#include "cellular_bridge.hpp" #include "evo/server/pg_slot.hpp" #include "evo/server/postgres_control_ledger.hpp" #include "evo/server/region_sessions.hpp" @@ -170,7 +172,8 @@ std::optional parse(int argc, char** argv) { const std::string_view value = argv[++i]; const auto n = number(value); if (flag == "--profile" && - (value == "lineage" || value == "environmental" || value == "primitive")) + (value == "lineage" || value == "environmental" || value == "primitive" || + value == "cellular")) o.profile = value; else if (flag == "--world" && n) o.host.key.world = *n; @@ -316,6 +319,13 @@ std::optional parse(int argc, char** argv) { (o.synthetic || !o.crash_point.empty() || o.session.any() || o.m10.any() || o.host.flush_interval != 1000ms || o.drain != 10000ms)) return std::nullopt; + if (o.profile == "cellular" && + (!server_app::m10_available || !o.m10.complete() || o.primitive_session.any() || + o.synthetic || !o.crash_point.empty() || o.session.any() || + !o.m10.area.empty() || !o.m10.handoff_key.empty() || !o.m10.neighbours.empty() || + o.m10.max_grant_lifetime != 0 || !o.m10.load_governor || + o.host.flush_interval != 1000ms || o.drain != 10000ms)) + return std::nullopt; if (std::ranges::find(o.host.retained_builds, o.host.build) == o.host.retained_builds.end()) o.host.retained_builds.push_back(o.host.build); return o; @@ -870,13 +880,195 @@ int serve_primitive(Options& o) { std::this_thread::sleep_for(5ms); } } + +#ifdef EVO_SERVER_M10 +int serve_cellular(Options& o) { + auto objects = persistence::FilesystemObjectStore::open( + o.objects, o.host.key, server::max_cellular_lives_bytes, nullptr, + o.objects_reserve_mib * 1024 * 1024); + if (!objects) { + std::puts("error objects"); + return 2; + } + const auto pg = o.pg; + auto made = server::CellularRegionHost::create( + {o.host.key, o.host.frame, o.host.lease, o.host.build, o.host.retained_builds}, + [pg] { return persistence::Postgres::connect(pg, 2s); }, std::move(*objects)); + if (!made) { + std::puts("error config"); + return 2; + } + auto& host = **made; + auto bridge = server_app::CellularBridge::open(o.m10, host, o.host.key, o.host.frame, o.pg); + if (!bridge) + return 2; + std::unique_ptr ops; + if (!o.ops_listen.empty()) { + auto listening = server::OpsServer::listen(o.ops_listen); + if (!listening) { + std::puts("error ops-listen"); + return 2; + } + ops = std::move(*listening); + std::printf("ops listening %u\n", static_cast(ops->port())); + } + bool ready = false; + std::optional phase; + std::optional next_tick; + std::vector awaiting_outcomes; + const auto set_ready = [&](bool now) { + if (ready != now) { + ready = now; + std::printf("ready %d\n", ready ? 1 : 0); + } + if (!o.ready_file.empty()) { + std::error_code ignored; + if (ready) + std::ofstream(o.ready_file) << "ready\n"; + else + std::filesystem::remove(o.ready_file, ignored); + } + }; + const auto deliver_finalized = [&] { + for (const auto& finalized : host.take_finalized_actions()) { + const auto pending = std::ranges::find_if(awaiting_outcomes, [&](const auto& action) { + return action.account.bytes == finalized.action.account && + action.action_id == finalized.action.action_id; + }); + if (pending == awaiting_outcomes.end()) + continue; + const std::array committed{*pending}; + bridge->committed(committed, finalized.commit_sequence); + awaiting_outcomes.erase(pending); + } + }; + const auto finish = [&](int code) { + set_ready(false); + const auto& status = host.status(); + std::printf("phase %s epoch=%llu committed=%llu simulated=%llu\n", + server::phase_name(status.phase), + static_cast(status.epoch), + static_cast(status.committed_tick), + static_cast(status.simulated_tick)); + std::printf("stats commits=%llu store_failures=%llu\n", + static_cast(status.commits), + static_cast(status.store_failures)); + std::printf("exit %d\n", code); + std::fflush(stdout); + return code; + }; + for (;;) { + if (stop_requested) { + bridge->shutdown(); + host.drain(); + return finish(0); + } + const auto& status = host.step(); + const auto wall = static_cast( + std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()) + .count()); + static_cast(bridge->accepting(wall)); + bridge->poll(status, wall); + deliver_finalized(); + set_ready(status.ready); + if (!phase || *phase != status.phase) { + phase = status.phase; + std::printf("phase %s epoch=%llu committed=%llu simulated=%llu\n", + server::phase_name(status.phase), + static_cast(status.epoch), + static_cast(status.committed_tick), + static_cast(status.simulated_tick)); + } + if (ops) { + const auto handler = [&](std::string_view path) -> server::OpsResponse { + const auto& current = host.status(); + if (path == "/livez") + return {200, "live\n"}; + if (path == "/readyz") + return current.ready ? server::OpsResponse{200, "ready\n"} + : server::OpsResponse{ + 503, std::string("not ready: ") + + server::phase_name(current.phase) + "\n"}; + if (path == "/metrics") + return {200, server::render_metrics(current, {})}; + return {404, "not found\n"}; + }; + ops->serve(handler); + } + if (status.phase == server::HostPhase::quarantined) + return finish(4); + + const auto* state = host.state(); + bool player_present = false; + if (status.ready && state) { + for (const auto& life : state->lives()) { + network::AccountId account; + account.bytes = life.account; + if (bridge->sessions().holder(account)) { + player_present = true; + break; + } + } + } + const bool birth_waiting = !host.queued_births().empty(); + if (status.ready && state && (player_present || birth_waiting)) { + const auto now = std::chrono::steady_clock::now(); + if (!next_tick) + next_tick = now + std::chrono::milliseconds{50}; + if (now >= *next_tick && state->tick() != UINT64_MAX) { + const auto tick = state->tick() + 1; + std::vector baseline; + baseline.reserve(state->lives().size()); + for (const auto& life : state->lives()) + baseline.push_back({life.identity.life, {}}); + std::ranges::sort(baseline, {}, &server::CellularLifeIntent::life_id); + auto actions = bridge->carry(tick); + auto builder = server::CellularSessionBatchBuilder::create( + *state, host.committed()->head); + if (!builder) { + bridge->dropped(actions, true); + } else { + const auto births = host.queued_births().first( + std::min(host.queued_births().size(), biology::max_cellular_tick_lives)); + const auto appended = builder->append(tick, baseline, actions, births); + if (!appended) { + bridge->dropped(actions, true); + } else { + awaiting_outcomes.insert(awaiting_outcomes.end(), actions.begin(), + actions.end()); + auto published = host.publish(builder->batch(), builder->actions()); + 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(); + }); + } + deliver_finalized(); + } + } + *next_tick = std::chrono::steady_clock::now() + std::chrono::microseconds{ + static_cast( + biology::cellular_motion_tick_microseconds)}; + } + } else { + next_tick.reset(); // A disconnected life does not accumulate offline ticks. + } + if (const auto* committed_state = host.state(); committed_state) + bridge->replicate(*committed_state); + std::fflush(stdout); + std::this_thread::sleep_for(2ms); + } +} +#endif } // namespace int main(int argc, char** argv) { auto options = parse(argc, argv); if (!options) { std::fputs( - "usage: evo-server --profile lineage|environmental|primitive --world N --region N " + "usage: evo-server --profile lineage|environmental|primitive|" + "cellular --world N --region N " "--frame N --pg-host H --pg-port P --pg-database D --pg-user U " "--objects DIR --build HEX64 [--retained HEX64]... [--pg-hostaddr A " "--pg-passfile F --pg-ca F] [--flush-ms N] [--drain-ms N] " @@ -904,6 +1096,10 @@ int main(int argc, char** argv) { std::signal(SIGPIPE, SIG_IGN); if (options->profile == "primitive") return serve_primitive(*options); +#ifdef EVO_SERVER_M10 + if (options->profile == "cellular") + return serve_cellular(*options); +#endif return options->profile == "lineage" ? serve(*options) : serve(*options); } diff --git a/docs/engineering/M11_3_DELIVERY_PLAN.md b/docs/engineering/M11_3_DELIVERY_PLAN.md index ac3c5b9..46ce9c3 100644 --- a/docs/engineering/M11_3_DELIVERY_PLAN.md +++ b/docs/engineering/M11_3_DELIVERY_PLAN.md @@ -346,6 +346,13 @@ scoped change before starting the next segment. interest, in addition to committing cellular actions. QUIC coverage confirms a v7 client can receive a cell entity and move its focus. The process-level cellular tick loop and resource patch view are still required before 11.3e. +- 11.3d-7n: `evo-server --profile cellular` now has a profile-4 serve path, + durable v5/v7 sessions, 50 ms host ticks, checkpointed carried actions and + live-cell replication. It pauses when no living account is connected and does + not accumulate catch-up ticks while offline. The path has not been compiled + with the Linux/PostgreSQL CMake preset or run against a region. Exact patch + perception and the engineering-client controls/view are still required for + the exploratory 11.3e playtest. ## Exit boundary diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index 7aa46c3..015268b 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -6487,3 +6487,15 @@ session, client session and QUIC test, and `git diff --check` passes. QUIC runtime tests and CI remain pending. The configured Windows CMake QUIC target produced no output for 25 seconds and was interrupted; it is not counted as a build or test result. + +## 2026-10-11 — M11.3d-7n cellular server process + +The app now has an opt-in `--profile cellular` serve path with the M10 session +window, durable control/action ledgers, cellular ticks and cell snapshots. It +only advances while a living account has an established session or a first +birth is queued. The new bridge header passes GCC 13.2 strict-warning syntax +checking; the `CellularLives` accessor and tests pass strict-warning syntax +checking; `git diff --check` passes. The POSIX-only `evo-server/main.cpp` cannot +be compiled in this Windows environment (`poll.h` is unavailable). The +configured Windows CMake QUIC target previously stalled without output and was +interrupted. Linux/PostgreSQL CI and a real region run remain pending. diff --git a/include/evo/server/lifecycle/cellular_lives.hpp b/include/evo/server/lifecycle/cellular_lives.hpp index b5bebdf..c757f01 100644 --- a/include/evo/server/lifecycle/cellular_lives.hpp +++ b/include/evo/server/lifecycle/cellular_lives.hpp @@ -85,6 +85,7 @@ class CellularLives { [[nodiscard]] std::uint64_t region() const noexcept; [[nodiscard]] std::uint64_t frame() const noexcept; [[nodiscard]] std::uint64_t tick() const noexcept; + [[nodiscard]] std::uint32_t ambient_temperature_millikelvin() const noexcept; [[nodiscard]] biology::CellularArea area() const noexcept; [[nodiscard]] std::span lives() const noexcept; [[nodiscard]] std::span corpses() const noexcept; diff --git a/src/server/lifecycle/cellular_lives.cpp b/src/server/lifecycle/cellular_lives.cpp index 1d19462..900bc91 100644 --- a/src/server/lifecycle/cellular_lives.cpp +++ b/src/server/lifecycle/cellular_lives.cpp @@ -379,6 +379,9 @@ std::uint64_t CellularLives::world() const noexcept { return state_->world; } std::uint64_t CellularLives::region() const noexcept { return state_->region; } std::uint64_t CellularLives::frame() const noexcept { return state_->frame; } std::uint64_t CellularLives::tick() const noexcept { return state_->config.tick; } +std::uint32_t CellularLives::ambient_temperature_millikelvin() const noexcept { + return state_->config.ambient_temperature_millikelvin; +} biology::CellularArea CellularLives::area() const noexcept { return state_->config.area; } std::span CellularLives::lives() const noexcept { return state_->lives; } std::span CellularLives::corpses() const noexcept { diff --git a/tests/cellular_lives_tests.cpp b/tests/cellular_lives_tests.cpp index bf7caa6..84769e4 100644 --- a/tests/cellular_lives_tests.cpp +++ b/tests/cellular_lives_tests.cpp @@ -53,6 +53,7 @@ std::unique_ptr empty_cellular_owner(Inventory funding, CHECK(habitat); auto owner = CellularLives::create(**primitive, std::move(*habitat), {{1'000, 1'000}, 0, 293'150}); CHECK(owner); + CHECK((*owner)->ambient_temperature_millikelvin() == 293'150); return std::move(*owner); }