Receipt governance activity in Bifrost

This commit is contained in:
Meta Vens 2026-06-17 05:21:29 +01:00
commit cab9d22f78
15 changed files with 2600 additions and 3 deletions

View file

@ -59,6 +59,8 @@ public sealed class BifrostDbContext(DbContextOptions<BifrostDbContext> options)
public DbSet<AgentTransportReceipt> AgentTransportReceipts => Set<AgentTransportReceipt>();
public DbSet<GovernanceActivityReceipt> GovernanceActivityReceipts => Set<GovernanceActivityReceipt>();
public DbSet<AuditEvent> AuditEvents => Set<AuditEvent>();
protected override void OnModelCreating(ModelBuilder modelBuilder)
@ -475,6 +477,26 @@ public sealed class BifrostDbContext(DbContextOptions<BifrostDbContext> options)
.OnDelete(DeleteBehavior.SetNull);
});
modelBuilder.Entity<GovernanceActivityReceipt>(entity =>
{
entity.Property(x => x.TopicId).HasMaxLength(120);
entity.Property(x => x.CommentId).HasMaxLength(120);
entity.Property(x => x.DispatchRequestId).HasMaxLength(240);
entity.Property(x => x.Title).HasMaxLength(240);
entity.Property(x => x.JurisdictionRepoName).HasMaxLength(120);
entity.Property(x => x.JurisdictionAgentIdentity).HasMaxLength(160);
entity.Property(x => x.ActivityKind).HasConversion<string>();
entity.Property(x => x.ActorKind).HasMaxLength(40);
entity.Property(x => x.ActorName).HasMaxLength(160);
entity.HasIndex(x => x.TopicId);
entity.HasIndex(x => x.OccurredAtUtc);
entity.HasIndex(x => new { x.JurisdictionRepoName, x.JurisdictionAgentIdentity, x.ActivityKind });
entity.HasOne(x => x.ActorUserAccount)
.WithMany(x => x.GovernanceActivityReceipts)
.HasForeignKey(x => x.ActorUserAccountId)
.OnDelete(DeleteBehavior.SetNull);
});
modelBuilder.Entity<AuditEvent>(entity =>
{
entity.Property(x => x.EntityType).HasMaxLength(120);

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,71 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Bifrost.Web.Data.Migrations
{
/// <inheritdoc />
public partial class AddGovernanceActivityReceipts : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "GovernanceActivityReceipts",
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
TopicId = table.Column<string>(type: "character varying(120)", maxLength: 120, nullable: false),
CommentId = table.Column<string>(type: "character varying(120)", maxLength: 120, nullable: false),
DispatchRequestId = table.Column<string>(type: "character varying(240)", maxLength: 240, nullable: false),
Title = table.Column<string>(type: "character varying(240)", maxLength: 240, nullable: false),
JurisdictionRepoName = table.Column<string>(type: "character varying(120)", maxLength: 120, nullable: false),
JurisdictionAgentIdentity = table.Column<string>(type: "character varying(160)", maxLength: 160, nullable: false),
ActivityKind = table.Column<string>(type: "text", nullable: false),
ActorKind = table.Column<string>(type: "character varying(40)", maxLength: 40, nullable: false),
ActorName = table.Column<string>(type: "character varying(160)", maxLength: 160, nullable: false),
Note = table.Column<string>(type: "text", nullable: false),
ActorUserAccountId = table.Column<Guid>(type: "uuid", nullable: true),
OccurredAtUtc = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_GovernanceActivityReceipts", x => x.Id);
table.ForeignKey(
name: "FK_GovernanceActivityReceipts_UserAccounts_ActorUserAccountId",
column: x => x.ActorUserAccountId,
principalTable: "UserAccounts",
principalColumn: "Id",
onDelete: ReferentialAction.SetNull);
});
migrationBuilder.CreateIndex(
name: "IX_GovernanceActivityReceipts_ActorUserAccountId",
table: "GovernanceActivityReceipts",
column: "ActorUserAccountId");
migrationBuilder.CreateIndex(
name: "IX_GovernanceActivityReceipts_JurisdictionRepoName_Jurisdictio~",
table: "GovernanceActivityReceipts",
columns: new[] { "JurisdictionRepoName", "JurisdictionAgentIdentity", "ActivityKind" });
migrationBuilder.CreateIndex(
name: "IX_GovernanceActivityReceipts_OccurredAtUtc",
table: "GovernanceActivityReceipts",
column: "OccurredAtUtc");
migrationBuilder.CreateIndex(
name: "IX_GovernanceActivityReceipts_TopicId",
table: "GovernanceActivityReceipts",
column: "TopicId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "GovernanceActivityReceipts");
}
}
}

