C#: database server answers writes with write_admitted / error.v1 (write-receipt spec)
Some checks failed
CultMesh QUIC native / Build win32-x64 (pull_request) Has been skipped
CultMesh QUIC native / Build linux-x64 (pull_request) Has been skipped
CultMesh QUIC native / Release native binaries (pull_request) Has been skipped
CultMesh portability and performance / ubuntu-latest reactive semantics and leases (pull_request) Failing after 5m46s
CultMesh QUIC native / Verify committed binaries match SHA256SUMS (pull_request) Successful in 36s

Opt-in per connection by the first hello (or first write); the server answers a
capable hello with its own. A put or delete commits, then gets write_admitted under
its id; every refusal is error.v1 with the id (an unreadable frame carries the id
when it still holds one). (actor, messageId) dedupe is bound to the content hash and
asked only after the write authority grants; same id other content is message_invalid.
The shard write forwarder says hello, waits for the primary's receipt, and the server
relays only what the primary answered under the original id.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Libby 2026-11-08 14:04:37 +00:00
commit b0ea18371f
7 changed files with 985 additions and 38 deletions

View file

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using GameCult.Caching;
using MessagePack;
namespace GameCult.Networking
@ -26,7 +27,8 @@ namespace GameCult.Networking
if (payload == null) writer.WriteNil();
else writer.Write(payload);
writer.Flush();
return SHA256.HashData(buffer.WrittenSpan);
using var sha = SHA256.Create();
return sha.ComputeHash(buffer.WrittenSpan.ToArray());
}
}
@ -101,4 +103,176 @@ namespace GameCult.Networking
}
}
}
/// <summary>
/// The messageId of a schema frame that does not decode (write-receipt.md, rule 3: a refusal carries the write's id whenever
/// the frame is readable enough to find it).
/// </summary>
internal static class CultNetReadableMessageId
{
/// <summary>The frame's top-level string <c>messageId</c>, or null when the bytes are not a map holding one.</summary>
public static string? From(byte[] payload)
{
try
{
var reader = new MessagePackReader(payload);
var entries = reader.ReadMapHeader();
for (var index = 0; index < entries; index++)
{
var key = reader.NextMessagePackType == MessagePackType.String ? reader.ReadString() : null;
if (key == "messageId" && reader.NextMessagePackType == MessagePackType.String)
{
var id = reader.ReadString();
return string.IsNullOrEmpty(id) ? null : id;
}
if (key == null) reader.Skip();
reader.Skip();
}
}
catch (Exception)
{
// Not a readable map: no id.
}
return null;
}
}
/// <summary>A resend the receipt session answered without applying it again, or refused as a reused id.</summary>
internal sealed class CultNetWriteResendException : Exception
{
public CultNetWriteResendException(CultNetWriteReceiptSession.Begin outcome)
: base("write answered from the connection's remembered writes")
{
Outcome = outcome;
}
public CultNetWriteReceiptSession.Begin Outcome { get; }
}
/// <summary>
/// One write on a receipt-capable connection: its dedupe claim and its single answer (write-receipt.md, rules 1 to 5).
/// </summary>
internal sealed class CultNetReceiptedWrite
{
private readonly CultNetWriteReceiptSession _session;
private readonly Action<ICultNetSchemaMessage> _send;
private readonly string _operation;
private readonly string _messageId;
private readonly string _schemaId;
private readonly string _recordKey;
private readonly byte[]? _payload;
private bool _claimed;
private bool _answered;
public CultNetReceiptedWrite(
CultNetWriteReceiptSession session,
Action<ICultNetSchemaMessage> send,
string operation,
string? messageId,
string? schemaId,
string? recordKey,
byte[]? payload)
{
_session = session;
_send = send;
_operation = operation;
_messageId = messageId ?? string.Empty;
_schemaId = schemaId ?? string.Empty;
_recordKey = recordKey ?? string.Empty;
_payload = payload;
}
/// <summary>
/// Wraps the server's authorization so the connection's remembered writes are consulted only after the actor is
/// authorized: authorize first, then claim the id. A resend of an admitted write throws
/// <see cref="CultNetWriteResendException"/> instead of applying it again.
/// </summary>
public Action<string, CultRecordKey> Guard(Action<string, CultRecordKey> authorize) => (schemaId, key) =>
{
authorize(schemaId, key);
if (_messageId.Length == 0) return;
var outcome = _session.TryBegin(_messageId, CultNetWriteContentHash.Compute(_operation, _schemaId, _recordKey, _payload));
if (outcome == CultNetWriteReceiptSession.Begin.New)
{
_claimed = true;
return;
}
throw new CultNetWriteResendException(outcome);
};
/// <summary>The write committed: remember it and send its one receipt.</summary>
public void Admitted()
{
if (_answered) return;
_answered = true;
if (_messageId.Length > 0) _session.Admitted(_messageId);
_send(new CultNetWriteAdmittedMessage
{
MessageId = _messageId,
Operation = _operation,
SchemaId = _schemaId,
RecordKey = _recordKey
});
}
/// <summary>The write was refused: forget its claim so a repeat is judged afresh, and send its one error.v1.</summary>
public void Refused(CultNetErrorMessage error)
{
if (_answered) return;
_answered = true;
Release();
_send(CultNetErrorV1Message.From(error, _messageId));
}
/// <summary>No answer is owed or possible (a forward the primary never answered): forget the claim, send nothing.</summary>
public void Unanswered()
{
if (_answered) return;
_answered = true;
Release();
}
/// <summary>Answers a resend per rule 5.</summary>
public void Resent(CultNetWriteResendException resend)
{
switch (resend.Outcome)
{
case CultNetWriteReceiptSession.Begin.AdmittedBefore:
_answered = true;
_send(new CultNetWriteAdmittedMessage
{
MessageId = _messageId,
Operation = _operation,
SchemaId = _schemaId,
RecordKey = _recordKey
});
break;
case CultNetWriteReceiptSession.Begin.InFlight:
// The first attempt is still being applied and will answer this id.
_answered = true;
break;
case CultNetWriteReceiptSession.Begin.DifferentContent:
Refused(CultNetErrorMessage.ForMessageInvalid("messageId", "the message id was already used for different content"));
break;
default:
Refused(new CultNetErrorMessage { Error = "too many writes are remembered for this connection" });
break;
}
}
/// <summary>Whether the write has had its answer (or has none owed).</summary>
public bool Settled => _answered;
/// <summary>A missing id cannot be answered under rule 1; refuse it with no id.</summary>
public bool HasId => _messageId.Length > 0;
private void Release()
{
if (_claimed) _session.Abandoned(_messageId);
_claimed = false;
}
}
}