diff --git a/.changeset/quiet-pipes-listen.md b/.changeset/quiet-pipes-listen.md new file mode 100644 index 00000000..309dcef8 --- /dev/null +++ b/.changeset/quiet-pipes-listen.md @@ -0,0 +1,5 @@ +--- +"@northguild/worktree": patch +--- + +`worktree branch --agent-stdin` now reads the piped brief when the branch comes from an issue flag (`--linear`, `--github`, `--jira`, `--clickup`) instead of failing with "The agent brief from --agent-stdin is empty", so `worktree linear-listen` delegations start their agent. diff --git a/src/commands/branch-stdin.test.ts b/src/commands/branch-stdin.test.ts new file mode 100644 index 00000000..06692e37 --- /dev/null +++ b/src/commands/branch-stdin.test.ts @@ -0,0 +1,63 @@ +import { Parser } from "@oclif/core"; +import { afterEach, describe, expect, it } from "vitest"; +import Branch from "./branch.js"; + +// The real oclif parse, not an injected `BriefStdin`: oclif fills a positional +// that was not given from piped stdin, which drained the pipe `--agent-stdin` +// reads its brief from (#359). +const realStdin = Object.getOwnPropertyDescriptor(process, "stdin"); + +async function parseWithPipedStdin(argv: string[], piped: string) { + const { Readable } = await import("node:stream"); + const stdin = Object.assign(Readable.from([piped]), { isTTY: undefined }); + Object.defineProperty(process, "stdin", { value: stdin, configurable: true }); + const cache = globalThis as { oclif?: { stdinCache?: string } }; + if (cache.oclif) cache.oclif.stdinCache = undefined; + + const parsed = await Parser.parse(argv, { + args: Branch.args, + flags: Branch.flags, + strict: Branch.strict, + }); + + return { parsed, remaining: await drain(process.stdin) }; +} + +async function drain(stream: NodeJS.ReadableStream): Promise { + let text = ""; + for await (const chunk of stream) text += chunk; + return text; +} + +afterEach(() => { + if (realStdin) Object.defineProperty(process, "stdin", realStdin); + const cache = globalThis as { oclif?: { stdinCache?: string } }; + if (cache.oclif) cache.oclif.stdinCache = undefined; +}); + +describe("branch parse with a piped brief", () => { + it.each([ + "--linear", + "--github", + "--jira", + "--clickup", + ])("leaves the brief on stdin with %s and no branch name", async (flag) => { + const { parsed, remaining } = await parseWithPipedStdin( + [flag, "ABC-1", "--agent-stdin"], + "Work on ABC-1.", + ); + + expect(parsed.args.branchName).toBeUndefined(); + expect(remaining).toBe("Work on ABC-1."); + }); + + it("leaves the brief on stdin with no issue flag and no branch name", async () => { + const { parsed, remaining } = await parseWithPipedStdin( + ["--agent-stdin"], + "Work on ABC-1.", + ); + + expect(parsed.args.branchName).toBeUndefined(); + expect(remaining).toBe("Work on ABC-1."); + }); +}); diff --git a/src/commands/branch.ts b/src/commands/branch.ts index 57834c7f..aa59ec5d 100644 --- a/src/commands/branch.ts +++ b/src/commands/branch.ts @@ -146,7 +146,12 @@ function markBareLinear(argv: string[], booleanChars: string[]): string[] { export default class Branch extends BaseCommand { static override args = { - branchName: Args.string({ description: "Name of the branch to create" }), + // oclif fills a positional that was not given from piped stdin, which would + // drain the pipe `--agent-stdin` reads the brief from (#359). + branchName: Args.string({ + description: "Name of the branch to create", + ignoreStdin: true, + }), }; // The first line is the summary oclif lists in `worktree --help`; the rest is // the `--json` shape and exit codes, kept to key names and nullability.