()
| 267 | * declined — the caller then falls back to managed-spawn. |
| 268 | */ |
| 269 | const ensureSupervisedConnection = async (): Promise<SidecarConnection | null> => { |
| 270 | // 1. Already running → attach. |
| 271 | const attached = await attachToSupervisedDaemon(); |
| 272 | if (attached) { |
| 273 | return acceptSupervisedConnection(attached); |
| 274 | } |
| 275 | |
| 276 | // Headless/e2e: the first-run background-service prompt and the systemd/ |
| 277 | // launchd install both need a user (and a real session bus) present. With |
| 278 | // this set, fall straight through to managed-spawn after the attach attempt |
| 279 | // so the packaged app can be driven without a window server. Mirrors |
| 280 | // EXECUTOR_TEST_AUTO_CONFIRM_RESET in reset-state.ts. |
| 281 | if (process.env.EXECUTOR_TEST_SKIP_BACKGROUND_SERVICE === "1") return null; |
| 282 | |
| 283 | const status = await supervisedServiceStatus(); |
| 284 | if (!status.supported) return null; |
| 285 | |
| 286 | // 2. Registered and running, but the first attach missed it. Kick once and |
| 287 | // wait only when the kick command succeeds. |
| 288 | if (status.registered) { |
| 289 | if (status.running) { |
| 290 | const attachedAfterStatus = await attachToSupervisedDaemon(); |
| 291 | if (attachedAfterStatus) return acceptSupervisedConnection(attachedAfterStatus); |
| 292 | // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: a restart failure just falls through to managed-spawn |
| 293 | try { |
| 294 | await restartSupervisedService(); |
| 295 | } catch (error) { |
| 296 | log.warn( |
| 297 | "Failed to restart running supervised service after attach failed; using managed sidecar", |
| 298 | error, |
| 299 | ); |
| 300 | return null; |
| 301 | } |
| 302 | return waitForSupervisedAttach(15_000); |
| 303 | } |
| 304 | |
| 305 | // 3. Registered but not running usually means a stale plist/unit/task. Try |
| 306 | // restart first, then reinstall the service if the OS manager cannot start it. |
| 307 | // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: restart failure triggers supervised service self-heal |
| 308 | try { |
| 309 | await restartSupervisedService(); |
| 310 | } catch (error) { |
| 311 | log.warn("Failed to restart registered supervised service; reinstalling", error); |
| 312 | return installSupervisedAndWait("registered service restart failure"); |
| 313 | } |
| 314 | return waitForSupervisedAttach(15_000); |
| 315 | } |
| 316 | |
| 317 | // 4. First run → ask, then install + start. The unit carries no secret; the |
| 318 | // supervised daemon mints/loads its bearer from auth.json under DESKTOP_DATA_DIR. |
| 319 | if (!(await confirmEnableBackgroundService())) return null; |
| 320 | return installSupervisedAndWait("first-run prompt"); |
| 321 | }; |
| 322 | |
| 323 | // Crash monitor for the supervised daemon: the OS service manager restarts it |
| 324 | // on crash, but during that window the window's requests fail. Poll, show a |
no test coverage detected