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(backend): make a skill package's files reachable and runnable in a turn (#14544)

Reinier van der Leer722d74de8176
diff --git a/autogpt_platform/backend/backend/copilot/sdk/security_hooks.py b/autogpt_platform/backend/backend/copilot/sdk/security_hooks.py
index d697220cc1..7d31f9736e 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/security_hooks.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/security_hooks.py
@@ -167,7 +167,7 @@ def _validate_user_isolation(
# The "path" param is a cloud storage key (e.g. "/ASEAN/report.md")
# where a leading "/" is normal. Only check for ".." traversal.
# Filesystem paths (source_path, save_to_path) are validated inside
- # the tool itself via _validate_ephemeral_path.
+ # the tool itself via workdir.validate_ephemeral_path.
path = tool_input.get("path", "") or tool_input.get("file_path", "")
if path and ".." in path:
logger.warning(f"Blocked path traversal attempt: {path} by user {user_id}")
diff --git a/autogpt_platform/backend/backend/copilot/tools/skills.py b/autogpt_platform/backend/backend/copilot/tools/skills.py
index db50b36007..e4678b2b58 100644
--- a/autogpt_platform/backend/backend/copilot/tools/skills.py
+++ b/autogpt_platform/backend/backend/copilot/tools/skills.py
@@ -22,8 +22,10 @@ and user-distilled knowledge.
from __future__ import annotations
import asyncio
+import hashlib
import json
import logging
+import posixpath
import re
import uuid
from dataclasses import dataclass
@@ -51,6 +53,13 @@ from backend.util.workspace import WorkspaceManager
from .base import BaseTool
from .models import ErrorResponse, ResponseType, ToolResponseBase
+from .workdir import (
+ make_executable,
+ read_workdir_bytes,
+ remove_from_workdir,
+ save_to_workdir,
+ workdir_root,
+)
logger = logging.getLogger(__name__)
@@ -75,6 +84,12 @@ MAX_BODY_CHARS = 20_000
# length so a misbehaving caller cannot blow the token budget.
MAX_TRIGGERS = 10
MAX_TRIGGER_CHARS = 64
+# Files a skill may carry beside its SKILL.md. The largest package in the
+# public ``anthropics/skills`` set is 83 files, so 100 clears the ecosystem
+# with room to spare while keeping one activation's copy into the sandbox
+# bounded. Enumeration fetches one more than this so a folder that exceeds
+# it is reported rather than silently truncated.
+MAX_PACKAGE_FILES = 100
SKILL_FOLDER = "/skills"
@@ -425,26 +440,31 @@ async def delete_user_skill(
description_for_log[:60],
)
- try:
- siblings = await manager.list_files(
- path=f"{skill_folder(expert_id)}/{slug}/",
- limit=50,
- include_all_sessions=True,
- )
- except Exception:
- siblings = []
await manager.delete_file(info.id)
- for sibling in siblings:
- if sibling.id == info.id:
- continue
+ # One page is capped at MAX_PACKAGE_FILES, so a larger folder needs more
+ # than one pass; anything left behind keeps consuming the user's quota and
+ # is inherited by the next skill stored under this slug. A pass that
+ # deletes nothing new ends the loop, so a file that cannot be deleted stops
+ # it rather than spinning it.
+ attempted: set[str] = set()
+ while True:
try:
- await manager.delete_file(sibling.id)
+ siblings = await _list_package_files(manager, skill_folder(expert_id), slug)
except Exception:
- logger.warning(
- "[skills] failed to delete sibling %s",
- sibling.path,
- exc_info=True,
- )
+ break
+ fresh = [s for s in siblings if s.file_id not in attempted]
+ if not fresh:
+ break
+ for sibling in fresh:
+ attempted.add(sibling.file_id)
+ try:
+ await manager.delete_file(sibling.file_id)
+ except Exception:
+ logger.warning(
+ "[skills] failed to delete sibling %s",
+ sibling.path,
+ exc_info=True,
+ )
await invalidate_skills_index_cache(user_id, expert_id)
if expert_id is not None:
await experts_db().remove_expert_skill_name(user_id, expert_id, slug)
@@ -666,19 +686,17 @@ async def _list_user_skills_from_workspace(
path and falls back to a parallelised read of the SKILL.md body for
any file missing the metadata (older skills, or skills written by a
deployment before the metadata-cache change shipped).
+
+ The SKILL.md filter is applied in the query, not after it: the listing
+ is ordered newest-first and capped, so filtering client-side let one
+ package's files push older skills out of the index entirely.
"""
manager = await _get_user_skill_manager(user_id, scope)
- files = await manager.list_files(
- path=f"{skill_folder(expert_id)}/",
- limit=MAX_USER_SKILLS * 4, # over-fetch in case of strays
- include_all_sessions=True,
- )
- md_files = [f for f in files if f.path.endswith("/SKILL.md")]
+ folder = skill_folder(expert_id)
skills: list[ParsedSkill] = []
needs_read: list[Any] = []
- for f in md_files:
- slug = f.path.rsplit("/", 2)[-2] if "/" in f.path else ""
+ for f, slug in await _list_skill_roots(manager, folder):
meta = f.metadata if isinstance(f.metadata, dict) else {}
entry = _index_entry_from_metadata(slug, meta)
if entry is not None:
@@ -958,12 +976,8 @@ async def list_user_skill_sibling_paths(
return []
try:
manager = await _get_user_skill_manager(user_id, scope)
- files = await manager.list_files(
- path=f"{skill_folder(expert_id)}/{slug}/",
- limit=50,
- include_all_sessions=True,
- )
- return [f.path for f in files if not f.path.endswith("/SKILL.md")]
+ files = await _list_package_files(manager, skill_folder(expert_id), slug)
+ return [f.path for f in files]
except Exception:
logger.warning(
"[skills] failed to list sibling files for %s", slug, exc_info=True
@@ -988,33 +1002,27 @@ async def find_user_skill_slugs(user_id: str, names: list[str]) -> dict[str, str
if not wanted:
return {}
manager = await _get_user_skill_manager(user_id)
- files = await manager.list_files(
- path=f"{SKILL_FOLDER}/", limit=MAX_USER_SKILLS * 4, include_all_sessions=True
- )
found: dict[str, str] = {}
unnamed: list[Any] = []
- for f in files:
- if not f.path.endswith("/SKILL.md"):
- continue
- folder = f.path.rsplit("/", 2)[-2]
- if folder.strip().lower() in wanted:
- found[folder.strip().lower()] = folder
+ for f, slug in await _list_skill_roots(manager, SKILL_FOLDER):
+ if slug.strip().lower() in wanted:
+ found[slug.strip().lower()] = slug
continue
meta = f.metadata if isinstance(f.metadata, dict) else {}
if meta.get(_META_KIND) != _META_KIND_VALUE:
- unnamed.append(f)
+ unnamed.append((f, slug))
missing = wanted - set(found)
if not missing or not unnamed:
return found
parsed = await asyncio.gather(
- *(_parse_skill_from_workspace(manager, f.path) for f in unnamed)
+ *(_parse_skill_from_workspace(manager, f.path) for f, _ in unnamed)
)
- for f, entry in zip(unnamed, parsed):
+ for (_, slug), entry in zip(unnamed, parsed):
if entry is None:
continue
name = entry.name.strip().lower()
if name in missing:
- found.setdefault(name, f.path.rsplit("/", 2)[-2])
+ found.setdefault(name, slug)
return found
@@ -1212,6 +1220,14 @@ class StoreSkillResponse(ToolResponseBase):
expert_id: str | None = None
+class SkillFileInfo(BaseModel):
+ """One file of a skill package, by its workspace path."""
+
+ path: str
+ file_id: str
+ size_bytes: int = 0
+
+
class ReadSkillResponse(ToolResponseBase):
type: ResponseType = ResponseType.SKILL_LOADED
name: str
@@ -1219,6 +1235,10 @@ class ReadSkillResponse(ToolResponseBase):
body: str
triggers: list[str] = []
sibling_files: list[str] = []
+ files: list[SkillFileInfo] = []
+ # Where the package was copied; ``None`` for a single-file skill or when
+ # the copy failed, in which case ``files`` still names the workspace paths.
+ package_dir: str | None = None
is_default: bool = False
expert_id: str | None = None
@@ -1368,7 +1388,8 @@ def _owner_label(expert_id: str | None) -> str:
class ReadSkillTool(BaseTool):
- """Load a skill's body + sibling-file listing by name."""
+ """Load a skill by name: its body, its package listing, and a copy of
+ that package in the working directory the model's shell runs in."""
@property
def name(self) -> str:
@@ -1377,8 +1398,9 @@ class ReadSkillTool(BaseTool):
@property
def description(self) -> str:
return (
- "Read a skill's body + sibling-file list by name. Call when "
- "a task matches an <available_skills> entry."
+ "Read a skill's body + sibling-file list by name, and copy any "
+ "package files into the working directory. Call when a task "
+ "matches an <available_skills> entry."
)
@property
@@ -1495,32 +1517,59 @@ class ReadSkillTool(BaseTool):
session_id=session_id,
)
- # List sibling files (references/, scripts/, assets/, etc.) so
+ # List the package files (references/, scripts/, assets/, ...) so
# the model knows what else lives in the bundle.
+ folder = skill_folder(owner.expert_id)
+ listed = True
try:
- siblings = await manager.list_files(
- path=f"{skill_folder(owner.expert_id)}/{name}/",
- limit=50,
- include_all_sessions=True,
- )
- sibling_paths = [
- f.path for f in siblings if not f.path.endswith("/SKILL.md")
- ]
+ package_files = await _list_package_files(manager, folder, name)
except Exception:
logger.warning(
- "[skills] failed to list siblings for %s", name, exc_info=True
+ "[skills] failed to list package files for %s", name, exc_info=True
+ )
+ package_files = []
+ listed = False
+
+ notes: list[str] = []
+ # A listing that failed is not an empty package: treating it as one
+ # would prune every file the last activation wrote.
+ complete = listed and len(package_files) <= MAX_PACKAGE_FILES
+ if listed and not complete:
+ package_files = package_files[:MAX_PACKAGE_FILES]
+ notes.append(
+ f"Only the first {MAX_PACKAGE_FILES} package files are listed."
+ )
+
+ # Runs even for a skill with no files: a package deleted and re-stored
+ # under the same slug leaves its old files in the working directory.
+ package_dir, warning = await _sync_skill_package(
+ manager,
+ package_files,
+ folder=folder,
+ slug=name,
+ session_id=session_id,
+ complete=complete,
+ )
+ if warning:
+ notes.append(warning)
+ if package_dir:
+ notes.append(
+ f"Package files are at {package_dir}; relative paths in the "
+ "body resolve there. Run scripts with bash_exec from that "
+ "directory."
)
- sibling_paths = []
return ReadSkillResponse(
name=parsed.name,
description=parsed.description,
body=parsed.body,
triggers=list(parsed.triggers),
- sibling_files=sibling_paths,
+ sibling_files=[f.path for f in package_files],
+ files=package_files,
+ package_dir=package_dir,
is_default=False,
expert_id=owner.expert_id,
- message=f"Loaded skill '{name}'.",
+ message=" ".join([f"Loaded skill '{name}'.", *notes]),
session_id=session_id,
)
@@ -1673,3 +1722,264 @@ class ListSkillsTool(BaseTool):
message=f"{len(payload)} skill(s) available for {_owner_label(owner_id)}.",
session_id=session.session_id,
)
+
+
+# ---------------------------------------------------------------------------
+# Package files — enumerating a skill's folder, and keeping the model's
+# working directory in step with it.
+# ---------------------------------------------------------------------------
+
+
+# Rows a listing will scan before giving up. The SKILL.md name filter runs in
+# the query but depth cannot, so a page of newest-first rows can be entirely
+# nested SKILL.md files and yield no roots at all; the bound is the most a
+# compliant folder can hold, every allowed skill carrying a full package.
+_MAX_ROOT_SCAN = MAX_USER_SKILLS * (MAX_PACKAGE_FILES + 1)
+
+
+async def _list_skill_roots(
+ manager: WorkspaceManager, folder: str
+) -> list[tuple[Any, str]]:
+ """``(file, slug)`` for every package root directly under *folder*.
+
+ Pages until the roots run out rather than filtering one capped page: a
+ package shipping its own example ``SKILL.md`` files would otherwise fill
+ the page and hide older skills, which is the defect this listing exists
+ to avoid.
+ """
+ page = MAX_USER_SKILLS * 4 # over-fetch in case of strays
+ roots: list[tuple[Any, str]] = []
+ offset = 0
+ while offset < _MAX_ROOT_SCAN and len(roots) <= MAX_USER_SKILLS:
+ rows = await manager.list_files(
+ path=f"{folder}/",
+ limit=page,
+ offset=offset,
+ include_all_sessions=True,
+ name_contains="SKILL.md",
+ )
+ for row in rows:
+ slug = _root_skill_slug(row.path, folder)
+ if slug is not None:
+ roots.append((row, slug))
+ if len(rows) < page:
+ break
+ offset += page
+ return roots
+
+
+def _root_skill_slug(path: str, folder: str) -> str | None:
+ """Folder slug when *path* is a package root, ``<folder>/<slug>/SKILL.md``.
+
+ ``None`` for a SKILL.md nested deeper: a package may ship one as an
+ example, and indexing it would invent a skill nobody stored.
+ """
+ prefix = f"{folder}/"
+ suffix = "/SKILL.md"
+ if not path.startswith(prefix) or not path.endswith(suffix):
+ return None
+ slug = path[len(prefix) : -len(suffix)]
+ return slug if slug and "/" not in slug else None
+
+
+async def _list_package_files(
+ manager: WorkspaceManager, folder: str, slug: str
+) -> list[SkillFileInfo]:
+ """Every file in a skill's folder except its own SKILL.md, at most
+ ``MAX_PACKAGE_FILES + 1`` so a caller can tell a full package from an
+ oversized one.
+
+ Nested paths are kept: a package's ``scripts/`` and ``references/`` are
+ what make it more than one file.
+ """
+ prefix = f"{folder}/{slug}/"
+ root = f"{prefix}SKILL.md"
+ page = MAX_PACKAGE_FILES + 1
+ files: list[SkillFileInfo] = []
+ offset = 0
+ while len(files) <= MAX_PACKAGE_FILES:
+ rows = await manager.list_files(
+ path=prefix, limit=page, offset=offset, include_all_sessions=True
+ )
+ for row in rows:
+ if row.path == root:
+ continue
+ files.append(
+ SkillFileInfo(
+ path=row.path, file_id=row.id, size_bytes=row.size_bytes or 0
+ )
+ )
+ if len(files) > MAX_PACKAGE_FILES:
+ break
+ if len(rows) < page:
+ break
+ offset += page
+ return files
+
+
+# A skill's body references its resources by relative path ("run
+# scripts/extract.py"), so the files have to exist where the model's shell
+# runs before any of that is actionable: the E2B box in production, the
+# bubblewrap directory locally. The manifest records what each file hashed
+# to, so re-activating a skill in a later turn copies only what changed.
+# Bookkeeping, not part of the package, so it is kept OUT of the directory the
+# package is copied into: a skill shipping its own ``.package.json`` would
+# otherwise be overwritten by it, and the digest would then match forever.
+_MANIFEST_DIR = ".skill-packages"
+_EXECUTABLE_PREFIX = "scripts/"
+# One E2B ``files.write`` is an HTTP round trip and measured 200 ms, so a
+# 60-file package copied serially would cost 12 s of the turn (0.35 s
+# concurrent). Bounded, because each copy also reads a blob.
+_COPY_CONCURRENCY = 16
+
+
+async def _sync_skill_package(
+ manager: WorkspaceManager,
+ files: list[SkillFileInfo],
+ *,
+ folder: str,
+ slug: str,
+ session_id: str,
+ complete: bool,
+) -> tuple[str | None, str | None]:
+ """Make the turn's working directory match the skill's package.
+
+ Returns ``(package_dir, warning)``, the directory being ``None`` when the
+ skill carries no files. A skill's body is worth having without its
+ resources, so every failure here is a warning the model reads rather than
+ an error that withholds the skill.
+
+ *complete* says whether *files* is the whole package: a truncated listing
+ cannot tell a removed file from an unlisted one, so it prunes nothing.
+ """
+ workdir = workdir_root(session_id)
+ package_dir = f"{workdir}/skills/{slug}"
+ manifest_path = f"{workdir}/{_MANIFEST_DIR}/{slug}.json"
+ prefix = f"{folder}/{slug}/"
+ manifest = await _read_package_manifest(manifest_path, session_id)
+ limit = asyncio.Semaphore(_COPY_CONCURRENCY)
+
+ async def copy(info: SkillFileInfo) -> tuple[str, str, str | None] | None:
+ """``(relative path, digest, path to mark executable)`` once the file
+ is in place, or ``None`` when it could not be put there."""
+ relative = info.path[len(prefix) :] if info.path.startswith(prefix) else ""
+ if not _is_safe_relative(relative):
+ logger.warning("[skills] skipping odd package path %s", info.path)
+ return None
+ async with limit:
+ try:
+ content = await manager.read_file(info.path)
+ except Exception:
+ logger.warning("[skills] failed to read %s", info.path, exc_info=True)
+ return None
+ digest = hashlib.sha256(content).hexdigest()
+ if manifest.get(relative) == digest:
+ return relative, digest, None
+ target = await save_to_workdir(
+ f"{package_dir}/{relative}", content, session_id
+ )
+ if isinstance(target, ErrorResponse):
+ logger.warning(
+ "[skills] failed to materialise %s: %s", info.path, target.message
+ )
+ return None
+ return (
+ relative,
+ digest,
+ target if relative.startswith(_EXECUTABLE_PREFIX) else None,
+ )
+
+ copied = await asyncio.gather(*(copy(info) for info in files))
+ written = {relative: digest for relative, digest, _ in filter(None, copied)}
+ executables = [path for _, _, path in filter(None, copied) if path]
+
+ await make_executable(executables, session_id)
+ # A file the skill no longer has must not stay where bash_exec can run it,
+ # and delete_skill followed by store_skill on the same slug is exactly that
+ # case. Prune by what the package HOLDS, not by what was copied: a copy
+ # that failed leaves a current file whose earlier copy is still wanted.
+ removed: set[str] = set()
+ if complete:
+ # The root is not a sibling and is never written here, so it can only
+ # reach the manifest by a hand edit; excluding it keeps the prune from
+ # acting on a name that does not belong to it.
+ current = {info.path[len(prefix) :] for info in files} | {"SKILL.md"}
+ removed = set(manifest) - current
+ await remove_from_workdir(
+ [f"{package_dir}/{relative}" for relative in sorted(removed)], session_id
+ )
+
+ # RESOLVING A CONFLICT HERE: the manifest may only lose an entry for a file
+ # we know is gone — one we removed, or one a listing we know was complete
+ # did not contain. Several branches narrow `removed` or hold an entry back
+ # for their own reason; each is that one rule applied to a different way of
+ # not knowing. Keep every reason rather than taking one side of the diff.
+ next_manifest = {
+ relative: digest
+ for relative, digest in manifest.items()
+ if relative not in written and relative not in removed
+ }
+ next_manifest.update(written)
+ # A single-file skill must not leave an empty package directory behind, so
+ # the manifest is written only when there is, or was, something to track.
+ if next_manifest or manifest:
+ await _write_package_manifest(manifest_path, next_manifest, session_id)
+
+ if not files:
+ return None, None
+ missing = len(files) - len(written)
+ if not written:
+ return None, (
+ f"Could not copy {missing} package file(s) into the working "
+ "directory; read them with read_workspace_file at the workspace "
+ "paths listed in files."
+ )
+ if missing:
+ return package_dir, f"{missing} of {len(files)} package file(s) failed to copy."
+ return package_dir, None
+
+
+def _is_safe_relative(path: str) -> bool:
+ """A package path must stay inside the package directory.
+
+ ``normpath`` alone does not settle it: it collapses ``ok/../../out`` to
+ ``../out``, but an already-normal ``../escape`` comes back unchanged and
+ compares equal. The parent segment is therefore rejected on its own.
+ """
+ return (
+ bool(path)
+ and not path.startswith("/")
+ and ".." not in path.split("/")
+ and posixpath.normpath(path) == path
+ )
+
+
+async def _read_package_manifest(manifest_path: str, session_id: str) -> dict[str, str]:
+ """Hashes written by the last activation, empty on a first run or any
+ unreadable manifest — a re-copy is cheap, a stale skip is not."""
+ raw = await read_workdir_bytes(manifest_path, session_id)
+ if not raw:
+ return {}
+ try:
+ loaded = json.loads(raw)
+ except ValueError:
+ return {}
+ if not isinstance(loaded, dict):
+ return {}
+ # The manifest lives in the model's own working directory, so its keys are
+ # untrusted input to a later rm: keep only paths we would have written.
+ return {
+ k: v
+ for k, v in loaded.items()
+ if isinstance(k, str) and isinstance(v, str) and _is_safe_relative(k)
+ }
+
+
+async def _write_package_manifest(
+ manifest_path: str, hashes: dict[str, str], session_id: str
+) -> None:
+ result = await save_to_workdir(
+ manifest_path, json.dumps(hashes).encode(), session_id
+ )
+ if isinstance(result, ErrorResponse):
+ logger.warning("[skills] failed to write package manifest: %s", result.message)
diff --git a/autogpt_platform/backend/backend/copilot/tools/skills_test.py b/autogpt_platform/backend/backend/copilot/tools/skills_test.py
index 3d2a425107..b18be16ff0 100644
--- a/autogpt_platform/backend/backend/copilot/tools/skills_test.py
+++ b/autogpt_platform/backend/backend/copilot/tools/skills_test.py
@@ -2,6 +2,9 @@
``<available_skills>`` index builder)."""
import json
+import os
+import shutil
+import tempfile
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
@@ -13,6 +16,7 @@ from backend.copilot.tools.skills import (
MAX_BODY_CHARS,
MAX_DESCRIPTION_CHARS,
MAX_NAME_CHARS,
+ MAX_PACKAGE_FILES,
MAX_TRIGGER_CHARS,
MAX_TRIGGERS,
MAX_USER_SKILLS,
@@ -27,11 +31,15 @@ from backend.copilot.tools.skills import (
SkillNotFoundError,
StoreSkillResponse,
StoreSkillTool,
+ _is_safe_relative,
+ _list_user_skills_from_workspace,
_validate_name,
build_skills_context,
delete_user_skill,
+ find_user_skill_slugs,
get_default_skills,
list_all_skills,
+ list_user_skill_sibling_paths,
parse_skill_markdown,
render_skill_markdown,
render_skills_index,
@@ -222,16 +230,33 @@ class _FakeWorkspaceManager:
raise FileNotFoundError(path)
return self.files[path]
- async def list_files(self, *, path, limit, include_all_sessions):
+ async def list_files(
+ self,
+ *,
+ path,
+ limit=None,
+ offset=0,
+ include_all_sessions=False,
+ name_contains=None,
+ ):
result = []
- for p in self.files:
- if p.startswith(path):
- info = MagicMock()
- info.path = p
- info.id = f"id-{p}"
- info.metadata = self.metadata.get(p, {})
- result.append(info)
- return result
+ # Newest first, as the real query orders by ``createdAt DESC``: that
+ # order is what lets one large package crowd an older skill out of a
+ # capped listing, so a fake in insertion order cannot show the bug.
+ for p in reversed(list(self.files)):
+ if not p.startswith(path):
+ continue
+ name = p.rsplit("/", 1)[-1]
+ if name_contains and name_contains.lower() not in name.lower():
+ continue
+ info = MagicMock()
+ info.path = p
+ info.id = f"id-{p}"
+ info.size_bytes = len(self.files[p])
+ info.metadata = self.metadata.get(p, {})
+ result.append(info)
+ result = result[offset:]
+ return result if limit is None else result[:limit]
async def get_file_info_by_path(self, path):
if path not in self.files:
@@ -251,11 +276,14 @@ class _FakeWorkspaceManager:
class _patch_skills_path:
"""Context manager that patches the workspace lookup, the Redis
- client, and the AsyncClusterLock used by ``StoreSkillTool``.
+ client, the AsyncClusterLock used by ``StoreSkillTool``, and the
+ working directory ``read_skill`` materialises packages into.
``store_skill`` calls ``await get_redis_async()`` and constructs an
``AsyncClusterLock`` even when running unit tests — without these
patches the real Redis client tries to dial out and hangs the test.
+ ``workdir`` is a fresh temp dir per block, so one test's manifest
+ cannot make another skip a write.
"""
def __init__(self, fake_manager: _FakeWorkspaceManager):
@@ -263,7 +291,12 @@ class _patch_skills_path:
fake_lock.owner_id = "test-owner"
fake_lock.try_acquire = AsyncMock(return_value="test-owner")
fake_lock.release = AsyncMock()
+ self.workdir = tempfile.mkdtemp(prefix="copilot-skills-test-")
self._patches = [
+ patch(
+ "backend.copilot.tools.workdir.make_session_path",
+ lambda session_id: self.workdir,
+ ),
patch(
"backend.copilot.tools.skills._get_user_skill_manager",
new=AsyncMock(return_value=fake_manager),
@@ -286,6 +319,7 @@ class _patch_skills_path:
def __exit__(self, *exc):
for p in reversed(self._patches):
p.__exit__(*exc)
+ shutil.rmtree(self.workdir, ignore_errors=True)
# ---------------------------------------------------------------------------
@@ -1276,3 +1310,419 @@ async def test_list_skills_returns_feature_disabled_when_flag_off():
assert isinstance(result, ErrorResponse)
assert result.error == "feature_disabled"
+
+
+# ---------------------------------------------------------------------------
+# Multi-file packages: indexing, enumeration, materialisation
+# ---------------------------------------------------------------------------
+
+
+def _package_manager(slug: str = "big", siblings: int = 0) -> _FakeWorkspaceManager:
+ """A fake holding one skill whose SKILL.md was created BEFORE its
+ siblings — the order that made the old client-side filter drop it."""
+ fake = _FakeWorkspaceManager()
+ fake.files[f"/skills/{slug}/SKILL.md"] = render_skill_markdown(
+ ParsedSkill(name=slug, description=f"{slug} description", body="steps")
+ ).encode()
+ for i in range(siblings):
+ fake.files[f"/skills/{slug}/references/r{i:03d}.md"] = f"ref {i}".encode()
+ return fake
+
+
+async def test_a_package_bigger_than_the_listing_cap_stays_indexed():
+ """201 sibling files created after the SKILL.md used to push it off the
+ 200-row, newest-first page, so the skill vanished from
+ <available_skills> entirely (#14525)."""
+ fake = _package_manager(siblings=201)
+ with _patch_skills_path(fake):
+ skills = await _list_user_skills_from_workspace("user-1")
+ assert [s.name for s in skills] == ["big"]
+
+
+async def test_a_nested_skill_md_is_neither_indexed_nor_a_second_skill():
+ """A package may ship a SKILL.md as an example. It is one of the
+ package's files, never a skill of its own."""
+ fake = _package_manager()
+ fake.files["/skills/big/references/examples/SKILL.md"] = b"example"
+ with _patch_skills_path(fake):
+ skills = await _list_user_skills_from_workspace("user-1")
+ slugs = await find_user_skill_slugs("user-1", ["big", "examples"])
+ siblings = await list_user_skill_sibling_paths("user-1", "big")
+ assert [s.name for s in skills] == ["big"]
+ assert slugs == {"big": "big"}
+ assert siblings == ["/skills/big/references/examples/SKILL.md"]
+
+
+async def test_package_enumeration_pages_past_the_old_fifty_row_limit():
+ fake = _package_manager(siblings=80)
+ with _patch_skills_path(fake):
+ siblings = await list_user_skill_sibling_paths("user-1", "big")
+ assert len(siblings) == 80
+
+
+async def test_package_enumeration_stops_one_past_the_cap():
+ """One over the cap, so ``read_skill`` can say the listing is partial
+ instead of quietly presenting a truncated package as whole."""
+ fake = _package_manager(siblings=MAX_PACKAGE_FILES + 50)
+ with _patch_skills_path(fake):
+ siblings = await list_user_skill_sibling_paths("user-1", "big")
+ assert len(siblings) == MAX_PACKAGE_FILES + 1
+
+
+async def test_delete_removes_more_siblings_than_one_page_holds():
+ fake = _package_manager(siblings=60)
+ with _patch_skills_path(fake):
+ await delete_user_skill("user-1", "big")
+ assert fake.files == {}
+
+
+async def test_read_skill_materialises_the_package_into_the_workdir():
+ fake = _package_manager()
+ fake.files["/skills/big/references/guide.md"] = b"read me"
+ fake.files["/skills/big/scripts/run.py"] = b"print('hi')\n"
+ with _patch_skills_path(fake) as patched:
+ result = await ReadSkillTool()._execute(
+ user_id="user-1", session=_make_session(), name="big"
+ )
+ assert isinstance(result, ReadSkillResponse)
+ package_dir = os.path.join(patched.workdir, "skills", "big")
+ assert result.package_dir == package_dir
+ with open(os.path.join(package_dir, "references", "guide.md"), "rb") as f:
+ assert f.read() == b"read me"
+ script = os.path.join(package_dir, "scripts", "run.py")
+ assert os.stat(script).st_mode & 0o111
+ assert {f.path for f in result.files} == {
+ "/skills/big/references/guide.md",
+ "/skills/big/scripts/run.py",
+ }
+ assert result.message.endswith(
+ f"Package files are at {package_dir}; relative paths in the body "
+ "resolve there. Run scripts with bash_exec from that directory."
+ )
+
+
+async def test_a_single_file_skill_materialises_nothing():
+ fake = _package_manager()
+ with _patch_skills_path(fake) as patched:
+ result = await ReadSkillTool()._execute(
+ user_id="user-1", session=_make_session(), name="big"
+ )
+ assert not os.path.exists(os.path.join(patched.workdir, "skills"))
+ assert isinstance(result, ReadSkillResponse)
+ assert result.package_dir is None
+ assert result.files == []
+ assert result.message == "Loaded skill 'big'."
+
+
+async def test_reactivating_a_skill_rewrites_only_what_changed():
+ """The sandbox persists between turns, so a second read_skill should
+ cost one write per changed file, not one per file."""
+ fake = _package_manager()
+ fake.files["/skills/big/references/guide.md"] = b"v1"
+ fake.files["/skills/big/references/stable.md"] = b"unchanged"
+ with _patch_skills_path(fake) as patched:
+ await ReadSkillTool()._execute(
+ user_id="user-1", session=_make_session(), name="big"
+ )
+ guide = os.path.join(patched.workdir, "skills", "big", "references", "guide.md")
+ stable = os.path.join(
+ patched.workdir, "skills", "big", "references", "stable.md"
+ )
+ stable_mtime = os.stat(stable).st_mtime_ns
+ os.utime(stable, ns=(0, 0))
+
+ fake.files["/skills/big/references/guide.md"] = b"v2"
+ await ReadSkillTool()._execute(
+ user_id="user-1", session=_make_session(), name="big"
+ )
+ with open(guide, "rb") as f:
+ assert f.read() == b"v2"
+ # Untouched: the manifest hash matched, so no write happened.
+ assert os.stat(stable).st_mtime_ns == 0
+ assert stable_mtime != 0
+
+
+async def test_a_package_that_cannot_be_copied_still_loads_the_body():
+ """The instructions are worth having without the resources, so a
+ materialisation failure is a note in the message, never an error."""
+ fake = _package_manager()
+ fake.files["/skills/big/references/guide.md"] = b"read me"
+ with _patch_skills_path(fake):
+ with patch(
+ "backend.copilot.tools.skills.save_to_workdir",
+ new=AsyncMock(return_value=ErrorResponse(message="disk full")),
+ ):
+ result = await ReadSkillTool()._execute(
+ user_id="user-1", session=_make_session(), name="big"
+ )
+ assert isinstance(result, ReadSkillResponse)
+ assert result.body.strip() == "steps"
+ assert result.package_dir is None
+ assert [f.path for f in result.files] == ["/skills/big/references/guide.md"]
+ assert "Could not copy 1 package file(s)" in result.message
+
+
+async def test_read_skill_says_when_a_package_exceeds_the_cap():
+ fake = _package_manager(siblings=MAX_PACKAGE_FILES + 5)
+ with _patch_skills_path(fake):
+ result = await ReadSkillTool()._execute(
+ user_id="user-1", session=_make_session(), name="big"
+ )
+ assert isinstance(result, ReadSkillResponse)
+ assert len(result.files) == MAX_PACKAGE_FILES
+ assert f"first {MAX_PACKAGE_FILES} package files" in result.message
+
+
+async def test_a_file_dropped_from_a_package_leaves_the_workdir():
+ """delete_skill then store_skill under the same slug is reachable in one
+ session, and a script left behind is one bash_exec can still run."""
+ fake = _package_manager()
+ fake.files["/skills/big/scripts/old.sh"] = b"#!/bin/bash\necho stale\n"
+ with _patch_skills_path(fake) as patched:
+ await ReadSkillTool()._execute(
+ user_id="user-1", session=_make_session(), name="big"
+ )
+ stale = os.path.join(patched.workdir, "skills", "big", "scripts", "old.sh")
+ assert os.path.exists(stale)
+
+ del fake.files["/skills/big/scripts/old.sh"]
+ result = await ReadSkillTool()._execute(
+ user_id="user-1", session=_make_session(), name="big"
+ )
+ assert not os.path.exists(stale)
+ assert isinstance(result, ReadSkillResponse)
+ assert result.package_dir is None
+ assert result.files == []
+
+
+async def test_a_truncated_listing_prunes_nothing():
+ """Past the cap a listing cannot tell a removed file from an unlisted one,
+ so pruning on it would delete files the package still has."""
+ fake = _package_manager()
+ fake.files["/skills/big/references/keep.md"] = b"keep me"
+ with _patch_skills_path(fake) as patched:
+ await ReadSkillTool()._execute(
+ user_id="user-1", session=_make_session(), name="big"
+ )
+ keep = os.path.join(patched.workdir, "skills", "big", "references", "keep.md")
+ assert os.path.exists(keep)
+
+ # keep.md is now pushed off the end of a capped, newest-first page.
+ for i in range(MAX_PACKAGE_FILES + 1):
+ fake.files[f"/skills/big/references/n{i:03d}.md"] = f"new {i}".encode()
+ await ReadSkillTool()._execute(
+ user_id="user-1", session=_make_session(), name="big"
+ )
+ assert os.path.exists(keep)
+
+
+async def test_delete_drains_a_folder_bigger_than_one_page():
+ """One page is capped, so a bigger folder needs more passes; what is left
+ behind keeps consuming quota and is inherited by the next skill at this
+ slug."""
+ fake = _package_manager(siblings=MAX_PACKAGE_FILES * 2 + 5)
+ with _patch_skills_path(fake):
+ await delete_user_skill("user-1", "big")
+ assert fake.files == {}
+
+
+async def test_a_manifest_path_with_a_parent_segment_is_dropped():
+ """The manifest is read back out of the working directory, which the
+ model's own shell can write, so its keys drive a delete only after the
+ same path check every written file passes."""
+ fake = _package_manager()
+ fake.files["/skills/big/references/guide.md"] = b"read me"
+ with _patch_skills_path(fake) as patched:
+ await ReadSkillTool()._execute(
+ user_id="user-1", session=_make_session(), name="big"
+ )
+ outside = os.path.join(patched.workdir, "outside.txt")
+ with open(outside, "w") as f:
+ f.write("not the package's")
+ manifest = os.path.join(patched.workdir, ".skill-packages", "big.json")
+ with open(manifest, "w") as f:
+ json.dump({"../../outside.txt": "deadbeef"}, f)
+
+ await ReadSkillTool()._execute(
+ user_id="user-1", session=_make_session(), name="big"
+ )
+ assert os.path.exists(outside)
+
+
+ "path", ["../escape", "../../etc/passwd", "..", "ok/../../out", "/abs", ""]
+)
+def test_package_paths_that_leave_the_package_are_refused(path: str):
+ assert _is_safe_relative(path) is False
+
+
[email protected]("path", ["SKILL.md", "refs/a.md", "scripts/run.sh"])
+def test_package_paths_inside_the_package_are_accepted(path: str):
+ assert _is_safe_relative(path) is True
+
+
+async def test_delete_drains_a_folder_no_fixed_number_of_passes_could_clear(
+ monkeypatch,
+):
+ """The loop ends when a pass deletes nothing new, not after a set number
+ of passes: any fixed limit leaves files behind on a folder big enough."""
+ monkeypatch.setattr("backend.copilot.tools.skills.MAX_PACKAGE_FILES", 2)
+ fake = _package_manager(siblings=100)
+ with _patch_skills_path(fake):
+ await delete_user_skill("user-1", "big")
+ assert fake.files == {}
+
+
+async def test_a_file_that_cannot_be_deleted_ends_the_drain():
+ fake = _package_manager(siblings=3)
+ stuck = "/skills/big/references/r001.md"
+
+ async def refuse(file_id):
+ if file_id == f"id-{stuck}":
+ raise RuntimeError("storage says no")
+ return await _FakeWorkspaceManager.delete_file(fake, file_id)
+
+ fake.delete_file = refuse
+ with _patch_skills_path(fake):
+ await delete_user_skill("user-1", "big")
+ assert list(fake.files) == [stuck]
+
+
+async def test_a_failed_package_listing_prunes_nothing():
+ """An enumeration that raised is not an empty package — pruning on it
+ would delete every file the last activation wrote."""
+ fake = _package_manager()
+ fake.files["/skills/big/references/guide.md"] = b"read me"
+ with _patch_skills_path(fake) as patched:
+ await ReadSkillTool()._execute(
+ user_id="user-1", session=_make_session(), name="big"
+ )
+ guide = os.path.join(patched.workdir, "skills", "big", "references", "guide.md")
+ assert os.path.exists(guide)
+
+ with patch(
+ "backend.copilot.tools.skills._list_package_files",
+ new=AsyncMock(side_effect=RuntimeError("workspace unavailable")),
+ ):
+ result = await ReadSkillTool()._execute(
+ user_id="user-1", session=_make_session(), name="big"
+ )
+ assert os.path.exists(guide)
+ assert isinstance(result, ReadSkillResponse)
+ assert result.body.strip() == "steps"
+
+
+async def test_nested_skill_md_files_cannot_hide_a_root_skill():
+ """A package shipping its own example SKILL.md files fills the capped,
+ newest-first page; the listing must page past them to the real roots."""
+ fake = _package_manager(slug="aaa-oldest")
+ for i in range(MAX_USER_SKILLS * 4 + 10):
+ fake.files[f"/skills/aaa-oldest/references/e{i:04d}/SKILL.md"] = b"example"
+ with _patch_skills_path(fake):
+ skills = await _list_user_skills_from_workspace("user-1")
+ slugs = await find_user_skill_slugs("user-1", ["aaa-oldest"])
+ assert [s.name for s in skills] == ["aaa-oldest"]
+ assert slugs == {"aaa-oldest": "aaa-oldest"}
+
+
+async def test_a_truncated_listing_keeps_the_entries_it_could_not_see():
+ """A capped listing proves presence, never absence, so it must not drop
+ the manifest entries for files it never saw: the next complete listing is
+ what decides whether those files are gone, and it can only reach them
+ through the manifest."""
+ fake = _package_manager()
+ fake.files["/skills/big/references/keep.md"] = b"v1"
+ with _patch_skills_path(fake) as patched:
+ await ReadSkillTool()._execute(
+ user_id="user-1", session=_make_session(), name="big"
+ )
+ keep = os.path.join(patched.workdir, "skills", "big", "references", "keep.md")
+ assert os.path.exists(keep)
+
+ # Past the cap keep.md falls off the newest-first page, so this
+ # activation cannot see it and must leave its entry alone.
+ extra = [
+ f"/skills/big/references/n{i:03d}.md" for i in range(MAX_PACKAGE_FILES + 5)
+ ]
+ for i, path in enumerate(extra):
+ fake.files[path] = f"new {i}".encode()
+ await ReadSkillTool()._execute(
+ user_id="user-1", session=_make_session(), name="big"
+ )
+ assert os.path.exists(keep)
+
+ # Back under the cap, with keep.md gone from the package: now the
+ # listing is complete, so the file is known gone and must be removed.
+ for path in extra:
+ del fake.files[path]
+ del fake.files["/skills/big/references/keep.md"]
+ await ReadSkillTool()._execute(
+ user_id="user-1", session=_make_session(), name="big"
+ )
+ assert not os.path.exists(keep)
+
+
+async def test_a_pruned_file_loses_its_manifest_entry():
+ """The other half of the rule: once a file is known gone and removed, its
+ entry goes too, or the manifest keeps describing a file that is not there
+ and every later activation re-prunes it."""
+ fake = _package_manager()
+ fake.files["/skills/big/references/gone.md"] = b"bye"
+ fake.files["/skills/big/references/stays.md"] = b"here"
+ with _patch_skills_path(fake) as patched:
+ await ReadSkillTool()._execute(
+ user_id="user-1", session=_make_session(), name="big"
+ )
+ manifest_path = os.path.join(patched.workdir, ".skill-packages", "big.json")
+ with open(manifest_path) as f:
+ assert set(json.load(f)) == {"references/gone.md", "references/stays.md"}
+
+ del fake.files["/skills/big/references/gone.md"]
+ await ReadSkillTool()._execute(
+ user_id="user-1", session=_make_session(), name="big"
+ )
+ with open(manifest_path) as f:
+ assert set(json.load(f)) == {"references/stays.md"}
+
+
+async def test_a_package_file_named_like_the_manifest_survives():
+ """The manifest is our bookkeeping, not part of the package, so it must
+ not sit where a package file could collide with it: the collision
+ overwrites the user's file and the digest then matches, so no later
+ activation ever restores it."""
+ fake = _package_manager()
+ fake.files["/skills/big/.package.json"] = b'{"mine": true}'
+ with _patch_skills_path(fake) as patched:
+ await ReadSkillTool()._execute(
+ user_id="user-1", session=_make_session(), name="big"
+ )
+ theirs = os.path.join(patched.workdir, "skills", "big", ".package.json")
+ with open(theirs, "rb") as f:
+ assert f.read() == b'{"mine": true}'
+
+ await ReadSkillTool()._execute(
+ user_id="user-1", session=_make_session(), name="big"
+ )
+ with open(theirs, "rb") as f:
+ assert f.read() == b'{"mine": true}'
diff --git a/autogpt_platform/backend/backend/copilot/tools/workdir.py b/autogpt_platform/backend/backend/copilot/tools/workdir.py
new file mode 100644
index 0000000000..f3ff384050
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/tools/workdir.py
@@ -0,0 +1,183 @@
+"""Writing files into the turn's working directory.
+
+The model's shell and file tools run on an E2B sandbox in production and in a
+bubblewrap-isolated ``/tmp/copilot-<session>`` directory locally. Both branches
+live here so every tool that puts a file where the model can reach it —
+``read_workspace_file``'s ``save_to_path``, ``read_skill``'s package
+materialisation — resolves and validates the destination the same way.
+"""
+
+import logging
+import os
+import shlex
+
+from backend.copilot.context import (
+ E2B_WORKDIR,
+ get_current_sandbox,
+ resolve_sandbox_path,
+)
+from backend.copilot.tools.sandbox import make_session_path
+
+from .models import ErrorResponse
+
+logger = logging.getLogger(__name__)
+
+
+def workdir_root(session_id: str) -> str:
+ """Directory the model's shell starts in for this turn."""
+ if get_current_sandbox() is not None:
+ return E2B_WORKDIR
+ return make_session_path(session_id)
+
+
+async def save_to_workdir(
+ 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.
+ """
+
+ sandbox = get_current_sandbox()
+ if sandbox is not None:
+ remote = resolve_sandbox_path_or_error(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
+
+
+async def read_workdir_bytes(path: str, session_id: str) -> bytes | None:
+ """Read *path* back out of the working directory, or ``None`` when it is
+ absent or unreadable — callers use this for bookkeeping files whose
+ absence is the ordinary first-run case."""
+ sandbox = get_current_sandbox()
+ try:
+ if sandbox is not None:
+ remote = resolve_sandbox_path_or_error(path, session_id, "path")
+ if isinstance(remote, ErrorResponse):
+ return None
+ return bytes(await sandbox.files.read(remote, format="bytes"))
+ validated = validate_ephemeral_path(
+ path, param_name="path", session_id=session_id
+ )
+ if isinstance(validated, ErrorResponse):
+ return None
+ with open(validated, "rb") as f:
+ return f.read()
+ except Exception:
+ return None
+
+
+async def remove_from_workdir(paths: list[str], session_id: str) -> None:
+ """Best-effort delete. A file left behind is one the model can still read
+ or run after it has left the package, so a failure here is logged loudly
+ rather than swallowed."""
+ if not paths:
+ return
+ sandbox = get_current_sandbox()
+ try:
+ if sandbox is not None:
+ quoted = " ".join(shlex.quote(p) for p in paths)
+ await sandbox.commands.run(f"rm -f {quoted}")
+ return
+ for path in paths:
+ try:
+ os.remove(path)
+ except FileNotFoundError:
+ pass
+ except Exception:
+ logger.warning(
+ "[workdir] failed to remove %d stale file(s) in session %s",
+ len(paths),
+ session_id,
+ exc_info=True,
+ )
+
+
+async def make_executable(paths: list[str], session_id: str) -> None:
+ """Best-effort ``chmod +x``. A package whose scripts are not executable is
+ still usable through ``python script.py``, so a failure here is logged and
+ never surfaced."""
+ if not paths:
+ return
+ sandbox = get_current_sandbox()
+ try:
+ if sandbox is not None:
+ quoted = " ".join(shlex.quote(p) for p in paths)
+ await sandbox.commands.run(f"chmod +x {quoted}")
+ return
+ for path in paths:
+ os.chmod(path, os.stat(path).st_mode | 0o111)
+ except Exception:
+ logger.warning(
+ "[workdir] failed to mark %d file(s) executable in session %s",
+ len(paths),
+ session_id,
+ exc_info=True,
+ )
+
+
+def resolve_sandbox_path_or_error(
+ 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.context.resolve_sandbox_path` and
+ wraps any ``ValueError`` into an :class:`ErrorResponse`.
+ """
+ try:
+ return resolve_sandbox_path(path)
+ except ValueError:
+ return ErrorResponse(
+ message=f"{param_name} must be within {E2B_WORKDIR}",
+ session_id=session_id,
+ )
+
+
+def validate_ephemeral_path(
+ path: str, *, param_name: str, session_id: str
+) -> ErrorResponse | str:
+ """Validate that *path* is inside the session's ephemeral directory.
+
+ Uses the session-specific directory (``make_session_path(session_id)``)
+ rather than the bare prefix, so ``/tmp/copilot-evil/...`` is rejected.
+
+ Returns the resolved real path on success, or an ``ErrorResponse`` when the
+ path escapes the session directory.
+ """
+ session_dir = os.path.realpath(make_session_path(session_id)) + os.sep
+ real = os.path.realpath(path)
+ if not real.startswith(session_dir):
+ return ErrorResponse(
+ message=(
+ f"{param_name} must be within the ephemeral working "
+ f"directory ({make_session_path(session_id)})"
+ ),
+ session_id=session_id,
+ )
+ return real
diff --git a/autogpt_platform/backend/backend/copilot/tools/workspace_files.py b/autogpt_platform/backend/backend/copilot/tools/workspace_files.py
index 94d9d404e8..8b468b30cb 100644
--- a/autogpt_platform/backend/backend/copilot/tools/workspace_files.py
+++ b/autogpt_platform/backend/backend/copilot/tools/workspace_files.py
@@ -8,17 +8,19 @@ from typing import Any, Optional
from backend.api.features.store.exceptions import VirusDetectedError, VirusScanError
from backend.copilot.context import (
- E2B_WORKDIR,
get_current_sandbox,
get_sdk_cwd,
get_workspace_manager,
is_allowed_local_path,
looks_like_sdk_tool_result_path,
- resolve_sandbox_path,
sdk_tool_result_redirect_hint,
)
from backend.copilot.model import ChatSession
-from backend.copilot.tools.sandbox import make_session_path
+from backend.copilot.tools.workdir import (
+ resolve_sandbox_path_or_error,
+ save_to_workdir,
+ validate_ephemeral_path,
+)
from backend.data.activity_event import ActivityEventDraft
from backend.data.workspace_scope import WorkspaceAccessDeniedError
from backend.util.settings import Config
@@ -93,29 +95,12 @@ async 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_sandbox_path`
- and wraps any ``ValueError`` into an :class:`ErrorResponse`.
- """
- try:
- return resolve_sandbox_path(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."""
sandbox = get_current_sandbox()
if sandbox is not None:
- remote = _resolve_sandbox_path(source_path, session_id, "source_path")
+ remote = resolve_sandbox_path_or_error(source_path, session_id, "source_path")
if isinstance(remote, ErrorResponse):
return remote
try:
@@ -128,7 +113,7 @@ async def _read_source_path(source_path: str, session_id: str) -> bytes | ErrorR
)
# Local fallback: validate path stays within ephemeral directory.
- validated = _validate_ephemeral_path(
+ validated = validate_ephemeral_path(
source_path, param_name="source_path", session_id=session_id
)
if isinstance(validated, ErrorResponse):
@@ -148,71 +133,6 @@ async def _read_source_path(source_path: str, session_id: str) -> bytes | ErrorR
)
-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.
- """
-
- 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:
- """Validate that *path* is inside the session's ephemeral directory.
-
- Uses the session-specific directory (``make_session_path(session_id)``)
- rather than the bare prefix, so ``/tmp/copilot-evil/...`` is rejected.
-
- Returns the resolved real path on success, or an ``ErrorResponse`` when the
- path escapes the session directory.
- """
- session_dir = os.path.realpath(make_session_path(session_id)) + os.sep
- real = os.path.realpath(path)
- if not real.startswith(session_dir):
- return ErrorResponse(
- message=(
- f"{param_name} must be within the ephemeral working "
- f"directory ({make_session_path(session_id)})"
- ),
- session_id=session_id,
- )
- return real
-
-
_TEXT_MIME_PREFIXES = (
"text/",
"application/json",
@@ -642,7 +562,7 @@ class ReadWorkspaceFileTool(BaseTool):
cached_content: bytes | None = None
if save_to_path:
cached_content = await manager.read_file_by_id(target_file_id)
- result = await _save_to_path(save_to_path, cached_content, session_id)
+ result = await save_to_workdir(save_to_path, cached_content, session_id)
if isinstance(result, ErrorResponse):
return result
save_to_path = result
@@ -749,14 +669,15 @@ class ReadWorkspaceFileTool(BaseTool):
)
-# Paths under ``/skills/`` are managed by the skills registry — the
-# ``store_skill`` / ``delete_skill`` tools enforce frontmatter validation,
-# the per-user cap, name regex, and content sanitisation. Allowing plain
-# write_workspace_file / delete_workspace_file there would bypass all of
-# that and let the model accidentally (or maliciously) corrupt the
-# registry. Reads stay open so the model can still inspect sibling
-# references inside a skill bundle.
+# Paths under ``/skills/`` and ``/experts/<id>/skills/`` are managed by the
+# skills registry — the ``store_skill`` / ``delete_skill`` tools enforce
+# frontmatter validation, the per-user cap, name regex, and content
+# sanitisation. Allowing plain write_workspace_file / delete_workspace_file
+# there would bypass all of that and let the model accidentally (or
+# maliciously) corrupt the registry. Reads stay open so the model can still
+# inspect sibling references inside a skill bundle.
_SKILLS_REGISTRY_PREFIX = "skills/"
+_EXPERTS_PREFIX = "experts/"
_SKILLS_REGISTRY_ERROR = (
"Path is managed by the skills registry; use store_skill / "
"delete_skill instead. (read_workspace_file can still read "
@@ -765,15 +686,25 @@ _SKILLS_REGISTRY_ERROR = (
def _path_under_skills_registry(path: str | None) -> bool:
- """Return ``True`` when *path* normalises to a location under
- the skills-registry folder (``/skills/...`` or ``skills/...``,
- case-insensitive)."""
+ """Return ``True`` when *path* normalises to a location under either
+ skills-registry folder — Otto's ``/skills/...`` or an expert's
+ ``/experts/<id>/skills/...`` — case-insensitively."""
if not path:
return False
# Strip leading slashes + whitespace, lower-case so case variants
# (``Skills/foo``) cannot bypass the check.
normalised = path.strip().lstrip("/").lower()
- return normalised.startswith(_SKILLS_REGISTRY_PREFIX) or normalised == "skills"
+ if normalised.startswith(_SKILLS_REGISTRY_PREFIX) or normalised == "skills":
+ return True
+ if not normalised.startswith(_EXPERTS_PREFIX):
+ return False
+ # ``experts/<id>/skills`` and anything below it; the id is any single
+ # segment, so a deeper path under another expert folder is not caught
+ # here — nothing else lives under ``/experts/`` in the workspace.
+ rest = normalised[len(_EXPERTS_PREFIX) :].split("/", 1)
+ return len(rest) == 2 and (
+ rest[1].startswith(_SKILLS_REGISTRY_PREFIX) or rest[1] == "skills"
+ )
class WriteWorkspaceFileTool(BaseTool):
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 d122ba4a03..c6ad5be81e 100644
--- a/autogpt_platform/backend/backend/copilot/tools/workspace_files_test.py
+++ b/autogpt_platform/backend/backend/copilot/tools/workspace_files_test.py
@@ -10,6 +10,7 @@ import pytest
from backend.copilot.context import SDK_PROJECTS_DIR, _current_project_dir
from backend.copilot.tools._test_data import make_session, setup_test_data
from backend.copilot.tools.models import ErrorResponse
+from backend.copilot.tools.workdir import validate_ephemeral_path
from backend.copilot.tools.workspace_files import (
_MAX_LOCAL_TOOL_RESULT_BYTES,
DeleteWorkspaceFileTool,
@@ -22,7 +23,6 @@ from backend.copilot.tools.workspace_files import (
WriteWorkspaceFileTool,
_read_local_tool_result,
_resolve_write_content,
- _validate_ephemeral_path,
)
# Re-export so pytest discovers the session-scoped fixture
@@ -39,14 +39,14 @@ def ephemeral_dir(tmp_path, monkeypatch):
session_dir.mkdir()
monkeypatch.setattr(
- "backend.copilot.tools.workspace_files.make_session_path",
+ "backend.copilot.tools.workdir.make_session_path",
lambda session_id: str(session_dir),
)
return session_dir
# ---------------------------------------------------------------------------
-# _validate_ephemeral_path
+# validate_ephemeral_path
# ---------------------------------------------------------------------------
@@ -54,7 +54,7 @@ class TestValidateEphemeralPath:
def test_valid_path(self, ephemeral_dir):
target = ephemeral_dir / "file.txt"
target.touch()
- result = _validate_ephemeral_path(
+ result = validate_ephemeral_path(
str(target), param_name="test", session_id="s1"
)
assert isinstance(result, str)
@@ -62,7 +62,7 @@ class TestValidateEphemeralPath:
def test_path_traversal_rejected(self, ephemeral_dir):
evil_path = str(ephemeral_dir / ".." / "etc" / "passwd")
- result = _validate_ephemeral_path(evil_path, param_name="test", session_id="s1")
+ result = validate_ephemeral_path(evil_path, param_name="test", session_id="s1")
# Should return ErrorResponse
from backend.copilot.tools.models import ErrorResponse
@@ -73,7 +73,7 @@ class TestValidateEphemeralPath:
other_dir.mkdir()
target = other_dir / "steal.txt"
target.touch()
- result = _validate_ephemeral_path(
+ result = validate_ephemeral_path(
str(target), param_name="test", session_id="s1"
)
from backend.copilot.tools.models import ErrorResponse
@@ -86,7 +86,7 @@ class TestValidateEphemeralPath:
outside_file.write_text("secret")
symlink = ephemeral_dir / "link.txt"
symlink.symlink_to(outside_file)
- result = _validate_ephemeral_path(
+ result = validate_ephemeral_path(
str(symlink), param_name="test", session_id="s1"
)
from backend.copilot.tools.models import ErrorResponse
@@ -98,7 +98,7 @@ class TestValidateEphemeralPath:
nested.mkdir(parents=True)
target = nested / "data.csv"
target.touch()
- result = _validate_ephemeral_path(
+ result = validate_ephemeral_path(
str(target), param_name="test", session_id="s1"
)
assert isinstance(result, str)
@@ -859,8 +859,9 @@ class TestReadWorkspaceFileSdkToolResultRedirect:
class TestSkillsRegistryACL:
- """Writes and deletes targeting ``/skills/`` must go through the
- skills registry (``store_skill`` / ``delete_skill``) which enforces
+ """Writes and deletes targeting either skills folder — Otto's
+ ``/skills/`` or an expert's ``/experts/<id>/skills/`` — must go through
+ the skills registry (``store_skill`` / ``delete_skill``) which enforces
frontmatter validation, the per-user cap, and content sanitisation.
A direct workspace write would bypass all of that."""
@@ -873,6 +874,10 @@ class TestSkillsRegistryACL:
" /Skills/foo/SKILL.md ", # case + whitespace tolerance
"skills",
"/skills/",
+ "/experts/expert-a/skills/foo/SKILL.md",
+ "experts/expert-a/skills/foo/scripts/run.py",
+ " /Experts/expert-a/Skills/foo/SKILL.md ",
+ "/experts/expert-a/skills",
],
)
async def test_write_workspace_file_rejects_skills_path(self, path):
@@ -909,13 +914,39 @@ class TestSkillsRegistryACL:
assert "skills registry" in result.message
@pytest.mark.asyncio
- async def test_delete_workspace_file_rejects_skills_path(self):
+ @pytest.mark.parametrize(
+ "path",
+ [
+ "/skills/foo/SKILL.md",
+ "/experts/expert-a/skills/foo/SKILL.md",
+ "/experts/expert-a/skills/foo/references/notes.md",
+ ],
+ )
+ async def test_delete_workspace_file_rejects_skills_path(self, path):
tool = DeleteWorkspaceFileTool()
session = make_session("user-acl-test")
result = await tool._execute(
user_id="user-acl-test",
session=session,
- path="/skills/foo/SKILL.md",
+ path=path,
)
assert isinstance(result, ErrorResponse)
assert "skills registry" in result.message
+
+ @pytest.mark.asyncio
+ @pytest.mark.parametrize(
+ "path",
+ [
+ "/experts/expert-a/notes.md",
+ "/experts",
+ "/expertskills/foo/SKILL.md",
+ "/documents/skills-notes.md",
+ ],
+ )
+ async def test_paths_outside_either_registry_are_not_blocked(self, path):
+ """The guard must not swallow an ordinary path that merely starts the
+ same way — an expert folder that is not its skills folder, or a
+ lookalike name."""
+ from backend.copilot.tools.workspace_files import _path_under_skills_registry
+
+ assert _path_under_skills_registry(path) is False
diff --git a/autogpt_platform/backend/backend/data/workspace_scope.py b/autogpt_platform/backend/backend/data/workspace_scope.py
index 23e80dd066..a086fab2e6 100644
--- a/autogpt_platform/backend/backend/data/workspace_scope.py
+++ b/autogpt_platform/backend/backend/data/workspace_scope.py
@@ -18,6 +18,12 @@ from pydantic import BaseModel, Field
SESSIONS_ROOT = "/sessions/"
EXPERTS_ROOT = "/experts/"
+SKILLS_ROOT = "/skills/"
+
+# Roots a WorkspaceManager takes literally instead of resolving under the
+# calling session: a skill package is shared across every session, so a
+# session prefix would make its files unreachable from the turn that read it.
+SHARED_ROOTS = (SESSIONS_ROOT, SKILLS_ROOT, EXPERTS_ROOT)
EXPERT_FILE_ACCESS_DENIED = (
"This file is outside this expert's scope. Experts can only access files "
diff --git a/autogpt_platform/backend/backend/util/workspace.py b/autogpt_platform/backend/backend/util/workspace.py
index f93e0875bc..c808394c8f 100644
--- a/autogpt_platform/backend/backend/util/workspace.py
+++ b/autogpt_platform/backend/backend/util/workspace.py
@@ -18,6 +18,7 @@ from backend.data.db_accessors import workspace_db
from backend.data.workspace import WorkspaceFile
from backend.data.workspace_scope import (
EXPERT_FILE_ACCESS_DENIED,
+ SHARED_ROOTS,
WorkspaceAccessDeniedError,
WorkspaceScope,
)
@@ -110,7 +111,10 @@ class WorkspaceManager:
"""
Resolve a path, defaulting to session folder if session_id is set.
- Cross-session access is allowed by explicitly using /sessions/other-session-id/...
+ An absolute path into a shared root (:data:`SHARED_ROOTS`) is taken
+ as written: another session's folder, or a skill package, which every
+ session of the account shares. :meth:`_authorize_path` still applies,
+ so an expert reaches only the roots its scope grants.
Args:
path: Virtual path (e.g., "/file.txt" or "/sessions/abc123/file.txt")
@@ -118,8 +122,7 @@ class WorkspaceManager:
Returns:
Resolved path with session prefix if applicable
"""
- # If path explicitly references a session folder, use it as-is
- if path.startswith("/sessions/"):
+ if path.startswith(SHARED_ROOTS):
return path
# If we have a session context, prepend session path
diff --git a/autogpt_platform/backend/backend/util/workspace_test.py b/autogpt_platform/backend/backend/util/workspace_test.py
index 961713b356..0e8124a459 100644
--- a/autogpt_platform/backend/backend/util/workspace_test.py
+++ b/autogpt_platform/backend/backend/util/workspace_test.py
@@ -290,3 +290,48 @@ async def test_write_file_storage_check_routes_through_workspace_db_accessor(
await manager.write_file(filename="test.txt", content=b"hello")
mock_db.get_workspace_total_size.assert_awaited_once_with("ws-123")
+
+
+# ---------------------------------------------------------------------------
+# Path resolution: which roots a session-scoped manager takes literally
+# ---------------------------------------------------------------------------
+
+
+def session_manager():
+ return WorkspaceManager(
+ user_id="user-123", workspace_id="ws-123", session_id="sess-1"
+ )
+
+
+ "path",
+ [
+ "/skills/pdf/SKILL.md",
+ "/skills/pdf/references/guide.md",
+ "/experts/expert-a/skills/pdf/scripts/extract.py",
+ "/sessions/other/report.pdf",
+ ],
+)
+def test_shared_roots_resolve_as_written(session_manager, path: str):
+ """A skill package belongs to the account, not to the chat that opened it:
+ prefixing the session folder would make ``read_workspace_file`` miss every
+ path ``read_skill`` hands the model."""
+ assert session_manager._resolve_path(path) == path
+
+
+ "path,expected",
+ [
+ ("/report.pdf", "/sessions/sess-1/report.pdf"),
+ ("report.pdf", "/sessions/sess-1/report.pdf"),
+ ("skills/pdf/SKILL.md", "/sessions/sess-1/skills/pdf/SKILL.md"),
+ ("/skillset/notes.md", "/sessions/sess-1/skillset/notes.md"),
+ ],
+)
+def test_everything_else_still_resolves_under_the_session(
+ session_manager, path: str, expected: str
+):
+ """Only an absolute path into a shared root escapes session scoping — a
+ relative ``skills/...`` and a lookalike folder must not."""
+ assert session_manager._resolve_path(path) == expected