The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor: cache probe verdicts on exit-0 / capability rejection only, trim tests
teknium116ae9cfccb33
diff --git a/tests/tools/test_docker_cgroup_limits.py b/tests/tools/test_docker_cgroup_limits.pyindex 970f99ef2de..eb411e09ca3 100644--- a/tests/tools/test_docker_cgroup_limits.py+++ b/tests/tools/test_docker_cgroup_limits.py@@ -63,69 +63,10 @@ def test_probe_result_is_cached(monkeypatch):assert len(calls) == 1 # probe runs once, then cached-def test_probe_timeout_is_not_cached_and_retried(monkeypatch):- """A transient probe failure (auto-pull of an uncached image exceeding the- 60s timeout, daemon cold-start) must not disable resource limits for the- process lifetime. The spawn degrades but the next spawn re-probes."""- monkeypatch.setattr(docker_env, "find_docker", lambda: "/usr/bin/docker")- calls = []-- def _run(cmd, *a, **k):- calls.append(cmd)- if len(calls) == 1:- raise subprocess.TimeoutExpired(cmd, 60)- return subprocess.CompletedProcess(cmd, 0, stdout="", stderr="")-- monkeypatch.setattr(docker_env.subprocess, "run", _run)- assert docker_env._cgroup_limits_available("img") is False- assert docker_env._cgroup_limits_available("img") is True- assert len(calls) == 2---def test_probe_non_cgroup_failure_is_not_cached(monkeypatch):- """A nonzero probe exit for reasons unrelated to cgroups (manifest/pull- error, daemon error) is not a definitive negative, so it is retried on the- next spawn."""- monkeypatch.setattr(docker_env, "find_docker", lambda: "/usr/bin/docker")- calls = []-- def _run(cmd, *a, **k):- calls.append(cmd)- if len(calls) == 1:- return subprocess.CompletedProcess(- cmd, 125, stdout="", stderr="docker: Error response from daemon: "- "manifest for hermes-agent:latest not found: manifest unknown")- return subprocess.CompletedProcess(cmd, 0, stdout="", stderr="")-- monkeypatch.setattr(docker_env.subprocess, "run", _run)- assert docker_env._cgroup_limits_available("img") is False- assert docker_env._cgroup_limits_available("img") is True- assert len(calls) == 2---def test_probe_pull_error_naming_cgroup_is_not_cached(monkeypatch):- """An image whose NAME contains 'cgroup' produces a pull error mentioning it;- that must not be read as a definitive cgroup rejection."""- monkeypatch.setattr(docker_env, "find_docker", lambda: "/usr/bin/docker")- calls = []-- def _run(cmd, *a, **k):- calls.append(cmd)- if len(calls) == 1:- return subprocess.CompletedProcess(- cmd, 125, stdout="", stderr="docker: Error response from daemon: "- "pull access denied for cgroup-tools, repository does not exist")- return subprocess.CompletedProcess(cmd, 0, stdout="", stderr="")-- monkeypatch.setattr(docker_env.subprocess, "run", _run)- assert docker_env._cgroup_limits_available("cgroup-tools") is False- assert docker_env._cgroup_limits_available("cgroup-tools") is True- assert len(calls) == 2--def test_probe_definitive_cgroup_failure_is_cached(monkeypatch):"""A daemon rejection that names cgroups IS a host property; caching it is- the point of the probe, so subsequent spawns do not pay it again."""+ the point of the probe, so subsequent spawns do not pay it again (#116162+ only stops caching failures that say nothing about cgroups)."""monkeypatch.setattr(docker_env, "find_docker", lambda: "/usr/bin/docker")calls = []@@ -142,78 +83,30 @@ def test_probe_definitive_cgroup_failure_is_cached(monkeypatch):assert len(calls) == 1-def test_missing_docker_is_not_cached(monkeypatch):- """find_docker() missing says nothing about cgroup support, so it is not cached."""- monkeypatch.setattr(docker_env, "find_docker", lambda: None)- assert docker_env._cgroup_limits_available("img") is False-+ "info, create, cached",+ [+ # `docker info` failing (daemon cold-start) is transient: not "not overlay2".+ ((1, "", "Cannot connect to the Docker daemon"), None, None),+ # The daemon rejecting --storage-opt is a host property: cached False.+ ((0, "overlay2\n", ""), (125, "", "Error response from daemon: --storage-opt is "+ "supported only for overlay2 with pquota"), False),+ # A create failure that never reached the storage check (pull error) is transient.+ ((0, "overlay2\n", ""), (125, "", "Unable to find image 'hello-world:latest'"), None),+ ],+)+def test_storage_opt_probe_caches_only_definitive_answers(monkeypatch, info, create, cached):+ """``_storage_opt_supported`` must not latch disk quota off for the process on a+ failure that says nothing about pquota support (#116162)."""monkeypatch.setattr(docker_env, "find_docker", lambda: "/usr/bin/docker")- monkeypatch.setattr(docker_env.subprocess, "run",- lambda cmd, *a, **k: subprocess.CompletedProcess(cmd, 0, stdout="", stderr=""))- assert docker_env._cgroup_limits_available("img") is True---class TestStorageOptProbeCaching:- """``_storage_opt_supported`` has the same transient-vs-definitive contract:- only a real answer (driver name, or a daemon rejection naming storage) may- be cached process-wide; timeouts and pull/daemon failures must retry."""-- def _fake_run(self, calls, info=None, create=None, info_exc=None):- def _run(cmd, *a, **k):- calls.append(cmd)- sub = cmd[1] if isinstance(cmd, list) and len(cmd) > 1 else ""- if sub == "info":- if info_exc:- raise info_exc- rc, stdout, stderr = info- return subprocess.CompletedProcess(cmd, rc, stdout=stdout, stderr=stderr)- if sub == "create":- rc, stdout, stderr = create- return subprocess.CompletedProcess(cmd, rc, stdout=stdout, stderr=stderr)- return subprocess.CompletedProcess(cmd, 0, stdout="", stderr="")- return _run-- def test_info_failure_not_cached(self, monkeypatch):- monkeypatch.setattr(docker_env, "find_docker", lambda: "/usr/bin/docker")- calls = []- monkeypatch.setattr(docker_env.subprocess, "run", self._fake_run(- calls, info=(1, "", "Cannot connect to the Docker daemon")))-- assert docker_env.DockerEnvironment._storage_opt_supported() is False- monkeypatch.setattr(docker_env.subprocess, "run", self._fake_run(- calls, info=(0, "overlay2\n", ""), create=(0, "cid123\n", "")))- assert docker_env.DockerEnvironment._storage_opt_supported() is True-- def test_info_exception_not_cached(self, monkeypatch):- monkeypatch.setattr(docker_env, "find_docker", lambda: "/usr/bin/docker")- calls = []- monkeypatch.setattr(docker_env.subprocess, "run", self._fake_run(- calls, info_exc=subprocess.TimeoutExpired("docker", 10)))- assert docker_env.DockerEnvironment._storage_opt_supported() is False- assert docker_env._storage_opt_ok is None-- def test_unsupported_create_error_cached(self, monkeypatch):- monkeypatch.setattr(docker_env, "find_docker", lambda: "/usr/bin/docker")- calls = []- monkeypatch.setattr(docker_env.subprocess, "run", self._fake_run(- calls, info=(0, "overlay2\n", ""),- create=(125, "", "Error response from daemon: --storage-opt is "- "supported only for overlay2 with pquota")))- assert docker_env.DockerEnvironment._storage_opt_supported() is False- assert docker_env.DockerEnvironment._storage_opt_supported() is False- assert len(calls) == 2 # info + create once; cached thereafter-- def test_create_pull_failure_not_cached(self, monkeypatch):- """A create-probe failure that never reached the storage check (image- pull error) is transient; retried, not latched."""- monkeypatch.setattr(docker_env, "find_docker", lambda: "/usr/bin/docker")- calls = []- monkeypatch.setattr(docker_env.subprocess, "run", self._fake_run(- calls, info=(0, "overlay2\n", ""),- create=(125, "", "Unable to find image 'hello-world:latest'")))- assert docker_env.DockerEnvironment._storage_opt_supported() is False- assert docker_env._storage_opt_ok is None+ def _run(cmd, *a, **k):+ rc, out, err = info if cmd[1] == "info" else create+ return subprocess.CompletedProcess(cmd, rc, stdout=out, stderr=err)++ monkeypatch.setattr(docker_env.subprocess, "run", _run)+ assert docker_env.DockerEnvironment._storage_opt_supported() is False+ assert docker_env._storage_opt_ok is cacheddef test_transient_probe_failure_recovers_on_next_spawn(monkeypatch):diff --git a/tools/environments/docker.py b/tools/environments/docker.pyindex b1bb2bb8ffd..1238a219322 100644--- a/tools/environments/docker.py+++ b/tools/environments/docker.py@@ -365,19 +365,6 @@ def _resolve_host_user_spec() -> Optional[str]:_storage_opt_ok: Optional[bool] = None # cached result across instances_cgroup_limits_ok: Optional[bool] = None # cached result across instances-# stderr fragments meaning the probe container/image never ran, so the failure says-# nothing about the capability being probed. Checked BEFORE capability keywords: an-# image named e.g. "cgroup-tools" produces a pull error mentioning "cgroup", which-# must not be read as a cgroup rejection.-_PROBE_NONDETERMINATE_STDERR = (- "unable to find image", "manifest", "pull access denied", "pulling fs layer",- "toomanyrequests", "failed to resolve reference", "error pulling",- "cannot connect to the docker daemon", "connection refused", "context deadline exceeded")---def _probe_stderr_indeterminate(stderr: str) -> bool:- return any(s in stderr for s in _PROBE_NONDETERMINATE_STDERR)-def _cgroup_limits_available(image: str) -> bool:"""Probe once per process whether ``--cpus``/``--memory``/``--pids-limit`` work here, via a@@ -407,21 +394,21 @@ def _cgroup_limits_available(image: str) -> bool:_cgroup_limits_ok = Truereturn Truestderr = (result.stderr or "").strip()- stderr_l = stderr.lower()- if "cgroup" in stderr_l and not _probe_stderr_indeterminate(stderr_l):- _cgroup_limits_ok = False- logger.warning(- "Cgroup resource limits (--cpus/--memory/--pids-limit) not "- "available in this environment. Containers will run without "- "CPU, memory or PID limits. To enable, delegate the cpu, "- "memory and pids cgroup controllers to this container. Probe stderr: %s",- stderr[:500])- else:+ if "cgroup" not in stderr.lower():+ # Pull/manifest/daemon errors say nothing about cgroup support: not cached.logger.warning("Cgroup limit probe could not determine support (docker exited %d: %s). ""Containers run without CPU/memory/PID limits until a probe succeeds.",result.returncode, stderr[:500])- return _cgroup_limits_ok or False+ return False+ _cgroup_limits_ok = False+ logger.warning(+ "Cgroup resource limits (--cpus/--memory/--pids-limit) not "+ "available in this environment. Containers will run without "+ "CPU, memory or PID limits. To enable, delegate the cpu, "+ "memory and pids cgroup controllers to this container. Probe stderr: %s",+ stderr[:500])+ return Falsedef _docker_unavailable(log_msg: str, *log_args, error: str, hint: str, exc_info: bool = False):@@ -999,10 +986,8 @@ class DockerEnvironment(BaseEnvironment):if probe.stdout.strip():subprocess.run([docker, "rm", probe.stdout.strip()],capture_output=True, timeout=5, stdin=subprocess.DEVNULL)- elif not _probe_stderr_indeterminate((probe.stderr or "").lower()) and \- any(s in (probe.stderr or "").lower()- for s in ("storage", "quota", "overlay2")):- _storage_opt_ok = False # daemon said storage-opt is unsupported; host property+ elif "storage" in (probe.stderr or "").lower():+ _storage_opt_ok = False # daemon rejected --storage-opt: a host property# else: pull/daemon failure unrelated to storage-opt; not cached, retried next spawnexcept Exception:return False # TimeoutExpired, missing binary; transient, retried next spawn