Say when a vault has moved, so nobody waits out the minute

The delta pull was cheap enough to run on a timer and the client did, once a
minute. That is fine for a machine and wrong for two people: an edit a colleague
makes is up to a minute stale, which is long enough for both of them to make it
and produce a conflict neither needed to have. Shortening the interval is the
obvious answer and the wrong one — it costs a request per client per interval
whether or not anything happened, and it converges on a busier server that is
still late.

So the server now says so. A client holds a WebSocket open at GET /api/v1/events,
subprotocol dodossh.events.v1, and gets a line down it when something it can read
has changed. ADR 0012 has the reasoning; three parts of it are worth repeating
here, because they are what everything else rests on.

**What crosses the socket is a notice, never data.** A frame names a vault and
how far its change log has got. No item, no ciphertext, not even which item it
was. The client's answer is the delta pull it would have run anyway, so there is
still exactly one code path that applies a change to a keychain, and it is not
this one. Pushing the items themselves would save a round trip and fork that path
in two, with the cursor, the merge and the tombstone rules duplicated across both
— ADR 0003 put every mutation through one write path for that reason, and this
keeps every read on one for the same one. It also makes a dropped notice
harmless, which is what lets the fan-out below be as simple as it is.

**Polling stays, and is what guarantees a pass.** The minute timer is unchanged.
A network that eats WebSockets, a server with Events:Enabled off, an older
server, a proxy that will not upgrade, a notice dropped under backpressure —
every one of those leaves a client behaving exactly as it did before this commit.
Nothing is reachable only over the socket and nothing is meant to become so;
VaultViewModel's AutoSyncInterval remark now says that where somebody changing it
will read it.

**The bearer token authorises the upgrade, unlike the relay's ticket.** Not an
inconsistency with ADR 0004: the relay's socket is a byte pipe whose whole
authorization decision — which host, which IPs, which port — is made before it
opens and never revisited, and it is the extraction seam for a process that must
hold no ACL code. This one is a view of the caller's own vault list and has to
keep answering "what may this account read" for as long as it is held. A ticket
would carry that answer in a token and be wrong the moment the account's access
changed. The two bounds that arrangement needs are met rather than waved at: the
socket is closed at the token's exp with close code 4401 and the client comes
straight back with a fresh one, and the vault set is re-resolved every few
minutes as well as on the changes known to affect it. Both bound *metadata*,
because a notice contains nothing else and reading a vault still needs a key this
server has never held.

**The fan-out.** VaultEventHub is a singleton holding the sockets this node
accepted; publishing walks them and asks each whether it cares, rather than
keeping a vault-to-subscriber index that every re-subscription would have to move
entries between under a lock publishing also takes. At a few hundred sockets per
node and an event rate bounded by how often people edit keychains, the walk is
not measurable and its races are obvious. Per-connection queues are bounded and
drop the *oldest*: a notice means "pull vault X, which is at least at sequence
N", so the newest subsumes what it displaces and the client's answer is identical
either way — which is what lets the publish path be void, never block, and never
fail.

Announced from the endpoint rather than from SyncService, and that placement is
the point: by then the push has committed and released the per-vault advisory
lock. From inside it would name a sequence no reader can see yet and would hold
the lock that serialises writers across a socket write. Only the highest
*applied* sequence, so a batch of pure conflicts announces nothing, and a
duplicate — already announced when it first landed — announces nothing either.

Grants and membership publish too, and those take the *recipient* rather than the
actor. This is what AdmitNewVaultsAsync has been apologising for since sharing
shipped — "the recipient is handed nothing, there is no push channel" — and the
README with it. A vault shared with somebody now turns up as it is shared. The
comment and the README paragraph both say what is true now, and both keep saying
that the pass is what *discovers* the vault, because a client with no socket has
to arrive at the same place.

