Advance the authoritative world on a fixed background clock

This commit is contained in:
faumaray committed 2026-12-02 21:52:31 +03:00
1 parent 2224299589
commit 7979fff16c
6 files changed
+930 -2

No files matched your search

@@ -0,0 +1,100 @@
#pragma once
#include "settlement/core/checkpoint.hpp"
#include "settlement/core/command_dispatcher.hpp"
#include <Void/Time/Clock.hpp>
#include <array>
#include <exception>
#include <expected>
#include <functional>
#include <future>
#include <memory>
#include <optional>
namespace settlement {
enum class RuntimeState { Running, Faulted, Closed };
enum class RuntimeAdmission { Accepted, Busy, Closed, InvalidRequest, IdExhausted };
enum class RuntimeOutcome { Completed, Faulted, Closed, Superseded, InvalidCandidate };
enum class RuntimePhase { BeforeTick, BeforeCapture, BeforeInstallCapture };
struct PublishedSnapshot {
SessionIdentity identity;
std::uint64_t revision;
std::shared_ptr<const World> world;
std::shared_ptr<const RulesManifest> rules;
std::array<std::uint64_t, colony_player_slots> next_requests;
};
struct RuntimeView {
RuntimeState state;
std::shared_ptr<const PublishedSnapshot> snapshot;
std::exception_ptr failure;
};
struct RuntimeResult {
RuntimeOutcome outcome;
std::optional<DispatchReceipt> receipt;
std::shared_ptr<const PublishedSnapshot> snapshot;
};
struct RuntimeSubmission {
RuntimeAdmission admission;
std::future<RuntimeResult> completion;
};
struct LoadTicket {
std::uint64_t request;
Void::Uuid::Uuid expected_session;
};
struct RuntimeOptions {
std::size_t capacity = 256; // Accepted operations, including the active operation.
bool ticking = true; // Presentation suspension is separate from World's pause.
std::shared_ptr<const Void::Time::MonotonicClock> clock;
CommandDispatcherOptions dispatcher;
// Runs on the owner thread. May block or throw for diagnostics/tests.
// Captures must outlive shutdown. Calling shutdown here throws logic_error.
std::function<void(RuntimePhase)> observer;
};
// A single background owner advances 50 ms logical steps and publishes independent
// immutable views. Supply a validated decoded checkpoint or an owned newly built
// world with a resolved manifest. No caller may retain mutable aliases to that world.
// Upstream publication replacement contract: https://git.faumaray.com/Void/Library/issues/5
class SimulationRuntime {
public:
explicit SimulationRuntime(WorldCheckpoint initial, RuntimeOptions options = {});
~SimulationRuntime();
SimulationRuntime(const SimulationRuntime&) = delete;
SimulationRuntime& operator=(const SimulationRuntime&) = delete;
[[nodiscard]] std::shared_ptr<const PublishedSnapshot> latest() const;
[[nodiscard]] RuntimeView view() const;
[[nodiscard]] std::expected<std::shared_ptr<const PublishedSnapshot>, RuntimeState> snapshot_for_save() const;
// Reads only the committed ledger, also available after a fault.
[[nodiscard]] std::optional<DispatchReceipt> lookup(
TrustedClient client, const CommandEnvelope& command) const;
[[nodiscard]] RuntimeSubmission try_submit(TrustedClient client, const CommandEnvelope& command);
[[nodiscard]] RuntimeSubmission set_ticking(bool enabled);
// Fences earlier queued operations and all eligible clock debt through the
// admission time. Advancing a ManualClock should be followed by this or notify_clock().
[[nodiscard]] RuntimeSubmission synchronize();
void notify_clock();
[[nodiscard]] std::expected<LoadTicket, RuntimeAdmission> begin_load();
// Moves candidate only on Accepted. Latest ticket and epoch are checked again
// at publication. Invalid/superseded candidates leave the live party intact.
[[nodiscard]] RuntimeSubmission try_install(LoadTicket ticket, WorldCheckpoint& candidate);
// Closes admission, settles queued futures as Closed, and joins the active
// operation. Safe concurrently with submissions and other shutdown calls.
// Object destruction itself requires callers to have stopped using the object.
void shutdown();
private:
struct Impl;
std::unique_ptr<Impl> impl_;
};
} // namespace settlement