Skip to content

The agent that grows with you

fix(install): an unreachable release fails a fresh install instead of publishing an empty one

Teknium4cfa3ecd97dd
diff --git a/scripts/install.ps1 b/scripts/install.ps1
index 7dd618db7bd..0516bfe72a3 100644
--- a/scripts/install.ps1
+++ b/scripts/install.ps1
@@ -897,9 +897,10 @@ function Get-ChosenChannel {
}
# The latest published stable release (vX.Y.Z), the release `hermes update` resolves for the
-# stable channel. github.com's /releases/latest redirect answers when the API quota is spent.
-# Returns '' when neither answers.
-function Get-LatestStableRelease {
+# stable channel. github.com's /releases/latest redirect answers when the API quota is spent, and
+# origin's highest vX.Y.Z tag (final tags are pushed by the publication run) when github.com's web
+# endpoints are unreachable but git is not. Returns '' when none answers.
+function Get-LatestStableRelease([string]$Origin) {
$pattern = '^v(0|[1-9]\d{0,2})\.(0|[1-9]\d*)\.(0|[1-9]\d*)$' # update_channel.STABLE_TAG_RE
$tag = ''
try {
@@ -919,6 +920,11 @@ function Get-LatestStableRelease {
finally { $response.Close() }
} catch { }
}
+ if ($tag -cnotmatch $pattern) {
+ $tags = @(Invoke-Native { git ls-remote --tags --refs $Origin 'refs/tags/v*' 2>$null } |
+ ForEach-Object { "$_" -replace '^.*refs/tags/', '' } | Where-Object { $_ -cmatch $pattern })
+ if ($tags) { $tag = $tags | Sort-Object { [version]$_.Substring(1) } | Select-Object -Last 1 }
+ }
if ($tag -cmatch $pattern) { return $tag }
return ''
}
@@ -932,19 +938,26 @@ function Get-StableTarget([string]$Origin) {
if ($Branch -cne 'main') { return '' }
if ($Origin -notmatch '^(https://github\.com/|git@github\.com:|ssh://git@github\.com/)NousResearch/hermes-agent(\.git)?/?$') { return '' }
}
- $release = Get-LatestStableRelease
+ $release = Get-LatestStableRelease $Origin
if ($release) { return $release }
return '-'
}
# Fetch tag $Release into checkout $Dir (releases can be cut off main, on release/* branches).
-# A mirror without the tag falls back to the branch unless stable was asked for by name.
function Receive-StableRelease([string]$Dir, [string]$Release) {
Invoke-Logged -MayFail "Fetching $Release" { git -C $Dir fetch origin "+refs/tags/${Release}:refs/tags/${Release}" }
- if (-not $LASTEXITCODE) { return $true }
- if ($Channel -ceq 'stable') { Fail "could not fetch $Release$(Get-NativeDetail)" git_fetch_failed }
- Write-Warn "could not fetch $Release; installing the tip of $Branch instead"
- return $false
+ return (-not $LASTEXITCODE)
+}
+
+# Stable applies but cannot be reached. A fresh install stops (installing main instead would be a
+# channel nobody chose); a rerun keeps its checkout for `hermes update`, unless stable was asked
+# for by name. $What says what failed.
+function Confirm-StableUnreachable([string]$What) {
+ if ((Test-Path (Join-Path $InstallDir '.git')) -and $Channel -cne 'stable') {
+ Write-Warn "$What; keeping the checkout where it is"
+ return
+ }
+ Fail "$What$(Get-NativeDetail). Rerun the installer to retry, or pass -Channel main to install main's tip" git_fetch_failed
}
function Stage-Repository {
@@ -1000,8 +1013,7 @@ function Stage-Repository {
}
if ($chosen -ceq 'main') { $chosen = '' }
if ($release -ceq '-') {
- # Moving a stable checkout onto main would be a channel switch nobody asked for.
- Write-Warn "could not look up the latest stable release; keeping the checkout where it is"
+ Confirm-StableUnreachable "could not look up the latest stable release"
$chosen = 'stable'
$release = ''
} elseif ($chosen) { Log "Repairing $InstallDir on the $chosen update channel it chose" }
@@ -1041,7 +1053,10 @@ function Stage-Repository {
Log "Keeping its checkout: ``hermes update`` moves it (``hermes update --set-channel main`` follows main)"
return
}
- if ($release -and -not (Receive-StableRelease $InstallDir $release)) { $release = '' }
+ if ($release -and -not (Receive-StableRelease $InstallDir $release)) {
+ Confirm-StableUnreachable "could not fetch $release"
+ return
+ }
if ($release) {
# Without a choice the default only moves forward, as `hermes update`'s does: a
# checkout that already contains the release (one that followed main) stays. A
@@ -1146,10 +1161,7 @@ function Stage-Repository {
# A pinned install materializes only the pin (below): checking out the
# branch tip first fetches and rewrites every file the two differ in.
$release = Get-StableTarget $RepoUrl
- if ($release -ceq '-') {
- Write-Warn "could not look up the latest stable release; installing the tip of $Branch instead"
- $release = ''
- }
+ if ($release -ceq '-') { Confirm-StableUnreachable "could not look up the latest stable release" }
if ($release) { Log "Installing the latest stable release ($release); -Channel main follows every commit" }
$checkout = @()
if ($Commit -or $release) { $checkout = @('--no-checkout') }
@@ -1187,7 +1199,8 @@ function Stage-Repository {
Invoke-Native { git -C $tree merge-base --is-ancestor $Commit "origin/$Branch" 2>$null }
if ($LASTEXITCODE) { Fail "commit $Commit is not on branch $Branch" commit_not_on_branch }
}
- if ($release -and -not (Receive-StableRelease $tree $release)) { $release = '' }
+ # A failure here leaves only the staging dir, which finally removes.
+ if ($release -and -not (Receive-StableRelease $tree $release)) { Confirm-StableUnreachable "could not fetch $release" }
if ($Commit -or $release -or $deferred) {
# The checkout step is where throttled downloads die: retry it alone.
# A pin detaches HEAD and leaves the branch at its tip, as the published pin did.
diff --git a/scripts/install.sh b/scripts/install.sh
index 02a6113a451..c0c5001400f 100755
--- a/scripts/install.sh
+++ b/scripts/install.sh
@@ -597,8 +597,9 @@ is_official_repo() {
}
# The latest published stable release (vX.Y.Z), the release `hermes update` resolves for the
-# stable channel. github.com's /releases/latest redirect answers when the API quota is spent.
-# Prints nothing when neither answers.
+# stable channel. github.com's /releases/latest redirect answers when the API quota is spent, and
+# origin's highest vX.Y.Z tag (final tags are pushed by the publication run) when github.com's web
+# endpoints are unreachable but git is not. $1 is the origin URL. Prints nothing when none answers.
latest_stable_release() {
local tag
tag="$(curl -fsSL --connect-timeout 5 --max-time 20 -H 'Accept: application/vnd.github+json' \
@@ -609,6 +610,10 @@ latest_stable_release() {
https://github.com/NousResearch/hermes-agent/releases/latest 2>/dev/null \
| tr -d '\r' | sed -n 's#^[Ll]ocation:.*/releases/tag/\([^/]*\)$#\1#p' | tail -n 1)"
fi
+ if ! valid_stable_tag "$tag"; then
+ tag="$(git ls-remote --tags --refs "$1" 'refs/tags/v*' 2>/dev/null | sed 's#.*refs/tags/##' \
+ | grep -E '^v(0|[1-9][0-9]{0,2})\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$' | sort -V | tail -n 1)"
+ fi
if valid_stable_tag "$tag"; then printf '%s' "$tag"; fi
}
# Same shape as hermes_cli.update_channel.STABLE_TAG_RE.
@@ -627,17 +632,24 @@ stable_target() {
*) [ "$BRANCH" = main ] && is_official_repo "$1" || return 0 ;;
esac
local release
- release="$(latest_stable_release)"
+ release="$(latest_stable_release "$1")"
printf '%s' "${release:--}"
}
# Fetch tag $release into checkout $1 (releases can be cut off main, on release/* branches).
-# A mirror without the tag falls back to the branch unless stable was asked for by name.
fetch_release() {
- run_logged --may-fail "Fetching $release" git -C "$1" fetch origin "+refs/tags/$release:refs/tags/$release" && return 0
- [ "$CHANNEL" != stable ] || fail "could not fetch $release$(native_detail)" git_fetch_failed
- log_warn "could not fetch $release; installing the tip of $BRANCH instead"
- return 1
+ run_logged --may-fail "Fetching $release" git -C "$1" fetch origin "+refs/tags/$release:refs/tags/$release"
+}
+
+# Stable applies but cannot be reached. A fresh install stops (installing main instead would be
+# a channel nobody chose); a rerun keeps its checkout for `hermes update`, unless stable was
+# asked for by name. $1 says what failed.
+stable_unreachable() {
+ if [ -d "$INSTALL_DIR/.git" ] && [ "$CHANNEL" != stable ]; then
+ log_warn "$1; keeping the checkout where it is"
+ return 0
+ fi
+ fail "$1$(native_detail). Rerun the installer to retry, or pass --channel main to install main's tip" git_fetch_failed
}
stage_repository() {
@@ -673,8 +685,7 @@ stage_repository() {
fi
[ "$channel" != main ] || channel=""
if [ "$release" = - ]; then
- # Moving a stable checkout onto main would be a channel switch nobody asked for.
- log_warn "could not look up the latest stable release; keeping the checkout where it is"
+ stable_unreachable "could not look up the latest stable release"
channel=stable release=""
elif [ -n "$channel" ]; then
log "Repairing $INSTALL_DIR on the $channel update channel it chose"
@@ -721,7 +732,10 @@ stage_repository() {
log "Keeping its checkout: \`hermes update\` moves it (\`hermes update --set-channel main\` follows main)"
return 0
fi
- [ -z "$release" ] || fetch_release "$INSTALL_DIR" || release=""
+ if [ -n "$release" ] && ! fetch_release "$INSTALL_DIR"; then
+ stable_unreachable "could not fetch $release"
+ return 0
+ fi
if [ -n "$release" ]; then
# Without a choice the default only moves forward, as `hermes update`'s does: a
# checkout that already contains the release (one that followed main) stays. A
@@ -812,10 +826,7 @@ stage_repository() {
mkdir -p "$(dirname "$INSTALL_DIR")"
local staged attempt label cloned=false progress=() materialize=(reset --hard HEAD) no_checkout=""
release="$(stable_target "$REPO_URL")"
- if [ "$release" = - ]; then
- log_warn "could not look up the latest stable release; installing the tip of $BRANCH instead"
- release=""
- fi
+ [ "$release" != - ] || stable_unreachable "could not look up the latest stable release"
[ -z "$INSTALL_COMMIT$release" ] || no_checkout=--no-checkout
[ -z "$release" ] || log "Installing the latest stable release ($release); --channel main follows every commit"
# Phase lines ("Receiving objects: 42%") feed the status line; git
@@ -859,8 +870,10 @@ stage_repository() {
git -C "$staged/tree" merge-base --is-ancestor "$INSTALL_COMMIT" "origin/$BRANCH" 2>/dev/null \
|| { rm -rf "$staged"; fail "commit $INSTALL_COMMIT is not on branch $BRANCH" commit_not_on_branch; }
fi
- [ -z "$release" ] || fetch_release "$staged/tree" || release=""
- [ -z "$release" ] || materialize=(checkout --force --detach "$release")
+ if [ -n "$release" ]; then
+ fetch_release "$staged/tree" || { rm -rf "$staged"; stable_unreachable "could not fetch $release"; }
+ materialize=(checkout --force --detach "$release")
+ fi
if [ -n "$INSTALL_COMMIT$release" ] || [ "$cloned" = deferred ]; then
# The checkout step is where throttled downloads die: retry it alone.
# A pin detaches HEAD and leaves the branch at its tip, as the published pin did.
diff --git a/tests/e2e/core/upgrade/_install_helpers.py b/tests/e2e/core/upgrade/_install_helpers.py
index 2994883f90e..46cdfacc2b1 100644
--- a/tests/e2e/core/upgrade/_install_helpers.py
+++ b/tests/e2e/core/upgrade/_install_helpers.py
@@ -161,8 +161,13 @@ def run_installer(sb: Sandbox, *, timeout: float = 1800) -> subprocess.Completed
"""HEAD's scripts/install.sh, non-interactive, as the documented `curl | bash` run does it."""
script = sb.root / "install.sh"
shutil.copy(H.WORKTREE / "scripts" / "install.sh", script)
- return sb.run(["bash", str(script), "--non-interactive"],
- timeout=timeout, input="")
+ env = dict(sb.env)
+ if not sb.checkout.exists():
+ # The origin publishes commits on main and no release tags, so a fresh install follows
+ # main; the channel it saves carries the reruns.
+ env["HERMES_INSTALL_CHANNEL"] = "main"
+ return H.run(["bash", str(script), "--non-interactive"], env=env, cwd=sb.root, writable=[sb.root],
+ timeout=timeout, input="")
def pin_main_channel(sb: Sandbox) -> None:
diff --git a/tests/scripts/test_install_ps1_stable_default.py b/tests/scripts/test_install_ps1_stable_default.py
index 303d15a66c5..9cdf0eaccbe 100644
--- a/tests/scripts/test_install_ps1_stable_default.py
+++ b/tests/scripts/test_install_ps1_stable_default.py
@@ -25,13 +25,15 @@ def _git(cwd, *args, env=None):
).stdout.strip()
[email protected]("start,channel,lands", [
- (None, "", "release"), # fresh install: the release, not main's tip
- (None, "main", "tip"), # explicit opt-out: main's tip
- ("before", "", "release"), # rerun behind the release: moves forward to it
- ("tip", "", "tip"), # rerun already containing it: never moves back
[email protected]("start,channel,tagged,lands", [
+ (None, "", True, "release"), # fresh install: the release, not main's tip
+ (None, "main", True, "tip"), # explicit opt-out: main's tip
+ ("before", "", True, "release"), # rerun behind the release: moves forward to it
+ ("tip", "", True, "tip"), # rerun already containing it: never moves back
+ (None, "", False, None), # release tag unreachable: fail, publish nothing
+ ("before", "", False, "before"), # ... a rerun keeps its checkout instead
])
-def test_install_ps1_lands_on_the_latest_stable_release(tmp_path, start, channel, lands):
+def test_install_ps1_lands_on_the_latest_stable_release(tmp_path, start, channel, tagged, lands):
powershell = shutil.which("powershell")
if not powershell:
pytest.skip("Windows PowerShell is required")
@@ -42,7 +44,8 @@ def test_install_ps1_lands_on_the_latest_stable_release(tmp_path, start, channel
_git(origin, "init", "-q", "-b", "main")
for label in ("before", "release", "newer"):
_git(origin, "commit", "-q", "--allow-empty", "-m", label)
- _git(origin, "tag", "-a", "v1.2.3", "-m", "release", "HEAD~1")
+ if tagged:
+ _git(origin, "tag", "-a", "v1.2.3", "-m", "release", "HEAD~1")
commits = {name: _git(origin, "rev-parse", ref)
for name, ref in (("before", "HEAD~2"), ("release", "HEAD~1"), ("tip", "HEAD"))}
gitconfig = tmp_path / "gitconfig"
@@ -65,5 +68,9 @@ def test_install_ps1_lands_on_the_latest_stable_release(tmp_path, start, channel
stdin=subprocess.DEVNULL, timeout=300, env=env,
)
+ if lands is None:
+ assert result.returncode != 0 and not install.exists(), result.stdout + result.stderr
+ return
assert result.returncode == 0, result.stdout + result.stderr
assert _git(install, "rev-parse", "HEAD", env=env) == commits[lands], result.stdout + result.stderr
+ assert _git(install, "status", "--porcelain", env=env) == "", result.stdout + result.stderr
diff --git a/tests/scripts/test_install_sh_stable_default.py b/tests/scripts/test_install_sh_stable_default.py
index 0d87f861e18..060330e6d15 100644
--- a/tests/scripts/test_install_sh_stable_default.py
+++ b/tests/scripts/test_install_sh_stable_default.py
@@ -24,13 +24,15 @@ def _git(cwd, *args, env=None):
).stdout.strip()
[email protected]("start,args,lands", [
- (None, [], "release"), # fresh install: the release, not main's tip
- (None, ["--channel", "main"], "tip"), # explicit opt-out: main's tip
- ("before", [], "release"), # rerun behind the release: moves forward to it
- ("tip", [], "tip"), # rerun already containing it: never moves back
[email protected]("start,args,tagged,lands", [
+ (None, [], True, "release"), # fresh install: the release, not main's tip
+ (None, ["--channel", "main"], True, "tip"), # explicit opt-out: main's tip
+ ("before", [], True, "release"), # rerun behind the release: moves forward to it
+ ("tip", [], True, "tip"), # rerun already containing it: never moves back
+ (None, [], False, None), # release tag unreachable: fail, publish nothing
+ ("before", [], False, "before"), # ... a rerun keeps its checkout instead
])
-def test_install_lands_on_the_latest_stable_release(tmp_path, start, args, lands):
+def test_install_lands_on_the_latest_stable_release(tmp_path, start, args, tagged, lands):
origin, home, bin_dir = tmp_path / "origin", tmp_path / "home", tmp_path / "bin"
install = home / ".hermes" / "hermes-agent"
for d in (origin, home, bin_dir):
@@ -38,7 +40,8 @@ def test_install_lands_on_the_latest_stable_release(tmp_path, start, args, lands
_git(origin, "init", "-q", "-b", "main")
for label in ("before", "release", "newer"):
_git(origin, "commit", "-q", "--allow-empty", "-m", label)
- _git(origin, "tag", "-a", "v1.2.3", "-m", "release", "HEAD~1")
+ if tagged:
+ _git(origin, "tag", "-a", "v1.2.3", "-m", "release", "HEAD~1")
commits = {name: _git(origin, "rev-parse", ref)
for name, ref in (("before", "HEAD~2"), ("release", "HEAD~1"), ("tip", "HEAD"))}
(home / ".gitconfig").write_text(f'[url "file://{origin}"]\n insteadOf = {OFFICIAL}\n', encoding="utf-8")
@@ -59,5 +62,9 @@ def test_install_lands_on_the_latest_stable_release(tmp_path, start, args, lands
check=False, capture_output=True, text=True, stdin=subprocess.DEVNULL, timeout=120, env=env,
)
+ if lands is None:
+ assert result.returncode != 0 and not install.exists(), result.stdout + result.stderr
+ return
assert result.returncode == 0, result.stdout + result.stderr
assert _git(install, "rev-parse", "HEAD") == commits[lands], result.stdout + result.stderr
+ assert _git(install, "status", "--porcelain") == "", result.stdout + result.stderr