Account merge ledger: fix the first Soul pass (suspension carried into a memberless survivor, merged status terminal, standing at the desk door, linked accounts and write time, journalled grace intake, survivor weight on moved votes, one-hop alias, State token and database guards for overlapping merges, live-Postgres tests)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Heimdall 2026-11-14 03:37:48 +00:00
commit f402eb13cc
23 changed files with 1273 additions and 97 deletions

View file

@ -628,9 +628,16 @@ public sealed class BifrostDbContext(DbContextOptions<BifrostDbContext> options)
modelBuilder.Entity<AccountMerge>(entity =>
{
entity.Property(x => x.State).HasConversion<string>().HasMaxLength(24);
// State is the token: a transition computed from a stale read (the sweeper's Final over a saga's UndoRequested) fails its save.
entity.Property(x => x.State).HasConversion<string>().HasMaxLength(24).IsConcurrencyToken();
entity.Property(x => x.FailureCode).HasMaxLength(64);
entity.HasIndex(x => new { x.FoldedUserAccountId, x.State });
// An account is folded once: the DB refuses a second live merge folding it. The cross-role case (a survivor that is also
// folded) cannot be one unique index; the migration's trigger refuses it.
entity.HasIndex(x => x.FoldedUserAccountId)
.IsUnique()
.HasDatabaseName("UX_AccountMerges_FoldedUserAccountId_Live")
.HasFilter("\"State\" IN ('Confirmed', 'HeimdallMoved', 'Committed', 'Final', 'UndoRequested', 'HeimdallReversed')");
entity.HasIndex(x => new { x.SurvivorUserAccountId, x.State });
entity.HasIndex(x => new { x.State, x.GraceEndsUtc });
entity.HasOne<UserAccount>()

View file

@ -105,6 +105,13 @@ namespace Bifrost.Web.Data.Migrations
columns: new[] { "MergeId", "Seq" },
unique: true);
migrationBuilder.CreateIndex(
name: "UX_AccountMerges_FoldedUserAccountId_Live",
table: "AccountMerges",
column: "FoldedUserAccountId",
unique: true,
filter: "\"State\" IN ('Confirmed', 'HeimdallMoved', 'Committed', 'Final', 'UndoRequested', 'HeimdallReversed')");
migrationBuilder.CreateIndex(
name: "IX_AccountMerges_FoldedUserAccountId_State",
table: "AccountMerges",
@ -119,11 +126,50 @@ namespace Bifrost.Web.Data.Migrations
name: "IX_AccountMerges_SurvivorUserAccountId_State",
table: "AccountMerges",
columns: new[] { "SurvivorUserAccountId", "State" });
// The cross-role guard no single unique index can express: a live merge's survivor is never another live merge's folded
// account (no chains), whichever order they arrive in. Advisory locks on both accounts, taken in a fixed order, serialise two
// transactions racing over the same account, so the second one's check sees the first's committed row.
migrationBuilder.Sql(@"
CREATE FUNCTION ""AccountMerges_refuse_chain""() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
IF NEW.""State"" IN ('Confirmed', 'HeimdallMoved', 'Committed', 'Final', 'UndoRequested', 'HeimdallReversed') THEN
PERFORM pg_advisory_xact_lock(hashtextextended(LEAST(NEW.""SurvivorUserAccountId""::text, NEW.""FoldedUserAccountId""::text), 0));
PERFORM pg_advisory_xact_lock(hashtextextended(GREATEST(NEW.""SurvivorUserAccountId""::text, NEW.""FoldedUserAccountId""::text), 0));
IF EXISTS (
SELECT 1 FROM ""AccountMerges"" m
WHERE m.""Id"" <> NEW.""Id""
AND m.""State"" IN ('Confirmed', 'HeimdallMoved', 'Committed', 'Final', 'UndoRequested', 'HeimdallReversed')
AND (m.""SurvivorUserAccountId"" = NEW.""FoldedUserAccountId"" OR m.""FoldedUserAccountId"" = NEW.""SurvivorUserAccountId""))
THEN
RAISE EXCEPTION 'account merge chain refused' USING ERRCODE = '23P01';
END IF;
END IF;
RETURN NEW;
END
$$;
CREATE TRIGGER ""AccountMerges_refuse_chain"" BEFORE INSERT OR UPDATE OF ""State"", ""SurvivorUserAccountId"", ""FoldedUserAccountId"" ON ""AccountMerges""
FOR EACH ROW EXECUTE FUNCTION ""AccountMerges_refuse_chain""();");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
// Voiding lives only in the new build's query filter and these columns: dropping them (or running an older build over them)
// counts every voided vote again, and a close in that window records a wrong result for good. Refuse while any is voided;
// see docs/account-merge.md (Rollback).
migrationBuilder.Sql(@"
DO $$
BEGIN
IF EXISTS (SELECT 1 FROM ""Votes"" WHERE ""VoidedByMergeId"" IS NOT NULL)
OR EXISTS (SELECT 1 FROM ""ConceptAppraisals"" WHERE ""VoidedByMergeId"" IS NOT NULL) THEN
RAISE EXCEPTION 'AddAccountMerges cannot be reverted while merged votes or appraisals are voided';
END IF;
END
$$;");
migrationBuilder.Sql(@"DROP TRIGGER ""AccountMerges_refuse_chain"" ON ""AccountMerges""; DROP FUNCTION ""AccountMerges_refuse_chain""();");
migrationBuilder.DropTable(
name: "AccountMergeEntries");

View file

@ -52,6 +52,7 @@ namespace Bifrost.Web.Data.Migrations
.HasColumnType("timestamp with time zone");
b.Property<string>("State")
.IsConcurrencyToken()
.IsRequired()
.HasMaxLength(24)
.HasColumnType("character varying(24)");
@ -64,6 +65,11 @@ namespace Bifrost.Web.Data.Migrations
b.HasKey("Id");
b.HasIndex("FoldedUserAccountId")
.IsUnique()
.HasDatabaseName("UX_AccountMerges_FoldedUserAccountId_Live")
.HasFilter("\"State\" IN ('Confirmed', 'HeimdallMoved', 'Committed', 'Final', 'UndoRequested', 'HeimdallReversed')");
b.HasIndex("FoldedUserAccountId", "State");
b.HasIndex("State", "GraceEndsUtc");