Skip to content

The agent that grows with you

fix(bootstrap-installer): never reuse a downloaded install script

ethernet274b412aa748
diff --git a/apps/bootstrap-installer/src-tauri/src/bootstrap.rs b/apps/bootstrap-installer/src-tauri/src/bootstrap.rs
index 8d6d81cb566..7d2c4bfaeaa 100644
--- a/apps/bootstrap-installer/src-tauri/src/bootstrap.rs
+++ b/apps/bootstrap-installer/src-tauri/src/bootstrap.rs
@@ -545,7 +545,6 @@ async fn run_bootstrap(
let source_note = match &script.source {
ScriptSource::DevCheckout => "dev checkout",
ScriptSource::Bundled => "bundled",
- ScriptSource::Cached => "cached",
ScriptSource::Downloaded => "downloaded",
};
emit_log(&format!(
diff --git a/apps/bootstrap-installer/src-tauri/src/install_script.rs b/apps/bootstrap-installer/src-tauri/src/install_script.rs
index da2400e510c..75f7ec9f5f3 100644
--- a/apps/bootstrap-installer/src-tauri/src/install_script.rs
+++ b/apps/bootstrap-installer/src-tauri/src/install_script.rs
@@ -35,7 +35,6 @@ pub struct ResolvedScript {
pub enum ScriptSource {
DevCheckout,
Bundled,
- Cached,
Downloaded,
}
@@ -70,27 +69,6 @@ fn is_valid_commit(s: &str) -> bool {
(7..=40).contains(&len) && s.chars().all(|c| c.is_ascii_hexdigit())
}
-/// Resolver cache plan for a pin that already has a local path computed.
-///
-/// Immutable commit pins reuse cache forever. Mutable branch/tag pins always
-/// refresh and never fall back to a stale cache: the repository stage checks
-/// out the live branch, so an old script would drive a tree it predates.
-#[derive(Debug, Clone, Copy, PartialEq, Eq)]
-pub(crate) enum CachePlan {
- /// On-disk hit for an immutable pin — skip the network.
- Reuse,
- /// Download (or re-download); a failure is fatal.
- Fetch,
-}
-
-pub(crate) fn cache_plan(immutable: bool, cached_exists: bool) -> CachePlan {
- if immutable && cached_exists {
- CachePlan::Reuse
- } else {
- CachePlan::Fetch
- }
-}
-
/// Resolves the install script to use for this run.
///
/// `pin` is the commit-or-branch from either Hermes-Setup's build-time
@@ -122,12 +100,12 @@ pub async fn resolve(
// 3. Network. Pin must be a real commit or a branch ref.
//
- // Commit SHAs are immutable — permanent cache reuse is safe.
- // Branch/tag pins are moving refs: always try to refresh so "Retry install"
- // cannot keep reusing a poisoned install-main.ps1 forever (#67193).
- let (commit_or_ref, immutable) = match (&pin.commit, &pin.branch) {
- (Some(c), _) if is_valid_commit(c) => (c.clone(), true),
- (_, Some(b)) if !b.trim().is_empty() => (b.clone(), false),
+ // Always download; a previously downloaded script is never reused. A
+ // stale script drives a tree it predates (the repository stage follows
+ // the live branch), and a failed download is fatal so Retry refetches.
+ let commit_or_ref = match (&pin.commit, &pin.branch) {
+ (Some(c), _) if is_valid_commit(c) => c.clone(),
+ (_, Some(b)) if !b.trim().is_empty() => b.clone(),
(Some(other), _) => {
return Err(anyhow!(
"install script pin commit `{other}` is not a valid git SHA"
@@ -140,47 +118,20 @@ pub async fn resolve(
}
};
- let cached = cached_path(kind, &commit_or_ref);
- match cache_plan(immutable, cached.exists()) {
- CachePlan::Reuse => {
- emit_log(&format!(
- "[bootstrap] using cached {} for {}",
- kind.filename(),
- truncate_ref(&commit_or_ref)
- ));
- // Immutable pins are cached forever, so a .ps1 cached by a
- // pre-BOM-fix installer would keep the #67193 encoding bug on
- // every retry. Upgrade it in place before handing it out.
- upgrade_cached_script(kind, &cached, emit_log);
- return Ok(ResolvedScript {
- path: cached,
- source: ScriptSource::Cached,
- commit: pin.commit.clone(),
- branch: pin.branch.clone(),
- });
- }
- CachePlan::Fetch => {
- emit_log(&format!(
- "[bootstrap] downloading {} for {} {} from GitHub",
- kind.filename(),
- if immutable {
- "commit"
- } else {
- "mutable ref"
- },
- truncate_ref(&commit_or_ref)
- ));
-
- download(kind, &commit_or_ref, &cached).await?;
- emit_log(&format!("[bootstrap] cached to {}", cached.display()));
- Ok(ResolvedScript {
- path: cached,
- source: ScriptSource::Downloaded,
- commit: pin.commit.clone(),
- branch: pin.branch.clone(),
- })
- }
- }
+ let dest = download_path(kind, &commit_or_ref);
+ emit_log(&format!(
+ "[bootstrap] downloading {} for {} from GitHub",
+ kind.filename(),
+ truncate_ref(&commit_or_ref)
+ ));
+ download(kind, &commit_or_ref, &dest).await?;
+ emit_log(&format!("[bootstrap] downloaded to {}", dest.display()));
+ Ok(ResolvedScript {
+ path: dest,
+ source: ScriptSource::Downloaded,
+ commit: pin.commit.clone(),
+ branch: pin.branch.clone(),
+ })
}
#[derive(Debug, Clone, Default)]
@@ -189,7 +140,7 @@ pub struct Pin {
pub branch: Option<String>,
}
-fn cached_path(kind: ScriptKind, commit_or_ref: &str) -> PathBuf {
+fn download_path(kind: ScriptKind, commit_or_ref: &str) -> PathBuf {
let safe = sanitize_ref(commit_or_ref);
let filename = match kind {
ScriptKind::Ps1 => format!("install-{safe}.ps1"),
@@ -247,59 +198,12 @@ pub(crate) fn prepare_cached_script_bytes(kind: ScriptKind, bytes: &[u8]) -> Vec
}
}
-/// Upgrade a cached script written by a pre-BOM-fix installer in place.
-///
-/// `prepare_cached_script_bytes` only runs inside `download()`, but immutable
-/// commit pins (and the stale-fallback path) reuse the on-disk file without
-/// re-downloading — so a BOM-less `.ps1` cached before the #67193 fix would
-/// keep reproducing the ANSI-codepage parse failure on every retry. Rewrites
-/// through the same atomic tmp+rename shape as `download()`. Best-effort: a
-/// failed upgrade logs a warning and keeps the original file (which is no
-/// worse than the pre-existing behavior).
-fn upgrade_cached_script(kind: ScriptKind, cached: &Path, emit_log: &impl Fn(&str)) {
- if !matches!(kind, ScriptKind::Ps1) {
- return;
- }
- let bytes = match std::fs::read(cached) {
- Ok(b) => b,
- Err(err) => {
- emit_log(&format!(
- "[bootstrap] WARNING: could not read cached script {} for BOM check: {err}",
- cached.display()
- ));
- return;
- }
- };
- if bytes.starts_with(UTF8_BOM) {
- return;
- }
- let upgraded = prepare_cached_script_bytes(kind, &bytes);
- let tmp = cached.with_extension("ps1.tmp");
- let result = std::fs::write(&tmp, &upgraded).and_then(|()| std::fs::rename(&tmp, cached));
- match result {
- Ok(()) => emit_log(&format!(
- "[bootstrap] upgraded cached {} with UTF-8 BOM (#67193)",
- cached.display()
- )),
- Err(err) => {
- let _ = std::fs::remove_file(&tmp);
- emit_log(&format!(
- "[bootstrap] WARNING: could not upgrade cached {} with UTF-8 BOM: {err}",
- cached.display()
- ));
- }
- }
-}
-
/// Downloads to `dest_path` via reqwest with rustls. Atomically renames
-/// `dest_path.tmp` → `dest_path` so partial writes don't poison the cache.
+/// `dest_path.tmp` → `dest_path` so a partial write is never executed.
///
-/// The client carries explicit timeouts: mutable branch pins call this on
-/// EVERY run (#67193 cache-refresh fix), and the stale-cache fallback in
-/// `resolve()` only fires when this returns `Err`. Without a timeout, a
-/// black-holed connection (captive portal, hung proxy, silently dropped
-/// packets) never errors — the whole bootstrap would hang here instead of
-/// falling back to the cached script.
+/// Explicit timeouts: this runs on every bootstrap, and a black-holed
+/// connection (captive portal, hung proxy) would otherwise hang forever
+/// instead of failing so the user can Retry.
async fn download(kind: ScriptKind, commit_or_ref: &str, dest_path: &Path) -> Result<()> {
let url = format!(
"https://raw.githubusercontent.com/NousResearch/hermes-agent/{}/scripts/{}",
@@ -413,56 +317,9 @@ mod tests {
}
#[test]
- fn commit_pins_are_immutable_branch_pins_are_not() {
- // Mirrors the resolve() immutable decision: SHA pins may reuse cache
- // forever; branch pins must refresh so Retry cannot keep a bad script.
+ fn commit_pins_are_distinguished_from_branch_pins() {
assert!(is_valid_commit("02d26981d3d4ad50e142399b8476f59ad5953ff0"));
assert!(!is_valid_commit("main"));
assert!(!is_valid_commit("release/1.2.3"));
}
-
- #[test]
- fn mutable_pins_always_refresh_without_stale_fallback() {
- // A cached install-main.ps1 predates the live branch the repository
- // stage checks out; running it against that tree broke installs.
- assert_eq!(cache_plan(/*immutable=*/ false, /*cached_exists=*/ true), CachePlan::Fetch);
- assert_eq!(cache_plan(/*immutable=*/ true, /*cached_exists=*/ true), CachePlan::Reuse);
- assert_eq!(cache_plan(/*immutable=*/ false, /*cached_exists=*/ false), CachePlan::Fetch);
- assert_eq!(cache_plan(/*immutable=*/ true, /*cached_exists=*/ false), CachePlan::Fetch);
- }
-
- #[test]
- fn upgrade_cached_script_adds_bom_to_legacy_ps1() {
- // A .ps1 cached by a pre-#67193 installer has no BOM; the Reuse path
- // must upgrade it in place instead of serving the broken bytes forever.
- let dir = std::env::temp_dir().join(format!("hermes-bom-test-{}", std::process::id()));
- std::fs::create_dir_all(&dir).unwrap();
- let cached = dir.join("install-abc1234.ps1");
- std::fs::write(&cached, b"Write-Host legacy\n").unwrap();
-
- upgrade_cached_script(ScriptKind::Ps1, &cached, &|_| {});
- let bytes = std::fs::read(&cached).unwrap();
- assert!(bytes.starts_with(UTF8_BOM), "legacy cache must gain a BOM");
- assert_eq!(&bytes[UTF8_BOM.len()..], b"Write-Host legacy\n");
-
- // Idempotent: a second pass must not double the BOM.
- upgrade_cached_script(ScriptKind::Ps1, &cached, &|_| {});
- let again = std::fs::read(&cached).unwrap();
- assert_eq!(again, bytes);
-
- std::fs::remove_dir_all(&dir).unwrap();
- }
-
- #[test]
- fn upgrade_cached_script_leaves_sh_untouched() {
- let dir = std::env::temp_dir().join(format!("hermes-bom-sh-test-{}", std::process::id()));
- std::fs::create_dir_all(&dir).unwrap();
- let cached = dir.join("install-main.sh");
- std::fs::write(&cached, b"#!/usr/bin/env bash\n").unwrap();
-
- upgrade_cached_script(ScriptKind::Sh, &cached, &|_| {});
- assert_eq!(std::fs::read(&cached).unwrap(), b"#!/usr/bin/env bash\n");
-
- std::fs::remove_dir_all(&dir).unwrap();
- }
}