sync update from LAN repo (source ji @ 85078a31)
This commit is contained in:
1 parent
5d6627af50
commit
e602d88695
864 files changed
+99150
-6346
No files matched your search
+131
-33
@@ -26,7 +26,33 @@ class ResolvedPath(NamedTuple):
|
||||
mapping: PathMapping | None
|
||||
|
||||
|
||||
class LocalSandbox(Sandbox):
|
||||
class LocalSandbox(Sandbox):
|
||||
_MODEL_COMMAND_HIDDEN_ENV_KEYS = frozenset(
|
||||
{
|
||||
"DEER_FLOW_CONFIG_PATH",
|
||||
"DEER_FLOW_EXTENSIONS_CONFIG_PATH",
|
||||
"DEER_FLOW_HOME",
|
||||
"DEER_FLOW_HOST_BASE_DIR",
|
||||
"DEER_FLOW_HOST_SKILLS_PATH",
|
||||
"DEER_FLOW_PROJECT_ROOT",
|
||||
"DEER_FLOW_SKILLS_PATH",
|
||||
"PYTHONPATH",
|
||||
}
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _model_command_environment() -> dict[str, str]:
|
||||
"""Return a reduced environment for model-originated host commands.
|
||||
|
||||
LocalSandbox is deliberately not an OS security sandbox, but it should
|
||||
not hand a model canonical skills/config paths through process
|
||||
environment variables. ``PYTHONPATH`` is also removed so an editable
|
||||
backend checkout is not advertised as an import root. Non-path
|
||||
``DEER_FLOW_*`` settings (for example the Gateway port), ordinary PATH,
|
||||
proxy and skill-specific credentials remain available.
|
||||
"""
|
||||
return {key: value for key, value in os.environ.items() if key.upper() not in LocalSandbox._MODEL_COMMAND_HIDDEN_ENV_KEYS}
|
||||
|
||||
@staticmethod
|
||||
def _shell_name(shell: str) -> str:
|
||||
"""Return the executable name for a shell path or command."""
|
||||
@@ -37,26 +63,26 @@ class LocalSandbox(Sandbox):
|
||||
"""Return whether the selected shell is a PowerShell executable."""
|
||||
return LocalSandbox._shell_name(shell) in {"powershell", "powershell.exe", "pwsh", "pwsh.exe"}
|
||||
|
||||
@staticmethod
|
||||
def _is_cmd_shell(shell: str) -> bool:
|
||||
"""Return whether the selected shell is cmd.exe."""
|
||||
return LocalSandbox._shell_name(shell) in {"cmd", "cmd.exe"}
|
||||
|
||||
@staticmethod
|
||||
def _normalize_powershell_command(command: str) -> str:
|
||||
"""Make bash-style command glue usable in Windows PowerShell 5.
|
||||
|
||||
The public tool is still named ``bash`` for model compatibility, but on
|
||||
Windows local sandbox it may fall back to PowerShell. PowerShell 5 does
|
||||
not support ``&&``, while the sandbox layer prepends commands with
|
||||
``cd <workspace> &&``. Rewriting the separator keeps skill scripts
|
||||
executable in that fallback shell.
|
||||
"""
|
||||
return command.replace("&&", ";")
|
||||
|
||||
@staticmethod
|
||||
def _find_first_available_shell(candidates: tuple[str, ...]) -> str | None:
|
||||
"""Return the first executable shell path or command found from candidates."""
|
||||
@staticmethod
|
||||
def _is_cmd_shell(shell: str) -> bool:
|
||||
"""Return whether the selected shell is cmd.exe."""
|
||||
return LocalSandbox._shell_name(shell) in {"cmd", "cmd.exe"}
|
||||
|
||||
@staticmethod
|
||||
def _normalize_powershell_command(command: str) -> str:
|
||||
"""Make bash-style command glue usable in Windows PowerShell 5.
|
||||
|
||||
The public tool is still named ``bash`` for model compatibility, but on
|
||||
Windows local sandbox it may fall back to PowerShell. PowerShell 5 does
|
||||
not support ``&&``, while the sandbox layer prepends commands with
|
||||
``cd <workspace> &&``. Rewriting the separator keeps skill scripts
|
||||
executable in that fallback shell.
|
||||
"""
|
||||
return command.replace("&&", ";")
|
||||
|
||||
@staticmethod
|
||||
def _find_first_available_shell(candidates: tuple[str, ...]) -> str | None:
|
||||
"""Return the first executable shell path or command found from candidates."""
|
||||
for shell in candidates:
|
||||
if os.path.isabs(shell):
|
||||
if os.path.isfile(shell) and os.access(shell, os.X_OK):
|
||||
@@ -246,7 +272,14 @@ class LocalSandbox(Sandbox):
|
||||
|
||||
def replace_match(match: re.Match) -> str:
|
||||
matched_path = match.group(0)
|
||||
return self._resolve_path(matched_path)
|
||||
resolved = self._resolve_path(matched_path)
|
||||
# Normalize to forward slashes: on Windows hosts the resolved path
|
||||
# contains backslashes that the shell (bash -c) would treat as
|
||||
# escape characters, corrupting the rewritten command (e.g.
|
||||
# F:\repo\... losing its separators). Same normalization as
|
||||
# ``_resolve_paths_in_content``; bash and Windows tools all accept
|
||||
# forward-slash paths.
|
||||
return resolved.replace("\\", "/")
|
||||
|
||||
return pattern.sub(replace_match, command)
|
||||
|
||||
@@ -314,14 +347,14 @@ class LocalSandbox(Sandbox):
|
||||
resolved_command = self._resolve_paths_in_command(command)
|
||||
shell = self._get_shell()
|
||||
|
||||
if os.name == "nt":
|
||||
if self._is_powershell(shell):
|
||||
args = [
|
||||
shell,
|
||||
"-NoProfile",
|
||||
"-Command",
|
||||
self._normalize_powershell_command(resolved_command),
|
||||
]
|
||||
if os.name == "nt":
|
||||
if self._is_powershell(shell):
|
||||
args = [
|
||||
shell,
|
||||
"-NoProfile",
|
||||
"-Command",
|
||||
self._normalize_powershell_command(resolved_command),
|
||||
]
|
||||
elif self._is_cmd_shell(shell):
|
||||
args = [shell, "/c", resolved_command]
|
||||
else:
|
||||
@@ -335,6 +368,7 @@ class LocalSandbox(Sandbox):
|
||||
encoding="utf-8",
|
||||
errors="replace",
|
||||
timeout=600,
|
||||
env=self._model_command_environment(),
|
||||
)
|
||||
else:
|
||||
args = [shell, "-c", resolved_command]
|
||||
@@ -346,8 +380,9 @@ class LocalSandbox(Sandbox):
|
||||
encoding="utf-8",
|
||||
errors="replace",
|
||||
timeout=600,
|
||||
env=self._model_command_environment(),
|
||||
)
|
||||
output = result.stdout
|
||||
output = result.stdout
|
||||
if result.stderr:
|
||||
output += f"\nStd Error:\n{result.stderr}" if output else result.stderr
|
||||
if result.returncode != 0:
|
||||
@@ -369,7 +404,7 @@ class LocalSandbox(Sandbox):
|
||||
result.append(f"{reversed_entry}/" if is_dir and not reversed_entry.endswith("/") else reversed_entry)
|
||||
return result
|
||||
|
||||
def read_file(self, path: str) -> str:
|
||||
def read_file(self, path: str) -> str:
|
||||
resolved_path = self._resolve_path(path)
|
||||
try:
|
||||
with open(resolved_path, encoding="utf-8") as f:
|
||||
@@ -383,7 +418,70 @@ class LocalSandbox(Sandbox):
|
||||
return content
|
||||
except OSError as e:
|
||||
# Re-raise with the original path for clearer error messages, hiding internal resolved paths
|
||||
raise type(e)(e.errno, e.strerror, path) from None
|
||||
raise type(e)(e.errno, e.strerror, path) from None
|
||||
|
||||
def read_file_range(
|
||||
self,
|
||||
path: str,
|
||||
*,
|
||||
start_line: int = 1,
|
||||
end_line: int | None = None,
|
||||
max_chars: int = 32_000,
|
||||
) -> tuple[str, int | None, bool]:
|
||||
"""Stream a bounded range without materializing the whole file."""
|
||||
resolved_path = self._resolve_path(path)
|
||||
lines: list[str] = []
|
||||
chars = 0
|
||||
next_line: int | None = None
|
||||
start_line = max(1, start_line)
|
||||
try:
|
||||
with open(resolved_path, encoding="utf-8", errors="replace", newline="") as handle:
|
||||
number = 0
|
||||
while True:
|
||||
fragment = handle.readline(4097)
|
||||
if not fragment:
|
||||
break
|
||||
number += 1
|
||||
remaining = max_chars - chars - (1 if lines else 0)
|
||||
capture = number >= start_line and (end_line is None or number <= end_line)
|
||||
if end_line is not None and number > end_line:
|
||||
break
|
||||
if capture and remaining <= 0:
|
||||
next_line = number
|
||||
break
|
||||
parts: list[str] = []
|
||||
captured = 0
|
||||
over_budget = False
|
||||
while True:
|
||||
ended = fragment.endswith(("\n", "\r"))
|
||||
body = fragment.rstrip("\r\n") if ended else fragment
|
||||
if capture and remaining > captured:
|
||||
piece = body[: remaining - captured]
|
||||
parts.append(piece)
|
||||
captured += len(piece)
|
||||
if len(piece) < len(body):
|
||||
over_budget = True
|
||||
elif capture and body:
|
||||
over_budget = True
|
||||
if ended:
|
||||
break
|
||||
fragment = handle.readline(4097)
|
||||
if not fragment:
|
||||
break
|
||||
if not capture:
|
||||
continue
|
||||
line = "".join(parts)
|
||||
lines.append(line)
|
||||
chars += len(line) + (1 if len(lines) > 1 else 0)
|
||||
if over_budget or chars >= max_chars:
|
||||
next_line = number + 1
|
||||
break
|
||||
content = "\n".join(lines)
|
||||
if resolved_path in self._agent_written_paths:
|
||||
content = self._reverse_resolve_paths_in_output(content)
|
||||
return content, next_line, next_line is not None
|
||||
except OSError as e:
|
||||
raise type(e)(e.errno, e.strerror, path) from None
|
||||
|
||||
def write_file(self, path: str, content: str, append: bool = False) -> None:
|
||||
resolved = self._resolve_path_with_mapping(path)
|
||||
|
||||
Reference in new issue
Block a user