Account merge saga: Confirm with consent record and sealed proofs, one crash-safe step function for the Heimdall move and reversal, one merge loop

AccountMergeSaga is the only caller of the move client and the only writer of Confirmed, HeimdallMoved, Failed, UndoRequested and HeimdallReversed; the
ledger keeps Committed, Final and Undone. Migration AddAccountMergeSaga adds the saga columns and the two move-id keys; its Down is roll-forward only.
AccountMergeSweeper becomes the one loop: saga advance, then finalize, every 30 s.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Heimdall 2026-11-14 06:48:43 +00:00
commit cdf64b2a10
8 changed files with 726 additions and 5 deletions

View file

@ -635,6 +635,17 @@ public sealed class BifrostDbContext(DbContextOptions<BifrostDbContext> options)
// 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.Property(x => x.HeimdallMoveId).HasMaxLength(64);
entity.Property(x => x.HeimdallReverseMoveId).HasMaxLength(64);
entity.Property(x => x.PreviewDigest).HasMaxLength(64);
entity.Property(x => x.SurvivorProofProvider).HasMaxLength(32);
entity.Property(x => x.FoldedProofProviders).HasMaxLength(120);
entity.Property(x => x.ProofFromTier).HasMaxLength(8);
entity.Property(x => x.ProofToTier).HasMaxLength(8);
entity.Property(x => x.LastOutcome).HasMaxLength(64);
// Heimdall's idempotency keys: one merge, one forward key and one reverse key, and no key shared by two merges.
entity.HasIndex(x => x.HeimdallMoveId).IsUnique().HasFilter("\"HeimdallMoveId\" IS NOT NULL");
entity.HasIndex(x => x.HeimdallReverseMoveId).IsUnique().HasFilter("\"HeimdallReverseMoveId\" IS NOT NULL");
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.

View file

@ -0,0 +1,78 @@
using System;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Bifrost.Web.Data.Migrations
{
// Hand-written like AddAccountMerges, the snapshot edited to match. Additive: the saga's columns on AccountMerges. Down is
// roll-forward only, like AddAccountMerges: it drops the Heimdall idempotency keys and the consent record, so it refuses while
// any merge row exists (heimdall-body:ruling:merge-roll-forward-only).
[DbContext(typeof(BifrostDbContext))]
[Migration("20261114130000_AddAccountMergeSaga")]
public partial class AddAccountMergeSaga : Migration
{
private static readonly string[] Columns =
[
"HeimdallMoveId", "HeimdallReverseMoveId", "SealedProofs", "PreviewDigest", "ConfirmedByUserAccountId", "ConfirmedUtc",
"SurvivorProofProvider", "FoldedProofProviders", "ProofFromTier", "ProofToTier", "MovedIdentityIds", "Attempts",
"NextAttemptUtc", "LastOutcome", "UndoRequestedUtc", "ReversedUtc"
];
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(name: "HeimdallMoveId", table: "AccountMerges", type: "character varying(64)", maxLength: 64, nullable: true);
migrationBuilder.AddColumn<string>(name: "HeimdallReverseMoveId", table: "AccountMerges", type: "character varying(64)", maxLength: 64, nullable: true);
migrationBuilder.AddColumn<string>(name: "SealedProofs", table: "AccountMerges", type: "text", nullable: true);
migrationBuilder.AddColumn<string>(name: "PreviewDigest", table: "AccountMerges", type: "character varying(64)", maxLength: 64, nullable: false, defaultValue: "");
migrationBuilder.AddColumn<Guid>(name: "ConfirmedByUserAccountId", table: "AccountMerges", type: "uuid", nullable: true);
migrationBuilder.AddColumn<DateTimeOffset>(name: "ConfirmedUtc", table: "AccountMerges", type: "timestamp with time zone", nullable: true);
migrationBuilder.AddColumn<string>(name: "SurvivorProofProvider", table: "AccountMerges", type: "character varying(32)", maxLength: 32, nullable: false, defaultValue: "");
migrationBuilder.AddColumn<string>(name: "FoldedProofProviders", table: "AccountMerges", type: "character varying(120)", maxLength: 120, nullable: false, defaultValue: "");
migrationBuilder.AddColumn<string>(name: "ProofFromTier", table: "AccountMerges", type: "character varying(8)", maxLength: 8, nullable: true);
migrationBuilder.AddColumn<string>(name: "ProofToTier", table: "AccountMerges", type: "character varying(8)", maxLength: 8, nullable: true);
migrationBuilder.AddColumn<string>(name: "MovedIdentityIds", table: "AccountMerges", type: "text", nullable: true);
migrationBuilder.AddColumn<int>(name: "Attempts", table: "AccountMerges", type: "integer", nullable: false, defaultValue: 0);
migrationBuilder.AddColumn<DateTimeOffset>(name: "NextAttemptUtc", table: "AccountMerges", type: "timestamp with time zone", nullable: true);
migrationBuilder.AddColumn<string>(name: "LastOutcome", table: "AccountMerges", type: "character varying(64)", maxLength: 64, nullable: false, defaultValue: "");
migrationBuilder.AddColumn<DateTimeOffset>(name: "UndoRequestedUtc", table: "AccountMerges", type: "timestamp with time zone", nullable: true);
migrationBuilder.AddColumn<DateTimeOffset>(name: "ReversedUtc", table: "AccountMerges", type: "timestamp with time zone", nullable: true);
migrationBuilder.CreateIndex(
name: "IX_AccountMerges_HeimdallMoveId",
table: "AccountMerges",
column: "HeimdallMoveId",
unique: true,
filter: "\"HeimdallMoveId\" IS NOT NULL");
migrationBuilder.CreateIndex(
name: "IX_AccountMerges_HeimdallReverseMoveId",
table: "AccountMerges",
column: "HeimdallReverseMoveId",
unique: true,
filter: "\"HeimdallReverseMoveId\" IS NOT NULL");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql(@"
DO $$
BEGIN
IF EXISTS (SELECT 1 FROM ""AccountMerges"") THEN
RAISE EXCEPTION 'AddAccountMergeSaga cannot be reverted once an account merge exists: Bifrost is roll-forward only';
END IF;
END
$$;");
migrationBuilder.DropIndex(name: "IX_AccountMerges_HeimdallMoveId", table: "AccountMerges");
migrationBuilder.DropIndex(name: "IX_AccountMerges_HeimdallReverseMoveId", table: "AccountMerges");
foreach (var column in Columns)
{
migrationBuilder.DropColumn(name: column, table: "AccountMerges");
}
}
}
}

