Files
aevolution/apps/evo-session-probe/main.cpp
T
dingoandClaude Opus 5.5 0bf9901c7f
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
Add M10.4c evo-server QUIC listener and headless session client
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>
2026-10-04 09:20:55 -04:00

284 lines
12 KiB
C++

// 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);
}