kjarni-cli 0.7.0

Kjarni from the shell: classification, embeddings, reranking, search, chat, summarization, translation and speech-to-text as commands, and an MCP server for AI agents. Same output as PyTorch.
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
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
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
//! The daemon's API end to end, with a scripted model in place of a real one.

use std::collections::{HashMap, HashSet, VecDeque};
use std::sync::Mutex;

use axum::body::Body;
use axum::http::{Request, StatusCode, header};
use futures::StreamExt;
use serde_json::{Value as Json, json};
use tower::ServiceExt;

use super::*;

const TOKEN: &str = "0123456789abcdef0123456789abcdef";

/// Replies from a list, in order, each streamed in a few pieces. A reply of
/// `None` says "Working" and never finishes.
struct Scripted(Mutex<VecDeque<Option<String>>>);

impl Scripted {
    fn new(replies: &[Option<&str>]) -> Arc<Self> {
        Arc::new(Self(Mutex::new(
            replies.iter().map(|r| r.map(str::to_string)).collect(),
        )))
    }
}

#[async_trait]
impl AgentModel for Scripted {
    fn render(&self, _: &Conversation) -> Result<String> {
        Ok(String::new())
    }
    async fn stream(&self, _: String) -> Result<TextStream> {
        let next = self
            .0
            .lock()
            .unwrap()
            .pop_front()
            .expect("a scripted reply");
        Ok(match next {
            Some(text) => {
                let pieces: Vec<Result<String>> = text
                    .split_inclusive(' ')
                    .map(|p| Ok(p.to_string()))
                    .collect();
                Box::pin(futures::stream::iter(pieces))
            }
            None => Box::pin(
                futures::stream::iter([Ok("Working".to_string())])
                    .chain(futures::stream::pending()),
            ),
        })
    }
    fn count_tokens(&self, text: &str) -> Result<usize> {
        Ok(text.len() / 4)
    }
    fn context_size(&self) -> usize {
        32_768
    }
    fn name(&self) -> String {
        "scripted".into()
    }
}

/// Loads models from a list for a change of model, and logs what it does.
/// A model not on the list fails to load. While `blocked`, loads wait for
/// it to be released.
#[derive(Default)]
struct FakeLoader {
    models: HashMap<String, Arc<dyn AgentModel>>,
    downloaded: Mutex<HashSet<String>>,
    log: Mutex<Vec<String>>,
    blocked: Mutex<Option<tokio::sync::oneshot::Receiver<()>>>,
}

impl FakeLoader {
    fn log(&self, line: String) {
        self.log.lock().unwrap().push(line);
    }
}

#[async_trait]
impl model::Loader for FakeLoader {
    fn downloaded(&self, name: &str) -> bool {
        self.downloaded.lock().unwrap().contains(name)
    }
    fn bytes_on_disk(&self, _: &str) -> u64 {
        0
    }
    async fn download(&self, name: &str) -> Result<()> {
        self.log(format!("download {name}"));
        self.downloaded.lock().unwrap().insert(name.to_string());
        Ok(())
    }
    async fn unload(&self, name: &str) {
        self.log(format!("unload {name}"));
    }
    async fn load(&self, name: &str) -> Result<Arc<dyn AgentModel>> {
        self.log(format!("load {name}"));
        let blocked = self.blocked.lock().unwrap().take();
        if let Some(rx) = blocked {
            let _ = rx.await;
        }
        self.models
            .get(name)
            .cloned()
            .ok_or_else(|| anyhow!("{name} would not load"))
    }
}

struct Daemon {
    app: Router,
    workdir: tempfile::TempDir,
    data: tempfile::TempDir,
}

fn daemon(model: Arc<dyn AgentModel>, any_host: bool) -> Daemon {
    daemon_with_guard(model, Guard::new(TOKEN.into(), any_host))
}

fn daemon_with_guard(model: Arc<dyn AgentModel>, guard: Guard) -> Daemon {
    daemon_with(model, guard, Arc::new(FakeLoader::default()))
}

/// A daemon running `model` as "scripted", that changes model with `loader`.
fn daemon_with(model: Arc<dyn AgentModel>, guard: Guard, loader: Arc<FakeLoader>) -> Daemon {
    let workdir = tempfile::tempdir().unwrap();
    let data = tempfile::tempdir().unwrap();
    let memory = Arc::new(Memory::open(data.path(), None).unwrap());
    let slot = Arc::new(model::Slot::new(model));
    let state = Arc::new(DaemonState {
        conversations: Arc::new(Conversations::new(
            AgentSetup {
                model: slot.clone(),
                memory: Some(memory.clone()),
                workdir: workdir.path().to_path_buf(),
                max_steps: 4,
                approve_all: false,
            },
            data.path().join("sessions"),
        )),
        guard: Arc::new(guard),
        memory: Some(memory),
        models: Arc::new(model::Models::new(
            slot,
            loader,
            "scripted".into(),
            model::settings_path(data.path()),
            None,
        )),
        gpu: false,
        schedules: Arc::new(schedules::Store::open(data.path().join("schedules.json")).unwrap()),
    });
    let serve_state = Arc::new(serve::AppState {
        models: serve::Models::new(serve::Defaults::default(), false, false),
    });
    Daemon {
        app: router(state, serve_state, false),
        workdir,
        data,
    }
}

fn request(method: &str, path: &str) -> axum::http::request::Builder {
    Request::builder()
        .method(method)
        .uri(path)
        .header(header::HOST, "127.0.0.1:7710")
}

impl Daemon {
    async fn call(&self, req: Request<Body>) -> (StatusCode, Json) {
        let response = self.app.clone().oneshot(req).await.unwrap();
        let status = response.status();
        let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
            .await
            .unwrap();
        let json = serde_json::from_slice(&bytes).unwrap_or(Json::Null);
        (status, json)
    }

    /// An authorized request with an optional JSON body.
    async fn api(&self, method: &str, path: &str, body: Option<Json>) -> (StatusCode, Json) {
        let builder =
            request(method, path).header(header::AUTHORIZATION, format!("Bearer {TOKEN}"));
        let req = match body {
            Some(b) => builder
                .header(header::CONTENT_TYPE, "application/json")
                .body(Body::from(b.to_string())),
            None => builder.body(Body::empty()),
        };
        self.call(req.unwrap()).await
    }

