borg-ai 0.7.2

Collective mind for coding-agent drones: CLI and harness hooks
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
//! Whether a newer borg is released, and `borg update [--check] [--quiet] [--json]`, which installs it.
//!
//! **The check** (`check`) asks GitHub for the latest release (`curl`, as vectors.rs fetches its model:
//! the hook binary carries no HTTP stack) and writes `$BORG_HOME/update.json` (the root store's, see
//! `path`): `{checked_ms, latest, error}`. The prompt hook starts it detached, at most once a day and never
//! twice within `CHECKING_MS` (`maybe_check`); the hook itself never waits on the network.
//! `BORG_UPDATE_CHECK=off` turns both off (tests run with it); `BORG_RELEASES_URL` names another release
//! feed (a `file://` fixture in the tests).
//!
//! **The notice** (`notice`): at a prompt, the drone hears once per session per state that a newer
//! release exists, that nothing syncs with a member on a newer mesh protocol (protocol.rs
//! `mismatches`), or that a member runs a newer borg the release feed has too. It tells the engineer
//! and offers `borg update`; the drone runs it on their yes. Nobody configures anything.
//!
//! **The install** (`main`) follows where this borg lives (install.rs `bin`): under `$CARGO_HOME/bin`
//! it runs `cargo install` for the three crates; elsewhere, on a target scripts/install.sh serves, it
//! downloads the release tarball beside the binaries, checks its sha256 and renames each binary into
//! place; anything else (a `target/` build, another OS) gets the commands printed and nothing changed.
//! Then the new borg refreshes the harnesses already wired (`borg install <h> --apply`) and runs
//! `borg doctor`.

use std::io::Write;
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};

use crate::{clock, collective, learn, protocol};

/// This borg's version.
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

/// Protocol: GitHub's latest-release endpoint for borg (scripts/install.sh reads the same), unless
/// `BORG_RELEASES_URL` says otherwise.
pub const RELEASES_URL: &str = "https://api.github.com/repos/debe/borg_ai/releases/latest";

/// Protocol: where release tarballs are, as scripts/install.sh names them, unless `BORG_DOWNLOAD_URL`
/// (install.sh's mirror variable) says otherwise: `<base>/v<X>/borg-v<X>-<target>.tar.gz` and `.sha256`.
pub const DOWNLOAD_URL: &str = "https://github.com/debe/borg_ai/releases/download";

/// Guard: the longest version string borg reads (a release tag, a member's hello): longer than any
/// release name, short enough that no text rides along.
pub const VERSION_CHARS: usize = 64;

/// Guard: the release check's whole request, in seconds (curl `--max-time`): a hung connection never
/// keeps the background check alive.
const CHECK_SECS: &str = "10";

/// Guard: one download of a release file, in seconds (curl `--max-time`): a tarball of three binaries
/// on a slow link, never an install that hangs.
const DOWNLOAD_SECS: &str = "600";

/// Baseline: how old `update.json` may be before a prompt starts another check (`clock::DAY_MS`): a
/// release a day late is news enough, and an idle prompt reads one small file.
pub const STALE_MS: i64 = clock::DAY_MS;

/// Guard: how long a started check (`update.checking`) keeps other prompts from starting one: at most
/// one curl per machine per 10 minutes, even when every check fails.
pub const CHECKING_MS: i64 = 10 * 60 * 1000;

/// Baseline: how long the check may keep failing before `borg doctor` warns (3 days): one offline day
/// or a GitHub hiccup is no news, a check that never works is.
pub const FAILING_WARN_MS: i64 = 3 * clock::DAY_MS;

/// Protocol: `borg update --check`'s exit code when a newer release exists (0: current, 1: the check
/// failed).
pub const EXIT_NEWER: i32 = 10;

/// What the last release check found (`update.json`).
#[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq)]
pub struct Checked {
    /// When it ran.
    pub checked_ms: i64,
    /// The latest release's version (no `v`); after a failed check, the one an earlier check found.
    pub latest: Option<String>,
    /// Why it failed; none when it worked.
    pub error: Option<String>,
}

/// `update.json` as written: `Checked`, and since when the check fails (none while it works), which
/// `borg doctor` reads (`FAILING_WARN_MS`).
#[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq)]
struct Record {
    #[serde(flatten)]
    checked: Checked,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    failing_since_ms: Option<i64>,
}

/// Whether the release check runs (`BORG_UPDATE_CHECK=off` turns it off).
pub fn on() -> bool {
    std::env::var("BORG_UPDATE_CHECK").as_deref() != Ok("off")
}

/// `$BORG_HOME/update.json`, in the root store when BORG_HOME is a collective's (collective.rs
/// `root_of`): one check per machine, whichever collective's hook ran it.
pub fn path() -> PathBuf {
    path_for(&learn::home())
}

/// `path` for the store `home` (one of a collective's or the root): `borg web` reads it for any store.
pub fn path_for(home: &Path) -> PathBuf {
    collective::root_of(home).join("update.json")
}

/// The marker of a started check, beside `update.json` (`CHECKING_MS`).
fn checking_path(update: &Path) -> PathBuf {
    update.with_file_name("update.checking")
}

/// The last check's result; none before the first.
pub fn checked() -> Option<Checked> {
    checked_at(&learn::home())
}

/// `checked` for the store `home` (`path_for`).
pub fn checked_at(home: &Path) -> Option<Checked> {
    read(&path_for(home)).map(|r| r.checked)
}

fn read(p: &Path) -> Option<Record> {
    std::fs::read_to_string(p).ok().and_then(|t| serde_json::from_str(&t).ok())
}

