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

fn fake_update_executable(path: &Path) -> PathBuf {
    std::fs::create_dir_all(path.parent().unwrap()).expect("executable directory");
    std::fs::write(path, b"test executable").expect("executable file");
    path.to_path_buf()
}

fn update_command_text(command: &Command) -> String {
    std::iter::once(command.get_program().to_string_lossy().into_owned())
        .chain(
            command
                .get_args()
                .map(|argument| argument.to_string_lossy().into_owned()),
        )
        .collect::<Vec<_>>()
        .join(" ")
}

#[test]
fn golden_install_method_detection_uses_cargo_and_package_manager_formats() {
    let root = tempfile::tempdir().expect("install roots");
    let version = env!("CARGO_PKG_VERSION");
    let source =
        format!("brokk-mjolnir {version} (registry+https://github.com/rust-lang/crates.io-index)");
    let voice_source = format!(
        "brokk-mj-voice-worker {version} (registry+https://github.com/rust-lang/crates.io-index)"
    );

    // Cargo's documented v1 install record shape, as written to .crates2.json.
    let cargo_json = root.path().join("cargo-json");
    std::fs::create_dir_all(cargo_json.join("bin")).expect("Cargo JSON bin directory");
    std::fs::write(
        cargo_json.join(".crates2.json"),
        serde_json::json!({
            "v1": 1,
            "installs": {
                (source.clone()): {"version": version, "bins": ["mj"], "features": []},
                (voice_source.clone()): {"version": version, "bins": ["mj-voice-worker"], "features": []}
            }
        })
        .to_string(),
    )
    .expect("Cargo JSON install record");
    let cargo_json_exe = fake_update_executable(&cargo_json.join("bin/mj"));

    // Cargo's compatibility v1 TOML install record, including a stale version
    // that must not be mistaken for the executable at the current version.
    let cargo_toml = root.path().join("cargo-toml");
    std::fs::create_dir_all(cargo_toml.join("bin")).expect("Cargo TOML bin directory");
    std::fs::write(
        cargo_toml.join(".crates.toml"),
        format!(
            "[v1]\n\"{source}\" = [\"mj\"]\n\"{voice_source}\" = [\"mj-voice-worker\"]\n\"brokk-mjolnir 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)\" = [\"mj\"]\n"
        ),
    )
    .expect("Cargo TOML install record");
    let cargo_toml_exe = fake_update_executable(&cargo_toml.join("bin/mj"));

    let homebrew_exe =
        fake_update_executable(&root.path().join("homebrew/Cellar/mjolnir/2.4.0/bin/mj"));
    let npm_exe =
        fake_update_executable(&root.path().join("npm/node_modules/@brokkai/mjolnir/bin/mj"));
    let direct_exe = fake_update_executable(&root.path().join("direct/mj"));

    let variables = |entries: &[(&str, &str)]| {
        entries
            .iter()
            .map(|(name, value)| ((*name).to_owned(), OsString::from(value)))
            .collect::<std::collections::HashMap<_, _>>()
    };
    let cases = [
        (
            "npx marker takes precedence",
            direct_exe.clone(),
            variables(&[
                (NPX_MANAGED_ENV, "1"),
                (NPM_MANAGED_ENV, "1"),
                (HOMEBREW_MANAGED_ENV, "1"),
            ]),
        ),
        (
            "npm marker",
            direct_exe.clone(),
            variables(&[(NPM_MANAGED_ENV, "1")]),
        ),
        (
            "Homebrew marker",
            direct_exe.clone(),
            variables(&[(HOMEBREW_MANAGED_ENV, "1")]),
        ),
        ("Homebrew Cellar layout", homebrew_exe, variables(&[])),
        ("npm bundle layout", npm_exe, variables(&[])),
        ("Cargo .crates2.json", cargo_json_exe, variables(&[])),
        ("Cargo .crates.toml", cargo_toml_exe, variables(&[])),
        ("unrecorded executable", direct_exe, variables(&[])),
    ];

    let mut rendered = String::new();
    for (label, executable, environment) in cases {
        let method =
            InstallMethod::detect(|name| environment.get(name).cloned(), Some(&executable));
        rendered.push_str(&format!(
            "=== {label} ===\nmethod: {method:?}\nupgrade: {}\n\n",
            method
                .update_command()
                .unwrap_or_else(|| "(none)".to_owned())
        ));
    }
    rendered.push_str("=== package-manager commands ===\n");
    rendered.push_str(&format!(
        "npm: {}\n",
        update_command_text(&npm_upgrade_command())
    ));
    rendered.push_str(&format!(
        "brew update: {}\n",
        update_command_text(&brew_update_command())
    ));
    rendered.push_str(&format!(
        "brew upgrade: {}",
        update_command_text(&brew_upgrade_command())
    ));
    mj_core::golden::assert_golden(
        env!("CARGO_MANIFEST_DIR"),
        "install-method-detection",
        &rendered,
    );
}

