Expand description
SP-token-broker-phase2 §4.7 — SSE long-connection bearer refresh.
For HTTP routes that hold a stream open (Celia’s /chat/stream,
healthkit’s bulk-export progress, etc.), the bearer must be
re-validated periodically — otherwise consent revocations propagate
only at the next request, which on a long-lived stream might be
never. This helper spawns a background task that polls
TokenBroker::resolve_bearer on a heartbeat and emits
RefreshEvents the adopter consumes from their SSE handler.
§Cadence (spec §4.7)
- Default cadence is
60s— matches the bound under which “consent withdrawals get honoured” the spec contracts for. - If
BearerIdentity.expires_atisSome(t), the next sleep ismin(t - now, cadence)so we never sleep past the advertised TTL. - The cadence is recomputed every iteration from the most recent
identity (a shrunk
expires_atfrom a revalidation immediately tightens the loop).
§What the helper does NOT do
- It does not cancel in-flight tool dispatches. Per spec §4.7, a tool call that began authorised must complete with that authority. Adopters propagate the cap change to the next request only.
- It does not emit SSE bytes. The adopter’s route handler maps
RefreshEvent::AuthLostto its own application-level frame (Celia choseevent: auth_lostper spec §4.7). - It does not auto-reconnect. When the helper emits
AuthLost, the loop terminates; the adopter route closes the stream; the client decides whether to reconnect with a fresh bearer.
Enums§
- Auth
Lost Reason - Why the bearer was invalidated. Maps 1:1 to the broker error variants per spec §4.4 — adopter UX can surface “session expired” vs “agent revoked” vs “server hiccup” distinctly.
- Refresh
Event - Events the refresh task emits to the adopter’s SSE route.
Constants§
- DEFAULT_
REFRESH_ CADENCE - Default heartbeat cadence per spec §4.7 (“matches the typical SSE keep-alive ping cadence; revocation window adopters accept”).
Functions§
- spawn_
bearer_ refresh - Spawn a background task that periodically re-validates
bearerviabrokerand emitsRefreshEvents on the returned channel. The task exits after emitting oneRefreshEvent::AuthLost; dropping theJoinHandleaborts it sooner.