sync update from LAN repo (source dhk-1 @ 3e87be55)
This commit is contained in:
1 parent
ab6068a7c1
commit
42f2a26fa8
1482 files changed
+227759
-9584
No files matched your search
+94
-7
@@ -1,8 +1,10 @@
|
||||
import base64
|
||||
import base64
|
||||
import json
|
||||
import logging
|
||||
import shlex
|
||||
import threading
|
||||
import uuid
|
||||
from contextlib import contextmanager
|
||||
|
||||
from agent_sandbox import Sandbox as AioSandboxClient
|
||||
|
||||
@@ -53,6 +55,20 @@ class AioSandbox(Sandbox):
|
||||
# output are not prematurely terminated by the sandbox's built-in 120 s
|
||||
# default.
|
||||
_DEFAULT_NO_CHANGE_TIMEOUT = 600
|
||||
_LOCK_WAIT_TIMEOUT = 30
|
||||
|
||||
@contextmanager
|
||||
def _operation_lock(self):
|
||||
# An abandoned/stalled request must not strand every later read and
|
||||
# command behind an unbounded acquire. The old request keeps ownership
|
||||
# until it actually finishes; callers receive a failure, never a retry
|
||||
# that could execute a state-changing shell command twice.
|
||||
if not self._lock.acquire(timeout=self._LOCK_WAIT_TIMEOUT):
|
||||
raise TimeoutError(f"Sandbox is busy: timed out after {self._LOCK_WAIT_TIMEOUT:g} seconds waiting for the previous operation")
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
self._lock.release()
|
||||
|
||||
def execute_command(self, command: str) -> str:
|
||||
"""Execute a shell command in the sandbox.
|
||||
@@ -70,7 +86,7 @@ class AioSandbox(Sandbox):
|
||||
Returns:
|
||||
The output of the command.
|
||||
"""
|
||||
with self._lock:
|
||||
with self._operation_lock():
|
||||
try:
|
||||
result = self._client.shell.exec_command(command=command, no_change_timeout=self._DEFAULT_NO_CHANGE_TIMEOUT)
|
||||
output = result.data.output if result.data else ""
|
||||
@@ -86,7 +102,7 @@ class AioSandbox(Sandbox):
|
||||
logger.error(f"Failed to execute command in sandbox: {e}")
|
||||
return f"Error: {e}"
|
||||
|
||||
def read_file(self, path: str) -> str:
|
||||
def read_file(self, path: str) -> str:
|
||||
"""Read the content of a file in the sandbox.
|
||||
|
||||
Args:
|
||||
@@ -100,7 +116,78 @@ class AioSandbox(Sandbox):
|
||||
return result.data.content if result.data else ""
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to read file in sandbox: {e}")
|
||||
return f"Error: {e}"
|
||||
return f"Error: {e}"
|
||||
|
||||
def read_file_range(
|
||||
self,
|
||||
path: str,
|
||||
*,
|
||||
start_line: int = 1,
|
||||
end_line: int | None = None,
|
||||
max_chars: int = 32_000,
|
||||
) -> tuple[str, int | None, bool]:
|
||||
"""Stream a bounded text range inside the container.
|
||||
|
||||
The file body is base64 encoded only after the in-container reader has
|
||||
applied the character cap, so neither the sandbox API nor Gateway ever
|
||||
receives the complete large file.
|
||||
"""
|
||||
program = r"""import base64,json,os,stat,sys
|
||||
p,start,end,limit=sys.argv[1],int(sys.argv[2]),int(sys.argv[3]),int(sys.argv[4])
|
||||
rows=[]; chars=0; physical=0; next_line=None
|
||||
fd=os.open(p,os.O_RDONLY|getattr(os,"O_NONBLOCK",0)|getattr(os,"O_BINARY",0))
|
||||
if not stat.S_ISREG(os.fstat(fd).st_mode):
|
||||
os.close(fd)
|
||||
raise OSError("Only regular files can be read")
|
||||
with os.fdopen(fd,"r",encoding="utf-8",errors="replace",newline="") as f:
|
||||
while True:
|
||||
part=f.readline(4097)
|
||||
if not part: break
|
||||
physical+=1
|
||||
if end and physical > end: break
|
||||
remain=limit-chars-(1 if rows else 0)
|
||||
capture=physical>=start
|
||||
if capture and remain<=0:
|
||||
next_line=physical; break
|
||||
pieces=[]; captured=0; over=False
|
||||
while True:
|
||||
ended=part.endswith("\n") or part.endswith("\r")
|
||||
body=part.rstrip("\r\n") if ended else part
|
||||
if capture and remain>captured:
|
||||
piece=body[:remain-captured]; pieces.append(piece); captured+=len(piece)
|
||||
if len(piece)<len(body): over=True
|
||||
elif capture and body: over=True
|
||||
if ended or (capture and captured>=remain): break
|
||||
part=f.readline(4097)
|
||||
if not part: break
|
||||
if not capture: continue
|
||||
line="".join(pieces)
|
||||
rows.append(line); chars+=len(line)+(1 if len(rows)>1 else 0)
|
||||
if over or chars>=limit:
|
||||
next_line=physical+1; break
|
||||
content="\n".join(rows).encode("utf-8")
|
||||
print(json.dumps({"content":base64.b64encode(content).decode("ascii"),"next":next_line,"truncated":next_line is not None},separators=(",",":")))"""
|
||||
command = " ".join(
|
||||
[
|
||||
"python3",
|
||||
"-c",
|
||||
shlex.quote(program),
|
||||
shlex.quote(path),
|
||||
str(max(1, start_line)),
|
||||
str(end_line or 0),
|
||||
str(max(1, max_chars)),
|
||||
]
|
||||
)
|
||||
with self._operation_lock():
|
||||
result = self._client.shell.exec_command(command=command, no_change_timeout=self._DEFAULT_NO_CHANGE_TIMEOUT)
|
||||
output = result.data.output.strip() if result.data and result.data.output else ""
|
||||
try:
|
||||
payload = json.loads(output.splitlines()[-1])
|
||||
content = base64.b64decode(payload["content"]).decode("utf-8", errors="replace")
|
||||
next_line = payload.get("next")
|
||||
return content, int(next_line) if next_line is not None else None, bool(payload.get("truncated"))
|
||||
except Exception as exc:
|
||||
raise OSError(f"bounded sandbox read failed: {output[-500:]}") from exc
|
||||
|
||||
def list_dir(self, path: str, max_depth: int = 2) -> list[str]:
|
||||
"""List the contents of a directory in the sandbox.
|
||||
@@ -112,7 +199,7 @@ class AioSandbox(Sandbox):
|
||||
Returns:
|
||||
The contents of the directory.
|
||||
"""
|
||||
with self._lock:
|
||||
with self._operation_lock():
|
||||
try:
|
||||
result = self._client.shell.exec_command(command=f"find {shlex.quote(path)} -maxdepth {max_depth} -type f -o -type d 2>/dev/null | head -500", no_change_timeout=self._DEFAULT_NO_CHANGE_TIMEOUT)
|
||||
output = result.data.output if result.data else ""
|
||||
@@ -131,7 +218,7 @@ class AioSandbox(Sandbox):
|
||||
content: The text content to write to the file.
|
||||
append: Whether to append the content to the file.
|
||||
"""
|
||||
with self._lock:
|
||||
with self._operation_lock():
|
||||
try:
|
||||
if append:
|
||||
existing = self.read_file(path)
|
||||
@@ -229,7 +316,7 @@ class AioSandbox(Sandbox):
|
||||
path: The absolute path of the file to update.
|
||||
content: The binary content to write to the file.
|
||||
"""
|
||||
with self._lock:
|
||||
with self._operation_lock():
|
||||
try:
|
||||
base64_content = base64.b64encode(content).decode("utf-8")
|
||||
self._client.file.write_file(file=path, content=base64_content, encoding="base64")
|
||||
|
||||
+193
-98
@@ -18,6 +18,7 @@ import signal
|
||||
import threading
|
||||
import time
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
try:
|
||||
import fcntl
|
||||
@@ -25,11 +26,16 @@ except ImportError: # pragma: no cover - Windows fallback
|
||||
fcntl = None # type: ignore[assignment]
|
||||
import msvcrt
|
||||
|
||||
from deerflow.config import get_app_config
|
||||
from deerflow.config.paths import VIRTUAL_PATH_PREFIX, get_paths
|
||||
from deerflow.config import get_app_config
|
||||
from deerflow.config.paths import VIRTUAL_PATH_PREFIX, get_paths, join_host_path
|
||||
from deerflow.runtime.user_context import get_effective_user_id
|
||||
from deerflow.sandbox.sandbox import Sandbox
|
||||
from deerflow.sandbox.sandbox_provider import SandboxProvider
|
||||
from deerflow.sandbox.sandbox import Sandbox
|
||||
from deerflow.sandbox.sandbox_provider import SandboxProvider
|
||||
from deerflow.sandbox.skill_view import (
|
||||
SKILL_VIEW_DIR_NAME,
|
||||
SKILL_VIEW_MARKER,
|
||||
refresh_skill_sandbox_view,
|
||||
)
|
||||
|
||||
from .aio_sandbox import AioSandbox
|
||||
from .backend import SandboxBackend, wait_for_sandbox_ready
|
||||
@@ -43,27 +49,27 @@ logger = logging.getLogger(__name__)
|
||||
DEFAULT_IMAGE = "enterprise-public-cn-beijing.cr.volces.com/vefaas-public/all-in-one-sandbox:latest"
|
||||
DEFAULT_PORT = 8080
|
||||
DEFAULT_CONTAINER_PREFIX = "deer-flow-sandbox"
|
||||
DEFAULT_IDLE_TIMEOUT = 600 # 10 minutes in seconds
|
||||
DEFAULT_REPLICAS = 3 # Maximum concurrent sandbox containers
|
||||
IDLE_CHECK_INTERVAL = 60 # Check every 60 seconds
|
||||
PACKAGE_MANAGER_ENV_KEYS = (
|
||||
"PIP_INDEX_URL",
|
||||
"PIP_EXTRA_INDEX_URL",
|
||||
"PIP_TRUSTED_HOST",
|
||||
"PIP_CERT",
|
||||
"PIP_CLIENT_CERT",
|
||||
"UV_INDEX_URL",
|
||||
"UV_EXTRA_INDEX_URL",
|
||||
"UV_DEFAULT_INDEX",
|
||||
"UV_INDEX",
|
||||
"UV_TRUSTED_HOST",
|
||||
"HTTP_PROXY",
|
||||
"HTTPS_PROXY",
|
||||
"NO_PROXY",
|
||||
"http_proxy",
|
||||
"https_proxy",
|
||||
"no_proxy",
|
||||
)
|
||||
DEFAULT_IDLE_TIMEOUT = 600 # 10 minutes in seconds
|
||||
DEFAULT_REPLICAS = 3 # Maximum concurrent sandbox containers
|
||||
IDLE_CHECK_INTERVAL = 60 # Check every 60 seconds
|
||||
PACKAGE_MANAGER_ENV_KEYS = (
|
||||
"PIP_INDEX_URL",
|
||||
"PIP_EXTRA_INDEX_URL",
|
||||
"PIP_TRUSTED_HOST",
|
||||
"PIP_CERT",
|
||||
"PIP_CLIENT_CERT",
|
||||
"UV_INDEX_URL",
|
||||
"UV_EXTRA_INDEX_URL",
|
||||
"UV_DEFAULT_INDEX",
|
||||
"UV_INDEX",
|
||||
"UV_TRUSTED_HOST",
|
||||
"HTTP_PROXY",
|
||||
"HTTPS_PROXY",
|
||||
"NO_PROXY",
|
||||
"http_proxy",
|
||||
"https_proxy",
|
||||
"no_proxy",
|
||||
)
|
||||
|
||||
|
||||
def _lock_file_exclusive(lock_file) -> None:
|
||||
@@ -160,9 +166,9 @@ class AioSandboxProvider(SandboxProvider):
|
||||
Local provider manages container lifecycle directly (start/stop).
|
||||
"""
|
||||
provisioner_url = self._config.get("provisioner_url")
|
||||
if provisioner_url:
|
||||
logger.info(f"Using remote sandbox backend with provisioner at {provisioner_url}")
|
||||
return RemoteSandboxBackend(provisioner_url=provisioner_url, environment=self._config["environment"])
|
||||
if provisioner_url:
|
||||
logger.info(f"Using remote sandbox backend with provisioner at {provisioner_url}")
|
||||
return RemoteSandboxBackend(provisioner_url=provisioner_url, environment=self._config["environment"])
|
||||
|
||||
logger.info("Using local container sandbox backend")
|
||||
return LocalContainerBackend(
|
||||
@@ -190,24 +196,24 @@ class AioSandboxProvider(SandboxProvider):
|
||||
"idle_timeout": idle_timeout if idle_timeout is not None else DEFAULT_IDLE_TIMEOUT,
|
||||
"replicas": replicas if replicas is not None else DEFAULT_REPLICAS,
|
||||
"mounts": sandbox_config.mounts or [],
|
||||
"environment": self._resolve_env_vars(sandbox_config.environment or {}),
|
||||
"environment": self._resolve_env_vars(sandbox_config.environment or {}),
|
||||
# provisioner URL for dynamic pod management (e.g. http://provisioner:8002)
|
||||
"provisioner_url": getattr(sandbox_config, "provisioner_url", None) or "",
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def _resolve_env_vars(env_config: dict[str, str]) -> dict[str, str]:
|
||||
"""Resolve environment variable references (values starting with $)."""
|
||||
resolved = {}
|
||||
for key in PACKAGE_MANAGER_ENV_KEYS:
|
||||
value = os.environ.get(key)
|
||||
if value:
|
||||
resolved[key] = value
|
||||
for key, value in env_config.items():
|
||||
if isinstance(value, str) and value.startswith("$"):
|
||||
env_name = value[1:]
|
||||
resolved[key] = os.environ.get(env_name, "")
|
||||
else:
|
||||
def _resolve_env_vars(env_config: dict[str, str]) -> dict[str, str]:
|
||||
"""Resolve environment variable references (values starting with $)."""
|
||||
resolved = {}
|
||||
for key in PACKAGE_MANAGER_ENV_KEYS:
|
||||
value = os.environ.get(key)
|
||||
if value:
|
||||
resolved[key] = value
|
||||
for key, value in env_config.items():
|
||||
if isinstance(value, str) and value.startswith("$"):
|
||||
env_name = value[1:]
|
||||
resolved[key] = os.environ.get(env_name, "")
|
||||
else:
|
||||
resolved[key] = str(value)
|
||||
return resolved
|
||||
|
||||
@@ -282,8 +288,43 @@ class AioSandboxProvider(SandboxProvider):
|
||||
mounts.append(skills_mount)
|
||||
logger.info(f"Adding skills mount: {skills_mount}")
|
||||
|
||||
mounts.extend(self._get_custom_mounts())
|
||||
|
||||
return mounts
|
||||
|
||||
@staticmethod
|
||||
def _get_custom_mounts() -> list[tuple[str, str, bool]]:
|
||||
"""Collect config.yaml ``sandbox.mounts`` extra mounts (mount-if-exists).
|
||||
|
||||
Mirrors LocalSandboxProvider semantics: a missing host path is skipped
|
||||
with a warning so an incompletely staged deployment (e.g. the web-app
|
||||
template without node_modules) never breaks sandbox creation.
|
||||
"""
|
||||
collected: list[tuple[str, str, bool]] = []
|
||||
try:
|
||||
config = get_app_config()
|
||||
sandbox_config = config.sandbox
|
||||
if not sandbox_config or not sandbox_config.mounts:
|
||||
return collected
|
||||
reserved = {config.skills.container_path, f"{VIRTUAL_PATH_PREFIX}", "/mnt/acp-workspace"}
|
||||
for mount in sandbox_config.mounts:
|
||||
host_path = Path(mount.host_path)
|
||||
container_path = mount.container_path.rstrip("/") or "/"
|
||||
if not host_path.is_absolute() or not container_path.startswith("/"):
|
||||
logger.warning(f"Skipping invalid custom mount: {mount.host_path} -> {mount.container_path}")
|
||||
continue
|
||||
if any(container_path == p or container_path.startswith(p + "/") for p in reserved):
|
||||
logger.warning(f"Skipping custom mount conflicting with reserved prefix: {mount.container_path}")
|
||||
continue
|
||||
if not host_path.exists():
|
||||
logger.warning(f"Custom mount host path missing, skipping: {host_path} -> {container_path}")
|
||||
continue
|
||||
host_mount = os.environ.get("DEER_FLOW_HOST_BASE_DIR") or str(host_path)
|
||||
collected.append((host_mount, container_path, mount.read_only))
|
||||
except Exception as e: # noqa: BLE001 - 配置异常不影响沙箱创建
|
||||
logger.warning(f"Could not setup custom mounts: {e}")
|
||||
return collected
|
||||
|
||||
@staticmethod
|
||||
def _get_thread_mounts(thread_id: str) -> list[tuple[str, str, bool]]:
|
||||
"""Get volume mounts for a thread's data directories.
|
||||
@@ -293,7 +334,7 @@ class AioSandboxProvider(SandboxProvider):
|
||||
mounted Docker socket (DooD), the host Docker daemon can resolve the paths.
|
||||
"""
|
||||
paths = get_paths()
|
||||
user_id = get_effective_user_id()
|
||||
user_id = get_effective_user_id()
|
||||
paths.ensure_thread_dirs(thread_id, user_id=user_id)
|
||||
|
||||
return [
|
||||
@@ -306,24 +347,49 @@ class AioSandboxProvider(SandboxProvider):
|
||||
]
|
||||
|
||||
@staticmethod
|
||||
def _get_skills_mount() -> tuple[str, str, bool] | None:
|
||||
"""Get the skills directory mount configuration.
|
||||
|
||||
Mount source uses DEER_FLOW_HOST_SKILLS_PATH when running inside Docker (DooD)
|
||||
so the host Docker daemon can resolve the path.
|
||||
"""
|
||||
try:
|
||||
config = get_app_config()
|
||||
skills_path = config.skills.get_skills_path()
|
||||
container_path = config.skills.container_path
|
||||
|
||||
if skills_path.exists():
|
||||
# When running inside Docker with DooD, use host-side skills path.
|
||||
host_skills = os.environ.get("DEER_FLOW_HOST_SKILLS_PATH") or str(skills_path)
|
||||
return (host_skills, container_path, True) # Read-only for security
|
||||
except Exception as e:
|
||||
logger.warning(f"Could not setup skills mount: {e}")
|
||||
return None
|
||||
def _get_skills_mount() -> tuple[str, str, bool] | None:
|
||||
"""Get the online-only skills capability mount configuration.
|
||||
|
||||
Mount source uses DEER_FLOW_HOST_BASE_DIR when running inside Docker
|
||||
(DooD) so the host Docker daemon can resolve the writable runtime view.
|
||||
"""
|
||||
try:
|
||||
config = get_app_config()
|
||||
view = refresh_skill_sandbox_view()
|
||||
skills_path = view.root
|
||||
container_path = config.skills.container_path
|
||||
|
||||
if skills_path.exists():
|
||||
# When running inside Docker with DooD, use host-side skills path.
|
||||
host_runtime_root = os.environ.get("DEER_FLOW_HOST_BASE_DIR")
|
||||
host_skills = (
|
||||
join_host_path(host_runtime_root, SKILL_VIEW_DIR_NAME)
|
||||
if host_runtime_root
|
||||
else str(skills_path)
|
||||
)
|
||||
return (host_skills, container_path, True) # Read-only for security
|
||||
except Exception as e:
|
||||
logger.warning(f"Could not setup skills mount: {e}")
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def _assert_online_skill_view(sandbox: AioSandbox) -> None:
|
||||
"""Fail closed unless the sandbox mounted the filtered capability.
|
||||
|
||||
This attestation is important for remote provisioners: an older
|
||||
provisioner may ignore requested extra mounts and expose its legacy
|
||||
full skills tree. The marker exists only at the root of the filtered
|
||||
view, never at the root of the canonical tree.
|
||||
"""
|
||||
config = get_app_config()
|
||||
view = refresh_skill_sandbox_view()
|
||||
marker_path = f"{config.skills.container_path.rstrip('/')}/{SKILL_VIEW_MARKER}"
|
||||
observed = sandbox.read_file(marker_path)
|
||||
if observed != view.marker_content:
|
||||
raise RuntimeError(
|
||||
"Sandbox did not mount the attested online-only skills view; "
|
||||
"refusing model execution"
|
||||
)
|
||||
|
||||
# ── Idle timeout management ──────────────────────────────────────────
|
||||
|
||||
@@ -485,18 +551,27 @@ class AioSandboxProvider(SandboxProvider):
|
||||
# Deterministic ID for thread-specific, random for anonymous
|
||||
sandbox_id = self._deterministic_sandbox_id(thread_id) if thread_id else str(uuid.uuid4())[:8]
|
||||
|
||||
# ── Layer 1.5: Warm pool (container still running, no cold-start) ──
|
||||
if thread_id:
|
||||
with self._lock:
|
||||
if sandbox_id in self._warm_pool:
|
||||
info, _ = self._warm_pool.pop(sandbox_id)
|
||||
sandbox = AioSandbox(id=sandbox_id, base_url=info.sandbox_url)
|
||||
self._sandboxes[sandbox_id] = sandbox
|
||||
self._sandbox_infos[sandbox_id] = info
|
||||
self._last_activity[sandbox_id] = time.time()
|
||||
self._thread_sandboxes[thread_id] = sandbox_id
|
||||
logger.info(f"Reclaimed warm-pool sandbox {sandbox_id} for thread {thread_id} at {info.sandbox_url}")
|
||||
return sandbox_id
|
||||
# ── Layer 1.5: Warm pool (container still running, no cold-start) ──
|
||||
if thread_id:
|
||||
warm_info: SandboxInfo | None = None
|
||||
with self._lock:
|
||||
if sandbox_id in self._warm_pool:
|
||||
warm_info, _ = self._warm_pool.pop(sandbox_id)
|
||||
if warm_info is not None:
|
||||
sandbox = AioSandbox(id=sandbox_id, base_url=warm_info.sandbox_url)
|
||||
try:
|
||||
self._assert_online_skill_view(sandbox)
|
||||
except Exception as exc:
|
||||
logger.warning("Discarding unattested warm sandbox %s: %s", sandbox_id, exc)
|
||||
self._backend.destroy(warm_info)
|
||||
else:
|
||||
with self._lock:
|
||||
self._sandboxes[sandbox_id] = sandbox
|
||||
self._sandbox_infos[sandbox_id] = warm_info
|
||||
self._last_activity[sandbox_id] = time.time()
|
||||
self._thread_sandboxes[thread_id] = sandbox_id
|
||||
logger.info(f"Reclaimed warm-pool sandbox {sandbox_id} for thread {thread_id} at {warm_info.sandbox_url}")
|
||||
return sandbox_id
|
||||
|
||||
# ── Layer 2: Backend discovery + create (protected by cross-process lock) ──
|
||||
# Use a file lock so that two processes racing to create the same sandbox
|
||||
@@ -514,7 +589,7 @@ class AioSandboxProvider(SandboxProvider):
|
||||
across multiple processes, preventing container-name conflicts.
|
||||
"""
|
||||
paths = get_paths()
|
||||
user_id = get_effective_user_id()
|
||||
user_id = get_effective_user_id()
|
||||
paths.ensure_thread_dirs(thread_id, user_id=user_id)
|
||||
lock_path = paths.thread_dir(thread_id, user_id=user_id) / f"{sandbox_id}.lock"
|
||||
|
||||
@@ -525,34 +600,49 @@ class AioSandboxProvider(SandboxProvider):
|
||||
locked = True
|
||||
# Re-check in-process caches under the file lock in case another
|
||||
# thread in this process won the race while we were waiting.
|
||||
with self._lock:
|
||||
if thread_id in self._thread_sandboxes:
|
||||
existing_id = self._thread_sandboxes[thread_id]
|
||||
if existing_id in self._sandboxes:
|
||||
warm_info: SandboxInfo | None = None
|
||||
with self._lock:
|
||||
if thread_id in self._thread_sandboxes:
|
||||
existing_id = self._thread_sandboxes[thread_id]
|
||||
if existing_id in self._sandboxes:
|
||||
logger.info(f"Reusing in-process sandbox {existing_id} for thread {thread_id} (post-lock check)")
|
||||
self._last_activity[existing_id] = time.time()
|
||||
return existing_id
|
||||
if sandbox_id in self._warm_pool:
|
||||
info, _ = self._warm_pool.pop(sandbox_id)
|
||||
sandbox = AioSandbox(id=sandbox_id, base_url=info.sandbox_url)
|
||||
self._sandboxes[sandbox_id] = sandbox
|
||||
self._sandbox_infos[sandbox_id] = info
|
||||
self._last_activity[sandbox_id] = time.time()
|
||||
self._thread_sandboxes[thread_id] = sandbox_id
|
||||
logger.info(f"Reclaimed warm-pool sandbox {sandbox_id} for thread {thread_id} (post-lock check)")
|
||||
return sandbox_id
|
||||
if sandbox_id in self._warm_pool:
|
||||
warm_info, _ = self._warm_pool.pop(sandbox_id)
|
||||
if warm_info is not None:
|
||||
sandbox = AioSandbox(id=sandbox_id, base_url=warm_info.sandbox_url)
|
||||
try:
|
||||
self._assert_online_skill_view(sandbox)
|
||||
except Exception as exc:
|
||||
logger.warning("Discarding unattested warm sandbox %s: %s", sandbox_id, exc)
|
||||
self._backend.destroy(warm_info)
|
||||
else:
|
||||
with self._lock:
|
||||
self._sandboxes[sandbox_id] = sandbox
|
||||
self._sandbox_infos[sandbox_id] = warm_info
|
||||
self._last_activity[sandbox_id] = time.time()
|
||||
self._thread_sandboxes[thread_id] = sandbox_id
|
||||
logger.info(f"Reclaimed warm-pool sandbox {sandbox_id} for thread {thread_id} (post-lock check)")
|
||||
return sandbox_id
|
||||
|
||||
# Backend discovery: another process may have created the container.
|
||||
discovered = self._backend.discover(sandbox_id)
|
||||
if discovered is not None:
|
||||
sandbox = AioSandbox(id=discovered.sandbox_id, base_url=discovered.sandbox_url)
|
||||
with self._lock:
|
||||
self._sandboxes[discovered.sandbox_id] = sandbox
|
||||
self._sandbox_infos[discovered.sandbox_id] = discovered
|
||||
self._last_activity[discovered.sandbox_id] = time.time()
|
||||
self._thread_sandboxes[thread_id] = discovered.sandbox_id
|
||||
logger.info(f"Discovered existing sandbox {discovered.sandbox_id} for thread {thread_id} at {discovered.sandbox_url}")
|
||||
return discovered.sandbox_id
|
||||
discovered = self._backend.discover(sandbox_id)
|
||||
if discovered is not None:
|
||||
sandbox = AioSandbox(id=discovered.sandbox_id, base_url=discovered.sandbox_url)
|
||||
try:
|
||||
self._assert_online_skill_view(sandbox)
|
||||
except Exception as exc:
|
||||
logger.warning("Destroying unattested discovered sandbox %s: %s", sandbox_id, exc)
|
||||
self._backend.destroy(discovered)
|
||||
else:
|
||||
with self._lock:
|
||||
self._sandboxes[discovered.sandbox_id] = sandbox
|
||||
self._sandbox_infos[discovered.sandbox_id] = discovered
|
||||
self._last_activity[discovered.sandbox_id] = time.time()
|
||||
self._thread_sandboxes[thread_id] = discovered.sandbox_id
|
||||
logger.info(f"Discovered existing sandbox {discovered.sandbox_id} for thread {thread_id} at {discovered.sandbox_url}")
|
||||
return discovered.sandbox_id
|
||||
|
||||
return self._create_sandbox(thread_id, sandbox_id)
|
||||
finally:
|
||||
@@ -616,8 +706,13 @@ class AioSandboxProvider(SandboxProvider):
|
||||
self._backend.destroy(info)
|
||||
raise RuntimeError(f"Sandbox {sandbox_id} failed to become ready within timeout at {info.sandbox_url}")
|
||||
|
||||
sandbox = AioSandbox(id=sandbox_id, base_url=info.sandbox_url)
|
||||
with self._lock:
|
||||
sandbox = AioSandbox(id=sandbox_id, base_url=info.sandbox_url)
|
||||
try:
|
||||
self._assert_online_skill_view(sandbox)
|
||||
except Exception:
|
||||
self._backend.destroy(info)
|
||||
raise
|
||||
with self._lock:
|
||||
self._sandboxes[sandbox_id] = sandbox
|
||||
self._sandbox_infos[sandbox_id] = info
|
||||
self._last_activity[sandbox_id] = time.time()
|
||||
|
||||
+5
-4
@@ -48,10 +48,11 @@ class SandboxBackend(ABC):
|
||||
"""Create/provision a new sandbox.
|
||||
|
||||
Args:
|
||||
thread_id: Thread ID for which the sandbox is being created. Useful for backends that want to organize sandboxes by thread.
|
||||
sandbox_id: Deterministic sandbox identifier.
|
||||
extra_mounts: Additional volume mounts as (host_path, container_path, read_only) tuples.
|
||||
Ignored by backends that don't manage containers (e.g., remote).
|
||||
thread_id: Thread ID for which the sandbox is being created. Useful for backends that want to organize sandboxes by thread.
|
||||
sandbox_id: Deterministic sandbox identifier.
|
||||
extra_mounts: Additional volume mounts as (host_path, container_path, read_only) tuples.
|
||||
Remote provisioners must apply this contract as well; callers
|
||||
may attest security-sensitive mounts after creation.
|
||||
|
||||
Returns:
|
||||
SandboxInfo with connection details.
|
||||
|
||||
+26
-12
@@ -89,18 +89,32 @@ class RemoteSandboxBackend(SandboxBackend):
|
||||
|
||||
# ── Provisioner API calls ─────────────────────────────────────────────
|
||||
|
||||
def _provisioner_create(self, thread_id: str, sandbox_id: str, extra_mounts: list[tuple[str, str, bool]] | None = None) -> SandboxInfo:
|
||||
"""POST /api/sandboxes → create Pod + Service."""
|
||||
try:
|
||||
resp = requests.post(
|
||||
f"{self._provisioner_url}/api/sandboxes",
|
||||
json={
|
||||
"sandbox_id": sandbox_id,
|
||||
"thread_id": thread_id,
|
||||
"environment": self._environment,
|
||||
},
|
||||
timeout=30,
|
||||
)
|
||||
def _provisioner_create(self, thread_id: str, sandbox_id: str, extra_mounts: list[tuple[str, str, bool]] | None = None) -> SandboxInfo:
|
||||
"""POST /api/sandboxes → create Pod + Service."""
|
||||
try:
|
||||
payload: dict[str, object] = {
|
||||
"sandbox_id": sandbox_id,
|
||||
"thread_id": thread_id,
|
||||
"environment": self._environment,
|
||||
}
|
||||
if extra_mounts:
|
||||
# Security contract with the provisioner. In particular this
|
||||
# carries the online-only skills view rather than the canonical
|
||||
# tree. A provisioner that ignores/rejects this contract is
|
||||
# caught by AioSandboxProvider's marker attestation.
|
||||
payload["extra_mounts"] = [
|
||||
{
|
||||
"host_path": host_path,
|
||||
"container_path": container_path,
|
||||
"read_only": read_only,
|
||||
}
|
||||
for host_path, container_path, read_only in extra_mounts
|
||||
]
|
||||
resp = requests.post(
|
||||
f"{self._provisioner_url}/api/sandboxes",
|
||||
json=payload,
|
||||
timeout=30,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
data = resp.json()
|
||||
logger.info(f"Provisioner created sandbox {sandbox_id}: sandbox_url={data['sandbox_url']}")
|
||||
|
||||
Reference in new issue
Block a user