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
@@ -114,6 +114,22 @@ class CellularRegionHost {
[[nodiscard]] std::expected<std::optional<persistence::CommittedCheckpoint>,
CellularPublishFailure>
advance_unattended(std::uint64_t due_ticks);
// Write-behind commit (rules-12 regions, Cellular_Region_Movement_Plan.md): commits a raw
// batch on a worker thread with its own connection while the caller keeps simulating.
// `candidate` is the state the batch's builder already computed, so nothing is replayed.
// At most one commit is in flight; its result is applied on this thread by poll_publish.
[[nodiscard]] std::expected<void, CellularPublishFailure>
begin_publish(CellularBatch batch, std::vector<CellularActionRecord> actions,
std::vector<CellularMutationActionRecord> mutation_actions,
std::unique_ptr<CellularLives> candidate);
[[nodiscard]] bool commit_in_flight() const noexcept {
return in_flight_ != nullptr;
}
// The finished background commit's outcome, applied exactly as publish applies its own;
// nullopt while none has finished.
[[nodiscard]] std::optional<
std::expected<persistence::PublishedCellularBatch, CellularPublishFailure>>
poll_publish();
[[nodiscard]] std::expected<persistence::PublishedCellularBatch, CellularPublishFailure>
publish(const CellularBatch& batch, std::span<const CellularActionRecord> actions = {},
std::span<const CellularMutationActionRecord> mutation_actions = {});
@@ -160,6 +176,13 @@ class CellularRegionHost {
private:
struct Pending;
struct InFlight;
[[nodiscard]] std::expected<persistence::PublishedCellularBatch, CellularPublishFailure>
finish_publication(const CellularBatch& batch, std::span<const CellularActionRecord> actions,
std::span<const CellularMutationActionRecord> mutation_actions,
std::unique_ptr<CellularLives> candidate,
std::expected<persistence::PublishedCellularBatch,
persistence::CellularPublicationFailure> published);
CellularRegionHost(CellularHostConfig config, CellularConnect connect,
std::unique_ptr<persistence::FilesystemObjectStore> objects);
bool database();
@@ -190,6 +213,9 @@ class CellularRegionHost {
CellularConnect connect_;
std::unique_ptr<persistence::FilesystemObjectStore> objects_;
std::unique_ptr<persistence::Postgres> db_;
// The write-behind commit and its own connection (libpq connections are not shared).
std::unique_ptr<InFlight> in_flight_;
std::unique_ptr<persistence::Postgres> commit_db_;
persistence::LeaseAuthority authority_;
// Exactly one owner. The common interface is a const biological view, never
// a second independently spendable cellular copy alongside a population.
@@ -82,6 +82,19 @@ class CellularSessionBatchBuilder {
[[nodiscard]] const CellularLives& candidate() const noexcept {
return *working_;
}
// Hands the working state to a commit; the builder is spent afterwards.
[[nodiscard]] std::unique_ptr<CellularLives> take_candidate() noexcept {
return std::move(working_);
}
// A batch started on an uncommitted predecessor (a commit still in flight) takes that
// predecessor's real head once it is known. The head must end where this batch begins.
[[nodiscard]] bool rebase(const persistence::RegionHead& head) noexcept {
if (head.frame != batch_.header.frame || head.end_tick != batch_.header.begin_tick)
return false;
batch_.header.predecessor = head.commit;
batch_.header.predecessor_digest = head.digest;
return true;
}
// The owner's own refusal behind the last `biology` error, for operator logs.
[[nodiscard]] std::optional<CellularLivesError> last_lives_error() const noexcept {
return last_lives_error_;