brokk-mj-controller 2.14.0

Daemon-side controller, session manager, and web server for Mjolnir
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
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
/// The poller used to re-read and re-deserialise every live session's whole
/// transcript on every runtime snapshot, then compare ordinals to discover
/// that nothing had moved. On a real session that is 28,066 rows and
/// 635 MiB, per poll. The comparison has to happen before the read.
#[test]
fn an_unchanged_session_is_recognised_without_reading_its_transcript() {
    let runtime = runtime_view("session-1", 42, "digest-42");
    let published = PublishedView::of(&runtime);

    assert!(
        published.matches(&runtime),
        "an identical snapshot was treated as a change, so it would be re-read"
    );

    // Anything a viewer would notice has to defeat the skip.
    let advanced = runtime_view("session-1", 43, "digest-43");
    assert!(
        !published.matches(&advanced),
        "a moved projection was mistaken for an unchanged one"
    );

    // A digest change at the same ordinal is a rewritten projection, not a
    // quiet one: the convergence path exists precisely for this.
    let rewritten = runtime_view("session-1", 42, "digest-other");
    assert!(
        !published.matches(&rewritten),
        "a rewritten projection at the same ordinal was skipped"
    );

    // The transcript can stand still while the agent starts a turn, and a
    // viewer has to see that.
    let mut busy = runtime_view("session-1", 42, "digest-42");
    busy.connected = false;
    assert!(
        !published.matches(&busy),
        "a disconnect was skipped as unchanged"
    );
}

fn runtime_view(
    session_id: &str,
    projection_ordinal: u64,
    projection_digest: &str,
) -> crate::daemon::RuntimeSessionView {
    crate::daemon::RuntimeSessionView {
        session_id: session_id.to_owned(),
        projection_ordinal,
        projection_digest: projection_digest.to_owned(),
        operational: None,
        latest_credential_sync_signal: None,
        connected: true,
        error: None,
    }
}
use super::*;

fn podman_controller(state: SessionState) -> Controller {
    let session_id = "0123456789abcdef0123456789abcdef";
    let mut config = Config::default();
    config.profiles.insert(
        "codex".into(),
        mj_core::config::HarnessProfile {
            enabled: true,
            kind: mj_core::config::HarnessKind::Codex,
            home: PathBuf::from("/home/dev/.codex"),
            environment: Default::default(),
            context_window_bytes: None,
            guardian_review_model: None,
        },
    );
    config.targets.insert(
        "podman".into(),
        mj_core::config::TargetTemplate::LocalPodman {
            container: mj_core::config::ContainerTemplate {
                build_cache: None,
                image: "ubuntu:24.04".into(),
                pull_policy: Default::default(),
                platform: None,
                cpus: None,
                memory: None,
                environment: std::collections::BTreeMap::new(),
                workspace_storage: Default::default(),
            },
        },
    );
    config.bundles.insert(
        "project".into(),
        mj_core::config::ProjectBundle {
            primary_repo: "project".into(),
            repositories: vec![mj_core::config::ProjectRepository {
                id: "project".into(),
                github: Some("owner/project".into()),
                local: None,
                destination: "project".into(),
                git_ref: None,
            }],
        },
    );
    let mut app_state = State::default();
    app_state.sessions.insert(
        session_id.into(),
        mj_core::state::SessionRecord {
            build_cache: None,
            container_workspace: None,
            mjolnir_subagents: None,
            create_managed_worktree: None,
            workspace_id: mj_core::workspace::DEFAULT_WORKSPACE_ID.to_owned(),
            archived: false,
            container_cpus: None,
            container_memory: None,
            id: session_id.into(),
            title: "poll target".into(),
            harness_kind: mj_core::config::HarnessKind::Codex,
            last_profile: "codex".into(),
            bundle_id: "project".into(),
            project_directory: None,
            managed_worktree: None,
            target_template_id: "podman".into(),
            resource_allocation: None,
            additional_mounts: Vec::new(),
            state,
            target: Some(mj_core::state::TargetLocator::LocalPodman {
                borrowed_from: None,
                container_id: "a".repeat(64),
                workspace_storage: Default::default(),
            }),
            native_session_id: None,
            acp_session_title: None,
            session_title_override: None,
            created_at: "2026-08-27T00:00:00Z".into(),
            updated_at: "2026-08-27T00:00:00Z".into(),
            viewed_through_event_ordinal: 0,
            draft_input: String::new(),
            last_error: None,
            last_checkpoint_error: None,
            checkpoint: None,
        },
    );
    Controller {
        config,
        state: app_state,
    }
}

#[test]
fn recoverable_error_session_stays_out_of_live_target_pollers() {
    let running = podman_controller(SessionState::Running);
    assert_eq!(dashboard_worker_targets(&running).len(), 1);
    assert_eq!(dashboard_resource_targets(&running).len(), 1);
    assert_eq!(credential_sync_targets(&running).len(), 1);

    let recoverable_error = podman_controller(SessionState::Error);
    assert!(
        recoverable_error
            .state
            .sessions
            .values()
            .all(|session| session.target.is_some()),
        "the test session keeps its target so the exclusion is about its state"
    );
    assert!(
        !recoverable_error
            .state
            .sessions
            .values()
            .any(session_target_is_pollable),
        "an errored session is not dialed even while its target exists"
    );
    assert!(dashboard_worker_targets(&recoverable_error).is_empty());
    assert!(dashboard_resource_targets(&recoverable_error).is_empty());
    assert!(credential_sync_targets(&recoverable_error).is_empty());
}

/// A session gets its `target` as soon as the target exists, which is
/// before its worker binary has finished being copied into place. Polling
/// that window runs `execve` on a file `cp` still holds open for writing:
/// `ETXTBSY`, and a session recorded as unreachable while it was merely
/// still being built.
#[test]
fn a_provisioning_session_is_not_polled_before_its_worker_exists() {
    let provisioning = podman_controller(SessionState::Provisioning);
    assert!(
        provisioning
            .state
            .sessions
            .values()
            .all(|session| session.target.is_some())
    );

    assert!(dashboard_worker_targets(&provisioning).is_empty());
    assert!(dashboard_resource_targets(&provisioning).is_empty());

    // Provisioning connects to its own worker and then marks the session
    // running, which is when there is something to poll.
    let running = podman_controller(SessionState::Running);
    assert_eq!(dashboard_worker_targets(&running).len(), 1);
    assert_eq!(dashboard_resource_targets(&running).len(), 1);
}

