The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(tools): share the task-id path sanitizer across backends; cover singularity overlays
Teknium410b1ec5559d
diff --git a/tools/environments/base.py b/tools/environments/base.pyindex 460c5eaee32..184d44cca0f 100644--- a/tools/environments/base.py+++ b/tools/environments/base.py@@ -7,6 +7,7 @@ or a temp file (local)."""import codecs+import hashlibimport jsonimport loggingimport os@@ -294,6 +295,58 @@ def get_sandbox_dir() -> Path:return p+# 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) or a+# writable-overlay directory (Singularity). 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" / "invalid mode", exit 125).+# Path separators would additionally escape the sandbox root, and Windows+# forbids ':' in path segments entirely.+_SANDBOX_DIR_UNSAFE_RE = re.compile(r"[^A-Za-z0-9._-]")+_SANDBOX_DIR_MAX_LEN = 128+_SANDBOX_DIR_HASH_LEN = 12+++def sanitize_task_id_for_path(task_id: str) -> str:+ """Return a bind-mountable directory name for *task_id*'s sandbox.++ Shared by every environment backend that turns a task id into a host+ filesystem path component (Docker persistent sandboxes, Singularity+ persistent overlays). 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 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}"++# ---------------------------------------------------------------------------# Shared constants and utilities# ---------------------------------------------------------------------------diff --git a/tools/environments/docker.py b/tools/environments/docker.pyindex 7d312ae97f1..eeb38318b75 100644--- a/tools/environments/docker.py+++ b/tools/environments/docker.py@@ -22,6 +22,7 @@ from tools.environments.base import (BaseEnvironment,EnvironmentConnectionError,_popen_bash,+ sanitize_task_id_for_path,)from tools.environments.local import (_HERMES_PROVIDER_ENV_BLOCKLIST,@@ -130,52 +131,11 @@ 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}"+# The task_id -> host-directory-name mapping is shared with every backend+# that persists per-task state on the host filesystem (Singularity overlays+# use the same helper), so the whole bug class is fixed in one place:+# tools.environments.base.sanitize_task_id_for_path.+_sandbox_dir_name = sanitize_task_id_for_pathdef _get_active_profile_name() -> str:diff --git a/tools/environments/singularity.py b/tools/environments/singularity.pyindex 0ea92c43b7e..4c2dce53bac 100644--- a/tools/environments/singularity.py+++ b/tools/environments/singularity.py@@ -20,6 +20,7 @@ from tools.environments.base import (_load_json_store,_popen_bash,_save_json_store,+ sanitize_task_id_for_path,)logger = logging.getLogger(__name__)@@ -191,7 +192,11 @@ class SingularityEnvironment(BaseEnvironment):if self._persistent:overlay_base = _get_scratch_dir() / "hermes-overlays"overlay_base.mkdir(parents=True, exist_ok=True)- self._overlay_dir = overlay_base / f"overlay-{task_id}"+ # A raw session-key task_id carries colons and other characters+ # that are unsafe in host path components (same class of bug as+ # the docker -v mount failure); route it through the shared+ # sanitizer so all backends agree on the mapping.+ self._overlay_dir = overlay_base / f"overlay-{sanitize_task_id_for_path(task_id)}"self._overlay_dir.mkdir(parents=True, exist_ok=True)self._start_instance()