Home / File/ test_shell_tool.py — langchain Source File

test_shell_tool.py — langchain Source File

Architecture documentation for test_shell_tool.py, a python file in the langchain codebase. 10 imports, 0 dependents.

File python LangChainCore ApiManagement 10 imports 26 functions 1 classes

Entity Profile

Dependency Diagram

graph LR
  f21fd460_1d0c_cb92_cfa1_eae0890e2f58["test_shell_tool.py"]
  391f4e32_cbb7_c983_c4f1_760073391eef["gc"]
  f21fd460_1d0c_cb92_cfa1_eae0890e2f58 --> 391f4e32_cbb7_c983_c4f1_760073391eef
  fae55357_8e00_f092_b2c8_957d1841a180["tempfile"]
  f21fd460_1d0c_cb92_cfa1_eae0890e2f58 --> fae55357_8e00_f092_b2c8_957d1841a180
  996b2db9_46dd_901f_f7eb_068bafab4b12["time"]
  f21fd460_1d0c_cb92_cfa1_eae0890e2f58 --> 996b2db9_46dd_901f_f7eb_068bafab4b12
  927570d8_11a6_5c17_0f0d_80baae0c733e["pathlib"]
  f21fd460_1d0c_cb92_cfa1_eae0890e2f58 --> 927570d8_11a6_5c17_0f0d_80baae0c733e
  feec1ec4_6917_867b_d228_b134d0ff8099["typing"]
  f21fd460_1d0c_cb92_cfa1_eae0890e2f58 --> feec1ec4_6917_867b_d228_b134d0ff8099
  f69d6389_263d_68a4_7fbf_f14c0602a9ba["pytest"]
  f21fd460_1d0c_cb92_cfa1_eae0890e2f58 --> f69d6389_263d_68a4_7fbf_f14c0602a9ba
  9444498b_8066_55c7_b3a2_1d90c4162a32["langchain_core.messages"]
  f21fd460_1d0c_cb92_cfa1_eae0890e2f58 --> 9444498b_8066_55c7_b3a2_1d90c4162a32
  95e05501_5650_2f3c_99db_c054e96d4e43["langchain_core.tools.base"]
  f21fd460_1d0c_cb92_cfa1_eae0890e2f58 --> 95e05501_5650_2f3c_99db_c054e96d4e43
  e07f6d54_afcc_052d_d33f_8ccdcc46f752["langgraph.runtime"]
  f21fd460_1d0c_cb92_cfa1_eae0890e2f58 --> e07f6d54_afcc_052d_d33f_8ccdcc46f752
  0f767285_fa0a_afed_9c89_dfda4eacfe4d["langchain.agents.middleware.shell_tool"]
  f21fd460_1d0c_cb92_cfa1_eae0890e2f58 --> 0f767285_fa0a_afed_9c89_dfda4eacfe4d
  style f21fd460_1d0c_cb92_cfa1_eae0890e2f58 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from __future__ import annotations

import gc
import tempfile
import time
from pathlib import Path
from typing import cast

import pytest
from langchain_core.messages import ToolMessage
from langchain_core.tools.base import ToolException
from langgraph.runtime import Runtime

from langchain.agents.middleware.shell_tool import (
    HostExecutionPolicy,
    RedactionRule,
    ShellToolMiddleware,
    ShellToolState,
    _SessionResources,
    _ShellToolInput,
)


def _empty_state() -> ShellToolState:
    return {"messages": []}


def test_executes_command_and_persists_state(tmp_path: Path) -> None:
    workspace = tmp_path / "workspace"
    middleware = ShellToolMiddleware(workspace_root=workspace)
    runtime = Runtime()
    state = _empty_state()
    try:
        updates = middleware.before_agent(state, runtime)
        if updates:
            state.update(cast("ShellToolState", updates))
        resources = middleware._get_or_create_resources(state)

        middleware._run_shell_tool(resources, {"command": "cd /"}, tool_call_id=None)
        result = middleware._run_shell_tool(resources, {"command": "pwd"}, tool_call_id=None)
        assert isinstance(result, str)
        assert result.strip() == "/"
        echo_result = middleware._run_shell_tool(
            resources, {"command": "echo ready"}, tool_call_id=None
        )
        assert "ready" in echo_result
    finally:
        middleware.after_agent(state, runtime)


def test_restart_resets_session_environment(tmp_path: Path) -> None:
    middleware = ShellToolMiddleware(workspace_root=tmp_path / "workspace")
    runtime = Runtime()
    state = _empty_state()
    try:
        updates = middleware.before_agent(state, runtime)
        if updates:
            state.update(cast("ShellToolState", updates))
        resources = middleware._get_or_create_resources(state)

// ... (489 more lines)

Domain

Subdomains

Classes

Dependencies

  • gc
  • langchain.agents.middleware.shell_tool
  • langchain_core.messages
  • langchain_core.tools.base
  • langgraph.runtime
  • pathlib
  • pytest
  • tempfile
  • time
  • typing

Frequently Asked Questions

What does test_shell_tool.py do?
test_shell_tool.py is a source file in the langchain codebase, written in python. It belongs to the LangChainCore domain, ApiManagement subdomain.
What functions are defined in test_shell_tool.py?
test_shell_tool.py defines 26 function(s): _empty_state, test_async_methods_delegate_to_sync, test_empty_output_replaced_with_no_output, test_executes_command_and_persists_state, test_get_or_create_resources_creates_when_missing, test_get_or_create_resources_reuses_existing, test_nonzero_exit_code_returns_error, test_normalize_commands_string_tuple_list, test_normalize_env_coercion, test_normalize_env_non_string_keys, and 16 more.
What does test_shell_tool.py depend on?
test_shell_tool.py imports 10 module(s): gc, langchain.agents.middleware.shell_tool, langchain_core.messages, langchain_core.tools.base, langgraph.runtime, pathlib, pytest, tempfile, and 2 more.
Where is test_shell_tool.py in the architecture?
test_shell_tool.py is located at libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_shell_tool.py (domain: LangChainCore, subdomain: ApiManagement, directory: libs/langchain_v1/tests/unit_tests/agents/middleware/implementations).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free