#[test]
fn cargo_index_uses_latest_non_yanked_version() {
    let index = concat!(
        r#"{"vers":"2.4.0","yanked":false}"#,
        "\n",
        r#"{"vers":"2.5.0","yanked":true}"#,
        "\n",
        r#"{"vers":"2.4.2","yanked":false}"#,
        "\n",
    );
    assert_eq!(
        parse_cargo_index_version(index).expect("version"),
        Version::parse("2.4.2").expect("semver")
    );
}

fn asset(name: &str) -> ReleaseAsset {
    ReleaseAsset {
        name: name.to_string(),
        browser_download_url: format!("https://example.com/{name}"),
    }
}

fn linux_x64() -> Platform {
    Platform {
        os_family: "linux",
        arch: "x86_64",
        rust_target: "x86_64-unknown-linux-gnu".to_string(),
    }
}

#[test]
fn release_newer_than_current_requires_checksum_asset() {
    let release = GitHubRelease {
        tag_name: "v2.5.0".to_string(),
        assets: vec![asset(
            "brokk-mjolnir-v2.5.0-x86_64-unknown-linux-gnu.tar.gz",
        )],
    };

    let error = update_info_from_release(
        &release,
        &Version::parse("2.4.0").expect("version"),
        &linux_x64(),
    )
    .expect_err("missing checksum should fail");

    assert!(error
        .to_string()
        .contains("missing required checksum asset brokk-mjolnir-v2.5.0-x86_64-unknown-linux-gnu.tar.gz.sha256"));
}

#[test]
fn prompt_eof_declines_but_enter_accepts() {
    assert!(!read_update_answer(&mut Cursor::new(b"")).expect("read EOF"));
    assert!(read_update_answer(&mut Cursor::new(b"\n")).expect("read Enter"));
    assert!(read_update_answer(&mut Cursor::new(b"yes\n")).expect("read yes"));
    assert!(!read_update_answer(&mut Cursor::new(b"n\n")).expect("read no"));
}

#[test]
fn golden_mj_update_output() {
    let version = Version::parse("2.5.0").expect("version");
    let mut output = String::new();
    for (label, method) in [
        ("Homebrew", InstallMethod::Homebrew),
        (
            "Cargo",
            InstallMethod::Cargo {
                voice_worker: false,
            },
        ),
    ] {
        let notice = managed_update_notice(&version, &method, "2.4.0").expect("update notice");
        output.push_str(&format!(
            "=== {label} update notice (stdout) ===\n{notice}\n\n"
        ));
    }
    output.pop();
    mj_core::golden::assert_golden(env!("CARGO_MANIFEST_DIR"), "mj-update-output", &output);
}

#[test]
fn managed_restart_retargets_homebrew_to_its_wrapper() {
    // npm must save its installation path before the old bundle is removed.
    // Homebrew must re-resolve the wrapper or the
    // restart would relaunch the old Cellar version.
    let exe = Path::new("/opt/homebrew/Cellar/mjolnir/2.4.0/libexec/mj");
    assert_eq!(
        managed_restart_target(&InstallMethod::Npm, exe).expect("target"),
        RestartTarget::SameExe(exe.to_path_buf())
    );
    assert_eq!(
        managed_restart_target(&InstallMethod::Homebrew, exe).expect("target"),
        RestartTarget::Wrapper
    );
    assert!(managed_restart_target(&InstallMethod::Npx, exe).is_err());
}

