brokk-mj-controller 2.22.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
use super::*;

/// Add lifecycle guidance only for targets that Hel destroys as a whole.
pub(super) fn append_hel_target_environment(
    harness: mj_core::config::HarnessKind,
    destination: &Path,
    target: &targets::TargetLocator,
) -> Result<()> {
    let environment = match target {
        targets::TargetLocator::LocalPodman { .. }
        | targets::TargetLocator::LocalDocker { .. }
        | targets::TargetLocator::AppleContainer { .. }
        | targets::TargetLocator::SshPodman { .. }
        | targets::TargetLocator::SshDocker { .. } => MJ_CONTAINER_ENVIRONMENT.to_owned(),
        targets::TargetLocator::AwsEc2 { workspace, .. } => format!(
            "## Mjolnir disposable environment\n\nThis session runs on a disposable Mjolnir EC2 instance. When the session closes, Mjolnir checkpoints everything in project workspace directories under `$HOME/{workspace}`, including committed work, staged and unstaged changes, and untracked files. Mjolnir then terminates the instance.\n\nEverything outside `$HOME/{workspace}`, including installed packages, the rest of `$HOME`, and `/tmp`, is ephemeral and will be lost. Keep durable results in the workspace or push them to a remote.\n\nNew workspaces start on their own session branch from the default network fetch remote’s default branch. Local unpublished commits and uncommitted files are not copied. Use normal git push to publish the current branch to the configured network push destination. Closing saves a checkpoint; it does not publish commits or update the original local checkout. Resumed sessions restore their saved work.\n"
        ),
        targets::TargetLocator::LocalBare { .. } | targets::TargetLocator::SshBare { .. } => {
            return Ok(());
        }
    };
    let path = destination.join(harness.agent_instructions_file());
    let separator = match std::fs::read_to_string(&path) {
        Ok(contents) if !contents.is_empty() && !contents.ends_with('\n') => "\n\n",
        Ok(contents) if !contents.is_empty() => "\n",
        Ok(_) => "",
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => "",
        Err(error) => return Err(error.into()),
    };
    use std::io::Write;

    let mut file = std::fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(&path)
        .with_context(|| format!("open staged harness instructions {}", path.display()))?;
    file.write_all(separator.as_bytes())?;
    file.write_all(environment.as_bytes())?;
    Ok(())
}

pub(super) fn copy_profile_entry(source: &Path, destination: &Path) -> Result<()> {
    copy_profile_entry_within(source, destination, &HashSet::new(), &[])
}

/// [`copy_profile_entry`], leaving out the paths in `excluded`. Each is
/// compared with the path the walk reaches it by, below `source` as given,
/// not with where a link resolves.
pub(super) fn copy_profile_entry_except(
    source: &Path,
    destination: &Path,
    excluded: &[PathBuf],
) -> Result<()> {
    copy_profile_entry_within(source, destination, &HashSet::new(), excluded)
}

/// Copy one profile entry, following symlinks so a profile home that links its
/// settings or instructions elsewhere still stages their contents. `entered`
/// holds the canonical paths of the directories already entered on this branch
/// of the recursion, which stops a symlinked directory cycle.
pub(super) fn copy_profile_entry_within(
    source: &Path,
    destination: &Path,
    entered: &HashSet<PathBuf>,
    excluded: &[PathBuf],
) -> Result<()> {
    std::fs::symlink_metadata(source)
        .with_context(|| format!("read staged profile entry metadata {}", source.display()))?;
    let metadata = match std::fs::metadata(source) {
        Ok(metadata) => metadata,
        // The entry exists but its link target does not; staging the rest of
        // the profile is more useful than failing on a stale link.
        Err(error) if error.kind() == ErrorKind::NotFound => {
            tracing::warn!(
                source = %source.display(),
                "skipping staged profile entry whose symlink target is missing"
            );
            return Ok(());
        }
        Err(error) => {
            return Err(anyhow::Error::new(error).context(format!(
                "read staged profile entry metadata {}",
                source.display()
            )));
        }
    };
    if metadata.is_file() {
        if let Some(parent) = destination.parent() {
            std::fs::create_dir_all(parent)
                .with_context(|| format!("create staged profile directory {}", parent.display()))?;
        }
        std::fs::copy(source, destination).with_context(|| {
            format!(
                "copy staged profile file {} to {}",
                source.display(),
                destination.display()
            )
        })?;
        return Ok(());
    }
    if metadata.is_dir() {
        let canonical = std::fs::canonicalize(source)
            .with_context(|| format!("resolve staged profile directory {}", source.display()))?;
        if entered.contains(&canonical) {
            tracing::warn!(
                source = %source.display(),
                target = %canonical.display(),
                "skipping staged profile directory that links back into itself"
            );
            return Ok(());
        }
        let mut entered = entered.clone();
        entered.insert(canonical);
        std::fs::create_dir_all(destination).with_context(|| {
            format!("create staged profile directory {}", destination.display())
        })?;
        let entries = std::fs::read_dir(source)
            .with_context(|| format!("list staged profile directory {}", source.display()))?
            .collect::<std::io::Result<Vec<_>>>()
            .with_context(|| {
                format!(
                    "read staged profile directory entries in {}",
                    source.display()
                )
            })?;
        // Sibling entries in one directory are independent, so recurse in
        // parallel; this is the level most likely to hold many files (e.g. a
        // skills or plugins tree).
        entries
            .par_iter()
            .filter(|entry| !excluded.contains(&entry.path()))
            .try_for_each(|entry| {
                copy_profile_entry_within(
                    &entry.path(),
                    &destination.join(entry.file_name()),
                    &entered,
                    excluded,
                )
            })?;
        std::fs::set_permissions(destination, metadata.permissions()).with_context(|| {
            format!(
                "set permissions for staged profile directory {}",
                destination.display()
            )
        })?;
    }
    Ok(())
}

