diff --git a/src/Bifrost.Web/Features/Desk/DeskDocuments.cs b/src/Bifrost.Web/Features/Desk/DeskDocuments.cs index 942ad24..3e84414 100644 --- a/src/Bifrost.Web/Features/Desk/DeskDocuments.cs +++ b/src/Bifrost.Web/Features/Desk/DeskDocuments.cs @@ -168,3 +168,14 @@ public static class DeskUnavailables "unavailable: the outcome is not confirmed, try again with the same request_id", retryAfterSeconds); } + +// Notes are returned only to deciders. For everyone else Bifrost nils every note at the door: the key stays written +// (every key written, nil where absent), so the stripped body is still the contract's shape. Cards, outcomes, codes +// and counts are untouched; Huginn returns the full record. +public static class DeskNotes +{ + public static ForkEntry Strip(ForkEntry entry) => + entry with { Record = entry.Record with { Answers = [.. entry.Record.Answers.Select(a => a with { Note = null })] } }; + + public static ForkList Strip(ForkList list) => list with { Forks = [.. list.Forks.Select(Strip)] }; +} diff --git a/src/Bifrost.Web/Features/Desk/DeskEndpoints.cs b/src/Bifrost.Web/Features/Desk/DeskEndpoints.cs index e4e54f0..d89c947 100644 --- a/src/Bifrost.Web/Features/Desk/DeskEndpoints.cs +++ b/src/Bifrost.Web/Features/Desk/DeskEndpoints.cs @@ -167,7 +167,7 @@ public static class DeskEndpoints var listed = await door.ListAsync(mind.Socket, context, listRequest! with { Mind = mind.Id }, cancellationToken); if (listed is DeskListOutcome.Listed(var list)) { - await WriteJsonAsync(http, 200, list); + await WriteJsonAsync(http, 200, IsDecider(context) ? list : DeskNotes.Strip(list)); } else if (listed is DeskListOutcome.NotListed(var listCode) && DoorMayRefuse(listCode)) { @@ -183,7 +183,7 @@ public static class DeskEndpoints var found = await door.ReadAsync(mind.Socket, context, forkId, cancellationToken); if (found is DeskReadOutcome.Found(var entry)) { - await WriteJsonAsync(http, 200, entry); + await WriteJsonAsync(http, 200, IsDecider(context) ? entry : DeskNotes.Strip(entry)); } else if (found is DeskReadOutcome.NotFound(var readCode) && DoorMayRefuse(readCode)) { @@ -238,6 +238,10 @@ public static class DeskEndpoints } } + // A note is a decider's own words: only a caller on the admin-set list reads them (visibility ruling c). + private static bool IsDecider(DeskDoorContext context) => + context.Deciders.Contains(context.Subject, StringComparer.Ordinal); + // Codes the door may hand back for nothing stored. The three the edge alone judges are never the door's. private static bool DoorMayRefuse(string code) => code is not (DeskRefusals.Unauthenticated or DeskRefusals.InsufficientScope or DeskRefusals.Forbidden) && diff --git a/tests/Bifrost.Web.Tests/DeskReadTests.cs b/tests/Bifrost.Web.Tests/DeskReadTests.cs index 01a76b4..6540a22 100644 --- a/tests/Bifrost.Web.Tests/DeskReadTests.cs +++ b/tests/Bifrost.Web.Tests/DeskReadTests.cs @@ -166,4 +166,99 @@ public sealed class DeskReadTests Assert.Equal(["list", "read", "answer"], factory.Door.Calls.Select(c => c.Kind).ToArray()); Assert.All(factory.Door.Calls, c => Assert.Equal(DeskWebApplicationFactory.DecidersOf("nibu"), c.Context.Deciders)); } + private const string Secret = "the decider's own words"; + + private static DeskAnswerDocument Doc(string id, string outcome, string? of, string? retracts, string? note) => new( + DeskSchemas.DeskAnswer, id, DeskReplay.ChatFork, outcome, of, retracts, null, "h", note == null ? null : "a", note, + "2026-11-12T19:00:00Z", "2026-11-12T19:00:01Z", false, DeskWebApplicationFactory.Alice, "Steward", "r-" + id); + + // Counted, Revision, Retraction and Refused documents, every one carrying the decider's note. + private static ForkEntry NotedEntry() + { + var entry = DeskReplay.Entry(DeskReplay.Card(DeskReplay.ChatFork)); + DeskAnswerDocument[] answers = + [ + Doc("n1", "Counted", null, null, Secret), + Doc("n2", "Revision", "n1", null, Secret + " 2"), + Doc("n3", "Retraction", null, "n2", Secret + " 3"), + Doc("n4", "Refused", null, null, Secret + " 4"), + ]; + return entry with { Record = entry.Record with { CountedAnswerId = "n1", Answers = answers } }; + } + + private static bool AnyNote(JsonNode? node) => node switch + { + JsonObject o => o.Any(kv => (kv.Key == "note" && kv.Value is not null) || AnyNote(kv.Value)), + JsonArray a => a.Any(AnyNote), + _ => false, + }; + + private static async Task<(string Read, string List)> BodiesAsync(DeskWebApplicationFactory factory, string account) + { + var entry = NotedEntry(); + factory.Door.ReadScript = (_, _) => new DeskReadOutcome.Found(entry); + factory.Door.ListScript = (_, r) => new DeskListOutcome.Listed( + new ForkList("gamecult.fork.list.v0", r.Mind, "s1", 1, 0, [entry, entry], null)); + var token = factory.Token(account, "bifrost-desk", "desk.read"); + var read = await factory.SendAsync("GET", DeskReplay.Target("read", "gamecult-chat", DeskReplay.ChatFork), null, token); + var list = await factory.SendAsync("GET", DeskReplay.Target("list", "gamecult-chat", null), null, token); + Assert.Equal(200, read.Status); + Assert.Equal(200, list.Status); + return (System.Text.Encoding.UTF8.GetString(read.Body), System.Text.Encoding.UTF8.GetString(list.Body)); + } + + [Fact] + public async Task A_non_decider_reads_and_lists_no_notes_anywhere_and_the_body_keeps_the_contract_shape() + { + using var factory = new DeskWebApplicationFactory(); + await factory.EnsureAccountAsync(DeskWebApplicationFactory.Member, true); + Assert.DoesNotContain(DeskWebApplicationFactory.Member, DeskWebApplicationFactory.DecidersOf("gamecult-chat")); + + var (read, list) = await BodiesAsync(factory, DeskWebApplicationFactory.Member); + + Assert.DoesNotContain("decider's own words", read); + Assert.DoesNotContain("decider's own words", list); + Assert.False(AnyNote(JsonNode.Parse(read))); + Assert.False(AnyNote(JsonNode.Parse(list))); + // Outcomes and cards stay; the key stays written as nil, so the body is still the contract's shape. + var expected = JsonSerializer.SerializeToNode(NotedEntry() with + { + Record = NotedEntry().Record with { Answers = [.. NotedEntry().Record.Answers.Select(a => a with { Note = null })] }, + }, DeskJson.Options); + Assert.True(DeskVectors.SameShape(JsonNode.Parse(read), expected)); + var answers = JsonNode.Parse(read)!["record"]!["answers"]!.AsArray(); + Assert.Equal(new[] { "Counted", "Revision", "Retraction", "Refused" }, answers.Select(a => (string)a!["outcome"]!).ToArray()); + Assert.All(answers, a => Assert.True(a!.AsObject().ContainsKey("note"))); + Assert.Equal(2, JsonNode.Parse(list)!["forks"]!.AsArray().Count); + } + + [Fact] + public async Task A_decider_reads_and_lists_the_notes() + { + using var factory = new DeskWebApplicationFactory(); + await factory.EnsureAccountAsync(DeskWebApplicationFactory.Alice, true); + + var (read, list) = await BodiesAsync(factory, DeskWebApplicationFactory.Alice); + + Assert.Contains(Secret + " 3", read); + Assert.Equal(8, JsonNode.Parse(list)!["forks"]!.AsArray().Sum(f => f!["record"]!["answers"]!.AsArray().Count(a => a!["note"] is not null))); + Assert.True(DeskVectors.SameShape(JsonNode.Parse(read), JsonSerializer.SerializeToNode(NotedEntry(), DeskJson.Options))); + } + + [Fact] + public async Task Decider_status_is_per_mind_the_same_account_is_stripped_where_it_is_not_on_the_list() + { + using var factory = new DeskWebApplicationFactory(); + await factory.EnsureAccountAsync(DeskWebApplicationFactory.Member, true); + Assert.Contains(DeskWebApplicationFactory.Member, DeskWebApplicationFactory.DecidersOf("nibu")); + factory.Door.ListScript = (_, r) => new DeskListOutcome.Listed( + new ForkList("gamecult.fork.list.v0", r.Mind, "s1", 1, 0, [NotedEntry()], null)); + var token = factory.Token(DeskWebApplicationFactory.Member, "bifrost-desk", "desk.read"); + + var onNibu = await factory.SendAsync("GET", "/api/desk/nibu/forks", null, token); + var onChat = await factory.SendAsync("GET", "/api/desk/gamecult-chat/forks", null, token); + + Assert.True(AnyNote(JsonNode.Parse(onNibu.Body))); + Assert.False(AnyNote(JsonNode.Parse(onChat.Body))); + } }