feat(background-task): add all parameter to cancel all running tasks at once

Allows OmO agent to cleanup all running background tasks before providing final answers.

🤖 GENERATED WITH ASSISTANCE OF [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode)
This commit is contained in:
YeonGyu-Kim
2025-12-16 21:02:38 +09:00
parent 059f2bfe13
commit 4112be7ad5
4 changed files with 42 additions and 6 deletions

View File

@@ -844,6 +844,7 @@ You are allowed to be proactive, but balance this with user expectations:
- **Stop when you have enough** - don't over-explore
- **Evidence for everything** - no evidence = not complete
- **Background pattern** - fire agents, continue working, collect with background_output
- **Cleanup before answering** - When ready to deliver your final answer, cancel ALL running background tasks with \`background_cancel(all=true)\` first, then respond. This conserves resources and ensures clean workflow completion.
- Complete accepted tasks fully - don't stop halfway through implementation
- But if you discover the task is larger or more complex than initially apparent, communicate this and confirm direction before investing significant effort
</Final_Reminders>

View File

@@ -25,9 +25,12 @@ Arguments:
The system automatically notifies when background tasks complete. You typically don't need block=true.`
export const BACKGROUND_CANCEL_DESCRIPTION = `Cancel a running background task.
export const BACKGROUND_CANCEL_DESCRIPTION = `Cancel running background task(s).
Only works for tasks with status "running". Aborts the background session and marks the task as cancelled.
Arguments:
- taskId: Required task ID to cancel.`
- taskId: Task ID to cancel (optional if all=true)
- all: Set to true to cancel ALL running background tasks at once (default: false)
**Cleanup Before Answer**: When you have gathered sufficient information and are ready to provide your final answer to the user, use \`all=true\` to cancel ALL running background tasks first, then deliver your response. This conserves resources and ensures clean workflow completion.`

View File

@@ -263,11 +263,42 @@ export function createBackgroundCancel(manager: BackgroundManager, client: Openc
return tool({
description: BACKGROUND_CANCEL_DESCRIPTION,
args: {
taskId: tool.schema.string().describe("Task ID to cancel"),
taskId: tool.schema.string().optional().describe("Task ID to cancel (required if all=false)"),
all: tool.schema.boolean().optional().describe("Cancel all running background tasks (default: false)"),
},
async execute(args: BackgroundCancelArgs) {
async execute(args: BackgroundCancelArgs, toolContext) {
try {
const task = manager.getTask(args.taskId)
const cancelAll = args.all === true
if (!cancelAll && !args.taskId) {
return `❌ Invalid arguments: Either provide a taskId or set all=true to cancel all running tasks.`
}
if (cancelAll) {
const tasks = manager.getTasksByParentSession(toolContext.sessionID)
const runningTasks = tasks.filter(t => t.status === "running")
if (runningTasks.length === 0) {
return `✅ No running background tasks to cancel.`
}
const results: string[] = []
for (const task of runningTasks) {
client.session.abort({
path: { id: task.sessionID },
}).catch(() => {})
task.status = "cancelled"
task.completedAt = new Date()
results.push(`- ${task.id}: ${task.description}`)
}
return `✅ Cancelled ${runningTasks.length} background task(s):
${results.join("\n")}`
}
const task = manager.getTask(args.taskId!)
if (!task) {
return `❌ Task not found: ${args.taskId}`
}

View File

@@ -11,5 +11,6 @@ export interface BackgroundOutputArgs {
}
export interface BackgroundCancelArgs {
taskId: string
taskId?: string
all?: boolean
}