@expo/code-review-cli 0.2.2 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -84,9 +84,11 @@ Options (most to least common):
84
84
  | `--no-fail` | Always exit 0 (otherwise a `request_changes` decision exits non-zero). |
85
85
  | `-h`, `--help` | Show help. |
86
86
 
87
- `--pr` uses the PR's diff (authoritative) but reads your checked-out files for
88
- surrounding context; for full fidelity, `gh pr checkout <n>` first and run a plain
89
- `ecr review`.
87
+ `--pr` uses the PR's diff (authoritative) and checks the PR head out into a
88
+ throwaway worktree so the agents' surrounding-source reads and the verifier see the
89
+ PR's versions of files — no manual `gh pr checkout` needed, and your working tree is
90
+ left untouched. (If that materialization can't run — e.g. not a git checkout — it
91
+ falls back to reading the current working directory.)
90
92
 
91
93
  In CI it runs automatically from the scaffolded workflows — by label or a `/review`
92
94
  comment (see **CI usage**). From Claude Code (or another agent), add a slash command
@@ -67,7 +67,28 @@ export async function runReview(source, options) {
67
67
  });
68
68
  return output;
69
69
  }
70
+ // Prepare auth BEFORE the chdir below: it doesn't depend on the working directory,
71
+ // and doing it first means nothing that can throw sits between the chdir and the
72
+ // guarded blocks — so a prepareAuth failure can't leak the worktree or leave cwd
73
+ // pointing at it.
70
74
  const auth = await prepareAuth(config);
75
+ // Read the PR-head tree (not the current checkout) when the source can materialize
76
+ // it, so the agents' surrounding-source reads and the verifier's re-reads see the
77
+ // versions that match the diff. Config is already fully loaded in memory, so the
78
+ // chdir doesn't affect it; run-log/patch paths are absolute; gh/git calls already
79
+ // ran above. Fails soft to the current directory.
80
+ const originalCwd = process.cwd();
81
+ const readRoot = (await source.prepareReadRootAsync?.()) ?? null;
82
+ const restoreCwd = async () => {
83
+ if (readRoot) {
84
+ process.chdir(originalCwd);
85
+ await readRoot.cleanup();
86
+ }
87
+ };
88
+ if (readRoot) {
89
+ progress('Reviewing the PR-head tree (so reads match the PR, not the checkout).');
90
+ process.chdir(readRoot.dir);
91
+ }
71
92
  progress('Starting OpenCode server…');
72
93
  let handle = null;
73
94
  try {
@@ -75,6 +96,7 @@ export async function runReview(source, options) {
75
96
  }
76
97
  catch (error) {
77
98
  await auth.cleanup();
99
+ await restoreCwd();
78
100
  throw new Error(`Failed to start the OpenCode server. Ensure the \`opencode\` CLI is installed and ` +
79
101
  `model credentials are configured.\n${errorMessage(error)}`);
80
102
  }
@@ -382,6 +404,7 @@ export async function runReview(source, options) {
382
404
  finally {
383
405
  handle?.close();
384
406
  await auth.cleanup();
407
+ await restoreCwd();
385
408
  }
386
409
  }
387
410
  /**
@@ -1,3 +1,6 @@
1
+ import { mkdtemp, rm } from 'node:fs/promises';
2
+ import { tmpdir } from 'node:os';
3
+ import path from 'node:path';
1
4
  import { run } from '../core/exec.js';
2
5
  import { parseUnifiedDiff } from '../core/diff.js';
3
6
  /**
@@ -33,4 +36,47 @@ export class GitHubPRSource {
33
36
  const { stdout } = await run('gh', ['pr', 'diff', String(this.options.prNumber), ...this.repoArgs()], { cwd: this.options.cwd });
34
37
  return parseUnifiedDiff(stdout);
35
38
  }
39
+ /**
40
+ * Check the PR HEAD out into a throwaway git worktree so the agents and verifier
41
+ * read the PR's versions of files (not whatever branch happens to be checked out).
42
+ * Fetches the head from the repo's own URL — `refs/pull/<n>/head`, which the base
43
+ * repo hosts even for fork PRs — so it's always the correct PR, independent of the
44
+ * local `origin`. Fails SOFT: any problem (not a git repo, fetch/worktree error)
45
+ * returns null, and the review falls back to reading the current checkout.
46
+ */
47
+ async prepareReadRootAsync() {
48
+ const cwd = this.options.cwd;
49
+ if (!this.options.repo) {
50
+ // Without an explicit owner/repo we can't build the fetch URL safely.
51
+ return null;
52
+ }
53
+ const url = `https://github.com/${this.options.repo}.git`;
54
+ const ref = `refs/pull/${this.options.prNumber}/head`;
55
+ let parent;
56
+ try {
57
+ await run('git', ['fetch', '--no-tags', '--depth=1', url, ref], { cwd });
58
+ parent = await mkdtemp(path.join(tmpdir(), 'ecr-prhead-'));
59
+ const dir = path.join(parent, 'head'); // must not pre-exist for `worktree add`
60
+ await run('git', ['worktree', 'add', '--detach', dir, 'FETCH_HEAD'], { cwd });
61
+ const removeParent = parent;
62
+ return {
63
+ dir,
64
+ cleanup: async () => {
65
+ try {
66
+ await run('git', ['worktree', 'remove', '--force', dir], { cwd });
67
+ }
68
+ catch {
69
+ // best effort — fall through to removing the temp dir
70
+ }
71
+ await rm(removeParent, { recursive: true, force: true });
72
+ },
73
+ };
74
+ }
75
+ catch {
76
+ if (parent) {
77
+ await rm(parent, { recursive: true, force: true }).catch(() => { });
78
+ }
79
+ return null;
80
+ }
81
+ }
36
82
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@expo/code-review-cli",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "description": "Generic, config-driven AI code reviewer engine. Repos supply their agents via .expo-code-review/.",
5
5
  "license": "MIT",
6
6
  "repository": {