atd-server-http 1.2.0

Streamable-HTTP (MCP JSON-RPC) transport for ATD-speaking servers — pair with atd-runtime, sibling of atd-server (Unix-socket transport).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//! 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
//! [`RefreshEvent`]s 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_at` is `Some(t)`, the next sleep is
//!   `min(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_at` from 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::AuthLost`] to its own application-level frame
//!   (Celia chose `event: auth_lost` per 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.

use std::sync::Arc;
use std::time::{Duration, SystemTime};

use atd_runtime::secrets::{BearerIdentity, BrokerError, TokenBroker};
use tokio::sync::mpsc;
use tokio::task::JoinHandle;

/// Default heartbeat cadence per spec §4.7 ("matches the typical SSE
/// keep-alive ping cadence; revocation window adopters accept").
pub const DEFAULT_REFRESH_CADENCE: Duration = Duration::from_secs(60);

/// Channel buffer for [`RefreshEvent`]s. Adopters typically read
/// one-at-a-time inside an SSE select! arm; 8 absorbs bursts without
/// back-pressuring the broker.
const CHANNEL_BUFFER: usize = 8;

/// Events the refresh task emits to the adopter's SSE route.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum RefreshEvent {
    /// Broker re-validated the bearer. `caps` is the latest authorised
    /// capability set — may be the same as before, or smaller (shrunk
    /// by a partial revoke). The adopter updates its per-stream cap
    /// reference for future requests but **does not** cancel in-flight
    /// tool dispatches.
    Refreshed {
        caps: Vec<String>,
        expires_at: Option<SystemTime>,
    },
    /// Bearer is no longer valid (expired, revoked, or
    /// broker-recognised-bad). Adopter route should emit its
    /// application-level `auth_lost` frame and close the stream. The
    /// refresh task exits after sending this; the channel closes.
    AuthLost { reason: AuthLostReason },
}

/// 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.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum AuthLostReason {
    Expired,
    Revoked(String),
    /// Broker started returning `Ok(None)` — bearer no longer
    /// recognised (admin deleted the pairing? token rotation gone
    /// wrong?). Treated the same as expired/revoked at the client.
    Unknown,
    /// Broker hard error (`Err(Internal)` or `Err(Lookup)`). Includes
    /// the broker's reason string. Distinct from `Expired`/`Revoked`
    /// so the adopter can decide whether to retry the validation
    /// (transient) or close the stream (persistent).
    BrokerError {
        reason: String,
        transient: bool,
    },
}

/// Spawn a background task that periodically re-validates `bearer` via
/// `broker` and emits [`RefreshEvent`]s on the returned channel. The
/// task exits after emitting one [`RefreshEvent::AuthLost`]; dropping
/// the [`JoinHandle`] aborts it sooner.
///
/// Per spec §4.7, the initial `resolve_bearer` call already happened at
/// stream open (so the route handler has the live `BearerIdentity`);
/// this task picks up *after* that. The caller supplies `initial` so
/// the helper can compute the first sleep duration from the recorded
/// `expires_at`.
pub fn spawn_bearer_refresh(
    broker: Arc<dyn TokenBroker>,
    bearer: String,
    initial: BearerIdentity,
    cadence: Duration,
) -> (JoinHandle<()>, mpsc::Receiver<RefreshEvent>) {
    let (tx, rx) = mpsc::channel(CHANNEL_BUFFER);

    let handle = tokio::spawn(async move {
        let mut current_expires_at = initial.expires_at;
        loop {
            let sleep_for = next_sleep_duration(cadence, current_expires_at);
            tokio::time::sleep(sleep_for).await;

            match broker.resolve_bearer(&bearer).await {
                Ok(Some(id)) => {
                    current_expires_at = id.expires_at;
                    let evt = RefreshEvent::Refreshed {
                        caps: id.granted_capabilities,
                        expires_at: id.expires_at,
                    };
                    if tx.send(evt).await.is_err() {
                        // Adopter dropped the receiver; stream is gone.
                        break;
                    }
                }
                Ok(None) => {
                    let _ = tx
                        .send(RefreshEvent::AuthLost {
                            reason: AuthLostReason::Unknown,
                        })
                        .await;
                    break;
                }
                Err(BrokerError::Expired) => {
                    let _ = tx
                        .send(RefreshEvent::AuthLost {
                            reason: AuthLostReason::Expired,
                        })
                        .await;
                    break;
                }
                Err(BrokerError::Revoked(msg)) => {
                    let _ = tx
                        .send(RefreshEvent::AuthLost {
                            reason: AuthLostReason::Revoked(msg),
                        })
                        .await;
                    break;
                }
                Err(BrokerError::Lookup(msg)) => {
                    // Transient — emit AuthLost(transient) but exit
                    // anyway; adopter decides whether to reconnect.
                    // SSE long-connection design (spec §4.7) prefers
                    // close-and-reconnect over silent retry.
                    let _ = tx
                        .send(RefreshEvent::AuthLost {
                            reason: AuthLostReason::BrokerError {
                                reason: msg,
                                transient: true,
                            },
                        })
                        .await;
                    break;
                }
                Err(BrokerError::Internal(msg)) => {
                    let _ = tx
                        .send(RefreshEvent::AuthLost {
                            reason: AuthLostReason::BrokerError {
                                reason: msg,
                                transient: false,
                            },
                        })
                        .await;
                    break;
                }
                Err(BrokerError::NotConfigured) => {
                    let _ = tx
                        .send(RefreshEvent::AuthLost {
                            reason: AuthLostReason::BrokerError {
                                reason: "broker NotConfigured".into(),
                                transient: false,
                            },
                        })
                        .await;
                    break;
                }
            }
        }
    });

    (handle, rx)
}

