fix(background-cancel): skip notification when user explicitly cancels tasks
- Add skipNotification option to cancelTask method - Apply skipNotification to background_cancel tool - Prevents unwanted notifications when user cancels via tool
This commit is contained in:
@@ -328,6 +328,58 @@ describe("background_cancel", () => {
|
||||
expect(output).toContain("| `task-a` | running task | running | `ses-a` |")
|
||||
expect(output).toContain("| `task-b` | pending task | pending | (not started) |")
|
||||
})
|
||||
|
||||
test("passes skipNotification: true to cancelTask to prevent deadlock", async () => {
|
||||
// #given
|
||||
const task = createTask({ id: "task-1", status: "running" })
|
||||
const cancelOptions: Array<{ taskId: string; options: unknown }> = []
|
||||
const manager = {
|
||||
getTask: (id: string) => (id === task.id ? task : undefined),
|
||||
getAllDescendantTasks: () => [task],
|
||||
cancelTask: async (taskId: string, options?: unknown) => {
|
||||
cancelOptions.push({ taskId, options })
|
||||
task.status = "cancelled"
|
||||
return true
|
||||
},
|
||||
} as unknown as BackgroundManager
|
||||
const client = { session: { abort: async () => ({}) } } as BackgroundCancelClient
|
||||
const tool = createBackgroundCancel(manager, client)
|
||||
|
||||
// #when - cancel all tasks
|
||||
await tool.execute({ all: true }, mockContext)
|
||||
|
||||
// #then - skipNotification should be true to prevent self-deadlock
|
||||
expect(cancelOptions).toHaveLength(1)
|
||||
expect(cancelOptions[0].options).toEqual(
|
||||
expect.objectContaining({ skipNotification: true })
|
||||
)
|
||||
})
|
||||
|
||||
test("passes skipNotification: true when cancelling single task", async () => {
|
||||
// #given
|
||||
const task = createTask({ id: "task-1", status: "running" })
|
||||
const cancelOptions: Array<{ taskId: string; options: unknown }> = []
|
||||
const manager = {
|
||||
getTask: (id: string) => (id === task.id ? task : undefined),
|
||||
getAllDescendantTasks: () => [task],
|
||||
cancelTask: async (taskId: string, options?: unknown) => {
|
||||
cancelOptions.push({ taskId, options })
|
||||
task.status = "cancelled"
|
||||
return true
|
||||
},
|
||||
} as unknown as BackgroundManager
|
||||
const client = { session: { abort: async () => ({}) } } as BackgroundCancelClient
|
||||
const tool = createBackgroundCancel(manager, client)
|
||||
|
||||
// #when - cancel single task
|
||||
await tool.execute({ taskId: task.id }, mockContext)
|
||||
|
||||
// #then - skipNotification should be true
|
||||
expect(cancelOptions).toHaveLength(1)
|
||||
expect(cancelOptions[0].options).toEqual(
|
||||
expect.objectContaining({ skipNotification: true })
|
||||
)
|
||||
})
|
||||
})
|
||||
type BackgroundOutputMessage = {
|
||||
id?: string
|
||||
|
||||
@@ -642,6 +642,7 @@ export function createBackgroundCancel(manager: BackgroundManager, client: Backg
|
||||
const cancelled = await manager.cancelTask(task.id, {
|
||||
source: "background_cancel",
|
||||
abortSession: originalStatus === "running",
|
||||
skipNotification: true,
|
||||
})
|
||||
if (!cancelled) continue
|
||||
cancelledInfo.push({
|
||||
@@ -690,6 +691,7 @@ Only running or pending tasks can be cancelled.`
|
||||
const cancelled = await manager.cancelTask(task.id, {
|
||||
source: "background_cancel",
|
||||
abortSession: task.status === "running",
|
||||
skipNotification: true,
|
||||
})
|
||||
if (!cancelled) {
|
||||
return `[ERROR] Failed to cancel task: ${task.id}`
|
||||
|
||||
Reference in New Issue
Block a user