Skip to content

Replace the trust callback with is_paired() - #234

Merged
kahrendt merged 2 commits into
mainfrom
paired-getter
Nov 3, 2026
Merged

kahrendt merged 2 commits into
mainfrom
paired-getter

Conversation

@kahrendt

@kahrendt kahrendt commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Replaces ConnectionTrust and on_trust_changed() with SendspinClient::is_paired(). The enum's NONE/USER came from an early spec draft's owner/user/none trust levels. The current spec has only paired and unpaired sessions (README.md "Definitions"), which is one fact, and a piece of state rather than an event: on_trust_changed() fired every time a connection went operational, whether or not anything changed. Removing its pairing note also removes the last library heap allocation between accept and admission.

Breaking: the enum and the callback are removed outright, so a listener that overrides on_trust_changed() no longer compiles.

Breaking Change Migration

  • Delete any on_trust_changed() override and any use of ConnectionTrust.

  • ConnectionTrust::USER maps to is_paired() returning true, ConnectionTrust::NONE to false.

  • Poll is_paired() where the state was cached from the callback, reading it before is_connected():

    const bool paired = client.is_paired();
    if (client.is_connected()) {
        show_paired_indicator(paired);
    }
  • To react the moment a pairing completes, keep using on_pairing_succeeded(); is_paired() turns true once the server's rekey onto the new long-term PSK finishes.

Change

  • is_paired() (any thread): true when the primary admitted connection is keyed by a long-term PSK, false for the Pairing or Sentinel PSK and while nothing is admitted.
  • refresh_published_state() stores it every tick in a std::atomic<bool> paired_ next to connected_, after lowering connected_, so a re-handshake changes it on the same connection.
  • Read order: a caller that reads is_paired() first and then sees is_connected() true knows that session's server/activate has been applied. The public doc says to read it before is_connected().
  • Deleted: ConnectionTrust, on_trust_changed(), note_trust_changed(), the TRUST_CHANGED note type and PairingNote::trust.
  • Examples: the basic and source clients print the session's state when it changes while connected; the TUI shows "Paired: yes/no", and its Pairing Token panel shows whenever no paired session is connected.
  • Docs: the guide's "Trust Levels" section is now "Paired Sessions", the enum's reference entry is gone, and UNAUTHORIZED's description no longer uses trust levels and now covers a revoked record.

Tests

  • The encrypted-lifecycle tests wait on is_paired() && is_connected() instead of recording trust callbacks.
  • New checks: false on a Sentinel session, false on the Pairing-PSK session before the post-pairing rekey, and back to false after a paired connection is rekeyed onto the Pairing PSK.
  • ADMISSION_ALLOCATIONS is lowered from 1 to 0.

ConnectionTrust (NONE/USER) and on_trust_changed() carried over the
owner/user/none trust levels of an early spec draft. The spec now has
only paired and unpaired sessions, which is one fact, and a piece of
state rather than an event: on_trust_changed() fired each time a
connection went operational, changed or not.

SendspinClient::is_paired() reports it from any thread. The protocol
task stores it each tick in refresh_published_state(), after lowering
the connected flag, so a reader that checks is_paired() before
is_connected() sees a re-handshake's value only with its activation.
The enum, the callback and the pairing note that delivered it are
gone, and with the note the last heap allocation between accept and
admission.

This breaks the public API: a listener overriding on_trust_changed()
no longer compiles.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The TUI can hide the Pairing Token while a paired session is disconnected and leave pairing indicators stale because pairing changes do not trigger a redraw.

5 open issues
What changed in this PR

Replaces the legacy trust enum and callback with SendspinClient::is_paired(), aligning the client API with the protocol’s paired/unpaired session model.

Changes:

  • Adds a thread-safe pairing-state query and removes trust notifications—a breaking API change.
  • Migrates examples and documentation to polling paired-session state.
  • Updates encrypted lifecycle checks and lowers expected admission allocations to zero.
