Add M10.4c evo-server QUIC listener and headless session client
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m46s
Foundation / checks (postgres) (push) Canceled after 0s
Foundation / checks (tsan) (push) Canceled after 0s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (checks) (push) Canceled after 3m44s
Foundation / checks (client) (push) Canceled after 3m49s
Foundation / checks (network) (push) Canceled after 1m59s

evo-server gains an opt-in M10 listener (--m10-*, QUIC builds only): RegionSessions
with the PostgreSQL control and action ledgers on their own connection under the
host's lease, carried actions submitted to the host and its acknowledgements and
drops turned into outcomes. evo-session-probe is a dev-only headless client. Real
process tests cover intentions, durable action outcomes, SIGKILL before and after
an action's commit, duplicate retries and the probe. The simulated watermark now
reaches every session every tick, so clients always know the server tick.
ADR-0092.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-10-04 09:20:55 -04:00
co-authored by Claude Opus 5.5
parent 69f3ad7cf8
commit 0bf9901c7f
27 changed files with 1053 additions and 42 deletions
+191
View File
@@ -0,0 +1,191 @@
#pragma once
// M10.4c QUIC session listener for evo-server (ADR-0091, ADR-0092). Compiled only with
// EVO_SERVER_M10 (EVO_BUILD_QUIC with the PostgreSQL build); otherwise the flags are refused
// at parse time. Opt-in, development and test only: nothing deploys it yet.
//
// Each loop iteration after the host's step: the sessions see the host's authority; actions
// scheduled for upcoming ticks are submitted to the host, which commits them with the batch
// of the tick they ride; the host's action acknowledgements and drops become outcomes.
// Session control and action records use their own connection under the host's lease.
#include "evo/persistence/postgres.hpp"
#include "evo/persistence/region_lease.hpp"
#include <cstdint>
#include <optional>
#include <string>
#ifdef EVO_SERVER_M10
#include "evo/server/pg_slot.hpp"
#include "evo/server/postgres_action_ledger.hpp"
#include "evo/server/postgres_control_ledger.hpp"
#include "evo/server/region_host.hpp"
#include "evo/server/region_sessions.hpp"
#include <algorithm>
#include <charconv>
#include <chrono>
#include <cstdio>
#include <fstream>
#include <iterator>
#include <map>
#include <memory>
#include <vector>
#endif
namespace evo::server_app {
struct M10Options {
std::string listen, certificate, key, issuer, environment;
[[nodiscard]] bool any() const noexcept {
return !listen.empty() || !certificate.empty() || !key.empty() || !issuer.empty() ||
!environment.empty();
}
[[nodiscard]] bool complete() const noexcept {
return !listen.empty() && !certificate.empty() && !key.empty() && !issuer.empty() &&
!environment.empty();
}
};
// Actions are handed to the host as soon as they are scheduled (at most 8 ticks ahead).
inline constexpr std::uint64_t m10_carry_ahead_ticks = 8;
#ifdef EVO_SERVER_M10
inline constexpr bool m10_available = true;
template <typename Host> class M10Bridge {
public:
// Empty bridge without M10 options; nullopt on a startup failure (the caller exits).
static std::optional<M10Bridge> open(const M10Options& o, Host& host,
persistence::RegionKey key, std::uint64_t frame,
const persistence::PgOptions& pg) {
M10Bridge bridge;
if (!o.any())
return bridge;
const auto colon = o.listen.rfind(':');
std::uint16_t port = 0;
if (colon == std::string::npos ||
std::from_chars(o.listen.data() + colon + 1, o.listen.data() + o.listen.size(), port)
.ec != std::errc{})
return fail("error m10-listen");
const auto issuer = read(o.issuer);
if (issuer.size() != network::grant_public_key_bytes)
return fail("error m10-issuer");
server::RegionSessionsConfig config;
config.transport.listen_address = o.listen.substr(0, colon);
config.transport.port = port;
config.transport.certificate_pem_file = o.certificate;
config.transport.key_pem_file = o.key;
config.control.environment = o.environment == "dev" ? network::DeploymentEnvironment::dev
: o.environment == "stage"
? network::DeploymentEnvironment::stage
: network::DeploymentEnvironment::world0;
config.control.world = {key.world, frame};
config.control.region = key.region;
config.control.epoch = 1; // Follows the host's epoch from the first poll.
server::IssuerKey issuer_key;
issuer_key.key_id = 1;
std::copy(issuer.begin(), issuer.end(), issuer_key.public_key.begin());
config.issuer_keys.push_back(issuer_key);
bridge.host_ = &host;
bridge.slot_ = std::make_unique<server::PgSlot>(pg, std::chrono::seconds{2});
const auto lease = [h = &host] { return h->authority(); };
bridge.control_ =
std::make_unique<server::PostgresControlLedger>(*bridge.slot_, key, lease);
bridge.actions_ = std::make_unique<server::PostgresActionLedger>(*bridge.slot_, key, lease);
config.ledger = bridge.control_.get();
config.actions = bridge.actions_.get();
auto sessions = server::RegionSessions::create(std::move(config));
if (!sessions)
return fail(sessions.error() == server::RegionSessionsError::keys ? "error m10-issuer"
: sessions.error() == server::RegionSessionsError::transport
? "error m10-listen"
: "error m10-config");
bridge.sessions_ = std::move(*sessions);
std::printf("m10 listening %u\n", static_cast<unsigned>(bridge.sessions_->port()));
return bridge;
}
[[nodiscard]] bool enabled() const noexcept {
return sessions_ != nullptr;
}
void after_step() {
if (!sessions_)
return;
auto& host = *host_;
const auto& s = host.status();
const server::HostFacts facts{s.ready && s.phase == server::HostPhase::running, s.epoch,
s.simulated_tick};
sessions_->poll(facts, wall_seconds(), std::chrono::steady_clock::now());
if (facts.ready)
for (const auto& a : sessions_->carry(s.simulated_tick + m10_carry_ahead_ticks)) {
const persistence::ActionRecord record{a.account.bytes, a.action_id, a.tick,
static_cast<std::uint16_t>(a.kind)};
if (!host.submit_action(record)) // Never reached the host: final.
sessions_->dropped(std::span{&a, 1}, true);
}
std::map<std::uint64_t, std::vector<network::CarriedAction>> committed;
for (const auto& ack : host.take_action_acks())
committed[ack.commit_sequence].push_back(carried(ack.record));
for (const auto& [sequence, actions] : committed) {
sessions_->committed(actions, sequence);
std::printf("m10 committed actions=%zu commit=%llu\n", actions.size(),
static_cast<unsigned long long>(sequence));
}
for (const auto& d : host.take_dropped_actions()) {
const auto a = carried(d.record);
sessions_->dropped(std::span{&a, 1}, d.final);
std::printf("m10 dropped action=%llu final=%d\n",
static_cast<unsigned long long>(d.record.action_id), d.final ? 1 : 0);
}
for (const auto& e : sessions_->take_events())
std::printf("m10 %s session=%llu account=%s reason=%u\n",
e.kind == server::RegionSessionEvent::Kind::opened ? "opened"
: e.kind == server::RegionSessionEvent::Kind::closed ? "closed"
: "refused",
static_cast<unsigned long long>(e.session), e.account.c_str(),
static_cast<unsigned>(e.reason));
}
void shutdown() {
if (sessions_)
sessions_->shutdown(std::chrono::steady_clock::now());
}
private:
static network::CarriedAction carried(const persistence::ActionRecord& r) {
network::CarriedAction a;
a.account.bytes = r.account;
a.action_id = r.action_id;
a.tick = r.tick;
a.kind = network::ActionKind::probe;
return a;
}
static std::uint64_t wall_seconds() {
return static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch())
.count());
}
static std::optional<M10Bridge> fail(const char* message) {
std::puts(message);
return std::nullopt;
}
static std::string read(const std::string& path) {
std::ifstream file{path, std::ios::binary};
return {std::istreambuf_iterator<char>{file}, std::istreambuf_iterator<char>{}};
}
Host* host_{};
// Heap-held so the sessions' pointers stay valid when the bridge moves.
std::unique_ptr<server::PgSlot> slot_;
std::unique_ptr<server::PostgresControlLedger> control_;
std::unique_ptr<server::PostgresActionLedger> actions_;
std::unique_ptr<server::RegionSessions> sessions_; // Destroyed first: uses the ledgers.
};
#else
inline constexpr bool m10_available = false;
template <typename Host> class M10Bridge {
public:
static std::optional<M10Bridge> open(const M10Options&, Host&, persistence::RegionKey,
std::uint64_t, const persistence::PgOptions&) {
return M10Bridge{};
}
[[nodiscard]] bool enabled() const noexcept {
return false;
}
void after_step() {}
void shutdown() {}
};
#endif
} // namespace evo::server_app
+25 -1
View File
@@ -51,6 +51,7 @@
#include "evo/processes/development_opening.hpp"
#include "evo/server/ops_http.hpp"
#include "evo/server/region_host.hpp"
#include "m10_bridge.hpp"
#include "session_bridge.hpp"
#include <algorithm>
@@ -91,6 +92,7 @@ struct Options {
std::uint64_t accelerate{1};
std::uint64_t objects_reserve_mib{1024};
server_app::SessionOptions session;
server_app::M10Options m10;
};
// Kills this process without any cleanup: the real SIGKILL, at a chosen boundary.
@@ -214,6 +216,17 @@ std::optional<Options> parse(int argc, char** argv) {
else if (flag == "--session-environment" &&
(value == "dev" || value == "stage" || value == "world0"))
o.session.environment = value;
else if (flag == "--m10-listen")
o.m10.listen = value;
else if (flag == "--m10-cert")
o.m10.certificate = value;
else if (flag == "--m10-key")
o.m10.key = value;
else if (flag == "--m10-issuer")
o.m10.issuer = value;
else if (flag == "--m10-environment" &&
(value == "dev" || value == "stage" || value == "world0"))
o.m10.environment = value;
else if (flag == "--crash-at") {
const auto colon = value.find(':');
const auto ordinal =
@@ -241,6 +254,9 @@ std::optional<Options> parse(int argc, char** argv) {
if (o.session.any() &&
(!server_app::sessions_available || !o.session.complete() || !o.synthetic))
return std::nullopt;
// M10 sessions (ADR-0091/0092) need every setting and the QUIC build.
if (o.m10.any() && (!server_app::m10_available || !o.m10.complete()))
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;
@@ -365,6 +381,10 @@ template <typename State> int serve(Options& o) {
auto sessions = server_app::SessionBridge::open(o.session, o.host.key, o.host.frame);
if (!sessions)
return 2;
auto m10 = server_app::M10Bridge<server::RegionHost<State>>::open(o.m10, host, o.host.key,
o.host.frame, o.pg);
if (!m10)
return 2;
std::fflush(stdout);
const std::uint64_t lookahead = sessions->enabled() ? server_app::session_lookahead_ticks : 64;
server::OpsCounters counters;
@@ -431,6 +451,7 @@ template <typename State> int serve(Options& o) {
};
for (;;) {
if (stop_requested && !drain_deadline) {
m10->shutdown();
host.drain();
drain_deadline = std::chrono::steady_clock::now() + o.drain;
}
@@ -528,6 +549,7 @@ template <typename State> int serve(Options& o) {
if (ops)
ops->serve(handler);
sessions->step(host.state(), host.status());
m10->after_step();
std::fflush(stdout);
if (o.crash_point == "after-ack" && !acknowledged.empty() && ++ack_steps == o.crash_ordinal)
crash_now();
@@ -563,7 +585,9 @@ int main(int argc, char** argv) {
"[--ops-listen ADDRESS:PORT] [--synthetic-inputs development-v1 "
"[--synthetic-until TICK] [--accelerate N] [--session-listen ADDRESS:PORT "
"--session-cert F --session-key F --session-grant-key F "
"--session-grant-ledger F --session-environment dev]]\n",
"--session-grant-ledger F --session-environment dev]] [--m10-listen "
"ADDRESS:PORT --m10-cert F --m10-key F --m10-issuer F "
"--m10-environment dev|stage|world0]\n",
stderr);
return 2;
}
+283
View File
@@ -0,0 +1,283 @@
// M10.4c headless session client (ADR-0091, ADR-0092). Development and test only.
//
// evo-session-probe keys --out DIR
// Writes a fresh dev issuer key pair: DIR/issuer.key (32-byte P-256 scalar, owner-only)
// and DIR/issuer.pub (65-byte public key for evo-server --m10-issuer).
// evo-session-probe run --connect HOST:PORT --server-name NAME --ca FILE --issuer-key FILE
// --world N --frame N --region N --account HEX32 --serial N [--lifetime S]
// [--intents N] [--actions N --action-base ID] [--query ID]... [--timeout-ms N]
// Signs a dev grant for the account and serial, logs in over QUIC, sends N intentions
// (one per tick, two ticks ahead) and N probe actions (three ticks ahead), queries the
// given action ids, and prints one line per answer:
// welcome SESSION EPOCH TICK | intention SEQ STATUS | simulated SEQ TICK
// receipt ID STATUS TICK | outcome ID RESULT TICK COMMIT | closed REASON | done
// Statuses, results and reasons are the session-v1 wire values. Exits 0 once every
// action is final and every query answered, 1 on timeout, 3 when the server closes.
// The issuer key is a development credential: the probe refuses any environment but dev.
#include "evo/client/region_session.hpp"
#include "evo/network/session_grant.hpp"
#include <algorithm>
#include <array>
#include <charconv>
#include <chrono>
#include <cstdio>
#include <filesystem>
#include <fstream>
#include <iterator>
#include <map>
#include <optional>
#include <set>
#include <string>
#include <string_view>
#include <thread>
#include <vector>
namespace {
using namespace evo;
using namespace evo::network;
using Clock = std::chrono::steady_clock;
std::optional<std::uint64_t> number(std::string_view v) {
std::uint64_t n = 0;
const auto [end, ec] = std::from_chars(v.data(), v.data() + v.size(), n);
if (ec != std::errc{} || end != v.data() + v.size())
return std::nullopt;
return n;
}
std::string read(const std::filesystem::path& path) {
std::ifstream file{path, std::ios::binary};
return {std::istreambuf_iterator<char>{file}, std::istreambuf_iterator<char>{}};
}
bool write(const std::filesystem::path& path, std::span<const std::uint8_t> bytes) {
std::ofstream file{path, std::ios::binary | std::ios::trunc};
file.write(reinterpret_cast<const char*>(bytes.data()),
static_cast<std::streamsize>(bytes.size()));
return file.good();
}
std::optional<AccountId> account_of(std::string_view hex) {
if (hex.size() != 32)
return std::nullopt;
AccountId id;
for (std::size_t i = 0; i < 16; ++i) {
unsigned value = 0;
const auto [end, ec] =
std::from_chars(hex.data() + (2 * i), hex.data() + (2 * i) + 2, value, 16);
if (ec != std::errc{} || end != hex.data() + (2 * i) + 2)
return std::nullopt;
id.bytes.at(i) = static_cast<std::uint8_t>(value);
}
return id.valid() ? std::optional{id} : std::nullopt;
}
int keys(const std::filesystem::path& out) {
std::filesystem::create_directories(out);
for (int attempt = 0; attempt < 8; ++attempt) {
std::array<std::uint8_t, grant_private_key_bytes> scalar{};
if (!random_bytes(scalar))
return 1;
auto key = GrantSigningKey::import(DeploymentEnvironment::dev, 1, scalar);
if (!key)
continue; // Out of the curve's range: draw again.
const auto pub = key->public_key();
if (!pub || !write(out / "issuer.key", scalar) || !write(out / "issuer.pub", *pub))
return 1;
std::filesystem::permissions(out / "issuer.key",
std::filesystem::perms::owner_read |
std::filesystem::perms::owner_write,
std::filesystem::perm_options::replace);
std::puts("keys written");
return 0;
}
return 1;
}
struct RunOptions {
std::string host, server_name, ca, issuer_key;
std::uint16_t port{};
std::uint64_t world{}, frame{}, region{}, serial{}, lifetime{60}, intents{}, actions{},
action_base{1}, timeout_ms{20000};
std::optional<AccountId> account;
std::vector<std::uint64_t> queries;
};
int run(const RunOptions& o) {
const auto scalar = read(o.issuer_key);
const auto key = GrantSigningKey::import(
DeploymentEnvironment::dev, 1,
std::span{reinterpret_cast<const std::uint8_t*>(scalar.data()), scalar.size()});
if (!key) {
std::puts("error issuer-key");
return 2;
}
GrantClaims claims;
std::array<std::uint8_t, 8> id{};
if (!random_bytes(id)) {
std::puts("error random");
return 2;
}
for (std::size_t i = 0; i < id.size(); ++i)
claims.grant_id |= static_cast<std::uint64_t>(id.at(i)) << (8U * i);
claims.grant_id = std::max<std::uint64_t>(claims.grant_id, 1);
if (!o.account)
return 2; // Required at parse time.
claims.account = *o.account;
claims.serial = o.serial;
claims.world = {o.world, o.frame};
claims.region = o.region;
claims.issued_at =
static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch())
.count());
claims.expires_at = claims.issued_at + o.lifetime;
const auto grant = key->sign(claims);
if (!grant) {
std::puts("error grant");
return 2;
}
client::RegionSessionClientConfig config;
config.transport.server_address = o.host;
config.transport.port = o.port;
config.transport.server_name = o.server_name;
config.transport.authority_pem = read(o.ca);
config.transport.authority_pem_file = o.ca;
config.grant = *grant;
auto made = client::RegionSessionClient::connect(std::move(config), Clock::now());
if (!made) {
std::puts("error connect");
return 2;
}
auto& c = **made;
const auto deadline = Clock::now() + std::chrono::milliseconds{o.timeout_ms};
bool welcomed = false, queried = false;
std::uint64_t sent_intents = 0, sent_actions = 0, last_tick = 0, simulated = 0;
std::set<std::uint64_t> pending_actions, pending_queries(o.queries.begin(), o.queries.end());
for (std::uint64_t i = 0; i < o.actions; ++i)
pending_actions.insert(o.action_base + i);
while (Clock::now() < deadline) {
c.poll(Clock::now());
if (c.state() == client::RegionSessionState::closed) {
std::printf("closed %u\n", static_cast<unsigned>(c.close_reason().value_or(
SessionCloseReason::connection_lost)));
return 3;
}
if (c.state() != client::RegionSessionState::established) {
std::this_thread::sleep_for(std::chrono::milliseconds{2});
continue;
}
if (!welcomed) {
welcomed = true;
const auto& w = *c.welcome();
std::printf("welcome %llu %llu %llu\n", static_cast<unsigned long long>(w.session),
static_cast<unsigned long long>(w.epoch),
static_cast<unsigned long long>(w.server_tick));
}
const auto tick = c.server_tick();
if (tick != last_tick) {
last_tick = tick;
if (sent_intents < o.intents && c.send_intention(tick + 2, 100, 0))
++sent_intents;
if (sent_actions < o.actions &&
c.send_action(o.action_base + sent_actions, ActionKind::probe, tick + 3))
++sent_actions;
}
if (!queried) {
queried = true;
for (const auto query : o.queries)
(void)c.query_action(query);
}
for (const auto& r : c.take_intention_receipts())
std::printf("intention %llu %u\n", static_cast<unsigned long long>(r.sequence),
static_cast<unsigned>(r.status));
if (const auto s = c.simulated(); s && s->sequence != simulated) {
simulated = s->sequence;
std::printf("simulated %llu %llu\n", static_cast<unsigned long long>(s->sequence),
static_cast<unsigned long long>(s->tick));
}
for (const auto& r : c.take_action_receipts())
std::printf("receipt %llu %u %llu\n", static_cast<unsigned long long>(r.action_id),
static_cast<unsigned>(r.status), static_cast<unsigned long long>(r.tick));
for (const auto& out : c.take_action_outcomes()) {
std::printf(
"outcome %llu %u %llu %llu\n", static_cast<unsigned long long>(out.action_id),
static_cast<unsigned>(out.result), static_cast<unsigned long long>(out.tick),
static_cast<unsigned long long>(out.commit_sequence));
pending_queries.erase(out.action_id);
if (out.result == ActionResult::committed_no_effect ||
out.result == ActionResult::not_committed)
pending_actions.erase(out.action_id);
}
std::fflush(stdout);
if (sent_actions == o.actions && pending_actions.empty() && pending_queries.empty() &&
sent_intents == o.intents && simulated >= std::min<std::uint64_t>(o.intents, 1)) {
std::puts("done");
c.close();
return 0;
}
std::this_thread::sleep_for(std::chrono::milliseconds{2});
}
std::puts("timeout");
return 1;
}
} // namespace
int main(int argc, char** argv) {
const std::vector<std::string_view> args(argv + 1, argv + argc);
if (args.size() == 3 && args[0] == "keys" && args[1] == "--out")
return keys(std::filesystem::path{std::string{args[2]}});
RunOptions o;
bool ok = !args.empty() && args[0] == "run" && args.size() % 2 == 1;
for (std::size_t i = 1; ok && i + 1 < args.size(); i += 2) {
const auto flag = args[i];
const auto value = args[i + 1];
const auto n = number(value);
if (flag == "--connect") {
const auto colon = value.rfind(':');
const auto port =
colon == std::string_view::npos ? std::nullopt : number(value.substr(colon + 1));
ok = port && *port > 0 && *port <= 65535;
if (ok) {
o.host = std::string{value.substr(0, colon)};
o.port = static_cast<std::uint16_t>(*port);
}
} else if (flag == "--server-name")
o.server_name = value;
else if (flag == "--ca")
o.ca = value;
else if (flag == "--issuer-key")
o.issuer_key = value;
else if (flag == "--account")
ok = (o.account = account_of(value)).has_value();
else if (flag == "--query" && n)
o.queries.push_back(*n);
else if (n && flag == "--world")
o.world = *n;
else if (n && flag == "--frame")
o.frame = *n;
else if (n && flag == "--region")
o.region = *n;
else if (n && flag == "--serial")
o.serial = *n;
else if (n && flag == "--lifetime" && *n > 0 && *n <= 3600)
o.lifetime = *n;
else if (n && flag == "--intents" && *n <= 1000)
o.intents = *n;
else if (n && flag == "--actions" && *n <= 1000)
o.actions = *n;
else if (n && flag == "--action-base" && *n > 0)
o.action_base = *n;
else if (n && flag == "--timeout-ms" && *n > 0)
o.timeout_ms = *n;
else
ok = false;
}
if (!ok || o.host.empty() || o.server_name.empty() || o.ca.empty() || o.issuer_key.empty() ||
!o.account || o.world == 0 || o.frame == 0 || o.region == 0 || o.serial == 0) {
std::fputs("usage: evo-session-probe keys --out DIR | run --connect HOST:PORT "
"--server-name NAME --ca FILE --issuer-key FILE --world N --frame N "
"--region N --account HEX32 --serial N [--lifetime S] [--intents N] "
"[--actions N --action-base ID] [--query ID]... [--timeout-ms N]\n",
stderr);
return 2;
}
return run(o);
}