From df97b062755ac206b63840553e2ceb186dace238 Mon Sep 17 00:00:00 2001 From: Ansho Rei Date: Thu, 20 Aug 2026 14:10:11 +0200 Subject: [PATCH] (3e8b6fa) Relay worklane's own error text from the opencode tools None of the five tools could report why a command failed. mv and comment spawned without stderr set, and Bun defaults it to inherit, so the text went to the plugin host and never to the model. show, ls and init used Bun.$, whose ShellError message is only "Failed with exit code 1", and each of them interpolated just that message. The CLI explained itself every time and nothing carried the explanation. Replace all four spawn and Bun.$ blocks with one run() helper that pipes both streams and throws with stderr, and strip anyhow's "Error: " prefix so a relayed message does not read "Error adding comment: Error: ...". Verified against the real binary with a Bun.spawn shim over node's child_process, since bun is not installed here: an ambiguous show, mv and comment now each return the candidate list to the caller. The file is twenty lines shorter than the duplication it replaces. Co-Authored-By: Claude Opus 5 (1M context) --- .opencode/tools/worklane.ts | 112 +++++++++++++++--------------------- 1 file changed, 47 insertions(+), 65 deletions(-) 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}`; }