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 <noreply@anthropic.com>
This commit is contained in:
parent
45ec27188c
commit
f1d2c9b707
4 changed files with 104 additions and 2 deletions
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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<byte[]?> ReadBodyAsync(HttpRequest request, int cap, CancellationToken cancellationToken)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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<Order>; 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
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue