The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
chore(langchain): fix types in `test_shell_execution_policies` (#34498)
Christophe Bornetb688e36e3822
diff --git a/libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_shell_execution_policies.py b/libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_shell_execution_policies.pyindex f4ef0491d9..4b6c4a5e94 100644--- a/libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_shell_execution_policies.py+++ b/libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_shell_execution_policies.py@@ -2,7 +2,11 @@ from __future__ import annotationsimport osimport shutil+import subprocess+import sys+from pathlib import Pathfrom typing import TYPE_CHECKING, Any+from unittest.mock import Mockimport pytest@@ -14,6 +18,7 @@ from langchain.agents.middleware.shell_tool import ()if TYPE_CHECKING:+ from collections.abc import Callable, Mapping, Sequencefrom pathlib import Path@@ -44,12 +49,9 @@ def _make_resource(def prlimit(self, pid: int, resource_name: int, limits: tuple[int, int]) -> None:self.prlimit_calls.append((pid, resource_name, limits))- else:+ return _Resource()- class _Resource(_BaseResource):- pass-- return _Resource()+ return _BaseResource()def test_host_policy_validations() -> None:@@ -72,20 +74,24 @@ def test_host_policy_requires_resource_for_limits(monkeypatch: pytest.MonkeyPatcdef test_host_policy_applies_prlimit(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:fake_resource = _make_resource(with_prlimit=True)monkeypatch.setattr(_execution, "resource", fake_resource, raising=False)- monkeypatch.setattr(_execution.sys, "platform", "linux")+ monkeypatch.setattr(sys, "platform", "linux")recorded: dict[str, Any] = {}- class DummyProcess:- pid = 1234-- def fake_launch(command, *, env, cwd, preexec_fn, start_new_session):+ def fake_launch(+ command: Sequence[str],+ *,+ env: Mapping[str, str],+ cwd: Path,+ preexec_fn: Callable[[], None] | None,+ start_new_session: bool,+ ) -> subprocess.Popen[str]:recorded["command"] = list(command)recorded["env"] = dict(env)recorded["cwd"] = cwdrecorded["preexec_fn"] = preexec_fnrecorded["start_new_session"] = start_new_session- return DummyProcess()+ return Mock(spec=subprocess.Popen, pid=1234)monkeypatch.setattr(_execution, "_launch_subprocess", fake_launch)@@ -106,17 +112,16 @@ def test_host_policy_applies_prlimit(monkeypatch: pytest.MonkeyPatch, tmp_path:def test_host_policy_uses_preexec_on_macos(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:fake_resource = _make_resource(with_prlimit=False)monkeypatch.setattr(_execution, "resource", fake_resource, raising=False)- monkeypatch.setattr(_execution.sys, "platform", "darwin")+ monkeypatch.setattr(sys, "platform", "darwin")captured: dict[str, Any] = {}- class DummyProcess:- pid = 4321-- def fake_launch(command, *, env, cwd, preexec_fn, start_new_session):+ def fake_launch(+ *_args: Any, preexec_fn: Callable[[], None] | None, start_new_session: bool, **_kwargs: Any+ ) -> subprocess.Popen[str]:captured["preexec_fn"] = preexec_fncaptured["start_new_session"] = start_new_session- return DummyProcess()+ return Mock(spec=subprocess.Popen, pid=4321)monkeypatch.setattr(_execution, "_launch_subprocess", fake_launch)@@ -141,16 +146,13 @@ def test_host_policy_respects_process_group_flag() -> None:fake_resource = _make_resource(with_prlimit=True)monkeypatch.setattr(_execution, "resource", fake_resource, raising=False)- monkeypatch.setattr(_execution.sys, "platform", "linux")+ monkeypatch.setattr(sys, "platform", "linux")recorded: dict[str, Any] = {}- class DummyProcess:- pid = 1111-- def fake_launch(command, *, env, cwd, preexec_fn, start_new_session):+ def fake_launch(*_args: Any, start_new_session: bool, **_kwargs: Any) -> subprocess.Popen[str]:recorded["start_new_session"] = start_new_session- return DummyProcess()+ return Mock(spec=subprocess.Popen, pid=1111)monkeypatch.setattr(_execution, "_launch_subprocess", fake_launch)@@ -166,13 +168,10 @@ def test_host_policy_falls_back_to_rlimit_data() -> None:fake_resource = _make_resource(with_prlimit=True, has_rlimit_as=False)monkeypatch.setattr(_execution, "resource", fake_resource, raising=False)- monkeypatch.setattr(_execution.sys, "platform", "linux")+ monkeypatch.setattr(sys, "platform", "linux")- class DummyProcess:- pid = 2222-- def fake_launch(command, *, env, cwd, preexec_fn, start_new_session):- return DummyProcess()+ def fake_launch(*_args: Any, **_kwargs: Any) -> subprocess.Popen[str]:+ return Mock(spec=subprocess.Popen, pid=2222)monkeypatch.setattr(_execution, "_launch_subprocess", fake_launch)@@ -190,19 +189,23 @@ def test_host_policy_falls_back_to_rlimit_data(shutil.which("codex") is None,reason="codex CLI not available on PATH",)-def test_codex_policy_spawns_codex_cli(monkeypatch, tmp_path: Path) -> None:+def test_codex_policy_spawns_codex_cli(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:recorded: dict[str, list[str]] = {}- class DummyProcess:- pass-- def fake_launch(command, *, env, cwd, preexec_fn, start_new_session):+ def fake_launch(+ command: Sequence[str],+ *,+ env: Mapping[str, str],+ cwd: Path,+ preexec_fn: Callable[[], None] | None,+ start_new_session: bool,+ ) -> subprocess.Popen[str]:recorded["command"] = list(command)assert cwd == tmp_pathassert env["TEST_VAR"] == "1"assert preexec_fn is Noneassert not start_new_session- return DummyProcess()+ return Mock()monkeypatch.setattr("langchain.agents.middleware._execution._launch_subprocess",@@ -229,26 +232,26 @@ def test_codex_policy_spawns_codex_cli(monkeypatch, tmp_path: Path) -> None:def test_codex_policy_auto_platform_linux(monkeypatch: pytest.MonkeyPatch) -> None:- monkeypatch.setattr(_execution.sys, "platform", "linux")+ monkeypatch.setattr(sys, "platform", "linux")policy = CodexSandboxExecutionPolicy(platform="auto")assert policy._determine_platform() == "linux"def test_codex_policy_auto_platform_macos(monkeypatch: pytest.MonkeyPatch) -> None:- monkeypatch.setattr(_execution.sys, "platform", "darwin")+ monkeypatch.setattr(sys, "platform", "darwin")policy = CodexSandboxExecutionPolicy(platform="auto")assert policy._determine_platform() == "macos"def test_codex_policy_resolve_missing_binary(monkeypatch: pytest.MonkeyPatch) -> None:- monkeypatch.setattr(_execution.shutil, "which", lambda _: None)+ monkeypatch.setattr(shutil, "which", lambda _: None)policy = CodexSandboxExecutionPolicy(binary="codex")with pytest.raises(RuntimeError):policy._resolve_binary()def test_codex_policy_auto_platform_failure(monkeypatch: pytest.MonkeyPatch) -> None:- monkeypatch.setattr(_execution.sys, "platform", "win32")+ monkeypatch.setattr(sys, "platform", "win32")policy = CodexSandboxExecutionPolicy(platform="auto")with pytest.raises(RuntimeError):policy._determine_platform()@@ -266,7 +269,7 @@ def test_codex_policy_formats_override_values() -> None:def test_codex_policy_sorts_config_overrides(monkeypatch: pytest.MonkeyPatch) -> None:- monkeypatch.setattr(_execution.shutil, "which", lambda _: "/usr/bin/codex")+ monkeypatch.setattr(shutil, "which", lambda _: "/usr/bin/codex")policy = CodexSandboxExecutionPolicy(config_overrides={"b": 2, "a": 1},platform="linux",@@ -281,18 +284,22 @@ def test_codex_policy_sorts_config_overrides(monkeypatch: pytest.MonkeyPatch) ->shutil.which("docker") is None,reason="docker CLI not available on PATH",)-def test_docker_policy_spawns_docker_run(monkeypatch, tmp_path: Path) -> None:+def test_docker_policy_spawns_docker_run(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:recorded: dict[str, list[str]] = {}- class DummyProcess:- pass-- def fake_launch(command, *, env, cwd, preexec_fn, start_new_session):+ def fake_launch(+ command: Sequence[str],+ *,+ env: Mapping[str, str],+ cwd: Path,+ start_new_session: bool,+ **_kwargs: Any,+ ) -> subprocess.Popen[str]:recorded["command"] = list(command)assert cwd == tmp_pathassert "PATH" in env # host environment should retain system PATHassert not start_new_session- return DummyProcess()+ return Mock()monkeypatch.setattr("langchain.agents.middleware._execution._launch_subprocess",@@ -335,17 +342,14 @@ def test_docker_policy_validates_memory() -> None:def test_docker_policy_skips_mount_for_temp_workspace(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:- monkeypatch.setattr(_execution.shutil, "which", lambda _: "/usr/bin/docker")+ monkeypatch.setattr(shutil, "which", lambda _: "/usr/bin/docker")recorded: dict[str, list[str]] = {}- class DummyProcess:- pass-- def fake_launch(command, *, env, cwd, preexec_fn, start_new_session):+ def fake_launch(command: Sequence[str], *, cwd: Path, **_kwargs: Any) -> subprocess.Popen[str]:recorded["command"] = list(command)assert cwd == workspace- return DummyProcess()+ return Mock()monkeypatch.setattr(_execution, "_launch_subprocess", fake_launch)@@ -377,16 +381,13 @@ def test_docker_policy_validates_user() -> None:def test_docker_policy_read_only_and_user(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:- monkeypatch.setattr(_execution.shutil, "which", lambda _: "/usr/bin/docker")+ monkeypatch.setattr(shutil, "which", lambda _: "/usr/bin/docker")recorded: dict[str, list[str]] = {}- class DummyProcess:- pass-- def fake_launch(command, *, env, cwd, preexec_fn, start_new_session):+ def fake_launch(command: Sequence[str], **_kwargs: Any) -> subprocess.Popen[str]:recorded["command"] = list(command)- return DummyProcess()+ return Mock()monkeypatch.setattr(_execution, "_launch_subprocess", fake_launch)@@ -402,7 +403,7 @@ def test_docker_policy_read_only_and_user(monkeypatch: pytest.MonkeyPatch, tmp_pdef test_docker_policy_resolve_missing_binary(monkeypatch: pytest.MonkeyPatch) -> None:- monkeypatch.setattr(_execution.shutil, "which", lambda _: None)+ monkeypatch.setattr(shutil, "which", lambda _: None)policy = DockerExecutionPolicy()with pytest.raises(RuntimeError):policy._resolve_binary()