Files
aevolution/tests/region_m10_session_tests.cpp
T
dingoandClaude Opus 5.5 1ba6748288
Foundation / checks (deploy) (push) Successful in 3m16s
Foundation / checks (ecs) (push) Successful in 1m54s
Foundation / checks (client) (push) Successful in 12m39s
Foundation / checks (network) (push) Successful in 12m56s
Foundation / checks (tsan) (push) Successful in 7m52s
Foundation / checks (checks) (push) Successful in 32m28s
Foundation / checks (postgres) (push) Failing after 26m52s
Foundation / postgres-tools (push) Skipped
Foundation / server-image (push) Skipped
Foundation / image (push) Skipped
Add M10.9b evo-load multi-client load generator and process smoke
ADR-0097. evo-load runs bots on worker threads, each a RegionSessionClient
with its own account and an in-process dev grant: one intention per tick
at its input lead with marker prediction, actions every 2 s, interest
moves every 10 s, and re-login after any close. Stepped ramps report
pooled and per-client distributions (response, receipt, outcome, login,
prediction error), snapshot rate, gaps and bandwidth, drops, failed
logins and refusals, generator CPU, a starvation check among connected
bots, a budget verdict and the region's evo_m10_* gauges, ending with the
sustainable bot count. A Postgres process smoke runs 4 then 8 bots
against a real evo-server.

Fixes found while ramping: the process fixture's short pumps never read
(a chatty child blocked on its stdout pipe), and the load governor
refused on a single tick of debt (now 2 refuse, 4 shed). Refreshes the
dev session window.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 09:02:57 -04:00

499 lines
23 KiB
C++