#[test]
fn failed_destruction_stays_out_of_pollers_without_an_active_lifecycle() {
    let mut controller = podman_controller(SessionState::Destroying);
    for session in controller.state.sessions.values_mut() {
        session.last_error =
            Some("verified checkpoint retained; cleanup is safely retryable".into());
    }
    // No in-flight lifecycle exclusion survives a failed close or restart.
    let excluded = std::collections::BTreeSet::new();
    for _ in 0..3 {
        assert!(dashboard_worker_targets_excluding(&controller, &excluded).is_empty());
        assert!(dashboard_resource_targets(&controller).is_empty());
        assert!(credential_sync_targets(&controller).is_empty());
    }
    let closing = podman_controller(SessionState::Closing);
    assert_eq!(dashboard_worker_targets(&closing).len(), 1);
}

#[test]
fn lifecycle_owned_session_stays_out_of_worker_targets() {
    let controller = podman_controller(SessionState::Running);
    assert_eq!(dashboard_worker_targets(&controller).len(), 1);

    let excluded = controller
        .state
        .sessions
        .keys()
        .cloned()
        .collect::<std::collections::BTreeSet<_>>();

    assert!(dashboard_worker_targets_excluding(&controller, &excluded).is_empty());
}

#[test]
fn projection_rollback_race_retries_before_reporting_integrity_failure() {
    let mismatch = ProjectionMismatch {
        published_ordinal: 39,
        published_digest: "published".into(),
        durable_ordinal: 36,
        durable_digest: "durable".into(),
    };
    let mut convergence = ProjectionConvergence::default();

    for _ in 0..PROJECTION_CONVERGENCE_RETRIES {
        assert!(convergence.should_retry("session-1", mismatch.clone()));
    }
    assert!(
        !convergence.should_retry("session-1", mismatch),
        "a persistent mismatch must still become an integrity error"
    );

    convergence.converged("session-1");
    assert!(convergence.attempts.is_empty());
}

#[test]
fn a_changed_projection_mismatch_gets_its_own_convergence_window() {
    let mut convergence = ProjectionConvergence::default();
    let stale_lineage = ProjectionMismatch {
        published_ordinal: 39,
        published_digest: "old-lineage".into(),
        durable_ordinal: 36,
        durable_digest: "checkpoint".into(),
    };
    for _ in 0..=PROJECTION_CONVERGENCE_RETRIES {
        convergence.should_retry("session-1", stale_lineage.clone());
    }
    let equal_frontier_different_lineage = ProjectionMismatch {
        published_ordinal: 39,
        published_digest: "old-lineage".into(),
        durable_ordinal: 39,
        durable_digest: "new-lineage".into(),
    };

    assert!(convergence.should_retry("session-1", equal_frontier_different_lineage));
}

#[test]
fn worker_diagnosis_is_coalesced_for_one_unreachable_episode() {
    let mut tracker = WorkerDiagnosisTracker::default();
    let episode = tracker
        .observe("session-1", false, Some("connection refused".into()))
        .unwrap();

    assert_eq!(
        tracker.observe("session-1", false, Some("still unreachable".into())),
        None
    );
    assert_eq!(
        tracker.finish("session-1", episode),
        WorkerDiagnosisCompletion {
            display_error: Some("still unreachable".into()),
            restart_episode: None,
        }
    );
    assert_eq!(
        tracker.observe("session-1", false, Some("third poll".into())),
        None
    );
}

#[test]
fn stale_worker_diagnosis_is_not_published_after_reconnect() {
    let mut tracker = WorkerDiagnosisTracker::default();
    let first = tracker
        .observe("session-1", false, Some("first outage".into()))
        .unwrap();
    assert_eq!(tracker.observe("session-1", true, None), None);
    assert_eq!(
        tracker.observe("session-1", false, Some("new outage".into())),
        None
    );

    let completion = tracker.finish("session-1", first);
    assert_eq!(completion.display_error, None);
    let second = completion.restart_episode.unwrap();
    assert_eq!(
        tracker.finish("session-1", second).display_error.as_deref(),
        Some("new outage")
    );
}

#[test]
fn stale_worker_diagnosis_is_not_published_after_a_terminal_poll_error() {
    let mut tracker = WorkerDiagnosisTracker::default();
    let episode = tracker
        .observe("session-1", false, Some("relay failed".into()))
        .unwrap();

    assert_eq!(tracker.observe("session-1", false, None), None);
    assert_eq!(
        tracker.finish("session-1", episode),
        WorkerDiagnosisCompletion::default()
    );
}

#[tokio::test]
async fn quota_refresh_completion_keeps_its_generation() {
    let mut quotas = QuotaManager::default();
    let (updates, mut received) = tokio::sync::mpsc::channel(4);
    assert!(refresh_profile_quotas(&mut quotas, 42, &[], &updates).await);
    assert!(matches!(
        received.recv().await,
        Some(QuotaUpdate::Refreshing {
            profile_ids,
        }) if profile_ids.is_empty()
    ));
    assert!(matches!(
        received.recv().await,
        Some(QuotaUpdate::Finished { generation: 42 })
    ));

    let mut pending = Some(43);
    assert!(!complete_manual_quota_refresh(&mut pending, 42));
    assert_eq!(pending, Some(43));
    assert!(complete_manual_quota_refresh(&mut pending, 43));
    assert_eq!(pending, None);
    quotas.shutdown().await;
}

#[test]
fn quota_refresh_requests_exclude_disabled_profiles() {
    let mut controller = podman_controller(SessionState::Stopped);
    let mut disabled = controller.config.profiles["codex"].clone();
    disabled.enabled = false;
    controller
        .config
        .profiles
        .insert("reserve".into(), disabled);

    let requests = quota_refresh_profiles(&controller);

    assert_eq!(
        requests
            .iter()
            .map(|request| request.profile_id.as_str())
            .collect::<Vec<_>>(),
        ["codex"]
    );
}

