errand-bot 0.1.0

Run a coding agent from a chat channel, in a sandbox it cannot escape.
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
//! Tests for the interface server, ported from `server_test.ts`.

use std::future::Future;
use std::pin::Pin;
use std::sync::{Arc, Mutex};

use serde_json::{Value, json};
use tokio::sync::watch;

use super::{Assets, WebServer};
use crate::admission::scheduler::{Clock, Scheduler, Timer};
use crate::agent::client::AgentProcess;
use crate::config::schema::SandboxBackend;
use crate::config::schema::WebConfig;
use crate::config::validate::validate_config;
use crate::log::{LogFields, Logger};
use crate::sandbox::backend::{
    CapabilityReport, SandboxLaunch, SandboxLaunchError, SandboxUnavailableError,
};
use crate::sandbox::paths;
use crate::session::event::EndReason;
use crate::session::event::SessionEvent;
use crate::session::manager::{
    CreatedThread, FoundView, MadeThread, ManagerOptions, SandboxPool, SessionManager,
    ThreadFactory,
};
use crate::session::registry::ThreadRegistry;
use crate::session::session::{IncomingMessage, RunningBox};
use crate::session::views::{SessionView, ViewError};

const OWNER: &str = "100000000000000001";

fn silent() -> Logger {
    Logger::new(LogFields::new(), Arc::new(|_level, _line| {}))
}

/// An agent that answers its readiness call and then says nothing.
struct QuietAgent {
    queue: Mutex<std::collections::VecDeque<u8>>,
    closed: Mutex<bool>,
    exit: watch::Receiver<Option<i32>>,
}

#[derive(Clone)]
struct QuietControls {
    fake: Arc<QuietAgent>,
    sender: Arc<Mutex<Option<watch::Sender<Option<i32>>>>>,
}

impl QuietAgent {
    fn new() -> (Arc<Self>, QuietControls) {
        let (exit_sender, exit_receiver) = watch::channel(None);
        let fake = Arc::new(Self {
            queue: Mutex::new(std::collections::VecDeque::new()),
            closed: Mutex::new(false),
            exit: exit_receiver.clone(),
        });
        let controls = QuietControls {
            fake: Arc::clone(&fake),
            sender: Arc::new(Mutex::new(Some(exit_sender))),
        };
        (fake, controls)
    }
}

impl QuietControls {
    fn end(&self, code: i32) {
        {
            let mut closed = self.fake.closed.lock().unwrap();
            if *closed {
                return;
            }
            *closed = true;
        }
        self.fake.queue.lock().unwrap().clear();
        if let Some(sender) = self.sender.lock().unwrap().take() {
            let _ = sender.send(Some(code));
        }
    }
}

impl AgentProcess for QuietAgent {
    fn write(&self, bytes: &[u8]) -> std::io::Result<()> {
        let line = String::from_utf8_lossy(bytes).trim().to_owned();
        let id = serde_json::from_str::<Value>(&line)
            .ok()
            .and_then(|parsed| parsed.get("id").cloned());
        if let Some(id) = id {
            self.queue.lock().unwrap().extend(
                json!({ "type": "response", "id": id, "success": true })
                    .to_string()
                    .into_bytes(),
            );
            self.queue.lock().unwrap().push_back(b'\n');
        }
        Ok(())
    }

    fn read_stdout<'a>(
        &'a self,
        buf: &'a mut [u8],
    ) -> Pin<Box<dyn Future<Output = std::io::Result<usize>> + Send + 'a>> {
        Box::pin(async move {
            loop {
                {
                    let mut queue = self.queue.lock().unwrap();
                    let count = queue.len().min(buf.len());
                    if count > 0 {
                        for (index, byte) in queue.drain(..count).enumerate() {
                            buf[index] = byte;
                        }
                        return Ok(count);
                    }
                    if *self.closed.lock().unwrap() {
                        return Ok(0);
                    }
                }
                tokio::time::sleep(std::time::Duration::from_millis(1)).await;
            }
        })
    }

    fn read_stderr<'a>(
        &'a self,
        _buf: &'a mut [u8],
    ) -> Pin<Box<dyn Future<Output = std::io::Result<usize>> + Send + 'a>> {
        Box::pin(async { Ok(0) })
    }

    fn exited(&self) -> Pin<Box<dyn Future<Output = Option<i32>> + Send>> {
        let mut receiver = self.exit.clone();
        Box::pin(async move {
            let _ = receiver.changed().await;
            *receiver.borrow()
        })
    }
}

