From 8d373695e03519ec24af4207db308ef64e5ba266 Mon Sep 17 00:00:00 2001 From: Shantur Rathore Date: Sat, 4 Oct 2025 02:15:01 +0100 Subject: [PATCH] Allow Task sub-sessions to be continued again. --- packages/opencode/src/tool/task.ts | 11 +++++++++-- packages/opencode/src/tool/task.txt | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/opencode/src/tool/task.ts b/packages/opencode/src/tool/task.ts index 845a0e8c8..36a8de8fa 100644 --- a/packages/opencode/src/tool/task.ts +++ b/packages/opencode/src/tool/task.ts @@ -23,11 +23,14 @@ export const TaskTool = Tool.define("task", async () => { description: z.string().describe("A short (3-5 words) description of the task"), prompt: z.string().describe("The task for the agent to perform"), subagent_type: z.string().describe("The type of specialized agent to use for this task"), + session_id: z.string().describe("Existing Task session to continue").optional(), }), async execute(params, ctx) { const agent = await Agent.get(params.subagent_type) if (!agent) throw new Error(`Unknown agent type: ${params.subagent_type} is not a valid agent type`) - const session = await Session.create({ + const session = params.session_id + ? await Session.get(params.session_id) + : await Session.create({ parentID: ctx.sessionID, title: params.description + ` (@${agent.name} subagent)`, }) @@ -92,13 +95,17 @@ export const TaskTool = Tool.define("task", async () => { all = await Session.messages({ sessionID: session.id }) all = all.filter((x) => x.info.role === "assistant") all = all.flatMap((msg) => msg.parts.filter((x: any) => x.type === "tool") as MessageV2.ToolPart[]) + const text = (result.parts.findLast((x: any) => x.type === "text") as any)?.text ?? "" + const output = text ? `${text} + +[task-session:${session.id}]` : `[task-session:${session.id}]` return { title: params.description, metadata: { summary: all, sessionId: session.id, }, - output: (result.parts.findLast((x: any) => x.type === "text") as any)?.text ?? "", + output, } }, } diff --git a/packages/opencode/src/tool/task.txt b/packages/opencode/src/tool/task.txt index c3a78d4d8..e6578b6f6 100644 --- a/packages/opencode/src/tool/task.txt +++ b/packages/opencode/src/tool/task.txt @@ -18,7 +18,7 @@ When NOT to use the Task tool: Usage notes: 1. Launch multiple agents concurrently whenever possible, to maximize performance; to do that, use a single message with multiple tool uses 2. When the agent is done, it will return a single message back to you. The result returned by the agent is not visible to the user. To show the user the result, you should send a text message back to the user with a concise summary of the result. -3. Each agent invocation is stateless. You will not be able to send additional messages to the agent, nor will the agent be able to communicate with you outside of its final report. Therefore, your prompt should contain a highly detailed task description for the agent to perform autonomously and you should specify exactly what information the agent should return back to you in its final and only message to you. +3. Each agent invocation is stateless. Provide the returned [task-session:] marker as session_id to continue; otherwise include every instruction the agent needs upfront. 4. The agent's outputs should generally be trusted 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 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.