Files
DeepTutor/deeptutor/services/workspace/execution.py
wumajiehechuan-lab a4c85477b5 fix: request UTF-8 mode for model-authored exec code
`prepare_workspace_execution_env()` sets LANG / PYTHONUNBUFFERED /
PYTHONDONTWRITEBYTECODE but never PYTHONUTF8, and the restricted subprocess
backend deliberately inherits only a fixed subset of host variables. On a
Windows host whose ANSI code page is not UTF-8 (cp936, cp1252, …) the model's
Python therefore ran with `locale.getpreferredencoding() == 'cp936'`.

Two symptoms follow, and they hit any skill or tool script that touches
non-ASCII text:

* a bare `Path.read_text()` / `open()` on a UTF-8 file raises
  `UnicodeDecodeError: 'gbk' codec can't decode byte 0x8d in position 146`
  (or silently mojibakes the content);
* `print()` of a non-ASCII value reaches the caller as GBK bytes, which the
  turn reads back as garbage.

Reproduced end to end with a workspace skill whose build script reads a UTF-8
HTML template: under the exec environment it failed on every invocation, while
the same script succeeded in an ordinary shell on the same host — the classic
environment-dependent failure that is hardest for a user to diagnose. Measured
in the exec environment (same interpreter): `PYTHONUTF8` unset →
`stdout=gbk files=cp936`; set → `stdout=utf-8 files=utf-8`.

One variable fixes both halves, because UTF-8 mode covers file I/O and stdio
together. It is a no-op where UTF-8 is already the locale default, so Linux and
macOS containers behave exactly as before.
2026-09-26 01:44:26 -07:00

120 lines
4.9 KiB
Python

"""Workspace-local environment for model-authored programs."""
from __future__ import annotations
import os
from pathlib import Path, PurePosixPath
def logicalize_workspace_text(value: str, workspace_root: str | Path | None) -> str:
"""Replace the physical workspace prefix with model-facing relative paths."""
text = str(value or "")
if not workspace_root:
return text
root = Path(workspace_root).expanduser().resolve()
prefixes = {str(root), root.as_posix()}
# A Windows subprocess can report either slash spelling regardless of the
# spelling Python used to construct the path.
prefixes.update({prefix.replace("\\", "/") for prefix in tuple(prefixes)})
prefixes.update({prefix.replace("/", "\\") for prefix in tuple(prefixes)})
for prefix in sorted(prefixes, key=len, reverse=True):
text = text.replace(prefix + "/", "")
text = text.replace(prefix + "\\", "")
text = text.replace(prefix, ".")
return text
def logical_workspace_path(path: str | Path, workspace_root: str | Path | None) -> str:
"""Return a POSIX workspace-relative path without leaking a host prefix."""
candidate = Path(path).resolve()
if not workspace_root:
return candidate.name
try:
relative = candidate.relative_to(Path(workspace_root).expanduser().resolve())
except ValueError:
return candidate.name
return PurePosixPath(*relative.parts).as_posix()
def prepare_workspace_execution_env(
turn_output_dir: str | Path,
*,
workspace_root: str | Path | None = None,
) -> dict[str, str]:
"""Return one shared execution environment for a turn.
Source and shell modes deliberately receive the same package target, HOME,
caches, and temporary directory. The caller also gives all exec calls in a
turn one stable writable working directory, so later calls can revise files
created by earlier calls.
Mutable runtime files stay in a hidden directory under the turn output.
That location is available to the isolated sidecar (which only mounts
``outputs/``), while workspace tools and artifact discovery exclude it.
"""
turn_root = Path(turn_output_dir).expanduser().resolve()
turn_root.mkdir(parents=True, exist_ok=True)
root = turn_root / ".deeptutor" / "execution"
cursor = turn_root
for part in (".deeptutor", "execution"):
cursor /= part
if cursor.is_symlink():
raise ValueError("Workspace execution state cannot contain symbolic links.")
cursor.mkdir(exist_ok=True)
locations = {
"home": root / "home",
"tmp": root / "tmp",
"cache": root / "cache",
"python": root / "python-packages",
"npm": root / "npm",
"cargo": root / "cargo",
"go": root / "go",
}
for path in locations.values():
path.mkdir(parents=True, exist_ok=True)
python_path = str(locations["python"])
env = {
"HOME": str(locations["home"]),
"TMPDIR": str(locations["tmp"]),
"TMP": str(locations["tmp"]),
"TEMP": str(locations["tmp"]),
"XDG_CACHE_HOME": str(locations["cache"]),
"PIP_CACHE_DIR": str(locations["cache"] / "pip"),
# PIP_TARGET makes a plain `pip install package` workspace-local even
# when a source install is running DeepTutor from a writable venv.
"PIP_TARGET": python_path,
"PYTHONPATH": python_path,
"PYTHONUSERBASE": str(root / "python-user"),
"npm_config_cache": str(locations["cache"] / "npm"),
"npm_config_prefix": str(locations["npm"]),
"CARGO_HOME": str(locations["cargo"]),
"GOPATH": str(locations["go"]),
"HF_HOME": str(locations["cache"] / "huggingface"),
"TORCH_HOME": str(locations["cache"] / "torch"),
"MPLCONFIGDIR": str(locations["cache"] / "matplotlib"),
"LANG": os.environ.get("LANG", "C.UTF-8"),
"PYTHONUNBUFFERED": "1",
"PYTHONDONTWRITEBYTECODE": "1",
# Model-authored Python must not fall back to the host's ANSI code page
# (cp936, cp1252, …): a bare ``open()`` / ``Path.read_text()`` would then
# mis-decode the UTF-8 files this workspace and every skill writes, and
# printing a non-ASCII path or message would reach the caller mojibaked.
# The exec environment inherits only a fixed subset of host variables,
# so UTF-8 mode has to be requested explicitly rather than assumed.
"PYTHONUTF8": "1",
}
if workspace_root:
# A stable symbolic handle for model-authored code that needs to open
# an existing binary workspace file. Prompts teach the model to append
# an exact workspace-relative path and never reveal the physical value.
env["DEEPTUTOR_WORKSPACE_ROOT"] = str(Path(workspace_root).expanduser().resolve())
return env
__all__ = [
"logical_workspace_path",
"logicalize_workspace_text",
"prepare_workspace_execution_env",
]