scv-server 0.3.13

Authoritative session and tool server for SCV
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
//! Unit tests for `src/components.rs`.

use super::*;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};

/// Wait for `condition`, polling, rather than for a fixed time that a busy
/// machine may overrun.
async fn wait_until(mut condition: impl FnMut() -> bool) {
    tokio::time::timeout(Duration::from_secs(10), async {
        while !condition() {
            tokio::time::sleep(Duration::from_millis(1)).await;
        }
    })
    .await
    .expect("condition was not met in time");
}

struct Fake {
    starts: Arc<AtomicUsize>,
    stops: Arc<AtomicUsize>,
    fail_first: bool,
}
#[async_trait]
impl Component for Fake {
    async fn run(&self, cancellation: CancellationToken, health: HealthReporter) -> Result<()> {
        let attempt = self.starts.fetch_add(1, Ordering::SeqCst);
        if self.fail_first && attempt == 0 {
            bail!("secret error must never enter status");
        }
        health.contact(true);
        cancellation.cancelled().await;
        self.stops.fetch_add(1, Ordering::SeqCst);
        Ok(())
    }
}

#[tokio::test]
async fn starts_once_recovers_reports_contact_and_joins_before_restoration() {
    let starts = Arc::new(AtomicUsize::new(0));
    let stops = Arc::new(AtomicUsize::new(0));
    let fake = Arc::new(Fake {
        starts: starts.clone(),
        stops: stops.clone(),
        fail_first: true,
    });
    let mut supervisor = Supervisor {
        initial_backoff: Duration::from_millis(10),
        ..Supervisor::default()
    };
    supervisor.start(
        fake.clone(),
        initial_health(ChannelKind::Wechat, "test", None, true),
    );
    supervisor.start(
        fake.clone(),
        initial_health(ChannelKind::Wechat, "test", None, true),
    );
    tokio::time::timeout(Duration::from_secs(2), async {
        loop {
            if supervisor.health()[0].state == ComponentState::Connected {
                break;
            }
            tokio::time::sleep(Duration::from_millis(1)).await;
        }
    })
    .await
    .unwrap();
    let health = &supervisor.health()[0];
    assert_eq!(starts.load(Ordering::SeqCst), 2);
    assert_eq!(health.restarts, 1);
    assert!(health.last_success_unix_seconds.is_some());
    assert!(health.error.is_none());
    supervisor.shutdown().await;
    assert_eq!(stops.load(Ordering::SeqCst), 1);
    supervisor.start(
        fake,
        initial_health(ChannelKind::Wechat, "test", None, true),
    );
    wait_until(|| starts.load(Ordering::SeqCst) == 3).await;
    supervisor.shutdown().await;
    assert_eq!(starts.load(Ordering::SeqCst), 3);
    assert_eq!(stops.load(Ordering::SeqCst), 2);
}

#[test]
fn credentials_are_not_connection_evidence() {
    let health = initial_health(ChannelKind::Wechat, "saved", None, true);
    assert_eq!(health.state, ComponentState::Starting);
    assert_eq!(health.last_success_unix_seconds, None);
}

#[tokio::test]
async fn busy_account_snapshot_preserves_live_work_but_invalid_settings_stop_it() {
    let starts = Arc::new(AtomicUsize::new(0));
    let stops = Arc::new(AtomicUsize::new(0));
    let mut components = Components::new(
        crate::test_support::test_instance("/unused"),
        PathBuf::from("/"),
    );
    components.supervisor.start(
        Arc::new(Fake {
            starts: starts.clone(),
            stops: stops.clone(),
            fail_first: false,
        }),
        initial_health(ChannelKind::Wechat, "test", None, true),
    );
    tokio::time::timeout(Duration::from_secs(1), async {
        while starts.load(Ordering::SeqCst) == 0 {
            tokio::task::yield_now().await;
        }
    })
    .await
    .unwrap();
    components
        .account_error(
            ChannelKind::Wechat,
            "test",
            std::io::Error::from(std::io::ErrorKind::WouldBlock).into(),
        )
        .await;
    assert_eq!(
        components.status().components[0].state,
        ComponentState::Connected
    );
    assert_eq!(starts.load(Ordering::SeqCst), 1);
    assert_eq!(stops.load(Ordering::SeqCst), 0);
    components
        .account_error(
            ChannelKind::Wechat,
            "test",
            anyhow::anyhow!("invalid settings"),
        )
        .await;
    assert_eq!(stops.load(Ordering::SeqCst), 1);
    assert_eq!(
        components.status().components[0].state,
        ComponentState::Failed
    );
}

