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

Function resolveExecutorServerConnection

apps/cli/src/main.ts:787–852  ·  view source on GitHub ↗
(
  target: ServerTarget,
)

Source from the content-addressed store, hash-verified

785 });
786
787const resolveExecutorServerConnection = (
788 target: ServerTarget,
789): Effect.Effect<ExecutorServerConnection, Error, FileSystem.FileSystem | PlatformPath.Path> =>
790 Effect.gen(function* () {
791 const requestedResult = yield* resolveRequestedExecutorServerConnection(target);
792 const active = yield* readActiveLocalServerManifest();
793 const decision = chooseCliServerConnectionWithActiveLocal({
794 requested: requestedResult.connection,
795 source: requestedResult.source,
796 active,
797 });
798
799 if (decision.kind === "conflict") {
800 return yield* Effect.fail(
801 new Error(
802 [
803 `A local Executor ${decision.active.kind} is already running at ${decision.active.connection.origin} (pid ${decision.active.pid}).`,
804 `It owns the current data directory: ${decision.active.dataDir}`,
805 "Refusing to auto-start another local server against the same database.",
806 `Use the active server, or stop it before starting ${cliPrefix} daemon run.`,
807 ].join("\n"),
808 ),
809 );
810 }
811
812 const requested = yield* refreshOAuthConnection(decision.connection);
813 if (decision.kind === "use-active") return requested;
814
815 if (!canAutoStartCliServerConnection(requested)) {
816 // An authenticated remote connection (oauth device-login, bearer key, or
817 // basic password): use it directly. The /api/health liveness probe is only
818 // a gate for the local auto-start decision and isn't necessarily exposed
819 // by hosted servers, the real API call surfaces any connectivity/auth
820 // error with proper context.
821 if (requested.auth) return requested;
822 if (yield* isServerReachable(requested.origin)) {
823 return requested;
824 }
825 return yield* Effect.fail(
826 new Error(
827 [
828 `Executor server is not reachable at ${requested.origin}.`,
829 "For hosted Executor, set EXECUTOR_API_KEY to a bearer API key.",
830 "For local or desktop servers, set EXECUTOR_AUTH_TOKEN to the server's bearer token.",
831 ].join("\n"),
832 ),
833 );
834 }
835
836 const daemonUrl = yield* ensureDaemon(requested.origin);
837 // The daemon we just ensured published a manifest carrying its bearer token
838 // (minted into auth.json). Prefer that authed connection — otherwise the
839 // next API call hits the now-gated server with no credential and 401s.
840 const started = yield* readActiveLocalServerManifest().pipe(Effect.orElseSucceed(() => null));
841 const daemonOrigin = normalizeExecutorServerConnection({ origin: daemonUrl }).origin;
842 const startedOrigin = started
843 ? normalizeExecutorServerConnection({ origin: started.connection.origin }).origin
844 : null;

Callers 3

executeCodeFunction · 0.85
runCallHelpFunction · 0.85
main.tsFile · 0.85

Tested by

no test coverage detected