Files
aevolution/migrations/apply.sql
T
dingoandClaude Opus 5.5 dd7e8028b0 Complete M7.5 durable batch outcomes and commit resolution
ADR-0063: manifest v2 (360 bytes) binds a per-tick validation trace
object and a canonical decision block by SHA-256. Publication replays the
trace from the committed predecessor's verified payload and requires
every report and the candidate bytes, derives deaths deterministically,
uploads payload and trace with verified read-back, then sends manifest
and decisions to one fenced statement. RegionBatch seals at a terminal
transition or 64 ticks and refuses ticks until durable confirmation.

Schema v5 records the checkpoint, its decisions and outbox rows
atomically; a unique index keeps one terminal outcome per actor
(EVL08), triggers make commits/decisions immutable (EVL09), an exact
retry of a committed ID returns the original receipt, changed bytes
conflict, and read-only resolve_region_commit answers committed/absent/
conflict. Outbox pending/acknowledge give idempotent at-least-once
delivery. Recovery reconciles durable deaths with restored lifecycle
(and archive deaths for the lineage profile).

Validated: Windows CTest 58/58; Linux GCC Debug/Release, clang-tidy and
ASan/UBSan CTest 58/58 each; PostgreSQL suites Release, analysis, ASan
(lost response, committed-after-timeout, terminated orphan retry,
changed bytes, repeated/partial/reordered decisions, gap, outbox
redelivery, history quarantine); five mutations each caught. No
Docker/Gitea CI, process kill, broker or deployment.

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

123 lines
6.1 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 \gset
\if :at_v5
\elif :at_v4
\ir 0005_batch_decisions.sql
\elif :at_v3
\ir 0004_lineage_profile.sql
\ir 0005_batch_decisions.sql
\elif :at_v2
\ir 0003_region_checkpoints.sql
\ir 0004_lineage_profile.sql
\ir 0005_batch_decisions.sql
\elif :at_v1
\ir 0002_region_fencing.sql
\ir 0003_region_checkpoints.sql
\ir 0004_lineage_profile.sql
\ir 0005_batch_decisions.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 0005_batch_decisions.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)
), 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 ten 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;