Repository navigation
Pairing: several attempts per window, and client/pair-retry (spec #194, #237) - #303
Conversation
…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.
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Round sequencing, stale asynchronous responses, and window lifetime handling need correction.
Review effort: Balanced
Findings: 2
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.
| // 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; |
There was a problem hiding this comment.
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.
| cpace.Dispose(); | ||
| state.CPace = null; | ||
| state.Round++; | ||
| SendAsync(new ClientPairRetryMessage()).SafeFireAndForget(_logger); |
There was a problem hiding this comment.
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.
| /// <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 |
There was a problem hiding this comment.
Declined: the spec says the opposite. pairing.md: "Window lifetime. From window opening, without pausing during attempts."


Brings the pairing window and round handling in line with
Sendspin/spec1.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)
PairingWindowwas 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 byTryAdmit(connection).server_kcon that connection, the drop (or disposal) of that connection,Close(), and lifetime expiry.StateChangedis now also raised for the completed-pairing, fifth-failure and connection-drop closes.Hold-back counts rounds from emission (Sendspin/spec#194)
dynamic_pairing_codecounter was incremented only on a failedserver_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 whenserver/pair-initarrives and the code is emitted.client/pair-retry(Sendspin/spec#237)server_kcin the dynamic flow now sendsclient/pair-retryinstead of aborting. The pairing code is unchanged, theroundin the CPacesidadvances, and the code is presented again on the nextserver/pair-init.pairing_code_mismatchas before. The static flow never retries.server/pair-authmust follow theserver/pair-initthat 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 atserver/pair-init, so without this a peer could skip it after a retry and guess uncounted.ClientPairRetryMessage/MessageTypes.ClientPairRetry.Breaking behaviour
ServerPairInitPayload.NonceAis nowstring?: the spec sends it in the first round only. A first-roundserver/pair-initwithout it is a protocol error.PresentPairingCodeAsyncis invoked once per round, so it can be called more than once for one attempt (same code, same cancellation token).PairingWindowno longer reads as closed once an attempt starts, and no longer closes when an attempt times out.MIGRATION-10.0.0.mdis updated for all three.Left as it was
server_kcdoes. The spec has the operator action reset it. Stricter than the spec, and not in this task's scope.Tests
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-retrywith round 2 and the same code, abort at the hold-back, round counting at emission, and rejection of aserver/pair-auththat skips its round'sserver/pair-init. OnePeerNullRejectionTestsrow changed from a nullnonce_Ato a null payload, sincenonce_Ais now optional.Interop
Against
aiosendspin[server]==10.0.0:static-pin(including the server's gesture-gating assertion),pairingandunpairedall pass. No interop scenario exercisesdynamic_pairing_code, soclient/pair-retryis covered by unit tests only; the 10.0.0 server does implement it.