diff --git a/CMakeLists.txt b/CMakeLists.txt index 40f09f9..5657cf2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -104,7 +104,7 @@ if(EVO_BUILD_POSTGRES) # SHA-256 via system OpenSSL libcrypto (already required by libpq TLS; ADR-0061). find_package(OpenSSL 3 REQUIRED COMPONENTS Crypto) add_library(evo_region_store STATIC src/persistence/sha256.cpp src/persistence/object_store.cpp - src/persistence/checkpoint_store.cpp) + src/persistence/checkpoint_store.cpp src/persistence/region_clone.cpp) add_library(evo::region_store ALIAS evo_region_store) target_link_libraries(evo_region_store PUBLIC evo::postgres evo::region_checkpoint PRIVATE OpenSSL::Crypto) @@ -117,14 +117,18 @@ if(EVO_BUILD_POSTGRES) add_executable(evo-server apps/evo-server/main.cpp) target_link_libraries(evo-server PRIVATE evo::region_host) evo_owned_target(evo-server) + # M7.9 clone and history audit tool (ADR-0067). + add_executable(evo-world-clone apps/evo-world-clone/main.cpp) + target_link_libraries(evo-world-clone PRIVATE evo::region_store) + evo_owned_target(evo-world-clone) if(BUILD_TESTING) # Contention tests use one thread and one connection per contender. add_executable(evo-postgres-tests tests/postgres_tests.cpp tests/region_lease_store_tests.cpp tests/region_checkpoint_store_tests.cpp tests/region_host_tests.cpp tests/region_server_process_tests.cpp tests/region_recovery_tests.cpp - tests/region_ops_tests.cpp) + tests/region_ops_tests.cpp tests/region_clone_tests.cpp) target_link_libraries(evo-postgres-tests PRIVATE evo::region_host Threads::Threads) - add_dependencies(evo-postgres-tests evo-server) # Spawned by the process tests. + add_dependencies(evo-postgres-tests evo-server evo-world-clone) # Spawned by process tests. evo_owned_target(evo-postgres-tests) # Intentionally not an ordinary CTest: tools/ci/postgres-check.sh owns its private DB. endif() diff --git a/apps/evo-world-clone/main.cpp b/apps/evo-world-clone/main.cpp new file mode 100644 index 0000000..ffa2a43 --- /dev/null +++ b/apps/evo-world-clone/main.cpp @@ -0,0 +1,333 @@ +// 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] +// +// --*pg-* is host, hostaddr, port, database, user, passfile, ca. 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 +// Exit: 0 done, 2 usage, 3 source/history failure, 4 target refused, 5 target unknown. +#include "evo/persistence/region_clone.hpp" + +#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}; + bool crash{}; +}; + +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; +} +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()); + 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") + 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" && 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.target.objects = value; + else if (clone && 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 (!clone && flag == "--max-commits" && n && *n > 0) + o.max_commits = *n; + else + return std::nullopt; + } + if (clone && o.target.key.region == 0) + o.target.key.region = o.source.key.region; + if (!complete(o.source) || 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) { + auto store = persistence::FilesystemObjectStore::open(s.objects, s.key, + persistence::max_lineage_payload_bytes); + 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); +} + +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; +} + +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 ... (see apps/evo-world-clone/main.cpp)\n", + stderr); + return 2; + } + return options->command == "clone" ? clone(*options) : audit(*options); +} diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index 36ae547..77cd22c 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -584,3 +584,12 @@ M7.8 implemented and locally qualified (2026-09-28), under ADR-0066. - The `server-runtime` image is non-root with no secrets; a CI smoke job and a Kubernetes example are included. Next: M7.9 clone, migrate and qualify durability. + +M7.9a implemented and locally qualified (2026-09-29), under ADR-0067: +- a verified committed head is cloned into another world and database, as a + re-identified clone genesis that names its source cut; +- outcomes and ancestry are inherited; +- the source session is read-only, and clone failures are atomic; +- a full-chain retention audit is added. +Next: M7.9b, covering the upgrade barrier and old-writer fencing, migration +failure while serving, and measured recovery and batching latency. diff --git a/docs/STATUS.md b/docs/STATUS.md index 5cdd026..fff97c7 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -901,3 +901,30 @@ Linux ran 59/59 in four configurations; the PostgreSQL suites passed in three. Docker itself was not run locally; an emulated image layout was checked instead. Next: M7.9. + +## M7.9a region clones and retained history (2026-09-29) + +ADR-0067 adds a consistent clone into an isolated world: +- **Re-identification:** `rebind_world` changes only the recorded world, and the + byte round trip is proven. +- **Schema v7 clone genesis:** it starts at the source tick, names the source + commit and digest, and digests the inherited outcomes. +- **`evo-world-clone clone`:** it reads the source read-only, requires a separate + database and user, and writes the target in one owner transaction. + +`audit_region_history` (also `evo-world-clone audit`) re-verifies every retained +link. + +Real processes showed: +- after re-identification, the clone and its source followed a byte-identical + trajectory; +- the clone cannot write its source; +- a killed clone left the source untouched and the target not ready; +- a missing historical trace is detected even when head recovery succeeds. + +Five mutations were caught. Windows ran 59/59; Linux ran 59/59 in four +configurations; the PostgreSQL suites passed in three. + +Next: M7.9b, covering the upgrade barrier and old-writer fencing, schema +migration failure while a host is serving, and measured recovery and batching +latency. diff --git a/docs/adr/0067-region-clones.md b/docs/adr/0067-region-clones.md new file mode 100644 index 0000000..ff643d8 --- /dev/null +++ b/docs/adr/0067-region-clones.md @@ -0,0 +1,111 @@ +# ADR-0067: Region clones and retained-history audit + +## Status +Accepted for M7.9a, 2026-09-29. It builds on ADR-0058 (durability contract), +ADR-0061/0062/0063 (checkpoints, lineage, batches) and ADR-0065 (recovery). Region +migration, sealing and durability qualification are M7.9b. + +## Context +M7.9 requires a consistent clone into an isolated world identity, retained +durable history and a baseline-versus-restored trajectory. The named risks are a +clone that shares production credentials and rules that change on restore. +OPERATIONS.md says a clone gets isolated world and runtime identities while +keeping source provenance. ADR-0044 already makes generated content depend on +descriptors, not the world ID. + +Every payload records its world ID in nested codecs: the envelope configuration, +the transfer header, the field store and the terrain snapshot. Recovery restores +it under the manifest's world. A clone into another world therefore has to +re-identify the payload; copying bytes is not enough. Sequence 0 used to require +tick 0, so a cut at tick N had no representation. + +## Decision +**Re-identification (portable).** `EnvironmentalCommunity::rebind_world` and +`LineageRegion::rebind_world` change only the recorded world ID and keep the +frame. That ID lives in the configuration and the field store; terrain snapshots +take it from the field store. The archive, binding, histories and every quantity +are identity-free, and no rule reads the world ID. Tests show that: +- only the new identity restores the rebound bytes; +- continuation reports match; +- rebinding back yields the original bytes exactly. + +**Clone genesis (manifest v2, schema v7).** A clone is sequence 0 of the target +region in another world. +- It starts at the source tick: `begin = end = source end tick`. +- Its predecessor fields name the source commit and the SHA-256 of its manifest. +- Its decision count and digest cover the inherited permanent outcomes. It has no + trace, holder or epoch. +- Profile, versions, quantum, frame and build are inherited. A clone never + converts versions. + +A provisioned genesis is unchanged (tick 0, no origin, no outcomes), and manifest +v1 cannot carry an origin. Earlier builds refuse clone manifests as malformed, so +they fail closed. Every later link chains from the clone genesis, so the whole +clone history transitively commits to the source cut. + +**Database (migration 0007).** +- The `region_heads` constraint becomes "sequence 0 or tick > 0". +- `region_origins` holds one immutable row per clone region. +- The parser accepts clone genesis. `provision_region_checkpoint` refuses origins + and clone worlds. +- The owner-only `provision_region_clone` enforces: + - the target world differs from the source world; + - a world is either original or a clone of exactly one source world; + - when the source is in the same database, the origin is exactly its committed + link (sequence, commit, digest, tick, versions, quantum, profile); + - inherited outcomes match the manifest digest, with each actor dying at most + once. +- It records the outcomes as sequence-0 decisions with no outbox rows: the source + already delivered them, and a clone has no production integrations. +- The runtime role gains nothing. + +**Clone (`clone_region`, Linux).** +1. Refuse a zero ID or the source world. +2. Switch the source session to read-only transactions, and confirm it. +3. Recover the source head exactly as readiness does: manifest, payload, tail + replay and outcomes. +4. Re-identify the payload. Restore it as the target, rebind it back, and require + the verified source bytes. +5. Upload to the target store with verified read-back. +6. Provision in one owner transaction, then compare the receipt. + +A failure before step 6 leaves at most target orphans. The source is never +written. A cut read without a lease ignores outcomes of links committed after the +head was read; recovery applies the same rule. + +**Tool (`evo-world-clone`).** Subcommands `clone` and `audit`. The source and +target need different databases and different users. The source user needs only +the runtime read functions. The target user is the target database's owner. An +optional full history audit runs first. The `--crash-at before-transaction` flag +is for tests only. + +**Retention audit (`audit_region_history`).** Verifies every link from genesis to +the head: +- the digest, position and chain; +- the retained build; +- that the payload is present and restores; +- that the trace replays the predecessor's payload to exactly this link's bytes; +- that the outcomes are exactly those derived; +- that the head state agrees with all outcomes. + +It is read-only, needs no lease, and is bounded by `max_commits`. Head recovery +alone does not prove retention; the audit does. + +## Consequences +- Tests covered clones between separate databases with separate runtime roles. + The same-database path is checked by the database too. +- History is still never deleted. Garbage collection must later treat clone + origins, heads and audit reachability as pins. +- Clone throughput and history-audit cost grow with payload size and chain length; + M7.9b measures them. +- A clone of a clone is allowed; each origin names its immediate source. + +## Alternatives rejected +- **Copying the whole source chain into the new world.** Every manifest digest + would change, so the copy would not be the source's history. Holders and epochs + would also leak across authority domains. +- **Keeping the source world ID and separating the durable namespace.** That + needs a new manifest field. Two worlds with one simulation identity also + defeats isolation. +- **Recording provenance only in a table.** The chain would not commit to its + origin, and cross-database clones could not verify it. diff --git a/docs/adr/README.md b/docs/adr/README.md index f338b9d..aa42952 100644 --- a/docs/adr/README.md +++ b/docs/adr/README.md @@ -77,3 +77,4 @@ design. No unadopted candidate is a link dependency of the proof. - [ADR-0064: Region host](0064-region-host.md) - accepted for M7.6 single-owner host state machine, bounded admission, final-only-after-commit and evo-server process. - [ADR-0065: Recovery before readiness](0065-recovery-before-readiness.md) - accepted for M7.7 head tail replay verification, schema v6 commit lookup and real kill-boundary tests. - [ADR-0066: Operations endpoints](0066-operations-endpoints.md) - accepted for M7.8 liveness vs authority readiness, bounded metrics, dependency-free responder and the server image. +- [ADR-0067: Region clones and retained-history audit](0067-region-clones.md) - accepted for M7.9a re-identified clone genesis naming its source cut, isolated clone databases and full-chain retention audit. diff --git a/docs/architecture/REGION_DURABILITY.md b/docs/architecture/REGION_DURABILITY.md index 98aab3f..e82a68f 100644 --- a/docs/architecture/REGION_DURABILITY.md +++ b/docs/architecture/REGION_DURABILITY.md @@ -220,3 +220,18 @@ after commit but before acknowledgement, and after acknowledgement. A SIGSTOPped owner resumed fenced: it was not ready, refused admission and acknowledged nothing new. Corrupt payloads and missing traces quarantined with no fallback. Host loss and clone or migration remain M8 and M7.9. + +M7.9a (ADR-0067) clones a verified committed head into another world and +database: +- The payload is re-identified, and the byte round trip is proven. +- The clone genesis names the source commit and digest, and inherits the source's + outcomes and ancestry. +- The source session is read-only. +- A clone killed before its target transaction left the source untouched and the + target unprovisioned (it quarantined rather than serve), and a retry completed. +- The same inputs from the cut reproduced the source trajectory exactly. + +`audit_region_history` re-verifies every retained link across owners and kills. +It names the exact link whose evidence is missing, even though head recovery +still succeeds. Migration, fencing of old writers, and measured recovery and +batching latency are still M7.9b. diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index f43566b..e5510e8 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -2422,3 +2422,66 @@ Only clean-to-v1 and exact-v1 restart are supported migration paths at this gate the workflow YAML parses; `bash -n` passed for the smoke script. - Not executed: the container build and smoke, the Gitea job, cluster apply, Prometheus scraping, and TSan. + +## M7.9a region clones and retained history - 2026-09-29 + +- Windows MSVC Release CTest passed 59/59. `lineage-region-contracts` now covers + rebinding: only the new identity restores, continuation matches, and rebinding + back is byte-exact. `region-checkpoint-contracts` covers the clone-genesis + manifest rules. +- WSL Linux `checks` set (GCC14 Debug and Release, clang19 analysis, clang19 + ASan/UBSan) passed CTest 59/59 each. The format check passed. +- The PostgreSQL suites passed in GCC Release, clang-tidy analysis and ASan/UBSan. + These runs used a second database, `evo_clone`, with its own runtime group and + login. clang-tidy first flagged branch-clone in the tool's option parsing and + optional access in the new tests; both were fixed, then the suites were rebuilt + and rerun. +- Harness additions: + - A populated v6 database (a v2 genesis) upgrades in place to v7. The replaced + head constraint is `region_heads_published_tick`, and the v7 parser reads the + stored genesis. + - Upgrade paths now end at 1..7. + - Version and checksum drift checks now include v7. +- Clone tests (real `evo-server` and `evo-world-clone` processes): + - Trajectory: world 7 was run to tick 30, with a death at tick 20, then cloned + to world 8 in `evo_clone`. The clone recovered under the clone runtime at + tick 30, with the inherited death and retained ancestry, and the audit found + one clone link naming the source cut. Both worlds then ran inputs 31..90; after + rebinding the clone to world 7, the snapshots were byte-identical. + - Isolation: + - A same-world target was refused as `identity`. + - A world that already holds an original region was refused (22023). + - A forged origin was refused (22023). + - The runtime role cannot provision a clone (42501). + - A clone world refuses an original region. + - Origins are immutable (EVL09). + - The source session is read-only (25006, even through SECURITY DEFINER). + - A clone lease publishing a successor to the source is fenced. + - The clone runtime login cannot reach the source's functions (42501). + - The source head and decisions were unchanged afterwards. + - Failure: a SIGKILL before the target transaction left the source unchanged, + the target unprovisioned with one orphan object, and a target server that + quarantined (exit 4) without becoming ready. The tool refused a same + database or same user (exit 2) and a same world (exit 4). The retry + succeeded and reused the same object. A source with a damaged head payload + was refused (exit 3), and its target stayed untouched. + - Retention: an owner was SIGKILLed after acknowledging tick 100, and its + successor continued to tick 200. The audit verified every link, and the tool + printed the same result. A lower `max_commits` returned `bounds`. Removing the + first link's trace made the audit report `tail` at sequence 1 while head + recovery still succeeded; restoring the trace made the audit pass again. + - Environmental profile: a clone inherited the tick-20 death. Moving that + durable row to tick 19 (triggers disabled as break-glass) made recovery + report `history` and the audit report `history` at sequence 0. Moving it back + made recovery pass. +- Mutations (each restored and rebuilt afterwards). Each was caught by its + intended check: + - dropping the isolated-world refusal; + - dropping the origin check; + - a read-write source session; + - skipping the inherited-outcome digest; + - skipping the audit's per-link tail verification. + The first attempt at the inherited-outcome mutation did not compile, so its + stale-binary run was discarded and the mutation was rerun. +- Not executed: clones across separate PostgreSQL clusters or hosts, Docker, the + Gitea CI, and cluster runs. diff --git a/docs/infrastructure/POSTGRESQL.md b/docs/infrastructure/POSTGRESQL.md index 778cc7e..b29a76c 100644 --- a/docs/infrastructure/POSTGRESQL.md +++ b/docs/infrastructure/POSTGRESQL.md @@ -124,8 +124,29 @@ else. Upgrading a v3/v4 database that has published checkpoints (not just genesis) is refused with EVL10 and leaves it unchanged: re-provision those regions, or wait for an explicit legacy-history conversion. -There is no operator CLI for provisioning yet, and no real world has been -provisioned. Migrating v2→v3 fails closed if any v2 head exists, because v2 heads +Schema v7 (ADR-0067) adds clones: +- `region_origins`, immutable (EVL09); +- the owner-only `provision_region_clone`; +- clone-genesis parsing, and a head constraint that allows sequence 0 at the + cloned tick. +The runtime function set is unchanged. + +Clone with `evo-world-clone clone`: +- Use a **separate target database** with its own runtime group and login, and + migrate it with that group as `EVO_PG_RUNTIME_ROLE`. +- Retain the source's build identity in the target before cloning. +- The source connection is the source runtime login (read functions only). It is + switched to read-only transactions. +- The target connection is the target owner. +- The tool refuses the same database or the same user on both sides. + +A clone world never receives original regions, and an original world never +receives clones. `evo-world-clone audit` verifies a region's whole retained +history; it is read-only and needs only runtime read access. Never point a clone +target at a production database or World0. + +There is no operator CLI for provisioning original regions yet, and no real world +has been provisioned. Migrating v2→v3 fails closed if any v2 head exists, because v2 heads have no payload. ## Host lease discipline diff --git a/include/evo/persistence/region_checkpoint.hpp b/include/evo/persistence/region_checkpoint.hpp index b014170..0aea4d7 100644 --- a/include/evo/persistence/region_checkpoint.hpp +++ b/include/evo/persistence/region_checkpoint.hpp @@ -52,7 +52,9 @@ struct CheckpointManifest { std::uint64_t frame{}; Uuid commit{}; std::uint64_t sequence{}; - Uuid predecessor_commit{}; // Zero only at genesis. + // Zero at provisioned genesis. At a clone genesis (sequence 0, ADR-0067) they + // name the source commit and the SHA-256 of its manifest bytes. + Uuid predecessor_commit{}; Sha256Digest predecessor_digest{}; // SHA-256 of the predecessor manifest bytes. std::uint64_t begin_tick{}, end_tick{}; Uuid holder{}; // Zero only at genesis (provisioning, not a lease). @@ -103,6 +105,14 @@ genesis_manifest(RegionKey key, std::uint64_t frame, Uuid commit, std::uint64_t const Sha256Digest& build, std::uint64_t payload_bytes, const Sha256Digest& payload_sha256, CheckpointProfile profile = CheckpointProfile::environmental_v1); +// Clone genesis (ADR-0067): sequence 0 of `target` in another world, at the source's +// end tick, whose predecessor fields name the source commit and manifest digest. +// Profile, versions, quantum, frame and build are inherited; `inherited` digests the +// source's permanent outcomes (no trace). The payload must be re-identified bytes. +[[nodiscard]] std::expected +clone_manifest(const CheckpointManifest& source, const Sha256Digest& source_digest, + RegionKey target, Uuid commit, std::uint64_t payload_bytes, + const Sha256Digest& payload_sha256, const BatchDigests& inherited); // Next link from the committed predecessor and its manifest digest under a lease. // Profile, versions and quantum are inherited: a region never changes them. [[nodiscard]] std::expected diff --git a/include/evo/persistence/region_clone.hpp b/include/evo/persistence/region_clone.hpp new file mode 100644 index 0000000..40c12c3 --- /dev/null +++ b/include/evo/persistence/region_clone.hpp @@ -0,0 +1,104 @@ +#pragma once +#include "evo/persistence/checkpoint_store.hpp" +#include +#include +#include +#include +#include + +// M7.9 region clones and history audit (ADR-0067). Linux only. +// +// A clone is a consistent cut: the verified committed head of a source region, +// re-identified into another world and provisioned there as a clone genesis whose +// manifest names the source commit and digest. The source session is made +// read-only before anything is read; nothing is ever written to the source. Order: +// full source recovery (payload, tail, outcomes) -> re-identify and prove the byte +// round trip -> verified target upload -> one owner transaction in the target +// database. A failure before that transaction leaves at most target orphans; the +// target is never partially provisioned. Rules, formats, quantum and build are +// inherited unchanged: a clone never converts versions. +namespace evo::persistence { +enum class CloneError : std::uint8_t { + identity, // Target is in the source world or has a zero ID; nothing read. + session, // The source session could not be made read-only; nothing read. + source, // Source head failed full recovery verification (see recovery). + candidate, // Re-identified payload, outcomes or manifest failed validation. + upload, // Target object write failed; nothing provisioned. + verify, // Target object read-back differs; nothing provisioned. + rejected, // Target database refused (see store); uploads are orphans. + unknown, // Target outcome unknown: load the target head before retrying. + receipt, // Target head disagrees with the clone manifest: fail closed. +}; +struct CloneFailure { + CloneError kind{}; + RecoveryFailure recovery{}; // Meaningful for source. + LeaseStoreFailure store{}; // Meaningful for session/rejected/unknown. + ObjectError object{}; // Meaningful for upload/verify. +}; +struct CloneOrigin { + RegionKey source{}; + std::uint64_t sequence{}, tick{}; + Uuid commit{}; + Sha256Digest digest{}; // SHA-256 of the source manifest bytes. + friend bool operator==(const CloneOrigin&, const CloneOrigin&) = default; +}; +struct ClonedRegion { + CommittedCheckpoint genesis; + CloneOrigin origin; + std::size_t inherited_outcomes{}; +}; +// Test/fault-injection seam, called after the verified target upload and +// immediately before the target transaction. Production callers pass nothing. +struct CloneHooks { + std::function before_transaction; +}; + +// `source` is any connection that may read the region (it is switched to read-only +// transactions for the rest of its life); `target_owner` must be a different +// connection with the target database's owner role. The target store is opened for +// target_key. Both profiles are supported; the profile is read from the source head. +[[nodiscard]] std::expected +clone_region(Postgres& source, const FilesystemObjectStore& source_objects, RegionKey source_key, + Postgres& target_owner, const FilesystemObjectStore& target_objects, + RegionKey target_key, Uuid commit, std::span retained_builds, + std::size_t restore_budget, std::chrono::milliseconds timeout, + const CloneHooks& hooks = {}); + +enum class AuditError : std::uint8_t { + store, // Database unavailable/failed (see store). + not_provisioned, // No such region. + bounds, // More commits than the caller allowed; nothing concluded. + manifest, // A link's bytes disagree with its digest or position. + chain, // Links do not form one contiguous chain from genesis. + unsupported, // Profile/versions/build outside the retained allowlist. + missing_payload, // A referenced payload object is gone. + corrupt_payload, // A payload object has the wrong length or digest. + payload, // Verified bytes fail full state validation. + tail, // A link's trace is missing, corrupt or does not reproduce it. + history, // Durable outcomes disagree with a link or the head state. +}; +struct AuditFailure { + AuditError kind{}; + std::uint64_t sequence{}; // The first link that failed. + LeaseStoreFailure store{}; +}; +struct HistoryAudit { + std::uint64_t commits{}, end_tick{}, outcomes{}; + CheckpointProfile profile{}; + // A clone genesis names its origin commit and manifest digest; the source + // world/region is recorded only in the target database (region_origins). + bool clone{}; + Uuid origin_commit{}; + Sha256Digest origin_digest{}; +}; +// Retention evidence: verifies every committed link from genesis to the head, as +// recovery verifies the head alone. Each manifest matches its digest and chain +// position, each payload is present and restores, each trace replays its +// predecessor's payload to exactly its own, and each link's durable outcomes are +// exactly those derived; the head state agrees with all of them. At most +// `max_commits` links are examined. Read-only; no lease is needed. +[[nodiscard]] std::expected +audit_region_history(Postgres& db, const FilesystemObjectStore& objects, RegionKey key, + std::span retained_builds, std::size_t restore_budget, + std::uint64_t max_commits, std::chrono::milliseconds timeout); +} // namespace evo::persistence diff --git a/include/evo/processes/environmental_community.hpp b/include/evo/processes/environmental_community.hpp index bf66993..0aebbe4 100644 --- a/include/evo/processes/environmental_community.hpp +++ b/include/evo/processes/environmental_community.hpp @@ -68,6 +68,13 @@ class EnvironmentalCommunity { [[nodiscard]] std::uint64_t tick_microseconds() const noexcept { return config_.tick_microseconds; } + [[nodiscard]] std::uint64_t world() const noexcept { + return config_.world; + } + // Clone support (ADR-0067): records the region under another nonzero world ID, + // keeping its frame. Rules, content and trajectory do not depend on the world ID + // (ADR-0044), so rebinding back yields byte-identical snapshots. False for 0. + [[nodiscard]] bool rebind_world(std::uint64_t world) noexcept; private: struct State { diff --git a/include/evo/processes/lineage_region.hpp b/include/evo/processes/lineage_region.hpp index 40c9e39..82b834e 100644 --- a/include/evo/processes/lineage_region.hpp +++ b/include/evo/processes/lineage_region.hpp @@ -66,6 +66,11 @@ class LineageRegion { [[nodiscard]] const LineageBinding& binding() const noexcept { return binding_; } + // Clone support (ADR-0067): only the nested community records a world; the + // archive and binding are identity-free. False for world 0 or a poisoned region. + [[nodiscard]] bool rebind_world(std::uint64_t world) noexcept { + return !poisoned_ && community_->rebind_world(world); + } private: LineageRegion(std::unique_ptr community, diff --git a/include/evo/world/transfers.hpp b/include/evo/world/transfers.hpp index 2affa71..f3d78b2 100644 --- a/include/evo/world/transfers.hpp +++ b/include/evo/world/transfers.hpp @@ -95,6 +95,11 @@ class EnvironmentalTransfers { private: friend class evo::processes::EnvironmentalCommunity; + // Clone support (ADR-0067): the field store and, through it, terrain snapshots + // carry this identity; no other resource state depends on it. + void rebind(WorldIdentity identity) noexcept { + fields_->identity_ = identity; + } [[nodiscard]] bool process_checkpoint(std::uint64_t tick, const std::array& environment, const std::array& producer, diff --git a/migrations/0007_region_clones.sql b/migrations/0007_region_clones.sql new file mode 100644 index 0000000..92496b7 --- /dev/null +++ b/migrations/0007_region_clones.sql @@ -0,0 +1,247 @@ +-- M7.9 region clones (ADR-0067). A clone is sequence 0 of a region in another, +-- isolated world: its genesis manifest names the source commit and manifest digest +-- (so every later link transitively commits to the source cut), starts at the source +-- tick and digests the inherited permanent outcomes. Only the owner provisions it; +-- the runtime role gains nothing. A world is either original or a clone of exactly +-- one source world, and origins are immutable. + +-- Sequence 0 may now sit at the cloned tick; a published link still ends after 0. +DO $$ +DECLARE + c name; +BEGIN + SELECT k.conname INTO c FROM pg_catalog.pg_constraint k + WHERE k.conrelid = 'evolution.region_heads'::regclass AND k.contype = 'c' + AND pg_catalog.pg_get_constraintdef(k.oid) LIKE '%end_tick%'; + IF c IS NULL THEN + RAISE EXCEPTION 'region_heads tick constraint not found'; + END IF; + EXECUTE pg_catalog.format('ALTER TABLE evolution.region_heads DROP CONSTRAINT %I', c); +END $$; +ALTER TABLE evolution.region_heads + ADD CONSTRAINT region_heads_published_tick CHECK (commit_sequence = 0 OR end_tick > 0); + +CREATE TABLE evolution.region_origins ( + world_id evolution.u64 NOT NULL, + region_id evolution.u64 NOT NULL, + source_world evolution.u64 NOT NULL, + source_region evolution.u64 NOT NULL, + source_sequence evolution.u64 NOT NULL, + source_commit uuid NOT NULL, + source_digest bytea NOT NULL CHECK (octet_length(source_digest) = 32), + source_tick evolution.u64 NOT NULL, + cloned_at timestamptz NOT NULL DEFAULT clock_timestamp(), + PRIMARY KEY (world_id, region_id), + FOREIGN KEY (world_id, region_id) REFERENCES evolution.region_leases, + CHECK (source_world <> world_id) +); +REVOKE ALL ON evolution.region_origins FROM PUBLIC; +CREATE TRIGGER region_origins_immutable BEFORE UPDATE OR DELETE ON evolution.region_origins + FOR EACH ROW EXECUTE FUNCTION evolution.refuse_history_rewrite(); +CREATE TRIGGER region_origins_no_truncate BEFORE TRUNCATE ON evolution.region_origins + FOR EACH STATEMENT EXECUTE FUNCTION evolution.refuse_history_rewrite(); + +-- Mirrors decode_manifest + supported() in src/persistence/region_checkpoint.cpp. +-- Changed from v5 only in the sequence-0 rules: a clone genesis keeps its origin. +CREATE OR REPLACE FUNCTION evolution.parse_checkpoint_manifest(m bytea) +RETURNS evolution.checkpoint_manifest LANGUAGE plpgsql IMMUTABLE +SET search_path = pg_catalog, pg_temp AS $$ +DECLARE + r evolution.checkpoint_manifest; + zero16 constant bytea := '\x00000000000000000000000000000000'; + zero32 constant bytea := decode(repeat('00', 32), 'hex'); + history numeric; + cap numeric; +BEGIN + IF m IS NULL OR octet_length(m) < 12 OR substring(m FROM 1 FOR 8) <> '\x4145564f52434d31' THEN + RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023'; + END IF; + IF evolution.manifest_uint(m, 8, 4) <> 2 THEN + RAISE EXCEPTION 'unsupported checkpoint manifest' USING ERRCODE = 'EVL06'; + END IF; + IF octet_length(m) <> 360 THEN + RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023'; + END IF; + r.profile := evolution.manifest_uint(m, 12, 4); + history := evolution.manifest_uint(m, 232, 4); + IF NOT ((r.profile = 1 AND history = 0) OR (r.profile = 2 AND history = 1)) + OR substring(m FROM 161 FOR 36) <> '\x020000000200000001000000010000000100000001000000010000000100000001000000' + OR evolution.manifest_uint(m, 228, 4) <> 0 OR evolution.manifest_uint(m, 236, 4) <> 1 THEN + RAISE EXCEPTION 'unsupported checkpoint manifest' USING ERRCODE = 'EVL06'; + END IF; + -- Payload caps: 1 MiB envelope; 72 + 1 MiB + lineage archive cap (3219496) container. + cap := CASE r.profile WHEN 1 THEN 1048576 ELSE 4268144 END; + r.world_id := evolution.manifest_uint(m, 16, 8); + r.region_id := evolution.manifest_uint(m, 24, 8); + r.frame_id := evolution.manifest_uint(m, 32, 8); + r.commit_id := encode(substring(m FROM 41 FOR 16), 'hex')::uuid; + r.commit_sequence := evolution.manifest_uint(m, 56, 8); + r.predecessor_commit := encode(substring(m FROM 65 FOR 16), 'hex')::uuid; + r.predecessor_digest := substring(m FROM 81 FOR 32); + r.begin_tick := evolution.manifest_uint(m, 112, 8); + r.end_tick := evolution.manifest_uint(m, 120, 8); + r.holder := encode(substring(m FROM 129 FOR 16), 'hex')::uuid; + r.epoch := evolution.manifest_uint(m, 144, 8); + r.tick_microseconds := evolution.manifest_uint(m, 152, 8); + r.versions := substring(m FROM 161 FOR 36); + r.build_identity := substring(m FROM 197 FOR 32); + r.payload_bytes := evolution.manifest_uint(m, 240, 8); + r.payload_sha256 := substring(m FROM 249 FOR 32); + r.trace_bytes := evolution.manifest_uint(m, 280, 8); + r.trace_sha256 := substring(m FROM 289 FOR 32); + r.decision_count := evolution.manifest_uint(m, 320, 4); + r.decisions_sha256 := substring(m FROM 329 FOR 32); + IF r.world_id = 0 OR r.region_id = 0 OR r.frame_id = 0 + OR substring(m FROM 41 FOR 16) = zero16 OR r.tick_microseconds = 0 + OR r.build_identity = zero32 OR r.payload_bytes < 1 OR r.payload_bytes > cap + OR r.payload_sha256 = zero32 OR evolution.manifest_uint(m, 324, 4) <> 0 THEN + RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023'; + END IF; + IF r.commit_sequence = 0 THEN + IF substring(m FROM 129 FOR 16) <> zero16 OR r.epoch <> 0 OR r.begin_tick <> r.end_tick + OR r.trace_bytes <> 0 OR r.trace_sha256 <> zero32 THEN + RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023'; + END IF; + IF substring(m FROM 65 FOR 16) = zero16 AND r.predecessor_digest = zero32 THEN + -- Provisioned genesis: tick 0, no origin, no outcomes. + IF r.end_tick <> 0 OR r.decision_count <> 0 OR r.decisions_sha256 <> zero32 THEN + RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023'; + END IF; + r.predecessor_commit := NULL; + ELSIF substring(m FROM 65 FOR 16) = zero16 OR r.predecessor_digest = zero32 + OR r.predecessor_commit = r.commit_id OR r.decision_count > 8 + OR (r.decision_count = 0) <> (r.decisions_sha256 = zero32) THEN + RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023'; + END IF; + r.holder := NULL; + ELSIF substring(m FROM 65 FOR 16) = zero16 OR r.predecessor_commit = r.commit_id + OR r.predecessor_digest = zero32 OR substring(m FROM 129 FOR 16) = zero16 + OR r.epoch = 0 OR r.end_tick <= r.begin_tick OR r.end_tick - r.begin_tick > 64 + -- One 304-byte trace step per tick after a 96-byte header. + OR r.trace_bytes <> 96 + 304 * (r.end_tick - r.begin_tick) OR r.trace_sha256 = zero32 + OR r.decision_count > 8 OR (r.decision_count = 0) <> (r.decisions_sha256 = zero32) THEN + RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023'; + END IF; + RETURN r; +END $$; + +-- Owner-only genesis of an original region; never in a clone world, never an origin. +CREATE OR REPLACE FUNCTION evolution.provision_region_checkpoint(p_manifest bytea) +RETURNS void LANGUAGE plpgsql SECURITY INVOKER SET search_path = pg_catalog, pg_temp AS $$ +DECLARE + g evolution.checkpoint_manifest := evolution.parse_checkpoint_manifest(p_manifest); + d bytea := sha256(p_manifest); +BEGIN + IF g.commit_sequence <> 0 OR g.predecessor_commit IS NOT NULL THEN + RAISE EXCEPTION 'genesis manifest required' USING ERRCODE = '22023'; + END IF; + IF EXISTS (SELECT 1 FROM evolution.region_origins o WHERE o.world_id = g.world_id) THEN + RAISE EXCEPTION 'world holds clones' USING ERRCODE = '22023'; + END IF; + INSERT INTO evolution.region_leases(world_id, region_id, frame_id) + VALUES (g.world_id, g.region_id, g.frame_id); + INSERT INTO evolution.region_commits(world_id, region_id, commit_sequence, commit_id, + commit_digest, manifest, begin_tick, end_tick, published_epoch, tick_microseconds, + versions, build_identity, payload_bytes, payload_sha256) + VALUES (g.world_id, g.region_id, 0, g.commit_id, d, p_manifest, 0, 0, 0, + g.tick_microseconds, g.versions, g.build_identity, g.payload_bytes, + g.payload_sha256); + INSERT INTO evolution.region_heads(world_id, region_id, commit_sequence, commit_id, digest, + end_tick, published_epoch) + VALUES (g.world_id, g.region_id, 0, g.commit_id, d, 0, 0); +END $$; + +-- Owner-only clone genesis. The source may live in another database; when it is +-- present here, the origin must be exactly its committed link. Inherited outcomes +-- are recorded as decisions of sequence 0 without outbox rows: the source already +-- delivered them, and a clone has no production integrations. +CREATE FUNCTION evolution.provision_region_clone(p_manifest bytea, p_source_world evolution.u64, + p_source_region evolution.u64, p_source_sequence evolution.u64, p_decisions bytea) +RETURNS void LANGUAGE plpgsql SECURITY INVOKER SET search_path = pg_catalog, pg_temp AS $$ +DECLARE + g evolution.checkpoint_manifest := evolution.parse_checkpoint_manifest(p_manifest); + d bytea := sha256(p_manifest); + tick numeric; + actor numeric; + outcome numeric; + organism numeric; + last_tick numeric := -1; + last_actor numeric := -1; + actors integer[] := '{}'; +BEGIN + IF g.commit_sequence <> 0 OR g.predecessor_commit IS NULL THEN + RAISE EXCEPTION 'clone genesis manifest required' USING ERRCODE = '22023'; + END IF; + IF p_source_world IS NULL OR p_source_region IS NULL OR p_source_sequence IS NULL + OR p_decisions IS NULL THEN + RAISE EXCEPTION 'invalid clone request' USING ERRCODE = '22023'; + END IF; + IF p_source_world = g.world_id THEN + RAISE EXCEPTION 'clone requires an isolated world' USING ERRCODE = '22023'; + END IF; + IF EXISTS (SELECT 1 FROM evolution.region_leases l + WHERE l.world_id = g.world_id + AND NOT EXISTS (SELECT 1 FROM evolution.region_origins o + WHERE o.world_id = l.world_id AND o.region_id = l.region_id)) + OR EXISTS (SELECT 1 FROM evolution.region_origins o + WHERE o.world_id = g.world_id AND o.source_world <> p_source_world) THEN + RAISE EXCEPTION 'target world is not an isolated clone of this source' USING ERRCODE = '22023'; + END IF; + IF EXISTS (SELECT 1 FROM evolution.region_leases l + WHERE l.world_id = p_source_world AND l.region_id = p_source_region) + AND NOT EXISTS (SELECT 1 FROM evolution.region_commits c + WHERE c.world_id = p_source_world AND c.region_id = p_source_region + AND c.commit_sequence = p_source_sequence + AND c.commit_id = g.predecessor_commit + AND c.commit_digest = g.predecessor_digest AND c.end_tick = g.end_tick + AND c.versions = g.versions + AND c.tick_microseconds = g.tick_microseconds + AND substring(c.manifest FROM 13 FOR 4) = substring(p_manifest FROM 13 FOR 4)) THEN + RAISE EXCEPTION 'origin is not the committed source link' USING ERRCODE = '22023'; + END IF; + IF octet_length(p_decisions) <> 24 * g.decision_count + OR (g.decision_count > 0 AND sha256(p_decisions) <> g.decisions_sha256) THEN + RAISE EXCEPTION 'inherited outcomes do not match manifest' USING ERRCODE = '22023'; + END IF; + FOR i IN 0 .. g.decision_count - 1 LOOP + tick := evolution.manifest_uint(p_decisions, 24 * i, 8); + actor := evolution.manifest_uint(p_decisions, 24 * i + 8, 4); + outcome := evolution.manifest_uint(p_decisions, 24 * i + 12, 4); + organism := evolution.manifest_uint(p_decisions, 24 * i + 16, 8); + IF outcome <> 1 OR actor NOT IN (0, 1) OR tick < 1 OR tick > g.end_tick + OR tick < last_tick OR (tick = last_tick AND actor <= last_actor) + OR actor::integer = ANY (actors) OR (g.profile = 1) <> (organism = 0) THEN + RAISE EXCEPTION 'invalid inherited outcome' USING ERRCODE = '22023'; + END IF; + actors := actors || actor::integer; + last_tick := tick; + last_actor := actor; + END LOOP; + INSERT INTO evolution.region_leases(world_id, region_id, frame_id) + VALUES (g.world_id, g.region_id, g.frame_id); + INSERT INTO evolution.region_origins(world_id, region_id, source_world, source_region, + source_sequence, source_commit, source_digest, source_tick) + VALUES (g.world_id, g.region_id, p_source_world, p_source_region, p_source_sequence, + g.predecessor_commit, g.predecessor_digest, g.end_tick); + INSERT INTO evolution.region_commits(world_id, region_id, commit_sequence, commit_id, + commit_digest, manifest, begin_tick, end_tick, published_epoch, tick_microseconds, + versions, build_identity, payload_bytes, payload_sha256, decisions) + VALUES (g.world_id, g.region_id, 0, g.commit_id, d, p_manifest, g.end_tick, g.end_tick, 0, + g.tick_microseconds, g.versions, g.build_identity, g.payload_bytes, + g.payload_sha256, p_decisions); + INSERT INTO evolution.region_heads(world_id, region_id, commit_sequence, commit_id, digest, + end_tick, published_epoch) + VALUES (g.world_id, g.region_id, 0, g.commit_id, d, g.end_tick, 0); + FOR i IN 0 .. g.decision_count - 1 LOOP + INSERT INTO evolution.region_decisions(world_id, region_id, outcome, actor, tick, organism, + commit_sequence) + VALUES (g.world_id, g.region_id, 1, evolution.manifest_uint(p_decisions, 24 * i + 8, 4), + evolution.manifest_uint(p_decisions, 24 * i, 8), + evolution.manifest_uint(p_decisions, 24 * i + 16, 8), 0); + END LOOP; +END $$; + +REVOKE ALL ON FUNCTION + evolution.provision_region_clone(bytea, evolution.u64, evolution.u64, evolution.u64, bytea) + FROM PUBLIC; +INSERT INTO evolution.schema_migrations(version, sha256) VALUES (7, :'migration_sha_7'); diff --git a/migrations/apply.sql b/migrations/apply.sql index 49f5f12..27d3228 100644 --- a/migrations/apply.sql +++ b/migrations/apply.sql @@ -27,25 +27,35 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') || ',5:' || :'migration_sha_5' AS at_v5, :'history' = '1:' || :'migration_sha' || ',2:' || :'migration_sha_2' || ',3:' || :'migration_sha_3' || ',4:' || :'migration_sha_4' - || ',5:' || :'migration_sha_5' || ',6:' || :'migration_sha_6' AS at_v6 \gset - \if :at_v6 + || ',5:' || :'migration_sha_5' || ',6:' || :'migration_sha_6' AS at_v6, + :'history' = '1:' || :'migration_sha' || ',2:' || :'migration_sha_2' + || ',3:' || :'migration_sha_3' || ',4:' || :'migration_sha_4' + || ',5:' || :'migration_sha_5' || ',6:' || :'migration_sha_6' + || ',7:' || :'migration_sha_7' AS at_v7 \gset + \if :at_v7 + \elif :at_v6 + \ir 0007_region_clones.sql \elif :at_v5 \ir 0006_commit_lookup.sql + \ir 0007_region_clones.sql \elif :at_v4 \ir pre_v5_guard.sql \ir 0005_batch_decisions.sql \ir 0006_commit_lookup.sql + \ir 0007_region_clones.sql \elif :at_v3 \ir 0004_lineage_profile.sql \ir pre_v5_guard.sql \ir 0005_batch_decisions.sql \ir 0006_commit_lookup.sql + \ir 0007_region_clones.sql \elif :at_v2 \ir 0003_region_checkpoints.sql \ir 0004_lineage_profile.sql \ir pre_v5_guard.sql \ir 0005_batch_decisions.sql \ir 0006_commit_lookup.sql + \ir 0007_region_clones.sql \elif :at_v1 \ir 0002_region_fencing.sql \ir 0003_region_checkpoints.sql @@ -53,6 +63,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir pre_v5_guard.sql \ir 0005_batch_decisions.sql \ir 0006_commit_lookup.sql + \ir 0007_region_clones.sql \else \echo 'Unknown or changed migration history' DO $$ BEGIN RAISE EXCEPTION 'unknown or changed migration history'; END $$; @@ -65,6 +76,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir pre_v5_guard.sql \ir 0005_batch_decisions.sql \ir 0006_commit_lookup.sql + \ir 0007_region_clones.sql \endif -- Effective-privilege audit on every run, including no-ops (ADR-0060). Attributes -- alone are insufficient: predefined roles such as pg_write_all_data, PUBLIC diff --git a/schemas/region-commit.md b/schemas/region-commit.md index 2adc697..1b63978 100644 --- a/schemas/region-commit.md +++ b/schemas/region-commit.md @@ -53,6 +53,22 @@ manifest and the exact decision block together. The database records the decisions and their outbox rows in the same statement as the head. An identical retry of a committed ID returns the original receipt. +**Clone genesis (M7.9, schema v7, ADR-0067).** Sequence 0 may name an origin, in +manifest v2 only: +- Offsets 64 and 80 hold the source commit ID and the SHA-256 of the source + manifest. Both are nonzero, and the commit ID differs from this one. +- begin_tick = end_tick = the source end tick. +- Holder, epoch and trace are zero. +- Offsets 320/328 count and digest the inherited decision block (0..8 records, + ticks 1..end_tick, same encoding as schemas/region-batch.md). +- World differs from the source world. Profile, versions, quantum, frame and build + equal the source's. + +A provisioned genesis still has zero origin fields, tick 0/0 and no decisions. A +half-present origin is malformed. The payload is the source payload re-identified +to the clone's world. The source world and region are recorded in the target +database's `region_origins`, not in the manifest. + Authority: ADR-0058 and docs/architecture/REGION_DURABILITY.md. Logical schema, not an implemented wire codec, SQL migration or permission to serialize structs. M7.2 chooses concrete storage types preserving these domains; M7.4 specifies diff --git a/src/persistence/checkpoint_detail.hpp b/src/persistence/checkpoint_detail.hpp new file mode 100644 index 0000000..4da8088 --- /dev/null +++ b/src/persistence/checkpoint_detail.hpp @@ -0,0 +1,278 @@ +#pragma once +// Private to src/persistence: payload codecs, verified uploads, receipt checks, tail +// verification and full recovery shared by publication (checkpoint_store.cpp) and +// clone/audit (region_clone.cpp). Not an installed header. +#include "evo/persistence/checkpoint_store.hpp" +#include "evo/persistence/sha256.hpp" +#include +#include +#include + +namespace evo::persistence::detail { +using std::chrono::milliseconds; + +struct Organisms { + std::uint64_t producer{}, consumer{}; +}; +// Lifecycle agreement shared by both profiles: exactly one durable death per +// terminal actor, none for an active one, never after the committed tick. +inline bool lifecycle_agrees(const processes::EnvironmentalProgress& progress, Organisms organisms, + std::span decisions, const RegionHead& head) { + for (const auto actor : {Actor::producer, Actor::consumer}) { + const auto terminal = + actor == Actor::producer ? progress.producer_terminal : progress.consumer_terminal; + const auto count = std::ranges::count_if( + decisions, [&](const CommittedDecision& d) { return d.decision.actor == actor; }); + if (count > 1 || (terminal == 1) != (count == 1)) + return false; + } + return std::ranges::all_of(decisions, [&](const CommittedDecision& d) { + const auto organism = + d.decision.actor == Actor::producer ? organisms.producer : organisms.consumer; + return d.decision.tick >= 1 && d.decision.tick <= head.end_tick && + d.commit_sequence <= head.sequence && d.decision.organism == organism; + }); +} + +// Per-profile payload operations; publication and recovery paths are shared. +struct EnvironmentalCodec { + using State = processes::EnvironmentalCommunity; + using Recovered = RecoveredCheckpoint; + static constexpr auto profile = CheckpointProfile::environmental_v1; + static auto snapshot(const State& s) { + return s.snapshot(); + } + static std::uint64_t tick(const State& s) { + return s.progress().tick; + } + static std::uint64_t quantum(const State& s) { + return s.tick_microseconds(); + } + static Organisms organisms(const State&) { + return {}; + } + static auto restore(std::span payload, const CheckpointManifest& m, + std::size_t budget) { + return restore_environmental_payload(payload, m, budget); + } + static bool reconcile(const State& s, std::span d, + const RegionHead& head) { + return lifecycle_agrees(s.progress(), organisms(s), d, head); + } +}; +struct LineageCodec { + using State = processes::LineageRegion; + using Recovered = RecoveredLineageCheckpoint; + static constexpr auto profile = CheckpointProfile::environmental_lineage_v1; + static auto snapshot(const State& s) { + return s.snapshot(); + } + static std::uint64_t tick(const State& s) { + return s.community().progress().tick; + } + static std::uint64_t quantum(const State& s) { + return s.community().tick_microseconds(); + } + static Organisms organisms(const State& s) { + return {s.binding().producer, s.binding().consumer}; + } + static auto restore(std::span payload, const CheckpointManifest& m, + std::size_t budget) { + return restore_lineage_payload(payload, m, budget); + } + // Plus: each durable death is the archive's death, at the same tick. + static bool reconcile(const State& s, std::span d, + const RegionHead& head) { + if (!lifecycle_agrees(s.community().progress(), organisms(s), d, head)) + return false; + return std::ranges::all_of(d, [&](const CommittedDecision& x) { + const auto record = s.archive().organism(x.decision.organism); + return record && !record->alive && record->died.value == x.decision.tick; + }); + } +}; + +inline PublicationFailure fail(PublicationError kind, ObjectError object = {}) { + return {kind, {}, object, {}}; +} +inline std::string hex_parameter(std::span bytes) { + constexpr char digits[] = "0123456789abcdef"; + std::string out = "\\x"; + for (const auto b : bytes) { + out.push_back(digits[b >> 4U]); + out.push_back(digits[b & 0x0FU]); + } + return out; +} +inline std::expected put_verified(const FilesystemObjectStore& objects, + const std::vector& bytes, + const VerifiedObject& expected) { + auto stored = objects.put(bytes); + if (!stored) + return std::unexpected{fail(PublicationError::upload, stored.error())}; + auto back = objects.get(*stored); + if (!back || *stored != expected || !std::ranges::equal(*back, bytes)) + return std::unexpected{ + fail(PublicationError::verify, back ? ObjectError::corrupt : back.error())}; + return {}; +} +// The receipt must agree with the local manifest the database digested. +inline std::expected +confirm(const std::expected& published, + std::vector manifest_bytes, const CheckpointManifest& manifest, + PreparedCommit prepared) { + if (!published) { + const auto kind = published.error().kind == LeaseStoreError::unknown + ? PublicationError::unknown + : PublicationError::rejected; + return std::unexpected{ + PublicationFailure{kind, published.error(), {}, std::move(prepared)}}; + } + const auto digest = sha256(manifest_bytes); + if (!digest || published->digest != *digest || published->sequence != manifest.sequence || + published->commit != manifest.commit || published->end_tick != manifest.end_tick || + published->frame != manifest.frame) + return std::unexpected{fail(PublicationError::receipt)}; + return CommittedCheckpoint{*published, std::move(manifest_bytes)}; +} + +// Replays the head's recorded batch from its committed predecessor (ADR-0065). +template +std::expected +verify_tail(Postgres& db, const FilesystemObjectStore& objects, RegionKey key, + const CheckpointManifest& m, std::span head_payload, + const typename Codec::State& head_state, std::span durable, + std::size_t budget, milliseconds timeout) { + const auto fail_with = [](RecoveryError kind) { + return std::unexpected{RecoveryFailure{kind, {}}}; + }; + if (m.sequence == 0) { + // Genesis has no batch. A clone genesis digests the outcomes it inherited + // (ADR-0067); a provisioned genesis has none. + std::vector inherited; + for (const auto& d : durable) + if (d.commit_sequence == 0) + inherited.push_back(d.decision); + bool agree = false; + if (const auto block = encode_decisions(inherited, 0, m.end_tick); block) { + const auto digest = sha256(*block); + agree = inherited.empty() ? m.batch.decisions_sha256 == Sha256Digest{} + : digest && *digest == m.batch.decisions_sha256; + } + if (!agree || inherited.size() != m.batch.decision_count) + return fail_with(RecoveryError::history); + return {}; + } + if (m.manifest_version != 2) + return fail_with(RecoveryError::unsupported); + auto trace_bytes = objects.get({m.batch.trace_sha256, m.batch.trace_bytes}); + if (!trace_bytes) + return fail_with(trace_bytes.error() == ObjectError::io ? RecoveryError::store + : RecoveryError::tail); + auto trace = decode_trace(*trace_bytes); + const TraceHeader expected{static_cast(Codec::profile), + key, + m.predecessor_commit, + m.predecessor_digest, + m.begin_tick, + m.end_tick}; + if (!trace || trace->header != expected) + return fail_with(RecoveryError::tail); + auto predecessor = load_region_commit(db, key, m.sequence - 1, timeout); + if (!predecessor) + return predecessor.error().kind == LeaseStoreError::not_provisioned + ? fail_with(RecoveryError::tail) + : std::unexpected{RecoveryFailure{RecoveryError::store, predecessor.error()}}; + const auto digest = sha256(predecessor->manifest); + auto previous = decode_manifest(predecessor->manifest); + if (!digest || *digest != predecessor->head.digest || + predecessor->head.digest != m.predecessor_digest || + predecessor->head.commit != m.predecessor_commit || !previous || + previous->end_tick != m.begin_tick) + return fail_with(RecoveryError::tail); + auto base = objects.get({previous->payload_sha256, previous->payload_bytes}); + if (!base) + return fail_with(base.error() == ObjectError::io ? RecoveryError::store + : RecoveryError::tail); + auto replay = Codec::restore(*base, *previous, budget); + if (!replay) + return fail_with(RecoveryError::tail); + for (const auto& step : trace->steps) { + auto report = (*replay)->advance(step.report.tick, step.input); + if (!report || *report != step.report) + return fail_with(RecoveryError::tail); + } + const auto replayed = Codec::snapshot(**replay); + if (!replayed || !std::ranges::equal(*replayed, head_payload)) + return fail_with(RecoveryError::tail); + // The head's durable outcomes are exactly those its trace derives. + const auto organisms = Codec::organisms(head_state); + const auto derived = derive_decisions(trace->steps, organisms.producer, organisms.consumer); + std::vector recorded; + for (const auto& d : durable) + if (d.commit_sequence == m.sequence) + recorded.push_back(d.decision); + if (derived != recorded || derived.size() != m.batch.decision_count) + return fail_with(RecoveryError::history); + return {}; +} + +template +std::expected +recover(Postgres& db, const FilesystemObjectStore& objects, RegionKey key, + std::span retained_builds, std::size_t budget, milliseconds timeout) { + const auto store_failure = [](const LeaseStoreFailure& f) { + return std::unexpected{RecoveryFailure{f.kind == LeaseStoreError::not_provisioned + ? RecoveryError::not_provisioned + : RecoveryError::store, + f}}; + }; + auto committed = load_region_checkpoint(db, key, timeout); + if (!committed) + return store_failure(committed.error()); + const auto fail_with = [](RecoveryError kind) { + return std::unexpected{RecoveryFailure{kind, {}}}; + }; + const auto digest = sha256(committed->manifest); + auto manifest = decode_manifest(committed->manifest); + const auto& head = committed->head; + if (!digest || *digest != head.digest) + return fail_with(RecoveryError::manifest); + if (!manifest) + return fail_with(manifest.error() == CheckpointError::unsupported + ? RecoveryError::unsupported + : RecoveryError::manifest); + if (manifest->key != key || manifest->frame != head.frame || + manifest->sequence != head.sequence || manifest->commit != head.commit || + manifest->end_tick != head.end_tick) + return fail_with(RecoveryError::manifest); + if (!supported(*manifest) || manifest->profile != Codec::profile || + std::ranges::find(retained_builds, manifest->build) == retained_builds.end()) + return fail_with(RecoveryError::unsupported); + auto payload = objects.get({manifest->payload_sha256, manifest->payload_bytes}); + if (!payload) + return fail_with(payload.error() == ObjectError::missing ? RecoveryError::missing_payload + : payload.error() == ObjectError::corrupt ? RecoveryError::corrupt_payload + : RecoveryError::store); + auto state = Codec::restore(*payload, *manifest, budget); + if (!state) + return fail_with(RecoveryError::payload); + // Durable permanent outcomes must agree with the restored state they finalized. + auto decisions = load_region_decisions(db, key, timeout); + if (!decisions) + return store_failure(decisions.error()); + // Outcomes of a link committed after the head was read (possible only when the + // reader holds no lease, e.g. a clone) belong to that newer, separately verified + // link. Every other durable outcome must agree with this head. + std::erase_if(*decisions, + [&](const CommittedDecision& d) { return d.commit_sequence > head.sequence; }); + if (!Codec::reconcile(**state, *decisions, head)) + return fail_with(RecoveryError::history); + if (auto tail = verify_tail(db, objects, key, *manifest, *payload, **state, *decisions, + budget, timeout); + !tail) + return std::unexpected{tail.error()}; + return typename Codec::Recovered{std::move(*committed), *manifest, std::move(*state), + std::move(*decisions)}; +} +} // namespace evo::persistence::detail diff --git a/src/persistence/checkpoint_store.cpp b/src/persistence/checkpoint_store.cpp index 11543c2..e42e1b7 100644 --- a/src/persistence/checkpoint_store.cpp +++ b/src/persistence/checkpoint_store.cpp @@ -1,138 +1,11 @@ #include "evo/persistence/checkpoint_store.hpp" -#include "evo/persistence/sha256.hpp" -#include -#include -#include +#include "checkpoint_detail.hpp" namespace evo::persistence { namespace { +using namespace detail; using std::chrono::milliseconds; -struct Organisms { - std::uint64_t producer{}, consumer{}; -}; -// Lifecycle agreement shared by both profiles: exactly one durable death per -// terminal actor, none for an active one, never after the committed tick. -bool lifecycle_agrees(const processes::EnvironmentalProgress& progress, Organisms organisms, - std::span decisions, const RegionHead& head) { - for (const auto actor : {Actor::producer, Actor::consumer}) { - const auto terminal = - actor == Actor::producer ? progress.producer_terminal : progress.consumer_terminal; - const auto count = std::ranges::count_if( - decisions, [&](const CommittedDecision& d) { return d.decision.actor == actor; }); - if (count > 1 || (terminal == 1) != (count == 1)) - return false; - } - return std::ranges::all_of(decisions, [&](const CommittedDecision& d) { - const auto organism = - d.decision.actor == Actor::producer ? organisms.producer : organisms.consumer; - return d.decision.tick >= 1 && d.decision.tick <= head.end_tick && - d.commit_sequence <= head.sequence && d.decision.organism == organism; - }); -} - -// Per-profile payload operations; publication and recovery paths are shared. -struct EnvironmentalCodec { - using State = processes::EnvironmentalCommunity; - using Recovered = RecoveredCheckpoint; - static constexpr auto profile = CheckpointProfile::environmental_v1; - static auto snapshot(const State& s) { - return s.snapshot(); - } - static std::uint64_t tick(const State& s) { - return s.progress().tick; - } - static std::uint64_t quantum(const State& s) { - return s.tick_microseconds(); - } - static Organisms organisms(const State&) { - return {}; - } - static auto restore(std::span payload, const CheckpointManifest& m, - std::size_t budget) { - return restore_environmental_payload(payload, m, budget); - } - static bool reconcile(const State& s, std::span d, - const RegionHead& head) { - return lifecycle_agrees(s.progress(), organisms(s), d, head); - } -}; -struct LineageCodec { - using State = processes::LineageRegion; - using Recovered = RecoveredLineageCheckpoint; - static constexpr auto profile = CheckpointProfile::environmental_lineage_v1; - static auto snapshot(const State& s) { - return s.snapshot(); - } - static std::uint64_t tick(const State& s) { - return s.community().progress().tick; - } - static std::uint64_t quantum(const State& s) { - return s.community().tick_microseconds(); - } - static Organisms organisms(const State& s) { - return {s.binding().producer, s.binding().consumer}; - } - static auto restore(std::span payload, const CheckpointManifest& m, - std::size_t budget) { - return restore_lineage_payload(payload, m, budget); - } - // Plus: each durable death is the archive's death, at the same tick. - static bool reconcile(const State& s, std::span d, - const RegionHead& head) { - if (!lifecycle_agrees(s.community().progress(), organisms(s), d, head)) - return false; - return std::ranges::all_of(d, [&](const CommittedDecision& x) { - const auto record = s.archive().organism(x.decision.organism); - return record && !record->alive && record->died.value == x.decision.tick; - }); - } -}; - -PublicationFailure fail(PublicationError kind, ObjectError object = {}) { - return {kind, {}, object, {}}; -} -std::string hex_parameter(std::span bytes) { - constexpr char digits[] = "0123456789abcdef"; - std::string out = "\\x"; - for (const auto b : bytes) { - out.push_back(digits[b >> 4U]); - out.push_back(digits[b & 0x0FU]); - } - return out; -} -std::expected put_verified(const FilesystemObjectStore& objects, - const std::vector& bytes, - const VerifiedObject& expected) { - auto stored = objects.put(bytes); - if (!stored) - return std::unexpected{fail(PublicationError::upload, stored.error())}; - auto back = objects.get(*stored); - if (!back || *stored != expected || !std::ranges::equal(*back, bytes)) - return std::unexpected{ - fail(PublicationError::verify, back ? ObjectError::corrupt : back.error())}; - return {}; -} -// The receipt must agree with the local manifest the database digested. -std::expected -confirm(const std::expected& published, - std::vector manifest_bytes, const CheckpointManifest& manifest, - PreparedCommit prepared) { - if (!published) { - const auto kind = published.error().kind == LeaseStoreError::unknown - ? PublicationError::unknown - : PublicationError::rejected; - return std::unexpected{ - PublicationFailure{kind, published.error(), {}, std::move(prepared)}}; - } - const auto digest = sha256(manifest_bytes); - if (!digest || published->digest != *digest || published->sequence != manifest.sequence || - published->commit != manifest.commit || published->end_tick != manifest.end_tick || - published->frame != manifest.frame) - return std::unexpected{fail(PublicationError::receipt)}; - return CommittedCheckpoint{*published, std::move(manifest_bytes)}; -} - template std::expected provision(Postgres& owner, const FilesystemObjectStore& objects, RegionKey key, std::uint64_t frame, @@ -241,125 +114,6 @@ publish_batch(Postgres& db, const FilesystemObjectStore& objects, const LeaseGra return PublishedBatch{std::move(*committed), decisions}; } -// Replays the head's recorded batch from its committed predecessor (ADR-0065). -template -std::expected -verify_tail(Postgres& db, const FilesystemObjectStore& objects, RegionKey key, - const CheckpointManifest& m, std::span head_payload, - const typename Codec::State& head_state, std::span durable, - std::size_t budget, milliseconds timeout) { - const auto fail_with = [](RecoveryError kind) { - return std::unexpected{RecoveryFailure{kind, {}}}; - }; - if (m.sequence == 0) - return {}; // Genesis has no batch. - if (m.manifest_version != 2) - return fail_with(RecoveryError::unsupported); - auto trace_bytes = objects.get({m.batch.trace_sha256, m.batch.trace_bytes}); - if (!trace_bytes) - return fail_with(trace_bytes.error() == ObjectError::io ? RecoveryError::store - : RecoveryError::tail); - auto trace = decode_trace(*trace_bytes); - const TraceHeader expected{static_cast(Codec::profile), - key, - m.predecessor_commit, - m.predecessor_digest, - m.begin_tick, - m.end_tick}; - if (!trace || trace->header != expected) - return fail_with(RecoveryError::tail); - auto predecessor = load_region_commit(db, key, m.sequence - 1, timeout); - if (!predecessor) - return predecessor.error().kind == LeaseStoreError::not_provisioned - ? fail_with(RecoveryError::tail) - : std::unexpected{RecoveryFailure{RecoveryError::store, predecessor.error()}}; - const auto digest = sha256(predecessor->manifest); - auto previous = decode_manifest(predecessor->manifest); - if (!digest || *digest != predecessor->head.digest || - predecessor->head.digest != m.predecessor_digest || - predecessor->head.commit != m.predecessor_commit || !previous || - previous->end_tick != m.begin_tick) - return fail_with(RecoveryError::tail); - auto base = objects.get({previous->payload_sha256, previous->payload_bytes}); - if (!base) - return fail_with(base.error() == ObjectError::io ? RecoveryError::store - : RecoveryError::tail); - auto replay = Codec::restore(*base, *previous, budget); - if (!replay) - return fail_with(RecoveryError::tail); - for (const auto& step : trace->steps) { - auto report = (*replay)->advance(step.report.tick, step.input); - if (!report || *report != step.report) - return fail_with(RecoveryError::tail); - } - const auto replayed = Codec::snapshot(**replay); - if (!replayed || !std::ranges::equal(*replayed, head_payload)) - return fail_with(RecoveryError::tail); - // The head's durable outcomes are exactly those its trace derives. - const auto organisms = Codec::organisms(head_state); - const auto derived = derive_decisions(trace->steps, organisms.producer, organisms.consumer); - std::vector recorded; - for (const auto& d : durable) - if (d.commit_sequence == m.sequence) - recorded.push_back(d.decision); - if (derived != recorded || derived.size() != m.batch.decision_count) - return fail_with(RecoveryError::history); - return {}; -} - -template -std::expected -recover(Postgres& db, const FilesystemObjectStore& objects, RegionKey key, - std::span retained_builds, std::size_t budget, milliseconds timeout) { - const auto store_failure = [](const LeaseStoreFailure& f) { - return std::unexpected{RecoveryFailure{f.kind == LeaseStoreError::not_provisioned - ? RecoveryError::not_provisioned - : RecoveryError::store, - f}}; - }; - auto committed = load_region_checkpoint(db, key, timeout); - if (!committed) - return store_failure(committed.error()); - const auto fail_with = [](RecoveryError kind) { - return std::unexpected{RecoveryFailure{kind, {}}}; - }; - const auto digest = sha256(committed->manifest); - auto manifest = decode_manifest(committed->manifest); - const auto& head = committed->head; - if (!digest || *digest != head.digest) - return fail_with(RecoveryError::manifest); - if (!manifest) - return fail_with(manifest.error() == CheckpointError::unsupported - ? RecoveryError::unsupported - : RecoveryError::manifest); - if (manifest->key != key || manifest->frame != head.frame || - manifest->sequence != head.sequence || manifest->commit != head.commit || - manifest->end_tick != head.end_tick) - return fail_with(RecoveryError::manifest); - if (!supported(*manifest) || manifest->profile != Codec::profile || - std::ranges::find(retained_builds, manifest->build) == retained_builds.end()) - return fail_with(RecoveryError::unsupported); - auto payload = objects.get({manifest->payload_sha256, manifest->payload_bytes}); - if (!payload) - return fail_with(payload.error() == ObjectError::missing ? RecoveryError::missing_payload - : payload.error() == ObjectError::corrupt ? RecoveryError::corrupt_payload - : RecoveryError::store); - auto state = Codec::restore(*payload, *manifest, budget); - if (!state) - return fail_with(RecoveryError::payload); - // Durable permanent outcomes must agree with the restored state they finalized. - auto decisions = load_region_decisions(db, key, timeout); - if (!decisions) - return store_failure(decisions.error()); - if (!Codec::reconcile(**state, *decisions, head)) - return fail_with(RecoveryError::history); - if (auto tail = verify_tail(db, objects, key, *manifest, *payload, **state, *decisions, - budget, timeout); - !tail) - return std::unexpected{tail.error()}; - return typename Codec::Recovered{std::move(*committed), *manifest, std::move(*state), - std::move(*decisions)}; -} } // namespace std::expected provision_environmental_region( diff --git a/src/persistence/region_checkpoint.cpp b/src/persistence/region_checkpoint.cpp index 40e3251..ebda175 100644 --- a/src/persistence/region_checkpoint.cpp +++ b/src/persistence/region_checkpoint.cpp @@ -61,9 +61,18 @@ bool structural(const CheckpointManifest& m) noexcept { if (m.manifest_version != 1 && m.manifest_version != 2) return false; const bool no_batch = b == BatchDigests{}; - if (m.sequence == 0) - return zero(m.predecessor_commit) && zero(m.predecessor_digest) && m.begin_tick == 0 && - m.end_tick == 0 && zero(m.holder) && m.epoch == 0 && no_batch; + if (m.sequence == 0) { + const bool genesis = zero(m.holder) && m.epoch == 0 && m.begin_tick == m.end_tick && + b.trace_bytes == 0 && zero(b.trace_sha256); + if (zero(m.predecessor_commit) && zero(m.predecessor_digest)) + return genesis && m.end_tick == 0 && no_batch; + // Clone genesis (ADR-0067, v2 only): the origin is the source commit and + // manifest digest; the block digests inherited permanent outcomes. + return genesis && m.manifest_version == 2 && !zero(m.predecessor_commit) && + !zero(m.predecessor_digest) && m.predecessor_commit != m.commit && + b.decision_count <= max_batch_decisions && + (b.decision_count == 0) == zero(b.decisions_sha256); + } const bool link = !zero(m.predecessor_commit) && m.predecessor_commit != m.commit && !zero(m.predecessor_digest) && !zero(m.holder) && m.epoch != 0 && m.end_tick > m.begin_tick && @@ -200,6 +209,33 @@ genesis_manifest(RegionKey key, std::uint64_t frame, Uuid commit, std::uint64_t return m; } +std::expected +clone_manifest(const CheckpointManifest& source, const Sha256Digest& source_digest, + RegionKey target, Uuid commit, std::uint64_t payload_bytes, + const Sha256Digest& payload_sha256, const BatchDigests& inherited) { + if (!supported(source)) + return std::unexpected{CheckpointError::unsupported}; + if (!structural(source) || target.world == source.key.world || commit == source.commit || + inherited.trace_bytes != 0 || !zero(inherited.trace_sha256)) + return std::unexpected{CheckpointError::chain}; + CheckpointManifest m = source; + m.manifest_version = 2; + m.key = target; + m.commit = commit; + m.sequence = 0; + m.predecessor_commit = source.commit; + m.predecessor_digest = source_digest; + m.begin_tick = source.end_tick; + m.holder = {}; + m.epoch = 0; + m.payload_bytes = payload_bytes; + m.payload_sha256 = payload_sha256; + m.batch = inherited; + if (!structural(m)) + return std::unexpected{CheckpointError::format}; + return m; +} + std::expected successor_manifest(const CheckpointManifest& p, const Sha256Digest& predecessor_digest, Uuid commit, const LeaseGrant& lease, std::uint64_t end_tick, const Sha256Digest& build, diff --git a/src/persistence/region_clone.cpp b/src/persistence/region_clone.cpp new file mode 100644 index 0000000..7ce0500 --- /dev/null +++ b/src/persistence/region_clone.cpp @@ -0,0 +1,269 @@ +#include "evo/persistence/region_clone.hpp" +#include "checkpoint_detail.hpp" +#include + +namespace evo::persistence { +namespace { +using namespace detail; +using std::chrono::milliseconds; + +std::unexpected clone_fail(CloneError kind, LeaseStoreFailure store = {}) { + return std::unexpected{CloneFailure{kind, {}, store, {}}}; +} +std::unexpected source_fail(RecoveryFailure recovery) { + return std::unexpected{CloneFailure{CloneError::source, recovery, {}, {}}}; +} +LeaseStoreFailure store_failure(const PgFailure& cause) { + return {cause.kind == PgError::unknown ? LeaseStoreError::unknown : LeaseStoreError::store, + cause}; +} + +template +std::expected +clone(Postgres& source, const FilesystemObjectStore& source_objects, RegionKey source_key, + Postgres& target, const FilesystemObjectStore& target_objects, RegionKey target_key, + Uuid commit, std::span builds, std::size_t budget, milliseconds timeout, + const CloneHooks& hooks) { + // The cut is the committed head, verified exactly as recovery verifies it. + auto recovered = recover(source, source_objects, source_key, builds, budget, timeout); + if (!recovered) + return source_fail(recovered.error()); + const auto& origin = recovered->manifest; + const auto origin_digest = recovered->committed.head.digest; + auto& state = *recovered->state; + const auto original = Codec::snapshot(state); + if (!original || !state.rebind_world(target_key.world)) + return clone_fail(CloneError::candidate); + const auto payload = Codec::snapshot(state); + std::vector inherited; + inherited.reserve(recovered->decisions.size()); + for (const auto& d : recovered->decisions) + inherited.push_back(d.decision); + const auto block = encode_decisions(inherited, 0, origin.end_tick); + if (!payload || !block) + return clone_fail(CloneError::candidate); + const auto payload_digest = sha256(*payload); + const auto block_digest = sha256(*block); + if (!payload_digest || !block_digest) + return clone_fail(CloneError::candidate); + const BatchDigests digests{0, + {}, + static_cast(inherited.size()), + inherited.empty() ? Sha256Digest{} : *block_digest}; + const auto manifest = clone_manifest(origin, origin_digest, target_key, commit, payload->size(), + *payload_digest, digests); + auto bytes = manifest ? encode_manifest(*manifest) + : std::expected, CheckpointError>{ + std::unexpected{CheckpointError::format}}; + if (!bytes) + return clone_fail(CloneError::candidate); + // The target bytes are a complete region of the target world, and rebinding them + // back reproduces the verified source bytes exactly: only the identity moved. + auto check = Codec::restore(*payload, *manifest, budget); + if (!check || !(*check)->rebind_world(source_key.world)) + return clone_fail(CloneError::candidate); + const auto back = Codec::snapshot(**check); + if (!back || *back != *original) + return clone_fail(CloneError::candidate); + + if (auto stored = put_verified(target_objects, *payload, {*payload_digest, payload->size()}); + !stored) + return std::unexpected{CloneFailure{stored.error().kind == PublicationError::upload + ? CloneError::upload + : CloneError::verify, + {}, + {}, + stored.error().object}}; + if (hooks.before_transaction) + hooks.before_transaction(); + const std::array p{hex_parameter(*bytes), std::to_string(source_key.world), + std::to_string(source_key.region), + std::to_string(origin.sequence), hex_parameter(*block)}; + auto done = target.query("SELECT evolution.provision_region_clone($1::bytea, " + "$2::evolution.u64, $3::evolution.u64, $4::evolution.u64, $5::bytea)", + p, timeout); + if (!done) { + const auto failure = store_failure(done.error()); + return clone_fail(failure.kind == LeaseStoreError::unknown ? CloneError::unknown + : CloneError::rejected, + failure); + } + auto committed = load_region_checkpoint(target, target_key, timeout); + if (!committed) + return clone_fail(CloneError::unknown, committed.error()); + auto confirmed = confirm(committed->head, std::move(*bytes), *manifest, {}); + if (!confirmed || committed->manifest != confirmed->manifest) + return clone_fail(CloneError::receipt); + return ClonedRegion{ + std::move(*confirmed), + CloneOrigin{source_key, origin.sequence, origin.end_tick, origin.commit, origin_digest}, + inherited.size()}; +} + +std::unexpected audit_fail(AuditError kind, std::uint64_t sequence, + LeaseStoreFailure store = {}) { + return std::unexpected{AuditFailure{kind, sequence, store}}; +} +AuditError from_recovery(RecoveryError kind) { + switch (kind) { + case RecoveryError::store: + return AuditError::store; + case RecoveryError::unsupported: + return AuditError::unsupported; + case RecoveryError::history: + return AuditError::history; + default: + return AuditError::tail; + } +} + +template +std::expected +audit(Postgres& db, const FilesystemObjectStore& objects, RegionKey key, + std::span builds, std::size_t budget, std::uint64_t max_commits, + milliseconds timeout, const CommittedCheckpoint& head) { + const auto last = head.head.sequence; + if (last >= max_commits) + return audit_fail(AuditError::bounds, 0); + auto loaded = load_region_decisions(db, key, timeout); + if (!loaded) + return audit_fail(AuditError::store, 0, loaded.error()); + // A live region may commit after `head` was read; judge only what it covers. + std::vector durable; + std::ranges::copy_if(*loaded, std::back_inserter(durable), + [&](const CommittedDecision& d) { return d.commit_sequence <= last; }); + + HistoryAudit out; + out.profile = Codec::profile; + CheckpointManifest previous; + Sha256Digest previous_digest{}; + std::unique_ptr state; + for (std::uint64_t sequence = 0; sequence <= last; ++sequence) { + auto link = sequence == last ? std::expected{head} + : load_region_commit(db, key, sequence, timeout); + if (!link) + return link.error().kind == LeaseStoreError::not_provisioned + ? audit_fail(AuditError::chain, sequence) + : audit_fail(AuditError::store, sequence, link.error()); + const auto digest = sha256(link->manifest); + auto m = decode_manifest(link->manifest); + if (!digest || *digest != link->head.digest) + return audit_fail(AuditError::manifest, sequence); + if (!m) + return audit_fail(m.error() == CheckpointError::unsupported ? AuditError::unsupported + : AuditError::manifest, + sequence); + if (m->key != key || m->sequence != sequence || m->commit != link->head.commit || + m->end_tick != link->head.end_tick || m->frame != link->head.frame) + return audit_fail(AuditError::manifest, sequence); + if (!supported(*m) || m->profile != Codec::profile || + std::ranges::find(builds, m->build) == builds.end()) + return audit_fail(AuditError::unsupported, sequence); + if (sequence == 0) { + out.clone = m->predecessor_commit != Uuid{}; + out.origin_commit = m->predecessor_commit; + out.origin_digest = m->predecessor_digest; + } else if (m->predecessor_commit != previous.commit || + m->predecessor_digest != previous_digest || m->begin_tick != previous.end_tick) { + return audit_fail(AuditError::chain, sequence); + } + auto payload = objects.get({m->payload_sha256, m->payload_bytes}); + if (!payload) + return audit_fail(payload.error() == ObjectError::missing ? AuditError::missing_payload + : payload.error() == ObjectError::corrupt + ? AuditError::corrupt_payload + : AuditError::store, + sequence); + auto restored = Codec::restore(*payload, *m, budget); + if (!restored) + return audit_fail(AuditError::payload, sequence); + if (auto tail = verify_tail(db, objects, key, *m, *payload, **restored, durable, + budget, timeout); + !tail) + return audit_fail(from_recovery(tail.error().kind), sequence, tail.error().store); + previous = *m; + previous_digest = *digest; + state = std::move(*restored); + } + if (!state || !Codec::reconcile(*state, durable, head.head)) + return audit_fail(AuditError::history, last); + out.commits = last + 1; + out.end_tick = head.head.end_tick; + out.outcomes = durable.size(); + return out; +} +} // namespace + +std::expected +clone_region(Postgres& source, const FilesystemObjectStore& source_objects, RegionKey source_key, + Postgres& target_owner, const FilesystemObjectStore& target_objects, + RegionKey target_key, Uuid commit, std::span retained_builds, + std::size_t restore_budget, milliseconds timeout, const CloneHooks& hooks) { + if (source_key.world == 0 || source_key.region == 0 || target_key.world == 0 || + target_key.region == 0 || target_key.world == source_key.world) + return clone_fail(CloneError::identity); + // Every later transaction on the source session is read-only, including the + // SECURITY DEFINER functions it calls: a clone cannot write to its source. + if (auto set = + source.query("SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY", {}, timeout); + !set) + return clone_fail(CloneError::session, store_failure(set.error())); + auto read_only = source.query( + "SELECT pg_catalog.current_setting('default_transaction_read_only')", {}, timeout); + if (!read_only) + return clone_fail(CloneError::session, store_failure(read_only.error())); + if (read_only->size() != 1 || (*read_only)[0].size() != 1 || (*read_only)[0][0] != "on") + return clone_fail(CloneError::session); + auto head = load_region_checkpoint(source, source_key, timeout); + if (!head) + return source_fail({head.error().kind == LeaseStoreError::not_provisioned + ? RecoveryError::not_provisioned + : RecoveryError::store, + head.error()}); + const auto manifest = decode_manifest(head->manifest); + if (!manifest) + return source_fail({manifest.error() == CheckpointError::unsupported + ? RecoveryError::unsupported + : RecoveryError::manifest, + {}}); + switch (manifest->profile) { + case CheckpointProfile::environmental_v1: + return clone(source, source_objects, source_key, target_owner, + target_objects, target_key, commit, retained_builds, + restore_budget, timeout, hooks); + case CheckpointProfile::environmental_lineage_v1: + return clone(source, source_objects, source_key, target_owner, target_objects, + target_key, commit, retained_builds, restore_budget, timeout, + hooks); + default: + return source_fail({RecoveryError::unsupported, {}}); + } +} + +std::expected +audit_region_history(Postgres& db, const FilesystemObjectStore& objects, RegionKey key, + std::span retained_builds, std::size_t restore_budget, + std::uint64_t max_commits, milliseconds timeout) { + auto head = load_region_checkpoint(db, key, timeout); + if (!head) + return audit_fail(head.error().kind == LeaseStoreError::not_provisioned + ? AuditError::not_provisioned + : AuditError::store, + 0, head.error()); + const auto manifest = decode_manifest(head->manifest); + if (!manifest) + return audit_fail(manifest.error() == CheckpointError::unsupported ? AuditError::unsupported + : AuditError::manifest, + head->head.sequence); + switch (manifest->profile) { + case CheckpointProfile::environmental_v1: + return audit(db, objects, key, retained_builds, restore_budget, + max_commits, timeout, *head); + case CheckpointProfile::environmental_lineage_v1: + return audit(db, objects, key, retained_builds, restore_budget, max_commits, + timeout, *head); + default: + return audit_fail(AuditError::unsupported, head->head.sequence); + } +} +} // namespace evo::persistence diff --git a/src/processes/environmental_community.cpp b/src/processes/environmental_community.cpp index 0e558df..d70ebfb 100644 --- a/src/processes/environmental_community.cpp +++ b/src/processes/environmental_community.cpp @@ -13,6 +13,13 @@ std::uint64_t portion(std::uint64_t x, std::uint64_t a) { return (x / 1000) * a + (x % 1000) * a / 1000; } } // namespace +bool EnvironmentalCommunity::rebind_world(std::uint64_t world) noexcept { + if (world == 0) + return false; + config_.world = world; + resources_->rebind({world, config_.frame}); + return true; +} bool EnvironmentalCommunity::configuration(const EnvironmentalConfig& c, const world::EnvironmentalTransfers& r) noexcept { if ((c.rules != 1 && c.rules != 2) || diff --git a/tests/lineage_region_tests.cpp b/tests/lineage_region_tests.cpp index 1db7310..5a0fc5a 100644 --- a/tests/lineage_region_tests.cpp +++ b/tests/lineage_region_tests.cpp @@ -130,6 +130,40 @@ void snapshots_restore_and_continue() { CHECK(older && (*older)->archive().organism(11)->alive); CHECK(!LineageRegion::restore(*bytes, {7, 10}, 8000000)); // Foreign frame. } + +// ADR-0067: a clone records another world ID; nothing else about the region, its +// history or its trajectory changes, and rebinding back is byte-exact. +void rebinding_changes_only_the_world() { + auto source = lineage_region(); + advance_lineage(*source, 25, true); // Consumer dead at tick 20. + const auto original = source->snapshot(); + CHECK(original); + auto clone = LineageRegion::restore(*original, region_identity, 8000000); + CHECK(clone); + CHECK(!(*clone)->rebind_world(0)); + CHECK((*clone)->rebind_world(8) && (*clone)->community().world() == 8); + const auto rebound = (*clone)->snapshot(); + CHECK(rebound && *rebound != *original && rebound->size() == original->size()); + // Only the new identity restores it; the source identity no longer does. + CHECK(!LineageRegion::restore(*rebound, region_identity, 8000000)); + auto restored = LineageRegion::restore(*rebound, {8, region_identity.frame}, 8000000); + CHECK(restored && (*restored)->archive().organism(11)->died.value == 20); + CHECK((*restored)->binding() == source->binding()); + // Identical continuation: every report matches, and so does the state once the + // clone is rebound to the source world. + for (std::uint64_t tick = 26; tick <= 90; ++tick) { + auto a = source->advance(tick, evo::test::region_input(tick)); + auto b = (*restored)->advance(tick, evo::test::region_input(tick)); + CHECK(a && b && *a == *b); + } + CHECK((*restored)->rebind_world(region_identity.world)); + CHECK((*restored)->snapshot() == source->snapshot()); + // The plain community rebinds the same way. + auto community = region_community(); + const auto before = community->snapshot(); + CHECK(community->rebind_world(9) && community->rebind_world(region_identity.world)); + CHECK(community->snapshot() == before); +} } // namespace int main() { @@ -137,6 +171,7 @@ int main() { closure_is_required(); deaths_are_recorded_in_the_same_tick(); snapshots_restore_and_continue(); + rebinding_changes_only_the_world(); std::puts("lineage region contracts passed"); return 0; } catch (const std::exception& e) { diff --git a/tests/postgres_tests.cpp b/tests/postgres_tests.cpp index 25e0fc3..a43d322 100644 --- a/tests/postgres_tests.cpp +++ b/tests/postgres_tests.cpp @@ -9,6 +9,7 @@ void run_region_host_tests(); void run_region_server_process_tests(); void run_region_recovery_tests(); void run_region_ops_tests(); +void run_region_clone_tests(); namespace { using namespace evo::persistence; using namespace std::chrono_literals; @@ -87,6 +88,8 @@ int main() { std::puts("region recovery integration passed"); run_region_ops_tests(); std::puts("region ops integration passed"); + run_region_clone_tests(); + std::puts("region clone and history integration passed"); return 0; } catch (const std::exception& e) { std::fprintf(stderr, "%s\n", e.what()); diff --git a/tests/region_checkpoint_tests.cpp b/tests/region_checkpoint_tests.cpp index d4a4713..0741261 100644 --- a/tests/region_checkpoint_tests.cpp +++ b/tests/region_checkpoint_tests.cpp @@ -281,6 +281,61 @@ void lineage_profile() { nested[64] = 1; // Nested envelope format 1 is not this profile's envelope. CHECK(restore_lineage_payload(nested, *m, 8000000).error() == CheckpointError::unsupported); } +// ADR-0067 clone genesis: sequence 0 at the source tick, naming its origin. +void clone_genesis() { + const auto source = next(16); + const auto digest = filled<32>(0xD1); + const RegionKey target{8, 3}; + const BatchDigests inherited{0, {}, 1, filled<32>(0x7E)}; + auto c = + clone_manifest(source, digest, target, filled<16>(0x05), 300, filled<32>(0xA5), inherited); + CHECK(c && c->sequence == 0 && c->key == target && c->frame == source.frame); + CHECK(c->begin_tick == 16 && c->end_tick == 16 && c->predecessor_commit == source.commit); + CHECK(c->predecessor_digest == digest && c->epoch == 0 && c->holder == Uuid{}); + CHECK(c->versions == source.versions && c->profile == source.profile && + c->tick_microseconds == source.tick_microseconds && c->build == source.build); + auto bytes = encode_manifest(*c); + CHECK(bytes && decode_manifest(*bytes) == *c && supported(*c)); + // A successor chains from the clone exactly as from any genesis. + auto after = successor_manifest(*c, filled<32>(0xD2), filled<16>(0x06), lease(), 20, build, 1, + filled<32>(0xA6), batch_for(4)); + CHECK(after && after->sequence == 1 && after->begin_tick == 16 && after->key == target); + // Isolation and shape: another world, a fresh commit, no trace, bounded outcomes. + CHECK( + clone_manifest(source, digest, {7, 4}, filled<16>(5), 300, filled<32>(0xA5), {}).error() == + CheckpointError::chain); + CHECK( + clone_manifest(source, digest, target, source.commit, 300, filled<32>(0xA5), {}).error() == + CheckpointError::chain); + CHECK(clone_manifest(source, digest, target, filled<16>(5), 300, filled<32>(0xA5), + {trace_bytes_for(1), filled<32>(1), 0, {}}) + .error() == CheckpointError::chain); + CHECK( + clone_manifest(source, digest, target, filled<16>(5), 300, filled<32>(0xA5), {0, {}, 1, {}}) + .error() == CheckpointError::format); + CHECK(clone_manifest(source, digest, target, filled<16>(5), 300, filled<32>(0xA5), + {0, {}, 9, filled<32>(1)}) + .error() == CheckpointError::format); + CHECK(clone_manifest(source, {}, target, filled<16>(5), 300, filled<32>(0xA5), {}).error() == + CheckpointError::format); + auto old = source; + old.versions.rules = 1; + CHECK(clone_manifest(old, digest, target, filled<16>(5), 300, filled<32>(0xA5), {}).error() == + CheckpointError::unsupported); + // A provisioned genesis still starts at tick 0 with no origin or outcomes. + auto g = genesis(); + g.batch = inherited; + CHECK(encode_manifest(g).error() == CheckpointError::format); + // Manifest v1 never carried an origin. + auto v1 = *c; + v1.manifest_version = 1; + v1.batch = {}; + CHECK(encode_manifest(v1).error() == CheckpointError::format); + // Half an origin is malformed. + auto half = *c; + half.predecessor_commit = {}; + CHECK(encode_manifest(half).error() == CheckpointError::format); +} } // namespace int main() { @@ -290,6 +345,7 @@ int main() { successor_chain(); payload_validation_and_restored_trajectory(); lineage_profile(); + clone_genesis(); std::puts("region checkpoint contracts passed"); return 0; } catch (const std::exception& e) { diff --git a/tests/region_clone_tests.cpp b/tests/region_clone_tests.cpp new file mode 100644 index 0000000..86a1a0a --- /dev/null +++ b/tests/region_clone_tests.cpp @@ -0,0 +1,481 @@ +// M7.9a region clones and retained history (ADR-0067) against two real PostgreSQL +// databases: the source (PGDATABASE, runtime EVO_PG_RUNTIME_USER) and an isolated +// clone database (EVO_PG_CLONE_DATABASE, its own runtime EVO_PG_CLONE_RUNTIME_USER). +// Real evo-server and evo-world-clone processes. Run by tools/ci/postgres-check.sh. +#include "evo/persistence/region_clone.hpp" +#include "evo/persistence/sha256.hpp" +#include "fixtures/server_process.hpp" +#include +#include +#include +#include + +void run_region_clone_tests(); + +namespace { +using namespace evo::test::process; +using namespace evo::persistence; +void check(bool value, std::source_location at = std::source_location::current()) { + if (!value) + throw std::runtime_error("region clone test line " + std::to_string(at.line())); +} +#define CHECK(...) check(static_cast(__VA_ARGS__)) +constexpr std::size_t budget = 8000000; +const char* const build_hex = "b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1"; +Sha256Digest build() { + Sha256Digest b{}; + b.fill(0xB1); + return b; +} +template std::array filled(std::uint8_t value) { + std::array out{}; + out.fill(value); + return out; +} +std::string hex(std::span 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; +} +std::unique_ptr connect_to(const std::string& database, const std::string& user) { + auto made = Postgres::connect({env("PGHOST"), {}, env("PGPORT"), database, user, {}, {}}, 5s); + CHECK(made); + return std::move(*made); +} +std::string source_db() { + return env("PGDATABASE"); +} +std::string clone_db() { + return env("EVO_PG_CLONE_DATABASE"); +} +std::string sqlstate(const PgFailure& f) { + return f.sqlstate.data(); +} +std::unique_ptr store(const ServerRegion& r) { + auto made = FilesystemObjectStore::open(r.root, r.key, max_lineage_payload_bytes); + CHECK(made); + return std::move(*made); +} +// An empty object root for a clone region. +ServerRegion clone_target(RegionKey key, const std::string& name) { + ServerRegion r{key, fs::path(env("PGHOST")) / name}; + fs::remove_all(r.root); + fs::create_directory(r.root); + fs::permissions(r.root, fs::perms::owner_all, fs::perm_options::replace); + return r; +} +void retain_in_clone_database() { + CHECK(connect_to(clone_db(), env("PGUSER")) + ->query("INSERT INTO evolution.retained_builds(build_identity) VALUES " + "(decode(repeat('b1', 32), 'hex')) ON CONFLICT DO NOTHING", + {}, 5s)); +} +std::vector with(std::vector args, const std::string& flag, + const std::string& value) { + const auto at = std::ranges::find(args, flag); + CHECK(at != args.end() && at + 1 != args.end()); + *(at + 1) = value; + return args; +} +// evo-server for a region of the clone database, with its own runtime login. +std::vector clone_server_args(const ServerRegion& r) { + return with(with(server_args(r), "--pg-database", clone_db()), "--pg-user", + env("EVO_PG_CLONE_RUNTIME_USER")); +} +std::vector clone_args(const ServerRegion& from, const ServerRegion& to, + std::vector extra = {}) { + std::vector args{env("EVO_WORLD_CLONE_BIN"), + "clone", + "--source-world", + std::to_string(from.key.world), + "--source-region", + std::to_string(from.key.region), + "--target-world", + std::to_string(to.key.world), + "--target-region", + std::to_string(to.key.region), + "--source-pg-host", + env("PGHOST"), + "--source-pg-port", + env("PGPORT"), + "--source-pg-database", + source_db(), + "--source-pg-user", + env("EVO_PG_RUNTIME_USER"), + "--target-pg-host", + env("PGHOST"), + "--target-pg-port", + env("PGPORT"), + "--target-pg-database", + clone_db(), + "--target-pg-user", + env("PGUSER"), + "--source-objects", + from.root.string(), + "--target-objects", + to.root.string(), + "--retained", + build_hex, + "--audit-max", + "1000"}; + args.insert(args.end(), extra.begin(), extra.end()); + return args; +} +int run_tool(const std::vector& args, std::vector* lines = nullptr) { + Child tool{args}; + const auto code = tool.wait_exit(60s); + if (lines != nullptr) + *lines = tool.lines(); + return code; +} +// Runs a fresh owner process over inputs first..last (consumer death at 20) and drains. +void run_inputs(const std::vector& args, std::uint64_t first, std::uint64_t last) { + Child server{args}; + CHECK(server.wait_for([](const std::string& l) { return l == "ready 1"; }, 20s)); + server.send(input_lines(first, last, true)); + CHECK(server.wait_line("ack " + request_hex(last) + " " + std::to_string(last) + " ", 30s)); + server.signal(SIGTERM); + CHECK(server.wait_exit(20s) == 0); +} +std::optional maybe_head(const std::string& database, RegionKey key) { + auto committed = load_region_checkpoint(*connect_to(database, env("PGUSER")), key, 5s); + if (!committed) + return std::nullopt; + return committed->head; +} +RegionHead head_in(const std::string& database, RegionKey key) { + const auto head = maybe_head(database, key); + CHECK(head.has_value()); + return head.value_or(RegionHead{}); +} +std::unique_ptr +recovered_state(const std::string& database, const std::string& user, const ServerRegion& r) { + const std::array retained{build()}; + auto recovered = recover_lineage_checkpoint(*connect_to(database, user), *store(r), r.key, + retained, budget, 5s); + CHECK(recovered); + return std::move(recovered->state); +} + +// Consistent clone into an isolated world and database; the clone continues exactly +// as its source does, and inherits its outcomes and ancestry. +void clone_matches_the_source_trajectory() { + retain_in_clone_database(); + const auto source = provision(71, 0xE8); + run_inputs(server_args(source), 1, 30); // Consumer dies at tick 20. + const auto cut = head_in(source_db(), source.key); + CHECK(cut.end_tick == 30 && cut.sequence >= 2); + + const auto target = clone_target({8, 71}, "clone-71"); + std::vector out; + CHECK(run_tool(clone_args(source, target), &out) == 0); + CHECK(out.size() == 1 && + out[0].starts_with("cloned source=7/71 sequence=" + std::to_string(cut.sequence) + + " tick=30 target=8/71 outcomes=1 digest=")); + // The clone is a verified region of world 8, recovered with the clone runtime. + const auto clone_user = env("EVO_PG_CLONE_RUNTIME_USER"); + auto cloned = recovered_state(clone_db(), clone_user, target); + CHECK(cloned->community().world() == 8 && cloned->community().progress().tick == 30); + const auto consumer = cloned->archive().organism(11); + CHECK(consumer && !consumer->alive && consumer->died.value == 20); + CHECK(cloned->archive().ancestry(2)->size() == 2); // Retained ancestry. + { + const std::array retained{build()}; + auto clone_reader = connect_to(clone_db(), clone_user); + auto decisions = load_region_decisions(*clone_reader, target.key, 5s); + CHECK(decisions && decisions->size() == 1 && (*decisions)[0].commit_sequence == 0 && + (*decisions)[0].decision == BatchDecision{20, Actor::consumer, Outcome::death, 11}); + auto audited = audit_region_history(*clone_reader, *store(target), target.key, retained, + budget, 16, 5s); + CHECK(audited && audited->commits == 1 && audited->clone && audited->end_tick == 30 && + audited->outcomes == 1 && audited->origin_commit == cut.commit && + audited->origin_digest == cut.digest); + // No outbox rows: the source already delivered those outcomes. + auto pending = pending_region_outbox(*clone_reader, target.key, 64, 5s); + CHECK(pending && pending->empty()); + } + // Baseline versus clone: the same inputs from the cut give the same world. + run_inputs(server_args(source), 31, 90); + run_inputs(clone_server_args(target), 31, 90); + auto baseline = recovered_state(source_db(), env("EVO_PG_RUNTIME_USER"), source); + auto restored = recovered_state(clone_db(), clone_user, target); + CHECK(baseline->community().progress().tick == 90 && + restored->community().progress().tick == 90); + CHECK(restored->rebind_world(7)); + CHECK(restored->snapshot() == baseline->snapshot()); + // The clone chain continues from its own genesis, which names the source cut. + const auto clone_head = head_in(clone_db(), target.key); + CHECK(clone_head.sequence >= 1 && clone_head.end_tick == 90); +} + +// A clone has no path to write its source: read-only source session, separate +// credentials and database, a different world identity and immutable origins. +void clone_cannot_write_its_source() { + const auto source = provision(72, 0xE9); + run_inputs(server_args(source), 1, 25); + const auto before = head_in(source_db(), source.key); + CHECK(before.end_tick == 25); + const std::array retained{build()}; + auto reader = connect_to(source_db(), env("EVO_PG_RUNTIME_USER")); + auto owner_db = connect_to(source_db(), env("PGUSER")); + auto source_objects = store(source); + const auto same_db = clone_target({10, 72}, "clone-72"); + + CHECK(clone_region(*reader, *source_objects, source.key, *owner_db, *store(same_db), {7, 99}, + filled<16>(0xEC), retained, budget, 5s) + .error() + .kind == CloneError::identity); + // A world that already holds an original region is not an isolated clone world. + { + const RegionKey original{9, 1}; + auto region = evo::test::lineage_region(); + CHECK(region->rebind_world(9)); + const auto root = clone_target(original, "original-9"); + CHECK(provision_lineage_region(*owner_db, *store(root), original, + evo::test::region_identity.frame, *region, filled<16>(0xF8), + build(), budget, 5s)); + const auto mixed = clone_target({9, 72}, "mixed-9"); + auto refused = clone_region(*reader, *source_objects, source.key, *owner_db, *store(mixed), + mixed.key, filled<16>(0xED), retained, budget, 5s); + CHECK(!refused && refused.error().kind == CloneError::rejected && + sqlstate(refused.error().store.cause) == "22023"); + CHECK(!maybe_head(source_db(), mixed.key)); + } + // Same-database clone into a fresh world: the database also checks the origin. + auto cloned = clone_region(*reader, *source_objects, source.key, *owner_db, *store(same_db), + same_db.key, filled<16>(0xEE), retained, budget, 5s); + CHECK(cloned && cloned->inherited_outcomes == 1 && cloned->origin.tick == 25 && + cloned->origin.sequence == before.sequence && cloned->origin.commit == before.commit); + // The source session is read-only for good, even through SECURITY DEFINER functions. + auto denied = acquire_region_lease(*reader, source.key, 4000ms, 5s); + CHECK(!denied && sqlstate(denied.error().cause) == "25006"); + // Authority over the clone is not authority over the source. + auto runtime = connect_to(source_db(), env("EVO_PG_RUNTIME_USER")); + auto lease = acquire_region_lease(*runtime, same_db.key, 4000ms, 5s); + CHECK(lease); + auto source_head = load_region_checkpoint(*runtime, source.key, 5s); + CHECK(source_head); + const auto source_manifest = decode_manifest(source_head->manifest); + CHECK(source_manifest); + auto forged = successor_manifest(*source_manifest, source_head->head.digest, filled<16>(0xEF), + *lease, 26, build(), 100, filled<32>(0xA4), + {trace_bytes_for(1), filled<32>(0x7C), 0, {}}); + CHECK(forged); + auto published = publish_region_checkpoint(*runtime, *encode_manifest(*forged), {}, 5s); + CHECK(!published && published.error().kind == LeaseStoreError::fenced); + // A forged origin is refused when the source is in the same database. + auto wrong = clone_manifest(*source_manifest, filled<32>(0x55), {11, 72}, filled<16>(0xF6), 100, + filled<32>(0xA5), {0, {}, 0, {}}); + CHECK(wrong); + const std::array p{"\\x" + hex(*encode_manifest(*wrong)), "7", "72", + std::to_string(before.sequence), "\\x"}; + const std::string provision_clone = + "SELECT evolution.provision_region_clone($1::bytea, $2::evolution.u64, " + "$3::evolution.u64, $4::evolution.u64, $5::bytea)"; + auto forged_origin = owner_db->query(provision_clone, p, 5s); + CHECK(!forged_origin && sqlstate(forged_origin.error()) == "22023"); + // Only the owner provisions clones; the runtime role cannot. + auto runtime_clone = runtime->query(provision_clone, p, 5s); + CHECK(!runtime_clone && sqlstate(runtime_clone.error()) == "42501"); + // A clone world never receives an original region, and origins are immutable. + { + auto region = evo::test::lineage_region(); + CHECK(region->rebind_world(10)); + const auto extra = clone_target({10, 73}, "clone-10-73"); + auto plain = provision_lineage_region(*owner_db, *store(extra), extra.key, + evo::test::region_identity.frame, *region, + filled<16>(0xF7), build(), budget, 5s); + CHECK(!plain && plain.error().kind == PublicationError::rejected); + } + auto rewrite = owner_db->query( + "UPDATE evolution.region_origins SET source_tick = 0 WHERE world_id = 10", {}, 5s); + CHECK(!rewrite && sqlstate(rewrite.error()) == "EVL09"); + // The clone database's runtime cannot reach the source database's functions. + auto foreign = connect_to(source_db(), env("EVO_PG_CLONE_RUNTIME_USER")); + auto crossed = acquire_region_lease(*foreign, source.key, 4000ms, 5s); + CHECK(!crossed && sqlstate(crossed.error().cause) == "42501"); + // Nothing above moved the source. + const auto after = head_in(source_db(), source.key); + CHECK(after.commit == before.commit && decisions_of(source.key) == 1); + CHECK(release_region_lease(*runtime, same_db.key, *lease, 5s)); +} + +std::vector read_file(const fs::path& p) { + std::ifstream in(p, std::ios::binary); + return {std::istreambuf_iterator(in), {}}; +} +void write_file(const fs::path& p, const std::vector& bytes) { + fs::remove(p); + std::ofstream(p, std::ios::binary) + .write(bytes.data(), static_cast(bytes.size())); + fs::permissions(p, fs::perms::owner_read | fs::perms::owner_write, fs::perm_options::replace); +} + +// A clone killed before its target transaction leaves the source untouched and the +// target unprovisioned (never ready); a retry completes. Bad inputs are refused. +void failed_clone_changes_nothing() { + const auto source = provision(73, 0xEA); + run_inputs(server_args(source), 1, 25); + const auto before = head_in(source_db(), source.key); + const auto target = clone_target({8, 73}, "clone-73"); + CHECK(run_tool(clone_args(source, target, {"--crash-at", "before-transaction"})) == + 128 + SIGKILL); + CHECK(!maybe_head(clone_db(), target.key)); + CHECK(object_count(target) == 1); // The verified upload is an unreferenced orphan. + const auto after = head_in(source_db(), source.key); + CHECK(after.commit == before.commit && decisions_of(source.key) == 1); + { + Child server{clone_server_args(target)}; + CHECK(server.wait_exit(20s) == 4); + CHECK(server.printed("phase quarantined") && !server.printed("ready 1")); + } + // Refusals before anything is read or written. + CHECK(run_tool(with(clone_args(source, target), "--target-pg-database", source_db())) == 2); + CHECK(run_tool(with(clone_args(source, target), "--target-pg-user", + env("EVO_PG_RUNTIME_USER"))) == 2); + CHECK(run_tool(with(clone_args(source, target), "--target-world", "7")) == 4); + CHECK(run_tool({env("EVO_WORLD_CLONE_BIN")}) == 2); + CHECK(!maybe_head(clone_db(), target.key)); + // Retry: the same verified bytes, one object, now provisioned. + CHECK(run_tool(clone_args(source, target)) == 0); + CHECK(object_count(target) == 1); + const auto cloned = head_in(clone_db(), target.key); + CHECK(cloned.sequence == 0 && cloned.end_tick == 25); + // A quarantined source (damaged head payload) is never cloned. + const auto m = decode_manifest( + load_region_checkpoint(*connect_to(source_db(), env("PGUSER")), source.key, 5s)->manifest); + CHECK(m); + const auto payload = source.objects() / hex(m->payload_sha256); + const auto original = read_file(payload); + auto flipped = original; + flipped[flipped.size() / 2] = static_cast(flipped[flipped.size() / 2] ^ 1); + write_file(payload, flipped); + const auto other = clone_target({8, 173}, "clone-173"); + CHECK(run_tool(clone_args(source, other)) == 3); + CHECK(!maybe_head(clone_db(), other.key)); + CHECK(!fs::exists(other.objects()) || object_count(other) == 0); + write_file(payload, original); +} + +// Every committed link stays verifiable across owners and kills; losing any +// historical evidence is detected even though head recovery still succeeds. +void history_is_retained() { + const auto source = provision(74, 0xEB); + { + Child first{server_args(source)}; + CHECK(first.wait_for([](const std::string& l) { return l == "ready 1"; }, 20s)); + first.send(input_lines(1, 100, true)); + CHECK(first.wait_line("ack " + request_hex(100) + " 100 ", 30s)); + first.signal(SIGKILL); + CHECK(first.wait_exit(20s) == 128 + SIGKILL); + } + run_inputs(server_args(source), 101, 200); // The successor waits out the lease. + const auto head = head_in(source_db(), source.key); + CHECK(head.end_tick == 200 && head.sequence >= 4); + const std::array retained{build()}; + auto reader = connect_to(source_db(), env("EVO_PG_RUNTIME_USER")); + auto objects = store(source); + auto audited = audit_region_history(*reader, *objects, source.key, retained, budget, 1000, 5s); + CHECK(audited && audited->commits == head.sequence + 1 && audited->end_tick == 200 && + audited->outcomes == 1 && !audited->clone); + std::vector out; + CHECK(run_tool({env("EVO_WORLD_CLONE_BIN"), "audit", "--world", "7", "--region", "74", + "--pg-host", env("PGHOST"), "--pg-port", env("PGPORT"), "--pg-database", + source_db(), "--pg-user", env("EVO_PG_RUNTIME_USER"), "--objects", + source.root.string(), "--retained", build_hex}, + &out) == 0); + CHECK(out.size() == 1 && out[0] == "audited commits=" + std::to_string(head.sequence + 1) + + " tick=200 outcomes=1 profile=2 clone=0"); + auto bounded = + audit_region_history(*reader, *objects, source.key, retained, budget, head.sequence, 5s); + CHECK(!bounded && bounded.error().kind == AuditError::bounds); + // Remove the first published link's trace: the head alone still recovers, but + // the audit names exactly the link whose evidence is gone. + auto first = load_region_commit(*reader, source.key, 1, 5s); + CHECK(first); + const auto m = decode_manifest(first->manifest); + CHECK(m); + const auto trace = source.objects() / hex(m->batch.trace_sha256); + const auto kept = read_file(trace); + fs::remove(trace); + auto missing = audit_region_history(*reader, *objects, source.key, retained, budget, 1000, 5s); + CHECK(!missing && missing.error().kind == AuditError::tail && missing.error().sequence == 1); + CHECK(recover_lineage_checkpoint(*reader, *objects, source.key, retained, budget, 5s)); + write_file(trace, kept); + CHECK(audit_region_history(*reader, *objects, source.key, retained, budget, 1000, 5s)); +} +// Environmental profile: the clone genesis digests its inherited outcomes. This +// profile names no organism, so only that digest pins the inherited death's tick. +void environmental_clone_binds_inherited_outcomes() { + const RegionKey key{7, 75}; + const auto source = clone_target(key, "env-75"); + auto owner_db = connect_to(source_db(), env("PGUSER")); + auto runtime = connect_to(source_db(), env("EVO_PG_RUNTIME_USER")); + auto source_objects = store(source); + auto genesis = provision_environmental_region( + *owner_db, *source_objects, key, evo::test::region_identity.frame, + *evo::test::region_community(), filled<16>(0xF9), build(), budget, 5s); + CHECK(genesis); + auto lease = acquire_region_lease(*runtime, key, 20000ms, 5s); + CHECK(lease); + auto state = evo::test::region_community(); + RegionBatch batch{*state}; + while (!batch.sealed()) + CHECK(batch.advance(evo::test::region_input(batch.end_tick() + 1, true))); + CHECK(batch.end_tick() == 20); + auto published = publish_environmental_batch(*runtime, *source_objects, *lease, *genesis, batch, + filled<16>(0xFA), build(), budget, 5s); + CHECK(published && published->decisions.size() == 1); + CHECK(release_region_lease(*runtime, key, *lease, 5s)); + + const std::array retained{build()}; + const auto target = clone_target({8, 75}, "clone-env-75"); + auto reader = connect_to(source_db(), env("EVO_PG_RUNTIME_USER")); + auto clone_owner = connect_to(clone_db(), env("PGUSER")); + auto cloned = clone_region(*reader, *source_objects, key, *clone_owner, *store(target), + target.key, filled<16>(0xFB), retained, budget, 5s); + CHECK(cloned && cloned->inherited_outcomes == 1 && cloned->origin.tick == 20); + auto clone_runtime = connect_to(clone_db(), env("EVO_PG_CLONE_RUNTIME_USER")); + const auto recover = [&] { + return recover_environmental_checkpoint(*clone_runtime, *store(target), target.key, + retained, budget, 5s); + }; + auto recovered = recover(); + CHECK(recovered && recovered->state->progress().tick == 20 && + recovered->state->progress().consumer_terminal == 1 && recovered->decisions.size() == 1); + // Break glass (owner disables immutability) to move the inherited death one tick. + const auto set_tick = [&](int from, int to) { + const std::array p{std::to_string(from), std::to_string(to)}; + CHECK(clone_owner->query("ALTER TABLE evolution.region_decisions DISABLE TRIGGER " + "region_decisions_immutable", + {}, 5s)); + CHECK(clone_owner->query("UPDATE evolution.region_decisions SET tick = $2::evolution.u64 " + "WHERE world_id = 8 AND region_id = 75 AND " + "tick = $1::evolution.u64", + p, 5s)); + CHECK(clone_owner->query("ALTER TABLE evolution.region_decisions ENABLE TRIGGER " + "region_decisions_immutable", + {}, 5s)); + }; + set_tick(20, 19); + auto moved = recover(); + CHECK(!moved && moved.error().kind == RecoveryError::history); + auto audited = + audit_region_history(*clone_runtime, *store(target), target.key, retained, budget, 8, 5s); + CHECK(!audited && audited.error().kind == AuditError::history && audited.error().sequence == 0); + set_tick(19, 20); + CHECK(recover()); +} +} // namespace + +void run_region_clone_tests() { + clone_matches_the_source_trajectory(); + clone_cannot_write_its_source(); + failed_clone_changes_nothing(); + history_is_retained(); + environmental_clone_binds_inherited_outcomes(); +} diff --git a/tools/ci/postgres-check.sh b/tools/ci/postgres-check.sh index 8703d11..ed15ef4 100644 --- a/tools/ci/postgres-check.sh +++ b/tools/ci/postgres-check.sh @@ -17,9 +17,16 @@ psql -X -v ON_ERROR_STOP=1 -c 'CREATE ROLE evo_runtime_test NOLOGIN' \ export EVO_PG_RUNTIME_ROLE=evo_runtime_test EVO_PG_RUNTIME_USER=evo_runtime_login bash "$root/tools/migrate-postgres.sh" bash "$root/tools/migrate-postgres.sh" # exact supported upgrade/restart is a no-op -# Process tests spawn the server built beside the test binary. -export EVO_SERVER_BIN +# M7.9 clone target: a separate database with its own runtime group and login, +# so a clone shares neither the source's database nor its runtime credentials. +psql -X -v ON_ERROR_STOP=1 -c 'CREATE ROLE evo_clone_runtime NOLOGIN' -c 'CREATE ROLE evo_clone_login LOGIN IN ROLE evo_clone_runtime' +createdb evo_clone +(export PGDATABASE=evo_clone EVO_PG_RUNTIME_ROLE=evo_clone_runtime; bash "$root/tools/migrate-postgres.sh") +export EVO_PG_CLONE_DATABASE=evo_clone EVO_PG_CLONE_RUNTIME_USER=evo_clone_login +# Process tests spawn the server and clone tool built beside the test binary. +export EVO_SERVER_BIN EVO_WORLD_CLONE_BIN EVO_SERVER_BIN="$(dirname "${1:?path to evo-postgres-tests}")/evo-server" +EVO_WORLD_CLONE_BIN="$(dirname "$1")/evo-world-clone" "$1" v1_sha="$(sha256sum "$root/migrations/0001_region_bootstrap.sql")" v1_sha="${v1_sha%% *}" @@ -46,7 +53,7 @@ PGDATABASE=upgrade_path psql -X -v ON_ERROR_STOP=1 -1 -v "migration_sha=$v1_sha" -f "$root/migrations/0001_region_bootstrap.sql" > /dev/null test "$(PGDATABASE=upgrade_path psql -XAtc "SELECT string_agg(version::text, ',') FROM evolution.schema_migrations")" = 1 PGDATABASE=upgrade_path bash "$root/tools/migrate-postgres.sh" -test "$(PGDATABASE=upgrade_path psql -XAtc "SELECT string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 1,2,3,4,5,6 +test "$(PGDATABASE=upgrade_path psql -XAtc "SELECT string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 1,2,3,4,5,6,7 # v2 heads have no payload binding: v2->v3 fails closed and leaves history at v2. v2_sha="$(sha256sum "$root/migrations/0002_region_fencing.sql")" v2_sha="${v2_sha%% *}" @@ -67,7 +74,7 @@ PGDATABASE=v3_commits psql -X -v ON_ERROR_STOP=1 \ -c "INSERT INTO evolution.retained_builds VALUES (decode(repeat('b1', 32), 'hex'))" \ -c "SELECT evolution.provision_region_checkpoint(decode('4145564f52434d31' || '01000000' || '01000000' || '0100000000000000' || '0100000000000000' || '0900000000000000' || repeat('11', 16) || repeat('00', 8) || repeat('00', 16) || repeat('00', 32) || repeat('00', 16) || repeat('00', 16) || repeat('00', 8) || '50c3000000000000' || '020000000200000001000000010000000100000001000000010000000100000001000000' || repeat('b1', 32) || '00000000' || '00000000' || '01000000' || '6400000000000000' || repeat('a0', 32), 'hex'))" > /dev/null PGDATABASE=v3_commits bash "$root/tools/migrate-postgres.sh" -test "$(PGDATABASE=v3_commits psql -XAtc "SELECT count(*) || ':' || string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 6:1,2,3,4,5,6 +test "$(PGDATABASE=v3_commits psql -XAtc "SELECT count(*) || ':' || string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 7:1,2,3,4,5,6,7 test "$(PGDATABASE=v3_commits psql -XAtc "SELECT count(*) FROM evolution.region_commits")" = 1 # Review P1: a v4 database holding a published (possibly terminal) checkpoint has # no durable decisions for it. The v5 upgrade must refuse before changing anything. @@ -104,6 +111,26 @@ test "$(PGDATABASE=v4_published psql -XAtc "SELECT string_agg(version::text, ',' test "$(PGDATABASE=v4_published psql -XAtc "SELECT count(*) || ':' || max(end_tick) FROM evolution.region_commits")" = 2:20 test "$(PGDATABASE=v4_published psql -XAtc "SELECT count(*) FROM pg_tables WHERE schemaname = 'evolution' AND tablename = 'region_decisions'")" = 0 test "$(PGDATABASE=v2_heads psql -XAtc "SELECT string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 1,2 +# M7.9: a populated v6 database (genesis at sequence 0, tick 0) upgrades to v7 in +# place; the replaced head constraint still admits it and the v7 parser reads it. +v5_sha="$(sha256sum "$root/migrations/0005_batch_decisions.sql")" +v5_sha="${v5_sha%% *}" +v6_sha="$(sha256sum "$root/migrations/0006_commit_lookup.sql")" +v6_sha="${v6_sha%% *}" +createdb v6_populated +PGDATABASE=v6_populated psql -X -v ON_ERROR_STOP=1 -1 -v "migration_sha=$v1_sha" \ + -v "migration_sha_2=$v2_sha" -v "migration_sha_3=$v3_sha" -v "migration_sha_4=$v4_sha" \ + -v "migration_sha_5=$v5_sha" -v "migration_sha_6=$v6_sha" -v runtime_role=evo_runtime_test \ + -f "$root/migrations/0001_region_bootstrap.sql" -f "$root/migrations/0002_region_fencing.sql" \ + -f "$root/migrations/0003_region_checkpoints.sql" -f "$root/migrations/0004_lineage_profile.sql" \ + -f "$root/migrations/0005_batch_decisions.sql" -f "$root/migrations/0006_commit_lookup.sql" > /dev/null +PGDATABASE=v6_populated psql -X -v ON_ERROR_STOP=1 \ + -c "INSERT INTO evolution.retained_builds VALUES (decode(repeat('b1', 32), 'hex'))" \ + -c "SELECT evolution.provision_region_checkpoint(decode('4145564f52434d31' || '02000000' || '01000000' || '0100000000000000' || '0100000000000000' || '0900000000000000' || repeat('11', 16) || repeat('00', 8) || repeat('00', 16) || repeat('00', 32) || repeat('00', 16) || repeat('00', 16) || repeat('00', 8) || '50c3000000000000' || '020000000200000001000000010000000100000001000000010000000100000001000000' || repeat('b1', 32) || '00000000' || '00000000' || '01000000' || '6400000000000000' || repeat('a0', 32) || repeat('00', 80), 'hex'))" > /dev/null +PGDATABASE=v6_populated bash "$root/tools/migrate-postgres.sh" +test "$(PGDATABASE=v6_populated psql -XAtc "SELECT string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 1,2,3,4,5,6,7 +test "$(PGDATABASE=v6_populated psql -XAtc "SELECT count(*) FROM evolution.region_commits c, LATERAL evolution.parse_checkpoint_manifest(c.manifest) m WHERE m.commit_sequence = 0 AND m.predecessor_commit IS NULL")" = 1 +test "$(PGDATABASE=v6_populated psql -XAtc "SELECT string_agg(conname, ',') FROM pg_constraint WHERE conrelid = 'evolution.region_heads'::regclass AND contype = 'c' AND pg_get_constraintdef(oid) LIKE '%end_tick%'")" = region_heads_published_tick # Inherited privileges bypass fencing, so every migrate run audits effective rights. psql -X -v ON_ERROR_STOP=1 -c 'CREATE ROLE evo_bad_runtime NOLOGIN IN ROLE pg_write_all_data' if (export PGDATABASE=migration_rollback EVO_PG_RUNTIME_ROLE=evo_bad_runtime; \ @@ -136,9 +163,11 @@ expect_audit_failure 'GRANT EXECUTE ON FUNCTION evolution.provision_region_check expect_audit_failure 'GRANT SELECT ON evolution.region_commits TO evo_runtime_test' 'REVOKE SELECT ON evolution.region_commits FROM evo_runtime_test' bash "$root/tools/migrate-postgres.sh" # Restored configuration passes the audit again. # Unknown future versions and checksum drift must not be repaired silently. -psql -X -v ON_ERROR_STOP=1 -c 'UPDATE evolution.schema_migrations SET version=7 WHERE version=6' +psql -X -v ON_ERROR_STOP=1 -c 'UPDATE evolution.schema_migrations SET version=8 WHERE version=7' +if bash "$root/tools/migrate-postgres.sh"; then exit 1; fi +psql -X -v ON_ERROR_STOP=1 -c "UPDATE evolution.schema_migrations SET version=7 WHERE version=8" +psql -X -v ON_ERROR_STOP=1 -c "UPDATE evolution.schema_migrations SET sha256=repeat('0',64) WHERE version=7" if bash "$root/tools/migrate-postgres.sh"; then exit 1; fi -psql -X -v ON_ERROR_STOP=1 -c "UPDATE evolution.schema_migrations SET version=6 WHERE version=7" psql -X -v ON_ERROR_STOP=1 -c "UPDATE evolution.schema_migrations SET sha256=repeat('0',64) WHERE version=6" if bash "$root/tools/migrate-postgres.sh"; then exit 1; fi psql -X -v ON_ERROR_STOP=1 -c "UPDATE evolution.schema_migrations SET sha256=repeat('0',64) WHERE version=5" diff --git a/tools/migrate-postgres.sh b/tools/migrate-postgres.sh index 107adbe..70ab03b 100644 --- a/tools/migrate-postgres.sh +++ b/tools/migrate-postgres.sh @@ -24,9 +24,11 @@ digest_5="$(sha256sum "$root/migrations/0005_batch_decisions.sql")" digest_5="${digest_5%% *}" digest_6="$(sha256sum "$root/migrations/0006_commit_lookup.sql")" digest_6="${digest_6%% *}" +digest_7="$(sha256sum "$root/migrations/0007_region_clones.sql")" +digest_7="${digest_7%% *}" psql -X --no-password --set=ON_ERROR_STOP=1 --set="migration_sha=$digest" \ --set="migration_sha_2=$digest_2" --set="migration_sha_3=$digest_3" \ --set="migration_sha_4=$digest_4" --set="migration_sha_5=$digest_5" \ - --set="migration_sha_6=$digest_6" \ + --set="migration_sha_6=$digest_6" --set="migration_sha_7=$digest_7" \ --set="runtime_role=$runtime_role" \ --file="$root/migrations/apply.sql"