From 4d18ea811db5335af2dc129dbf8bddc077eba45b Mon Sep 17 00:00:00 2001 From: bincxz <16399091+binaricat@users.noreply.github.com> Date: Fri, 24 Jul 2026 04:27:59 +0800 Subject: [PATCH] fix(ci): harden Cursor automation until Codex CLI review is clean Address multi-round Codex CLI findings: trusted control markers, reaction-only clean detection with head pins, draft-until-clean gates, secret leak scans isolated from agent git hooks, fix eligibility limited to maintainers/actions bot, P3 handoff, open-PR-only bot PR detection, and related safety fixes. --- .github/workflows/cursor-automation.yml | 925 ++++++++++++++++++++++-- scripts/cursor-automation.cjs | 580 ++++++++++++--- scripts/cursor-automation.test.cjs | 383 +++++++++- 3 files changed, 1717 insertions(+), 171 deletions(-) diff --git a/.github/workflows/cursor-automation.yml b/.github/workflows/cursor-automation.yml index ca08591f1..2d207d59c 100644 --- a/.github/workflows/cursor-automation.yml +++ b/.github/workflows/cursor-automation.yml @@ -14,6 +14,10 @@ on: # Codex often finishes as a submitted PR review (plus optional issue summary). pull_request_review: types: [submitted] + # Reaction-only clean (👍 on @codex request) does not emit review events; + # poll open automation PRs so the loop can observe and mark ready. + schedule: + - cron: '*/20 * * * *' workflow_dispatch: inputs: issue_number: @@ -36,16 +40,23 @@ concurrency: }} cancel-in-progress: false +# Default least privilege: route/bootstrap only need read. Jobs that mutate +# issues/PRs/contents declare their own write permissions. permissions: - contents: write - issues: write - pull-requests: write + contents: read + issues: read + pull-requests: read actions: read jobs: route: name: Route event runs-on: ubuntu-latest + permissions: + contents: read + issues: read + pull-requests: read + actions: read outputs: kind: ${{ steps.decide.outputs.kind }} issue_number: ${{ steps.decide.outputs.issue_number }} @@ -101,6 +112,12 @@ jobs: core.setOutput('reason', extra.reason || kind); }; + if (event === 'schedule') { + return set('codex_poll', { + reason: 'scheduled poll for reaction-only clean / expired requests', + }); + } + if (event === 'workflow_dispatch') { if (process.env.DISPATCH_ISSUE) { return set('issue_classify', { @@ -230,18 +247,16 @@ jobs: } if (context.payload.action === 'synchronize') { const sender = String(context.payload.sender?.login || ''); - // Bot fix jobs already post @codex after push; skip their synchronize. - if ( - sender === 'github-actions[bot]' || - sender.endsWith('[bot]') - ) { + // Only this workflow's publish jobs re-@codex after push. + // Other bots (dependabot, pre-commit, etc.) still need a request. + if (sender === 'github-actions[bot]') { return set('skip', { - reason: 'own pr bot push already requested codex', + reason: 'own pr github-actions push already requested codex', }); } return set('own_rerequest_codex', { pull_number: String(pr.number), - reason: 'own pr human push — re-@codex', + reason: 'own pr non-automation push — re-@codex', }); } return set('skip', { @@ -274,6 +289,12 @@ jobs: if: needs.route.outputs.kind == 'issue_classify' runs-on: ubuntu-latest timeout-minutes: 30 + # Per-issue only: a global group drops pending issues (GitHub keeps one + # pending job). Same-issue retriages still serialize; daily limit is + # best-effort via triage:admitted markers. + concurrency: + group: cursor-triage-${{ needs.route.outputs.issue_number || github.run_id }} + cancel-in-progress: false permissions: contents: read issues: write @@ -292,6 +313,15 @@ jobs: with: persist-credentials: false + - name: Freeze trusted helper before agent + run: | + set -euo pipefail + test -f scripts/cursor-automation.cjs + cp scripts/cursor-automation.cjs "$RUNNER_TEMP/cursor-automation.cjs" + # Prompt copy for classify (agent must not rewrite the policy helper). + mkdir -p "$RUNNER_TEMP/cursor-prompts" + cp .github/cursor/prompts/classify.md "$RUNNER_TEMP/cursor-prompts/classify.md" + - name: Prepare issue context id: prepare uses: actions/github-script@v7 @@ -301,7 +331,7 @@ jobs: DAILY_LIMIT: ${{ vars.CURSOR_TRIAGE_DAILY_LIMIT || '10' }} with: script: | - const auto = require(`${process.env.GITHUB_WORKSPACE}/scripts/cursor-automation.cjs`); + const auto = require(`${process.env.RUNNER_TEMP}/cursor-automation.cjs`); await auto.prepareIssueContext({ github, context, @@ -326,7 +356,10 @@ jobs: if: steps.prepare.outputs.should_run == 'true' run: | curl https://cursor.com/install -fsS | bash + # Official installer places `agent` under ~/.local/bin (and sometimes ~/.cursor/bin). + echo "$HOME/.local/bin" >> "$GITHUB_PATH" echo "$HOME/.cursor/bin" >> "$GITHUB_PATH" + command -v agent || ls -la "$HOME/.local/bin" "$HOME/.cursor/bin" 2>/dev/null || true - name: Classify with Cursor CLI if: steps.prepare.outputs.should_run == 'true' @@ -338,24 +371,39 @@ jobs: set -euo pipefail mkdir -p .cursor-runtime git config --local --unset-all http.https://github.com/.extraheader || true - PROMPT="$(cat .github/cursor/prompts/classify.md) + # Restore frozen helper in case agent rewrote workspace copy mid-run (post-step). + PROMPT="$(cat "$RUNNER_TEMP/cursor-prompts/classify.md") ---- Issue payload path: .cursor-runtime/issue.json Write the JSON result to .cursor-runtime/classification.json as well as printing it. " - agent -p --mode=ask --trust --model auto --output-format text \ + # Sandbox limits shell/network exfil of CURSOR_API_KEY from untrusted prompts. + agent -p --mode=ask --trust --sandbox enabled --model auto --output-format text \ --workspace "$GITHUB_WORKSPACE" \ "$PROMPT" | tee .cursor-runtime/classify-raw.txt + # Always parse with the frozen helper, never workspace scripts. if [[ ! -s .cursor-runtime/classification.json ]]; then - node - <<'NODE' - const fs = require('node:fs'); - const auto = require('./scripts/cursor-automation.cjs'); - const text = fs.readFileSync('.cursor-runtime/classify-raw.txt', 'utf8'); - const parsed = auto.parseClassificationText(text); - auto.writeJson('.cursor-runtime/classification.json', parsed); - NODE + node -e ' + const fs = require("node:fs"); + const auto = require(process.env.RUNNER_TEMP + "/cursor-automation.cjs"); + const text = fs.readFileSync(".cursor-runtime/classify-raw.txt", "utf8"); + const parsed = auto.parseClassificationText(text); + auto.writeJson(".cursor-runtime/classification.json", parsed); + ' fi + # Fail closed if the untrusted prompt caused the agent to echo the key. + node -e ' + const auto = require(process.env.RUNNER_TEMP + "/cursor-automation.cjs"); + auto.assertFilesDoNotContainSecret( + [ + ".cursor-runtime/classify-raw.txt", + ".cursor-runtime/classification.json", + ], + process.env.CURSOR_API_KEY, + "classify-output", + ); + ' - name: Apply classification id: apply @@ -365,7 +413,7 @@ jobs: ISSUE_NUMBER: ${{ steps.prepare.outputs.issue_number }} with: script: | - const auto = require(`${process.env.GITHUB_WORKSPACE}/scripts/cursor-automation.cjs`); + const auto = require(`${process.env.RUNNER_TEMP}/cursor-automation.cjs`); await auto.applyClassification({ github, context, @@ -386,7 +434,7 @@ jobs: WORKFLOW_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} run: | node - <<'NODE' - const auto = require('./scripts/cursor-automation.cjs'); + const auto = require(process.env.RUNNER_TEMP + '/cursor-automation.cjs'); const payload = auto.buildSlackPayload({ status: process.env.STATUS, issueUrl: process.env.ISSUE_URL, @@ -536,12 +584,16 @@ jobs: if: steps.existing.outputs.exists != 'true' run: | curl https://cursor.com/install -fsS | bash + # Official installer places `agent` under ~/.local/bin (and sometimes ~/.cursor/bin). + echo "$HOME/.local/bin" >> "$GITHUB_PATH" echo "$HOME/.cursor/bin" >> "$GITHUB_PATH" + command -v agent || ls -la "$HOME/.local/bin" "$HOME/.cursor/bin" 2>/dev/null || true - name: Implement with Cursor CLI if: steps.existing.outputs.exists != 'true' env: CURSOR_API_KEY: ${{ secrets.CURSOR_API_KEY }} + BASE_SHA: ${{ steps.branch.outputs.base_sha }} # Explicitly strip GitHub credentials from the agent environment. GITHUB_TOKEN: '' GH_TOKEN: '' @@ -551,9 +603,33 @@ jobs: # Drop any accidental git auth helpers for the agent process. git config --local --unset-all http.https://github.com/.extraheader || true PROMPT="$(cat .github/cursor/prompts/implement.md)" - agent -p --force --trust --model auto --output-format text \ + # Sandbox restricts shell/network while still allowing workspace edits. + agent -p --force --trust --sandbox enabled --model auto --output-format text \ --workspace "$GITHUB_WORKSPACE" \ "$PROMPT" | tee .cursor-runtime/implement-raw.txt + # Do not run `git diff` here with CURSOR_API_KEY present — agent may have + # configured textconv/hooks. Committed range is scanned later via format-patch. + node -e ' + const { execSync } = require("node:child_process"); + const auto = require(process.env.RUNNER_TEMP + "/cursor-automation.cjs"); + auto.assertFilesDoNotContainSecret( + [".cursor-runtime/implement-raw.txt"], + process.env.CURSOR_API_KEY, + "implement-output", + ); + const untracked = execSync( + "git -c core.hooksPath=/dev/null ls-files --others --exclude-standard", + { encoding: "utf8" }, + ) + .split("\n") + .filter(Boolean) + .filter((p) => !p.startsWith(".cursor-runtime/")); + auto.assertFilesDoNotContainSecret( + untracked, + process.env.CURSOR_API_KEY, + "implement-untracked", + ); + ' - name: Detect changes if: steps.existing.outputs.exists != 'true' @@ -598,6 +674,13 @@ jobs: message: 'The automatic implementation pass did not produce a safe focused change. A maintainer needs to take a look.', }); + - name: Install test shell dependencies + if: steps.existing.outputs.exists != 'true' && steps.changes.outputs.present == 'true' + run: | + # Match .github/workflows/test.yml — osc7Setup.test.ts requires fish in CI. + sudo apt-get update + sudo apt-get install -y fish + - name: Verify if: steps.existing.outputs.exists != 'true' && steps.changes.outputs.present == 'true' id: verify @@ -619,16 +702,23 @@ jobs: - name: Prepare patch for isolated publish if: steps.existing.outputs.exists != 'true' && steps.changes.outputs.present == 'true' && steps.verify.outputs.passed == 'true' id: patch + # No CURSOR_API_KEY here: agent may have installed hooks. Git runs with hooks disabled. + env: + ISSUE_NUMBER: ${{ needs.route.outputs.issue_number }} run: | set -euo pipefail - status="$(git status --porcelain --untracked-files=all -- . ':(exclude).cursor-runtime' ':(exclude).cursor-runtime/**')" + # Neutralize agent-controlled hooks/config for all subsequent git ops. + GITH='git -c core.hooksPath=/dev/null' + $GITH config --local --unset-all core.hooksPath 2>/dev/null || true + $GITH config --local --unset-all core.fsmonitor 2>/dev/null || true + status="$($GITH status --porcelain --untracked-files=all -- . ':(exclude).cursor-runtime' ':(exclude).cursor-runtime/**')" if [[ -n "$status" ]]; then - git add -A - git reset -- .cursor-runtime >/dev/null 2>&1 || true - git -c user.name="netcatty-cursor-bot" -c user.email="41898282+github-actions[bot]@users.noreply.github.com" \ + $GITH add -A + $GITH reset -- .cursor-runtime >/dev/null 2>&1 || true + $GITH -c user.name="netcatty-cursor-bot" -c user.email="41898282+github-actions[bot]@users.noreply.github.com" \ commit -m "fix(#${ISSUE_NUMBER}): automated Cursor CLI fix" || true fi - name_status="$(git diff --name-status -M "${{ steps.branch.outputs.base_sha }}" HEAD)" + name_status="$($GITH diff --name-status -M "${{ steps.branch.outputs.base_sha }}" HEAD)" node -e ' const auto = require(process.env.RUNNER_TEMP + "/cursor-automation.cjs"); const hits = auto.hasProtectedChangesInSources({ nameStatusText: process.argv[1] }); @@ -637,8 +727,15 @@ jobs: process.exit(1); } ' "$name_status" + # Reject commits that still contain runtime artifacts (agent may have committed them). + runtime_hits="$($GITH diff --name-only "${{ steps.branch.outputs.base_sha }}" HEAD | grep -E '^\.cursor-runtime(/|$)' || true)" + if [[ -n "$runtime_hits" ]]; then + echo "Refusing to publish commits that include .cursor-runtime paths:" >&2 + echo "$runtime_hits" >&2 + exit 1 + fi mkdir -p .cursor-runtime - git format-patch --stdout "${{ steps.branch.outputs.base_sha }}" > .cursor-runtime/implement.patch + $GITH format-patch --stdout "${{ steps.branch.outputs.base_sha }}" > .cursor-runtime/implement.patch test -s .cursor-runtime/implement.patch if [[ -f .cursor-runtime/implement-status.txt ]]; then cp .cursor-runtime/implement-status.txt .cursor-runtime/implement-status.out.txt @@ -647,6 +744,26 @@ jobs: fi echo "should_publish=true" >> "$GITHUB_OUTPUT" + - name: Scan implement patch for secret leaks + if: steps.patch.outputs.should_publish == 'true' + env: + CURSOR_API_KEY: ${{ secrets.CURSOR_API_KEY }} + run: | + set -euo pipefail + # Pure Node scan only — no git with the secret in the environment. + node -e ' + const auto = require(process.env.RUNNER_TEMP + "/cursor-automation.cjs"); + auto.assertFilesDoNotContainSecret( + [ + ".cursor-runtime/implement.patch", + ".cursor-runtime/implement-status.out.txt", + ".cursor-runtime/implement-status.txt", + ], + process.env.CURSOR_API_KEY, + "implement-publish", + ); + ' + - name: Upload implement patch if: steps.patch.outputs.should_publish == 'true' uses: actions/upload-artifact@v4 @@ -690,7 +807,8 @@ jobs: ISSUE_TITLE: ${{ needs.implement.outputs.issue_title }} BRANCH: ${{ needs.implement.outputs.branch }} BASE_SHA: ${{ needs.implement.outputs.base_sha }} - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + # Prefer a PAT so push/PR events trigger normal CI (GITHUB_TOKEN does not). + GH_TOKEN: ${{ secrets.TRIAGE_GITHUB_TOKEN || secrets.GITHUB_TOKEN }} DEFAULT_BRANCH: ${{ github.event.repository.default_branch }} steps: - name: Download implement patch @@ -706,6 +824,8 @@ jobs: rm -rf "$PUBLISH" git clone --no-checkout "https://x-access-token:${GH_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" "$PUBLISH" cd "$PUBLISH" + git -c core.hooksPath=/dev/null config user.name "netcatty-cursor-bot" + git -c core.hooksPath=/dev/null config user.email "41898282+github-actions[bot]@users.noreply.github.com" git -c core.hooksPath=/dev/null fetch --depth=50 origin "${BASE_SHA}" git -c core.hooksPath=/dev/null checkout -B "$BRANCH" "$BASE_SHA" git -c core.hooksPath=/dev/null am --3way "$GITHUB_WORKSPACE/patch-in/implement.patch" @@ -780,7 +900,7 @@ jobs: await github.rest.issues.createComment({ ...context.repo, issue_number: pr.number, - body: auto.buildCodexReviewRequestComment(1), + body: auto.buildCodexReviewRequestComment(1, pr.head.sha), }); core.info(`Opened draft PR ${pr.html_url}`); @@ -900,54 +1020,112 @@ jobs: .map((r) => ({ body: r.body, created_at: r.submitted_at || r.created_at, + // Authoritative commit for this submitted review (may lack body pin). + commit_id: r.commit_id || '', })); const summaryCandidates = [ ...codexIssue.map((c) => ({ body: c.body, created_at: c.created_at, + commit_id: '', })), ...codexSubmitted, - ].sort( - (a, b) => - new Date(b.created_at || 0) - new Date(a.created_at || 0), + ] + .map((c) => ({ + ...c, + pin: + auto.extractReviewedCommitSha(c.body) || + String(c.commit_id || '').toLowerCase(), + })) + .sort( + (a, b) => + new Date(b.created_at || 0) - new Date(a.created_at || 0), + ); + // Only summaries pinned to the current head may drive clean/dirty. + // An older clean comment must not win the sort over a new-head review. + const headSummaries = summaryCandidates.filter( + (c) => + c.pin && auto.commitShasMatch(pr.head.sha, c.pin), ); - const summaryText = summaryCandidates[0]?.body || ''; + const summaryText = headSummaries[0]?.body || ''; + const summaryCommitId = headSummaries[0]?.pin || ''; + // Only trust control markers from automation bots / own actors. + const controlOpts = { ownActors: process.env.OWN_ACTORS }; + const requestComments = issueComments.filter((c) => + auto.isAutomationControlComment(c, controlOpts), + ); + // Codex often 👍s the @codex request instead of posting clean prose. + const reactionsByCommentId = {}; + for (const comment of requestComments.slice(-5)) { + try { + const { data: reactions } = await github.rest.reactions.listForIssueComment({ + ...context.repo, + comment_id: comment.id, + per_page: 100, + }); + reactionsByCommentId[comment.id] = reactions; + } catch (err) { + core.warning(`Failed to list reactions for comment ${comment.id}: ${err.message}`); + } + } + const reactionResult = auto.hasCodexCleanReactionOnRequest({ + requestComments, + reactionsByCommentId, + headSha: pr.head.sha, + ownActors: process.env.OWN_ACTORS, + }); const outcome = auto.parseCodexReviewOutcome({ summaryText, reviewComments: codexReviews, issueComments: codexIssue, headSha: pr.head.sha, + cleanReaction: reactionResult.clean, + reactionRequestHeadSha: reactionResult.requestHeadSha || '', + summaryCommitId, }); - const round = auto.getCodexRoundFromComments(issueComments); + const round = auto.getCodexRoundFromComments( + issueComments, + controlOpts, + ); const hasCodexActivity = Boolean(summaryText) || auto.filterCodexReviewCommentsForHead(codexReviews, pr.head.sha) - .length > 0; + .length > 0 || + reactionResult.clean; const lastAutomationRequestAt = auto.getLatestCommentTime( issueComments, - (c) => - //.test(String(c.body || '')) || - //.test(body) || + /`, - ].join('\n'); + ]; + const sha = String(headSha || '') + .trim() + .toLowerCase(); + if (/^[0-9a-f]{7,40}$/.test(sha)) { + lines.push(``); + } + return lines.join('\n'); +} + +function extractRequestedHeadSha(body) { + const match = String(body || '').match( + //i, + ); + return match ? match[1].toLowerCase() : ''; +} + +/** GitHub reaction content that Codex uses for a clean / no-findings signal. */ +const CODEX_CLEAN_REACTION_CONTENTS = new Set(['+1', 'hooray', 'heart', 'rocket']); + +function isCodexCleanReaction(reaction) { + const content = String(reaction?.content || reaction || '').toLowerCase(); + if (!CODEX_CLEAN_REACTION_CONTENTS.has(content)) return false; + const login = reaction?.user?.login || reaction?.login || ''; + // When only the content string is provided, still treat +1-like as clean candidate. + if (!login && typeof reaction === 'string') return true; + if (!login) return true; + return isCodexBotLogin(login); +} + +/** + * True when the latest trusted automation @codex request has a Codex clean reaction. + * Used when the connector only 👍s without posting clean summary text. + */ +function hasCodexCleanReactionOnRequest({ + requestComments = [], + reactionsByCommentId = {}, + headSha = '', + ownActors, +} = {}) { + const trusted = (requestComments || []).filter((c) => + isTrustedAutomationControlAuthor(c?.user?.login || c?.login, { ownActors }), + ); + if (!trusted.length) return { clean: false, requestHeadSha: '', commentId: null }; + + const sorted = [...trusted].sort( + (a, b) => new Date(b.created_at || 0) - new Date(a.created_at || 0), + ); + + // Prefer the newest request that both (a) has a clean reaction and + // (b) pins the current head. A later unanswered retry must not hide a + // valid clean reaction on an earlier same-head request. + for (const comment of sorted) { + const requestHead = extractRequestedHeadSha(comment.body); + if (headSha) { + if (!requestHead || !commitShasMatch(headSha, requestHead)) continue; + } + const reactions = + reactionsByCommentId[comment.id] || + reactionsByCommentId[String(comment.id)] || + comment.reactions_list || + []; + const clean = (Array.isArray(reactions) ? reactions : []).some((r) => + isCodexCleanReaction(r), + ); + if (!clean) continue; + return { + clean: true, + requestHeadSha: requestHead, + commentId: comment.id, + reason: 'codex_clean_reaction', + }; + } + + const latest = sorted[0]; + return { + clean: false, + requestHeadSha: extractRequestedHeadSha(latest.body), + commentId: latest.id, + }; } /** @@ -300,9 +497,15 @@ function buildExternalCodexRerequestComment(headSha) { function shouldSkipExternalCodexRerequest({ existingComments = [], headSha, + ownActors, } = {}) { const marker = ``; - return existingComments.some((c) => String(c.body || '').includes(marker)); + return existingComments.some( + (c) => + isTrustedAutomationControlAuthor(c?.user?.login || c?.login, { + ownActors, + }) && String(c.body || '').includes(marker), + ); } function buildSlackPayload({ @@ -393,21 +596,35 @@ const CODEX_DIRTY_PATTERNS = [ /\bP2\b/, ]; +// P3 is terminal-but-not-auto-fixed: do not leave the loop waiting forever. +const CODEX_P3_PATTERNS = [ + /!\[P3 Badge\]/i, + /\bP3\b/, +]; + function isCodexTerminalReviewText(body) { const text = String(body || ''); if (!text.trim()) return false; return ( /Codex Review:/i.test(text) || CODEX_CLEAN_PATTERNS.some((re) => re.test(text)) || - CODEX_DIRTY_PATTERNS.some((re) => re.test(text)) + CODEX_DIRTY_PATTERNS.some((re) => re.test(text)) || + CODEX_P3_PATTERNS.some((re) => re.test(text)) ); } +function commentLooksP3Only(body) { + const text = String(body || ''); + if (!CODEX_P3_PATTERNS.some((re) => re.test(text))) return false; + // If any P0–P2 is also present, this is not P3-only. + return !CODEX_DIRTY_PATTERNS.some((re) => re.test(text)); +} + /** Parse "Reviewed commit: `abc1234`" style markers from Codex summaries. */ function extractReviewedCommitSha(summaryText) { const text = String(summaryText || ''); const match = - text.match(/Reviewed commit:\**\s*`([0-9a-f]{7,40})`/i) || + text.match(/Reviewed commit:[\s*]*`([0-9a-f]{7,40})`/i) || text.match(/Reviewed commit:[^\n0-9a-f]*([0-9a-f]{7,40})/i); return match ? match[1].toLowerCase() : ''; } @@ -464,6 +681,15 @@ function parseCodexReviewOutcome({ reviewComments = [], issueComments = [], headSha = '', + /** When true, latest trusted @codex request has a Codex 👍 clean reaction. */ + cleanReaction = false, + /** Head SHA pinned on that request comment, if any. */ + reactionRequestHeadSha = '', + /** + * Authoritative commit from the GitHub review object (`commit_id`), used when + * the review body has no textual "Reviewed commit" marker. + */ + summaryCommitId = '', } = {}) { const scopedReviews = filterCodexReviewCommentsForHead( reviewComments, @@ -475,27 +701,65 @@ function parseCodexReviewOutcome({ const inlineDirty = scopedReviews.some((c) => commentLooksDirty(c.body || c), ); + const resolveReviewed = (text) => + extractReviewedCommitSha(text) || + String(summaryCommitId || '').toLowerCase(); - if (summaryClean && !summaryDirty) { - return { - clean: true, - reason: 'codex_clean_summary', - actionable: false, - }; - } - if (summaryDirty || (inlineDirty && !summaryClean)) { + // Current-head inline P0–P2 findings always win over an older clean summary. + if (inlineDirty && !(summaryClean && !summaryDirty)) { return { clean: false, reason: summaryDirty ? 'codex_findings' : 'codex_inline_findings', actionable: true, }; } - if (summaryClean && inlineDirty) { - // Stale inlines after a clean summary → trust summary. + if (summaryDirty) { + const reviewed = resolveReviewed(summary); + // Dirty summary for an older commit must not drive fixes on a new head. + if (reviewed && headSha && !commitShasMatch(headSha, reviewed)) { + if (inlineDirty) { + return { + clean: false, + reason: 'codex_inline_findings', + actionable: true, + }; + } + return { + clean: false, + reason: 'stale_dirty_summary', + actionable: false, + }; + } + return { + clean: false, + reason: 'codex_findings', + actionable: true, + reviewedCommitSha: reviewed || '', + }; + } + if (summaryClean) { + const reviewed = resolveReviewed(summary); + if (inlineDirty) { + // Clean summary only overrides current-head inlines when pinned to this head. + if (reviewed && headSha && commitShasMatch(headSha, reviewed)) { + return { + clean: true, + reason: 'codex_clean_summary', + actionable: false, + reviewedCommitSha: reviewed, + }; + } + return { + clean: false, + reason: 'codex_inline_findings', + actionable: true, + }; + } return { clean: true, - reason: 'codex_clean_summary_ignore_stale_inline', + reason: 'codex_clean_summary', actionable: false, + reviewedCommitSha: reviewed || '', }; } if (scopedReviews.length > 0 && inlineDirty) { @@ -506,19 +770,59 @@ function parseCodexReviewOutcome({ }; } + // Connector often 👍 the request comment instead of posting clean prose. + if (cleanReaction && !inlineDirty && !summaryDirty) { + if ( + reactionRequestHeadSha && + headSha && + !commitShasMatch(headSha, reactionRequestHeadSha) + ) { + return { + clean: false, + reason: 'stale_clean_reaction', + actionable: false, + }; + } + return { + clean: true, + reason: 'codex_clean_reaction', + actionable: false, + reviewedCommitSha: reactionRequestHeadSha || '', + }; + } + + // P3-only reviews are terminal: not clean, not auto-fixed — hand to human. + const summaryP3 = commentLooksP3Only(summary); + const inlineP3 = scopedReviews.some((c) => commentLooksP3Only(c.body || c)); + if ((summaryP3 || inlineP3) && !summaryDirty && !inlineDirty) { + return { + clean: false, + reason: 'codex_p3_only', + actionable: false, + }; + } + // No clear signal — do not start a fix loop. return { clean: false, reason: 'codex_unknown', actionable: false }; } -function hasAutomationCodexRequest(comments = []) { - return comments.some((comment) => - //.test(String(comment.body || '')), +function hasAutomationCodexRequest(comments = [], options = {}) { + return comments.some( + (comment) => + isTrustedAutomationControlAuthor( + comment?.user?.login || comment?.login, + options, + ) && + //.test(String(comment.body || '')), ); } /** * Decide codex_loop action from pure inputs (testable). */ +/** Default age after which a still-unanswered @codex request may be retried. */ +const CODEX_REQUEST_RETRY_MS = 30 * 60 * 1000; + function decideCodexLoopAction({ eligible, outcome, @@ -530,6 +834,14 @@ function decideCodexLoopAction({ summaryText = '', lastAutomationRequestAt = 0, lastCodexSummaryAt = 0, + /** Head SHA pinned on the latest automation @codex request comment. */ + requestedHeadSha = '', + /** Manual workflow_dispatch should always be able to re-request. */ + forceRetry = false, + /** Current time (ms); injectable for tests. */ + nowMs = Date.now(), + /** How long to wait for Codex before re-requesting. */ + requestRetryMs = CODEX_REQUEST_RETRY_MS, } = {}) { if (!eligible) { return { action: 'skip', reason: 'not_fix_eligible' }; @@ -538,37 +850,65 @@ function decideCodexLoopAction({ // Exception: actionable inline-only findings already present for this head. const hasActionableFindings = Boolean(outcome) && outcome.clean === false && outcome.actionable === true; - if ( + const requestIsPending = lastAutomationRequestAt > 0 && - lastAutomationRequestAt > (lastCodexSummaryAt || 0) && - !hasActionableFindings - ) { + lastAutomationRequestAt > (lastCodexSummaryAt || 0); + const requestExpired = + lastAutomationRequestAt > 0 && + Number(nowMs) - Number(lastAutomationRequestAt) >= Number(requestRetryMs); + + if (requestIsPending && !hasActionableFindings && !forceRetry && !requestExpired) { return { action: 'skip', reason: 'awaiting_codex_for_new_head' }; } if (!hasCodexActivity) { - if (hasAutomationRequest) { + if (hasAutomationRequest && !forceRetry && !requestExpired) { return { action: 'skip', reason: 'awaiting_codex' }; } - return { action: 'request_review', reason: 'no_codex_yet' }; + return { + action: 'request_review', + reason: hasAutomationRequest ? 'retry_request' : 'no_codex_yet', + }; } if (outcome?.clean) { - const reviewed = extractReviewedCommitSha(summaryText); - if (reviewed && headSha && !commitShasMatch(headSha, reviewed)) { - return { action: 'skip', reason: 'stale_clean_summary' }; - } - // Fresh request still pending and no commit pin → don't mark ready early. - if ( - lastAutomationRequestAt > 0 && - lastAutomationRequestAt > (lastCodexSummaryAt || 0) - ) { - return { action: 'skip', reason: 'awaiting_codex_for_new_head' }; + const reviewed = + extractReviewedCommitSha(summaryText) || + String(outcome.reviewedCommitSha || requestedHeadSha || ''); + const pinnedToHead = + Boolean(reviewed) && + Boolean(headSha) && + commitShasMatch(headSha, reviewed); + // Only mark ready when the clean result is explicitly pinned to this head + // (summary Reviewed commit, or reaction on a request with cursor-codex-head). + // Unpinned reactions must not approve a later push. + if (!pinnedToHead) { + if (forceRetry || requestExpired) { + return { action: 'request_review', reason: 'retry_request' }; + } + if (requestIsPending) { + return { action: 'skip', reason: 'awaiting_codex_for_new_head' }; + } + return { + action: 'skip', + reason: reviewed ? 'stale_clean_summary' : 'clean_summary_unpinned', + }; } return { action: 'mark_ready', reason: outcome.reason || 'codex_clean' }; } if (outcome && outcome.actionable === false) { + // P3-only: stop the auto-fix loop and hand off to a human. + if (outcome.reason === 'codex_p3_only') { + return { action: 'give_up', reason: 'codex_p3_only' }; + } + // Stale/unknown outcomes still allow a manual or timed re-request. + if (forceRetry || requestExpired) { + return { action: 'request_review', reason: 'retry_request' }; + } return { action: 'skip', reason: outcome.reason || 'codex_unknown' }; } - if (Number(round) >= Number(maxRounds)) { + // `round` is the last request marker (1 on the first dirty review, before any + // fix). Allow `maxRounds` completed fix attempts: give up only after that many + // requests have already been answered dirty (i.e. round > maxRounds). + if (Number(round) > Number(maxRounds)) { return { action: 'give_up', reason: 'max_rounds' }; } return { action: 'fix', reason: outcome?.reason || 'codex_findings' }; @@ -587,19 +927,49 @@ function shouldReTriageIssueComment({ labels = [], commenterLogin, issueAuthorLo return commenter === author; } +/** + * Decode a path as printed by Git when it contains special characters + * (leading quote + C-style escapes). Unquoted paths are returned as-is. + */ +function unquoteGitPath(raw) { + let p = String(raw || ''); + if (p.length >= 2 && p.startsWith('"') && p.endsWith('"')) { + p = p + .slice(1, -1) + .replace(/\\([abtnvfr"\\])/g, (_, ch) => { + const map = { + a: '\x07', + b: '\b', + t: '\t', + n: '\n', + v: '\v', + f: '\f', + r: '\r', + '"': '"', + '\\': '\\', + }; + return map[ch] ?? ch; + }) + .replace(/\\([0-7]{1,3})/g, (_, oct) => + String.fromCharCode(parseInt(oct, 8)), + ); + } + return p; +} + function pathsFromGitStatusPorcelain(gitStatusPorcelain) { const paths = []; for (const line of String(gitStatusPorcelain || '').split('\n')) { const trimmed = line.trim(); if (!trimmed) continue; - // porcelain: XY path OR XY old -> new + // porcelain: XY path OR XY old -> new (paths may be C-quoted) const rest = trimmed.replace(/^[ MADRCU?!]{1,2}\s+/, ''); if (rest.includes(' -> ')) { - const [from, to] = rest.split(' -> ').map((p) => p.trim()); + const [from, to] = rest.split(' -> ').map((p) => unquoteGitPath(p.trim())); if (from) paths.push(from); if (to) paths.push(to); } else if (rest) { - paths.push(rest); + paths.push(unquoteGitPath(rest)); } } return paths; @@ -616,11 +986,11 @@ function pathsFromGitDiffNameStatus(nameStatusText) { if (!trimmed) continue; const parts = trimmed.split(/\t/); if (parts.length >= 3 && /^R\d*/.test(parts[0])) { - paths.push(parts[1], parts[2]); + paths.push(unquoteGitPath(parts[1]), unquoteGitPath(parts[2])); } else if (parts.length >= 2) { - paths.push(parts[1]); + paths.push(unquoteGitPath(parts[1])); } else { - paths.push(trimmed.replace(/^[A-Z]\d*\s+/, '')); + paths.push(unquoteGitPath(trimmed.replace(/^[A-Z]\d*\s+/, ''))); } } return paths.filter(Boolean); @@ -658,9 +1028,19 @@ function formatCodexFindingsMarkdown({ ); } } - const botIssue = issueComments.filter((c) => - isCodexBotLogin(c.user?.login || c.login), - ); + const botIssue = issueComments + .filter((c) => isCodexBotLogin(c.user?.login || c.login)) + .filter((c) => { + // When head is known, only keep comments explicitly pinned to it. + // Unpinned multi-round summaries are often stale and regress fixes. + if (!headSha) return true; + const reviewed = extractReviewedCommitSha(c.body); + if (!reviewed) return false; + return commitShasMatch(headSha, reviewed); + }) + .sort( + (a, b) => new Date(b.created_at || 0) - new Date(a.created_at || 0), + ); if (botIssue.length) { lines.push('## Issue comments from Codex', ''); for (const comment of botIssue.slice(0, 10)) { @@ -674,13 +1054,15 @@ function listProtectedPathHits(filePaths) { const hits = []; for (const filePath of filePaths || []) { const normalized = String(filePath || '').replace(/\\/g, '/'); - if ( - PROTECTED_PATH_PREFIXES.some( - (prefix) => - normalized === prefix.replace(/\/$/, '') || - normalized.startsWith(prefix), - ) - ) { + const base = normalized.split('/').pop() || normalized; + const prefixHit = PROTECTED_PATH_PREFIXES.some( + (prefix) => + normalized === prefix.replace(/\/$/, '') || + normalized.startsWith(prefix), + ); + const baseHit = PROTECTED_PATH_BASENAMES.includes(base); + const builderHit = /electron-builder/i.test(normalized); + if (prefixHit || baseHit || builderHit) { hits.push(normalized); } } @@ -707,9 +1089,17 @@ function hasProtectedChangesInSources({ ]); } -function getCodexRoundFromComments(comments = []) { +function getCodexRoundFromComments(comments = [], options = {}) { let maxRound = 0; for (const comment of comments) { + if ( + !isTrustedAutomationControlAuthor( + comment?.user?.login || comment?.login, + options, + ) + ) { + continue; + } const body = String(comment.body || ''); const match = body.match(//); if (match) { @@ -780,11 +1170,13 @@ async function prepareIssueContext({ // Daily limit only gates first admission. Follow-ups on already-admitted or // needs-info issues must still re-classify when the author replies. + // Do NOT treat generic `triage` / `bug` / `enhancement` as admission — those + // are applied by templates and would disable the daily limit entirely. const alreadyAdmitted = labelNames.includes('triage:admitted') || labelNames.includes('needs-info') || labelNames.includes('triage:bug-needs-info') || - labelNames.includes('triage') || + labelNames.includes('triage:unclear') || labelNames.includes('ready-for-agent') || labelNames.includes('ready-for-human'); @@ -982,10 +1374,9 @@ function isBotPrForIssue(pull, issueNumber) { !pull.base?.repo?.full_name || pull.head.repo.full_name === pull.base.repo.full_name; if (!sameRepo) return false; - const mentionsIssue = - body.includes(`Fixes #${n}`) || - body.includes(`fixes #${n}`) || - headRef.startsWith(prefix); + // Boundary after the issue number so Fixes #1 does not match Fixes #10. + const fixesRe = new RegExp(`(?:^|\\W)(?:Fixes|fixes) #${n}(?!\\d)`); + const mentionsIssue = fixesRe.test(body) || headRef.startsWith(prefix); if (!mentionsIssue) return false; return ( isBotPrMarker(body) || @@ -999,9 +1390,9 @@ function isBotPrForIssue(pull, issueNumber) { async function findOpenBotPrForIssue({ github, context, issueNumber }) { const n = String(issueNumber); - // Targeted search is far more reliable than scanning the latest 100 PRs. + // Only open PRs count — a closed/unmerged automation PR must not block retries. try { - const q = `repo:${context.repo.owner}/${context.repo.repo} is:pr ("Fixes #${n}" OR "fixes #${n}" OR head:cursor/issue-${n}-)`; + const q = `repo:${context.repo.owner}/${context.repo.repo} is:pr is:open ("Fixes #${n}" OR "fixes #${n}" OR head:cursor/issue-${n}-)`; const items = await github.paginate( github.rest.search.issuesAndPullRequests, { q, per_page: 20 }, @@ -1009,29 +1400,27 @@ async function findOpenBotPrForIssue({ github, context, issueNumber }) { ); for (const item of items) { if (!item.pull_request || !item.number) continue; + // Search may still return closed briefly; re-check state. + if (item.state && item.state !== 'open') continue; const { data: pull } = await github.rest.pulls.get({ ...context.repo, pull_number: item.number, }); + if (pull.state && pull.state !== 'open') continue; if (isBotPrForIssue(pull, issueNumber)) return pull; } } catch { // fall through to list scan } - const states = ['open', 'closed']; - for (const state of states) { - const pulls = await github.paginate(github.rest.pulls.list, { - ...context.repo, - state, - per_page: 100, - sort: 'updated', - direction: 'desc', - }); - const match = pulls.find((pull) => isBotPrForIssue(pull, issueNumber)); - if (match) return match; - } - return null; + const pulls = await github.paginate(github.rest.pulls.list, { + ...context.repo, + state: 'open', + per_page: 100, + sort: 'updated', + direction: 'desc', + }); + return pulls.find((pull) => isBotPrForIssue(pull, issueNumber)) || null; } module.exports = { @@ -1042,11 +1431,16 @@ module.exports = { CATEGORY_LABELS, MANAGED_LABELS, PROTECTED_PATH_PREFIXES, + PROTECTED_PATH_BASENAMES, IMPLEMENT_CATEGORIES, sanitizeUntrustedText, isValidIssueFormat, parseOwnActors, isCodexBotLogin, + isTrustedAutomationControlAuthor, + isAutomationControlComment, + assertTextDoesNotContainSecret, + assertFilesDoNotContainSecret, isBotPrMarker, isAutomationBranch, isFixEligiblePr, @@ -1056,6 +1450,10 @@ module.exports = { buildPullRequestBody, buildPullRequestComment, buildCodexReviewRequestComment, + extractRequestedHeadSha, + CODEX_CLEAN_REACTION_CONTENTS, + isCodexCleanReaction, + hasCodexCleanReactionOnRequest, buildExternalCodexRerequestComment, shouldSkipExternalCodexRerequest, buildSlackPayload, @@ -1070,10 +1468,12 @@ module.exports = { filterCodexReviewCommentsForHead, parseCodexReviewOutcome, hasAutomationCodexRequest, + CODEX_REQUEST_RETRY_MS, decideCodexLoopAction, shouldReTriageIssueComment, formatCodexFindingsMarkdown, listProtectedPathHits, + unquoteGitPath, pathsFromGitStatusPorcelain, pathsFromGitDiffNameStatus, hasProtectedChanges, diff --git a/scripts/cursor-automation.test.cjs b/scripts/cursor-automation.test.cjs index 27b73fa3b..974ab2bcf 100644 --- a/scripts/cursor-automation.test.cjs +++ b/scripts/cursor-automation.test.cjs @@ -77,7 +77,21 @@ test('labelsForCategory swaps bug/enhancement correctly', () => { assert.ok(!labels.includes('needs-triage')); }); -test('isFixEligiblePr allows bot marker on same-repo branch', () => { +test('isFixEligiblePr allows automation bot author with bot marker', () => { + const pr = { + user: { login: 'github-actions[bot]' }, + body: `${auto.BOT_PR_MARKER}\nFixes #1`, + head: { + ref: 'cursor/issue-1-99', + repo: { full_name: 'binaricat/Netcatty' }, + }, + base: { repo: { full_name: 'binaricat/Netcatty' } }, + labels: ['automation:bot-pr'], + }; + assert.equal(auto.isFixEligiblePr(pr, { repository: 'binaricat/Netcatty' }), true); +}); + +test('isFixEligiblePr rejects contributor spoofing bot marker', () => { const pr = { user: { login: 'random-contributor' }, body: `${auto.BOT_PR_MARKER}\nFixes #1`, @@ -86,9 +100,9 @@ test('isFixEligiblePr allows bot marker on same-repo branch', () => { repo: { full_name: 'binaricat/Netcatty' }, }, base: { repo: { full_name: 'binaricat/Netcatty' } }, - labels: [], + labels: ['automation:bot-pr'], }; - assert.equal(auto.isFixEligiblePr(pr, { repository: 'binaricat/Netcatty' }), true); + assert.equal(auto.isFixEligiblePr(pr, { repository: 'binaricat/Netcatty' }), false); }); test('isFixEligiblePr rejects forks', () => { @@ -158,6 +172,64 @@ test('parseCodexReviewOutcome ignores stale head inlines when summary clean', () assert.equal(outcome.clean, true); }); +test('parseCodexReviewOutcome prefers current-head inline over unpinned clean', () => { + const outcome = auto.parseCodexReviewOutcome({ + summaryText: "Codex Review: Didn't find any major issues. Swish!", + headSha: 'abc1234deadbeef', + reviewComments: [ + { + body: '![P2 Badge](x) current head bug', + commit_id: 'abc1234deadbeef', + }, + ], + }); + assert.equal(outcome.clean, false); + assert.equal(outcome.actionable, true); +}); + +test('parseCodexReviewOutcome rejects dirty summary for other head', () => { + const outcome = auto.parseCodexReviewOutcome({ + summaryText: + 'Codex Review: found issues\n**Reviewed commit:** `aaaaaaaaaaaaaaaa`\n![P2 Badge](x) old', + headSha: 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', + reviewComments: [], + }); + assert.equal(outcome.clean, false); + assert.equal(outcome.actionable, false); + assert.equal(outcome.reason, 'stale_dirty_summary'); +}); + +test('labelsForCategory preserves triage:admitted', () => { + const labels = auto.labelsForCategory('unclear', [ + 'triage:admitted', + 'needs-triage', + ]); + assert.ok(labels.includes('triage:admitted')); + assert.ok(labels.includes('triage:unclear')); +}); + +test('labelsForCategory drops standalone unclear label', () => { + const labels = auto.labelsForCategory('bug_ready', ['unclear', 'triage:unclear', 'user-tag']); + assert.ok(labels.includes('bug')); + assert.ok(labels.includes('user-tag')); + assert.ok(!labels.includes('unclear')); + assert.ok(!labels.includes('triage:unclear')); +}); + +test('decideCodexLoopAction forceRetry does not mark ready on stale clean', () => { + const d = auto.decideCodexLoopAction({ + eligible: true, + hasCodexActivity: true, + forceRetry: true, + lastAutomationRequestAt: 5000, + lastCodexSummaryAt: 1000, + summaryText: "Didn't find any major issues. Swish!", + outcome: { clean: true, actionable: false, reason: 'codex_clean_summary' }, + }); + assert.equal(d.action, 'request_review'); + assert.equal(d.reason, 'retry_request'); +}); + test('parseCodexReviewOutcome unknown is not actionable', () => { const outcome = auto.parseCodexReviewOutcome({ summaryText: 'Codex is still thinking', @@ -168,15 +240,64 @@ test('parseCodexReviewOutcome unknown is not actionable', () => { assert.equal(outcome.reason, 'codex_unknown'); }); +test('parseCodexReviewOutcome treats P3-only as non-actionable handoff', () => { + const outcome = auto.parseCodexReviewOutcome({ + summaryText: 'Codex Review: only nitpicks left\n![P3 Badge](x)\n**P3** style', + reviewComments: [], + headSha: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', + summaryCommitId: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', + }); + assert.equal(outcome.clean, false); + assert.equal(outcome.actionable, false); + assert.equal(outcome.reason, 'codex_p3_only'); + const d = auto.decideCodexLoopAction({ + eligible: true, + hasCodexActivity: true, + outcome, + }); + assert.equal(d.action, 'give_up'); + assert.equal(d.reason, 'codex_p3_only'); +}); + test('decideCodexLoopAction skips when awaiting existing @codex request', () => { + const now = 10_000_000; const d = auto.decideCodexLoopAction({ eligible: true, hasAutomationRequest: true, hasCodexActivity: false, + lastAutomationRequestAt: now - 1000, + nowMs: now, outcome: { clean: false, actionable: false, reason: 'codex_unknown' }, }); assert.equal(d.action, 'skip'); - assert.equal(d.reason, 'awaiting_codex'); + // With a request timestamp newer than any summary, this is the new-head wait path. + assert.equal(d.reason, 'awaiting_codex_for_new_head'); +}); + +test('decideCodexLoopAction retries after expired unanswered request', () => { + const now = 10_000_000; + const d = auto.decideCodexLoopAction({ + eligible: true, + hasAutomationRequest: true, + hasCodexActivity: false, + lastAutomationRequestAt: now - auto.CODEX_REQUEST_RETRY_MS - 1, + nowMs: now, + outcome: { clean: false, actionable: false, reason: 'codex_unknown' }, + }); + assert.equal(d.action, 'request_review'); + assert.equal(d.reason, 'retry_request'); +}); + +test('decideCodexLoopAction forceRetry re-requests immediately', () => { + const d = auto.decideCodexLoopAction({ + eligible: true, + hasAutomationRequest: true, + hasCodexActivity: false, + lastAutomationRequestAt: Date.now(), + forceRetry: true, + }); + assert.equal(d.action, 'request_review'); + assert.equal(d.reason, 'retry_request'); }); test('decideCodexLoopAction ignores stale clean summary for other head', () => { @@ -192,12 +313,25 @@ test('decideCodexLoopAction ignores stale clean summary for other head', () => { assert.equal(d.reason, 'stale_clean_summary'); }); +test('decideCodexLoopAction marks ready only when clean is pinned to head', () => { + const d = auto.decideCodexLoopAction({ + eligible: true, + hasCodexActivity: true, + headSha: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', + summaryText: + "Codex Review: Didn't find any major issues. Swish!\n**Reviewed commit:** `aaaaaaaa`", + outcome: { clean: true, actionable: false, reason: 'codex_clean_summary' }, + }); + assert.equal(d.action, 'mark_ready'); +}); + test('decideCodexLoopAction awaits when request is newer than summary', () => { const d = auto.decideCodexLoopAction({ eligible: true, hasCodexActivity: true, lastAutomationRequestAt: 2000, lastCodexSummaryAt: 1000, + nowMs: 2500, outcome: { clean: true, actionable: false, reason: 'codex_clean_summary' }, summaryText: "Didn't find any major issues. Swish!", }); @@ -298,16 +432,66 @@ test('normalizeClassification replaces closing-language unclear replies', () => assert.doesNotMatch(result.reply, /will be closed/i); }); -test('normalizeClassification keeps localized low-confidence bug reply', () => { - const result = auto.normalizeClassification({ +test('normalizeClassification always rewrites low-confidence bug_ready reply', () => { + const en = auto.normalizeClassification({ category: 'bug_ready', confidence: 0.5, summary: 'maybe', reasoning: 'unclear', - reply: '请补充复现步骤和日志。', + reply: 'A focused change is being prepared.', }); - assert.equal(result.category, 'bug_needs_info'); - assert.equal(result.reply, '请补充复现步骤和日志。'); + assert.equal(en.category, 'bug_needs_info'); + assert.match(en.reply, /steps to reproduce|Expected vs actual/i); + assert.doesNotMatch(en.reply, /focused change is being prepared/i); + + const zh = auto.normalizeClassification({ + category: 'bug_ready', + confidence: 0.5, + summary: 'maybe', + reasoning: 'unclear', + reply: '我们正在准备修复这个问题。', + }); + assert.equal(zh.category, 'bug_needs_info'); + assert.match(zh.reply, /复现步骤|期望行为/); + assert.doesNotMatch(zh.reply, /正在准备修复/); +}); + +test('normalizeClassification rewrites implementation promise on downgrade', () => { + const bug = auto.normalizeClassification({ + category: 'bug_ready', + confidence: 0.5, + summary: 'maybe', + reasoning: 'low conf', + reply: 'A focused change is being prepared for this report.', + }); + assert.equal(bug.category, 'bug_needs_info'); + assert.doesNotMatch(bug.reply, /focused change is being prepared/i); + assert.match(bug.reply, /steps to reproduce|logs/i); + + const feature = auto.normalizeClassification({ + category: 'feature_quick_win', + confidence: 0.4, + summary: 'maybe', + reasoning: 'low conf', + reply: 'A focused change is being prepared.', + }); + assert.equal(feature.category, 'feature_defer'); + assert.doesNotMatch(feature.reply, /focused change is being prepared/i); + assert.match(feature.reply, /maintainer/i); +}); + +test('parseCodexReviewOutcome uses summaryCommitId when body has no pin', () => { + const outcome = auto.parseCodexReviewOutcome({ + summaryText: "Didn't find any major issues. Swish!", + reviewComments: [], + headSha: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', + summaryCommitId: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', + }); + assert.equal(outcome.clean, true); + assert.equal( + outcome.reviewedCommitSha, + 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', + ); }); test('isBotPrForIssue matches marker + Fixes', () => { assert.equal( @@ -332,6 +516,15 @@ test('hasProtectedChangesInSources checks commit names', () => { assert.deepEqual(hits, ['.github/workflows/x.yml']); }); +test('hasProtectedChangesInSources blocks electron-builder configs', () => { + const hits = auto.hasProtectedChangesInSources({ + changedFiles: ['electron-builder.config.cjs', 'components/App.tsx', 'nix/release.nix'], + }); + assert.ok(hits.includes('electron-builder.config.cjs')); + assert.ok(hits.includes('nix/release.nix')); + assert.ok(!hits.includes('components/App.tsx')); +}); + test('pathsFromGitStatusPorcelain keeps both rename sides', () => { const paths = auto.pathsFromGitStatusPorcelain( 'R scripts/cursor-automation.cjs -> scripts/evil.cjs\n', @@ -340,6 +533,28 @@ test('pathsFromGitStatusPorcelain keeps both rename sides', () => { assert.ok(paths.includes('scripts/evil.cjs')); }); +test('pathsFromGitStatusPorcelain unquotes C-style paths', () => { + const paths = auto.pathsFromGitStatusPorcelain( + 'A ".github/workflows/evil\\tname.yml"\n', + ); + assert.deepEqual(paths, ['.github/workflows/evil\tname.yml']); + const hits = auto.hasProtectedChangesInSources({ + gitStatusPorcelain: 'A ".github/workflows/evil\\tname.yml"\n', + }); + assert.ok(hits.some((p) => p.startsWith('.github/'))); +}); + +test('isBotPrForIssue requires complete issue number boundary', () => { + const prFor10 = { + body: `${auto.BOT_PR_MARKER}\nFixes #10`, + head: { ref: 'cursor/issue-10-1', repo: { full_name: 'o/r' } }, + base: { repo: { full_name: 'o/r' } }, + labels: [], + }; + assert.equal(auto.isBotPrForIssue(prFor10, 10), true); + assert.equal(auto.isBotPrForIssue(prFor10, 1), false); +}); + test('pathsFromGitDiffNameStatus keeps rename source and dest', () => { const paths = auto.pathsFromGitDiffNameStatus( 'R100\t.github/workflows/x.yml\tunprotected.yml\nM\tsrc/a.ts\n', @@ -366,13 +581,16 @@ test('hasProtectedChanges flags workflow edits', () => { assert.deepEqual(hits, ['.github/workflows/cursor-automation.yml']); }); -test('shouldSkipExternalCodexRerequest matches head sha marker', () => { +test('shouldSkipExternalCodexRerequest matches trusted head sha marker only', () => { const sha = 'abc123'; assert.equal( auto.shouldSkipExternalCodexRerequest({ headSha: sha, existingComments: [ - { body: auto.buildExternalCodexRerequestComment(sha) }, + { + user: { login: 'github-actions[bot]' }, + body: auto.buildExternalCodexRerequestComment(sha), + }, ], }), true, @@ -380,10 +598,76 @@ test('shouldSkipExternalCodexRerequest matches head sha marker', () => { assert.equal( auto.shouldSkipExternalCodexRerequest({ headSha: sha, - existingComments: [{ body: 'unrelated' }], + existingComments: [ + { + user: { login: 'attacker' }, + body: auto.buildExternalCodexRerequestComment(sha), + }, + ], }), false, ); + assert.equal( + auto.shouldSkipExternalCodexRerequest({ + headSha: sha, + existingComments: [{ user: { login: 'github-actions[bot]' }, body: 'unrelated' }], + }), + false, + ); +}); + +test('parseCodexReviewOutcome accepts clean reaction without summary text', () => { + const outcome = auto.parseCodexReviewOutcome({ + summaryText: '', + reviewComments: [], + headSha: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', + cleanReaction: true, + reactionRequestHeadSha: 'aaaaaaaa', + }); + assert.equal(outcome.clean, true); + assert.equal(outcome.reason, 'codex_clean_reaction'); +}); + +test('decideCodexLoopAction marks ready on pinned clean reaction', () => { + const d = auto.decideCodexLoopAction({ + eligible: true, + hasCodexActivity: true, + headSha: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', + requestedHeadSha: 'aaaaaaaa', + outcome: { + clean: true, + actionable: false, + reason: 'codex_clean_reaction', + reviewedCommitSha: 'aaaaaaaa', + }, + }); + assert.equal(d.action, 'mark_ready'); +}); + +test('decideCodexLoopAction rejects unpinned clean reaction', () => { + const d = auto.decideCodexLoopAction({ + eligible: true, + hasCodexActivity: true, + headSha: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', + requestedHeadSha: '', + outcome: { + clean: true, + actionable: false, + reason: 'codex_clean_reaction', + reviewedCommitSha: '', + }, + }); + assert.equal(d.action, 'skip'); + assert.equal(d.reason, 'clean_summary_unpinned'); +}); + +test('buildCodexReviewRequestComment pins head sha', () => { + const body = auto.buildCodexReviewRequestComment( + 2, + 'deadbeefcafebabe000000000000000000000001', + ); + assert.match(body, /cursor-codex-round:2/); + assert.match(body, /cursor-codex-head:deadbeefcafebabe000000000000000000000001/); }); test('buildExternalCodexRerequestComment only asks Codex', () => { @@ -393,14 +677,82 @@ test('buildExternalCodexRerequestComment only asks Codex', () => { assert.doesNotMatch(body, /Cursor CLI/i); }); -test('getCodexRoundFromComments reads max round', () => { +test('getCodexRoundFromComments reads max round from trusted authors only', () => { assert.equal( auto.getCodexRoundFromComments([ - { body: '' }, - { body: '' }, + { user: { login: 'github-actions[bot]' }, body: '' }, + { user: { login: 'github-actions[bot]' }, body: '' }, + { user: { login: 'random-user' }, body: '' }, + { user: { login: 'other-app[bot]' }, body: '' }, ]), 3, ); + assert.equal( + auto.getCodexRoundFromComments( + [{ user: { login: 'binaricat' }, body: '' }], + { ownActors: 'binaricat' }, + ), + 5, + ); + assert.equal( + auto.getCodexRoundFromComments([ + { user: { login: 'attacker' }, body: '' }, + ]), + 0, + ); +}); + +test('hasAutomationCodexRequest ignores untrusted markers', () => { + assert.equal( + auto.hasAutomationCodexRequest([ + { user: { login: 'attacker' }, body: '' }, + ]), + false, + ); + assert.equal( + auto.hasAutomationCodexRequest([ + { + user: { login: 'github-actions[bot]' }, + body: '', + }, + ]), + true, + ); +}); + +test('decideCodexLoopAction forceRetry re-requests on stale dirty', () => { + const d = auto.decideCodexLoopAction({ + eligible: true, + hasCodexActivity: true, + forceRetry: true, + outcome: { + clean: false, + actionable: false, + reason: 'stale_dirty_summary', + }, + }); + assert.equal(d.action, 'request_review'); + assert.equal(d.reason, 'retry_request'); +}); + +test('decideCodexLoopAction allows fix on round equal to maxRounds', () => { + const d = auto.decideCodexLoopAction({ + eligible: true, + hasCodexActivity: true, + round: 1, + maxRounds: 1, + outcome: { clean: false, actionable: true, reason: 'codex_findings' }, + }); + assert.equal(d.action, 'fix'); + const giveUp = auto.decideCodexLoopAction({ + eligible: true, + hasCodexActivity: true, + round: 2, + maxRounds: 1, + outcome: { clean: false, actionable: true, reason: 'codex_findings' }, + }); + assert.equal(giveUp.action, 'give_up'); + assert.equal(giveUp.reason, 'max_rounds'); }); test('parseClassificationFile accepts pure JSON file', () => { @@ -424,4 +776,5 @@ test('buildCodexReviewRequestComment includes mention', () => { const body = auto.buildCodexReviewRequestComment(2); assert.match(body, /@codex review/); assert.match(body, /cursor-codex-round:2/); + assert.doesNotMatch(body, /cursor-codex-head:/); });