dalo 1.2.0

AI agent skill manager: version, review, and sync skills through your agent.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
//! Passive release notices and install-channel-specific upgrade guidance.

use std::env;
use std::ffi::OsStr;
use std::fs;
use std::fs::OpenOptions;
use std::io::IsTerminal;
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};
use std::sync::mpsc::{self, Receiver};
use std::thread;
use std::time::Duration;

use serde::Deserialize;
use update_informer::http_client::{GenericHttpClient, HttpClient};
use update_informer::{Check, Package, Registry};

const REPOSITORY: &str = "sebastian-software/dalo";
const RELEASE_API: &str = "https://api.github.com/repos/sebastian-software/dalo/releases/latest";
const INSTALL_RECEIPT: &str = ".dalo-install-channel";
const CHECK_TIMEOUT: Duration = Duration::from_secs(1);
/// How long a finished check stays authoritative before the next one runs.
const CHECK_INTERVAL: Duration = Duration::from_secs(24 * 60 * 60);
/// How long a check that never finished suppresses the next attempt.
const RETRY_INTERVAL: Duration = Duration::from_secs(5 * 60);
/// How long a finishing command waits for a check it started itself.
///
/// Short enough that an interactive command still feels immediate, long enough
/// that a normal release-API round trip lands inside the same run. Commands
/// that never start a check (`--json`, CI, `DALO_OFFLINE=1`,
/// `DALO_UPDATE_CHECK=never`, non-interactive stderr) never wait at all.
const NOTICE_WAIT: Duration = Duration::from_millis(150);
/// Record of the last release check inside the update-notice cache directory.
const CHECK_RECORD: &str = "last-check";
/// Schema version of [`CHECK_RECORD`].
///
/// Bump it whenever the stored keys change meaning. A record written by a newer
/// schema is ignored, which makes the next run check again.
const CHECK_RECORD_SCHEMA: u32 = 1;

/// Outcome of one release check.
#[derive(Debug, Clone, PartialEq, Eq)]
enum CheckOutcome {
    /// The release API answered with a version newer than the running binary.
    Newer(String),
    /// The release API answered and the running binary is current.
    UpToDate,
    /// The check did not finish, so the next run retries it.
    Failed,
}

/// A passive update notice waiting to be printed.
///
/// The source stays private so the CLI cannot depend on how the notice was
/// obtained: an earlier run's record and a check started by this run print the
/// same way.
pub struct PendingNotice(NoticeSource);

enum NoticeSource {
    /// A newer release an earlier run already recorded.
    Recorded(String),
    /// A check started for this run.
    Running(Receiver<CheckOutcome>),
}

impl PendingNotice {
    fn recorded(latest: String) -> Self {
        Self(NoticeSource::Recorded(latest))
    }

    fn running(receiver: Receiver<CheckOutcome>) -> Self {
        Self(NoticeSource::Running(receiver))
    }

    /// Whether the notice needs no further wait.
    #[cfg(test)]
    fn is_recorded(&self) -> bool {
        matches!(self.0, NoticeSource::Recorded(_))
    }
}

/// What this invocation should do about the release check.
#[derive(Debug, Clone, PartialEq, Eq)]
enum CheckPlan {
    /// Announce this recorded release without touching the network.
    Announce(String),
    /// Start a check for this run.
    Check,
    /// Stay silent: a recent record already answers this run.
    Silent,
}

/// State of the recorded release check.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum CheckState {
    /// A check was started but never reported back.
    Pending,
    /// A check reached the release API and recorded its answer.
    Complete,
}

impl CheckState {
    fn as_str(self) -> &'static str {
        match self {
            Self::Pending => "pending",
            Self::Complete => "complete",
        }
    }

    fn parse(value: &str) -> Option<Self> {
        match value {
            "pending" => Some(Self::Pending),
            "complete" => Some(Self::Complete),
            _ => None,
        }
    }

    /// How long a record in this state stays authoritative.
    fn interval(self) -> Duration {
        match self {
            Self::Pending => RETRY_INTERVAL,
            Self::Complete => CHECK_INTERVAL,
        }
    }
}

/// The persisted result of the last release check.
#[derive(Debug, Clone, PartialEq, Eq)]
struct CheckRecord {
    state: CheckState,
    /// Version that ran the check. A different version invalidates the record.
    installed: String,
    /// Newer release the check found, if any.
    latest: Option<String>,
}

/// Start a passive release check when this invocation is eligible for one.
///
/// Update checks are advisory and fail open: network, cache, parsing, and
/// install-channel detection failures never change the requested command's
/// output or exit status.
pub fn start_notice_check() -> Option<PendingNotice> {
    if !update_checks_enabled() {
        return None;
    }

    let cache_dir = update_notice_cache_dir()?;
    start_notice_check_in(cache_dir, env!("CARGO_PKG_VERSION"))
}

