fix(docker): sanitize the session-key task_id used as a sandbox path
With terminal.backend: docker and container_persistent: true, every gateway session failed on its first tool call: docker run exited 125 with "invalid spec ... too many colons" and no command could execute. _resolve_container_task_id() returns "session:<key>" whenever a session key is present, and gateway session keys are colon-delimited (session:agent:main:telegram:dm:<chat_id>). DockerEnvironment joined that id into the persistent sandbox path verbatim, so the -v spec became ".../docker/session:agent:main:telegram:dm:<id>/home:/root" — docker splits a spec on ':', read the extra fields as extra mount options, and refused the run. The container label a few lines below already guards this exact value class via _sanitize_label_value(); the bind-mount source did not. Derive the directory name through _sandbox_dir_name() instead. Ids that are already bind-mountable are returned verbatim, so the shared "default" sandbox and RL/benchmark rollouts keep their existing directory and no installed package or /root state moves; only ids that could never have produced a working mount are rewritten. A rewrite carries a digest of the original id, because ':' -> '_' alone is not injective and would otherwise collapse two chats onto one persistent /root.
This commit is contained in:
@@ -130,6 +130,54 @@ def _sanitize_label_value(value: str) -> str:
|
||||
return cleaned
|
||||
|
||||
|
||||
# A persistent sandbox's host directory is named after task_id, and that name
|
||||
# then becomes the source half of a `-v <source>:<target>` spec. Docker splits
|
||||
# the spec on ':', so a colon-bearing name arrives as extra mount fields and
|
||||
# the run is refused outright ("invalid spec ... too many colons", exit 125).
|
||||
# Path separators would additionally escape the sandbox root.
|
||||
_SANDBOX_DIR_UNSAFE_RE = re.compile(r"[^A-Za-z0-9._-]")
|
||||
_SANDBOX_DIR_MAX_LEN = 128
|
||||
_SANDBOX_DIR_HASH_LEN = 12
|
||||
|
||||
|
||||
def _sandbox_dir_name(task_id: str) -> str:
|
||||
"""Return a bind-mountable directory name for *task_id*'s sandbox.
|
||||
|
||||
Names that are already safe are returned verbatim, so the shared
|
||||
``default`` sandbox and RL/benchmark task ids keep resolving to the
|
||||
directory they have always used — no installed package or ``/root`` state
|
||||
moves. Only ids that could never have produced a working bind mount are
|
||||
rewritten.
|
||||
|
||||
A rewrite also appends a digest of the original id, because the character
|
||||
substitution alone is not injective: ``a:b`` and ``a_b`` would otherwise
|
||||
share one persistent sandbox and leak one session's ``/root`` into
|
||||
another's container. The digest is a pure function of the id, so the same
|
||||
session resolves to the same directory in every process — cross-process
|
||||
container reuse depends on that.
|
||||
"""
|
||||
value = task_id if isinstance(task_id, str) else ""
|
||||
if not value:
|
||||
# An empty component collapses the path onto the docker sandbox root,
|
||||
# which would bind-mount every task's state at once.
|
||||
return "default"
|
||||
|
||||
cleaned = _SANDBOX_DIR_UNSAFE_RE.sub("_", value)
|
||||
if (
|
||||
cleaned == value
|
||||
and len(value) <= _SANDBOX_DIR_MAX_LEN
|
||||
and value not in {".", ".."}
|
||||
# Windows silently strips trailing dots/spaces, aliasing two ids onto
|
||||
# one directory.
|
||||
and not value.endswith((".", " "))
|
||||
):
|
||||
return value
|
||||
|
||||
digest = hashlib.sha256(value.encode("utf-8")).hexdigest()[:_SANDBOX_DIR_HASH_LEN]
|
||||
stem = cleaned[: _SANDBOX_DIR_MAX_LEN - _SANDBOX_DIR_HASH_LEN - 1].strip("._")
|
||||
return f"{stem or 'task'}-{digest}"
|
||||
|
||||
|
||||
def _get_active_profile_name() -> str:
|
||||
"""Return the active Hermes profile name, or ``"default"`` on any error.
|
||||
|
||||
@@ -980,7 +1028,9 @@ class DockerEnvironment(BaseEnvironment):
|
||||
self._home_dir: Optional[str] = None
|
||||
writable_args = []
|
||||
if self._persistent:
|
||||
sandbox = get_sandbox_dir() / "docker" / task_id
|
||||
# _sandbox_dir_name(): a raw session-key task_id carries colons,
|
||||
# which `-v` reads as extra spec fields (exit 125).
|
||||
sandbox = get_sandbox_dir() / "docker" / _sandbox_dir_name(task_id)
|
||||
self._home_dir = str(sandbox / "home")
|
||||
os.makedirs(self._home_dir, exist_ok=True)
|
||||
writable_args.extend([
|
||||
|
||||
Reference in New Issue
Block a user