#[test]
fn resource_samples_are_throttled_to_one_per_minute() {
    let started = tokio::time::Instant::now();
    assert!(!resource_sample_is_due(
        Some(&started),
        started + Duration::from_secs(59),
    ));
    assert!(resource_sample_is_due(
        Some(&started),
        started + RESOURCE_POLL_INTERVAL,
    ));
}

struct PendingCapacityProbe {
    target: DeploymentCapacityTarget,
    finish: tokio::sync::oneshot::Sender<Result<Option<DeploymentCapacityUsage>>>,
}

struct CapacityPollerFixture {
    targets: tokio::sync::watch::Sender<Vec<DeploymentCapacityTarget>>,
    triggers: tokio::sync::mpsc::Sender<()>,
    updates: tokio::sync::mpsc::Receiver<CapacityPollUpdate>,
    started: tokio::sync::mpsc::UnboundedReceiver<PendingCapacityProbe>,
}

impl CapacityPollerFixture {
    fn new() -> Self {
        let (started_tx, started) = tokio::sync::mpsc::unbounded_channel();
        let (targets, triggers, updates) = spawn_capacity_poller_with(move |target| {
            let started_tx = started_tx.clone();
            async move {
                let (finish, result) = tokio::sync::oneshot::channel();
                started_tx
                    .send(PendingCapacityProbe { target, finish })
                    .unwrap();
                result.await.context("test probe completion dropped")?
            }
        });
        Self {
            targets,
            triggers,
            updates,
            started,
        }
    }

    async fn assert_no_start(&mut self) {
        assert!(
            tokio::time::timeout(Duration::from_millis(1), self.started.recv())
                .await
                .is_err()
        );
    }
}

fn capacity_target(id: &str) -> DeploymentCapacityTarget {
    DeploymentCapacityTarget {
        id: id.into(),
        host: id.into(),
        target_ids: vec![id.into()],
        kind: DeploymentCapacityKind::Host,
        local: true,
        probes: Vec::new(),
        probe_error: None,
    }
}

#[tokio::test(start_paused = true)]
async fn capacity_samples_follow_timer_and_manual_refresh_not_unchanged_publications() {
    let mut fixture = CapacityPollerFixture::new();
    let targets = vec![capacity_target("local")];
    fixture.targets.send_replace(targets.clone());
    fixture
        .started
        .recv()
        .await
        .unwrap()
        .finish
        .send(Ok(None))
        .unwrap();
    assert!(fixture.updates.recv().await.unwrap().result.is_ok());

    fixture.targets.send_replace(targets);
    fixture.assert_no_start().await;
    tokio::time::advance(Duration::from_secs(29)).await;
    fixture.assert_no_start().await;
    tokio::time::advance(Duration::from_secs(1)).await;
    fixture
        .started
        .recv()
        .await
        .unwrap()
        .finish
        .send(Ok(None))
        .unwrap();
    assert!(fixture.updates.recv().await.unwrap().result.is_ok());

    fixture.triggers.send(()).await.unwrap();
    fixture
        .started
        .recv()
        .await
        .unwrap()
        .finish
        .send(Ok(None))
        .unwrap();
    assert!(fixture.updates.recv().await.unwrap().result.is_ok());
    fixture.assert_no_start().await;
}

#[tokio::test(start_paused = true)]
async fn capacity_busy_targets_coalesce_requests_without_blocking_other_targets() {
    let mut fixture = CapacityPollerFixture::new();
    let first_target = capacity_target("first");
    fixture.targets.send_replace(vec![first_target.clone()]);
    let first = fixture.started.recv().await.unwrap();
    fixture
        .targets
        .send_replace(vec![first_target, capacity_target("second")]);
    let second = fixture.started.recv().await.unwrap();
    assert_eq!(second.target.id, "second");

    fixture.triggers.send(()).await.unwrap();
    fixture.assert_no_start().await;
    tokio::time::advance(CAPACITY_POLL_INTERVAL).await;
    fixture.assert_no_start().await;
    first.finish.send(Ok(None)).unwrap();
    second.finish.send(Ok(None)).unwrap();
    assert!(fixture.updates.recv().await.unwrap().result.is_ok());
    assert!(fixture.updates.recv().await.unwrap().result.is_ok());
    fixture.assert_no_start().await;
}

#[tokio::test(start_paused = true)]
async fn capacity_changed_targets_get_one_follow_up_and_removed_results_are_discarded() {
    let mut fixture = CapacityPollerFixture::new();
    let mut target = capacity_target("local");
    fixture.targets.send_replace(vec![target.clone()]);
    let first = fixture.started.recv().await.unwrap();
    target.host = "new-host".into();
    fixture.targets.send_replace(vec![target.clone()]);
    fixture.assert_no_start().await;
    first.finish.send(Ok(None)).unwrap();
    let changed = fixture.started.recv().await.unwrap();
    assert_eq!(changed.target, target);
    assert!(
        fixture.updates.try_recv().is_err(),
        "old configuration result escaped"
    );
    changed
        .finish
        .send(Err(anyhow::anyhow!("new host unavailable")))
        .unwrap();
    assert!(
        fixture
            .updates
            .recv()
            .await
            .unwrap()
            .result
            .unwrap_err()
            .contains("new host unavailable")
    );

    fixture.triggers.send(()).await.unwrap();
    let removed = fixture.started.recv().await.unwrap();
    fixture.targets.send_replace(Vec::new());
    fixture.assert_no_start().await;
    removed.finish.send(Ok(None)).unwrap();
    assert!(
        tokio::time::timeout(Duration::from_millis(1), fixture.updates.recv())
            .await
            .is_err()
    );
    fixture.targets.send_replace(vec![target]);
    let mut last = fixture.started.recv().await.unwrap();
    drop(fixture.updates);
    last.finish.closed().await;
}