fn start_notice_check_in(cache_dir: PathBuf, installed: &str) -> Option<PendingNotice> {
    match plan_check(read_check_record(&cache_dir), installed) {
        CheckPlan::Silent => None,
        CheckPlan::Announce(latest) => Some(PendingNotice::recorded(latest)),
        CheckPlan::Check => {
            // Claim the slot before the request so parallel and back-to-back
            // commands do not each open their own connection.
            write_check_record(
                &cache_dir,
                &CheckRecord {
                    state: CheckState::Pending,
                    installed: installed.to_owned(),
                    latest: None,
                },
            );
            let installed = installed.to_owned();
            spawn_notice_check(move || {
                let outcome = check_latest_version();
                record_outcome(&cache_dir, &installed, &outcome);
                outcome
            })
        }
    }
}

/// Decide what to do from the recorded check and its age.
fn plan_check(record: Option<(CheckRecord, Duration)>, installed: &str) -> CheckPlan {
    let Some((record, age)) = record else {
        // No record at all, including the first run after a fresh install.
        return CheckPlan::Check;
    };
    if record.installed != installed || age >= record.state.interval() {
        return CheckPlan::Check;
    }
    match (record.state, record.latest) {
        (CheckState::Complete, Some(latest)) => CheckPlan::Announce(latest),
        _ => CheckPlan::Silent,
    }
}

fn spawn_notice_check<F>(check: F) -> Option<PendingNotice>
where
    F: FnOnce() -> CheckOutcome + Send + 'static,
{
    let (sender, receiver) = mpsc::sync_channel(1);
    thread::Builder::new()
        .name("dalo-update-check".to_owned())
        .spawn(move || {
            let _ = sender.send(check());
        })
        .ok()?;
    Some(PendingNotice::running(receiver))
}

fn check_latest_version() -> CheckOutcome {
    // The interval is zero so `update_informer` performs the request instead of
    // consulting its own cache: the cadence is this module's `CHECK_RECORD`,
    // which does not seed itself with the running version and therefore lets a
    // fresh install check on its first interactive command.
    let informer = update_informer::new(DaloGitHub, REPOSITORY, env!("CARGO_PKG_VERSION"))
        .interval(Duration::ZERO)
        .timeout(CHECK_TIMEOUT);
    match informer.check_version() {
        Ok(Some(version)) => CheckOutcome::Newer(version.semver().to_string()),
        Ok(None) => CheckOutcome::UpToDate,
        Err(_) => CheckOutcome::Failed,
    }
}

/// Persist a finished check. An unfinished one keeps the pending record.
fn record_outcome(cache_dir: &Path, installed: &str, outcome: &CheckOutcome) {
    let latest = match outcome {
        CheckOutcome::Newer(version) => Some(version.clone()),
        CheckOutcome::UpToDate => None,
        CheckOutcome::Failed => return,
    };
    write_check_record(
        cache_dir,
        &CheckRecord {
            state: CheckState::Complete,
            installed: installed.to_owned(),
            latest,
        },
    );
}

fn render_check_record(record: &CheckRecord) -> String {
    let mut text = format!(
        "schema={CHECK_RECORD_SCHEMA}\nstate={}\ninstalled={}\n",
        record.state.as_str(),
        record.installed
    );
    if let Some(latest) = &record.latest {
        text.push_str(&format!("latest={latest}\n"));
    }
    text
}

fn parse_check_record(text: &str) -> Option<CheckRecord> {
    let mut schema = None;
    let mut state = None;
    let mut installed = None;
    let mut latest = None;
    for line in text.lines() {
        let Some((key, value)) = line.split_once('=') else {
            continue;
        };
        let value = value.trim();
        match key.trim() {
            "schema" => schema = value.parse::<u32>().ok(),
            "state" => state = CheckState::parse(value),
            "installed" => installed = Some(value.to_owned()),
            "latest" => latest = Some(value.to_owned()),
            _ => {}
        }
    }
    if schema != Some(CHECK_RECORD_SCHEMA) {
        return None;
    }
    Some(CheckRecord {
        state: state?,
        installed: installed?,
        latest,
    })
}

/// Read the recorded check together with how long ago it was written.
fn read_check_record(cache_dir: &Path) -> Option<(CheckRecord, Duration)> {
    let path = cache_dir.join(CHECK_RECORD);
    let age = fs::metadata(&path)
        .and_then(|metadata| metadata.modified())
        .ok()?
        .elapsed()
        .unwrap_or_default();
    let record = parse_check_record(&fs::read_to_string(&path).ok()?)?;
    Some((record, age))
}

/// Write the record, ignoring failures: the check is advisory.
fn write_check_record(cache_dir: &Path, record: &CheckRecord) {
    if fs::create_dir_all(cache_dir).is_err() {
        return;
    }
    let _ = fs::write(cache_dir.join(CHECK_RECORD), render_check_record(record));
}

