Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/desktop/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ npm run test:desktop:all

Boot logs land in `HERMES_HOME/logs/desktop.log` (includes backend output and recent Python tracebacks) — check it first if the app reports a boot failure.

If you already have a CLI-first source install in the standard `HERMES_HOME/hermes-agent` location, Desktop reuses it automatically. To force Desktop to ignore that existing install and run the isolated first-launch bootstrap path instead, launch with `HERMES_DESKTOP_IGNORE_EXISTING=1`.

**macOS / Linux:**

```bash
Expand Down
37 changes: 36 additions & 1 deletion apps/desktop/electron/main.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const { probeGatewayWebSocket } = require('./gateway-ws-probe.cjs')
const { adoptServedDashboardToken } = require('./dashboard-token.cjs')
const { waitForDashboardPort } = require('./backend-ready.cjs')
const { serializeJsonBody, setJsonRequestHeaders } = require('./oauth-net-request.cjs')
const { hasUsableActiveInstall } = require('./runtime-resolver.cjs')
const { fetchMarketplaceThemes, searchMarketplaceThemes } = require('./vscode-marketplace.cjs')
const { buildDesktopBackendEnv } = require('./backend-env.cjs')
const { readDirForIpc } = require('./fs-read-dir.cjs')
Expand Down Expand Up @@ -275,6 +276,12 @@ const BOOTSTRAP_MARKER_SCHEMA_VERSION = 1

const DESKTOP_CONNECTION_CONFIG_PATH = path.join(app.getPath('userData'), 'connection.json')
const DESKTOP_UPDATE_CONFIG_PATH = path.join(app.getPath('userData'), 'updates.json')
// Cache the successful CLI import probe used by the CLI-first runtime resolver.
// Spawning Python is expensive on some cold-start paths (notably Windows with
// antivirus), so a same-version Desktop launch can skip the subprocess after a
// known-good result. The cache lives in Electron userData and is invalidated by
// the app version, active root, and venv Python path.
const DESKTOP_RESOLVER_CACHE_PATH = path.join(app.getPath('userData'), 'resolverCache.json')
// active-profile.json records which Hermes profile the desktop launches its
// local backend as. When set, startHermes() passes `hermes --profile <name>
// dashboard …`, which deterministically pins HERMES_HOME (see
Expand Down Expand Up @@ -2305,7 +2312,35 @@ function resolveHermesBackend(dashboardArgs) {
return createActiveBackend(dashboardArgs)
}

// 4. Existing `hermes` on PATH -- installed via install.ps1 / install.sh from
// 4. Existing source install at ACTIVE_HERMES_ROOT without a desktop marker.
// This is the common CLI-first path on macOS/Linux: Finder-launched apps
// often cannot see ~/.local/bin on PATH, but the canonical checkout and
// venv are already present and runnable. Reuse it without writing the
// desktop bootstrap marker so Desktop does not claim ownership of a
// manual install.
if (
process.env.HERMES_DESKTOP_IGNORE_EXISTING !== '1' &&
hasUsableActiveInstall({
activeRoot: ACTIVE_HERMES_ROOT,
appVersion: resolveHermesVersion(),
canImportHermesCli,
fileExists,
getVenvPython,
isHermesSourceRoot,
readResolverCache: () => readJson(DESKTOP_RESOLVER_CACHE_PATH),
rememberLog,
venvRoot: VENV_ROOT,
writeResolverCache: cache => {
fs.mkdirSync(path.dirname(DESKTOP_RESOLVER_CACHE_PATH), { recursive: true })
writeFileAtomic(DESKTOP_RESOLVER_CACHE_PATH, JSON.stringify(cache, null, 2) + '\n', 'utf8')
}
})
) {
rememberLog(`Using existing Hermes source install at ${ACTIVE_HERMES_ROOT} without desktop bootstrap marker.`)
return createActiveBackend(dashboardArgs)
}

// 5. Existing `hermes` on PATH -- installed via install.ps1 / install.sh from
// a previous tool-only setup, or pip-installed system-wide. Use it but
// do NOT write a bootstrap marker; the user did this themselves and we
// don't want to take ownership of an install we didn't perform.
Expand Down
86 changes: 86 additions & 0 deletions apps/desktop/electron/runtime-resolver.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
'use strict'

const RESOLVER_CACHE_SCHEMA_VERSION = 1