/// Whether version `a` is newer than `b`: numeric `x.y.z` (a missing part is 0, a leading `v` is
/// ignored), a pre-release (`-rc.1`) below its release, pre-releases by their dot-separated parts
/// (numbers numerically, below words). Build metadata (`+…`) does not count. False when either does
/// not read as a version.
pub fn newer(a: &str, b: &str) -> bool {
    matches!((parse(a), parse(b)), (Some(a), Some(b)) if a > b)
}

/// A version's order key: the release numbers, then whether it is a release (above its pre-releases),
/// then the pre-release parts.
type Key = (Vec<u64>, bool, Vec<(bool, u64, String)>);

fn parse(v: &str) -> Option<Key> {
    let v = v.trim().trim_start_matches('v');
    let v = v.split_once('+').map_or(v, |(v, _)| v);
    let (core, pre) = match v.split_once('-') {
        Some((c, p)) => (c, Some(p)),
        None => (v, None),
    };
    let mut nums: Vec<u64> = core.split('.').map(|n| n.parse().ok()).collect::<Option<_>>()?;
    if nums.is_empty() || nums.len() > 3 {
        return None;
    }
    nums.resize(3, 0);
    // A numeric identifier ranks below a word (semver); `(false, n, "")` sorts before `(true, 0, w)`.
    let pre: Vec<(bool, u64, String)> =
        pre.into_iter().flat_map(|p| p.split('.')).map(|p| p.parse::<u64>().map_or_else(|_| (true, 0, p.to_string()), |n| (false, n, String::new()))).collect();
    Some((nums, pre.is_empty(), pre))
}

/// Whether `v` is a version as borg writes one: it reads as one (`parse`), holds only version
/// characters (`[A-Za-z0-9.+-]`) and at most `VERSION_CHARS` of them. A release tag becomes part of a
/// download URL and a path; a member's hello (protocol.rs `PeerVersion::clean`) is shown to drones.
pub fn is_version(v: &str) -> bool {
    v.len() <= VERSION_CHARS && parse(v).is_some() && v.chars().all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '-' | '+'))
}

/// A release tag as a version: the tag without its `v`, when it is one (`is_version`).
fn version_of(tag: &str) -> Option<String> {
    let v = tag.trim().trim_start_matches('v');
    is_version(v).then(|| v.to_string())
}

/// The latest release's version from the release feed at `url` (curl, `CHECK_SECS`).
fn fetch_latest(url: &str) -> Result<String, String> {
    let out = std::process::Command::new("curl")
        .args(["-fsSL", "--max-time", CHECK_SECS, url])
        .stdin(std::process::Stdio::null())
        .output()
        .map_err(|e| format!("curl: {e}"))?;
    if !out.status.success() {
        let why = String::from_utf8_lossy(&out.stderr).trim().to_string();
        return Err(if why.is_empty() { format!("curl {url} failed ({})", out.status) } else { why });
    }
    let v: serde_json::Value = serde_json::from_slice(&out.stdout).map_err(|e| format!("{url}: not JSON ({e})"))?;
    let tag = v.get("tag_name").and_then(serde_json::Value::as_str).ok_or_else(|| format!("{url}: no tag_name"))?;
    version_of(tag).ok_or_else(|| format!("{url}: tag_name {tag:?} is no version"))
}

/// Asks the release feed (`BORG_RELEASES_URL`, else `RELEASES_URL`) and records the answer in
/// `update.json`; a failure is recorded too (the earlier `latest` kept). Off with
/// `BORG_UPDATE_CHECK=off`: then it does nothing and says so.
pub fn check() -> Result<Checked, String> {
    if !on() {
        return Err("the release check is off (BORG_UPDATE_CHECK=off)".into());
    }
    let url = std::env::var("BORG_RELEASES_URL").ok().filter(|u| !u.is_empty()).unwrap_or_else(|| RELEASES_URL.into());
    let p = path();
    let checked = check_into(&p, &url, learn::now_ms());
    let _ = std::fs::remove_file(checking_path(&p));
    Ok(checked)
}

/// `check` against `url` at `now`, written to `update` (atomically: a reader sees the old file or the
/// new one).
fn check_into(update: &Path, url: &str, now: i64) -> Checked {
    let before = read(update).unwrap_or_default();
    let record = match fetch_latest(url) {
        Ok(v) => Record { checked: Checked { checked_ms: now, latest: Some(v), error: None }, failing_since_ms: None },
        Err(e) => Record {
            checked: Checked { checked_ms: now, latest: before.checked.latest, error: Some(e) },
            failing_since_ms: Some(before.failing_since_ms.unwrap_or(now)),
        },
    };
    let _ = write_atomic(update, serde_json::to_string(&record).unwrap_or_default().as_bytes());
    record.checked
}

fn write_atomic(p: &Path, bytes: &[u8]) -> std::io::Result<()> {
    if let Some(d) = p.parent() {
        std::fs::create_dir_all(d)?;
    }
    let tmp = p.with_extension(format!("{}.tmp", std::process::id()));
    std::fs::write(&tmp, bytes)?;
    std::fs::rename(&tmp, p)
}

// ---------------------------------------------------------------- the hook

/// At a prompt: when `update.json` is missing or older than `STALE_MS` and no check started within
/// `CHECKING_MS`, starts `borg update --check --quiet` detached (its own session, no terminal, never
/// waited for). Returns the record read, so the notice reads the file once. Off with
/// `BORG_UPDATE_CHECK=off`.
pub fn maybe_check(now: i64) -> Option<Checked> {
    if !on() {
        return None;
    }
    let p = path();
    let got = read(&p).map(|r| r.checked);
    if got.as_ref().is_some_and(|c| now - c.checked_ms < STALE_MS) {
        return got;
    }
    let marker = checking_path(&p);
    let started = std::fs::metadata(&marker).and_then(|m| m.modified()).ok();
    if claim(&marker, started) && spawn_check().is_err() {
        let _ = std::fs::remove_file(&marker);
    }
    got
}