struct Stubborn;
#[async_trait]
impl Component for Stubborn {
    async fn run(&self, _: CancellationToken, _: HealthReporter) -> Result<()> {
        std::future::pending().await
    }
}

#[tokio::test]
async fn bounded_stop_aborts_uncooperative_component_and_cancels_backoff() {
    let mut supervisor = Supervisor {
        grace: Duration::from_millis(20),
        ..Supervisor::default()
    };
    supervisor.start(
        Arc::new(Stubborn),
        initial_health(ChannelKind::Wechat, "stubborn", None, true),
    );
    tokio::task::yield_now().await;
    tokio::time::timeout(Duration::from_secs(1), supervisor.shutdown())
        .await
        .unwrap();
    assert!(supervisor.health().is_empty());
    let fake = Arc::new(Fake {
        starts: Arc::new(AtomicUsize::new(0)),
        stops: Arc::new(AtomicUsize::new(0)),
        fail_first: true,
    });
    supervisor.start(
        fake,
        initial_health(ChannelKind::Wechat, "backoff", None, true),
    );
    // The first run fails at once; the one-second backoff that follows is
    // long enough to observe.
    wait_until(|| supervisor.health()[0].state == ComponentState::Backoff).await;
    assert_eq!(
        supervisor.health()[0].error.as_deref(),
        Some("Component stopped unexpectedly; retrying")
    );
    tokio::time::timeout(Duration::from_millis(100), supervisor.shutdown())
        .await
        .unwrap();
}

#[test]
fn remote_tools_require_owner_mode_and_known_owner() {
    let wechat = |user_id: Option<&str>| {
        ChannelCredentials::Wechat(scv_channels::wechat::Account {
            token: "token".into(),
            base_url: "https://example.invalid".into(),
            bot_id: Some("bot".into()),
            user_id: user_id.map(Into::into),
        })
    };
    let feishu = |owner: Option<&str>| {
        ChannelCredentials::Feishu(scv_channels::feishu::Account {
            app_id: "cli_a1b2".into(),
            app_secret: "secret".into(),
            brand: scv_channels::feishu::Brand::Feishu,
            owner_open_id: owner.map(Into::into),
        })
    };
    let owner = AccountSettings {
        remote_tools: RemoteTools::Owner,
        ..Default::default()
    };
    assert_eq!(
        tool_owner(&wechat(Some("owner@im.wechat")), &owner).as_deref(),
        Some("owner@im.wechat")
    );
    assert_eq!(tool_owner(&wechat(None), &owner), None);
    assert_eq!(tool_owner(&wechat(Some("")), &owner), None);
    assert_eq!(
        tool_owner(
            &wechat(Some("owner@im.wechat")),
            &AccountSettings::default()
        ),
        None
    );
    assert_eq!(
        tool_owner(&feishu(Some("ou_owner")), &owner).as_deref(),
        Some("ou_owner")
    );
    assert_eq!(tool_owner(&feishu(None), &owner), None);
    assert_eq!(
        tool_owner(&feishu(Some("ou_owner")), &AccountSettings::default()),
        None
    );
}

#[test]
fn channels_are_named_and_health_shows_the_app_and_owner() {
    assert_eq!(ChannelKind::parse("wechat").unwrap(), ChannelKind::Wechat);
    assert_eq!(ChannelKind::parse("feishu").unwrap(), ChannelKind::Feishu);
    assert!(ChannelKind::parse("lark").is_err());
    let credentials = ChannelCredentials::Feishu(scv_channels::feishu::Account {
        app_id: "cli_a1b2".into(),
        app_secret: "secret".into(),
        brand: scv_channels::feishu::Brand::Feishu,
        owner_open_id: Some("ou_owner".into()),
    });
    let health = initial_health(ChannelKind::Feishu, "default", Some(&credentials), true);
    assert_eq!(health.id, "feishu:default");
    assert_eq!(health.channel, "feishu");
    assert_eq!(health.bot_id.as_deref(), Some("cli_a1b2"));
    assert_eq!(health.user_id.as_deref(), Some("ou_owner"));
    assert!(!serde_json::to_string(&health).unwrap().contains("secret"));
}

