Skip to content

The agent that grows with you

perf(install): a pinned install.ps1 clone checks out only the pin, before it publishes

Tekniumdbbf98371d15
diff --git a/scripts/install.ps1 b/scripts/install.ps1
index 4a197c92358..49cd1a61a8d 100644
--- a/scripts/install.ps1
+++ b/scripts/install.ps1
@@ -810,6 +810,7 @@ function Disable-TreelessGraphWrites([string]$Dir) {
}
function Stage-Repository {
+ $pinned = $false
# Refuse an occupied non-checkout before provisioning Git. This check
# needs no tool download and must not overwrite a user's existing files.
if (-not (Test-Path (Join-Path $InstallDir ".git")) -and (Test-Path -LiteralPath $InstallDir)) {
@@ -949,8 +950,13 @@ function Stage-Repository {
# prints none to a pipe unless asked.
$progress = @()
if (Test-QuietOutput) { $progress = @('--progress') }
+ # A pinned install materializes only the pin (below): checking out the
+ # branch tip first fetches and rewrites every file the two differ in.
+ $checkout = @()
+ if ($Commit) { $checkout = @('--no-checkout') }
try {
$cloned = $false
+ $deferred = $false
foreach ($attempt in 1..3) {
# Blobless: every commit, tree and release tag (runtime identity is
# the nearest reachable release; -Commit pins and branch switches
@@ -959,7 +965,7 @@ function Stage-Repository {
# checkout and history walk (#129712).
$cloneLabel = "Cloning $RepoUrl ($Branch) into $InstallDir"
if ($attempt -gt 1) { $cloneLabel += " (attempt $attempt of 3)" }
- Invoke-Logged $cloneLabel { git clone @progress --filter=blob:none --branch $Branch $RepoUrl $tree }
+ Invoke-Logged $cloneLabel { git clone @progress --filter=blob:none @checkout --branch $Branch $RepoUrl $tree }
if (-not $LASTEXITCODE) { $cloned = $true; break }
Remove-Item -LiteralPath $tree -Recurse -Force -ErrorAction SilentlyContinue
if ($attempt -lt 3) { Start-Sleep -Seconds ($attempt * 5) }
@@ -969,15 +975,33 @@ function Stage-Repository {
# graph alone, then retry materializing the tree separately.
Write-Warn "direct clone failed; trying deferred checkout"
Invoke-Logged "Cloning history" { git clone @progress --filter=blob:none --no-checkout --branch $Branch $RepoUrl $tree }
- if (-not $LASTEXITCODE) {
- foreach ($attempt in 1..2) {
- Invoke-Logged "Checking out files (attempt $attempt of 2)" { git -C $tree reset --hard HEAD }
- if (-not $LASTEXITCODE) { $cloned = $true; break }
- if ($attempt -lt 2) { Start-Sleep -Seconds 5 }
+ if (-not $LASTEXITCODE) { $cloned = $true; $deferred = $true }
+ }
+ if (-not $cloned) { Fail "git clone failed; no checkout published" git_clone_failed }
+ if ($Commit) {
+ # Pin before publishing: a pinned or deferred clone has no files yet,
+ # and a failure here leaves only the staging dir, which finally removes.
+ 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 ($Commit -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.
+ $what = if ($Commit) { $Commit } else { "files" }
+ $materialized = $false
+ foreach ($attempt in 1..2) {
+ Invoke-Logged "Checking out $what (attempt $attempt of 2)" {
+ if ($Commit) { git -C $tree checkout --force --detach $Commit } else { git -C $tree reset --hard HEAD }
}
+ if (-not $LASTEXITCODE) { $materialized = $true; break }
+ if ($attempt -lt 2) { Start-Sleep -Seconds 5 }
}
+ if (-not $materialized) {
+ $what = if ($Commit) { $Commit } else { "the cloned tree" }
+ Fail "could not check out $what; no checkout published" git_checkout_failed
+ }
+ $pinned = [bool]$Commit
}
- if (-not $cloned) { Fail "git clone failed; no checkout published" git_clone_failed }
Move-Item -LiteralPath $tree -Destination $InstallDir
Disable-TreelessGraphWrites $InstallDir
Write-Ok "Hermes Agent cloned"
@@ -985,10 +1009,11 @@ function Stage-Repository {
Remove-Item -LiteralPath $staged -Recurse -Force -ErrorAction SilentlyContinue
}
}
- if ($Commit) {
- # A pin must come from the branch being installed: the complete marker
- # records both, and a commit off that branch would make the next plain
- # rerun "update" onto a different line.
+ if ($Commit -and -not $pinned) {
+ # A rerun over an existing checkout pins here (a fresh clone pinned
+ # before publishing). A pin must come from the branch being installed:
+ # the complete marker records both, and a commit off that branch would
+ # make the next plain rerun "update" onto a different line.
Invoke-Native { git -C $InstallDir merge-base --is-ancestor $Commit "origin/$Branch" 2>$null }
if ($LASTEXITCODE) { Fail "commit $Commit is not on branch $Branch" commit_not_on_branch }
Invoke-Logged "Pinning $Commit" { git -C $InstallDir checkout $Commit }
diff --git a/tests/scripts/install/test_install_ps1_native_calls.py b/tests/scripts/install/test_install_ps1_native_calls.py
index 92f35617b80..f7078e37f95 100644
--- a/tests/scripts/install/test_install_ps1_native_calls.py
+++ b/tests/scripts/install/test_install_ps1_native_calls.py
@@ -52,8 +52,8 @@ def _git(repo: Path, *args: str) -> str:
check=True, capture_output=True, text=True).stdout.strip()
-def _stage(origin: Path, home: Path, *extra: str) -> tuple[subprocess.CompletedProcess, dict]:
- env = dict(os.environ, HERMES_REPO_URL=str(origin), HERMES_HOME=str(home))
+def _stage(origin: Path, home: Path, *extra: str, **env_extra: str) -> tuple[subprocess.CompletedProcess, dict]:
+ env = dict(os.environ, HERMES_REPO_URL=str(origin), HERMES_HOME=str(home), **env_extra)
result = subprocess.run([_powershell(), "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", str(INSTALLER),
"-Stage", "repository", "-Json", *extra],
env=env, capture_output=True, text=True, timeout=180)
@@ -66,26 +66,75 @@ def test_rerun_parks_local_work_and_pins_only_branch_commits(tmp_path):
origin = tmp_path / "origin"
origin.mkdir()
_git(origin, "init", "-q", "-b", "main")
- (origin / "README").write_text("one")
+ (origin / "README").write_text("one", encoding="utf-8")
_git(origin, "add", "README")
_git(origin, "commit", "-qm", "one")
home = tmp_path / "home"
install = home / "hermes-agent"
assert _stage(origin, home)[1]["ok"] is True
- (install / "README").write_text("local edit")
- (origin / "README").write_text("two")
+ (install / "README").write_text("local edit", encoding="utf-8")
+ (origin / "README").write_text("two", encoding="utf-8")
_git(origin, "commit", "-qam", "two")
_git(origin, "checkout", "-qb", "side")
- (origin / "README").write_text("side")
+ (origin / "README").write_text("side", encoding="utf-8")
_git(origin, "commit", "-qam", "side")
off_branch = _git(origin, "rev-parse", "HEAD")
_git(origin, "checkout", "-q", "main")
result, frame = _stage(origin, home)
assert frame["ok"] is True, result.stdout + result.stderr
- assert (install / "README").read_text() == "two"
+ assert (install / "README").read_text(encoding="utf-8-sig") == "two"
assert "local edit" in _git(install, "stash", "show", "-p", "stash@{0}")
result, frame = _stage(origin, home, "-Commit", off_branch)
assert frame["ok"] is False
assert "is not on branch main" in frame["reason"]
+
+
+def _pin_fixture(tmp_path: Path) -> tuple[Path, str, str]:
+ """main: pin <- tip; side: a commit off main."""
+ origin = tmp_path / "origin"
+ origin.mkdir()
+ _git(origin, "init", "-q", "-b", "main")
+ (origin / "README").write_text("one", encoding="utf-8")
+ _git(origin, "add", "README")
+ _git(origin, "commit", "-qm", "one")
+ pin = _git(origin, "rev-parse", "HEAD")
+ (origin / "README").write_text("tip", encoding="utf-8")
+ _git(origin, "commit", "-qam", "tip")
+ _git(origin, "config", "uploadpack.allowFilter", "true")
+ _git(origin, "checkout", "-qb", "side")
+ (origin / "README").write_text("side", encoding="utf-8")
+ _git(origin, "commit", "-qam", "side")
+ off_branch = _git(origin, "rev-parse", "HEAD")
+ _git(origin, "checkout", "-q", "main")
+ return origin, pin, off_branch
+
+
+def _clone_argv(trace: Path) -> list[str]:
+ [line] = [line for line in trace.read_text(encoding="utf-8-sig", errors="replace").splitlines() if "built-in: git clone " in line]
+ return line.split("built-in: git clone ", 1)[1].split()
+
+
+def test_pinned_fresh_clone_never_materializes_the_branch_tip(tmp_path):
+ """A pinned install (the desktop bootstrap passes -Commit) checks out only the pin, as install.sh does."""
+ origin, pin, _ = _pin_fixture(tmp_path)
+ for name, extra, readme, no_checkout in (("pinned", ("-Commit", pin), "one", True), ("tip", (), "tip", False)):
+ home, trace = tmp_path / name, tmp_path / f"{name}.trace"
+ result, frame = _stage(origin, home, *extra, GIT_TRACE=str(trace))
+ assert frame["ok"] is True, result.stdout + result.stderr
+ install = home / "hermes-agent"
+ assert (install / "README").read_text(encoding="utf-8-sig") == readme
+ assert _git(install, "status", "--porcelain") == ""
+ assert ("--no-checkout" in _clone_argv(trace)) is no_checkout
+ assert _git(tmp_path / "pinned" / "hermes-agent", "rev-parse", "HEAD") == pin
+
+
+def test_pinned_fresh_clone_publishes_nothing_when_the_pin_is_refused(tmp_path):
+ origin, _, off_branch = _pin_fixture(tmp_path)
+ home = tmp_path / "home"
+ result, frame = _stage(origin, home, "-Commit", off_branch)
+ assert frame["ok"] is False
+ assert "is not on branch main" in frame["reason"]
+ assert not (home / "hermes-agent").exists()
+ assert not list(home.glob(".hermes-clone-*"))