Class LocalFilesystem

java.lang.Object
io.agentscope.harness.agent.filesystem.local.LocalFilesystem
All Implemented Interfaces:
AbstractFilesystem
Direct Known Subclasses:
LocalFilesystemWithShell

public class LocalFilesystem extends Object implements AbstractFilesystem
AbstractFilesystem implementation that reads and writes files on the local disk.

Path-resolution behaviour is controlled by the LocalFsMode passed at construction:

  • LocalFsMode.SANDBOXED — paths anchored to rootDir, .. and outside-root absolute paths blocked. Equivalent to legacy virtualMode=true.
  • LocalFsMode.ROOTED — absolute paths accepted only when they fall under one of the configured PathPolicy roots; relative paths anchor to rootDir.
  • LocalFsMode.UNRESTRICTED — absolute paths pass through; relative paths anchor to rootDir. Equivalent to legacy virtualMode=false.
  • Constructor Details

    • LocalFilesystem

      public LocalFilesystem(String rootDir)
      Same as LocalFilesystem(Path) with Path.of(path) after String.strip(). Pass null for the same CWD semantics as a null Path. Blank strings are rejected.
      Parameters:
      rootDir - filesystem root as a path string, or null for process working directory
    • LocalFilesystem

      public LocalFilesystem(Path rootDir)
      Creates a abstract filesystem rooted at the given directory.
      Parameters:
      rootDir - root directory for all operations (null means CWD)
    • LocalFilesystem

      public LocalFilesystem(Path rootDir, boolean virtualMode, int maxFileSizeMb)
      Creates a abstract filesystem with explicit configuration.
      Parameters:
      rootDir - root directory for all operations (null means CWD)
      virtualMode - when true, all paths are anchored to rootDir and traversal is blocked
      maxFileSizeMb - maximum file size in megabytes for search operations
    • LocalFilesystem

      public LocalFilesystem(String rootDir, boolean virtualMode, int maxFileSizeMb)
      Same as LocalFilesystem(Path, boolean, int) with a path string; see LocalFilesystem(String) for null / blank rules.
    • LocalFilesystem

      public LocalFilesystem(Path rootDir, boolean virtualMode, int maxFileSizeMb, NamespaceFactory namespaceFactory)
      Legacy constructor: maps virtualMode to LocalFsMode.SANDBOXED or LocalFsMode.UNRESTRICTED and uses an empty PathPolicy. Prefer the mode-aware constructor below when you need LocalFsMode.ROOTED.
      Parameters:
      rootDir - root directory for all operations (null means CWD)
      virtualMode - when true, all paths are anchored to rootDir and traversal is blocked
      maxFileSizeMb - maximum file size in megabytes for search operations
      namespaceFactory - optional namespace factory for path scoping (null for none)
    • LocalFilesystem

      public LocalFilesystem(String rootDir, boolean virtualMode, int maxFileSizeMb, NamespaceFactory namespaceFactory)
      Same as LocalFilesystem(Path, boolean, int, NamespaceFactory) with a path string; see LocalFilesystem(String) for null / blank rules.
    • LocalFilesystem

      public LocalFilesystem(Path rootDir, LocalFsMode mode, PathPolicy pathPolicy, int maxFileSizeMb, NamespaceFactory namespaceFactory)
      Creates a filesystem with explicit mode and path policy.

      The mode controls how the agent's absolute paths are validated; see LocalFsMode for the three options. In LocalFsMode.ROOTED mode, an absolute path is accepted only when it falls under one of the pathPolicy roots or under rootDir itself; in LocalFsMode.SANDBOXED every path is anchored to rootDir; in LocalFsMode.UNRESTRICTED absolute paths pass through unchanged.

      Parameters:
      rootDir - root directory for relative paths (null means CWD)
      mode - path-resolution policy (null defaults to LocalFsMode.UNRESTRICTED)
      pathPolicy - allow-list for LocalFsMode.ROOTED; ignored otherwise (null treated as empty)
      maxFileSizeMb - maximum file size in megabytes for search operations
      namespaceFactory - optional namespace factory for path scoping (null for none)
  • Method Details

    • getCwd

      public Path getCwd()
      Returns the root directory for this filesystem.
    • getMode

      public LocalFsMode getMode()
      Returns the active path-resolution mode.
    • getPathPolicy

      public PathPolicy getPathPolicy()
      Returns the configured path policy. Empty when getMode() is not LocalFsMode.ROOTED; even with an empty policy, LocalFsMode.ROOTED still implicitly accepts paths under getCwd().
    • ls

      public LsResult ls(io.agentscope.core.agent.RuntimeContext runtimeContext, String path)
      Description copied from interface: AbstractFilesystem
      List all files in a directory with metadata.
      Specified by:
      ls in interface AbstractFilesystem
      Parameters:
      runtimeContext - per-call agent runtime; RuntimeContext.empty() when none
      path - absolute path to the directory to list (must start with '/')
      Returns:
      LsResult with directory entries or error
    • read

      public ReadResult read(io.agentscope.core.agent.RuntimeContext runtimeContext, String filePath, int offset, int limit)
      Description copied from interface: AbstractFilesystem
      Read file content with optional line-based pagination.
      Specified by:
      read in interface AbstractFilesystem
      Parameters:
      runtimeContext - per-call agent runtime; RuntimeContext.empty() when none
      filePath - absolute path to the file to read (must start with '/')
      offset - line number to start reading from (0-indexed). Default: 0
      limit - maximum number of lines to read. Default: 2000
      Returns:
      ReadResult with file data on success or error on failure
    • write

      public WriteResult write(io.agentscope.core.agent.RuntimeContext runtimeContext, String filePath, String content)
      Description copied from interface: AbstractFilesystem
      Write content to a new file, error if file already exists.
      Specified by:
      write in interface AbstractFilesystem
      Parameters:
      runtimeContext - per-call agent runtime; RuntimeContext.empty() when none
      filePath - absolute path where the file should be created
      content - string content to write to the file
      Returns:
      WriteResult with path on success, or error if the file already exists or write fails
    • edit

      public EditResult edit(io.agentscope.core.agent.RuntimeContext runtimeContext, String filePath, String oldString, String newString, boolean replaceAll)
      Description copied from interface: AbstractFilesystem
      Perform exact string replacements in an existing file.
      Specified by:
      edit in interface AbstractFilesystem
      Parameters:
      runtimeContext - per-call agent runtime; RuntimeContext.empty() when none
      filePath - absolute path to the file to edit
      oldString - exact string to search for and replace
      newString - string to replace oldString with (must be different from oldString)
      replaceAll - if true, replace all occurrences; if false, oldString must be unique
      Returns:
      EditResult with path and occurrence count on success, or error on failure
    • grep

      public GrepResult grep(io.agentscope.core.agent.RuntimeContext runtimeContext, String pattern, String path, String glob)
      Description copied from interface: AbstractFilesystem
      Search for a literal text pattern in files.
      Specified by:
      grep in interface AbstractFilesystem
      Parameters:
      runtimeContext - per-call agent runtime; RuntimeContext.empty() when none
      pattern - literal string to search for (not regex)
      path - optional directory path to search in (null searches current working directory)
      glob - optional glob pattern to filter which files to search (e.g., "*.java")
      Returns:
      GrepResult with matches or error
    • glob

      public GlobResult glob(io.agentscope.core.agent.RuntimeContext runtimeContext, String pattern, String path)
      Description copied from interface: AbstractFilesystem
      Find files matching a glob pattern.
      Specified by:
      glob in interface AbstractFilesystem
      Parameters:
      runtimeContext - per-call agent runtime; RuntimeContext.empty() when none
      pattern - glob pattern with wildcards to match file paths
      path - base directory to search from (default: "/")
      Returns:
      GlobResult with matching files or error
    • uploadFiles

      public List<FileUploadResponse> uploadFiles(io.agentscope.core.agent.RuntimeContext runtimeContext, List<Map.Entry<String,byte[]>> files)
      Description copied from interface: AbstractFilesystem
      Upload multiple files.
      Specified by:
      uploadFiles in interface AbstractFilesystem
      Parameters:
      runtimeContext - per-call agent runtime; RuntimeContext.empty() when none
      files - list of path-to-content mappings to upload
      Returns:
      list of FileUploadResponse objects, one per input file (order matches input order)
    • downloadFiles

      public List<FileDownloadResponse> downloadFiles(io.agentscope.core.agent.RuntimeContext runtimeContext, List<String> paths)
      Description copied from interface: AbstractFilesystem
      Download multiple files.
      Specified by:
      downloadFiles in interface AbstractFilesystem
      Parameters:
      runtimeContext - per-call agent runtime; RuntimeContext.empty() when none
      paths - list of file paths to download
      Returns:
      list of FileDownloadResponse objects, one per input path (order matches input order)
    • delete

      public WriteResult delete(io.agentscope.core.agent.RuntimeContext runtimeContext, String path)
      Description copied from interface: AbstractFilesystem
      Delete a file or directory (recursive for directories).

      Idempotent: deleting a path that does not exist is treated as success.

      Specified by:
      delete in interface AbstractFilesystem
      Parameters:
      runtimeContext - per-call agent runtime; RuntimeContext.empty() when none
      path - absolute path to the file or directory to delete
      Returns:
      WriteResult success when deleted (or already absent), failure on I/O error
    • move

      public WriteResult move(io.agentscope.core.agent.RuntimeContext runtimeContext, String fromPath, String toPath)
      Description copied from interface: AbstractFilesystem
      Move (rename) a file or directory from fromPath to toPath.

      Implementations that span multiple stores (e.g. CompositeFilesystem) may fall back to a read + write + delete sequence when source and destination live in different backend filesystems.

      Specified by:
      move in interface AbstractFilesystem
      Parameters:
      runtimeContext - per-call agent runtime; RuntimeContext.empty() when none
      fromPath - absolute source path
      toPath - absolute destination path
      Returns:
      WriteResult success on completion, failure on I/O error or missing source
    • exists

      public boolean exists(io.agentscope.core.agent.RuntimeContext runtimeContext, String path)
      Description copied from interface: AbstractFilesystem
      Check whether a file or directory exists.

      Implementations may approximate this with a lightweight read probe where a dedicated exists API is unavailable, but should avoid reading full file content.

      Specified by:
      exists in interface AbstractFilesystem
      Parameters:
      runtimeContext - per-call agent runtime; RuntimeContext.empty() when none
      path - absolute path to check
      Returns:
      true if the path exists, false otherwise
    • getNamespaceFactory

      protected NamespaceFactory getNamespaceFactory()
    • resolvePath

      protected Path resolvePath(io.agentscope.core.agent.RuntimeContext rc, String key)
    • toVirtualPath

      protected String toVirtualPath(Path path)