From 338d476eeb56004508f08da2f38d09278451ff55 Mon Sep 17 00:00:00 2001 From: Heimdall Date: Wed, 11 Nov 2026 19:22:46 +0000 Subject: [PATCH] Desk door: close Huginn's Answered, Forks and Fork wrapper maps DeskResponse declares Answered { answer, counted_answer_id, as_of: u64 }, Forks { list, as_of: u64 } and Fork { entry, as_of: u64 } (desk_wire.rs at b299d6f7; the golden replies carry exactly those keys). The door now requires as_of as an unsigned integer and refuses any other key, like every other contract map. First red: the six wrapper tests (missing, nil, negative, text, bool, array, object as_of; extra keys). Co-Authored-By: Claude Sonnet 5.5 --- .../Features/Desk/UnixSocketDeskDoor.cs | 12 +++- .../UnixSocketDeskDoorWrapperTests.cs | 70 +++++++++++++++++++ 2 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 tests/Bifrost.Web.Tests/UnixSocketDeskDoorWrapperTests.cs diff --git a/src/Bifrost.Web/Features/Desk/UnixSocketDeskDoor.cs b/src/Bifrost.Web/Features/Desk/UnixSocketDeskDoor.cs index 6891a66..3f01f99 100644 --- a/src/Bifrost.Web/Features/Desk/UnixSocketDeskDoor.cs +++ b/src/Bifrost.Web/Features/Desk/UnixSocketDeskDoor.cs @@ -14,7 +14,7 @@ namespace Bifrost.Web.Features.Desk; // `unavailable`; this is the one type the client throws for every one of those, so a failure is never an empty page. public sealed class DeskDoorException(string message, Exception? inner = null) : Exception(message, inner); -// The production IDeskDoor: a client of Huginn's desk door (Huginn docs/desk-feed-door.md at 79d954c). One new +// The production IDeskDoor: a client of Huginn's desk door (Huginn docs/desk-feed-door.md at b299d6f7). One new // connection per call, one request frame, one reply frame, closed after, all inside one deadline. The client holds no // connection state, so a dead socket fails only the calls addressed to that Mind. // @@ -22,8 +22,9 @@ public sealed class DeskDoorException(string message, Exception? inner = null) : // payload = standard base64 of the named MessagePack of Huginn's externally tagged DeskRequest / DeskResponse // // The reply is decoded by key, never by position, and strictly: every contract document is a closed map whose keys are -// all present with the right type, a key written twice anywhere is a failure, and so is a float, a binary or an -// extension value (the contract has none; integers keep every digit). The card inside an entry is not read beyond +// all present with the right type (the variants' own maps too: as_of is an unsigned integer, as Huginn's DeskResponse +// declares), a key written twice anywhere is a failure, and so is a float, a binary or an extension value (the +// contract has none; integers keep every digit). The card inside an entry is not read beyond // being a map: it stays an opaque JsonElement, so whatever keys and integers Huginn wrote leave unchanged. public sealed partial class UnixSocketDeskDoor : IDeskDoor { @@ -79,6 +80,7 @@ public sealed partial class UnixSocketDeskDoor : IDeskDoor switch (variant) { case "Answered": + Closed(value, "Answered", string.Empty, ("answer", Kind.Map), ("counted_answer_id", Kind.TextOrNull), ("as_of", Kind.Unsigned)); var answer = AnswerDocument(value, "answer"); // Huginn's reply carries no card: a stale_fork answer gets the current card from the edge's read. @@ -113,6 +115,7 @@ public sealed partial class UnixSocketDeskDoor : IDeskDoor switch (variant) { case "Fork": + Closed(value, "Fork", string.Empty, ("entry", Kind.Map), ("as_of", Kind.Unsigned)); return new DeskReadOutcome.Found(Entry(Required(value, "entry"), "entry")); case "NotStored": var code = RefusalCode(value); @@ -148,6 +151,7 @@ public sealed partial class UnixSocketDeskDoor : IDeskDoor switch (variant) { case "Forks": + Closed(value, "Forks", string.Empty, ("list", Kind.Map), ("as_of", Kind.Unsigned)); var list = List(Required(value, "list")); return list.Mind == context.Mind ? new DeskListOutcome.Listed(list) @@ -185,6 +189,7 @@ public sealed partial class UnixSocketDeskDoor : IDeskDoor TextOrNull, Flag, Count, + Unsigned, Map, Items, } @@ -212,6 +217,7 @@ public sealed partial class UnixSocketDeskDoor : IDeskDoor Kind.TextOrNull => v.ValueKind is JsonValueKind.String or JsonValueKind.Null, Kind.Flag => v.ValueKind is JsonValueKind.True or JsonValueKind.False, Kind.Count => v.ValueKind == JsonValueKind.Number && v.TryGetInt32(out var n) && n >= 0, + Kind.Unsigned => v.ValueKind == JsonValueKind.Number && v.TryGetUInt64(out _), Kind.Map => v.ValueKind == JsonValueKind.Object, _ => v.ValueKind == JsonValueKind.Array, }; diff --git a/tests/Bifrost.Web.Tests/UnixSocketDeskDoorWrapperTests.cs b/tests/Bifrost.Web.Tests/UnixSocketDeskDoorWrapperTests.cs new file mode 100644 index 0000000..b82590e --- /dev/null +++ b/tests/Bifrost.Web.Tests/UnixSocketDeskDoorWrapperTests.cs @@ -0,0 +1,70 @@ +using System.Text.Json.Nodes; +using MessagePack; +using Bifrost.Web.Features.Desk; +using Bifrost.Web.Tests.Support; + +namespace Bifrost.Web.Tests; + +// Huginn's DeskResponse variants are maps with a fixed key set (desk_wire.rs): Answered { answer, counted_answer_id, +// as_of: u64 }, Forks { list, as_of: u64 }, Fork { entry, as_of: u64 }. The door closes them like every other contract map: +// every key present, no other key, as_of an unsigned integer. (Huginn's own golden replies, list-empty and list-counts, +// carry exactly list and as_of.) +public sealed class UnixSocketDeskDoorWrapperTests +{ + private static string Entry => DeskServe.Entry("entry-open-counted").ToJsonString(DeskJson.Options); + + private static string Page => DeskServe.PageOf(DeskServe.Entry("entry-open-counted")).ToJsonString(DeskJson.Options); + + private static string Answer => DeskServe.Entry("entry-open-counted")["record"]!["answers"]![0]!.ToJsonString(DeskJson.Options); + + // variant, call, and a function from the wrapper's tail to the reply JSON. + public static TheoryData Variants() => ["Forks", "Fork", "Answered"]; + + private static string Reply(string variant, string tail) => variant switch + { + "Forks" => "{\"Forks\":{\"list\":" + Page + tail + "}}", + "Fork" => "{\"Fork\":{\"entry\":" + Entry + tail + "}}", + _ => "{\"Answered\":{\"answer\":" + Answer + ",\"counted_answer_id\":\"n1\"" + tail + "}}", + }; + + private static async Task Serve(string variant, byte[] payload) => variant switch + { + "Forks" => await DeskServe.ListPayloadAsync(payload), + "Fork" => await DeskServe.ReadPayloadAsync(payload), + _ => await DeskServe.AnswerPayloadAsync(payload), + }; + + private static byte[] Pack(string json) => MessagePackSerializer.ConvertFromJson(json); + + [Theory] + [MemberData(nameof(Variants))] + public async Task A_wrapper_with_as_of_alone_beside_its_value_decodes_and_as_of_may_be_any_u64(string variant) + { + await Serve(variant, Pack(Reply(variant, ",\"as_of\":0"))); + await Serve(variant, Pack(Reply(variant, ",\"as_of\":4294967296"))); + await Serve(variant, Pack(Reply(variant, ",\"as_of\":18446744073709551615"))); + } + + [Theory] + [MemberData(nameof(Variants))] + public async Task A_wrapper_without_as_of_or_with_one_that_is_not_an_unsigned_integer_is_a_door_failure(string variant) + { + foreach (var tail in new[] + { + "", ",\"as_of\":null", ",\"as_of\":-1", ",\"as_of\":\"1\"", ",\"as_of\":true", ",\"as_of\":1.5", ",\"as_of\":[1]", ",\"as_of\":{}", + }) + { + await Assert.ThrowsAsync(() => Serve(variant, Pack(Reply(variant, tail)))); + } + } + + [Theory] + [MemberData(nameof(Variants))] + public async Task A_wrapper_with_a_key_beyond_the_variants_declared_ones_is_a_door_failure(string variant) + { + foreach (var tail in new[] { ",\"as_of\":1,\"viewer\":\"x\"", ",\"viewer\":1,\"as_of\":1", ",\"as_of\":1,\"as_of_extra\":null" }) + { + await Assert.ThrowsAsync(() => Serve(variant, Pack(Reply(variant, tail)))); + } + } +}