Repository navigation
fix(harness): stop a running background subagent on task cancel - #4533
liulangjietou wants to merge 2 commits into
Conversation
|
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
3ed1272 to
a3730bc
Compare
|
@juruoO-o Rebased onto main at 634fe8d; the head is now a3730bc. The conflict came from #4530: Cancellation on both paths:
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.
AgentScope-Java Version
2.1.0-SNAPSHOT (main at 634fe8d)
Description
Fixes #2790.
Problem
TaskRepository.cancelTaskrecorded 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.timeout_seconds=0):WorkspaceTaskRepositoryran the local supplier throughCompletableFuture.supplyAsync.cancelTaskcallsBackgroundTask.cancel(true), butCompletableFuture.cancelnever interrupts the thread computing it, so the supplier stayed blocked ininvokeAgent(...).block().bridge.thenApply(...), and cancelling that derived future did not reach the child's subscription.Change
WorkspaceTaskRepositoryruns the local execution as aFutureTaskand 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, andReActAgentalready cancels its run and pending tool calls when its subscription is cancelled.AgentSpawnTooldisposes the inner subscription when a promoted task's future is cancelled. This reuses the existingdoFinally(CANCEL)handler, which interrupts the child and releases its lease.runLocalSuppliertake theLocalTaskRunSpec(forwakeOnCompletion) and routed the promoted task throughsubmitTask.runLocalSuppliernow takes both the spec and theFutureTask, and the cancel hook is registered beforesubmitTask.Tests
WorkspaceTaskRepositoryTest#cancelTask_stopsRunningLocalExecution:cancelTaskstops a running execution; the record is CANCELLED without an error message, and no completion callback fires.WorkspaceTaskRepositoryTest#cancellingLocalTaskHandle_recordsCancellationAfterInterrupt: cancelling theBackgroundTaskhandle, which persists nothing, stops the execution, and the worker records CANCELLED itself.AgentSpawnToolLifecycleTest#cancellingRunningBackgroundSpawnStopsTheChildand#cancellingTimeoutPromotedSpawnStopsTheChild: with the realWorkspaceTaskRepository, cancelling the task cancels the child's subscription on both spawn paths.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:checkpasses.Limitations
AgentSpawnTooltests use a stub child whosecall()records the cancel signal. I did not run a model-backed child agent.task_cancelissued on another node still only setscancelRequested. 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 ofsubagent.mdsays the executing node polls the flag and aborts, which currently holds for remote tasks only. I left this for a follow-up.interruptAgentlogs its existing warning, which says the parent tool call subscription was cancelled.Related
#4102 and #3825 also change
WorkspaceTaskRepository.Checklist
mvn spotless:applymvn test): only the harness module tests were run locally, see Teststask_cancelis documented as cancelling a running background task