/// Compute the next sleep duration: `min(expires_at - now, cadence)`.
/// If `expires_at` is `None`, sleeps the full cadence. If `expires_at`
/// is in the past, returns the cadence (the next `resolve_bearer` will
/// confirm the expiry); we don't burn CPU on a zero-duration sleep loop.
fn next_sleep_duration(cadence: Duration, expires_at: Option<SystemTime>) -> Duration {
    match expires_at {
        Some(t) => {
            let now = SystemTime::now();
            match t.duration_since(now) {
                Ok(until_expiry) => cadence.min(until_expiry),
                Err(_) => cadence, // already expired; next tick discovers
            }
        }
        None => cadence,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use atd_runtime::secrets::{ResolveBearerFuture, ResolveFuture};
    use std::sync::Mutex;

    /// Broker whose `resolve_bearer` response is mutated by tests.
    /// Wrapped in `Arc<Mutex<...>>` so the test thread can flip the
    /// behaviour mid-refresh and the spawned task picks it up on its
    /// next iteration.
    #[derive(Debug, Default)]
    struct MutBroker {
        state: Mutex<MutState>,
    }
    #[derive(Default)]
    enum MutState {
        #[default]
        Ok,
        OkShrunk,
        Expired,
        Revoked(String),
        Unknown,
        Internal(String),
        Lookup(String),
    }
    // Impl Debug for the enum so `derive(Debug)` on the broker works.
    impl std::fmt::Debug for MutState {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            match self {
                Self::Ok => write!(f, "Ok"),
                Self::OkShrunk => write!(f, "OkShrunk"),
                Self::Expired => write!(f, "Expired"),
                Self::Revoked(s) => write!(f, "Revoked({s})"),
                Self::Unknown => write!(f, "Unknown"),
                Self::Internal(s) => write!(f, "Internal({s})"),
                Self::Lookup(s) => write!(f, "Lookup({s})"),
            }
        }
    }
    impl MutBroker {
        fn set(&self, st: MutState) {
            *self.state.lock().unwrap() = st;
        }
    }
    impl TokenBroker for MutBroker {
        fn resolve<'a>(&'a self, _caller_id: Option<&'a str>) -> ResolveFuture<'a> {
            Box::pin(async { Ok(None) })
        }
        fn resolve_bearer<'a>(&'a self, _bearer: &'a str) -> ResolveBearerFuture<'a> {
            let st = std::mem::take(&mut *self.state.lock().unwrap());
            // After read, default back to `Ok` so subsequent ticks find
            // a fresh state if the test hasn't reset it explicitly.
            // (Tests that need a sticky state reset before each tick.)
            *self.state.lock().unwrap() = match &st {
                MutState::Ok => MutState::Ok,
                MutState::OkShrunk => MutState::OkShrunk,
                _ => MutState::default(),
            };
            // Snapshot value we'll respond with
            let snapshot = st;
            Box::pin(async move {
                match snapshot {
                    MutState::Ok => Ok(Some(BearerIdentity {
                        caller_id: "agent-A".into(),
                        granted_capabilities: vec!["records:read".into(), "summary:read".into()],
                        secrets: None,
                        expires_at: None,
                        cache_until: None,
                    })),
                    MutState::OkShrunk => Ok(Some(BearerIdentity {
                        caller_id: "agent-A".into(),
                        granted_capabilities: vec!["records:read".into()],
                        secrets: None,
                        expires_at: None,
                        cache_until: None,
                    })),
                    MutState::Expired => Err(BrokerError::Expired),
                    MutState::Revoked(s) => Err(BrokerError::Revoked(s)),
                    MutState::Unknown => Ok(None),
                    MutState::Internal(s) => Err(BrokerError::Internal(s)),
                    MutState::Lookup(s) => Err(BrokerError::Lookup(s)),
                }
            })
        }
    }

    fn ok_identity() -> BearerIdentity {
        BearerIdentity {
            caller_id: "agent-A".into(),
            granted_capabilities: vec!["records:read".into(), "summary:read".into()],
            secrets: None,
            expires_at: None,
            cache_until: None,
        }
    }

    // ---- next_sleep_duration ----

    #[test]
    fn no_expires_at_sleeps_full_cadence() {
        let dur = next_sleep_duration(Duration::from_secs(60), None);
        assert_eq!(dur, Duration::from_secs(60));
    }

    #[test]
    fn expires_at_in_the_past_returns_cadence_not_zero() {
        let past = SystemTime::now() - Duration::from_secs(100);
        let dur = next_sleep_duration(Duration::from_secs(60), Some(past));
        assert_eq!(dur, Duration::from_secs(60));
    }

    #[test]
    fn expires_at_sooner_than_cadence_clamps_sleep() {
        let soon = SystemTime::now() + Duration::from_secs(5);
        let dur = next_sleep_duration(Duration::from_secs(60), Some(soon));
        // 5 +/- 1s scheduling slack
        assert!(dur < Duration::from_secs(7));
        assert!(dur > Duration::from_secs(3));
    }

    #[test]
    fn expires_at_far_in_future_keeps_cadence() {
        let far = SystemTime::now() + Duration::from_secs(3600);
        let dur = next_sleep_duration(Duration::from_secs(60), Some(far));
        assert_eq!(dur, Duration::from_secs(60));
    }

    // ---- spawn_bearer_refresh ----
    //
    // Tests use very short cadences (50-200ms) so the suite stays fast.

    #[tokio::test]
    async fn happy_refresh_emits_refreshed_with_current_caps() {
        let broker: Arc<dyn TokenBroker> = Arc::new(MutBroker::default());
        let (handle, mut rx) = spawn_bearer_refresh(
            broker.clone(),
            "tok".into(),
            ok_identity(),
            Duration::from_millis(50),
        );

        let evt = tokio::time::timeout(Duration::from_millis(500), rx.recv())
            .await
            .expect("event within 500ms")
            .expect("non-closed channel");

        match evt {
            RefreshEvent::Refreshed { caps, .. } => {
                assert!(caps.contains(&"records:read".to_string()));
                assert!(caps.contains(&"summary:read".to_string()));
            }
            other => panic!("expected Refreshed, got {other:?}"),
        }
        handle.abort();
    }

    #[tokio::test]
    async fn shrunk_caps_propagate_via_refreshed_event() {
        let broker_concrete = Arc::new(MutBroker::default());
        broker_concrete.set(MutState::OkShrunk);
        let broker: Arc<dyn TokenBroker> = broker_concrete.clone();

        let (handle, mut rx) = spawn_bearer_refresh(
            broker,
            "tok".into(),
            ok_identity(), // initial = 2 caps
            Duration::from_millis(50),
        );

        let evt = tokio::time::timeout(Duration::from_millis(500), rx.recv())
            .await
            .expect("event within 500ms")
            .expect("non-closed channel");

        match evt {
            RefreshEvent::Refreshed { caps, .. } => {
                assert_eq!(caps, vec!["records:read".to_string()]);
            }
            other => panic!("expected Refreshed(shrunk), got {other:?}"),
        }
        handle.abort();
    }

    #[tokio::test]
    async fn mid_stream_revoke_emits_auth_lost_revoked_and_closes_channel() {
        let broker_concrete = Arc::new(MutBroker::default());
        let broker: Arc<dyn TokenBroker> = broker_concrete.clone();

        let (handle, mut rx) = spawn_bearer_refresh(
            broker,
            "tok".into(),
            ok_identity(),
            Duration::from_millis(50),
        );

        // First tick should be a happy Refreshed.
        let evt1 = tokio::time::timeout(Duration::from_millis(500), rx.recv())
            .await
            .expect("first event")
            .expect("non-closed");
        assert!(matches!(evt1, RefreshEvent::Refreshed { .. }));

        // Now flip the broker to revoked; next tick should emit AuthLost.
        broker_concrete.set(MutState::Revoked("user revoked".into()));

        let evt2 = tokio::time::timeout(Duration::from_millis(500), rx.recv())
            .await
            .expect("second event")
            .expect("non-closed before AuthLost");
        match evt2 {
            RefreshEvent::AuthLost {
                reason: AuthLostReason::Revoked(msg),
            } => assert_eq!(msg, "user revoked"),
            other => panic!("expected AuthLost(Revoked), got {other:?}"),
        }

        // Channel should close after AuthLost — next recv returns None.
        let evt3 = tokio::time::timeout(Duration::from_millis(500), rx.recv())
            .await
            .expect("channel-close detection within 500ms");
        assert!(evt3.is_none(), "channel must close after AuthLost");

        // Task exited; handle is await-able to confirm completion.
        let _ = handle.await;
    }

    #[tokio::test]
    async fn mid_stream_expired_emits_auth_lost_expired() {
        let broker_concrete = Arc::new(MutBroker::default());
        broker_concrete.set(MutState::Expired);
        let broker: Arc<dyn TokenBroker> = broker_concrete.clone();

        let (handle, mut rx) = spawn_bearer_refresh(
            broker,
            "tok".into(),
            ok_identity(),
            Duration::from_millis(50),
        );

        let evt = tokio::time::timeout(Duration::from_millis(500), rx.recv())
            .await
            .expect("event within 500ms")
            .expect("non-closed");
        assert!(matches!(
            evt,
            RefreshEvent::AuthLost {
                reason: AuthLostReason::Expired
            }
        ));
        handle.abort();
    }

    #[tokio::test]
    async fn broker_internal_error_emits_persistent_auth_lost() {
        let broker_concrete = Arc::new(MutBroker::default());
        broker_concrete.set(MutState::Internal("oh no".into()));
        let broker: Arc<dyn TokenBroker> = broker_concrete.clone();

        let (handle, mut rx) = spawn_bearer_refresh(
            broker,
            "tok".into(),
            ok_identity(),
            Duration::from_millis(50),
        );

        let evt = tokio::time::timeout(Duration::from_millis(500), rx.recv())
            .await
            .expect("event")
            .expect("non-closed");
        match evt {
            RefreshEvent::AuthLost {
                reason: AuthLostReason::BrokerError { transient, .. },
            } => assert!(!transient),
            other => panic!("expected persistent BrokerError, got {other:?}"),
        }
        handle.abort();
    }

    #[tokio::test]
    async fn broker_lookup_error_emits_transient_auth_lost() {
        let broker_concrete = Arc::new(MutBroker::default());
        broker_concrete.set(MutState::Lookup("sqlite locked".into()));
        let broker: Arc<dyn TokenBroker> = broker_concrete.clone();

        let (handle, mut rx) = spawn_bearer_refresh(
            broker,
            "tok".into(),
            ok_identity(),
            Duration::from_millis(50),
        );

        let evt = tokio::time::timeout(Duration::from_millis(500), rx.recv())
            .await
            .expect("event")
            .expect("non-closed");
        match evt {
            RefreshEvent::AuthLost {
                reason: AuthLostReason::BrokerError { transient, .. },
            } => assert!(transient),
            other => panic!("expected transient BrokerError, got {other:?}"),
        }
        handle.abort();
    }

    #[tokio::test]
    async fn unknown_bearer_emits_auth_lost_unknown() {
        let broker_concrete = Arc::new(MutBroker::default());
        broker_concrete.set(MutState::Unknown);
        let broker: Arc<dyn TokenBroker> = broker_concrete.clone();

        let (handle, mut rx) = spawn_bearer_refresh(
            broker,
            "tok".into(),
            ok_identity(),
            Duration::from_millis(50),
        );

        let evt = tokio::time::timeout(Duration::from_millis(500), rx.recv())
            .await
            .expect("event")
            .expect("non-closed");
        assert!(matches!(
            evt,
            RefreshEvent::AuthLost {
                reason: AuthLostReason::Unknown
            }
        ));
        handle.abort();
    }

    #[tokio::test]
    async fn dropping_receiver_aborts_refresh_loop() {
        let broker: Arc<dyn TokenBroker> = Arc::new(MutBroker::default());
        let (handle, rx) = spawn_bearer_refresh(
            broker,
            "tok".into(),
            ok_identity(),
            Duration::from_millis(50),
        );

        // Drop the receiver immediately.
        drop(rx);
        // Wait for the next tick, then the next send fails and the task exits.
        let exited = tokio::time::timeout(Duration::from_millis(500), handle).await;
        assert!(
            exited.is_ok(),
            "task must exit within ~1 cadence after receiver drop"
        );
    }
}