modal_runtime.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. import os
  2. import tempfile
  3. import threading
  4. from pathlib import Path
  5. from typing import Callable, Generator
  6. import modal
  7. import requests
  8. import tenacity
  9. from openhands.core.config import AppConfig
  10. from openhands.events import EventStream
  11. from openhands.runtime.impl.eventstream.eventstream_runtime import (
  12. EventStreamRuntime,
  13. LogBuffer,
  14. )
  15. from openhands.runtime.plugins import PluginRequirement
  16. from openhands.runtime.utils.command import get_remote_startup_command
  17. from openhands.runtime.utils.runtime_build import (
  18. BuildFromImageType,
  19. prep_build_folder,
  20. )
  21. from openhands.utils.async_utils import call_sync_from_async
  22. # FIXME: this will not work in HA mode. We need a better way to track IDs
  23. MODAL_RUNTIME_IDS: dict[str, str] = {}
  24. # Modal's log generator returns strings, but the upstream LogBuffer expects bytes.
  25. def bytes_shim(string_generator) -> Generator[bytes, None, None]:
  26. for line in string_generator:
  27. yield line.encode('utf-8')
  28. class ModalLogBuffer(LogBuffer):
  29. """Synchronous buffer for Modal sandbox logs.
  30. This class provides a thread-safe way to collect, store, and retrieve logs
  31. from a Modal sandbox. It uses a list to store log lines and provides methods
  32. for appending, retrieving, and clearing logs.
  33. """
  34. def __init__(self, sandbox: modal.Sandbox):
  35. self.init_msg = 'Runtime client initialized.'
  36. self.buffer: list[str] = []
  37. self.lock = threading.Lock()
  38. self._stop_event = threading.Event()
  39. self.log_generator = bytes_shim(sandbox.stderr)
  40. self.log_stream_thread = threading.Thread(target=self.stream_logs)
  41. self.log_stream_thread.daemon = True
  42. self.log_stream_thread.start()
  43. class ModalRuntime(EventStreamRuntime):
  44. """This runtime will subscribe the event stream.
  45. When receive an event, it will send the event to runtime-client which run inside the Modal sandbox environment.
  46. Args:
  47. config (AppConfig): The application configuration.
  48. event_stream (EventStream): The event stream to subscribe to.
  49. sid (str, optional): The session ID. Defaults to 'default'.
  50. plugins (list[PluginRequirement] | None, optional): List of plugin requirements. Defaults to None.
  51. env_vars (dict[str, str] | None, optional): Environment variables to set. Defaults to None.
  52. """
  53. container_name_prefix = 'openhands-sandbox-'
  54. sandbox: modal.Sandbox | None
  55. def __init__(
  56. self,
  57. config: AppConfig,
  58. event_stream: EventStream,
  59. sid: str = 'default',
  60. plugins: list[PluginRequirement] | None = None,
  61. env_vars: dict[str, str] | None = None,
  62. status_callback: Callable | None = None,
  63. attach_to_existing: bool = False,
  64. headless_mode: bool = True,
  65. ):
  66. assert config.modal_api_token_id, 'Modal API token id is required'
  67. assert config.modal_api_token_secret, 'Modal API token secret is required'
  68. self.config = config
  69. self.sandbox = None
  70. self.modal_client = modal.Client.from_credentials(
  71. config.modal_api_token_id, config.modal_api_token_secret
  72. )
  73. self.app = modal.App.lookup(
  74. 'openhands', create_if_missing=True, client=self.modal_client
  75. )
  76. # workspace_base cannot be used because we can't bind mount into a sandbox.
  77. if self.config.workspace_base is not None:
  78. self.log(
  79. 'warning',
  80. 'Setting workspace_base is not supported in the modal runtime.',
  81. )
  82. # This value is arbitrary as it's private to the container
  83. self.container_port = 3000
  84. self.session = requests.Session()
  85. self.status_callback = status_callback
  86. self.base_container_image_id = self.config.sandbox.base_container_image
  87. self.runtime_container_image_id = self.config.sandbox.runtime_container_image
  88. self.action_semaphore = threading.Semaphore(1) # Ensure one action at a time
  89. # Buffer for container logs
  90. self.log_buffer: LogBuffer | None = None
  91. if self.config.sandbox.runtime_extra_deps:
  92. self.log(
  93. 'debug',
  94. f'Installing extra user-provided dependencies in the runtime image: {self.config.sandbox.runtime_extra_deps}',
  95. )
  96. self.init_base_runtime(
  97. config,
  98. event_stream,
  99. sid,
  100. plugins,
  101. env_vars,
  102. status_callback,
  103. attach_to_existing,
  104. headless_mode,
  105. )
  106. async def connect(self):
  107. self.send_status_message('STATUS$STARTING_RUNTIME')
  108. self.log('debug', f'ModalRuntime `{self.sid}`')
  109. self.image = self._get_image_definition(
  110. self.base_container_image_id,
  111. self.runtime_container_image_id,
  112. self.config.sandbox.runtime_extra_deps,
  113. )
  114. if self.attach_to_existing:
  115. if self.sid in MODAL_RUNTIME_IDS:
  116. sandbox_id = MODAL_RUNTIME_IDS[self.sid]
  117. self.log('debug', f'Attaching to existing Modal sandbox: {sandbox_id}')
  118. self.sandbox = modal.Sandbox.from_id(
  119. sandbox_id, client=self.modal_client
  120. )
  121. else:
  122. self.send_status_message('STATUS$PREPARING_CONTAINER')
  123. await call_sync_from_async(
  124. self._init_sandbox,
  125. sandbox_workspace_dir=self.config.workspace_mount_path_in_sandbox,
  126. plugins=self.plugins,
  127. )
  128. self.send_status_message('STATUS$CONTAINER_STARTED')
  129. self.log_buffer = ModalLogBuffer(self.sandbox)
  130. if self.sandbox is None:
  131. raise Exception('Sandbox not initialized')
  132. tunnel = self.sandbox.tunnels()[self.container_port]
  133. self.api_url = tunnel.url
  134. self.log('debug', f'Container started. Server url: {self.api_url}')
  135. if not self.attach_to_existing:
  136. self.log('debug', 'Waiting for client to become ready...')
  137. self.send_status_message('STATUS$WAITING_FOR_CLIENT')
  138. self._wait_until_alive()
  139. self.setup_initial_env()
  140. if not self.attach_to_existing:
  141. self.send_status_message(' ')
  142. def _get_image_definition(
  143. self,
  144. base_container_image_id: str | None,
  145. runtime_container_image_id: str | None,
  146. runtime_extra_deps: str | None,
  147. ) -> modal.Image:
  148. if runtime_container_image_id:
  149. base_runtime_image = modal.Image.from_registry(runtime_container_image_id)
  150. elif base_container_image_id:
  151. build_folder = tempfile.mkdtemp()
  152. prep_build_folder(
  153. build_folder=Path(build_folder),
  154. base_image=base_container_image_id,
  155. build_from=BuildFromImageType.SCRATCH,
  156. extra_deps=runtime_extra_deps,
  157. )
  158. base_runtime_image = modal.Image.from_dockerfile(
  159. path=os.path.join(build_folder, 'Dockerfile'),
  160. context_mount=modal.Mount.from_local_dir(
  161. local_path=build_folder,
  162. remote_path='.', # to current WORKDIR
  163. ),
  164. )
  165. else:
  166. raise ValueError(
  167. 'Neither runtime container image nor base container image is set'
  168. )
  169. return base_runtime_image.run_commands(
  170. """
  171. # Disable bracketed paste
  172. # https://github.com/pexpect/pexpect/issues/669
  173. echo "set enable-bracketed-paste off" >> /etc/inputrc && \\
  174. echo 'export INPUTRC=/etc/inputrc' >> /etc/bash.bashrc
  175. """.strip()
  176. )
  177. @tenacity.retry(
  178. stop=tenacity.stop_after_attempt(5),
  179. wait=tenacity.wait_exponential(multiplier=1, min=4, max=60),
  180. )
  181. def _init_sandbox(
  182. self,
  183. sandbox_workspace_dir: str,
  184. plugins: list[PluginRequirement] | None = None,
  185. ):
  186. try:
  187. self.log('debug', 'Preparing to start container...')
  188. plugin_args = []
  189. if plugins is not None and len(plugins) > 0:
  190. plugin_args.append('--plugins')
  191. plugin_args.extend([plugin.name for plugin in plugins])
  192. # Combine environment variables
  193. environment: dict[str, str | None] = {
  194. 'port': str(self.container_port),
  195. 'PYTHONUNBUFFERED': '1',
  196. }
  197. if self.config.debug:
  198. environment['DEBUG'] = 'true'
  199. browsergym_args = []
  200. if self.config.sandbox.browsergym_eval_env is not None:
  201. browsergym_args = [
  202. '-browsergym-eval-env',
  203. self.config.sandbox.browsergym_eval_env,
  204. ]
  205. env_secret = modal.Secret.from_dict(environment)
  206. self.log('debug', f'Sandbox workspace: {sandbox_workspace_dir}')
  207. sandbox_start_cmd = get_remote_startup_command(
  208. self.container_port,
  209. sandbox_workspace_dir,
  210. 'openhands' if self.config.run_as_openhands else 'root',
  211. self.config.sandbox.user_id,
  212. plugin_args,
  213. browsergym_args,
  214. is_root=not self.config.run_as_openhands, # is_root=True when running as root
  215. )
  216. self.log('debug', f'Starting container with command: {sandbox_start_cmd}')
  217. self.sandbox = modal.Sandbox.create(
  218. *sandbox_start_cmd,
  219. secrets=[env_secret],
  220. workdir='/openhands/code',
  221. encrypted_ports=[self.container_port],
  222. image=self.image,
  223. app=self.app,
  224. client=self.modal_client,
  225. timeout=60 * 60,
  226. )
  227. MODAL_RUNTIME_IDS[self.sid] = self.sandbox.object_id
  228. self.log('debug', 'Container started')
  229. except Exception as e:
  230. self.log(
  231. 'error', f'Error: Instance {self.sid} FAILED to start container!\n'
  232. )
  233. self.log('error', str(e))
  234. self.close()
  235. raise e
  236. def close(self):
  237. """Closes the ModalRuntime and associated objects."""
  238. # if self.temp_dir_handler:
  239. # self.temp_dir_handler.__exit__(None, None, None)
  240. if self.log_buffer:
  241. self.log_buffer.close()
  242. if self.session:
  243. self.session.close()
  244. if not self.attach_to_existing and self.sandbox:
  245. self.sandbox.terminate()