#[tokio::test]
async fn capacity_probe_panics_are_reported_and_do_not_prevent_retry() {
    let first = AtomicBool::new(true);
    let (targets, triggers, mut updates) = spawn_capacity_poller_with(move |_| {
        if first.swap(false, Ordering::SeqCst) {
            panic!("test capacity probe panic");
        }
        async { Ok(None) }
    });
    targets.send_replace(vec![capacity_target("local")]);
    let failure = updates.recv().await.unwrap();
    assert_eq!(failure.target_id, "local");
    assert!(
        failure
            .result
            .unwrap_err()
            .contains("test capacity probe panic")
    );
    triggers.send(()).await.unwrap();
    assert!(updates.recv().await.unwrap().result.is_ok());
}

#[tokio::test(start_paused = true)]
async fn capacity_results_are_revalidated_after_output_backpressure() {
    let mut fixture = CapacityPollerFixture::new();
    let mut targets: Vec<_> = (0..65).map(|id| capacity_target(&id.to_string())).collect();
    fixture.targets.send_replace(targets.clone());
    let mut pending = Vec::new();
    for _ in 0..65 {
        pending.push(fixture.started.recv().await.unwrap());
    }
    let last = pending.pop().unwrap();
    let last_id = last.target.id;
    for probe in pending {
        probe.finish.send(Ok(None)).unwrap();
    }
    fixture.assert_no_start().await;
    assert_eq!(fixture.updates.len(), 64);
    last.finish.send(Ok(None)).unwrap();
    fixture.assert_no_start().await;

    targets
        .iter_mut()
        .find(|target| target.id == last_id)
        .unwrap()
        .host = "changed".into();
    fixture.targets.send_replace(targets);
    for _ in 0..64 {
        let update = fixture.updates.recv().await.unwrap();
        assert_ne!(update.target_id, last_id);
    }
    let changed = fixture.started.recv().await.unwrap();
    assert_eq!(changed.target.id, last_id);
    assert_eq!(changed.target.host, "changed");
    assert!(
        fixture.updates.try_recv().is_err(),
        "stale blocked result escaped"
    );
    changed.finish.send(Ok(None)).unwrap();
    assert_eq!(fixture.updates.recv().await.unwrap().target_id, last_id);
    fixture.assert_no_start().await;
}

#[tokio::test(start_paused = true)]
async fn capacity_timeout_retains_blocking_sample_until_it_exits() {
    let (started_tx, started_rx) = tokio::sync::oneshot::channel();
    let (finish_tx, finish_rx) = std::sync::mpsc::channel();
    let sample = tokio::spawn(collect_local_capacity_with(move || {
        started_tx.send(()).unwrap();
        // Dropping finish_tx on a test failure also releases this thread.
        finish_rx.recv().context("test sample was cancelled")?;
        Ok(DeploymentCapacityUsage {
            cpu_percent: Some(10),
            memory_used_bytes: 1,
            memory_total_bytes: 2,
            logical_cores: 4,
            disk_total_bytes: None,
        })
    }));
    started_rx.await.unwrap();
    tokio::time::advance(RESOURCE_POLL_TIMEOUT + Duration::from_secs(1)).await;
    tokio::task::yield_now().await;
    assert!(
        !sample.is_finished(),
        "timeout released a still-running blocking sample"
    );
    finish_tx.send(()).unwrap();
    assert!(
        sample
            .await
            .unwrap()
            .unwrap_err()
            .to_string()
            .contains("timed out")
    );
}

#[test]
fn a_new_credential_signal_waits_out_the_cooldown_without_being_lost() {
    let signal = |ordinal, reason| CredentialSyncSignal { ordinal, reason };
    let mut tracker = CredentialSyncSignalTracker::default();
    let started = Instant::now();
    tracker.observe(
        "session",
        "work",
        signal(41, CredentialSyncReason::AuthenticationFailure),
    );
    assert_eq!(
        tracker.drain_due(started),
        vec![(
            "session".into(),
            "work".into(),
            CredentialSyncReason::AuthenticationFailure
        )]
    );

    tracker.observe(
        "session",
        "work",
        signal(42, CredentialSyncReason::AuthenticationFailure),
    );
    assert!(
        tracker
            .drain_due(started + Duration::from_secs(60))
            .is_empty()
    );
    tracker.observe(
        "session",
        "new-profile",
        signal(43, CredentialSyncReason::EmptyPromptResponse),
    );
    assert_eq!(tracker.pending["session"].signal.ordinal, 43);

    // No repeated observation is needed: the loop timer drains the sticky
    // failure once its cooldown expires.
    assert_eq!(
        tracker.drain_due(started + IMMEDIATE_CREDENTIAL_SYNC_COOLDOWN),
        vec![(
            "session".into(),
            "new-profile".into(),
            CredentialSyncReason::EmptyPromptResponse
        )]
    );
    tracker.observe(
        "session",
        "new-profile",
        signal(43, CredentialSyncReason::EmptyPromptResponse),
    );
    assert!(
        tracker
            .drain_due(started + (IMMEDIATE_CREDENTIAL_SYNC_COOLDOWN * 2))
            .is_empty()
    );

    tracker.observe(
        "other",
        "personal",
        signal(1, CredentialSyncReason::AuthenticationFailure),
    );
    assert_eq!(
        tracker.drain_due(started + Duration::from_secs(60)),
        vec![(
            "other".into(),
            "personal".into(),
            CredentialSyncReason::AuthenticationFailure
        )]
    );
}

#[test]
fn a_healthy_credential_cycle_stays_out_of_the_ui() {
    let result = mj_core::credentials::CredentialSyncResult {
        profile_id: "work".into(),
        trigger: None,
        failure: None,
        outcomes: Vec::new(),
    };
    assert_eq!(CredentialSyncNotices::default().notice(&result, None), None);
}