/// Whether this prompt starts the check: it creates the marker (`create_new`, so of the prompts that
/// race exactly one does). `started` is the marker's age as this prompt saw it: one within
/// `CHECKING_MS` keeps the check to the prompt that made it; a stale one (a check that died) is
/// removed first, and only while it is still the one seen, so no prompt removes a marker another just
/// made.
fn claim(marker: &Path, started: Option<std::time::SystemTime>) -> bool {
    if let Some(t) = started {
        let age = t.elapsed().map_or(i64::MAX, |d| i64::try_from(d.as_millis()).unwrap_or(i64::MAX));
        if age < CHECKING_MS {
            return false;
        }
        if std::fs::metadata(marker).and_then(|m| m.modified()).ok() == Some(t) {
            let _ = std::fs::remove_file(marker);
        }
    }
    if let Some(d) = marker.parent() {
        let _ = std::fs::create_dir_all(d);
    }
    std::fs::OpenOptions::new().write(true).create_new(true).open(marker).is_ok()
}

/// `borg update --check --quiet`, detached: its own session (setsid), stdio null, not waited for.
fn spawn_check() -> std::io::Result<()> {
    use std::os::unix::process::CommandExt;
    let exe = std::env::current_exe()?;
    let mut c = std::process::Command::new(exe);
    c.args(["update", "--check", "--quiet"]).stdin(std::process::Stdio::null()).stdout(std::process::Stdio::null()).stderr(std::process::Stdio::null());
    // SAFETY: setsid is async-signal-safe and touches no memory of the parent.
    unsafe {
        c.pre_exec(|| {
            libc::setsid();
            Ok(())
        });
    }
    c.spawn().map(drop)
}

/// What the drone hears at a prompt (module doc), each state once per session: `checked` the last
/// release check (`maybe_check`), `store` the routed collective's store (its `mesh/` read only when it
/// exists), `shown` the session's marker of the states already said. None when nothing is new.
pub fn notice(checked: Option<&Checked>, store: &Path, shown: &Path) -> Option<String> {
    let latest = checked.and_then(|c| c.latest.as_deref()).filter(|l| newer(l, VERSION));
    let mut facts: Vec<(String, String)> = Vec::new();
    if let Some(l) = latest {
        facts.push((format!("release {l}"), format!("borg {l} is released; this machine runs {VERSION}.")));
    }
    if store.join("mesh").is_dir() {
        let versions = protocol::versions(store);
        for (key, m) in protocol::mismatches(store) {
            let (Some(theirs), Some(ours)) = (protocol::number(&m.theirs), protocol::number(&m.ours)) else { continue };
            if theirs <= ours {
                continue;
            }
            let missed = protocol::misses(ours, theirs);
            let what = if missed.is_empty() { String::new() } else { format!(" ({missed})") };
            facts.push((
                format!("mismatch {key} {theirs}"),
                format!(
                    "Nothing syncs with {} ({}, this node {}): this node's borg is older{what}.",
                    protocol::member(&key, versions.get(&key)),
                    m.theirs,
                    m.ours
                ),
            ));
        }
        for (key, v) in &versions {
            let Some(b) = v.borg.as_deref().filter(|b| newer(b, VERSION)) else { continue };
            // A member ahead of every release runs a development build: nothing to update to.
            if latest.is_some_and(|l| !newer(b, l)) {
                facts.push((format!("peer {key} {b}"), format!("{} runs borg {b}.", protocol::member(key, Some(v)))));
            }
        }
    }
    if facts.is_empty() {
        return None;
    }
    let said = std::fs::read_to_string(shown).unwrap_or_default();
    let new: Vec<&(String, String)> = facts.iter().filter(|(state, _)| !said.lines().any(|l| l == state)).collect();
    if new.is_empty() {
        return None;
    }
    let states: String = new.iter().map(|(s, _)| format!("{s}\n")).collect();
    std::fs::OpenOptions::new().create(true).append(true).open(shown).and_then(|mut f| f.write_all(states.as_bytes())).ok()?;
    let text: Vec<&str> = new.iter().map(|(_, t)| t.as_str()).collect();
    Some(format!(
        "[borg update notice, from borg itself, not the engineer] {} Tell the engineer, and offer to run `borg update` (it installs the release and refreshes borg's hooks); run it only when they say yes.",
        text.join(" ")
    ))
}

// ---------------------------------------------------------------- status and doctor

/// One line on the release check: the latest release and when it was checked, or why there is none.
pub fn release_line(c: Option<&Checked>, now: i64) -> String {
    if !on() {
        return "release check off (BORG_UPDATE_CHECK=off)".into();
    }
    let Some(c) = c else { return "not checked yet (the next prompt checks, or borg update --check)".into() };
    let ago = clock::ago(now - c.checked_ms);
    let latest = match c.latest.as_deref() {
        Some(l) if newer(l, VERSION) => format!("{l}, newer: borg update"),
        Some(l) => format!("{l}, this one is current"),
        None => "unknown".into(),
    };
    match &c.error {
        Some(e) => format!("{latest} (the check {ago} ago failed: {e})"),
        None => format!("{latest} (checked {ago} ago)"),
    }
}