**On the client**, VaultEventStream is really a reconnection policy wrapped round
a ClientWebSocket: a dropped socket is the ordinary case here — laptops sleep,
proxies time out, tokens expire, servers are redeployed — so nothing in it treats
a failure as exceptional, and every path ends in "wait, then dial again". A
connection that lived long enough to say hello resets the backoff, so a laptop
that woke, worked, and lost its network an hour later does not inherit a
minute-long wait it has already proved it need not take. A 4401 close skips the
backoff entirely and asks the token provider again, which is the whole reason
that close code is distinct. A server that does not advertise the events feature
gets IdleVaultEventStream, which never delivers — so IVaultServer.Events is never
null and every caller stays on one shape, because the correct behaviour without a
socket is the behaviour with a silent one.

The shell's background loop now selects between the timer and a notice, and both
waits are held across iterations. That is load-bearing rather than tidy:
PeriodicTimer permits one outstanding WaitForNextTickAsync and throws on a
second, and an abandoned channel read stays registered and consumes the next
notice written. Either defect leaves the first notice working and every one after
it silently lost, which is why NoticesKeepWakingTheLoop_NotJustTheFirst pushes
three and not one. Notices are coalesced over a quarter of a second, so one
person's save — a host and its log entry are two items — and a colleague clearing
a folder each cost one pass rather than a dozen.

**The kind is a string, not an enum**, and that is a compatibility decision.
UseStringEnumConverter throws on a value it does not know, so a newer server
sending a kind an older client had never heard of would not add an unreadable
frame — it would break that client's socket outright. A string is ignored
instead. ProblemCodes is the same shape for the same reason.

**Tested on both sides, through the real pipeline.** The endpoint suite opens a
genuine socket against TestServer and proves a push produces a notice, that
another account's push does not reach it, that a ping is answered, and that a
frame this server cannot parse does not end the connection. Two of those assert
on *ordering* rather than on absence within a timeout — the stranger's write goes
first, so a socket that leaked would have announced it before the one the test
waits for — because "nothing arrived in two seconds" is a test that passes on a
slow machine for the wrong reason. And ANoticeCarriesNoCiphertext asserts on the
bytes that crossed the wire rather than on the record's fields, since the latter
would only prove that this type has no payload member, which is a tautology; the
former is what catches a field added later without anybody thinking about
disclosure.

The client suite drives VaultEventStream through an injected connector, because
the one thing a test cannot do to a real network is make it fail on cue — and
failure is the entire subject. The shell suite proves a notice produces a pull
inside ten seconds against a sixty-second timer, so the timer cannot be what
caused it.

**Two limits, stated rather than left to be discovered.** Fan-out is in-process,
so a deployment running more than one API replica only pushes for writes its own
replica handled and the rest arrive on the timer. IVaultEventPublisher is the
seam a PostgreSQL LISTEN/NOTIFY backplane implements and it is deliberately not
implemented: an untested backplane is worse than a documented gap, and multiple
replicas degrade to the behaviour before this commit rather than breaking. And a
client is notified of its own writes; it pushed, so it already pulled, and the
extra pass finds nothing. Suppressing that echo correctly needs a per-device
identity on the socket, and the same user's other machines must still be told.

Manual checks phase 15 covers what no test here can reach, which is the network
in between: a proxy that will not upgrade, one that drops an idle socket without
telling either end, a laptop lid, a token expiring. Every one of those is
invisible inside a test host, and every check there passes only if the change
arrives quickly *and* still arrives with the socket taken away.

ADR 0012 also fixes one thing about the shared terminal session this is the
transport for, so it need not be renegotiated later: session data will be binary
frames on this same socket, because base64 in a JSON envelope is the wrong shape
for the one payload here that is continuous rather than occasional. Two questions
it explicitly does not answer by implication — whether those bytes go through the
API at all, and what end-to-end encryption means when the second party watches a
stream rather than holding a key — are ADR 0001 questions and get their own
decision.