#[test]
fn github_tokens_sync_to_every_remote_target_but_raw_localhost() {
    use mj_core::state::TargetLocator;

    let remotes = [
        TargetLocator::LocalPodman {
            borrowed_from: None,
            container_id: "podman".into(),
            workspace_storage: Default::default(),
        },
        TargetLocator::AppleContainer {
            borrowed_from: None,
            container_id: "apple".into(),
        },
        TargetLocator::AwsEc2 {
            instance_id: "i-123".into(),
            address: Some("example.invalid".into()),
        },
        TargetLocator::SshBare {
            host: "ssh.example".into(),
            workspace: "/workspace".into(),
            worker_id: None,
        },
        TargetLocator::SshPodman {
            borrowed_from: None,
            host: "ssh.example".into(),
            container_id: "remote-podman".into(),
            workspace_storage: Default::default(),
        },
        TargetLocator::SshDocker {
            borrowed_from: None,
            host: "ssh.example".into(),
            container_id: "remote-docker".into(),
        },
    ];
    for target in &remotes {
        assert!(target_syncs_github_token(Some(target)), "{target:?}");
    }
    assert!(!target_syncs_github_token(Some(
        &TargetLocator::LocalBare {
            worker_root: "/tmp/worker".into(),
        }
    )));
    assert!(!target_syncs_github_token(None));
}

#[test]
fn an_authentication_failure_notice_says_whether_anything_was_pushed() {
    use mj_core::credentials::{CredentialSyncAction, CredentialSyncOutcome, CredentialSyncResult};

    let mut notices = CredentialSyncNotices::default();
    let pushed = CredentialSyncResult {
        profile_id: "work".into(),
        trigger: Some(CredentialSyncCause {
            session_id: "018f9dd2-a3b4".into(),
            reason: CredentialSyncReason::AuthenticationFailure,
        }),
        failure: None,
        outcomes: vec![CredentialSyncOutcome {
            session_id: "018f9dd2-a3b4".into(),
            outcome: Ok(vec![CredentialSyncAction::Pushed]),
        }],
    };
    let notice = notices.notice(&pushed, None).unwrap();
    assert!(notice.contains("were pushed"), "{notice}");
    assert!(notice.contains("mj login --profile work"), "{notice}");

    let nothing_to_push = CredentialSyncResult {
        trigger: Some(CredentialSyncCause {
            session_id: "018f9dd2-a3b4".into(),
            reason: CredentialSyncReason::AuthenticationFailure,
        }),
        outcomes: Vec::new(),
        ..pushed
    };
    let notice = notices.notice(&nothing_to_push, None).unwrap();
    assert!(notice.contains("nothing fresher"), "{notice}");
    assert!(notice.contains("mj login --profile work"), "{notice}");
    // The per-session cooldown upstream limits these; the dedup must not.
    assert_eq!(notices.notice(&nothing_to_push, None), Some(notice));
}

#[test]
fn a_claude_authentication_failure_offers_the_long_lived_token() {
    use mj_core::config::HarnessKind;
    use mj_core::credentials::{CredentialSyncOutcome, CredentialSyncResult};

    let result = CredentialSyncResult {
        profile_id: "claude-max".into(),
        trigger: Some(CredentialSyncCause {
            session_id: "018f9dd2-a3b4".into(),
            reason: CredentialSyncReason::AuthenticationFailure,
        }),
        failure: None,
        outcomes: Vec::new(),
    };

    let claude = CredentialSyncNotices::default()
        .notice(&result, Some(HarnessKind::Claude))
        .unwrap();
    assert!(
        claude.ends_with(
            "Run `mj login --profile claude-max`, or store a long-lived token with `mj login --profile claude-max --setup-token`."
        ),
        "{claude}"
    );

    // Only Claude can rotate ahead of expiry this way.
    let codex = CredentialSyncNotices::default()
        .notice(&result, Some(HarnessKind::Codex))
        .unwrap();
    assert!(
        codex.ends_with("Run `mj login --profile claude-max`."),
        "{codex}"
    );

    // The advice also reaches a failed reconciliation, not only a clean one.
    let failed = CredentialSyncResult {
        outcomes: vec![CredentialSyncOutcome {
            session_id: "018f9dd2-a3b4".into(),
            outcome: Err("worker proxy disconnected".into()),
        }],
        ..result
    };
    let claude_failure = CredentialSyncNotices::default()
        .notice(&failed, Some(HarnessKind::Claude))
        .unwrap();
    assert!(
        claude_failure.contains("--setup-token`."),
        "{claude_failure}"
    );
}

#[test]
fn an_empty_prompt_notice_does_not_claim_authentication_failed() {
    use mj_core::credentials::{CredentialSyncAction, CredentialSyncOutcome, CredentialSyncResult};

    let result = CredentialSyncResult {
        profile_id: "work".into(),
        trigger: Some(CredentialSyncCause {
            session_id: "018f9dd2-a3b4".into(),
            reason: CredentialSyncReason::EmptyPromptResponse,
        }),
        failure: None,
        outcomes: vec![CredentialSyncOutcome {
            session_id: "018f9dd2-a3b4".into(),
            outcome: Ok(vec![CredentialSyncAction::Pushed]),
        }],
    };
    let notice = CredentialSyncNotices::default()
        .notice(&result, None)
        .unwrap();
    assert!(notice.contains("returned no response"), "{notice}");
    assert!(notice.contains("were pushed"), "{notice}");
    assert!(!notice.contains("Auth failure"), "{notice}");
}

#[test]
fn an_immediate_sync_failure_is_not_reported_as_no_new_credentials() {
    use mj_core::credentials::CredentialSyncResult;

    let result = CredentialSyncResult {
        profile_id: "work".into(),
        trigger: Some(CredentialSyncCause {
            session_id: "018f9dd2-a3b4".into(),
            reason: CredentialSyncReason::AuthenticationFailure,
        }),
        failure: Some("controller credential file is unreadable".into()),
        outcomes: Vec::new(),
    };
    let notice = CredentialSyncNotices::default()
        .notice(&result, None)
        .unwrap();
    assert!(notice.contains("reconciliation failed"), "{notice}");
    assert!(notice.contains("credential file is unreadable"), "{notice}");
    assert!(!notice.contains("nothing fresher"), "{notice}");
}

