Skip to content

The agent that grows with you

test(tools): shared sanitizer contract + singularity overlay coverage

Tekniumec44116d596d
diff --git a/tests/tools/test_base_environment.py b/tests/tools/test_base_environment.py
index 3d875598d6f..cb883ea5c48 100644
--- a/tests/tools/test_base_environment.py
+++ b/tests/tools/test_base_environment.py
@@ -405,3 +405,68 @@ class TestCwdMarker:
env1 = _TestableEnv()
env2 = _TestableEnv()
assert env1._cwd_marker != env2._cwd_marker
+
+
+class TestSanitizeTaskIdForPath:
+ """sanitize_task_id_for_path must yield mountable, collision-free segments.
+
+ A raw task id like ``session:agent:main:telegram:dm:12345`` used as a
+ sandbox directory name made docker -v split the bind-mount on the embedded
+ colons and the daemon rejected it with "invalid mode" / exit 125 (#92414).
+ The helper is shared by every backend that builds host paths from task_id
+ (docker persistent sandboxes, singularity overlays), fixing the class once.
+ """
+
+ def test_docker_unsafe_characters_are_replaced(self):
+ from tools.environments.base import sanitize_task_id_for_path
+
+ out = sanitize_task_id_for_path("session:agent:main:telegram:dm:12345")
+ assert ":" not in out
+ assert "/" not in out and "\\" not in out
+
+ def test_safe_ids_pass_through_verbatim(self):
+ """Existing sandboxes keep resolving to their current directory."""
+ from tools.environments.base import sanitize_task_id_for_path
+
+ for value in ("default", "task-01.abc_def", "astropy__astropy-12907"):
+ assert sanitize_task_id_for_path(value) == value
+
+ def test_deterministic_and_collision_free_for_distinct_inputs(self):
+ from tools.environments.base import sanitize_task_id_for_path
+
+ assert sanitize_task_id_for_path("a:b") == sanitize_task_id_for_path("a:b")
+ # substitution alone is not injective — the digest must disambiguate
+ assert sanitize_task_id_for_path("a:b") != sanitize_task_id_for_path("a_b")
+ assert sanitize_task_id_for_path("!!!") != sanitize_task_id_for_path("@@@")
+
+ def test_empty_and_traversal_inputs_are_neutralized(self):
+ from tools.environments.base import sanitize_task_id_for_path
+
+ assert sanitize_task_id_for_path("") == "default"
+ for value in (".", "..", "../../etc", "..\\..\\escape"):
+ out = sanitize_task_id_for_path(value)
+ assert out not in {".", ".."}
+ assert "/" not in out and "\\" not in out
+
+ def test_oversized_input_truncates_with_unique_digest(self):
+ from tools.environments.base import (
+ _SANDBOX_DIR_MAX_LEN,
+ sanitize_task_id_for_path,
+ )
+
+ long_a = "a" * 300 + ":1"
+ long_b = "a" * 300 + ":2"
+ out_a = sanitize_task_id_for_path(long_a)
+ out_b = sanitize_task_id_for_path(long_b)
+ assert len(out_a) <= _SANDBOX_DIR_MAX_LEN
+ assert ":" not in out_a
+ assert out_a != out_b
+
+ def test_sanitized_dir_is_creatable(self, tmp_path):
+ from tools.environments.base import sanitize_task_id_for_path
+
+ target = tmp_path / "docker" / sanitize_task_id_for_path(
+ "session:agent:main:telegram:dm:12345"
+ )
+ target.mkdir(parents=True)
+ assert target.is_dir()
diff --git a/tests/tools/test_singularity_persistent_overlay.py b/tests/tools/test_singularity_persistent_overlay.py
new file mode 100644
index 00000000000..b17995ccab2
--- /dev/null
+++ b/tests/tools/test_singularity_persistent_overlay.py
@@ -0,0 +1,80 @@
+"""Persistent-overlay path safety for the Singularity environment.
+
+A raw task id used as an overlay directory name carries the same class of
+bug as the Docker persistent-sandbox mount path (#92414): colon-bearing
+session ids break bind specs and are unrepresentable on Windows. Overlay
+components must go through the shared sanitizer in tools.environments.base.
+"""
+
+from tools.environments import singularity as singularity_env
+from tools.environments.base import sanitize_task_id_for_path
+
+
+def _stub_singularity(monkeypatch, tmp_path):
+ monkeypatch.setattr(
+ singularity_env, "_ensure_singularity_available", lambda: "/usr/bin/apptainer"
+ )
+ monkeypatch.setattr(
+ singularity_env,
+ "_get_or_build_sif",
+ lambda image, executable="apptainer": str(tmp_path / "image.sif"),
+ )
+ monkeypatch.setattr(singularity_env, "_get_scratch_dir", lambda: tmp_path / "scratch")
+ monkeypatch.setattr(
+ singularity_env.SingularityEnvironment, "_start_instance", lambda self: None
+ )
+ monkeypatch.setattr(
+ singularity_env.SingularityEnvironment, "init_session", lambda self: None
+ )
+
+
+def test_persistent_overlay_dir_sanitizes_colon_in_task_id(monkeypatch, tmp_path):
+ _stub_singularity(monkeypatch, tmp_path)
+
+ task_id = "session:agent:main:telegram:dm:12345"
+ env = singularity_env.SingularityEnvironment(
+ image="python:3.11",
+ persistent_filesystem=True,
+ task_id=task_id,
+ )
+
+ assert env._overlay_dir is not None
+ assert env._overlay_dir.name == f"overlay-{sanitize_task_id_for_path(task_id)}"
+ assert ":" not in env._overlay_dir.name
+ assert env._overlay_dir.is_dir()
+
+
+def test_persistent_overlay_dir_keeps_safe_task_ids_verbatim(monkeypatch, tmp_path):
+ """Existing overlays for already-safe ids must keep resolving to the same
+ directory — renaming would strand a user's persistent overlay state."""
+ _stub_singularity(monkeypatch, tmp_path)
+
+ env = singularity_env.SingularityEnvironment(
+ image="python:3.11",
+ persistent_filesystem=True,
+ task_id="default",
+ )
+
+ assert env._overlay_dir is not None
+ assert env._overlay_dir.name == "overlay-default"
+
+
+def test_distinct_session_keys_get_distinct_overlay_dirs(monkeypatch, tmp_path):
+ """Sanitization must stay injective across backends: two ids that differ
+ only in colon placement must not share one overlay."""
+ _stub_singularity(monkeypatch, tmp_path)
+
+ names = set()
+ for task_id in (
+ "session:agent:main:telegram:dm:111",
+ "session_agent_main_telegram_dm_111",
+ ):
+ env = singularity_env.SingularityEnvironment(
+ image="python:3.11",
+ persistent_filesystem=True,
+ task_id=task_id,
+ )
+ assert env._overlay_dir is not None
+ names.add(env._overlay_dir.name)
+
+ assert len(names) == 2, f"overlay dirs collided: {names}"