    async fn new_conversation(&self) -> String {
        let (status, body) = self.api("POST", "/api/conversations", None).await;
        assert_eq!(status, StatusCode::CREATED);
        body["id"].as_str().unwrap().to_string()
    }

    /// Reads the conversation's events after `after` until `stop` says to.
    async fn follow(&self, id: &str, after: u64, mut stop: impl FnMut(&Json) -> bool) -> Vec<Json> {
        let req = request(
            "GET",
            &format!("/api/conversations/{id}/events?after={after}"),
        )
        .header(header::AUTHORIZATION, format!("Bearer {TOKEN}"))
        .body(Body::empty())
        .unwrap();
        let response = self.app.clone().oneshot(req).await.unwrap();
        assert_eq!(response.status(), StatusCode::OK);
        let mut body = response.into_body().into_data_stream();
        let mut buffer = String::new();
        let mut events = Vec::new();
        let deadline = tokio::time::Instant::now() + Duration::from_secs(10);
        loop {
            let chunk = tokio::time::timeout_at(deadline, body.next())
                .await
                .expect("events before the timeout")
                .expect("the stream stays open")
                .unwrap();
            buffer.push_str(std::str::from_utf8(&chunk).unwrap());
            while let Some((frame, rest)) = buffer.split_once("\n\n") {
                let frame = frame.to_string();
                buffer = rest.to_string();
                let Some(data) = frame.lines().find_map(|l| l.strip_prefix("data: ")) else {
                    continue;
                };
                let event: Json = serde_json::from_str(data).unwrap();
                let done = stop(&event);
                events.push(event);
                if done {
                    return events;
                }
            }
        }
    }
}

fn ends_turn(e: &Json) -> bool {
    matches!(e["type"].as_str(), Some("done" | "error" | "stopped"))
}

fn types(events: &[Json]) -> Vec<&str> {
    events
        .iter()
        .map(|e| e["type"].as_str().unwrap())
        .filter(|t| *t != "text")
        .collect()
}

#[tokio::test]
async fn the_token_is_required_and_other_hosts_and_origins_are_refused() {
    let d = daemon(Scripted::new(&[]), false);
    let no_token = request("GET", "/api/status").body(Body::empty()).unwrap();
    assert_eq!(d.call(no_token).await.0, StatusCode::UNAUTHORIZED);
    let wrong = request("GET", "/api/status")
        .header(header::AUTHORIZATION, "Bearer nope")
        .body(Body::empty())
        .unwrap();
    assert_eq!(d.call(wrong).await.0, StatusCode::UNAUTHORIZED);
    let (status, body) = d.api("GET", "/api/status", None).await;
    assert_eq!(status, StatusCode::OK);
    assert_eq!(body["model"], "scripted");

    // The OpenAI-compatible routes sit behind the same token.
    let v1 = request("GET", "/v1/models").body(Body::empty()).unwrap();
    assert_eq!(d.call(v1).await.0, StatusCode::UNAUTHORIZED);
    assert_eq!(d.api("GET", "/v1/models", None).await.0, StatusCode::OK);

    // A page on another domain pointed at 127.0.0.1 (DNS rebinding).
    let rebound = Request::builder()
        .uri("/api/status")
        .header(header::HOST, "evil.example:7710")
        .header(header::AUTHORIZATION, format!("Bearer {TOKEN}"))
        .body(Body::empty())
        .unwrap();
    assert_eq!(d.call(rebound).await.0, StatusCode::FORBIDDEN);

    // A page on another origin, even with the cookie the browser attaches.
    let cross = request("POST", "/api/conversations")
        .header(header::ORIGIN, "https://evil.example")
        .header(header::COOKIE, format!("{}={TOKEN}", auth::COOKIE))
        .body(Body::empty())
        .unwrap();
    assert_eq!(d.call(cross).await.0, StatusCode::FORBIDDEN);
    let same = request("GET", "/api/status")
        .header(header::ORIGIN, "http://127.0.0.1:7710")
        .header(header::COOKIE, format!("{}={TOKEN}", auth::COOKIE))
        .body(Body::empty())
        .unwrap();
    assert_eq!(d.call(same).await.0, StatusCode::OK);

    // Listening on a network address accepts any host, still with the token.
    let open = daemon(Scripted::new(&[]), true);
    let remote = Request::builder()
        .uri("/api/status")
        .header(header::HOST, "192.168.1.5:7710")
        .header(header::AUTHORIZATION, format!("Bearer {TOKEN}"))
        .body(Body::empty())
        .unwrap();
    assert_eq!(open.call(remote).await.0, StatusCode::OK);
}

#[tokio::test]
async fn a_login_code_signs_a_browser_in_once() {
    let d = daemon(Scripted::new(&[]), false);
    let (status, body) = d.api("POST", "/api/login-code", None).await;
    assert_eq!(status, StatusCode::OK);
    let code = body["code"].as_str().unwrap();
    assert_eq!(
        body["url"],
        format!("http://127.0.0.1:7710/login?code={code}")
    );

    let login = || {
        request("GET", &format!("/login?code={code}"))
            .body(Body::empty())
            .unwrap()
    };
    let response = d.app.clone().oneshot(login()).await.unwrap();
    assert_eq!(response.status(), StatusCode::SEE_OTHER);
    let cookie = response.headers()[header::SET_COOKIE].to_str().unwrap();
    assert_eq!(
        cookie,
        format!("kjarni_token={TOKEN}; Path=/; HttpOnly; SameSite=Strict; Max-Age=31536000")
    );
    assert_eq!(
        d.call(login()).await.0,
        StatusCode::UNAUTHORIZED,
        "used twice"
    );

    let bad = request("GET", "/login?code=guess")
        .body(Body::empty())
        .unwrap();
    assert_eq!(d.call(bad).await.0, StatusCode::UNAUTHORIZED);
}

#[test]
fn loopback_hosts() {
    for host in [
        "127.0.0.1",
        "127.0.0.1:7710",
        "localhost:80",
        "LOCALHOST",
        "[::1]",
        "[::1]:7710",
    ] {
        assert!(auth::is_loopback_host(host), "{host}");
    }
    for host in [
        "",
        "evil.example",
        "127.0.0.1.evil.example",
        "localhost.evil.example:7710",
        "[::1]x",
        "127.0.0.2",
        "localhost:abc",
    ] {
        assert!(!auth::is_loopback_host(host), "{host}");
    }
}

