mirror of
https://github.com/stablyai/orca.git
synced 2026-10-25 08:04:05 +00:00
* fix(runtime): add single-instance lock + owned-metadata clear to prevent orca-runtime.json corruption Closes #1312. Every AppImage/.app relaunch was booting a fresh Electron main that clobbered `<userData>/orca-runtime.json` and `agent-hooks/endpoint.env`. When the newest instance quit, metadata pointed at a dead pid and `orca status` reported `stale_bootstrap` even though the original Orca was still running. SIGKILL'd predecessors also left orphaned `o-<pid>-*.sock` files in userData. Three surgical changes: 1. `app.requestSingleInstanceLock()` in a new `src/main/startup/single-instance-lock.ts` helper, wired into `src/main/index.ts` after `configureDevUserDataPath(is.dev)` so dev and packaged runs lock in separate namespaces. Losing instances focus the primary's window via `second-instance` and quit without touching userData. 2. `clearRuntimeMetadataIfOwned(userData, pid, runtimeId)` in `runtime-metadata.ts` — compares both pid AND runtimeId against the current file before clearing, so the auto-updater handoff window never erases the replacement process's fresh bootstrap. Called from a rewritten `will-quit` handler that folds `runtimeRpc.stop()` + owned-clear into the same `Promise.allSettled([disconnectDaemon, …]).then(app.quit)` chain (inside the `!daemonDisconnectDone` guard so the second-pass re-entry can't re-invoke stop+clear). 3. `sweepOrphanedRuntimeSockets()` in `runtime-rpc.ts` runs at the top of `start()` on POSIX, using `process.kill(pid, 0)` to probe liveness and remove `o-<dead-pid>-*.sock` orphans left by SIGKILL/OOM-kill. Tests (37 new/updated): - `single-instance-lock.test.ts` (3): lock-failed does not register listener; lock-acquired registers exactly one; callback dispatches correctly. - `runtime-metadata.test.ts` (+4): clearRuntimeMetadataIfOwned matched / pid-mismatch / runtimeId-mismatch / no-file branches. - `runtime-socket-sweep.test.ts` (4): own-pid-skip / alive-retain / dead-sweep / regex-miss separated via synthetic ownPid=1; two regex-invariant tests assert the sweep regex matches the real `createRuntimeTransportMetadata` output (including the 'rt' fallback). Design doc: `docs/fix-missing-single-instance-lock.md`. Co-authored-by: Orca <help@stably.ai> * fix(runtime): focus hidden windows on second-instance event focus() alone is a silent no-op when the primary window is hidden (close-to-tray on macOS via Cmd+W, or on a different macOS Space) or behind other apps on Windows. Call show() before focus() so a second launch attempt reliably surfaces the existing window regardless of state. Co-authored-by: Orca <help@stably.ai> --------- Co-authored-by: Orca <help@stably.ai>
68 lines
2.4 KiB
TypeScript
68 lines
2.4 KiB
TypeScript
import type { App } from 'electron'
|
|
import { describe, expect, it, vi } from 'vitest'
|
|
import { acquireSingleInstanceLock } from './single-instance-lock'
|
|
|
|
type Listener = (...args: unknown[]) => void
|
|
|
|
function makeFakeApp(lockResult: boolean): {
|
|
app: App
|
|
requestSingleInstanceLock: ReturnType<typeof vi.fn>
|
|
on: ReturnType<typeof vi.fn>
|
|
listeners: Record<string, Listener[]>
|
|
} {
|
|
const listeners: Record<string, Listener[]> = {}
|
|
const requestSingleInstanceLock = vi.fn(() => lockResult)
|
|
const on = vi.fn((event: string, cb: Listener) => {
|
|
listeners[event] = listeners[event] ?? []
|
|
listeners[event].push(cb)
|
|
})
|
|
const app = {
|
|
requestSingleInstanceLock,
|
|
on
|
|
} as unknown as App
|
|
return { app, requestSingleInstanceLock, on, listeners }
|
|
}
|
|
|
|
describe('acquireSingleInstanceLock', () => {
|
|
it('returns false and does NOT register second-instance when the lock is held', () => {
|
|
const onSecondInstance = vi.fn()
|
|
const fake = makeFakeApp(false)
|
|
|
|
const acquired = acquireSingleInstanceLock(fake.app, onSecondInstance)
|
|
|
|
expect(acquired).toBe(false)
|
|
expect(fake.requestSingleInstanceLock).toHaveBeenCalledTimes(1)
|
|
// Why: if we registered the listener on a losing process, focusing the
|
|
// existing window would become our job even though the primary owns
|
|
// that UX surface. Verify no listener was added.
|
|
expect(fake.on).not.toHaveBeenCalled()
|
|
expect(fake.listeners['second-instance']).toBeUndefined()
|
|
})
|
|
|
|
it('returns true and registers exactly one second-instance listener when the lock is acquired', () => {
|
|
const onSecondInstance = vi.fn()
|
|
const fake = makeFakeApp(true)
|
|
|
|
const acquired = acquireSingleInstanceLock(fake.app, onSecondInstance)
|
|
|
|
expect(acquired).toBe(true)
|
|
expect(fake.requestSingleInstanceLock).toHaveBeenCalledTimes(1)
|
|
expect(fake.on).toHaveBeenCalledTimes(1)
|
|
expect(fake.on).toHaveBeenCalledWith('second-instance', onSecondInstance)
|
|
expect(fake.listeners['second-instance']).toHaveLength(1)
|
|
})
|
|
|
|
it('fires the registered callback when second-instance dispatches', () => {
|
|
const onSecondInstance = vi.fn()
|
|
const fake = makeFakeApp(true)
|
|
|
|
acquireSingleInstanceLock(fake.app, onSecondInstance)
|
|
|
|
const [registered] = fake.listeners['second-instance'] ?? []
|
|
expect(registered).toBeDefined()
|
|
registered?.()
|
|
|
|
expect(onSecondInstance).toHaveBeenCalledTimes(1)
|
|
})
|
|
})
|