#[test]
fn a_failed_credential_sync_is_reported() {
    use mj_core::credentials::{CredentialSyncOutcome, CredentialSyncResult};

    let result = CredentialSyncResult {
        profile_id: "work".into(),
        trigger: None,
        failure: None,
        outcomes: vec![CredentialSyncOutcome {
            session_id: "018f9dd2-a3b4".into(),
            outcome: Err("worker proxy disconnected".into()),
        }],
    };
    let notice = CredentialSyncNotices::default()
        .notice(&result, None)
        .unwrap();
    assert!(notice.contains("worker proxy disconnected"), "{notice}");
}

#[test]
fn a_repeated_credential_failure_is_reported_once_until_it_changes() {
    use mj_core::credentials::{CredentialSyncAction, CredentialSyncOutcome, CredentialSyncResult};

    let failed = |detail: &str| CredentialSyncResult {
        profile_id: "work".into(),
        trigger: None,
        failure: None,
        outcomes: vec![CredentialSyncOutcome {
            session_id: "018f9dd2-a3b4".into(),
            outcome: Err(detail.to_owned()),
        }],
    };
    let mut notices = CredentialSyncNotices::default();

    assert!(
        notices
            .notice(&failed("worker proxy disconnected"), None)
            .is_some()
    );
    assert_eq!(
        notices.notice(&failed("worker proxy disconnected"), None),
        None
    );

    let changed = notices.notice(&failed("container is gone"), None).unwrap();
    assert!(changed.contains("container is gone"), "{changed}");
    assert_eq!(notices.notice(&failed("container is gone"), None), None);

    // A clean cycle forgets the failure, so a recurrence is reported again.
    let healthy = CredentialSyncResult {
        profile_id: "work".into(),
        trigger: None,
        failure: None,
        outcomes: vec![CredentialSyncOutcome {
            session_id: "018f9dd2-a3b4".into(),
            outcome: Ok(vec![CredentialSyncAction::Pushed]),
        }],
    };
    assert_eq!(notices.notice(&healthy, None), None);
    assert!(notices.notice(&failed("container is gone"), None).is_some());
}

#[test]
fn a_repeated_whole_sync_failure_is_reported_once_per_profile() {
    use mj_core::credentials::CredentialSyncResult;

    let failed = |profile_id: &str| CredentialSyncResult {
        profile_id: profile_id.to_owned(),
        trigger: None,
        failure: Some("controller home is unreadable".into()),
        outcomes: Vec::new(),
    };
    let mut notices = CredentialSyncNotices::default();

    let notice = notices.notice(&failed("work"), None).unwrap();
    assert!(notice.contains("profile work"), "{notice}");
    assert_eq!(notices.notice(&failed("work"), None), None);
    // Another profile failing the same way is its own key.
    assert!(notices.notice(&failed("personal"), None).is_some());
    assert_eq!(notices.notice(&failed("work"), None), None);
}

#[test]
fn skills_and_github_syncs_speak_while_harness_credentials_stay_out_of_the_notice() {
    use mj_core::credentials::{CredentialSyncAction, CredentialSyncOutcome, CredentialSyncResult};

    let result = CredentialSyncResult {
        profile_id: "work".into(),
        trigger: None,
        failure: None,
        outcomes: vec![
            CredentialSyncOutcome {
                session_id: "018f9dd2-a3b4".into(),
                outcome: Ok(vec![
                    CredentialSyncAction::Pushed,
                    CredentialSyncAction::SkillsPushed,
                    CredentialSyncAction::GithubTokenPushed,
                ]),
            },
            CredentialSyncOutcome {
                session_id: "018f9dd2-bbbb".into(),
                outcome: Ok(vec![
                    CredentialSyncAction::SkillsPushed,
                    CredentialSyncAction::GithubTokenRemoved,
                ]),
            },
        ],
    };
    let notice = CredentialSyncNotices::default()
        .notice(&result, None)
        .unwrap();
    assert!(!notice.contains("harness credentials"), "{notice}");
    assert!(
        notice.contains("Synced skills for profile work to 2 session(s)."),
        "{notice}"
    );
    assert!(
        notice.contains("Synced the GitHub CLI token to 1 session(s)."),
        "{notice}"
    );
    assert!(
        notice.contains("Removed the GitHub CLI token from 1 session(s)."),
        "{notice}"
    );
}

#[test]
fn aws_capacity_sums_live_instance_allocations() {
    let total = aggregate_aws_capacity(&[
        DeploymentCapacityUsage {
            cpu_percent: None,
            memory_used_bytes: 0,
            memory_total_bytes: 8,
            logical_cores: 2,
            disk_total_bytes: Some(100),
        },
        DeploymentCapacityUsage {
            cpu_percent: None,
            memory_used_bytes: 0,
            memory_total_bytes: 16,
            logical_cores: 4,
            disk_total_bytes: Some(200),
        },
    ])
    .unwrap();

    assert_eq!(total.memory_total_bytes, 24);
    assert_eq!(total.logical_cores, 6);
    assert_eq!(total.disk_total_bytes, Some(300));
}

/// Collects what the daemon would have told the user about a download.
#[derive(Default)]
struct RefreshReports(std::sync::Mutex<Vec<ImageRefreshReport>>);

impl RefreshReports {
    fn record(&self) -> impl Fn(ImageRefreshReport) + '_ {
        |report| self.0.lock().unwrap().push(report)
    }

    fn taken(&self) -> Vec<ImageRefreshReport> {
        std::mem::take(&mut *self.0.lock().unwrap())
    }
}

/// The pre-pull only helps if it starts before the person opens the New
/// Session wizard, so the first refresh is a startup refresh and the hourly
/// interval follows it.
#[tokio::test(start_paused = true)]
async fn the_first_refresh_runs_at_startup() {
    assert!(
        IMAGE_REFRESH_DELAY <= Duration::from_secs(5),
        "the first refresh is the pre-pull for the first session: {IMAGE_REFRESH_DELAY:?}"
    );

    let calls = Arc::new(std::sync::atomic::AtomicUsize::new(0));
    let cancellation = tokio_util::sync::CancellationToken::new();
    let refresher = spawn_image_refresher(
        {
            let calls = calls.clone();
            move || {
                calls.fetch_add(1, Ordering::Release);
                Vec::new()
            }
        },
        |_report| {},
        cancellation.clone(),
    );

    // Let the task reach its first await so the interval's deadline is set
    // from the same instant the test then advances past.
    tokio::task::yield_now().await;
    tokio::time::advance(IMAGE_REFRESH_DELAY + Duration::from_millis(1)).await;
    tokio::task::yield_now().await;
    assert_eq!(
        calls.load(Ordering::Acquire),
        1,
        "the refresher should have planned a refresh at startup"
    );

    tokio::time::advance(IMAGE_REFRESH_INTERVAL).await;
    tokio::task::yield_now().await;
    assert_eq!(
        calls.load(Ordering::Acquire),
        2,
        "the hourly interval should follow the startup refresh"
    );

    cancellation.cancel();
    refresher.await.expect("the refresher stops when cancelled");
}

