Skip to content

fix(harness): stop a running background subagent on task cancel - #4533

Open
liulangjietou wants to merge 2 commits into
agentscope-ai:mainfrom
liulangjietou:fix/background-subagent-cancel
Open

liulangjietou wants to merge 2 commits into
agentscope-ai:mainfrom
liulangjietou:fix/background-subagent-cancel

Conversation

@liulangjietou

@liulangjietou liulangjietou commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

AgentScope-Java Version

2.1.0-SNAPSHOT (main at 634fe8d)

Description

Fixes #2790.

Problem

TaskRepository.cancelTask recorded CANCELLED for a running local background task but did not stop it, so the subagent kept calling the model and tools until it finished on its own.

  • Explicit background spawns (timeout_seconds=0): WorkspaceTaskRepository ran the local supplier through CompletableFuture.supplyAsync. cancelTask calls BackgroundTask.cancel(true), but CompletableFuture.cancel never interrupts the thread computing it, so the supplier stayed blocked in invokeAgent(...).block().
  • Spawns promoted to background after a sync timeout: the repository adopted bridge.thenApply(...), and cancelling that derived future did not reach the child's subscription.

Change

  • WorkspaceTaskRepository runs the local execution as a FutureTask and cancels it together with the task's future. The interrupt reaches the worker only while the execution itself runs, never during the record updates before or after it. block() reacts by disposing the child's subscription, and ReActAgent already cancels its run and pending tool calls when its subscription is cancelled.
  • When a cancelled execution returns, the worker consumes the interrupt and records CANCELLED. It does not record FAILED or wake the parent, which matches the existing branch for a cancellation observed after the run. Results and failures of executions that were not cancelled are handled as before.
  • AgentSpawnTool disposes the inner subscription when a promoted task's future is cancelled. This reuses the existing doFinally(CANCEL) handler, which interrupts the child and releases its lease.
  • Rebased onto 634fe8d. feat(harness): support per-task subagent completion wakeups #4530 made runLocalSupplier take the LocalTaskRunSpec (for wakeOnCompletion) and routed the promoted task through submitTask. runLocalSupplier now takes both the spec and the FutureTask, and the cancel hook is registered before submitTask.

Tests

  • WorkspaceTaskRepositoryTest#cancelTask_stopsRunningLocalExecution: cancelTask stops a running execution; the record is CANCELLED without an error message, and no completion callback fires.
  • WorkspaceTaskRepositoryTest#cancellingLocalTaskHandle_recordsCancellationAfterInterrupt: cancelling the BackgroundTask handle, which persists nothing, stops the execution, and the worker records CANCELLED itself.
  • AgentSpawnToolLifecycleTest#cancellingRunningBackgroundSpawnStopsTheChild and #cancellingTimeoutPromotedSpawnStopsTheChild: with the real WorkspaceTaskRepository, cancelling the task cancels the child's subscription on both spawn paths.
  • After the rebase, I put back the two production classes from main at 634fe8d and kept the new tests: all four fail (the execution or the child is never cancelled). With the fix they pass.
  • mvn -T1C -pl agentscope-harness test, with the dependencies installed from this branch: 2413 tests, 0 failures, 37 skipped (environment-dependent SQLite, Docker and sandbox tests).
  • spotless:check passes.

Limitations

  • The AgentSpawnTool tests use a stub child whose call() records the cancel signal. I did not run a model-backed child agent.
  • A task_cancel issued on another node still only sets cancelRequested. The executing node checks that flag before and after the run, so a running local task is not interrupted across nodes. The "Background task storage" section of subagent.md says the executing node polls the flag and aborts, which currently holds for remote tasks only. I left this for a follow-up.
  • When a promoted task is cancelled, the reused interruptAgent logs its existing warning, which says the parent tool call subscription was cancelled.
  • Cancelling a promoted task still fires the completion callback through the adopted branch, as it did before this PR, while a cancelled local task does not. I left that as is.

Related

#4102 and #3825 also change WorkspaceTaskRepository.

Checklist

  • Code has been formatted with mvn spotless:apply
  • All tests are passing (mvn test): only the harness module tests were run locally, see Tests
  • Javadoc comments are complete and follow project conventions
  • Related documentation has been updated (e.g. links, examples, etc.): no change needed, task_cancel is documented as cancelling a running background task
  • Code is ready for review

@juruoO-o

juruoO-o commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

The current head passes Ubuntu and Windows CI but conflicts with main. Please resolve the conflict against the current background-task implementation and verify cancellation for both explicitly backgrounded tasks and tasks moved to the background on timeout.

cancelTask only recorded CANCELLED for a local background task that was
already running. The task ran through CompletableFuture.supplyAsync, and
CompletableFuture.cancel(true) never interrupts the thread computing it,
so the subagent kept calling the model and tools until it finished. A
task promoted after a sync timeout had the same gap: the registered
future was derived from the bridge, and cancelling it did not reach the
child's subscription.

WorkspaceTaskRepository now runs the local execution as a FutureTask and
cancels it together with the task. The interrupt reaches the thread only
while the execution runs, so block() disposes the child's subscription
and the record updates around the execution are never interrupted. The
interrupt is consumed once the execution stops, and the task is recorded
as CANCELLED without failing it or waking its parent, matching a
cancellation observed after the run.

AgentSpawnTool disposes the inner subscription when a promoted task is
cancelled, reusing doFinally(CANCEL) to interrupt the child and release
its lease.

Fixes agentscope-ai#2790
@liulangjietou
liulangjietou force-pushed the fix/background-subagent-cancel branch from 3ed1272 to a3730bc Compare December 2, 2026 10:09
@liulangjietou

Copy link
Copy Markdown
Contributor Author

@juruoO-o Rebased onto main at 634fe8d; the head is now a3730bc.

The conflict came from #4530: runLocalSupplier now takes the LocalTaskRunSpec to read wakeOnCompletion, and a promoted task is registered through submitTask. The resolution keeps both. runLocalSupplier takes the spec and the FutureTask, and the cancel hook on the promoted future is registered before submitTask.

Cancellation on both paths:

  • AgentSpawnToolLifecycleTest#cancellingRunningBackgroundSpawnStopsTheChild spawns with timeout_seconds=0, and #cancellingTimeoutPromotedSpawnStopsTheChild lets a sync spawn time out and get promoted. Both cancel the task through the real WorkspaceTaskRepository and require the child's subscription to be cancelled.
  • With the two production classes from 634fe8d and the new tests, both fail, as do WorkspaceTaskRepositoryTest#cancelTask_stopsRunningLocalExecution and #cancellingLocalTaskHandle_recordsCancellationAfterInterrupt. With this PR all four pass.
  • mvn -T1C -pl agentscope-harness test: 2413 tests, 0 failures, 37 skipped.

One thing I left as it is: cancelling a promoted task still fires the completion callback through the adopted branch, while a cancelled local task does not. That predates this PR and #4530.

cancellingRunningBackgroundSpawnStopsTheChild returned as soon as the
child was cancelled. The repository worker then still wrote the
CANCELLED record into the temporary workspace, so JUnit could delete
the directory while the record was being written and fail with
DirectoryNotEmptyException, as seen on the Ubuntu CI runner.

Shut the executor down and wait for it after the child stops, as the
repository-level cancellation tests already do.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: TaskRepository.cancelTask cannot stop an already-running async subagent

2 participants