View file

@ -28,9 +28,18 @@ namespace Bifrost.Web.Data.Migrations
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<int>("Attempts")
.HasColumnType("integer");
b.Property<DateTimeOffset?>("CommittedUtc")
.HasColumnType("timestamp with time zone");
b.Property<Guid?>("ConfirmedByUserAccountId")
.HasColumnType("uuid");
b.Property<DateTimeOffset?>("ConfirmedUtc")
.HasColumnType("timestamp with time zone");
b.Property<DateTimeOffset>("CreatedUtc")
.HasColumnType("timestamp with time zone");
@ -45,24 +54,75 @@ namespace Bifrost.Web.Data.Migrations
b.Property<DateTimeOffset?>("FinalizedUtc")
.HasColumnType("timestamp with time zone");
b.Property<string>("FoldedProofProviders")
.IsRequired()
.HasMaxLength(120)
.HasColumnType("character varying(120)");
b.Property<Guid>("FoldedUserAccountId")
.HasColumnType("uuid");
b.Property<DateTimeOffset?>("GraceEndsUtc")
.HasColumnType("timestamp with time zone");
b.Property<string>("HeimdallMoveId")
.HasMaxLength(64)
.HasColumnType("character varying(64)");
b.Property<string>("HeimdallReverseMoveId")
.HasMaxLength(64)
.HasColumnType("character varying(64)");
b.Property<string>("LastOutcome")
.IsRequired()
.HasMaxLength(64)
.HasColumnType("character varying(64)");
b.Property<string>("MovedIdentityIds")
.HasColumnType("text");
b.Property<DateTimeOffset?>("NextAttemptUtc")
.HasColumnType("timestamp with time zone");
b.Property<string>("PreviewDigest")
.IsRequired()
.HasMaxLength(64)
.HasColumnType("character varying(64)");
b.Property<string>("ProofFromTier")
.HasMaxLength(8)
.HasColumnType("character varying(8)");
b.Property<string>("ProofToTier")
.HasMaxLength(8)
.HasColumnType("character varying(8)");
b.Property<DateTimeOffset?>("ReversedUtc")
.HasColumnType("timestamp with time zone");
b.Property<string>("SealedProofs")
.HasColumnType("text");
b.Property<string>("State")
.IsConcurrencyToken()
.IsRequired()
.HasMaxLength(24)
.HasColumnType("character varying(24)");
b.Property<string>("SurvivorProofProvider")
.IsRequired()
.HasMaxLength(32)
.HasColumnType("character varying(32)");
b.Property<Guid>("SurvivorUserAccountId")
.HasColumnType("uuid");
b.Property<DateTimeOffset?>("UndoneUtc")
.HasColumnType("timestamp with time zone");
b.Property<DateTimeOffset?>("UndoRequestedUtc")
.HasColumnType("timestamp with time zone");
b.HasKey("Id");
b.HasIndex("FoldedUserAccountId")
@ -72,6 +132,14 @@ namespace Bifrost.Web.Data.Migrations
b.HasIndex("FoldedUserAccountId", "State");
b.HasIndex("HeimdallMoveId")
.IsUnique()
.HasFilter("\"HeimdallMoveId\" IS NOT NULL");
b.HasIndex("HeimdallReverseMoveId")
.IsUnique()
.HasFilter("\"HeimdallReverseMoveId\" IS NOT NULL");
b.HasIndex("State", "GraceEndsUtc");
b.HasIndex("SurvivorUserAccountId", "State");