hermes-agent/apps/desktop/electron/primary-profile-pin.test.ts
Hermes Agent b401fb2e49 fix(desktop): clear PrimaryProfilePin on current-owner exit/error and make one launch-profile decision per start (#108417)
Salvages the pin-clear half of PR #109059 (salch-cred) and goes further:

- The current child's 'error' and 'exit' handlers now clear the pin
  after the clearForCurrentProcess guard, so an unexpected exit cannot
  leave routing pinned to a dead backend while the respawn reads the
  new --profile. A stale exit (older child) still returns before the
  clear and never touches a newer primary's pin.
- The connection IIFE's catch clears the pin behind the attempt guard:
  a failed startup releases its routing identity; a superseded
  attempt's failure never clears the newer attempt's pin.
- resolveLaunchProfile(readPreference) makes the launch decision ONE
  read per attempt: startHermes() now derives routingProfile (pin,
  setActiveGatewayProfile, remote resolve, child env identity) and
  argvProfile (the --profile flag) from the same decision, instead of
  pinning primaryProfileKey() up top and re-reading
  readActiveDesktopProfile() deep inside the IIFE — the split that let
  a mid-startup hermes:profile:remember produce 'routing alpha,
  --profile beta'. Unset preference keeps the legacy flag-less launch.
- #108118's invariant is preserved: a live primary still answers
  primaryProfileKey() from the pin while a preference change lands, so
  no duplicate backend spawns mid-life.
2026-09-25 00:53:16 -05:00

65 lines
2.2 KiB
TypeScript

import assert from 'node:assert/strict'
import { test } from 'vitest'
import { PrimaryProfilePin, resolveLaunchProfile } from './primary-profile-pin'
test('a live primary keeps answering for its booted profile after the preference moves', () => {
const pin = new PrimaryProfilePin()
let preference: null | string = 'default'
// startHermes() boots the primary as the preference at that moment.
assert.equal(pin.pin(preference), 'default')
assert.equal(
pin.resolve(() => preference),
'default'
)
// hermes:profile:remember rewrites active-profile.json without re-homing.
preference = 'claude'
// Routing must still see the running primary as "default": otherwise a
// request for "default" falls through to the pool and a second backend is
// spawned for the same HERMES_HOME.
assert.equal(
pin.resolve(() => preference),
'default'
)
})
test('teardown releases the pin so the next start follows the preference', () => {
const pin = new PrimaryProfilePin()
pin.pin('default')
pin.clear()
assert.equal(pin.booted, null)
assert.equal(
pin.resolve(() => 'claude'),
'claude'
)
})
// #108417: one authoritative launch-profile decision per startup attempt.
// startHermes used to pin primaryProfileKey() at the top and separately
// re-read the preference deep inside the connection IIFE for --profile and
// the child env — two reads that a mid-startup hermes:profile:remember could
// split into "routing says alpha, argv says beta". resolveLaunchProfile makes
// them ONE read with two encodings of the unset case.
test('one launch decision feeds routing, argv, and env from the same read', () => {
const named = resolveLaunchProfile(() => 'beta')
assert.equal(named.routingProfile, 'beta')
assert.equal(named.argvProfile, 'beta')
// Unset preference: routing falls back to 'default', the launch argument
// keeps the legacy shape (no --profile flag at all).
const unset = resolveLaunchProfile(() => null)
assert.equal(unset.routingProfile, 'default')
assert.equal(unset.argvProfile, null)
const blank = resolveLaunchProfile(() => ' ')
assert.equal(blank.routingProfile, 'default')
assert.equal(blank.argvProfile, null)
})