Skip to content

The agent that grows with you

fix(desktop): keep bootstrap script aligned with checkout

JoaoMarcos4482cff2ed43b5
diff --git a/apps/desktop/electron/bootstrap-runner.test.ts b/apps/desktop/electron/bootstrap-runner.test.ts
index 82bdec3c062..1bc29d47f33 100644
--- a/apps/desktop/electron/bootstrap-runner.test.ts
+++ b/apps/desktop/electron/bootstrap-runner.test.ts
@@ -11,7 +11,6 @@ import {
cachedScriptPath,
cleanInstallerLogLine,
hasExistingGitCheckout,
- installedAgentInstallScript,
installRefForStamp,
isPinnedCommit,
resolveInstallScript,
@@ -50,24 +49,6 @@ test('runBootstrap bails immediately when the signal is already aborted', async
)
})
-test('installedAgentInstallScript resolves the installer in the agent checkout', () => {
- const home = mkTmpHome()
-
- try {
- assert.equal(installedAgentInstallScript(home), null, 'absent before the checkout exists')
-
- const scriptsDir = path.join(home, 'hermes-agent', 'scripts')
- fs.mkdirSync(scriptsDir, { recursive: true })
- const scriptPath = path.join(scriptsDir, SCRIPT_NAME)
- fs.writeFileSync(scriptPath, '#!/bin/sh\necho hi\n')
-
- assert.equal(installedAgentInstallScript(home), scriptPath)
- assert.equal(installedAgentInstallScript(null), null, 'null home -> null')
- } finally {
- fs.rmSync(home, { recursive: true, force: true })
- }
-})
-
test('existing checkout detection requires git metadata', () => {
const home = mkTmpHome()
@@ -117,7 +98,7 @@ test('fallback install stamps use an unpinned branch ref', () => {
assert.equal(isPinnedCommit(ZERO_COMMIT), false)
assert.deepEqual(installRefForStamp(stamp), {
ref: 'main',
- cacheKey: 'fallback-main',
+ cacheKey: 'branch-main',
pinned: false
})
// Must NOT pass -Commit / --commit for the all-zero placeholder.
@@ -132,6 +113,21 @@ test('fallback install stamps use an unpinned branch ref', () => {
)
})
+test('existing-checkout installer ref follows the branch instead of the packaged commit', () => {
+ const stamp = { commit: 'a'.repeat(40), branch: 'main' }
+
+ assert.deepEqual(installRefForStamp(stamp, { pinCommit: false }), {
+ ref: 'main',
+ cacheKey: 'branch-main',
+ pinned: false
+ })
+ assert.deepEqual(installRefForStamp(stamp), {
+ ref: stamp.commit,
+ cacheKey: stamp.commit,
+ pinned: true
+ })
+})
+
test('resolveMarkerPinnedCommit prefers installed checkout HEAD over the packaged artifact', () => {
const realHead = 'c'.repeat(40)
assert.equal(
@@ -159,14 +155,17 @@ test('resolveInstallScript downloads fallback stamps by branch instead of zero c
const home = mkTmpHome()
try {
- const logs = []
+ const cached = cachedScriptPath(home, 'branch-main')
+ fs.mkdirSync(path.dirname(cached), { recursive: true })
+ fs.writeFileSync(cached, 'stale branch installer\n')
+
const refs = []
const result = await resolveInstallScript({
installStamp: { commit: ZERO_COMMIT, branch: 'main' },
sourceRepoRoot: null,
hermesHome: home,
- emit: ev => logs.push(ev),
+ emit: () => {},
_download: async (ref, destPath) => {
refs.push(ref)
fs.mkdirSync(path.dirname(destPath), { recursive: true })
@@ -179,87 +178,102 @@ test('resolveInstallScript downloads fallback stamps by branch instead of zero c
assert.deepEqual(refs, ['main'])
assert.equal(result.source, 'download')
assert.equal(result.commit, null)
- assert.equal(result.path, cachedScriptPath(home, 'fallback-main'))
+ assert.equal(result.path, cached)
+ assert.match(fs.readFileSync(cached, 'utf8'), /fallback branch/)
} finally {
fs.rmSync(home, { recursive: true, force: true })
}
})
-test('resolveInstallScript prefers a cached script without touching the network', async () => {
+test('resolveInstallScript refreshes the live branch for an existing checkout', async () => {
const home = mkTmpHome()
try {
const commit = 'a'.repeat(40)
- const cached = cachedScriptPath(home, commit)
+ const cached = cachedScriptPath(home, 'branch-main')
fs.mkdirSync(path.dirname(cached), { recursive: true })
- fs.writeFileSync(cached, '#!/bin/sh\necho cached\n')
+ fs.writeFileSync(cached, 'stale installer\n')
- const logs = []
+ const refs = []
const result = await resolveInstallScript({
- installStamp: { commit },
+ installStamp: { commit, branch: 'main' },
sourceRepoRoot: null,
hermesHome: home,
- emit: ev => logs.push(ev)
+ emit: () => {},
+ pinCommit: false,
+ _download: async (ref, destPath) => {
+ refs.push(ref)
+ fs.writeFileSync(destPath, 'fresh branch installer\n')
+
+ return destPath
+ }
})
- assert.equal(result.source, 'cache')
+ assert.deepEqual(refs, ['main'])
+ assert.equal(result.source, 'download')
+ assert.equal(result.commit, null)
assert.equal(result.path, cached)
+ assert.equal(fs.readFileSync(cached, 'utf8'), 'fresh branch installer\n')
} finally {
fs.rmSync(home, { recursive: true, force: true })
}
})
-test('resolveInstallScript falls back to the installed agent checkout on a 404', async () => {
+test('resolveInstallScript refreshes an immutable-pin cache on a fresh install', async () => {
const home = mkTmpHome()
try {
const commit = 'a'.repeat(40)
- // Seed the installed agent checkout so the fallback has something to resolve.
- const scriptsDir = path.join(home, 'hermes-agent', 'scripts')
- fs.mkdirSync(scriptsDir, { recursive: true })
- const installed = path.join(scriptsDir, SCRIPT_NAME)
- fs.writeFileSync(installed, '#!/bin/sh\necho fallback\n')
+ const cached = cachedScriptPath(home, commit)
+ fs.mkdirSync(path.dirname(cached), { recursive: true })
+ fs.writeFileSync(cached, 'stale installer\n')
- const logs = []
+ const refs = []
const result = await resolveInstallScript({
- installStamp: { commit },
+ installStamp: { commit, branch: 'main' },
sourceRepoRoot: null,
hermesHome: home,
- emit: ev => logs.push(ev),
- // Simulate GitHub returning a 404 for the pinned commit.
- _download: async () => {
- throw new Error('Failed to download install.sh: HTTP 404')
+ emit: () => {},
+ _download: async (ref, destPath) => {
+ refs.push(ref)
+ fs.writeFileSync(destPath, 'fresh pinned installer\n')
+
+ return destPath
}
})
- assert.equal(result.source, 'installed-agent')
- // It should have copied the installer into the bootstrap cache.
- assert.equal(result.path, cachedScriptPath(home, commit))
- assert.ok(fs.existsSync(result.path), 'fallback script copied into cache')
+ assert.deepEqual(refs, [commit])
+ assert.equal(result.source, 'download')
+ assert.equal(result.commit, commit)
+ assert.equal(result.path, cached)
+ assert.equal(fs.readFileSync(cached, 'utf8'), 'fresh pinned installer\n')
} finally {
fs.rmSync(home, { recursive: true, force: true })
}
})
-test('resolveInstallScript rethrows when the 404 fallback is unavailable', async () => {
+test('resolveInstallScript fails closed instead of executing an installed stale script', async () => {
const home = mkTmpHome()
try {
const commit = 'a'.repeat(40)
- // No installed agent checkout seeded -> nothing to fall back to.
+ const scriptsDir = path.join(home, 'hermes-agent', 'scripts')
+ fs.mkdirSync(scriptsDir, { recursive: true })
+ fs.writeFileSync(path.join(scriptsDir, SCRIPT_NAME), 'stale installed script\n')
+
await assert.rejects(
resolveInstallScript({
- installStamp: { commit },
+ installStamp: { commit, branch: 'main' },
sourceRepoRoot: null,
hermesHome: home,
emit: () => {},
_download: async () => {
- throw new Error('Failed to download install.sh: HTTP 404')
+ throw new Error('Failed to download install script: HTTP 404')
}
}),
- /HTTP 404|Failed to download/
+ /HTTP 404/
)
} finally {
fs.rmSync(home, { recursive: true, force: true })
diff --git a/apps/desktop/electron/bootstrap-runner.ts b/apps/desktop/electron/bootstrap-runner.ts
index 396bb004677..d4a443d4af0 100644
--- a/apps/desktop/electron/bootstrap-runner.ts
+++ b/apps/desktop/electron/bootstrap-runner.ts
@@ -34,7 +34,6 @@
import { execFileSync, spawn } from 'node:child_process'
import fs from 'node:fs'
-import fsp from 'node:fs/promises'
import https from 'node:https'
import path from 'node:path'
@@ -137,13 +136,14 @@ function resolveMarkerPinnedCommit(
}
/**
- * Map an install stamp to the GitHub ref used to fetch install.ps1/sh.
- * Real CI/git stamps pin an immutable SHA. Non-git fallback stamps carry an
- * all-zero placeholder -- treat those as an unpinned branch ref so bootstrap
- * never asks GitHub for commit 0000000... (#50823).
+ * Map an install stamp to the same source identity the installer stages use.
+ * Fresh installs use the packaged immutable SHA. Existing checkouts
+ * intentionally ignore that old app pin and follow the branch, so the script
+ * must follow the branch too or old installer code can drive a newer tree.
+ * Non-git fallback stamps also follow the branch (#50823).
*/
-function installRefForStamp(installStamp) {
- if (installStamp && isPinnedCommit(installStamp.commit)) {
+function installRefForStamp(installStamp, { pinCommit = true } = {}) {
+ if (pinCommit && installStamp && isPinnedCommit(installStamp.commit)) {
return {
ref: installStamp.commit,
cacheKey: installStamp.commit,
@@ -151,12 +151,17 @@ function installRefForStamp(installStamp) {
}
}
- if (installStamp && typeof installStamp.commit === 'string' && FALLBACK_COMMIT_RE.test(installStamp.commit)) {
+ const validStamp =
+ installStamp &&
+ typeof installStamp.commit === 'string' &&
+ (isPinnedCommit(installStamp.commit) || FALLBACK_COMMIT_RE.test(installStamp.commit))
+
+ if (validStamp) {
const ref = installStamp.branch || FALLBACK_BRANCH
return {
ref,
- cacheKey: `fallback-${String(ref).replace(/[^0-9A-Za-z._-]/g, '_')}`,
+ cacheKey: `branch-${String(ref).replace(/[^0-9A-Za-z._-]/g, '_')}`,
pinned: false
}
}
@@ -203,26 +208,6 @@ function bootstrapCacheDir(hermesHome) {
return path.join(hermesHome, 'bootstrap-cache')
}
-// The install.sh / install.ps1 that ships inside the already-installed agent
-// checkout under ~/.hermes/hermes-agent. Used as a last-resort fallback when
-// the pinned commit can't be fetched from GitHub (e.g. a locally-built desktop
-// app stamped to an unpushed HEAD).
-function installedAgentInstallScript(hermesHome) {
- if (!hermesHome) {
- return null
- }
-
- const candidate = path.join(hermesHome, 'hermes-agent', 'scripts', installScriptName())
-
- try {
- fs.accessSync(candidate, fs.constants.R_OK)
-
- return candidate
- } catch {
- return null
- }
-}
-
function hasExistingGitCheckout(activeRoot) {
if (!activeRoot) {
return false
@@ -331,6 +316,7 @@ async function resolveInstallScript({
sourceRepoRoot,
hermesHome,
emit,
+ pinCommit = true,
_download = downloadInstallScript
}) {
// 1. Dev shortcut: prefer a local checkout's installer so we can iterate
@@ -344,10 +330,10 @@ async function resolveInstallScript({
return { path: localScript, source: 'local', kind: installScriptKind() }
}
- // 2. Packaged path: download from GitHub at the install stamp's ref.
- // Non-git fallback builds carry an all-zero commit; treat that as an
- // unpinned branch ref instead of trying to fetch a non-existent SHA.
- const installRef = installRefForStamp(installStamp)
+ // 2. Packaged path: download using the same identity policy as the stages.
+ // Fresh installs use the packaged commit; existing checkouts and non-git
+ // fallback builds follow the branch.
+ const installRef = installRefForStamp(installStamp, { pinCommit })
if (!installRef) {
throw new Error(
@@ -359,59 +345,20 @@ async function resolveInstallScript({
const cached = cachedScriptPath(hermesHome, installRef.cacheKey)
const resolvedCommit = installRef.pinned ? installRef.ref : null
- try {
- await fsp.access(cached, fs.constants.R_OK)
- emit({
- type: 'log',
- line: `[bootstrap] using cached ${installScriptName()} for ${installRef.ref.slice(0, 12)}`
- })
-
- return { path: cached, source: 'cache', commit: resolvedCommit, kind: installScriptKind() }
- } catch {
- // not cached; download
- }
-
+ // The cache is only this run's -File target, never a source of truth.
+ // Refreshing also makes old Desktop builds pick up installer fixes instead
+ // of repeatedly executing bytes cached before those fixes existed.
emit({
type: 'log',
line:
`[bootstrap] fetching ${installScriptName()} for ${installRef.ref.slice(0, 12)} from GitHub` +
- (installRef.pinned ? '' : ' (fallback, unpinned)')
+ (installRef.pinned ? '' : ' (unpinned branch)')
})
- try {
- await _download(installRef.ref, cached)
- emit({ type: 'log', line: `[bootstrap] saved to ${cached}` })
-
- return { path: cached, source: 'download', commit: resolvedCommit, kind: installScriptKind() }
- } catch (err) {
- // The pinned commit may not be fetchable from GitHub -- most commonly a
- // locally-built desktop app stamped to an unpushed HEAD (see
- // write-build-stamp.mjs fromLocalGit). Fall back to the installer that
- // ships inside the already-installed agent checkout so dev/self-builds can
- // still bootstrap instead of dying with a fatal 404.
- const installed = installedAgentInstallScript(hermesHome)
-
- if (installed) {
- emit({
- type: 'log',
- line:
- `[bootstrap] GitHub fetch failed (${err.message}); ` +
- `falling back to installed agent ${installScriptName()} at ${installed}`
- })
-
- try {
- fs.mkdirSync(path.dirname(cached), { recursive: true })
- fs.copyFileSync(installed, cached)
+ await _download(installRef.ref, cached)
+ emit({ type: 'log', line: `[bootstrap] saved to ${cached}` })
- return { path: cached, source: 'installed-agent', commit: resolvedCommit, kind: installScriptKind() }
- } catch {
- // Cache copy failed (read-only FS, etc.) -- use the source path directly.
- return { path: installed, source: 'installed-agent', commit: resolvedCommit, kind: installScriptKind() }
- }
- }
-
- throw err
- }
+ return { path: cached, source: 'download', commit: resolvedCommit, kind: installScriptKind() }
}
// ---------------------------------------------------------------------------
@@ -979,7 +926,7 @@ async function runBootstrap(opts) {
}
// 1. Resolve the platform installer.
- const scriptInfo = await resolveInstallScript({ installStamp, sourceRepoRoot, hermesHome, emit })
+ const scriptInfo = await resolveInstallScript({ installStamp, sourceRepoRoot, hermesHome, emit, pinCommit })
abortSignal?.throwIfAborted()
const installerKind = scriptInfo.kind || 'powershell'
@@ -1090,7 +1037,6 @@ export {
cachedScriptPath,
cleanInstallerLogLine,
hasExistingGitCheckout,
- installedAgentInstallScript,
installRefForStamp,
isPinnedCommit,
// Exposed for testability