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
4 changes: 3 additions & 1 deletion apps/hook/server/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,9 @@ export const SUBCOMMAND_HELP: Record<string, string> = {
" Relative to the invoking directory; quote paths containing spaces.",
"",
" Accepts one DIRECTORY or PR_URL. Neither can be combined with --patch-file.",
" A sole word or a path-like word (./x, ../x, a/b, ~/x) is a directory target;",
" A sole word is a directory target: if no such folder exists (or it is a file),",
" the command stops with an error and opens nothing. Among several words, a",
" path-like word (./x, ../x, a/b, ~/x) that names a folder is the target;",
" other words are ignored with a notice.",
"",
"JSON output:",
Expand Down
57 changes: 56 additions & 1 deletion apps/hook/server/review-directory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync,
import { tmpdir } from "node:os";
import { join, relative, resolve } from "node:path";
import { removeStubbedHtml, stubHookDistHtml } from "../../../tests/helpers/hook-dist-stub";
import { NO_BROWSER } from "../../../tests/helpers/no-browser";

const entry = resolve(import.meta.dir, "index.ts");
const roots: string[] = [];
Expand Down Expand Up @@ -272,10 +273,64 @@ test("invalid targets exit before opening the invoking repository", async () =>
]);
expect(code).toBe(1);
expect(stdout).toBe("");
expect(stderr).toMatch(/does not exist|not a directory|only one directory/);
expect(stderr).toMatch(/No folder|is a file, not a folder|only one directory/);
expect(existsSync(ready)).toBe(false);
} finally {
proc.kill();
}
}
}, 15_000);

/** Runs `review <args>` (or the OpenCode bridge with those arguments) to its exit. */
async function runToExit(cwd: string, args: string[], dataDir: string, bridge: boolean) {
const ready = join(dataDir, `unexpected-ready-${crypto.randomUUID()}.json`);
const proc = Bun.spawn([process.execPath, "run", entry, ...(bridge ? ["opencode-review"] : ["review", ...args])], {
cwd,
env: {
...process.env, PLANNOTATOR_CWD: cwd, PLANNOTATOR_DATA_DIR: dataDir, PLANNOTATOR_READY_FILE: ready,
PLANNOTATOR_BROWSER: NO_BROWSER, PLANNOTATOR_SKIP_BROWSER_OPEN: "1", PLANNOTATOR_AI: "disabled",
},
stdin: bridge ? new TextEncoder().encode(JSON.stringify({ arguments: args.join(" ") })) : "ignore",
stdout: "pipe", stderr: "pipe",
});
try {
const [stdout, stderr, code] = await Promise.all([
new Response(proc.stdout).text(), new Response(proc.stderr).text(), proc.exited,
]);
return { stdout, stderr, code, opened: existsSync(ready) };
} finally {
proc.kill();
}
}

for (const bridge of [false, true]) {
test(`${bridge ? "OpenCode bridge" : "CLI"}: a sole folder name that is not there stops and says what to run`, async () => {
const { caller, dataDir } = fixture();
mkdirSync(join(caller, "backend"));
const typo = await runToExit(caller, ["backnd"], dataDir, bridge);
expect(typo).toMatchObject({ code: 1, stdout: "", opened: false });
expect(typo.stderr).toContain(
`No folder "backnd" in ${caller}; nothing was opened.\n` +
"Did you mean backend? Run: plannotator review backend\n" +
"To review the current project instead, run: plannotator review\n",
);
const unknown = await runToExit(caller, ["--diff-type", "uncommitted", "otherproj"], dataDir, bridge);
expect(unknown).toMatchObject({ code: 1, stdout: "", opened: false });
expect(unknown.stderr).toContain(`No folder "otherproj" in ${caller}; nothing was opened.\n`);
expect(unknown.stderr).toContain("plannotator review <path to an existing folder>");
}, 20_000);
}

