diff --git a/.opencode/tools/worklane.ts b/.opencode/tools/worklane.ts index 67840d8..3fc21d8 100644 --- a/.opencode/tools/worklane.ts +++ b/.opencode/tools/worklane.ts @@ -5,15 +5,46 @@ function resolveWorklaneDir(context: ToolContext): string { return path.join(context.worktree || context.directory || "."); } +/** + * Run worklane and return its stdout, throwing with its stderr on failure. + * + * Both streams have to be piped. Bun defaults stderr to inherit, so it went to + * this process and never reached the model, and Bun.$ throws a ShellError whose + * message is only "Failed with exit code 1". Either way the CLI explained itself + * and nothing relayed the explanation. + * + * stdout is the fallback, not the source: worklane writes errors to stderr. + */ +async function run(cmd: string[], context: ToolContext): Promise { + const proc = Bun.spawn(["worklane", ...cmd], { + cwd: resolveWorklaneDir(context), + stdout: "pipe", + stderr: "pipe", + }); + + const [stdout, stderr, exitCode] = await Promise.all([ + new Response(proc.stdout).text(), + new Response(proc.stderr).text(), + proc.exited, + ]); + + if (exitCode !== 0) { + // anyhow prefixes its own "Error: ", and every caller here adds a prefix of + // its own, so drop it rather than relaying "Error adding comment: Error: ...". + const reason = stderr.trim().replace(/^Error:\s*/, ""); + throw new Error(reason || stdout.trim() || `worklane exited with code ${exitCode}`); + } + + return stdout.trim(); +} + export const init = tool({ description: "Initialize worklane in the current project. Creates the .worklane/ directory with backlog, committed, reviewing, and done subdirectories.", args: {}, async execute(_args: Record, context: ToolContext) { - const workdir = resolveWorklaneDir(context); try { - const result = await Bun.$`worklane init`.cwd(workdir).text(); - return result.trim(); + return await run(["init"], context); } catch (err: any) { return `Error initializing worklane: ${err.message}`; } @@ -50,7 +81,7 @@ export const add = tool({ if (!args.title) { return "Error adding ticket: title is required"; } - const cmd: (string | number)[] = ["add", "--title", args.title]; + const cmd: string[] = ["add", "--title", args.title]; if (args.description) { args.description .split("\n") @@ -79,26 +110,10 @@ export const add = tool({ if (args.due) cmd.push("--due", args.due); if (args.owner) cmd.push("--owner", args.owner); - const workdir = resolveWorklaneDir(context); try { - const proc = Bun.spawn(["worklane", ...cmd], { - cwd: workdir, - stdout: "pipe", - stderr: "pipe", - }); - const [stdout, stderr, exitCode] = await Promise.all([ - new Response(proc.stdout).text(), - new Response(proc.stderr).text(), - proc.exited, - ]); - if (exitCode !== 0) { - throw new Error( - stderr.trim() || `Exit code ${exitCode}, stderr: ${stderr}`, - ); - } - return stdout; + return await run(cmd, context); } catch (err: any) { - return `Error adding ticket: ${err.message}, cmd: ${cmd}`; + return `Error adding ticket: ${err.message}`; } }, }); @@ -113,19 +128,13 @@ export const ls = tool({ .optional(), }, async execute(args: Record, context: ToolContext) { - const workdir = resolveWorklaneDir(context); + const cmd = args.status ? ["ls", args.status] : ["ls"]; try { - if (args.status) { - const result = await Bun.$`worklane ls ${args.status}` - .cwd(workdir) - .text(); - return result.trim(); - } else { - const result = await Bun.$`worklane ls`.cwd(workdir).text(); - return result.trim(); - } + return await run(cmd, context); } catch (err: any) { - return `Error listing tickets: ${err.message}, Dir: ${workdir}`; + // The directory is worth naming here: the usual failure is that there is + // no .worklane above it. + return `Error listing tickets: ${err.message}, Dir: ${resolveWorklaneDir(context)}`; } }, }); @@ -142,10 +151,8 @@ export const show = tool({ if (!args.id) { return "Error showing ticket: id is required"; } - const workdir = resolveWorklaneDir(context); try { - const result = await Bun.$`worklane show ${args.id}`.cwd(workdir).text(); - return result.trim(); + return await run(["show", args.id], context); } catch (err: any) { return `Error showing ticket: ${err.message}`; } @@ -174,26 +181,11 @@ export const mv = tool({ if (!args.status) { return "Error moving ticket: status is required. Must be one of: backlog, committed, reviewing, done"; } - const cmd: (string | number)[] = [ - "mv", - args.ticket, - "--status", - args.status, - ]; + const cmd: string[] = ["mv", args.ticket, "--status", args.status]; if (args.force) cmd.push("--force"); - const workdir = resolveWorklaneDir(context); try { - const proc = Bun.spawn(["worklane", ...cmd], { cwd: workdir }); - const [stdout, stderr, exitCode] = await Promise.all([ - new Response(proc.stdout).text(), - new Response(proc.stderr).text(), - proc.exited, - ]); - if (exitCode !== 0) { - throw new Error(stderr.trim() || `Exit code ${exitCode}`); - } - return stdout.trim(); + return await run(cmd, context); } catch (err: any) { return `Error moving ticket: ${err.message}`; } @@ -222,21 +214,11 @@ export const comment = tool({ return "Error adding comment: body is required"; } - const cmd: (string | number)[] = ["comment", args.ticket, args.body]; + const cmd: string[] = ["comment", args.ticket, args.body]; if (args.owner) cmd.push("--owner", args.owner); - const workdir = resolveWorklaneDir(context); try { - const proc = Bun.spawn(["worklane", ...cmd], { cwd: workdir }); - const [stdout, stderr, exitCode] = await Promise.all([ - new Response(proc.stdout).text(), - new Response(proc.stderr).text(), - proc.exited, - ]); - if (exitCode !== 0) { - throw new Error(stderr.trim() || `Exit code ${exitCode}`); - } - return stdout.trim(); + return await run(cmd, context); } catch (err: any) { return `Error adding comment: ${err.message}`; }