From 2d2fdeb0e2d6a08a2c070b8c081392ab1f07749b Mon Sep 17 00:00:00 2001 From: Heimdall Date: Wed, 11 Nov 2026 19:20:53 +0000 Subject: [PATCH] Desk door: the wire walker's Closed is the only owner of required keys JsonRequired on DeskAnswerDocument.Answerer and ForkRecord.AnswererCount was a second owner of what Closed already decides at the boundary; nothing the typed vector replays needed is lost (they now go through the door). Every key of the list, entry, record and answer documents, taken from a real CultLib entry, is removed and nulled: a missing key, or a nil where the contract has none, is a door failure. First red: the test that the typed records declare no required keys. Co-Authored-By: Claude Sonnet 5.5 --- .../Features/Desk/DeskDocuments.cs | 10 +- .../UnixSocketDeskDoorRequiredKeysTests.cs | 96 +++++++++++++++++++ 2 files changed, 101 insertions(+), 5 deletions(-) create mode 100644 tests/Bifrost.Web.Tests/UnixSocketDeskDoorRequiredKeysTests.cs diff --git a/src/Bifrost.Web/Features/Desk/DeskDocuments.cs b/src/Bifrost.Web/Features/Desk/DeskDocuments.cs index 6f38e34..5108e12 100644 --- a/src/Bifrost.Web/Features/Desk/DeskDocuments.cs +++ b/src/Bifrost.Web/Features/Desk/DeskDocuments.cs @@ -4,7 +4,7 @@ using System.Text.Json.Serialization; namespace Bifrost.Web.Features.Desk; // The fork-desk documents Bifrost reads or writes, spelled as CultLib's contracts/cultnet/governance/fork-card.md -// (commit 3a51bbab) spells them: snake_case keys, every key written (nil where absent), declared in the +// (commit d3025d3c) spells them: snake_case keys, every key written (nil where absent), declared in the // contract's key order. Bifrost authors only DeskRefusal and DeskUnavailable. AnswerRequest is the client's body; // DeskAnswerDocument, ForkList and AnswerResult are Huginn's documents, typed here so they pass through unchanged. // The card (gamecult.fork.card.v0) is deliberately NOT typed: the Huginn door owns what a reader's card holds (the @@ -72,9 +72,9 @@ public sealed record DeskAnswerDocument( [property: JsonPropertyName("answered_at")] string AnsweredAt, [property: JsonPropertyName("received_at")] string ReceivedAt, [property: JsonPropertyName("claim_in_future")] bool ClaimInFuture, - // Nil in a non-decider's view (Huginn withholds the ids); the key is required, so a missing one is a door fault, - // never a nil Bifrost invented. - [property: JsonPropertyName("answerer"), JsonRequired] string? Answerer, + // Nil in a non-decider's view (Huginn withholds the ids). The key is required, and the door's wire walker (Closed in + // UnixSocketDeskDoor) is the one owner of that: a missing key is a door fault, never a nil Bifrost invented. + [property: JsonPropertyName("answerer")] string? Answerer, [property: JsonPropertyName("authority")] string Authority, [property: JsonPropertyName("request_id")] string RequestId); @@ -83,7 +83,7 @@ public sealed record ForkRecord( [property: JsonPropertyName("fork_id")] string ForkId, [property: JsonPropertyName("options_hash")] string OptionsHash, [property: JsonPropertyName("counted_answer_id")] string? CountedAnswerId, - [property: JsonPropertyName("answerer_count"), JsonRequired] int AnswererCount, + [property: JsonPropertyName("answerer_count")] int AnswererCount, [property: JsonPropertyName("answers")] DeskAnswerDocument[] Answers); public sealed record ForkEntry( diff --git a/tests/Bifrost.Web.Tests/UnixSocketDeskDoorRequiredKeysTests.cs b/tests/Bifrost.Web.Tests/UnixSocketDeskDoorRequiredKeysTests.cs new file mode 100644 index 0000000..ad95a73 --- /dev/null +++ b/tests/Bifrost.Web.Tests/UnixSocketDeskDoorRequiredKeysTests.cs @@ -0,0 +1,96 @@ +using System.Text.Json.Nodes; +using Bifrost.Web.Features.Desk; +using Bifrost.Web.Tests.Support; + +namespace Bifrost.Web.Tests; + +// The wire walker's closed maps (Closed in UnixSocketDeskDoor) are the single owner of "every contract key present with +// its type": the typed records carry no JsonRequired of their own. Each key of each contract document, taken from a real +// CultLib entry, is removed and, where the contract does not allow nil, nulled: every one is a door failure. +public sealed class UnixSocketDeskDoorRequiredKeysTests +{ + private static readonly HashSet Nullable = ["next_cursor", "counted_answer_id", "of", "retracts", "code", "choice", "note", "answerer"]; + + private static JsonObject Level(JsonObject page, string level) => level switch + { + "list" => page, + "entry" => page["forks"]![0]!.AsObject(), + "record" => page["forks"]![0]!["record"]!.AsObject(), + "answer" => page["forks"]![0]!["record"]!["answers"]![0]!.AsObject(), + _ => throw new ArgumentOutOfRangeException(nameof(level)), + }; + + public static TheoryData Keys() + { + var page = DeskServe.PageOf(DeskServe.Entry("entry-open-counted")); + var data = new TheoryData(); + foreach (var level in new[] { "list", "entry", "record", "answer" }) + { + foreach (var key in Level(page, level).Select(p => p.Key)) + { + data.Add(level, key); + } + } + + return data; + } + + private static JsonObject Page() => DeskServe.PageOf(DeskServe.Entry("entry-open-counted")); + + [Theory] + [MemberData(nameof(Keys))] + public async Task A_missing_required_key_is_a_door_failure(string level, string key) + { + var page = Page(); + Level(page, level).Remove(key); + + await Assert.ThrowsAsync(() => DeskServe.ListPayloadAsync(DeskServe.Forks(DeskServe.FromJson(page)))); + } + + [Theory] + [MemberData(nameof(Keys))] + public async Task A_nil_where_the_contract_has_no_nil_is_a_door_failure(string level, string key) + { + var page = Page(); + Level(page, level)[key] = null; + var failing = !Nullable.Contains(key); + + var served = DeskServe.ListPayloadAsync(DeskServe.Forks(DeskServe.FromJson(page))); + + if (failing) + { + await Assert.ThrowsAsync(() => served); + } + else + { + Assert.IsType(await served); + } + } + + [Theory] + [InlineData("answerer")] + [InlineData("answerer_count_is_not_an_answer_key")] + public async Task A_stored_answer_in_an_answered_reply_needs_the_same_keys(string key) + { + var answer = DeskServe.Entry("entry-open-counted")["record"]!["answers"]![0]!.AsObject(); + if (answer.ContainsKey(key)) + { + answer.Remove(key); + } + else + { + answer[key] = 1; + } + + await Assert.ThrowsAsync(() => DeskServe.AnswerPayloadAsync(DeskServe.Answered(DeskServe.FromJson(answer), "n1"))); + } + + [Fact] + public void The_typed_records_declare_no_required_keys_of_their_own() + { + var typed = new[] { typeof(DeskAnswerDocument), typeof(ForkRecord), typeof(ForkEntry), typeof(ForkList), typeof(AnswerResult) }; + var required = typed.SelectMany(t => t.GetProperties()).Where(p => p.GetCustomAttributes(typeof(System.Text.Json.Serialization.JsonRequiredAttribute), true).Length > 0); + + Assert.Empty(required.Select(p => p.DeclaringType!.Name + "." + p.Name)); + } +}