#[cfg(target_os = "linux")]
#[test]
fn npm_upgrade_restarts_after_the_running_package_is_removed() {
    const FIXTURE_ENV: &str = "MJ_UPDATE_RESTART_FIXTURE";
    if std::env::var_os(FIXTURE_ENV).is_some() {
        let target = run_managed_upgrade(
            &Version::parse("9.9.9").expect("version"),
            &InstallMethod::Npm,
        )
        .expect("fake npm upgrade");
        restart_current_process(target).expect("restart updated binary");
        unreachable!("exec does not return on success");
    }

    // The fixture lives beside the test binary so the package entry below can
    // be a hard link to it. Copying the binary and then exec'ing the copy is
    // the ETXTBSY race described on `install_fake_command`: the copy is long,
    // and any other test thread that forks during it inherits the open write
    // descriptor and keeps the new file busy. A hard link opens nothing for
    // writing, and because the kernel records the path used by `execve`, the
    // child still sees `current_exe()` inside the package, which is what the
    // npm restart target is resolved from.
    let binary = std::env::current_exe().expect("test binary");
    let root = tempfile::tempdir_in(binary.parent().expect("test binary directory"))
        .expect("fixture directory");
    let package_bin = root.path().join("package/bin");
    let manager_bin = root.path().join("manager");
    std::fs::create_dir_all(&package_bin).expect("package bin");
    std::fs::create_dir(&manager_bin).expect("manager bin");
    let executable = package_bin.join("mj");
    std::fs::hard_link(&binary, &executable).expect("link the test binary into the package");
    let replacement = root.path().join("replacement");
    std::fs::write(&replacement, "#!/bin/sh\necho UPDATED_MJ_RESTARTED\n")
        .expect("replacement script");
    // `npm` and `replacement` are only ever read: the fake npm script is run
    // through the shared dispatcher, and `replacement` is copied by that
    // script before anything execs the copy.
    mj_core::test_hooks::install_fake_command(
        &manager_bin,
        "npm",
        r#"#!/bin/sh
set -eu
mv "$MJ_UPDATE_RESTART_FIXTURE/package" "$MJ_UPDATE_RESTART_FIXTURE/retired"
mkdir -p "$MJ_UPDATE_RESTART_FIXTURE/package/bin"
cp "$MJ_UPDATE_RESTART_FIXTURE/replacement" "$MJ_UPDATE_RESTART_FIXTURE/package/bin/mj"
rm "$MJ_UPDATE_RESTART_FIXTURE/retired/bin/mj"
"#,
    );
    use std::os::unix::fs::PermissionsExt;
    std::fs::set_permissions(&replacement, std::fs::Permissions::from_mode(0o755))
        .expect("executable script");
    let mut paths = vec![manager_bin];
    paths.extend(std::env::split_paths(
        &std::env::var_os("PATH").unwrap_or_default(),
    ));
    let mut child = Command::new(executable);
    child.args([
        "--exact",
        "controller::update::tests::npm_upgrade_restarts_after_the_running_package_is_removed",
        "--nocapture",
    ]);
    child.env(FIXTURE_ENV, root.path());
    child.env("PATH", std::env::join_paths(paths).expect("fixture PATH"));
    let output =
        mj_core::subprocess::run_with_input(&mut child, b"").expect("run copied test executable");
    assert!(
        output.status.success(),
        "{}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(String::from_utf8_lossy(&output.stdout).contains("UPDATED_MJ_RESTARTED"));
}

#[cfg(unix)]
fn release_tar(entries: &[(&str, &[u8])]) -> Vec<u8> {
    let gz = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::fast());
    let mut archive = tar::Builder::new(gz);
    for (name, bytes) in entries {
        let mut header = tar::Header::new_gnu();
        header.set_size(bytes.len() as u64);
        header.set_mode(0o755);
        header.set_cksum();
        archive
            .append_data(&mut header, name, *bytes)
            .expect("tar entry");
    }
    archive
        .into_inner()
        .expect("tar archive")
        .finish()
        .expect("gzip archive")
}