#[test]
fn the_token_file_is_made_once_and_private() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("daemon/token");
    let token = auth::load_or_create_token(&path).unwrap();
    assert_eq!(token.len(), 64);
    assert!(token.chars().all(|c| c.is_ascii_hexdigit()));
    assert_eq!(auth::load_or_create_token(&path).unwrap(), token);
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let mode = std::fs::metadata(&path).unwrap().permissions().mode();
        assert_eq!(mode & 0o777, 0o600);
    }
}

#[tokio::test]
async fn a_message_streams_its_reply_and_is_saved() {
    let d = daemon(
        Scripted::new(&[Some("<think>easy</think>Hello there.")]),
        false,
    );
    let id = d.new_conversation().await;
    let (status, _) = d
        .api(
            "POST",
            &format!("/api/conversations/{id}/messages"),
            Some(json!({"text": "Hi"})),
        )
        .await;
    assert_eq!(status, StatusCode::ACCEPTED);

    let events = d.follow(&id, 0, ends_turn).await;
    assert_eq!(types(&events), ["user", "done"]);
    assert_eq!(events[0]["text"], "Hi");
    let streamed: String = events.iter().filter_map(|e| e["delta"].as_str()).collect();
    assert_eq!(streamed, "<think>easy</think>Hello there.");
    let done = events.last().unwrap();
    assert_eq!(done["text"], "Hello there.");
    let seqs: Vec<u64> = events.iter().map(|e| e["seq"].as_u64().unwrap()).collect();
    assert!(seqs.windows(2).all(|w| w[0] < w[1]));

    // Reconnecting after an event replays only what came after it.
    let replay = d.follow(&id, seqs[1], ends_turn).await;
    assert_eq!(replay.first().unwrap()["seq"].as_u64(), Some(seqs[2]));

    let (_, conversation) = d
        .api("GET", &format!("/api/conversations/{id}"), None)
        .await;
    assert_eq!(conversation["busy"], false);
    assert_eq!(conversation["events"], json!([]));
    assert_eq!(
        conversation["messages"],
        json!([
            {"role": "user", "text": "Hi"},
            {"role": "assistant", "text": "Hello there.", "reasoning": "easy", "tool_calls": []},
        ])
    );
    let (_, list) = d.api("GET", "/api/conversations", None).await;
    assert_eq!(list["conversations"][0]["id"], id.as_str());
    assert_eq!(list["conversations"][0]["title"], "Hi");
}

const WRITE_CALL: &str = "I'll save it.\n<tool_call>\n<function=write_file>\n<parameter=path>\nnote.txt\n</parameter>\n<parameter=content>\nhello\n</parameter>\n</function>\n</tool_call>";

#[tokio::test]
async fn a_write_waits_for_approval_from_the_client() {
    let d = daemon(
        Scripted::new(&[
            Some(WRITE_CALL),
            Some("Saved."),
            Some(WRITE_CALL),
            Some("Left it."),
        ]),
        false,
    );
    let id = d.new_conversation().await;
    d.api(
        "POST",
        &format!("/api/conversations/{id}/messages"),
        Some(json!({"text": "Save hello"})),
    )
    .await;
    let events = d.follow(&id, 0, |e| e["type"] == "approval").await;
    assert_eq!(types(&events), ["user", "tool_call", "approval"]);
    let call = events.iter().find(|e| e["type"] == "tool_call").unwrap();
    assert_eq!(call["name"], "write_file");
    assert_eq!(call["access"], "write");
    assert_eq!(
        call["arguments"],
        json!({"path": "note.txt", "content": "hello"})
    );
    let approval = events.last().unwrap()["approval"].as_u64().unwrap();
    assert!(!d.workdir.path().join("note.txt").exists());

    // Opening the conversation now shows the turn so far and what it waits for.
    let (_, open) = d
        .api("GET", &format!("/api/conversations/{id}"), None)
        .await;
    assert_eq!(open["busy"], true);
    assert_eq!(open["pending_approval"], approval);
    assert_eq!(open["messages"], json!([]));
    assert_eq!(open["events"].as_array().unwrap().len(), events.len());

    // A second message while it waits is refused; so is a wrong approval.
    let (busy, _) = d
        .api(
            "POST",
            &format!("/api/conversations/{id}/messages"),
            Some(json!({"text": "and?"})),
        )
        .await;
    assert_eq!(busy, StatusCode::CONFLICT);
    let path = format!("/api/conversations/{id}/approvals/{}", approval + 100);
    assert_eq!(
        d.api("POST", &path, Some(json!({"decision": "once"})))
            .await
            .0,
        StatusCode::NOT_FOUND
    );
    let path = format!("/api/conversations/{id}/approvals/{approval}");
    assert_eq!(
        d.api("POST", &path, Some(json!({"decision": "maybe"})))
            .await
            .0,
        StatusCode::BAD_REQUEST
    );

    assert_eq!(
        d.api("POST", &path, Some(json!({"decision": "once"})))
            .await
            .0,
        StatusCode::NO_CONTENT
    );
    let last = events.last().unwrap()["seq"].as_u64().unwrap();
    let rest = d.follow(&id, last, ends_turn).await;
    assert_eq!(types(&rest), ["approved", "tool_result", "done"]);
    assert_eq!(rest[0]["decision"], "once");
    assert_eq!(rest[1]["ok"], true);
    assert_eq!(rest.last().unwrap()["text"], "Saved.");
    assert_eq!(
        std::fs::read_to_string(d.workdir.path().join("note.txt")).unwrap(),
        "hello"
    );

    // Declined, the call does not run and the model is told.
    std::fs::remove_file(d.workdir.path().join("note.txt")).unwrap();
    d.api(
        "POST",
        &format!("/api/conversations/{id}/messages"),
        Some(json!({"text": "Again"})),
    )
    .await;
    let events = d.follow(&id, 0, |e| e["type"] == "approval").await;
    let approval = events.last().unwrap()["approval"].as_u64().unwrap();
    let path = format!("/api/conversations/{id}/approvals/{approval}");
    d.api("POST", &path, Some(json!({"decision": "deny"})))
        .await;
    let rest = d.follow(&id, approval, ends_turn).await;
    let result = rest.iter().find(|e| e["type"] == "tool_result").unwrap();
    assert_eq!(
        (result["ok"].as_bool(), result["denied"].as_bool()),
        (Some(false), Some(true))
    );
    assert!(!d.workdir.path().join("note.txt").exists());

    let (_, conversation) = d
        .api("GET", &format!("/api/conversations/{id}"), None)
        .await;
    let messages = conversation["messages"].as_array().unwrap();
    assert_eq!(messages.len(), 8);
    assert_eq!(messages[1]["tool_calls"][0]["name"], "write_file");
    assert_eq!(messages[1]["text"], "I'll save it.");
    assert_eq!(messages[2]["role"], "tool");
}

