Skip to content

Commit 1fd1128

Browse files
committed
fix(tool): fix cancel_task to abort subagent session instead of parent (#54)
1 parent 119053e commit 1fd1128

3 files changed

Lines changed: 16 additions & 4 deletions

File tree

‎packages/opencode/src/session/index.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ function sanitizeError(error: string): string {
8989

9090
const pendingBackgroundTasks = new Map<string, Promise<string | undefined | void>>()
9191
const pendingTaskMetadata = new Map<string, TaskMetadata>()
92+
const subagentSessionIDs = new Map<string, string>()
9293
const cancelledTasks = new Set<string>()
9394
const reservedTaskSlots = new Map<string, Set<string>>()
9495

@@ -198,6 +199,9 @@ export namespace Session {
198199
if (metadata) {
199200
pendingTaskMetadata.set(id, metadata)
200201
}
202+
if (sessionID) {
203+
subagentSessionIDs.set(id, sessionID)
204+
}
201205

202206
if (sessionID && closingSessions.has(sessionID)) {
203207
log.warn("task rejected: session closed during tracking", { task_id: id, session_id: sessionID })
@@ -285,6 +289,7 @@ export namespace Session {
285289
} finally {
286290
pendingBackgroundTasks.delete(id)
287291
pendingTaskMetadata.delete(id)
292+
subagentSessionIDs.delete(id)
288293

289294
if (metadata?.release_slot) {
290295
try {
@@ -539,10 +544,11 @@ export namespace Session {
539544
}
540545
}
541546

542-
const sessionID = metadata?.session_id
543-
if (sessionID) {
544-
SessionPrompt.cancel(sessionID)
547+
const subagentSessionID = subagentSessionIDs.get(id)
548+
if (subagentSessionID) {
549+
SessionPrompt.cancel(subagentSessionID)
545550
}
551+
subagentSessionIDs.delete(id)
546552

547553
return true
548554
}
@@ -924,6 +930,7 @@ export namespace Session {
924930
backgroundTaskResults.delete(id)
925931
deliveredTaskResults.delete(id)
926932
cancelledTasks.delete(id)
933+
subagentSessionIDs.delete(id)
927934
}
928935
}
929936
}

‎packages/opencode/src/tool/cancel_task.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export const CancelTaskTool = Tool.define<z.ZodObject<{ task_id: z.ZodString }>,
2626
.regex(/^[a-zA-Z0-9_-]+$/)
2727
.describe("The ID of the background task to cancel"),
2828
}),
29-
async execute(params) {
29+
async execute(params, ctx) {
3030
const result = Session.tryCancel(params.task_id)
3131

3232
const response: CancelTaskResult = {

‎packages/opencode/src/tool/task.txt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ Usage notes:
2323
5. Clearly tell the agent whether you expect it to write code or just to do research (search, file reads, web fetches, etc.), since it is not aware of the user's intent
2424
6. If the agent description mentions that it should be used proactively, then you should try your best to use it without the user having to ask for it first. Use your judgement.
2525

26+
Task lifecycle management:
27+
- If new information makes a running task obsolete, cancel it with cancel_task before launching a replacement. Do not leave stale tasks running.
28+
- Use check_task to inspect task status before deciding to cancel or wait.
29+
- When cancelling and re-launching, provide the updated context in the new task's prompt.
30+
2631
Example usage (NOTE: The agents below are fictional examples for illustration only - use the actual agents listed above):
2732

2833
<example_agent_descriptions>

0 commit comments

Comments
 (0)