/**
* Small, dependency-injected helpers for deciding whether Desktop can reuse an
* already-installed Hermes Agent runtime. Keep this separate from main.cjs so
* the decision is unit-testable without booting Electron.
*/
function hasUsableActiveInstall(opts) {
const {
activeRoot,
appVersion,
canImportHermesCli,
fileExists,
getVenvPython,
isHermesSourceRoot,
readResolverCache,
rememberLog,
venvRoot,
writeResolverCache
} = opts || {}

if (!activeRoot || !venvRoot) return false
if (typeof isHermesSourceRoot !== 'function' || typeof fileExists !== 'function') return false
if (typeof getVenvPython !== 'function' || typeof canImportHermesCli !== 'function') return false

if (!isHermesSourceRoot(activeRoot)) return false

const venvPython = getVenvPython(venvRoot)
if (!venvPython || !fileExists(venvPython)) return false

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This positive cache can survive an in-place venv/dependency regression because its key contains only Desktop version and paths. Current main deliberately performs a fresh import-health probe to avoid selecting broken venvs; preserve that guarantee or extend invalidation to cover runtime changes.

if (isResolverCacheHit({ activeRoot, appVersion, readResolverCache, venvPython })) {
return true
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please pass the active source root through the probe environment. Current main validates this runtime with PYTHONPATH containing ACTIVE_HERMES_ROOT (main.ts:3098-3102); without it, a valid source-tree venv that does not separately install hermes_cli will be rejected.

if (!canImportHermesCli(venvPython)) {
if (typeof rememberLog === 'function') {
rememberLog(`Ignoring existing Hermes install at ${activeRoot}: hermes_cli is not importable from ${venvPython}.`)
}
return false
}

writeResolverCacheHit({ activeRoot, appVersion, venvPython, writeResolverCache })
return true
}

function isResolverCacheHit(opts) {
const { activeRoot, appVersion, readResolverCache, venvPython } = opts || {}
if (!activeRoot || !appVersion || !venvPython || typeof readResolverCache !== 'function') return false

try {
const cache = readResolverCache()
return Boolean(
cache &&
cache.schemaVersion === RESOLVER_CACHE_SCHEMA_VERSION &&
cache.activeRoot === activeRoot &&
cache.venvPython === venvPython &&
cache.appVersion === appVersion &&
cache.canImportHermesCli === true
)
} catch {
return false
}
}

function writeResolverCacheHit(opts) {
const { activeRoot, appVersion, venvPython, writeResolverCache } = opts || {}
if (!activeRoot || !appVersion || !venvPython || typeof writeResolverCache !== 'function') return

try {
writeResolverCache({
schemaVersion: RESOLVER_CACHE_SCHEMA_VERSION,
activeRoot,
venvPython,
appVersion,
canImportHermesCli: true,
checkedAt: new Date().toISOString()
})
} catch {
// Cache writes are strictly an optimization; runtime resolution must not
// fail just because userData is read-only or temporarily unavailable.
}
}

module.exports = { hasUsableActiveInstall, isResolverCacheHit, writeResolverCacheHit }
138 changes: 138 additions & 0 deletions apps/desktop/electron/runtime-resolver.test.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
'use strict'

const assert = require('node:assert/strict')
const test = require('node:test')

const { hasUsableActiveInstall, isResolverCacheHit, writeResolverCacheHit } = require('./runtime-resolver.cjs')

function baseOpts(overrides = {}) {
return {
activeRoot: '/home/user/.hermes/hermes-agent',
canImportHermesCli: () => true,
fileExists: () => true,
getVenvPython: venvRoot => `${venvRoot}/bin/python`,
isHermesSourceRoot: () => true,
rememberLog: () => {},
venvRoot: '/home/user/.hermes/hermes-agent/venv',
...overrides
}
}

test('hasUsableActiveInstall accepts a CLI-first source checkout with a working venv', () => {
assert.equal(hasUsableActiveInstall(baseOpts()), true)
})

test('hasUsableActiveInstall rejects missing source roots and venvs', () => {
assert.equal(hasUsableActiveInstall(baseOpts({ isHermesSourceRoot: () => false })), false)
assert.equal(hasUsableActiveInstall(baseOpts({ fileExists: () => false })), false)
})

test('hasUsableActiveInstall rejects venvs that cannot import hermes_cli and logs why', () => {
const lines = []
const ok = hasUsableActiveInstall(
baseOpts({
canImportHermesCli: () => false,
rememberLog: line => lines.push(line)
})
)

assert.equal(ok, false)
assert.match(lines.join('\n'), /hermes_cli is not importable/)
})

test('hasUsableActiveInstall skips the Python import probe when resolver cache matches', () => {
let importProbeCount = 0
const ok = hasUsableActiveInstall(
baseOpts({
appVersion: '1.2.3',
canImportHermesCli: () => {
importProbeCount += 1
return false
},
readResolverCache: () => ({
schemaVersion: 1,
activeRoot: '/home/user/.hermes/hermes-agent',
venvPython: '/home/user/.hermes/hermes-agent/venv/bin/python',
appVersion: '1.2.3',
canImportHermesCli: true
})
})
)

assert.equal(ok, true)
assert.equal(importProbeCount, 0)
})

test('hasUsableActiveInstall invalidates resolver cache on app version change', () => {
let importProbeCount = 0
const ok = hasUsableActiveInstall(
baseOpts({
appVersion: '1.2.4',
canImportHermesCli: () => {
importProbeCount += 1
return true
},
readResolverCache: () => ({
schemaVersion: 1,
activeRoot: '/home/user/.hermes/hermes-agent',
venvPython: '/home/user/.hermes/hermes-agent/venv/bin/python',
appVersion: '1.2.3',
canImportHermesCli: true
})
})
)

assert.equal(ok, true)
assert.equal(importProbeCount, 1)
})

test('hasUsableActiveInstall invalidates resolver cache for a different install path', () => {
let importProbeCount = 0
const ok = hasUsableActiveInstall(
baseOpts({
appVersion: '1.2.3',
canImportHermesCli: () => {
importProbeCount += 1
return true
},
readResolverCache: () => ({
schemaVersion: 1,
activeRoot: '/tmp/other/hermes-agent',
venvPython: '/tmp/other/hermes-agent/venv/bin/python',
appVersion: '1.2.3',
canImportHermesCli: true
})
})
)

assert.equal(ok, true)
assert.equal(importProbeCount, 1)
})

test('hasUsableActiveInstall writes a positive resolver cache after a successful probe', () => {
let written = null
const ok = hasUsableActiveInstall(
baseOpts({
appVersion: '1.2.3',
writeResolverCache: cache => {
written = cache
}
})
)

assert.equal(ok, true)
assert.equal(written.schemaVersion, 1)
assert.equal(written.activeRoot, '/home/user/.hermes/hermes-agent')
assert.equal(written.venvPython, '/home/user/.hermes/hermes-agent/venv/bin/python')
assert.equal(written.appVersion, '1.2.3')
assert.equal(written.canImportHermesCli, true)
})

test('resolver cache helpers tolerate missing callbacks and malformed cache data', () => {
assert.equal(isResolverCacheHit({ activeRoot: 'a', appVersion: '1', venvPython: 'p' }), false)
assert.equal(
isResolverCacheHit({ activeRoot: 'a', appVersion: '1', venvPython: 'p', readResolverCache: () => null }),
false
)
assert.doesNotThrow(() => writeResolverCacheHit({ activeRoot: 'a', appVersion: '1', venvPython: 'p' }))
})
2 changes: 1 addition & 1 deletion apps/desktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"test:desktop:nsis": "node scripts/test-desktop.mjs nsis",
"test:desktop:existing": "node scripts/test-desktop.mjs existing",
"test:desktop:fresh": "node scripts/test-desktop.mjs fresh",
"test:desktop:platforms": "node --test electron/bootstrap-platform.test.cjs electron/hardening.test.cjs electron/backend-env.test.cjs electron/backend-probes.test.cjs electron/bootstrap-runner.test.cjs electron/connection-config.test.cjs electron/dashboard-token.test.cjs electron/gateway-ws-probe.test.cjs electron/oauth-net-request.test.cjs electron/desktop-uninstall.test.cjs electron/session-windows.test.cjs electron/workspace-cwd.test.cjs electron/fs-read-dir.test.cjs electron/git-root.test.cjs electron/windows-child-process.test.cjs electron/update-remote.test.cjs",
"test:desktop:platforms": "node --test electron/bootstrap-platform.test.cjs electron/hardening.test.cjs electron/backend-probes.test.cjs electron/bootstrap-runner.test.cjs electron/connection-config.test.cjs electron/dashboard-token.test.cjs electron/gateway-ws-probe.test.cjs electron/oauth-net-request.test.cjs electron/desktop-uninstall.test.cjs electron/session-windows.test.cjs electron/workspace-cwd.test.cjs electron/fs-read-dir.test.cjs electron/git-root.test.cjs electron/windows-child-process.test.cjs electron/update-remote.test.cjs electron/runtime-resolver.test.cjs electron/backend-env.test.cjs",
"typecheck": "tsc -p . --noEmit",
"lint": "eslint src/ electron/",
"lint:fix": "eslint src/ electron/ --fix",
Expand Down