tree_agent.md · 4.5 KB

Tree Agent Documentation

Overview

The TreeAgent provides secure directory navigation and file system exploration capabilities for mindX agents. It enables agents to explore directory structures and find files within a sandboxed root path, ensuring safe file system operations.

File: tools/tree_agent.py Class: TreeAgent Version: 1.0.0 Status: ✅ Active

Architecture

Design Principles

  • Sandboxed Execution: All commands are restricted to a specified root path
  • Command Whitelisting: Only allows safe commands (ls and find)
  • Path Security: Prevents directory traversal attacks
  • Process Logging: Logs all operations for auditing
  • Core Components

    class TreeAgent(BaseTool):
        - root_path: Path - Sandboxed root directory
        - shell: ShellCommandTool - For executing commands
        - memory_agent: MemoryAgent - For logging operations
    

    Usage

    Basic Directory Listing

    from agents.tree_agent import TreeAgent

    agent = TreeAgent(root_path="/home/user/project")

    List directory contents

    result = await agent.execute("ls -la")

    File Search

    # Find files matching pattern
    result = await agent.execute("find . -name '.py'")
    

    Security Features

    1. Command Whitelisting

    Only allows specific safe commands:

  • ls: List directory contents
  • find: Search for files
  • All other commands are rejected with an error message.

    2. Sandboxed Root Path

    All commands are executed relative to the specified root path:

  • Prevents access to files outside the sandbox
  • Ensures agents can only explore designated areas
  • Protects system files and sensitive directories
  • 3. Path Construction

    Commands are automatically prefixed with the root path:

    # User command: "find . -name file.txt"
    

    Executed as: "find /root/path . -name file.txt"

    Limitations

    Current Limitations

  • Limited Commands: Only supports ls and find
  • No Recursive Operations: Limited recursive capabilities
  • No File Operations: Cannot read, write, or modify files
  • No Metadata: Doesn't provide file metadata (size, dates, etc.)
  • Basic Error Handling: Minimal error context
  • Recommended Improvements

  • More Commands: Support tree, stat, file commands
  • File Metadata: Return file size, modification dates, permissions
  • Pattern Matching: Enhanced pattern matching capabilities
  • Recursive Depth Limits: Configurable recursion depth
  • Result Formatting: Structured output (JSON) instead of raw text
  • Caching: Cache directory listings for performance
  • Integration

    With Memory Agent

    All operations are logged to the memory agent:

    await self.memory_agent.log_process(
        process_name='tree_agent_execution',
        data={'command': full_command, 'success': success, 'result': result},
        metadata={'agent_id': self.bdi_agent_ref.agent_id, 'tool_id': 'tree_agent'}
    )
    

    With Shell Command Tool

    Uses ShellCommandTool internally for command execution, inheriting its security and error handling features.

    Examples

    List Current Directory

    result = await agent.execute("ls")
    

    Returns: List of files and directories

    Find Python Files

    result = await agent.execute("find . -name '.py'")
    

    Returns: List of Python files

    Find by Pattern

    result = await agent.execute("find . -type f -name 'test_*.py'")
    

    Returns: Test files matching pattern

    Technical Details

    Dependencies

  • tools.shell_command_tool.ShellCommandTool: For command execution
  • core.bdi_agent.BaseTool: Base tool class
  • agents.memory_agent.MemoryAgent: For operation logging
  • Command Processing

    # Validate command
    if not command.startswith("ls") and not command.startswith("find"):
        return "Error: Only 'ls' and 'find' commands are allowed."

    Construct sandboxed command

    parts = command.split(" ") cmd_base = parts[0] cmd_args = " ".join(parts[1:]) full_command = f"{cmd_base} {self.root_path} {cmd_args}"

    Execute via shell tool

    success, result = await self.shell.execute(command=full_command)

    Future Enhancements

  • Structured Output: Return JSON-structured results
  • File Metadata: Include file size, dates, permissions
  • Advanced Filtering: Support for complex filters
  • Directory Tree Visualization: Generate tree structures
  • Performance Optimization: Cache directory listings
  • Multi-Path Support: Support multiple root paths
  • Virtual File System: Support for virtual file systems

  • All DocumentsDocument IndexThe Book of mindXImprovement JournalAPI Reference