#[cfg(unix)]
#[test]
fn release_upgrade_replaces_every_packaged_binary_and_preserves_unrelated_files() {
    let root = tempfile::tempdir().expect("install directory");
    let executable = root.path().join("custom-mj-name");
    std::fs::write(&executable, b"old controller").expect("old controller");
    std::fs::write(root.path().join("README.md"), b"user notes").expect("unrelated file");
    let new_binary = vec![b'n'; 128 * 1024];
    let entries: Vec<(&str, &[u8])> = [
        "release/mj",
        "release/mj-desktop",
        "release/mj-voice-worker",
        "release/mj-worker",
        "release/mj-worker-x86_64-unknown-linux-musl",
        "release/mj-worker-aarch64-unknown-linux-musl",
    ]
    .into_iter()
    .map(|name| (name, new_binary.as_slice()))
    .collect();
    for (name, _) in &entries[1..] {
        std::fs::write(
            root.path().join(Path::new(name).file_name().unwrap()),
            b"old helper",
        )
        .expect("old helper");
    }
    let mut archive_entries = entries.clone();
    archive_entries.push(("release/README.md", b"release notes"));
    let archive = release_tar(&archive_entries);
    let installed = install_release_archive(&executable, "release.tar.gz", &archive)
        .expect("install complete release");
    assert_eq!(
        installed,
        executable.canonicalize().expect("resolved controller")
    );
    assert_eq!(std::fs::read(&executable).unwrap(), new_binary);
    use std::os::unix::fs::PermissionsExt;
    for (name, _) in &entries[1..] {
        let helper = root.path().join(Path::new(name).file_name().unwrap());
        assert_eq!(std::fs::read(&helper).unwrap(), new_binary);
        assert_eq!(
            std::fs::metadata(&helper).unwrap().permissions().mode() & 0o777,
            0o755
        );
    }
    assert_eq!(
        std::fs::read(root.path().join("README.md")).unwrap(),
        b"user notes"
    );
    assert_eq!(
        std::fs::read_dir(root.path()).unwrap().count(),
        entries.len() + 1
    );
}

#[cfg(unix)]
#[test]
fn malformed_release_leaves_installed_binaries_unchanged() {
    let root = tempfile::tempdir().expect("install directory");
    let executable = root.path().join("mj");
    let worker = root.path().join("mj-worker");
    for archive in [
        release_tar(&[("mj", b"new controller"), ("mj-worker", b"")]),
        release_tar(&[("mj", b"new controller"), ("mj", b"duplicate")]),
        release_tar(&[("mj-worker", b"new worker")]),
    ] {
        std::fs::write(&executable, b"old controller").unwrap();
        std::fs::write(&worker, b"old worker").unwrap();
        assert!(install_release_archive(&executable, "release.tar.gz", &archive).is_err());
        assert_eq!(std::fs::read(&executable).unwrap(), b"old controller");
        assert_eq!(std::fs::read(&worker).unwrap(), b"old worker");
        assert_eq!(std::fs::read_dir(root.path()).unwrap().count(), 2);
    }
}

#[cfg(unix)]
#[test]
fn release_upgrade_allows_retired_companions() {
    let root = tempfile::tempdir().expect("install directory");
    let executable = root.path().join("mj");
    std::fs::write(&executable, b"old controller").unwrap();
    let archive = release_tar(&[("mj", b"new controller")]);
    install_release_archive(&executable, "release.tar.gz", &archive).expect("main-only release");
    assert_eq!(std::fs::read(&executable).unwrap(), b"new controller");
}

#[test]
fn zip_release_stages_application_binaries_without_extracting_documents() {
    let mut archive = zip::ZipWriter::new(Cursor::new(Vec::new()));
    let options = zip::write::SimpleFileOptions::default();
    for name in [
        "release/mj.exe",
        "release/mj-worker.exe",
        "release/mj-desktop.exe",
        "release/LICENSE",
    ] {
        archive.start_file(name, options).expect("zip entry");
        archive.write_all(b"binary contents").expect("zip contents");
    }
    let bytes = archive.finish().expect("zip archive").into_inner();
    let root = tempfile::tempdir().expect("staging directory");
    let binaries = stage_release_archive("release.zip", &bytes, root.path()).expect("stage zip");
    assert_eq!(binaries.len(), 3);
    for name in ["mj.exe", "mj-worker.exe", "mj-desktop.exe"] {
        assert_eq!(
            std::fs::read(root.path().join(name)).unwrap(),
            b"binary contents"
        );
    }
    assert!(!root.path().join("LICENSE").exists());
}