View file

@ -494,6 +494,79 @@ namespace Bifrost.Web.Data.Migrations
b.ToTable("GitHubPullRequestLinks");
});
modelBuilder.Entity("Bifrost.Web.Domain.GovernanceActivityReceipt", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<string>("ActivityKind")
.IsRequired()
.HasColumnType("text");
b.Property<string>("ActorKind")
.IsRequired()
.HasMaxLength(40)
.HasColumnType("character varying(40)");
b.Property<string>("ActorName")
.IsRequired()
.HasMaxLength(160)
.HasColumnType("character varying(160)");
b.Property<Guid?>("ActorUserAccountId")
.HasColumnType("uuid");
b.Property<string>("CommentId")
.IsRequired()
.HasMaxLength(120)
.HasColumnType("character varying(120)");
b.Property<string>("DispatchRequestId")
.IsRequired()
.HasMaxLength(240)
.HasColumnType("character varying(240)");
b.Property<string>("JurisdictionAgentIdentity")
.IsRequired()
.HasMaxLength(160)
.HasColumnType("character varying(160)");
b.Property<string>("JurisdictionRepoName")
.IsRequired()
.HasMaxLength(120)
.HasColumnType("character varying(120)");
b.Property<string>("Note")
.IsRequired()
.HasColumnType("text");
b.Property<DateTimeOffset>("OccurredAtUtc")
.HasColumnType("timestamp with time zone");
b.Property<string>("Title")
.IsRequired()
.HasMaxLength(240)
.HasColumnType("character varying(240)");
b.Property<string>("TopicId")
.IsRequired()
.HasMaxLength(120)
.HasColumnType("character varying(120)");
b.HasKey("Id");
b.HasIndex("ActorUserAccountId");
b.HasIndex("OccurredAtUtc");
b.HasIndex("TopicId");
b.HasIndex("JurisdictionRepoName", "JurisdictionAgentIdentity", "ActivityKind");
b.ToTable("GovernanceActivityReceipts");
});
modelBuilder.Entity("Bifrost.Web.Domain.LedgerEntry", b =>
{
b.Property<Guid>("Id")
@ -1509,6 +1582,16 @@ namespace Bifrost.Web.Data.Migrations
b.Navigation("WorkItem");
});
modelBuilder.Entity("Bifrost.Web.Domain.GovernanceActivityReceipt", b =>
{
b.HasOne("Bifrost.Web.Domain.UserAccount", "ActorUserAccount")
.WithMany("GovernanceActivityReceipts")
.HasForeignKey("ActorUserAccountId")
.OnDelete(DeleteBehavior.SetNull);
b.Navigation("ActorUserAccount");
});
modelBuilder.Entity("Bifrost.Web.Domain.LedgerEntry", b =>
{
b.HasOne("Bifrost.Web.Domain.UserAccount", "CreatedByUserAccount")
@ -1900,6 +1983,8 @@ namespace Bifrost.Web.Data.Migrations
b.Navigation("CreatedMotions");
b.Navigation("GovernanceActivityReceipts");
b.Navigation("LedgerEntries");
b.Navigation("MemberProfile");

View file

@ -231,6 +231,14 @@ public enum AgentTransportReceiptKind
Closed
}
public enum GovernanceActivityReceiptKind
{
TopicOpened,
TopicCommented,
TopicApproved,
TopicPromoted
}
public sealed class UserAccount
{
public Guid Id { get; set; } = Guid.NewGuid();
@ -285,6 +293,8 @@ public sealed class UserAccount
public ICollection<AgentTransportReceipt> AgentTransportReceipts { get; set; } = [];
public ICollection<GovernanceActivityReceipt> GovernanceActivityReceipts { get; set; } = [];
public ICollection<AuditEvent> AuditEvents { get; set; } = [];
}
@ -1014,6 +1024,37 @@ public sealed class AgentTransportReceipt
public UserAccount? ActorUserAccount { get; set; }
}
public sealed class GovernanceActivityReceipt
{
public Guid Id { get; set; } = Guid.NewGuid();
public string TopicId { get; set; } = string.Empty;
public string CommentId { get; set; } = string.Empty;
public string DispatchRequestId { get; set; } = string.Empty;
public string Title { get; set; } = string.Empty;
public string JurisdictionRepoName { get; set; } = string.Empty;
public string JurisdictionAgentIdentity { get; set; } = string.Empty;
public GovernanceActivityReceiptKind ActivityKind { get; set; } = GovernanceActivityReceiptKind.TopicOpened;
public string ActorKind { get; set; } = string.Empty;
public string ActorName { get; set; } = string.Empty;
public string Note { get; set; } = string.Empty;
public Guid? ActorUserAccountId { get; set; }
public DateTimeOffset OccurredAtUtc { get; set; }
public UserAccount? ActorUserAccount { get; set; }
}
public sealed class AuditEvent
{
public Guid Id { get; set; } = Guid.NewGuid();

View file

@ -0,0 +1,104 @@
using Bifrost.Web.Data;
using Bifrost.Web.Domain;
using Bifrost.Web.Features.Shared;
namespace Bifrost.Web.Features.Bridge;
public sealed class GovernanceActivityReceiptService(
BifrostDbContext dbContext,
AuditTrailService auditTrailService,
TimeProvider timeProvider)
{
public async Task<GovernanceActivityReceiptResult> RecordAsync(
GovernanceActivityReceiptRequest request,
Guid? actorUserAccountId,
CancellationToken cancellationToken)
{
var receipt = new GovernanceActivityReceipt
{
TopicId = NormalizeText(request.TopicId),
CommentId = NormalizeText(request.CommentId),
DispatchRequestId = NormalizeText(request.DispatchRequestId),
Title = NormalizeText(request.Title),
JurisdictionRepoName = NormalizeText(request.JurisdictionRepoName),
JurisdictionAgentIdentity = NormalizeText(request.JurisdictionAgentIdentity),
ActivityKind = request.ActivityKind,
ActorKind = NormalizeText(request.ActorKind),
ActorName = NormalizeText(request.ActorName),
Note = NormalizeText(request.Note),
ActorUserAccountId = actorUserAccountId,
OccurredAtUtc = timeProvider.GetUtcNow()
};
dbContext.GovernanceActivityReceipts.Add(receipt);
await dbContext.SaveChangesAsync(cancellationToken);
await auditTrailService.RecordAsync(
actorUserAccountId,
nameof(GovernanceActivityReceipt),
receipt.Id,
$"governance.{ToAuditSuffix(receipt.ActivityKind)}",
BuildAuditDetail(receipt),
cancellationToken);
return GovernanceActivityReceiptResult.From(receipt);
}
private static string ToAuditSuffix(GovernanceActivityReceiptKind activityKind) => activityKind switch
{
GovernanceActivityReceiptKind.TopicOpened => "topic-opened",
GovernanceActivityReceiptKind.TopicCommented => "topic-commented",
GovernanceActivityReceiptKind.TopicApproved => "topic-approved",
GovernanceActivityReceiptKind.TopicPromoted => "topic-promoted",
_ => "activity",
};
private static string BuildAuditDetail(GovernanceActivityReceipt receipt)
{
var actorName = string.IsNullOrWhiteSpace(receipt.ActorName) ? "unknown actor" : receipt.ActorName;
var topicId = string.IsNullOrWhiteSpace(receipt.TopicId) ? "unknown topic" : receipt.TopicId;
return $"{actorName} recorded {receipt.ActivityKind} for governance topic {topicId}.";
}
private static string NormalizeText(string? value) => value?.Trim() ?? string.Empty;
}
public sealed record GovernanceActivityReceiptRequest(
string TopicId,
string CommentId,
string DispatchRequestId,
string Title,
string JurisdictionRepoName,
string JurisdictionAgentIdentity,
GovernanceActivityReceiptKind ActivityKind,
string ActorKind,
string ActorName,
string Note);
public sealed record GovernanceActivityReceiptResult(
Guid Id,
string TopicId,
string CommentId,
string DispatchRequestId,
string Title,
string JurisdictionRepoName,
string JurisdictionAgentIdentity,
GovernanceActivityReceiptKind ActivityKind,
string ActorKind,
string ActorName,
string Note,
DateTimeOffset OccurredAtUtc)
{
public static GovernanceActivityReceiptResult From(GovernanceActivityReceipt receipt) => new(
receipt.Id,
receipt.TopicId,
receipt.CommentId,
receipt.DispatchRequestId,
receipt.Title,
receipt.JurisdictionRepoName,
receipt.JurisdictionAgentIdentity,
receipt.ActivityKind,
receipt.ActorKind,
receipt.ActorName,
receipt.Note,
receipt.OccurredAtUtc);
}

View file

@ -151,6 +151,25 @@ public sealed class DashboardSnapshotService(BifrostDbContext dbContext, TimePro
x.OccurredAtUtc))
.ToListAsync(cancellationToken);
var recentGovernanceReceipts = await dbContext.GovernanceActivityReceipts
.AsNoTracking()
.OrderByDescending(x => x.OccurredAtUtc)
.Take(6)
.Select(x => new DashboardGovernanceReceipt(
x.Id,
x.TopicId,
x.CommentId,
x.DispatchRequestId,
x.Title,
x.JurisdictionRepoName,
x.JurisdictionAgentIdentity,
x.ActivityKind.ToString(),
x.ActorKind,
x.ActorName,
x.Note,
x.OccurredAtUtc))
.ToListAsync(cancellationToken);
var payoutPreviewNominal = await dbContext.LedgerEntries
.AsNoTracking()
.Where(x => x.Status == LedgerEntryStatus.Approved)
@ -180,7 +199,8 @@ public sealed class DashboardSnapshotService(BifrostDbContext dbContext, TimePro
RecentActivity: recentActivity,
RecentBridgeActions: recentBridgeActions,
RecentDispatchRuns: recentDispatchRuns,
RecentTransportReceipts: recentTransportReceipts);
RecentTransportReceipts: recentTransportReceipts,
RecentGovernanceReceipts: recentGovernanceReceipts);
}
}
@ -202,7 +222,8 @@ public sealed record DashboardSnapshot(
IReadOnlyList<DashboardActivity> RecentActivity,
IReadOnlyList<DashboardBridgeAction> RecentBridgeActions,
IReadOnlyList<DashboardDispatchRun> RecentDispatchRuns,
IReadOnlyList<DashboardTransportReceipt> RecentTransportReceipts);
IReadOnlyList<DashboardTransportReceipt> RecentTransportReceipts,
IReadOnlyList<DashboardGovernanceReceipt> RecentGovernanceReceipts);
public sealed record DashboardWorkItem(
Guid WorkItemId,
@ -271,3 +292,17 @@ public sealed record DashboardTransportReceipt(
string ActorName,
string Note,
DateTimeOffset OccurredAtUtc);
public sealed record DashboardGovernanceReceipt(
Guid GovernanceActivityReceiptId,
string TopicId,
string CommentId,
string DispatchRequestId,
string Title,
string JurisdictionRepoName,
string JurisdictionAgentIdentity,
string ActivityKind,
string ActorKind,
string ActorName,
string Note,
DateTimeOffset OccurredAtUtc);

View file

@ -269,6 +269,46 @@
}
</article>
<article class="panel">
<div class="panel-header">
<div>
<h2>Governance receipts</h2>
<p class="muted">Hosted receipts for topic opens, comments, approvals, and promotions.</p>
</div>
</div>
@if (Model.Snapshot.RecentGovernanceReceipts.Count == 0)
{
<div class="empty-state">No governance receipts have been recorded yet.</div>
}
else
{
<div class="list-grid">
@foreach (var receipt in Model.Snapshot.RecentGovernanceReceipts)
{
<div class="status-banner">
<strong>@receipt.JurisdictionRepoName@if (!string.IsNullOrWhiteSpace(receipt.JurisdictionAgentIdentity)) {<text> / @receipt.JurisdictionAgentIdentity</text>}</strong>
<span class="muted">@receipt.Title</span>
<div class="inline-actions">
<span class="pill">@receipt.ActivityKind</span>
<span class="pill">@receipt.ActorName</span>
</div>
<div class="muted">Topic @receipt.TopicId</div>
@if (!string.IsNullOrWhiteSpace(receipt.DispatchRequestId))
{
<div class="muted">Dispatch @receipt.DispatchRequestId</div>
}
@if (!string.IsNullOrWhiteSpace(receipt.Note))
{
<div class="muted">@receipt.Note</div>
}
<div class="muted">@receipt.OccurredAtUtc.ToString("dd MMM yyyy HH:mm") UTC</div>
</div>
}
</div>
}
</article>
<article class="panel">
<div class="panel-header">
<div>

