(
self,
sandbox_id: str,
thread_id: str,
uid: str,
env: dict[str, str] | None = None,
*,
file_thread_id: str | None = None,
skills_thread_id: str | None = None,
)
| 347 | return None |
| 348 | |
| 349 | def create( |
| 350 | self, |
| 351 | sandbox_id: str, |
| 352 | thread_id: str, |
| 353 | uid: str, |
| 354 | env: dict[str, str] | None = None, |
| 355 | *, |
| 356 | file_thread_id: str | None = None, |
| 357 | skills_thread_id: str | None = None, |
| 358 | ) -> SandboxRecord: |
| 359 | with self._lock: |
| 360 | safe_thread_id = self._validate_thread_id(thread_id) |
| 361 | safe_file_thread_id = self._validate_thread_id(file_thread_id or safe_thread_id) |
| 362 | safe_skills_thread_id = self._validate_thread_id(skills_thread_id or safe_thread_id) |
| 363 | safe_uid = self._validate_uid(uid) |
| 364 | existing = self._get_container(sandbox_id) |
| 365 | if existing is not None: |
| 366 | existing.reload() |
| 367 | if not self._is_expected_skills_mount(existing, safe_skills_thread_id): |
| 368 | logger.info("Recreating sandbox %s because skills mount is stale", sandbox_id) |
| 369 | self.delete(sandbox_id) |
| 370 | existing = None |
| 371 | elif not self._has_expected_user_data_mounts(existing, safe_file_thread_id, safe_uid): |
| 372 | logger.info("Recreating sandbox %s because user-data mounts are stale", sandbox_id) |
| 373 | self.delete(sandbox_id) |
| 374 | existing = None |
| 375 | if existing is not None: |
| 376 | if existing.status == "running": |
| 377 | try: |
| 378 | self._ensure_user_data_writable(existing) |
| 379 | record = self._to_record(existing, sandbox_id) |
| 380 | if not record.sandbox_url: |
| 381 | raise RuntimeError(f"sandbox {sandbox_id} has no mapped host port") |
| 382 | if not wait_for_sandbox_ready(record.sandbox_url, timeout_seconds=self._health_timeout_seconds): |
| 383 | raise RuntimeError(f"sandbox {sandbox_id} is not ready at {record.sandbox_url}") |
| 384 | return record |
| 385 | except Exception as exc: |
| 386 | logger.warning("Recreating unhealthy sandbox %s: %s", sandbox_id, exc) |
| 387 | |
| 388 | try: |
| 389 | self.delete(sandbox_id) |
| 390 | except Exception as exc: |
| 391 | logger.warning("Failed to delete stale sandbox %s before recreate: %s", sandbox_id, exc) |
| 392 | |
| 393 | shared_workspace = self._shared_workspace_host_path(safe_uid) |
| 394 | shared_workspace.mkdir(parents=True, exist_ok=True) |
| 395 | thread_uploads = self._thread_uploads_host_path(safe_file_thread_id) |
| 396 | thread_outputs = self._thread_outputs_host_path(safe_file_thread_id) |
| 397 | thread_uploads.mkdir(parents=True, exist_ok=True) |
| 398 | thread_outputs.mkdir(parents=True, exist_ok=True) |
| 399 | thread_skills = self._thread_skills_host_path(safe_skills_thread_id) |
| 400 | thread_skills.mkdir(parents=True, exist_ok=True) |
| 401 | |
| 402 | container_name = self._container_name(sandbox_id) |
| 403 | run_kwargs = { |
| 404 | "name": container_name, |
| 405 | "detach": True, |
| 406 | "labels": { |
nothing calls this directly
no test coverage detected