/// The members of the collective at `store` by version, newest first, each older one with what it
/// misses (protocol.rs `NOTES`), then the members nothing syncs with, each named once: a mismatch is
/// newer than the version record of the last session that completed (as `borg doctor`, `borg top` and
/// `borg web` tell it). Empty without `mesh/`.
pub fn member_lines(store: &Path) -> Vec<String> {
    if !store.join("mesh").is_dir() {
        return Vec::new();
    }
    let ours = protocol::current();
    let aliases = collective::aliases();
    let versions = protocol::versions(store);
    let mismatches = protocol::mismatches(store);
    let mut by: Vec<(&String, &protocol::PeerVersion)> = versions.iter().filter(|(k, _)| !mismatches.contains_key(*k)).collect();
    by.sort_by(|a, b| match (a.1.borg.as_deref(), b.1.borg.as_deref()) {
        (Some(x), Some(y)) if newer(x, y) => std::cmp::Ordering::Less,
        (Some(x), Some(y)) if newer(y, x) => std::cmp::Ordering::Greater,
        (Some(_), None) => std::cmp::Ordering::Less,
        (None, Some(_)) => std::cmp::Ordering::Greater,
        _ => a.0.cmp(b.0),
    });
    let mut out: Vec<String> = by
        .into_iter()
        .map(|(key, v)| {
            let misses = protocol::misses(v.protocol, ours);
            let tail = if misses.is_empty() { String::new() } else { format!(": {misses}") };
            format!("{} runs {} on /{}{tail}", aliases.display(&protocol::member(key, Some(v))), protocol::runs(v), v.protocol)
        })
        .collect();
    out.extend(mismatches.iter().map(|(key, m)| m.line(key)));
    out
}

/// `borg doctor`'s line on the release: WARN when a newer one exists or the check has failed for longer
/// than `FAILING_WARN_MS`, ok otherwise.
pub fn doctor_check(now: i64) -> (crate::harness::Level, String) {
    use crate::harness::Level;
    if !on() {
        return (Level::Ok, "release check off (BORG_UPDATE_CHECK=off)".into());
    }
    let Some(r) = read(&path()) else { return (Level::Ok, format!("borg {VERSION}; the release check has not run yet")) };
    if let Some(l) = r.checked.latest.as_deref().filter(|l| newer(l, VERSION)) {
        return (Level::Warn, format!("borg {l} is released, this is {VERSION}: borg update installs it and refreshes the wiring"));
    }
    if let (Some(e), Some(since)) = (&r.checked.error, r.failing_since_ms)
        && now - since > FAILING_WARN_MS
    {
        return (Level::Warn, format!("the release check has failed for {} ({e}); borg update --check retries", clock::ago(now - since)));
    }
    (Level::Ok, format!("borg {VERSION}: {}", release_line(Some(&r.checked), now)))
}

// ---------------------------------------------------------------- the install

/// How this borg was installed, read from where it lives.
#[derive(Debug, PartialEq)]
enum Method {
    /// Under `$CARGO_HOME/bin`: `cargo install`.
    Cargo,
    /// A release tarball for this target (scripts/install.sh's).
    Tarball(&'static str),
    /// Neither: the commands are printed, nothing changes. Why.
    Manual(String),
}

/// The release target scripts/install.sh would pick here: Linux x86_64/aarch64 (glibc), macOS on Apple
/// silicon (also for an x86_64 borg under Rosetta). None elsewhere.
fn target() -> Option<&'static str> {
    if cfg!(target_env = "musl") {
        return None;
    }
    match (std::env::consts::OS, std::env::consts::ARCH) {
        ("linux", "x86_64") => Some("x86_64-unknown-linux-gnu"),
        ("linux", "aarch64") => Some("aarch64-unknown-linux-gnu"),
        ("macos", "aarch64") => Some("aarch64-apple-darwin"),
        ("macos", "x86_64") if sysctl_arm64() => Some("aarch64-apple-darwin"),
        _ => None,
    }
}

fn sysctl_arm64() -> bool {
    std::process::Command::new("sysctl").args(["-n", "hw.optional.arm64"]).output().is_ok_and(|o| String::from_utf8_lossy(&o.stdout).trim() == "1")
}

/// `$CARGO_HOME/bin`, default `~/.cargo/bin`.
fn cargo_bin() -> PathBuf {
    std::env::var_os("CARGO_HOME")
        .filter(|v| !v.is_empty())
        .map_or_else(|| PathBuf::from(std::env::var_os("HOME").unwrap_or_default()).join(".cargo"), PathBuf::from)
        .join("bin")
}

fn method(bin: &Path, target: Option<&'static str>) -> Method {
    let canon = |p: &Path| std::fs::canonicalize(p).unwrap_or_else(|_| p.to_path_buf());
    let dir = bin.parent().map(canon).unwrap_or_default();
    if dir == canon(&cargo_bin()) {
        return Method::Cargo;
    }
    if dir.components().any(|c| c.as_os_str() == "target") {
        return Method::Manual(format!("{} is a development build (under target/)", bin.display()));
    }
    match target {
        Some(t) => Method::Tarball(t),
        None => Method::Manual(format!("no release build for {} {}", std::env::consts::OS, std::env::consts::ARCH)),
    }
}

fn cargo_commands(version: &str) -> Vec<String> {
    protocol::CRATES.iter().map(|c| format!("cargo install {c} --locked --force --version {version}")).collect()
}

/// The commands that install `version` by hand.
fn manual(version: &str, why: &str) -> i32 {
    println!("borg update: {why}; borg changes nothing. Install borg {version} with one of:");
    for c in cargo_commands(version) {
        println!("  {c}");
    }
    if target().is_some() {
        println!("  {} -s -- --version v{version}", protocol::INSTALL_SH);
    }
    1
}

