Files
privateGPT/tests/components/tools/test_code_execution_local.py
Javier Martinez f2cffd1ab9 feat: code execution v2 (#2278)
* fix: ensure that the publisher is ready to publish new messages

* fix: dockerfile

* fix: clones

(cherry picked from commit bc0a77e050)

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* feat: M3 persistent sessions + M4 isolated bash + OpenSandbox provider

Introduces mount-aware sessions with canonical path abstraction:
- ContentBundle / SessionMount abstraction for skill/plugin mounting
- PathTranslator: rewrites commands and scrubs output (canonical ↔ real paths)
- LocalMount + ReadOnlyMount: local FS-backed mounts; read-only cache shared across sessions
- BashExecutor: asyncio subprocess with setsid + setrlimit isolation + killpg on timeout
- SkillLoader: downloads skill files from object storage as ContentBundles
- LocalCodeExecutionProvider: rewritten to use mounts, TTL reaper, BashExecutor
- OpenSandboxCodeExecutionProvider: new Docker/K8s backend via opensandbox SDK
- Async cascade: create_session / get_or_create_session / build_tool all async
- ObjectStorage.list_files() added to ABC + both implementations
- New settings: session_ttl_seconds, bash rlimit fields, OpenSandboxSettings

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* refactor: generic ContentBundle with BundledFile, remove skill_filter from code_execution layer

- Replace ContentBundle/dataclass with pydantic BaseModel; add BundledFile with path, content, permissions
- Update ReadOnlyMount to use list[BundledFile] with per-file chmod
- Remove skill_filter and SkillLoader from create_session() in base, local, and code_execution_component
- Move skill-to-bundle resolution into BashToolBuilder (inject SkillLoader there)
- Update SkillLoader.load() to return list[BundledFile] objects

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix: remove opensandbox

* feat: use sandbox inside of code executor

* fix: mypy

* feat: add workspace manager

* feat: improvements

* feat: add env abstraction

* feat: remove leftovers

* feat: improve skill content

* fix: macos

* fix: mypy

* feat: add code executor prompt

* feat: add content bundle in stack

* fix: mypy

* fix: layout

* feat: add skill prompt

* fix: order

* feat: allow to present final files

* feat: add container block

* fix: remove default config

* fix: session pers

* fix: do lazy env

* fix: mounter

* feat: refactor mounter

* fix: container

* feat: add requirements

* fix: add container registry

* fix: move to be lazy

* fix: mypy

* fix: current folder

* fix: stop sandbox in tabular

* fi: ensure to use absolute paths in bash & text editor

* feat: add files router

* feat: simplify

* feat: update present files

* fix: download files

* fix: mypy

* fix: tests

* fix: tests

* fix: bash executor in linux

* Revert "fix: bash executor in linux"

This reverts commit 483e208a96.

* Revert "fix: tests"

This reverts commit 50d9288f5e.

* test: remove test in ci

---------

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-29 18:29:29 +02:00

90 lines
2.8 KiB
Python

from pathlib import Path
import pytest
from private_gpt.components.code_execution.local import LocalCodeExecutionProvider
from private_gpt.settings.settings import unsafe_typed_settings
def _settings(tmp_path: Path):
settings = unsafe_typed_settings.model_copy(deep=True)
settings.code_execution.provider = "local"
settings.code_execution.volume_root = None
settings.code_execution.workspace_path = str(tmp_path / "workspaces")
settings.code_execution.timeout = 5
return settings
@pytest.mark.asyncio
async def test_local_code_execution_session_supports_file_operations(
tmp_path: Path,
) -> None:
provider = LocalCodeExecutionProvider(_settings(tmp_path))
session = await provider.create_session("session-2")
created = await session.create("notes.txt", "alpha\nbeta\n")
assert created.success is True
view_all = await session.view("notes.txt")
assert view_all.output == "1: alpha\n2: beta"
replaced = await session.str_replace("notes.txt", "beta", "gamma")
assert replaced.success is True
inserted = await session.insert("notes.txt", 1, "between")
assert inserted.success is True
view_range = await session.view("notes.txt", (2, -1))
assert view_range.output == "2: between\n3: gamma"
listing = await session.view("/home/agent/workspace/")
assert listing.success is True
assert listing.output == "[file] notes.txt"
await session.close()
@pytest.mark.asyncio
async def test_local_code_execution_session_rejects_unmounted_paths(
tmp_path: Path,
) -> None:
provider = LocalCodeExecutionProvider(_settings(tmp_path))
session = await provider.create_session("session-3")
result = await session.create("/home/agent/../escape.txt", "nope")
assert result.success is False
assert "does not match any session mount" in (result.error or "")
await session.close()
@pytest.mark.asyncio
async def test_local_code_execution_session_rejects_readonly_writes(
tmp_path: Path,
) -> None:
provider = LocalCodeExecutionProvider(_settings(tmp_path))
session = await provider.create_session("session-4")
result = await session.create("/mnt/user-data/uploads/x.txt", "nope")
assert result.success is False
assert "read-only" in (result.error or "")
await session.close()
@pytest.mark.asyncio
async def test_local_code_execution_files_survive_session_recreation(
tmp_path: Path,
) -> None:
provider = LocalCodeExecutionProvider(_settings(tmp_path))
first = await provider.create_session("session-5")
await first.create("keep.txt", "data")
provider.delete_session(first)
second = await provider.create_session("session-5")
kept = await second.view("keep.txt")
# The host directories outlive the sandbox — files reappear on reconnect.
assert kept.success is True
assert "data" in kept.output