struct FakeSandbox;

impl SandboxPool for FakeSandbox {
    fn probe(
        &self,
    ) -> Pin<Box<dyn Future<Output = Result<CapabilityReport, SandboxUnavailableError>> + Send + '_>>
    {
        Box::pin(async move {
            Ok(CapabilityReport {
                backend: SandboxBackend::Bailey,
                gaps: Vec::new(),
                notes: Vec::new(),
            })
        })
    }

    fn launch(
        self: Arc<Self>,
        launch: SandboxLaunch,
    ) -> Pin<Box<dyn Future<Output = Result<RunningBox, SandboxLaunchError>> + Send>> {
        Box::pin(async move {
            let (_agent, controls) = QuietAgent::new();
            let project_path = launch.project_path.clone();
            Ok(RunningBox {
                process: controls.fake.clone(),
                to_host_path: Arc::new(move |path: &str| {
                    paths::host_path_under("/workspace", &project_path, path)
                }),
                stop: Arc::new(move || {
                    controls.end(0);
                    Box::pin(async { false }) as Pin<Box<dyn Future<Output = bool> + Send>>
                }),
            })
        })
    }

    fn list_orphans(&self) -> Pin<Box<dyn Future<Output = Vec<String>> + Send + '_>> {
        Box::pin(async { Vec::new() })
    }

    fn remove_orphans<'a>(
        &'a self,
        names: &'a [String],
    ) -> Pin<Box<dyn Future<Output = usize> + Send + 'a>> {
        Box::pin(async move { names.len() })
    }
}

struct QuietThreadView;

impl SessionView for QuietThreadView {
    fn observe<'a>(
        &'a self,
        _event: &'a SessionEvent,
    ) -> Pin<Box<dyn Future<Output = Result<(), ViewError>> + Send + 'a>> {
        Box::pin(async { Ok(()) })
    }
}

struct FakeThreads {
    next: Mutex<u32>,
}

impl ThreadFactory for FakeThreads {
    fn create(self: Arc<Self>, _message: IncomingMessage, name: String) -> MadeThread {
        let _ = name;
        Box::pin(async move {
            let id = {
                let mut next = self.next.lock().unwrap();
                let id = format!("thread-{next}");
                *next += 1;
                id
            };
            Ok(CreatedThread {
                id,
                view: Arc::new(QuietThreadView),
            })
        })
    }

    fn open(self: Arc<Self>, name: String, _opener: String) -> MadeThread {
        Box::pin(async move {
            let id = {
                let mut next = self.next.lock().unwrap();
                let id = format!("thread-{next}");
                *next += 1;
                id
            };
            let _ = name;
            Ok(CreatedThread {
                id,
                view: Arc::new(QuietThreadView),
            })
        })
    }

    fn port_for(self: Arc<Self>, thread_id: String) -> FoundView {
        Box::pin(async move {
            thread_id
                .starts_with("thread-")
                .then(|| Arc::new(QuietThreadView) as Arc<dyn SessionView>)
        })
    }

    fn release(&self, _thread_id: &str) {}
}

struct NoClock;

impl Clock for NoClock {
    fn now(&self) -> i64 {
        0
    }

    fn set_timeout(&self, _action: Timer, _ms: i64) -> u64 {
        0
    }

    fn clear_timeout(&self, _handle: u64) {}
}

struct Harness {
    server: Arc<WebServer>,
    manager: Arc<SessionManager>,
    root: tempfile::TempDir,
    base: String,
}

/// Stands in for the built interface, with the two files a test asks for.
static BUILT_FOR_TESTS: Assets = &[
    ("app.js", b"export const ready = true;\n"),
    ("index.html", b"<!doctype html><title>errand</title>"),
];