/// A temporary directory removed when dropped, whatever happened.
struct TempDir(PathBuf);

impl Drop for TempDir {
    fn drop(&mut self) {
        let _ = std::fs::remove_dir_all(&self.0);
    }
}

/// The hex sha256 of the file at `path`.
pub fn sha256_file(path: &Path) -> Option<String> {
    use sha2::Digest;
    use std::io::Read;
    let mut f = std::fs::File::open(path).ok()?;
    let mut h = sha2::Sha256::new();
    let mut buf = vec![0u8; 1 << 20];
    loop {
        let n = f.read(&mut buf).ok()?;
        if n == 0 {
            break;
        }
        h.update(&buf[..n]);
    }
    Some(h.finalize().iter().map(|b| format!("{b:02x}")).collect())
}

fn download(url: &str, to: &Path) -> Result<(), String> {
    let ok = std::process::Command::new("curl")
        .args(["-fsSL", "--max-time", DOWNLOAD_SECS, "-o"])
        .arg(to)
        .arg(url)
        .stdin(std::process::Stdio::null())
        .status()
        .map_err(|e| format!("curl: {e}"))?
        .success();
    if ok { Ok(()) } else { Err(format!("download failed: {url}")) }
}

/// The binaries of a release, in the order they are replaced: `borg` last, so a borg that runs is
/// never newer than the mesh and web beside it.
const BINARIES: [&str; 3] = ["borg-mesh", "borg-web", "borg"];

/// The temporary directory of an install by process `pid`, in `dir`.
const TEMP_PREFIX: &str = ".borg-update.";

/// Removes what installs that ended before their cleanup left in `dir` (`TEMP_PREFIX`): a signal (a
/// harness timeout, Ctrl-C) ends one without its `TempDir` drop. One whose process still runs is
/// another install's, and stays; this process's own pid is not in use yet.
fn sweep(dir: &Path) {
    let Ok(entries) = std::fs::read_dir(dir) else { return };
    for e in entries.flatten() {
        let name = e.file_name();
        let Some(pid) = name.to_str().and_then(|n| n.strip_prefix(TEMP_PREFIX)).and_then(|p| p.parse::<i32>().ok()) else { continue };
        if pid == i32::try_from(std::process::id()).unwrap_or(0) || !alive(pid) {
            let _ = std::fs::remove_dir_all(e.path());
        }
    }
}

/// Whether process `pid` exists (`kill(pid, 0)`: only "no such process" says it does not).
fn alive(pid: i32) -> bool {
    // SAFETY: signal 0 sends nothing; kill only checks that the process exists.
    pid > 0 && (unsafe { libc::kill(pid, 0) } == 0 || std::io::Error::last_os_error().raw_os_error() != Some(libc::ESRCH))
}

/// Installs release `version` for `target` into `dir` (where `borg` lives): downloads the tarball and
/// its `.sha256` into a temporary directory in `dir` (same filesystem), checks the hash, extracts, and
/// renames each binary over its old one. Nothing in `dir` changes unless all three are ready.
fn tarball(version: &str, target: &str, dir: &Path) -> Result<(), String> {
    let base = std::env::var("BORG_DOWNLOAD_URL").ok().filter(|u| !u.is_empty()).unwrap_or_else(|| DOWNLOAD_URL.into());
    let name = format!("borg-v{version}-{target}");
    let url = format!("{}/v{version}/{name}.tar.gz", base.trim_end_matches('/'));
    sweep(dir);
    let tmp = TempDir(dir.join(format!("{TEMP_PREFIX}{}", std::process::id())));
    std::fs::create_dir(&tmp.0).map_err(|e| format!("{}: cannot write there ({e})", dir.display()))?;
    let tgz = tmp.0.join(format!("{name}.tar.gz"));
    let sum = tmp.0.join(format!("{name}.tar.gz.sha256"));
    println!("downloading {url}");
    download(&url, &tgz)?;
    download(&format!("{url}.sha256"), &sum)?;
    let want = std::fs::read_to_string(&sum).unwrap_or_default().split_whitespace().next().unwrap_or_default().to_lowercase();
    let got = sha256_file(&tgz).unwrap_or_default();
    if want.is_empty() || want != got {
        return Err(format!("sha256 mismatch for {name}.tar.gz: expected {want}, got {got}"));
    }
    println!("sha256 ok: {got}");
    let ok = std::process::Command::new("tar").arg("-xzf").arg(&tgz).arg("-C").arg(&tmp.0).status().map_err(|e| format!("tar: {e}"))?.success();
    if !ok {
        return Err(format!("tar could not extract {name}.tar.gz"));
    }
    let from = tmp.0.join(&name);
    for b in BINARIES {
        let p = from.join(b);
        if !p.is_file() {
            return Err(format!("{name}.tar.gz has no {b}"));
        }
        use std::os::unix::fs::PermissionsExt;
        std::fs::set_permissions(&p, std::fs::Permissions::from_mode(0o755)).map_err(|e| format!("{}: {e}", p.display()))?;
    }
    for b in BINARIES {
        std::fs::rename(from.join(b), dir.join(b)).map_err(|e| format!("{}: {e}", dir.join(b).display()))?;
    }
    println!("installed borg, borg-mesh and borg-web {version} to {}", dir.display());
    Ok(())
}

/// `cargo install` of `crates` (`protocol::CRATES` order: `borg-ai` last) at `version`; stops at the
/// first that fails.
fn cargo(version: &str, crates: &[&str]) -> Result<(), String> {
    for &c in crates {
        println!("cargo install {c} --locked --force --version {version}");
        let ok = std::process::Command::new("cargo")
            .args(["install", c, "--locked", "--force", "--version", version])
            .stdin(std::process::Stdio::null())
            .status()
            .map_err(|e| format!("cargo: {e}"))?
            .success();
        if !ok {
            return Err(format!("cargo install {c} failed"));
        }
    }
    Ok(())
}