#[tokio::test]
async fn stopping_a_turn_frees_the_conversation() {
    let d = daemon(Scripted::new(&[None, Some("Back.")]), false);
    let id = d.new_conversation().await;
    let stop = format!("/api/conversations/{id}/stop");
    assert_eq!(
        d.api("POST", &stop, None).await.0,
        StatusCode::CONFLICT,
        "nothing to stop"
    );

    d.api(
        "POST",
        &format!("/api/conversations/{id}/messages"),
        Some(json!({"text": "Go"})),
    )
    .await;
    // Once the model is generating.
    d.follow(&id, 0, |e| e["type"] == "text").await;
    assert_eq!(d.api("POST", &stop, None).await.0, StatusCode::NO_CONTENT);
    let events = d.follow(&id, 0, ends_turn).await;
    assert_eq!(types(&events), ["user", "stopped"]);

    // The stopped message stays in the conversation, and the next one runs.
    let (status, _) = d
        .api(
            "POST",
            &format!("/api/conversations/{id}/messages"),
            Some(json!({"text": "Again"})),
        )
        .await;
    assert_eq!(status, StatusCode::ACCEPTED);
    let events = d.follow(&id, 0, ends_turn).await;
    assert_eq!(events.last().unwrap()["text"], "Back.");
    let (_, conversation) = d
        .api("GET", &format!("/api/conversations/{id}"), None)
        .await;
    let users: Vec<&str> = conversation["messages"]
        .as_array()
        .unwrap()
        .iter()
        .filter(|m| m["role"] == "user")
        .map(|m| m["text"].as_str().unwrap())
        .collect();
    assert_eq!(users, ["Go", "Again"]);
}

#[tokio::test]
async fn conversations_are_found_by_id_and_deleted() {
    let d = daemon(Scripted::new(&[Some("Hello.")]), false);
    let id = d.new_conversation().await;
    for bad in ["nope", "..%2F..%2Fetc%2Fpasswd", "a.b"] {
        let (status, _) = d
            .api("GET", &format!("/api/conversations/{bad}"), None)
            .await;
        assert_eq!(status, StatusCode::NOT_FOUND, "{bad}");
    }
    let (empty, _) = d
        .api(
            "POST",
            &format!("/api/conversations/{id}/messages"),
            Some(json!({"text": "  "})),
        )
        .await;
    assert_eq!(empty, StatusCode::BAD_REQUEST);

    let path = format!("/api/conversations/{id}");
    assert_eq!(d.api("DELETE", &path, None).await.0, StatusCode::NO_CONTENT);
    assert_eq!(d.api("GET", &path, None).await.0, StatusCode::NOT_FOUND);
    assert_eq!(d.api("DELETE", &path, None).await.0, StatusCode::NOT_FOUND);
}

#[tokio::test]
async fn memory_can_be_listed_added_to_and_forgotten() {
    let d = daemon(Scripted::new(&[]), false);
    let (status, fact) = d
        .api(
            "POST",
            "/api/memory",
            Some(json!({"text": "Prefers tabs."})),
        )
        .await;
    assert_eq!(status, StatusCode::CREATED);
    let id = fact["id"].as_u64().unwrap();
    let (_, list) = d.api("GET", "/api/memory", None).await;
    assert_eq!(list["facts"][0]["text"], "Prefers tabs.");
    assert_eq!(
        d.api("POST", "/api/memory", Some(json!({"text": " "})))
            .await
            .0,
        StatusCode::BAD_REQUEST
    );

    assert_eq!(
        d.api("DELETE", &format!("/api/memory/{id}"), None).await.0,
        StatusCode::NO_CONTENT
    );
    assert_eq!(
        d.api("DELETE", &format!("/api/memory/{id}"), None).await.0,
        StatusCode::NOT_FOUND
    );
    let (_, list) = d.api("GET", "/api/memory", None).await;
    assert_eq!(list["facts"], json!([]));
}

#[test]
fn install_downloads_only_what_the_cache_lacks() {
    let cache = tempfile::tempdir().unwrap();
    let model = kjarni::ModelType::from_cli_name("qwen3.5-4b").unwrap();
    let dir = model.cache_dir(cache.path());
    std::fs::create_dir_all(&dir).unwrap();
    assert!(!super::is_cached_in(cache.path(), "qwen3.5-4b", true));
    for file in ["config.json", "tokenizer.json", "model.gguf"] {
        std::fs::write(dir.join(file), "").unwrap();
    }
    assert!(super::is_cached_in(cache.path(), "qwen3.5-4b", true));
    // Full precision loads safetensors, which are not there.
    assert!(!super::is_cached_in(cache.path(), "qwen3.5-4b", false));
    std::fs::write(dir.join("model.safetensors"), "").unwrap();
    assert!(super::is_cached_in(cache.path(), "qwen3.5-4b", false));
    assert!(!super::is_cached_in(cache.path(), "no-such-model", false));
}

