diff --git a/.github/scripts/pr-review.mjs b/.github/scripts/pr-review.mjs index 2cd68a5..a2eee3b 100644 --- a/.github/scripts/pr-review.mjs +++ b/.github/scripts/pr-review.mjs @@ -616,7 +616,7 @@ export function resumeSessionDir(settings = process.env) { const stateDirectory = settings.PR_REVIEW_STATE_DIR; const home = settings.PR_REVIEW_SESSION_HOME?.trim(); return join(home || (settings.LOCALAPPDATA ? join(settings.LOCALAPPDATA, "reims-go", "pr-review") : "pr-review"), - "sessions", stateKey(stateDirectory, settings)); + stateKey(stateDirectory, settings)); } function stateKey(stateDirectory, settings) { @@ -773,14 +773,8 @@ async function prepareGitWorkspace({ repo, home, repository, number, pull, token if (!/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(repository)) throw new Error("Invalid GitHub repository name"); const env = isolatedEnvironment(home); const fetchEnv = gitEnvironment(home, token); - const source = "https://github.com/" + repository + ".git"; git(repo, ["init", "--quiet", "--template="], env); - // The checkout survives between runs, so the remote is usually already there. - if (git(repo, ["remote"], env).stdout.split(/\r?\n/).some((name) => name.trim() === "origin")) { - git(repo, ["remote", "set-url", "origin", source], env); - } else { - git(repo, ["remote", "add", "origin", source], env); - } + git(repo, ["remote", "add", "origin", "https://github.com/" + repository + ".git"], env); let headRef = headSha; const fetchRange = (depth) => git(repo, ["fetch", "--no-tags", "--no-recurse-submodules", depth, "origin", baseSha, headRef], fetchEnv); try { @@ -807,9 +801,6 @@ async function prepareGitWorkspace({ repo, home, repository, number, pull, token if (!mergeBase) throw new Error("Git could not determine the PR merge base"); git(repo, ["config", "core.symlinks", "false"], env); git(repo, ["checkout", "--force", "--detach", headSha], env); - // A checkout reused between runs keeps files the new head deleted; OCR scans - // the worktree, so only tracked files of this head may remain. - git(repo, ["clean", "-ffdx"], env); // Keep PR-owned OCR settings out of the filesystem configuration search. await rm(join(repo, ".opencodereview"), { recursive: true, force: true }); return { @@ -904,17 +895,14 @@ export async function run() { const stateDirectory = required("PR_REVIEW_STATE_DIR"); await mkdir(stateDirectory, { recursive: true }); await writeFile(join(stateDirectory, "state.json"), JSON.stringify({ repository, pr: number }) + "\n"); - // Whatever this run replaces goes first: an aborted run leaves a checkout - // whose worktree may not match the new commits. - if (process.env.PR_REVIEW_FRESH_STATE === "true") { - await rm(join(resumeSessionDir(), "repo"), { recursive: true, force: true }); - await rm(join(resumeSessionDir(), "home"), { recursive: true, force: true }); - } - // The checkout and the OCR home survive between runs so a review that stopped - // short continues instead of paying for the same files again. The OCR config - // records how to read the token (printenv), never the token itself. + // The OCR home survives between runs, so a review that stopped short continues + // from its session records instead of paying for the same files again. It + // records how to read the token (printenv), never the token itself. The + // checkout is rebuilt every run: a worktree reused across heads keeps files + // the new head deleted, and only the session records are worth keeping. const repo = join(resumeSessionDir(), "repo"); const home = join(resumeSessionDir(), "home"); + await rm(repo, { recursive: true, force: true }); await mkdir(repo, { recursive: true }); await mkdir(home, { recursive: true }); const workspace = await prepareGitWorkspace({ repo, home, repository, number, pull, token }); diff --git a/.github/scripts/pr-review.test.mjs b/.github/scripts/pr-review.test.mjs index b6f1403..85802bf 100644 --- a/.github/scripts/pr-review.test.mjs +++ b/.github/scripts/pr-review.test.mjs @@ -513,11 +513,11 @@ test("each pull request gets its own review state", async (t) => { const directory = await tempDirectory(t, "ocr-state-"); await writeFile(join(directory, "state.json"), JSON.stringify({ repository: "Nwflower/Reims-Go", pr: 163 })); const dir = resumeSessionDir({ PR_REVIEW_STATE_DIR: directory, PR_REVIEW_SESSION_HOME: join(directory, "home") }); - assert.equal(dir, join(directory, "home", "sessions", "Nwflower-Reims-Go-163")); + assert.equal(dir, join(directory, "home", "Nwflower-Reims-Go-163")); const other = await tempDirectory(t, "ocr-state-"); await writeFile(join(other, "state.json"), "not json"); assert.equal(resumeSessionDir({ PR_REVIEW_STATE_DIR: other, PR_REVIEW_SESSION_HOME: join(other, "home"), GITHUB_WORKSPACE: "/w/repo" }), - join(other, "home", "sessions", "repo")); + join(other, "home", "repo")); }); test("an incomplete previous review is recognised and never anchors an incremental range", () => { diff --git a/.github/workflows/pr-review.yml b/.github/workflows/pr-review.yml index 9e366a1..a0ff81c 100644 --- a/.github/workflows/pr-review.yml +++ b/.github/workflows/pr-review.yml @@ -60,22 +60,23 @@ jobs: path: ${{ runner.temp }}/ocr-bin key: opencodereview-${{ env.OCR_VERSION }}-${{ env.OCR_SHA256 }} - # Keep the OCR session records and the checkout between runs. A review - # that stops at its token budget continues from here on the next run - # instead of paying for the same files again. - # One entry per pull request: a cache entry cannot be overwritten, so a key - # with the run id would add a full checkout per run until the quota runs - # out. The entry seeds the next run; the staying-ahead work happens inside - # a single job. - - name: Cache PR review state + # Only the OCR session records are kept: the checkout is rebuilt each run, + # and the session records are what let a review continue where it stopped. + # A cache entry cannot be overwritten, so each run saves under its own key + # and the next run restores the newest entry with the prefix below; GitHub + # evicts entries that stay unused for seven days, and least recently used + # ones when the repository reaches its cache quota. + - name: Cache PR review sessions uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4 with: - path: ${{ runner.temp }}/pr-review-state - key: pr-review-state-${{ github.repository }}-${{ github.event.pull_request.number || inputs.pull_number }} + path: ${{ runner.temp }}/pr-review-state/sessions + key: pr-review-sessions-${{ github.repository }}-${{ github.event.pull_request.number || inputs.pull_number }}-${{ github.run_id }} + restore-keys: | + pr-review-sessions-${{ github.repository }}-${{ github.event.pull_request.number || inputs.pull_number }}- - name: Ensure PR review state directories shell: bash - run: mkdir -p "$RUNNER_TEMP/pr-review-state" "$RUNNER_TEMP/pr-review-state/sessions" "$RUNNER_TEMP/pr-review-state/repos" + run: mkdir -p "$RUNNER_TEMP/pr-review-state/sessions" - name: Download OpenCodeReview if: steps.ocr-cache.outputs.cache-hit != 'true' @@ -104,7 +105,7 @@ jobs: PR_REVIEW_LLM_PROTOCOL: ${{ vars.PR_REVIEW_LLM_PROTOCOL || 'anthropic' }} PR_REVIEW_LLM_EXTRA_BODY: ${{ vars.PR_REVIEW_LLM_EXTRA_BODY }} PR_REVIEW_CODE_SUGGESTIONS: ${{ vars.PR_REVIEW_CODE_SUGGESTIONS }} - PR_REVIEW_STATE_DIR: ${{ runner.temp }}/pr-review-state - PR_REVIEW_SESSION_HOME: ${{ runner.temp }}/pr-review-state + PR_REVIEW_STATE_DIR: ${{ runner.temp }}/pr-review-state/sessions + PR_REVIEW_SESSION_HOME: ${{ runner.temp }}/pr-review-state/sessions PR_REVIEW_REUSE_INCOMPLETE: "true" run: node .github/scripts/pr-review.mjs