Files
aevolution/migrations/0069_admin_end_life.sql
T
dingoandClaude Opus 5.5 6de2b7b1ca
Foundation / checks (client) (push) Successful in 5s
Foundation / checks (checks) (push) Successful in 6s
Foundation / checks (deploy) (push) Successful in 6s
Foundation / checks (ecs) (push) Successful in 5s
Foundation / checks (network) (push) Successful in 4s
Foundation / checks (postgres) (push) Successful in 4s
Foundation / checks (tsan) (push) Successful in 5s
Foundation / postgres-tools (push) Successful in 5m17s
Foundation / image (push) Successful in 6m23s
Foundation / server-image (push) Successful in 8m26s
feat(admin): end a stranded life from the portal (schema 69)
ADR-0182 section 18. Region 16 refused every birth for an account with
"Existing cell is already bound to this account" although no cell in it
held the account: a published reservation was never ended, and the only
remedy was tools/ops/end-account-life.sql as the database owner.

- Account action POST /api/accounts/<uuid>/end-life?request=N. The admin
  API reads the life's region from the account's lives and refuses while
  that region's live view lists a cell holding the account (also a bud,
  split offspring or organism control moved to), a region it cannot view,
  an incomplete view, an unpublished request or a life no longer open.
- Schema 69: admin_end_account_life_v1 (admin role only) ends it at the
  region's latest commit and appends who and why to admin_life_endings.
- The account page shows End life for each published life not ended.
- A region refusing a birth because the account holds a cell (life_exists)
  could never record it: reject_primitive_life accepted reasons 1..3 only.
  The reservation stayed open and unpublished and refused every later
  birth. Schema 69 and the C++ wrapper accept existing_life (4).
- A region with no ticking cells ignored queued admin commands (such as a
  spawn) until something else woke it; a queued command now keeps it
  ticking until applied.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-29 07:32:08 -04:00

84 lines
4.7 KiB
PL/PgSQL

-- Admin: end a stranded life (ADR-0182 section 18, 2026-10-29). A published life whose
-- reservation was never ended refuses every new birth for the account ("Existing cell is
-- already bound to this account"). The admin API first checks that no cell in the life's
-- region holds the account; this function then ends the reservation at the region's latest
-- commit, exactly as end_cellular_lives does, and records who did it and why. Unpublished
-- reservations are left to the region, which still holds them as queued births. The region's
-- checkpoints and history are unchanged.
CREATE TABLE evolution.admin_life_endings (
sequence bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
world_id evolution.u64 NOT NULL,
account_id uuid NOT NULL,
request_id evolution.u64 NOT NULL,
ended_commit uuid NOT NULL,
author_subject text NOT NULL CHECK (octet_length(author_subject) BETWEEN 1 AND 255),
reason text NOT NULL DEFAULT '' CHECK (octet_length(reason) <= 500),
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
);
CREATE TRIGGER admin_life_endings_immutable BEFORE UPDATE OR DELETE ON evolution.admin_life_endings
FOR EACH ROW EXECUTE FUNCTION evolution.refuse_history_rewrite();
-- Returns true when this call ended the life, false when it was not open (already ended,
-- refused or cancelled). Raises EVL21 for an unknown or unpublished request.
CREATE FUNCTION evolution.admin_end_account_life_v1(p_world evolution.u64, p_account uuid,
p_request evolution.u64, p_subject text, p_reason text)
RETURNS boolean LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$
DECLARE r evolution.life_reservations%ROWTYPE; latest uuid;
BEGIN
IF p_world IS NULL OR p_world = 0 OR p_account IS NULL OR p_request IS NULL OR p_request = 0 THEN
RAISE EXCEPTION 'invalid life ending' USING ERRCODE = '22023';
END IF;
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 NOT EXISTS (SELECT 1 FROM evolution.primitive_life_bindings b
WHERE b.world_id = p_world AND b.account_id = p_account AND b.request_id = p_request) THEN
RAISE EXCEPTION 'no published life for this request' USING ERRCODE = 'EVL21';
END IF;
IF r.rejection <> 0 OR r.ended_commit IS NOT NULL THEN
RETURN false;
END IF;
SELECT c.commit_id INTO latest FROM evolution.region_commits c
WHERE c.world_id = p_world AND c.region_id = r.region_id
ORDER BY c.commit_sequence DESC LIMIT 1;
IF latest IS NULL THEN
RAISE EXCEPTION 'region has no commit' USING ERRCODE = 'EVL21';
END IF;
UPDATE evolution.life_reservations x SET ended_commit = latest
WHERE x.world_id = p_world AND x.account_id = p_account AND x.request_id = p_request;
INSERT INTO evolution.admin_life_endings(world_id, account_id, request_id, ended_commit,
author_subject, reason)
VALUES (p_world, p_account, p_request, latest, p_subject, coalesce(p_reason, ''));
RETURN true;
END $$;
REVOKE ALL ON FUNCTION evolution.admin_end_account_life_v1(evolution.u64, uuid, evolution.u64,
text, text) FROM PUBLIC;
-- A region refuses a birth whose account still holds a cell in its state (life_exists) and
-- records that refusal as existing_life (4). Schema 15 accepted only reasons 1..3, so the
-- refusal was never recorded: the reservation stayed open and unpublished, and refused every
-- later birth for the account. Schema 17 already let the column hold 4.
CREATE OR REPLACE 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 4 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 $$;
INSERT INTO evolution.schema_migrations(version,sha256) VALUES(69,:'migration_sha_69');