Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

feat(copilot): E2B cloud sandbox — unified file tools, persistent execution, output truncation (#12212)

Zamil Majdy29da8db48e3d
diff --git a/autogpt_platform/.gitignore b/autogpt_platform/.gitignore
index a5b214c8ee..3e31a9970e 100644
--- a/autogpt_platform/.gitignore
+++ b/autogpt_platform/.gitignore
@@ -1,2 +1,3 @@
*.ignore.*
-*.ign.*
\ No newline at end of file
+*.ign.*
+.application.logs
diff --git a/autogpt_platform/backend/Dockerfile b/autogpt_platform/backend/Dockerfile
index 2a9696d3e5..5c4c467cb2 100644
--- a/autogpt_platform/backend/Dockerfile
+++ b/autogpt_platform/backend/Dockerfile
@@ -95,7 +95,7 @@ ENV DEBIAN_FRONTEND=noninteractive
# Install Python, FFmpeg, ImageMagick, and CLI tools for agent use.
# bubblewrap provides OS-level sandbox (whitelist-only FS + no network)
-# for the bash_exec MCP tool.
+# for the bash_exec MCP tool (fallback when E2B is not configured).
# Using --no-install-recommends saves ~650MB by skipping unnecessary deps like llvm, mesa, etc.
RUN apt-get update && apt-get install -y --no-install-recommends \
python3.13 \
diff --git a/autogpt_platform/backend/backend/api/features/chat/routes.py b/autogpt_platform/backend/backend/api/features/chat/routes.py
index 54ecda9695..de4226d2d0 100644
--- a/autogpt_platform/backend/backend/api/features/chat/routes.py
+++ b/autogpt_platform/backend/backend/api/features/chat/routes.py
@@ -247,6 +247,18 @@ async def delete_session(
detail=f"Session {session_id} not found or access denied",
)
+ # Best-effort cleanup of the E2B sandbox (if any).
+ config = ChatConfig()
+ if config.use_e2b_sandbox and config.e2b_api_key:
+ from backend.copilot.tools.e2b_sandbox import kill_sandbox
+
+ try:
+ await kill_sandbox(session_id, config.e2b_api_key)
+ except Exception:
+ logger.warning(
+ "[E2B] Failed to kill sandbox for session %s", session_id[:12]
+ )
+
return Response(status_code=204)
diff --git a/autogpt_platform/backend/backend/copilot/config.py b/autogpt_platform/backend/backend/copilot/config.py
index ce6d37830f..828307d085 100644
--- a/autogpt_platform/backend/backend/copilot/config.py
+++ b/autogpt_platform/backend/backend/copilot/config.py
@@ -93,12 +93,49 @@ class ChatConfig(BaseSettings):
"history compression. Falls back to compression when unavailable.",
)
+ # E2B Sandbox Configuration
+ use_e2b_sandbox: bool = Field(
+ default=True,
+ description="Use E2B cloud sandboxes for persistent bash/python execution. "
+ "When enabled, bash_exec routes commands to E2B and SDK file tools "
+ "operate directly on the sandbox via E2B's filesystem API.",
+ )
+ e2b_api_key: str | None = Field(
+ default=None,
+ description="E2B API key. Falls back to E2B_API_KEY environment variable.",
+ )
+ e2b_sandbox_template: str = Field(
+ default="base",
+ description="E2B sandbox template to use for copilot sessions.",
+ )
+ e2b_sandbox_timeout: int = Field(
+ default=43200, # 12 hours — same as session_ttl
+ description="E2B sandbox keepalive timeout in seconds.",
+ )
+
# Extended thinking configuration for Claude models
thinking_enabled: bool = Field(
default=True,
description="Enable adaptive thinking for Claude models via OpenRouter",
)
+ @field_validator("use_e2b_sandbox", mode="before")
+ @classmethod
+ def get_use_e2b_sandbox(cls, v):
+ """Get use_e2b_sandbox from environment if not provided."""
+ env_val = os.getenv("CHAT_USE_E2B_SANDBOX", "").lower()
+ if env_val:
+ return env_val in ("true", "1", "yes", "on")
+ return True if v is None else v
+
+ @field_validator("e2b_api_key", mode="before")
+ @classmethod
+ def get_e2b_api_key(cls, v):
+ """Get E2B API key from environment if not provided."""
+ if v is None:
+ v = os.getenv("CHAT_E2B_API_KEY") or os.getenv("E2B_API_KEY")
+ return v
+
@field_validator("api_key", mode="before")
@classmethod
def get_api_key(cls, v):
diff --git a/autogpt_platform/backend/backend/copilot/db.py b/autogpt_platform/backend/backend/copilot/db.py
index e1792e98e5..a3330e6172 100644
--- a/autogpt_platform/backend/backend/copilot/db.py
+++ b/autogpt_platform/backend/backend/copilot/db.py
@@ -16,7 +16,7 @@ from prisma.types import (
)
from backend.data import db
-from backend.util.json import SafeJson
+from backend.util.json import SafeJson, sanitize_string
from .model import ChatMessage, ChatSession, ChatSessionInfo
@@ -101,15 +101,16 @@ async def add_chat_message(
"sequence": sequence,
}
- # Add optional string fields
+ # Add optional string fields — sanitize to strip PostgreSQL-incompatible
+ # control characters (null bytes etc.) that may appear in tool outputs.
if content is not None:
- data["content"] = content
+ data["content"] = sanitize_string(content)
if name is not None:
data["name"] = name
if tool_call_id is not None:
data["toolCallId"] = tool_call_id
if refusal is not None:
- data["refusal"] = refusal
+ data["refusal"] = sanitize_string(refusal)
# Add optional JSON fields only when they have values
if tool_calls is not None:
@@ -170,15 +171,16 @@ async def add_chat_messages_batch(
"createdAt": now,
}
- # Add optional string fields
+ # Add optional string fields — sanitize to strip
+ # PostgreSQL-incompatible control characters.
if msg.get("content") is not None:
- data["content"] = msg["content"]
+ data["content"] = sanitize_string(msg["content"])
if msg.get("name") is not None:
data["name"] = msg["name"]
if msg.get("tool_call_id") is not None:
data["toolCallId"] = msg["tool_call_id"]
if msg.get("refusal") is not None:
- data["refusal"] = msg["refusal"]
+ data["refusal"] = sanitize_string(msg["refusal"])
# Add optional JSON fields only when they have values
if msg.get("tool_calls") is not None:
@@ -312,7 +314,7 @@ async def update_tool_message_content(
"toolCallId": tool_call_id,
},
data={
- "content": new_content,
+ "content": sanitize_string(new_content),
},
)
if result == 0:
diff --git a/autogpt_platform/backend/backend/copilot/response_model.py b/autogpt_platform/backend/backend/copilot/response_model.py
index b60c4dfcf8..a650199d4b 100644
--- a/autogpt_platform/backend/backend/copilot/response_model.py
+++ b/autogpt_platform/backend/backend/copilot/response_model.py
@@ -13,6 +13,7 @@ from typing import Any
from pydantic import BaseModel, Field
from backend.util.json import dumps as json_dumps
+from backend.util.truncate import truncate
logger = logging.getLogger(__name__)
@@ -150,6 +151,9 @@ class StreamToolInputAvailable(StreamBaseResponse):
)
+_MAX_TOOL_OUTPUT_SIZE = 100_000 # ~100 KB; truncate to avoid bloating SSE/DB
+
+
class StreamToolOutputAvailable(StreamBaseResponse):
"""Tool execution result."""
@@ -164,6 +168,10 @@ class StreamToolOutputAvailable(StreamBaseResponse):
default=True, description="Whether the tool execution succeeded"
)
+ def model_post_init(self, __context: Any) -> None:
+ """Truncate oversized outputs after construction."""
+ self.output = truncate(self.output, _MAX_TOOL_OUTPUT_SIZE)
+
def to_sse(self) -> str:
"""Convert to SSE format, excluding non-spec fields."""
data = {
diff --git a/autogpt_platform/backend/backend/copilot/sdk/e2b_file_tools.py b/autogpt_platform/backend/backend/copilot/sdk/e2b_file_tools.py
new file mode 100644
index 0000000000..98216ff58e
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/sdk/e2b_file_tools.py
@@ -0,0 +1,360 @@
+"""MCP file-tool handlers that route to the E2B cloud sandbox.
+
+When E2B is active, these tools replace the SDK built-in Read/Write/Edit/
+Glob/Grep so that all file operations share the same ``/home/user``
+filesystem as ``bash_exec``.
+
+SDK-internal paths (``~/.claude/projects/…/tool-results/``) are handled
+by the separate ``Read`` MCP tool registered in ``tool_adapter.py``.
+"""
+
+from __future__ import annotations
+
+import itertools
+import json
+import logging
+import os
+import shlex
+from typing import Any, Callable
+
+from backend.copilot.tools.e2b_sandbox import E2B_WORKDIR
+
+logger = logging.getLogger(__name__)
+
+
+# Lazy imports to break circular dependency with tool_adapter.
+
+
+def _get_sandbox(): # type: ignore[return]
+ from .tool_adapter import get_current_sandbox # noqa: E402
+
+ return get_current_sandbox()
+
+
+def _is_allowed_local(path: str) -> bool:
+ from .tool_adapter import is_allowed_local_path # noqa: E402
+
+ return is_allowed_local_path(path)
+
+
+def _resolve_remote(path: str) -> str:
+ """Normalise *path* to an absolute sandbox path under ``/home/user``.
+
+ Raises :class:`ValueError` if the resolved path escapes the sandbox.
+ """
+ candidate = path if os.path.isabs(path) else os.path.join(E2B_WORKDIR, path)
+ normalized = os.path.normpath(candidate)
+ if normalized != E2B_WORKDIR and not normalized.startswith(E2B_WORKDIR + "/"):
+ raise ValueError(f"Path must be within {E2B_WORKDIR}: {path}")
+ return normalized
+
+
+def _mcp(text: str, *, error: bool = False) -> dict[str, Any]:
+ if error:
+ text = json.dumps({"error": text, "type": "error"})
+ return {"content": [{"type": "text", "text": text}], "isError": error}
+
+
+def _get_sandbox_and_path(
+ file_path: str,
+) -> tuple[Any, str] | dict[str, Any]:
+ """Common preamble: get sandbox + resolve path, or return MCP error."""
+ sandbox = _get_sandbox()
+ if sandbox is None:
+ return _mcp("No E2B sandbox available", error=True)
+ try:
+ remote = _resolve_remote(file_path)
+ except ValueError as exc:
+ return _mcp(str(exc), error=True)
+ return sandbox, remote
+
+
+# Tool handlers
+
+
+async def _handle_read_file(args: dict[str, Any]) -> dict[str, Any]:
+ file_path: str = args.get("file_path", "")
+ offset: int = max(0, int(args.get("offset", 0)))
+ limit: int = max(1, int(args.get("limit", 2000)))
+
+ if not file_path:
+ return _mcp("file_path is required", error=True)
+
+ # SDK-internal paths (tool-results, ephemeral working dir) stay on the host.
+ if _is_allowed_local(file_path):
+ return _read_local(file_path, offset, limit)
+
+ result = _get_sandbox_and_path(file_path)
+ if isinstance(result, dict):
+ return result
+ sandbox, remote = result
+
+ try:
+ raw: bytes = await sandbox.files.read(remote, format="bytes")
+ content = raw.decode("utf-8", errors="replace")
+ except Exception as exc:
+ return _mcp(f"Failed to read {remote}: {exc}", error=True)
+
+ lines = content.splitlines(keepends=True)
+ selected = list(itertools.islice(lines, offset, offset + limit))
+ numbered = "".join(
+ f"{i + offset + 1:>6}\t{line}" for i, line in enumerate(selected)
+ )
+ return _mcp(numbered)
+
+
+async def _handle_write_file(args: dict[str, Any]) -> dict[str, Any]:
+ file_path: str = args.get("file_path", "")
+ content: str = args.get("content", "")
+
+ if not file_path:
+ return _mcp("file_path is required", error=True)
+
+ result = _get_sandbox_and_path(file_path)
+ if isinstance(result, dict):
+ return result
+ sandbox, remote = result
+
+ try:
+ parent = os.path.dirname(remote)
+ if parent and parent != E2B_WORKDIR:
+ await sandbox.files.make_dir(parent)
+ await sandbox.files.write(remote, content)
+ except Exception as exc:
+ return _mcp(f"Failed to write {remote}: {exc}", error=True)
+
+ return _mcp(f"Successfully wrote to {remote}")
+
+
+async def _handle_edit_file(args: dict[str, Any]) -> dict[str, Any]:
+ file_path: str = args.get("file_path", "")
+ old_string: str = args.get("old_string", "")
+ new_string: str = args.get("new_string", "")
+ replace_all: bool = args.get("replace_all", False)
+
+ if not file_path:
+ return _mcp("file_path is required", error=True)
+ if not old_string:
+ return _mcp("old_string is required", error=True)
+
+ result = _get_sandbox_and_path(file_path)
+ if isinstance(result, dict):
+ return result
+ sandbox, remote = result
+
+ try:
+ raw: bytes = await sandbox.files.read(remote, format="bytes")
+ content = raw.decode("utf-8", errors="replace")
+ except Exception as exc:
+ return _mcp(f"Failed to read {remote}: {exc}", error=True)
+
+ count = content.count(old_string)
+ if count == 0:
+ return _mcp(f"old_string not found in {file_path}", error=True)
+ if count > 1 and not replace_all:
+ return _mcp(
+ f"old_string appears {count} times in {file_path}. "
+ "Use replace_all=true or provide a more unique string.",
+ error=True,
+ )
+
+ updated = (
+ content.replace(old_string, new_string)
+ if replace_all
+ else content.replace(old_string, new_string, 1)
+ )
+ try:
+ await sandbox.files.write(remote, updated)
+ except Exception as exc:
+ return _mcp(f"Failed to write {remote}: {exc}", error=True)
+
+ return _mcp(f"Edited {remote} ({count} replacement{'s' if count > 1 else ''})")
+
+
+async def _handle_glob(args: dict[str, Any]) -> dict[str, Any]:
+ pattern: str = args.get("pattern", "")
+ path: str = args.get("path", "")
+
+ if not pattern:
+ return _mcp("pattern is required", error=True)
+
+ sandbox = _get_sandbox()
+ if sandbox is None:
+ return _mcp("No E2B sandbox available", error=True)
+
+ try:
+ search_dir = _resolve_remote(path) if path else E2B_WORKDIR
+ except ValueError as exc:
+ return _mcp(str(exc), error=True)
+
+ cmd = f"find {shlex.quote(search_dir)} -name {shlex.quote(pattern)} -type f 2>/dev/null | head -500"
+ try:
+ result = await sandbox.commands.run(cmd, cwd=E2B_WORKDIR, timeout=10)
+ except Exception as exc:
+ return _mcp(f"Glob failed: {exc}", error=True)
+
+ files = [line for line in (result.stdout or "").strip().splitlines() if line]
+ return _mcp(json.dumps(files, indent=2))
+
+
+async def _handle_grep(args: dict[str, Any]) -> dict[str, Any]:
+ pattern: str = args.get("pattern", "")
+ path: str = args.get("path", "")
+ include: str = args.get("include", "")
+
+ if not pattern:
+ return _mcp("pattern is required", error=True)
+
+ sandbox = _get_sandbox()
+ if sandbox is None:
+ return _mcp("No E2B sandbox available", error=True)
+
+ try:
+ search_dir = _resolve_remote(path) if path else E2B_WORKDIR
+ except ValueError as exc:
+ return _mcp(str(exc), error=True)
+
+ parts = ["grep", "-rn", "--color=never"]
+ if include:
+ parts.extend(["--include", include])
+ parts.extend([pattern, search_dir])
+ cmd = " ".join(shlex.quote(p) for p in parts) + " 2>/dev/null | head -200"
+
+ try:
+ result = await sandbox.commands.run(cmd, cwd=E2B_WORKDIR, timeout=15)
+ except Exception as exc:
+ return _mcp(f"Grep failed: {exc}", error=True)
+
+ output = (result.stdout or "").strip()
+ return _mcp(output if output else "No matches found.")
+
+
+# Local read (for SDK-internal paths)
+
+
+def _read_local(file_path: str, offset: int, limit: int) -> dict[str, Any]:
+ """Read from the host filesystem (defence-in-depth path check)."""
+ if not _is_allowed_local(file_path):
+ return _mcp(f"Path not allowed: {file_path}", error=True)
+ expanded = os.path.realpath(os.path.expanduser(file_path))
+ try:
+ with open(expanded) as fh:
+ selected = list(itertools.islice(fh, offset, offset + limit))
+ numbered = "".join(
+ f"{i + offset + 1:>6}\t{line}" for i, line in enumerate(selected)
+ )
+ return _mcp(numbered)
+ except FileNotFoundError:
+ return _mcp(f"File not found: {file_path}", error=True)
+ except Exception as exc:
+ return _mcp(f"Error reading {file_path}: {exc}", error=True)
+
+
+# Tool descriptors (name, description, schema, handler)
+
+E2B_FILE_TOOLS: list[tuple[str, str, dict[str, Any], Callable[..., Any]]] = [
+ (
+ "read_file",
+ "Read a file from the cloud sandbox (/home/user). "
+ "Use offset and limit for large files.",
+ {
+ "type": "object",
+ "properties": {
+ "file_path": {
+ "type": "string",
+ "description": "Path (relative to /home/user, or absolute).",
+ },
+ "offset": {
+ "type": "integer",
+ "description": "Line to start reading from (0-indexed). Default: 0.",
+ },
+ "limit": {
+ "type": "integer",
+ "description": "Number of lines to read. Default: 2000.",
+ },
+ },
+ "required": ["file_path"],
+ },
+ _handle_read_file,
+ ),
+ (
+ "write_file",
+ "Write or create a file in the cloud sandbox (/home/user). "
+ "Parent directories are created automatically.",
+ {
+ "type": "object",
+ "properties": {
+ "file_path": {
+ "type": "string",
+ "description": "Path (relative to /home/user, or absolute).",
+ },
+ "content": {"type": "string", "description": "Content to write."},
+ },
+ "required": ["file_path", "content"],
+ },
+ _handle_write_file,
+ ),
+ (
+ "edit_file",
+ "Targeted text replacement in a sandbox file. "
+ "old_string must appear in the file and is replaced with new_string.",
+ {
+ "type": "object",
+ "properties": {
+ "file_path": {
+ "type": "string",
+ "description": "Path (relative to /home/user, or absolute).",
+ },
+ "old_string": {"type": "string", "description": "Text to find."},
+ "new_string": {"type": "string", "description": "Replacement text."},
+ "replace_all": {
+ "type": "boolean",
+ "description": "Replace all occurrences (default: false).",
+ },
+ },
+ "required": ["file_path", "old_string", "new_string"],
+ },
+ _handle_edit_file,
+ ),
+ (
+ "glob",
+ "Search for files by name pattern in the cloud sandbox.",
+ {
+ "type": "object",
+ "properties": {
+ "pattern": {
+ "type": "string",
+ "description": "Glob pattern (e.g. *.py).",
+ },
+ "path": {
+ "type": "string",
+ "description": "Directory to search. Default: /home/user.",
+ },
+ },
+ "required": ["pattern"],
+ },
+ _handle_glob,
+ ),
+ (
+ "grep",
+ "Search file contents by regex in the cloud sandbox.",
+ {
+ "type": "object",
+ "properties": {
+ "pattern": {"type": "string", "description": "Regex pattern."},
+ "path": {
+ "type": "string",
+ "description": "File or directory. Default: /home/user.",
+ },
+ "include": {
+ "type": "string",
+ "description": "Glob to filter files (e.g. *.py).",
+ },
+ },
+ "required": ["pattern"],
+ },
+ _handle_grep,
+ ),
+]
+
+E2B_FILE_TOOL_NAMES: list[str] = [name for name, *_ in E2B_FILE_TOOLS]
diff --git a/autogpt_platform/backend/backend/copilot/sdk/e2b_file_tools_test.py b/autogpt_platform/backend/backend/copilot/sdk/e2b_file_tools_test.py
new file mode 100644
index 0000000000..5d3276f3c3
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/sdk/e2b_file_tools_test.py
@@ -0,0 +1,153 @@
+"""Tests for E2B file-tool path validation and local read safety.
+
+Pure unit tests with no external dependencies (no E2B, no sandbox).
+"""
+
+import os
+
+import pytest
+
+from .e2b_file_tools import _read_local, _resolve_remote
+from .tool_adapter import _current_project_dir
+
+_SDK_PROJECTS_DIR = os.path.realpath(os.path.expanduser("~/.claude/projects"))
+
+
+# ---------------------------------------------------------------------------
+# _resolve_remote — sandbox path normalisation & boundary enforcement
+# ---------------------------------------------------------------------------
+
+
+class TestResolveRemote:
+ def test_relative_path_resolved(self):
+ assert _resolve_remote("src/main.py") == "/home/user/src/main.py"
+
+ def test_absolute_within_sandbox(self):
+ assert _resolve_remote("/home/user/file.txt") == "/home/user/file.txt"
+
+ def test_workdir_itself(self):
+ assert _resolve_remote("/home/user") == "/home/user"
+
+ def test_relative_dotslash(self):
+ assert _resolve_remote("./README.md") == "/home/user/README.md"
+
+ def test_traversal_blocked(self):
+ with pytest.raises(ValueError, match="must be within /home/user"):
+ _resolve_remote("../../etc/passwd")
+
+ def test_absolute_traversal_blocked(self):
+ with pytest.raises(ValueError, match="must be within /home/user"):
+ _resolve_remote("/home/user/../../etc/passwd")
+
+ def test_absolute_outside_sandbox_blocked(self):
+ with pytest.raises(ValueError, match="must be within /home/user"):
+ _resolve_remote("/etc/passwd")
+
+ def test_root_blocked(self):
+ with pytest.raises(ValueError, match="must be within /home/user"):
+ _resolve_remote("/")
+
+ def test_home_other_user_blocked(self):
+ with pytest.raises(ValueError, match="must be within /home/user"):
+ _resolve_remote("/home/other/file.txt")
+
+ def test_deep_nested_allowed(self):
+ assert _resolve_remote("a/b/c/d/e.txt") == "/home/user/a/b/c/d/e.txt"
+
+ def test_trailing_slash_normalised(self):
+ assert _resolve_remote("src/") == "/home/user/src"
+
+ def test_double_dots_within_sandbox_ok(self):
+ """Path that resolves back within /home/user is allowed."""
+ assert _resolve_remote("a/b/../c.txt") == "/home/user/a/c.txt"
+
+
+# ---------------------------------------------------------------------------
+# _read_local — host filesystem reads with allowlist enforcement
+#
+# In E2B mode, _read_local only allows tool-results paths (via
+# is_allowed_local_path without sdk_cwd). Regular files live on the
+# sandbox, not the host.
+# ---------------------------------------------------------------------------
+
+
+class TestReadLocal:
+ def _make_tool_results_file(self, encoded: str, filename: str, content: str) -> str:
+ """Create a tool-results file and return its path."""
+ tool_results_dir = os.path.join(_SDK_PROJECTS_DIR, encoded, "tool-results")
+ os.makedirs(tool_results_dir, exist_ok=True)
+ filepath = os.path.join(tool_results_dir, filename)
+ with open(filepath, "w") as f:
+ f.write(content)
+ return filepath
+
+ def test_read_tool_results_file(self):
+ """Reading a tool-results file should succeed."""
+ encoded = "-tmp-copilot-e2b-test-read"
+ filepath = self._make_tool_results_file(
+ encoded, "result.txt", "line 1\nline 2\nline 3\n"
+ )
+ token = _current_project_dir.set(encoded)
+ try:
+ result = _read_local(filepath, offset=0, limit=2000)
+ assert result["isError"] is False
+ assert "line 1" in result["content"][0]["text"]
+ assert "line 2" in result["content"][0]["text"]
+ finally:
+ _current_project_dir.reset(token)
+ os.unlink(filepath)
+
+ def test_read_disallowed_path_blocked(self):
+ """Reading /etc/passwd should be blocked by the allowlist."""
+ result = _read_local("/etc/passwd", offset=0, limit=10)
+ assert result["isError"] is True
+ assert "not allowed" in result["content"][0]["text"].lower()
+
+ def test_read_nonexistent_tool_results(self):
+ """A tool-results path that doesn't exist returns FileNotFoundError."""
+ encoded = "-tmp-copilot-e2b-test-nofile"
+ tool_results_dir = os.path.join(_SDK_PROJECTS_DIR, encoded, "tool-results")
+ os.makedirs(tool_results_dir, exist_ok=True)
+ filepath = os.path.join(tool_results_dir, "nonexistent.txt")
+ token = _current_project_dir.set(encoded)
+ try:
+ result = _read_local(filepath, offset=0, limit=10)
+ assert result["isError"] is True
+ assert "not found" in result["content"][0]["text"].lower()
+ finally:
+ _current_project_dir.reset(token)
+ os.rmdir(tool_results_dir)
+
+ def test_read_traversal_path_blocked(self):
+ """A traversal attempt that escapes allowed directories is blocked."""
+ result = _read_local("/tmp/copilot-abc/../../etc/shadow", offset=0, limit=10)
+ assert result["isError"] is True
+ assert "not allowed" in result["content"][0]["text"].lower()
+
+ def test_read_arbitrary_host_path_blocked(self):
+ """Arbitrary host paths are blocked even if they exist."""
+ result = _read_local("/proc/self/environ", offset=0, limit=10)
+ assert result["isError"] is True
+
+ def test_read_with_offset_and_limit(self):
+ """Offset and limit should control which lines are returned."""
+ encoded = "-tmp-copilot-e2b-test-offset"
+ content = "".join(f"line {i}\n" for i in range(10))
+ filepath = self._make_tool_results_file(encoded, "lines.txt", content)
+ token = _current_project_dir.set(encoded)
+ try:
+ result = _read_local(filepath, offset=3, limit=2)
+ assert result["isError"] is False
+ text = result["content"][0]["text"]
+ assert "line 3" in text
+ assert "line 4" in text
+ assert "line 2" not in text
+ assert "line 5" not in text
+ finally:
+ _current_project_dir.reset(token)
+ os.unlink(filepath)
+
+ def test_read_without_project_dir_blocks_all(self):
+ """Without _current_project_dir set, all paths are blocked."""
+ result = _read_local("/tmp/anything.txt", offset=0, limit=10)
+ assert result["isError"] is True
diff --git a/autogpt_platform/backend/backend/copilot/sdk/security_hooks.py b/autogpt_platform/backend/backend/copilot/sdk/security_hooks.py
index c2e7788d18..0d6e4817ae 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/security_hooks.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/security_hooks.py
@@ -6,7 +6,6 @@ ensuring multi-user isolation and preventing unauthorized operations.
import json
import logging
-import os
import re
from collections.abc import Callable
from typing import Any, cast
@@ -16,6 +15,7 @@ from .tool_adapter import (
DANGEROUS_PATTERNS,
MCP_TOOL_PREFIX,
WORKSPACE_SCOPED_TOOLS,
+ is_allowed_local_path,
stash_pending_tool_output,
)
@@ -38,40 +38,20 @@ def _validate_workspace_path(
) -> dict[str, Any]:
"""Validate that a workspace-scoped tool only accesses allowed paths.
- Allowed directories:
+ Delegates to :func:`is_allowed_local_path` which permits:
- The SDK working directory (``/tmp/copilot-<session>/``)
- - The SDK tool-results directory (``~/.claude/projects/…/tool-results/``)
+ - The current session's tool-results directory
+ (``~/.claude/projects/<encoded-cwd>/tool-results/``)
"""
path = tool_input.get("file_path") or tool_input.get("path") or ""
if not path:
# Glob/Grep without a path default to cwd which is already sandboxed
return {}
- # Resolve relative paths against sdk_cwd (the SDK sets cwd so the LLM
- # naturally uses relative paths like "test.txt" instead of absolute ones).
- # Tilde paths (~/) are home-dir references, not relative — expand first.
- if path.startswith("~"):
- resolved = os.path.realpath(os.path.expanduser(path))
- elif not os.path.isabs(path) and sdk_cwd:
- resolved = os.path.realpath(os.path.join(sdk_cwd, path))
- else:
- resolved = os.path.realpath(path)
-
- # Allow access within the SDK working directory
- if sdk_cwd:
- norm_cwd = os.path.realpath(sdk_cwd)
- if resolved.startswith(norm_cwd + os.sep) or resolved == norm_cwd:
- return {}
-
- # Allow access to ~/.claude/projects/*/tool-results/ (big tool results)
- claude_dir = os.path.realpath(os.path.expanduser("~/.claude/projects"))
- tool_results_seg = os.sep + "tool-results" + os.sep
- if resolved.startswith(claude_dir + os.sep) and tool_results_seg in resolved:
+ if is_allowed_local_path(path, sdk_cwd):
return {}
- logger.warning(
- f"Blocked {tool_name} outside workspace: {path} (resolved={resolved})"
- )
+ logger.warning(f"Blocked {tool_name} outside workspace: {path}")
workspace_hint = f" Allowed workspace: {sdk_cwd}" if sdk_cwd else ""
return _deny(
f"[SECURITY] Tool '{tool_name}' can only access files within the workspace "
diff --git a/autogpt_platform/backend/backend/copilot/sdk/security_hooks_test.py b/autogpt_platform/backend/backend/copilot/sdk/security_hooks_test.py
index bc49bbb1b9..bf05bb33e2 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/security_hooks_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/security_hooks_test.py
@@ -120,17 +120,31 @@ def test_read_no_cwd_denies_absolute():
def test_read_tool_results_allowed():
+ from .tool_adapter import _current_project_dir
+
home = os.path.expanduser("~")
path = f"{home}/.claude/projects/-tmp-copilot-abc123/tool-results/12345.txt"
- result = _validate_tool_access("Read", {"file_path": path}, sdk_cwd=SDK_CWD)
- assert result == {}
+ # is_allowed_local_path requires the session's encoded cwd to be set
+ token = _current_project_dir.set("-tmp-copilot-abc123")
+ try:
+ result = _validate_tool_access("Read", {"file_path": path}, sdk_cwd=SDK_CWD)
+ assert result == {}
+ finally:
+ _current_project_dir.reset(token)
+
+def test_read_claude_projects_session_dir_allowed():
+ """Files within the current session's project dir are allowed."""
+ from .tool_adapter import _current_project_dir
-def test_read_claude_projects_without_tool_results_denied():
home = os.path.expanduser("~")
path = f"{home}/.claude/projects/-tmp-copilot-abc123/settings.json"
- result = _validate_tool_access("Read", {"file_path": path}, sdk_cwd=SDK_CWD)
- assert _is_denied(result)
+ token = _current_project_dir.set("-tmp-copilot-abc123")
+ try:
+ result = _validate_tool_access("Read", {"file_path": path}, sdk_cwd=SDK_CWD)
+ assert not _is_denied(result)
+ finally:
+ _current_project_dir.reset(token)
# -- Built-in Bash is blocked (use bash_exec MCP tool instead) ---------------
diff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.py
index 7016b081ed..1ea1ab1a85 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/service.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py
@@ -5,18 +5,28 @@ import base64
import json
import logging
import os
+import shutil
import sys
import uuid
from collections.abc import AsyncGenerator
from dataclasses import dataclass
from typing import Any, cast
+import openai
+from claude_agent_sdk import (
+ AssistantMessage,
+ ClaudeAgentOptions,
+ ClaudeSDKClient,
+ ResultMessage,
+ ToolUseBlock,
+)
from langfuse import propagate_attributes
from langsmith.integrations.claude_agent_sdk import configure_claude_agent_sdk
from backend.data.redis_client import get_redis_async
from backend.executor.cluster_lock import AsyncClusterLock
from backend.util.exceptions import NotFoundError
+from backend.util.prompt import compress_context
from backend.util.settings import Settings
from ..config import ChatConfig
@@ -42,14 +52,15 @@ from ..service import (
_generate_session_title,
_is_langfuse_configured,
)
+from ..tools.e2b_sandbox import get_or_create_sandbox
from ..tools.sandbox import WORKSPACE_PREFIX, make_session_path
from ..tracking import track_user_message
from .response_adapter import SDKResponseAdapter
from .security_hooks import create_security_hooks
from .tool_adapter import (
- COPILOT_TOOL_NAMES,
- SDK_DISALLOWED_TOOLS,
create_copilot_mcp_server,
+ get_copilot_tool_names,
+ get_sdk_disallowed_tools,
set_execution_context,
wait_for_stash,
)
@@ -148,9 +159,36 @@ _HEARTBEAT_INTERVAL = 3.0 # seconds
# Appended to the system prompt to inform the agent about available tools.
# The SDK built-in Bash is NOT available — use mcp__copilot__bash_exec instead,
# which has kernel-level network isolation (unshare --net).
-def _build_sdk_tool_supplement(cwd: str) -> str:
- """Build the SDK tool supplement with the actual working directory injected."""
- return f"""
+_SHARED_TOOL_NOTES = """\
+
+### Sharing files with the user
+After saving a file to the persistent workspace with `write_workspace_file`,
+share it with the user by embedding the `download_url` from the response in
+your message as a Markdown link or image:
+
+- **Any file** — shows as a clickable download link:
+ `[report.csv](workspace://file_id#text/csv)`
+- **Image** — renders inline in chat:
+ `![chart](workspace://file_id#image/png)`
+- **Video** — renders inline in chat with player controls:
+ `![recording](workspace://file_id#video/mp4)`
+
+The `download_url` field in the `write_workspace_file` response is already
+in the correct format — paste it directly after the `(` in the Markdown.
+
+### Long-running tools
+Long-running tools (create_agent, edit_agent, etc.) are handled
+asynchronously. You will receive an immediate response; the actual result
+is delivered to the user via a background stream.
+
+### Sub-agent tasks
+- When using the Task tool, NEVER set `run_in_background` to true.
+ All tasks must run in the foreground.
+"""
+
+
+_LOCAL_TOOL_SUPPLEMENT = (
+ """
## Tool notes
@@ -192,31 +230,57 @@ When you create or modify important files (code, configs, outputs), you MUST:
1. Save them using `write_workspace_file` so they persist
2. At the start of a new turn, call `list_workspace_files` to see what files
are available from previous turns
+"""
+ + _SHARED_TOOL_NOTES
+)
-### Sharing files with the user
-After saving a file to the persistent workspace with `write_workspace_file`,
-share it with the user by embedding the `download_url` from the response in
-your message as a Markdown link or image:
-- **Any file** — shows as a clickable download link:
- `[report.csv](workspace://file_id#text/csv)`
-- **Image** — renders inline in chat:
- `![chart](workspace://file_id#image/png)`
-- **Video** — renders inline in chat with player controls:
- `![recording](workspace://file_id#video/mp4)`
+_E2B_TOOL_SUPPLEMENT = (
+ """
-The `download_url` field in the `write_workspace_file` response is already
-in the correct format — paste it directly after the `(` in the Markdown.
+## Tool notes
-### Long-running tools
-Long-running tools (create_agent, edit_agent, etc.) are handled
-asynchronously. You will receive an immediate response; the actual result
-is delivered to the user via a background stream.
+### Shell commands
+- The SDK built-in Bash tool is NOT available. Use the `bash_exec` MCP tool
+ for shell commands — it runs in a cloud sandbox with full internet access.
-### Sub-agent tasks
-- When using the Task tool, NEVER set `run_in_background` to true.
- All tasks must run in the foreground.
+### Working directory
+- Your working directory is: `/home/user` (cloud sandbox)
+- All file tools (`read_file`, `write_file`, `edit_file`, `glob`, `grep`)
+ AND `bash_exec` operate on the **same cloud sandbox filesystem**.
+- Files created by `bash_exec` are immediately visible to `read_file` and
+ vice-versa — they share one filesystem.
+- Use relative paths (resolved from `/home/user`) or absolute paths.
+
+### Two storage systems — CRITICAL to understand
+
+1. **Cloud sandbox** (`/home/user`):
+ - Shared by all file tools AND `bash_exec` — same filesystem
+ - Files **persist across turns** within the current session
+ - Full Linux environment with internet access
+ - Lost when the session expires (12 h inactivity)
+
+2. **Persistent workspace** (cloud storage):
+ - Files here **survive across sessions indefinitely**
+ - Use `write_workspace_file` to save important files permanently
+ - Use `read_workspace_file` to retrieve previously saved files
+ - Use `list_workspace_files` to see what files you've saved before
+ - Call `list_workspace_files(include_all_sessions=True)` to see files from
+ all sessions
+
+### Moving files between sandbox and persistent storage
+- **Sandbox → Persistent**: Use `write_workspace_file` with `source_path`
+ to copy from the sandbox to permanent storage
+- **Persistent → Sandbox**: Use `read_workspace_file` with `save_to_path`
+ to download into the sandbox for processing
+
+### File persistence workflow
+Important files that must survive beyond this session should be saved with
+`write_workspace_file`. Sandbox files persist across turns but are lost
+when the session expires.
"""
+ + _SHARED_TOOL_NOTES
+)
STREAM_LOCK_PREFIX = "copilot:stream:lock:"
@@ -291,8 +355,6 @@ def _cleanup_sdk_tool_results(cwd: str) -> None:
Security: *cwd* MUST be created by ``_make_sdk_cwd()`` which sanitizes
the session_id.
"""
- import shutil
-
normalized = os.path.normpath(cwd)
if not normalized.startswith(_SDK_CWD_PREFIX):
logger.warning(f"[SDK] Rejecting cleanup for path outside workspace: {cwd}")
@@ -324,8 +386,6 @@ async def _compress_conversation_history(
if len(messages) < 2:
return messages
- from backend.util.prompt import compress_context
-
# Convert ChatMessages to dicts for compress_context
messages_dict = []
for msg in messages:
@@ -339,8 +399,6 @@ async def _compress_conversation_history(
messages_dict.append(msg_dict)
try:
- import openai
-
async with openai.AsyncOpenAI(
api_key=config.api_key, base_url=config.base_url, timeout=30.0
) as client:
@@ -550,6 +608,7 @@ async def stream_chat_completion_sdk(
message_id = str(uuid.uuid4())
stream_id = str(uuid.uuid4())
stream_completed = False
+ e2b_sandbox = None
use_resume = False
resume_file: str | None = None
captured_transcript = CapturedTranscript()
@@ -579,7 +638,7 @@ async def stream_chat_completion_sdk(
# OTEL context manager — initialized inside the try and cleaned up in finally.
_otel_ctx: Any = None
- # Make sure there is no more code between the lock acquitition and try-block.
+ # Make sure there is no more code between the lock acquisition and try-block.
try:
# Build system prompt (reuses non-SDK path with Langfuse support).
# Pre-compute the cwd here so the exact working directory path can be
@@ -597,17 +656,47 @@ async def stream_chat_completion_sdk(
code="sdk_cwd_error",
)
return
+ # Set up E2B sandbox for persistent cloud execution when configured.
+ # When active, MCP file tools route directly to the sandbox filesystem
+ # so bash_exec and file tools share the same /home/user directory.
+ if config.use_e2b_sandbox and not config.e2b_api_key:
+ logger.warning(
+ "[E2B] [%s] E2B sandbox enabled but no API key configured "
+ "(CHAT_E2B_API_KEY / E2B_API_KEY) — falling back to bubblewrap",
+ session_id[:12],
+ )
+ if config.use_e2b_sandbox and config.e2b_api_key:
+ try:
+ e2b_sandbox = await get_or_create_sandbox(
+ session_id,
+ api_key=config.e2b_api_key,
+ template=config.e2b_sandbox_template,
+ timeout=config.e2b_sandbox_timeout,
+ )
+ except Exception as e2b_err:
+ logger.error(
+ "[E2B] [%s] Setup failed: %s",
+ session_id[:12],
+ e2b_err,
+ exc_info=True,
+ )
+ e2b_sandbox = None
+
+ use_e2b = e2b_sandbox is not None
+
system_prompt, _ = await _build_system_prompt(
user_id, has_conversation_history=has_history
)
- system_prompt += _build_sdk_tool_supplement(sdk_cwd)
+ system_prompt += (
+ _E2B_TOOL_SUPPLEMENT
+ if use_e2b
+ else _LOCAL_TOOL_SUPPLEMENT.format(cwd=sdk_cwd)
+ )
yield StreamStart(messageId=message_id, sessionId=session_id)
- set_execution_context(user_id, session)
+ set_execution_context(user_id, session, sandbox=e2b_sandbox, sdk_cwd=sdk_cwd)
try:
- from claude_agent_sdk import ClaudeAgentOptions, ClaudeSDKClient
-
# Fail fast when no API credentials are available at all
sdk_env = _build_sdk_env()
if not sdk_env and not os.environ.get("ANTHROPIC_API_KEY"):
@@ -617,7 +706,7 @@ async def stream_chat_completion_sdk(
"or ANTHROPIC_API_KEY for direct Anthropic access."
)
- mcp_server = create_copilot_mcp_server()
+ mcp_server = create_copilot_mcp_server(use_e2b=use_e2b)
sdk_model = _resolve_sdk_model()
@@ -678,11 +767,13 @@ async def stream_chat_completion_sdk(
f"({len(session.messages)} messages in session)"
)
+ allowed = get_copilot_tool_names(use_e2b=use_e2b)
+ disallowed = get_sdk_disallowed_tools(use_e2b=use_e2b)
sdk_options_kwargs: dict[str, Any] = {
"system_prompt": system_prompt,
"mcp_servers": {"copilot": mcp_server},
- "allowed_tools": COPILOT_TOOL_NAMES,
- "disallowed_tools": SDK_DISALLOWED_TOOLS,
+ "allowed_tools": allowed,
+ "disallowed_tools": disallowed,
"hooks": security_hooks,
"cwd": sdk_cwd,
"max_buffer_size": config.claude_agent_max_buffer_size,
@@ -827,12 +918,6 @@ async def stream_chat_completion_sdk(
# AssistantMessages (each containing only
# ToolUseBlocks), we must NOT wait/flush — the prior
# tools are still executing concurrently.
- from claude_agent_sdk import (
- AssistantMessage,
- ResultMessage,
- ToolUseBlock,
- )
-
is_parallel_continuation = isinstance(
sdk_msg, AssistantMessage
) and all(isinstance(b, ToolUseBlock) for b in sdk_msg.content)
diff --git a/autogpt_platform/backend/backend/copilot/sdk/tool_adapter.py b/autogpt_platform/backend/backend/copilot/sdk/tool_adapter.py
index fdc1cea863..ae81a74e4f 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/tool_adapter.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/tool_adapter.py
@@ -9,20 +9,84 @@ import itertools
import json
import logging
import os
+import re
import uuid
from contextvars import ContextVar
-from typing import Any
+from typing import TYPE_CHECKING, Any
+
+from claude_agent_sdk import create_sdk_mcp_server, tool
from backend.copilot.model import ChatSession
from backend.copilot.tools import TOOL_REGISTRY
from backend.copilot.tools.base import BaseTool
+from backend.util.truncate import truncate
+
+from .e2b_file_tools import E2B_FILE_TOOL_NAMES, E2B_FILE_TOOLS
+
+if TYPE_CHECKING:
+ from e2b import AsyncSandbox
logger = logging.getLogger(__name__)
# Allowed base directory for the Read tool (SDK saves oversized tool results here).
# Restricted to ~/.claude/projects/ and further validated to require "tool-results"
# in the path — prevents reading settings, credentials, or other sensitive files.
-_SDK_PROJECTS_DIR = os.path.expanduser("~/.claude/projects/")
+_SDK_PROJECTS_DIR = os.path.realpath(os.path.expanduser("~/.claude/projects"))
+
+# Max MCP response size in chars — keeps tool output under the SDK's 10 MB JSON buffer.
+_MCP_MAX_CHARS = 500_000
+
+# Context variable holding the encoded project directory name for the current
+# session (e.g. "-private-tmp-copilot-<uuid>"). Set by set_execution_context()
+# so that path validation can scope tool-results reads to the current session.
+_current_project_dir: ContextVar[str] = ContextVar("_current_project_dir", default="")
+
+
+def _encode_cwd_for_cli(cwd: str) -> str:
+ """Encode a working directory path the same way the Claude CLI does.
+
+ The CLI replaces all non-alphanumeric characters with ``-``.
+ """
+ return re.sub(r"[^a-zA-Z0-9]", "-", os.path.realpath(cwd))
+
+
+def is_allowed_local_path(path: str, sdk_cwd: str | None = None) -> bool:
+ """Check whether *path* is an allowed host-filesystem path.
+
+ Allowed:
+ - Files under *sdk_cwd* (``/tmp/copilot-<session>/``)
+ - Files under ``~/.claude/projects/<encoded-cwd>/`` — the SDK's
+ project directory for this session (tool-results, transcripts, etc.)
+
+ Both checks are scoped to the **current session** so sessions cannot
+ read each other's data.
+ """
+ if not path:
+ return False
+
+ if path.startswith("~"):
+ resolved = os.path.realpath(os.path.expanduser(path))
+ elif not os.path.isabs(path) and sdk_cwd:
+ resolved = os.path.realpath(os.path.join(sdk_cwd, path))
+ else:
+ resolved = os.path.realpath(path)
+
+ # Allow access within the SDK working directory
+ if sdk_cwd:
+ norm_cwd = os.path.realpath(sdk_cwd)
+ if resolved == norm_cwd or resolved.startswith(norm_cwd + os.sep):
+ return True
+
+ # Allow access within the current session's CLI project directory
+ # (~/.claude/projects/<encoded-cwd>/).
+ encoded = _current_project_dir.get("")
+ if encoded:
+ session_project = os.path.join(_SDK_PROJECTS_DIR, encoded)
+ if resolved == session_project or resolved.startswith(session_project + os.sep):
+ return True
+
+ return False
+
# MCP server naming - the SDK prefixes tool names as "mcp__{server_name}__{tool}"
MCP_SERVER_NAME = "copilot"
@@ -33,6 +97,12 @@ _current_user_id: ContextVar[str | None] = ContextVar("current_user_id", default
_current_session: ContextVar[ChatSession | None] = ContextVar(
"current_session", default=None
)
+# E2B cloud sandbox for the current turn (None when E2B is not configured).
+# Passed to bash_exec so commands run on E2B instead of the local bwrap sandbox.
+_current_sandbox: ContextVar["AsyncSandbox | None"] = ContextVar(
+ "_current_sandbox", default=None
+)
+
# Stash for MCP tool outputs before the SDK potentially truncates them.
# Keyed by tool_name → full output string. Consumed (popped) by the
# response adapter when it builds StreamToolOutputAvailable.
@@ -53,22 +123,33 @@ _stash_event: ContextVar[asyncio.Event | None] = ContextVar(
def set_execution_context(
user_id: str | None,
session: ChatSession,
+ sandbox: "AsyncSandbox | None" = None,
+ sdk_cwd: str | None = None,
) -> None:
"""Set the execution context for tool calls.
This must be called before streaming begins to ensure tools have access
- to user_id and session information.
+ to user_id, session, and (optionally) an E2B sandbox for bash execution.
Args:
user_id: Current user's ID.
session: Current chat session.
+ sandbox: Optional E2B sandbox; when set, bash_exec routes commands there.
+ sdk_cwd: SDK working directory; used to scope tool-results reads.
"""
_current_user_id.set(user_id)
_current_session.set(session)
+ _current_sandbox.set(sandbox)
+ _current_project_dir.set(_encode_cwd_for_cli(sdk_cwd) if sdk_cwd else "")
_pending_tool_outputs.set({})
_stash_event.set(asyncio.Event())
+def get_current_sandbox() -> "AsyncSandbox | None":
+ """Return the E2B sandbox for the current turn, or None."""
+ return _current_sandbox.get()
+
+
def get_execution_context() -> tuple[str | None, ChatSession | None]:
"""Get the current execution context."""
return (
@@ -182,11 +263,6 @@ async def _execute_tool_sync(
result.output if isinstance(result.output, str) else json.dumps(result.output)
)
- # Stash the full output before the SDK potentially truncates it.
- pending = _pending_tool_outputs.get(None)
- if pending is not None:
- pending.setdefault(base_tool.name, []).append(text)
-
content_blocks: list[dict[str, str]] = [{"type": "text", "text": text}]
# If the tool result contains inline image data, add an MCP image block
@@ -284,29 +360,32 @@ def _build_input_schema(base_tool: BaseTool) -> dict[str, Any]:
async def _read_file_handler(args: dict[str, Any]) -> dict[str, Any]:
- """Read a file with optional offset/limit. Restricted to SDK working directory.
+ """Read a local file with optional offset/limit.
- After reading, the file is deleted to prevent accumulation in long-running pods.
+ Only allows paths that pass :func:`is_allowed_local_path` — the current
+ session's tool-results directory and ephemeral working directory.
"""
file_path = args.get("file_path", "")
offset = args.get("offset", 0)
limit = args.get("limit", 2000)
- # Security: only allow reads under ~/.claude/projects/**/tool-results/
- real_path = os.path.realpath(file_path)
- if not real_path.startswith(_SDK_PROJECTS_DIR) or "tool-results" not in real_path:
+ if not is_allowed_local_path(file_path):
return {
"content": [{"type": "text", "text": f"Access denied: {file_path}"}],
"isError": True,
}
+ resolved = os.path.realpath(os.path.expanduser(file_path))
try:
- with open(real_path) as f:
+ with open(resolved) as f:
selected = list(itertools.islice(f, offset, offset + limit))
content = "".join(selected)
# Cleanup happens in _cleanup_sdk_tool_results after session ends;
# don't delete here — the SDK may read in multiple chunks.
- return {"content": [{"type": "text", "text": content}], "isError": False}
+ return {
+ "content": [{"type": "text", "text": content}],
+ "isError": False,
+ }
except FileNotFoundError:
return {
"content": [{"type": "text", "text": f"File not found: {file_path}"}],
@@ -345,49 +424,82 @@ _READ_TOOL_SCHEMA = {
# Create the MCP server configuration
-def create_copilot_mcp_server():
+def _text_from_mcp_result(result: dict[str, Any]) -> str:
+ """Extract concatenated text from an MCP response's content blocks."""
+ content = result.get("content", [])
+ if isinstance(content, list):
+ parts = [
+ b.get("text", "")
+ for b in content
+ if isinstance(b, dict) and b.get("type") == "text"
+ ]
+ return "".join(parts)
+ return ""
+
+
+def create_copilot_mcp_server(*, use_e2b: bool = False):
"""Create an in-process MCP server configuration for CoPilot tools.
- This can be passed to ClaudeAgentOptions.mcp_servers.
-
- Note: The actual SDK MCP server creation depends on the claude-agent-sdk
- package being available. This function returns the configuration that
- can be used with the SDK.
+ When *use_e2b* is True, five additional MCP file tools are registered
+ that route directly to the E2B sandbox filesystem, and the caller should
+ disable the corresponding SDK built-in tools via
+ :func:`get_sdk_disallowed_tools`.
"""
- try:
- from claude_agent_sdk import create_sdk_mcp_server, tool
-
- # Create decorated tool functions
- sdk_tools = []
-
- for tool_name, base_tool in TOOL_REGISTRY.items():
- handler = create_tool_handler(base_tool)
- decorated = tool(
- tool_name,
- base_tool.description,
- _build_input_schema(base_tool),
- )(handler)
- sdk_tools.append(decorated)
- # Add the Read tool so the SDK can read back oversized tool results
- read_tool = tool(
- _READ_TOOL_NAME,
- _READ_TOOL_DESCRIPTION,
- _READ_TOOL_SCHEMA,
- )(_read_file_handler)
- sdk_tools.append(read_tool)
-
- server = create_sdk_mcp_server(
- name=MCP_SERVER_NAME,
- version="1.0.0",
- tools=sdk_tools,
- )
-
- return server
+ def _truncating(fn, tool_name: str):
+ """Wrap a tool handler so its response is truncated to stay under the
+ SDK's 10 MB JSON buffer, and stash the (truncated) output for the
+ response adapter before the SDK can apply its own head-truncation.
+
+ Applied once to every registered tool."""
+
+ async def wrapper(args: dict[str, Any]) -> dict[str, Any]:
+ result = await fn(args)
+ truncated = truncate(result, _MCP_MAX_CHARS)
+
+ # Stash the text so the response adapter can forward our
+ # middle-out truncated version to the frontend instead of the
+ # SDK's head-truncated version (for outputs >~100 KB the SDK
+ # persists to tool-results/ with a 2 KB head-only preview).
+ if not truncated.get("isError"):
+ text = _text_from_mcp_result(truncated)
+ if text:
+ stash_pending_tool_output(tool_name, text)
+
+ return truncated
+
+ return wrapper
+
+ sdk_tools = []
+
+ for tool_name, base_tool in TOOL_REGISTRY.items():
+ handler = create_tool_handler(base_tool)
+ decorated = tool(
+ tool_name,
+ base_tool.description,
+ _build_input_schema(base_tool),
+ )(_truncating(handler, tool_name))
+ sdk_tools.append(decorated)
+
+ # E2B file tools replace SDK built-in Read/Write/Edit/Glob/Grep.
+ if use_e2b:
+ for name, desc, schema, handler in E2B_FILE_TOOLS:
+ decorated = tool(name, desc, schema)(_truncating(handler, name))
+ sdk_tools.append(decorated)
- except ImportError:
- # Let ImportError propagate so service.py handles the fallback
- raise
+ # Read tool for SDK-truncated tool results (always needed).
+ read_tool = tool(
+ _READ_TOOL_NAME,
+ _READ_TOOL_DESCRIPTION,
+ _READ_TOOL_SCHEMA,
+ )(_truncating(_read_file_handler, _READ_TOOL_NAME))
+ sdk_tools.append(read_tool)
+
+ return create_sdk_mcp_server(
+ name=MCP_SERVER_NAME,
+ version="1.0.0",
+ tools=sdk_tools,
+ )
# SDK built-in tools allowed within the workspace directory.
@@ -397,16 +509,11 @@ def create_copilot_mcp_server():
# Task allows spawning sub-agents (rate-limited by security hooks).
# WebSearch uses Brave Search via Anthropic's API — safe, no SSRF risk.
# TodoWrite manages the task checklist shown in the UI — no security concern.
-_SDK_BUILTIN_TOOLS = [
- "Read",
- "Write",
- "Edit",
- "Glob",
- "Grep",
- "Task",
- "WebSearch",
- "TodoWrite",
-]
+# In E2B mode, all five are disabled — MCP equivalents provide direct sandbox
+# access. read_file also handles local tool-results and ephemeral reads.
+_SDK_BUILTIN_FILE_TOOLS = ["Read", "Write", "Edit", "Glob", "Grep"]
+_SDK_BUILTIN_ALWAYS = ["Task", "WebSearch", "TodoWrite"]
+_SDK_BUILTIN_TOOLS = [*_SDK_BUILTIN_FILE_TOOLS, *_SDK_BUILTIN_ALWAYS]
# SDK built-in tools that must be explicitly blocked.
# Bash: dangerous — agent uses mcp__copilot__bash_exec with kernel-level
@@ -453,11 +560,37 @@ DANGEROUS_PATTERNS = [
r"subprocess",
]
-# List of tool names for allowed_tools configuration
-# Include MCP tools, the MCP Read tool for oversized results,
-# and SDK built-in file tools for workspace operations.
+# Static tool name list for the non-E2B case (backward compatibility).
COPILOT_TOOL_NAMES = [
*[f"{MCP_TOOL_PREFIX}{name}" for name in TOOL_REGISTRY.keys()],
f"{MCP_TOOL_PREFIX}{_READ_TOOL_NAME}",
*_SDK_BUILTIN_TOOLS,
]
+
+
+def get_copilot_tool_names(*, use_e2b: bool = False) -> list[str]:
+ """Build the ``allowed_tools`` list for :class:`ClaudeAgentOptions`.
+
+ When *use_e2b* is True the SDK built-in file tools are replaced by MCP
+ equivalents that route to the E2B sandbox.
+ """
+ if not use_e2b:
+ return list(COPILOT_TOOL_NAMES)
+
+ return [
+ *[f"{MCP_TOOL_PREFIX}{name}" for name in TOOL_REGISTRY.keys()],
+ f"{MCP_TOOL_PREFIX}{_READ_TOOL_NAME}",
+ *[f"{MCP_TOOL_PREFIX}{name}" for name in E2B_FILE_TOOL_NAMES],
+ *_SDK_BUILTIN_ALWAYS,
+ ]
+
+
+def get_sdk_disallowed_tools(*, use_e2b: bool = False) -> list[str]:
+ """Build the ``disallowed_tools`` list for :class:`ClaudeAgentOptions`.
+
+ When *use_e2b* is True the SDK built-in file tools are also disabled
+ because MCP equivalents provide direct sandbox access.
+ """
+ if not use_e2b:
+ return list(SDK_DISALLOWED_TOOLS)
+ return [*SDK_DISALLOWED_TOOLS, *_SDK_BUILTIN_FILE_TOOLS]
diff --git a/autogpt_platform/backend/backend/copilot/sdk/tool_adapter_test.py b/autogpt_platform/backend/backend/copilot/sdk/tool_adapter_test.py
new file mode 100644
index 0000000000..f6e9590d44
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/sdk/tool_adapter_test.py
@@ -0,0 +1,145 @@
+"""Tests for tool_adapter helpers: _text_from_mcp_result, truncation stash."""
+
+import pytest
+
+from backend.util.truncate import truncate
+
+from .tool_adapter import (
+ _MCP_MAX_CHARS,
+ _text_from_mcp_result,
+ pop_pending_tool_output,
+ set_execution_context,
+ stash_pending_tool_output,
+)
+
+# ---------------------------------------------------------------------------
+# _text_from_mcp_result
+# ---------------------------------------------------------------------------
+
+
+class TestTextFromMcpResult:
+ def test_single_text_block(self):
+ result = {"content": [{"type": "text", "text": "hello"}]}
+ assert _text_from_mcp_result(result) == "hello"
+
+ def test_multiple_text_blocks_concatenated(self):
+ result = {
+ "content": [
+ {"type": "text", "text": "one"},
+ {"type": "text", "text": "two"},
+ ]
+ }
+ assert _text_from_mcp_result(result) == "onetwo"
+
+ def test_non_text_blocks_ignored(self):
+ result = {
+ "content": [
+ {"type": "image", "data": "..."},
+ {"type": "text", "text": "only this"},
+ ]
+ }
+ assert _text_from_mcp_result(result) == "only this"
+
+ def test_empty_content_list(self):
+ assert _text_from_mcp_result({"content": []}) == ""
+
+ def test_missing_content_key(self):
+ assert _text_from_mcp_result({}) == ""
+
+ def test_non_list_content(self):
+ assert _text_from_mcp_result({"content": "raw string"}) == ""
+
+ def test_missing_text_field(self):
+ result = {"content": [{"type": "text"}]}
+ assert _text_from_mcp_result(result) == ""
+
+
+# ---------------------------------------------------------------------------
+# stash / pop round-trip (the mechanism _truncating relies on)
+# ---------------------------------------------------------------------------
+
+
+class TestToolOutputStash:
+ @pytest.fixture(autouse=True)
+ def _init_context(self):
+ """Initialise the context vars that stash_pending_tool_output needs."""
+ set_execution_context(
+ user_id="test",
+ session=None, # type: ignore[arg-type]
+ sandbox=None,
+ sdk_cwd="/tmp/test",
+ )
+
+ def test_stash_and_pop(self):
+ stash_pending_tool_output("my_tool", "output1")
+ assert pop_pending_tool_output("my_tool") == "output1"
+
+ def test_pop_empty_returns_none(self):
+ assert pop_pending_tool_output("nonexistent") is None
+
+ def test_fifo_order(self):
+ stash_pending_tool_output("t", "first")
+ stash_pending_tool_output("t", "second")
+ assert pop_pending_tool_output("t") == "first"
+ assert pop_pending_tool_output("t") == "second"
+ assert pop_pending_tool_output("t") is None
+
+ def test_dict_serialised_to_json(self):
+ stash_pending_tool_output("t", {"key": "value"})
+ assert pop_pending_tool_output("t") == '{"key": "value"}'
+
+ def test_separate_tool_names(self):
+ stash_pending_tool_output("a", "alpha")
+ stash_pending_tool_output("b", "beta")
+ assert pop_pending_tool_output("b") == "beta"
+ assert pop_pending_tool_output("a") == "alpha"
+
+
+# ---------------------------------------------------------------------------
+# _truncating wrapper (integration via create_copilot_mcp_server)
+# ---------------------------------------------------------------------------
+
+
+class TestTruncationAndStashIntegration:
+ """Test truncation + stash behavior that _truncating relies on."""
+
+ @pytest.fixture(autouse=True)
+ def _init_context(self):
+ set_execution_context(
+ user_id="test",
+ session=None, # type: ignore[arg-type]
+ sandbox=None,
+ sdk_cwd="/tmp/test",
+ )
+
+ def test_small_output_stashed(self):
+ """Non-error output is stashed for the response adapter."""
+ result = {
+ "content": [{"type": "text", "text": "small output"}],
+ "isError": False,
+ }
+ truncated = truncate(result, _MCP_MAX_CHARS)
+ text = _text_from_mcp_result(truncated)
+ assert text == "small output"
+ stash_pending_tool_output("test_tool", text)
+ assert pop_pending_tool_output("test_tool") == "small output"
+
+ def test_error_result_not_stashed(self):
+ """Error results should not be stashed."""
+ result = {
+ "content": [{"type": "text", "text": "error msg"}],
+ "isError": True,
+ }
+ # _truncating only stashes when not result.get("isError")
+ if not result.get("isError"):
+ stash_pending_tool_output("err_tool", "should not happen")
+ assert pop_pending_tool_output("err_tool") is None
+
+ def test_large_output_truncated(self):
+ """Output exceeding _MCP_MAX_CHARS is truncated before stashing."""
+ big_text = "x" * (_MCP_MAX_CHARS + 100_000)
+ result = {"content": [{"type": "text", "text": big_text}]}
+ truncated = truncate(result, _MCP_MAX_CHARS)
+ text = _text_from_mcp_result(truncated)
+ assert len(text) < len(big_text)
+ assert len(str(truncated)) <= _MCP_MAX_CHARS
diff --git a/autogpt_platform/backend/backend/copilot/tools/bash_exec.py b/autogpt_platform/backend/backend/copilot/tools/bash_exec.py
index 6e32a3c720..dd861ea259 100644
--- a/autogpt_platform/backend/backend/copilot/tools/bash_exec.py
+++ b/autogpt_platform/backend/backend/copilot/tools/bash_exec.py
@@ -1,19 +1,30 @@
-"""Bash execution tool — run shell commands in a bubblewrap sandbox.
+"""Bash execution tool — run shell commands on E2B or in a bubblewrap sandbox.
-Full Bash scripting is allowed (loops, conditionals, pipes, functions, etc.).
-Safety comes from OS-level isolation (bubblewrap): only system dirs visible
-read-only, writable workspace only, clean env, no network.
+When an E2B sandbox is available in the current execution context the command
+runs directly on the remote E2B cloud environment. This means:
-Requires bubblewrap (``bwrap``) — the tool is disabled when bwrap is not
-available (e.g. macOS development).
+- **Persistent filesystem**: files survive across turns via HTTP-based sync
+ with the sandbox's ``/home/user`` directory (E2B files API), shared with
+ SDK Read/Write/Edit tools.
+- **Full internet access**: E2B sandboxes have unrestricted outbound network.
+- **Execution isolation**: E2B provides a fresh, containerised Linux environment.
+
+When E2B is *not* configured the tool falls back to **bubblewrap** (bwrap):
+OS-level isolation with a whitelist-only filesystem, no network, and resource
+limits. Requires bubblewrap to be installed (Linux only).
"""
import logging
+import shlex
from typing import Any
+from e2b import AsyncSandbox
+from e2b.exceptions import TimeoutException
+
from backend.copilot.model import ChatSession
from .base import BaseTool
+from .e2b_sandbox import E2B_WORKDIR
from .models import BashExecResponse, ErrorResponse, ToolResponseBase
from .sandbox import get_workspace_dir, has_full_sandbox, run_sandboxed
@@ -21,7 +32,7 @@ logger = logging.getLogger(__name__)
class BashExecTool(BaseTool):
- """Execute Bash commands in a bubblewrap sandbox."""
+ """Execute Bash commands on E2B or in a bubblewrap sandbox."""
@property
def name(self) -> str:
@@ -29,28 +40,16 @@ class BashExecTool(BaseTool):
@property
def description(self) -> str:
- if not has_full_sandbox():
- return (
- "Bash execution is DISABLED — bubblewrap sandbox is not "
- "available on this platform. Do not call this tool."
- )
return (
- "Execute a Bash command or script in a bubblewrap sandbox. "
+ "Execute a Bash command or script. "
"Full Bash scripting is supported (loops, conditionals, pipes, "
"functions, etc.). "
- "The sandbox shares the same working directory as the SDK Read/Write "
- "tools — files created by either are accessible to both. "
- "SECURITY: Only system directories (/usr, /bin, /lib, /etc) are "
- "visible read-only, the per-session workspace is the only writable "
- "path, environment variables are wiped (no secrets), all network "
- "access is blocked at the kernel level, and resource limits are "
- "enforced (max 64 processes, 512MB memory, 50MB file size). "
- "Application code, configs, and other directories are NOT accessible. "
- "To fetch web content, use the web_fetch tool instead. "
+ "The working directory is shared with the SDK Read/Write/Edit/Glob/Grep "
+ "tools — files created by either are immediately visible to both. "
"Execution is killed after the timeout (default 30s, max 120s). "
"Returns stdout and stderr. "
- "Useful for file manipulation, data processing with Unix tools "
- "(grep, awk, sed, jq, etc.), and running shell scripts."
+ "Useful for file manipulation, data processing, running scripts, "
+ "and installing packages."
)
@property
@@ -85,15 +84,8 @@ class BashExecTool(BaseTool):
) -> ToolResponseBase:
session_id = session.session_id if session else None
- if not has_full_sandbox():
- return ErrorResponse(
- message="bash_exec requires bubblewrap sandbox (Linux only).",
- error="sandbox_unavailable",
- session_id=session_id,
- )
-
command: str = (kwargs.get("command") or "").strip()
- timeout: int = kwargs.get("timeout", 30)
+ timeout: int = int(kwargs.get("timeout", 30))
if not command:
return ErrorResponse(
@@ -102,6 +94,21 @@ class BashExecTool(BaseTool):
session_id=session_id,
)
+ # E2B path: run on remote cloud sandbox when available.
+ from backend.copilot.sdk.tool_adapter import get_current_sandbox
+
+ sandbox = get_current_sandbox()
+ if sandbox is not None:
+ return await self._execute_on_e2b(sandbox, command, timeout, session_id)
+
+ # Bubblewrap fallback: local isolated execution.
+ if not has_full_sandbox():
+ return ErrorResponse(
+ message="bash_exec requires bubblewrap sandbox (Linux only).",
+ error="sandbox_unavailable",
+ session_id=session_id,
+ )
+
workspace = get_workspace_dir(session_id or "default")
stdout, stderr, exit_code, timed_out = await run_sandboxed(
@@ -122,3 +129,43 @@ class BashExecTool(BaseTool):
timed_out=timed_out,
session_id=session_id,
)
+
+ async def _execute_on_e2b(
+ self,
+ sandbox: AsyncSandbox,
+ command: str,
+ timeout: int,
+ session_id: str | None,
+ ) -> ToolResponseBase:
+ """Execute *command* on the E2B sandbox via commands.run()."""
+ try:
+ result = await sandbox.commands.run(
+ f"bash -c {shlex.quote(command)}",
+ cwd=E2B_WORKDIR,
+ timeout=timeout,
+ envs={"PATH": "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"},
+ )
+ return BashExecResponse(
+ message=f"Command executed on E2B (exit {result.exit_code})",
+ stdout=result.stdout or "",
+ stderr=result.stderr or "",
+ exit_code=result.exit_code,
+ timed_out=False,
+ session_id=session_id,
+ )
+ except Exception as exc:
+ if isinstance(exc, TimeoutException):
+ return BashExecResponse(
+ message="Execution timed out",
+ stdout="",
+ stderr=f"Timed out after {timeout}s",
+ exit_code=-1,
+ timed_out=True,
+ session_id=session_id,
+ )
+ logger.error("[E2B] bash_exec failed: %s", exc, exc_info=True)
+ return ErrorResponse(
+ message=f"E2B execution failed: {exc}",
+ error="e2b_execution_error",
+ session_id=session_id,
+ )
diff --git a/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox.py b/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox.py
new file mode 100644
index 0000000000..b57b962b84
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox.py
@@ -0,0 +1,170 @@
+"""E2B sandbox lifecycle for CoPilot: persistent cloud execution.
+
+Each session gets a long-lived E2B cloud sandbox. ``bash_exec`` runs commands
+directly on the sandbox via ``sandbox.commands.run()``. SDK file tools
+(read_file/write_file/edit_file/glob/grep) route to the sandbox's
+``/home/user`` directory via E2B's HTTP-based filesystem API — all tools
+share a single coherent filesystem with no local sync required.
+
+Lifecycle
+---------
+1. **Turn start** – connect to the existing sandbox (sandbox_id in Redis) or
+ create a new one via ``get_or_create_sandbox()``.
+2. **Execution** – ``bash_exec`` and MCP file tools operate directly on the
+ sandbox's ``/home/user`` filesystem.
+3. **Session expiry** – E2B sandbox is killed by its own timeout (session_ttl).
+"""
+
+import asyncio
+import logging
+
+from e2b import AsyncSandbox
+
+from backend.data.redis_client import get_redis_async
+
+logger = logging.getLogger(__name__)
+
+_SANDBOX_REDIS_PREFIX = "copilot:e2b:sandbox:"
+E2B_WORKDIR = "/home/user"
+_CREATING = "__creating__"
+_CREATION_LOCK_TTL = 60
+_MAX_WAIT_ATTEMPTS = 20 # 20 * 0.5s = 10s max wait
+
+
+async def _try_reconnect(
+ sandbox_id: str, api_key: str, redis_key: str, timeout: int
+) -> "AsyncSandbox | None":
+ """Try to reconnect to an existing sandbox. Returns None on failure."""
+ try:
+ sandbox = await AsyncSandbox.connect(sandbox_id, api_key=api_key)
+ if await sandbox.is_running():
+ redis = await get_redis_async()
+ await redis.expire(redis_key, timeout)
+ return sandbox
+ except Exception as exc:
+ logger.warning("[E2B] Reconnect to %.12s failed: %s", sandbox_id, exc)
+
+ # Stale — clear Redis so a new sandbox can be created.
+ redis = await get_redis_async()
+ await redis.delete(redis_key)
+ return None
+
+
+async def get_or_create_sandbox(
+ session_id: str,
+ api_key: str,
+ template: str = "base",
+ timeout: int = 43200,
+) -> AsyncSandbox:
+ """Return the existing E2B sandbox for *session_id* or create a new one.
+
+ The sandbox_id is persisted in Redis so the same sandbox is reused
+ across turns. Concurrent calls for the same session are serialised
+ via a Redis ``SET NX`` creation lock.
+ """
+ redis = await get_redis_async()
+ redis_key = f"{_SANDBOX_REDIS_PREFIX}{session_id}"
+
+ # 1. Try reconnecting to an existing sandbox.
+ raw = await redis.get(redis_key)
+ if raw:
+ sandbox_id = raw if isinstance(raw, str) else raw.decode()
+ if sandbox_id != _CREATING:
+ sandbox = await _try_reconnect(sandbox_id, api_key, redis_key, timeout)
+ if sandbox:
+ logger.info(
+ "[E2B] Reconnected to %.12s for session %.12s",
+ sandbox_id,
+ session_id,
+ )
+ return sandbox
+
+ # 2. Claim creation lock. If another request holds it, wait for the result.
+ claimed = await redis.set(redis_key, _CREATING, nx=True, ex=_CREATION_LOCK_TTL)
+ if not claimed:
+ for _ in range(_MAX_WAIT_ATTEMPTS):
+ await asyncio.sleep(0.5)
+ raw = await redis.get(redis_key)
+ if not raw:
+ break # Lock expired — fall through to retry creation
+ sandbox_id = raw if isinstance(raw, str) else raw.decode()
+ if sandbox_id != _CREATING:
+ sandbox = await _try_reconnect(sandbox_id, api_key, redis_key, timeout)
+ if sandbox:
+ return sandbox
+ break # Stale sandbox cleared — fall through to create
+
+ # Try to claim creation lock again after waiting.
+ claimed = await redis.set(redis_key, _CREATING, nx=True, ex=_CREATION_LOCK_TTL)
+ if not claimed:
+ # Another process may have created a sandbox — try to use it.
+ raw = await redis.get(redis_key)
+ if raw:
+ sandbox_id = raw if isinstance(raw, str) else raw.decode()
+ if sandbox_id != _CREATING:
+ sandbox = await _try_reconnect(
+ sandbox_id, api_key, redis_key, timeout
+ )
+ if sandbox:
+ return sandbox
+ raise RuntimeError(
+ f"Could not acquire E2B creation lock for session {session_id[:12]}"
+ )
+
+ # 3. Create a new sandbox.
+ try:
+ sandbox = await AsyncSandbox.create(
+ template=template, api_key=api_key, timeout=timeout
+ )
+ except Exception:
+ await redis.delete(redis_key)
+ raise
+
+ await redis.setex(redis_key, timeout, sandbox.sandbox_id)
+ logger.info(
+ "[E2B] Created sandbox %.12s for session %.12s",
+ sandbox.sandbox_id,
+ session_id,
+ )
+ return sandbox
+
+
+async def kill_sandbox(session_id: str, api_key: str) -> bool:
+ """Kill the E2B sandbox for *session_id* and clean up its Redis entry.
+
+ Returns ``True`` if a sandbox was found and killed, ``False`` otherwise.
+ Safe to call even when no sandbox exists for the session.
+ """
+ redis = await get_redis_async()
+ redis_key = f"{_SANDBOX_REDIS_PREFIX}{session_id}"
+ raw = await redis.get(redis_key)
+ if not raw:
+ return False
+
+ sandbox_id = raw if isinstance(raw, str) else raw.decode()
+ await redis.delete(redis_key)
+
+ if sandbox_id == _CREATING:
+ return False
+
+ try:
+
+ async def _connect_and_kill():
+ sandbox = await AsyncSandbox.connect(sandbox_id, api_key=api_key)
+ await sandbox.kill()
+
+ await asyncio.wait_for(_connect_and_kill(), timeout=10)
+ logger.info(
+ "[E2B] Killed sandbox %.12s for session %.12s",
+ sandbox_id,
+ session_id,
+ )
+ return True
+ except Exception as exc:
+ logger.warning(
+ "[E2B] Failed to kill sandbox %.12s for session %.12s: %s",
+ sandbox_id,
+ session_id,
+ exc,
+ )
+ return False
diff --git a/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox_test.py b/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox_test.py
new file mode 100644
index 0000000000..31af1fe385
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox_test.py
@@ -0,0 +1,272 @@
+"""Tests for e2b_sandbox: get_or_create_sandbox, _try_reconnect, kill_sandbox.
+
+Uses mock Redis and mock AsyncSandbox — no external dependencies.
+Tests are synchronous (using asyncio.run) to avoid conflicts with the
+session-scoped event loop in conftest.py.
+"""
+
+import asyncio
+from unittest.mock import AsyncMock, MagicMock, patch
+
+import pytest
+
+from .e2b_sandbox import (
+ _CREATING,
+ _SANDBOX_REDIS_PREFIX,
+ _try_reconnect,
+ get_or_create_sandbox,
+ kill_sandbox,
+)
+
+_KEY = f"{_SANDBOX_REDIS_PREFIX}sess-123"
+_API_KEY = "test-api-key"
+_TIMEOUT = 300
+
+
+def _mock_sandbox(sandbox_id: str = "sb-abc", running: bool = True) -> MagicMock:
+ sb = MagicMock()
+ sb.sandbox_id = sandbox_id
+ sb.is_running = AsyncMock(return_value=running)
+ return sb
+
+
+def _mock_redis(get_val: str | bytes | None = None, set_nx_result: bool = True):
+ r = AsyncMock()
+ r.get = AsyncMock(return_value=get_val)
+ r.set = AsyncMock(return_value=set_nx_result)
+ r.setex = AsyncMock()
+ r.delete = AsyncMock()
+ r.expire = AsyncMock()
+ return r
+
+
+def _patch_redis(redis):
+ return patch(
+ "backend.copilot.tools.e2b_sandbox.get_redis_async",
+ new_callable=AsyncMock,
+ return_value=redis,
+ )
+
+
+# ---------------------------------------------------------------------------
+# _try_reconnect
+# ---------------------------------------------------------------------------
+
+
+class TestTryReconnect:
+ def test_reconnect_success(self):
+ sb = _mock_sandbox()
+ redis = _mock_redis()
+ with (
+ patch("backend.copilot.tools.e2b_sandbox.AsyncSandbox") as mock_cls,
+ _patch_redis(redis),
+ ):
+ mock_cls.connect = AsyncMock(return_value=sb)
+ result = asyncio.run(_try_reconnect("sb-abc", _API_KEY, _KEY, _TIMEOUT))
+
+ assert result is sb
+ redis.expire.assert_awaited_once_with(_KEY, _TIMEOUT)
+ redis.delete.assert_not_awaited()
+
+ def test_reconnect_not_running_clears_key(self):
+ sb = _mock_sandbox(running=False)
+ redis = _mock_redis()
+ with (
+ patch("backend.copilot.tools.e2b_sandbox.AsyncSandbox") as mock_cls,
+ _patch_redis(redis),
+ ):
+ mock_cls.connect = AsyncMock(return_value=sb)
+ result = asyncio.run(_try_reconnect("sb-abc", _API_KEY, _KEY, _TIMEOUT))
+
+ assert result is None
+ redis.delete.assert_awaited_once_with(_KEY)
+
+ def test_reconnect_exception_clears_key(self):
+ redis = _mock_redis()
+ with (
+ patch("backend.copilot.tools.e2b_sandbox.AsyncSandbox") as mock_cls,
+ _patch_redis(redis),
+ ):
+ mock_cls.connect = AsyncMock(side_effect=ConnectionError("gone"))
+ result = asyncio.run(_try_reconnect("sb-abc", _API_KEY, _KEY, _TIMEOUT))
+
+ assert result is None
+ redis.delete.assert_awaited_once_with(_KEY)
+
+
+# ---------------------------------------------------------------------------
+# get_or_create_sandbox
+# ---------------------------------------------------------------------------
+
+
+class TestGetOrCreateSandbox:
+ def test_reconnect_existing(self):
+ """When Redis has a valid sandbox_id, reconnect to it."""
+ sb = _mock_sandbox()
+ redis = _mock_redis(get_val="sb-abc")
+ with (
+ patch("backend.copilot.tools.e2b_sandbox.AsyncSandbox") as mock_cls,
+ _patch_redis(redis),
+ ):
+ mock_cls.connect = AsyncMock(return_value=sb)
+ result = asyncio.run(
+ get_or_create_sandbox("sess-123", _API_KEY, timeout=_TIMEOUT)
+ )
+
+ assert result is sb
+ mock_cls.create.assert_not_called()
+
+ def test_create_new_when_no_key(self):
+ """When Redis is empty, claim lock and create a new sandbox."""
+ sb = _mock_sandbox("sb-new")
+ redis = _mock_redis(get_val=None, set_nx_result=True)
+ with (
+ patch("backend.copilot.tools.e2b_sandbox.AsyncSandbox") as mock_cls,
+ _patch_redis(redis),
+ ):
+ mock_cls.create = AsyncMock(return_value=sb)
+ result = asyncio.run(
+ get_or_create_sandbox("sess-123", _API_KEY, timeout=_TIMEOUT)
+ )
+
+ assert result is sb
+ redis.setex.assert_awaited_once_with(_KEY, _TIMEOUT, "sb-new")
+
+ def test_create_failure_clears_lock(self):
+ """If sandbox creation fails, the Redis lock is deleted."""
+ redis = _mock_redis(get_val=None, set_nx_result=True)
+ with (
+ patch("backend.copilot.tools.e2b_sandbox.AsyncSandbox") as mock_cls,
+ _patch_redis(redis),
+ ):
+ mock_cls.create = AsyncMock(side_effect=RuntimeError("quota"))
+ with pytest.raises(RuntimeError, match="quota"):
+ asyncio.run(
+ get_or_create_sandbox("sess-123", _API_KEY, timeout=_TIMEOUT)
+ )
+
+ redis.delete.assert_awaited_once_with(_KEY)
+
+ def test_wait_for_lock_then_reconnect(self):
+ """When another process holds the lock, wait and reconnect."""
+ sb = _mock_sandbox("sb-other")
+ redis = _mock_redis()
+ redis.get = AsyncMock(side_effect=[_CREATING, "sb-other"])
+ redis.set = AsyncMock(return_value=False)
+ with (
+ patch("backend.copilot.tools.e2b_sandbox.AsyncSandbox") as mock_cls,
+ _patch_redis(redis),
+ patch(
+ "backend.copilot.tools.e2b_sandbox.asyncio.sleep",
+ new_callable=AsyncMock,
+ ),
+ ):
+ mock_cls.connect = AsyncMock(return_value=sb)
+ result = asyncio.run(
+ get_or_create_sandbox("sess-123", _API_KEY, timeout=_TIMEOUT)
+ )
+
+ assert result is sb
+
+ def test_stale_reconnect_clears_and_creates(self):
+ """When stored sandbox is stale, clear key and create a new one."""
+ stale_sb = _mock_sandbox("sb-stale", running=False)
+ new_sb = _mock_sandbox("sb-fresh")
+ redis = _mock_redis(get_val="sb-stale", set_nx_result=True)
+ with (
+ patch("backend.copilot.tools.e2b_sandbox.AsyncSandbox") as mock_cls,
+ _patch_redis(redis),
+ ):
+ mock_cls.connect = AsyncMock(return_value=stale_sb)
+ mock_cls.create = AsyncMock(return_value=new_sb)
+ result = asyncio.run(
+ get_or_create_sandbox("sess-123", _API_KEY, timeout=_TIMEOUT)
+ )
+
+ assert result is new_sb
+ redis.delete.assert_awaited()
+
+
+# ---------------------------------------------------------------------------
+# kill_sandbox
+# ---------------------------------------------------------------------------
+
+
+class TestKillSandbox:
+ def test_kill_existing_sandbox(self):
+ """Kill a running sandbox and clean up Redis."""
+ sb = _mock_sandbox()
+ sb.kill = AsyncMock()
+ redis = _mock_redis(get_val="sb-abc")
+ with (
+ patch("backend.copilot.tools.e2b_sandbox.AsyncSandbox") as mock_cls,
+ _patch_redis(redis),
+ ):
+ mock_cls.connect = AsyncMock(return_value=sb)
+ result = asyncio.run(kill_sandbox("sess-123", _API_KEY))
+
+ assert result is True
+ redis.delete.assert_awaited_once_with(_KEY)
+ sb.kill.assert_awaited_once()
+
+ def test_kill_no_sandbox(self):
+ """No-op when no sandbox exists in Redis."""
+ redis = _mock_redis(get_val=None)
+ with _patch_redis(redis):
+ result = asyncio.run(kill_sandbox("sess-123", _API_KEY))
+
+ assert result is False
+ redis.delete.assert_not_awaited()
+
+ def test_kill_creating_state(self):
+ """Clears Redis key but returns False when sandbox is still being created."""
+ redis = _mock_redis(get_val=_CREATING)
+ with _patch_redis(redis):
+ result = asyncio.run(kill_sandbox("sess-123", _API_KEY))
+
+ assert result is False
+ redis.delete.assert_awaited_once_with(_KEY)
+
+ def test_kill_connect_failure(self):
+ """Returns False and cleans Redis if connect/kill fails."""
+ redis = _mock_redis(get_val="sb-abc")
+ with (
+ patch("backend.copilot.tools.e2b_sandbox.AsyncSandbox") as mock_cls,
+ _patch_redis(redis),
+ ):
+ mock_cls.connect = AsyncMock(side_effect=ConnectionError("gone"))
+ result = asyncio.run(kill_sandbox("sess-123", _API_KEY))
+
+ assert result is False
+ redis.delete.assert_awaited_once_with(_KEY)
+
+ def test_kill_with_bytes_redis_value(self):
+ """Redis may return bytes — kill_sandbox should decode correctly."""
+ sb = _mock_sandbox()
+ sb.kill = AsyncMock()
+ redis = _mock_redis(get_val=b"sb-abc")
+ with (
+ patch("backend.copilot.tools.e2b_sandbox.AsyncSandbox") as mock_cls,
+ _patch_redis(redis),
+ ):
+ mock_cls.connect = AsyncMock(return_value=sb)
+ result = asyncio.run(kill_sandbox("sess-123", _API_KEY))
+
+ assert result is True
+ sb.kill.assert_awaited_once()
+
+ def test_kill_timeout_returns_false(self):
+ """Returns False when E2B API calls exceed the 10s timeout."""
+ redis = _mock_redis(get_val="sb-abc")
+ with (
+ _patch_redis(redis),
+ patch(
+ "backend.copilot.tools.e2b_sandbox.asyncio.wait_for",
+ new_callable=AsyncMock,
+ side_effect=asyncio.TimeoutError,
+ ),
+ ):
+ result = asyncio.run(kill_sandbox("sess-123", _API_KEY))
+
+ assert result is False
+ redis.delete.assert_awaited_once_with(_KEY)
diff --git a/autogpt_platform/backend/backend/copilot/tools/workspace_files.py b/autogpt_platform/backend/backend/copilot/tools/workspace_files.py
index b1d54f6653..0032db63b1 100644
--- a/autogpt_platform/backend/backend/copilot/tools/workspace_files.py
+++ b/autogpt_platform/backend/backend/copilot/tools/workspace_files.py
@@ -8,6 +8,7 @@ from typing import Any, Optional
from pydantic import BaseModel
from backend.copilot.model import ChatSession
+from backend.copilot.tools.e2b_sandbox import E2B_WORKDIR
from backend.copilot.tools.sandbox import make_session_path
from backend.data.db_accessors import workspace_db
from backend.util.settings import Config
@@ -20,7 +21,7 @@ from .models import ErrorResponse, ResponseType, ToolResponseBase
logger = logging.getLogger(__name__)
-def _resolve_write_content(
+async def _resolve_write_content(
content_text: str | None,
content_b64: str | None,
source_path: str | None,
@@ -30,6 +31,9 @@ def _resolve_write_content(
Returns the raw bytes on success, or an ``ErrorResponse`` on validation
failure (wrong number of sources, invalid path, file not found, etc.).
+
+ When an E2B sandbox is active, ``source_path`` reads from the sandbox
+ filesystem instead of the local ephemeral directory.
"""
# Normalise empty strings to None so counting and dispatch stay in sync.
if content_text is not None and content_text == "":
@@ -54,24 +58,7 @@ def _resolve_write_content(
)
if source_path is not None:
- validated = _validate_ephemeral_path(
- source_path, param_name="source_path", session_id=session_id
- )
- if isinstance(validated, ErrorResponse):
- return validated
- try:
- with open(validated, "rb") as f:
- return f.read()
- except FileNotFoundError:
- return ErrorResponse(
- message=f"Source file not found: {source_path}",
- session_id=session_id,
- )
- except Exception as e:
- return ErrorResponse(
- message=f"Failed to read source file: {e}",
- session_id=session_id,
- )
+ return await _read_source_path(source_path, session_id)
if content_b64 is not None:
try:
@@ -91,6 +78,106 @@ def _resolve_write_content(
return content_text.encode("utf-8")
+def _resolve_sandbox_path(
+ path: str, session_id: str | None, param_name: str
+) -> str | ErrorResponse:
+ """Normalize *path* to an absolute sandbox path under :data:`E2B_WORKDIR`.
+
+ Delegates to :func:`~backend.copilot.sdk.e2b_file_tools._resolve_remote`
+ and wraps any ``ValueError`` into an :class:`ErrorResponse`.
+ """
+ from backend.copilot.sdk.e2b_file_tools import _resolve_remote
+
+ try:
+ return _resolve_remote(path)
+ except ValueError:
+ return ErrorResponse(
+ message=f"{param_name} must be within {E2B_WORKDIR}",
+ session_id=session_id,
+ )
+
+
+async def _read_source_path(source_path: str, session_id: str) -> bytes | ErrorResponse:
+ """Read *source_path* from E2B sandbox or local ephemeral directory."""
+ from backend.copilot.sdk.tool_adapter import get_current_sandbox
+
+ sandbox = get_current_sandbox()
+ if sandbox is not None:
+ remote = _resolve_sandbox_path(source_path, session_id, "source_path")
+ if isinstance(remote, ErrorResponse):
+ return remote
+ try:
+ data = await sandbox.files.read(remote, format="bytes")
+ return bytes(data)
+ except Exception as exc:
+ return ErrorResponse(
+ message=f"Source file not found on sandbox: {source_path} ({exc})",
+ session_id=session_id,
+ )
+
+ # Local fallback: validate path stays within ephemeral directory.
+ validated = _validate_ephemeral_path(
+ source_path, param_name="source_path", session_id=session_id
+ )
+ if isinstance(validated, ErrorResponse):
+ return validated
+ try:
+ with open(validated, "rb") as f:
+ return f.read()
+ except FileNotFoundError:
+ return ErrorResponse(
+ message=f"Source file not found: {source_path}",
+ session_id=session_id,
+ )
+ except Exception as e:
+ return ErrorResponse(
+ message=f"Failed to read source file: {e}",
+ session_id=session_id,
+ )
+
+
+async def _save_to_path(
+ path: str, content: bytes, session_id: str
+) -> str | ErrorResponse:
+ """Write *content* to *path* on E2B sandbox or local ephemeral directory.
+
+ Returns the resolved path on success, or an ``ErrorResponse`` on failure.
+ """
+ from backend.copilot.sdk.tool_adapter import get_current_sandbox
+
+ sandbox = get_current_sandbox()
+ if sandbox is not None:
+ remote = _resolve_sandbox_path(path, session_id, "save_to_path")
+ if isinstance(remote, ErrorResponse):
+ return remote
+ try:
+ await sandbox.files.write(remote, content)
+ except Exception as exc:
+ return ErrorResponse(
+ message=f"Failed to write to sandbox: {path} ({exc})",
+ session_id=session_id,
+ )
+ return remote
+
+ validated = _validate_ephemeral_path(
+ path, param_name="save_to_path", session_id=session_id
+ )
+ if isinstance(validated, ErrorResponse):
+ return validated
+ try:
+ dir_path = os.path.dirname(validated)
+ if dir_path:
+ os.makedirs(dir_path, exist_ok=True)
+ with open(validated, "wb") as f:
+ f.write(content)
+ except Exception as exc:
+ return ErrorResponse(
+ message=f"Failed to write to local path: {path} ({exc})",
+ session_id=session_id,
+ )
+ return validated
+
+
def _validate_ephemeral_path(
path: str, *, param_name: str, session_id: str
) -> ErrorResponse | str:
@@ -429,15 +516,6 @@ class ReadWorkspaceFileTool(BaseTool):
message="Please provide either file_id or path", session_id=session_id
)
- # Validate and resolve save_to_path (use sanitized real path).
- if save_to_path:
- validated_save = _validate_ephemeral_path(
- save_to_path, param_name="save_to_path", session_id=session_id
- )
- if isinstance(validated_save, ErrorResponse):
- return validated_save
- save_to_path = validated_save
-
try:
manager = await _get_manager(user_id, session_id)
resolved = await _resolve_file(manager, file_id, path, session_id)
@@ -449,11 +527,10 @@ class ReadWorkspaceFileTool(BaseTool):
cached_content: bytes | None = None
if save_to_path:
cached_content = await manager.read_file_by_id(target_file_id)
- dir_path = os.path.dirname(save_to_path)
- if dir_path:
- os.makedirs(dir_path, exist_ok=True)
- with open(save_to_path, "wb") as f:
- f.write(cached_content)
+ result = await _save_to_path(save_to_path, cached_content, session_id)
+ if isinstance(result, ErrorResponse):
+ return result
+ save_to_path = result
is_small = file_info.size_bytes <= self.MAX_INLINE_SIZE_BYTES
is_text = _is_text_mime(file_info.mime_type)
@@ -629,7 +706,7 @@ class WriteWorkspaceFileTool(BaseTool):
content_text: str | None = kwargs.get("content")
content_b64: str | None = kwargs.get("content_base64")
- resolved = _resolve_write_content(
+ resolved = await _resolve_write_content(
content_text,
content_b64,
source_path_arg,
diff --git a/autogpt_platform/backend/backend/copilot/tools/workspace_files_test.py b/autogpt_platform/backend/backend/copilot/tools/workspace_files_test.py
index a34a44d6e9..0370ad8d98 100644
--- a/autogpt_platform/backend/backend/copilot/tools/workspace_files_test.py
+++ b/autogpt_platform/backend/backend/copilot/tools/workspace_files_test.py
@@ -102,67 +102,68 @@ class TestValidateEphemeralPath:
# ---------------------------------------------------------------------------
[email protected](loop_scope="session")
class TestResolveWriteContent:
- def test_no_sources_returns_error(self):
+ async def test_no_sources_returns_error(self):
from backend.copilot.tools.models import ErrorResponse
- result = _resolve_write_content(None, None, None, "s1")
+ result = await _resolve_write_content(None, None, None, "s1")
assert isinstance(result, ErrorResponse)
- def test_multiple_sources_returns_error(self):
+ async def test_multiple_sources_returns_error(self):
from backend.copilot.tools.models import ErrorResponse
- result = _resolve_write_content("text", "b64data", None, "s1")
+ result = await _resolve_write_content("text", "b64data", None, "s1")
assert isinstance(result, ErrorResponse)
- def test_plain_text_content(self):
- result = _resolve_write_content("hello world", None, None, "s1")
+ async def test_plain_text_content(self):
+ result = await _resolve_write_content("hello world", None, None, "s1")
assert result == b"hello world"
- def test_base64_content(self):
+ async def test_base64_content(self):
raw = b"binary data"
b64 = base64.b64encode(raw).decode()
- result = _resolve_write_content(None, b64, None, "s1")
+ result = await _resolve_write_content(None, b64, None, "s1")
assert result == raw
- def test_invalid_base64_returns_error(self):
+ async def test_invalid_base64_returns_error(self):
from backend.copilot.tools.models import ErrorResponse
- result = _resolve_write_content(None, "not-valid-b64!!!", None, "s1")
+ result = await _resolve_write_content(None, "not-valid-b64!!!", None, "s1")
assert isinstance(result, ErrorResponse)
assert "base64" in result.message.lower()
- def test_source_path(self, ephemeral_dir):
+ async def test_source_path(self, ephemeral_dir):
target = ephemeral_dir / "input.txt"
target.write_bytes(b"file content")
- result = _resolve_write_content(None, None, str(target), "s1")
+ result = await _resolve_write_content(None, None, str(target), "s1")
assert result == b"file content"
- def test_source_path_not_found(self, ephemeral_dir):
+ async def test_source_path_not_found(self, ephemeral_dir):
from backend.copilot.tools.models import ErrorResponse
missing = str(ephemeral_dir / "nope.txt")
- result = _resolve_write_content(None, None, missing, "s1")
+ result = await _resolve_write_content(None, None, missing, "s1")
assert isinstance(result, ErrorResponse)
- def test_source_path_outside_ephemeral(self, ephemeral_dir, tmp_path):
+ async def test_source_path_outside_ephemeral(self, ephemeral_dir, tmp_path):
from backend.copilot.tools.models import ErrorResponse
outside = tmp_path / "outside.txt"
outside.write_text("nope")
- result = _resolve_write_content(None, None, str(outside), "s1")
+ result = await _resolve_write_content(None, None, str(outside), "s1")
assert isinstance(result, ErrorResponse)
- def test_empty_string_sources_treated_as_none(self):
+ async def test_empty_string_sources_treated_as_none(self):
from backend.copilot.tools.models import ErrorResponse
# All empty strings → same as no sources
- result = _resolve_write_content("", "", "", "s1")
+ result = await _resolve_write_content("", "", "", "s1")
assert isinstance(result, ErrorResponse)
- def test_empty_string_source_path_with_text(self):
+ async def test_empty_string_source_path_with_text(self):
# source_path="" should be normalised to None, so only content counts
- result = _resolve_write_content("hello", "", "", "s1")
+ result = await _resolve_write_content("hello", "", "", "s1")
assert result == b"hello"
diff --git a/autogpt_platform/backend/backend/util/json.py b/autogpt_platform/backend/backend/util/json.py
index 9cd253580e..43445202a6 100644
--- a/autogpt_platform/backend/backend/util/json.py
+++ b/autogpt_platform/backend/backend/util/json.py
@@ -105,8 +105,13 @@ def validate_with_jsonschema(
return str(e)
-def _sanitize_string(value: str) -> str:
- """Remove PostgreSQL-incompatible control characters from string."""
+def sanitize_string(value: str) -> str:
+ """Remove PostgreSQL-incompatible control characters from string.
+
+ Strips \\x00-\\x08, \\x0B-\\x0C, \\x0E-\\x1F, \\x7F while keeping tab,
+ newline, and carriage return. Use this before inserting free-form text
+ into PostgreSQL text/varchar columns.
+ """
return POSTGRES_CONTROL_CHARS.sub("", value)
@@ -116,7 +121,7 @@ def sanitize_json(data: Any) -> Any:
# 1. First convert to basic JSON-serializable types (handles Pydantic models)
# 2. Then sanitize strings in the result
basic_result = to_dict(data)
- return to_dict(basic_result, custom_encoder={str: _sanitize_string})
+ return to_dict(basic_result, custom_encoder={str: sanitize_string})
except Exception as e:
# Log the failure and fall back to string representation
logger.error(
@@ -129,7 +134,7 @@ def sanitize_json(data: Any) -> Any:
)
# Ultimate fallback: convert to string representation and sanitize
- return _sanitize_string(str(data))
+ return sanitize_string(str(data))
class SafeJson(Json):
diff --git a/autogpt_platform/backend/backend/util/truncate.py b/autogpt_platform/backend/backend/util/truncate.py
index cf7de9f8fe..5d28868d37 100644
--- a/autogpt_platform/backend/backend/util/truncate.py
+++ b/autogpt_platform/backend/backend/util/truncate.py
@@ -79,6 +79,10 @@ def truncate(value: Any, size_limit: int) -> Any:
largest str_limit and list_limit that fit.
"""
+ # Fast path: plain strings don't need the binary search machinery.
+ if isinstance(value, str):
+ return _truncate_string_middle(value, size_limit)
+
def measure(val):
try:
return len(str(val))
@@ -86,7 +90,7 @@ def truncate(value: Any, size_limit: int) -> Any:
return sys.getsizeof(val)
# Reasonable bounds for string and list limits
- STR_MIN, STR_MAX = 8, 2**16
+ STR_MIN, STR_MAX = min(8, size_limit), size_limit
LIST_MIN, LIST_MAX = 1, 2**12
# Binary search for the largest str_limit and list_limit that fit
diff --git a/autogpt_platform/docker-compose.platform.yml b/autogpt_platform/docker-compose.platform.yml
index 906cbb40a7..cbd928b4ef 100644
--- a/autogpt_platform/docker-compose.platform.yml
+++ b/autogpt_platform/docker-compose.platform.yml
@@ -162,7 +162,7 @@ services:
context: ../
dockerfile: autogpt_platform/backend/Dockerfile
target: server
- command: ["python", "-m", "backend.copilot.executor"]
+ command: ["python", "-u", "-m", "backend.copilot.executor"]
develop:
watch:
- path: ./
@@ -182,6 +182,7 @@ services:
<<: *backend-env-files
environment:
<<: *backend-env
+ PYTHONUNBUFFERED: "1"
ports:
- "8008:8008"
networks:
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/tools/GenericTool/GenericTool.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/GenericTool/GenericTool.tsx
index 35147af037..8c0cccce83 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/tools/GenericTool/GenericTool.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/GenericTool/GenericTool.tsx
@@ -66,18 +66,23 @@ function getToolCategory(toolName: string): ToolCategory {
case "WebFetch":
return "web";
case "read_workspace_file":
+ case "read_file":
case "Read":
return "file-read";
case "write_workspace_file":
+ case "write_file":
case "Write":
return "file-write";
case "delete_workspace_file":
return "file-delete";
case "list_workspace_files":
+ case "glob":
case "Glob":
return "file-list";
+ case "grep":
case "Grep":
return "search";
+ case "edit_file":
case "Edit":
return "edit";
case "TodoWrite":
@@ -185,12 +190,14 @@ function getInputSummary(toolName: string, input: unknown): string | null {
case "WebSearch":
return typeof inp.query === "string" ? inp.query : null;
case "read_workspace_file":
+ case "read_file":
case "Read":
return (
(typeof inp.file_path === "string" ? inp.file_path : null) ??
(typeof inp.path === "string" ? inp.path : null)
);
case "write_workspace_file":
+ case "write_file":
case "Write":
return (
(typeof inp.file_path === "string" ? inp.file_path : null) ??
@@ -198,10 +205,13 @@ function getInputSummary(toolName: string, input: unknown): string | null {
);
case "delete_workspace_file":
return typeof inp.file_path === "string" ? inp.file_path : null;
+ case "glob":
case "Glob":
return typeof inp.pattern === "string" ? inp.pattern : null;
+ case "grep":
case "Grep":
return typeof inp.pattern === "string" ? inp.pattern : null;
+ case "edit_file":
case "Edit":
return typeof inp.file_path === "string" ? inp.file_path : null;
case "TodoWrite": {
@@ -418,16 +428,22 @@ function getBashAccordionData(
description: truncate(command, 80),
content: (
<div className="space-y-2">
+ {command && (
+ <div>
+ <p className="mb-1 text-xs font-medium text-slate-500">command</p>
+ <ContentCodeBlock>{command}</ContentCodeBlock>
+ </div>
+ )}
{stdout && (
<div>
<p className="mb-1 text-xs font-medium text-slate-500">stdout</p>
- <ContentCodeBlock>{truncate(stdout, 2000)}</ContentCodeBlock>
+ <ContentCodeBlock>{stdout}</ContentCodeBlock>
</div>
)}
{stderr && (
<div>
<p className="mb-1 text-xs font-medium text-slate-500">stderr</p>
- <ContentCodeBlock>{truncate(stderr, 1000)}</ContentCodeBlock>
+ <ContentCodeBlock>{stderr}</ContentCodeBlock>
</div>
)}
{!stdout && !stderr && message && (
@@ -475,18 +491,17 @@ function getWebAccordionData(
: "Search results",
description: truncate(url, 80),
content: content ? (
- <ContentCodeBlock>{truncate(content, 2000)}</ContentCodeBlock>
+ <ContentCodeBlock>{content}</ContentCodeBlock>
) : message ? (
<ContentMessage>{message}</ContentMessage>
) : Object.keys(output).length > 0 ? (
- <ContentCodeBlock>
- {truncate(JSON.stringify(output, null, 2), 2000)}
- </ContentCodeBlock>
+ <ContentCodeBlock>{JSON.stringify(output, null, 2)}</ContentCodeBlock>
) : null,
};
}
function getFileAccordionData(
+ category: ToolCategory,
input: unknown,
output: Record<string, unknown>,
): AccordionData {
@@ -529,6 +544,20 @@ function getFileAccordionData(
displayContent = extractMcpText(output);
}
+ // For edit: show old/new diff; for write: show written content if output is just a status
+ const oldString =
+ category === "edit"
+ ? getStringField(inp as Record<string, unknown>, "old_string")
+ : null;
+ const newString =
+ category === "edit"
+ ? getStringField(inp as Record<string, unknown>, "new_string")
+ : null;
+ const writtenContent =
+ category === "file-write"
+ ? getStringField(inp as Record<string, unknown>, "content")
+ : null;
+
// For Glob/list results, try to show file list
// Files can be either strings (from Glob) or objects (from list_workspace_files)
const files = Array.isArray(output.files) ? output.files : null;
@@ -562,18 +591,33 @@ function getFileAccordionData(
fileListText = fileLines.join("\n");
}
+ const isWriteOrEdit = category === "file-write" || category === "edit";
+
return {
- title: message ?? "File output",
+ title:
+ message ??
+ (isWriteOrEdit ? `Wrote ${truncate(filePath, 60)}` : "File output"),
description: truncate(filePath, 80),
content: (
<div className="space-y-2">
- {displayContent && (
- <ContentCodeBlock>{truncate(displayContent, 2000)}</ContentCodeBlock>
- )}
- {fileListText && (
- <ContentCodeBlock>{truncate(fileListText, 2000)}</ContentCodeBlock>
- )}
- {!displayContent && !fileListText && message && (
+ {oldString && newString != null ? (
+ <>
+ <div>
+ <p className="mb-1 text-xs font-medium text-red-400">removed</p>
+ <ContentCodeBlock>{oldString}</ContentCodeBlock>
+ </div>
+ <div>
+ <p className="mb-1 text-xs font-medium text-green-400">added</p>
+ <ContentCodeBlock>{newString}</ContentCodeBlock>
+ </div>
+ </>
+ ) : writtenContent ? (
+ <ContentCodeBlock>{writtenContent}</ContentCodeBlock>
+ ) : displayContent ? (
+ <ContentCodeBlock>{displayContent}</ContentCodeBlock>
+ ) : null}
+ {fileListText && <ContentCodeBlock>{fileListText}</ContentCodeBlock>}
+ {!displayContent && !fileListText && !writtenContent && message && (
<ContentMessage>{message}</ContentMessage>
)}
</div>
@@ -675,9 +719,7 @@ function getDefaultAccordionData(
return {
title: "Output",
description: message ?? undefined,
- content: (
- <ContentCodeBlock>{truncate(displayContent, 2000)}</ContentCodeBlock>
- ),
+ content: <ContentCodeBlock>{displayContent}</ContentCodeBlock>,
};
}
@@ -697,7 +739,7 @@ function getAccordionData(
case "file-list":
case "search":
case "edit":
- return getFileAccordionData(input, output);
+ return getFileAccordionData(category, input, output);
case "todo":
return getTodoAccordionData(input);
default:
@@ -738,20 +780,18 @@ export function GenericTool({ part }: Props) {
return (
<div className="py-2">
- {/* Only show loading text when NOT showing accordion */}
- {!showAccordion && (
- <div className="flex items-center gap-2 text-sm text-muted-foreground">
- <ToolIcon
- category={category}
- isStreaming={isStreaming}
- isError={isError}
- />
- <MorphingTextAnimation
- text={text}
- className={isError ? "text-red-500" : undefined}
- />
- </div>
- )}
+ {/* Status line: always visible so the user sees what tool ran */}
+ <div className="flex items-center gap-2 text-sm text-muted-foreground">
+ <ToolIcon
+ category={category}
+ isStreaming={isStreaming}
+ isError={isError}
+ />
+ <MorphingTextAnimation
+ text={text}
+ className={isError ? "text-red-500" : undefined}
+ />
+ </div>
{showAccordion && accordionData ? (
<ToolAccordion