Class CodingAgentFileSystem

  • All Implemented Interfaces:

    
    public final class CodingAgentFileSystem
    
                        

    Full filesystem access layer for AI coding agents.

    Unlike AgentFileSystem which is sandboxed to a specific directory and limited to a few file extensions, this class provides the agent with read/write access across the entire filesystem (subject to OS permissions), supports all file types, and includes directory operations, glob-based file search, and content diffing.

    Operations outside the workspaceRoot log a warning but are permitted when allowExternalAccess is true. Destructive operations (delete, move outside workspace) require explicit opt-in.

    val cfs = CodingAgentFileSystem(workspaceRoot = Path.of("/home/user/project"))
    cfs.writeFile("src/main.kt", code)
    val content = cfs.readFile("src/main.kt")
    val results = cfs.glob("src/**/*.kt")
    • Constructor Detail

      • CodingAgentFileSystem

        CodingAgentFileSystem(Path workspaceRoot, Boolean allowExternalAccess, Boolean allowDestructive, Set<String> searchExcludedDirs, Set<String> protectedFiles)
    • Method Detail

      • readFile

         final String readFile(String path, Charset encoding)

        Read a text file. Path is resolved relative to workspaceRoot, or absolute when allowExternalAccess is true.

      • writeFile

         final String writeFile(String path, String content, Charset encoding, Boolean createSnapshot)

        Write content to a file. Creates parent directories if needed. Takes a snapshot before writing for potential revert.

      • replaceRegexInFile

         final String replaceRegexInFile(String path, String regex, String replacement, Integer count)

        Replace all matches of a regular expression in a file.

        Parameters:
        regex - Java regex pattern; the whole match is replaced by replacement.
        count - max number of matches to replace; -1 = replace all
      • revert

         final String revert(String path)

        Restore a file to its snapshot (the state before the first tracked write). Fails if no snapshot exists — use changeSummary to see tracked files.

      • delete

         final String delete(String path, Boolean recursive)

        Delete a file or empty directory.

        Hard protections (always active, independent of allowDestructive):

        • the workspace root itself can never be deleted

        • version-control directories (.git/.svn/.hg) can never be deleted recursively

      • glob

         final String glob(String pattern, Integer maxResults)

        Search for files matching a glob pattern. Supports ** for recursive matching.

      • collectTextFiles

         final Map<String, String> collectTextFiles(String path)

        Collect all text files under path (recursively) as relative-path → content, skipping searchExcludedDirs, binary extensions, and files over MAX_READ_SIZE_BYTES. Returns an empty map when nothing readable is found or the path is not a directory.

        Used by the multi-file live-template extraction (coding.scaffoldFromExample with a directory path) and other whole-subtree operations.

      • diff

         final String diff(String path, String algorithm)

        Get a unified diff between the snapshot and current content of a file.

        Parameters:
        algorithm - "myers" (default, fastest, edit-distance optimal) or "patience" (anchors on unique lines — reads better for code moves and repeated boilerplate).
      • changeSummary

         final String changeSummary(Duration maxAge)

        Get change summary since tracking started.

        Change tracking lives on this filesystem instance. Long-lived shared instances (e.g. the session-less standalone coding shell) accumulate entries from every caller, so a maxAge window (default 24h) filters out stale noise from other callers/earlier sessions; hidden entries are counted and reported so agents know the listing is windowed. diff/revert still see snapshots of any age.

      • resolvePathString

         final String resolvePathString(String path)

        Resolve a path against the workspace sandbox and return the absolute path string.

        Returns null when the path is blocked (external access without allowExternalAccess, or traversal outside workspaceRoot) — same policy as readFile/writeFile. Use this for tools that hand the path to third-party validators that do raw IO (e.g. ArtifactValidator.validatePlugin), so they cannot escape the sandbox.

      • protect

         final String protect(String path, Boolean on)

        Add (or remove) a session-level protection on a specific file.

        Parameters:
        path - file path (resolved against the workspace root)
        on - true = protect (destructive ops blocked), false = unprotect
        Returns:

        confirmation message