#[test]
fn the_daemon_downloads_its_models_with_progress_before_loading() {
    let cache = tempfile::tempdir().unwrap();
    // An empty cache: the chat model with progress, then the memory model quietly.
    assert_eq!(
        super::missing(cache.path(), "qwen3.5-4b", true, true),
        vec![
            ("qwen3.5-4b", true, false),
            (super::MEMORY_MODEL, false, true)
        ]
    );
    // Without memory, only the chat model.
    assert_eq!(
        super::missing(cache.path(), "qwen3.5-4b", true, false),
        vec![("qwen3.5-4b", true, false)]
    );
    // Nothing once both are cached.
    for (name, files) in [
        (
            "qwen3.5-4b",
            &["config.json", "tokenizer.json", "model.gguf"][..],
        ),
        (
            super::MEMORY_MODEL,
            &["config.json", "tokenizer.json", "model.safetensors"][..],
        ),
    ] {
        let dir = kjarni::ModelType::from_cli_name(name)
            .unwrap()
            .cache_dir(cache.path());
        std::fs::create_dir_all(&dir).unwrap();
        for file in files {
            std::fs::write(dir.join(file), "").unwrap();
        }
    }
    assert!(super::missing(cache.path(), "qwen3.5-4b", true, true).is_empty());
}

#[tokio::test]
async fn the_token_signs_in_a_browser_on_another_device() {
    let d = daemon(Scripted::new(&[]), true);
    let sign_in = |token: &str| {
        request("POST", "/login")
            .header(header::CONTENT_TYPE, "application/json")
            .body(Body::from(json!({ "token": token }).to_string()))
            .unwrap()
    };
    let response = d.app.clone().oneshot(sign_in(TOKEN)).await.unwrap();
    assert_eq!(response.status(), StatusCode::NO_CONTENT);
    assert_eq!(
        response.headers()[header::SET_COOKIE].to_str().unwrap(),
        format!("kjarni_token={TOKEN}; Path=/; HttpOnly; SameSite=Strict; Max-Age=31536000")
    );
    let wrong = d.app.clone().oneshot(sign_in("guess")).await.unwrap();
    assert_eq!(wrong.status(), StatusCode::UNAUTHORIZED);
    assert!(wrong.headers().get(header::SET_COOKIE).is_none());

    // Another web page cannot sign the user's browser in to its own choice.
    let forged = request("POST", "/login")
        .header(header::ORIGIN, "http://evil.example")
        .header(header::CONTENT_TYPE, "application/json")
        .body(Body::from(json!({ "token": TOKEN }).to_string()))
        .unwrap();
    assert_eq!(d.call(forged).await.0, StatusCode::FORBIDDEN);
}

#[tokio::test]
async fn the_web_ui_is_served_without_signing_in() {
    let d = daemon(Scripted::new(&[]), false);
    let get = |path: &str| request("GET", path).body(Body::empty()).unwrap();

    for path in ["/", "/c/abc123"] {
        let response = d.app.clone().oneshot(get(path)).await.unwrap();
        assert_eq!(response.status(), StatusCode::OK, "{path}");
        let headers = response.headers();
        assert_eq!(headers[header::CONTENT_TYPE], "text/html; charset=utf-8");
        let csp = headers[header::CONTENT_SECURITY_POLICY].to_str().unwrap();
        assert!(csp.contains("script-src 'self'"), "{csp}");
        assert!(csp.contains("frame-ancestors 'none'"), "{csp}");
        let body = axum::body::to_bytes(response.into_body(), usize::MAX)
            .await
            .unwrap();
        assert!(String::from_utf8_lossy(&body).contains("/assets/app.js"));
    }

    let js = d.app.clone().oneshot(get("/assets/app.js")).await.unwrap();
    assert_eq!(js.status(), StatusCode::OK);
    assert_eq!(
        js.headers()[header::CONTENT_TYPE],
        "text/javascript; charset=utf-8"
    );
    let font = d
        .app
        .clone()
        .oneshot(get("/fonts/martian-mono-latin.woff2"))
        .await
        .unwrap();
    assert_eq!(font.headers()[header::CONTENT_TYPE], "font/woff2");
    assert!(
        font.headers()[header::CACHE_CONTROL]
            .to_str()
            .unwrap()
            .contains("immutable")
    );

    assert_eq!(
        d.call(get("/assets/missing.js")).await.0,
        StatusCode::NOT_FOUND
    );
    // The API's own paths never fall through to the page.
    assert_eq!(
        d.api("GET", "/api/no-such-thing", None).await.0,
        StatusCode::NOT_FOUND
    );
    assert_eq!(d.call(get("/api/status")).await.0, StatusCode::UNAUTHORIZED);
}

#[tokio::test]
async fn an_allowed_host_name_works_over_https_as_tailscale_serve_sends_it() {
    let guard = Guard::new(TOKEN.into(), false).allow_hosts(vec!["box.tail1234.ts.net".into()]);
    let d = daemon_with_guard(Scripted::new(&[]), guard);
    let status = |host: &str, origin: Option<&str>| {
        let mut r = Request::builder()
            .method("POST")
            .uri("/api/conversations")
            .header(header::HOST, host)
            .header(header::AUTHORIZATION, format!("Bearer {TOKEN}"));
        if let Some(o) = origin {
            r = r.header(header::ORIGIN, o);
        }
        r.body(Body::empty()).unwrap()
    };

    for host in [
        "box.tail1234.ts.net",
        "BOX.tail1234.ts.net:443",
        "127.0.0.1:7710",
    ] {
        assert_eq!(
            d.call(status(host, None)).await.0,
            StatusCode::CREATED,
            "{host}"
        );
    }
    assert_eq!(
        d.call(status(
            "box.tail1234.ts.net",
            Some("https://box.tail1234.ts.net")
        ))
        .await
        .0,
        StatusCode::CREATED
    );

    // Another name is refused, with what to do if it is this computer's.
    let (code, body) = d.call(status("other.tail1234.ts.net", None)).await;
    assert_eq!(code, StatusCode::FORBIDDEN);
    assert!(
        body["error"]["message"]
            .as_str()
            .unwrap()
            .contains("--allow-host other.tail1234.ts.net")
    );
    // A page on another site still cannot use the allowed name.
    assert_eq!(
        d.call(status("box.tail1234.ts.net", Some("https://evil.example")))
            .await
            .0,
        StatusCode::FORBIDDEN
    );
    assert_eq!(
        d.call(status(
            "box.tail1234.ts.net",
            Some("https://box.tail1234.ts.net.evil.example")
        ))
        .await
        .0,
        StatusCode::FORBIDDEN
    );
}