/// The pre-pull's whole point: an image the host does not have is downloaded
/// once, and the hourly refresh after that only checks that it is still there.
#[test]
fn a_missing_image_is_pulled_once_and_not_again_when_present() {
    /// Reports the image as absent until a pull has run, the way a host
    /// behaves the first time it sees an image.
    struct FirstPullExecutor {
        commands: std::sync::Mutex<Vec<Vec<String>>>,
        pulled: std::sync::atomic::AtomicBool,
    }

    impl CommandExecutor for FirstPullExecutor {
        fn execute(&self, command: &CommandSpec) -> Result<CommandOutput> {
            self.commands.lock().unwrap().push(command.args.clone());
            if command.args.first().map(String::as_str) == Some("pull") {
                self.pulled.store(true, Ordering::Release);
                return Ok(CommandOutput {
                    status: 0,
                    stdout: Vec::new(),
                    stderr: Vec::new(),
                });
            }
            if command.args.contains(&"inspect".to_owned()) && !self.pulled.load(Ordering::Acquire)
            {
                return Ok(CommandOutput {
                    status: 125,
                    stdout: Vec::new(),
                    stderr: b"no such image".to_vec(),
                });
            }
            Ok(CommandOutput {
                status: 0,
                stdout: b"sha256:1111\n".to_vec(),
                stderr: Vec::new(),
            })
        }
    }

    let refresh = crate::targets::image_refresh(
        crate::targets::ImageHost::LocalPodman,
        "ghcr.io/example/dev:1.2.3",
        None,
        mj_core::config::ImagePullPolicy::Auto,
    )
    .expect("a versioned tag is downloaded when the host lacks it");
    assert_eq!(refresh.when, crate::targets::RefreshWhen::WhenAbsent);

    let executor = FirstPullExecutor {
        commands: std::sync::Mutex::new(Vec::new()),
        pulled: std::sync::atomic::AtomicBool::new(false),
    };
    let reports = RefreshReports::default();
    assert_eq!(
        refresh_host_image(&refresh, &executor, &reports.record())
            .expect("the first refresh downloads the image"),
        ImageRefreshOutcome::Pulled {
            id: "sha256:1111".to_owned()
        }
    );
    assert_eq!(
        reports.taken(),
        vec![
            ImageRefreshReport::Started {
                host: "local podman".to_owned(),
                image: "ghcr.io/example/dev:1.2.3".to_owned(),
            },
            ImageRefreshReport::Pulled {
                host: "local podman".to_owned(),
                image: "ghcr.io/example/dev:1.2.3".to_owned(),
            },
        ],
        "the user should hear about the download and about it finishing"
    );
    assert_eq!(
        refresh_host_image(&refresh, &executor, &reports.record())
            .expect("the second refresh finds it present"),
        ImageRefreshOutcome::Present
    );
    assert!(
        reports.taken().is_empty(),
        "an hourly check that downloads nothing has nothing to say"
    );

    let commands = executor.commands.lock().unwrap();
    let pulls = commands
        .iter()
        .filter(|args| args.first().map(String::as_str) == Some("pull"))
        .count();
    assert_eq!(pulls, 1, "the image was downloaded twice: {commands:?}");
    let prunes = commands
        .iter()
        .filter(|args| args.contains(&"prune".to_owned()))
        .count();
    assert_eq!(
        prunes, 1,
        "only the refresh that downloaded the image has anything to prune: {commands:?}"
    );
    assert!(
        commands
            .last()
            .is_some_and(|args| args.contains(&"inspect".to_owned())),
        "the second refresh should stop after finding the image present: {commands:?}"
    );
}

/// A moving tag keeps its hourly pull: a present image is not the same as a
/// current one.
#[test]
fn an_always_refresh_pulls_even_when_the_image_is_present() {
    struct PresentExecutor {
        commands: std::sync::Mutex<Vec<Vec<String>>>,
    }

    impl CommandExecutor for PresentExecutor {
        fn execute(&self, command: &CommandSpec) -> Result<CommandOutput> {
            self.commands.lock().unwrap().push(command.args.clone());
            Ok(CommandOutput {
                status: 0,
                stdout: b"sha256:2222\n".to_vec(),
                stderr: Vec::new(),
            })
        }
    }

    let refresh = crate::targets::image_refresh(
        crate::targets::ImageHost::LocalPodman,
        "ghcr.io/example/dev:latest",
        None,
        mj_core::config::ImagePullPolicy::Auto,
    )
    .expect("a remote latest image is refreshed");
    assert_eq!(refresh.when, crate::targets::RefreshWhen::Always);

    let executor = PresentExecutor {
        commands: std::sync::Mutex::new(Vec::new()),
    };
    assert_eq!(
        refresh_host_image(&refresh, &executor, &|_report| {}).expect("the refresh runs"),
        ImageRefreshOutcome::Unchanged
    );

    let commands = executor.commands.lock().unwrap();
    assert!(
        commands
            .iter()
            .any(|args| args.first().map(String::as_str) == Some("pull")),
        "a moving tag must still be pulled: {commands:?}"
    );
    assert!(
        commands
            .iter()
            .any(|args| args.contains(&"prune".to_owned())),
        "a pull that ran still prunes: {commands:?}"
    );
}

