Files
aevolution/apps/evo-world-clone/main.cpp
T
dingoandClaude Opus 5.5 5e45be3b6b
Foundation / checks (checks) (push) Successful in 40m17s
Foundation / checks (deploy) (push) Successful in 3m54s
Foundation / checks (ecs) (push) Successful in 2m24s
Foundation / checks (network) (push) Successful in 18m40s
Foundation / checks (postgres) (push) Successful in 42m11s
Foundation / checks (tsan) (push) Successful in 9m39s
Foundation / checks (client) (push) Successful in 13m33s
Foundation / postgres-tools (push) Successful in 1m13s
Foundation / image (push) Successful in 3m10s
Foundation / server-image (push) Successful in 3m16s
Resolve a cellular genesis under the build that wrote it
Promoting the dev server image changed --build, and the cellular
provision rerun re-encoded the genesis manifest with the new build, so it
refused region 100/2 as a different genesis. The check could also never
pass once the head moved past genesis. Match an unadvanced cellular
genesis under its recorded build, and accept profile and frame past
genesis as the other openings do. A different commit or opening is still
refused. Add a PostgreSQL process test and record the finding in ADR-0123.
Also refresh the dev session window (3 h).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 13:14:15 -04:00

889 lines
40 KiB
C++

// M7.9 region clone and history audit tool (ADR-0067). Linux only; requires the
// PostgreSQL build.
//
// evo-world-clone clone --source-world W --source-region R --target-world W2
// [--target-region R2] --source-pg-* ... --target-pg-* ... --source-objects DIR
// --target-objects DIR --retained HEX [--retained HEX]... [--commit HEX32]
// [--audit-max N] [--crash-at before-transaction]
// evo-world-clone audit --world W --region R --pg-* ... --objects DIR --retained HEX...
// [--max-commits N]
// evo-world-clone check-store --world W --region R --pg-* ... --objects DIR
// [--reserve-mib N]
// evo-world-clone provision --world W --region R --opening
// development-v1|primitive-v1|cellular-v1
// --pg-* ... --objects DIR --build HEX [--commit HEX32] [--retain-build]
// primitive-v1 also requires --frame N --source-id N, every --funding-*-* channel,
// and one or more ordered --site X,Y values.
// cellular-v1 also requires --width-um N --height-um N --ambient-temperature-mk N
// and one or more ordered --patch X,Y,SOURCE,ENERGY,C,N,P,O values plus a stable --commit
// HEX32.
// primitive-v1 and cellular-v1 accept --life-allocator-start N (ADR-0123): the world's
// life-identity allocator is created once at the operator-audited start N; a later run
// leaves it untouched and refuses one that is below N.
// evo-world-clone compare --world A --region R --other-world B --pg-* ...
// --objects DIR [--other-objects DIR] --retained HEX... [--max-commits N]
//
// --*pg-* is host, hostaddr, port, database, user, passfile, ca, or service (a libpq
// service file whose [evolution] section supplies everything but the password file). The source
// connection needs only the runtime read functions and is switched to read-only
// transactions; the target connection is the target database's owner. Source and
// target must be different databases reached with different users: a clone never
// shares its source's credentials or authority. --audit-max N first audits up to N
// links of the source history. --crash-at (tests only) SIGKILLs this process after
// the verified target upload, before the target transaction.
//
// Output: cloned source=W/R sequence=S tick=T target=W2/R2 outcomes=N digest=HEX
// audited commits=N tick=T outcomes=N profile=P clone=0|1
// check-store (M8.3) qualifies an environment's stores from inside the cluster with
// the runtime identity. Objects: the volume must already exist (a missing or
// unmounted volume is refused), a fixed probe object is created once and verified by
// digest on every later run (so a restarted or rescheduled pod proves retention),
// and free space is reported against the reserve. Database: the adapter enforces
// server 18.6 and verify-full TLS off-host; the runtime login must reach the fenced
// functions but be refused table access.
// Output: objects available_mib=A reserve_mib=R / probe created|verified digest=HEX
// database version=V user=U ssl=S / database tables=denied functions=reachable
//
// provision (M8.4) creates a region's genesis with the owner (migrator) login, once:
// an already provisioned region is reported and never touched. --retain-build first
// records the build identity (the server image digest) in retained_builds. The only
// opening is development-v1 (evo/processes/development_opening.hpp), for dev and
// stage; World0's opening is a separate, reviewed decision.
// primitive-v1 requires operator-supplied frame/source IDs, all five finite source inventory
// channels, and ordered habitat sites; no channel, site or profile is inferred.
// Output: provisioned|already-provisioned world=W region=R sequence=S tick=T
//
// compare (M8.9, ADR-0076) fully audits both regions of one database, then requires
// the same world: equal head tick, frame, quantum and versions, identical head state
// once B is rebound to A's world, and identical durable outcomes. Builds may differ.
// Output: compared tick=T outcomes=N commits_a=N commits_b=N state=HEX
// compare differs ticks|versions|state|outcomes (exit 6)
//
// Exit: 0 done, 2 usage, 3 source/history/database failure, 4 target/objects refused,
// 5 target unknown, 6 compared histories differ.
#include "evo/biology/cellular_habitat.hpp"
#include "evo/persistence/region_clone.hpp"
#include "evo/persistence/sha256.hpp"
#include "evo/processes/development_opening.hpp"
#include "evo/server/lifecycle/cellular_lives.hpp"
#include "evo/server/lifecycle/primitive_lives.hpp"
#include <algorithm>
#include <array>
#include <charconv>
#include <csignal>
#include <cstdio>
#include <filesystem>
#include <limits>
#include <optional>
#include <span>
#include <string>
#include <string_view>
#include <sys/random.h>
#include <unistd.h>
#include <vector>
namespace {
using namespace evo;
using namespace std::chrono_literals;
constexpr std::size_t restore_budget = 8000000;
constexpr auto timeout = 10s;
struct Side {
persistence::RegionKey key{};
persistence::PgOptions pg;
std::filesystem::path objects;
};
struct Options {
std::string command;
Side source, target;
std::vector<persistence::Sha256Digest> retained;
std::optional<persistence::Uuid> commit;
std::uint64_t audit_max{}, max_commits{1u << 20U}, reserve_mib{1024};
std::string opening;
std::optional<std::uint64_t> primitive_frame, primitive_source;
std::array<std::optional<std::uint64_t>, 5> primitive_funding;
std::vector<server::SpawnSite> primitive_sites;
std::optional<std::uint32_t> cellular_width_um, cellular_height_um, cellular_temperature_mk;
std::vector<biology::CellularPatch> cellular_patches;
std::optional<std::uint64_t> life_allocator_start;
std::optional<persistence::Sha256Digest> build;
bool retain_build{};
bool crash{};
};
std::optional<std::uint64_t> number(std::string_view text) {
std::uint64_t value{};
const auto [end, error] = std::from_chars(text.data(), text.data() + text.size(), value);
if (error != std::errc{} || end != text.data() + text.size())
return std::nullopt;
return value;
}
std::optional<server::SpawnSite> site(std::string_view text) {
if (text.empty())
return std::nullopt;
const auto comma = text.find(',');
if (comma == std::string_view::npos || text.find(',', comma + 1) != std::string_view::npos)
return std::nullopt;
const auto x_text = text.substr(0, comma);
const auto y_text = text.substr(comma + 1);
std::int32_t x{}, y{};
const auto [x_end, x_error] = std::from_chars(x_text.begin(), x_text.end(), x);
const auto [y_end, y_error] = std::from_chars(y_text.begin(), y_text.end(), y);
if (x_error != std::errc{} || x_end != x_text.end() || y_error != std::errc{} ||
y_end != y_text.end())
return std::nullopt;
return server::SpawnSite{x, y};
}
std::optional<biology::CellularPatch> cellular_patch(std::string_view text) {
std::array<std::uint64_t, 8> fields{};
for (std::size_t i = 0; i < fields.size(); ++i) {
const auto comma = text.find(',');
const auto field = i + 1 == fields.size() ? text : text.substr(0, comma);
if (field.empty() || (i + 1 != fields.size() && comma == std::string_view::npos))
return std::nullopt;
const auto [end, error] =
std::from_chars(field.data(), field.data() + field.size(), fields[i]);
if (error != std::errc{} || end != field.data() + field.size())
return std::nullopt;
if (i + 1 != fields.size())
text.remove_prefix(comma + 1);
else if (comma != std::string_view::npos)
return std::nullopt;
}
if (fields[0] > std::numeric_limits<std::uint32_t>::max() ||
fields[1] > std::numeric_limits<std::uint32_t>::max())
return std::nullopt;
biology::CellularPatch result{};
result.address = {static_cast<std::uint32_t>(fields[0]), static_cast<std::uint32_t>(fields[1])};
result.source = fields[2];
result.stock.energy.value = fields[3];
for (std::size_t i = 0; i < result.stock.matter.size(); ++i)
result.stock.matter[i].value = fields[4 + i];
return result;
}
template <std::size_t N> std::optional<std::array<std::uint8_t, N>> hex(std::string_view text) {
if (text.size() != N * 2)
return std::nullopt;
std::array<std::uint8_t, N> out{};
for (std::size_t i = 0; i < N; ++i) {
unsigned value{};
const auto [end, error] =
std::from_chars(text.data() + (2 * i), text.data() + (2 * i) + 2, value, 16);
if (error != std::errc{} || end != text.data() + (2 * i) + 2)
return std::nullopt;
out[i] = static_cast<std::uint8_t>(value);
}
return out;
}
template <std::size_t N> std::string text(const std::array<std::uint8_t, N>& bytes) {
constexpr char digits[] = "0123456789abcdef";
std::string out;
for (const auto b : bytes) {
out.push_back(digits[b >> 4U]);
out.push_back(digits[b & 0x0FU]);
}
return out;
}
// Connection flags with the given prefix ("--pg-", "--source-pg-", "--target-pg-").
bool pg_flag(std::string_view flag, std::string_view prefix, std::string_view value,
persistence::PgOptions& pg) {
if (!flag.starts_with(prefix))
return false;
const auto name = flag.substr(prefix.size());
if (name == "service") {
// [evolution] section of a libpq service file; the password file stays separate.
auto service = persistence::read_pg_service(std::string(value), "evolution");
if (!service)
return false;
service->password_file = pg.password_file;
pg = *service;
return true;
}
std::string* field = name == "host" ? &pg.host
: name == "hostaddr" ? &pg.hostaddr
: name == "port" ? &pg.port
: name == "database" ? &pg.database
: name == "user" ? &pg.user
: name == "passfile" ? &pg.password_file
: name == "ca" ? &pg.ca_file
: nullptr;
if (field == nullptr)
return false;
*field = value;
return true;
}
bool complete(const Side& s) {
return s.key.world != 0 && s.key.region != 0 && !s.pg.host.empty() && !s.pg.port.empty() &&
!s.pg.database.empty() && !s.pg.user.empty() && !s.objects.empty();
}
std::optional<Options> parse(int argc, char** argv) {
if (argc < 2)
return std::nullopt;
Options o;
o.command = argv[1];
if (o.command != "clone" && o.command != "audit" && o.command != "check-store" &&
o.command != "provision" && o.command != "compare")
return std::nullopt;
const bool clone = o.command == "clone";
for (int i = 2; i < argc; ++i) {
const std::string_view flag = argv[i];
if (i + 1 >= argc)
return std::nullopt;
const std::string_view value = argv[++i];
const auto n = number(value);
if (clone && pg_flag(flag, "--source-pg-", value, o.source.pg))
continue;
if (clone && pg_flag(flag, "--target-pg-", value, o.target.pg))
continue;
if (!clone && pg_flag(flag, "--pg-", value, o.source.pg))
continue;
if (flag == "--retained") {
const auto digest = hex<32>(value);
if (!digest)
return std::nullopt;
o.retained.push_back(*digest);
} else if (flag == (clone ? "--source-world" : "--world") && n)
o.source.key.world = *n;
else if (flag == (clone ? "--source-region" : "--region") && n)
o.source.key.region = *n;
else if (((clone && flag == "--target-world") ||
(o.command == "compare" && flag == "--other-world")) &&
n)
o.target.key.world = *n;
else if (clone && flag == "--target-region" && n)
o.target.key.region = *n;
else if (flag == (clone ? "--source-objects" : "--objects"))
o.source.objects = value;
else if ((clone && flag == "--target-objects") ||
(o.command == "compare" && flag == "--other-objects"))
o.target.objects = value;
else if ((clone || o.command == "provision") && flag == "--commit" && hex<16>(value))
o.commit = hex<16>(value);
else if (clone && flag == "--audit-max" && n && *n > 0)
o.audit_max = *n;
else if (clone && flag == "--crash-at" && value == "before-transaction")
o.crash = true;
else if ((o.command == "audit" || o.command == "compare") && flag == "--max-commits" && n &&
*n > 0)
o.max_commits = *n;
else if (o.command == "check-store" && flag == "--reserve-mib" && n && *n <= 1048576)
o.reserve_mib = *n;
else if (o.command == "provision" && flag == "--opening" &&
(value == "development-v1" || value == "primitive-v1" || value == "cellular-v1"))
o.opening = value;
else if (o.command == "provision" && flag == "--frame" && n)
o.primitive_frame = n;
else if (o.command == "provision" && flag == "--source-id" && n)
o.primitive_source = n;
else if (o.command == "provision" && flag == "--funding-energy-pj" && n)
o.primitive_funding[0] = n;
else if (o.command == "provision" && flag == "--funding-carbon-fg" && n)
o.primitive_funding[1] = n;
else if (o.command == "provision" && flag == "--funding-nitrogen-fg" && n)
o.primitive_funding[2] = n;
else if (o.command == "provision" && flag == "--funding-phosphorus-fg" && n)
o.primitive_funding[3] = n;
else if (o.command == "provision" && flag == "--funding-other-fg" && n)
o.primitive_funding[4] = n;
else if (o.command == "provision" && flag == "--site") {
const auto parsed = site(value);
if (!parsed || o.primitive_sites.size() == server::max_primitive_lives)
return std::nullopt;
o.primitive_sites.push_back(*parsed);
} else if (o.command == "provision" && flag == "--width-um" && n &&
*n <= std::numeric_limits<std::uint32_t>::max())
o.cellular_width_um = static_cast<std::uint32_t>(*n);
else if (o.command == "provision" && flag == "--height-um" && n &&
*n <= std::numeric_limits<std::uint32_t>::max())
o.cellular_height_um = static_cast<std::uint32_t>(*n);
else if (o.command == "provision" && flag == "--ambient-temperature-mk" && n &&
*n <= std::numeric_limits<std::uint32_t>::max())
o.cellular_temperature_mk = static_cast<std::uint32_t>(*n);
else if (o.command == "provision" && flag == "--patch") {
const auto parsed = cellular_patch(value);
if (!parsed || o.cellular_patches.size() == biology::max_cellular_patches)
return std::nullopt;
o.cellular_patches.push_back(*parsed);
} else if (o.command == "provision" && flag == "--life-allocator-start" && n && *n > 0 &&
*n < std::numeric_limits<std::uint64_t>::max())
o.life_allocator_start = n;
else if (o.command == "provision" && flag == "--build" && hex<32>(value))
o.build = hex<32>(value);
else if (o.command == "provision" && flag == "--retain-build" && value == "true")
o.retain_build = true;
else
return std::nullopt;
}
if (clone && o.target.key.region == 0)
o.target.key.region = o.source.key.region;
if (o.command == "compare") {
if (o.target.key.world == 0)
return std::nullopt;
o.target.key.region = o.source.key.region;
o.target.pg = o.source.pg;
if (o.target.objects.empty())
o.target.objects = o.source.objects;
}
const bool needs_builds = o.command != "check-store" && o.command != "provision";
if (o.command == "provision") {
if (o.opening.empty() || !o.build)
return std::nullopt;
const bool primitive_options =
o.primitive_frame || o.primitive_source ||
std::ranges::any_of(o.primitive_funding, [](const auto& v) { return v.has_value(); }) ||
!o.primitive_sites.empty();
const bool cellular_options = o.cellular_width_um || o.cellular_height_um ||
o.cellular_temperature_mk || !o.cellular_patches.empty();
if (o.opening == "primitive-v1") {
if (!o.primitive_frame || *o.primitive_frame == 0 || !o.primitive_source ||
*o.primitive_source == 0 || o.primitive_sites.empty() ||
!std::ranges::all_of(o.primitive_funding,
[](const auto& v) { return v.has_value(); }))
return std::nullopt;
if (cellular_options)
return std::nullopt;
} else if (o.opening == "cellular-v1") {
const bool has_stock = std::ranges::any_of(o.cellular_patches, [](const auto& patch) {
return patch.stock.energy.value != 0 ||
std::ranges::any_of(patch.stock.matter,
[](const auto& amount) { return amount.value != 0; });
});
if (!o.primitive_frame || *o.primitive_frame == 0 || !o.primitive_source ||
*o.primitive_source == 0 || o.primitive_sites.empty() ||
!std::ranges::all_of(o.primitive_funding,
[](const auto& v) { return v.has_value(); }) ||
!o.cellular_width_um || !o.cellular_height_um || !o.cellular_temperature_mk ||
o.cellular_patches.empty() || !has_stock || !o.commit)
return std::nullopt;
} else if (primitive_options || cellular_options || o.life_allocator_start) {
return std::nullopt; // The development opening has no lives to allocate.
}
}
if (!complete(o.source) || (needs_builds && o.retained.empty()) ||
(clone && !complete(o.target)))
return std::nullopt;
return o;
}
[[noreturn]] void crash_now() {
std::fflush(stdout);
::kill(::getpid(), SIGKILL);
for (;;)
::pause();
}
const char* name(persistence::AuditError e) {
using enum persistence::AuditError;
switch (e) {
case store:
return "store";
case not_provisioned:
return "not_provisioned";
case bounds:
return "bounds";
case manifest:
return "manifest";
case chain:
return "chain";
case unsupported:
return "unsupported";
case missing_payload:
return "missing_payload";
case corrupt_payload:
return "corrupt_payload";
case payload:
return "payload";
case tail:
return "tail";
case history:
return "history";
}
return "unknown";
}
const char* name(persistence::CloneError e) {
using enum persistence::CloneError;
switch (e) {
case identity:
return "identity";
case session:
return "session";
case source:
return "source";
case candidate:
return "candidate";
case upload:
return "upload";
case verify:
return "verify";
case rejected:
return "rejected";
case unknown:
return "unknown";
case receipt:
return "receipt";
}
return "unknown";
}
std::unique_ptr<persistence::FilesystemObjectStore>
open_objects(const Side& s, std::size_t max_payload = persistence::max_lineage_payload_bytes) {
auto store = persistence::FilesystemObjectStore::open(s.objects, s.key, max_payload);
if (!store)
return nullptr;
return std::move(*store);
}
std::unique_ptr<persistence::Postgres> connect(const Side& s) {
auto db = persistence::Postgres::connect(s.pg, timeout);
if (!db)
return nullptr;
return std::move(*db);
}
// A fixed 64 KiB probe: the same bytes on every run, so its digest names it.
std::vector<std::uint8_t> probe_bytes() {
std::vector<std::uint8_t> out(65536);
const std::string_view marker = "aevolution-store-probe-v1";
for (std::size_t i = 0; i < out.size(); ++i)
out[i] = i < marker.size() ? static_cast<std::uint8_t>(marker[i])
: static_cast<std::uint8_t>((i * 131U) ^ (i >> 7U));
return out;
}
int provision(const Options& o) {
const bool primitive = o.opening == "primitive-v1";
const bool cellular = o.opening == "cellular-v1";
const auto primitive_frame = o.primitive_frame.value_or(0);
if ((primitive || cellular) && primitive_frame == 0)
return 2;
const auto payload_limit = cellular ? server::max_cellular_lives_bytes
: primitive ? server::max_primitive_lives_bytes
: persistence::max_lineage_payload_bytes;
auto store = open_objects(o.source, payload_limit);
if (!store) {
std::puts("objects refused");
return 4;
}
auto db = connect(o.source);
if (!db) {
std::puts("database refused");
return 3;
}
if (!o.build)
return 2; // Required by parse(); checked again for the reader.
const auto& build = *o.build;
if (o.retain_build) {
const std::array<std::string, 1> p{"\\x" + text(build)};
if (!db->query("INSERT INTO evolution.retained_builds(build_identity) VALUES ($1::bytea) "
"ON CONFLICT DO NOTHING",
p, timeout)) {
std::puts("retain-build refused");
return 3;
}
}
// ADR-0123: the world's life-identity allocator is owner state, created once at the
// operator-audited start before any genesis check, so an already provisioned region
// still gets it. It only ever advances, so one below the start is a conflict.
if (o.life_allocator_start) {
const auto start = *o.life_allocator_start;
const std::array<std::string, 2> p{std::to_string(o.source.key.world),
std::to_string(start)};
const std::array<std::string, 1> world{p[0]};
auto rows = db->query("INSERT INTO evolution.life_allocators(world_id, next_identity) "
"VALUES ($1::evolution.u64, $2::evolution.u64) "
"ON CONFLICT (world_id) DO NOTHING",
p, timeout);
if (rows)
rows = db->query("SELECT next_identity FROM evolution.life_allocators "
"WHERE world_id = $1::evolution.u64",
world, timeout);
std::optional<std::uint64_t> next;
if (rows && rows->size() == 1 && (*rows)[0].size() == 1) {
const auto& cell = (*rows)[0][0];
if (cell.has_value())
next = number(*cell);
}
if (!next) {
std::puts("life-allocator refused");
return 3;
}
if (*next < start) {
std::puts("life-allocator refused existing below start");
return 4;
}
std::printf("life-allocator world=%llu next=%llu\n",
static_cast<unsigned long long>(o.source.key.world),
static_cast<unsigned long long>(*next));
}
std::unique_ptr<server::PrimitiveLives> primitive_opening;
std::unique_ptr<server::CellularLives> cellular_opening;
if (primitive || cellular) {
server::NurseryConfig config{};
config.world = o.source.key.world;
config.region = o.source.key.region;
config.frame = primitive_frame;
config.source = o.primitive_source.value_or(0U);
config.funding.energy.value = o.primitive_funding[0].value_or(0U);
for (std::size_t i = 0; i < 4; ++i)
config.funding.matter[i].value = o.primitive_funding[i + 1].value_or(0U);
config.sites = o.primitive_sites;
auto opening = server::PrimitiveLives::create(std::move(config));
if (!opening) {
std::printf("primitive opening refused %u\n", static_cast<unsigned>(opening.error()));
return 4;
}
primitive_opening = std::move(*opening);
}
if (cellular) {
// The provision parser requires these for cellular-v1, but keep this
// execution boundary safe if an incomplete Options value is ever passed.
const auto cellular_width_um = o.cellular_width_um.value_or(0U);
const auto cellular_height_um = o.cellular_height_um.value_or(0U);
const auto cellular_temperature_mk = o.cellular_temperature_mk.value_or(0U);
biology::CellularHabitatConfig habitat_config{};
habitat_config.world = o.source.key.world;
habitat_config.region = o.source.key.region;
habitat_config.frame = primitive_frame;
habitat_config.width_um = cellular_width_um;
habitat_config.height_um = cellular_height_um;
habitat_config.patches = o.cellular_patches;
auto habitat = biology::CellularHabitat::create(std::move(habitat_config));
if (!habitat) {
std::printf("cellular habitat refused %u\n", static_cast<unsigned>(habitat.error()));
return 4;
}
auto opening = server::CellularLives::create(
*primitive_opening, std::move(*habitat),
{{cellular_width_um, cellular_height_um}, 0, cellular_temperature_mk});
if (!opening) {
std::printf("cellular opening refused %u\n", static_cast<unsigned>(opening.error()));
return 4;
}
cellular_opening = std::move(*opening);
}
const auto report = [&](const char* what, const persistence::RegionHead& head) {
std::printf("%s world=%llu region=%llu sequence=%llu tick=%llu\n", what,
static_cast<unsigned long long>(o.source.key.world),
static_cast<unsigned long long>(o.source.key.region),
static_cast<unsigned long long>(head.sequence),
static_cast<unsigned long long>(head.end_tick));
return 0;
};
persistence::Uuid commit{};
if (o.commit) {
commit = *o.commit;
} else if (::getrandom(commit.data(), commit.size(), 0) !=
static_cast<ssize_t>(commit.size())) {
return 4;
}
// Never re-provision or report success for a different profile/frame.
auto existing = persistence::load_region_checkpoint(*db, o.source.key, timeout);
if (existing) {
const auto manifest = persistence::decode_manifest(existing->manifest);
if (cellular) {
// The genesis records the build that wrote it, and image promotions change
// --build, so an unadvanced genesis is matched exactly under its own build
// (ADR-0123). Past genesis the head is no longer the genesis manifest; as for
// the other openings, the profile and frame identify the region.
if (!manifest ||
manifest->profile != persistence::CheckpointProfile::cellular_lives_v1 ||
manifest->frame != primitive_frame) {
std::puts("provision refused existing profile/frame mismatch");
return 4;
}
if (manifest->sequence > 0)
return report("already-provisioned", existing->head);
auto same_genesis = persistence::provision_cellular_region(
*db, *store, o.source.key, primitive_frame, *cellular_opening, commit,
manifest->build, timeout);
if (same_genesis)
return report("already-provisioned", same_genesis->head);
std::puts("provision refused existing cellular region; use its original --commit to "
"resolve genesis");
return 4;
}
const auto expected_profile =
primitive ? persistence::CheckpointProfile::primitive_lives_v1
: persistence::CheckpointProfile::environmental_lineage_v1;
const auto expected_frame =
primitive ? primitive_frame : processes::development_opening_frame;
if (!manifest || manifest->profile != expected_profile ||
manifest->frame != expected_frame) {
std::puts("provision refused existing profile/frame mismatch");
return 4;
}
if (primitive) {
const std::array<persistence::Sha256Digest, 1> retained{manifest->build};
auto recovered = persistence::recover_primitive_checkpoint(
*db, *store, o.source.key,
std::span<const persistence::Sha256Digest>{retained.data(), retained.size()},
timeout);
if (!recovered || recovered->state->snapshot() != primitive_opening->snapshot()) {
std::puts("provision refused existing opening mismatch");
return 4;
}
}
return report("already-provisioned", existing->head);
}
if (existing.error().kind != persistence::LeaseStoreError::not_provisioned) {
std::puts("database refused");
return 3;
}
if (cellular) {
auto made = persistence::provision_cellular_region(
*db, *store, o.source.key, primitive_frame, *cellular_opening, commit, build, timeout);
if (!made) {
std::printf("cellular provision refused %u\n",
static_cast<unsigned>(made.error().kind));
return 4;
}
return report("provisioned", made->head);
}
if (primitive) {
auto made = persistence::provision_primitive_region(
*db, *store, o.source.key, primitive_frame, *primitive_opening, commit, build, timeout);
if (!made) {
std::printf("provision refused %u\n", static_cast<unsigned>(made.error().kind));
return 4;
}
return report("provisioned", made->head);
}
auto opening = processes::development_opening(o.source.key.world);
if (!opening)
return 4;
auto made = persistence::provision_lineage_region(
*db, *store, o.source.key, processes::development_opening_frame, **opening, commit, build,
restore_budget, timeout);
if (!made) {
std::printf("provision refused %u\n", static_cast<unsigned>(made.error().kind));
return 4;
}
return report("provisioned", made->head);
}
int check_store(const Options& o) {
auto store = persistence::FilesystemObjectStore::open(o.source.objects, o.source.key,
persistence::max_lineage_payload_bytes,
nullptr, o.reserve_mib * 1024 * 1024);
if (!store) {
std::printf("objects refused %s\n", store.error() == persistence::ObjectError::invalid
? "missing-or-not-a-directory"
: "io");
return 4;
}
const auto available = (*store)->available_bytes();
std::printf(
"objects available_mib=%llu reserve_mib=%llu\n",
static_cast<unsigned long long>(available.value_or(0) / (std::uint64_t{1024} * 1024)),
static_cast<unsigned long long>(o.reserve_mib));
const auto bytes = probe_bytes();
const auto digest = persistence::sha256(bytes);
if (!digest)
return 4;
const persistence::VerifiedObject probe{*digest, bytes.size()};
const char* state = "verified";
if (auto existing = (*store)->get(probe); !existing) {
if (existing.error() != persistence::ObjectError::missing) {
std::printf("probe %s\n", existing.error() == persistence::ObjectError::corrupt
? "corrupt"
: "unreadable");
return 4;
}
auto stored = (*store)->put(bytes);
if (!stored) {
std::printf("probe refused %s\n",
stored.error() == persistence::ObjectError::full ? "full" : "io");
return 4;
}
auto back = (*store)->get(probe);
if (!back || *back != bytes)
return 4;
state = "created";
}
std::printf("probe %s digest=%s\n", state, text(*digest).c_str());
std::fflush(stdout);
auto db = persistence::Postgres::connect(o.source.pg, timeout);
if (!db) {
const auto kind = db.error().kind;
std::printf("database refused %s\n", kind == persistence::PgError::version ? "version"
: kind == persistence::PgError::config ? "config"
: kind == persistence::PgError::connect ? "connect"
: "other");
return 3;
}
auto about = (*db)->query(
"SELECT pg_catalog.current_setting('server_version_num'), current_user::text, "
"coalesce((SELECT s.ssl::text || '/' || coalesce(s.version, '') FROM "
"pg_catalog.pg_stat_ssl s WHERE s.pid = pg_catalog.pg_backend_pid()), 'unknown')",
{}, timeout);
if (!about || about->size() != 1 || (*about)[0].size() != 3)
return 3;
const auto& row = (*about)[0];
std::printf("database version=%s user=%s ssl=%s\n", row[0].value_or("?").c_str(),
row[1].value_or("?").c_str(), row[2].value_or("?").c_str());
auto tables = (*db)->query("SELECT 1 FROM evolution.region_heads LIMIT 1", {}, timeout);
const bool denied = !tables && std::string_view(tables.error().sqlstate.data(), 5) == "42501";
auto head = persistence::load_region_head(**db, o.source.key, timeout);
const bool reachable =
head || head.error().kind == persistence::LeaseStoreError::not_provisioned;
std::printf("database tables=%s functions=%s head=%s\n", denied ? "denied" : "READABLE",
reachable ? "reachable" : "unreachable",
head ? std::to_string(head->sequence).c_str() : "none");
return denied && reachable ? 0 : 3;
}
int audit(const Options& o) {
auto db = connect(o.source);
auto objects = open_objects(o.source);
if (!db || !objects) {
std::fputs("audit failed store\n", stderr);
return 3;
}
auto result = persistence::audit_region_history(*db, *objects, o.source.key, o.retained,
restore_budget, o.max_commits, timeout);
if (!result) {
std::fprintf(stderr, "audit failed %s sequence=%llu\n", name(result.error().kind),
static_cast<unsigned long long>(result.error().sequence));
return 3;
}
std::printf("audited commits=%llu tick=%llu outcomes=%llu profile=%u clone=%d\n",
static_cast<unsigned long long>(result->commits),
static_cast<unsigned long long>(result->end_tick),
static_cast<unsigned long long>(result->outcomes),
static_cast<unsigned>(result->profile), result->clone ? 1 : 0);
return 0;
}
const char* name(persistence::CompareError e) {
using enum persistence::CompareError;
switch (e) {
case identity:
return "identity";
case audit:
return "audit";
case ticks:
return "ticks";
case versions:
return "versions";
case state:
return "state";
case outcomes:
return "outcomes";
}
return "unknown";
}
int compare(const Options& o) {
auto db = connect(o.source);
auto objects_a = open_objects(o.source);
auto objects_b = open_objects(o.target);
if (!db || !objects_a || !objects_b) {
std::fputs("compare failed store\n", stderr);
return 3;
}
auto result = persistence::compare_region_histories(*db, *objects_a, o.source.key, *db,
*objects_b, o.target.key, o.retained,
restore_budget, o.max_commits, timeout);
if (!result) {
const auto& f = result.error();
using persistence::CompareError;
if (f.kind == CompareError::identity || f.kind == CompareError::audit) {
std::fprintf(stderr, "compare failed %s side=%c audit=%s sequence=%llu\n", name(f.kind),
f.side == 0 ? '-' : f.side, name(f.audit.kind),
static_cast<unsigned long long>(f.audit.sequence));
return f.kind == CompareError::identity ? 2 : 3;
}
std::printf("compare differs %s\n", name(f.kind));
return 6;
}
std::printf("compared tick=%llu outcomes=%llu commits_a=%llu commits_b=%llu state=%s\n",
static_cast<unsigned long long>(result->tick),
static_cast<unsigned long long>(result->outcomes),
static_cast<unsigned long long>(result->commits_a),
static_cast<unsigned long long>(result->commits_b), text(result->state).c_str());
return 0;
}
int clone(const Options& o) {
const auto& s = o.source.pg;
const auto& t = o.target.pg;
if ((s.host == t.host && s.hostaddr == t.hostaddr && s.port == t.port &&
s.database == t.database) ||
s.user == t.user) {
std::fputs("clone refused: source and target need separate databases and users\n", stderr);
return 2;
}
auto source = connect(o.source);
auto source_objects = open_objects(o.source);
if (!source || !source_objects) {
std::fputs("clone failed source store\n", stderr);
return 3;
}
if (o.audit_max > 0) {
auto audited =
persistence::audit_region_history(*source, *source_objects, o.source.key, o.retained,
restore_budget, o.audit_max, timeout);
if (!audited) {
std::fprintf(stderr, "clone failed audit %s sequence=%llu\n",
name(audited.error().kind),
static_cast<unsigned long long>(audited.error().sequence));
return 3;
}
}
auto target = connect(o.target);
auto target_objects = open_objects(o.target);
if (!target || !target_objects) {
std::fputs("clone failed target store\n", stderr);
return 4;
}
persistence::Uuid commit{};
if (o.commit)
commit = *o.commit;
else if (::getrandom(commit.data(), commit.size(), 0) != static_cast<ssize_t>(commit.size()))
return 4;
persistence::CloneHooks hooks;
if (o.crash)
hooks.before_transaction = [] { crash_now(); };
auto cloned =
persistence::clone_region(*source, *source_objects, o.source.key, *target, *target_objects,
o.target.key, commit, o.retained, restore_budget, timeout, hooks);
if (!cloned) {
const auto kind = cloned.error().kind;
std::fprintf(stderr, "clone failed %s\n", name(kind));
using persistence::CloneError;
return kind == CloneError::source || kind == CloneError::session ? 3
: kind == CloneError::unknown ? 5
: 4;
}
std::printf("cloned source=%llu/%llu sequence=%llu tick=%llu target=%llu/%llu outcomes=%zu "
"digest=%s\n",
static_cast<unsigned long long>(o.source.key.world),
static_cast<unsigned long long>(o.source.key.region),
static_cast<unsigned long long>(cloned->origin.sequence),
static_cast<unsigned long long>(cloned->origin.tick),
static_cast<unsigned long long>(o.target.key.world),
static_cast<unsigned long long>(o.target.key.region), cloned->inherited_outcomes,
text(cloned->genesis.head.digest).c_str());
return 0;
}
} // namespace
int main(int argc, char** argv) {
const auto options = parse(argc, argv);
if (!options) {
std::fputs("usage: evo-world-clone clone|audit|check-store|provision|compare ... "
"(see apps/evo-world-clone/main.cpp)\n",
stderr);
return 2;
}
if (options->command == "check-store")
return check_store(*options);
if (options->command == "provision")
return provision(*options);
if (options->command == "compare")
return compare(*options);
return options->command == "clone" ? clone(*options) : audit(*options);
}