Class WorkspaceTaskRepository
- All Implemented Interfaces:
TaskRepository
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:
- Task execution is sticky to the originating node (the node that called
putTask(io.agentscope.core.agent.RuntimeContext, java.lang.String, java.lang.String, java.lang.String, io.agentscope.harness.agent.subagent.task.TaskRunSpec)). - Any node can read task status via
getTask(io.agentscope.core.agent.RuntimeContext, java.lang.String, java.lang.String)orlistTasks(io.agentscope.core.agent.RuntimeContext, java.lang.String, io.agentscope.harness.agent.subagent.task.TaskStatus)by falling back to workspace records when no local future exists. block=trueon a non-originating node degrades gracefully to reading the latest persisted terminal state without hanging.- Cancellation sets a
TaskRecord.isCancelRequested()flag in workspace storage; the originating node checks this flag before invoking the subagent for best-effort cancel. - Remote
TaskRunSpec.RemoteTaskRunSpectasks use aRemoteSubagentTransport(Agent Protocol by default) and persistTaskRecord.getRemoteBaseUrl()for cross-node resume. - Orphan sweeping is throttled via
PeriodicGate: useStoreBackedPeriodicGatefor cross-replica deduplication, orLocalPeriodicGate(the default) for single-process deployments. Prefer a control-plane hostedTaskRepositorywhen available — that path runs a leader-only sweep and does not use this workspace repository at all.
-
Nested Class Summary
Nested classes/interfaces inherited from interface io.agentscope.harness.agent.subagent.task.TaskRepository
TaskRepository.TaskCompletionCallback -
Field Summary
Fields inherited from interface io.agentscope.harness.agent.subagent.task.TaskRepository
SUPPRESS_COMPLETION_CALLBACK -
Constructor Summary
ConstructorsConstructorDescriptionWorkspaceTaskRepository(WorkspaceManager workspaceManager, String parentAgentId) WorkspaceTaskRepository(WorkspaceManager workspaceManager, String parentAgentId, PeriodicGate periodicGate) Creates a repository with an explicitPeriodicGatefor orphan-sweep throttling.WorkspaceTaskRepository(WorkspaceManager workspaceManager, String parentAgentId, ExecutorService executor) -
Method Summary
Modifier and TypeMethodDescriptionbooleancancelTask(io.agentscope.core.agent.RuntimeContext rc, String sessionId, String taskId) Cancel a running task by session and task ID.findPendingDeliveries(io.agentscope.core.agent.RuntimeContext rc, String sessionId) Returns terminal-state tasks for the given session whose completions have not yet been pushed back to the parent agent.static WorkspaceTaskRepositoryforTests(WorkspaceManager workspaceManager, String parentAgentId) Test-only factory without background heartbeat or orphan sweeper threads.static WorkspaceTaskRepositoryforTests(WorkspaceManager workspaceManager, String parentAgentId, PeriodicGate periodicGate) Test-only factory with a caller-suppliedPeriodicGateand no maintenance threads.static WorkspaceTaskRepositoryforTests(WorkspaceManager workspaceManager, String parentAgentId, ExecutorService executor) Test-only factory with a caller-supplied executor and no maintenance threads.Retrieve a background task by session and task ID, ornullif not found.booleanisDelivered(io.agentscope.core.agent.RuntimeContext rc, String sessionId, String taskId) Whether the given task has already been delivered to the parent agent.listTasks(io.agentscope.core.agent.RuntimeContext rc, String sessionId, TaskStatus filter) List all tracked tasks for the given session, optionally filtered by status.voidmarkDelivered(io.agentscope.core.agent.RuntimeContext rc, String sessionId, String taskId) StampsdeliveredAton the persisted record.putTask(io.agentscope.core.agent.RuntimeContext rc, String taskId, String subAgentId, String sessionId, TaskRunSpec spec) Submit a new background task according toTaskRunSpec.voidRegisters a callback invoked when any task reaches a terminal state (COMPLETED or FAILED).voidshutdown()Shuts down background executors owned by this repository.
-
Constructor Details
-
WorkspaceTaskRepository
-
WorkspaceTaskRepository
public WorkspaceTaskRepository(WorkspaceManager workspaceManager, String parentAgentId, PeriodicGate periodicGate) Creates a repository with an explicitPeriodicGatefor orphan-sweep throttling.Pass a
StoreBackedPeriodicGatewhen a sharedBaseStoreis available so only one replica sweeps per interval. -
WorkspaceTaskRepository
public WorkspaceTaskRepository(WorkspaceManager workspaceManager, String parentAgentId, ExecutorService executor)
-
-
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()andsweepOrphanedTasks(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-suppliedPeriodicGateand 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
Description copied from interface:TaskRepositoryRegisters a callback invoked when any task reaches a terminal state (COMPLETED or FAILED). Used bySubagentsMiddlewareto push results to the session inbox and enqueue a wakeup signal. Theresultargument isnullfor failed tasks.Callers retaining execution ownership can set
TaskRepository.SUPPRESS_COMPLETION_CALLBACKin 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:
setCompletionCallbackin interfaceTaskRepository
-
putTask
public BackgroundTask putTask(io.agentscope.core.agent.RuntimeContext rc, String taskId, String subAgentId, String sessionId, TaskRunSpec spec) Description copied from interface:TaskRepositorySubmit a new background task according toTaskRunSpec.Implementations should capture
rcso that the background execution thread can persist task state under the originating user's namespace.- Specified by:
putTaskin interfaceTaskRepository- Parameters:
rc- the current call's runtime context; may beRuntimeContext.empty()taskId- unique identifier for the tasksubAgentId- the subagent type executing this tasksessionId- the parent session scopespec- 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:TaskRepositoryRetrieve a background task by session and task ID, ornullif not found.- Specified by:
getTaskin interfaceTaskRepository- Parameters:
rc- the current call's runtime context; may beRuntimeContext.empty()sessionId- the parent session scopetaskId- unique task identifier
-
listTasks
public Collection<BackgroundTask> listTasks(io.agentscope.core.agent.RuntimeContext rc, String sessionId, TaskStatus filter) Description copied from interface:TaskRepositoryList all tracked tasks for the given session, optionally filtered by status.- Specified by:
listTasksin interfaceTaskRepository- Parameters:
rc- the current call's runtime context; may beRuntimeContext.empty()sessionId- the parent session scopefilter- 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:TaskRepositoryCancel a running task by session and task ID.- Specified by:
cancelTaskin interfaceTaskRepository- Parameters:
rc- the current call's runtime context; may beRuntimeContext.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:TaskRepositoryReturns 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:
findPendingDeliveriesin interfaceTaskRepository
-
markDelivered
public void markDelivered(io.agentscope.core.agent.RuntimeContext rc, String sessionId, String taskId) StampsdeliveredAton 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 throughupdateStatus(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:
markDeliveredin interfaceTaskRepository
-
isDelivered
public boolean isDelivered(io.agentscope.core.agent.RuntimeContext rc, String sessionId, String taskId) Description copied from interface:TaskRepositoryWhether the given task has already been delivered to the parent agent. Used bySubagentsMiddleware.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:
isDeliveredin interfaceTaskRepository
-
shutdown
public void shutdown()Description copied from interface:TaskRepositoryShuts down background executors owned by this repository. Default is a no-op.- Specified by:
shutdownin interfaceTaskRepository
-