/// A background refresh is a chore, not a launch. One host that cannot
/// reach its registry must not cost the other hosts their pull, and the
/// failure has to say which host, which image, and what the engine
/// reported. `refresh_images` gives every host its own task for the same
/// reason.
#[test]
fn a_failed_pull_is_reported_and_leaves_the_other_host_alone() {
    struct FailingPullExecutor {
        failing_image: String,
        commands: std::sync::Mutex<Vec<(String, Vec<String>)>>,
    }

    impl CommandExecutor for FailingPullExecutor {
        fn execute(&self, command: &CommandSpec) -> Result<CommandOutput> {
            self.commands
                .lock()
                .unwrap()
                .push((command.program.clone(), command.args.clone()));
            if command.args.contains(&"pull".to_owned())
                && command.args.contains(&self.failing_image)
            {
                return Ok(CommandOutput {
                    status: 125,
                    stdout: Vec::new(),
                    stderr: b"short-name resolution failed".to_vec(),
                });
            }
            Ok(CommandOutput {
                status: 0,
                stdout: b"sha256:1111\n".to_vec(),
                stderr: Vec::new(),
            })
        }
    }

    let failing_image = "ghcr.io/example/broken:latest";
    let broken = crate::targets::image_refresh(
        crate::targets::ImageHost::LocalPodman,
        failing_image,
        None,
        mj_core::config::ImagePullPolicy::Auto,
    )
    .expect("a remote latest image is refreshed");
    let healthy = crate::targets::image_refresh(
        crate::targets::ImageHost::LocalDocker,
        "ghcr.io/example/dev:latest",
        None,
        mj_core::config::ImagePullPolicy::Auto,
    )
    .expect("a remote latest image is refreshed");
    let executor = FailingPullExecutor {
        failing_image: failing_image.to_owned(),
        commands: std::sync::Mutex::new(Vec::new()),
    };

    let reports = RefreshReports::default();
    let mut last_failures = BTreeMap::new();

    let reported = refresh_host_image(&broken, &executor, &reports.record())
        .expect_err("a failed pull has to reach the caller");
    let reported = format!("{reported:#}");
    assert!(
        reported.contains("short-name resolution failed"),
        "{reported}"
    );
    assert!(reported.contains(failing_image), "{reported}");
    record_refresh_result(
        &mut last_failures,
        &broken.host.label(),
        &broken.image,
        Some(reported.clone()),
        &reports.record(),
    );

    refresh_host_image(&healthy, &executor, &reports.record())
        .expect("the second host still refreshes");
    record_refresh_result(
        &mut last_failures,
        &healthy.host.label(),
        &healthy.image,
        None,
        &reports.record(),
    );

    let failures = reports
        .taken()
        .into_iter()
        .filter(|report| matches!(report, ImageRefreshReport::Failed { .. }))
        .collect::<Vec<_>>();
    assert_eq!(
        failures,
        vec![ImageRefreshReport::Failed {
            host: "local podman".to_owned(),
            image: failing_image.to_owned(),
            error: reported,
        }],
        "only the host that could not pull is reported as failed"
    );

    let commands = executor.commands.lock().unwrap();
    let ran = |program: &str, args: &[&str]| {
        commands
            .iter()
            .any(|(command, arguments)| command == program && arguments == args)
    };
    assert!(ran("podman", &["pull", failing_image]), "{commands:?}");
    assert!(
        !ran("podman", &["image", "prune", "-f"]),
        "a host that could not pull has nothing to prune: {commands:?}"
    );
    assert!(
        ran("docker", &["pull", "ghcr.io/example/dev:latest"]),
        "{commands:?}"
    );
    assert!(ran("docker", &["image", "prune", "-f"]), "{commands:?}");
}

/// An unreachable host fails the same way every hour. The user hears about it
/// once, and hears again only when something changes.
#[test]
fn a_failed_pull_is_reported_once_until_the_error_changes() {
    let reports = RefreshReports::default();
    let mut last_failures = BTreeMap::new();
    let host = "podman on builder.example.test";
    let image = "ghcr.io/example/dev:latest";
    let fail = |last_failures: &mut BTreeMap<String, String>, error: &str| {
        record_refresh_result(
            last_failures,
            host,
            image,
            Some(error.to_owned()),
            &reports.record(),
        );
    };

    fail(
        &mut last_failures,
        "ssh: connect to host builder: timed out",
    );
    assert_eq!(reports.taken().len(), 1, "the first failure is news");

    fail(
        &mut last_failures,
        "ssh: connect to host builder: timed out",
    );
    assert!(
        reports.taken().is_empty(),
        "the same failure an hour later is not news"
    );

    fail(&mut last_failures, "podman: no space left on device");
    assert_eq!(
        reports.taken(),
        vec![ImageRefreshReport::Failed {
            host: host.to_owned(),
            image: image.to_owned(),
            error: "podman: no space left on device".to_owned(),
        }],
        "a different failure is news again"
    );

    // A success clears the record, so the next failure is news even if it
    // reads exactly like the last one.
    record_refresh_result(&mut last_failures, host, image, None, &reports.record());
    assert!(reports.taken().is_empty(), "a success says nothing here");
    fail(&mut last_failures, "podman: no space left on device");
    assert_eq!(
        reports.taken().len(),
        1,
        "a failure after a success is news again"
    );
}

/// The default configuration names every local engine, installed or not. An
/// engine that is not on the machine is skipped, not reported as a failed
/// download; a remote host is always tried, because its engine is elsewhere.
#[test]
fn an_uninstalled_local_engine_is_skipped_by_the_image_refresh() {
    let directory = tempfile::tempdir().unwrap();
    std::fs::write(directory.path().join("podman"), "").unwrap();
    let path = std::env::join_paths([directory.path()]).unwrap();

    assert!(local_engine_installed(&ImageHost::LocalPodman, Some(&path)));
    assert!(!local_engine_installed(
        &ImageHost::LocalDocker,
        Some(&path)
    ));
    assert!(!local_engine_installed(&ImageHost::LocalPodman, None));
    assert!(local_engine_installed(
        &ImageHost::SshDocker(crate::targets::SshTarget {
            destination: "build@example".into(),
            ssh_args: Vec::new(),
        }),
        None,
    ));
}