/// Print a passive release notice, waiting at most 150 milliseconds for a check
/// this run started.
pub fn print_notice_if_ready(pending: Option<PendingNotice>) {
    let Some(latest_version) = pending.and_then(take_notice) else {
        return;
    };
    if !mark_version_notified(&latest_version) {
        return;
    }

    let executable = env::current_exe().ok();
    let channel = detect_install_channel(executable.as_deref());
    eprintln!(
        "\n{}",
        render_notice(
            &latest_version,
            env!("CARGO_PKG_VERSION"),
            channel,
            executable.as_deref()
        )
    );
}

/// Resolve the notice, waiting for a running check only up to [`NOTICE_WAIT`].
///
/// A check that misses the window is not lost: the background thread records
/// its answer when it still can, and the next run announces it without waiting.
fn take_notice(pending: PendingNotice) -> Option<String> {
    match pending.0 {
        NoticeSource::Recorded(version) => Some(version),
        NoticeSource::Running(receiver) => match receiver.recv_timeout(NOTICE_WAIT) {
            Ok(CheckOutcome::Newer(version)) => Some(version),
            _ => None,
        },
    }
}

fn render_notice(
    latest_version: &str,
    installed_version: &str,
    channel: InstallChannel,
    executable: Option<&Path>,
) -> String {
    let mut notice = format!(
        "update available: dalo v{latest_version} (installed v{installed_version} via {})",
        channel.label()
    );
    if let Some(command) = channel.upgrade_command(executable) {
        notice.push_str(&format!("\nupgrade with: {command}"));
    } else {
        notice.push_str("\nupgrade guide: https://dalo.sh/install.md");
    }
    notice
}

fn update_checks_enabled() -> bool {
    update_checks_enabled_for(
        env::var("DALO_UPDATE_CHECK").ok().as_deref(),
        std::io::stderr().is_terminal(),
        env_truthy("CI"),
        env_truthy("DALO_OFFLINE"),
    )
}

fn update_checks_enabled_for(
    setting: Option<&str>,
    stderr_is_terminal: bool,
    ci: bool,
    offline: bool,
) -> bool {
    let disabled = setting.is_some_and(|value| {
        matches!(
            value.to_ascii_lowercase().as_str(),
            "never" | "off" | "false" | "0"
        )
    });
    !disabled && stderr_is_terminal && !ci && !offline
}

fn env_truthy(name: &str) -> bool {
    env::var(name)
        .map(|value| {
            !matches!(
                value.to_ascii_lowercase().as_str(),
                "" | "0" | "false" | "off"
            )
        })
        .unwrap_or(false)
}

fn mark_version_notified(version: &str) -> bool {
    let Some(cache_dir) = update_notice_cache_dir() else {
        return true;
    };
    mark_version_notified_in(&cache_dir, version)
}

fn mark_version_notified_in(cache_dir: &Path, version: &str) -> bool {
    if fs::create_dir_all(cache_dir).is_err() {
        return true;
    }

    let marker = cache_dir.join(format!("notified-{version}"));
    match OpenOptions::new().write(true).create_new(true).open(marker) {
        Ok(_) => true,
        Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => false,
        Err(_) => true,
    }
}

fn update_notice_cache_dir() -> Option<PathBuf> {
    env::var_os("XDG_CACHE_HOME")
        .filter(|path| Path::new(path).is_absolute())
        .map(PathBuf::from)
        .or_else(|| env::var_os("HOME").map(|home| PathBuf::from(home).join(".cache")))
        .map(|cache| cache.join("dalo/update-notices"))
}

#[derive(Debug, Deserialize)]
struct GitHubRelease {
    tag_name: String,
}

struct DaloGitHub;

impl Registry for DaloGitHub {
    const NAME: &'static str = "github-dalo";

    fn get_latest_version<T: HttpClient>(
        http_client: GenericHttpClient<T>,
        _package: &Package,
    ) -> update_informer::Result<Option<String>> {
        let release = http_client
            .add_header("Accept", "application/vnd.github+json")
            .add_header("User-Agent", "dalo-update-informer")
            .get::<GitHubRelease>(&release_api())?;

        Ok(normalize_release_tag(&release.tag_name).map(str::to_owned))
    }
}

/// The release endpoint to query.
#[cfg(not(test))]
fn release_api() -> String {
    RELEASE_API.to_owned()
}

/// Test-only endpoint override.
///
/// It exists behind `cfg(test)`, so it is compiled out of the shipped binary
/// and is not part of the CLI surface: unit tests point the check at a
/// loopback server instead of reaching the real release API.
#[cfg(test)]
static TEST_RELEASE_API: std::sync::Mutex<Option<String>> = std::sync::Mutex::new(None);

/// Where an unconfigured test check goes: a closed loopback port, so a check
/// left running by an earlier test can never reach the real release API.
#[cfg(test)]
const TEST_UNREACHABLE_API: &str = "http://127.0.0.1:1/no-release-api-in-tests";

#[cfg(test)]
fn release_api() -> String {
    TEST_RELEASE_API
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner)
        .clone()
        .unwrap_or_else(|| TEST_UNREACHABLE_API.to_owned())
}

