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
6 changes: 5 additions & 1 deletion apps/hook/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
* PLANNOTATOR_PORT - Fixed port to use (default: random locally, 19432 for remote)
*/

import { childEnv, isAskAiChild } from "@plannotator/shared/child-env";
import { childEnv, GIT_REPO_ENV, isAskAiChild } from "@plannotator/shared/child-env";
import {
startPlannotatorServer as startPlannotatorServerUnguarded,
handleServerReady,
Expand Down Expand Up @@ -761,6 +761,10 @@ function resolveCliReviewDirectory(reviewArgs: ParsedReviewArgs, cwd: string): s
const target = resolveReviewTarget(reviewArgs, cwd);
const notice = formatIgnoredReviewWords(target);
if (notice) console.error(notice);
// The named directory is the repository under review: a GIT_DIR the
// calling shell exported (a git hook, a script) would send every git run
// of the session, and the processes it starts, to another repository.
if (target.directory) for (const name of GIT_REPO_ENV) delete process.env[name];
return target.directory;
} catch (error) {
console.error(error instanceof Error ? error.message : String(error));
Expand Down
31 changes: 30 additions & 1 deletion apps/hook/server/review-directory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function fixture() {
return { root, caller, target, dataDir };
}

async function launch(cwd: string, args: string[], dataDir: string, bridge = false, invocationCwd = cwd) {
async function launch(cwd: string, args: string[], dataDir: string, bridge = false, invocationCwd = cwd, extraEnv: Record<string, string> = {}) {
const ready = join(dataDir, `ready-${crypto.randomUUID()}.json`);
const proc = Bun.spawn([process.execPath, "run", entry, ...(bridge ? ["opencode-review-directory"] : ["review", ...args, "--json"])], {
cwd,
Expand All @@ -69,6 +69,7 @@ async function launch(cwd: string, args: string[], dataDir: string, bridge = fal
PLANNOTATOR_GIT_REMOTE_CHECK: "0",
PLANNOTATOR_REVIEW_PROGRESS: "1",
PLANNOTATOR_FEEDBACK_HISTORY: "1",
...extraEnv,
},
stdin: bridge ? new TextEncoder().encode(JSON.stringify({ arguments: args.map(arg => `"${arg}"`).join(" ") })) : "ignore",
stdout: "pipe",
Expand Down Expand Up @@ -187,6 +188,34 @@ for (const bridge of [false, true]) {
session.stop();
}
}, 15_000);

test(`${bridge ? "OpenCode bridge" : "CLI"}: a named directory wins over the caller's exported GIT_DIR`, async () => {
const { caller, target, dataDir } = fixture();
// As from a git hook of the caller's repo, or a shell that exported them.
const callerGit = { GIT_DIR: join(caller, ".git"), GIT_WORK_TREE: caller };
for (const env of [{ GIT_DIR: callerGit.GIT_DIR }, callerGit]) {
const session = await launch(caller, ["--diff-type", "uncommitted", target], dataDir, bridge, caller, env);
try {
const diff = await session.api("/api/diff");
expect(diff.gitContext.cwd).toBe(target);
expect(diff.rawPatch).toContain("+export const selected = 2;");
expect(diff.rawPatch).not.toContain("caller");
await session.api("/api/exit", {});
await session.finish();
} finally {
session.stop();
}
}
// With no directory named, the exported repository is still the one reviewed.
const session = await launch(target, ["--diff-type", "uncommitted"], dataDir, bridge, target, callerGit);
try {
expect((await session.api("/api/diff")).rawPatch).toContain("+export const caller = 2;");
await session.api("/api/exit", {});
await session.finish();
} finally {
session.stop();
}
}, 45_000);
}

test("direct CLI honors PLANNOTATOR_CWD only for an explicit directory", async () => {
Expand Down
7 changes: 6 additions & 1 deletion docs/agent-guides/code-review.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ 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
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. OpenCode uses the internal `opencode-review-directory`
to its detected VCS root. A named directory also removes `GIT_DIR` and its kin
(`GIT_REPO_ENV`, `@plannotator/shared/child-env`) from the CLI's env, so a value
the calling shell exported (a git hook, a script) cannot point the session's git
at another repository; with no directory named they still decide (Pi's
`/plannotator-review` runs in Pi's own process and does not change its env).
OpenCode uses the internal `opencode-review-directory`
command for these targets so an older CLI rejects them rather than silently
reviewing the caller's repo; ordinary reviews keep `opencode-review`.
Feedback still goes to the invoking agent
Expand Down
4 changes: 1 addition & 3 deletions packages/server/inbox-launch-clone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { spawn, type ChildProcess } from "node:child_process";
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
import { delimiter, dirname, isAbsolute, join, relative, sep } from "node:path";
import { inboxGitRemoteKey } from "@plannotator/core/inbox-git-url";
import { GIT_REPO_ENV } from "@plannotator/shared/child-env";
import { prepareGitCommand } from "@plannotator/shared/review-core";

/** Why a clone or a check failed, as the run line and the clone row say it. */
Expand Down Expand Up @@ -171,9 +172,6 @@ export function gitBinary(env: Readonly<Record<string, string>>): string {
return "/usr/bin/git";
}

/** What git is never handed from the login env: they point git at another repository's files. */
const GIT_REPO_ENV = ["GIT_DIR", "GIT_WORK_TREE", "GIT_INDEX_FILE", "GIT_OBJECT_DIRECTORY", "GIT_ALTERNATE_OBJECT_DIRECTORIES", "GIT_COMMON_DIR", "GIT_NAMESPACE", "GIT_CEILING_DIRECTORIES"];

/**
* One git command as it runs: the person's own env (their SSH agent, their credential helper, their proxy) with
* nothing that could ask (`prepareGitCommand`'s no-interaction mode: no terminal prompt, `credential.interactive=false`,
Expand Down
3 changes: 3 additions & 0 deletions packages/shared/child-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ export const SANDBOX_NO_KEYCHAIN_ENV: Readonly<Record<string, string>> = Object.
GH_NO_UPDATE_NOTIFIER: "1",
});

/** Variables that point git at another repository's files, whatever its cwd. */
export const GIT_REPO_ENV = ["GIT_DIR", "GIT_WORK_TREE", "GIT_INDEX_FILE", "GIT_OBJECT_DIRECTORY", "GIT_ALTERNATE_OBJECT_DIRECTORIES", "GIT_COMMON_DIR", "GIT_NAMESPACE", "GIT_CEILING_DIRECTORIES"] as const;

/**
* A fresh copy of the live `process.env`, with `extra` laid over it. A key in
* `extra` whose value is `undefined` removes that variable from the child's
Expand Down
Loading