Skip to content

The agent that grows with you

fix(install): keep dropped commits behind a rescue ref on the installer reset

brooklyn!c7d2985ae3e2
diff --git a/scripts/install.ps1 b/scripts/install.ps1
index 1d52aa90f14..ca17df8fcf6 100644
--- a/scripts/install.ps1
+++ b/scripts/install.ps1
@@ -2378,6 +2378,25 @@ function Install-Repository {
git -c windows.appendAtomically=false pull --ff-only origin $Branch
if ($LASTEXITCODE -ne 0) {
Write-Warn "Fast-forward not possible; resetting managed install to origin/$Branch..."
+ # Park commits the reset drops behind a rescue ref, same namespace
+ # as ``hermes update`` (which also prunes these refs).
+ $dropped = 0
+ $droppedText = ((& git -c windows.appendAtomically=false rev-list --count "origin/$Branch..HEAD" 2>$null) | Out-String).Trim()
+ [void][int]::TryParse($droppedText, [ref]$dropped)
+ if ($dropped -gt 0) {
+ git -c windows.appendAtomically=false merge-base HEAD "origin/$Branch" 2>$null | Out-Null
+ $rescueKind = if ($LASTEXITCODE -eq 0) { "diverged" } else { "orphan" }
+ $preResetSha = ((& git -c windows.appendAtomically=false rev-parse HEAD 2>$null) | Out-String).Trim()
+ $rescueStamp = [DateTime]::UtcNow.ToString("yyyyMMdd-HHmmss")
+ $rescueRef = "refs/hermes-update-backups/$rescueKind-$Branch-$rescueStamp-$($preResetSha.Substring(0, [Math]::Min(12, $preResetSha.Length)))"
+ git -c windows.appendAtomically=false update-ref $rescueRef HEAD
+ if ($LASTEXITCODE -eq 0) {
+ Write-Warn "$dropped commit(s) not on origin/$Branch backed up to $rescueRef"
+ Write-Warn "List them with: git -C `"$InstallDir`" log origin/$Branch..$rescueRef"
+ } else {
+ Write-Warn "Could not back up local commits (HEAD was $preResetSha)"
+ }
+ }
git -c windows.appendAtomically=false reset --hard "origin/$Branch"
if ($LASTEXITCODE -ne 0) { throw "git reset --hard origin/$Branch failed (exit $LASTEXITCODE)" }
}
diff --git a/scripts/install.sh b/scripts/install.sh
index 096290221a4..4e68442551a 100755
--- a/scripts/install.sh
+++ b/scripts/install.sh
@@ -1578,6 +1578,21 @@ clone_repo() {
# fetched remote so bootstrap/install can recover.
if ! git pull --ff-only origin "$BRANCH"; then
log_warn "Fast-forward not possible; resetting managed install to origin/$BRANCH..."
+ # Park commits the reset drops behind a rescue ref, same namespace
+ # as ``hermes update`` (which also prunes these refs).
+ local dropped rescue_kind rescue_ref
+ dropped="$(git rev-list --count "origin/$BRANCH..HEAD" 2>/dev/null || echo 0)"
+ if [ "${dropped:-0}" -gt 0 ]; then
+ rescue_kind="diverged"
+ git merge-base HEAD "origin/$BRANCH" >/dev/null 2>&1 || rescue_kind="orphan"
+ rescue_ref="refs/hermes-update-backups/$rescue_kind-$BRANCH-$(date -u +%Y%m%d-%H%M%S)-$(git rev-parse --short=12 HEAD)"
+ if git update-ref "$rescue_ref" HEAD; then
+ log_warn "$dropped commit(s) not on origin/$BRANCH backed up to $rescue_ref"
+ log_warn "List them with: git -C \"$INSTALL_DIR\" log origin/$BRANCH..$rescue_ref"
+ else
+ log_warn "Could not back up local commits (HEAD was $(git rev-parse HEAD))"
+ fi
+ fi
git reset --hard "origin/$BRANCH"
fi
diff --git a/tests/scripts/install/test_install_diverged_rescue_ref.py b/tests/scripts/install/test_install_diverged_rescue_ref.py
new file mode 100644
index 00000000000..f96265efdd9
--- /dev/null
+++ b/tests/scripts/install/test_install_diverged_rescue_ref.py
@@ -0,0 +1,107 @@
+"""Regression: the installer's update reset must not drop local commits unanchored.
+
+Re-running ``install.sh`` / ``install.ps1`` over an existing checkout (desktop
+bootstrap and its update retry do this) falls back to
+``reset --hard origin/<branch>`` when a fast-forward fails. Commits made on that
+branch must survive behind a ``refs/hermes-update-backups/`` rescue ref, the same
+namespace ``hermes update`` uses, and the installer must print the ref.
+"""
+
+from __future__ import annotations
+
+import os
+import shutil
+import subprocess
+from pathlib import Path
+
+import pytest
+
+REPO_ROOT = Path(__file__).resolve().parent.parent.parent.parent
+INSTALL_SH = REPO_ROOT / "scripts" / "install.sh"
+INSTALL_PS1 = REPO_ROOT / "scripts" / "install.ps1"
+POWERSHELL = next(
+ (candidate for candidate in ("pwsh", "powershell") if shutil.which(candidate)),
+ None,
+)
+
+
+def _git(cwd: Path, *args: str) -> str:
+ return subprocess.run(
+ ["git", "-c", "user.email=t@t", "-c", "user.name=t", *args],
+ cwd=cwd, check=True, capture_output=True, text=True,
+ ).stdout.strip()
+
+
+def _commit(repo: Path, name: str) -> str:
+ (repo / name).write_text(f"{name}\n", encoding="utf-8")
+ _git(repo, "add", name)
+ _git(repo, "commit", "-q", "-m", f"add {name}")
+ return _git(repo, "rev-parse", "HEAD")
+
+
+def _diverged_managed_checkout(tmp_path: Path) -> tuple[Path, str]:
+ """A managed checkout on ``main`` with one local commit, while its origin moved on."""
+ upstream = tmp_path / "upstream"
+ upstream.mkdir()
+ _git(upstream, "init", "-q", "-b", "main")
+ _commit(upstream, "shared.txt")
+ managed = tmp_path / "hermes-agent"
+ _git(tmp_path, "clone", "-q", str(upstream), str(managed))
+ local_sha = _commit(managed, "local-fix.txt")
+ _commit(upstream, "upstream-only.txt")
+ return managed, local_sha
+
+
+def _assert_local_commit_parked(repo: Path, local_sha: str, output: str) -> None:
+ assert _git(repo, "rev-parse", "HEAD") == _git(repo, "rev-parse", "origin/main")
+ refs = dict(
+ line.split()[::-1] for line in _git(
+ repo, "for-each-ref", "--format=%(refname) %(objectname)",
+ "refs/hermes-update-backups/").splitlines())
+ ref = refs.get(local_sha)
+ assert ref and ref.startswith("refs/hermes-update-backups/diverged-main-"), refs
+ assert ref in output, "the installer must print where the commits went"
+ assert _git(repo, "log", "--format=%H", f"origin/main..{ref}").split() == [local_sha]
+
+
[email protected]_system_guard_bypass
+ shutil.which("git") is None or shutil.which("bash") is None,
+ reason="needs git and bash",
+)
+def test_install_sh_repository_stage_parks_local_commits_before_reset(tmp_path: Path) -> None:
+ managed, local_sha = _diverged_managed_checkout(tmp_path)
+ env = os.environ | {
+ "HERMES_HOME": str(tmp_path / "hermes-home"),
+ "HERMES_INSTALL_DIR": str(managed),
+ }
+
+ result = subprocess.run(
+ ["bash", str(INSTALL_SH), "--stage", "repository", "--non-interactive"],
+ cwd=tmp_path, env=env, capture_output=True, text=True,
+ )
+
+ assert result.returncode == 0, result.stderr
+ _assert_local_commit_parked(managed, local_sha, result.stdout + result.stderr)
+
+
[email protected]_system_guard_bypass
+ shutil.which("git") is None or POWERSHELL is None,
+ reason="needs git and PowerShell",
+)
+def test_install_ps1_repository_stage_parks_local_commits_before_reset(tmp_path: Path) -> None:
+ managed, local_sha = _diverged_managed_checkout(tmp_path)
+
+ result = subprocess.run(
+ [
+ POWERSHELL, "-NoProfile", "-File", str(INSTALL_PS1),
+ "-Stage", "repository", "-NonInteractive",
+ "-InstallDir", str(managed),
+ "-HermesHome", str(tmp_path / "hermes-home"),
+ ],
+ cwd=tmp_path, capture_output=True, text=True,
+ )
+
+ assert result.returncode == 0, result.stderr
+ _assert_local_commit_parked(managed, local_sha, result.stdout + result.stderr)