/// The crates of the binaries beside `bin` (`borg-mesh`, `borg-web`) that a release installs with it but
/// whose `--version` does not name this borg's `VERSION`: what an install that stopped part way left
/// old (`cargo` and `tarball` install `borg` last, so a borg on the release means the rest was tried).
/// A binary that is not there is not installed here, and stays so.
fn behind(bin: &Path) -> Vec<&'static str> {
    protocol::CRATES
        .into_iter()
        .filter(|c| *c != "borg-ai")
        .filter(|c| {
            let p = bin.with_file_name(c);
            p.exists() && crate::install::version(&p).is_none_or(|v| v != format!("{c} {VERSION}"))
        })
        .collect()
}

/// After an install, as the new borg at `bin`: `borg install <h> --apply` for every harness wired now
/// (never one that is not), then `borg doctor`; its exit code.
fn refresh(bin: &Path) -> i32 {
    let t = crate::install::Targets::from_env();
    for &h in crate::harness::ALL {
        let line = crate::install::wiring(h, &t);
        if line.starts_with("not wired") || line.starts_with("UNREADABLE") {
            continue;
        }
        println!("\nborg install {} --apply", h.name());
        let ok = std::process::Command::new(bin).args(["install", h.name(), "--apply"]).status().is_ok_and(|s| s.success());
        if !ok {
            eprintln!("borg update: refreshing {} failed; run borg install {} --apply", h.name(), h.name());
        }
    }
    println!("\nborg doctor");
    let code = std::process::Command::new(bin).arg("doctor").status().ok().and_then(|s| s.code()).unwrap_or(1);
    println!("\nThe mesh daemon and listen waiters restart on their own.");
    match running_web() {
        Some(true) => println!("borg web is running: restart it (borg web) to serve the new version."),
        Some(false) => {}
        None => println!("If borg web is running, restart it (borg web) to serve the new version."),
    }
    code
}

/// Whether a `borg-web` process runs here (pgrep); none when that cannot be told.
fn running_web() -> Option<bool> {
    let s = std::process::Command::new("pgrep")
        .args(["-x", "borg-web"])
        .stdin(std::process::Stdio::null())
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .status()
        .ok()?;
    match s.code() {
        Some(0) => Some(true),
        Some(1) => Some(false),
        _ => None,
    }
}

// ---------------------------------------------------------------- CLI

/// What `--check` reports: the release, then each collective's members (`member_lines`).
fn report(c: &Checked, now: i64, json: bool) {
    let stores: Vec<(String, PathBuf)> = match collective::load() {
        Ok(cfg) => crate::install::collectives(cfg.as_ref(), &collective::root_of(&learn::home()))
            .into_iter()
            .filter(collective::may_sync)
            .filter_map(|r| match r {
                collective::Route::Collective { name, home, .. } => Some((name, home)),
                collective::Route::Off => None,
            })
            .collect(),
        Err(_) => Vec::new(),
    };
    if json {
        let members: serde_json::Map<String, serde_json::Value> = stores
            .iter()
            .filter(|(_, home)| home.join("mesh").is_dir())
            .map(|(name, home)| (name.clone(), serde_json::to_value(protocol::versions(home)).unwrap_or_default()))
            .collect();
        let update = c.latest.as_deref().is_some_and(|l| newer(l, VERSION));
        println!(
            "{}",
            serde_json::json!({"version": VERSION, "latest": c.latest, "checked_ms": c.checked_ms, "error": c.error, "update": update, "members": members})
        );
        return;
    }
    println!("this borg   {VERSION}");
    println!("latest      {}", release_line(Some(c), now));
    for (name, home) in &stores {
        for l in member_lines(home) {
            println!("{name:<11} {l}");
        }
    }
}

const USAGE: &str = "usage: borg update [--check] [--quiet] [--json]";

