Skip to main content

dev_prune/commands/
update.rs

1// Copyright 2026 VKrishna04
2// SPDX-License-Identifier: Apache-2.0
3
4// Handler for `dev-prune update`, and the periodic release check behind it.
5//
6// The check is opt-*out*. An out-of-date cleanup tool is a tool whose safety fixes you do
7// not have, so `devp update` asks GitHub for the latest release by default, and `devp
8// run` / `devp status` repeat that quietly at most once a week. Both are switched off by
9// `devp config set update_check false`, and `devp update --offline` skips a single run.
10//
11// What leaves the machine is one unauthenticated GET to the public releases page. It
12// carries no identifier, no configuration, no repository paths and no usage data — the
13// only thing the server learns is that some copy of dev-prune asked what the latest
14// version is. Nothing else in the binary opens a socket. See `docs/PRIVACY.md`.
15//
16// Nothing is downloaded until someone says so: `devp update` asks `[y/N]` at a terminal
17// when a newer release exists, `--install` (or `-y`) is the same answer given up front,
18// and a copy run from a script gets the printed upgrade command and nothing else. The
19// scheduled pass is never interrupted by an upgrade: it runs the managed copy under
20// `<config>/bin`, which is replaced by atomic rename and refreshed from the new binary
21// on the next healthy run (`setup::stable_exe_path`), so a pass already in flight keeps
22// its loaded image and the next pass picks up the new one.
23
24use std::cmp::Ordering;
25use std::fs;
26use std::io::Read;
27use std::path::{Path, PathBuf};
28use std::time::Duration;
29
30use anyhow::{Context, Result};
31use chrono::Utc;
32
33use crate::channel::Channel;
34use crate::config::Registry;
35use crate::constants;
36use crate::output;
37
38pub fn run(offline: bool, install: bool, channels: bool, yes: bool) -> Result<()> {
39    if install {
40        return run_install();
41    }
42    if channels {
43        return run_channels();
44    }
45    output::print_header("dev-prune version & upgrade");
46
47    output::print_info(&format!("Installed version: v{}", constants::VERSION));
48
49    let mut registry = Registry::load().ok();
50    let mut newer = None;
51
52    if offline {
53        output::print_info("Skipping the release check because `--offline` was passed.");
54    } else if let Some(reg) = registry.as_mut() {
55        if reg.settings.update_check {
56            // An explicit `devp update` always asks, regardless of when the last
57            // automatic check ran — the user is standing there waiting for the answer.
58            match refresh_latest(reg) {
59                Ok(latest) => {
60                    report_comparison(&latest);
61                    if compare_versions(constants::VERSION, &latest) == Some(Ordering::Less) {
62                        newer = Some(latest);
63                    }
64                }
65                // A failed check is not a failed command. Someone offline, behind a
66                // proxy, or hitting a rate limit still wants the upgrade instructions.
67                // `:#` so the cause is printed under the label: "request failed" alone
68                // once hid an HTTP 403 and sent a user looking at their network.
69                Err(e) => output::print_warning(&format!(
70                    "Could not check the latest release ({e:#}). The upgrade commands below still apply."
71                )),
72            }
73            let _ = reg.save();
74        } else {
75            output::print_info(
76                "The release check is off (`devp config set update_check true` re-enables it).",
77            );
78        }
79    }
80
81    println!();
82    println!("  Latest releases:  {}", constants::RELEASES_URL);
83    println!();
84
85    // Under a pin, the upgrade command is the one command that undoes it. Printing it
86    // here would answer "how do I upgrade this" with the wrong answer, so the pin is
87    // what the section says instead.
88    if registry.is_some_and(|r| r.settings.version_lock) {
89        output::print_info(&locked_notice(None));
90        return Ok(());
91    }
92    print_upgrade_commands();
93
94    // The command already knows a newer release exists and how to fetch it, so making
95    // the user retype it with `--install` was one round trip too many. The default is
96    // still "no": Enter, a pipe, or a script leaves the binary exactly as it was.
97    if let Some(latest) = newer
98        && confirm_install(&latest, yes)
99    {
100        println!();
101        return install_latest(&latest);
102    }
103
104    Ok(())
105}
106
107/// Ask whether to install `latest` right now. `yes` answers without asking.
108///
109/// Without a terminal on stdin the answer is silently "no": the upgrade command was
110/// just printed, and a prompt nobody can answer would hang whatever called us.
111fn confirm_install(latest: &str, yes: bool) -> bool {
112    use std::io::{IsTerminal, Write};
113
114    if yes {
115        return true;
116    }
117    if !std::io::stdin().is_terminal() {
118        return false;
119    }
120    eprint!("Install v{latest} now? [y/N]: ");
121    if std::io::stderr().flush().is_err() {
122        return false;
123    }
124    let mut input = String::new();
125    if std::io::stdin().read_line(&mut input).is_err() {
126        return false;
127    }
128    matches!(input.trim().to_lowercase().as_str(), "y" | "yes")
129}
130
131/// The one sentence every refusal prints, so the pin and the way out of it always
132/// arrive together.
133///
134/// A lock that silently does nothing is indistinguishable from an update path that has
135/// broken, and "it stopped updating" is what people conclude when a tool goes quiet.
136/// `latest` is passed on the paths that already know a newer release exists, because
137/// "there is one, and you are deliberately not getting it" is a different fact from
138/// "you are pinned".
139pub(crate) fn locked_notice(latest: Option<&str>) -> String {
140    let head = match latest {
141        Some(latest) => format!("dev-prune v{latest} is out. "),
142        None => String::new(),
143    };
144    format!(
145        "{head}`version_lock` is on, so this copy stays at v{}. \
146         `devp config set version_lock false` releases it.",
147        constants::VERSION
148    )
149}
150
151/// Ask GitHub right now — no interval — and say where the installed build stands.
152///
153/// For `devp init`, which is deliberate and infrequent enough to be worth a round trip:
154/// setting a machine up is exactly the moment to learn the binary is a version behind.
155/// `devp run` deliberately does not use this; it goes through [`notify_if_outdated`],
156/// which is interval-gated so everyday work never waits on the network.
157///
158/// Returns `true` when the registry changed and needs saving.
159pub fn check_now(registry: &mut Registry) -> bool {
160    if !registry.settings.update_check {
161        return false;
162    }
163
164    match refresh_latest(registry) {
165        Ok(latest) => {
166            report_comparison(&latest);
167            if compare_versions(constants::VERSION, &latest) == Some(Ordering::Less) {
168                print_upgrade_commands();
169            }
170        }
171        // Not being able to reach GitHub is not a failed `init`.
172        Err(e) => output::print_info(&format!("Could not check for a newer release ({e:#}).")),
173    }
174    true
175}
176
177/// Name the one command that upgrades *this* copy, and only fall back to the menu.
178///
179/// The old version printed all eight channels and told the reader to pick the one they
180/// installed from. Nobody remembers that — it was a decision made once, possibly a year
181/// ago, on a machine they have since reimaged. The channel is written in the path of the
182/// running binary and `Channel::detect` already reads it, so asking the user to recall it
183/// was asking for information dev-prune already had.
184fn print_upgrade_commands() {
185    let channel = Channel::detect();
186    match channel.upgrade_command() {
187        Some(command) => {
188            println!("  Installed with {} — upgrade with:", channel.label());
189            println!("    {command}");
190            println!();
191            println!("  Or `devp update --install` to let dev-prune do it for you.");
192            println!();
193            // Named, not printed. The channel above is the answer for this copy; the
194            // rest of the table is for the reader who has a second machine, or who does
195            // not believe the detection.
196            println!("  `devp update --channels` lists the command for every channel.");
197        }
198        // `Unknown` means the binary sits somewhere no channel owns — a dev build, a
199        // hand-copied file, a distro package. There is no manager to name, so this is the
200        // one case where the full list is the honest answer.
201        None => {
202            println!("  This copy is not in a location any install channel owns, so there");
203            println!("  is no package manager to name. Replace it in place with:");
204            println!("    devp update --install");
205            println!();
206            println!("  Or install through a channel, which keeps it upgradeable:");
207            print_every_upgrade_command();
208        }
209    }
210}
211
212/// `devp update --channels`: the whole table, and which row this copy is on.
213///
214/// Deliberately offline. The one question it answers — "what do I type to upgrade a
215/// dev-prune installed through X" — does not depend on what the latest release is, and
216/// making it wait on the network would make it useless on the machine where it is most
217/// often needed.
218fn run_channels() -> Result<()> {
219    output::print_header("dev-prune upgrade commands");
220    let current = Channel::detect();
221    println!();
222    println!("  This copy came from {}.", current.label());
223    println!();
224    print_every_upgrade_command();
225    println!();
226    output::print_info(
227        "`devp update --install` replaces this copy directly, without the manager. \
228         `devp install --channel <name>` moves it to a different one.",
229    );
230    Ok(())
231}
232
233/// Every channel's own upgrade command, one per line, widest label first.
234///
235/// Printed from the table rather than typed out. The version of this list that was typed
236/// out named five channels of the nine that existed, and the copy the user was holding
237/// had been installed through one of the four it did not mention.
238fn print_every_upgrade_command() {
239    let channels = [
240        Channel::Installer,
241        Channel::Cargo,
242        Channel::Npm,
243        Channel::Bun,
244        Channel::Pnpm,
245        Channel::Yarn,
246        Channel::UvTool,
247        Channel::Pipx,
248        Channel::Pip,
249        Channel::WinGet,
250        Channel::Scoop,
251        Channel::Homebrew,
252    ];
253    let width = channels.iter().map(|c| c.label().len()).max().unwrap_or(0);
254    for channel in channels {
255        if let Some(command) = channel.upgrade_command() {
256            println!("    {:<width$}  {command}", channel.label());
257        }
258    }
259}
260
261/// `devp update --install`: upgrade this installation to the latest release.
262///
263/// Downloads the release binary from GitHub and replaces the files itself, rather than
264/// asking whichever package manager delivered the first copy to do it. That inversion is
265/// deliberate. There is exactly one binary that matters — the managed copy under
266/// `<config>/bin`, which the git hooks, the scheduler and `PATH` all point at — and it
267/// does not live inside `node_modules`, a uv tool directory or `~/.cargo/bin`. Asking
268/// `uv` to upgrade a file it has never heard of was never going to work, and asking it to
269/// upgrade its *own* copy left the one that actually runs untouched.
270///
271/// So both are replaced: the managed copy first, because that is what runs unattended,
272/// then the running binary if it is a different file, because that is what the user
273/// types. The channel's own bookkeeping (what `uv tool list` believes is installed) is
274/// left stale on purpose — correcting it means running the channel's installer, which is
275/// the one thing this route exists to avoid — and the command to resync it is printed.
276///
277/// Falls back to the channel's own upgrade command when there is no published binary for
278/// this platform or the download fails, so a release-page outage costs the fast path and
279/// not the upgrade.
280fn run_install() -> Result<()> {
281    output::print_header("dev-prune self-update");
282
283    let mut registry = Registry::load()?;
284
285    // Checked before the network is touched: a refusal the configuration already
286    // guarantees should cost nothing and say why.
287    if registry.settings.version_lock {
288        anyhow::bail!("{}", locked_notice(None));
289    }
290
291    if crate::setup::offline_requested() {
292        anyhow::bail!(
293            "{} is set — an install needs the network by definition.",
294            constants::ENV_OFFLINE
295        );
296    }
297
298    // Know before downloading whether there is anything to download. A failed check is
299    // fatal here (unlike `devp update`): running an installer blind would "upgrade" to
300    // the version already installed.
301    let latest = refresh_latest(&mut registry)?;
302    let _ = registry.save();
303    if compare_versions(constants::VERSION, &latest) != Some(Ordering::Less) {
304        output::print_success(&format!(
305            "v{} is already the latest release — nothing to install.",
306            constants::VERSION
307        ));
308        // "Nothing to install" is about the console binaries. The scheduler twin can
309        // still be behind them (the incident: consoles on the current release, devpw
310        // three releases back, every scheduled pass silently running old code), so it
311        // is reconciled even when there is nothing else to do.
312        #[cfg(windows)]
313        {
314            let dirs = default_twin_dirs();
315            let refs: Vec<&Path> = dirs.iter().map(|p| p.as_path()).collect();
316            reconcile_windowless_twins(constants::VERSION, &refs);
317        }
318        return Ok(());
319    }
320    install_latest(&latest)
321}
322
323/// Replace the running binary with release `latest`, already known to be newer.
324///
325/// Shared by `--install` and by the `[y/N]` prompt at the end of plain `devp update`,
326/// which has already fetched `latest` and checked the pin — this does not ask again.
327fn install_latest(latest: &str) -> Result<()> {
328    output::print_info(&format!("Upgrading v{} -> v{latest} …", constants::VERSION));
329
330    let exe = std::env::current_exe().context("could not locate the running binary")?;
331    let managed = crate::setup::managed_exe_path().ok();
332    let channel = Channel::detect_at(&exe, managed.as_deref());
333
334    match install_directly(latest, &exe, managed.as_deref(), channel) {
335        Ok(()) => {
336            output::print_success(&format!("dev-prune v{latest} installed."));
337            report_channel_bookkeeping(channel);
338            output::print_info(
339                "The scheduled pass was not interrupted: it runs the managed copy, which \
340                 was replaced by atomic rename, so a pass already in flight keeps the \
341                 image it loaded and the next one picks up the new binary.",
342            );
343            return Ok(());
344        }
345        Err(e) => output::print_warning(&format!(
346            "Direct download did not work ({e:#}).\nFalling back to the channel that \
347             installed this copy."
348        )),
349    }
350
351    // On Windows a running executable's file is locked against replacement but not
352    // against rename. Moving it aside first lets the channel write a fresh file at the
353    // real path; the `.old` left behind is swept up by the *next* run, when nothing is
354    // executing it any more.
355    #[cfg(windows)]
356    let aside = {
357        let aside = exe.with_extension("exe.old");
358        let _ = fs::remove_file(&aside);
359        fs::rename(&exe, &aside).ok().map(|_| aside)
360    };
361
362    let result = spawn_channel_upgrade(channel);
363
364    #[cfg(windows)]
365    if let Some(aside) = aside {
366        if result.is_ok() {
367            // Best effort: the file is still our running image, so Windows may refuse
368            // the delete. The sweep at the top of the next `--install` gets it then.
369            let _ = fs::remove_file(&aside);
370        } else if !exe.exists() {
371            // The upgrade never wrote a new binary — put the old one back so the
372            // command the user has on PATH still exists.
373            let _ = fs::rename(&aside, &exe);
374        }
375    }
376    result?;
377
378    output::print_success(&format!("dev-prune v{latest} installed."));
379    output::print_info(
380        "The scheduled pass was not interrupted: it runs the managed copy, which \
381         refreshes itself from the new binary on its next run.",
382    );
383    Ok(())
384}
385
386/// Replace every copy of the binary this installation actually runs, from one download.
387///
388/// The managed copy is done first and is the only one whose failure aborts the upgrade:
389/// it is what the scheduler and the git hooks invoke, so a machine with a fresh managed
390/// copy is upgraded even if nothing else could be written.
391///
392/// Every other path is then written from the same verified bytes, and each is written
393/// with the same rename-aside dance rather than through `ensure_alias`. That matters on
394/// Windows: `ensure_alias` deletes the twin before relinking, and the delete fails when
395/// the twin is the running image — which is exactly the case when the user typed `devp
396/// update --install`. Renaming a running executable is allowed where deleting it is not,
397/// so this route leaves no copy behind on the previous release.
398fn install_directly(
399    latest: &str,
400    exe: &Path,
401    managed: Option<&Path>,
402    channel: Channel,
403) -> Result<()> {
404    let bytes = fetch_release_binary(latest)?;
405    let primary = managed.unwrap_or(exe);
406    install_bytes_at(&bytes, primary)?;
407
408    // …except when a package manager owns the directory the running copy sits in and
409    // replaces that directory wholesale on upgrade. Writing new bytes there leaves WinGet,
410    // Scoop or Homebrew certain they still have the old version installed, and the next
411    // `winget upgrade` puts the old binary back over the top. Their copy is left exactly
412    // as the manager wrote it; `report_channel_bookkeeping` names the command that
413    // actually moves it forward. A foreign tree — Volta, mise, Nix, the system package
414    // manager — is the same ownership situation without a resync command to print:
415    // overwriting a shim breaks the shim manager, and writing into `/nix/store` or
416    // `/usr/bin` desyncs a store this tool cannot correct. Its copy is left alone too.
417    let replace_exe_dir = primary != exe && exe.is_file() && !manager_owns_exe_dir(channel);
418    for path in companion_copies(primary, managed.is_some(), exe, replace_exe_dir) {
419        if let Err(e) = install_bytes_at(&bytes, &path) {
420            output::print_warning(&format!(
421                "The managed copy is now v{latest}, but {} could not be replaced ({e:#}). Until it \
422                 is, that copy runs the previous version whenever it is the one invoked.",
423                path.display()
424            ));
425        }
426    }
427
428    // The console binaries are now on `latest`; the scheduler twin must not be left
429    // behind them. It is a separate `[[bin]]` image, so it cannot be written from the
430    // bytes above: it gets its own verified download, and only where a twin already
431    // exists.
432    #[cfg(windows)]
433    {
434        let mut dirs: Vec<&Path> = Vec::new();
435        if let Some(dir) = primary.parent() {
436            dirs.push(dir);
437        }
438        if replace_exe_dir && let Some(dir) = exe.parent() {
439            dirs.push(dir);
440        }
441        reconcile_windowless_twins(latest, &dirs);
442    }
443
444    // Still worth running for the copy `get_exe_path` resolves when that is neither
445    // the managed directory nor the running one. `place_windowless_twin` never
446    // regresses a twin past what sits beside the running binary, so after the
447    // reconcile above this can only move a stale copy forward.
448    crate::daemon::refresh_windowless_twin();
449
450    // The receipt beside the managed copy now names the version that was there a minute
451    // ago. Only ever updated, never created: this path also upgrades a managed copy some
452    // other manager installed, and writing a fresh receipt there would claim one of our
453    // installers ran when none did.
454    crate::receipt::refresh_after_upgrade(latest);
455    Ok(())
456}
457
458/// True when a package manager owns the running copy's directory outright, so no file
459/// in it may be rewritten: the versioned-directory trio, which swaps the whole
460/// directory on upgrade, and a foreign manager's tree — a shim, a Nix store path, a
461/// system package — whose contents dev-prune has no command to resync afterwards.
462fn manager_owns_exe_dir(channel: Channel) -> bool {
463    channel.replaces_its_directory() || matches!(channel, Channel::Foreign(_))
464}
465
466/// Every other file that is a copy of the binary being replaced — both public names,
467/// in both directories that hold one.
468///
469/// Left alone, a copy keeps running the previous release silently, because the
470/// scheduler and the hooks both discard their own output by design. `devp` is a full
471/// second executable rather than a link, so a directory that holds one name usually
472/// holds the other. The managed directory owns both names outright and gets both
473/// written whether they exist yet or not; the running copy's directory belongs to
474/// whatever put the binary there, so only files already present are touched.
475///
476/// Both names, deliberately: through 1.12.0 this list held only the primary's `devp`
477/// twin plus the running file itself, so `devp update --install` typed at a
478/// cargo-installed `devp` upgraded everything except the `dev-prune` sitting beside
479/// it — the exact silent staleness the list exists to prevent.
480fn companion_copies(
481    primary: &Path,
482    primary_is_managed: bool,
483    exe: &Path,
484    replace_exe_dir: bool,
485) -> Vec<PathBuf> {
486    let names: [&str; 2] = if cfg!(windows) {
487        ["dev-prune.exe", "devp.exe"]
488    } else {
489        ["dev-prune", "devp"]
490    };
491    let mut also: Vec<PathBuf> = Vec::new();
492    if let Some(dir) = primary.parent() {
493        for name in names {
494            let twin = dir.join(name);
495            if twin != primary && (primary_is_managed || twin.is_file()) {
496                also.push(twin);
497            }
498        }
499    }
500    if replace_exe_dir {
501        if !also.contains(&exe.to_path_buf()) {
502            also.push(exe.to_path_buf());
503        }
504        if let Some(dir) = exe.parent() {
505            for name in names {
506                let twin = dir.join(name);
507                if twin != *exe && twin != primary && twin.is_file() && !also.contains(&twin) {
508                    also.push(twin);
509                }
510            }
511        }
512    }
513    also
514}
515
516/// Name the channel's own upgrade command after a direct install, for the one thing the
517/// direct route deliberately leaves untouched: the manager's record of what it installed.
518fn report_channel_bookkeeping(channel: Channel) {
519    // A foreign manager's copy was left alone the same way the trio's is, but there is
520    // no resync command to name — dev-prune does not know how to drive Volta, mise or
521    // Nix. Saying nothing here read as "everything was replaced", which is exactly what
522    // did not happen to that copy.
523    if let Channel::Foreign(manager) = channel {
524        output::print_info(&format!(
525            "The managed copy is now v{}. The copy that {manager} installed was left \
526             exactly as it wrote it — replacing a file inside a manager-owned tree only \
527             makes {manager} and the disk disagree. Upgrade that copy through {manager} \
528             itself.",
529            constants::VERSION
530        ));
531        return;
532    }
533    // The installer's copy *is* the managed one, and an unrecognised copy has no manager
534    // keeping a version record that could disagree with the binary.
535    let Some(resync) = channel
536        .owns_its_files()
537        .then(|| channel.upgrade_command())
538        .flatten()
539    else {
540        return;
541    };
542    if channel.replaces_its_directory() {
543        output::print_info(&format!(
544            "The managed copy is now v{}. The copy {} installed was left exactly as it \
545             wrote it — replacing a file inside a versioned package directory only makes \
546             the manager and the disk disagree. Run `{resync}` to move that one forward \
547             too.",
548            constants::VERSION,
549            channel.label()
550        ));
551    } else {
552        output::print_info(&format!(
553            "The binaries are up to date. `{resync}` also updates that manager's own \
554             record of the version, which still reads v{}.",
555            constants::VERSION
556        ));
557    }
558}
559
560/// Download release `version`'s binary for this platform and put it at `target`.
561///
562/// The direct route, and the reason `devp update --install` no longer depends on the
563/// package manager that happened to deliver the first copy. Whatever installed it, the
564/// binary the hooks, the scheduler and `PATH` all point at is one file in the config
565/// directory, and this replaces that file. `uv`, `npm` and `cargo` are delivery
566/// channels; they are not the source of truth, and asking one of them to upgrade a file
567/// living under another one's directory was never going to work.
568///
569/// Refuses to install anything whose SHA-256 does not match the sidecar published beside
570/// it. That check is the entire safety story for this path: the bytes are about to
571/// become the binary the machine runs on a schedule.
572fn fetch_release_binary(version: &str) -> Result<Vec<u8>> {
573    let asset = constants::release_asset_name(version).with_context(|| {
574        format!(
575            "no published binary for {}-{}; upgrade through the channel that installed \
576             this copy instead",
577            std::env::consts::OS,
578            std::env::consts::ARCH
579        )
580    })?;
581    fetch_verified_asset(version, &asset)
582}
583
584/// Download release `version`'s windowless scheduler binary, verified the same way.
585///
586/// Separate from [`fetch_release_binary`] because it is a different asset with its own
587/// sidecar, not a different verification story: the scheduled task runs these bytes
588/// unattended, which is exactly why they get the same checksum gate as the console
589/// binary.
590#[cfg(windows)]
591fn fetch_windowless_binary(version: &str) -> Result<Vec<u8>> {
592    let asset = constants::windowless_release_asset_name(version).with_context(|| {
593        format!(
594            "no published windowless binary for windows-{}",
595            std::env::consts::ARCH
596        )
597    })?;
598    fetch_verified_asset(version, &asset)
599}
600
601/// The shared tail of every asset download: fetch the `.sha256` sidecar, fetch the
602/// bytes, and refuse them unless the digests match.
603fn fetch_verified_asset(version: &str, asset: &str) -> Result<Vec<u8>> {
604    let base = format!("{}/v{version}/{asset}", constants::RELEASE_DOWNLOAD_BASE);
605
606    let expected = fetch_expected_hash(&format!("{base}.sha256"))?;
607    output::print_info(&format!("Downloading {asset} …"));
608    let bytes = fetch_bytes(&base)?;
609
610    let actual = {
611        use sha2::{Digest, Sha256};
612        use std::fmt::Write as _;
613        let mut h = Sha256::new();
614        h.update(&bytes);
615        // Hex-encoded by hand: sha2 0.11 returns a `hybrid_array::Array`, which has no
616        // `LowerHex`, and the sidecar is lower-case hex either way.
617        h.finalize().iter().fold(String::new(), |mut s, b| {
618            let _ = write!(s, "{b:02x}");
619            s
620        })
621    };
622    if actual != expected {
623        anyhow::bail!(
624            "checksum mismatch for {asset}\n  expected {expected}\n  got      {actual}\n\
625             The download was corrupted or tampered with; nothing was installed."
626        );
627    }
628
629    Ok(bytes)
630}
631
632/// What one reconcile pass over the windowless twins amounted to.
633#[cfg(windows)]
634enum TwinReconcile {
635    /// Every twin that exists is already on `version`, or none exists at all.
636    UpToDate,
637    /// At least one stale twin was replaced and none failed.
638    Updated,
639    /// The download or a write failed; the message has already been printed.
640    Failed(String),
641}
642
643/// Bring every existing `devpw.exe` in `dirs` up to release `version`.
644///
645/// The upgrade path used to refresh the twin from the devpw *beside* the running
646/// binary, but that copy is the previous delivery's: the console binaries moved forward
647/// and the scheduled task kept running the old release, silently, every night. So the
648/// twin is reconciled against the release itself: any copy whose build stamp is older
649/// than `version`, or missing entirely (built before 1.17.0), is replaced from a
650/// checksum-verified download of the published `devpw` asset.
651///
652/// Only existing files are touched. An installation that never had a windowless twin
653/// does not grow one here; `devp daemon install` is what creates one, and the npm
654/// family ships its own under a different mechanism.
655#[cfg(windows)]
656fn reconcile_windowless_twins(version: &str, dirs: &[&Path]) -> TwinReconcile {
657    let mut stale: Vec<PathBuf> = Vec::new();
658    for dir in dirs {
659        let twin = dir.join(constants::WINDOWS_WINDOWLESS_BIN);
660        if stale.contains(&twin) || !twin.is_file() {
661            continue;
662        }
663        let stamped = fs::read(&twin)
664            .ok()
665            .and_then(|b| crate::commands::trust::version_from_stamp(&b));
666        // Equal or newer is left alone; the twin never regresses. A copy with no
667        // readable stamp predates stamping and is by definition behind.
668        let up_to_date = stamped
669            .as_deref()
670            .and_then(|v| compare_versions(v, version))
671            .is_some_and(|o| o != Ordering::Less);
672        if !up_to_date {
673            stale.push(twin);
674        }
675    }
676    if stale.is_empty() {
677        return TwinReconcile::UpToDate;
678    }
679
680    let bytes = match fetch_windowless_binary(version) {
681        Ok(b) => b,
682        Err(e) => {
683            let why = format!(
684                "The scheduler binary {} could not be downloaded ({e:#}). The scheduled \
685                 pass keeps running the version it has until `devp update --install` or \
686                 `devp doctor --fix` succeeds.",
687                constants::WINDOWS_WINDOWLESS_BIN
688            );
689            output::print_warning(&why);
690            return TwinReconcile::Failed(why);
691        }
692    };
693
694    let mut failed = None;
695    for twin in &stale {
696        if let Err(e) = install_bytes_at(&bytes, twin) {
697            let why = format!(
698                "{} could not be replaced ({e:#}); the scheduled pass keeps running the \
699                 previous version from that copy.",
700                twin.display()
701            );
702            output::print_warning(&why);
703            failed = Some(why);
704        }
705    }
706    match failed {
707        Some(why) => TwinReconcile::Failed(why),
708        None => {
709            output::print_success(&format!(
710                "Scheduler binary {} updated to v{version}.",
711                constants::WINDOWS_WINDOWLESS_BIN
712            ));
713            TwinReconcile::Updated
714        }
715    }
716}
717
718/// The directories whose windowless twin this installation actually runs: the managed
719/// directory the scheduler points at, and the directory of the running binary.
720#[cfg(windows)]
721fn default_twin_dirs() -> Vec<PathBuf> {
722    let mut dirs = Vec::new();
723    if let Ok(managed) = crate::setup::managed_exe_path()
724        && let Some(dir) = managed.parent()
725    {
726        dirs.push(dir.to_path_buf());
727    }
728    if let Ok(exe) = std::env::current_exe()
729        && let Some(dir) = exe.parent()
730    {
731        dirs.push(dir.to_path_buf());
732    }
733    dirs
734}
735
736/// The `devp doctor --fix` entry point for a stale scheduler binary: same reconcile,
737/// same guardrails, reported through the repair machinery instead of the upgrade
738/// transcript.
739pub(crate) fn repair_windowless_twins() -> crate::setup::Outcome {
740    #[cfg(windows)]
741    {
742        use crate::setup::Outcome;
743        match Registry::load() {
744            Ok(r) if r.settings.version_lock => {
745                return Outcome::Skipped(
746                    "version_lock is set, and nothing dev-prune does replaces a binary \
747                     while it is"
748                        .to_string(),
749                );
750            }
751            _ => {}
752        }
753        if crate::setup::offline_requested() {
754            return Outcome::Skipped(format!(
755                "{} is set and this repair needs a download",
756                constants::ENV_OFFLINE
757            ));
758        }
759        // A development build carries whatever version Cargo.toml says next, and the
760        // release asset for that version does not exist until the tag is pushed, so
761        // reconciling from `target/` would report a spurious download failure once per
762        // version bump. The real install heals itself; a dev build stands down.
763        if std::env::current_exe().is_ok_and(|exe| crate::commands::uninstall::is_dev_build(&exe)) {
764            return Outcome::Skipped(
765                "this is a development build, and its version may not be released yet".to_string(),
766            );
767        }
768        let dirs = default_twin_dirs();
769        let refs: Vec<&Path> = dirs.iter().map(|p| p.as_path()).collect();
770        match reconcile_windowless_twins(constants::VERSION, &refs) {
771            TwinReconcile::UpToDate => Outcome::AlreadyPresent,
772            TwinReconcile::Updated => Outcome::Installed,
773            TwinReconcile::Failed(why) => Outcome::Failed(why),
774        }
775    }
776    #[cfg(not(windows))]
777    {
778        // The finding this repairs is only ever raised on Windows; this arm exists for
779        // the compiler, not for a caller.
780        crate::setup::Outcome::AlreadyPresent
781    }
782}
783
784/// Write already-verified bytes over one binary.
785///
786/// Separate from the download so a single transfer can serve every copy that has to be
787/// replaced — the managed binary, its `devp` twin, and whatever the user is running —
788/// instead of fetching the same megabytes once per path.
789fn install_bytes_at(bytes: &[u8], target: &Path) -> Result<()> {
790    // Staged beside the target and renamed in, so a write that dies half-way leaves the
791    // working binary untouched rather than a truncated file where the scheduler expects
792    // an executable.
793    let staging = target.with_extension("new");
794    if let Some(parent) = target.parent() {
795        fs::create_dir_all(parent).ok();
796    }
797    if let Err(e) = fs::write(&staging, bytes) {
798        // A write that dies part-way (disk full, permissions revoked mid-stream) leaves
799        // a truncated `.new` beside the binary forever — nothing else ever looks at it.
800        let _ = fs::remove_file(&staging);
801        return Err(e).with_context(|| format!("could not write {}", staging.display()));
802    }
803
804    #[cfg(unix)]
805    {
806        use std::os::unix::fs::PermissionsExt;
807        // Downloaded files are 0644; the scheduler needs to be able to run this.
808        let _ = fs::set_permissions(&staging, fs::Permissions::from_mode(0o755));
809    }
810
811    replace_binary(&staging, target)
812}
813
814/// Read the hash out of a `.sha256` sidecar published beside a release asset.
815fn fetch_expected_hash(url: &str) -> Result<String> {
816    let body = String::from_utf8(fetch_bytes(url)?).context("the checksum sidecar was not text")?;
817    parse_sha256_sidecar(&body)
818}
819
820/// The parsing half of [`fetch_expected_hash`], which is `sha256sum` format: the hex
821/// digest, two spaces, the file name.
822///
823/// Validated rather than trusted, because the failure this guards against is not a
824/// malformed checksum — it is a 404 page or a proxy error blob arriving where the sidecar
825/// should be. Comparing a digest against `<!DOCTYPE html>` would report a checksum
826/// mismatch, which reads as "someone tampered with the download" and sends the user
827/// somewhere alarming and wrong.
828fn parse_sha256_sidecar(body: &str) -> Result<String> {
829    let hash = body
830        .split_whitespace()
831        .next()
832        .context("the checksum sidecar was empty")?
833        .to_ascii_lowercase();
834    if hash.len() != 64 || !hash.bytes().all(|b| b.is_ascii_hexdigit()) {
835        anyhow::bail!("the checksum sidecar did not contain a SHA-256 digest");
836    }
837    Ok(hash)
838}
839
840fn fetch_bytes(url: &str) -> Result<Vec<u8>> {
841    let mut body = ureq::get(url)
842        .header("User-Agent", &format!("dev-prune/{}", constants::VERSION))
843        .config()
844        .timeout_global(Some(Duration::from_secs(
845            constants::UPDATE_DOWNLOAD_TIMEOUT_SECS,
846        )))
847        .build()
848        .call()
849        .with_context(|| format!("could not download {url}"))?;
850    let mut buf = Vec::new();
851    body.body_mut()
852        .as_reader()
853        .read_to_end(&mut buf)
854        .with_context(|| format!("could not read {url}"))?;
855    Ok(buf)
856}
857
858/// Move `staged` onto `target`, working around the one platform that will not overwrite
859/// a file it is executing.
860fn replace_binary(staged: &Path, target: &Path) -> Result<()> {
861    // On Windows a running image is locked against replacement but not against rename,
862    // so the live file steps aside and the new one takes its name. The `.old` is swept
863    // by the next run, when nothing holds it open any more.
864    #[cfg(windows)]
865    let aside = {
866        let aside = target.with_extension("exe.old");
867        let _ = fs::remove_file(&aside);
868        target
869            .exists()
870            .then(|| fs::rename(target, &aside).ok().map(|_| aside))
871            .flatten()
872    };
873
874    match fs::rename(staged, target) {
875        Ok(()) => {
876            #[cfg(windows)]
877            if let Some(aside) = aside {
878                let _ = fs::remove_file(&aside);
879            }
880            Ok(())
881        }
882        Err(e) => {
883            let _ = fs::remove_file(staged);
884            #[cfg(windows)]
885            if let Some(aside) = aside
886                && !target.exists()
887            {
888                // Put the working binary back rather than leaving the machine with no
889                // `dev-prune` at all.
890                let _ = fs::rename(&aside, target);
891            }
892            Err(e).with_context(|| format!("could not install {}", target.display()))
893        }
894    }
895}
896
897/// Run one channel's own upgrade command, wired to the terminal so its progress and
898/// prompts reach the user directly.
899fn spawn_channel_upgrade(channel: Channel) -> Result<()> {
900    let Some(argv) = channel.upgrade_argv() else {
901        output::print_warning(
902            "Could not tell which channel installed this binary, so nothing was \
903             changed. Upgrade it yourself with one of:",
904        );
905        print_upgrade_commands();
906        anyhow::bail!("unrecognised install channel");
907    };
908
909    output::print_info(&format!("Running: {}", argv.join(" ")));
910    let status = crate::spawn::command(crate::adapters::resolve_program(&argv[0]))
911        .args(&argv[1..])
912        .status()
913        .with_context(|| format!("could not start `{}`", argv[0]))?;
914    if !status.success() {
915        anyhow::bail!("`{}` exited with {status}", argv.join(" "));
916    }
917    Ok(())
918}
919
920/// The end-of-run hook behind `auto_update`: when the setting is on and the last release
921/// check already knows a newer version exists, replace the binary without being asked.
922///
923/// Warn-never-fail, like everything else that runs as a side effect of `devp run` — a
924/// broken upgrade path must not turn a successful prune into a failed command.
925///
926/// Deliberately *not* `run_install`. That function falls back to running the package
927/// manager that installed this copy, and this is the path that runs unattended: from the
928/// scheduled pass, from a git hook, from `devp run` in the middle of someone else's
929/// work. Spawning `winget upgrade` there can raise an elevation prompt and can pull in
930/// upgrades nobody asked about. Download-and-replace is safe unattended; handing the
931/// machine to a package manager is a decision, and decisions stay with the person.
932pub fn maybe_auto_update(registry: &Registry) {
933    if !registry.settings.auto_update
934        || crate::setup::offline_requested()
935        || crate::setup::no_auto_setup_requested()
936    {
937        return;
938    }
939    let Some(latest) = registry.latest_known_version.as_deref() else {
940        return;
941    };
942    if compare_versions(constants::VERSION, latest) != Some(Ordering::Less) {
943        return;
944    }
945
946    // Announced here rather than at the top of the function, so the line appears on
947    // exactly the runs where the pin changed the outcome. A pass with nothing to
948    // install stays as silent as it has always been.
949    if registry.settings.version_lock {
950        println!();
951        output::print_info(&locked_notice(Some(latest)));
952        return;
953    }
954
955    let Ok(exe) = std::env::current_exe() else {
956        return;
957    };
958    let managed = crate::setup::managed_exe_path().ok();
959    let channel = Channel::detect_at(&exe, managed.as_deref());
960
961    // WinGet, Scoop and Homebrew swap their whole package directory on upgrade, so bytes
962    // written there are undone by the next `winget upgrade` — which would still believe
963    // the old version is installed. Those channels own the upgrade, and
964    // `notify_if_outdated` has already printed the line naming the right command.
965    if channel.replaces_its_directory() {
966        return;
967    }
968
969    // A foreign tree — Volta, mise, Nix, the system package manager — is owned the same
970    // way, but there is no resync command to have printed. With a managed copy present
971    // the pass below keeps that copy fresh and `install_directly` leaves the manager's
972    // file alone; without one, the only writable target *is* the manager's file, and an
973    // unattended pass must not desync a tree it has no way to correct.
974    if matches!(channel, Channel::Foreign(_)) && managed.is_none() {
975        return;
976    }
977
978    println!();
979    output::print_info(&format!(
980        "Updating dev-prune v{} -> v{latest} …",
981        constants::VERSION
982    ));
983    match install_directly(latest, &exe, managed.as_deref(), channel) {
984        Ok(()) => {
985            output::print_success(&format!("dev-prune v{latest} installed."));
986            report_channel_bookkeeping(channel);
987        }
988        Err(e) => output::print_warning(&format!(
989            "Automatic update failed ({e:#}). Run `devp update --install` yourself, or \
990             `devp config set auto_update false` to stop trying."
991        )),
992    }
993}
994
995/// Quietly keep the release check current and print a one-line notice when the installed
996/// build is behind. Returns `true` when the registry changed and needs saving.
997///
998/// Called from `devp run` and `devp status`. Never returns an error: a background
999/// convenience must not be able to fail the command the user actually asked for.
1000pub fn notify_if_outdated(registry: &mut Registry) -> bool {
1001    if !registry.settings.update_check {
1002        return false;
1003    }
1004
1005    let due = check_due(registry);
1006
1007    if due {
1008        // The result is deliberately ignored: `refresh_latest` moves the timestamp even
1009        // when the request fails, and retrying on every command while the machine is
1010        // offline would put a five-second stall in front of everyday work.
1011        let _ = refresh_latest(registry);
1012    }
1013
1014    if let Some(latest) = registry.latest_known_version.as_deref()
1015        && compare_versions(constants::VERSION, latest) == Some(Ordering::Less)
1016    {
1017        if registry.settings.version_lock {
1018            output::print_info(&locked_notice(Some(latest)));
1019        } else {
1020            output::print_info(&format!(
1021                "dev-prune v{latest} is out (you have v{}). `devp update` has the commands; \
1022                 `devp config set update_check false` silences this.",
1023                constants::VERSION
1024            ));
1025        }
1026    }
1027
1028    due
1029}
1030
1031/// Whether the periodic release check should run now.
1032///
1033/// A check that failed gets one day, not the whole interval: the timestamp still
1034/// advances on failure (so an offline machine is not stalled on every command), but the
1035/// version it froze is stale, and `maybe_auto_update` trusts nothing else — one
1036/// timed-out request otherwise silences the auto-update for a week.
1037fn check_due(registry: &Registry) -> bool {
1038    let interval = if registry.last_update_check_failed {
1039        registry.settings.update_check_interval_days.min(1)
1040    } else {
1041        registry.settings.update_check_interval_days
1042    };
1043    registry
1044        .last_update_check
1045        .is_none_or(|last| Utc::now().signed_duration_since(last).num_days() >= interval)
1046}
1047
1048/// Ask GitHub for the latest release and record the answer on the registry.
1049///
1050/// The caller is responsible for saving; that keeps this usable from both the
1051/// already-loaded-registry path and the standalone command.
1052fn refresh_latest(registry: &mut Registry) -> Result<String> {
1053    let result = latest_release(registry.settings.update_check_timeout_secs);
1054    registry.last_update_check = Some(Utc::now());
1055    registry.last_update_check_failed = result.is_err();
1056    let latest = result?;
1057    registry.latest_known_version = Some(latest.clone());
1058    Ok(latest)
1059}
1060
1061/// Say whether the installed build is behind, current, or ahead of the latest release.
1062fn report_comparison(latest: &str) {
1063    let installed = constants::VERSION;
1064    match compare_versions(installed, latest) {
1065        Some(Ordering::Less) => {
1066            output::print_warning(&format!(
1067                "Latest release:    v{latest} — an upgrade is available."
1068            ));
1069        }
1070        Some(Ordering::Equal) => {
1071            output::print_success(&format!(
1072                "Latest release:    v{latest} — you are up to date."
1073            ));
1074        }
1075        Some(Ordering::Greater) => {
1076            // Normal when running a local build between releases.
1077            output::print_info(&format!(
1078                "Latest release:    v{latest} — your build is newer than the last published one."
1079            ));
1080        }
1081        None => {
1082            output::print_info(&format!(
1083                "Latest release:    v{latest} (could not compare it to v{installed})."
1084            ));
1085        }
1086    }
1087}
1088
1089/// Fetch the tag name of the most recent published release.
1090///
1091/// Returns the version without any leading `v`, so it can be compared to
1092/// `CARGO_PKG_VERSION` directly.
1093///
1094/// The tag comes from the `Location` of the redirect GitHub answers with, not from a
1095/// page body: `max_redirects(0)` hands the `302` back as an ordinary response, so the
1096/// release page itself is never downloaded and no API quota is spent (see
1097/// [`constants::LATEST_RELEASE_URL`]).
1098fn latest_release(timeout_secs: u64) -> Result<String> {
1099    if crate::setup::offline_requested() {
1100        anyhow::bail!("{} is set", constants::ENV_OFFLINE);
1101    }
1102    let response = ureq::get(constants::LATEST_RELEASE_URL)
1103        .header("User-Agent", &format!("dev-prune/{}", constants::VERSION))
1104        .config()
1105        .timeout_global(Some(Duration::from_secs(timeout_secs.max(1))))
1106        .max_redirects(0)
1107        .build()
1108        .call()
1109        .context("request failed")?;
1110
1111    let location = response
1112        .headers()
1113        .get("location")
1114        .and_then(|v| v.to_str().ok())
1115        .context("GitHub did not answer with a redirect")?;
1116
1117    version_from_release_location(location).context("GitHub did not redirect to a release tag")
1118}
1119
1120/// Read the version out of the URL GitHub redirects `releases/latest` to.
1121///
1122/// A repository with no releases answers with the releases page itself, and a renamed
1123/// repository answers with the new repository's `releases/latest`; neither carries a tag
1124/// and neither must be mistaken for one.
1125fn version_from_release_location(location: &str) -> Option<String> {
1126    let tag = location
1127        .split_once("/releases/tag/")?
1128        .1
1129        .trim_end_matches('/');
1130    if tag.is_empty() || tag.contains('/') {
1131        return None;
1132    }
1133    Some(tag.trim_start_matches('v').to_string())
1134}
1135
1136/// Compare two dotted numeric versions, ignoring any pre-release suffix.
1137///
1138/// Returns `None` when either side is not `major.minor.patch` — better to say "could not
1139/// compare" than to claim an upgrade exists because `1.0.0` sorts before `1.0.0-rc.1`
1140/// as a string.
1141pub(crate) fn compare_versions(a: &str, b: &str) -> Option<Ordering> {
1142    let parse = |v: &str| -> Option<[u64; 3]> {
1143        let core = v.split(['-', '+']).next()?;
1144        let mut parts = core.split('.');
1145        let out = [
1146            parts.next()?.parse().ok()?,
1147            parts.next()?.parse().ok()?,
1148            parts.next()?.parse().ok()?,
1149        ];
1150        // A fourth component means this is not the scheme we release under.
1151        if parts.next().is_some() {
1152            return None;
1153        }
1154        Some(out)
1155    };
1156    Some(parse(a)?.cmp(&parse(b)?))
1157}
1158
1159#[cfg(test)]
1160mod tests {
1161    use super::*;
1162    use chrono::Duration as ChronoDuration;
1163
1164    #[test]
1165    fn the_tag_is_read_out_of_the_redirect_and_its_v_is_dropped() {
1166        assert_eq!(
1167            version_from_release_location(
1168                "https://github.com/Life-Experimentalist/dev-prune/releases/tag/v1.18.0"
1169            )
1170            .as_deref(),
1171            Some("1.18.0")
1172        );
1173        assert_eq!(
1174            version_from_release_location("/Life-Experimentalist/dev-prune/releases/tag/1.2.3/")
1175                .as_deref(),
1176            Some("1.2.3")
1177        );
1178    }
1179
1180    #[test]
1181    fn a_redirect_that_is_not_a_release_tag_is_not_a_version() {
1182        // No releases yet: GitHub sends the releases page itself.
1183        assert_eq!(
1184            version_from_release_location(
1185                "https://github.com/Life-Experimentalist/dev-prune/releases"
1186            ),
1187            None
1188        );
1189        // A renamed repository redirects to the new name's `releases/latest`.
1190        assert_eq!(
1191            version_from_release_location("https://github.com/other/name/releases/latest"),
1192            None
1193        );
1194        assert_eq!(
1195            version_from_release_location("https://github.com/o/r/releases/tag/"),
1196            None
1197        );
1198        assert_eq!(
1199            version_from_release_location("https://github.com/o/r/releases/tag/v1/extra"),
1200            None
1201        );
1202    }
1203
1204    #[test]
1205    fn orders_by_component_not_lexically() {
1206        // "1.10.0" < "1.9.0" as strings, which is the bug this function exists to avoid.
1207        assert_eq!(compare_versions("1.9.0", "1.10.0"), Some(Ordering::Less));
1208        assert_eq!(compare_versions("1.0.0", "1.0.0"), Some(Ordering::Equal));
1209        assert_eq!(
1210            compare_versions("2.0.0", "1.99.99"),
1211            Some(Ordering::Greater)
1212        );
1213    }
1214
1215    #[test]
1216    fn pre_release_suffixes_compare_by_their_core() {
1217        assert_eq!(
1218            compare_versions("1.0.0", "1.0.0-rc.1"),
1219            Some(Ordering::Equal)
1220        );
1221        assert_eq!(
1222            compare_versions("1.0.0+build7", "1.0.1"),
1223            Some(Ordering::Less)
1224        );
1225    }
1226
1227    #[test]
1228    fn unparseable_versions_report_no_answer_rather_than_a_wrong_one() {
1229        assert_eq!(compare_versions("1.0", "1.0.0"), None);
1230        assert_eq!(compare_versions("1.0.0.1", "1.0.0"), None);
1231        assert_eq!(compare_versions("nightly", "1.0.0"), None);
1232    }
1233
1234    fn bin_names() -> [&'static str; 2] {
1235        if cfg!(windows) {
1236            ["dev-prune.exe", "devp.exe"]
1237        } else {
1238            ["dev-prune", "devp"]
1239        }
1240    }
1241
1242    #[test]
1243    fn an_update_reaches_the_twin_beside_the_copy_the_user_typed() {
1244        // The 1.12.0 bug: `devp update --install` typed at a cargo-installed `devp`
1245        // upgraded the managed pair and the running file, and left the `dev-prune`
1246        // beside it on the previous release.
1247        let [prune, devp] = bin_names();
1248        let managed_dir = tempfile::tempdir().unwrap();
1249        let cargo_dir = tempfile::tempdir().unwrap();
1250        let primary = managed_dir.path().join(prune);
1251        let exe = cargo_dir.path().join(devp);
1252        std::fs::write(&exe, b"old").unwrap();
1253        std::fs::write(cargo_dir.path().join(prune), b"old").unwrap();
1254
1255        let also = companion_copies(&primary, true, &exe, true);
1256        assert!(also.contains(&managed_dir.path().join(devp)));
1257        assert!(also.contains(&exe));
1258        assert!(also.contains(&cargo_dir.path().join(prune)));
1259        assert!(!also.contains(&primary));
1260    }
1261
1262    #[test]
1263    fn a_name_that_does_not_exist_outside_the_managed_directory_is_not_invented() {
1264        // The managed directory owns both names; the running copy's directory belongs
1265        // to whatever installed it, so a missing twin there stays missing.
1266        let [prune, devp] = bin_names();
1267        let managed_dir = tempfile::tempdir().unwrap();
1268        let solo_dir = tempfile::tempdir().unwrap();
1269        let primary = managed_dir.path().join(prune);
1270        let exe = solo_dir.path().join(devp);
1271        std::fs::write(&exe, b"old").unwrap();
1272
1273        let also = companion_copies(&primary, true, &exe, true);
1274        assert!(also.contains(&managed_dir.path().join(devp)));
1275        assert!(also.contains(&exe));
1276        assert!(!also.contains(&solo_dir.path().join(prune)));
1277    }
1278
1279    #[test]
1280    fn a_manager_owned_directory_is_left_exactly_as_the_manager_wrote_it() {
1281        // replace_exe_dir is false for WinGet/Scoop/Homebrew copies; nothing in the
1282        // running copy's directory may be rewritten, twins included.
1283        let [prune, devp] = bin_names();
1284        let managed_dir = tempfile::tempdir().unwrap();
1285        let store_dir = tempfile::tempdir().unwrap();
1286        let primary = managed_dir.path().join(prune);
1287        let exe = store_dir.path().join(devp);
1288        std::fs::write(&exe, b"old").unwrap();
1289        std::fs::write(store_dir.path().join(prune), b"old").unwrap();
1290
1291        let also = companion_copies(&primary, true, &exe, false);
1292        assert_eq!(also, vec![managed_dir.path().join(devp)]);
1293    }
1294
1295    #[test]
1296    fn exactly_the_manager_owned_channels_keep_their_directory_untouched() {
1297        // The trio because the next `winget upgrade` would undo the write anyway, and
1298        // Foreign because overwriting a shim or a store path desyncs a manager this
1299        // tool has no resync command for. Everything else is a plain file the direct
1300        // route may replace.
1301        for owned in [
1302            Channel::WinGet,
1303            Channel::Scoop,
1304            Channel::Homebrew,
1305            Channel::Foreign("Volta"),
1306            Channel::Foreign("the system package manager"),
1307        ] {
1308            assert!(manager_owns_exe_dir(owned), "{owned:?}");
1309        }
1310        for replaceable in [
1311            Channel::Installer,
1312            Channel::Cargo,
1313            Channel::Npm,
1314            Channel::Bun,
1315            Channel::Pnpm,
1316            Channel::Yarn,
1317            Channel::UvTool,
1318            Channel::Pipx,
1319            Channel::Pip,
1320            Channel::Unknown,
1321        ] {
1322            assert!(!manager_owns_exe_dir(replaceable), "{replaceable:?}");
1323        }
1324    }
1325
1326    #[test]
1327    fn without_a_managed_copy_only_existing_files_beside_the_binary_are_replaced() {
1328        let [prune, devp] = bin_names();
1329        let dir = tempfile::tempdir().unwrap();
1330        let primary = dir.path().join(devp);
1331        std::fs::write(&primary, b"old").unwrap();
1332
1333        // Alone, its absent `dev-prune` twin is not created…
1334        assert!(companion_copies(&primary, false, &primary, false).is_empty());
1335
1336        // …but a twin that exists is stale the moment the primary is replaced.
1337        std::fs::write(dir.path().join(prune), b"old").unwrap();
1338        assert_eq!(
1339            companion_copies(&primary, false, &primary, false),
1340            vec![dir.path().join(prune)]
1341        );
1342    }
1343
1344    #[test]
1345    fn the_check_is_on_unless_the_user_turns_it_off() {
1346        assert!(Registry::default().settings.update_check);
1347    }
1348
1349    #[test]
1350    fn a_disabled_check_touches_neither_the_network_nor_the_registry() {
1351        let mut registry = Registry::default();
1352        registry.settings.update_check = false;
1353        assert!(!notify_if_outdated(&mut registry));
1354        assert!(registry.last_update_check.is_none());
1355    }
1356
1357    #[test]
1358    fn auto_update_is_on_by_default_and_silent_with_nothing_to_install() {
1359        let registry = Registry::default();
1360        assert!(registry.settings.auto_update);
1361        // No release check has run, so `latest_known_version` is unset and this must
1362        // return without touching the network or the terminal. The default being *on* is
1363        // what makes that early return load-bearing rather than incidental.
1364        assert!(registry.latest_known_version.is_none());
1365        maybe_auto_update(&registry);
1366    }
1367
1368    #[test]
1369    fn the_pin_is_off_until_somebody_asks_for_it() {
1370        // Every other path in this file is written on the assumption that the pin costs
1371        // nothing when nobody has set it, so the default is the part worth asserting.
1372        assert!(!Registry::default().settings.version_lock);
1373    }
1374
1375    #[test]
1376    fn the_refusal_names_the_version_it_is_holding_and_the_way_out() {
1377        // Both halves matter. A refusal that does not say which version it is holding
1378        // cannot be audited, and one that does not say how to release it is
1379        // indistinguishable, to the person reading it, from an update path that broke.
1380        let notice = locked_notice(None);
1381        assert!(notice.contains(constants::VERSION), "{notice}");
1382        assert!(
1383            notice.contains("devp config set version_lock false"),
1384            "{notice}"
1385        );
1386        assert!(!notice.contains("is out"), "{notice}");
1387    }
1388
1389    #[test]
1390    fn a_known_release_is_named_in_the_refusal_that_withholds_it() {
1391        // "You are pinned" and "there is a 2.0.0 out that you are not getting" are
1392        // different facts, and the second is the one that makes somebody go and look at
1393        // the setting.
1394        let notice = locked_notice(Some("2.0.0"));
1395        assert!(notice.contains("v2.0.0 is out"), "{notice}");
1396        assert!(notice.contains(constants::VERSION), "{notice}");
1397    }
1398
1399    #[test]
1400    fn a_failed_check_is_retried_after_a_day_not_a_whole_interval() {
1401        let mut registry = Registry {
1402            last_update_check: Some(Utc::now() - ChronoDuration::days(2)),
1403            last_update_check_failed: true,
1404            ..Default::default()
1405        };
1406        // Two days since a *failed* check: due again, well inside the ordinary
1407        // interval. This is the incident: a timed-out check froze the known version at
1408        // 1.14.0, and the next scheduled pass — a day and a half later — trusted it.
1409        assert!(check_due(&registry));
1410        registry.last_update_check_failed = false;
1411        assert!(
1412            !check_due(&registry),
1413            "a successful check keeps the interval"
1414        );
1415    }
1416
1417    #[test]
1418    fn even_a_failed_check_is_not_retried_within_the_day() {
1419        // The failure backoff must not reintroduce the per-command stall it was
1420        // written around: offline for an afternoon means one timeout, not one per
1421        // command.
1422        let registry = Registry {
1423            last_update_check: Some(Utc::now() - ChronoDuration::hours(2)),
1424            last_update_check_failed: true,
1425            ..Default::default()
1426        };
1427        assert!(!check_due(&registry));
1428    }
1429
1430    #[test]
1431    fn a_recent_check_is_not_repeated() {
1432        let mut registry = Registry::default();
1433        let stamp = Utc::now() - ChronoDuration::days(constants::UPDATE_CHECK_INTERVAL_DAYS - 1);
1434        registry.last_update_check = Some(stamp);
1435        // No network call, so the stamp survives untouched and nothing needs saving.
1436        assert!(!notify_if_outdated(&mut registry));
1437        assert_eq!(registry.last_update_check, Some(stamp));
1438    }
1439
1440    #[test]
1441    fn the_asset_name_matches_what_the_release_workflow_builds() {
1442        // This string is a contract with `.github/workflows/release.yml`. Getting it
1443        // wrong is not a compile error and not a test failure anywhere else — it is a
1444        // self-update that 404s for every user on the day of a release.
1445        let name = constants::release_asset_name("1.4.0");
1446        let expected = match (std::env::consts::OS, std::env::consts::ARCH) {
1447            ("windows", "x86_64") => Some("dev-prune-v1.4.0-windows-x64.exe"),
1448            ("windows", "aarch64") => Some("dev-prune-v1.4.0-windows-arm64.exe"),
1449            ("windows", "x86") => Some("dev-prune-v1.4.0-windows-x86.exe"),
1450            ("linux", "x86_64") => Some("dev-prune-v1.4.0-linux-x64"),
1451            ("linux", "aarch64") => Some("dev-prune-v1.4.0-linux-arm64"),
1452            ("macos", "x86_64") => Some("dev-prune-v1.4.0-darwin-x64"),
1453            ("macos", "aarch64") => Some("dev-prune-v1.4.0-darwin-arm64"),
1454            // A platform the release does not build for must decline the direct route
1455            // rather than download some other platform's binary.
1456            _ => None,
1457        };
1458        assert_eq!(name.as_deref(), expected);
1459    }
1460
1461    #[cfg(windows)]
1462    #[test]
1463    fn the_windowless_asset_name_matches_what_the_release_workflow_builds() {
1464        // Same contract, same failure mode as the console asset above: a wrong name is
1465        // a scheduler binary that 404s on the day of a release and stays stale.
1466        let name = constants::windowless_release_asset_name("1.4.0");
1467        let expected = match std::env::consts::ARCH {
1468            "x86_64" => Some("devpw-v1.4.0-windows-x64.exe"),
1469            "aarch64" => Some("devpw-v1.4.0-windows-arm64.exe"),
1470            "x86" => Some("devpw-v1.4.0-windows-x86.exe"),
1471            _ => None,
1472        };
1473        assert_eq!(name.as_deref(), expected);
1474    }
1475
1476    #[test]
1477    fn only_windows_has_a_32_bit_asset() {
1478        // The matrix builds `x86` for Windows alone. On a 32-bit Linux there is nothing
1479        // to download, and guessing `x64` would install a binary that cannot run.
1480        let name = constants::release_asset_name("9.9.9");
1481        if std::env::consts::ARCH == "x86" {
1482            assert_eq!(name.is_some(), std::env::consts::OS == "windows");
1483        }
1484    }
1485
1486    #[test]
1487    fn a_sidecar_is_read_as_the_first_field_of_sha256sum_format() {
1488        let digest = "a".repeat(64);
1489        assert_eq!(
1490            parse_sha256_sidecar(&format!("{digest}  dev-prune-v1.4.0-linux-x64\n")).unwrap(),
1491            digest
1492        );
1493        // The Windows step writes it with no trailing newline, and GitHub may serve
1494        // either line ending.
1495        assert_eq!(
1496            parse_sha256_sidecar(&format!("{digest}  asset.exe")).unwrap(),
1497            digest
1498        );
1499        assert_eq!(
1500            parse_sha256_sidecar(&format!("{}  asset\r\n", digest.to_uppercase())).unwrap(),
1501            digest,
1502            "an upper-case digest must compare equal to the one we compute"
1503        );
1504    }
1505
1506    #[test]
1507    fn anything_that_is_not_a_digest_is_refused_before_it_is_compared() {
1508        // A 404 page, an error blob or a truncated read must fail as "not a digest"
1509        // rather than as a mismatch — the two send the user to very different places.
1510        for bad in [
1511            "",
1512            "   ",
1513            "<!DOCTYPE html>",
1514            "not-a-hash  asset",
1515            &"a".repeat(63),
1516            &"a".repeat(65),
1517            &format!("{}g  asset", "a".repeat(63)),
1518        ] {
1519            assert!(
1520                parse_sha256_sidecar(bad).is_err(),
1521                "{bad:?} must not be accepted as a digest"
1522            );
1523        }
1524    }
1525}