fn normalize_release_tag(tag: &str) -> Option<&str> {
    tag.strip_prefix("dalo-v")
        .or_else(|| tag.strip_prefix('v'))
        .or_else(|| {
            tag.chars()
                .next()
                .is_some_and(|character| character.is_ascii_digit())
                .then_some(tag)
        })
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum InstallChannel {
    Homebrew,
    Npm,
    Npx,
    Mise,
    MiseUbi,
    Nix,
    Cargo,
    Standalone,
    Unknown,
}

impl InstallChannel {
    fn label(self) -> &'static str {
        match self {
            Self::Homebrew => "Homebrew",
            Self::Npm => "npm",
            Self::Npx => "npx",
            Self::Mise | Self::MiseUbi => "mise",
            Self::Nix => "Nix",
            Self::Cargo => "Cargo",
            Self::Standalone => "the hosted installer",
            Self::Unknown => "an unknown installation method",
        }
    }

    fn upgrade_command(self, executable: Option<&Path>) -> Option<String> {
        match self {
            Self::Homebrew => Some("brew upgrade sebastian-software/tap/dalo".to_owned()),
            Self::Npm => Some("npm install --global getdalo@latest".to_owned()),
            Self::Npx => Some("npx getdalo@latest".to_owned()),
            Self::Mise => Some("mise upgrade github:sebastian-software/dalo".to_owned()),
            Self::MiseUbi => Some("mise upgrade ubi:sebastian-software/dalo".to_owned()),
            Self::Nix => Some("nix profile upgrade dalo".to_owned()),
            Self::Cargo => Some(cargo_upgrade_command()),
            Self::Standalone => standalone_upgrade_command(executable),
            Self::Unknown => None,
        }
    }
}

fn detect_install_channel(executable: Option<&Path>) -> InstallChannel {
    detect_install_channel_from(
        env::var("DALO_INSTALL_CHANNEL").ok().as_deref(),
        executable,
        env::var_os("HOME").as_deref().map(Path::new),
        env::var_os("CARGO_HOME").as_deref().map(Path::new),
    )
}

fn detect_install_channel_from(
    explicit: Option<&str>,
    executable: Option<&Path>,
    home: Option<&Path>,
    cargo_home: Option<&Path>,
) -> InstallChannel {
    if let Some(channel) = explicit.and_then(parse_install_channel) {
        return channel;
    }

    let Some(executable) = executable else {
        return InstallChannel::Unknown;
    };
    let components = executable
        .components()
        .map(|part| part.as_os_str())
        .collect::<Vec<_>>();

    if components
        .windows(2)
        .any(|parts| parts[0] == OsStr::new("Cellar") && parts[1] == OsStr::new("dalo"))
    {
        return InstallChannel::Homebrew;
    }

    if let Some(install_id) = components.windows(3).find_map(|parts| {
        (parts[0] == OsStr::new("mise") && parts[1] == OsStr::new("installs"))
            .then(|| parts[2].to_string_lossy().to_ascii_lowercase())
    }) {
        return if install_id == "ubi" || install_id.starts_with("ubi-") {
            InstallChannel::MiseUbi
        } else {
            InstallChannel::Mise
        };
    }

    let cargo_root = cargo_home
        .map(Path::to_path_buf)
        .or_else(|| home.map(|home| home.join(".cargo")));
    if cargo_root
        .as_deref()
        .is_some_and(|root| executable == root.join("bin/dalo"))
        || executable
            .parent()
            .and_then(Path::parent)
            .is_some_and(|root| root.join(".crates2.json").is_file())
    {
        return InstallChannel::Cargo;
    }

    if let Some(channel) = read_install_receipt(executable) {
        return channel;
    }

    InstallChannel::Unknown
}

fn parse_install_channel(channel: &str) -> Option<InstallChannel> {
    match channel.to_ascii_lowercase().as_str() {
        "homebrew" | "brew" => Some(InstallChannel::Homebrew),
        "npm" => Some(InstallChannel::Npm),
        "npx" => Some(InstallChannel::Npx),
        "mise" | "mise-github" => Some(InstallChannel::Mise),
        "mise-ubi" => Some(InstallChannel::MiseUbi),
        "nix" => Some(InstallChannel::Nix),
        "cargo" | "cargo-binstall" => Some(InstallChannel::Cargo),
        "standalone" | "installer" => Some(InstallChannel::Standalone),
        _ => None,
    }
}

fn read_install_receipt(executable: &Path) -> Option<InstallChannel> {
    executable
        .parent()
        .and_then(|parent| fs::read_to_string(parent.join(INSTALL_RECEIPT)).ok())
        .and_then(|receipt| match receipt.trim() {
            "standalone" => Some(InstallChannel::Standalone),
            "nix" => Some(InstallChannel::Nix),
            _ => None,
        })
}

fn cargo_upgrade_command() -> String {
    if command_exists("cargo-binstall") {
        "cargo binstall dalo".to_owned()
    } else {
        "cargo install dalo --locked --force".to_owned()
    }
}