#[test]
fn allow_host_takes_a_name_or_a_pasted_address() {
    assert_eq!(
        auth::allowed_host("box.tail1234.ts.net").unwrap(),
        "box.tail1234.ts.net"
    );
    assert_eq!(
        auth::allowed_host(" https://Box.Tail1234.ts.net/ ").unwrap(),
        "box.tail1234.ts.net"
    );
    assert_eq!(auth::allowed_host("box.lan:7710").unwrap(), "box.lan");
    for bad in ["", "https://box.ts.net/path", "box ts net", "user@box"] {
        assert!(auth::allowed_host(bad).is_err(), "{bad:?}");
    }
}

#[test]
fn install_carries_allowed_hosts_into_the_startup_entry() {
    let options = super::InstallOptions {
        model: None,
        gpu: false,
        cpu: false,
        full_precision: false,
        port: 7710,
        allow_hosts: vec!["box.tail1234.ts.net".into()],
        dir: None,
        yes: false,
        no_memory: false,
        max_steps: 16,
        no_linger: false,
    };
    let args = super::run_arguments(
        &options,
        true,
        std::path::Path::new("/home/ada/kjarni"),
        std::path::Path::new("/tmp/daemon.log"),
    );
    let at = args.iter().position(|a| a == "--allow-host").unwrap();
    assert_eq!(args[at + 1], "box.tail1234.ts.net");
    assert!(args.contains(&"--gpu".to_string()));
    // The daemon loads the saved model, so a change in the web UI lasts.
    assert!(!args.contains(&"--model".to_string()));
}

const RUN_CALL: &str = "Let me look.\n<tool_call>\n<function=run_command>\n<parameter=command>\nls\n</parameter>\n</function>\n</tool_call>";

/// A stand-in ntfy server: each publish arrives on the channel as its
/// `Authorization` header and JSON body.
async fn fake_ntfy() -> (
    String,
    tokio::sync::mpsc::UnboundedReceiver<(Option<String>, Json)>,
) {
    let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
    let app = axum::Router::new().route(
        "/",
        axum::routing::post(move |headers: axum::http::HeaderMap, body: String| {
            let tx = tx.clone();
            async move {
                let auth = headers
                    .get(header::AUTHORIZATION)
                    .map(|v| v.to_str().unwrap().to_string());
                tx.send((auth, serde_json::from_str(&body).unwrap()))
                    .unwrap();
                "{}"
            }
        }),
    );
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let address = listener.local_addr().unwrap();
    tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
    (format!("http://{address}"), rx)
}

#[tokio::test]
async fn a_scheduled_run_uses_only_the_tools_it_allows_and_notifies() {
    let d = daemon(
        Scripted::new(&[Some(WRITE_CALL), Some(RUN_CALL), Some("Saved the note.")]),
        false,
    );
    let (ntfy, mut published) = fake_ntfy().await;
    let (status, _) = d
        .api(
            "PUT",
            "/api/notifications",
            Some(json!({
                "url": format!("{ntfy}/kjarni-test"),
                "token": "tk_secret",
                "open_url": "https://box.tail1234.ts.net/",
            })),
        )
        .await;
    assert_eq!(status, StatusCode::NO_CONTENT);

    let (status, made) = d
        .api(
            "POST",
            "/api/schedules",
            Some(json!({
                "name": "Morning note",
                "prompt": "Save hello",
                "when": {"kind": "daily", "time": "07:00", "days": ["mon", "fri"]},
                "allow": ["write_file"],
            })),
        )
        .await;
    assert_eq!(status, StatusCode::CREATED, "{made}");
    assert_eq!(made["running"], false);
    assert_eq!(made["notify"], true);
    assert!(made["next_run"].as_i64().unwrap() > made["created"].as_i64().unwrap());
    let id = made["id"].as_str().unwrap();

    let (status, run) = d
        .api("POST", &format!("/api/schedules/{id}/run"), None)
        .await;
    assert_eq!(status, StatusCode::ACCEPTED);
    let conversation = run["conversation"].as_str().unwrap();
    let events = d.follow(conversation, 0, ends_turn).await;
    assert_eq!(
        types(&events),
        [
            "user",
            "tool_call",
            "approval",
            "approved",
            "tool_result",
            "tool_call",
            "approval",
            "notice",
            "approved",
            "tool_result",
            "done"
        ]
    );
    let decisions: Vec<_> = events
        .iter()
        .filter(|e| e["type"] == "approved")
        .map(|e| e["decision"].as_str().unwrap())
        .collect();
    assert_eq!(decisions, ["once", "deny"]);
    let notice = events.iter().find(|e| e["type"] == "notice").unwrap();
    assert!(
        notice["message"]
            .as_str()
            .unwrap()
            .contains("run_command is not allowed")
    );
    assert_eq!(
        std::fs::read_to_string(d.workdir.path().join("note.txt")).unwrap(),
        "hello"
    );

    let (auth, body) = tokio::time::timeout(Duration::from_secs(10), published.recv())
        .await
        .expect("a notification")
        .unwrap();
    assert_eq!(auth.as_deref(), Some("Bearer tk_secret"));
    assert_eq!(body["topic"], "kjarni-test");
    assert_eq!(body["title"], "Morning note");
    assert_eq!(body["message"], "Saved the note.");
    assert_eq!(
        body["click"],
        format!("https://box.tail1234.ts.net/c/{conversation}")
    );

    let (_, listed) = d.api("GET", "/api/schedules", None).await;
    let s = &listed["schedules"][0];
    assert_eq!(s["last_result"], "done");
    assert_eq!(s["last_conversation"], conversation);
    assert_eq!(s["running"], false);
    // Clients learn a token is set, never the token.
    assert_eq!(listed["notify"]["has_token"], true);
    assert!(!listed.to_string().contains("tk_secret"));
    assert_eq!(
        listed["tools"],
        json!([
            "fetch_url",
            "write_file",
            "edit_file",
            "run_command",
            "remember",
            "forget"
        ])
    );
}

