-- M11.2c-1, ADR-0100. Durable allocation only: no reservation is a living body. -- Owner provisions an audited unused identity range for a world. Runtime cannot -- initialize/reset it. One common counter allocates life/organism/genome/event -- values in their DISTINCT namespaces; it never reuses a reserved value. CREATE TABLE evolution.life_allocators ( world_id evolution.u64 PRIMARY KEY CHECK (world_id > 0), next_identity evolution.u64 NOT NULL CHECK (next_identity > 0) ); CREATE TABLE evolution.life_reservations ( world_id evolution.u64 NOT NULL REFERENCES evolution.life_allocators, account_id uuid NOT NULL CHECK (account_id <> '00000000-0000-0000-0000-000000000000'), request_id evolution.u64 NOT NULL CHECK (request_id > 0), identity evolution.u64 NOT NULL CHECK (identity > 0), region_id evolution.u64 NOT NULL, rules integer NOT NULL CHECK (rules = 1), rejection smallint NOT NULL DEFAULT 0 CHECK (rejection BETWEEN 0 AND 3), PRIMARY KEY (world_id, account_id, request_id), UNIQUE (world_id, identity), FOREIGN KEY (world_id, region_id) REFERENCES evolution.region_leases ); -- 0 reserved; 1 insufficient funding; 2 no placement/capacity; 3 cancelled. -- Retain rejections and allocations forever. A new request may follow rejection. CREATE UNIQUE INDEX one_reserved_life_per_account ON evolution.life_reservations(world_id, account_id) WHERE rejection = 0; REVOKE ALL ON evolution.life_allocators, evolution.life_reservations FROM PUBLIC; -- Extends lock order: upgrade -> lease -> account -> reservation -> allocator. -- Internal helper, never granted to runtime. Checks database time AFTER lock waits. CREATE FUNCTION evolution.check_life_control(p_world evolution.u64, p_region evolution.u64, p_holder uuid, p_epoch evolution.u64, p_account uuid, p_generation evolution.u64) RETURNS void LANGUAGE plpgsql SECURITY INVOKER SET search_path = pg_catalog, pg_temp AS $$ DECLARE c evolution.account_controls%ROWTYPE; BEGIN IF p_account IS NULL OR p_account = '00000000-0000-0000-0000-000000000000' OR p_generation IS NULL OR p_generation = 0 THEN RAISE EXCEPTION 'invalid life control' USING ERRCODE = '22023'; END IF; PERFORM evolution.check_region_lease(p_world, p_region, p_holder, p_epoch); SELECT * INTO c FROM evolution.account_controls a WHERE a.world_id = p_world AND a.account_id = p_account FOR UPDATE; IF NOT FOUND OR c.generation <> p_generation OR c.region_id IS DISTINCT FROM p_region OR c.region_epoch IS DISTINCT FROM p_epoch OR c.expires_at <= clock_timestamp() OR c.expires_at IS NULL THEN RAISE EXCEPTION 'life control fenced' USING ERRCODE = 'EVL02'; END IF; PERFORM evolution.check_region_lease(p_world, p_region, p_holder, p_epoch); END $$; -- Life request IDs are a separate operation namespace from M10 no-effect probes. -- Exact retry returns original owner/identity even after account control moves; -- caller must route to owner, never create another body in the new region. CREATE 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; 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) THEN RAISE EXCEPTION 'life already reserved' USING ERRCODE = 'EVL16'; 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; -- A wait for the allocator must not let an expired controller originate work. PERFORM evolution.check_life_control(p_world, p_region, p_holder, p_epoch, p_account, p_generation); 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 $$; CREATE FUNCTION evolution.reject_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_reason smallint) RETURNS smallint LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ DECLARE r evolution.life_reservations%ROWTYPE; BEGIN IF p_request IS NULL OR p_request = 0 OR p_reason IS NULL OR p_reason NOT BETWEEN 1 AND 3 THEN RAISE EXCEPTION 'invalid life rejection' 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 FOR UPDATE; IF NOT FOUND OR r.region_id <> p_region THEN RAISE EXCEPTION 'life reservation not owned' USING ERRCODE = 'EVL04'; END IF; IF r.rejection <> 0 THEN RETURN r.rejection; END IF; UPDATE evolution.life_reservations SET rejection = p_reason WHERE world_id = p_world AND account_id = p_account AND request_id = p_request; RETURN p_reason; END $$; REVOKE ALL ON FUNCTION evolution.check_life_control(evolution.u64,evolution.u64,uuid,evolution.u64,uuid,evolution.u64), evolution.reserve_primitive_life(evolution.u64,evolution.u64,uuid,evolution.u64,uuid,evolution.u64,evolution.u64,integer), evolution.reject_primitive_life(evolution.u64,evolution.u64,uuid,evolution.u64,uuid,evolution.u64,evolution.u64,smallint) FROM PUBLIC; GRANT EXECUTE ON FUNCTION evolution.reserve_primitive_life(evolution.u64,evolution.u64,uuid,evolution.u64,uuid,evolution.u64,evolution.u64,integer), evolution.reject_primitive_life(evolution.u64,evolution.u64,uuid,evolution.u64,uuid,evolution.u64,evolution.u64,smallint) TO :"runtime_role"; INSERT INTO evolution.schema_migrations(version, sha256) VALUES (15, :'migration_sha_15');