Class WorkspaceTaskRepository

java.lang.Object
io.agentscope.harness.agent.subagent.task.WorkspaceTaskRepository
All Implemented Interfaces:
TaskRepository

public class WorkspaceTaskRepository extends Object implements TaskRepository
Workspace-backed TaskRepository that uses WorkspaceManager as the authoritative truth source for task state, while maintaining in-memory BackgroundTask handles as a local performance overlay for tasks running on the current node.

Storage layout: agents/<parentAgentId>/tasks/<sessionId>.json — a JSON map of taskId → TaskRecord, consistent with how sessions are stored. In distributed deployments using RemoteFilesystemSpec, this path is automatically routed to shared storage, making task state visible to any node. In sandbox mode the injected filesystem is a per-call proxy with no live sandbox between calls, so task records are persisted on the host workspace instead of inside the sandbox — see the task-record routing in WorkspaceManager.

The in-memory localTasks map is keyed by "<sessionId>:<taskId>" to preserve session isolation when multiple sessions coexist in the same process.

Distributed semantics:

  • Constructor Details

  • Method Details

    • forTests

      public static WorkspaceTaskRepository forTests(WorkspaceManager workspaceManager, String parentAgentId)
      Test-only factory without background heartbeat or orphan sweeper threads.

      Unit tests invoke heartbeat() and sweepOrphanedTasks(java.time.Duration, java.time.Duration) directly; leaving the maintenance scheduler enabled causes flaky races on slow CI hosts (notably Windows).

    • forTests

      public static WorkspaceTaskRepository forTests(WorkspaceManager workspaceManager, String parentAgentId, PeriodicGate periodicGate)
      Test-only factory with a caller-supplied PeriodicGate and no maintenance threads.
    • forTests

      public static WorkspaceTaskRepository forTests(WorkspaceManager workspaceManager, String parentAgentId, ExecutorService executor)
      Test-only factory with a caller-supplied executor and no maintenance threads.
    • setCompletionCallback

      public void setCompletionCallback(TaskRepository.TaskCompletionCallback callback)
      Description copied from interface: TaskRepository
      Registers a callback invoked when any task reaches a terminal state (COMPLETED or FAILED). Used by SubagentsMiddleware to push results to the session inbox and enqueue a wakeup signal. The result argument is null for failed tasks.

      Callers retaining execution ownership can set TaskRepository.SUPPRESS_COMPLETION_CALLBACK in their RuntimeContext to suppress inbox/wakeup callbacks while preserving durable results. Implementations supporting this callback should honor the flag.

      Default is a no-op. Implementations that support push delivery should override.

      Specified by:
      setCompletionCallback in interface TaskRepository
    • putTask

      public BackgroundTask putTask(io.agentscope.core.agent.RuntimeContext rc, String taskId, String subAgentId, String sessionId, TaskRunSpec spec)
      Description copied from interface: TaskRepository
      Submit a new background task according to TaskRunSpec.

      Implementations should capture rc so that the background execution thread can persist task state under the originating user's namespace.

      Specified by:
      putTask in interface TaskRepository
      Parameters:
      rc - the current call's runtime context; may be RuntimeContext.empty()
      taskId - unique identifier for the task
      subAgentId - the subagent type executing this task
      sessionId - the parent session scope
      spec - local supplier execution or remote HTTP task protocol
      Returns:
      the created background task
    • getTask

      public BackgroundTask getTask(io.agentscope.core.agent.RuntimeContext rc, String sessionId, String taskId)
      Description copied from interface: TaskRepository
      Retrieve a background task by session and task ID, or null if not found.
      Specified by:
      getTask in interface TaskRepository
      Parameters:
      rc - the current call's runtime context; may be RuntimeContext.empty()
      sessionId - the parent session scope
      taskId - unique task identifier
    • listTasks

      public Collection<BackgroundTask> listTasks(io.agentscope.core.agent.RuntimeContext rc, String sessionId, TaskStatus filter)
      Description copied from interface: TaskRepository
      List all tracked tasks for the given session, optionally filtered by status.
      Specified by:
      listTasks in interface TaskRepository
      Parameters:
      rc - the current call's runtime context; may be RuntimeContext.empty()
      sessionId - the parent session scope
      filter - if non-null, only return tasks with this status; null returns all tasks
    • cancelTask

      public boolean cancelTask(io.agentscope.core.agent.RuntimeContext rc, String sessionId, String taskId)
      Description copied from interface: TaskRepository
      Cancel a running task by session and task ID.
      Specified by:
      cancelTask in interface TaskRepository
      Parameters:
      rc - the current call's runtime context; may be RuntimeContext.empty()
      Returns:
      true if the task was found and cancellation was attempted
    • findPendingDeliveries

      public List<TaskDelivery> findPendingDeliveries(io.agentscope.core.agent.RuntimeContext rc, String sessionId)
      Description copied from interface: TaskRepository
      Returns terminal-state tasks for the given session whose completions have not yet been pushed back to the parent agent.

      Implementations should order the result deterministically (typically by completion time ascending) so the parent agent sees deliveries in the order they actually happened.

      Default returns an empty list — in-memory repositories do not currently support push.

      Specified by:
      findPendingDeliveries in interface TaskRepository
    • markDelivered

      public void markDelivered(io.agentscope.core.agent.RuntimeContext rc, String sessionId, String taskId)
      Stamps deliveredAt on the persisted record. Idempotent — the first non-null write wins; subsequent calls bail out without touching workspace storage. Uses an independent read-modify-write path rather than going through updateStatus(io.agentscope.core.agent.RuntimeContext, java.lang.String, java.lang.String, io.agentscope.harness.agent.subagent.task.TaskStatus, java.lang.String, java.lang.String) so the heartbeat / orphan-sweeper cannot accidentally clobber the field via their RUNNING/FAILED writes (those paths reconstruct the record around status-only fields).
      Specified by:
      markDelivered in interface TaskRepository
    • isDelivered

      public boolean isDelivered(io.agentscope.core.agent.RuntimeContext rc, String sessionId, String taskId)
      Description copied from interface: TaskRepository
      Whether the given task has already been delivered to the parent agent. Used by SubagentsMiddleware.buildTaskSummary(io.agentscope.harness.agent.subagent.task.TaskRepository, io.agentscope.core.agent.RuntimeContext) to omit already-delivered terminal tasks from the SYSTEM-prompt summary.

      Default returns false.

      Specified by:
      isDelivered in interface TaskRepository
    • shutdown

      public void shutdown()
      Description copied from interface: TaskRepository
      Shuts down background executors owned by this repository. Default is a no-op.
      Specified by:
      shutdown in interface TaskRepository