Files
Javier Martinez 21d42fd97a feat: code execution v4 (#2295)
* feat: add bundle to remove

* fix: spaces

* feat: add xml render as skill spec

* feat: add skill volume root to cache

* fix: deduplicate values

* fix: change the mount path to skill_id

* fix: use different paths

* feat: add principal

(cherry picked from commit 5db64fe721d5706440ce9f342b1388ffe742bc16)

# Conflicts:
#	private_gpt/components/code_execution/base.py
#	private_gpt/components/code_execution/code_execution_component.py
#	private_gpt/components/code_execution/local.py
#	private_gpt/components/environment/manager.py
#	private_gpt/components/tools/builders/bash_tool_builder.py
#	private_gpt/components/tools/builders/text_editor_tool_builder.py
#	private_gpt/components/tools/processors/bash_processor.py

* fix: mypy

...

* fix: config

* fix: sandbox config

* feat: add forward cookies

* fix: loop

* feat: add present server

* feat: add feature flag for tools

* refactor: move principal to another better place

* feat: add api key principal

* docs: fix docs

* docs: add present server

* fix: principal

* fix: mypy

* fix: avoid to block the loop

* fix: blocks in expansion

* fix: remove maximum concurrent users

...

* fix: multiplexer

* fix: readers

* fix: more fixes

...

* fix: impl

* feat: tool scheduler

* feat: add adaptative

* feat: add chat worker

* fix: config

* fix: max

* feat: add chat/tools workers

* fix: mypy

* feat: add generic scheduler

* fix: get result

* feat: do serializable the tool executor

* fix: tools

* fix: config

* fix: config

* fix: args

* fix: config

* fix: serializer

* Revert "fix: blocks in expansion"

This reverts commit a2110f94a8.

* fix: unify all logic

* feat: add ingestion scheduler

* fix: settings

* fix: config

* feat: add arq worker to chat

* fix: arq worker

* fix: add nest

* fix: mypy

* fix: await

* fix: script stress

* fix: tokenizer

* fix: chat scheduler

* fix: mypy

* fix: add async tokenizer

* fix: improve condense

* fix: tool scheduler

* feat: add initial real async chat worker

* fix: mypy

* fix: do resumable local executor

...

...

...

fix: revert usleess changes

fix: remove parent chat job

fix: refactor

fix: loop

ref: rename models

fix: chat engine

fix: mypy

...

...

...

fix: fix deps

* fix: tests

* fix: tests

* ...

* fix: stream

* fix: config

* fix: scheduler

* Potential fix for pull request finding

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

* Potential fix for pull request finding

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

* Potential fix for pull request finding

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

* Potential fix for pull request finding

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

* Potential fix for pull request finding

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

* Potential fix for pull request finding

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

* Handle PGPT_WORKER_MODE=celery in health check worker status

* fix: cancel

* fix: arch

* fix: test ingestion

* fix: deserialization of chat messages

* fix: broken results

* fix: mypy

* fix: test

* fix: config

* fix: remove arq tool worker

* fix: output cls

* fix: preserve early resumable tool callbacks

* fix: preserve async tool result order

* refactor: address worker PR review comments

* fix: mypy

* test: colocate ARQ chat enqueue coverage

* fix: remove redis from tests

* fix: mypy

* fix: tests

* test: isolate chat mocks and cancellation timing

* fix: tests

* fix: tests

* fix: test

(cherry picked from commit f8ee460af2)

* fix: worker config

* test: remove flaky chat cancellation assertion

(cherry picked from commit 1115ff2349)

# Conflicts:
#	tests/server/chat/test_chat_routes.py

* fix: emit chat pings from stream listeners

* fix: don't duplciate the output

* fix: rss memory

...

* fix: pass the args

* fix: threads

* fix: websearch

---------

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
2026-07-15 13:05:23 +02:00

161 lines
6.3 KiB
Python

from __future__ import annotations
import posixpath
from typing import TYPE_CHECKING
from private_gpt.components.code_execution.base import CodeExecutionSession
from private_gpt.components.code_execution.results import (
BashExecutionResult,
FileOperationResult,
)
from private_gpt.components.sandbox.base import SandboxExecOptions
if TYPE_CHECKING:
from private_gpt.components.environment.environment import Environment
from private_gpt.components.sandbox.base import SandboxLink, SandboxSession
class SandboxCodeExecutionSession(CodeExecutionSession):
"""CodeExecutionSession tool protocol over a managed Environment.
The environment owns lifetime/idle tracking; its sandbox owns path
translation and permission enforcement. This class only adapts the
tool protocol on top.
"""
def __init__(self, environment: Environment) -> None:
self._env = environment
self._id = environment.id
@property
def _sandbox(self) -> SandboxSession:
return self._env.sandbox
def _resolve_path(self, path: str) -> str:
if posixpath.isabs(path):
return path
return posixpath.join(self._env.workspace, path)
async def execute_bash(
self, command: str, timeout: int | None = None, restart: bool = False
) -> BashExecutionResult:
workspace = self._env.workspace
if restart:
# No cwd: the default is the workspace itself, which every backend
# can resolve (cwd="/" is outside the local translator's mounts).
await self._env.exec(f"rm -rf {workspace}* {workspace}.[!.]*")
await self._sandbox.make_dir(workspace)
result = await self._env.exec(
command,
SandboxExecOptions(timeout=timeout, cwd=workspace),
)
return BashExecutionResult(
success=result.success,
stdout=result.stdout,
stderr=result.stderr,
exit_code=result.exit_code,
execution_time_ms=result.execution_time_ms,
)
async def view(
self, path: str, view_range: tuple[int, int] | None = None
) -> FileOperationResult:
path = self._resolve_path(path)
self._env.touch()
try:
if not await self._sandbox.path_exists(path):
return FileOperationResult(
success=False, error=f"File not found: {path}"
)
if await self._sandbox.is_dir(path):
entries = await self._sandbox.list_dir(path)
return FileOperationResult(success=True, output="\n".join(entries))
raw = await self._sandbox.read_file(path)
text = raw.decode("utf-8", errors="replace")
lines = text.splitlines()
base_line = 1
if view_range is not None:
start, end = view_range
start_idx = max(start, 1) - 1
end_idx = None if end == -1 else max(end, 0)
lines = lines[start_idx:end_idx]
base_line = start_idx + 1
output = "\n".join(
f"{i}: {line}" for i, line in enumerate(lines, start=base_line)
)
return FileOperationResult(success=True, output=output)
except Exception as exc:
return FileOperationResult(success=False, error=str(exc))
async def str_replace(
self, path: str, old_str: str, new_str: str
) -> FileOperationResult:
path = self._resolve_path(path)
self._env.touch()
try:
raw = await self._sandbox.read_file(path)
text = raw.decode("utf-8", errors="replace")
occurrences = text.count(old_str)
if occurrences == 0:
return FileOperationResult(
success=False, error="old_str was not found in the file."
)
if occurrences > 1:
return FileOperationResult(
success=False,
error="old_str appears more than once in the file.",
)
updated = text.replace(old_str, new_str, 1)
await self._sandbox.write_file(path, updated.encode("utf-8"))
return FileOperationResult(success=True, output=f"Updated {path}")
except Exception as exc:
return FileOperationResult(success=False, error=str(exc))
async def create(self, path: str, file_text: str) -> FileOperationResult:
path = self._resolve_path(path)
self._env.touch()
try:
if await self._sandbox.path_exists(path):
return FileOperationResult(
success=False, error=f"File already exists: {path}"
)
await self._sandbox.write_file(path, file_text.encode("utf-8"))
return FileOperationResult(success=True, output=f"Created {path}")
except Exception as exc:
return FileOperationResult(success=False, error=str(exc))
async def insert(
self, path: str, insert_line: int, new_str: str
) -> FileOperationResult:
path = self._resolve_path(path)
self._env.touch()
try:
raw = await self._sandbox.read_file(path)
text = raw.decode("utf-8", errors="replace")
lines = text.splitlines()
if insert_line < 0 or insert_line > len(lines):
return FileOperationResult(
success=False,
error=f"insert_line {insert_line} is out of range.",
)
insertion = new_str.splitlines()
updated_lines = lines[:insert_line] + insertion + lines[insert_line:]
updated = "\n".join(updated_lines)
if text.endswith("\n") or new_str.endswith("\n"):
updated += "\n"
await self._sandbox.write_file(path, updated.encode("utf-8"))
return FileOperationResult(success=True, output=f"Updated {path}")
except Exception as exc:
return FileOperationResult(success=False, error=str(exc))
async def get_endpoint(self, port: int) -> SandboxLink | None:
return await self._sandbox.get_endpoint(port)
async def read_file(self, path: str) -> bytes:
path = self._resolve_path(path)
self._env.touch()
return await self._sandbox.read_file(path)
async def close(self) -> None:
await self._sandbox.close()