MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / ensureSupervisedConnection

Function ensureSupervisedConnection

apps/desktop/src/main/index.ts:309–361  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

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

Callers 1

bootFunction · 0.85

Calls 8

attachToSupervisedDaemonFunction · 0.90
supervisedServiceStatusFunction · 0.90
restartSupervisedServiceFunction · 0.90
waitForSupervisedAttachFunction · 0.85
installSupervisedAndWaitFunction · 0.85
warnMethod · 0.80

Tested by

no test coverage detected