/// Serves canned bodies per path prefix from a loopback port and returns
/// sources pointed at it, so channel fetches never leave the machine.
async fn serve_update_sources(
    routes: Vec<(&'static str, &'static str)>,
) -> (UpdateSources, std::net::SocketAddr) {
    use tokio::io::{AsyncReadExt, AsyncWriteExt};

    let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
        .await
        .expect("bind loopback listener");
    let addr = listener.local_addr().expect("listener address");
    tokio::spawn(async move {
        while let Ok((mut socket, _)) = listener.accept().await {
            let mut buffer = vec![0u8; 4096];
            let read = socket.read(&mut buffer).await.unwrap_or(0);
            let request = String::from_utf8_lossy(&buffer[..read]).to_string();
            let path = request.split_whitespace().nth(1).unwrap_or_default();
            let matched = routes
                .iter()
                .find(|(route, _)| path.starts_with(route))
                .map(|(_, body)| *body);
            let (status, body) = match matched {
                Some(body) => ("200 OK", body),
                None => ("404 Not Found", ""),
            };
            let response = format!(
                "HTTP/1.1 {status}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
                body.len()
            );
            let _ = socket.write_all(response.as_bytes()).await;
            let _ = socket.shutdown().await;
        }
    });

    let base = format!("http://{addr}");
    (
        UpdateSources {
            latest_release: format!("{base}/release"),
            npm_latest: format!("{base}/npm"),
            homebrew_formula: format!("{base}/formula"),
            cargo_index: format!("{base}/index"),
        },
        addr,
    )
}

#[tokio::test]
async fn npm_registry_update_is_offered_when_newer() {
    let (sources, _) = serve_update_sources(vec![("/npm", r#"{"version":"9.9.9"}"#)]).await;

    let update = latest_update(&sources, &InstallMethod::Npm)
        .await
        .expect("update check");

    assert_eq!(
        update,
        Some(AvailableUpdate::Managed {
            version: Version::parse("9.9.9").expect("version"),
            method: InstallMethod::Npm,
        })
    );
}

#[tokio::test]
async fn up_to_date_channel_offers_nothing() {
    let (sources, _) = serve_update_sources(vec![("/npm", r#"{"version":"0.0.1"}"#)]).await;

    let update = latest_update(&sources, &InstallMethod::Npm)
        .await
        .expect("update check");

    assert_eq!(update, None);
}

#[tokio::test]
async fn homebrew_formula_update_is_offered_when_newer() {
    let formula = "class Mjolnir < Formula\n  version \"9.9.9\"\nend\n";
    let (sources, _) = serve_update_sources(vec![("/formula", formula)]).await;

    let update = latest_update(&sources, &InstallMethod::Homebrew)
        .await
        .expect("update check");

    assert_eq!(
        update,
        Some(AvailableUpdate::Managed {
            version: Version::parse("9.9.9").expect("version"),
            method: InstallMethod::Homebrew,
        })
    );
}

#[tokio::test]
async fn failed_channel_fetch_is_reported_as_an_error() {
    // No route matches /npm, so the stub answers 404.
    let (sources, _) = serve_update_sources(vec![("/formula", "unused")]).await;

    let error = latest_update(&sources, &InstallMethod::Npm)
        .await
        .expect_err("404 should fail the check");

    assert!(format!("{error:#}").contains("404"));
}

#[tokio::test]
async fn direct_installs_read_the_release_endpoint() {
    if std::env::consts::OS != "linux" || std::env::consts::ARCH != "x86_64" {
        return;
    }

    let release = concat!(
        r#"{"tag_name":"v9.9.9","assets":["#,
        r#"{"name":"brokk-mjolnir-v9.9.9-x86_64-unknown-linux-gnu.tar.gz","#,
        r#""browser_download_url":"https://example.com/mj.tar.gz"},"#,
        r#"{"name":"brokk-mjolnir-v9.9.9-x86_64-unknown-linux-gnu.tar.gz.sha256","#,
        r#""browser_download_url":"https://example.com/mj.tar.gz.sha256"}]}"#,
    );
    let (sources, _) = serve_update_sources(vec![("/release", release)]).await;

    let update = latest_update(&sources, &InstallMethod::Direct)
        .await
        .expect("release response")
        .expect("newer release");

    match update {
        AvailableUpdate::Direct(info) => {
            assert_eq!(info.version, Version::parse("9.9.9").expect("version"));
            assert_eq!(info.tag, "v9.9.9");
            assert_eq!(
                info.asset.name,
                "brokk-mjolnir-v9.9.9-x86_64-unknown-linux-gnu.tar.gz"
            );
            assert_eq!(
                info.checksum_asset.name,
                "brokk-mjolnir-v9.9.9-x86_64-unknown-linux-gnu.tar.gz.sha256"
            );
        }
        other => panic!("expected a direct update, got {other:?}"),
    }
}