The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): never reap a backend whose parent Electron is alive
YappLeCunta7b25b3c002e
diff --git a/apps/desktop/electron/backend-ownership.test.ts b/apps/desktop/electron/backend-ownership.test.tsindex b2afbae7b0b..98b03a3b5f9 100644--- a/apps/desktop/electron/backend-ownership.test.ts+++ b/apps/desktop/electron/backend-ownership.test.ts@@ -53,6 +53,8 @@ function deferred() {function createOwnership(store = memoryStore(), overrides: Partial<Parameters<typeof createBackendOwnership>[0]> = {}) {return createBackendOwnership({matchesIdentity: async () => true,+ // Unknown parent (no record / legacy) preserves the pre-parent behaviour.+ matchesParent: async () => undefined,stop: () => {},store,...overrides@@ -176,6 +178,67 @@ test('startup reap preserves failed stops for the next launch', async () => {assert.deepEqual(parseBackendOwnership(store.value()), [entry])})+test('startup reap never stops a backend whose parent Electron is still alive', async () => {+ const entry = { ...ownershipEntry({ pid: 54 }), parentPid: 100, parentStartMarker: 'os-start-parent' }+ const store = memoryStore(stored([entry]))+ const stop = vi.fn()++ const ownership = createOwnership(store, {+ matchesParent: async () => true,+ stop+ })++ assert.deepEqual(await ownership.reapOrphans(), [])+ assert.equal(stop.mock.calls.length, 0)+ assert.deepEqual(parseBackendOwnership(store.value()), [entry])+})++test('startup reap still reaps a backend whose parent is gone or reused', async () => {+ const gone = { ...ownershipEntry({ pid: 55 }), parentPid: 200, parentStartMarker: 'os-start-dead' }+ const reused = { ...ownershipEntry({ pid: 56 }), parentPid: 201, parentStartMarker: 'os-start-old' }+ const store = memoryStore(stored([gone, reused]))+ const stop = vi.fn()++ const ownership = createOwnership(store, {+ matchesParent: async entry => (entry.parentPid === 201 ? true : false),+ stop+ })++ assert.deepEqual(await ownership.reapOrphans(), [55])+ assert.deepEqual(stop.mock.calls, [[gone]])+ assert.deepEqual(parseBackendOwnership(store.value()), [reused])+})++test('startup reap preserves a record when parent liveness probing fails', async () => {+ const entry = { ...ownershipEntry({ pid: 57 }), parentPid: 300, parentStartMarker: 'os-start-parent' }+ const store = memoryStore(stored([entry]))+ const stop = vi.fn()++ const ownership = createOwnership(store, {+ matchesParent: async () => {+ throw new Error('process table unavailable')+ },+ stop+ })++ assert.deepEqual(await ownership.reapOrphans(), [])+ assert.equal(stop.mock.calls.length, 0)+ assert.deepEqual(parseBackendOwnership(store.value()), [entry])+})++test('claim persists the parent identity so a later reap can see it', async () => {+ const store = memoryStore()+ const ownership = createOwnership(store)+ const claim = { ...ownershipEntry(), parentPid: 42, parentStartMarker: 'os-start-parent' }++ const entry = await ownership.claim(claim)++ assert.equal(entry.parentPid, 42)+ assert.equal(entry.parentStartMarker, 'os-start-parent')+ assert.deepEqual(parseBackendOwnership(store.value())[0].parentPid, 42)+ assert.deepEqual(parseBackendOwnership(store.value())[0].parentStartMarker, 'os-start-parent')+})+test('release removes only the exact identity rather than every record for its PID', () => {const oldProcess = ownershipEntry({ nonce: 'old', startMarker: 'start-old' })const reusedPid = ownershipEntry({ nonce: 'new', startMarker: 'start-new' })diff --git a/apps/desktop/electron/backend-ownership.ts b/apps/desktop/electron/backend-ownership.tsindex feb50fb8e6b..32af3b2074b 100644--- a/apps/desktop/electron/backend-ownership.ts+++ b/apps/desktop/electron/backend-ownership.ts@@ -7,6 +7,10 @@ export interface BackendIdentity {export interface BackendOwnershipEntry extends BackendIdentity {command?: string+ /** PID of the Electron parent that spawned this backend, when known. */+ parentPid?: number+ /** Start marker of that parent, so a reused PID is not mistaken for it. */+ parentStartMarker?: string}export interface BackendOwnershipStore {@@ -16,12 +20,16 @@ export interface BackendOwnershipStore {export interface BackendOwnershipDeps {matchesIdentity: (identity: BackendIdentity) => Promise<boolean | undefined>+ /** True when the recorded parent is still running; undefined when unknown. */+ matchesParent: (entry: BackendOwnershipEntry) => Promise<boolean | undefined>stop: (identity: BackendIdentity) => Promise<void> | voidstore: BackendOwnershipStore}export interface BackendClaim extends BackendIdentity {command?: string+ parentPid?: number+ parentStartMarker?: string}function isNonEmptyString(value: unknown): value is string {@@ -88,6 +96,14 @@ export function parseBackendOwnership(contents: unknown): BackendOwnershipEntry[entry.command = candidate.command}+ if (Number.isInteger(candidate.parentPid) && Number(candidate.parentPid) > 0) {+ entry.parentPid = candidate.parentPid+ }++ if (isNonEmptyString(candidate.parentStartMarker)) {+ entry.parentStartMarker = candidate.parentStartMarker+ }+if (!entries.some(existing => identitiesMatch(existing, entry))) {entries.push(entry)}@@ -127,6 +143,14 @@ export function createBackendOwnership(deps: BackendOwnershipDeps) {entry.command = claim.command}+ if (Number.isInteger(claim.parentPid) && Number(claim.parentPid) > 0) {+ entry.parentPid = claim.parentPid+ }++ if (isNonEmptyString(claim.parentStartMarker)) {+ entry.parentStartMarker = claim.parentStartMarker+ }+try {const entries = read().filter(candidate => candidate.pid !== entry.pid)write([...entries, entry])@@ -162,6 +186,26 @@ export function createBackendOwnership(deps: BackendOwnershipDeps) {const reaped: number[] = []for (const entry of entries) {+ // A backend whose Electron parent is still running is NOT an orphan:+ // reaping it would kill a live instance's session. This is what stops+ // a second launch from SIGTERMing the running instance's backend even+ // if it reaches reapOrphans (see main.ts startHermes + #87295).+ let parentAlive: boolean | undefined++ try {+ parentAlive = await deps.matchesParent(entry)+ } catch {+ survivors.push(entry)++ continue+ }++ if (parentAlive === true) {+ survivors.push(entry)++ continue+ }+let matches: boolean | undefinedtry {diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.tsindex 483a80ca830..e47797b90d0 100644--- a/apps/desktop/electron/main.ts+++ b/apps/desktop/electron/main.ts@@ -3043,6 +3043,22 @@ async function backendIdentityMatches(identity) {return command === null ? undefined : backendCommandMatches(command)}+// True when the recorded parent Electron is still running (same PID AND start+// marker); false when it is gone or its PID was reused; undefined when the+// ownership record predates parent tracking. Undefined deliberately falls back+// to the pre-parent reap behaviour so legacy orphan cleanup keeps working.+async function backendParentMatches(entry) {+ if (!Number.isInteger(entry.parentPid) || typeof entry.parentStartMarker !== 'string' || !entry.parentStartMarker) {+ return undefined+ }++ try {+ return (await processStartMarker(entry.parentPid)) === entry.parentStartMarker+ } catch (error) {+ return error?.code === 'ENOENT' || error?.code === 'ESRCH' ? false : undefined+ }+}+async function stopOwnedBackend(identity) {if ((await processIdentityMatches(identity)) !== true) {return@@ -3092,6 +3108,7 @@ async function stopOwnedBackend(identity) {const backendOwnership = createBackendOwnership({matchesIdentity: backendIdentityMatches,+ matchesParent: backendParentMatches,stop: stopOwnedBackend,store: {read: () => {@@ -3120,7 +3137,12 @@ async function claimBackendChild(child, command, profile, nonce) {nonce,pid: child.pid,profile,- startMarker: await processStartMarker(child.pid)+ startMarker: await processStartMarker(child.pid),+ // Record the spawning Electron so reapOrphans can tell an orphaned+ // backend (parent gone) from one owned by a live instance — a live+ // parent's backend is never reaped (#87295).+ parentPid: process.pid,+ parentStartMarker: await desktopParentStartMarker()})child.hermesBackendIdentity = identity