1512 tests pass. DodoSSH.SystemTests was not run — it needs the whole compose
stack — so the end-to-end path is unverified for this change beyond what the
manual checks describe.
This commit is contained in:
2026-08-04 16:37:41 +02:00
parent 176df67861
commit 4b706bc3c3
31 changed files with 3285 additions and 13 deletions
@@ -0,0 +1,461 @@
using System.Net;
using System.Net.WebSockets;
using System.Text;
using System.Text.Json;
using DodoSSH.Contracts;
using DodoSSH.Domain;
using DodoSSH.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
namespace DodoSSH.Api.Tests;
/// <summary>
/// The push channel, over a real socket through the real authentication pipeline.
/// </summary>
/// <remarks>
/// <para>
/// The tests that matter most here are the two negatives: an unauthenticated upgrade is refused, and a
/// change to somebody else's vault does not reach this socket. A push channel that leaked <em>which
/// vault ids exist and when they change</em> would be a disclosure the pull path takes deliberate
/// trouble to avoid — <c>SyncPullEndpoint</c> answers 404 rather than 403 for exactly that reason —
/// and it would be invisible in a test that only checked that notices arrive.
/// </para>
/// <para>
/// Ordering is asserted rather than absence-within-a-timeout wherever possible. "Nothing arrived in
/// two seconds" is a test that passes on a slow machine for the wrong reason; "the first notice this
/// socket saw was about its own vault, although another vault was written to first" is not.
/// </para>
/// </remarks>
[Collection(ApiCollection.Name)]
public sealed class EventsEndpointTests(ApiFixture fixture)
{
private static readonly DateTimeOffset Now = new(2026, 8, 4, 12, 0, 0, TimeSpan.Zero);
/// <summary>
/// How long a test will wait for a frame before calling it a failure.
/// </summary>
/// <remarks>
/// Generous, because it is not a measurement: every wait here is for something already committed,
/// so the only thing this bounds is how long a genuinely broken build hangs before it reports.
/// </remarks>
private static readonly TimeSpan FrameTimeout = TimeSpan.FromSeconds(30);
// ---- The handshake ----
[Fact]
public async Task WithoutAToken_TheUpgradeIsRefused()
{
var client = fixture.Server.CreateWebSocketClient();
client.SubProtocols.Add(VaultEvents.SubProtocol);
var connecting = client.ConnectAsync(
new Uri(fixture.Server.BaseAddress, VaultEvents.Path.TrimStart('/')),
TestContext.Current.CancellationToken);
await Should.ThrowAsync<InvalidOperationException>(connecting);
}
[Fact]
public async Task BeforeEnrolling_Is403WithAnActionableCode()
{
// The same bar as sync: a caller with no identity key holds no vault key either, so every
// notice this socket could carry is about ciphertext they cannot read.
var client = fixture.CreateClientFor(NewSubject());
var response = await client.GetAsync(
new Uri(VaultEvents.Path, UriKind.Relative),
TestContext.Current.CancellationToken);
response.StatusCode.ShouldBe(HttpStatusCode.Forbidden);
var problem = await response.Content.ReadProblemAsync();
problem.ShouldNotBeNull();
problem.Code.ShouldBe(ProblemCodes.EnrollmentRequired);
}
[Fact]
public async Task APlainGet_SaysItIsAWebSocket()
{
// A person, or a client with the wrong URL. Answering with a problem document rather than a
// socket that closes is the difference between a diagnosable mistake and a mysterious one.
var client = fixture.CreateClientFor(await SeedEnrolledUserAsync());
var response = await client.GetAsync(
new Uri(VaultEvents.Path, UriKind.Relative),
TestContext.Current.CancellationToken);
response.StatusCode.ShouldBe(HttpStatusCode.BadRequest);
var problem = await response.Content.ReadProblemAsync();
problem.ShouldNotBeNull();
problem.Code.ShouldBe(ProblemCodes.MalformedRequest);
problem.Detail.ShouldNotBeNull().ShouldContain(VaultEvents.SubProtocol);
}
[Fact]
public async Task AnUpgradeWithoutTheSubprotocol_IsRefused()
{
// The subprotocol is this socket's version negotiation, so accepting an upgrade that did not
// offer it would mean answering a client in a dialect it never agreed to read.
var (subject, _) = await SeedUserWithVaultAsync();
var client = fixture.Server.CreateWebSocketClient();
client.ConfigureRequest = request => request.Headers.Authorization =
$"Bearer {fixture.IdentityProvider.MintToken(subject)}";
var connecting = client.ConnectAsync(
new Uri(fixture.Server.BaseAddress, VaultEvents.Path.TrimStart('/')),
TestContext.Current.CancellationToken);
await Should.ThrowAsync<InvalidOperationException>(connecting);
}
[Fact]
public async Task TheFirstFrameIsHello()
{
var (subject, _) = await SeedUserWithVaultAsync();
using var timeout = Timeout();
using var socket = await fixture.ConnectEventsAsync(subject, timeout.Token);
var hello = await ReadAsync(socket, timeout.Token);
hello.Kind.ShouldBe(VaultEventKinds.Hello);
// Sent so a client knows when silence means the socket is dead rather than quiet, and so a
// socket following nothing — a real state, for an account with no vaults — is distinguishable
// from one that is broken.
hello.HeartbeatSeconds.ShouldNotBeNull().ShouldBeGreaterThan(0);
hello.VaultCount.ShouldBe(1);
}
[Fact]
public async Task MetaAdvertisesTheFeature()
{
// How a client decides whether to hold a socket open at all. Absence is not an error — it
// means synchronise on the timer, which is what every client did before this existed.
var client = fixture.CreateClient();
var meta = await (await client.GetAsync(
new Uri("/api/v1/meta", UriKind.Relative),
TestContext.Current.CancellationToken))
.Content.ReadContractAsync<MetaResponse>();
meta.ShouldNotBeNull();
meta.Features.ShouldContain(
feature => string.Equals(feature, VaultEvents.Feature, StringComparison.Ordinal));
}
// ---- Notices ----
[Fact]
public async Task APush_AnnouncesTheVaultToAFollowingSocket()
{
var (subject, vaultId) = await SeedUserWithVaultAsync();
using var timeout = Timeout();
using var socket = await fixture.ConnectEventsAsync(subject, timeout.Token);
await ReadAsync(socket, timeout.Token);
var client = fixture.CreateClientFor(subject);
var push = await client.PostContractAsync(PushUrl(vaultId), NewCreateBatch());
push.EnsureSuccessStatusCode();
var notice = await ReadUntilAsync(socket, VaultEventKinds.VaultChanged, timeout.Token);
notice.VaultId.ShouldBe(vaultId);
// A hint for logging and coalescing, never a cursor: cursors are opaque and integrity-tagged,
// and a client that tried to resume from this would be resuming from a number it invented.
notice.Sequence.ShouldNotBeNull().ShouldBeGreaterThan(0);
}
[Fact]
public async Task ANoticeCarriesNoCiphertext()
{
// The load-bearing property of the whole design. A notice says only that a vault moved; the
// client's answer is the delta pull it would have run on its timer anyway, which keeps exactly
// one code path applying changes. See ADR 0012.
var (subject, vaultId) = await SeedUserWithVaultAsync();
using var timeout = Timeout();
using var socket = await fixture.ConnectEventsAsync(subject, timeout.Token);
await ReadAsync(socket, timeout.Token);
var client = fixture.CreateClientFor(subject);
var batch = NewCreateBatch();
await client.PostContractAsync(PushUrl(vaultId), batch);
var raw = await ReadRawUntilAsync(socket, VaultEventKinds.VaultChanged, timeout.Token);
// The envelope this test pushed, as it would appear if a payload had been forwarded.
raw.ShouldNotContain(Convert.ToBase64String(batch.Operations[0].Payload!.Envelope));
raw.ShouldNotContain("payload", Case.Insensitive);
raw.ShouldNotContain(batch.Operations[0].EntityId.ToString());
}
[Fact]
public async Task APushToAnotherAccountsVault_IsNotAnnouncedHere()
{
// The disclosure that would matter: a socket learning that vault ids it cannot read exist,
// and when somebody works on them.
var (subject, vaultId) = await SeedUserWithVaultAsync();
var (stranger, strangersVaultId) = await SeedUserWithVaultAsync();
using var timeout = Timeout();
using var socket = await fixture.ConnectEventsAsync(subject, timeout.Token);
await ReadAsync(socket, timeout.Token);
// The stranger's write goes first, so a socket that leaked would have announced it before the
// one this test then waits for. Ordering, not a timeout: "nothing arrived in two seconds"
// passes on a slow machine for the wrong reason.
var strangersClient = fixture.CreateClientFor(stranger);
(await strangersClient.PostContractAsync(PushUrl(strangersVaultId), NewCreateBatch()))
.EnsureSuccessStatusCode();
var ownClient = fixture.CreateClientFor(subject);
(await ownClient.PostContractAsync(PushUrl(vaultId), NewCreateBatch()))
.EnsureSuccessStatusCode();
var notice = await ReadUntilAsync(socket, VaultEventKinds.VaultChanged, timeout.Token);
notice.VaultId.ShouldBe(vaultId);
notice.VaultId.ShouldNotBe(strangersVaultId);
}
[Fact]
public async Task APushThatAppliedNothing_AnnouncesNothing()
{
// A batch of pure conflicts moved no vault. Announcing one anyway would have every client on
// it pull for a change that is not there.
var (subject, vaultId) = await SeedUserWithVaultAsync();
var client = fixture.CreateClientFor(subject);
using var timeout = Timeout();
using var socket = await fixture.ConnectEventsAsync(subject, timeout.Token);
await ReadAsync(socket, timeout.Token);
// An update to an item that does not exist: rejected as a conflict, nothing written.
var stale = new SyncPushRequest(
[
NewOperation(Guid.CreateVersion7(), expectedVersion: 7, envelope: [9, 9]),
]);
var conflicted = await client.PostContractAsync(PushUrl(vaultId), stale);
conflicted.EnsureSuccessStatusCode();
var results = await conflicted.Content.ReadContractAsync<SyncPushResponse>();
results.ShouldNotBeNull();
results.Results[0].Status.ShouldBe(SyncOperationStatus.Conflict);
// Then a write that did land. The first notice must be that one.
(await client.PostContractAsync(PushUrl(vaultId), NewCreateBatch())).EnsureSuccessStatusCode();
var notice = await ReadUntilAsync(socket, VaultEventKinds.VaultChanged, timeout.Token);
notice.Sequence.ShouldNotBeNull().ShouldBeGreaterThan(0);
}
[Fact]
public async Task APing_IsAnswered()
{
var (subject, _) = await SeedUserWithVaultAsync();
using var timeout = Timeout();
using var socket = await fixture.ConnectEventsAsync(subject, timeout.Token);
await ReadAsync(socket, timeout.Token);
await SendAsync(socket, new VaultEvent(VaultEventKinds.Ping), timeout.Token);
var pong = await ReadUntilAsync(socket, VaultEventKinds.Pong, timeout.Token);
pong.Kind.ShouldBe(VaultEventKinds.Pong);
}
[Fact]
public async Task AFrameThisServerCannotRead_DoesNotEndTheSocket()
{
// A control channel whose failure mode is "the client polls instead" should tolerate a frame
// from a newer client rather than cost that client its push for the whole session.
var (subject, vaultId) = await SeedUserWithVaultAsync();
using var timeout = Timeout();
using var socket = await fixture.ConnectEventsAsync(subject, timeout.Token);
await ReadAsync(socket, timeout.Token);
await socket.SendAsync(
Encoding.UTF8.GetBytes("{ not json at all"),
WebSocketMessageType.Text,
endOfMessage: true,
timeout.Token);
var client = fixture.CreateClientFor(subject);
(await client.PostContractAsync(PushUrl(vaultId), NewCreateBatch())).EnsureSuccessStatusCode();
var notice = await ReadUntilAsync(socket, VaultEventKinds.VaultChanged, timeout.Token);
notice.VaultId.ShouldBe(vaultId);
socket.State.ShouldBe(WebSocketState.Open);
}
// ---- Helpers ----
/// <summary>A token that gives up rather than letting a broken build hang the suite.</summary>
private static CancellationTokenSource Timeout()
{
var source = CancellationTokenSource.CreateLinkedTokenSource(
TestContext.Current.CancellationToken);
source.CancelAfter(FrameTimeout);
return source;
}
private static async Task<VaultEvent> ReadAsync(WebSocket socket, CancellationToken cancellationToken)
{
var json = await ReadRawAsync(socket, cancellationToken);
return JsonSerializer.Deserialize(json, DodoSshJsonContext.Default.VaultEvent)
?? throw new InvalidOperationException($"The server sent a null frame: {json}");
}
private static async Task<string> ReadRawAsync(WebSocket socket, CancellationToken cancellationToken)
{
var buffer = new byte[8 * 1024];
var received = await socket.ReceiveAsync(buffer, cancellationToken);
if (received.MessageType == WebSocketMessageType.Close)
{
throw new InvalidOperationException(
$"The server closed the socket: {received.CloseStatus} {received.CloseStatusDescription}");
}
return Encoding.UTF8.GetString(buffer, 0, received.Count);
}
/// <summary>Reads past the frames a test does not care about — hello, and heartbeats.</summary>
private static async Task<VaultEvent> ReadUntilAsync(
WebSocket socket,
string kind,
CancellationToken cancellationToken)
{
while (true)
{
var frame = await ReadAsync(socket, cancellationToken);
if (string.Equals(frame.Kind, kind, StringComparison.Ordinal))
{
return frame;
}
}
}
/// <summary>
/// The same, but keeping the bytes.
/// </summary>
/// <remarks>
/// Deserialising and asserting on the fields would prove only that this <em>record</em> has no
/// payload member, which is a tautology. Asserting on what actually crossed the socket is what
/// would catch a field added to the frame later without anybody thinking about disclosure.
/// </remarks>
private static async Task<string> ReadRawUntilAsync(
WebSocket socket,
string kind,
CancellationToken cancellationToken)
{
while (true)
{
var json = await ReadRawAsync(socket, cancellationToken);
var frame = JsonSerializer.Deserialize(json, DodoSshJsonContext.Default.VaultEvent);
if (string.Equals(frame?.Kind, kind, StringComparison.Ordinal))
{
return json;
}
}
}
private static Task SendAsync(WebSocket socket, VaultEvent frame, CancellationToken cancellationToken) =>
socket.SendAsync(
JsonSerializer.SerializeToUtf8Bytes(frame, DodoSshJsonContext.Default.VaultEvent),
WebSocketMessageType.Text,
endOfMessage: true,
cancellationToken);
private static string PushUrl(Guid vaultId) => $"/api/v1/vaults/{vaultId}/sync/push";
private static string NewSubject() => $"events-{Guid.CreateVersion7():N}";
private static SyncPushOperation NewOperation(Guid entityId, int? expectedVersion, byte[] envelope) =>
new(
Guid.CreateVersion7(),
SyncEntityType.Host,
entityId,
SyncOperation.Upsert,
expectedVersion,
new EncryptedPayload(envelope, [0xD, 0xE], Guid.CreateVersion7(), 1, 1),
new SyncPlaintextFields());
private static SyncPushRequest NewCreateBatch() =>
new([NewOperation(Guid.CreateVersion7(), expectedVersion: null, envelope: [1, 2, 3, 4])]);
private async Task<string> SeedEnrolledUserAsync()
{
var subject = NewSubject();
await using var scope = fixture.CreateScope();
var database = scope.ServiceProvider.GetRequiredService<DodoDbContext>();
var user = NewUser(subject);
database.Users.Add(user);
database.UserKeys.Add(Seed.CurrentKey(user.Id, Now));
await database.SaveChangesAsync();
return subject;
}
private async Task<(string Subject, Guid VaultId)> SeedUserWithVaultAsync()
{
var subject = NewSubject();
await using var scope = fixture.CreateScope();
var database = scope.ServiceProvider.GetRequiredService<DodoDbContext>();
var user = NewUser(subject);
var vault = new Vault
{
Id = Guid.CreateVersion7(),
Name = "Personal",
OwnerKind = VaultOwnerKind.Personal,
OwnerUserId = user.Id,
KeyGeneration = 1,
CreatedAtUtc = Now,
UpdatedAtUtc = Now,
};
database.Users.Add(user);
database.UserKeys.Add(Seed.CurrentKey(user.Id, Now));
database.Vaults.Add(vault);
await database.SaveChangesAsync();
return (subject, vault.Id);
}
private UserAccount NewUser(string subject) => new()
{
Id = Guid.CreateVersion7(),
Issuer = fixture.IdentityProvider.Authority,
Subject = subject,
Status = UserStatus.Active,
CreatedAtUtc = Now,
UpdatedAtUtc = Now,
};
}