Mill votes ranked read: cursor carries the requested filter, not the resolved address; tie-break and no-address paging tests

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Heimdall 2026-11-10 21:06:55 +00:00
commit d84614d7fc
4 changed files with 52 additions and 12 deletions

View file

@ -204,15 +204,16 @@ public sealed class ConceptAppraisalService(
// The ranked read: live subjects of one collection ordered by net (up minus down), total (up plus down) or
// registration time, largest first, ties by key ascending, so the order is total and a cursor names a position.
// The cursor is keyset: the next page is whatever sorts after the last row's (value, key), so a vote landing
// between pages can move rows but never makes a page repeat the cursor's own row. With a network tag,
// subjects whose stored tag equals it are left out ("not yet voted by this network"); the tag is only compared
// between pages can move rows but never makes a page repeat the cursor's own row. With unvoted and a client
// address, subjects whose stored tag equals the client's are left out (no address filters nothing) ("not yet voted by this network"); the tag is only compared
// inside the query and is never returned. Null when the collection has no live subject.
public async Task<RankedPage?> GetRankedAsync(
string collection,
RankOrder order,
int limit,
RankCursor? after,
IPAddress? unvotedBy,
bool unvoted,
IPAddress? client,
CancellationToken cancellationToken)
{
var live = await dbContext.ConceptSubjects.AsNoTracking()
@ -232,10 +233,10 @@ public sealed class ConceptAppraisalService(
.ToListAsync(cancellationToken);
var voted = new HashSet<Guid>();
if (unvotedBy is not null)
if (unvoted && client is not null)
{
var opts = options.Value;
var tag = NetworkPrefix.Tag(unvotedBy, opts.IPv4PrefixLength, opts.IPv6PrefixLength, opts.NetworkTagSecret);
var tag = NetworkPrefix.Tag(client, opts.IPv4PrefixLength, opts.IPv6PrefixLength, opts.NetworkTagSecret);
voted = (await dbContext.ConceptAppraisals.AsNoTracking()
.Where(x => ids.Contains(x.SubjectId) && x.NetworkTag == tag)
.Select(x => x.SubjectId)
@ -263,7 +264,7 @@ public sealed class ConceptAppraisalService(
var page = rows.Take(limit).ToList();
var next = rows.Count > limit
? new RankCursor(order, unvotedBy is not null, page[^1].Value, page[^1].Key).Encode()
? new RankCursor(order, unvoted, page[^1].Value, page[^1].Key).Encode()
: null;
return new RankedPage(page.Select(r => new RankedSubject(r.Key, r.Title, r.Tally)).ToList(), next);
}

View file

@ -94,7 +94,7 @@ public static class ConceptTallyExport
}
var page = await appraisals.GetRankedAsync(
collection, order, limit, cursor, unvoted ? http.Connection.RemoteIpAddress : null, cancellationToken);
collection, order, limit, cursor, unvoted, http.Connection.RemoteIpAddress, cancellationToken);
if (page is null)
{
return Results.NotFound();

View file

@ -1,21 +1,18 @@
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.WebUtilities;
namespace Bifrost.Web.Features.ConceptVotes;
// The opaque cursor of the ranked read: the last row's sort value and key, plus the order and filter it was issued
// for, so a cursor cannot be replayed under another order or filter. Text "1|order|u|value|key", base64url without
// padding. Not signed: it names a position in public data and grants nothing. Decoding is strict and canonical (a
// padding; the length cap only bounds decoding work. Not signed: it names a position in public data and grants nothing. Decoding is strict and canonical (a
// cursor that does not re-encode to the same text is refused), and every field is re-validated, so no cursor text
// reaches a query unparsed.
public sealed record RankCursor(RankOrder Order, bool Unvoted, long Value, string Key)
{
public const int MaxLength = 200;
private static readonly Regex Alphabet = new(@"^[A-Za-z0-9_-]+\z", RegexOptions.CultureInvariant);
public string Encode() =>
WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(
string.Create(CultureInfo.InvariantCulture, $"1|{(int)Order}|{(Unvoted ? 1 : 0)}|{Value}|{Key}")));
@ -23,7 +20,7 @@ public sealed record RankCursor(RankOrder Order, bool Unvoted, long Value, strin
public static bool TryDecode(string? text, out RankCursor cursor)
{
cursor = null!;
if (string.IsNullOrEmpty(text) || text.Length > MaxLength || !Alphabet.IsMatch(text))
if (string.IsNullOrEmpty(text) || text.Length > MaxLength)
{
return false;
}

View file

@ -99,6 +99,48 @@ public sealed class ConceptRankedExportTests : IClassFixture<ConceptRankedExport
Assert.Equal(3, pages);
}
[Fact]
public async Task Ties_break_by_ordinal_key_even_when_keys_hold_hyphens_and_were_registered_out_of_order()
{
var c = "c" + Guid.NewGuid().ToString("N");
string[] ordinal = ["a-b", "a-c", "ab", "b", "b-a"];
using (var scope = _factory.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<BifrostDbContext>();
foreach (var key in ordinal.Reverse())
{
db.ConceptSubjects.Add(new ConceptSubject { Collection = c, Key = key, Title = key, RegisteredAtUtc = Epoch });
}
await db.SaveChangesAsync();
}
var walked = new List<string>();
string? cursor = null;
do
{
var body = await GetOk(c, "?limit=1" + (cursor is null ? "" : "&cursor=" + cursor));
walked.AddRange(Keys(body));
cursor = body.GetProperty("next_cursor").ValueKind == JsonValueKind.Null ? null : body.GetProperty("next_cursor").GetString();
}
while (cursor is not null && walked.Count < 10);
Assert.Equal(ordinal, walked);
Assert.Equal(ordinal, Keys(await GetOk(c, "?sort=newest")));
}
[Fact]
public async Task Unvoted_without_a_client_address_still_pages()
{
var c = await Seed();
var first = await GetOk(c, "?unvoted=network&limit=2");
var second = await GetOk(c, "?unvoted=network&limit=2&cursor=" + first.GetProperty("next_cursor").GetString());
Assert.Equal(["bravo", "alpha"], Keys(first));
Assert.Equal(["charlie", "delta"], Keys(second));
}
[Fact]
public async Task A_vote_between_pages_never_repeats_the_cursors_own_row()
{