From f1d2c9b707fc87a0f64e1087c1cd2a2331ab12c1 Mon Sep 17 00:00:00 2001 From: Heimdall Date: Wed, 11 Nov 2026 00:52:29 +0000 Subject: [PATCH] desk: fork_too_large is a list-only 422; a body refusal on read or list is request_invalid Co-Authored-By: Claude Sonnet 5.5 --- .../Features/Desk/DeskDocuments.cs | 13 ++++ .../Features/Desk/DeskEndpoints.cs | 15 +++- src/Bifrost.Web/Features/Desk/DeskPorts.cs | 4 + tests/Bifrost.Web.Tests/DeskReadTests.cs | 74 +++++++++++++++++++ 4 files changed, 104 insertions(+), 2 deletions(-) diff --git a/src/Bifrost.Web/Features/Desk/DeskDocuments.cs b/src/Bifrost.Web/Features/Desk/DeskDocuments.cs index 3e84414..aaa652f 100644 --- a/src/Bifrost.Web/Features/Desk/DeskDocuments.cs +++ b/src/Bifrost.Web/Features/Desk/DeskDocuments.cs @@ -151,6 +151,19 @@ public static class DeskRefusals [RequestIdConflict] = (409, "request_id_conflict: this request_id was used for a different body"), }; + // fork_too_large (Libby's ruling) is list-only and names its fork, so it is built here, not read from the table. + // Not a literal const: the pinned vectors' doorRefusals do not name it yet, and the schema test pins literals to them. + public static string ForkTooLarge => "fork_too_large"; + + public static int ForkTooLargeStatus => 422; + + public static DeskRefusal ForkTooLargeDocument(string forkId, int order) => new( + DeskSchemas.Refusal, + ForkTooLarge, + order > 0 + ? $"fork_too_large: fork {forkId} (order {order}) is too large for a page; continue with cursor c{order}" + : $"fork_too_large: fork {forkId} (order {order}) is too large for a page; the list has reached its end"); + public static bool IsKnown(string code) => Table.ContainsKey(code); public static int StatusOf(string code) => Table[code].Status; diff --git a/src/Bifrost.Web/Features/Desk/DeskEndpoints.cs b/src/Bifrost.Web/Features/Desk/DeskEndpoints.cs index d89c947..d09552a 100644 --- a/src/Bifrost.Web/Features/Desk/DeskEndpoints.cs +++ b/src/Bifrost.Web/Features/Desk/DeskEndpoints.cs @@ -169,9 +169,13 @@ public static class DeskEndpoints { await WriteJsonAsync(http, 200, IsDecider(context) ? list : DeskNotes.Strip(list)); } + else if (listed is DeskListOutcome.ForkTooLarge(var largeId, var largeOrder)) + { + await WriteJsonAsync(http, DeskRefusals.ForkTooLargeStatus, DeskRefusals.ForkTooLargeDocument(largeId, largeOrder)); + } else if (listed is DeskListOutcome.NotListed(var listCode) && DoorMayRefuse(listCode)) { - await WriteRefusalAsync(http, listCode); + await WriteRefusalAsync(http, ReadListCode(listCode)); } else { @@ -187,7 +191,7 @@ public static class DeskEndpoints } else if (found is DeskReadOutcome.NotFound(var readCode) && DoorMayRefuse(readCode)) { - await WriteRefusalAsync(http, readCode); + await WriteRefusalAsync(http, ReadListCode(readCode)); } else { @@ -247,6 +251,13 @@ public static class DeskEndpoints code is not (DeskRefusals.Unauthenticated or DeskRefusals.InsufficientScope or DeskRefusals.Forbidden) && DeskRefusals.IsKnown(code); + // A read or list sends no body. A door that refuses one for its body (an `authority` field among them) is + // refusing the request, which is request_invalid; it is not an outage. + private static string ReadListCode(string code) => + code is DeskRefusals.BodyForbiddenField or DeskRefusals.BodyUnknownField or DeskRefusals.BodyInvalid + ? DeskRefusals.RequestInvalid + : code; + // At most `cap` bytes. Null when the body is longer, judged by Content-Length first and then while reading. private static async Task ReadBodyAsync(HttpRequest request, int cap, CancellationToken cancellationToken) { diff --git a/src/Bifrost.Web/Features/Desk/DeskPorts.cs b/src/Bifrost.Web/Features/Desk/DeskPorts.cs index 9f33da5..84ddba4 100644 --- a/src/Bifrost.Web/Features/Desk/DeskPorts.cs +++ b/src/Bifrost.Web/Features/Desk/DeskPorts.cs @@ -45,6 +45,10 @@ public abstract record DeskListOutcome public sealed record Listed(ForkList List) : DeskListOutcome; public sealed record NotListed(string Code) : DeskListOutcome; + + // The next fork is too large for any page: the page holds nothing past the cursor. The client continues with + // cursor c; at Order 0 it has reached the end. List only: no other call can be refused this way. + public sealed record ForkTooLarge(string ForkId, int Order) : DeskListOutcome; } public interface IDeskDoor diff --git a/tests/Bifrost.Web.Tests/DeskReadTests.cs b/tests/Bifrost.Web.Tests/DeskReadTests.cs index 6540a22..38df03f 100644 --- a/tests/Bifrost.Web.Tests/DeskReadTests.cs +++ b/tests/Bifrost.Web.Tests/DeskReadTests.cs @@ -122,6 +122,80 @@ public sealed class DeskReadTests Assert.Equal(404, missing.Status); } + [Fact] + public async Task A_fork_too_large_for_a_page_is_a_422_that_names_the_fork_and_the_cursor_to_continue_with() + { + using var factory = new DeskWebApplicationFactory(); + await factory.EnsureAccountAsync(DeskWebApplicationFactory.Alice, true); + + factory.Door.ListScript = (_, _) => new DeskListOutcome.ForkTooLarge("nibu:question:huge", 7); + var reply = await factory.SendAsync("GET", "/api/desk/nibu/forks", null, ReadToken(factory)); + var doc = JsonNode.Parse(reply.Body)!; + Assert.Equal(422, reply.Status); + Assert.Equal("fork_too_large", (string?)doc["code"]); + var error = (string)doc["error"]!; + Assert.Contains("nibu:question:huge", error); + Assert.Contains("order 7", error); + Assert.Contains("c7", error); + + // At order 0 there is no cursor to continue with: the list has reached its end. + factory.Door.ListScript = (_, _) => new DeskListOutcome.ForkTooLarge("nibu:question:first", 0); + var end = await factory.SendAsync("GET", "/api/desk/nibu/forks", null, ReadToken(factory)); + var endError = (string)JsonNode.Parse(end.Body)!["error"]!; + Assert.Equal(422, end.Status); + Assert.Contains("nibu:question:first", endError); + Assert.Contains("order 0", endError); + Assert.DoesNotContain("cursor c0", endError); + Assert.Contains("end", endError); + } + + [Fact] + public async Task fork_too_large_is_list_only_elsewhere_the_door_saying_it_is_unavailable() + { + using var factory = new DeskWebApplicationFactory(); + await factory.EnsureAccountAsync(DeskWebApplicationFactory.Alice, true); + + factory.Door.ReadScript = (_, _) => new DeskReadOutcome.NotFound("fork_too_large"); + factory.Door.ListScript = (_, _) => new DeskListOutcome.NotListed("fork_too_large"); + foreach (var target in new[] { "/api/desk/nibu/forks", DeskReplay.Target("read", "nibu", "nibu:question:glow-order") }) + { + var reply = await factory.SendAsync("GET", target, null, ReadToken(factory)); + Assert.Equal(503, reply.Status); + } + } + + [Fact] + public async Task A_short_page_with_a_cursor_below_its_last_entry_passes_through_unchanged() + { + using var factory = new DeskWebApplicationFactory(); + await factory.EnsureAccountAsync(DeskWebApplicationFactory.Alice, true); + factory.Door.ListScript = (_, r) => new DeskListOutcome.Listed(new ForkList("gamecult.fork.list.v0", r.Mind, "s1", 3, 0, [], "c2")); + + var reply = await factory.SendAsync("GET", "/api/desk/nibu/forks?limit=10", null, ReadToken(factory)); + + Assert.Equal(200, reply.Status); + Assert.Equal("c2", (string?)JsonNode.Parse(reply.Body)!["next_cursor"]); + } + + [Fact] + public async Task A_door_that_refuses_a_read_or_list_for_a_body_field_such_as_authority_is_request_invalid_not_unavailable() + { + using var factory = new DeskWebApplicationFactory(); + await factory.EnsureAccountAsync(DeskWebApplicationFactory.Alice, true); + + foreach (var code in new[] { "body_forbidden_field", "body_unknown_field", "body_invalid" }) + { + factory.Door.ReadScript = (_, _) => new DeskReadOutcome.NotFound(code); + factory.Door.ListScript = (_, _) => new DeskListOutcome.NotListed(code); + foreach (var target in new[] { "/api/desk/nibu/forks", DeskReplay.Target("read", "nibu", "nibu:question:glow-order") }) + { + var reply = await factory.SendAsync("GET", target, null, ReadToken(factory)); + Assert.Equal(400, reply.Status); + Assert.Equal("request_invalid", (string?)JsonNode.Parse(reply.Body)!["code"]); + } + } + } + [Fact] public async Task Door_failure_on_list_and_read_is_unavailable() {