()
| 306 | * declined — the caller then falls back to managed-spawn. |
| 307 | */ |
| 308 | const ensureSupervisedConnection = async (): Promise<SidecarConnection | null> => { |
| 309 | // 1. Already running → attach. |
| 310 | const attached = await attachToSupervisedDaemon(); |
| 311 | if (attached) { |
| 312 | return acceptSupervisedConnection(attached); |
| 313 | } |
| 314 | |
| 315 | // Headless/e2e: the first-run background-service prompt and the systemd/ |
| 316 | // launchd install both need a user (and a real session bus) present. With |
| 317 | // this set, fall straight through to managed-spawn after the attach attempt |
| 318 | // so the packaged app can be driven without a window server. Mirrors |
| 319 | // EXECUTOR_TEST_AUTO_CONFIRM_RESET in reset-state.ts. |
| 320 | if (process.env.EXECUTOR_TEST_SKIP_BACKGROUND_SERVICE === "1") return null; |
| 321 | |
| 322 | const status = await supervisedServiceStatus(); |
| 323 | if (!status.supported) return null; |
| 324 | |
| 325 | // 2. Registered and running, but the first attach missed it. Kick once and |
| 326 | // wait only when the kick command succeeds. |
| 327 | if (status.registered) { |
| 328 | if (status.running) { |
| 329 | const attachedAfterStatus = await attachToSupervisedDaemon(); |
| 330 | if (attachedAfterStatus) return acceptSupervisedConnection(attachedAfterStatus); |
| 331 | // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: a restart failure just falls through to managed-spawn |
| 332 | try { |
| 333 | await restartSupervisedService(); |
| 334 | } catch (error) { |
| 335 | log.warn( |
| 336 | "Failed to restart running supervised service after attach failed; using managed sidecar", |
| 337 | error, |
| 338 | ); |
| 339 | return null; |
| 340 | } |
| 341 | return waitForSupervisedAttach(15_000); |
| 342 | } |
| 343 | |
| 344 | // 3. Registered but not running usually means a stale plist/unit/task. Try |
| 345 | // restart first, then reinstall the service if the OS manager cannot start it. |
| 346 | // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: restart failure triggers supervised service self-heal |
| 347 | try { |
| 348 | await restartSupervisedService(); |
| 349 | } catch (error) { |
| 350 | log.warn("Failed to restart registered supervised service; reinstalling", error); |
| 351 | return installSupervisedAndWait("registered service restart failure"); |
| 352 | } |
| 353 | return waitForSupervisedAttach(15_000); |
| 354 | } |
| 355 | |
| 356 | // 4. First run → ask, then install + start. The unit carries no secret; the |
| 357 | // supervised daemon mints/loads its bearer from auth.json under DESKTOP_DATA_DIR. |
| 358 | if (!(await confirmEnableBackgroundService())) return null; |
| 359 | return installSupervisedAndWait("first-run prompt"); |
| 360 | }; |
| 361 | |
| 362 | // Crash monitor for the supervised daemon: the OS service manager restarts it |
| 363 | // on crash, but during that window the window's requests fail. Poll, show a |
no test coverage detected