File Description
tests/​test_encrypted_lifecycle.cpp Replaces trust callback assertions with pairing-state checks.
tests/​test_client_lifecycle.cpp Updates commentary after trust-event removal.
tests/​test_allocations.cpp Sets expected admission allocations to zero.
src/​inbox.h Removes trust wording from the pairing-topic comment.
src/​connection_manager.h Adds the pairing-state atomic, getter, and contract documentation.
src/​connection_manager.cpp Publishes the primary connection’s pairing state.
src/​client.cpp Implements the public query and removes deferred trust notifications.
include/​sendspin/​types.h Removes ConnectionTrust and revises UNAUTHORIZED documentation.
include/​sendspin/​client.h Replaces the trust callback with the documented is_paired() API.
examples/​tui_client/​tui.h Replaces stored trust state with a pairing boolean.
examples/​tui_client/​tui.cpp Polls pairing state and updates pairing-related display conditions.
examples/​tui_client/​README.md Documents paired-session display and token visibility.
examples/​tui_client/​main.cpp Removes the trust listener override.
examples/​source_client/​main.cpp Reports pairing-state changes through polling.
examples/​basic_client/​main.cpp Polls pairing state and updates session messages.
docs/​internals.md Documents pairing-state publication and removes trust-note references.
docs/​integration-guide.md Updates listener examples, state queries, and pairing references.

Review effort: Max

Comment thread examples/tui_client/tui.cpp Resolved
Comment thread examples/tui_client/tui.cpp Resolved
Comment thread docs/integration-guide.md Resolved
Comment thread src/connection_manager.cpp Resolved
Comment thread src/connection_manager.cpp Resolved
The TUI's redraw check now compares the paired flag, so a rekey that
finishes between two polls no longer leaves the Paired row and the
token panel stale. The guide's polling snippet reads is_paired() before
is_connected(), as its doc asks, and the token panel's docs say it shows
while the client has no paired session.

TheConnectedFlagRisesOnlyAtTheEndOfTheTick checks is_paired() too: true
for the admitted LONG_TERM session, false after a drop, and still true
while a re-handshake awaits its server/activate, through a tick that
must not raise the connected flag.
@kahrendt
kahrendt merged commit 93a2c67 into main Nov 3, 2026
16 checks passed
@kahrendt
kahrendt deleted the paired-getter branch November 3, 2026 18:17
kahrendt added a commit that referenced this pull request Nov 3, 2026
## Summary

A coherence review of the allocation series (#225-#234) found the
migrated pieces consistent with each other, plus a set of small
inconsistencies at their edges. This PR fixes those. No behavior change.

## Change

- **Caps:** `MAX_SERVER_INIT_BYTES` moves from `noise_handshake.h` to
`crypto/constants.h`, beside `MAX_HANDSHAKE_MESSAGE_BYTES`; each comment
names the other.
- **Inline text:**
- `InlineText::assign()` is `[[nodiscard]]`; each caller checks it, or
casts to `(void)` with the reason its text fits (or, for the last-played
id, that clearing is the documented behavior).
- `set_noise_handshake_result()` and `run_rehandshake_msg1()` take
`const IdText&` instead of re-assigning views. `last_played_server_id()`
returns `const IdText&`, like `get_server_id()`.
- `ResolvedPsk::counterparty_id` is a plain `IdText`, empty for a PSK
bound to no server, instead of `std::optional<IdText>`.
- `SENTINEL_PSK_ID` is a `constexpr std::string_view` literal instead of
a global `std::string` built during static initialization.
- The connection's id and name members sit in its "String fields" group.
- **`ParsedJsonMessage::extract()`** static_asserts that an extractor
does not return a pointer, a `std::string_view`, or a
`JsonVariant`/`JsonObject`/`JsonArray`/`JsonString` (alone or in
`std::optional`), since the document is released before the caller reads
it.
- **Comments:** the stale "hub" comments name the real callers (protocol
task, `ConnectionManager`, transport threads); the ESP `start()` comment
describes when the connection is delivered.
- **Naming:** the activation check left over from `ConnectionTrust` is
"the admissibility check" in src, docs and tests, matching
`admissible()` and `inadmissible_reject_reason()`.

## Tests

- Tests build ids through a new `id_text()` helper in
`tests/record_test_helpers.h`, which fails a test whose id would not
fit.
- `CryptoConstants.SentinelPskIdMatchesSpecConstant` now checks that
`psk_id_for(SENTINEL_PSK)` derives the literal, so it fails if either
drifts.
- The `extract()` guard was checked with a probe: an extractor returning
a `std::string_view` fails to compile with the new message, one
returning an `int` compiles.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants