Skip to main content

agentmux_srv_core/muxbus/delivery_status/
machine.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The pure half of [`super`]: what state cloud delivery is in, given what
5//! the cloud subscriber and the credential broker last said, and what to do
6//! when it changes. No clock, no I/O; every call takes `now_ms`.
7
8use std::collections::BTreeMap;
9
10use crate::backend::rpc_types::{MuxBusDeliveryState, MuxBusDeliveryStatus};
11use crate::broker::CredentialState;
12
13/// How long cloud delivery must have been paused before the agents are told:
14/// a reconnect or a token refresh that recovers within this never reaches
15/// them. The status bar's amber "reconnecting" waits the same time.
16pub const PAUSE_NOTICE_MS: i64 = 2 * 60 * 1000;
17
18/// What the cloud subscriber's loop is doing, as it last reported.
19#[derive(Debug, Clone, PartialEq, Eq)]
20pub enum Link {
21    /// Nothing stored to sign in with: the loop waits for a sign-in.
22    NoSignIn,
23    /// A sign-in is stored but can't work anymore (the reason).
24    SignInStale(String),
25    /// Signed in, but the relay isn't reachable or the token can't be
26    /// refreshed for now (the reason). The loop retries on its own.
27    Unreachable(String),
28    /// A sign-in was just made and saved; the loop is on its way to the
29    /// relay. The broker may still hold the previous sign-in's refusal, which
30    /// says nothing about this one.
31    Connecting,
32    /// The WebSocket to the relay is open.
33    Connected,
34}
35
36/// Something [`Machine`] decided the runtime should do.
37#[derive(Debug, Clone, PartialEq, Eq)]
38pub enum Effect {
39    /// The status changed: tell the UI.
40    Changed(MuxBusDeliveryStatus),
41    /// A needs-sign-in episode began: one OS notification.
42    NotifySignInNeeded,
43    /// That episode ended: take the notification down if it's still up.
44    RetractSignInNeeded,
45    /// Cloud delivery has been paused for [`PAUSE_NOTICE_MS`]: tell the
46    /// agents, since `since_ms`, because of `reason`.
47    PauseNote { since_ms: i64, reason: &'static str },
48    /// Delivery came back after a pause the agents were told about.
49    Resumed { at_ms: i64 },
50}
51
52/// The state a set of inputs comes to, and why it isn't connected.
53pub fn derive(
54    session_known: bool,
55    link: &Link,
56    broker: Option<&CredentialState>,
57) -> (MuxBusDeliveryState, Option<String>) {
58    match link {
59        Link::Connected => (MuxBusDeliveryState::Connected, None),
60        _ if !session_known => (MuxBusDeliveryState::SignedOut, None),
61        Link::SignInStale(reason) => (MuxBusDeliveryState::NeedsSignIn, Some(reason.clone())),
62        Link::Connecting => (MuxBusDeliveryState::Reconnecting, Some("connecting".to_string())),
63        // Signed in before, and now nothing readable is stored: the sign-in
64        // was lost, not given up (a deliberate sign-out clears `session_known`).
65        Link::NoSignIn => (
66            MuxBusDeliveryState::NeedsSignIn,
67            Some("the stored sign-in can't be read".to_string()),
68        ),
69        Link::Unreachable(error) => match broker {
70            Some(CredentialState::NeedsReauth { rejected: true, reason, .. }) => {
71                (MuxBusDeliveryState::NeedsSignIn, Some(format!("the sign-in was refused: {reason}")))
72            }
73            _ => (MuxBusDeliveryState::Reconnecting, Some(error.clone())),
74        },
75    }
76}
77
78/// Why the agents' pause note says messages are paused.
79pub fn pause_reason(state: MuxBusDeliveryState) -> &'static str {
80    match state {
81        MuxBusDeliveryState::NeedsSignIn => "MuxBus needs a sign-in",
82        _ => "AgentMux can't reach MuxBus",
83    }
84}
85
86/// Cloud delivery's state over time, and the episodes that drive the
87/// notification and the agents' notes.
88#[derive(Debug, Clone)]
89pub struct Machine {
90    session_known: bool,
91    email: Option<String>,
92    link: Link,
93    /// Agents whose REST pull is failing (agent → reason) though the
94    /// WebSocket may be open: their messages can't be fetched, so that is
95    /// not connected either. An agent's entry clears when its own pull
96    /// answers, not another agent's.
97    pull_errors: BTreeMap<String, String>,
98    status: MuxBusDeliveryStatus,
99    /// When the current pause began; open until delivery is connected again.
100    /// A sign-out ends one only if the agents weren't told about it.
101    paused_since_ms: Option<i64>,
102    /// The agents were told about the current pause.
103    pause_noted: bool,
104    /// The current needs-sign-in episode has had its notification.
105    sign_in_notified: bool,
106}
107
108impl Machine {
109    pub fn new(now_ms: i64) -> Self {
110        Machine {
111            session_known: false,
112            email: None,
113            link: Link::NoSignIn,
114            pull_errors: BTreeMap::new(),
115            status: MuxBusDeliveryStatus { since_ms: now_ms, ..Default::default() },
116            paused_since_ms: None,
117            pause_noted: false,
118            sign_in_notified: false,
119        }
120    }
121
122    pub fn status(&self) -> &MuxBusDeliveryStatus {
123        &self.status
124    }
125
126    /// When the current pause began, if one is open.
127    #[cfg(test)]
128    pub fn paused_since_ms(&self) -> Option<i64> {
129        self.paused_since_ms
130    }
131
132    /// The subscriber found a sign-in to use (`email` when it could read one).
133    pub fn saw_session(&mut self, email: Option<String>, broker: Option<&CredentialState>, now_ms: i64) -> Vec<Effect> {
134        self.session_known = true;
135        if email.as_deref().is_some_and(|e| !e.is_empty()) {
136            self.email = email;
137        }
138        // There is a sign-in now, so the loop is on its way to connecting:
139        // not a lost one (which would notify).
140        if self.link == Link::NoSignIn {
141            self.link = Link::Unreachable("connecting".to_string());
142        }
143        self.update(broker, now_ms)
144    }
145
146    /// The user just signed in (`muxbus.login` saved it): connecting, whatever
147    /// the broker still says about the sign-in this one replaces.
148    pub fn fresh_sign_in(&mut self, email: Option<String>, now_ms: i64) -> Vec<Effect> {
149        self.session_known = true;
150        if email.as_deref().is_some_and(|e| !e.is_empty()) {
151            self.email = email;
152        }
153        self.link = Link::Connecting;
154        self.update(None, now_ms)
155    }
156
157    /// The user signed out on purpose: not a lost sign-in.
158    pub fn signed_out(&mut self, now_ms: i64) -> Vec<Effect> {
159        self.session_known = false;
160        self.email = None;
161        self.link = Link::NoSignIn;
162        self.pull_errors.clear();
163        self.update(None, now_ms)
164    }
165
166    pub fn set_link(&mut self, link: Link, broker: Option<&CredentialState>, now_ms: i64) -> Vec<Effect> {
167        self.link = link;
168        self.update(broker, now_ms)
169    }
170
171    /// The relay answered `agent`'s pull: delivery is healthy as of `now_ms`.
172    pub fn relay_answered(&mut self, agent: &str, broker: Option<&CredentialState>, now_ms: i64) -> Vec<Effect> {
173        self.status.last_ok_ms = Some(now_ms);
174        self.pull_errors.remove(agent);
175        self.update(broker, now_ms)
176    }
177
178    /// `agent`'s pull (fetch or claim) got no usable answer from the relay.
179    pub fn pull_failed(&mut self, agent: &str, reason: String, broker: Option<&CredentialState>, now_ms: i64) -> Vec<Effect> {
180        self.pull_errors.insert(agent.to_string(), reason);
181        self.update(broker, now_ms)
182    }
183
184    /// Forget pull failures of agents no longer subscribed: they won't pull
185    /// again to clear them.
186    pub fn keep_pull_errors_of(&mut self, agents: &[String]) {
187        self.pull_errors.retain(|agent, _| agents.contains(agent));
188    }
189
190    /// Re-derive (the broker may have moved on its own) and release the
191    /// pause note once it is due.
192    pub fn tick(&mut self, broker: Option<&CredentialState>, now_ms: i64) -> Vec<Effect> {
193        let mut out = self.update(broker, now_ms);
194        if let Some(since_ms) = self.paused_since_ms {
195            if !self.pause_noted && self.status.state.is_paused() && now_ms - since_ms >= PAUSE_NOTICE_MS {
196                self.pause_noted = true;
197                out.push(Effect::PauseNote { since_ms, reason: pause_reason(self.status.state) });
198            }
199        }
200        out
201    }
202
203    fn update(&mut self, broker: Option<&CredentialState>, now_ms: i64) -> Vec<Effect> {
204        let link = match (&self.link, self.pull_errors.values().next()) {
205            (Link::Connected, Some(e)) => Link::Unreachable(e.clone()),
206            (link, _) => link.clone(),
207        };
208        let (state, error) = derive(self.session_known, &link, broker);
209        let sign_in_unreachable = matches!(
210            broker,
211            Some(CredentialState::Failed { offline: true, .. } | CredentialState::Refreshing { offline: true, .. })
212        );
213        let mut out = Vec::new();
214        let prev = self.status.state;
215        let changed = state != prev
216            || error != self.status.last_error
217            || self.email != self.status.account_email
218            || sign_in_unreachable != self.status.sign_in_unreachable;
219        if state == MuxBusDeliveryState::Connected || prev == MuxBusDeliveryState::Connected {
220            self.status.last_ok_ms = Some(now_ms);
221        }
222        if state != prev {
223            self.status.since_ms = now_ms;
224            if state == MuxBusDeliveryState::NeedsSignIn && !self.sign_in_notified {
225                self.sign_in_notified = true;
226                out.push(Effect::NotifySignInNeeded);
227            }
228            if matches!(state, MuxBusDeliveryState::Connected | MuxBusDeliveryState::SignedOut) && self.sign_in_notified {
229                self.sign_in_notified = false;
230                out.push(Effect::RetractSignInNeeded);
231            }
232            // A sign-out ends a pause the agents were never told about, so a
233            // later sign-in starts a new one rather than dating it from the
234            // old outage. A pause they were told about stays open for its
235            // resume note.
236            if state == MuxBusDeliveryState::SignedOut && !self.pause_noted {
237                self.paused_since_ms = None;
238            }
239            if state.is_paused() && self.paused_since_ms.is_none() {
240                self.paused_since_ms = Some(now_ms);
241            }
242            if state == MuxBusDeliveryState::Connected {
243                if self.paused_since_ms.take().is_some() && self.pause_noted {
244                    out.push(Effect::Resumed { at_ms: now_ms });
245                }
246                self.pause_noted = false;
247            }
248        }
249        self.status.state = state;
250        self.status.last_error = error;
251        self.status.account_email = self.email.clone();
252        self.status.sign_in_unreachable = sign_in_unreachable;
253        if changed {
254            out.insert(0, Effect::Changed(self.status.clone()));
255        }
256        out
257    }
258}
259
260#[cfg(test)]
261mod tests {
262    use super::*;
263    use MuxBusDeliveryState::*;
264
265    const T0: i64 = 1_791_000_000_000;
266
267    fn refused() -> CredentialState {
268        CredentialState::NeedsReauth { since_unix: 0, reason: "invalid_grant".into(), rejected: true }
269    }
270
271    fn piled_up() -> CredentialState {
272        CredentialState::NeedsReauth { since_unix: 0, reason: "network".into(), rejected: false }
273    }
274
275    fn connected_machine() -> Machine {
276        let mut m = Machine::new(T0);
277        m.saw_session(Some("a@b.c".into()), None, T0);
278        m.set_link(Link::Connected, None, T0);
279        m
280    }
281
282    fn kinds(effects: &[Effect]) -> Vec<&'static str> {
283        effects
284            .iter()
285            .map(|e| match e {
286                Effect::Changed(_) => "changed",
287                Effect::NotifySignInNeeded => "notify",
288                Effect::RetractSignInNeeded => "retract",
289                Effect::PauseNote { .. } => "pause",
290                Effect::Resumed { .. } => "resumed",
291            })
292            .collect()
293    }
294
295    #[test]
296    fn each_state_derives_from_its_inputs() {
297        assert_eq!(derive(true, &Link::Connected, Some(&refused())).0, Connected, "an open link wins");
298        assert_eq!(derive(false, &Link::NoSignIn, None).0, SignedOut);
299        assert_eq!(derive(false, &Link::Unreachable("x".into()), Some(&refused())).0, SignedOut);
300        assert_eq!(derive(true, &Link::NoSignIn, None).0, NeedsSignIn, "a sign-in that vanished");
301        assert_eq!(derive(true, &Link::SignInStale("pool".into()), None), (NeedsSignIn, Some("pool".into())));
302        assert_eq!(derive(true, &Link::Unreachable("timeout".into()), None), (Reconnecting, Some("timeout".into())));
303        assert_eq!(derive(true, &Link::Unreachable("x".into()), Some(&piled_up())).0, Reconnecting);
304        let (state, why) = derive(true, &Link::Unreachable("x".into()), Some(&refused()));
305        assert_eq!(state, NeedsSignIn);
306        assert!(why.unwrap().contains("invalid_grant"));
307    }
308
309    #[test]
310    fn a_channel_never_signed_in_stays_signed_out_and_quiet() {
311        let mut m = Machine::new(T0);
312        let fx = m.set_link(Link::NoSignIn, Some(&refused()), T0 + 1);
313        assert!(fx.is_empty(), "{fx:?}");
314        assert_eq!(m.status().state, SignedOut);
315        assert!(m.tick(Some(&refused()), T0 + 10 * PAUSE_NOTICE_MS).is_empty());
316    }
317
318    #[test]
319    fn transitions_record_when_they_happened() {
320        let mut m = connected_machine();
321        assert_eq!(m.status().state, Connected);
322        assert_eq!(m.status().account_email.as_deref(), Some("a@b.c"));
323        m.set_link(Link::Unreachable("closed".into()), None, T0 + 5_000);
324        assert_eq!((m.status().state, m.status().since_ms), (Reconnecting, T0 + 5_000));
325        assert_eq!(m.status().last_ok_ms, Some(T0 + 5_000), "last ok is when it left connected");
326        m.set_link(Link::SignInStale("refused".into()), None, T0 + 9_000);
327        assert_eq!((m.status().state, m.status().since_ms), (NeedsSignIn, T0 + 9_000));
328        m.set_link(Link::Connected, None, T0 + 20_000);
329        assert_eq!((m.status().state, m.status().since_ms), (Connected, T0 + 20_000));
330        let fx = m.signed_out(T0 + 30_000);
331        assert_eq!(m.status().state, SignedOut);
332        assert_eq!(m.status().account_email, None);
333        assert_eq!(kinds(&fx), ["changed"]);
334    }
335
336    #[test]
337    fn the_broker_alone_can_move_reconnecting_to_needs_sign_in() {
338        let mut m = connected_machine();
339        m.set_link(Link::Unreachable("closed".into()), None, T0 + 1);
340        let fx = m.tick(Some(&refused()), T0 + 2);
341        assert_eq!(m.status().state, NeedsSignIn);
342        assert_eq!(kinds(&fx), ["changed", "notify"]);
343    }
344
345    /// The relay can stay connected while the sign-in server can't be
346    /// reached; the status says so (Sign out asks first then).
347    #[test]
348    fn a_connected_relay_still_reports_an_unreachable_sign_in_server() {
349        let mut m = connected_machine();
350        let offline = CredentialState::Failed { consecutive_failures: 0, last_error: "connect".into(), offline: true };
351        let fx = m.tick(Some(&offline), T0 + 1);
352        assert_eq!(m.status().state, Connected);
353        assert!(m.status().sign_in_unreachable);
354        assert_eq!(kinds(&fx), ["changed"]);
355        // A retry in flight is still offline until something answers.
356        let retrying = CredentialState::Refreshing { prior_failures: 0, prior_reauth: None, offline: true };
357        m.tick(Some(&retrying), T0 + 2);
358        assert!(m.status().sign_in_unreachable);
359        m.tick(Some(&CredentialState::Fresh), T0 + 3);
360        assert!(!m.status().sign_in_unreachable);
361    }
362
363    #[test]
364    fn one_notification_per_needs_sign_in_episode() {
365        let mut m = connected_machine();
366        let fx = m.set_link(Link::SignInStale("refused".into()), None, T0 + 1);
367        assert_eq!(kinds(&fx), ["changed", "notify"]);
368        // Flapping inside the episode does not notify again.
369        m.set_link(Link::Unreachable("x".into()), None, T0 + 2);
370        let fx = m.set_link(Link::SignInStale("refused".into()), None, T0 + 3);
371        assert_eq!(kinds(&fx), ["changed"]);
372        assert!(!kinds(&m.tick(None, T0 + 10 * PAUSE_NOTICE_MS)).contains(&"notify"));
373        // Connected ends it and takes the notification down; the next one notifies.
374        let fx = m.set_link(Link::Connected, None, T0 + 20 * PAUSE_NOTICE_MS);
375        assert!(kinds(&fx).contains(&"retract"));
376        let fx = m.set_link(Link::NoSignIn, None, T0 + 21 * PAUSE_NOTICE_MS);
377        assert_eq!(kinds(&fx), ["changed", "notify"]);
378    }
379
380    #[test]
381    fn signing_out_ends_the_sign_in_episode_without_another_notification() {
382        let mut m = connected_machine();
383        m.set_link(Link::SignInStale("refused".into()), None, T0 + 1);
384        let fx = m.signed_out(T0 + 2);
385        assert_eq!(kinds(&fx), ["changed", "retract"]);
386    }
387
388    #[test]
389    fn the_pause_note_waits_two_minutes_and_goes_out_once() {
390        let mut m = connected_machine();
391        let start = T0 + 1_000;
392        m.set_link(Link::Unreachable("closed".into()), None, start);
393        assert!(!kinds(&m.tick(None, start + PAUSE_NOTICE_MS - 1)).contains(&"pause"));
394        let fx = m.tick(None, start + PAUSE_NOTICE_MS);
395        assert_eq!(fx, [Effect::PauseNote { since_ms: start, reason: "AgentMux can't reach MuxBus" }]);
396        assert!(m.tick(None, start + 5 * PAUSE_NOTICE_MS).is_empty(), "once per pause");
397        // The pause carries on into needs-sign-in: still the same pause, no second note.
398        m.set_link(Link::SignInStale("refused".into()), None, start + 6 * PAUSE_NOTICE_MS);
399        assert!(!kinds(&m.tick(None, start + 9 * PAUSE_NOTICE_MS)).contains(&"pause"));
400        let fx = m.set_link(Link::Connected, None, start + 10 * PAUSE_NOTICE_MS);
401        assert!(fx.contains(&Effect::Resumed { at_ms: start + 10 * PAUSE_NOTICE_MS }));
402    }
403
404    #[test]
405    fn the_pause_reason_follows_the_state_at_note_time() {
406        let mut m = connected_machine();
407        m.set_link(Link::SignInStale("refused".into()), None, T0 + 1);
408        let fx = m.tick(None, T0 + 1 + PAUSE_NOTICE_MS);
409        assert!(fx.contains(&Effect::PauseNote { since_ms: T0 + 1, reason: "MuxBus needs a sign-in" }));
410    }
411
412    #[test]
413    fn a_blip_under_two_minutes_tells_the_agents_nothing() {
414        let mut m = connected_machine();
415        m.set_link(Link::Unreachable("closed".into()), None, T0 + 1);
416        m.tick(None, T0 + PAUSE_NOTICE_MS - 10);
417        let fx = m.set_link(Link::Connected, None, T0 + PAUSE_NOTICE_MS - 5);
418        assert_eq!(kinds(&fx), ["changed"], "no resume without a pause note");
419        assert!(m.tick(None, T0 + 10 * PAUSE_NOTICE_MS).is_empty());
420        assert_eq!(m.paused_since_ms(), None);
421    }
422
423    #[test]
424    fn a_pause_survives_a_sign_out_and_resumes_on_the_next_connect() {
425        let mut m = connected_machine();
426        m.set_link(Link::SignInStale("refused".into()), None, T0);
427        m.tick(None, T0 + PAUSE_NOTICE_MS);
428        m.signed_out(T0 + PAUSE_NOTICE_MS + 1);
429        assert!(m.tick(None, T0 + 4 * PAUSE_NOTICE_MS).is_empty(), "signed out: no further note");
430        let fx = m.saw_session(None, None, T0 + 5 * PAUSE_NOTICE_MS);
431        assert_eq!(m.status().state, Reconnecting, "signing in again is not a lost sign-in");
432        assert!(!kinds(&fx).contains(&"notify"));
433        let fx = m.set_link(Link::Connected, None, T0 + 5 * PAUSE_NOTICE_MS);
434        assert!(kinds(&fx).contains(&"resumed"));
435    }
436
437    #[test]
438    fn a_short_pause_ended_by_a_sign_out_is_not_dated_from_later() {
439        let mut m = connected_machine();
440        m.set_link(Link::Unreachable("closed".into()), None, T0);
441        m.signed_out(T0 + PAUSE_NOTICE_MS / 2);
442        assert_eq!(m.paused_since_ms(), None);
443        let later = T0 + 100 * PAUSE_NOTICE_MS;
444        m.saw_session(None, None, later);
445        assert!(!kinds(&m.tick(None, later + 1)).contains(&"pause"), "no note dated from the old outage");
446        let fx = m.tick(None, later + PAUSE_NOTICE_MS);
447        assert!(fx.contains(&Effect::PauseNote { since_ms: later, reason: pause_reason(Reconnecting) }));
448    }
449
450    #[test]
451    fn an_open_link_whose_pulls_fail_is_not_connected() {
452        let mut m = connected_machine();
453        m.pull_failed("a", "the relay answered 503 to a message fetch".into(), None, T0 + 1);
454        assert_eq!(m.status().state, Reconnecting);
455        assert_eq!(m.status().last_error.as_deref(), Some("the relay answered 503 to a message fetch"));
456        let fx = m.tick(None, T0 + 1 + PAUSE_NOTICE_MS);
457        assert!(kinds(&fx).contains(&"pause"), "the agents are told, as for any pause");
458        m.relay_answered("b", None, T0 + 2 * PAUSE_NOTICE_MS - 1);
459        assert_eq!(m.status().state, Reconnecting, "another agent's pull doesn't clear a's failure");
460        let fx = m.relay_answered("a", None, T0 + 2 * PAUSE_NOTICE_MS);
461        assert_eq!(m.status().state, Connected);
462        assert!(kinds(&fx).contains(&"resumed"));
463        // A failing pull while the link itself is down changes nothing more.
464        m.set_link(Link::Unreachable("closed".into()), None, T0 + 3 * PAUSE_NOTICE_MS);
465        m.pull_failed("a", "x".into(), None, T0 + 3 * PAUSE_NOTICE_MS + 1);
466        assert_eq!(m.status().last_error.as_deref(), Some("closed"));
467        // An agent that leaves can't clear its own failure; it is forgotten.
468        m.set_link(Link::Connected, None, T0 + 4 * PAUSE_NOTICE_MS);
469        assert_eq!(m.status().state, Reconnecting);
470        m.keep_pull_errors_of(&["b".to_string()]);
471        m.tick(None, T0 + 4 * PAUSE_NOTICE_MS + 1);
472        assert_eq!(m.status().state, Connected);
473    }
474
475    #[test]
476    fn a_fresh_sign_in_is_not_judged_by_the_refusal_it_replaces() {
477        let mut m = connected_machine();
478        m.set_link(Link::Unreachable("x".into()), Some(&refused()), T0 + 1);
479        assert_eq!(m.status().state, NeedsSignIn);
480        m.signed_out(T0 + 2);
481        let fx = m.fresh_sign_in(Some("a@b.c".into()), T0 + 3);
482        assert_eq!(m.status().state, Reconnecting);
483        assert!(!kinds(&fx).contains(&"notify"));
484        // The broker still holds the old refusal until the loop refreshes.
485        assert_eq!(m.tick(Some(&refused()), T0 + 4).len(), 0);
486        assert_eq!(m.status().state, Reconnecting);
487        m.set_link(Link::Connected, Some(&refused()), T0 + 5);
488        assert_eq!(m.status().state, Connected);
489    }
490
491    #[test]
492    fn an_unchanged_report_changes_nothing() {
493        let mut m = connected_machine();
494        assert!(m.set_link(Link::Connected, None, T0 + 1).is_empty());
495        assert!(m.saw_session(Some("a@b.c".into()), None, T0 + 2).is_empty());
496    }
497    #[test]
498    fn a_socket_that_went_silent_is_paused_then_noted_once_and_resumed_once() {
499        let offline = CredentialState::Failed { consecutive_failures: 0, last_error: "dns".into(), offline: true };
500        let mut m = connected_machine();
501        // The watchdog closed the silent socket; the refresh cannot reach the server either.
502        let fx = m.set_link(Link::Unreachable("no word from the relay for 541s".into()), Some(&offline), T0 + 1_000);
503        assert_eq!(kinds(&fx), vec!["changed"]);
504        assert_eq!(m.status().state, Reconnecting);
505        assert!(m.status().sign_in_unreachable, "the sign-in cannot be refreshed");
506        assert!(m.tick(Some(&offline), T0 + PAUSE_NOTICE_MS - 1).is_empty(), "nothing before the pause is due");
507        let fx = m.tick(Some(&offline), T0 + 1_000 + PAUSE_NOTICE_MS);
508        assert_eq!(kinds(&fx), vec!["pause"], "the agents are told once the pause is two minutes old");
509        assert!(m.tick(Some(&offline), T0 + 10 * PAUSE_NOTICE_MS).is_empty(), "and only once");
510        // The network is back: the refresh worked and the socket is open again.
511        let fx = m.set_link(Link::Connected, Some(&CredentialState::Fresh), T0 + 11 * PAUSE_NOTICE_MS);
512        assert_eq!(kinds(&fx), vec!["changed", "resumed"]);
513        assert_eq!(m.status().state, Connected);
514        assert!(!m.status().sign_in_unreachable, "cleared by the first success");
515    }
516}