Files
aevolution/migrations/apply.sql
T
dingoandClaude Opus 5.5 30411a83f9 Complete M7.7 recovery verification before readiness
ADR-0065: before any readiness, recovery now verifies the head's tail.
The trace object must exist with its manifest digest, its header must
bind profile, region, predecessor and range; the predecessor (schema v6
load_region_commit) must hash to the digest the head names; replaying
the trace from the predecessor's verified payload must reproduce every
report and the exact head bytes; and trace-derived decisions must equal
the head's durable decisions. Missing, corrupt or non-reproducing tails
quarantine (RecoveryError::tail); nothing older or empty is served.

Test-only crash seams (HostConfig::crash_hook, PublishHooks) and
evo-server --crash-at deliver real SIGKILL at before-upload,
before-transaction, after-commit and after-ack. The kill matrix,
a SIGSTOP/SIGCONT stale owner and damaged-head startups all behave as
the durability contract requires; successors verify before readiness.

Validated: PostgreSQL suites under GCC Release, clang-tidy and
ASan/UBSan; tail-skip and replay-equality mutations each caught. No
Docker/Gitea CI, production-lease kill test or deployment.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 22:31:40 -04:00

139 lines
6.7 KiB
PL/PgSQL

\set ON_ERROR_STOP on
BEGIN;
SET LOCAL search_path = pg_catalog;
SET LOCAL statement_timeout = '10s';
SET LOCAL lock_timeout = '5s';
SELECT pg_catalog.pg_advisory_xact_lock(71420001);
SELECT current_setting('server_version_num')::int = 180006 AS supported \gset
\if :supported
\else
\echo 'Unsupported PostgreSQL version; expected 18.6'
DO $$ BEGIN RAISE EXCEPTION 'unsupported PostgreSQL version'; END $$;
\endif
SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') AS exists \gset
\if :exists
-- Only an exact prefix of the checked-in history is admitted. Unknown or
-- partial/foreign schemas fail closed; no IF NOT EXISTS repair of foreign data.
SELECT coalesce(string_agg(version || ':' || sha256, ',' ORDER BY version), '')
AS history FROM evolution.schema_migrations \gset
SELECT :'history' = '1:' || :'migration_sha' AS at_v1,
:'history' = '1:' || :'migration_sha' || ',2:' || :'migration_sha_2' AS at_v2,
:'history' = '1:' || :'migration_sha' || ',2:' || :'migration_sha_2'
|| ',3:' || :'migration_sha_3' AS at_v3,
:'history' = '1:' || :'migration_sha' || ',2:' || :'migration_sha_2'
|| ',3:' || :'migration_sha_3' || ',4:' || :'migration_sha_4' AS at_v4,
:'history' = '1:' || :'migration_sha' || ',2:' || :'migration_sha_2'
|| ',3:' || :'migration_sha_3' || ',4:' || :'migration_sha_4'
|| ',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
\elif :at_v5
\ir 0006_commit_lookup.sql
\elif :at_v4
\ir pre_v5_guard.sql
\ir 0005_batch_decisions.sql
\ir 0006_commit_lookup.sql
\elif :at_v3
\ir 0004_lineage_profile.sql
\ir pre_v5_guard.sql
\ir 0005_batch_decisions.sql
\ir 0006_commit_lookup.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
\elif :at_v1
\ir 0002_region_fencing.sql
\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
\else
\echo 'Unknown or changed migration history'
DO $$ BEGIN RAISE EXCEPTION 'unknown or changed migration history'; END $$;
\endif
\else
\ir 0001_region_bootstrap.sql
\ir 0002_region_fencing.sql
\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
\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
-- grants or SET-able memberships would bypass the fenced functions. Checks the
-- runtime group and every role that is (transitively) a member of it.
SELECT pg_catalog.set_config('evolution.runtime_role', :'runtime_role', true);
DO $$
DECLARE
runtime oid := (SELECT r.oid FROM pg_catalog.pg_roles r
WHERE r.rolname = pg_catalog.current_setting('evolution.runtime_role'));
bad text;
BEGIN
IF runtime IS NULL THEN
RAISE EXCEPTION 'runtime role does not exist';
END IF;
WITH RECURSIVE closure(oid) AS (
SELECT runtime
UNION
SELECT m.member FROM pg_catalog.pg_auth_members m JOIN closure c ON m.roleid = c.oid
), fenced(oid) AS (VALUES
('evolution.acquire_region_lease(evolution.u64,evolution.u64,integer)'::regprocedure::oid),
('evolution.renew_region_lease(evolution.u64,evolution.u64,uuid,evolution.u64,integer)'::regprocedure::oid),
('evolution.release_region_lease(evolution.u64,evolution.u64,uuid,evolution.u64)'::regprocedure::oid),
('evolution.load_region_head(evolution.u64,evolution.u64)'::regprocedure::oid),
('evolution.publish_region_checkpoint(bytea,bytea)'::regprocedure::oid),
('evolution.load_region_checkpoint(evolution.u64,evolution.u64)'::regprocedure::oid),
('evolution.resolve_region_commit(evolution.u64,evolution.u64,uuid,bytea,bytea)'::regprocedure::oid),
('evolution.load_region_decisions(evolution.u64,evolution.u64)'::regprocedure::oid),
('evolution.pending_region_outbox(evolution.u64,evolution.u64,integer)'::regprocedure::oid),
('evolution.acknowledge_region_outbox(evolution.u64,evolution.u64,integer,integer,evolution.u64)'::regprocedure::oid),
('evolution.load_region_commit(evolution.u64,evolution.u64,evolution.u64)'::regprocedure::oid)
), violations(role_oid, reason) AS (
SELECT r.oid, 'privileged attribute or owner identity' FROM pg_catalog.pg_roles r
JOIN closure c ON c.oid = r.oid
WHERE r.rolsuper OR r.rolcreaterole OR r.rolcreatedb OR r.rolreplication
OR r.rolbypassrls OR r.rolname = current_user
UNION ALL
-- Any membership outside the runtime closure (inherited or SET-able).
SELECT m.member, 'membership outside the runtime group' FROM pg_catalog.pg_auth_members m
JOIN closure c ON c.oid = m.member
WHERE m.roleid NOT IN (SELECT oid FROM closure)
UNION ALL
SELECT c.oid, 'table privilege in evolution schema' FROM closure c
CROSS JOIN pg_catalog.pg_class t
WHERE t.relnamespace = 'evolution'::regnamespace AND t.relkind IN ('r', 'p', 'v', 'm', 'S')
AND pg_catalog.has_table_privilege(c.oid, t.oid,
'SELECT,INSERT,UPDATE,DELETE,TRUNCATE,REFERENCES,TRIGGER,MAINTAIN')
UNION ALL
SELECT c.oid, 'CREATE on schema or database' FROM closure c
WHERE pg_catalog.has_schema_privilege(c.oid, 'evolution', 'CREATE')
OR pg_catalog.has_database_privilege(c.oid, pg_catalog.current_database(), 'CREATE')
OR EXISTS (SELECT 1 FROM pg_catalog.pg_namespace n WHERE n.nspname = 'public'
AND pg_catalog.has_schema_privilege(c.oid, n.oid, 'CREATE'))
UNION ALL
-- Only the eleven guarded functions are executable; provisioning/internal are not.
SELECT c.oid, 'EXECUTE outside the fenced function set' FROM closure c
CROSS JOIN pg_catalog.pg_proc p
WHERE p.pronamespace = 'evolution'::regnamespace
AND p.oid NOT IN (SELECT oid FROM fenced)
AND pg_catalog.has_function_privilege(c.oid, p.oid, 'EXECUTE')
UNION ALL
SELECT runtime, 'missing EXECUTE on a fenced function' FROM fenced f
WHERE NOT pg_catalog.has_function_privilege(runtime, f.oid, 'EXECUTE')
)
SELECT pg_catalog.string_agg(DISTINCT v.reason, '; ') INTO bad FROM violations v;
IF bad IS NOT NULL THEN
-- Reasons only; no role names or connection details are printed.
RAISE EXCEPTION 'runtime role audit failed: %', bad;
END IF;
END $$;
COMMIT;