async fn with_server_where(
    observer: bool,
    assets: bool,
    run: impl FnOnce(&Harness) -> Pin<Box<dyn Future<Output = ()> + Send + '_>>,
) {
    let root = tempfile::tempdir().expect("a temp directory");
    // The interface a test serves, so what is asserted does not depend on
    // whether the real bundle happened to be built on this machine.
    let bundle: Assets = if assets { BUILT_FOR_TESTS } else { &[] };

    // A port nothing else is likely to be on, reserved and then released.
    let probe = std::net::TcpListener::bind("127.0.0.1:0").expect("a probe port");
    let port = probe.local_addr().expect("the probe address").port();
    drop(probe);

    let settings = validate_config(&json!({
        "chat": {
            "token": "a.token.value",
            "channelId": "chan",
            "allowedUserIds": [OWNER],
        },
        "agent": {
            "provider": "anthropic",
            "credentialName": "ANTHROPIC_API_KEY",
            "credential": "secret",
        },
        "projectRoot": root.path().join("projects").display().to_string(),
        "stateDir": root.path().join("state").display().to_string(),
        "web": {
            "host": "127.0.0.1",
            "port": port,
            "observer": observer,
        },
    }))
    .expect("the test configuration is accepted");
    let web_config = settings.web.clone().expect("the web settings");

    let registry = Arc::new(Mutex::new(ThreadRegistry::new(
        ThreadRegistry::path_for(root.path().join("state").to_str().expect("a state path")),
        silent(),
    )));
    let scheduler = Scheduler::start(
        settings.limits.clone(),
        Arc::new(NoClock),
        5_000,
        300_000,
        750,
    );
    let id = Mutex::new(0);

    let manager = Arc::new(SessionManager::new(ManagerOptions {
        config: settings,
        sandbox: Arc::new(FakeSandbox) as Arc<dyn SandboxPool>,
        scheduler: Arc::clone(&scheduler),
        threads: Arc::new(FakeThreads {
            next: Mutex::new(1),
        }) as Arc<dyn ThreadFactory>,
        registry,
        log: silent(),
        make_id: Some(Arc::new(move || {
            let mut id = id.lock().unwrap();
            *id += 1;
            format!("s{id}")
        })),
        unavailable: None,
        operator_ids: None,
        memory: None,
        describe_images: None,
        public_url: None,
        available_models: Vec::new(),
        delegate_base_url: None,
        now: Some(Arc::new(|| 1_000)),
    }));

    let server = WebServer::new(
        web_config,
        Arc::clone(&manager),
        bundle,
        silent(),
        Some("guild-1".to_owned()),
        Some(Arc::new(|id: &str| {
            (id == OWNER).then(|| "amelia".to_owned())
        })),
    );
    if assets {
        server.start().await.expect("the interface starts");
    }
    let base = match server.bound_port() {
        Some(port) => format!("http://127.0.0.1:{port}"),
        None => String::new(),
    };

    let harness = Harness {
        server,
        manager,
        root,
        base,
    };
    run(&harness).await;

    harness.server.stop();
    harness.manager.shutdown().await;
}

async fn message(client: &reqwest::Client, base: &str, path: &str) -> reqwest::Response {
    client
        .get(format!("{base}{path}"))
        .send()
        .await
        .expect("the request answers")
}

async fn post(client: &reqwest::Client, base: &str, path: &str, body: &Value) -> reqwest::Response {
    client
        .post(format!("{base}{path}"))
        .json(body)
        .send()
        .await
        .expect("the request answers")
}

async fn started_session(harness: &Harness, _client: &reqwest::Client) {
    let outcome = harness
        .manager
        .start(IncomingMessage {
            id: "m1".to_owned(),
            author_id: OWNER.to_owned(),
            author_name: Some("amelia".to_owned()),
            content: "demo: go".to_owned(),
            attachments: Vec::new(),
        })
        .await;
    assert!(outcome.is_started(), "the session starts");
}

#[tokio::test]
async fn the_interface_says_what_it_will_and_will_not_allow() {
    with_server_where(false, true, |harness| {
        Box::pin(async move {
            let client = reqwest::Client::new();
            let answer = message(&client, &harness.base, "/api/interface").await;

            assert_eq!(
                answer.json::<Value>().await.unwrap(),
                json!({ "observer": false, "guildId": "guild-1" })
            );
        })
    })
    .await;
}

#[tokio::test]
async fn a_session_that_is_running_is_listed_with_what_it_was_asked() {
    with_server_where(false, true, |harness| {
        Box::pin(async move {
            started_session(harness, &reqwest::Client::new()).await;
            let client = reqwest::Client::new();

            let listed = message(&client, &harness.base, "/api/sessions")
                .await
                .json::<Value>()
                .await
                .unwrap();

            assert_eq!(listed.as_array().map(Vec::len), Some(1));
            assert_eq!(listed[0]["id"], "demo-s1");
            assert_eq!(listed[0]["project"], "demo");
            assert_eq!(listed[0]["live"], true);
            assert_eq!(listed[0]["opening"], "go");
            assert_eq!(listed[0]["threadId"], "thread-1");
        })
    })
    .await;
}

