C#: receipt writes need a reliable, non-sequenced channel; a refusal without a record key or schema id carries a null detail value

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Libby 2026-11-08 17:01:59 +00:00
commit 40b34295e3
4 changed files with 58 additions and 3 deletions

View file

@ -3581,14 +3581,14 @@ namespace GameCult.Networking
}
/// <summary>
/// A put or delete that arrived on an unreliable channel of a receipt session (write-receipt.md, Reliable channel only):
/// A put or delete that arrived on an unreliable or sequenced channel of a receipt session (write-receipt.md, Reliable channel only):
/// answered <c>message_invalid</c>, details <c>channel</c>, fixed text, carrying the write's id, and not applied. True when
/// the frame was such a write and has been answered here.
/// </summary>
internal bool RefuseUnreliableWrite(ICultNetSchemaMessage message, DeliveryMethod method)
{
if (WriteReceipts == null) return false;
if (method is DeliveryMethod.ReliableOrdered or DeliveryMethod.ReliableUnordered or DeliveryMethod.ReliableSequenced) return false;
if (method is DeliveryMethod.ReliableOrdered or DeliveryMethod.ReliableUnordered) return false;
var messageId = message switch
{
CultNetDocumentPutRawMessage put => put.MessageId,

View file

@ -90,6 +90,8 @@ namespace GameCult.Networking
public static CultNetRememberedRefusal Of(CultNetErrorMessage error) =>
new(error.Code, error.Details != null, error.Details?.Field, error.RoutingHint);
private static string? NullIfEmpty(string value) => value.Length == 0 ? null : value;
/// <summary>The refusal, in fixed text, naming the write's own record key or schema id where its details do.</summary>
public CultNetErrorMessage ToError(string schemaId, string recordKey) => new()
{
@ -105,7 +107,7 @@ namespace GameCult.Networking
? new CultNetErrorDetails
{
Field = Field,
Value = Field switch { "recordKey" => recordKey, "schemaId" => schemaId, _ => null }
Value = Field switch { "recordKey" => NullIfEmpty(recordKey), "schemaId" => NullIfEmpty(schemaId), _ => null }
}
: null
};

View file

@ -50,6 +50,9 @@ namespace GameCult.Networking.Tests
public void SendUnreliable<T>(T message) where T : ICultNetSchemaMessage =>
_peer.Send(CultNetSchemaMessageSerialization.Serialize(message), DeliveryMethod.Unreliable);
public void Send<T>(T message, DeliveryMethod method) where T : ICultNetSchemaMessage =>
_peer.Send(CultNetSchemaMessageSerialization.Serialize(message), method);
public void SendRaw(byte[] bytes) => _peer.Send(bytes, DeliveryMethod.ReliableOrdered);
public T Next<T>() where T : ICultNetSchemaMessage
@ -202,6 +205,36 @@ namespace GameCult.Networking.Tests
await WaitFor(() => running.Cache.Get<NetworkSchemaNote>(KeyA)?.Text == "kept");
}
// write-receipt.md, Reliable channel only: reliable and not sequenced. Reliable ordered and unordered qualify; reliable
// sequenced does not, and nor does any unreliable method.
[TestCase(DeliveryMethod.ReliableSequenced, true)]
[TestCase(DeliveryMethod.Sequenced, true)]
[TestCase(DeliveryMethod.Unreliable, true)]
[TestCase(DeliveryMethod.ReliableUnordered, false)]
[TestCase(DeliveryMethod.ReliableOrdered, false)]
public async Task OnlyAReliableNonSequencedChannelCarriesAWriteOfAReceiptPeer(DeliveryMethod method, bool refused)
{
using var running = Running.Start();
using var client = running.Connect();
client.Send(HelloOf(BothFrames));
client.Next<CultNetHelloMessage>();
client.Send(running.PutOf("c1", KeyA, "text"), method);
if (refused)
{
var error = client.Next<CultNetErrorV1Message>();
Assert.That(error.Details!.Field, Is.EqualTo("channel"));
Assert.That(error.MessageId, Is.EqualTo("c1"));
Assert.That(running.Cache.Get<NetworkSchemaNote>(KeyA), Is.Null);
}
else
{
Assert.That(client.Next<CultNetWriteAdmittedMessage>().MessageId, Is.EqualTo("c1"));
await WaitFor(() => running.Cache.Get<NetworkSchemaNote>(KeyA)?.Text == "text");
}
}
[Test]
public void ADeleteOnAnUnreliableChannelOfAReceiptPeerIsRefusedWithItsId()
{

View file

@ -1205,5 +1205,25 @@ namespace GameCult.Networking.Tests
Assert.That(routedAgain.RoutingHint!.Reason, Is.EqualTo("not_primary"));
Assert.That(routedAgain.Error, Is.EqualTo(CultNetDatabaseServer.RoutingRefusalText));
}
// A write that carries no record key or schema id has none to echo: the detail value is null, not an empty string.
[Test]
public void AReplayedRefusalOfAWriteWithoutTheNamedFieldCarriesANullValue()
{
var session = new CultNetWriteReceiptSession();
var sent = new List<ICultNetSchemaMessage>();
CultNetReceiptedWrite Write() => new(session, sent.Add, "delete", "m1", null, null, null);
var first = Write();
Assert.That(first.Begin(), Is.True);
first.Refused(DeniedWith("ignored", "reason"));
Assert.That(Write().Begin(), Is.False);
foreach (var message in sent)
{
var error = (CultNetErrorV1Message)message;
Assert.That(error.Details!.Field, Is.EqualTo("recordKey"));
Assert.That(error.Details.Value, Is.Null);
}
}
}
}