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

Function ensureSupervisedConnection

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

Source from the content-addressed store, hash-verified

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