From 4130092d7a31988a1f7561330b4ca94f900bdf81 Mon Sep 17 00:00:00 2001 From: dingo Date: Wed, 14 Oct 2026 07:26:33 -0400 Subject: [PATCH] End dead lives durably so a player can start again M11.6a (ADR-0127, schema v37): a published life is marked ended once, by a commit of its region, through the lease-fenced end_cellular_lives. One active life per account replaces one life per account, so after a death the account reserves and publishes a new primitive founder while the corpse, archive and descendants stay. The host records endings after the death's publication and again for every founder corpse on recovery. The cellular server now refuses only an unusable carried action, not the whole tick. Co-Authored-By: Claude Opus 5.5 --- apps/evo-server/main.cpp | 20 +- .../0127-cellular-permadeath-and-restart.md | 73 +++++++ docs/adr/README.md | 1 + docs/engineering/M11_6_DELIVERY_PLAN.md | 38 ++++ docs/engineering/VALIDATION.md | 21 ++ .../persistence/life_reservation_store.hpp | 8 + include/evo/server/cellular_region_host.hpp | 9 + migrations/0037_cellular_life_endings.sql | 202 ++++++++++++++++++ migrations/apply.sql | 27 ++- src/persistence/region_lease_store.cpp | 27 +++ src/server/cellular_region_host.cpp | 48 +++++ tests/cellular_publication_tests.cpp | 167 +++++++++++++-- tests/region_migration_tests.cpp | 2 +- tools/ci/postgres-check.sh | 10 +- tools/migrate-postgres.sh | 3 + 15 files changed, 626 insertions(+), 30 deletions(-) create mode 100644 docs/adr/0127-cellular-permadeath-and-restart.md create mode 100644 docs/engineering/M11_6_DELIVERY_PLAN.md create mode 100644 migrations/0037_cellular_life_endings.sql diff --git a/apps/evo-server/main.cpp b/apps/evo-server/main.cpp index 6dc3c30..bed0656 100644 --- a/apps/evo-server/main.cpp +++ b/apps/evo-server/main.cpp @@ -1071,8 +1071,24 @@ int serve_cellular(Options& o) { if (!builder) { bridge->dropped(actions, true); } else { - const auto appended = builder->append(tick, baseline, actions, held_births, - host.queued_mutations()); + auto appended = builder->append(tick, baseline, actions, held_births, + host.queued_mutations()); + if (!appended && !actions.empty()) { + // ADR-0127: one account's unusable action (its life died, or it + // names an unperceived patch) refuses only that action, never the + // tick for every other player. + std::vector usable, refused; + for (const auto& action : actions) + (server::prepare_cellular_session_tick(*state, tick, baseline, + std::span{&action, 1}) + ? usable + : refused) + .push_back(action); + bridge->dropped(refused, true); + actions = std::move(usable); + appended = builder->append(tick, baseline, actions, held_births, + host.queued_mutations()); + } if (!appended) { bridge->dropped(actions, true); } else { diff --git a/docs/adr/0127-cellular-permadeath-and-restart.md b/docs/adr/0127-cellular-permadeath-and-restart.md new file mode 100644 index 0000000..ff34f57 --- /dev/null +++ b/docs/adr/0127-cellular-permadeath-and-restart.md @@ -0,0 +1,73 @@ +# ADR-0127: Cellular permadeath and restart + +## Status + +Accepted engineering contract for M11.6 (2026-10-14). Implements the absolute +permadeath requirement (PROJECT_VISION, ADR-0098) for cellular regions. + +## Context + +A cellular life dies by starvation or engulfment inside a replayed tick; the owner +removes it, records its death in the archive and keeps a corpse (ADR-0107, +ADR-0124). Nothing durable outside the checkpoint said the life had ended. Schema +v15 allowed one unrefused reservation per account (`one_reserved_life_per_account`), +and `primitive_life_bindings` held one published life per account, so a player +whose cell died could never start again: every new request was refused as +`existing_life`. In the M11.3 probe this showed up as players making a new account +after each death. + +## Decision + +### Death is terminal + +- The owner never revives a life: recovery replays committed history, and a dead + identity can never appear again in a checkpoint (unique life identities, archive + death records, corpse retention). +- Corpses keep their records, and the archive keeps the organism's death; the dead + life's descendants (ADR-0125) are unaffected. + +### Ending a life durably (schema v37) + +- `life_reservations.ended_commit` marks a published life ended, once, by a commit + of the region that holds it. A trigger refuses changing or clearing it, or setting + it on an unpublished or refused reservation. +- `end_cellular_lives(world, region, holder, epoch, commit, identities)` is the + lease-fenced runtime call. It accepts at most 256 identities, ignores identities + that were never reserved (opening founders), treats repeats as no-ops, and refuses + a life of another region or a commit outside the region. +- The region host records endings right after the publication in which an owned + life disappeared, before the next session poll. On recovery it records every + founder corpse again, so an ending lost to a crash is restored. Failures are + retried on later steps and never block publication. + +### Restart + +- "One active life per account" replaces "one life per account": the unique index + becomes `one_active_life_per_account` (`rejection = 0 AND ended_commit IS NULL`), + the per-account uniqueness of published bindings is dropped, and + `reserve_primitive_life`, `primitive_life_owner` and the cellular birth publisher + consider only active lives. +- The next life is an ordinary new request: a primitive founder at a nursery site + with a new identity (ADR-0100). It inherits nothing from the dead life. +- While a life is active, a new request is still a permanent `existing_life` + refusal for that request id. + +### Old sessions + +- A carried action whose account has no living life, or that names a patch the life + cannot perceive, is refused on its own (`not_committed`); it no longer refuses the + whole tick for every player. + +## Alternatives + +- **Bind deaths inside the publication call:** rejected for now. The SQL cannot + replay the trace, so the death list would be the writer's claim either way; a + separate fenced call keeps publication unchanged and is idempotent. +- **Let any reservation start a new life:** rejected. Two concurrent lives for one + account would break control and routing. + +## Consequences + +Players can die and start again with the same account; history, corpses and +descendants remain. The death screen and restart flow in the client are the +maintainer's (client UI is deferred). diff --git a/docs/adr/README.md b/docs/adr/README.md index d1404b0..db6ab3d 100644 --- a/docs/adr/README.md +++ b/docs/adr/README.md @@ -27,6 +27,7 @@ - [ADR-0124: Cellular engulfment and detritus](0124-cellular-engulfment-and-detritus.md) - resolves engulfment of a sufficiently smaller contacted cell and deposits every corpse's inventory into the patch grid as absorbable detritus, under cellular rules 3. - [ADR-0125: Cellular budding and wildlife descendants](0125-cellular-budding-and-wildlife-descendants.md) - M11.5: a bud is v10 action kind 3, paid from the parent's reserve (ADR-0032 investment plus a 30 s provision), placed touching the parent, identified in a region namespace above 2^63, carrying the parent's genome; unowned wildlife driven by a pure deterministic controller whose intentions ride the trace. - [ADR-0126: PostgreSQL parameter bound for cellular traces](0126-postgres-parameter-bound-for-cellular-traces.md) - each query parameter may hold 4 MiB of text (was 4 KiB, ADR-0059), so every admitted cellular trace (64 ticks of 256 lives as hex bytea) reaches the database in one atomic publication. +- [ADR-0127: Cellular permadeath and restart](0127-cellular-permadeath-and-restart.md) - M11.6: schema v37 marks a published life ended once (`end_cellular_lives`, recorded by the host after the death and again on recovery); one active life per account replaces one life per account, so a dead player starts a new primitive founder; a dead account's stale action no longer refuses the whole tick. - [ADR-0058: Region durability contract](0058-region-durability-contract.md) - accepted M7.1 full checkpoint batches and explicit state closure; storage implementation follows. diff --git a/docs/engineering/M11_6_DELIVERY_PLAN.md b/docs/engineering/M11_6_DELIVERY_PLAN.md new file mode 100644 index 0000000..8a4a32a --- /dev/null +++ b/docs/engineering/M11_6_DELIVERY_PLAN.md @@ -0,0 +1,38 @@ +# M11.6: Commit permadeath and complete restart + +A cellular life that dies stays dead, its corpse and history remain, and its +player starts again as a new primitive founder. The engineering contract is +[ADR-0127](../adr/0127-cellular-permadeath-and-restart.md). The client death and +restart flow is the maintainer's (client UI is deferred). + +## Boundaries + +Read ADR-0098, ADR-0100, ADR-0107, ADR-0124, ADR-0125 and ADR-0127. Recovery and +replay must never revive a life. A restart is an ordinary new life request; it +grants no carried-over biology, position or inventory. + +## Segments + +| Segment | Affected modules | Acceptance evidence | +|---|---|---| +| 11.6a: durable endings and restart | Schema v37, life reservation store, cellular host, server tick build | A starved founder's ending is recorded; the account has no active owner region; a new request is born with a new identity while the corpse remains; the first request still reports its birth; a request during a live life is refused; a restart recovers the same state; a stale action of a dead account refuses only itself | +| 11.6b: death and restart presentation | Engineering client (maintainer) | Death cause and restart are readable; no corpse retrieval or resurrection shortcut | + +## Required checks + +- Death versus mutation, bud and logout in the same tick; crash between the + publication and the ending record (recovery records it); an old session's action + after death; repeated lives of one account are independent. +- Linux CI PostgreSQL evidence before claiming the live path. + +## Progress + +- 2026-10-14: 11.6a implemented. Schema v37 adds `life_reservations.ended_commit` + (set once by a trigger-guarded transition), `one_active_life_per_account`, drops + the one-binding-per-account constraint, and updates the reservation, owner lookup + and birth publisher to consider only active lives; `end_cellular_lives` records + endings. The host records endings after the publication of a death and again for + every founder corpse on recovery. `serve_cellular` refuses only an unusable + action. `cellular_death_is_final_and_account_restarts` (PostgreSQL) covers the + acceptance evidence above except the stale-action path, which is server-loop + behaviour. diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index d25d144..2b87a34 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -7402,3 +7402,24 @@ without moving (or buds), and that the owner accepts the intention. Executed locally: MSVC Debug `cellular-lives-owner` and `budding-contracts`; g++-14 `-Werror -Wshadow` and clang-tidy-19 on the controller, budding, owner tests and server main; clang-format 19.1.5. + +## 2026-10-14 — M11.6a permadeath and restart (CI pending) + +Schema v37 (ADR-0127) replaces "one life per account" with "one active life per +account": `ended_commit` on the reservation, a final-transition trigger, the new +unique index, and active-only reservation, owner lookup and birth publication; +`end_cellular_lives` records endings under the region lease. The cellular host +records endings after a death's publication and for every founder corpse on +recovery. `serve_cellular` refuses only the unusable carried action instead of +the whole tick. The publication tests build host configs as prvalues: copying +`CellularHostConfig` tripped a GCC 14 `-O3` array-bounds false positive. + +Tests: `cellular_death_is_final_and_account_restarts` (world 9768) starves a +founder, checks the corpse and that the account has no active owner region, +starts a second life with a new identity, checks the first request still reports +its birth and a third request is `existing_life`, then restarts the host and +compares the recovered state. + +Executed locally: `postgres-check.sh --full` (WSL g++-14 Release, 488 s) passes; g++-14 +`-Werror -Wshadow` and clang-tidy-19 on the host, lease store, tests and server +main; clang-format 19.1.5. diff --git a/include/evo/persistence/life_reservation_store.hpp b/include/evo/persistence/life_reservation_store.hpp index b6a2e90..026343d 100644 --- a/include/evo/persistence/life_reservation_store.hpp +++ b/include/evo/persistence/life_reservation_store.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include namespace evo::persistence { @@ -94,6 +95,13 @@ reserve_cellular_mutation( std::uint64_t generation, std::uint64_t action_id, const std::array& input, std::chrono::milliseconds timeout); +// Schema v37, ADR-0127: records that these published lives of this region had ended by +// `commit` (a commit of this region), so their accounts may start new lives. At most +// 256 identities; repeats are no-ops. Returns how many were newly ended. +inline constexpr std::size_t max_life_endings_per_call = 256; +[[nodiscard]] std::expected +end_cellular_lives(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& commit, + std::span identities, std::chrono::milliseconds timeout); // Pending selections are reloaded under the recovered region lease after restart. [[nodiscard]] std::expected, LifeReservationFailure> load_pending_cellular_mutations(Postgres& db, RegionKey key, const LeaseGrant& lease, diff --git a/include/evo/server/cellular_region_host.hpp b/include/evo/server/cellular_region_host.hpp index 3c3cae9..3766fd0 100644 --- a/include/evo/server/cellular_region_host.hpp +++ b/include/evo/server/cellular_region_host.hpp @@ -138,6 +138,10 @@ class CellularRegionHost { void finalize_births(std::span births); void queue_birth(const CellularTickIntents::Birth& birth); void reload_mutations(); + // ADR-0127: owned lives that died record their ending so the account may start again. + void note_endings(const CellularLives& before, const CellularLives& after, + const persistence::Uuid& commit); + void flush_endings(); void finalize_mutations(std::span actions, std::span results); [[nodiscard]] network::PrimitiveLifeOutcome query_birth(const persistence::Uuid& account, @@ -159,6 +163,11 @@ class CellularRegionHost { bool mutations_uncertain_{}; std::vector finalized_mutations_; std::vector recent_mutations_; // Bounded, for exact repeats. + struct Ending { + persistence::Uuid commit{}; + std::uint64_t life{}; + }; + std::vector pending_endings_; // Recorded endings not yet acknowledged. HostStatus status_{}; }; } // namespace evo::server diff --git a/migrations/0037_cellular_life_endings.sql b/migrations/0037_cellular_life_endings.sql new file mode 100644 index 0000000..13ec55e --- /dev/null +++ b/migrations/0037_cellular_life_endings.sql @@ -0,0 +1,202 @@ +-- M11.6, ADR-0127: permadeath and a complete restart. A life's reservation is marked +-- ended, once and forever, by the region that holds its commit; an account may then +-- reserve and publish a new founder. One ACTIVE life per account replaces one life +-- per account. Corpses, archives, descendants and every earlier row stay unchanged. + +ALTER TABLE evolution.life_reservations ADD COLUMN ended_commit uuid; +DROP INDEX evolution.one_reserved_life_per_account; +CREATE UNIQUE INDEX one_active_life_per_account + ON evolution.life_reservations(world_id, account_id) + WHERE rejection = 0 AND ended_commit IS NULL; +-- A published binding no longer implies the account's only life. +ALTER TABLE evolution.primitive_life_bindings + DROP CONSTRAINT primitive_life_bindings_world_id_account_id_key; + +-- An ending is set once, only on a published, unrefused life, and never undone. +CREATE FUNCTION evolution.life_ending_is_final() RETURNS trigger +LANGUAGE plpgsql SECURITY INVOKER SET search_path = pg_catalog, pg_temp AS $$ +BEGIN + IF OLD.ended_commit IS NOT NULL AND NEW.ended_commit IS DISTINCT FROM OLD.ended_commit THEN + RAISE EXCEPTION 'life ending is final' USING ERRCODE = 'EVL16'; + END IF; + IF NEW.ended_commit IS NOT NULL AND OLD.ended_commit IS NULL + AND (NEW.rejection <> 0 OR NOT EXISTS (SELECT 1 FROM evolution.primitive_life_bindings b + WHERE b.world_id = NEW.world_id AND b.account_id = NEW.account_id + AND b.request_id = NEW.request_id)) THEN + RAISE EXCEPTION 'only a published life can end' USING ERRCODE = 'EVL16'; + END IF; + RETURN NEW; +END $$; +CREATE TRIGGER life_ending_is_final BEFORE UPDATE OF ended_commit + ON evolution.life_reservations FOR EACH ROW EXECUTE FUNCTION evolution.life_ending_is_final(); + +-- Unchanged from schema v17 except that an ended life no longer blocks a new request. +CREATE OR REPLACE FUNCTION evolution.reserve_primitive_life(p_world evolution.u64, + p_region evolution.u64, p_holder uuid, p_epoch evolution.u64, p_account uuid, + p_generation evolution.u64, p_request evolution.u64, p_rules integer) +RETURNS TABLE (life_identity evolution.u64, life_region evolution.u64, rejection smallint) +LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ +DECLARE r evolution.life_reservations%ROWTYPE; allocated numeric; +BEGIN + IF p_request IS NULL OR p_request = 0 OR p_rules IS DISTINCT FROM 1 THEN + RAISE EXCEPTION 'invalid life request' USING ERRCODE = '22023'; + END IF; + PERFORM evolution.check_life_control(p_world, p_region, p_holder, p_epoch, p_account, p_generation); + SELECT * INTO r FROM evolution.life_reservations x + WHERE x.world_id = p_world AND x.account_id = p_account AND x.request_id = p_request; + IF FOUND THEN + RETURN QUERY SELECT r.identity, r.region_id, r.rejection; + RETURN; + END IF; + SELECT a.next_identity INTO allocated FROM evolution.life_allocators a + WHERE a.world_id = p_world FOR UPDATE; + IF NOT FOUND THEN + RAISE EXCEPTION 'life allocator not provisioned' USING ERRCODE = 'EVL04'; + END IF; + IF allocated = 18446744073709551615 THEN + RAISE EXCEPTION 'life identities exhausted' USING ERRCODE = 'EVL05'; + END IF; + PERFORM evolution.check_life_control(p_world, p_region, p_holder, p_epoch, p_account, p_generation); + IF EXISTS (SELECT 1 FROM evolution.life_reservations x + WHERE x.world_id = p_world AND x.account_id = p_account AND x.rejection = 0 + AND x.ended_commit IS NULL) THEN + UPDATE evolution.life_allocators SET next_identity = allocated + 1 WHERE world_id = p_world; + INSERT INTO evolution.life_reservations(world_id, account_id, request_id, identity, + region_id, rules, rejection) + VALUES (p_world, p_account, p_request, allocated, p_region, p_rules, 4); + RETURN QUERY SELECT allocated::evolution.u64, p_region, 4::smallint; + RETURN; + END IF; + UPDATE evolution.life_allocators SET next_identity = allocated + 1 WHERE world_id = p_world; + INSERT INTO evolution.life_reservations(world_id, account_id, request_id, identity, region_id, rules) + VALUES (p_world, p_account, p_request, allocated, p_region, p_rules); + RETURN QUERY SELECT allocated::evolution.u64, p_region, 0::smallint; +END $$; + +-- Unchanged from schema v24 except that only the active life has an owner region. +CREATE OR REPLACE FUNCTION evolution.primitive_life_owner(p_world evolution.u64, + p_region evolution.u64, p_holder uuid, p_epoch evolution.u64, p_account uuid) +RETURNS TABLE (owner_region evolution.u64) +LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ +BEGIN + IF p_world IS NULL OR p_world = 0 OR p_region IS NULL OR p_region = 0 + OR p_holder IS NULL OR p_epoch IS NULL OR p_epoch = 0 + OR p_account IS NULL OR p_account = '00000000-0000-0000-0000-000000000000' THEN + RAISE EXCEPTION 'invalid life owner lookup' USING ERRCODE = '22023'; + END IF; + PERFORM evolution.check_region_lease(p_world, p_region, p_holder, p_epoch); + RETURN QUERY SELECT r.region_id FROM evolution.life_reservations r + WHERE r.world_id = p_world AND r.account_id = p_account AND r.rejection = 0 + AND r.ended_commit IS NULL; +END $$; + +-- Unchanged from schema v31 (renamed in v35) except that only an active binding of the +-- account conflicts with a new birth. +CREATE OR REPLACE FUNCTION evolution.publish_cellular_birth_checkpoint(p_manifest bytea, + p_decisions bytea, p_actions bytea, p_trace bytea) +RETURNS TABLE(region_frame evolution.u64, head_sequence evolution.u64, head_commit uuid, + head_digest text, head_tick evolution.u64) +LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ +DECLARE + c evolution.checkpoint_manifest := evolution.parse_checkpoint_manifest(p_manifest); + birth_row record; + r evolution.life_reservations%ROWTYPE; +BEGIN + IF c.profile <> 4 OR c.commit_sequence = 0 OR p_actions IS NULL THEN + RAISE EXCEPTION 'invalid cellular publication' USING ERRCODE = '22023'; + END IF; + PERFORM evolution.check_cellular_action_block(p_actions, c.begin_tick, c.end_tick); + PERFORM * FROM evolution.cellular_births(p_trace, p_manifest); + IF EXISTS (SELECT b.account_id FROM evolution.cellular_births(p_trace, p_manifest) b + GROUP BY b.account_id HAVING count(*) > 1) + OR EXISTS (SELECT b.identity FROM evolution.cellular_births(p_trace, p_manifest) b + GROUP BY b.identity HAVING count(*) > 1) THEN + RAISE EXCEPTION 'duplicate cellular birth identity' USING ERRCODE = '22023'; + END IF; + PERFORM evolution.check_region_lease(c.world_id, c.region_id, c.holder, c.epoch); + -- The lease lock fences competing commits. Exact retries are accepted only + -- when the immutable binding rows agree with every birth in the trace. + IF EXISTS (SELECT 1 FROM evolution.region_commits x WHERE x.commit_id = c.commit_id) THEN + IF NOT evolution.cellular_birth_bindings_match(c.world_id, c.commit_id, p_trace, p_manifest) THEN + RAISE EXCEPTION 'cellular birth evidence differs on retry' USING ERRCODE = 'EVL07'; + END IF; + RETURN QUERY SELECT * FROM evolution.publish_cellular_checkpoint( + p_manifest, p_decisions, p_actions); + RETURN; + END IF; + -- Lock accounts in stable order and require the exact current control generation + -- and previously allocated request identity before inserting any binding. + FOR birth_row IN SELECT * FROM evolution.cellular_births(p_trace, p_manifest) ORDER BY account_id LOOP + PERFORM evolution.check_life_control(c.world_id, c.region_id, c.holder, c.epoch, + birth_row.account_id, birth_row.generation); + SELECT * INTO r FROM evolution.life_reservations x WHERE x.world_id = c.world_id + AND x.account_id = birth_row.account_id AND x.request_id = birth_row.request_id FOR UPDATE; + IF NOT FOUND OR r.region_id <> c.region_id OR r.identity <> birth_row.identity + OR r.rejection <> 0 OR r.rules <> 1 + OR EXISTS (SELECT 1 FROM evolution.primitive_life_bindings x + JOIN evolution.life_reservations y USING (world_id, account_id, request_id) + WHERE x.world_id = c.world_id AND x.account_id = birth_row.account_id + AND y.ended_commit IS NULL) THEN + RAISE EXCEPTION 'cellular birth reservation conflicts' USING ERRCODE = 'EVL16'; + END IF; + END LOOP; + FOR birth_row IN SELECT * FROM evolution.cellular_births(p_trace, p_manifest) ORDER BY account_id LOOP + PERFORM evolution.check_life_control(c.world_id, c.region_id, c.holder, c.epoch, + birth_row.account_id, birth_row.generation); + END LOOP; + INSERT INTO evolution.primitive_life_bindings(world_id, account_id, request_id, born, commit_id) + SELECT c.world_id, b.account_id, b.request_id, b.born, c.commit_id + FROM evolution.cellular_births(p_trace, p_manifest) b; + RETURN QUERY SELECT * FROM evolution.publish_cellular_checkpoint( + p_manifest, p_decisions, p_actions); +END $$; + +-- The region holding the commit records which of its published lives had ended by then. +-- Identities are u64 little-endian, at most 256 per call. Repeats and identities never +-- reserved (opening founders) are no-ops; a life of +-- another region, unpublished or refused, or an unknown commit is refused. +CREATE FUNCTION evolution.end_cellular_lives(p_world evolution.u64, p_region evolution.u64, + p_holder uuid, p_epoch evolution.u64, p_commit uuid, p_identities bytea) +RETURNS integer LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ +DECLARE + n integer; + ended integer := 0; + ending_identity numeric; + r evolution.life_reservations%ROWTYPE; +BEGIN + IF p_commit IS NULL OR p_identities IS NULL OR octet_length(p_identities) % 8 <> 0 + OR octet_length(p_identities) / 8 > 256 THEN + RAISE EXCEPTION 'invalid life endings' USING ERRCODE = '22023'; + END IF; + PERFORM evolution.check_region_lease(p_world, p_region, p_holder, p_epoch); + IF NOT EXISTS (SELECT 1 FROM evolution.region_commits x WHERE x.world_id = p_world + AND x.region_id = p_region AND x.commit_id = p_commit) THEN + RAISE EXCEPTION 'life ending commit not in region' USING ERRCODE = '22023'; + END IF; + n := octet_length(p_identities) / 8; + FOR i IN 0 .. n - 1 LOOP + ending_identity := evolution.manifest_uint(p_identities, 8 * i, 8); + SELECT * INTO r FROM evolution.life_reservations x + WHERE x.world_id = p_world AND x.identity = ending_identity FOR UPDATE; + IF NOT FOUND THEN + CONTINUE; -- An opening founder was never reserved; nothing blocks its account. + END IF; + IF r.region_id <> p_region OR r.rejection <> 0 THEN + RAISE EXCEPTION 'life ending not owned' USING ERRCODE = 'EVL04'; + END IF; + IF r.ended_commit IS NULL THEN + UPDATE evolution.life_reservations SET ended_commit = p_commit + WHERE world_id = p_world AND account_id = r.account_id AND request_id = r.request_id; + ended := ended + 1; + END IF; + END LOOP; + RETURN ended; +END $$; + +REVOKE ALL ON FUNCTION evolution.life_ending_is_final(), + evolution.end_cellular_lives(evolution.u64, evolution.u64, uuid, evolution.u64, uuid, bytea) + FROM PUBLIC; +GRANT EXECUTE ON FUNCTION + evolution.end_cellular_lives(evolution.u64, evolution.u64, uuid, evolution.u64, uuid, bytea) + TO :"runtime_role"; +INSERT INTO evolution.schema_migrations(version, sha256) VALUES (37, :'migration_sha_37'); diff --git a/migrations/apply.sql b/migrations/apply.sql index 5be281e..5446607 100644 --- a/migrations/apply.sql +++ b/migrations/apply.sql @@ -112,6 +112,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') SELECT :'history_v33' || ',34:' || :'migration_sha_34' AS history_v34 \gset SELECT :'history_v34' || ',35:' || :'migration_sha_35' AS history_v35 \gset SELECT :'history_v35' || ',36:' || :'migration_sha_36' AS history_v36 \gset + SELECT :'history_v36' || ',37:' || :'migration_sha_37' AS history_v37 \gset SELECT :'history' = :'history_v22' AS at_v22, :'history' = :'history_v23' AS at_v23, :'history' = :'history_v24' AS at_v24, @@ -126,8 +127,10 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') :'history' = :'history_v33' AS at_v33, :'history' = :'history_v34' AS at_v34, :'history' = :'history_v35' AS at_v35, - :'history' = :'history_v36' AS at_v36 \gset - \if :at_v36 + :'history' = :'history_v36' AS at_v36, + :'history' = :'history_v37' AS at_v37 \gset + \if :at_v37 + \elif :at_v36 \elif :at_v35 \elif :at_v34 \elif :at_v33 @@ -493,7 +496,8 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24, coalesce(max(version), 0) = 33 AS at_v33, coalesce(max(version), 0) = 34 AS at_v34, coalesce(max(version), 0) = 35 AS at_v35, - coalesce(max(version), 0) = 36 AS at_v36 + coalesce(max(version), 0) = 36 AS at_v36, + coalesce(max(version), 0) = 37 AS at_v37 FROM evolution.schema_migrations \gset \if :at_v24 \ir 0025_cellular_v2_bundle.sql @@ -508,6 +512,7 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24, \ir 0034_cellular_v4_engulf_bundle.sql \ir 0035_cellular_mutation_publication.sql \ir 0036_cellular_v5_budding_bundle.sql + \ir 0037_cellular_life_endings.sql \elif :at_v25 \ir 0026_cellular_profile_migration.sql \ir 0027_cellular_v3_migration.sql @@ -520,6 +525,7 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24, \ir 0034_cellular_v4_engulf_bundle.sql \ir 0035_cellular_mutation_publication.sql \ir 0036_cellular_v5_budding_bundle.sql + \ir 0037_cellular_life_endings.sql \elif :at_v26 \ir 0027_cellular_v3_migration.sql \ir 0028_cellular_v3_trace_bounds.sql @@ -531,6 +537,7 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24, \ir 0034_cellular_v4_engulf_bundle.sql \ir 0035_cellular_mutation_publication.sql \ir 0036_cellular_v5_budding_bundle.sql + \ir 0037_cellular_life_endings.sql \elif :at_v27 \ir 0028_cellular_v3_trace_bounds.sql \ir 0029_cellular_mutation_reservations.sql @@ -541,6 +548,7 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24, \ir 0034_cellular_v4_engulf_bundle.sql \ir 0035_cellular_mutation_publication.sql \ir 0036_cellular_v5_budding_bundle.sql + \ir 0037_cellular_life_endings.sql \elif :at_v28 \ir 0029_cellular_mutation_reservations.sql \ir 0030_cellular_birth_publication_binding.sql @@ -550,6 +558,7 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24, \ir 0034_cellular_v4_engulf_bundle.sql \ir 0035_cellular_mutation_publication.sql \ir 0036_cellular_v5_budding_bundle.sql + \ir 0037_cellular_life_endings.sql \elif :at_v29 \ir 0030_cellular_birth_publication_binding.sql \ir 0031_fix_cellular_birth_publication_record.sql @@ -558,6 +567,7 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24, \ir 0034_cellular_v4_engulf_bundle.sql \ir 0035_cellular_mutation_publication.sql \ir 0036_cellular_v5_budding_bundle.sql + \ir 0037_cellular_life_endings.sql \elif :at_v30 \ir 0031_fix_cellular_birth_publication_record.sql \ir 0032_fix_cellular_v3_manifest_normalization.sql @@ -565,29 +575,37 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24, \ir 0034_cellular_v4_engulf_bundle.sql \ir 0035_cellular_mutation_publication.sql \ir 0036_cellular_v5_budding_bundle.sql + \ir 0037_cellular_life_endings.sql \elif :at_v31 \ir 0032_fix_cellular_v3_manifest_normalization.sql \ir 0033_fix_cellular_action_tick_offset.sql \ir 0034_cellular_v4_engulf_bundle.sql \ir 0035_cellular_mutation_publication.sql \ir 0036_cellular_v5_budding_bundle.sql + \ir 0037_cellular_life_endings.sql \elif :at_v32 \ir 0033_fix_cellular_action_tick_offset.sql \ir 0034_cellular_v4_engulf_bundle.sql \ir 0035_cellular_mutation_publication.sql \ir 0036_cellular_v5_budding_bundle.sql + \ir 0037_cellular_life_endings.sql \elif :at_v33 \ir 0034_cellular_v4_engulf_bundle.sql \ir 0035_cellular_mutation_publication.sql \ir 0036_cellular_v5_budding_bundle.sql + \ir 0037_cellular_life_endings.sql \elif :at_v34 \ir 0035_cellular_mutation_publication.sql \ir 0036_cellular_v5_budding_bundle.sql + \ir 0037_cellular_life_endings.sql \elif :at_v35 \ir 0036_cellular_v5_budding_bundle.sql + \ir 0037_cellular_life_endings.sql \elif :at_v36 + \ir 0037_cellular_life_endings.sql +\elif :at_v37 \else - \echo 'Unknown or changed migration history after v36' + \echo 'Unknown or changed migration history after v37' DO $$ BEGIN RAISE EXCEPTION 'unknown or changed migration history'; END $$; \endif -- Effective-privilege audit on every run, including no-ops (ADR-0060). Attributes @@ -641,6 +659,7 @@ BEGIN ('evolution.publish_cellular_mutation_checkpoint(bytea,bytea,bytea,bytea,bytea,bytea)'::regprocedure::oid), ('evolution.resolve_cellular_mutation_commit(evolution.u64,evolution.u64,uuid,bytea,bytea,bytea,bytea,bytea,bytea)'::regprocedure::oid), ('evolution.load_cellular_mutation_publications(evolution.u64,evolution.u64,uuid,evolution.u64,uuid)'::regprocedure::oid), + ('evolution.end_cellular_lives(evolution.u64,evolution.u64,uuid,evolution.u64,uuid,bytea)'::regprocedure::oid), ('evolution.load_cellular_publication_actions(evolution.u64,evolution.u64,uuid,evolution.u64,uuid)'::regprocedure::oid), ('evolution.load_cellular_publication_births(evolution.u64,evolution.u64,uuid,evolution.u64,uuid)'::regprocedure::oid), ('evolution.publish_cellular_profile_migration(bytea)'::regprocedure::oid), diff --git a/src/persistence/region_lease_store.cpp b/src/persistence/region_lease_store.cpp index 709b68c..e5ea964 100644 --- a/src/persistence/region_lease_store.cpp +++ b/src/persistence/region_lease_store.cpp @@ -1376,6 +1376,33 @@ std::expected reserve_cellu target_tick, *identity, input}; } +std::expected +end_cellular_lives(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& commit, + std::span identities, milliseconds timeout) { + if (commit == Uuid{} || identities.empty() || identities.size() > max_life_endings_per_call || + std::ranges::find(identities, std::uint64_t{0}) != identities.end()) + return std::unexpected{LifeReservationFailure{LifeReservationError::invalid, {}}}; + std::vector packed; + packed.reserve(identities.size() * 8); + for (const auto identity : identities) + for (unsigned i = 0; i < 8; ++i) + packed.push_back(static_cast(identity >> (i * 8U))); + const std::array p{std::to_string(key.world), std::to_string(key.region), + uuid_text(lease.holder), std::to_string(lease.epoch), + uuid_text(commit), "\\x" + hex(packed)}; + auto rows = db.query("SELECT evolution.end_cellular_lives($1::evolution.u64, " + "$2::evolution.u64, $3::uuid, $4::evolution.u64, $5::uuid, $6::bytea)", + p, timeout); + if (!rows) + return std::unexpected{life_failure(rows.error())}; + if (rows->size() != 1 || (*rows)[0].size() != 1) + return std::unexpected{LifeReservationFailure{LifeReservationError::unknown, {}}}; + const auto ended = parse_integer((*rows)[0][0]); + if (!ended || *ended > identities.size()) + return std::unexpected{LifeReservationFailure{LifeReservationError::unknown, {}}}; + return *ended; +} + std::expected, LifeReservationFailure> load_pending_cellular_mutations(Postgres& db, RegionKey key, const LeaseGrant& lease, milliseconds timeout) { diff --git a/src/server/cellular_region_host.cpp b/src/server/cellular_region_host.cpp index f28a585..11f0088 100644 --- a/src/server/cellular_region_host.cpp +++ b/src/server/cellular_region_host.cpp @@ -184,6 +184,11 @@ void CellularRegionHost::acquire() { committed_ = std::move(recovered->committed); queued_mutations_.clear(); mutations_uncertain_ = true; // Reserved choices are reloaded under the new lease. + // ADR-0127: every founder corpse is an ended life; record any ending a crash lost. + pending_endings_.clear(); + for (const auto& corpse : state_->corpses()) + if (corpse.parent_life == 0) + pending_endings_.push_back({committed_->head.commit, corpse.life_id}); status_.phase = HostPhase::running; status_.simulated_tick = committed_->head.end_tick; status_.committed_tick = committed_->head.end_tick; @@ -301,6 +306,7 @@ void CellularRegionHost::resolve() { } return; } + note_endings(*state_, *pending_->candidate, retried->head.commit); state_ = std::move(pending_->candidate); committed_ = std::move(*retried); for (const auto& action : pending_->actions) @@ -338,10 +344,50 @@ const HostStatus& CellularRegionHost::step() { acquire(); if (!pending_ && state_ && mutations_uncertain_ && status_.phase == HostPhase::running) reload_mutations(); + if (!pending_ && state_ && status_.phase == HostPhase::running) + flush_endings(); refresh(); return status_; } +void CellularRegionHost::note_endings(const CellularLives& before, const CellularLives& after, + const persistence::Uuid& commit) { + for (const auto& life : before.lives()) + if (life.account != LifeAccount{} && + std::ranges::find(after.lives(), life.identity.life, [](const CellularOwnedLife& next) { + return next.identity.life; + }) == after.lives().end()) + pending_endings_.push_back({commit, life.identity.life}); +} + +void CellularRegionHost::flush_endings() { + while (!pending_endings_.empty()) { + const auto grant = authority_.grant(); + if (!grant || !authority_.may_begin_commit(LeaseClock::now()) || !database()) + return; + // One call per commit, at most the SQL bound of identities. + const auto commit = pending_endings_.front().commit; + std::vector lives; + for (const auto& ending : pending_endings_) + if (ending.commit == commit && lives.size() < persistence::max_life_endings_per_call) + lives.push_back(ending.life); + const auto recorded = persistence::end_cellular_lives(*db_, config_.key, *grant, commit, + lives, config_.lease.commit_timeout); + if (!recorded) { + if (recorded.error().kind == persistence::LifeReservationError::fenced) { + authority_.fence(); + status_.ready = false; + } + db_.reset(); + ++status_.store_failures; + return; // Retried on a later step; a new life waits for it. + } + std::erase_if(pending_endings_, [&](const Ending& ending) { + return ending.commit == commit && std::ranges::find(lives, ending.life) != lives.end(); + }); + } +} + void CellularRegionHost::reload_mutations() { const auto grant = authority_.grant(); if (!grant || !committed_ || !database()) @@ -515,8 +561,10 @@ CellularRegionHost::publish(const CellularBatch& batch, CellularPublishFailure{CellularHostError::publication, published.error().kind, published.error().store, published.error().object}}; } + note_endings(*state_, **candidate, published->committed.head.commit); state_ = std::move(*candidate); committed_ = published->committed; + flush_endings(); for (const auto& action : actions) finalized_actions_.push_back({action, committed_->head.sequence}); const auto births = births_in(batch); diff --git a/tests/cellular_publication_tests.cpp b/tests/cellular_publication_tests.cpp index d06fa94..f98d05b 100644 --- a/tests/cellular_publication_tests.cpp +++ b/tests/cellular_publication_tests.cpp @@ -112,8 +112,9 @@ void cellular_birth_publication_is_durable() { {}}, 5s); }; - CellularHostConfig config{key, 1, {}, build, {build}}; - auto made = CellularRegionHost::create(config, connect, std::move(*objects)); + // A fresh value per host: copying the config trips a GCC 14 -O3 array-bounds false positive. + const auto config = [&] { return CellularHostConfig{key, 1, {}, build, {build}}; }; + auto made = CellularRegionHost::create(config(), connect, std::move(*objects)); CHECK(made); auto host = std::move(*made); CHECK(host->step().ready); @@ -184,7 +185,7 @@ void cellular_birth_publication_is_durable() { FilesystemObjectStore::open(env("PGHOST"), key, max_cellular_lives_bytes); CHECK(restarted_objects); auto restarted_made = - CellularRegionHost::create(config, connect, std::move(*restarted_objects)); + CellularRegionHost::create(config(), connect, std::move(*restarted_objects)); CHECK(restarted_made); auto restarted = std::move(*restarted_made); CHECK(restarted->step().ready && restarted->state()->tick() == 1 && @@ -245,11 +246,12 @@ void cellular_profile_migration_is_durable_and_recovers_without_a_tick() { {}}, 5s); }; - CellularHostConfig config{key, 1, {}, build, {build}}; + // A fresh value per host: copying the config trips a GCC 14 -O3 array-bounds false positive. + const auto config = [&] { return CellularHostConfig{key, 1, {}, build, {build}}; }; auto migration_objects = FilesystemObjectStore::open(env("PGHOST"), key, max_cellular_lives_bytes); CHECK(migration_objects); - auto made = CellularRegionHost::create(config, connect, std::move(*objects)); + auto made = CellularRegionHost::create(config(), connect, std::move(*objects)); CHECK(made); auto host = std::move(*made); CHECK(host->step().ready); @@ -306,7 +308,8 @@ void cellular_profile_migration_is_durable_and_recovers_without_a_tick() { const auto target_payload = (*recovery_objects)->get({target_manifest->payload_sha256, target_manifest->payload_bytes}); CHECK(target_payload); - auto restarted_made = CellularRegionHost::create(config, connect, std::move(*recovery_objects)); + auto restarted_made = + CellularRegionHost::create(config(), connect, std::move(*recovery_objects)); CHECK(restarted_made); auto restarted = std::move(*restarted_made); CHECK(restarted->step().ready); @@ -369,8 +372,9 @@ void cellular_mutation_publication_binds_results() { {}}, 5s); }; - CellularHostConfig config{key, 1, {}, build, {build}}; - auto made = CellularRegionHost::create(config, connect, std::move(*objects)); + // A fresh value per host: copying the config trips a GCC 14 -O3 array-bounds false positive. + const auto config = [&] { return CellularHostConfig{key, 1, {}, build, {build}}; }; + auto made = CellularRegionHost::create(config(), connect, std::move(*objects)); CHECK(made); auto host = std::move(*made); CHECK(host->step().ready); @@ -467,7 +471,7 @@ void cellular_mutation_publication_binds_results() { FilesystemObjectStore::open(env("PGHOST"), key, max_cellular_lives_bytes); CHECK(restarted_objects); auto restarted_made = - CellularRegionHost::create(config, connect, std::move(*restarted_objects)); + CellularRegionHost::create(config(), connect, std::move(*restarted_objects)); CHECK(restarted_made); auto restarted = std::move(*restarted_made); CHECK(restarted->step().ready && restarted->state()->snapshot() == (*candidate)->snapshot()); @@ -522,8 +526,9 @@ void cellular_host_admits_mutation_choices() { {}}, 5s); }; - CellularHostConfig config{key, 1, {}, build, {build}}; - auto made = CellularRegionHost::create(config, connect, std::move(*objects)); + // A fresh value per host: copying the config trips a GCC 14 -O3 array-bounds false positive. + const auto config = [&] { return CellularHostConfig{key, 1, {}, build, {build}}; }; + auto made = CellularRegionHost::create(config(), connect, std::move(*objects)); CHECK(made); auto host = std::move(*made); CHECK(host->step().ready); @@ -595,7 +600,7 @@ void cellular_host_admits_mutation_choices() { host.reset(); auto reopened = FilesystemObjectStore::open(env("PGHOST"), key, max_cellular_lives_bytes); CHECK(reopened); - auto restarted_made = CellularRegionHost::create(config, connect, std::move(*reopened)); + auto restarted_made = CellularRegionHost::create(config(), connect, std::move(*reopened)); CHECK(restarted_made); host = std::move(*restarted_made); CHECK(host->step().ready && host->state()->tick() + 1 == next); @@ -684,8 +689,9 @@ void cellular_bud_publishes_and_recovers() { {}}, 5s); }; - CellularHostConfig config{key, 1, {}, build, {build}}; - auto made = CellularRegionHost::create(config, connect, std::move(*objects)); + // A fresh value per host: copying the config trips a GCC 14 -O3 array-bounds false positive. + const auto config = [&] { return CellularHostConfig{key, 1, {}, build, {build}}; }; + auto made = CellularRegionHost::create(config(), connect, std::move(*objects)); CHECK(made); auto host = std::move(*made); CHECK(host->step().ready); @@ -737,7 +743,130 @@ void cellular_bud_publishes_and_recovers() { auto reopened = FilesystemObjectStore::open(env("PGHOST"), key, max_cellular_lives_bytes); CHECK(reopened); - auto restarted = CellularRegionHost::create(config, connect, std::move(*reopened)); + auto restarted = CellularRegionHost::create(config(), connect, std::move(*reopened)); + CHECK(restarted); + CHECK((*restarted)->step().ready && (*restarted)->state()->snapshot() == snapshot); + (*restarted)->drain(); +} + +// ADR-0127 (schema v37): a dead life stays dead, its corpse stays, and its account starts +// a new primitive life with a new identity; the first life's history is unchanged. +void cellular_death_is_final_and_account_restarts() { + using namespace evo::biology; + constexpr RegionKey key{9768, 1}; + auto owner = connect_as(env("PGUSER")); + auto runtime = connect_as(env("EVO_PG_RUNTIME_USER")); + const auto build = filled<32>(0xDC); + CHECK(owner->query("INSERT INTO evolution.retained_builds(build_identity) VALUES " + "(decode(repeat('dc',32),'hex')) ON CONFLICT DO NOTHING", + {}, 5s)); + auto endowed = primitive_endowment(); + CHECK(endowed); + auto funding = endowed->body; + funding.energy.value = endowed->reserve.energy.value + endowed->construction.value; + funding.energy.value *= 2; + for (auto& channel : funding.matter) + channel.value *= 2; + auto primitive = + PrimitiveLives::create({key.world, key.region, 1, 17, funding, {{0, 0}, {2'000, 0}}}); + CHECK(primitive); + auto habitat = CellularHabitat::create({key.world, key.region, 1, 3'000, 2'000, {}}); + CHECK(habitat); + auto opening = + CellularLives::create(**primitive, std::move(*habitat), {{3'000, 2'000}, 0, 293'150}, + CellularLivesProfile::engulf); + CHECK(opening); + auto objects = FilesystemObjectStore::open(env("PGHOST"), key, max_cellular_lives_bytes); + CHECK(objects); + CHECK(provision_cellular_region(*owner, **objects, key, 1, **opening, distinct_commit(0x9E), + build, 5s)); + CHECK(owner->query("INSERT INTO evolution.life_allocators VALUES(9768,1000) " + "ON CONFLICT DO NOTHING", + {}, 5s)); + const auto connect = [&]() -> std::expected, PgFailure> { + return Postgres::connect({env("PGHOST"), + {}, + env("PGPORT"), + env("PGDATABASE"), + env("EVO_PG_RUNTIME_USER"), + {}, + {}}, + 5s); + }; + // A fresh value per host: copying the config trips a GCC 14 -O3 array-bounds false positive. + const auto config = [&] { return CellularHostConfig{key, 1, {}, build, {build}}; }; + auto made = CellularRegionHost::create(config(), connect, std::move(*objects)); + CHECK(made); + auto host = std::move(*made); + CHECK(host->step().ready); + const auto account = filled<16>(0x94); + const auto control = claim_account_control(*runtime, key, checked_value(host->authority()), + account, 1, 1, 60'000ms, 5s); + CHECK(control); + const auto header = [&](std::uint64_t ticks) { + const auto& committed = checked_value(host->committed()); + const auto digest = sha256(committed.manifest); + CHECK(digest); + return CellularBatchHeader{key.world, + key.region, + 1, + committed.head.commit, + checked_value(digest), + host->state()->tick(), + host->state()->tick() + ticks}; + }; + const auto born = [&](std::uint64_t request) { + CHECK(host->create_primitive_life(account, control->generation, request).result == + evo::network::PrimitiveLifeResult::pending); + CellularBatch birth; + birth.header = header(1); + std::vector idle; + for (const auto& life : host->state()->lives()) + idle.push_back({life.identity.life, {}}); + birth.ticks.push_back({idle, {host->queued_births().front()}, {}}); + CHECK(host->publish(birth)); + const auto outcome = host->query_primitive_life(account, control->generation, request); + CHECK(outcome.result == evo::network::PrimitiveLifeResult::born); + return outcome.life_identity; + }; + const auto first = born(11); + // Unfed in an empty habitat, the founder starves. A batch that runs past the death + // names a dead life and is refused by replay, so near the end publish single ticks. + for (int round = 0; round < 4'000 && !host->state()->lives().empty(); ++round) { + CHECK(host->step().ready); + const auto idle = [&](std::uint64_t ticks) { + CellularBatch batch; + batch.header = header(ticks); + for (std::uint64_t tick = 0; tick < ticks; ++tick) + batch.ticks.push_back({{{first, {}}}, {}, {}}); + return batch; + }; + if (!host->publish(idle(32))) + CHECK(host->publish(idle(1))); + } + CHECK(host->state()->lives().empty() && host->state()->corpses().size() == 1 && + host->state()->corpses().front().life_id == first); + CHECK(host->step().ready); // Records any ending a failed flush left behind. + const auto owner_region = + primitive_life_owner(*runtime, key, checked_value(host->authority()), account, 5s); + CHECK(owner_region && !*owner_region); // No active life anywhere. + + // The same account starts a new primitive life; the first one is history. + const auto second = born(12); + CHECK(second != first && host->state()->lives().size() == 1 && + host->state()->lives().front().identity.life == second && + host->state()->corpses().size() == 1); + CHECK(host->query_primitive_life(account, control->generation, 11).result == + evo::network::PrimitiveLifeResult::born); + // While the second life lives, a third request is an existing-life refusal. + CHECK(host->create_primitive_life(account, control->generation, 13).result == + evo::network::PrimitiveLifeResult::existing_life); + const auto snapshot = host->state()->snapshot(); + host->drain(); + host.reset(); + auto reopened = FilesystemObjectStore::open(env("PGHOST"), key, max_cellular_lives_bytes); + CHECK(reopened); + auto restarted = CellularRegionHost::create(config(), connect, std::move(*reopened)); CHECK(restarted); CHECK((*restarted)->step().ready && (*restarted)->state()->snapshot() == snapshot); (*restarted)->drain(); @@ -750,6 +879,7 @@ void run_cellular_publication_tests() { cellular_mutation_publication_binds_results(); cellular_host_admits_mutation_choices(); cellular_bud_publishes_and_recovers(); + cellular_death_is_final_and_account_restarts(); auto owner = connect_as(env("PGUSER")); const auto build = filled<32>(0xD9); CHECK(owner->query("INSERT INTO evolution.retained_builds(build_identity) VALUES " @@ -836,8 +966,9 @@ void run_cellular_publication_tests() { {}}, 5s); }; - CellularHostConfig config{key, 1, {}, build, {build}}; - auto made_host = CellularRegionHost::create(config, connect, std::move(*objects)); + // A fresh value per host: copying the config trips a GCC 14 -O3 array-bounds false positive. + const auto config = [&] { return CellularHostConfig{key, 1, {}, build, {build}}; }; + auto made_host = CellularRegionHost::create(config(), connect, std::move(*objects)); CHECK(made_host); auto host = std::move(*made_host); CHECK(host->step().ready); @@ -903,7 +1034,7 @@ void run_cellular_publication_tests() { FilesystemObjectStore::open(env("PGHOST"), key, max_cellular_lives_bytes); CHECK(restarted_objects); auto made_restarted = - CellularRegionHost::create(config, connect, std::move(*restarted_objects)); + CellularRegionHost::create(config(), connect, std::move(*restarted_objects)); CHECK(made_restarted); auto restarted = std::move(*made_restarted); CHECK(restarted->step().ready); diff --git a/tests/region_migration_tests.cpp b/tests/region_migration_tests.cpp index 496b2bb..1fdbc71 100644 --- a/tests/region_migration_tests.cpp +++ b/tests/region_migration_tests.cpp @@ -212,7 +212,7 @@ void upgrade_across_migration_and_barrier() { " EVO_PG_RUNTIME_ROLE=evo_runtime_test bash \"$EVO_SOURCE_ROOT/" "tools/migrate-postgres.sh\" > /dev/null") == 0); CHECK(history(*owner_db) == "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24," - "25,26,27,28,29,30,31,32,33,34,35,36"); + "25,26,27,28,29,30,31,32,33,34,35,36,37"); CHECK(scalar(*owner_db, "SELECT (c.condeferrable AND c.condeferred AND " "c.confrelid = 'evolution.region_commits'::regclass)::text " "FROM pg_catalog.pg_constraint c WHERE c.conrelid = " diff --git a/tools/ci/postgres-check.sh b/tools/ci/postgres-check.sh index 7f224b7..74a5034 100644 --- a/tools/ci/postgres-check.sh +++ b/tools/ci/postgres-check.sh @@ -122,7 +122,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,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36 +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,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37 # 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%% *}" @@ -143,7 +143,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")" = 36:1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36 +test "$(PGDATABASE=v3_commits psql -XAtc "SELECT count(*) || ':' || string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 37:1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37 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. @@ -197,7 +197,7 @@ 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,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36 +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,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37 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 phase permission-audit @@ -250,9 +250,9 @@ expect_audit_failure 'REVOKE EXECUTE ON FUNCTION evolution.claim_account_control bash "$root/tools/migrate-postgres.sh" # Restored configuration passes the audit again. phase checksum-drift # 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=37 WHERE version=36' +psql -X -v ON_ERROR_STOP=1 -c 'UPDATE evolution.schema_migrations SET version=38 WHERE version=37' 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=36 WHERE version=37" +psql -X -v ON_ERROR_STOP=1 -c "UPDATE evolution.schema_migrations SET version=37 WHERE version=38" psql -X -v ON_ERROR_STOP=1 -c "UPDATE evolution.schema_migrations SET sha256=repeat('0',64) WHERE version=14" 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=13" diff --git a/tools/migrate-postgres.sh b/tools/migrate-postgres.sh index 5d82383..e14ceea 100644 --- a/tools/migrate-postgres.sh +++ b/tools/migrate-postgres.sh @@ -84,6 +84,8 @@ digest_35="$(sha256sum "$root/migrations/0035_cellular_mutation_publication.sql" digest_35="${digest_35%% *}" digest_36="$(sha256sum "$root/migrations/0036_cellular_v5_budding_bundle.sql")" digest_36="${digest_36%% *}" +digest_37="$(sha256sum "$root/migrations/0037_cellular_life_endings.sql")" +digest_37="${digest_37%% *}" 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" \ @@ -113,5 +115,6 @@ psql -X --no-password --set=ON_ERROR_STOP=1 --set="migration_sha=$digest" \ --set="migration_sha_34=$digest_34" \ --set="migration_sha_35=$digest_35" \ --set="migration_sha_36=$digest_36" \ + --set="migration_sha_37=$digest_37" \ --set="runtime_role=$runtime_role" \ --file="$root/migrations/apply.sql"