|
| 1 | +import type { AppLogBackgroundProcess, AppLogLiveSnapshot } from '@agent-device/contracts/platform'; |
| 2 | + |
| 3 | +export type PidScopedProcess = Readonly<{ |
| 4 | + pid: string; |
| 5 | + process: AppLogBackgroundProcess; |
| 6 | +}>; |
| 7 | + |
| 8 | +export type PidScopedProcessMonitor = Readonly<{ |
| 9 | + initialProcess: PidScopedProcess | undefined; |
| 10 | + stopped(): boolean; |
| 11 | + setActive(process: AppLogBackgroundProcess | undefined): void; |
| 12 | + setState(state: AppLogLiveSnapshot['state']): void; |
| 13 | + resolvePid(): Promise<string>; |
| 14 | + startProcess(pid: string): Promise<AppLogBackgroundProcess>; |
| 15 | + sleep(milliseconds: number, signal?: AbortSignal): Promise<void>; |
| 16 | +}>; |
| 17 | + |
| 18 | +type SubsequentProcessStart = |
| 19 | + | Readonly<{ status: 'stopped' }> |
| 20 | + | Readonly<{ status: 'waiting' }> |
| 21 | + | Readonly<{ status: 'started'; process: PidScopedProcess }>; |
| 22 | + |
| 23 | +/** Monitors a PID-scoped stream and rotates it when the application process changes. */ |
| 24 | +export async function monitorPidScopedProcess(input: PidScopedProcessMonitor): Promise<void> { |
| 25 | + let process = input.initialProcess; |
| 26 | + while (!input.stopped()) { |
| 27 | + if (process) { |
| 28 | + await settleActiveProcess(input, process); |
| 29 | + process = undefined; |
| 30 | + await pauseBeforeProcessRestart(input); |
| 31 | + continue; |
| 32 | + } |
| 33 | + |
| 34 | + const started = await startAndAdoptSubsequentProcess(input); |
| 35 | + if (started.status === 'stopped') return; |
| 36 | + if (started.status === 'waiting') continue; |
| 37 | + process = started.process; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +async function settleActiveProcess( |
| 42 | + input: PidScopedProcessMonitor, |
| 43 | + scopedProcess: PidScopedProcess, |
| 44 | +): Promise<void> { |
| 45 | + const { process } = scopedProcess; |
| 46 | + input.setActive(process); |
| 47 | + try { |
| 48 | + input.setState('active'); |
| 49 | + await observeActiveProcess(input, scopedProcess); |
| 50 | + } finally { |
| 51 | + await disposeAdoptedProcess(input, process); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +async function observeActiveProcess( |
| 56 | + input: PidScopedProcessMonitor, |
| 57 | + scopedProcess: PidScopedProcess, |
| 58 | +): Promise<void> { |
| 59 | + const wait = observeProcessWait(scopedProcess.process); |
| 60 | + try { |
| 61 | + await pollActiveProcessPid(input, scopedProcess, wait); |
| 62 | + await terminateStoppedProcess(input, scopedProcess.process, wait); |
| 63 | + await assertProcessWaitSucceeded(wait.outcome); |
| 64 | + } finally { |
| 65 | + wait.controller.abort(); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +type ProcessWaitObservation = Readonly<{ |
| 70 | + controller: AbortController; |
| 71 | + outcome: Promise<Readonly<{ status: 'exited' }> | Readonly<{ status: 'failed'; error: unknown }>>; |
| 72 | + settled(): boolean; |
| 73 | +}>; |
| 74 | + |
| 75 | +function observeProcessWait(process: AppLogBackgroundProcess): ProcessWaitObservation { |
| 76 | + const controller = new AbortController(); |
| 77 | + let settled = false; |
| 78 | + const finish = () => { |
| 79 | + settled = true; |
| 80 | + controller.abort(); |
| 81 | + }; |
| 82 | + const outcome = process.wait.then( |
| 83 | + () => { |
| 84 | + finish(); |
| 85 | + return { status: 'exited' } as const; |
| 86 | + }, |
| 87 | + (error: unknown) => { |
| 88 | + finish(); |
| 89 | + return { status: 'failed', error } as const; |
| 90 | + }, |
| 91 | + ); |
| 92 | + return { controller, outcome, settled: () => settled }; |
| 93 | +} |
| 94 | + |
| 95 | +async function pollActiveProcessPid( |
| 96 | + input: PidScopedProcessMonitor, |
| 97 | + scopedProcess: PidScopedProcess, |
| 98 | + wait: ProcessWaitObservation, |
| 99 | +): Promise<void> { |
| 100 | + while (!input.stopped() && !wait.settled()) { |
| 101 | + if (!(await waitForPidPoll(input, wait))) return; |
| 102 | + const observedPid = await input.resolvePid(); |
| 103 | + if (input.stopped()) return; |
| 104 | + if (observedPid === scopedProcess.pid) continue; |
| 105 | + await scopedProcess.process.terminate(); |
| 106 | + return; |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +async function waitForPidPoll( |
| 111 | + input: PidScopedProcessMonitor, |
| 112 | + wait: ProcessWaitObservation, |
| 113 | +): Promise<boolean> { |
| 114 | + try { |
| 115 | + await input.sleep(500, wait.controller.signal); |
| 116 | + } catch (error) { |
| 117 | + if (!wait.settled()) throw error; |
| 118 | + } |
| 119 | + return !wait.settled(); |
| 120 | +} |
| 121 | + |
| 122 | +async function terminateStoppedProcess( |
| 123 | + input: PidScopedProcessMonitor, |
| 124 | + process: AppLogBackgroundProcess, |
| 125 | + wait: ProcessWaitObservation, |
| 126 | +): Promise<void> { |
| 127 | + if (input.stopped() && !wait.settled()) await process.terminate(); |
| 128 | +} |
| 129 | + |
| 130 | +async function assertProcessWaitSucceeded( |
| 131 | + outcome: ProcessWaitObservation['outcome'], |
| 132 | +): Promise<void> { |
| 133 | + const settled = await outcome; |
| 134 | + if (settled.status === 'failed') throw settled.error; |
| 135 | +} |
| 136 | + |
| 137 | +async function pauseBeforeProcessRestart(input: PidScopedProcessMonitor): Promise<void> { |
| 138 | + if (input.stopped()) return; |
| 139 | + input.setState('recovering'); |
| 140 | + await input.sleep(500); |
| 141 | +} |
| 142 | + |
| 143 | +async function startAndAdoptSubsequentProcess( |
| 144 | + input: PidScopedProcessMonitor, |
| 145 | +): Promise<SubsequentProcessStart> { |
| 146 | + const pid = await input.resolvePid(); |
| 147 | + if (input.stopped()) return { status: 'stopped' }; |
| 148 | + if (!pid) { |
| 149 | + input.setState('recovering'); |
| 150 | + await input.sleep(1_000); |
| 151 | + return { status: 'waiting' }; |
| 152 | + } |
| 153 | + const process = await input.startProcess(pid); |
| 154 | + input.setActive(process); |
| 155 | + if (!input.stopped()) return { status: 'started', process: { pid, process } }; |
| 156 | + await stopAdoptedProcess(input, process); |
| 157 | + return { status: 'stopped' }; |
| 158 | +} |
| 159 | + |
| 160 | +async function stopAdoptedProcess( |
| 161 | + input: PidScopedProcessMonitor, |
| 162 | + process: AppLogBackgroundProcess, |
| 163 | +): Promise<void> { |
| 164 | + try { |
| 165 | + await process.terminate(); |
| 166 | + await process.wait; |
| 167 | + } finally { |
| 168 | + await disposeAdoptedProcess(input, process); |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +async function disposeAdoptedProcess( |
| 173 | + input: PidScopedProcessMonitor, |
| 174 | + process: AppLogBackgroundProcess, |
| 175 | +): Promise<void> { |
| 176 | + try { |
| 177 | + await process[Symbol.asyncDispose](); |
| 178 | + } finally { |
| 179 | + input.setActive(undefined); |
| 180 | + } |
| 181 | +} |
0 commit comments