Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/quiet-pipes-listen.md
Original file line number Diff line number Diff line change
@@ -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.
63 changes: 63 additions & 0 deletions src/commands/branch-stdin.test.ts
Original file line number Diff line number Diff line change
@@ -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<string> {
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.");
});
});
7 changes: 6 additions & 1 deletion src/commands/branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down