node-device-session 7.6.219

Shared server for /econ-v1/device-session/1 behind transport-neutral traits (C-5)
Documentation
# node-device-session

Shared server for the `/econ-v1/device-session/1` wire protocol (C-5 / ADR-100).

Carriers (`node-app-iroh`, `node-app-p2p`) implement [`SessionStreams`] and
[`HostBus`], then call [`serve`]. This crate has **no** iroh (or other carrier)
dependency.

## Public API

- `ALPN` — `b"/econ-v1/device-session/1"`
- `SessionStreams` / `HostBus` / `WebRtcSignaling`
- `serve(conn, bus, ingress, path, webrtc)` — pass `Some(hook)` when the
  carrier answers `webrtc.signal/1`; `None` refuses with `unsupported_transport`
- `serve_with_registry(..., registry)` — same as `serve`, plus live-session
  registration for periodic revalidation
- `SessionRegistry` / `revalidate` — close revoked / expired / invalid sessions
- `webrtc::WebRtcAnswerer` + `webrtc::WebRtcCarrier` — shared str0m answerer

Wire methods: `session.open/1`, `session.renew/1`, `device.call/1`,
`http.request/1`, `ws.stream/1`, `webrtc.signal/1`.

## Live-session revalidation (once a minute)

A device that is revoked, unpaired or expired after it connected must lose its
live session within a minute on every carrier. Wire it like this:

1. Hold one shared [`SessionRegistry`] in the carrier app.
2. Accept connections with [`serve_with_registry`] plain [`serve`] is unchanged
   and does not register.
3. In `WebRtcCarrier::bind_session`, call
   `registry.bind_webrtc(&identity.device_id, handle.clone())` so a later
   revoke also tears down the WebRTC path.
4. Expose a capability such as `<app>.device.revalidate` and register a cron
   row whose trigger is that capability on schedule `0 * * * * *` (every
   minute). The handler calls `registry.revalidate(&bus).await`.

Only definitive verify refusals (`device_revoked`, `expired`,
`invalid_credential`, `endpoint_mismatch`) close a session.
`verifier_unavailable`, timeouts and other errors leave it open.

## Wiring the shared WebRTC answerer

The answerer owns signaling, ICE (str0m), DataChannel framing, and the
`http.request/1` / `ws.stream/1` tunnels over those channels. A carrier only
supplies what is carrier-specific via [`WebRtcCarrier`]:

| Carrier concern | Trait method |
|---|---|
| STUN server list (no TURN) | `stun_servers()` |
| Optional ICE UDP bind | `bind_addr()` |
| Router / UPnP / relay portmap | `port_mapper()` + `mapping_description()` |
| Clock for str0m / ICE | `now()` (default: `Instant::now()`) |
| Bind handle into session registry | `bind_session(...)` |

Iroh-only adapters (gossip discovery → STUN list, `RelaySessionPortMapper`)
stay in `node-app-iroh` and implement the trait. p2p does the same with its
own STUN / portmap sources. Neither carrier reimplements the answerer.

```rust
use node_device_session::{
    serve, IngressSocket, SessionPath, StaticWebRtcCarrier, WebRtcAnswerer,
};
use std::sync::Arc;

// Inside the carrier's ProtocolHandler / accept path:
let ingress = Arc::new(IngressSocket { path: Some(sock.into()) });
let carrier = StaticWebRtcCarrier {
    stun: stun_from_discovery.await,       // carrier-owned
    bind: None,
    mapper: my_port_mapper,                // Arc<dyn SessionPortMapper>
    mapping_description: "iroh-webrtc",    // or "p2p-webrtc"
};
let webrtc = Arc::new(WebRtcAnswerer::new(ingress.clone(), carrier));
serve(streams, bus, (*ingress).clone(), SessionPath::Relay, Some(webrtc)).await?;
```

For revocation / replace, override `WebRtcCarrier::bind_session` to store the
[`WebRtcHandle`] in the carrier's session registry and return `false` when the
session cannot accept WebRTC. Call `handle.close()` on revoke.

Wire protocol for `webrtc.signal/1` is unchanged.