#[tokio::test]
async fn a_cancelled_channel_account_stops_before_touching_anything() {
    let cancellation = CancellationToken::new();
    cancellation.cancel();
    // Everything here is invalid, so any work at all would fail the run.
    let account = ChannelAccount {
        kind: ChannelKind::Wechat,
        account: "../invalid".into(),
        credentials: ChannelCredentials::Wechat(scv_channels::wechat::Account {
            token: "token".into(),
            base_url: "invalid".into(),
            bot_id: None,
            user_id: None,
        }),
        settings: AccountSettings::default(),
        workspace: "/".into(),
        instance: crate::test_support::test_instance("/missing"),
        tools: false,
        link: scv_channels::hub::Link::detached(),
    };
    let health = HealthReporter(Arc::new(Mutex::new(initial_health(
        ChannelKind::Wechat,
        "invalid",
        None,
        true,
    ))));
    account.run(cancellation, health.clone()).await.unwrap();
    assert_eq!(health.snapshot().state, ComponentState::Starting);
}

#[test]
fn a_mailbox_and_a_mail_chat_never_take_tools_or_other_senders() {
    use scv_protocol::Purpose;
    let settings = |remote_tools, senders, purpose| AccountSettings {
        remote_tools,
        senders,
        purpose,
        ..Default::default()
    };
    let chat = settings(RemoteTools::Owner, Senders::Anyone, Purpose::Chat);
    assert_eq!(refusal(ChannelKind::Feishu, &chat), None);
    for (remote_tools, senders) in [
        (RemoteTools::Owner, Senders::Owner),
        (RemoteTools::None, Senders::Anyone),
    ] {
        assert!(
            refusal(
                ChannelKind::Feishu,
                &settings(remote_tools, senders, Purpose::Mail)
            )
            .is_some()
        );
        assert!(
            refusal(
                ChannelKind::Email,
                &settings(remote_tools, senders, Purpose::Chat)
            )
            .is_some()
        );
    }
    let quiet = settings(RemoteTools::None, Senders::Owner, Purpose::Mail);
    assert_eq!(refusal(ChannelKind::Feishu, &quiet), None);
    assert!(refusal(ChannelKind::Email, &quiet).is_some());
    assert_eq!(
        refusal(ChannelKind::Email, &AccountSettings::default()),
        None
    );
}

#[tokio::test]
async fn status_shows_an_email_accounts_counts_and_nothing_else() {
    let hub = scv_channels::hub::Hub::new(None);
    let components = Components::with_hub(
        crate::test_support::test_instance("/unused"),
        std::path::PathBuf::from("/"),
        Arc::clone(&hub),
    )
    .unwrap();
    let registration = hub.register_mail("email:default", vec!["feishu:mail".into()]);
    registration.set_counts(scv_protocol::MailCounts {
        queued: 3,
        ..Default::default()
    });
    assert_eq!(hub.mail_counts("email:default").unwrap().queued, 3);
    drop(registration);
    assert_eq!(hub.mail_counts("email:default"), None);
    assert!(components.status().components.is_empty());
}

#[tokio::test]
async fn creating_a_project_starts_the_supervised_orchestrator() {
    let home = tempfile::tempdir().unwrap();
    let workspace = tempfile::tempdir().unwrap();
    let mut components = Components::new(
        crate::test_support::test_instance(home.path()),
        workspace.path().to_path_buf(),
    );
    let status = components
        .control(DaemonCommand::ProjectCreate {
            name: "orchestrated".into(),
            workspace: workspace.path().display().to_string(),
        })
        .await
        .unwrap();
    assert!(
        status
            .components
            .iter()
            .any(|component| component.id == "project:orchestrator")
    );
    components.shutdown().await;
}

#[test]
fn slack_contact_failure_has_actionable_status_and_success_clears_it() {
    let health = super::initial_health(scv_channels::ChannelKind::Slack, "test", None, true);
    let reporter = super::HealthReporter(std::sync::Arc::new(std::sync::Mutex::new(health)));
    reporter.contact(false);
    let snapshot = reporter.snapshot();
    assert_eq!(snapshot.state, scv_protocol::ComponentState::Disconnected);
    assert!(snapshot.error.unwrap().contains("Socket Mode is on"));
    reporter.contact(true);
    assert!(reporter.snapshot().error.is_none());
}

struct TimedStop {
    grace: Option<Duration>,
    /// How long the run takes to return after it is cancelled.
    linger: Duration,
    started: Arc<AtomicBool>,
    finished: Arc<AtomicBool>,
}

#[async_trait]
impl Component for TimedStop {
    async fn run(&self, cancellation: CancellationToken, _: HealthReporter) -> Result<()> {
        self.started.store(true, Ordering::SeqCst);
        cancellation.cancelled().await;
        tokio::time::sleep(self.linger).await;
        self.finished.store(true, Ordering::SeqCst);
        Ok(())
    }

