Class WorkspaceManager

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

public class WorkspaceManager extends Object implements AutoCloseable
Stateless accessor for workspace content using a two-layer read architecture.

Read path: For every read (AGENTS.md, MEMORY.md, knowledge, etc.), the AbstractFilesystem is queried first. If it returns non-empty content, that content is used (filesystem overrides). Otherwise, the local workspace disk is read as a fallback. The filesystem layer applies user/session scoping transparently via NamespaceFactory.

Write path: All writes (memory, sessions, etc.) go through the AbstractFilesystem.

Listing: File listings (memory files, knowledge files, session logs) union results from both the filesystem layer and local disk, deduplicating by relative path.

Expected layout:

 workspace/
 ├── AGENTS.md
 ├── MEMORY.md
 ├── memory/YYYY-MM-DD.md
 ├── skills/<skill-name>/SKILL.md
 ├── knowledge/KNOWLEDGE.md
 ├── knowledge/*
 ├── subagents/<id>.md                     (subagent declarations)
 ├── agents/<agentId>/workspace/           (isolated subagent runtime root, auto-created)
 ├── agents/<agentId>/sessions/sessions.json
 └── agents/<agentId>/sessions/<sessionId>.log.jsonl
 
  • Constructor Details

  • Method Details

    • close

      public void close()
      Releases the SQLite-backed WorkspaceIndex when this manager owns it.

      Required for tests that use @TempDir on Windows: the JDBC driver keeps a file handle on .index/workspace.db, and Windows refuses to delete the temp directory while the handle is open.

      Specified by:
      close in interface AutoCloseable
    • getNamespaceFactory

      public NamespaceFactory getNamespaceFactory()
    • getIndex

      public WorkspaceIndex getIndex()
      Returns the best-effort workspace index; may be null when unavailable.
    • getFilesystem

      public AbstractFilesystem getFilesystem()
    • validate

      public void validate()
      Validates the workspace exists and key files are present. Logs warnings for anything missing. Called once at HarnessAgent build time.
    • getWorkspace

      public Path getWorkspace()
    • resolveRuntimeDataPath

      public Path resolveRuntimeDataPath(io.agentscope.core.agent.RuntimeContext rc, String relativePath)
      Resolves a workspace-relative path for runtime user data, applying namespace prefix. Use for paths that contain per-user data (sessions, tasks, memory).

      The runtime context is forwarded to the NamespaceFactory so per-call identity (user/session) drives the namespace, not a shared mutable reference.

    • readAgentsMd

      public String readAgentsMd(io.agentscope.core.agent.RuntimeContext rc)
      Reads AGENTS.md content, returns empty string if not found.
    • readKnowledgeMd

      public String readKnowledgeMd(io.agentscope.core.agent.RuntimeContext rc)
      Reads KNOWLEDGE.md content from the knowledge directory.
    • readMemoryMd

      public String readMemoryMd(io.agentscope.core.agent.RuntimeContext rc)
      Reads MEMORY.md content (two-layer: filesystem override, local fallback).
    • readManagedWorkspaceFileUtf8

      public String readManagedWorkspaceFileUtf8(io.agentscope.core.agent.RuntimeContext rc, String relativePath)
      Reads a UTF-8 file under the workspace, using the two-layer pattern: filesystem first, then local disk fallback.
    • getMemoryDir

      public Path getMemoryDir(io.agentscope.core.agent.RuntimeContext rc)
    • getSkillsDir

      public Path getSkillsDir()
    • getKnowledgeDir

      public Path getKnowledgeDir()
    • listKnowledgeFiles

      public List<Path> listKnowledgeFiles(io.agentscope.core.agent.RuntimeContext rc)
      Lists all files under the knowledge directory tree (union of filesystem + local disk).
    • getSessionDir

      public Path getSessionDir(io.agentscope.core.agent.RuntimeContext rc, String agentId)
    • resolveSessionFile

      @Deprecated public Path resolveSessionFile(io.agentscope.core.agent.RuntimeContext rc, String agentId, String sessionId)
      Deprecated.
      Returns the legacy session file path (.json) without creating directories.
    • resolveSessionContextFile

      public Path resolveSessionContextFile(io.agentscope.core.agent.RuntimeContext rc, String agentId, String sessionId)
      Returns the JSONL session context file path (LLM-facing, compacted).
    • resolveSessionLogFile

      public Path resolveSessionLogFile(io.agentscope.core.agent.RuntimeContext rc, String agentId, String sessionId)
      Returns the JSONL session log file path (full history, append-only).
    • appendUtf8WorkspaceRelative

      public void appendUtf8WorkspaceRelative(io.agentscope.core.agent.RuntimeContext rc, String relativePath, String content)
      Appends UTF-8 text to a workspace-relative file, creating parent directories when needed. All writes go through the AbstractFilesystem.

      A per-path ReentrantLock serialises concurrent callers so that the read→merge→write cycle is atomic within this process. Across replicas the append is last-writer-wins; this method does not perform CAS.

    • updateSessionIndex

      public void updateSessionIndex(io.agentscope.core.agent.RuntimeContext rc, String agentId, String sessionId, String summary)
      Upserts metadata for a session in agents/&lt;agentId&gt;/sessions/sessions.json (small mutable JSON, keyed by sessionId).

      A per-path ReentrantLock serialises concurrent callers so that the read→merge→write cycle is atomic within this process.

    • writeTaskRecord

      public void writeTaskRecord(io.agentscope.core.agent.RuntimeContext rc, String agentId, String sessionId, TaskRecord record)
      Upserts a TaskRecord in agents/<agentId>/tasks/<sessionId>.json.

      Reads the existing map, merges or inserts the record keyed by taskId, then writes the updated map back. Acquires a per-file ReentrantLock to prevent concurrent read-modify-write races when multiple tasks share the same session file.

    • readTaskRecord

      public Optional<TaskRecord> readTaskRecord(io.agentscope.core.agent.RuntimeContext rc, String agentId, String sessionId, String taskId)
      Reads a single TaskRecord by task ID, or Optional.empty() if not found.
    • listTaskRecords

      public Collection<TaskRecord> listTaskRecords(io.agentscope.core.agent.RuntimeContext rc, String agentId, String sessionId)
      Returns all TaskRecords for the given agent and session, in insertion order.
    • listAllTaskRecords

      public Collection<TaskRecord> listAllTaskRecords(io.agentscope.core.agent.RuntimeContext rc, String agentId, Duration recentWindow)
      Returns all TaskRecords for the given agent across all sessions that have been active within recentWindow, in no particular order.

      Unions task JSON files from the local disk and the filesystem layer. Files whose last modification time (from disk mtime or FileInfo.modifiedAt()) is known and older than recentWindow are skipped: once all tasks in a session reach a terminal state the file is never modified again, so stale files cannot contain orphaned tasks.

      Used by the orphan-task sweeper in WorkspaceTaskRepository to bound the number of files read per sweep cycle without missing any genuinely running tasks.

      Parameters:
      agentId - the parent agent identifier
      recentWindow - only consider files modified within this duration; files known to be older are assumed to contain only terminal tasks and are skipped
    • writeUtf8WorkspaceRelative

      public void writeUtf8WorkspaceRelative(io.agentscope.core.agent.RuntimeContext rc, String relativePath, String content)
      Overwrites a workspace-relative UTF-8 file. All writes go through the filesystem.
    • writeDraftSkillFile

      public void writeDraftSkillFile(io.agentscope.core.agent.RuntimeContext rc, String relativePath, String content)
      Write a single file under skills/_drafts/ (or whatever drafts dir the caller passes). When the underlying filesystem is an OverlayFilesystem backed by a RemoteFilesystem lower layer, the draft is written directly to that lower layer so it is visible to other replicas / external approval systems. Otherwise it goes through the standard upper-layer write path.
      Parameters:
      rc - the runtime context (passed straight through to the filesystem)
      relativePath - workspace-relative path (must include the drafts prefix)
      content - UTF-8 content
    • moveSkill

      public boolean moveSkill(io.agentscope.core.agent.RuntimeContext rc, String fromRelative, String toRelative)
      Move a directory inside the workspace (used by promotion: skills/_drafts/<x> → skills/<x>). Best-effort — falls back to the underlying filesystem's native move when available; returns true on success.
    • listMemoryFilePaths

      public List<String> listMemoryFilePaths(io.agentscope.core.agent.RuntimeContext rc)
      Returns workspace-relative paths of all memory files (MEMORY.md and memory/*.md). Unions results from the AbstractFilesystem layer and the local disk, deduplicating by relative path.
    • listSessionLogFiles

      public List<String> listSessionLogFiles(io.agentscope.core.agent.RuntimeContext rc)
      Lists workspace-relative paths of all session log files (*.log.jsonl). Unions results from the AbstractFilesystem layer and the local disk.
    • toWorkspaceRelativeString

      public String toWorkspaceRelativeString(Path absoluteUnderWorkspace)
      Workspace-relative path for indexing.