Expand description
Staged pair session for MCP.
Splits the magic-wormhole pair flow (cli::pair_orchestrate) into discrete
stages so an MCP server can drive it across multiple JSON-RPC tool calls.
State machine:
pair_session_open
│
▼
┌─Opened ─────────┐
│ (peer not in) │ pair_session_try_sas (idempotent poll)
└─────────────────┘
│ peer's SPAKE2 msg arrives
▼
SasReady (sas + aead_key cached)
│ user types digits → pair_session_confirm_sas
▼
Confirmed
│ pair_session_finalize
▼
Finalized (peer pinned, relay coords saved)
Aborted = SAS mismatch / TTL expired / peer aborted (terminal, removed from store)Concurrency: each session is keyed by its relay pair_id (unique per
pairing). Sessions are independent — pairing with N peers concurrently
creates N sessions in the store, each with its own pair_id at the relay
and its own Mutex<PairSessionState>. No cross-session locking.
Structs§
- Pair
Session State - One in-flight pair session held in the MCP server process.
Constants§
- SESSION_
TTL - Session evicted after this much wall time. 10min covers human-pace OOB
code-phrase sharing (voice/text) plus AEAD bootstrap exchange comfortably,
matches the relay pair-slot TTL ceiling, and forces a fresh
pair_initiateif a session has been abandoned.
Functions§
- derive_
code_ hash - Insert a fresh session, returning the public session_id. SHA-256 hex of the domain-tagged code phrase. Both the host and guest derive the same value from the shared code, and the relay uses it as the pair-slot lookup key.
- init_
self_ idempotent - MCP-callable init: idempotent if already inited under the same handle, errors on different-handle conflict, accepts optional –relay binding.
- pair_
session_ confirm_ sas - Stage 3. Validate the user-typed digits against the cached SAS.
- pair_
session_ finalize - Stage 4. Seal+exchange bootstrap, AEAD-open peer’s, pin peer.
- pair_
session_ open - Stage 1. Open a pair session at the relay.
- pair_
session_ try_ sas - Stage 2. Try to advance a session to SAS-ready. Single non-blocking poll of the relay. Idempotent: re-calling after SAS is cached returns the same digits without further network I/O.
- pair_
session_ wait_ for_ sas - Stage 2 helper. Bounded loop wrapping
try_sas. Used both by CLI (long timeout, blocking) and MCP (short timeout, fall through to async poll). - restore_
pair_ session - Reconstruct a
PairSessionStatefrom persisted fields without any network I/O. The caller (typically the daemon’scleanup_on_startup) has already recoveredseed,code,code_hash,pair_id, slot info, androlefrom the on-disk pending-pair file; this builds an in-memory state that can resumepair_session_try_sasfrom where the prior daemon left off. - store_
get - Look up a session by id. Returns None if missing or evicted.
- store_
insert - store_
remove - Remove a session. Used after finalize, abort, or TTL eviction.
- store_
sweep_ expired - Sweep expired sessions. Called opportunistically before each public op.