Class WorkspaceIndex

java.lang.Object
io.agentscope.harness.agent.workspace.WorkspaceIndex
All Implemented Interfaces:
AutoCloseable

public class WorkspaceIndex extends Object implements AutoCloseable
Best-effort SQLite index for the local workspace.

Tracks files that have been materialized locally under two path prefixes: agents/*/sessions/** and memory/**. The index is used to speed up ls / glob / exists / grep in remote-backed workspace mode by avoiding full-store key scans when enumerating paths under a prefix. File content is never stored in the index — grep still fetches each candidate file from the remote store authoritatively.

Consistency model: the index is best-effort and may lag remote changes. Remote writes remain authoritative. Index update failures are silently logged and never propagate to callers.

Thread-safety: SQLite serialises concurrent writers through its own transaction machinery. No external locks are required.

  • Method Details

    • open

      public static WorkspaceIndex open(Path workspaceRoot)
      Opens (or creates) the workspace index for the given workspace root.

      Returns null if the index cannot be initialized — callers should treat a null index as "unavailable" and fall back to remote scan.

      Parameters:
      workspaceRoot - absolute path to the workspace root directory
      Returns:
      a ready-to-use WorkspaceIndex, or null on failure
    • upsert

      public void upsert(String path, long sizeBytes, String modifiedAt)
      Upserts a file entry in the index. Silently no-ops if the path is not under an indexed prefix, or if any error occurs.
      Parameters:
      path - workspace-relative path (forward slashes)
      sizeBytes - file size in bytes; pass -1 if unknown
      modifiedAt - ISO-8601 timestamp string; pass null to use current time
    • upsertFromLocalFile

      public void upsertFromLocalFile(String path, Path localFile)
      Convenience overload that reads file size from the local file if it exists.
      Parameters:
      path - workspace-relative path
      localFile - absolute path on disk (used for size / mtime); may not exist
    • remove

      public void remove(String path)
      Removes a file entry from the index. Silently no-ops on errors.
      Parameters:
      path - workspace-relative path
    • rename

      public void rename(String fromPath, String toPath)
      Renames (moves) an index entry from fromPath to toPath. Silently no-ops on errors.
    • exists

      public boolean exists(String path)
      Returns true if the index contains an entry for the given path with present_local = 1.
    • listByPrefix

      public List<String> listByPrefix(String prefix)
      Returns all locally-present paths that start with the given prefix.
      Parameters:
      prefix - workspace-relative directory prefix (e.g. agents/a1/sessions/)
      Returns:
      list of matching paths, may be empty
    • hasPrefix

      public boolean hasPrefix(String prefix)
      Returns true if the index has any entries under the given prefix. Faster than listByPrefix(java.lang.String) when only presence is needed.
    • rebuildFromDisk

      public void rebuildFromDisk(Path workspaceRoot)
      Rebuilds the index by walking the local workspace directories (agents/&#42;/sessions and memory). Existing entries are replaced; stale entries for files that no longer exist are removed.

      This is a best-effort operation: errors are logged and do not throw.

      Parameters:
      workspaceRoot - absolute path to workspace root
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable