( target: ServerTarget, )
| 812 | }); |
| 813 | |
| 814 | const resolveExecutorServerConnection = ( |
| 815 | target: ServerTarget, |
| 816 | ): Effect.Effect<ExecutorServerConnection, Error, FileSystem.FileSystem | PlatformPath.Path> => |
| 817 | Effect.gen(function* () { |
| 818 | const requestedResult = yield* resolveRequestedExecutorServerConnection(target); |
| 819 | const active = yield* readActiveLocalServerManifest(); |
| 820 | const decision = chooseCliServerConnectionWithActiveLocal({ |
| 821 | requested: requestedResult.connection, |
| 822 | source: requestedResult.source, |
| 823 | active, |
| 824 | }); |
| 825 | |
| 826 | if (decision.kind === "conflict") { |
| 827 | return yield* Effect.fail( |
| 828 | new Error( |
| 829 | [ |
| 830 | `A local Executor ${decision.active.kind} is already running at ${decision.active.connection.origin} (pid ${decision.active.pid}).`, |
| 831 | `It owns the current data directory: ${decision.active.dataDir}`, |
| 832 | "Refusing to auto-start another local server against the same database.", |
| 833 | `Use the active server, or stop it before starting ${cliPrefix} daemon run.`, |
| 834 | ].join("\n"), |
| 835 | ), |
| 836 | ); |
| 837 | } |
| 838 | |
| 839 | const requested = yield* refreshOAuthConnection(decision.connection); |
| 840 | if (decision.kind === "use-active") return requested; |
| 841 | |
| 842 | if (!canAutoStartCliServerConnection(requested)) { |
| 843 | // An authenticated remote connection (oauth device-login, bearer key, or |
| 844 | // basic password): use it directly. The /api/health liveness probe is only |
| 845 | // a gate for the local auto-start decision and isn't necessarily exposed |
| 846 | // by hosted servers, the real API call surfaces any connectivity/auth |
| 847 | // error with proper context. |
| 848 | if (requested.auth) return requested; |
| 849 | if (yield* isServerReachable(requested.origin)) { |
| 850 | return requested; |
| 851 | } |
| 852 | return yield* Effect.fail( |
| 853 | new Error( |
| 854 | [ |
| 855 | `Executor server is not reachable at ${requested.origin}.`, |
| 856 | "For hosted Executor, set EXECUTOR_API_KEY to a bearer API key.", |
| 857 | "For local or desktop servers, set EXECUTOR_AUTH_TOKEN to the server's bearer token.", |
| 858 | ].join("\n"), |
| 859 | ), |
| 860 | ); |
| 861 | } |
| 862 | |
| 863 | const daemonUrl = yield* ensureDaemon(requested.origin); |
| 864 | // The daemon we just ensured published a manifest carrying its bearer token |
| 865 | // (minted into auth.json). Prefer that authed connection — otherwise the |
| 866 | // next API call hits the now-gated server with no credential and 401s. |
| 867 | const started = yield* readActiveLocalServerManifest().pipe(Effect.orElseSucceed(() => null)); |
| 868 | const daemonOrigin = normalizeExecutorServerConnection({ origin: daemonUrl }).origin; |
| 869 | const startedOrigin = started |
| 870 | ? normalizeExecutorServerConnection({ origin: started.connection.origin }).origin |
| 871 | : null; |
no test coverage detected