#!/usr/bin/env bash # Private postgres-check cluster only. No deployed database or biological rules. set -euo pipefail root="$(cd "$(dirname "$0")/../.." && pwd)" createdb evo_history_domain export PGDATABASE=evo_history_domain bash "$root/tools/migrate-postgres.sh" > /dev/null sql() { psql -XAt --no-password -v ON_ERROR_STOP=1 "$@"; } fail() { echo "history-domain qualification FAILED: $*" >&2; exit 1; } original="$(sql -c 'SELECT evolution.load_history_domain_v1()')" [ -n "$original" ] || fail 'missing adoption domain' bash "$root/tools/migrate-postgres.sh" > /dev/null [ "$(sql -c 'SELECT evolution.load_history_domain_v1()')" = "$original" ] || fail 'no-op changed domain' work="$(mktemp -d /tmp/evo-history-domain.XXXXXXXX)" objects="$work/objects" mkdir -m 700 "$objects" trap 'rm -rf -- "$work"' EXIT "${EVO_WORLD_CLONE_BIN:?private fixture tool}" provision --world 70201 --region 1 \ --opening development-v1 --pg-host "$PGHOST" --pg-port "$PGPORT" \ --pg-database "$PGDATABASE" --pg-user "$PGUSER" --objects "$objects" \ --build "$(printf 'cd%.0s' {1..32})" --retain-build true > /dev/null request=51515151-5151-5151-5151-515151515151 if sql -c "SELECT evolution.fork_restored_history_v1('$request')" > /dev/null 2>&1; then fail 'fork accepted a current build' fi [ "$(sql -c 'SELECT evolution.load_history_domain_v1()')" = "$original" ] || fail 'refusal mutated domain' sql -c 'UPDATE evolution.retained_builds SET retired_at=clock_timestamp()' > /dev/null if PGUSER="$EVO_PG_RUNTIME_USER" sql -c "SELECT evolution.fork_restored_history_v1('$request')" > /dev/null 2>&1; then fail 'runtime can fork history' fi if PGUSER="$EVO_PG_RUNTIME_USER" sql -c 'SELECT * FROM evolution.current_history_domain' > /dev/null 2>&1; then fail 'runtime can access context tables' fi sql -c "UPDATE evolution.region_leases SET epoch=1, holder=pg_catalog.gen_random_uuid(), expires_at=clock_timestamp()+interval '1 hour'" > /dev/null if sql -c "SELECT evolution.fork_restored_history_v1('$request')" > /dev/null 2>&1; then fail 'fork accepted a held lease' fi sql -c 'UPDATE evolution.region_leases SET holder=NULL, expires_at=NULL' > /dev/null # An aborted transaction leaves both the namespace and provenance unchanged. sql -c "BEGIN; SELECT evolution.fork_restored_history_v1('$request'); ROLLBACK" > /dev/null [ "$(sql -c 'SELECT count(*) FROM evolution.history_domains')" = 1 ] && [ "$(sql -c 'SELECT count(*) FROM evolution.history_domain_cuts')" = 0 ] || fail 'rollback retained fork evidence' fork="$(sql -c "SELECT evolution.fork_restored_history_v1('$request')")" [ "$fork" != "$original" ] && [ -n "$fork" ] || fail 'fork reused parent domain' [ "$(sql -c "SELECT evolution.fork_restored_history_v1('$request')")" = "$fork" ] && [ "$(sql -c 'SELECT count(*) FROM evolution.history_domains')" = 2 ] || fail 'retry duplicated fork' [ "$(PGUSER="$EVO_PG_RUNTIME_USER" sql -c 'SELECT evolution.load_history_domain_v1()')" = "$fork" ] || fail 'runtime context read' [ "$(sql -c "SELECT parent::text||':'||kind FROM evolution.history_domains WHERE domain='$fork'")" = "$original:restore" ] || fail 'parent provenance' [ "$(sql -c "SELECT count(*) FROM evolution.history_domain_cuts d JOIN evolution.region_heads h USING(world_id,region_id) JOIN evolution.region_leases l USING(world_id,region_id) WHERE d.domain='$fork' AND d.frame_id=l.frame_id AND d.commit_sequence=h.commit_sequence AND d.commit_id=h.commit_id AND d.digest=h.digest AND d.end_tick=h.end_tick")" = 1 ] || fail 'exact fork cuts' sql -c "SELECT evolution.fork_restored_history_v1('52525252-5252-5252-5252-525252525252')" > /dev/null if sql -c "SELECT evolution.fork_restored_history_v1('$request')" > /dev/null 2>&1; then fail 'stale request switched context backwards' fi for invalid in NULL "'00000000-0000-0000-0000-000000000000'"; do if sql -c "SELECT evolution.fork_restored_history_v1($invalid)" > /dev/null 2>&1; then fail 'invalid request accepted' fi done # Reconstruct the exact pre-context metadata schema in this disposable database. # Restore the resulting real legacy backup through both supported workflows. sql > /dev/null <<'SQL' DROP FUNCTION evolution.fork_restored_history_v1(uuid); DROP FUNCTION evolution.load_history_domain_v1(); DROP TABLE evolution.history_domain_cuts, evolution.current_history_domain, evolution.history_domains; DELETE FROM evolution.schema_migrations WHERE version=51; UPDATE evolution.retained_builds SET retired_at=NULL; SQL bash "$root/tools/backup/backup-world.sh" --objects "$objects" --out "$work/legacy" --label legacy-context > /dev/null createdb evo_history_legacy_isolated PGDATABASE=evo_history_legacy_isolated bash "$root/tools/backup/restore-world.sh" \ --bundle "$work/legacy" --objects "$work/isolated" > "$work/isolated.log" 2>&1 || { cat "$work/isolated.log" >&2; fail 'legacy isolated restore'; } [ "$(PGDATABASE=evo_history_legacy_isolated sql -c "SELECT to_regprocedure('evolution.load_history_domain_v1()') IS NULL")" = t ] || fail 'invented legacy source context' createdb evo_history_legacy_inplace PGDATABASE=evo_history_legacy_inplace bash "$root/tools/backup/restore-world.sh" \ --bundle "$work/legacy" --objects "$work/inplace" --in-place \ --runtime-role "$EVO_PG_RUNTIME_ROLE" --accept-rewind > "$work/inplace.log" 2>&1 || { cat "$work/inplace.log" >&2; fail 'legacy in-place restore'; } [ "$(PGDATABASE=evo_history_legacy_inplace sql -c "SELECT count(*) FROM evolution.history_domains WHERE kind='adoption' AND parent IS NULL AND request IS NULL")" = 1 ] && [ "$(PGDATABASE=evo_history_legacy_inplace sql -c 'SELECT count(*) FROM evolution.history_domain_cuts')" = 0 ] || fail 'invented legacy branch provenance' echo 'history-domain adoption, quarantine, rollback, exact cuts, retries, privileges and legacy restores passed'