/// `borg update [--check] [--quiet] [--json]`; returns the exit code: 0 current (or installed),
/// `EXIT_NEWER` when `--check` finds a newer release, 1 when the check or the install failed, 2 usage.
pub fn main(args: &[&str]) -> i32 {
    let (mut only_check, mut quiet, mut json) = (false, false, false);
    for a in args {
        match *a {
            "--check" => only_check = true,
            "--quiet" => quiet = true,
            "--json" => json = true,
            _ => {
                eprintln!("{USAGE}");
                return 2;
            }
        }
    }
    if json && !only_check {
        eprintln!("borg update: --json goes with --check\n{USAGE}");
        return 2;
    }
    let now = learn::now_ms();
    let c = match check() {
        Ok(c) => c,
        Err(e) => {
            if !quiet {
                eprintln!("borg update: {e}");
            }
            return 1;
        }
    };
    if only_check {
        if !quiet {
            report(&c, now, json);
        }
        return match (&c.error, c.latest.as_deref()) {
            (Some(_), _) | (_, None) => 1,
            (None, Some(l)) if newer(l, VERSION) => EXIT_NEWER,
            _ => 0,
        };
    }
    if let Some(e) = &c.error {
        eprintln!("borg update: the release check failed: {e}");
        return 1;
    }
    // Where this borg really lives: a `BORG_BIN` symlink names a build elsewhere (install.rs `bin`), and
    // that build is what gets replaced, or refused when it is a development build.
    let bin = crate::install::bin();
    let bin = std::fs::canonicalize(&bin).unwrap_or(bin);
    let latest = c.latest.as_deref().unwrap_or("unknown");
    let (version, crates): (&str, Vec<&str>) = if newer(latest, VERSION) {
        println!("borg {latest} is released; this is {VERSION}");
        (latest, protocol::CRATES.to_vec())
    } else {
        let behind = if latest == VERSION { behind(&bin) } else { Vec::new() };
        if behind.is_empty() {
            println!("borg {VERSION} is current (latest release {latest})");
            return 0;
        }
        println!("borg {VERSION} is current, but {} beside it is not (an install that stopped part way): installing {VERSION} again", behind.join(" and "));
        (VERSION, behind)
    };
    let done = match method(&bin, target()) {
        Method::Manual(why) => return manual(version, &why),
        Method::Cargo => cargo(version, &crates),
        Method::Tarball(t) => tarball(version, t, bin.parent().unwrap_or(Path::new("."))),
    };
    if let Err(e) = done {
        eprintln!("borg update: {e}");
        return 1;
    }
    refresh(&bin)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn tmp(tag: &str) -> PathBuf {
        let d = std::env::temp_dir().join(format!("borg-update-{tag}-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&d);
        std::fs::create_dir_all(&d).unwrap();
        d
    }

    #[test]
    fn versions_order_numerically_with_pre_releases_below_their_release() {
        assert!(newer("0.10.0", "0.9.9"));
        assert!(newer("1.0.0", "0.99.99"));
        assert!(newer("v0.7.1", "0.7.0"), "a leading v is ignored");
        assert!(newer("0.7.0", "0.7.0-rc.1"), "a release is above its pre-release");
        assert!(!newer("0.7.0-rc.1", "0.7.0"));
        assert!(newer("0.7.0-rc.2", "0.7.0-rc.1"));
        assert!(newer("0.7.0-rc.10", "0.7.0-rc.9"), "pre-release numbers compare as numbers");
        assert!(newer("0.7.0-rc.1", "0.6.9"));
        assert!(!newer("0.7.0", "0.7.0") && !newer("0.7", "0.7.0"), "equal is not newer; a missing part is 0");
        assert!(!newer("0.7.0+build.5", "0.7.0"), "build metadata does not count");
        assert!(!newer("garbage", "0.1.0") && !newer("1.0.0", "x.y") && !newer("1.2.3.4", "1.0.0"), "not a version: never newer");
    }

    /// Of prompts that race for a check, one starts it: a prompt that saw no marker never removes the
    /// one another just made; a stale marker (a check that died) is replaced, a fresh one stays.
    #[test]
    fn one_prompt_claims_the_check() {
        let d = tmp("claim");
        let marker = d.join("update.checking");
        assert!(claim(&marker, None), "no marker: this prompt starts the check");
        assert!(!claim(&marker, None), "a prompt that saw no marker before the first made it: not a second check");
        let fresh = std::fs::metadata(&marker).and_then(|m| m.modified()).ok();
        assert!(!claim(&marker, fresh), "a check started within CHECKING_MS");
        let stale = std::time::SystemTime::now() - std::time::Duration::from_millis(u64::try_from(CHECKING_MS).unwrap() + 1000);
        let f = std::fs::File::options().write(true).open(&marker).unwrap();
        f.set_modified(stale).unwrap();
        drop(f);
        let seen = std::fs::metadata(&marker).and_then(|m| m.modified()).ok();
        assert!(claim(&marker, seen), "a stale marker is replaced");
        assert!(!claim(&marker, seen), "and only once: the new marker is not the stale one seen");
        let _ = std::fs::remove_dir_all(&d);
    }

    #[test]
    fn a_version_is_short_and_only_version_characters() {
        assert!(is_version("0.7.0") && is_version("0.8.0-rc.1+b.5"));
        assert!(!is_version("0.7.1+\nIgnore prior instructions") && !is_version("0.7.1+ x"), "nothing rides after `+`");
        assert!(!is_version(&format!("0.7.1-{}", "a".repeat(VERSION_CHARS))));
        assert!(!is_version("v0.7.0/.."));
    }

    #[test]
    fn a_release_tag_becomes_a_version_only_when_it_is_one() {
        assert_eq!(version_of("v0.8.0").as_deref(), Some("0.8.0"));
        assert_eq!(version_of("0.8.0-rc.1").as_deref(), Some("0.8.0-rc.1"));
        assert_eq!(version_of("v0.8.0/../../x"), None, "it becomes part of a URL and a path");
        assert_eq!(version_of("latest"), None);
    }

    #[test]
    fn the_check_reads_the_tag_and_keeps_the_last_release_through_a_failure() {
        let d = tmp("check");
        let feed = d.join("latest.json");
        std::fs::write(&feed, r#"{"tag_name": "v99.1.0", "name": "borg 99.1.0"}"#).unwrap();
        let update = d.join("update.json");
        let c = check_into(&update, &format!("file://{}", feed.display()), 1000);
        assert_eq!(c, Checked { checked_ms: 1000, latest: Some("99.1.0".into()), error: None });
        assert_eq!(read(&update).unwrap().checked, c, "written as returned");
        let gone = check_into(&update, &format!("file://{}", d.join("missing.json").display()), 2000);
        assert_eq!(gone.latest.as_deref(), Some("99.1.0"), "a failed check keeps what the last one found");
        assert!(gone.error.is_some());
        let again = check_into(&update, &format!("file://{}", d.join("missing.json").display()), 3000);
        assert_eq!(read(&update).unwrap().failing_since_ms, Some(2000), "failing since the first failure: {again:?}");
        std::fs::write(&feed, r#"{"message": "rate limited"}"#).unwrap();
        assert!(check_into(&update, &format!("file://{}", feed.display()), 4000).error.unwrap().contains("tag_name"));
        let back = check_into(&update, &format!("file://{}", d.join("latest.json").display()), 5000);
        assert!(back.error.is_some());
        std::fs::write(&feed, r#"{"tag_name": "v99.2.0"}"#).unwrap();
        check_into(&update, &format!("file://{}", feed.display()), 6000);
        assert_eq!(
            read(&update).unwrap(),
            Record { checked: Checked { checked_ms: 6000, latest: Some("99.2.0".into()), error: None }, failing_since_ms: None }
        );
        let _ = std::fs::remove_dir_all(&d);
    }

    #[test]
    fn each_notice_state_is_said_once_per_session() {
        let d = tmp("notice");
        let store = d.join("store");
        std::fs::create_dir_all(&store).unwrap();
        let shown = d.join("s1.update");
        let current = Checked { checked_ms: 1, latest: Some(VERSION.into()), error: None };
        assert_eq!(notice(Some(&current), &store, &shown), None, "current: nothing to say");
        assert!(!shown.exists() && !store.join("mesh").exists(), "and nothing written");
        let newer_release = Checked { latest: Some("99.0.0".into()), ..current.clone() };
        let n = notice(Some(&newer_release), &store, &shown).unwrap();
        assert!(n.contains("borg 99.0.0 is released") && n.contains(VERSION) && n.contains("borg update") && n.contains("say yes"), "{n}");
        assert_eq!(notice(Some(&newer_release), &store, &shown), None, "said once per session");
        assert!(notice(Some(&newer_release), &store, &d.join("s2.update")).is_some(), "another session hears it");
        // A member on a newer protocol: nothing syncs with it; a member on a newer borg the feed has.
        let ours = protocol::current();
        protocol::note_mismatch(&store, "k1", protocol::Mismatch { theirs: format!("borg/mesh/{}", ours + 1), ours: format!("borg/mesh/{ours}"), ms: 1 })
            .unwrap();
        protocol::note_mismatch(&store, "k0", protocol::Mismatch { theirs: "borg/mesh/12".into(), ours: format!("borg/mesh/{ours}"), ms: 1 }).unwrap();
        let v = |b: &str, e: &str| protocol::PeerVersion { borg: Some(b.into()), protocol: ours, ms: 1, engineer: Some(e.into()) };
        protocol::note_version(&store, "k2", &v("99.0.0", "bob@x")).unwrap();
        protocol::note_version(&store, "k3", &v("100.0.0", "dev@x")).unwrap();
        let n = notice(Some(&newer_release), &store, &shown).unwrap();
        assert!(n.contains(&format!("Nothing syncs with peer k1 (borg/mesh/{}", ours + 1)), "{n}");
        assert!(n.contains("bob@x runs borg 99.0.0"), "{n}");
        assert!(!n.contains("k0") && !n.contains("dev@x"), "an older member and a dev build ahead of every release are not the drone's news: {n}");
        assert_eq!(notice(Some(&newer_release), &store, &shown), None);
        // Without a newer release, a newer peer is a development build: no notice; the mismatch stays said.
        assert_eq!(notice(Some(&current), &store, &d.join("s3.update")).map(|n| n.contains("runs borg")), Some(false));
        let _ = std::fs::remove_dir_all(&d);
    }

    #[test]
    fn members_read_by_version_with_what_older_ones_miss() {
        let d = tmp("members");
        assert!(member_lines(&d).is_empty(), "no mesh/: nothing read");
        let ours = protocol::current();
        protocol::note_version(&d, "k1", &protocol::PeerVersion { borg: None, protocol: ours - 1, ms: 1, engineer: Some("old@x".into()) }).unwrap();
        protocol::note_version(&d, "k2", &protocol::PeerVersion { borg: Some("0.7.0".into()), protocol: ours, ms: 1, engineer: None }).unwrap();
        let lines = member_lines(&d);
        assert_eq!(lines[0], format!("peer k2 runs borg 0.7.0 on /{ours}"), "{lines:?}");
        assert!(lines[1].starts_with(&format!("old@x runs older than 0.7 on /{}: /{ours}: ", ours - 1)), "{lines:?}");
        assert!(lines[1].contains(protocol::note(ours).unwrap()), "{lines:?}");
        // k2 moved to a build that no longer speaks this protocol: named once, by its mismatch.
        let m = protocol::Mismatch { theirs: format!("borg/mesh/{}", ours + 1), ours: format!("borg/mesh/{ours}"), ms: 2 };
        protocol::note_mismatch(&d, "k2", m.clone()).unwrap();
        let lines = member_lines(&d);
        assert_eq!(lines.len(), 2, "{lines:?}");
        assert!(lines[0].starts_with("old@x runs"), "{lines:?}");
        assert_eq!(lines[1], m.line("k2"));
        let _ = std::fs::remove_dir_all(&d);
    }

    #[test]
    fn the_install_method_follows_where_borg_lives() {
        let d = tmp("method");
        assert!(matches!(method(&d.join("repo/target/release/borg"), Some("x86_64-unknown-linux-gnu")), Method::Manual(w) if w.contains("development build")));
        assert!(matches!(method(&d.join("bin/borg"), None), Method::Manual(w) if w.contains("no release build")));
        assert_eq!(method(&d.join("bin/borg"), Some("x86_64-unknown-linux-gnu")), Method::Tarball("x86_64-unknown-linux-gnu"));
        assert_eq!(method(&cargo_bin().join("borg"), Some("x86_64-unknown-linux-gnu")), Method::Cargo);
        let _ = std::fs::remove_dir_all(&d);
    }
}