Skip to content

Pairing: several attempts per window, and client/pair-retry (spec #194, #237) - #303

Merged
chrisuthe merged 3 commits into
mainfrom
chrisuthe/task/pairing-several-attempts-per-window-and-client
Oct 6, 2026
Merged

chrisuthe merged 3 commits into
mainfrom
chrisuthe/task/pairing-several-attempts-per-window-and-client

Conversation

@chrisuthe

@chrisuthe chrisuthe commented Oct 6, 2026 •

Copy link
Copy Markdown
Member

Brings the pairing window and round handling in line with Sendspin/spec 1.0.0-rc1 (671a34d) in the two places the audit flagged. Both rows were marked unverified; both turned out to be real gaps.

What changed

Pairing window admits several attempts (Sendspin/spec#194)

The window admits attempts only on the connection that carries its first, and closes on a completed pairing, its fifth failed attempt (the client's verification of server_kc fails), drop of that connection, operator cancellation, or window-lifetime expiry. An attempt that ends any other way - timed out or cancelled - does not itself close the window.

  • PairingWindow was single-use: the first attempt it admitted closed it, and an attempt timeout closed it too. It now stays open across attempts and binds to the connection carrying its first one. TryConsume() (internal) is replaced by TryAdmit(connection).
  • It closes on exactly the spec's list: a completed pairing, the fifth failed server_kc on that connection, the drop (or disposal) of that connection, Close(), and lifetime expiry.
  • StateChanged is now also raised for the completed-pairing, fifth-failure and connection-drop closes.

Hold-back counts rounds from emission (Sendspin/spec#194)

an abandoned attempt does not count against a pairing window, and counts toward the round limit only when the code was already being emitted

  • The dynamic_pairing_code counter was incremented only on a failed server_kc, so a round abandoned after its code was shown never counted. Here the SDK was looser than the spec, not stricter. It now counts each round when server/pair-init arrives and the code is emitted.
  • No change to the lockout store format or its interface.

client/pair-retry (Sendspin/spec#237)

On failure the client either sends client/pair-retry, asking for another round, or ends the attempt with pair/abort reason pairing_code_mismatch. The client SHOULD retry, and MUST abort instead at the round limit. A retry keeps the attempt, its pairing code, and its running attempt timeout in place

  • A failed server_kc in the dynamic flow now sends client/pair-retry instead of aborting. The pairing code is unchanged, the round in the CPace sid advances, and the code is presented again on the next server/pair-init.
  • Once the method is held back, a failed round aborts with pairing_code_mismatch as before. The static flow never retries.
  • Each dynamic server/pair-auth must follow the server/pair-init that begins its round; one that does not is a protocol error. Every CPace run is one guess at the code, and the round is counted at server/pair-init, so without this a peer could skip it after a retry and guess uncounted.
  • New ClientPairRetryMessage / MessageTypes.ClientPairRetry.

Breaking behaviour

  • ServerPairInitPayload.NonceA is now string?: the spec sends it in the first round only. A first-round server/pair-init without it is a protocol error.
  • PresentPairingCodeAsync is invoked once per round, so it can be called more than once for one attempt (same code, same cancellation token).
  • An open PairingWindow no longer reads as closed once an attempt starts, and no longer closes when an attempt times out.

MIGRATION-10.0.0.md is updated for all three.

Left as it was

  • The hold-back threshold stays at 10. The spec's round limit is 20 and it allows holding back earlier, so this conforms, but now that the counter holds rounds rather than failures it is twice as strict as the spec's number. Say if you want it moved to 20.
  • An operator gesture does not reset the counter; only a verified server_kc does. The spec has the operator action reset it. Stricter than the spec, and not in this task's scope.

Tests

Framework Before After
net8.0 Passed: 1186 Passed: 1206
net10.0 Passed: 1188 Passed: 1208

No failures on either. New coverage: the window staying open after a timed-out attempt and admitting the next one without a gesture, admission only on the first connection, the fifth-failure close, the connection-drop close, client/pair-retry with round 2 and the same code, abort at the hold-back, round counting at emission, and rejection of a server/pair-auth that skips its round's server/pair-init. One PeerNullRejectionTests row changed from a null nonce_A to a null payload, since nonce_A is now optional.

Interop

Against aiosendspin[server]==10.0.0: static-pin (including the server's gesture-gating assertion), pairing and unpaired all pass. No interop scenario exercises dynamic_pairing_code, so client/pair-retry is covered by unit tests only; the 10.0.0 server does implement it.

…c rounds

Spec #194: the pairing window is no longer single-use. An opening admits
attempts on the connection that carries its first and stays open across
them; it closes on a completed pairing, its fifth failed server_kc, the
drop of that connection, Close(), or lifetime expiry. An attempt timeout
no longer closes it. The dynamic hold-back counter now counts a round
from the moment its code is emitted, so a round abandoned afterwards
counts.

Spec #237: a failed server_kc in the dynamic flow is answered with
client/pair-retry instead of pair/abort. The pairing code is unchanged
across rounds and the round number in the CPace sid advances; nonce_A is
present on the first server/pair-init only. Once held back, a failed
round aborts as before.
A round is counted when server/pair-init arrives, but after a
client/pair-retry the pairing code is already derived, so a
server/pair-auth sent without a new server/pair-init ran another CPace
guess that was never counted. Require each dynamic server/pair-auth to
follow the server/pair-init that begins its round; anything else is a
sequence violation, as it already was before the first round.
@chrisuthe
chrisuthe marked this pull request as ready for review October 6, 2026 16:55
@chrisuthe
chrisuthe requested a balanced review from Copilot October 6, 2026 22:08

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Round sequencing, stale asynchronous responses, and window lifetime handling need correction.

Review effort: Balanced
Findings: 2 High severity · 1 Medium severity

Open (3)
What changed in this PR

Updates SDK pairing toward Sendspin 1.0.0-rc1 behavior, supporting multiple attempts per window and dynamic-code retries.

Changes:

  • Adds connection-bound window admission and revised closure rules.
  • Implements client/pair-retry, per-round presentation, and emission-based counting.
  • Expands regression tests and migration guidance.
File Description
tests/​Sendspin.SDK.Tests/​Protocol/​PeerNullRejectionTests.cs Adjusts nullable-nonce validation coverage.
tests/​Sendspin.SDK.Tests/​Connection/​PairingWindowEndToEndTests.cs Updates window lifecycle assertions.
tests/​Sendspin.SDK.Tests/​Client/​PairingWindowTests.cs Tests admission, binding, and closure.
tests/​Sendspin.SDK.Tests/​Client/​PairingRoundTests.cs Tests retries, counting, and sequencing.
tests/​Sendspin.SDK.Tests/​Client/​PairingGatingTests.cs Expands gating coverage and test helpers.
tests/​Sendspin.SDK.Tests/​Client/​PairingAttemptTimeoutTests.cs Tests window reuse after timeout.
src/​Sendspin.SDK/​Protocol/​PeerMessageValidation.cs Allows omitted round nonce.
src/​Sendspin.SDK/​Protocol/​MessageSerializerContext.cs Registers retry serialization metadata.
src/​Sendspin.SDK/​Protocol/​MessageSerializer.cs Adds retry deserialization.
src/​Sendspin.SDK/​Protocol/​Messages/​PairingMessages.cs Adds retry messages and nullable nonce.
src/​Sendspin.SDK/​Protocol/​Messages/​MessageTypes.cs Defines the retry message type.
src/​Sendspin.SDK/​MIGRATION-10.0.0.md Documents pairing behavior changes.
src/​Sendspin.SDK/​Connection/​Noise/​Pairing/​PairingCodes.cs Clarifies counter documentation.
src/​Sendspin.SDK/​Client/​SendspinClientOptions.cs Documents per-round presentation callbacks.
src/​Sendspin.SDK/​Client/​SendSpinClient.cs Integrates retries, counting, and window lifecycle.
src/​Sendspin.SDK/​Client/​PairingWindow.cs Implements connection-bound, multi-attempt windows.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +3361 to +3364
// The round counts toward the hold-back from here, where its code is emitted, so a
// round abandoned afterwards still counts.
RecordPairingCodeFailure(state.Method);
state.RoundBegun = true;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Declined: every server/pair-init is counted toward the hold-back, so a repeated one cannot yield an uncounted guess; a server that does this only spends its own allowance.

Comment on lines +3509 to +3512
cpace.Dispose();
state.CPace = null;
state.Round++;
SendAsync(new ClientPairRetryMessage()).SafeFireAndForget(_logger);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Declined: it needs a server that sends pair-confirm before the client's auth, and the only thing it can spoil is its own pairing attempt.

Comment on lines +45 to +46
/// <see cref="DefaultLifetime"/>. Measured from opening, without pausing during attempts:
/// an attempt already in progress runs to its own end, but starting another needs a new

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Declined: the spec says the opposite. pairing.md: "Window lifetime. From window opening, without pausing during attempts."

@chrisuthe
chrisuthe merged commit 80ff878 into main Oct 6, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants