Files
privateGPT/private_gpt/components/code_execution/bash_executor.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

123 lines
3.6 KiB
Python

from __future__ import annotations
import asyncio
import contextlib
import os
import signal
import sys
import time
from asyncio.subprocess import PIPE
from typing import TYPE_CHECKING
from private_gpt.components.code_execution.base import BashExecutionResult
if TYPE_CHECKING:
from pathlib import Path
_ISOLATION_AVAILABLE = sys.platform != "win32"
def _child_setup(cpu_s: int, mem_mb: int, fsize_mb: int, nproc: int) -> None:
"""Runs in child process before exec — sets up isolation via rlimit + setsid.
setrlimit calls are best-effort: some platforms (macOS) reject certain
limits (e.g. RLIMIT_AS always fails with a finite value).
"""
os.setsid()
import resource as r
def _set(which: int, limit: tuple[int, int]) -> None:
with contextlib.suppress(ValueError, OSError):
r.setrlimit(which, limit)
_set(r.RLIMIT_CPU, (cpu_s, cpu_s))
_set(r.RLIMIT_AS, (mem_mb << 20, mem_mb << 20))
_set(r.RLIMIT_FSIZE, (fsize_mb << 20, fsize_mb << 20))
_set(r.RLIMIT_NPROC, (nproc, nproc))
def _cap_bytes(data: bytes, limit: int) -> str:
if len(data) > limit:
truncated = data[:limit]
return (
truncated.decode("utf-8", errors="replace")
+ f"\n[output truncated at {limit} bytes]"
)
return data.decode("utf-8", errors="replace")
class LocalBashExecutor:
"""Async subprocess executor with resource isolation (Unix) and output capping.
On Unix: uses os.setsid + setrlimit (CPU, virtual memory, file size, nproc).
On Windows: runs without isolation (same behavior as before).
Timeout kills the entire process group via SIGKILL.
"""
def __init__(
self,
cpu_limit_seconds: int = 30,
memory_limit_mb: int = 512,
fsize_limit_mb: int = 50,
nproc_limit: int = 50,
output_cap_bytes: int = 10 * 1024 * 1024,
) -> None:
self._cpu_s = cpu_limit_seconds
self._mem_mb = memory_limit_mb
self._fsize_mb = fsize_limit_mb
self._nproc = nproc_limit
self._cap = output_cap_bytes
async def run(
self,
command: str,
cwd: Path,
timeout: int | None = None,
) -> BashExecutionResult:
import functools
preexec = (
functools.partial(
_child_setup, self._cpu_s, self._mem_mb, self._fsize_mb, self._nproc
)
if _ISOLATION_AVAILABLE
else None
)
t0 = time.monotonic()
proc = await asyncio.create_subprocess_shell(
command,
stdout=PIPE,
stderr=PIPE,
cwd=str(cwd),
preexec_fn=preexec,
)
try:
out, err = await asyncio.wait_for(
proc.communicate(), timeout=float(timeout) if timeout else 60.0
)
except TimeoutError:
try:
if _ISOLATION_AVAILABLE:
os.killpg(os.getpgid(proc.pid), signal.SIGKILL)
else:
proc.kill()
except ProcessLookupError:
pass
await proc.wait()
return BashExecutionResult(
success=False,
stdout="",
stderr=f"Command timed out after {timeout}s.",
exit_code=124,
execution_time_ms=int((time.monotonic() - t0) * 1000),
)
return BashExecutionResult(
success=(proc.returncode == 0),
stdout=_cap_bytes(out, self._cap),
stderr=_cap_bytes(err, self._cap),
exit_code=proc.returncode or 0,
execution_time_ms=int((time.monotonic() - t0) * 1000),
)