Repository navigation
fix(desktop): reuse existing source installs #37471
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AJV20
wants to merge
6
commits into
NousResearch:main
Choose a base branch
from
AJV20:fix/desktop-existing-install
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
983189d
fix(desktop): reuse existing source installs
AJV20 1b1cd23
fix(desktop): cache existing runtime probe
AJV20 8e0a13a
Merge remote-tracking branch 'upstream/main' into fix/desktop-existin…
AJV20 eefe58b
Merge remote-tracking branch 'origin/main' into fix/desktop-existing-…
AJV20 28a48a2
Merge remote-tracking branch 'origin/main' into pr-37471
AJV20 fd52442
Merge remote-tracking branch 'origin/main' into fix/desktop-existing-…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
|
||
| if (isResolverCacheHit({ activeRoot, appVersion, readResolverCache, venvPython })) { | ||
| return true | ||
| } | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| 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 } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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' })) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.