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

112 lines
4.5 KiB
Python

from __future__ import annotations
import re
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from pathlib import Path
class PathTranslator:
"""Stateless value object built once per session from its mount table.
Maps LLM-visible canonical paths (e.g. /home/agent/) to real local paths
and back. Mounts are sorted longest-canonical-prefix-first to avoid
ambiguous prefix matching.
All methods are pure (no I/O).
"""
def __init__(self, mounts: list[tuple[str, Path, bool]]) -> None:
# mounts: list of (canonical_prefix, real_path, writable)
self._mounts = sorted(mounts, key=lambda m: len(m[0]), reverse=True)
self._rebuild_regex()
def _rebuild_regex(self) -> None:
# Pre-compile a regex that matches any canonical prefix in a string.
# Patterns are sorted longest-first so the leftmost-longest rule applies.
escaped = [re.escape(canonical) for canonical, _, _ in self._mounts]
if escaped:
self._canonical_re = re.compile("|".join(escaped))
else:
self._canonical_re = re.compile(r"(?!)") # never matches
# Reverse: match any real path prefix.
real_escaped = [re.escape(str(real)) + r"(/|$)" for _, real, _ in self._mounts]
if real_escaped:
self._real_re = re.compile("|".join(real_escaped))
else:
self._real_re = re.compile(r"(?!)")
def register(self, canonical: str, real_path: Path, writable: bool) -> None:
"""Add or update a mount mapping and rebuild the internal regex."""
self._mounts = [(c, r, w) for c, r, w in self._mounts if c != canonical]
self._mounts.append((canonical, real_path, writable))
self._mounts.sort(key=lambda m: len(m[0]), reverse=True)
self._rebuild_regex()
def unregister(self, canonical: str) -> None:
"""Remove a mount mapping and rebuild the internal regex."""
self._mounts = [(c, r, w) for c, r, w in self._mounts if c != canonical]
self._rebuild_regex()
# ------------------------------------------------------------------
# Path translation helpers
# ------------------------------------------------------------------
def to_real(self, canonical_path: str) -> Path:
"""Translate a canonical path to its real filesystem Path.
Raises ValueError if the path does not start with any known mount prefix.
"""
for canonical, real, _ in self._mounts:
if canonical_path.startswith(canonical):
relative = canonical_path[len(canonical) :]
return real / relative
raise ValueError(f"Path '{canonical_path}' does not match any session mount.")
def to_canonical(self, real: Path | str) -> str:
"""Reverse-translate a real path to its canonical form.
Raises ValueError if the real path is outside all mount points.
"""
real_str = str(real)
for canonical, mount_real, _ in self._mounts:
mount_str = str(mount_real)
if real_str == mount_str or real_str.startswith(mount_str + "/"):
relative = real_str[len(mount_str) :]
return canonical + relative.lstrip("/")
raise ValueError(f"Real path '{real}' is not inside any session mount.")
# ------------------------------------------------------------------
# String rewriting (commands and output)
# ------------------------------------------------------------------
def rewrite_command(self, command: str) -> str:
"""Replace all canonical path prefixes in a command string with real paths."""
if not self._mounts:
return command
def _replace(match: re.Match[str]) -> str:
canonical = match.group(0)
for can, real, _ in self._mounts:
if canonical == can:
return str(real)
return canonical # should never happen
return self._canonical_re.sub(_replace, command)
def scrub_output(self, output: str) -> str:
"""Replace all real mount paths in stdout/stderr with canonical paths."""
if not self._mounts:
return output
result = output
# Process longest real paths first (already sorted by canonical length desc,
# which correlates with real path length).
for canonical, real, _ in self._mounts:
real_str = str(real)
result = result.replace(real_str + "/", canonical)
result = result.replace(real_str, canonical.rstrip("/"))
return result