(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) <noreply@anthropic.com>
This commit is contained in:
Ansho Rei
2026-08-20 14:10:11 +02:00
co-authored by Claude Opus 5
parent a434f82928
commit df97b06275
+47 -65
View File
@@ -5,15 +5,46 @@ function resolveWorklaneDir(context: ToolContext): string {
return path.join(context.worktree || context.directory || "."); 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<string> {
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({ export const init = tool({
description: description:
"Initialize worklane in the current project. Creates the .worklane/ directory with backlog, committed, reviewing, and done subdirectories.", "Initialize worklane in the current project. Creates the .worklane/ directory with backlog, committed, reviewing, and done subdirectories.",
args: {}, args: {},
async execute(_args: Record<string, any>, context: ToolContext) { async execute(_args: Record<string, any>, context: ToolContext) {
const workdir = resolveWorklaneDir(context);
try { try {
const result = await Bun.$`worklane init`.cwd(workdir).text(); return await run(["init"], context);
return result.trim();
} catch (err: any) { } catch (err: any) {
return `Error initializing worklane: ${err.message}`; return `Error initializing worklane: ${err.message}`;
} }
@@ -50,7 +81,7 @@ export const add = tool({
if (!args.title) { if (!args.title) {
return "Error adding ticket: title is required"; 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) { if (args.description) {
args.description args.description
.split("\n") .split("\n")
@@ -79,26 +110,10 @@ export const add = tool({
if (args.due) cmd.push("--due", args.due); if (args.due) cmd.push("--due", args.due);
if (args.owner) cmd.push("--owner", args.owner); if (args.owner) cmd.push("--owner", args.owner);
const workdir = resolveWorklaneDir(context);
try { try {
const proc = Bun.spawn(["worklane", ...cmd], { return await run(cmd, context);
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;
} catch (err: any) { } 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(), .optional(),
}, },
async execute(args: Record<string, any>, context: ToolContext) { async execute(args: Record<string, any>, context: ToolContext) {
const workdir = resolveWorklaneDir(context); const cmd = args.status ? ["ls", args.status] : ["ls"];
try { try {
if (args.status) { return await run(cmd, context);
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();
}
} catch (err: any) { } 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) { if (!args.id) {
return "Error showing ticket: id is required"; return "Error showing ticket: id is required";
} }
const workdir = resolveWorklaneDir(context);
try { try {
const result = await Bun.$`worklane show ${args.id}`.cwd(workdir).text(); return await run(["show", args.id], context);
return result.trim();
} catch (err: any) { } catch (err: any) {
return `Error showing ticket: ${err.message}`; return `Error showing ticket: ${err.message}`;
} }
@@ -174,26 +181,11 @@ export const mv = tool({
if (!args.status) { if (!args.status) {
return "Error moving ticket: status is required. Must be one of: backlog, committed, reviewing, done"; return "Error moving ticket: status is required. Must be one of: backlog, committed, reviewing, done";
} }
const cmd: (string | number)[] = [ const cmd: string[] = ["mv", args.ticket, "--status", args.status];
"mv",
args.ticket,
"--status",
args.status,
];
if (args.force) cmd.push("--force"); if (args.force) cmd.push("--force");
const workdir = resolveWorklaneDir(context);
try { try {
const proc = Bun.spawn(["worklane", ...cmd], { cwd: workdir }); return await run(cmd, context);
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();
} catch (err: any) { } catch (err: any) {
return `Error moving ticket: ${err.message}`; return `Error moving ticket: ${err.message}`;
} }
@@ -222,21 +214,11 @@ export const comment = tool({
return "Error adding comment: body is required"; 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); if (args.owner) cmd.push("--owner", args.owner);
const workdir = resolveWorklaneDir(context);
try { try {
const proc = Bun.spawn(["worklane", ...cmd], { cwd: workdir }); return await run(cmd, context);
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();
} catch (err: any) { } catch (err: any) {
return `Error adding comment: ${err.message}`; return `Error adding comment: ${err.message}`;
} }