// 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 to cellular-v10 // --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 to cellular-v6 also require --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. cellular-v2 is the same opening on rules 3 (ADR-0124: // engulfment, detritus); cellular-v3 is it on rules 4 (ADR-0125: budding wildlife); // cellular-v4 on rules 5 (ADR-0128: offline freeze and resume). // cellular-v5 on rules 6 (2026-10-15 balance: finer mutations, cheaper construction). // cellular-v6 on rules 7 (ADR-0137: percentage mutations, skilled budding, twins). // cellular-v7 on rules 8 (ADR-0136 step A: pools and bubbles fed by contact) takes no // --patch: one or more --pool X,Y,RADIUS,SOURCE,ENERGY,C,N,P,O (centres, any micrometre) // and/or --motes COUNT,SOURCE,ENERGY,C,N,P,O (bubbles per channel amount, scattered // from the --commit bytes, so a genesis is reproducible). // cellular-v8 (ADR-0176) also requires --light-pj N. cellular-v9 (rules 12, ADR-0177) // is cellular-v8 followed by the liquid upgrade: it also requires --tuning FILE (the Git // tuning file, whose region size must match --width-um/--height-um) and accepts // --current-seed N (default: derived from the --commit bytes). cellular-v10 (rules 13, // ADR-0179) is cellular-v9 followed by the revamp upgrade: it also requires // --revamp-tuning FILE (the Git revamp tuning file). // A nonzero playtest.starting_wildlife_cells (ADR-0180) additionally requires // --playtest-wildlife-first-id N: an audited unused range below --life-allocator-start. // primitive-v1 and cellular-v1/v2/v3 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/biology/cellular_revamp_tuning.hpp" #include "evo/persistence/checkpoint_store.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/cellular_play_summary.hpp" #include "evo/server/lifecycle/primitive_lives.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include 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 retained; std::optional commit; std::uint64_t audit_max{}, max_commits{1u << 20U}, reserve_mib{1024}; std::string opening; std::optional primitive_frame, primitive_source; std::array, 5> primitive_funding; std::vector primitive_sites; std::optional cellular_width_um, cellular_height_um, cellular_temperature_mk; std::vector cellular_patches; std::vector cellular_pools; // cellular-v7 std::optional cellular_motes; // cellular-v7 and v8 std::optional cellular_mixed; // Mixed bubbles (v7 and later) std::optional light_pj; // cellular-v8/v9: pJ per square um per tick std::optional tuning_path; // cellular-v9: the Git tuning file std::optional current_seed; std::optional tuning; // Parsed from tuning_path. std::optional revamp_tuning_path; // cellular-v10 std::optional revamp_tuning; // Parsed from it. std::optional playtest_wildlife_first_id; // Audited DEV-only reserved range. std::optional life_allocator_start; std::optional build; bool retain_build{}; bool crash{}; }; // The openings that declare light: cellular-v8, cellular-v9 which adds the liquid upgrade, and // cellular-v10 which adds the revamp upgrade after it. bool lit_opening(std::string_view opening) { return opening == "cellular-v8" || opening == "cellular-v9" || opening == "cellular-v10"; } bool liquid_opening(std::string_view opening) { return opening == "cellular-v9" || opening == "cellular-v10"; } std::optional 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 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 cellular_patch(std::string_view text) { std::array 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::max() || fields[1] > std::numeric_limits::max()) return std::nullopt; biology::CellularPatch result{}; result.address = {static_cast(fields[0]), static_cast(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; } // Comma-separated unsigned fields, exactly N of them. template std::optional> fields_of(std::string_view text) { std::array fields{}; for (std::size_t i = 0; i < N; ++i) { const auto comma = text.find(','); const auto field = i + 1 == N ? text : text.substr(0, comma); if (field.empty() || (i + 1 != N && comma == std::string_view::npos)) return std::nullopt; const auto [end, error] = std::from_chars(field.data(), field.data() + field.size(), fields.at(i)); if (error != std::errc{} || end != field.data() + field.size()) return std::nullopt; if (i + 1 != N) text.remove_prefix(comma + 1); else if (comma != std::string_view::npos) return std::nullopt; } return fields; } // --pool X,Y,RADIUS,SOURCE,ENERGY,C,N,P,O std::optional cellular_pool(std::string_view text) { const auto f = fields_of<9>(text); if (!f || (*f)[0] > std::numeric_limits::max() || (*f)[1] > std::numeric_limits::max() || (*f)[2] > biology::max_cellular_deposit_radius_um) return std::nullopt; biology::CellularPatch pool{}; pool.address = {static_cast((*f)[0]), static_cast((*f)[1])}; pool.radius_um = static_cast((*f)[2]); pool.source = (*f)[3]; pool.stock.energy.value = (*f)[4]; for (std::size_t i = 0; i < pool.stock.matter.size(); ++i) pool.stock.matter.at(i).value = (*f)[5 + i]; return pool; } // --motes COUNT,SOURCE,ENERGY,C,N,P,O std::optional cellular_motes(std::string_view text) { const auto f = fields_of<7>(text); if (!f || (*f)[0] > biology::max_cellular_patches) return std::nullopt; biology::CellularMoteField field{}; field.count = static_cast((*f)[0]); field.source = (*f)[1]; field.amount.energy.value = (*f)[2]; for (std::size_t i = 0; i < field.amount.matter.size(); ++i) field.amount.matter.at(i).value = (*f)[3 + i]; return field; } template std::optional> hex(std::string_view text) { if (text.size() != N * 2) return std::nullopt; std::array 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(value); } return out; } template std::string text(const std::array& 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 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" && o.command != "summary") 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" || value == "cellular-v2" || value == "cellular-v3" || value == "cellular-v4" || value == "cellular-v5" || value == "cellular-v6" || value == "cellular-v7" || value == "cellular-v8" || value == "cellular-v9" || value == "cellular-v10")) o.opening = value; else if (o.command == "provision" && flag == "--mixed-motes" && cellular_motes(value)) o.cellular_mixed = cellular_motes(value); else if (o.command == "provision" && flag == "--revamp-tuning" && !value.empty()) o.revamp_tuning_path = std::string{value}; else if (o.command == "provision" && flag == "--playtest-wildlife-first-id") { o.playtest_wildlife_first_id = number(value); if (!o.playtest_wildlife_first_id || *o.playtest_wildlife_first_id == 0) return std::nullopt; } else if (o.command == "provision" && flag == "--light-pj" && n && *n > 0) o.light_pj = n; else if (o.command == "provision" && flag == "--tuning" && !value.empty()) o.tuning_path = std::string{value}; else if (o.command == "provision" && flag == "--current-seed" && n) o.current_seed = n; 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::max()) o.cellular_width_um = static_cast(*n); else if (o.command == "provision" && flag == "--height-um" && n && *n <= std::numeric_limits::max()) o.cellular_height_um = static_cast(*n); else if (o.command == "provision" && flag == "--ambient-temperature-mk" && n && *n <= std::numeric_limits::max()) o.cellular_temperature_mk = static_cast(*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 == "--pool") { const auto parsed = cellular_pool(value); if (!parsed || o.cellular_pools.size() == biology::max_cellular_patches) return std::nullopt; o.cellular_pools.push_back(*parsed); } else if (o.command == "provision" && flag == "--motes") { const auto parsed = cellular_motes(value); if (!parsed || o.cellular_motes) return std::nullopt; o.cellular_motes = parsed; } else if (o.command == "provision" && flag == "--life-allocator-start" && n && *n > 0 && *n < std::numeric_limits::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" && o.command != "summary"; 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() || !o.cellular_pools.empty() || o.cellular_motes; 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-v7" || lit_opening(o.opening)) { // Pools and bubbles only: square patches belong to the earlier openings. Only // cellular-v8 to v10 (ADR-0176) declare light; v9 and v10 take a tuning file, and // v10 the revamp tuning file. if (lit_opening(o.opening) != o.light_pj.has_value() || liquid_opening(o.opening) != o.tuning_path.has_value() || (o.opening == "cellular-v10") != o.revamp_tuning_path.has_value() || (o.current_seed && !liquid_opening(o.opening))) return std::nullopt; if (o.revamp_tuning_path) { std::ifstream file(*o.revamp_tuning_path, std::ios::binary); const std::string text{std::istreambuf_iterator(file), std::istreambuf_iterator()}; if (!file.good() && !file.eof()) { std::printf("revamp tuning file unreadable: %s\n", o.revamp_tuning_path->c_str()); return std::nullopt; } auto parsed = biology::parse_cellular_revamp_tuning(text); if (!parsed) { std::printf("revamp tuning file refused at line %zu (%s): %s\n", parsed.error().line, parsed.error().key.c_str(), std::string{biology::describe(parsed.error().kind)}.c_str()); return std::nullopt; } o.revamp_tuning = *parsed; const auto count = parsed->playtest_starting_wildlife_cells; if (count != 0 && (!o.playtest_wildlife_first_id || !o.life_allocator_start || *o.playtest_wildlife_first_id == 0 || *o.playtest_wildlife_first_id >= *o.life_allocator_start || static_cast(count) > *o.life_allocator_start - *o.playtest_wildlife_first_id)) return std::nullopt; } if (o.tuning_path) { std::ifstream file(*o.tuning_path, std::ios::binary); const std::string text{std::istreambuf_iterator(file), std::istreambuf_iterator()}; auto parsed = biology::parse_cellular_tuning(text); if (!file.good() && !file.eof()) { std::printf("tuning file unreadable: %s\n", o.tuning_path->c_str()); return std::nullopt; } if (!parsed) { std::printf("tuning file refused at line %zu (%s): %s\n", parsed.error().line, parsed.error().key.c_str(), std::string{biology::describe(parsed.error().kind)}.c_str()); return std::nullopt; } if (!o.cellular_width_um || !o.cellular_height_um || parsed->region_width_um != *o.cellular_width_um || parsed->region_height_um != *o.cellular_height_um) { std::puts("tuning region size differs from --width-um/--height-um"); return std::nullopt; } o.tuning = *parsed; } 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() || (o.cellular_pools.empty() && (!o.cellular_motes || o.cellular_motes->count == 0)) || !o.commit) return std::nullopt; } else if (o.opening == "cellular-v1" || o.opening == "cellular-v2" || o.opening == "cellular-v3" || o.opening == "cellular-v4" || o.opening == "cellular-v5" || o.opening == "cellular-v6") { if (!o.cellular_pools.empty() || o.cellular_motes) return std::nullopt; 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 (o.playtest_wildlife_first_id && o.opening != "cellular-v10") return std::nullopt; 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 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 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 probe_bytes() { std::vector 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(marker[i]) : static_cast((i * 131U) ^ (i >> 7U)); return out; } // cellular-v9 (rules 12): the liquid rules from the tuning file, for the opening's area and // a current seed (given, or derived from the genesis commit's bytes 8-15). std::optional liquid_rules(const Options& o) { if (!liquid_opening(o.opening) || !o.tuning || !o.cellular_width_um || !o.cellular_height_um) return std::nullopt; std::uint64_t seed = o.current_seed.value_or(0); if (!o.current_seed) { const auto commit = o.commit.value_or(persistence::Uuid{}); for (std::size_t i = 8; i < 16; ++i) seed = (seed << 8U) | commit.at(i); } return server::cellular_liquid_rules(*o.tuning, {*o.cellular_width_um, *o.cellular_height_um}, seed); } // cellular-v10 (rules 13): the revamp rules from the revamp tuning file. std::optional revamp_rules(const Options& o) { if (o.opening != "cellular-v10" || !o.revamp_tuning) return std::nullopt; return server::cellular_revamp_rules(*o.revamp_tuning); } // ADR-0176 cellular-v8: a cellular-v7 genesis taken through one empty tick and the durable // bundle 9 -> 10 -> 11 -> 12 -> 13 upgrades, declaring the region's light. cellular-v9 adds the // 13 -> 14 liquid upgrade and cellular-v10 the 14 -> 15 revamp upgrade. Resumable: it continues // from whichever bundle the head has reached, and does nothing at its target bundle. int finish_photosynthesis_opening(const Options& o, persistence::Postgres& db, const persistence::FilesystemObjectStore& store, std::uint64_t frame, const persistence::Sha256Digest& build, std::uint64_t light_pj, const std::optional& liquid, const std::optional& revamp) { const auto& key = o.source.key; std::uint32_t wildlife_count{}; if (o.revamp_tuning) wildlife_count = static_cast(o.revamp_tuning->playtest_starting_wildlife_cells); const auto target = revamp ? persistence::cellular_v15_versions : liquid ? persistence::cellular_v14_versions : persistence::cellular_v13_versions; // A finished opening needs no lease: a rerun (every Argo sync's provision hook) runs // while the region server holds it, and must report the region rather than fail. if (const auto current = persistence::load_region_checkpoint(db, key, timeout); current) { const auto manifest = persistence::decode_manifest(current->manifest); if (manifest && manifest->profile == persistence::CheckpointProfile::cellular_lives_v1 && manifest->frame == frame && manifest->cellular_versions == target) return 0; } auto lease = persistence::acquire_region_lease(db, key, build, std::chrono::seconds{60}, timeout); if (!lease) { std::puts("photosynthesis opening lease refused"); return 3; } const auto finish = [&](int code) { // A failed release only delays the next owner until the lease expires. if (!persistence::release_region_lease(db, key, *lease, timeout)) std::puts("photosynthesis opening lease left to expire"); return code; }; auto head = persistence::load_region_checkpoint(db, key, timeout); if (!head) return finish(3); for (unsigned step = 0; step < 8; ++step) { const auto manifest = persistence::decode_manifest(head->manifest); if (!manifest || manifest->profile != persistence::CheckpointProfile::cellular_lives_v1 || manifest->frame != frame) return finish(4); const auto& versions = manifest->cellular_versions; if (versions == target) return finish(0); const auto id = [&] { persistence::Uuid made{}; if (::getrandom(made.data(), made.size(), 0) != static_cast(made.size())) made = {}; return made; }(); if (id == persistence::Uuid{}) return finish(4); if (manifest->sequence == 0) { // Upgrades follow a committed tick, never the genesis itself. const std::array builds{build}; auto opening = persistence::recover_cellular_checkpoint(db, store, key, *lease, builds, timeout); if (!opening) return finish(4); const server::CellularBatchHeader h{key.world, key.region, frame, head->head.commit, head->head.digest, head->head.end_tick, head->head.end_tick + 1}; const server::CellularBatch tick{h, {{}}}; auto next = server::replay_cellular_batch(*opening->state, tick); if (!next) return finish(4); auto published = persistence::publish_cellular_batch(db, store, *lease, *head, tick, **next, id, build, timeout); if (!published) return finish(4); head = std::move(published->committed); continue; } std::expected upgraded = std::unexpected(persistence::CellularProfileMigrationFailure{}); if (versions == persistence::cellular_v9_versions) upgraded = persistence::migrate_cellular_boundary_profile(db, store, *lease, *head, id, build, timeout); else if (versions == persistence::cellular_v10_versions) upgraded = persistence::migrate_cellular_maturation_profile(db, store, *lease, *head, id, build, timeout); else if (versions == persistence::cellular_v11_versions) upgraded = persistence::migrate_cellular_inheritance_profile(db, store, *lease, *head, id, build, timeout); else if (versions == persistence::cellular_v12_versions) upgraded = persistence::migrate_cellular_photosynthesis_profile( db, store, *lease, *head, id, build, {light_pj, 1, {std::numeric_limits::max() / 2}}, timeout); else if (versions == persistence::cellular_v13_versions && liquid) upgraded = persistence::migrate_cellular_liquid_profile(db, store, *lease, *head, id, build, *liquid, timeout); else if (versions == persistence::cellular_v14_versions && liquid && revamp) upgraded = persistence::migrate_cellular_revamp_profile( db, store, *lease, *head, id, build, *revamp, timeout, {}, wildlife_count, wildlife_count == 0 ? 0 : o.playtest_wildlife_first_id.value_or(0)); else return finish(4); if (!upgraded) { std::printf("photosynthesis opening upgrade refused %u\n", static_cast(upgraded.error().kind)); return finish(4); } head = std::move(*upgraded); } return finish(4); } int provision(const Options& o) { const bool primitive = o.opening == "primitive-v1"; const bool cellular = o.opening == "cellular-v1" || o.opening == "cellular-v2" || o.opening == "cellular-v3" || o.opening == "cellular-v4" || o.opening == "cellular-v5" || o.opening == "cellular-v6" || o.opening == "cellular-v7" || lit_opening(o.opening); 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 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 p{std::to_string(o.source.key.world), std::to_string(start)}; const std::array 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 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(o.source.key.world), static_cast(*next)); } std::unique_ptr primitive_opening; std::unique_ptr 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(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; if (o.opening == "cellular-v7" || lit_opening(o.opening)) { // ADR-0136 step A: pools and bubbles scattered from the genesis commit's bytes. auto field = o.cellular_motes.value_or(biology::CellularMoteField{0, 1, {}, 0}); if (const auto& mixed = o.cellular_mixed) { // Mixed bubbles hold every nutrient of the --mixed-motes amounts at once. if (!o.cellular_motes) field.source = mixed->source; field.mixed_count = mixed->count; field.mixed = mixed->amount; } // Liquid openings: a pool rests in still water, never in a current. auto pools = o.cellular_pools; if (const auto liquid = liquid_rules(o)) { const auto& c = liquid->currents; const auto flow = biology::CellularFlowField::generate( {c.seed, c.width_um, c.height_um, c.pitch_um, c.min_count, c.max_count, c.min_route_width_um, c.max_route_width_um, c.min_speed_um_per_s, c.max_speed_um_per_s}); if (!flow) { std::puts("cellular currents refused"); return 4; } for (auto& pool : pools) { const auto moved = biology::cellular_still_centre(*flow, pool.address, pool.radius_um); if (moved != pool.address) std::printf("pool %u,%u moved out of a current to %u,%u\n", pool.address.x_um, pool.address.y_um, moved.x_um, moved.y_um); pool.address = moved; } } const auto commit = o.commit.value_or(persistence::Uuid{}); for (std::size_t i = 0; i < 8; ++i) field.seed = (field.seed << 8U) | commit.at(i); auto scattered = biology::scatter_deposits(habitat_config, pools, field); if (!scattered) { std::printf("cellular habitat refused %u\n", static_cast(scattered.error())); return 4; } habitat_config = std::move(*scattered); } auto habitat = biology::CellularHabitat::create(std::move(habitat_config)); if (!habitat) { std::printf("cellular habitat refused %u\n", static_cast(habitat.error())); return 4; } auto opening = server::CellularLives::create( *primitive_opening, std::move(*habitat), {{cellular_width_um, cellular_height_um}, 0, cellular_temperature_mk}, o.opening == "cellular-v7" || lit_opening(o.opening) ? server::CellularLivesProfile::foraging : o.opening == "cellular-v6" ? server::CellularLivesProfile::reproduction : o.opening == "cellular-v5" ? server::CellularLivesProfile::balance : o.opening == "cellular-v4" ? server::CellularLivesProfile::presence : o.opening == "cellular-v3" ? server::CellularLivesProfile::budding : o.opening == "cellular-v2" ? server::CellularLivesProfile::engulf : server::CellularLivesProfile::offers); if (!opening) { std::printf("cellular opening refused %u\n", static_cast(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(o.source.key.world), static_cast(o.source.key.region), static_cast(head.sequence), static_cast(head.end_tick)); return 0; }; persistence::Uuid commit{}; if (o.commit) { commit = *o.commit; } else if (::getrandom(commit.data(), commit.size(), 0) != static_cast(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 (lit_opening(o.opening)) { if (manifest->sequence == 0 && !persistence::provision_cellular_region(*db, *store, o.source.key, primitive_frame, *cellular_opening, commit, manifest->build, timeout)) { std::puts("provision refused existing cellular region; use its original " "--commit to resolve genesis"); return 4; } if (const auto finished = finish_photosynthesis_opening( o, *db, *store, primitive_frame, build, o.light_pj.value_or(0), liquid_rules(o), revamp_rules(o)); finished != 0) return finished; auto head = persistence::load_region_checkpoint(*db, o.source.key, timeout); return head ? report("already-provisioned", head->head) : 3; } 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 retained{manifest->build}; auto recovered = persistence::recover_primitive_checkpoint( *db, *store, o.source.key, std::span{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(made.error().kind)); return 4; } if (lit_opening(o.opening)) { if (const auto finished = finish_photosynthesis_opening( o, *db, *store, primitive_frame, build, o.light_pj.value_or(0), liquid_rules(o), revamp_rules(o)); finished != 0) return finished; auto head = persistence::load_region_checkpoint(*db, o.source.key, timeout); return head ? report("provisioned", head->head) : 3; } 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(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(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(available.value_or(0) / (std::uint64_t{1024} * 1024)), static_cast(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; } // M11.9 (ADR-0130): read-only, account-free play aggregates of a cellular region's // committed head, for playtest review. Nothing is written. int summary(const Options& o) { auto db = connect(o.source); auto objects = open_objects(o.source, server::max_cellular_lives_bytes); if (!db || !objects) { std::fputs("summary failed store\n", stderr); return 3; } const auto head = persistence::load_region_checkpoint(*db, o.source.key, timeout); const auto manifest = head ? persistence::decode_manifest(head->manifest) : std::expected{ std::unexpected{persistence::CheckpointError::format}}; if (!manifest || manifest->profile != persistence::CheckpointProfile::cellular_lives_v1) { std::fputs("summary failed head\n", stderr); return 3; } const auto payload = objects->get({manifest->payload_sha256, manifest->payload_bytes}); auto state = payload ? persistence::restore_cellular_payload(*payload, *manifest) : std::unexpected{persistence::CheckpointError::payload}; const auto play = state ? server::summarize_cellular_play(**state) : std::unexpected{server::CellularLivesError::format}; if (!play) { std::fputs("summary failed state\n", stderr); return 3; } std::printf("cellular summary world=%llu region=%llu sequence=%llu tick=%llu\n", static_cast(o.source.key.world), static_cast(o.source.key.region), static_cast(manifest->sequence), static_cast(play->tick)); std::printf("living owned=%llu wildlife=%llu lingering=%llu suspended=%llu\n", static_cast(play->owned), static_cast(play->wildlife), static_cast(play->lingering), static_cast(play->suspended)); std::printf("deaths total=%llu starved=%llu engulfed=%llu unrecorded=%llu owned=%llu " "wildlife=%llu\n", static_cast(play->deaths), static_cast(play->starved), static_cast(play->engulfed), static_cast(play->unrecorded), static_cast(play->owned_deaths), static_cast(play->wildlife_deaths)); std::printf("revamp merged=%llu emerged=%llu organisms=%llu largest=%llu offers_pending=%llu " "offers_earned=%llu spent_fg=%llu\n", static_cast(play->merged), static_cast(play->emerged), static_cast(play->organisms), static_cast(play->largest_organism), static_cast(play->offers_pending), static_cast(play->offers_earned), static_cast(play->spent_matter_fg)); std::printf("lifespan_ticks min=%llu median=%llu max=%llu\n", static_cast(play->lifespan_min), static_cast(play->lifespan_median), static_cast(play->lifespan_max)); std::printf("evolution buds=%llu mutations=%llu living_genomes=%llu deepest_lineage=%llu\n", static_cast(play->buds), static_cast(play->mutations), static_cast(play->living_genomes), static_cast(play->deepest_lineage)); return 0; } 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(result.error().sequence)); return 3; } std::printf("audited commits=%llu tick=%llu outcomes=%llu profile=%u clone=%d\n", static_cast(result->commits), static_cast(result->end_tick), static_cast(result->outcomes), static_cast(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(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(result->tick), static_cast(result->outcomes), static_cast(result->commits_a), static_cast(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(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(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(o.source.key.world), static_cast(o.source.key.region), static_cast(cloned->origin.sequence), static_cast(cloned->origin.tick), static_cast(o.target.key.world), static_cast(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|summary ... " "(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); if (options->command == "summary") return summary(*options); return options->command == "clone" ? clone(*options) : audit(*options); }