/// A session that stopped is not finished: sending to it picks it back up.
#[tokio::test]
async fn a_session_whose_sandbox_has_gone_is_still_listed() {
    with_server_where(false, true, |harness| {
        Box::pin(async move {
            started_session(harness, &reqwest::Client::new()).await;
            harness
                .manager
                .end_thread("thread-1", EndReason::Idle)
                .await;
            let client = reqwest::Client::new();

            let listed = message(&client, &harness.base, "/api/sessions")
                .await
                .json::<Value>()
                .await
                .unwrap();

            assert_eq!(listed.as_array().map(Vec::len), Some(1));
            assert_eq!(listed[0]["live"], false);
            assert_eq!(listed[0]["id"], "demo-s1");
        })
    })
    .await;
}

#[tokio::test]
async fn a_session_can_be_started_from_the_interface() {
    with_server_where(false, true, |harness| {
        Box::pin(async move {
            let client = reqwest::Client::new();
            let answer = post(
                &client,
                &harness.base,
                "/api/sessions",
                &json!({ "project": "demo", "prompt": "fix the parser" }),
            )
            .await;

            assert_eq!(answer.status(), 200);
            assert_eq!(answer.json::<Value>().await.unwrap()["id"], "demo-s1");
            assert_eq!(harness.manager.sessions().len(), 1);
        })
    })
    .await;
}

#[tokio::test]
async fn a_session_with_nothing_asked_of_it_is_refused() {
    with_server_where(false, true, |harness| {
        Box::pin(async move {
            let client = reqwest::Client::new();
            let answer = post(
                &client,
                &harness.base,
                "/api/sessions",
                &json!({ "project": "demo", "prompt": "  " }),
            )
            .await;

            assert_eq!(answer.status(), 400);
            assert!(
                answer.json::<Value>().await.unwrap()["error"]
                    .as_str()
                    .unwrap()
                    .contains("a prompt is required")
            );
        })
    })
    .await;
}

#[tokio::test]
async fn a_message_reaches_the_session_it_names() {
    with_server_where(false, true, |harness| {
        Box::pin(async move {
            started_session(harness, &reqwest::Client::new()).await;
            let client = reqwest::Client::new();

            let answer = post(
                &client,
                &harness.base,
                "/api/sessions/demo-s1/send",
                &json!({ "text": "carry on" }),
            )
            .await;

            assert_eq!(answer.status(), 200);
            assert_eq!(answer.json::<Value>().await.unwrap()["accepted"], true);
        })
    })
    .await;
}

#[tokio::test]
async fn a_message_to_a_session_that_never_existed_says_so() {
    with_server_where(false, true, |harness| {
        Box::pin(async move {
            let client = reqwest::Client::new();
            let answer = post(
                &client,
                &harness.base,
                "/api/sessions/nobody/send",
                &json!({ "text": "hello" }),
            )
            .await;

            assert_eq!(answer.status(), 404);
        })
    })
    .await;
}

/// An observer that drew a composer would offer what it always refuses.
#[tokio::test]
async fn an_observing_interface_changes_nothing_and_says_why() {
    with_server_where(true, true, |harness| {
        Box::pin(async move {
            let client = reqwest::Client::new();
            let describe = message(&client, &harness.base, "/api/interface").await;
            assert_eq!(describe.json::<Value>().await.unwrap()["observer"], true);

            let started = post(
                &client,
                &harness.base,
                "/api/sessions",
                &json!({ "project": "demo", "prompt": "go" }),
            )
            .await;
            let sent = post(
                &client,
                &harness.base,
                "/api/sessions/demo-s1/send",
                &json!({ "text": "hello" }),
            )
            .await;

            assert_eq!(started.status(), 403);
            assert_eq!(sent.status(), 403);
            assert!(
                sent.json::<Value>().await.unwrap()["error"]
                    .as_str()
                    .unwrap()
                    .contains("cannot change anything")
            );
        })
    })
    .await;
}

#[tokio::test]
async fn a_live_session_is_streamed_starting_with_a_reset() {
    with_server_where(false, true, |harness| {
        Box::pin(async move {
            started_session(harness, &reqwest::Client::new()).await;
            let client = reqwest::Client::new();

            let mut answer = message(&client, &harness.base, "/api/sessions/demo-s1/stream").await;
            assert_eq!(
                answer.headers().get("content-type").unwrap(),
                "text/event-stream"
            );

            let chunk = answer.chunk().await.unwrap().expect("a first chunk");
            assert!(String::from_utf8_lossy(&chunk).contains("event: reset"));
        })
    })
    .await;
}