#[tokio::test]
async fn a_running_schedule_cannot_start_twice_and_a_stop_is_recorded() {
    let d = daemon(Scripted::new(&[None]), false);
    let (_, made) = d
        .api(
            "POST",
            "/api/schedules",
            Some(json!({
                "name": "Slow",
                "prompt": "Take your time",
                "when": {"kind": "every", "minutes": 30},
                "notify": false,
            })),
        )
        .await;
    let id = made["id"].as_str().unwrap();
    let (_, run) = d
        .api("POST", &format!("/api/schedules/{id}/run"), None)
        .await;
    let conversation = run["conversation"].as_str().unwrap();
    d.follow(conversation, 0, |e| e["type"] == "text").await;

    let (again, _) = d
        .api("POST", &format!("/api/schedules/{id}/run"), None)
        .await;
    assert_eq!(again, StatusCode::CONFLICT);
    let (_, listed) = d.api("GET", "/api/schedules", None).await;
    assert_eq!(listed["schedules"][0]["running"], true);
    assert_eq!(listed["schedules"][0]["last_result"], "running");

    let (stopped, _) = d
        .api(
            "POST",
            &format!("/api/conversations/{conversation}/stop"),
            None,
        )
        .await;
    assert_eq!(stopped, StatusCode::NO_CONTENT);
    let deadline = tokio::time::Instant::now() + Duration::from_secs(10);
    loop {
        let (_, listed) = d.api("GET", "/api/schedules", None).await;
        if listed["schedules"][0]["last_result"] == "stopped" {
            assert_eq!(listed["schedules"][0]["running"], false);
            break;
        }
        assert!(tokio::time::Instant::now() < deadline, "{listed}");
        tokio::time::sleep(Duration::from_millis(20)).await;
    }
}

#[tokio::test]
async fn schedules_and_notification_settings_are_checked() {
    let d = daemon(Scripted::new(&[]), false);
    let base = json!({"name": "X", "prompt": "Y", "when": {"kind": "every", "minutes": 60}});

    let mut reads = base.clone();
    reads["allow"] = json!(["read_file"]);
    let (status, body) = d.api("POST", "/api/schedules", Some(reads)).await;
    assert_eq!(status, StatusCode::BAD_REQUEST);
    assert!(
        body["error"]["message"]
            .as_str()
            .unwrap()
            .contains("fetch_url")
    );

    let mut often = base.clone();
    often["when"] = json!({"kind": "every", "minutes": 1});
    let (status, _) = d.api("POST", "/api/schedules", Some(often)).await;
    assert_eq!(status, StatusCode::BAD_REQUEST);

    let (status, _) = d
        .api("PUT", "/api/schedules/nope", Some(base.clone()))
        .await;
    assert_eq!(status, StatusCode::NOT_FOUND);
    let (status, _) = d.api("POST", "/api/schedules/nope/run", None).await;
    assert_eq!(status, StatusCode::NOT_FOUND);
    let (status, _) = d.api("DELETE", "/api/schedules/nope", None).await;
    assert_eq!(status, StatusCode::NOT_FOUND);

    let (status, _) = d.api("POST", "/api/notifications/test", None).await;
    assert_eq!(status, StatusCode::BAD_REQUEST);
    for bad in [
        json!({"url": "ntfy.sh/topic"}),
        json!({"url": "https://ntfy.sh/"}),
        json!({"url": "https://ntfy.sh/t", "open_url": "javascript:alert(1)"}),
    ] {
        let (status, _) = d.api("PUT", "/api/notifications", Some(bad.clone())).await;
        assert_eq!(status, StatusCode::BAD_REQUEST, "{bad}");
    }

    // Leaving the token out keeps it while the address stays the same.
    d.api(
        "PUT",
        "/api/notifications",
        Some(json!({"url": "https://ntfy.sh/t", "token": "tk"})),
    )
    .await;
    d.api(
        "PUT",
        "/api/notifications",
        Some(json!({"url": "https://ntfy.sh/t"})),
    )
    .await;
    let (_, listed) = d.api("GET", "/api/schedules", None).await;
    assert_eq!(listed["notify"]["has_token"], true);
    d.api(
        "PUT",
        "/api/notifications",
        Some(json!({"url": "https://ntfy.sh/u"})),
    )
    .await;
    let (_, listed) = d.api("GET", "/api/schedules", None).await;
    assert_eq!(listed["notify"]["has_token"], false);
    let (status, _) = d
        .api("PUT", "/api/notifications", Some(json!({"url": ""})))
        .await;
    assert_eq!(status, StatusCode::NO_CONTENT);
    let (_, listed) = d.api("GET", "/api/schedules", None).await;
    assert_eq!(listed["notify"], Json::Null);
}

#[test]
fn schedules_can_allow_exactly_the_tools_that_ask() {
    let dir = tempfile::tempdir().unwrap();
    let memory = Arc::new(Memory::open(dir.path(), None).unwrap());
    let workspace = Arc::new(kjarni::agent::tools::Workspace::new(dir.path()));
    let mut asking: Vec<String> = kjarni::agent::tools::builtin(workspace, Some(memory))
        .iter()
        .filter(|t| t.access() != kjarni::agent::Access::Read)
        .map(|t| t.spec().name.clone())
        .collect();
    asking.sort();
    let mut listed: Vec<String> = schedules::ASKING_TOOLS
        .iter()
        .map(|s| s.to_string())
        .collect();
    listed.sort();
    assert_eq!(asking, listed);
}

impl Daemon {
    /// Sends `text` in a new conversation and returns the reply.
    async fn ask(&self, text: &str) -> String {
        let id = self.new_conversation().await;
        let (status, _) = self
            .api(
                "POST",
                &format!("/api/conversations/{id}/messages"),
                Some(json!({ "text": text })),
            )
            .await;
        assert_eq!(status, StatusCode::ACCEPTED);
        let events = self.follow(&id, 0, ends_turn).await;
        events.last().unwrap()["text"].as_str().unwrap().to_string()
    }

    /// `/api/models` once `phase` is no longer downloading or loading.
    async fn settled_models(&self) -> Json {
        let deadline = tokio::time::Instant::now() + Duration::from_secs(10);
        loop {
            let (_, body) = self.api("GET", "/api/models", None).await;
            let state = body["phase"]["state"].as_str().unwrap();
            if state == "idle" || state == "failed" {
                return body;
            }
            assert!(tokio::time::Instant::now() < deadline, "the switch ended");
            tokio::time::sleep(Duration::from_millis(20)).await;
        }
    }
}