View file

@ -11,7 +11,7 @@ public sealed class IndexModel(
public CurrentBifrostActor Actor { get; private set; } = CurrentBifrostActor.Anonymous;
public DashboardSnapshot Snapshot { get; private set; } =
new(0, 0, 0, 0, 0, 0, 0, 0, 0m, [], [], [], [], [], [], [], [], []);
new(0, 0, 0, 0, 0, 0, 0, 0, 0m, [], [], [], [], [], [], [], [], [], []);
public async Task OnGetAsync(CancellationToken cancellationToken)
{

View file

@ -82,6 +82,7 @@ builder.Services.AddScoped<DashboardSnapshotService>();
builder.Services.AddScoped<BridgeActionService>();
builder.Services.AddScoped<AgentDispatchRunService>();
builder.Services.AddScoped<AgentTransportReceiptService>();
builder.Services.AddScoped<GovernanceActivityReceiptService>();
builder.Services.AddScoped<MembershipSynchronizationService>();
builder.Services.AddScoped<GitHubWebhookService>();
builder.Services.AddScoped<MotionGovernanceService>();
@ -557,6 +558,26 @@ transportReceipts.MapPost("", async (
return JsonResult(result, appJsonSerializerOptions, StatusCodes.Status202Accepted);
});
var governanceReceipts = app.MapGroup("/governance/receipts");
governanceReceipts.MapPost("", async (
HttpRequest request,
GovernanceActivityReceiptRequest command,
ICurrentBifrostActorAccessor actorAccessor,
GovernanceActivityReceiptService governanceActivityReceiptService,
IOptions<BridgeOptions> bridgeOptions,
CancellationToken cancellationToken) =>
{
var actor = await actorAccessor.GetAsync(cancellationToken);
if (!HasValidLocalBridgeToken(request, bridgeOptions.Value) && !actor.IsActiveMember)
{
return Results.Unauthorized();
}
var result = await governanceActivityReceiptService.RecordAsync(command, actor.UserAccount?.Id, cancellationToken);
return JsonResult(result, appJsonSerializerOptions, StatusCodes.Status202Accepted);
});
var eveGovernance = app.MapGroup("/eve/governance")
.RequireAuthorization(ActiveMemberRequirement.PolicyName);