From 2c57c3f08e3b485b900663b28c0a49f49632c556 Mon Sep 17 00:00:00 2001 From: Michael Ramos Date: Sat, 14 Nov 2026 16:41:09 -0500 Subject: [PATCH] fix(review): a named directory is reviewed even when the shell exported GIT_DIR plannotator review run with GIT_DIR (or GIT_WORK_TREE) set by the calling shell, as in a git hook or a script, showed the diff of the repository those variables name instead of the folder's. The CLI now removes them once a directory is named, before any git runs; with no directory they still decide. The list moves to @plannotator/shared/child-env, shared with the Inbox's clone. --- apps/hook/server/index.ts | 6 ++++- apps/hook/server/review-directory.test.ts | 31 ++++++++++++++++++++++- docs/agent-guides/code-review.md | 7 ++++- packages/server/inbox-launch-clone.ts | 4 +-- packages/shared/child-env.ts | 3 +++ 5 files changed, 45 insertions(+), 6 deletions(-) diff --git a/apps/hook/server/index.ts b/apps/hook/server/index.ts index b2a9634325..c7eab77442 100644 --- a/apps/hook/server/index.ts +++ b/apps/hook/server/index.ts @@ -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, @@ -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)); diff --git a/apps/hook/server/review-directory.test.ts b/apps/hook/server/review-directory.test.ts index 6be1ef594b..5adf921388 100644 --- a/apps/hook/server/review-directory.test.ts +++ b/apps/hook/server/review-directory.test.ts @@ -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 = {}) { const ready = join(dataDir, `ready-${crypto.randomUUID()}.json`); const proc = Bun.spawn([process.execPath, "run", entry, ...(bridge ? ["opencode-review-directory"] : ["review", ...args, "--json"])], { cwd, @@ -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", @@ -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 () => { diff --git a/docs/agent-guides/code-review.md b/docs/agent-guides/code-review.md index 8ef6abca7b..d8c9652cac 100644 --- a/docs/agent-guides/code-review.md +++ b/docs/agent-guides/code-review.md @@ -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 diff --git a/packages/server/inbox-launch-clone.ts b/packages/server/inbox-launch-clone.ts index 2cfb386b8f..b8fa2cbb74 100644 --- a/packages/server/inbox-launch-clone.ts +++ b/packages/server/inbox-launch-clone.ts @@ -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. */ @@ -171,9 +172,6 @@ export function gitBinary(env: Readonly>): 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`, diff --git a/packages/shared/child-env.ts b/packages/shared/child-env.ts index ec628cc569..81de545fb8 100644 --- a/packages/shared/child-env.ts +++ b/packages/shared/child-env.ts @@ -39,6 +39,9 @@ export const SANDBOX_NO_KEYCHAIN_ENV: Readonly> = 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