Skip to content

The agent that grows with you

fix(desktop): re-score heuristic active-profile.json pins

Sergey Tiraspolskyf6c9cb7b9046
diff --git a/apps/desktop/electron/profile-migration.test.ts b/apps/desktop/electron/profile-migration.test.ts
index 7b361db192a..4996e289427 100644
--- a/apps/desktop/electron/profile-migration.test.ts
+++ b/apps/desktop/electron/profile-migration.test.ts
@@ -27,6 +27,7 @@ import {
PROFILE_SCORE_MIN_SIZE_BYTES,
profileGatewayPidPath,
profileStateDbPath,
+ readExistingPreference,
readLegacyActiveProfile,
scoreStateDb,
withDefaultCandidate
@@ -402,11 +403,20 @@ test('decideMigration still flags _migrated when legacy is invalid (undefined) b
// migrateActiveProfileIfMissing (orchestrator)
// ---------------------------------------------------------------------------
-test('migrateActiveProfileIfMissing is a no-op when the preference file exists', () => {
+test('migrateActiveProfileIfMissing is a no-op when a user-selected preference file exists', () => {
+ // No `_migrated` flag = explicit user/CLI choice. Even a huge other profile
+ // must not steal the pin.
let written: unknown = null
+ const fs = makeFs({
+ '/cfg/active-profile.json': { content: '{"profile":"coder"}' },
+ '/home/u/.hermes/profiles/coder': { dir: true },
+ '/home/u/.hermes/profiles/writer': { dir: true },
+ '/home/u/.hermes/profiles/writer/state.db': { size: 400 * 1024 * 1024, mtime: NOW - 86_400_000 }
+ })
+
const deps = baseDeps({
- existsSync: (p: string) => p === '/cfg/active-profile.json',
+ ...fs,
writeJson: (_p: string, payload: unknown) => {
written = payload
}
@@ -620,3 +630,58 @@ test('migrateActiveProfileIfMissing does not pin default when only default gatew
assert.equal(migrateActiveProfileIfMissing('/cfg/active-profile.json', deps), false)
assert.equal(written, null)
})
+
+test('readExistingPreference treats _migrated as heuristic-owned', () => {
+ const fs = makeFs({
+ '/cfg/active-profile.json': { content: '{"profile":"conduit","_migrated":true}' }
+ })
+
+ assert.deepEqual(readExistingPreference('/cfg/active-profile.json', fs.readFileSync), {
+ profile: 'conduit',
+ migrated: true
+ })
+})
+
+test('migrateActiveProfileIfMissing repairs a pre-existing heuristic pin when default now wins', () => {
+ // Sol P1 / #100576: file already exists with _migrated:true so first-boot
+ // skip left affected installs stuck. Re-score and clear.
+ let written: unknown = null
+
+ const fs = makeFs({
+ '/cfg/active-profile.json': { content: '{"profile":"conduit","_migrated":true}' },
+ '/home/u/.hermes/profiles/conduit': { dir: true },
+ '/home/u/.hermes/state.db': { size: 409 * 1024 * 1024, mtime: NOW - 86_400_000 },
+ '/home/u/.hermes/profiles/conduit/state.db': { size: 2 * 1024 * 1024, mtime: NOW - 60_000 }
+ })
+
+ const deps = baseDeps({
+ ...fs,
+ writeJson: (_p: string, payload: unknown) => {
+ written = payload
+ }
+ })
+
+ assert.equal(migrateActiveProfileIfMissing('/cfg/active-profile.json', deps), true)
+ assert.deepEqual(written, { profile: null })
+})
+
+test('migrateActiveProfileIfMissing leaves a still-correct heuristic pin alone', () => {
+ let written: unknown = null
+
+ const fs = makeFs({
+ '/cfg/active-profile.json': { content: '{"profile":"work","_migrated":true}' },
+ '/home/u/.hermes/profiles/work': { dir: true },
+ '/home/u/.hermes/state.db': { size: 5 * 1024 * 1024, mtime: NOW - 86_400_000 },
+ '/home/u/.hermes/profiles/work/state.db': { size: 200 * 1024 * 1024, mtime: NOW - 86_400_000 }
+ })
+
+ const deps = baseDeps({
+ ...fs,
+ writeJson: (_p: string, payload: unknown) => {
+ written = payload
+ }
+ })
+
+ assert.equal(migrateActiveProfileIfMissing('/cfg/active-profile.json', deps), false)
+ assert.equal(written, null)
+})
diff --git a/apps/desktop/electron/profile-migration.ts b/apps/desktop/electron/profile-migration.ts
index 0947872d5f9..bca67f55607 100644
--- a/apps/desktop/electron/profile-migration.ts
+++ b/apps/desktop/electron/profile-migration.ts
@@ -30,7 +30,7 @@ export interface MigrationDeps {
}
export interface MigrationDecision {
- profile: string
+ profile: string | null
/** True when chosen from the state.db heuristic (auto-detected), undefined when explicit. */
_migrated?: boolean
}
@@ -234,13 +234,47 @@ export function withDefaultCandidate(named: string[]): string[] {
}
/**
- * Orchestrator. Idempotent: writes at most once when the preference file is
- * missing. Thin on top of the decision helpers above; the testable surface is
- * `decideMigration` + the individual rung helpers, this function just glues them
- * to the deps bag.
+ * Read an existing active-profile.json. Returns null when missing/malformed.
+ * `_migrated: true` means the first-boot heuristic wrote it (safe to re-score).
+ * Absence of that flag is a user/CLI choice and must not be overwritten.
+ */
+export function readExistingPreference(
+ desktopProfileConfigPath: string,
+ readFile: MigrationDeps['readFileSync']
+): { profile: string | null; migrated: boolean } | null {
+ let parsed: unknown
+
+ try {
+ parsed = JSON.parse(readFile(desktopProfileConfigPath, 'utf8'))
+ } catch {
+ return null
+ }
+
+ if (!parsed || typeof parsed !== 'object') {
+ return null
+ }
+
+ const rec = parsed as { profile?: unknown; _migrated?: unknown }
+ const raw = typeof rec.profile === 'string' ? rec.profile.trim() : ''
+
+ return {
+ profile: raw || null,
+ migrated: rec._migrated === true
+ }
+}
+
+/**
+ * First-boot seed, plus repair of heuristic-owned files (`_migrated: true`).
+ * User-selected files (no `_migrated`) are never overwritten. When a repaired
+ * heuristic would now pick default, write `{ profile: null }` so Desktop drops
+ * `--profile` instead of pinning `default`.
*/
export function migrateActiveProfileIfMissing(desktopProfileConfigPath: string, deps: MigrationDeps): boolean {
- if (deps.existsSync(desktopProfileConfigPath)) {
+ const existing = deps.existsSync(desktopProfileConfigPath)
+ ? readExistingPreference(desktopProfileConfigPath, deps.readFileSync)
+ : null
+
+ if (existing && !existing.migrated) {
return false
}
@@ -258,6 +292,15 @@ export function migrateActiveProfileIfMissing(desktopProfileConfigPath: string,
// launches `hermes --profile default` and is worse than writing nothing
// (legacy sticky / implicit default). Covers a lone default gateway.pid.
if (!decision || decision.profile === 'default') {
+ if (existing?.migrated) {
+ deps.writeJson(desktopProfileConfigPath, { profile: null })
+ return true
+ }
+
+ return false
+ }
+
+ if (existing?.migrated && existing.profile === decision.profile) {
return false
}