#[tokio::test]
async fn switching_models_frees_the_old_one_before_loading_and_saves_the_choice() {
    let (release, blocked) = tokio::sync::oneshot::channel();
    let loader = Arc::new(FakeLoader {
        models: HashMap::from([(
            "qwen3.5-9b".to_string(),
            Scripted::new(&[Some("From the new model.")]) as Arc<dyn AgentModel>,
        )]),
        blocked: Mutex::new(Some(blocked)),
        ..Default::default()
    });
    let d = daemon_with(
        Scripted::new(&[]),
        Guard::new(TOKEN.into(), false),
        loader.clone(),
    );

    let (_, body) = d.api("GET", "/api/models", None).await;
    assert_eq!(body["current"], "scripted");
    let names: Vec<_> = body["choices"]
        .as_array()
        .unwrap()
        .iter()
        .map(|c| c["name"].as_str().unwrap())
        .collect();
    assert_eq!(names, model::CHOICES);
    assert_eq!(body["choices"][3]["params_millions"], 9653);

    let (status, _) = d
        .api(
            "PUT",
            "/api/models/current",
            Some(json!({ "model": "qwen3.5-9b" })),
        )
        .await;
    assert_eq!(status, StatusCode::ACCEPTED);

    // While it loads, messages and a second switch wait.
    let deadline = tokio::time::Instant::now() + Duration::from_secs(10);
    while d.api("GET", "/api/models", None).await.1["phase"]["state"] != "loading" {
        assert!(tokio::time::Instant::now() < deadline);
        tokio::time::sleep(Duration::from_millis(10)).await;
    }
    let id = d.new_conversation().await;
    let (status, body) = d
        .api(
            "POST",
            &format!("/api/conversations/{id}/messages"),
            Some(json!({ "text": "Hello?" })),
        )
        .await;
    assert_eq!(status, StatusCode::CONFLICT);
    assert!(
        body["error"]["message"]
            .as_str()
            .unwrap()
            .contains("switching models")
    );
    let (status, _) = d
        .api(
            "PUT",
            "/api/models/current",
            Some(json!({ "model": "qwen3.5-2b" })),
        )
        .await;
    assert_eq!(status, StatusCode::CONFLICT);

    release.send(()).unwrap();
    let body = d.settled_models().await;
    assert_eq!(body["phase"]["state"], "idle");
    assert_eq!(body["current"], "qwen3.5-9b");
    assert_eq!(
        *loader.log.lock().unwrap(),
        ["download qwen3.5-9b", "unload scripted", "load qwen3.5-9b"]
    );
    assert_eq!(
        d.api("GET", "/api/status", None).await.1["model"],
        "qwen3.5-9b"
    );
    assert_eq!(
        model::Settings::load(&model::settings_path(d.data.path()))
            .model
            .as_deref(),
        Some("qwen3.5-9b")
    );
    assert_eq!(d.ask("Hello?").await, "From the new model.");
}

#[tokio::test]
async fn a_model_that_will_not_load_leaves_the_old_one_running() {
    let loader = Arc::new(FakeLoader {
        // Loaded again after the new one fails.
        models: HashMap::from([(
            "scripted".to_string(),
            Scripted::new(&[Some("Still here.")]) as Arc<dyn AgentModel>,
        )]),
        downloaded: Mutex::new(HashSet::from(["qwen3.5-9b".to_string()])),
        ..Default::default()
    });
    let d = daemon_with(
        Scripted::new(&[]),
        Guard::new(TOKEN.into(), false),
        loader.clone(),
    );
    let (status, _) = d
        .api(
            "PUT",
            "/api/models/current",
            Some(json!({ "model": "qwen3.5-9b" })),
        )
        .await;
    assert_eq!(status, StatusCode::ACCEPTED);
    let body = d.settled_models().await;
    assert_eq!(body["phase"]["state"], "failed");
    assert_eq!(body["phase"]["model"], "qwen3.5-9b");
    assert!(
        body["phase"]["message"]
            .as_str()
            .unwrap()
            .contains("would not load")
    );
    assert_eq!(body["current"], "scripted");
    assert_eq!(
        *loader.log.lock().unwrap(),
        ["unload scripted", "load qwen3.5-9b", "load scripted"]
    );
    assert!(!model::settings_path(d.data.path()).exists());
    assert_eq!(d.ask("Hello?").await, "Still here.");
}

#[tokio::test]
async fn only_tool_models_are_offered_and_not_while_a_conversation_answers() {
    let d = daemon(Scripted::new(&[None]), false);
    let (status, body) = d
        .api(
            "PUT",
            "/api/models/current",
            Some(json!({ "model": "llama3.2-1b-instruct" })),
        )
        .await;
    assert_eq!(status, StatusCode::BAD_REQUEST);
    assert!(
        body["error"]["message"]
            .as_str()
            .unwrap()
            .contains("tool calls")
    );

    // A reply that never finishes keeps the conversation answering.
    let id = d.new_conversation().await;
    d.api(
        "POST",
        &format!("/api/conversations/{id}/messages"),
        Some(json!({ "text": "Go on" })),
    )
    .await;
    let (status, body) = d
        .api(
            "PUT",
            "/api/models/current",
            Some(json!({ "model": "qwen3.5-2b" })),
        )
        .await;
    assert_eq!(status, StatusCode::CONFLICT);
    assert!(
        body["error"]["message"]
            .as_str()
            .unwrap()
            .contains("answering")
    );
    assert_eq!(
        d.api("GET", "/api/models", None).await.1["phase"]["state"],
        "idle"
    );
}

#[test]
fn the_model_given_wins_over_the_saved_one_and_the_saved_one_over_the_default() {
    let dir = tempfile::tempdir().unwrap();
    let path = model::settings_path(dir.path());
    let none = model::Settings::load(&path);
    assert_eq!(
        model::starting_model(None, &none),
        kjarni_cli::DEFAULT_AGENT_MODEL
    );
    model::Settings {
        model: Some("qwen3.5-9b".into()),
    }
    .save(&path)
    .unwrap();
    let saved = model::Settings::load(&path);
    assert_eq!(model::starting_model(None, &saved), "qwen3.5-9b");
    assert_eq!(
        model::starting_model(Some("qwen3.5-2b"), &saved),
        "qwen3.5-2b"
    );
}