Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/opencode/src/tool/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ export const TaskTool = Tool.define("task", async (ctx) => {

const session = await iife(async () => {
if (params.session_id) {
const found = await Session.get(params.session_id).catch(() => {})
if (found) return found
try {
const found = await Session.get(params.session_id)
if (found) return found
} catch {}
}

return await Session.create({
Expand Down
68 changes: 68 additions & 0 deletions packages/opencode/test/tool/task.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { describe, expect, test } from "bun:test"
import path from "path"
import { TaskTool } from "../../src/tool/task"
import { Instance } from "../../src/project/instance"
import { Session } from "../../src/session"
import { Identifier } from "../../src/id/id"
import { Tool } from "../../src/tool/tool"

const projectRoot = path.join(__dirname, "../..")

describe("tool.task", () => {
test("handles invalid session_id gracefully", async () => {
await Instance.provide({
directory: projectRoot,
fn: async () => {
const session = await Session.create({})
const messageID = Identifier.ascending("message")

await Session.updateMessage({
id: messageID,
sessionID: session.id,
role: "assistant",
parentID: Identifier.ascending("message"),
modelID: "dummy-model",
providerID: "dummy-provider",
mode: "auto",
agent: "primary",
path: { cwd: "/", root: "/" },
cost: 0,
tokens: {
input: 0,
output: 0,
reasoning: 0,
cache: { read: 0, write: 0 }
},
time: { created: Date.now() }
})

const ctx: Tool.Context = {
sessionID: session.id,
messageID: messageID,
agent: "primary",
abort: AbortSignal.any([]),
metadata: () => {},
ask: async () => {},
}

let error: Error | undefined
try {
const task = await TaskTool.init({ agent: { name: "primary" } as any })
await task.execute(
{
description: "test subtask",
prompt: "do something",
subagent_type: "explore",
session_id: "invalid_id_format",
},
ctx,
)
} catch (e) {
error = e as Error
}
expect(error).toBeDefined()
expect(error?.message).not.toContain("Invalid string: must start with")
}
})
})
})