feat(server): commit cellular batches on a background thread
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (network) (push) Successful in 4s
Foundation / checks (postgres) (push) Successful in 4s
Foundation / checks (tsan) (push) Successful in 4s
Foundation / checks (deploy) (push) Successful in 4m28s
Foundation / postgres-tools (push) Successful in 23s
Foundation / server-image (push) Successful in 4m27s
Foundation / image (push) Successful in 4m31s

The rules-12 loop blocked for ~411 ms per 20-tick commit, so players saw
one-second bursts. The host now commits a batch on a worker thread with
its own connection (begin_publish / poll_publish), using the builder's
already-simulated candidate. The next batch builds on that candidate
under a provisional head and is rebased when the commit lands. One commit
is in flight at a time; births and mutation choices wait for a batch that
starts on the durable head, and host reconciliation pauses meanwhile.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-10-22 08:41:56 -04:00
co-authored by Claude Opus 5.5
parent 4112bef52c
commit ff333d97c2
6 changed files with 403 additions and 37 deletions
+137 -1
View File
@@ -4,8 +4,12 @@
#include "evo/persistence/life_reservation_store.hpp"
#include "evo/persistence/sha256.hpp"
#include "evo/server/cellular_region_host.hpp"
#ifdef EVO_POSTGRES_M10
#include "evo/server/cellular_session_batch.hpp"
#endif
#include <algorithm>
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <optional>
@@ -13,6 +17,7 @@
#include <stdexcept>
#include <string>
#include <string_view>
#include <thread>
void run_cellular_publication_tests();
namespace {
@@ -927,6 +932,134 @@ void cellular_bud_publishes_and_recovers() {
(*restarted)->drain();
}
#ifdef EVO_POSTGRES_M10
// Write-behind commits (evo-server's rules-12 loop): each batch commits on the host's worker
// thread while the next one builds on its candidate under a provisional head, rebased when
// the commit lands; recovery reloads exactly the chained state.
void cellular_write_behind_commits_chain_and_recover() {
using namespace evo::biology;
constexpr RegionKey key{9778, 1};
auto owner = connect_as(env("PGUSER"));
auto runtime = connect_as(env("EVO_PG_RUNTIME_USER"));
const auto build = filled<32>(0xDB);
CHECK(owner->query("INSERT INTO evolution.retained_builds(build_identity) VALUES "
"(decode(repeat('db',32),'hex')) ON CONFLICT DO NOTHING",
{}, 5s));
auto endowed = primitive_endowment();
CHECK(endowed);
auto funding = endowed->body;
funding.energy.value = endowed->reserve.energy.value + endowed->construction.value;
auto primitive = PrimitiveLives::create({key.world, key.region, 1, 17, funding, {{0, 0}}});
CHECK(primitive);
constexpr std::uint64_t stock = 10'000'000'000;
auto habitat = CellularHabitat::create(
{key.world,
key.region,
1,
3'000,
2'000,
{{{0, 0}, 29, {{stock}, {{{stock}, {stock}, {stock}, {stock}}}}}}});
CHECK(habitat);
auto opening =
CellularLives::create(**primitive, std::move(*habitat), {{3'000, 2'000}, 0, 293'150},
CellularLivesProfile::budding);
CHECK(opening);
auto objects = FilesystemObjectStore::open(env("PGHOST"), key, max_cellular_lives_bytes);
CHECK(objects);
CHECK(provision_cellular_region(*owner, **objects, key, 1, **opening, distinct_commit(0x9E),
build, 5s));
CHECK(owner->query("INSERT INTO evolution.life_allocators VALUES(9778,1000) "
"ON CONFLICT DO NOTHING",
{}, 5s));
const auto connect = [&]() -> std::expected<std::unique_ptr<Postgres>, PgFailure> {
return Postgres::connect({env("PGHOST"),
{},
env("PGPORT"),
env("PGDATABASE"),
env("EVO_PG_RUNTIME_USER"),
{},
{}},
5s);
};
const auto config = [&] { return CellularHostConfig{key, 1, {}, build, {build}}; };
auto made = CellularRegionHost::create(config(), connect, std::move(*objects));
CHECK(made);
auto host = std::move(*made);
CHECK(host->step().ready);
const auto account = filled<16>(0x85);
const auto control = claim_account_control(*runtime, key, checked_value(host->authority()),
account, 1, 1, 60'000ms, 5s);
CHECK(control);
CHECK(host->create_primitive_life(account, control->generation, 11).result ==
evo::network::PrimitiveLifeResult::pending);
const auto& committed = checked_value(host->committed());
const auto digest = sha256(committed.manifest);
CHECK(digest);
CellularBatch birth;
birth.header = {key.world,
key.region,
1,
committed.head.commit,
checked_value(digest),
host->state()->tick(),
host->state()->tick() + 1};
birth.ticks.push_back({{}, {host->queued_births().front()}, {}});
CHECK(host->publish(birth));
CellularStepIntent feed{};
feed.intake_target = CellularPatchAddress{0, 0};
const std::array baseline{
CellularLifeIntent{host->state()->lives().front().identity.life, feed}};
auto created =
CellularSessionBatchBuilder::create(*host->state(), checked_value(host->committed()).head);
CHECK(created);
std::optional<CellularSessionBatchBuilder> open{std::move(*created)};
const auto fill = [&](int ticks) {
for (int i = 0; i < ticks; ++i)
CHECK(open->append(open->candidate().tick() + 1, baseline, {}));
};
fill(20);
for (int round = 0; round < 3; ++round) {
const auto& candidate = open->candidate();
const auto end = candidate.tick();
RegionHead provisional{
candidate.frame(), checked_value(host->committed()).head.sequence + 1, {}, {}, end};
provisional.commit.fill(0xFF);
provisional.digest.fill(0xFF);
auto next = CellularSessionBatchBuilder::create(candidate, provisional);
CHECK(next);
const auto batch = open->batch();
CHECK(host->begin_publish(batch, {}, {}, open->take_candidate()));
// One commit at a time; the synchronous path waits too.
CHECK(host->commit_in_flight() && !host->publish(batch) &&
!host->begin_publish(batch, {}, {}, nullptr));
open.emplace(std::move(*next));
fill(20); // Simulation continues while the commit runs.
std::optional<std::expected<PublishedCellularBatch, CellularPublishFailure>> landed;
for (int wait = 0; wait < 1'000 && !landed; ++wait) {
landed = host->poll_publish();
if (!landed)
std::this_thread::sleep_for(10ms);
}
CHECK(landed && *landed && !host->commit_in_flight() && host->state()->tick() == end &&
checked_value(host->committed()).head.sequence == provisional.sequence);
CHECK(open->rebase(checked_value(host->committed()).head) && host->step().ready);
}
const auto snapshot = host->state()->snapshot();
const auto tick = host->state()->tick();
host->drain();
host.reset();
auto reopened = FilesystemObjectStore::open(env("PGHOST"), key, max_cellular_lives_bytes);
CHECK(reopened);
auto restarted = CellularRegionHost::create(config(), connect, std::move(*reopened));
CHECK(restarted);
CHECK((*restarted)->step().ready && (*restarted)->state()->tick() == tick &&
(*restarted)->state()->snapshot() == snapshot);
(*restarted)->drain();
}
#endif
// ADR-0127 (schema v37): a dead life stays dead, its corpse stays, and its account starts
// a new primitive life with a new identity; the first life's history is unchanged.
void cellular_death_is_final_and_account_restarts() {
@@ -956,7 +1089,7 @@ void cellular_death_is_final_and_account_restarts() {
CHECK(opening);
auto objects = FilesystemObjectStore::open(env("PGHOST"), key, max_cellular_lives_bytes);
CHECK(objects);
CHECK(provision_cellular_region(*owner, **objects, key, 1, **opening, distinct_commit(0x9E),
CHECK(provision_cellular_region(*owner, **objects, key, 1, **opening, distinct_commit(0x9F),
build, 5s));
CHECK(owner->query("INSERT INTO evolution.life_allocators VALUES(9768,1000) "
"ON CONFLICT DO NOTHING",
@@ -1078,6 +1211,9 @@ void run_cellular_publication_tests() {
cellular_mutation_publication_binds_results();
cellular_host_admits_mutation_choices();
cellular_bud_publishes_and_recovers();
#ifdef EVO_POSTGRES_M10
cellular_write_behind_commits_chain_and_recover();
#endif
cellular_balance_region_founds_and_recovers({{9771, 1},
CellularLivesProfile::balance,
evo::biology::primitive_founder_v2,