// Container copies can create root-owned files even when exec defaults to a
// non-root image user. The worker directory was created by that user, so use
// its ownership for uploaded files before restricting their permissions.
pub(in crate::controller) fn container_upload_ownership_args(
    container_id: &str,
    worker_root: &str,
    paths: &[&str],
) -> Vec<String> {
    let mut args = vec![
        "exec".into(),
        "--user".into(),
        "0".into(),
        container_id.into(),
        "sh".into(),
        "-c".into(),
        // GNU and BusyBox stat both support this numeric ownership format.
        r#"set -eu; owner=$(stat -c '%u:%g' -- "$1"); shift; chown -R "$owner" -- "$@""#.into(),
        "sh".into(),
        worker_root.into(),
    ];
    args.extend(paths.iter().map(|path| (*path).to_owned()));
    args
}

/// Shell that writes the host's mbx configuration into the container user's
/// own home, where the container's mbx reads it.
const MBX_CONFIG_SCRIPT: &str =
    r#"set -eu; mkdir -p "$HOME/.config/mbx"; cat > "$HOME/.config/mbx/config.toml""#;

/// Install `bin/mbx` and its `bin/cargo` shim beside the worker, and hand the
/// container the host's mbx configuration when the host has one.
///
/// `bin` is the directory the worker later writes its `gh` wrapper into and
/// prepends to `PATH`; it creates that directory without clearing it, so these
/// two files survive and the shim is found before the image's own Cargo.
pub(super) fn install_mbx_files(
    executor: &impl CommandExecutor,
    locator: &targets::TargetLocator,
    session_id: &str,
    worker_root: &str,
    binary: &Path,
    configuration: Option<&str>,
) -> Result<()> {
    let bin = format!("{worker_root}/bin");
    let mbx = format!("{bin}/mbx");
    let cargo = format!("{bin}/cargo");
    // A hard link keeps one copy of a 30 MB binary; a copy is the fallback for
    // images whose layer cannot link.
    let shim_script = format!(r#"ln -f "{mbx}" "{cargo}" 2>/dev/null || cp -f "{mbx}" "{cargo}""#);
    let (engine, container_id, ssh) = match locator {
        targets::TargetLocator::LocalPodman { container_id, .. } => ("podman", container_id, None),
        targets::TargetLocator::LocalDocker { container_id, .. } => ("docker", container_id, None),
        targets::TargetLocator::SshPodman {
            ssh, container_id, ..
        } => ("podman", container_id, Some(ssh)),
        targets::TargetLocator::SshDocker {
            ssh, container_id, ..
        } => ("docker", container_id, Some(ssh)),
        targets::TargetLocator::LocalBare { .. }
        | targets::TargetLocator::AppleContainer { .. }
        | targets::TargetLocator::AwsEc2 { .. }
        | targets::TargetLocator::SshBare { .. } => {
            bail!("the build cache is only installed into Podman and Docker containers")
        }
    };
    // Remote hosts keep the binary in a content-addressed cache so it crosses
    // the network once per unique mbx, exactly as the worker binary does.
    let source = match ssh {
        None => binary.to_string_lossy().into_owned(),
        Some(ssh) => {
            let digest = mj_core::worker_launch::worker_executable_digest(binary)?;
            let cache_dir = format!(".cache/mjolnir/mbx/{digest}");
            let cached = format!("{cache_dir}/mbx");
            let present = matches!(
                executor.execute(
                    &crate::targets::ssh_command(ssh, ["test", "-f", &cached])
                        .purpose("probe the cached remote mbx binary"),
                ),
                Ok(output) if output.status == 0
            );
            if !present {
                execute_checked(
                    executor,
                    crate::targets::ssh_command(ssh, ["mkdir", "-p", &cache_dir])
                        .purpose("create the remote mbx cache"),
                )?;
                let partial = format!("{cache_dir}/mbx.partial-{session_id}");
                execute_checked(
                    executor,
                    crate::targets::scp_upload(ssh, binary, &partial, false)
                        .purpose("upload the remote mbx binary"),
                )?;
                execute_checked(
                    executor,
                    crate::targets::ssh_command(ssh, ["mv", &partial, &cached])
                        .purpose("publish the cached remote mbx binary"),
                )?;
            }
            cached
        }
    };
    let steps: Vec<(Vec<String>, &str)> = vec![
        (
            vec![
                engine.into(),
                "exec".into(),
                container_id.clone(),
                "mkdir".into(),
                "-p".into(),
                bin.clone(),
            ],
            "create the session binary directory",
        ),
        (
            vec![
                engine.into(),
                "cp".into(),
                source,
                format!("{container_id}:{mbx}"),
            ],
            "upload the mbx build cache binary",
        ),
        (
            std::iter::once(engine.to_owned())
                .chain(container_upload_ownership_args(
                    container_id,
                    worker_root,
                    &[&bin],
                ))
                .collect(),
            "assign the mbx binary to the worker user",
        ),
        (
            vec![
                engine.into(),
                "exec".into(),
                container_id.clone(),
                "sh".into(),
                "-c".into(),
                shim_script,
            ],
            "install the mbx Cargo shim",
        ),
        (
            vec![
                engine.into(),
                "exec".into(),
                container_id.clone(),
                "chmod".into(),
                "755".into(),
                mbx.clone(),
                cargo.clone(),
            ],
            "make the mbx build cache executable",
        ),
    ];
    for (args, purpose) in steps {
        let command = match ssh {
            None => CommandSpec::new(args[0].clone(), args[1..].iter().cloned()),
            Some(ssh) => crate::targets::ssh_command(ssh, args),
        }
        .purpose(purpose)
        .stage(ProvisionStage::Syncing);
        execute_checked(executor, command)?;
    }
    if let Some(configuration) = configuration {
        let args = vec![
            engine.to_owned(),
            "exec".into(),
            "-i".into(),
            container_id.clone(),
            "sh".into(),
            "-c".into(),
            MBX_CONFIG_SCRIPT.to_owned(),
        ];
        let command = match ssh {
            None => CommandSpec::new(args[0].clone(), args[1..].iter().cloned()),
            Some(ssh) => crate::targets::ssh_command(ssh, args),
        }
        .purpose("install the host mbx configuration")
        .stage(ProvisionStage::Syncing)
        .with_sensitive_stdin(configuration.as_bytes().to_vec());
        execute_checked(executor, command)?;
    }
    Ok(())
}

#[allow(clippy::too_many_arguments)]
pub(super) fn install_worker_files(
    executor: &impl CommandExecutor,
    locator: &targets::TargetLocator,
    session_id: &str,
    worker_root: &str,
    profile_home: &str,
    worker_binary: &Path,
    launch_config: &Path,
    ownership: &Path,
    profile_stage: &Path,
) -> Result<()> {
    verify_worker_build(worker_binary)?;
    match locator {
        targets::TargetLocator::LocalBare { .. } => {
            if profile_stage.is_dir() {
                // A staged home that is a link to a profile home, left for a
                // session an earlier release started there, is replaced by a
                // directory of its own. Copying through it would write this
                // stage over the person's own configuration.
                if std::fs::symlink_metadata(profile_home)
                    .is_ok_and(|metadata| metadata.file_type().is_symlink())
                {
                    std::fs::remove_file(profile_home).with_context(|| {
                        format!("unlink the earlier session's profile home link {profile_home}")
                    })?;
                }
                std::fs::create_dir_all(profile_home).context("create isolated local profile")?;
                for entry in std::fs::read_dir(profile_stage)? {
                    let entry = entry?;
                    copy_profile_entry(
                        &entry.path(),
                        &Path::new(profile_home).join(entry.file_name()),
                    )?;
                }
            }
            for command in [
                CommandSpec::new("mkdir", ["-p", worker_root])
                    .purpose("create local bare worker directory"),
                CommandSpec::new(
                    "cp",
                    [
                        worker_binary.to_string_lossy().into_owned(),
                        format!("{worker_root}/hel"),
                    ],
                )
                .purpose("install local Mjolnir worker"),
                CommandSpec::new(
                    "cp",
                    [
                        launch_config.to_string_lossy().into_owned(),
                        format!("{worker_root}/launch.json"),
                    ],
                )
                .purpose("install local worker launch configuration"),
                CommandSpec::new(
                    "cp",
                    [
                        ownership.to_string_lossy().into_owned(),
                        format!("{worker_root}/ownership.json"),
                    ],
                )
                .purpose("install local worker ownership marker"),
                CommandSpec::new("chmod", ["700", &format!("{worker_root}/hel")])
                    .purpose("make local Mjolnir worker executable"),
            ] {
                execute_checked(executor, command)?;
            }
        }
        targets::TargetLocator::LocalPodman { container_id, .. }
        | targets::TargetLocator::LocalDocker { container_id, .. }
        | targets::TargetLocator::AppleContainer { container_id, .. } => {
            let engine = match locator {
                targets::TargetLocator::LocalPodman { .. } => "podman",
                targets::TargetLocator::LocalDocker { .. } => "docker",
                targets::TargetLocator::AppleContainer { .. } => "container",
                _ => unreachable!("matched local container target"),
            };
            for command in [
                CommandSpec::new(
                    engine,
                    [
                        "exec".into(),
                        container_id.clone(),
                        "mkdir".into(),
                        "-p".into(),
                        worker_root.into(),
                        profile_home.into(),
                    ],
                )
                .purpose("create target worker directories"),
                CommandSpec::new(
                    engine,
                    [
                        "cp".into(),
                        worker_binary.to_string_lossy().into_owned(),
                        format!("{container_id}:{worker_root}/hel"),
                    ],
                )
                .purpose("upload Mjolnir worker"),
                CommandSpec::new(
                    engine,
                    [
                        "cp".into(),
                        launch_config.to_string_lossy().into_owned(),
                        format!("{container_id}:{worker_root}/launch.json"),
                    ],
                )
                .purpose("upload worker launch configuration"),
                CommandSpec::new(
                    engine,
                    [
                        "cp".into(),
                        ownership.to_string_lossy().into_owned(),
                        format!("{container_id}:{worker_root}/ownership.json"),
                    ],
                )
                .purpose("upload worker ownership marker"),
                CommandSpec::new(
                    engine,
                    [
                        "cp".into(),
                        format!("{}/.", profile_stage.display()),
                        format!("{container_id}:{profile_home}"),
                    ],
                )
                .purpose("upload harness profile allowlist"),
                CommandSpec::new(
                    engine,
                    container_upload_ownership_args(
                        container_id,
                        worker_root,
                        &[
                            &format!("{worker_root}/hel"),
                            &format!("{worker_root}/launch.json"),
                            &format!("{worker_root}/ownership.json"),
                            profile_home,
                        ],
                    ),
                )
                .purpose("assign uploaded files to the worker user"),
                CommandSpec::new(
                    engine,
                    [
                        "exec".into(),
                        container_id.clone(),
                        "chmod".into(),
                        "700".into(),
                        format!("{worker_root}/hel"),
                    ],
                )
                .purpose("make Mjolnir worker executable"),
                CommandSpec::new(
                    engine,
                    [
                        "exec".into(),
                        container_id.clone(),
                        "chmod".into(),
                        "-R".into(),
                        "go-rwx".into(),
                        profile_home.into(),
                    ],
                )
                .purpose("restrict harness profile permissions"),
            ] {
                execute_checked(executor, command)?;
            }
        }
        // A disposable EC2 instance hosts one session and is terminated with
        // it, so a cache there would only hold a second copy of the worker.
        targets::TargetLocator::AwsEc2 { ssh, .. } => {
            install_worker_over_ssh(
                executor,
                ssh,
                None,
                worker_root,
                profile_home,
                worker_binary,
                launch_config,
                ownership,
                profile_stage,
            )?;
        }
        targets::TargetLocator::SshBare { ssh, .. } => {
            let cached_worker = cache_worker_on_ssh_host(executor, ssh, session_id, worker_binary)?;
            install_worker_over_ssh(
                executor,
                ssh,
                Some(&cached_worker),
                worker_root,
                profile_home,
                worker_binary,
                launch_config,
                ownership,
                profile_stage,
            )?;
        }
        targets::TargetLocator::SshPodman {
            ssh, container_id, ..
        }
        | targets::TargetLocator::SshDocker {
            ssh, container_id, ..
        } => {
            let engine = match locator {
                targets::TargetLocator::SshPodman { .. } => "podman",
                targets::TargetLocator::SshDocker { .. } => "docker",
                _ => unreachable!("matched remote container target"),
            };
            let cached_worker = cache_worker_on_ssh_host(executor, ssh, session_id, worker_binary)?;
            let upload = format!("{}/{session_id}", targets::REMOTE_UPLOAD_STAGING);
            execute_checked(
                executor,
                crate::targets::ssh_command(ssh, ["mkdir", "-p", &upload])
                    .purpose("create remote upload staging"),
            )?;
            for (source, name) in [
                (launch_config, "launch.json"),
                (ownership, "ownership.json"),
            ] {
                execute_checked(
                    executor,
                    crate::targets::scp_upload(ssh, source, &format!("{upload}/{name}"), false)
                        .purpose("upload remote container worker file"),
                )?;
            }
            execute_checked(
                executor,
                crate::targets::scp_upload(ssh, profile_stage, &format!("{upload}/profile"), true)
                    .purpose("upload remote container profile allowlist"),
            )?;
            let remote = [
                vec![
                    engine.into(),
                    "exec".into(),
                    container_id.clone(),
                    "mkdir".into(),
                    "-p".into(),
                    worker_root.into(),
                    profile_home.into(),
                ],
                vec![
                    engine.into(),
                    "cp".into(),
                    cached_worker.clone(),
                    format!("{container_id}:{worker_root}/hel"),
                ],
                vec![
                    engine.into(),
                    "cp".into(),
                    format!("{upload}/launch.json"),
                    format!("{container_id}:{worker_root}/launch.json"),
                ],
                vec![
                    engine.into(),
                    "cp".into(),
                    format!("{upload}/ownership.json"),
                    format!("{container_id}:{worker_root}/ownership.json"),
                ],
                vec![
                    engine.into(),
                    "cp".into(),
                    format!("{upload}/profile/."),
                    format!("{container_id}:{profile_home}"),
                ],
                std::iter::once(engine.to_owned())
                    .chain(container_upload_ownership_args(
                        container_id,
                        worker_root,
                        &[
                            &format!("{worker_root}/hel"),
                            &format!("{worker_root}/launch.json"),
                            &format!("{worker_root}/ownership.json"),
                            profile_home,
                        ],
                    ))
                    .collect(),
                vec![
                    engine.into(),
                    "exec".into(),
                    container_id.clone(),
                    "chmod".into(),
                    "700".into(),
                    format!("{worker_root}/hel"),
                ],
                vec![
                    engine.into(),
                    "exec".into(),
                    container_id.clone(),
                    "chmod".into(),
                    "-R".into(),
                    "go-rwx".into(),
                    profile_home.into(),
                ],
                vec!["rm".into(), "-rf".into(), "--".into(), upload.clone()],
            ];
            for args in remote {
                execute_checked(
                    executor,
                    crate::targets::ssh_command(ssh, args)
                        .purpose("install remote container worker"),
                )?;
            }
        }
    }
    Ok(())
}

/// Put the worker binary in the SSH host's content-addressed cache and answer
/// with its path there. The binary crosses the network only when the host
/// does not hold this build yet.
///
/// A worker binary is well over 100 MB and identical across sessions. Uploading
/// it once per session made each create on a slow link hold a daemon action
/// slot for minutes (R3-4). Every session on the host, SSH-bare or in a remote
/// container, copies its own worker from here instead.
///
/// Two sessions that find the cache empty at the same time each upload to
/// their own partial name and rename it into place, so the cached path only
/// ever names a complete file.
fn cache_worker_on_ssh_host(
    executor: &impl CommandExecutor,
    ssh: &SshTarget,
    session_id: &str,
    worker_binary: &Path,
) -> Result<String> {
    let digest = mj_core::worker_launch::worker_executable_digest(worker_binary)?;
    // Home-relative, not "~/": targets::ssh_command single-quotes every
    // argument, so a tilde would stay literal in the remote shell while scp
    // expands it, and the two sides would disagree. Both ssh commands (cwd is
    // the login home) and scp resolve a relative path against the remote home.
    let cache_dir = format!(".cache/mjolnir/workers/{digest}");
    let cached_worker = format!("{cache_dir}/hel");
    let cached = matches!(
        executor.execute(
            &crate::targets::ssh_command(ssh, ["test", "-f", &cached_worker])
                .purpose("probe cached remote Mjolnir worker"),
        ),
        Ok(output) if output.status == 0
    );
    if cached {
        return Ok(cached_worker);
    }
    execute_checked(
        executor,
        crate::targets::ssh_command(ssh, ["mkdir", "-p", &cache_dir])
            .purpose("create remote worker cache"),
    )?;
    let partial = format!("{cache_dir}/hel.partial-{session_id}");
    if let Err(error) = execute_checked(
        executor,
        crate::targets::scp_upload(ssh, worker_binary, &partial, false)
            .purpose("upload Mjolnir worker to the remote cache"),
    ) {
        // The partial file is outside every session's worker root, so no
        // session cleanup would ever remove it. Best effort: a connection
        // that failed the upload may fail this too.
        let _ = executor.execute(
            &crate::targets::ssh_command(ssh, ["rm", "-f", "--", &partial])
                .purpose("remove partial remote worker upload"),
        );
        return Err(error);
    }
    execute_checked(
        executor,
        crate::targets::ssh_command(ssh, ["mv", &partial, &cached_worker])
            .purpose("publish cached remote Mjolnir worker"),
    )?;
    Ok(cached_worker)
}

/// Install a worker on a host reached over SSH with no container.
///
/// `cached_worker` is the host's cached copy of the worker binary, from
/// [`cache_worker_on_ssh_host`]; the session gets its own copy of it, so its
/// worker root and cleanup are the same as with an upload. Without one, the
/// local binary is uploaded straight into the worker root.
#[allow(clippy::too_many_arguments)]
pub(super) fn install_worker_over_ssh(
    executor: &impl CommandExecutor,
    ssh: &SshTarget,
    cached_worker: Option<&str>,
    worker_root: &str,
    profile_home: &str,
    worker_binary: &Path,
    launch_config: &Path,
    ownership: &Path,
    profile_stage: &Path,
) -> Result<()> {
    execute_checked(
        executor,
        crate::targets::ssh_command(ssh, ["mkdir", "-p", worker_root, profile_home])
            .purpose("create SSH worker directories"),
    )?;
    let worker = format!("{worker_root}/hel");
    let install_worker = match cached_worker {
        Some(cached_worker) => crate::targets::ssh_command(ssh, ["cp", cached_worker, &worker])
            .purpose("copy cached Mjolnir worker into the session"),
        None => crate::targets::scp_upload(ssh, worker_binary, &worker, false)
            .purpose("upload SSH worker file"),
    };
    execute_checked(executor, install_worker)?;
    for (source, remote) in [
        (launch_config, format!("{worker_root}/launch.json")),
        (ownership, format!("{worker_root}/ownership.json")),
    ] {
        execute_checked(
            executor,
            crate::targets::scp_upload(ssh, source, &remote, false)
                .purpose("upload SSH worker file"),
        )?;
    }
    let incoming_profile = format!("{profile_home}.incoming");
    execute_checked(
        executor,
        crate::targets::scp_upload(ssh, profile_stage, &incoming_profile, true)
            .purpose("upload SSH harness profile allowlist"),
    )?;
    execute_checked(
        executor,
        crate::targets::ssh_command(
            ssh,
            ["cp", "-R", &format!("{incoming_profile}/."), profile_home],
        )
        .purpose("install SSH harness profile allowlist"),
    )?;
    execute_checked(
        executor,
        crate::targets::ssh_command(ssh, ["rm", "-rf", "--", &incoming_profile])
            .purpose("remove SSH profile staging"),
    )?;
    execute_checked(
        executor,
        crate::targets::ssh_command(ssh, ["chmod", "700", &format!("{worker_root}/hel")])
            .purpose("make SSH worker executable"),
    )?;
    execute_checked(
        executor,
        crate::targets::ssh_command(ssh, ["chmod", "-R", "go-rwx", profile_home])
            .purpose("restrict SSH harness profile permissions"),
    )?;
    Ok(())
}