// Real process tests for M10.4c (ADR-0091, ADR-0092): evo-server with PostgreSQL, synthetic
// ticks and the QUIC session listener; in-process RegionSessionClients and the headless
// probe. Intentions get receipts and the simulated watermark; actions get durable outcomes
// that survive SIGKILL after their commit, and resolve as "no record" (then commit once on
// retry) after SIGKILL before it. Region 7/151. Run by tools/ci/postgres-check.sh
// when the build has EVO_BUILD_QUIC.
#include "evo/client/region_session.hpp"
#include "evo/network/dev_credentials.hpp"
#include "evo/network/session_grant.hpp"
#include "fixtures/server_process.hpp"
#include <algorithm>
#include <arpa/inet.h>
#include <array>
#include <chrono>
#include <fcntl.h>
#include <functional>
#include <netinet/in.h>
#include <optional>
#include <source_location>
#include <sstream>
#include <string>
#include <sys/socket.h>
#include <thread>
#include <unistd.h>
void run_region_m10_session_tests();
namespace {
using namespace evo::test::process;
using namespace evo;
using namespace evo::network;
using client::RegionSessionClient;
using client::RegionSessionState;
using evo::test::region_identity;
void check(bool value, std::source_location at = std::source_location::current()) {
if (!value)
throw std::runtime_error("region m10 session test line " + std::to_string(at.line()));
}
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
template <typename T>
const T& must(const std::optional<T>& o,
std::source_location at = std::source_location::current()) {
if (!o)
throw std::runtime_error("region m10 session missing value line " +
std::to_string(at.line()));
return *o;
}
constexpr const char* name = "dev-region.evolution.test";
void write_file(const fs::path& path, std::span<const std::uint8_t> bytes) {
const int fd = ::open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);
expect(fd >= 0 &&
::write(fd, bytes.data(), bytes.size()) == static_cast<ssize_t>(bytes.size()) &&
::close(fd) == 0,
"write file");
}
void write_text(const fs::path& path, const std::string& text) {
write_file(path, {reinterpret_cast<const std::uint8_t*>(text.data()), text.size()});
}
AccountId account(std::uint8_t tag) {
AccountId a;
a.bytes.fill(tag);
return a;
}
std::string hex(const AccountId& a) {
constexpr char digits[] = "0123456789abcdef";
std::string out;
for (const auto b : a.bytes) {
out.push_back(digits[b >> 4U]);
out.push_back(digits[b & 0xFU]);
}
return out;
}
// GET /metrics from the ops listener; the value of an unlabelled series (ADR-0097 adds the
// evo_m10_* families when the listener is enabled).
std::optional<std::uint64_t> metric(std::uint16_t port, const std::string& series) {
const int fd = ::socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
expect(fd >= 0, "socket");
sockaddr_in a{};
a.sin_family = AF_INET;
a.sin_port = htons(port);
a.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
timeval timeout{3, 0};
::setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof timeout);
std::string raw;
if (::connect(fd, reinterpret_cast<const sockaddr*>(&a), sizeof a) == 0) {
const std::string request = "GET /metrics HTTP/1.1\r\nHost: test\r\n\r\n";
if (::send(fd, request.data(), request.size(), MSG_NOSIGNAL) ==
static_cast<ssize_t>(request.size())) {
std::array<char, 4096> chunk{};
for (ssize_t n = 0; (n = ::recv(fd, chunk.data(), chunk.size(), 0)) > 0;)
raw.append(chunk.data(), static_cast<std::size_t>(n));
}
}
::close(fd);
const auto at = raw.find("\n" + series + " ");
if (at == std::string::npos)
return std::nullopt;
return std::stoull(raw.substr(at + series.size() + 2));
}
// One region's credentials, issuer key and server process arguments.
struct Setup {
ServerRegion region;
std::string ca_pem;
std::optional<GrantSigningKey> issuer;
explicit Setup(std::uint64_t r, std::uint8_t genesis) : region{provision(r, genesis)} {
const auto made = generate_dev_credentials(DeploymentEnvironment::dev, name, 1,
std::chrono::system_clock::now());
expect(made.has_value(), "credentials");
ca_pem = made->authority_pem;
write_text(region.root / "ca.pem", made->authority_pem);
write_text(region.root / "server.pem", made->server_certificate_pem);
write_text(region.root / "server.key", made->server_key_pem);
for (int attempt = 0; attempt < 8 && !issuer; ++attempt) {
std::array<std::uint8_t, grant_private_key_bytes> scalar{};
expect(random_bytes(scalar).has_value(), "random");
if (auto key = GrantSigningKey::import(DeploymentEnvironment::dev, 1, scalar)) {
write_file(region.root / "issuer.key", scalar);
write_file(region.root / "issuer.pub", *key->public_key());
issuer.emplace(std::move(*key));
}
}
expect(issuer.has_value(), "issuer key");
}
[[nodiscard]] std::vector<std::string> args() const {
return server_args(
region,
{"--synthetic-inputs", "development-v1", "--ops-listen", "127.0.0.1:0", "--m10-listen",
"127.0.0.1:0", "--m10-cert", (region.root / "server.pem").string(), "--m10-key",
(region.root / "server.key").string(), "--m10-issuer",
(region.root / "issuer.pub").string(), "--m10-environment", "dev",
"--m10-max-grant-lifetime", "900", "--session-window",
"test-" +
std::to_string(static_cast<std::uint64_t>(
std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch())
.count())) +
":" +
std::to_string(static_cast<std::uint64_t>(
std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch())
.count()) +
1200)});
}
[[nodiscard]] WireGrant grant(const AccountId& who, std::uint64_t serial) const {
GrantClaims c;
c.grant_id = 9000 + serial;
c.account = who;
c.serial = serial;
c.world = {region.key.world, region_identity.frame};
c.region = region.key.region;
c.issued_at =
static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch())
.count());
c.expires_at = c.issued_at + 120;
const auto g = must(issuer).sign(c);
expect(g.has_value(), "grant");
return *g;
}
};
// A running evo-server and its QUIC port, its output drained while waiting.
struct Server {
std::unique_ptr<Child> child;
std::uint16_t port{}, ops{};
explicit Server(const Setup& s) : child{std::make_unique<Child>(s.args())} {
CHECK(child->wait_line("m10 listening ", 30s));
for (const auto& l : child->lines()) {
if (starts(l, "m10 listening "))
port = static_cast<std::uint16_t>(std::stoul(l.substr(14)));
if (starts(l, "ops listening "))
ops = static_cast<std::uint16_t>(std::stoul(l.substr(14)));
}
CHECK(ops != 0);
CHECK(port != 0 && child->wait_line("ready 1", 30s));
}
void pump() {
child->pump(1ms);
}
void kill() {
child->signal(SIGKILL);
CHECK(child->wait_exit(30s) == 128 + SIGKILL);
}
};
std::unique_ptr<RegionSessionClient> connect(const Setup& s, Server& server,
const WireGrant& grant) {
client::RegionSessionClientConfig c;
c.transport.port = server.port;
c.transport.server_name = name;
c.transport.authority_pem = s.ca_pem;
c.transport.authority_pem_file = (s.region.root / "ca.pem").string();
c.grant = grant;
auto made = RegionSessionClient::connect(std::move(c), std::chrono::steady_clock::now());
CHECK(made.has_value());
return std::move(*made);
}
// Polls client and server until `done`, at most `limit`.
bool until(Server& server, RegionSessionClient& c, const std::function<bool()>& done,
std::chrono::milliseconds limit = 20s) {
const auto end = Clock::now() + limit;
while (Clock::now() < end) {
c.poll(Clock::now());
server.pump();
if (done())
return true;
}
return done();
}
bool established(Server& server, RegionSessionClient& c) {
return until(server, c,
[&] {
return c.state() == RegionSessionState::established ||
c.state() == RegionSessionState::closed;
}) &&
c.state() == RegionSessionState::established;
}
std::optional<ActionOutcome> outcome_of(Server& server, RegionSessionClient& c, std::uint64_t id,
bool final_only) {
std::optional<ActionOutcome> found;
until(server, c, [&] {
for (const auto& o : c.take_action_outcomes())
if (o.action_id == id &&
(!final_only || o.result == ActionResult::committed_no_effect ||
o.result == ActionResult::not_committed))
found = o;
return found.has_value();
});
return found;
}
std::optional<ActionReceipt> receipt_of(Server& server, RegionSessionClient& c, std::uint64_t id) {
std::optional<ActionReceipt> found;
until(server, c, [&] {
for (const auto& r : c.take_action_receipts())
if (r.action_id == id)
found = r;
return found.has_value();
});
return found;
}
// "tick:commit" of the durable record of (account, id) in world 7, or empty.
std::string record(const AccountId& who, std::uint64_t id) {
const std::array<std::string, 2> p{hex(who), std::to_string(id)};
auto rows =
owner()->query("SELECT tick || ':' || commit_sequence FROM evolution.region_actions "
"WHERE world_id = 7 AND account_id = $1::uuid AND "
"action_id = $2::evolution.u64",
p, 5s);
CHECK(rows.has_value());
return rows->empty() ? std::string{} : (*rows)[0][0].value_or("");
}
void end_to_end() {
Setup s{151, 0xC5};
const auto alice = account(0x51), bob = account(0x52);
auto server = std::make_unique<Server>(s);
auto a = connect(s, *server, s.grant(alice, 1));
CHECK(established(*server, *a));
// Snapshots of the real region (ADR-0093): the default interest is the first habitat
// site with radius 7; every cell lies in one slice and one 15 x 15 square.
const auto first_tick = a->server_tick();
CHECK(until(*server, *a, [&] { return a->view() && !a->view()->environment.empty(); }));
const auto shown = a->environment();
CHECK(shown && !shown->cells().empty() && shown->cells().size() <= 225);
const auto [min_x, max_x] =
std::ranges::minmax(shown->cells(), {}, [](const auto& c) { return c.voxel.x; });
const auto [min_y, max_y] =
std::ranges::minmax(shown->cells(), {}, [](const auto& c) { return c.voxel.y; });
CHECK(max_x.voxel.x - min_x.voxel.x <= 14 && max_y.voxel.y - min_y.voxel.y <= 14);
CHECK(std::ranges::all_of(shown->cells(),
[&](const auto& c) { return c.voxel.z == shown->slice_z(); }));
CHECK(until(*server, *a, [&] { return a->server_tick() >= first_tick + 40; }));
// M10.9a (ADR-0097): the listener's load is exposed; a lightly loaded region measures
// per-tick work, accepts logins and sheds nothing.
const auto sessions = metric(server->ops, "evo_m10_sessions");
const auto p95 = metric(server->ops, "evo_m10_tick_work_p95_microseconds");
std::printf("m10 load report sessions=%llu p95_us=%llu p99_us=%llu session_p99_us=%llu\n",
static_cast<unsigned long long>(sessions.value_or(0)),
static_cast<unsigned long long>(p95.value_or(0)),
static_cast<unsigned long long>(
metric(server->ops, "evo_m10_tick_work_p99_microseconds").value_or(0)),
static_cast<unsigned long long>(
metric(server->ops, "evo_m10_session_work_p99_microseconds").value_or(0)));
CHECK(sessions == 1u && p95.value_or(0) > 0 && metric(server->ops, "evo_m10_refusing") == 0u &&
metric(server->ops, "evo_m10_shed_level") == 0u &&
metric(server->ops, "evo_m10_refused_logins_total") == 0u);
std::printf("m10 snapshot report cells=%zu ticks=%llu chunks=%llu bytes=%llu\n",
shown->cells().size(),
static_cast<unsigned long long>(a->server_tick() - first_tick),
static_cast<unsigned long long>(a->snapshot_chunks()),
static_cast<unsigned long long>(a->snapshot_bytes()));
// Intentions: receipts, then the watermark.
// Datagrams may be refused until the QUIC datagram extension is negotiated: retry.
std::optional<std::uint64_t> sequence;
CHECK(until(*server, *a, [&] {
return a->server_tick() > 0 &&
(sequence = a->send_intention(a->server_tick() + 2, 100, 0)).has_value();
}));
CHECK(sequence == 1u);
std::vector<IntentionReceipt> receipts;
CHECK(until(*server, *a, [&] { return !(receipts = a->take_intention_receipts()).empty(); }));
CHECK(receipts[0].sequence == 1 && receipts[0].status == AdmitStatus::admitted);
CHECK(until(*server, *a, [&] { return a->simulated() && a->simulated()->sequence >= 1; }));
// Action: receipt, then a final outcome naming the durable commit.
CHECK(a->send_action(1, ActionKind::probe, a->server_tick() + 3));
const auto r1 = receipt_of(*server, *a, 1);
CHECK(r1 && must(r1).status == AdmitStatus::admitted);
const auto o1 = outcome_of(*server, *a, 1, true);
CHECK(o1 && must(o1).result == ActionResult::committed_no_effect &&
must(o1).tick == must(r1).tick && must(o1).commit_sequence > 0);
CHECK(record(alice, 1) ==
std::to_string(must(o1).tick) + ":" + std::to_string(must(o1).commit_sequence));
// SIGKILL after the commit (the record exists), whether or not the outcome was sent.
CHECK(a->send_action(2, ActionKind::probe, a->server_tick() + 3));
const auto r2 = receipt_of(*server, *a, 2);
CHECK(r2 && must(r2).status == AdmitStatus::admitted);
const bool recorded = until(*server, *a, [&] { return !record(alice, 2).empty(); });
if (!recorded)
for (const auto& line : server->child->lines())
if (starts(line, "m10") || starts(line, "phase") || starts(line, "dropped"))
std::printf("server: %s\n", line.c_str());
CHECK(recorded);
server->kill();
server = std::make_unique<Server>(s); // Waits out the old lease.
auto b = connect(s, *server, s.grant(alice, 2));
CHECK(established(*server, *b));
CHECK(b->query_action(2));
const auto o2 = outcome_of(*server, *b, 2, false);
CHECK(o2 && must(o2).result == ActionResult::committed_no_effect &&
record(alice, 2) ==
std::to_string(must(o2).tick) + ":" + std::to_string(must(o2).commit_sequence));
// A retry of a committed action on the new process: duplicate, same outcome.
CHECK(b->send_action(1, ActionKind::probe, b->server_tick() + 3));
const auto d1 = receipt_of(*server, *b, 1);
CHECK(d1 && must(d1).status == AdmitStatus::duplicate && must(d1).tick == must(o1).tick);
CHECK(outcome_of(*server, *b, 1, true) == o1);
// SIGKILL before the commit: the action's tick has not run when the process dies.
CHECK(b->send_action(3, ActionKind::probe, b->server_tick() + 8));
const auto r3 = receipt_of(*server, *b, 3);
CHECK(r3 && must(r3).status == AdmitStatus::admitted);
server->kill();
CHECK(record(alice, 3).empty());
server = std::make_unique<Server>(s);
auto c = connect(s, *server, s.grant(alice, 3));
CHECK(established(*server, *c));
CHECK(c->query_action(3));
const auto unknown = outcome_of(*server, *c, 3, false);
CHECK(unknown && unknown->result == ActionResult::unknown); // No record: never committed.
CHECK(c->send_action(3, ActionKind::probe, c->server_tick() + 3)); // Safe to retry.
CHECK(must(receipt_of(*server, *c, 3)).status == AdmitStatus::admitted);
const auto o3 = outcome_of(*server, *c, 3, true);
CHECK(o3 && must(o3).result == ActionResult::committed_no_effect && !record(alice, 3).empty());
// The headless probe: login, intentions, actions and a query, then a clean exit. After the
// restarts the load governor (ADR-0097) may still refuse logins while the host catches up;
// the probe does not retry, so wait until it accepts.
CHECK(until(*server, *c, [&] { return metric(server->ops, "evo_m10_refusing") == 0u; }));
const auto probe = fs::path(env("EVO_SERVER_BIN")).parent_path() / "evo-session-probe";
Child p{{probe.string(), "run",
"--connect", "127.0.0.1:" + std::to_string(server->port),
"--server-name", name,
"--ca", (s.region.root / "ca.pem").string(),
"--issuer-key", (s.region.root / "issuer.key").string(),
"--world", "7",
"--frame", std::to_string(region_identity.frame),
"--region", "151",
"--account", hex(bob),
"--serial", "1",
"--lifetime", "900", // Over the default 300 s: allowed by the server flag.
"--intents", "3",
"--actions", "2",
"--action-base", "10",
"--query", "10",
"--snapshots", "8"}};
int code = -1;
for (const auto end = Clock::now() + 30s; Clock::now() < end && code < 0;) {
server->pump();
p.pump(5ms);
if (p.printed("done") || p.printed("timeout") || p.printed("closed"))
code = p.wait_exit(10s);
}
const bool probed = code == 0 && p.printed("welcome ") && p.printed("intention 1 0") &&
p.printed("outcome 10 1 ") && p.printed("outcome 11 1 ") &&
p.printed("snapshot ") && p.printed("snapshot-bytes ");
if (!probed) {
for (const auto& line : p.lines())
if (!starts(line, "snapshot ") && !starts(line, "marker "))
std::printf("probe: %s\n", line.c_str());
for (const auto& line : server->child->lines())
if (starts(line, "m10 load") || starts(line, "m10 refused") || starts(line, "phase"))
std::printf("server: %s\n", line.c_str());
}
CHECK(probed);
CHECK(!record(bob, 10).empty() && !record(bob, 11).empty());
// The probe's marker (ADR-0094): its intentions (100, 0) were applied and moved it +x.
std::vector<std::array<long long, 4>> marks; // tick, x, y, applied
for (const auto& line : p.lines())
if (line.starts_with("marker ")) {
std::array<long long, 4> m{};
std::istringstream in{line.substr(7)};
in >> m[0] >> m[1] >> m[2] >> m[3];
if (in)
marks.push_back(m);
}
CHECK(marks.size() >= 2 && marks.back()[3] >= 1 && marks.back()[1] > marks.front()[1] &&
marks.back()[2] == marks.front()[2]);
server->child->signal(SIGTERM);
CHECK(server->child->wait_exit(30s) == 0);
CHECK(server->child->printed("m10 opened ") && server->child->printed("m10 committed ") &&
server->child->printed("m10 motion area "));
}
// M10.9b (ADR-0097): evo-load against a real evo-server, two steps with the region's
// metrics. A smoke test of the tool and the server under concurrent sessions, not
// qualification (only evolution-dev runs count): it checks that every bot logs in, that
// every step measures responses, outcomes and snapshots for its bots, and that the report
// is complete. The lines are kept in the log as a development reference.
void load_smoke() {
Setup s{152, 0xC6};
Server server{s};
const auto tool = fs::path(env("EVO_SERVER_BIN")).parent_path() / "evo-load";
Child load{{tool.string(),
"--connect",
"127.0.0.1:" + std::to_string(server.port),
"--server-name",
name,
"--ca",
(s.region.root / "ca.pem").string(),
"--issuer-key",
(s.region.root / "issuer.key").string(),
"--world",
"7",
"--frame",
std::to_string(region_identity.frame),
"--region",
"152",
"--steps",
"4,8",
"--hold",
"8",
"--warmup",
"3",
"--ramp-ms",
"50",
"--threads",
"2",
"--lifetime",
"900",
"--metrics",
"127.0.0.1:" + std::to_string(server.ops),
"--per-client"}};
int code = -1;
for (const auto end = Clock::now() + 120s; Clock::now() < end && code < 0;) {
server.pump();
load.pump(5ms);
if (load.printed("load result "))
code = load.wait_exit(30s);
}
for (const auto& line : load.lines())
if (starts(line, "load "))
std::printf("%s\n", line.c_str());
CHECK(code == 0);
std::size_t steps = 0;
for (const auto& line : load.lines()) {
if (!starts(line, "load step "))
continue;
++steps;
// Every bot logged in once: 4 in the first step, 4 more in the second.
CHECK((starts(line, "load step N=4 established=4 logins=4 ") ||
starts(line, "load step N=8 established=8 logins=4 ")) &&
line.find(" drops=0 login_failures=0 refused=0 ") != std::string::npos);
}
const auto has = [&](std::string_view prefix) {
return std::ranges::count_if(load.lines(), [&](const std::string& l) {
return starts(l, std::string{prefix});
}) == 2;
};
CHECK(steps == 2 && has("load response_ms ") && has("load outcome_ms ") &&
has("load snapshots ") && has("load server ") && has("load verdict "));
CHECK(std::ranges::none_of(load.lines(), [](const std::string& l) {
return starts(l, "load response_ms") && l.find(" n=0 ") != std::string::npos;
}));
CHECK(std::ranges::count_if(
load.lines(), [](const std::string& l) { return starts(l, "load client "); }) == 12);
server.child->signal(SIGTERM);
CHECK(server.child->wait_exit(30s) == 0);
}
} // namespace
void run_region_m10_session_tests() {
end_to_end();
load_smoke();
std::puts("region m10 session integration passed");
}