()
| 441 | * is handled by the existing single-instance / ownership logic. |
| 442 | */ |
| 443 | export async function attachToSupervisedDaemon(): Promise<SidecarConnection | null> { |
| 444 | const dataDir = executorScopeDir(); |
| 445 | const manifest = readManifest(dataDir); |
| 446 | const decision = await resolveSupervisedDaemonAttach(manifest, { |
| 447 | isReachable: isDaemonReachable, |
| 448 | isPidAlive, |
| 449 | }); |
| 450 | if (decision.kind === "attach") { |
| 451 | const { manifest, authToken } = decision; |
| 452 | const origin = manifest.connection.origin; |
| 453 | const url = new URL(origin); |
| 454 | sidecarLog.info(`attaching to supervised daemon at ${origin} (pid ${manifest.pid})`); |
| 455 | return { |
| 456 | baseUrl: origin, |
| 457 | hostname: url.hostname, |
| 458 | port: Number.parseInt(url.port, 10) || (url.protocol === "https:" ? 443 : 80), |
| 459 | username: SERVER_SETTINGS_USERNAME, |
| 460 | authToken, |
| 461 | child: null, |
| 462 | supervisedDaemon: true, |
| 463 | ownerVersion: manifest.owner.version, |
| 464 | ownerClient: manifest.owner.client, |
| 465 | ownerExecutablePath: manifest.owner.executablePath, |
| 466 | }; |
| 467 | } |
| 468 | |
| 469 | if (decision.kind === "remove-stale-manifest") { |
| 470 | removeManifestIfOwnedBy(dataDir, decision.pid); |
| 471 | return null; |
| 472 | } |
| 473 | |
| 474 | if (!manifest || manifest.kind !== "cli-daemon") return null; |
| 475 | |
| 476 | sidecarLog.warn( |
| 477 | `supervised daemon at ${manifest.connection.origin} (pid ${manifest.pid}) did not answer the health probe; keeping its manifest because the process is still alive`, |
| 478 | ); |
| 479 | return null; |
| 480 | } |
| 481 | |
| 482 | export async function stopSidecar(child: ChildProcess): Promise<void> { |
| 483 | expectedExits.add(child); |
no test coverage detected