hermes-agent/apps/desktop/electron/backend-connection-state.ts
ethernet b4a294fff9 Merge origin/main; keep PM as plugin dependency owner
Reconcile plugin declarations and validation through PM's atomic generation publication; preserve external runtimes, target markers, and conflict refusal. Keep one source-update completion owner and port upstream lifecycle changes to the PM desktop/runtime paths.
2026-09-17 13:52:05 -04:00

193 lines
5.4 KiB
TypeScript

export type BackendConnectionAttempt<TConnection> = {
generation: number
promise: Promise<TConnection> | null
}
export type BackendProcessOwner<TProcess> = {
generation: number
process: TProcess
}
interface PendingBackendStop<TProcess> {
process: TProcess
completion: Promise<void>
failed: boolean
}
export interface BackendConnectionState<TProcess, TConnection> {
startAttempt(): BackendConnectionAttempt<TConnection>
setPromise(attempt: BackendConnectionAttempt<TConnection>, promise: Promise<TConnection>): boolean
isCurrentAttempt(attempt: BackendConnectionAttempt<TConnection>): boolean
assertCurrentAttempt(attempt: BackendConnectionAttempt<TConnection>): void
attachProcess(attempt: BackendConnectionAttempt<TConnection>, process: TProcess): BackendProcessOwner<TProcess> | null
claimProcess(
attempt: BackendConnectionAttempt<TConnection>,
process: TProcess,
claim: (current: TProcess) => Promise<unknown>
): Promise<BackendProcessOwner<TProcess> | null>
clearForCurrentProcess(owner: BackendProcessOwner<TProcess>): boolean
clearPromiseForAttempt(attempt: BackendConnectionAttempt<TConnection>): boolean
getProcess(): TProcess | null
getPromise(): Promise<TConnection> | null
getPendingPromise(): Promise<TConnection> | null
invalidate(): TProcess | null
stopProcess(stop: (current: TProcess) => Promise<void>): Promise<void>
}
export function createBackendConnectionState<TProcess, TConnection>(): BackendConnectionState<TProcess, TConnection> {
let generation = 0
let process: TProcess | null = null
let promise: Promise<TConnection> | null = null
let pendingPromise: Promise<TConnection> | null = null
let stopping: PendingBackendStop<TProcess> | null = null
function invalidate(): TProcess | null {
const currentProcess = process
generation += 1
process = null
promise = null
pendingPromise = null
return currentProcess
}
return {
startAttempt(): BackendConnectionAttempt<TConnection> {
if (stopping) {
throw new Error('The previous backend has not stopped. Retry its shutdown before starting a replacement.')
}
return { generation, promise: null }
},
setPromise(attempt: BackendConnectionAttempt<TConnection>, nextPromise: Promise<TConnection>): boolean {
if (attempt.generation !== generation) {
return false
}
attempt.promise = nextPromise
promise = nextPromise
pendingPromise = nextPromise
void nextPromise.then(
() => {
if (attempt.generation === generation && promise === nextPromise) {
pendingPromise = null
}
},
() => {
if (attempt.generation === generation && promise === nextPromise) {
pendingPromise = null
}
}
)
return true
},
isCurrentAttempt(attempt: BackendConnectionAttempt<TConnection>): boolean {
return attempt.generation === generation
},
assertCurrentAttempt(attempt: BackendConnectionAttempt<TConnection>): void {
if (attempt.generation !== generation) {
throw new Error('Hermes backend start was superseded by a newer connection attempt.')
}
},
attachProcess(
attempt: BackendConnectionAttempt<TConnection>,
nextProcess: TProcess
): BackendProcessOwner<TProcess> | null {
if (attempt.generation !== generation) {
return null
}
process = nextProcess
return { generation, process: nextProcess }
},
async claimProcess(
attempt: BackendConnectionAttempt<TConnection>,
nextProcess: TProcess,
claim: (current: TProcess) => Promise<unknown>
): Promise<BackendProcessOwner<TProcess> | null> {
const owner = this.attachProcess(attempt, nextProcess)
if (!owner) {
return null
}
await claim(nextProcess)
return owner.generation === generation && process === nextProcess ? owner : null
},
clearForCurrentProcess(owner: BackendProcessOwner<TProcess>): boolean {
if (owner.generation !== generation || owner.process !== process) {
return false
}
process = null
promise = null
pendingPromise = null
return true
},
clearPromiseForAttempt(attempt: BackendConnectionAttempt<TConnection>): boolean {
if (attempt.generation !== generation || (promise !== null && attempt.promise !== promise)) {
return false
}
promise = null
pendingPromise = null
return true
},
getProcess(): TProcess | null {
return process
},
getPromise(): Promise<TConnection> | null {
return promise
},
getPendingPromise(): Promise<TConnection> | null {
return pendingPromise
},
invalidate,
stopProcess(stop: (current: TProcess) => Promise<void>): Promise<void> {
if (stopping && !stopping.failed) {
return stopping.completion
}
const current = stopping?.process ?? invalidate()
if (current === null) {
return Promise.resolve()
}
const completion = Promise.resolve()
.then((): Promise<void> => stop(current))
.then(
(): void => {
stopping = null
},
(error: unknown): never => {
pending.failed = true
throw error
}
)
const pending: PendingBackendStop<TProcess> = { process: current, completion, failed: false }
stopping = pending
return completion
}
}
}