#[tokio::test]
async fn a_stream_for_a_session_that_is_not_live_is_refused() {
    with_server_where(false, true, |harness| {
        Box::pin(async move {
            let client = reqwest::Client::new();
            let answer = message(&client, &harness.base, "/api/sessions/nobody/stream").await;

            assert_eq!(answer.status(), 404);
        })
    })
    .await;
}

/// A stopped session has no stream, so its history is read back from disk.
#[tokio::test]
async fn a_stopped_sessions_transcript_is_read_from_where_it_was_written() {
    with_server_where(false, true, |harness| {
        Box::pin(async move {
            started_session(harness, &reqwest::Client::new()).await;
            harness
                .manager
                .end_thread("thread-1", EndReason::Idle)
                .await;
            let client = reqwest::Client::new();

            let answer = message(&client, &harness.base, "/api/sessions/demo-s1/transcript")
                .await
                .json::<Value>()
                .await
                .unwrap();

            assert!(answer["entries"].is_array());
            assert_eq!(answer["grouped"], true);
            assert!(
                answer["entries"]
                    .as_array()
                    .unwrap()
                    .iter()
                    .any(|entry| entry["kind"] == "prompt")
            );
            assert_eq!(answer["dropped"], 0);
        })
    })
    .await;
}

#[tokio::test]
async fn a_transcript_for_a_session_nobody_has_heard_of_is_not_found() {
    with_server_where(false, true, |harness| {
        Box::pin(async move {
            let client = reqwest::Client::new();
            let answer = message(&client, &harness.base, "/api/sessions/nobody/transcript").await;

            assert_eq!(answer.status(), 404);
        })
    })
    .await;
}

#[tokio::test]
async fn a_sessions_project_can_be_listed_and_read() {
    with_server_where(false, true, |harness| {
        Box::pin(async move {
            started_session(harness, &reqwest::Client::new()).await;
            std::fs::write(
                harness
                    .root
                    .path()
                    .join("projects")
                    .join("demo")
                    .join("readme.md"),
                "hello\n",
            )
            .expect("the file");
            let client = reqwest::Client::new();

            let tree = message(&client, &harness.base, "/api/sessions/demo-s1/tree")
                .await
                .json::<Value>()
                .await
                .unwrap();
            let file = message(
                &client,
                &harness.base,
                "/api/sessions/demo-s1/file?path=readme.md",
            )
            .await
            .json::<Value>()
            .await
            .unwrap();

            assert!(
                tree.as_array()
                    .unwrap()
                    .iter()
                    .any(|entry| entry["name"] == "readme.md")
            );
            assert_eq!(file["text"], "hello\n");
            assert_eq!(file["language"], "md");
        })
    })
    .await;
}

/// The same containment the sandbox applies, not a second looser one.
#[tokio::test]
async fn a_path_outside_the_project_is_refused() {
    with_server_where(false, true, |harness| {
        Box::pin(async move {
            started_session(harness, &reqwest::Client::new()).await;
            let client = reqwest::Client::new();

            let answer = message(
                &client,
                &harness.base,
                "/api/sessions/demo-s1/file?path=../../etc/passwd",
            )
            .await;

            assert_eq!(answer.status(), 403);
            assert!(
                answer.json::<Value>().await.unwrap()["error"]
                    .as_str()
                    .unwrap()
                    .contains("outside this session's project")
            );
        })
    })
    .await;
}

#[tokio::test]
async fn a_file_can_be_downloaded_as_itself() {
    with_server_where(false, true, |harness| {
        Box::pin(async move {
            started_session(harness, &reqwest::Client::new()).await;
            std::fs::write(
                harness
                    .root
                    .path()
                    .join("projects")
                    .join("demo")
                    .join("notes.txt"),
                "some notes\n",
            )
            .expect("the file");
            let client = reqwest::Client::new();

            let answer = message(
                &client,
                &harness.base,
                "/api/sessions/demo-s1/download?path=notes.txt",
            )
            .await;

            assert!(
                answer
                    .headers()
                    .get("content-disposition")
                    .unwrap()
                    .to_str()
                    .unwrap()
                    .contains("filename=\"notes.txt\"")
            );
            assert_eq!(answer.text().await.unwrap(), "some notes\n");
        })
    })
    .await;
}