fn command_exists(program: &str) -> bool {
    env::var_os("PATH").is_some_and(|path| {
        env::split_paths(&path).any(|directory| is_executable_file(&directory.join(program)))
    })
}

#[cfg(unix)]
fn is_executable_file(path: &Path) -> bool {
    fs::metadata(path)
        .is_ok_and(|metadata| metadata.is_file() && metadata.permissions().mode() & 0o111 != 0)
}

#[cfg(not(unix))]
fn is_executable_file(path: &Path) -> bool {
    path.is_file()
}

fn standalone_upgrade_command(executable: Option<&Path>) -> Option<String> {
    let install_dir = executable?.parent()?;
    let default_dir = env::var_os("HOME")
        .map(PathBuf::from)
        .map(|home| home.join(".local/bin"));
    if default_dir.as_deref() == Some(install_dir) {
        return Some("curl -fsSL https://dalo.sh/install.sh | sh".to_owned());
    }

    Some(format!(
        "curl -fsSL https://dalo.sh/install.sh | DALO_INSTALL_DIR={} sh",
        shell_quote(install_dir)
    ))
}

fn shell_quote(path: &Path) -> String {
    let value = path.to_string_lossy();
    format!("'{}'", value.replace('\'', "'\"'\"'"))
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::{Mutex, MutexGuard, PoisonError};
    use std::time::Instant;
    use tempfile::tempdir;

    /// Upper bound for how long resolving a notice may take a command.
    ///
    /// Generous next to [`NOTICE_WAIT`] so a loaded runner cannot fail the
    /// test, tight enough that waiting for the request itself would.
    const WAIT_BOUND: Duration = Duration::from_secs(2);

    /// Serializes the tests that repoint the release endpoint.
    static RELEASE_API_GUARD: Mutex<()> = Mutex::new(());

    fn lock_release_api() -> MutexGuard<'static, ()> {
        RELEASE_API_GUARD
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
    }

    fn set_release_api(url: Option<String>) {
        *TEST_RELEASE_API
            .lock()
            .unwrap_or_else(PoisonError::into_inner) = url;
    }

    /// A loopback stand-in for the release API, so no test leaves the machine.
    struct FakeReleaseEndpoint {
        url: String,
    }

    impl FakeReleaseEndpoint {
        /// An endpoint that answers every request with `body`.
        fn answering(body: &'static str) -> Self {
            Self::serve(Some(body))
        }

        /// An endpoint that accepts the connection and never answers.
        fn silent() -> Self {
            Self::serve(None)
        }

        fn serve(body: Option<&'static str>) -> Self {
            let listener =
                std::net::TcpListener::bind(("127.0.0.1", 0)).expect("loopback port should bind");
            let port = listener.local_addr().expect("local address").port();
            thread::spawn(move || {
                let mut held = Vec::new();
                for stream in listener.incoming() {
                    let Ok(mut stream) = stream else { continue };
                    let mut request = [0_u8; 1024];
                    let _ = std::io::Read::read(&mut stream, &mut request);
                    match body {
                        Some(body) => {
                            let response = format!(
                                "HTTP/1.1 200 OK\r\n\
                                 Content-Type: application/json\r\n\
                                 Content-Length: {}\r\n\
                                 Connection: close\r\n\r\n{body}",
                                body.len()
                            );
                            let _ = std::io::Write::write_all(&mut stream, response.as_bytes());
                        }
                        // Held open so the client waits rather than seeing EOF.
                        None => held.push(stream),
                    }
                }
            });
            Self {
                url: format!("http://127.0.0.1:{port}/releases/latest"),
            }
        }

        fn url(&self) -> String {
            self.url.clone()
        }
    }

    #[test]
    fn the_release_endpoint_should_point_at_this_project() {
        assert_eq!(
            RELEASE_API,
            format!("https://api.github.com/repos/{REPOSITORY}/releases/latest")
        );
    }

    #[test]
    fn release_tags_should_accept_dalo_prefix_and_reject_unrelated_tags() {
        assert_eq!(normalize_release_tag("dalo-v1.2.3"), Some("1.2.3"));
        assert_eq!(normalize_release_tag("v1.2.3"), Some("1.2.3"));
        assert_eq!(normalize_release_tag("1.2.3"), Some("1.2.3"));
        assert_eq!(normalize_release_tag("getdalo-v1.2.3"), None);
    }

    #[test]
    fn update_policy_should_require_an_interactive_online_non_ci_run() {
        assert!(update_checks_enabled_for(None, true, false, false));
        assert!(!update_checks_enabled_for(None, false, false, false));
        assert!(!update_checks_enabled_for(None, true, true, false));
        assert!(!update_checks_enabled_for(None, true, false, true));
        assert!(!update_checks_enabled_for(
            Some("NEVER"),
            true,
            false,
            false
        ));
    }

    #[test]
    fn unfinished_update_check_should_not_delay_command_completion() {
        let (started_sender, started_receiver) = mpsc::sync_channel(1);
        let (release_sender, release_receiver) = mpsc::sync_channel(1);
        let pending = spawn_notice_check(move || {
            started_sender.send(()).expect("test should observe start");
            release_receiver
                .recv()
                .expect("test should release update check");
            CheckOutcome::Newer("9.9.9".to_owned())
        })
        .expect("update thread should start");
        started_receiver
            .recv_timeout(Duration::from_secs(1))
            .expect("update check should start");

        let start = Instant::now();
        assert_eq!(take_notice(pending), None);
        let waited = start.elapsed();
        assert!(
            waited >= NOTICE_WAIT,
            "a started check should get its bounded wait, waited {waited:?}"
        );
        assert!(
            waited < WAIT_BOUND,
            "an unfinished check should not hold the command, waited {waited:?}"
        );
        release_sender
            .send(())
            .expect("update check should be released");
    }

    #[test]
    fn completed_update_check_should_return_its_notice() {
        let (sender, receiver) = mpsc::sync_channel(1);
        sender
            .send(CheckOutcome::Newer("9.9.9".to_owned()))
            .expect("notice should be queued");

        assert_eq!(
            take_notice(PendingNotice::running(receiver)),
            Some("9.9.9".to_owned())
        );
    }

    #[test]
    fn recorded_release_should_be_announced_without_waiting() {
        let start = Instant::now();
        assert_eq!(
            take_notice(PendingNotice::recorded("9.9.9".to_owned())),
            Some("9.9.9".to_owned())
        );
        assert!(start.elapsed() < NOTICE_WAIT);
    }

    #[test]
    fn up_to_date_check_should_not_produce_a_notice() {
        let (sender, receiver) = mpsc::sync_channel(1);
        sender
            .send(CheckOutcome::UpToDate)
            .expect("outcome should be queued");

        assert_eq!(take_notice(PendingNotice::running(receiver)), None);
    }

    #[test]
    fn check_record_should_survive_a_round_trip_and_ignore_foreign_schemas() {
        let recorded = CheckRecord {
            state: CheckState::Complete,
            installed: "1.2.3".to_owned(),
            latest: Some("1.2.4".to_owned()),
        };
        assert_eq!(
            parse_check_record(&render_check_record(&recorded)),
            Some(recorded)
        );

        let up_to_date = CheckRecord {
            state: CheckState::Pending,
            installed: "1.2.3".to_owned(),
            latest: None,
        };
        assert_eq!(
            parse_check_record(&render_check_record(&up_to_date)),
            Some(up_to_date)
        );

        assert_eq!(
            parse_check_record("schema=9999\nstate=complete\ninstalled=1.2.3\n"),
            None
        );
        assert_eq!(parse_check_record("not a record"), None);
    }

    #[test]
    fn a_fresh_install_should_check_on_its_first_interactive_command() {
        assert_eq!(plan_check(None, "1.2.3"), CheckPlan::Check);
    }

    #[test]
    fn a_recorded_check_should_hold_for_a_day_and_then_check_again() {
        let record = CheckRecord {
            state: CheckState::Complete,
            installed: "1.2.3".to_owned(),
            latest: Some("1.2.4".to_owned()),
        };

        assert_eq!(
            plan_check(Some((record.clone(), Duration::from_secs(60))), "1.2.3"),
            CheckPlan::Announce("1.2.4".to_owned())
        );
        assert_eq!(
            plan_check(Some((record.clone(), CHECK_INTERVAL)), "1.2.3"),
            CheckPlan::Check
        );
        // A replaced binary invalidates the record whatever its age.
        assert_eq!(
            plan_check(Some((record, Duration::from_secs(60))), "1.2.4"),
            CheckPlan::Check
        );

        let current = CheckRecord {
            state: CheckState::Complete,
            installed: "1.2.3".to_owned(),
            latest: None,
        };
        assert_eq!(
            plan_check(Some((current, Duration::from_secs(60))), "1.2.3"),
            CheckPlan::Silent
        );
    }

    #[test]
    fn an_unfinished_check_should_be_retried_instead_of_burning_the_day() {
        let record = CheckRecord {
            state: CheckState::Pending,
            installed: "1.2.3".to_owned(),
            latest: None,
        };

        assert_eq!(
            plan_check(Some((record.clone(), Duration::from_secs(60))), "1.2.3"),
            CheckPlan::Silent
        );
        assert_eq!(
            plan_check(Some((record, RETRY_INTERVAL)), "1.2.3"),
            CheckPlan::Check
        );
    }

    #[test]
    fn only_a_finished_check_should_be_recorded() {
        let temp = tempdir().expect("tempdir");

        record_outcome(temp.path(), "1.2.3", &CheckOutcome::Failed);
        assert_eq!(read_check_record(temp.path()), None);

        record_outcome(
            temp.path(),
            "1.2.3",
            &CheckOutcome::Newer("1.2.4".to_owned()),
        );
        let (record, _) = read_check_record(temp.path()).expect("record should be written");
        assert_eq!(
            record,
            CheckRecord {
                state: CheckState::Complete,
                installed: "1.2.3".to_owned(),
                latest: Some("1.2.4".to_owned()),
            }
        );

        record_outcome(temp.path(), "1.2.3", &CheckOutcome::UpToDate);
        let (record, _) = read_check_record(temp.path()).expect("record should be written");
        assert_eq!(record.latest, None);
    }

    #[test]
    fn a_fast_command_should_deliver_the_notice_from_a_fake_endpoint() {
        let _guard = lock_release_api();
        let temp = tempdir().expect("tempdir");
        let endpoint = FakeReleaseEndpoint::answering(r#"{"tag_name":"dalo-v99.0.0"}"#);
        set_release_api(Some(endpoint.url()));

        let start = Instant::now();
        let pending = start_notice_check_in(temp.path().to_path_buf(), env!("CARGO_PKG_VERSION"))
            .expect("a fresh cache should start a check");
        let notice = take_notice(pending);
        let elapsed = start.elapsed();

        set_release_api(None);
        assert_eq!(notice, Some("99.0.0".to_owned()));
        assert!(
            elapsed < WAIT_BOUND,
            "a fast command should not be held up, took {elapsed:?}"
        );
        let (record, _) = read_check_record(temp.path()).expect("the check should be recorded");
        assert_eq!(record.state, CheckState::Complete);
        assert_eq!(record.latest.as_deref(), Some("99.0.0"));

        // The recorded release is announced again without any network access.
        set_release_api(Some("http://127.0.0.1:1/never-reached".to_owned()));
        let pending = start_notice_check_in(temp.path().to_path_buf(), env!("CARGO_PKG_VERSION"))
            .expect("the recorded release should still be announced");
        assert!(pending.is_recorded());
        assert_eq!(take_notice(pending), Some("99.0.0".to_owned()));
        set_release_api(None);
    }

    #[test]
    fn a_slow_endpoint_should_not_delay_a_fast_command() {
        let _guard = lock_release_api();
        let temp = tempdir().expect("tempdir");
        let endpoint = FakeReleaseEndpoint::silent();
        set_release_api(Some(endpoint.url()));

        let start = Instant::now();
        let pending = start_notice_check_in(temp.path().to_path_buf(), env!("CARGO_PKG_VERSION"))
            .expect("a fresh cache should start a check");
        let notice = take_notice(pending);
        let elapsed = start.elapsed();
        set_release_api(None);

        assert_eq!(notice, None);
        assert!(
            elapsed < WAIT_BOUND,
            "a silent endpoint should not be waited out, took {elapsed:?}"
        );
        let (record, _) = read_check_record(temp.path()).expect("the attempt should be recorded");
        assert_eq!(
            record.state,
            CheckState::Pending,
            "an unfinished check stays pending so the next run retries it"
        );
    }

    #[test]
    fn explicit_launcher_channel_should_win_over_path_detection() {
        let channel = detect_install_channel_from(
            Some("npx"),
            Some(Path::new("/home/user/.cache/dalo/1.2.3/dalo")),
            Some(Path::new("/home/user")),
            None,
        );

        assert_eq!(channel, InstallChannel::Npx);
        assert_eq!(
            channel.upgrade_command(None).as_deref(),
            Some("npx getdalo@latest")
        );
    }

    #[test]
    fn managed_install_paths_should_be_detected_conservatively() {
        assert_eq!(
            detect_install_channel_from(
                None,
                Some(Path::new("/opt/homebrew/Cellar/dalo/1.2.3/bin/dalo")),
                Some(Path::new("/Users/user")),
                None,
            ),
            InstallChannel::Homebrew
        );
        assert_eq!(
            detect_install_channel_from(
                None,
                Some(Path::new(
                    "/home/rubicon/.local/share/mise/installs/github-sebastian-software-dalo/latest/bin/dalo"
                )),
                Some(Path::new("/home/rubicon")),
                None,
            ),
            InstallChannel::Mise
        );
        assert_eq!(
            detect_install_channel_from(
                None,
                Some(Path::new(
                    "/home/user/.local/share/mise/installs/ubi-sebastian-software-dalo/latest/bin/dalo"
                )),
                Some(Path::new("/home/user")),
                None,
            ),
            InstallChannel::MiseUbi
        );
        assert_eq!(
            detect_install_channel_from(
                None,
                Some(Path::new("/home/user/.cargo/bin/dalo")),
                Some(Path::new("/home/user")),
                None,
            ),
            InstallChannel::Cargo
        );
    }

    #[test]
    fn installer_receipt_should_preserve_custom_install_directory() {
        let temp = tempdir().expect("tempdir");
        let executable = temp.path().join("custom bin/dalo");
        fs::create_dir_all(executable.parent().expect("parent")).expect("create bin dir");
        fs::write(
            executable.parent().expect("parent").join(INSTALL_RECEIPT),
            "standalone\n",
        )
        .expect("write receipt");

        let channel = detect_install_channel_from(None, Some(&executable), None, None);
        assert_eq!(channel, InstallChannel::Standalone);
        let expected = format!(
            "curl -fsSL https://dalo.sh/install.sh | DALO_INSTALL_DIR={} sh",
            shell_quote(executable.parent().expect("parent"))
        );
        assert_eq!(channel.upgrade_command(Some(&executable)), Some(expected));
    }

    #[test]
    fn nix_receipt_should_select_nix_profile_upgrade() {
        let temp = tempdir().expect("tempdir");
        let executable = temp.path().join("bin/.dalo-wrapped");
        fs::create_dir_all(executable.parent().expect("parent")).expect("create bin dir");
        fs::write(
            executable.parent().expect("parent").join(INSTALL_RECEIPT),
            "nix\n",
        )
        .expect("write receipt");

        let channel = detect_install_channel_from(None, Some(&executable), None, None);
        assert_eq!(channel, InstallChannel::Nix);
        assert_eq!(
            channel.upgrade_command(Some(&executable)).as_deref(),
            Some("nix profile upgrade dalo")
        );
    }

    #[test]
    fn bare_default_install_path_should_not_guess_standalone() {
        let channel = detect_install_channel_from(
            None,
            Some(Path::new("/home/user/.local/bin/dalo")),
            Some(Path::new("/home/user")),
            None,
        );

        assert_eq!(channel, InstallChannel::Unknown);
        assert_eq!(channel.upgrade_command(None), None);
    }

    #[test]
    fn unknown_installation_should_not_guess_an_upgrade_command() {
        let temp = tempdir().expect("tempdir");
        let executable = temp.path().join("bin/dalo");
        fs::create_dir_all(executable.parent().expect("parent")).expect("create bin dir");

        let channel = detect_install_channel_from(
            Some("not-a-channel"),
            Some(&executable),
            Some(temp.path()),
            None,
        );

        assert_eq!(channel, InstallChannel::Unknown);
        assert_eq!(channel.upgrade_command(None), None);
    }

    #[test]
    fn notice_should_include_channel_specific_or_generic_guidance() {
        let latest_version = update_informer::fake(DaloGitHub, "dalo", "1.2.3", "1.2.4")
            .check_version()
            .expect("fake version check should succeed")
            .expect("fake version check should return a version");

        assert_eq!(
            render_notice(
                &latest_version.semver().to_string(),
                "1.2.3",
                InstallChannel::Homebrew,
                None,
            ),
            "update available: dalo v1.2.4 (installed v1.2.3 via Homebrew)\n\
             upgrade with: brew upgrade sebastian-software/tap/dalo"
        );
        assert_eq!(
            render_notice(
                &latest_version.semver().to_string(),
                "1.2.3",
                InstallChannel::Unknown,
                None,
            ),
            "update available: dalo v1.2.4 (installed v1.2.3 via an unknown installation method)\n\
             upgrade guide: https://dalo.sh/install.md"
        );
    }

    #[test]
    fn major_release_notice_should_keep_a_single_version_prefix() {
        assert_eq!(normalize_release_tag("dalo-v1.0.0"), Some("1.0.0"));

        let latest_version = update_informer::fake(DaloGitHub, "dalo", "0.16.0", "1.0.0")
            .check_version()
            .expect("fake version check should succeed")
            .expect("fake version check should return a version");

        assert_eq!(
            render_notice(
                &latest_version.semver().to_string(),
                "0.16.0",
                InstallChannel::Npm,
                None,
            ),
            "update available: dalo v1.0.0 (installed v0.16.0 via npm)\n\
             upgrade with: npm install --global getdalo@latest"
        );
    }

    #[test]
    fn each_new_version_should_be_notified_only_once() {
        let temp = tempdir().expect("tempdir");

        assert!(mark_version_notified_in(temp.path(), "v1.2.3"));
        assert!(!mark_version_notified_in(temp.path(), "v1.2.3"));
        assert!(mark_version_notified_in(temp.path(), "v1.2.4"));
    }

    #[test]
    fn shell_quote_should_handle_single_quotes() {
        assert_eq!(
            shell_quote(Path::new("/tmp/dalo's bin")),
            "'/tmp/dalo'\"'\"'s bin'"
        );
    }

    #[cfg(unix)]
    #[test]
    fn command_detection_should_require_an_executable_file() {
        let temp = tempdir().expect("tempdir");
        let command = temp.path().join("cargo-binstall");
        fs::write(&command, "not really a binary").expect("write command");

        let mut permissions = fs::metadata(&command).expect("metadata").permissions();
        permissions.set_mode(0o644);
        fs::set_permissions(&command, permissions).expect("remove execute permission");
        assert!(!is_executable_file(&command));

        let mut permissions = fs::metadata(&command).expect("metadata").permissions();
        permissions.set_mode(0o755);
        fs::set_permissions(&command, permissions).expect("add execute permission");
        assert!(is_executable_file(&command));
    }
}