test("prose of several words still reviews the invoking repository", async () => {
const { caller, dataDir } = fixture();
for (const args of [["please", "review", "my", "changes"], ["look", "at", "the", "api/users", "code"]]) {
const session = await launch(caller, args, dataDir);
try {
expect((await session.api("/api/diff")).rawPatch).toContain("+export const caller = 2;");
await session.api("/api/exit", {});
await session.finish();
} finally {
session.stop();
}
}
}, 30_000);
6 changes: 4 additions & 2 deletions apps/marketing/src/content/docs/commands/code-review.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ repositories together. The target selects the review workspace, not a file filte
Feedback returns to the invoking session and names the directory active at submission,
including after switching worktrees.

Supply one directory or PR URL. A sole word is a directory target: a file, or a
missing path-shaped word (containing `/`, or starting with `.` or `~`), is an error.
Supply one directory or PR URL. A sole word is a directory target: if no such
folder exists, or it is a file, the command stops and opens nothing. The error
names the folder it looked in, suggests a close folder name when there is one
("Did you mean src/components?"), and shows the command to run instead.
Inside a sentence, a path-shaped word that names an existing directory is the target;
one that names nothing or a file is prose. Two directories, or a directory combined
with `--patch-file`, is an error. Other words are ignored as prose with a notice, so
Expand Down
9 changes: 7 additions & 2 deletions apps/opencode-plugin/cli-bridge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,14 @@ process.exit(1);
// A sole path-shaped typo is refused before the CLI runs, visibly.
rmSync(opened);
client.tui.showToast.mockClear();
await handleCliCommand({ command: "plannotator-review", client, sessionId: "caller", cwd: path.join(root, "caller"), rawArgs: "./backnd" });
await handleCliCommand({ command: "plannotator-review", client, sessionId: "caller", cwd: path.join(root, "caller"), rawArgs: "../targt" });
expect(existsSync(opened)).toBe(false);
expect(toasts()).toContain("does not exist");
expect(toasts()).toContain("Did you mean ../target? Run: plannotator review ../target");
// So is a sole bare word that names no folder.
client.tui.showToast.mockClear();
await handleCliCommand({ command: "plannotator-review", client, sessionId: "caller", cwd: path.join(root, "caller"), rawArgs: "backnd" });
expect(existsSync(opened)).toBe(false);
expect(toasts()).toContain(`No folder "backnd" in ${path.join(root, "caller")}; nothing was opened.`);
} finally {
if (previous === undefined) delete process.env.PLANNOTATOR_BIN;
else process.env.PLANNOTATOR_BIN = previous;
Expand Down
10 changes: 8 additions & 2 deletions apps/opencode-plugin/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ describe("handleReviewCommand open state (--base / --diff-type)", () => {
const caller = initGitRepo("caller");
const target = initGitRepo("target");
mkdirSync(path.join(target, "src"));
mkdirSync(path.join(caller, "src"));
const hostCwd = process.cwd();
writeFileSync(path.join(caller, "caller.ts"), "caller-only\n");
writeFileSync(path.join(target, "target.ts"), "target-only\n");
Expand Down Expand Up @@ -182,9 +183,14 @@ describe("handleReviewCommand open state (--base / --diff-type)", () => {
deps.startReviewServer.mockClear();
await handleReviewCommand({ properties: { arguments: "./missing-directory", sessionID: "original-session" } }, deps as any);
expect(deps.startReviewServer).not.toHaveBeenCalled();
expect(deps.client.app.log.mock.calls.map((call: any[]) => call[0].message).join("\n")).toContain("does not exist");
expect(deps.client.app.log.mock.calls.map((call: any[]) => call[0].message).join("\n")).toContain('No folder "./missing-directory"');
// app.log never reaches the TUI, so the refusal must also be toasted.
expect(deps.client.tui.showToast.mock.calls.map((call: any[]) => call[0].body.message).join("\n")).toContain("does not exist");
expect(deps.client.tui.showToast.mock.calls.map((call: any[]) => call[0].body.message).join("\n")).toContain('No folder "./missing-directory"');

// A sole bare folder name that is not there stops too, naming the close folder.
await handleReviewCommand({ properties: { arguments: "srx", sessionID: "original-session" } }, deps as any);
expect(deps.startReviewServer).not.toHaveBeenCalled();
expect(deps.client.tui.showToast.mock.calls.map((call: any[]) => call[0].body.message).join("\n")).toContain("Did you mean src? Run: plannotator review src");

// Prose that names no directory keeps the pre-directory behavior (#1483):
// the invoking repo is reviewed and the ignored words are reported.
Expand Down
5 changes: 3 additions & 2 deletions apps/pi-extension/review-args-parity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ describe("vendored review-args parity", () => {
expect(target("look at the api/users code").directory).toBeUndefined();
expect(target("look at ./notes.txt please").ignored).toContain("./notes.txt");
expect(target("look at ./backend please").directory).toBe(join(root, "backend"));
expect(() => target("./backnd")).toThrow("does not exist");
expect(() => target("./notes.txt")).toThrow("not a directory");
expect(() => target("./backnd")).toThrow("Did you mean ./backend? Run: plannotator review ./backend");
expect(() => target("backnd")).toThrow(`No folder "backnd" in ${root}; nothing was opened.`);
expect(() => target("./notes.txt")).toThrow("is a file, not a folder");
} finally {
rmSync(root, { recursive: true, force: true });
}
Expand Down
17 changes: 13 additions & 4 deletions docs/agent-guides/code-review.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,19 @@ through each server's real spawn path, on Windows only: the Windows workflow's
and Pi) accepts one local repository/worktree or multi-repo workspace directory.
The shared `parseReviewArgs` + `resolveReviewTarget` resolve it against the
invoking terminal/session cwd, validate it before discovery, and reject multiple
targets or a directory combined with `--patch-file`. Prose stays tolerated
(#1483, the annotate-target precedent): a sole word or any path-shaped word is a
directory candidate (path-shaped typos and files error), every other word is
ignored with a notice and the invoking cwd is reviewed. Hosts pass the resolved cwd
targets or a directory combined with `--patch-file`. A sole word is the target:
when it names no folder, or a file, every host refuses it (CLI exit 1, OpenCode
log and toast, Pi notify) and nothing opens, as `annotate` refuses a target that
names nothing. The message names the word and the directory it was resolved
against, then the commands to run: a close existing folder ("Did you mean …?",
each missing path segment matched against its parent's folders), `--diff-type`
for a diff mode's name, the PR URL form for a number, and `plannotator review`
for the current project. Two sole words stay prose: a quoted sentence (a word
with whitespace that is not path-shaped) and a bare word beside `--patch-file`.
Prose of several words stays tolerated (#1483): a path-shaped word naming a
folder is the target, every other word (including a missing or file path-shaped
word) is ignored with a notice and the invoking cwd is reviewed. A folder that
exists but holds no repository fails in VCS discovery. Hosts pass the resolved cwd
through existing VCS/workspace discovery and project detection; never use
`process.chdir()` in a plugin host. Explicit targets inside a repository normalize
to its detected VCS root. A named directory also removes `GIT_DIR` and its kin
Expand Down
53 changes: 48 additions & 5 deletions packages/shared/review-args.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ describe("review directory targets", () => {
try {
mkdirSync(join(root, "repo with spaces"));
mkdirSync(join(root, "backend"));
mkdirSync(join(root, "myproject"));
mkdirSync(join(root, "src", "components"), { recursive: true });
writeFileSync(join(root, "file"), "not a directory");
symlinkSync(join(root, "repo with spaces"), join(root, "link"), "dir");
run(root);
Expand All @@ -304,16 +306,55 @@ describe("review directory targets", () => {
expect(target("backend", root).directory).toBe(join(root, "backend"));
expect(target("~", root).directory).toBe(homedir());
expect(target("", root)).toEqual({ ignored: [] });
expect(() => target("file", root)).toThrow("not a directory");
expect(() => target("file", root)).toThrow(
`${join(root, "file")} is a file, not a folder; nothing was opened.\n` +
"To comment on the file, run: plannotator annotate file\n" +
"To review the current project instead, run: plannotator review",
);
});
});

test("a sole path-shaped typo fails loudly instead of reviewing the caller's repo", () => {
withTree((root) => {
for (const input of ["./missing", "../missing", "missing/sub", "~/plannotator-missing-dir-xyz"]) {
expect(() => target(input, root)).toThrow("does not exist");
for (const input of ["./missing", "../missing", "missing/sub"]) {
expect(() => target(input, root)).toThrow(`No folder "${input}" in ${root}; nothing was opened.`);
}
expect(() => target("./file", root)).toThrow("not a directory");
const home = join(homedir(), "plannotator-missing-dir-xyz");
expect(() => target("~/plannotator-missing-dir-xyz", root)).toThrow(`No folder at ${home}; nothing was opened.`);
expect(() => target("./file", root)).toThrow("is a file, not a folder");
});
});

test("a sole bare word that names no folder stops with commands that recover in one step", () => {
withTree((root) => {
// The reported case: a folder name that is not there must not review the caller's repo.
expect(() => target("myproj", root)).toThrow(
`No folder "myproj" in ${root}; nothing was opened.\n` +
"Did you mean myproject? Run: plannotator review myproject\n" +
"To review the current project instead, run: plannotator review",
);
expect(() => target("please", root)).toThrow(
`No folder "please" in ${root}; nothing was opened.\n` +
"To review another folder, run: plannotator review <path to an existing folder>\n" +
"To review the current project instead, run: plannotator review",
);
// Flags are not words: the folder is still the sole word.
expect(() => target("--diff-type uncommitted myproj", root)).toThrow("Did you mean myproject?");
expect(() => target("staged", root)).toThrow("run: plannotator review --diff-type staged");
expect(() => target("1483", root)).toThrow("pass its URL: plannotator review https://github.com/<owner>/<repo>/pull/1483");
});
});

test("did you mean: a typo'd segment is matched against its parent's folders, as typed", () => {
withTree((root) => {
expect(() => target("src/componnets", root)).toThrow("Did you mean src/components? Run: plannotator review src/components");
expect(() => target("./src/compnents/", root)).toThrow("Did you mean ./src/components?");
expect(() => target("srx/components", root)).toThrow("Did you mean src/components?");
expect(() => target(join(root, "backnd"), root)).toThrow(`Did you mean ${join(root, "backend")}?`);
expect(() => target('"repo with spacs/"', root)).toThrow('Run: plannotator review "repo with spaces"');
// A file is not offered as a folder, and a far name is not offered at all.
expect(() => target("fil/", root)).toThrow("To review another folder");
expect(() => target("zzzzzz", root)).toThrow("To review another folder");
});
});

Expand Down Expand Up @@ -341,7 +382,9 @@ describe("review directory targets", () => {
test("prose that names no directory falls back to the invoking cwd (#1483)", () => {
withTree((root) => {
expect(target("please review my changes", root)).toEqual({ ignored: ["please", "review", "my", "changes"] });
expect(target("focus", root)).toEqual({ ignored: ["focus"] });
// A quoted sentence is one word, still prose; a sole bare word is a target.
expect(target('"please review my changes"', root)).toEqual({ ignored: ["please review my changes"] });
expect(() => target("focus", root)).toThrow('No folder "focus"');
// In prose a bare word that happens to match a directory never hijacks
// the review; a path-shaped word is the target.
expect(target("please review backend", root)).toEqual({ ignored: ["please", "review", "backend"] });
Expand Down
Loading
Loading