#[tokio::test]
async fn the_built_interface_is_served_and_a_deep_path_lands_on_it() {
    with_server_where(false, true, |harness| {
        Box::pin(async move {
            let client = reqwest::Client::new();
            let index = message(&client, &harness.base, "/").await;
            let asset = message(&client, &harness.base, "/app.js").await;
            let deep = message(&client, &harness.base, "/session/s1/whatever").await;

            assert!(
                index
                    .text()
                    .await
                    .unwrap()
                    .contains("<title>errand</title>")
            );
            assert!(
                asset
                    .headers()
                    .get("content-type")
                    .unwrap()
                    .to_str()
                    .unwrap()
                    .contains("text/javascript")
            );
            assert!(deep.text().await.unwrap().contains("<title>errand</title>"));
        })
    })
    .await;
}

/// A traversal in a request must not reach outside the built assets.
#[tokio::test]
async fn an_asset_path_that_climbs_out_is_not_served() {
    with_server_where(false, true, |harness| {
        Box::pin(async move {
            std::fs::write(harness.root.path().join("secret.txt"), "not for you")
                .expect("the file");
            let client = reqwest::Client::new();

            let answer = message(&client, &harness.base, "/../secret.txt").await;

            assert!(!answer.text().await.unwrap().contains("not for you"));
        })
    })
    .await;
}

#[tokio::test]
async fn an_unknown_api_route_is_not_the_interface() {
    with_server_where(false, true, |harness| {
        Box::pin(async move {
            let client = reqwest::Client::new();
            let answer = message(&client, &harness.base, "/api/nothing-here").await;

            assert_eq!(answer.status(), 404);
            assert!(
                answer.json::<Value>().await.unwrap()["error"]
                    .as_str()
                    .unwrap()
                    .contains("no such route")
            );
        })
    })
    .await;
}

/// Serving something broken is worse than saying it is not there.
#[tokio::test]
async fn an_interface_that_was_never_built_refuses_to_serve() {
    with_server_where(false, false, |harness| {
        Box::pin(async move {
            let error = harness.server.start().await.unwrap_err();
            assert!(error.to_string().contains("carries no interface"));
        })
    })
    .await;
}

/// A warning in a log is not a control, so a public bind is a refusal.
#[tokio::test]
async fn an_interface_asked_to_bind_publicly_refuses_to_start() {
    let root = tempfile::tempdir().expect("a temp directory");
    std::fs::write(root.path().join("index.html"), "<!doctype html>").expect("the file");
    let settings = validate_config(&json!({
        "chat": {
            "token": "a.token.value",
            "channelId": "chan",
            "allowedUserIds": [OWNER],
        },
        "agent": {
            "provider": "anthropic",
            "credentialName": "ANTHROPIC_API_KEY",
            "credential": "secret",
        },
        "projectRoot": root.path().join("projects").display().to_string(),
        "stateDir": root.path().join("state").display().to_string(),
    }))
    .expect("the test configuration is accepted");
    let registry = Arc::new(Mutex::new(ThreadRegistry::new(
        ThreadRegistry::path_for(root.path().join("state").to_str().expect("a state path")),
        silent(),
    )));
    let scheduler = Scheduler::start(
        settings.limits.clone(),
        Arc::new(NoClock),
        5_000,
        300_000,
        750,
    );
    let manager = Arc::new(SessionManager::new(ManagerOptions {
        config: settings,
        sandbox: Arc::new(FakeSandbox) as Arc<dyn SandboxPool>,
        scheduler: Arc::clone(&scheduler),
        threads: Arc::new(FakeThreads {
            next: Mutex::new(1),
        }) as Arc<dyn ThreadFactory>,
        registry,
        log: silent(),
        make_id: None,
        unavailable: None,
        operator_ids: None,
        memory: None,
        describe_images: None,
        public_url: None,
        available_models: Vec::new(),
        delegate_base_url: None,
        now: None,
    }));

    let server = WebServer::new(
        WebConfig {
            host: "0.0.0.0".to_owned(),
            port: 39_999,
            observer: false,
            public_url: None,
        },
        manager,
        &[],
        silent(),
        None,
        None,
    );

    let error = server.start().await.unwrap_err();
    assert!(error.to_string().contains("every interface"));
}