Merge origin/main into hands/mill-votes-cut-5

This commit is contained in:
Meta Vens 2026-10-11 00:06:51 +02:00
commit 0c6f9216b6
8 changed files with 1141 additions and 8 deletions

View file

@ -1,5 +1,4 @@
using System.Net;
using System.Text.RegularExpressions;
using Bifrost.Web.Configuration;
using Bifrost.Web.Data;
using Bifrost.Web.Domain;
@ -40,8 +39,6 @@ public sealed class ConceptAppraisalService(
{
private const string UniqueViolation = "23505";
private static readonly Regex Slug = new("^[a-z0-9-]{1,80}$", RegexOptions.CultureInvariant);
public async Task<CastResult> CastAsync(
string collection,
string key,
@ -260,7 +257,7 @@ public sealed class ConceptAppraisalService(
}
private Task<ConceptSubject?> FindSubjectAsync(string collection, string key, CancellationToken cancellationToken) =>
Slug.IsMatch(collection) && Slug.IsMatch(key)
ConceptKey.IsValid(collection) && ConceptKey.IsValid(key)
? dbContext.ConceptSubjects
.SingleOrDefaultAsync(x => x.Collection == collection && x.Key == key, cancellationToken)
: Task.FromResult<ConceptSubject?>(null);

View file

@ -0,0 +1,70 @@
using Microsoft.Extensions.DependencyInjection;
namespace Bifrost.Web.Features.ConceptVotes;
// Host command: Bifrost.Web.dll concepts plan|apply <collection> <manifest.json> [--retire k1,k2]
public static class ConceptCommand
{
public const int Ok = 0;
public const int Refused = 1;
public const int Usage = 2;
// args are the words after "concepts".
public static async Task<int> RunAsync(string[] args, IServiceProvider services, TextWriter output)
{
var known = args.Length > 0 && args[0] is "plan" or "apply";
var hasRetire = args.Length == 5 && args[3] == "--retire";
if (!known || !(args.Length == 3 || hasRetire))
{
await output.WriteLineAsync("usage: concepts plan|apply <collection> <manifest.json> [--retire k1,k2]");
return Usage;
}
string[] retire = hasRetire ? args[4].Split(',', StringSplitOptions.RemoveEmptyEntries) : [];
try
{
var ships = ConceptSubjectRegistry.ParseManifest(await ReadManifestAsync(args[2]));
await using var scope = services.CreateAsyncScope();
var registry = scope.ServiceProvider.GetRequiredService<ConceptSubjectRegistry>();
var plan = args[0] == "apply"
? await registry.ApplyAsync(args[1], ships, retire, default)
: await registry.PlanAsync(args[1], ships, retire, default);
foreach (var entry in plan.Entries)
{
await output.WriteLineAsync($"{entry.Change.ToString().ToLowerInvariant()} {entry.Key}");
}
return Ok;
}
catch (ManifestRefusedException ex)
{
await output.WriteLineAsync($"refused {ex.Field} {ex.Code}");
return Refused;
}
}
// Reads at most one byte past the bound, so a wrong file is never read whole.
private static async Task<byte[]> ReadManifestAsync(string path)
{
try
{
await using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 1, true);
var buffer = new byte[ConceptSubjectRegistry.MaxManifestBytes + 1];
var length = 0;
int read;
while (length < buffer.Length && (read = await stream.ReadAsync(buffer.AsMemory(length))) > 0)
{
length += read;
}
return buffer[..length];
}
// An empty or NUL path is an ArgumentException; a path the platform cannot name is NotSupported.
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or ArgumentException
or NotSupportedException or System.Security.SecurityException)
{
throw new ManifestRefusedException("manifest", "Unreadable");
}
}
}

View file

@ -0,0 +1,13 @@
using System.Text.RegularExpressions;
namespace Bifrost.Web.Features.ConceptVotes;
// The one grammar for a concept collection or key. \z, not $: $ admits a trailing line feed.
public static class ConceptKey
{
public const int MaxLength = 80;
private static readonly Regex Shape = new(@"^[a-z0-9-]{1,80}\z", RegexOptions.CultureInvariant);
public static bool IsValid(string? value) => value is not null && Shape.IsMatch(value);
}

View file

@ -0,0 +1,352 @@
using System.Globalization;
using System.Text;
using System.Text.Json;
using Bifrost.Web.Data;
using Bifrost.Web.Domain;
using Bifrost.Web.Features.Shared;
using Microsoft.EntityFrameworkCore;
namespace Bifrost.Web.Features.ConceptVotes;
public enum SubjectChange
{
Add,
Rename,
Unchanged,
Revive,
Absent,
Retire,
Retired
}
public sealed record ManifestShip(string Key, string Title);
public sealed record SubjectPlanEntry(SubjectChange Change, string Key, string Title);
public sealed record SubjectPlan(string Collection, IReadOnlyList<SubjectPlanEntry> Entries);
// A refusal names the field and a code, never the offending value.
public sealed class ManifestRefusedException(string field, string code)
: Exception($"{field}: {code}")
{
public string Field { get; } = field;
public string Code { get; } = code;
}
// The only writer of ConceptSubject: decides which concept keys exist, their titles and their retirement.
public sealed class ConceptSubjectRegistry(
BifrostDbContext dbContext,
AuditTrailService auditTrail,
TimeProvider timeProvider)
{
public const int MaxTitleLength = 240;
// Defaulted bounds (a Mill manifest is tens of ships): the whole file is refused above either.
public const int MaxManifestBytes = 1024 * 1024;
public const int MaxShips = 1000;
private static readonly UTF8Encoding StrictUtf8 = new(false, true);
// The Mill manifest is JSON from outside the Verse. Only ships[].id and ships[].name are read.
// This is the one input boundary: every malformed input leaves as a ManifestRefusedException.
public static IReadOnlyList<ManifestShip> ParseManifest(ReadOnlyMemory<byte> utf8)
{
using var document = ParseDocument(utf8);
var ships = ShipsOf(document.RootElement);
if (ships.ValueKind != JsonValueKind.Array)
{
throw new ManifestRefusedException("ships", "Missing");
}
if (ships.GetArrayLength() > MaxShips)
{
throw new ManifestRefusedException("ships", "TooMany");
}
var result = new List<ManifestShip>();
var seen = new HashSet<string>(StringComparer.Ordinal);
foreach (var ship in ships.EnumerateArray())
{
var at = $"ships[{result.Count}]";
var key = ReadString(ship, "id", at);
var title = ReadString(ship, "name", at);
if (!ConceptKey.IsValid(key))
{
throw new ManifestRefusedException($"{at}.id", "InvalidKey");
}
if (!seen.Add(key))
{
throw new ManifestRefusedException($"{at}.id", "Duplicate");
}
if (!IsValidTitle(title))
{
throw new ManifestRefusedException($"{at}.name", "InvalidTitle");
}
if (title.Length > MaxTitleLength)
{
throw new ManifestRefusedException($"{at}.name", "TooLong");
}
result.Add(new ManifestShip(key, title));
}
return result;
}
private static JsonElement ShipsOf(JsonElement root)
{
if (root.ValueKind != JsonValueKind.Object)
{
return default;
}
return root.TryGetProperty("ships", out var ships) ? ships : default;
}
// A displayed title: no control, format (bidi, zero-width, BOM), line or paragraph separator
// character, no leading or trailing whitespace, and at least one letter or digit. This is the
// character-class rule of the mind schema's Title type, kept here as the one copy in Bifrost.
private static bool IsValidTitle(string title)
{
if (title.Length == 0 || title != title.Trim())
{
return false;
}
var hasLetterOrDigit = false;
foreach (var rune in title.EnumerateRunes())
{
if (Rune.GetUnicodeCategory(rune) is UnicodeCategory.Control or UnicodeCategory.Format
or UnicodeCategory.LineSeparator or UnicodeCategory.ParagraphSeparator)
{
return false;
}
hasLetterOrDigit |= Rune.IsLetterOrDigit(rune);
}
return hasLetterOrDigit;
}
private static JsonDocument ParseDocument(ReadOnlyMemory<byte> utf8)
{
if (utf8.Length > MaxManifestBytes)
{
throw new ManifestRefusedException("manifest", "TooLarge");
}
try
{
StrictUtf8.GetCharCount(utf8.Span);
}
catch (DecoderFallbackException)
{
throw new ManifestRefusedException("manifest", "NotUtf8");
}
try
{
RejectDuplicateProperties(utf8.Span);
return JsonDocument.Parse(utf8);
}
catch (JsonException)
{
throw new ManifestRefusedException("manifest", "NotJson");
}
catch (InvalidOperationException)
{
// A property name holding a lone surrogate escape: not text.
throw new ManifestRefusedException("manifest", "BadText");
}
}
// System.Text.Json (net8.0) keeps the last of two equal property names; a manifest that says
// one thing twice is refused instead.
private static void RejectDuplicateProperties(ReadOnlySpan<byte> utf8)
{
var reader = new Utf8JsonReader(utf8);
var scopes = new Stack<HashSet<string>?>();
while (reader.Read())
{
switch (reader.TokenType)
{
case JsonTokenType.StartObject:
scopes.Push(new HashSet<string>(StringComparer.Ordinal));
break;
case JsonTokenType.StartArray:
scopes.Push(null);
break;
case JsonTokenType.EndObject:
case JsonTokenType.EndArray:
scopes.Pop();
break;
case JsonTokenType.PropertyName:
if (!scopes.Peek()!.Add(reader.GetString()!))
{
throw new ManifestRefusedException("manifest", "DuplicateProperty");
}
break;
}
}
}
public async Task<SubjectPlan> PlanAsync(
string collection,
IReadOnlyList<ManifestShip> ships,
IReadOnlyCollection<string> retire,
CancellationToken cancellationToken)
{
if (!ConceptKey.IsValid(collection))
{
throw new ManifestRefusedException("collection", "InvalidKey");
}
var named = new HashSet<string>(StringComparer.Ordinal);
foreach (var key in retire)
{
if (!ConceptKey.IsValid(key))
{
throw new ManifestRefusedException("retire", "InvalidKey");
}
named.Add(key);
}
var registered = await dbContext.ConceptSubjects.AsNoTracking()
.Where(x => x.Collection == collection)
.ToDictionaryAsync(x => x.Key, StringComparer.Ordinal, cancellationToken);
var entries = new List<SubjectPlanEntry>();
foreach (var ship in ships)
{
var change = !registered.TryGetValue(ship.Key, out var row)
? SubjectChange.Add
: row.RetiredAtUtc is not null
? SubjectChange.Revive
: row.Title != ship.Title ? SubjectChange.Rename : SubjectChange.Unchanged;
entries.Add(new SubjectPlanEntry(change, ship.Key, ship.Title));
}
var inManifest = ships.Select(x => x.Key).ToHashSet(StringComparer.Ordinal);
var absent = registered.Values
.Where(x => x.RetiredAtUtc is null && !inManifest.Contains(x.Key))
.OrderBy(x => x.Key, StringComparer.Ordinal)
.ToList();
foreach (var subject in absent)
{
entries.Add(new SubjectPlanEntry(
named.Remove(subject.Key) ? SubjectChange.Retire : SubjectChange.Absent,
subject.Key,
subject.Title));
}
// A named key that an earlier run already retired is done: a rerun converges.
foreach (var key in named.OrderBy(x => x, StringComparer.Ordinal).ToList())
{
if (registered.TryGetValue(key, out var done) && done.RetiredAtUtc is not null && !inManifest.Contains(key))
{
entries.Add(new SubjectPlanEntry(SubjectChange.Retired, key, done.Title));
named.Remove(key);
}
}
// Whatever is still named is not a registered key missing from the manifest.
if (named.Count > 0)
{
throw new ManifestRefusedException("retire", "NotAbsent");
}
return new SubjectPlan(collection, entries);
}
public async Task<SubjectPlan> ApplyAsync(
string collection,
IReadOnlyList<ManifestShip> ships,
IReadOnlyCollection<string> retire,
CancellationToken cancellationToken)
{
var plan = await PlanAsync(collection, ships, retire, cancellationToken);
var rows = await dbContext.ConceptSubjects
.Where(x => x.Collection == collection)
.ToDictionaryAsync(x => x.Key, StringComparer.Ordinal, cancellationToken);
foreach (var entry in plan.Entries)
{
if (entry.Change is SubjectChange.Unchanged or SubjectChange.Absent or SubjectChange.Retired)
{
continue;
}
ConceptSubject subject;
if (entry.Change == SubjectChange.Add)
{
subject = new ConceptSubject
{
Collection = collection,
Key = entry.Key,
Title = entry.Title,
RegisteredAtUtc = timeProvider.GetUtcNow()
};
dbContext.ConceptSubjects.Add(subject);
}
else
{
subject = rows[entry.Key];
if (entry.Change == SubjectChange.Retire)
{
subject.RetiredAtUtc = timeProvider.GetUtcNow();
}
else
{
subject.Title = entry.Title;
subject.RetiredAtUtc = null;
}
}
// The audit write saves the subject change in the same SaveChanges.
await auditTrail.RecordAsync(
null,
nameof(ConceptSubject),
subject.Id,
entry.Change.ToString(),
$"actor=host-command collection={collection} key={entry.Key}",
cancellationToken);
}
return plan;
}
private static string ReadString(JsonElement ship, string name, string at)
{
if (ship.ValueKind != JsonValueKind.Object)
{
throw new ManifestRefusedException($"{at}.{name}", "Missing");
}
if (!ship.TryGetProperty(name, out var value))
{
throw new ManifestRefusedException($"{at}.{name}", "Missing");
}
if (value.ValueKind != JsonValueKind.String)
{
throw new ManifestRefusedException($"{at}.{name}", "NotString");
}
try
{
return value.GetString()!;
}
catch (InvalidOperationException)
{
// A lone surrogate escape: not text.
throw new ManifestRefusedException($"{at}.{name}", "BadText");
}
}
}

View file

@ -24,6 +24,23 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using System.Net;
// Host command: registers Mill concept subjects with configuration and the database only; no web app, no migrations.
if (args.Length > 0 && args[0] == "concepts")
{
var host = Host.CreateApplicationBuilder([]);
host.Services.AddDbContext<BifrostDbContext>(options => options.UseNpgsql(BifrostConnectionString(host.Configuration)));
host.Services.AddSingleton(TimeProvider.System);
host.Services.AddScoped<AuditTrailService>();
host.Services.AddScoped<ConceptSubjectRegistry>();
using var commandHost = host.Build();
Environment.ExitCode = await ConceptCommand.RunAsync(args[1..], commandHost.Services, Console.Out);
return;
}
static string BifrostConnectionString(IConfiguration configuration) =>
configuration.GetConnectionString("Bifrost")
?? "Host=127.0.0.1;Database=bifrost;Username=postgres;Password=postgres";
var builder = WebApplication.CreateBuilder(args);
builder.Services
@ -71,10 +88,7 @@ builder.Services.ConfigureHttpJsonOptions(options =>
builder.Services.AddDbContext<BifrostDbContext>(options =>
{
var connectionString = builder.Configuration.GetConnectionString("Bifrost")
?? "Host=127.0.0.1;Database=bifrost;Username=postgres;Password=postgres";
options.UseNpgsql(connectionString);
options.UseNpgsql(BifrostConnectionString(builder.Configuration));
});
builder.Services.AddHttpLogging(options =>
@ -105,6 +119,7 @@ builder.Services.AddScoped<StripeWebhookService>();
builder.Services.AddScoped<HeimdallPatronSupportIntakeService>();
builder.Services.AddScoped<ConceptAppraisalService>();
builder.Services.AddScoped<VoterKeyResolver>();
builder.Services.AddScoped<ConceptSubjectRegistry>();
builder.Services.AddScoped<ReadinessService>();
builder.Services.AddScoped<ApplicationBootstrapper>();
builder.Services.AddSingleton<StartupConfigurationValidator>();