fix(ci): close Cursor automation handoffs and label state machine (#2461)

- Format recovery dispatches triage before clearing invalid-format
- Shared CJK-friendly title rules; codex label terminal state machine
- Research steps in classify; agent TITLE + PR body for bot PRs
- Single @codex review via CODEX_REQUEST_GITHUB_TOKEN (binaricat)
This commit is contained in:
陈大猫 authored and GitHub committed 2026-07-24 20:10:28 +08:00
1 parent 5b1af6ce16
commit 48b2cd1f67
7 files changed
+1032 -145

No files matched your search

+142 -57
View File
@@ -752,6 +752,11 @@ jobs:
else
echo "OK: automated fix" > .cursor-runtime/implement-status.out.txt
fi
if [[ -f .cursor-runtime/implement-pr-body.md ]]; then
cp .cursor-runtime/implement-pr-body.md .cursor-runtime/implement-pr-body.out.md
else
: > .cursor-runtime/implement-pr-body.out.md
fi
echo "should_publish=true" >> "$GITHUB_OUTPUT"
- name: Scan implement patch for secret leaks
@@ -768,6 +773,8 @@ jobs:
".cursor-runtime/implement.patch",
".cursor-runtime/implement-status.out.txt",
".cursor-runtime/implement-status.txt",
".cursor-runtime/implement-pr-body.out.md",
".cursor-runtime/implement-pr-body.md",
],
process.env.CURSOR_API_KEY,
"implement-publish",
@@ -782,6 +789,7 @@ jobs:
path: |
.cursor-runtime/implement.patch
.cursor-runtime/implement-status.out.txt
.cursor-runtime/implement-pr-body.out.md
if-no-files-found: error
- name: Mark needs human on implement failure
@@ -866,7 +874,8 @@ jobs:
git show "FETCH_HEAD:scripts/cursor-automation.cjs" > "$RUNNER_TEMP/cursor-automation.cjs"
test -s "$RUNNER_TEMP/cursor-automation.cjs"
- name: Open draft PR and request Codex review
- name: Open draft PR
id: openpr
uses: actions/github-script@v7
env:
BRANCH: ${{ needs.implement.outputs.branch }}
@@ -876,20 +885,39 @@ jobs:
with:
# GITHUB_TOKEN cannot create PRs unless the repo enables
# "Allow GitHub Actions to create and approve pull requests".
# Prefer TRIAGE_GITHUB_TOKEN (PAT) so implement can open draft PRs.
# Prefer TRIAGE_GITHUB_TOKEN (PAT) so implement can open draft PRs
# as netcatty-bot — keep this separate from @codex identity.
github-token: ${{ secrets.TRIAGE_GITHUB_TOKEN || secrets.GITHUB_TOKEN }}
script: |
const auto = require(`${process.env.RUNNER_TEMP}/cursor-automation.cjs`);
const fs = require('node:fs');
let summary = '';
let statusText = '';
let agentBody = '';
try {
summary = fs.readFileSync(`${process.env.GITHUB_WORKSPACE}/patch-in/implement-status.out.txt`, 'utf8');
statusText = fs.readFileSync(
`${process.env.GITHUB_WORKSPACE}/patch-in/implement-status.out.txt`,
'utf8',
);
} catch {}
const title = `fix(#${process.env.ISSUE_NUMBER}): ${process.env.ISSUE_TITLE}`.slice(0, 110);
try {
agentBody = fs.readFileSync(
`${process.env.GITHUB_WORKSPACE}/patch-in/implement-pr-body.out.md`,
'utf8',
);
} catch {}
const parsed = auto.parseImplementStatus(statusText);
const summary = parsed.summary || statusText;
const title = auto.selectBotPrTitle({
agentTitle: parsed.title,
issueNumber: process.env.ISSUE_NUMBER,
issueTitle: process.env.ISSUE_TITLE,
maxLength: 110,
});
const body = auto.buildPullRequestBody({
issueNumber: process.env.ISSUE_NUMBER,
issueTitle: process.env.ISSUE_TITLE,
summary,
agentBody,
});
const { data: pr } = await github.rest.pulls.create({
...context.repo,
@@ -912,12 +940,33 @@ jobs:
clean: false,
}),
});
// Expose PR number for a separate @codex step that may use a
// human PAT (CODEX_REQUEST_GITHUB_TOKEN) without changing PR author.
core.setOutput('pull_number', String(pr.number));
core.setOutput('head_sha', pr.head.sha);
core.info(`Opened draft PR ${pr.html_url}`);
- name: Request Codex review on implement PR
if: steps.openpr.outputs.pull_number != ''
uses: actions/github-script@v7
env:
PULL_NUMBER: ${{ steps.openpr.outputs.pull_number }}
HEAD_SHA: ${{ steps.openpr.outputs.head_sha }}
with:
# Prefer maintainer PAT so Codex connector sees a human identity.
github-token: ${{ secrets.CODEX_REQUEST_GITHUB_TOKEN || secrets.TRIAGE_GITHUB_TOKEN || secrets.GITHUB_TOKEN }}
script: |
const auto = require(`${process.env.RUNNER_TEMP}/cursor-automation.cjs`);
const pull_number = Number(process.env.PULL_NUMBER);
const headSha = process.env.HEAD_SHA || '';
await github.rest.issues.createComment({
...context.repo,
issue_number: pr.number,
body: auto.buildCodexReviewRequestComment(1, pr.head.sha),
issue_number: pull_number,
body: auto.buildCodexReviewRequestComment(1, headSha, {
includeExternalMarker: true,
}),
});
core.info(`Opened draft PR ${pr.html_url}`);
core.info(`Requested @codex review on PR #${pull_number}`);
codex_loop:
name: Codex review loop
@@ -1176,7 +1225,8 @@ jobs:
if: steps.codex.outputs.action == 'request_review'
uses: actions/github-script@v7
with:
github-token: ${{ secrets.TRIAGE_GITHUB_TOKEN || secrets.GITHUB_TOKEN }}
# @codex must come from a human identity connected to Codex (binaricat PAT).
github-token: ${{ secrets.CODEX_REQUEST_GITHUB_TOKEN || secrets.TRIAGE_GITHUB_TOKEN || secrets.GITHUB_TOKEN }}
script: |
const auto = require(`${process.env.RUNNER_TEMP}/cursor-automation.cjs`);
const pull_number = Number(process.env.PULL_NUMBER);
@@ -1243,7 +1293,9 @@ jobs:
await github.rest.issues.createComment({
...context.repo,
issue_number: pull_number,
body: auto.buildCodexReviewRequestComment(1, pr.head.sha),
body: auto.buildCodexReviewRequestComment(1, pr.head.sha, {
includeExternalMarker: true,
}),
});
- name: Mark PR ready after clean Codex
@@ -1280,14 +1332,14 @@ jobs:
;;
esac
fi
# Tolerate already-ready / closed / permission noise.
# Tolerate already-ready / closed / permission noise. Still apply labels below.
ready_out="$(gh pr ready "$PULL_NUMBER" --repo "$GITHUB_REPOSITORY" 2>&1)" || {
if echo "$ready_out" | grep -qiE 'already ready|not a draft|is not a draft|is closed|closed\.|only draft'; then
echo "PR not convertible to ready (ok to skip): $ready_out"
exit 0
echo "PR not convertible to ready (ok to continue labeling): $ready_out"
else
echo "$ready_out" >&2
exit 1
fi
echo "$ready_out" >&2
exit 1
}
# Re-check head after gh pr ready (another push could race).
live_head2="$(gh pr view "$PULL_NUMBER" --repo "$GITHUB_REPOSITORY" --json headRefOid -q .headRefOid)"
@@ -1296,11 +1348,39 @@ jobs:
gh pr ready "$PULL_NUMBER" --repo "$GITHUB_REPOSITORY" --undo 2>/dev/null || true
exit 0
fi
gh pr edit "$PULL_NUMBER" --repo "$GITHUB_REPOSITORY" \
--add-label "automation:codex-clean" \
--add-label "automation:bot-pr" || true
gh pr edit "$PULL_NUMBER" --repo "$GITHUB_REPOSITORY" \
--remove-label "automation:codex-loop" || true
# Terminal label transition: drop codex-loop, add codex-clean (pure helper).
node -e '
const { execSync } = require("node:child_process");
const auto = require(process.env.RUNNER_TEMP + "/cursor-automation.cjs");
const pull = process.env.PULL_NUMBER;
const repo = process.env.GITHUB_REPOSITORY;
const raw = execSync(
`gh pr view ${pull} --repo ${repo} --json labels -q "[.labels[].name]"`,
{ encoding: "utf8" },
);
const existing = JSON.parse(raw);
const next = auto.nextCodexTerminalLabels(existing, "mark_ready");
const remove = existing.filter((l) => !next.includes(l));
const add = next.filter((l) => !existing.includes(l));
for (const name of remove) {
try {
execSync(
`gh pr edit ${pull} --repo ${repo} --remove-label ${JSON.stringify(name)}`,
{ stdio: "inherit" },
);
} catch (err) {
console.error("Failed to remove label", name, err.message);
process.exit(1);
}
}
if (add.length) {
const args = add.map((n) => `--add-label ${JSON.stringify(n)}`).join(" ");
execSync(`gh pr edit ${pull} --repo ${repo} ${args}`, {
stdio: "inherit",
});
}
console.log("mark_ready labels:", next.join(", "));
'
# Confirm draft=false before claiming success.
draft="$(gh pr view "$PULL_NUMBER" --repo "$GITHUB_REPOSITORY" --json isDraft -q .isDraft)"
if [[ "$draft" == "true" ]]; then
@@ -1326,10 +1406,11 @@ jobs:
script: |
const auto = require(`${process.env.RUNNER_TEMP}/cursor-automation.cjs`);
const pull_number = Number(process.env.PULL_NUMBER);
await github.rest.issues.addLabels({
...context.repo,
issue_number: pull_number,
labels: ['ready-for-human'],
await auto.applyCodexTerminalLabels({
github,
context,
pullNumber: pull_number,
terminal: 'give_up',
});
const reason = process.env.REASON || 'max_rounds';
const message =
@@ -1626,7 +1707,10 @@ jobs:
- name: Mark needs human when no fix produced
if: steps.codex.outputs.action == 'fix' && steps.fixpatch.outputs.empty == 'true'
uses: actions/github-script@v7
env:
PULL_NUMBER: ${{ needs.route.outputs.pull_number }}
with:
github-token: ${{ secrets.TRIAGE_GITHUB_TOKEN || secrets.GITHUB_TOKEN }}
script: |
const auto = require(`${process.env.RUNNER_TEMP}/cursor-automation.cjs`);
const pull_number = Number(process.env.PULL_NUMBER);
@@ -1640,13 +1724,12 @@ jobs:
'Automatic fix did not produce additional code changes for the latest Codex findings. Marking for human review.',
].join('\n'),
});
await github.rest.issues.addLabels({
...context.repo,
issue_number: pull_number,
labels: ['ready-for-human'],
await auto.applyCodexTerminalLabels({
github,
context,
pullNumber: pull_number,
terminal: 'empty_fix',
});
env:
PULL_NUMBER: ${{ needs.route.outputs.pull_number }}
- name: Mark needs human on fix failure
if: failure() && steps.codex.outputs.action == 'fix'
@@ -1654,6 +1737,7 @@ jobs:
env:
PULL_NUMBER: ${{ needs.route.outputs.pull_number }}
with:
github-token: ${{ secrets.TRIAGE_GITHUB_TOKEN || secrets.GITHUB_TOKEN }}
script: |
const fs = require('node:fs');
const helper = fs.existsSync(`${process.env.RUNNER_TEMP}/cursor-automation.cjs`)
@@ -1671,10 +1755,11 @@ jobs:
'Automatic Codex fix did not finish cleanly (agent, protected paths, or verification failed). Marking for human review.',
].join('\n'),
});
await github.rest.issues.addLabels({
...context.repo,
issue_number: pull_number,
labels: ['ready-for-human'],
await auto.applyCodexTerminalLabels({
github,
context,
pullNumber: pull_number,
terminal: 'verify_fail',
});
publish_codex_fix:
@@ -1736,6 +1821,7 @@ jobs:
- name: Re-request Codex after fix push
uses: actions/github-script@v7
with:
github-token: ${{ secrets.CODEX_REQUEST_GITHUB_TOKEN || secrets.TRIAGE_GITHUB_TOKEN || secrets.GITHUB_TOKEN }}
script: |
const auto = require(`${process.env.RUNNER_TEMP}/cursor-automation.cjs`);
const pull_number = Number(process.env.PULL_NUMBER);
@@ -1747,7 +1833,9 @@ jobs:
await github.rest.issues.createComment({
...context.repo,
issue_number: pull_number,
body: auto.buildCodexReviewRequestComment(nextRound, pr.head.sha),
body: auto.buildCodexReviewRequestComment(nextRound, pr.head.sha, {
includeExternalMarker: true,
}),
});
own_rerequest_codex:
@@ -1797,7 +1885,8 @@ jobs:
- name: Comment @codex review after human push
uses: actions/github-script@v7
with:
github-token: ${{ secrets.TRIAGE_GITHUB_TOKEN || secrets.GITHUB_TOKEN }}
# Human-connected Codex identity (binaricat PAT); not netcatty-bot.
github-token: ${{ secrets.CODEX_REQUEST_GITHUB_TOKEN || secrets.TRIAGE_GITHUB_TOKEN || secrets.GITHUB_TOKEN }}
script: |
const auto = require(`${process.env.GITHUB_WORKSPACE}/scripts/cursor-automation.cjs`);
const pull_number = Number(process.env.PULL_NUMBER);
@@ -1887,13 +1976,13 @@ jobs:
);
}
}
// Exactly one @codex review mention; plant both marker families for dedupe.
await github.rest.issues.createComment({
...context.repo,
issue_number: pull_number,
body: [
auto.buildCodexReviewRequestComment(round, pr.head.sha),
auto.buildExternalCodexRerequestComment(pr.head.sha),
].join('\n'),
body: auto.buildCodexReviewRequestComment(round, pr.head.sha, {
includeExternalMarker: true,
}),
});
external_rerequest_codex:
@@ -1942,8 +2031,8 @@ jobs:
- name: Comment @codex review after contributor push
uses: actions/github-script@v7
with:
# pull_request_target provides write GITHUB_TOKEN for forks; optional PAT override.
github-token: ${{ secrets.TRIAGE_GITHUB_TOKEN || secrets.GITHUB_TOKEN }}
# Prefer human Codex-connected PAT; fall back for forks/write token.
github-token: ${{ secrets.CODEX_REQUEST_GITHUB_TOKEN || secrets.TRIAGE_GITHUB_TOKEN || secrets.GITHUB_TOKEN }}
script: |
const auto = require(`${process.env.GITHUB_WORKSPACE}/scripts/cursor-automation.cjs`);
// External job checks out default branch only; tree is trusted.
@@ -2032,7 +2121,8 @@ jobs:
- name: Poll open automation PRs
uses: actions/github-script@v7
with:
github-token: ${{ secrets.TRIAGE_GITHUB_TOKEN || secrets.GITHUB_TOKEN }}
# Poll may re-@codex; use human-connected token for those comments.
github-token: ${{ secrets.CODEX_REQUEST_GITHUB_TOKEN || secrets.TRIAGE_GITHUB_TOKEN || secrets.GITHUB_TOKEN }}
script: |
const auto = require(`${process.env.GITHUB_WORKSPACE}/scripts/cursor-automation.cjs`);
const ownActors = process.env.OWN_ACTORS;
@@ -2272,20 +2362,12 @@ jobs:
);
continue;
}
await github.rest.issues.addLabels({
...context.repo,
issue_number: pr.number,
labels: ['automation:codex-clean'],
await auto.applyCodexTerminalLabels({
github,
context,
pullNumber: pr.number,
terminal: 'mark_ready',
});
try {
await github.rest.issues.removeLabel({
...context.repo,
issue_number: pr.number,
name: 'automation:codex-loop',
});
} catch {
// label may already be absent
}
core.info(`PR #${pr.number}: marked ready via clean reaction`);
handled += 1;
} catch (err) {
@@ -2321,6 +2403,7 @@ jobs:
body: auto.buildCodexReviewRequestComment(
round,
pr.head.sha,
{ includeExternalMarker: true },
),
});
core.info(
@@ -2338,7 +2421,9 @@ jobs:
await github.rest.issues.createComment({
...context.repo,
issue_number: pr.number,
body: auto.buildCodexReviewRequestComment(round, pr.head.sha),
body: auto.buildCodexReviewRequestComment(round, pr.head.sha, {
includeExternalMarker: true,
}),
});
core.info(`PR #${pr.number}: re-requested Codex after timeout`);
handled += 1;
+59 -46
View File
@@ -5,7 +5,11 @@ on:
types: [opened, edited]
permissions:
contents: read
issues: write
# workflow_dispatch is allowed from GITHUB_TOKEN (unlike issues.reopened)
# so format recovery can start triage without a human re-dispatch.
actions: write
jobs:
validate:
@@ -15,74 +19,83 @@ jobs:
github.event.issue.user.type != 'Bot' &&
!contains(github.event.issue.labels.*.name, 'format-exempt')
steps:
- name: Checkout format helpers
uses: actions/checkout@v5
with:
sparse-checkout: |
scripts/cursor-automation.cjs
sparse-checkout-cone-mode: false
- name: Validate title and body
uses: actions/github-script@v7
with:
script: |
const auto = require(`${process.env.GITHUB_WORKSPACE}/scripts/cursor-automation.cjs`);
const issue = context.payload.issue;
const title = issue.title.trim();
const body = (issue.body || '').trim();
const errors = [];
const modernTitle = /^\[(Bug|Feature)\] .{8,}/.test(title);
const legacyAppTitle = /^Bug:\s*.{5,}/i.test(title);
if (!modernTitle && !legacyAppTitle) {
errors.push(
'Title must start with `[Bug]` or `[Feature]` followed by a short summary (at least 8 characters after the prefix). Legacy app links using `Bug: ...` are also accepted. Example: `[Bug] SFTP upload fails on Windows`'
);
}
if (body.length < 120) {
errors.push(
'Body is too short. Please use the Bug Report or Feature Request template and fill in all required fields.'
);
}
const templateMarkers = [
'Steps to reproduce',
'Expected behavior',
'Actual behavior',
'Describe the problem',
'Problem / pain point',
'Proposed solution',
'Operating system',
];
const hasTemplateStructure = templateMarkers.some((marker) =>
body.includes(marker)
);
if (!hasTemplateStructure) {
errors.push(
'Body does not look like it came from an issue template. Choose **Bug Report** or **Feature Request** when opening an issue.'
);
}
const errors = auto.getIssueFormatErrors(issue);
const labels = new Set(
(issue.labels || []).map((label) =>
typeof label === 'string' ? label : label.name
)
);
const formatOk = errors.length === 0;
const recovery = auto.shouldRecoverIssueFormat({
state: issue.state,
labels: [...labels],
formatOk,
});
if (formatOk) {
if (recovery.recover) {
// Dispatch FIRST while invalid-format is still present and the
// issue may still be closed. workflow_dispatch sets manual=true
// so classify is eligible even with the label. Only after a
// successful dispatch do we reopen / clear the label — so a
// failed dispatch leaves a recoverable state (Codex P2).
const ref =
context.payload.repository?.default_branch || 'main';
try {
await github.rest.actions.createWorkflowDispatch({
owner: context.repo.owner,
repo: context.repo.repo,
workflow_id: 'cursor-automation.yml',
ref,
inputs: {
issue_number: String(issue.number),
},
});
core.info(
`Dispatched cursor-automation for recovered issue #${issue.number}`,
);
} catch (err) {
core.setFailed(
`Triage dispatch failed after format recovery; leaving invalid-format in place for retry: ${err.message}`,
);
return;
}
if (errors.length === 0) {
if (
issue.state === 'closed' &&
labels.has('invalid-format')
) {
labels.delete('invalid-format');
await github.rest.issues.update({
const update = {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue.number,
state: 'open',
labels: [...labels],
});
};
if (recovery.reopen) {
update.state = 'open';
}
await github.rest.issues.update(update);
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue.number,
body: '<!-- issue-format-bot --> Format looks good now. Reopening this issue.',
body: recovery.reopen
? '<!-- issue-format-bot --> Format looks good now. Reopening this issue and starting triage.'
: '<!-- issue-format-bot --> Format looks good now. Starting triage.',
});
} else {
core.info('Issue format OK');
}
core.info('Issue format OK');
return;
}