    fn stop_grace(&self) -> Option<Duration> {
        self.grace
    }
}

/// Start `component` and stop it once `started` is set. The supervisor's own
/// grace is `supervisor_grace`; the component may override it.
async fn stop_once_started(
    component: Arc<dyn Component>,
    started: &AtomicBool,
    id: &str,
    supervisor_grace: Duration,
) {
    let mut supervisor = Supervisor {
        grace: supervisor_grace,
        ..Supervisor::default()
    };
    supervisor.start(
        component,
        initial_health(ChannelKind::Wechat, id, None, true),
    );
    wait_until(|| started.load(Ordering::SeqCst)).await;
    tokio::time::timeout(Duration::from_secs(2), supervisor.shutdown())
        .await
        .expect("stop finished within the grace under test");
}

#[tokio::test]
async fn a_component_is_stopped_with_its_own_grace_or_the_supervisor_default() {
    let started = Arc::new(AtomicBool::new(false));
    let finished = Arc::new(AtomicBool::new(false));
    stop_once_started(
        Arc::new(TimedStop {
            // Longer than the supervisor's 40ms, and longer than the linger.
            grace: Some(Duration::from_secs(2)),
            linger: Duration::from_millis(150),
            started: Arc::clone(&started),
            finished: Arc::clone(&finished),
        }),
        &started,
        "own-grace",
        Duration::from_millis(40),
    )
    .await;
    assert!(
        finished.load(Ordering::SeqCst),
        "a run that returns within its own grace is joined, not aborted"
    );

    let started = Arc::new(AtomicBool::new(false));
    let finished = Arc::new(AtomicBool::new(false));
    stop_once_started(
        Arc::new(TimedStop {
            grace: None,
            linger: Duration::from_secs(30),
            started: Arc::clone(&started),
            finished: Arc::clone(&finished),
        }),
        &started,
        "default-grace",
        Duration::from_millis(40),
    )
    .await;
    assert!(
        !finished.load(Ordering::SeqCst),
        "with no grace of its own, the supervisor's grace aborts a run that outlasts it"
    );

    let started = Arc::new(AtomicBool::new(false));
    let finished = Arc::new(AtomicBool::new(false));
    stop_once_started(
        Arc::new(TimedStop {
            grace: Some(Duration::from_millis(40)),
            linger: Duration::from_secs(30),
            started: Arc::clone(&started),
            finished: Arc::clone(&finished),
        }),
        &started,
        "short-grace",
        Duration::from_secs(30),
    )
    .await;
    assert!(
        !finished.load(Ordering::SeqCst),
        "a run that outlasts its own grace is aborted even when the supervisor default is longer"
    );
    assert_eq!(ChannelKind::Wechat.stop_grace(), None);
    assert_eq!(
        ChannelKind::Email.stop_grace(),
        Some(scv_channels::email::STOP_GRACE)
    );
}

#[tokio::test]
async fn a_cancelled_email_account_is_not_dropped_before_its_own_run_returns() {
    let cancellation = CancellationToken::new();
    cancellation.cancel();
    let account = ChannelAccount {
        kind: ChannelKind::Email,
        account: "../invalid".into(),
        credentials: ChannelCredentials::Email(scv_channels::email::Account::Imap {
            host: "example.invalid".into(),
            port: 993,
            username: "user".into(),
            password: "secret".into(),
            address: None,
            smtp: None,
        }),
        settings: AccountSettings::default(),
        workspace: "/".into(),
        instance: crate::test_support::test_instance("/missing"),
        tools: false,
        link: scv_channels::hub::Link::detached(),
    };
    assert_eq!(
        account.stop_grace(),
        Some(scv_channels::email::STOP_GRACE),
        "an email account asks for long enough to finish an action"
    );
    let health = HealthReporter(Arc::new(Mutex::new(initial_health(
        ChannelKind::Email,
        "invalid",
        None,
        true,
    ))));
    let error = account
        .run(cancellation, health.clone())
        .await
        .expect_err("an invalid account name fails inside the email run");
    let message = format!("{error:#}");
    assert!(
        message.contains("invalid channel account name"),
        "cancellation must not skip the email run: {message}"
    );
    assert!(
        !message.contains("secret"),
        "the failure must not carry the mailbox secret: {message}"
    );
    assert_eq!(health.snapshot().state, ComponentState::Disconnected);
}