fix(ci): address Codex review findings for automation safety

Isolate publish onto fresh runners, harden Codex loop head scoping,
protect fork bootstrapping, and polish classification replies and
mark-ready error handling after local codex review.
This commit is contained in:
bincxz committed 2026-07-24 01:54:30 +08:00
1 parent ba5932e285
commit 073083c6ee
4 files changed
+845 -145

No files matched your search

+133 -10
View File
@@ -181,20 +181,28 @@ function normalizeClassification(raw) {
if (confidence < 0.8 && category === 'bug_ready') {
category = 'bug_needs_info';
reply =
'Thanks for the report. We still need a bit more evidence before changing the code. Please share the missing details below so we can reproduce it.';
// Keep the model's localized reply when present; only fall back to English.
if (!String(raw.reply || '').trim()) {
reply =
'Thanks for the report. We still need a bit more evidence before changing the code. Please share the missing details below so we can reproduce it.';
}
}
if (confidence < 0.8 && category === 'feature_quick_win') {
category = 'feature_defer';
reply =
'Thanks for the suggestion. The scope or tradeoffs are not clear enough for an automatic change yet, so a maintainer will take a look first.';
if (!String(raw.reply || '').trim()) {
reply =
'Thanks for the suggestion. The scope or tradeoffs are not clear enough for an automatic change yet, so a maintainer will take a look first.';
}
}
// Never auto-close low-confidence "unclear" issues.
if (confidence < 0.8 && category === 'unclear') {
category = 'bug_needs_info';
// Always replace "will be closed" style unclear replies with needs-info tone.
reply =
reply ||
'Thanks for writing in. We need a bit more detail before we can act on this. Please add the missing context below.';
String(raw.reply || '').trim() &&
!/clos(e|ing)|关闭|关掉|将关闭/i.test(String(raw.reply || ''))
? sanitizeUntrustedText(raw.reply, 3_000)
: 'Thanks for writing in. We need a bit more detail before we can act on this. Please add the missing context below.';
}
const label_corrections = Array.isArray(raw.label_corrections)
@@ -395,6 +403,32 @@ function isCodexTerminalReviewText(body) {
);
}
/** 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:[^\n0-9a-f]*([0-9a-f]{7,40})/i);
return match ? match[1].toLowerCase() : '';
}
function commitShasMatch(headSha, reviewedSha) {
const head = String(headSha || '').toLowerCase();
const reviewed = String(reviewedSha || '').toLowerCase();
if (!head || !reviewed) return false;
return head.startsWith(reviewed) || reviewed.startsWith(head.slice(0, 7));
}
function getLatestCommentTime(comments = [], predicate) {
let latest = 0;
for (const comment of comments) {
if (!predicate(comment)) continue;
const ts = Date.parse(comment.created_at || '') || 0;
if (ts > latest) latest = ts;
}
return latest;
}
/**
* Keep review comments that belong to the current head (or lack commit_id).
* Drops clearly stale comments pinned to older commits when headSha is known.
@@ -492,10 +526,25 @@ function decideCodexLoopAction({
maxRounds = 40,
hasAutomationRequest = false,
hasCodexActivity = false,
headSha = '',
summaryText = '',
lastAutomationRequestAt = 0,
lastCodexSummaryAt = 0,
} = {}) {
if (!eligible) {
return { action: 'skip', reason: 'not_fix_eligible' };
}
// New @codex request after the last summary → still waiting for that review.
// Exception: actionable inline-only findings already present for this head.
const hasActionableFindings =
Boolean(outcome) && outcome.clean === false && outcome.actionable === true;
if (
lastAutomationRequestAt > 0 &&
lastAutomationRequestAt > (lastCodexSummaryAt || 0) &&
!hasActionableFindings
) {
return { action: 'skip', reason: 'awaiting_codex_for_new_head' };
}
if (!hasCodexActivity) {
if (hasAutomationRequest) {
return { action: 'skip', reason: 'awaiting_codex' };
@@ -503,6 +552,17 @@ function decideCodexLoopAction({
return { action: 'request_review', reason: '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' };
}
return { action: 'mark_ready', reason: outcome.reason || 'codex_clean' };
}
if (outcome && outcome.actionable === false) {
@@ -545,6 +605,27 @@ function pathsFromGitStatusPorcelain(gitStatusPorcelain) {
return paths;
}
/**
* Parse `git diff --name-status -M` output so renames include both sides.
* Examples: "M\tfile", "A\tfile", "D\tfile", "R100\told\tnew"
*/
function pathsFromGitDiffNameStatus(nameStatusText) {
const paths = [];
for (const line of String(nameStatusText || '').split('\n')) {
const trimmed = line.trim();
if (!trimmed) continue;
const parts = trimmed.split(/\t/);
if (parts.length >= 3 && /^R\d*/.test(parts[0])) {
paths.push(parts[1], parts[2]);
} else if (parts.length >= 2) {
paths.push(parts[1]);
} else {
paths.push(trimmed.replace(/^[A-Z]\d*\s+/, ''));
}
}
return paths.filter(Boolean);
}
function formatCodexFindingsMarkdown({
summaryText = '',
reviewComments = [],
@@ -614,10 +695,16 @@ function hasProtectedChanges(gitStatusPorcelain) {
function hasProtectedChangesInSources({
gitStatusPorcelain = '',
changedFiles = [],
nameStatusText = '',
} = {}) {
const fromStatus = pathsFromGitStatusPorcelain(gitStatusPorcelain);
const fromCommits = (changedFiles || []).map(String);
return listProtectedPathHits([...fromStatus, ...fromCommits]);
const fromNameStatus = pathsFromGitDiffNameStatus(nameStatusText);
return listProtectedPathHits([
...fromStatus,
...fromCommits,
...fromNameStatus,
]);
}
function getCodexRoundFromComments(comments = []) {
@@ -691,8 +778,19 @@ async function prepareIssueContext({
return { shouldRun: false, issue };
}
// Daily limit only gates first admission. Follow-ups on already-admitted or
// needs-info issues must still re-classify when the author replies.
const alreadyAdmitted =
labelNames.includes('triage:admitted') ||
labelNames.includes('needs-info') ||
labelNames.includes('triage:bug-needs-info') ||
labelNames.includes('triage') ||
labelNames.includes('ready-for-agent') ||
labelNames.includes('ready-for-human');
if (
!manual &&
!alreadyAdmitted &&
!['OWNER', 'MEMBER', 'COLLABORATOR'].includes(issue.author_association)
) {
const startOfDay = new Date();
@@ -748,11 +846,11 @@ async function prepareIssueContext({
});
}
const { data: comments } = await github.rest.issues.listComments({
const comments = await github.paginate(github.rest.issues.listComments, {
owner,
repo,
issue_number: issue.number,
per_page: 50,
per_page: 100,
});
const input = {
@@ -900,9 +998,30 @@ 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.
try {
const q = `repo:${context.repo.owner}/${context.repo.repo} is:pr ("Fixes #${n}" OR "fixes #${n}" OR head:cursor/issue-${n}-)`;
const items = await github.paginate(
github.rest.search.issuesAndPullRequests,
{ q, per_page: 20 },
(response) => response.data.items,
);
for (const item of items) {
if (!item.pull_request || !item.number) continue;
const { data: pull } = await github.rest.pulls.get({
...context.repo,
pull_number: item.number,
});
if (isBotPrForIssue(pull, issueNumber)) return pull;
}
} catch {
// fall through to list scan
}
const states = ['open', 'closed'];
for (const state of states) {
const { data: pulls } = await github.rest.pulls.list({
const pulls = await github.paginate(github.rest.pulls.list, {
...context.repo,
state,
per_page: 100,
@@ -945,6 +1064,9 @@ module.exports = {
parseClassificationText,
parseClassificationFile,
isCodexTerminalReviewText,
extractReviewedCommitSha,
commitShasMatch,
getLatestCommentTime,
filterCodexReviewCommentsForHead,
parseCodexReviewOutcome,
hasAutomationCodexRequest,
@@ -953,6 +1075,7 @@ module.exports = {
formatCodexFindingsMarkdown,
listProtectedPathHits,
pathsFromGitStatusPorcelain,
pathsFromGitDiffNameStatus,
hasProtectedChanges,
hasProtectedChangesInSources,
getCodexRoundFromComments,
+83
View File
@@ -179,6 +179,53 @@ test('decideCodexLoopAction skips when awaiting existing @codex request', () =>
assert.equal(d.reason, 'awaiting_codex');
});
test('decideCodexLoopAction ignores stale clean summary for other head', () => {
const d = auto.decideCodexLoopAction({
eligible: true,
hasCodexActivity: true,
headSha: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
summaryText:
"Codex Review: Didn't find any major issues. Swish!\n**Reviewed commit:** `bbbbbbb`",
outcome: { clean: true, actionable: false, reason: 'codex_clean_summary' },
});
assert.equal(d.action, 'skip');
assert.equal(d.reason, 'stale_clean_summary');
});
test('decideCodexLoopAction awaits when request is newer than summary', () => {
const d = auto.decideCodexLoopAction({
eligible: true,
hasCodexActivity: true,
lastAutomationRequestAt: 2000,
lastCodexSummaryAt: 1000,
outcome: { clean: true, actionable: false, reason: 'codex_clean_summary' },
summaryText: "Didn't find any major issues. Swish!",
});
assert.equal(d.action, 'skip');
assert.equal(d.reason, 'awaiting_codex_for_new_head');
});
test('decideCodexLoopAction still fixes inline-only findings after request', () => {
const d = auto.decideCodexLoopAction({
eligible: true,
hasCodexActivity: true,
lastAutomationRequestAt: 2000,
lastCodexSummaryAt: 0,
round: 1,
maxRounds: 40,
outcome: { clean: false, actionable: true, reason: 'codex_inline_findings' },
});
assert.equal(d.action, 'fix');
});
test('extractReviewedCommitSha parses Codex marker', () => {
assert.equal(
auto.extractReviewedCommitSha(
'Codex Review\n**Reviewed commit:** `fd871e86f1`\n',
),
'fd871e86f1',
);
});
test('decideCodexLoopAction requests review when no activity', () => {
const d = auto.decideCodexLoopAction({
eligible: true,
@@ -236,8 +283,32 @@ test('normalizeClassification does not auto-close low-confidence unclear', () =>
});
assert.equal(result.category, 'bug_needs_info');
assert.equal(result.should_implement, false);
assert.match(result.reply, /Please clarify|more detail/i);
});
test('normalizeClassification replaces closing-language unclear replies', () => {
const result = auto.normalizeClassification({
category: 'unclear',
confidence: 0.2,
summary: 'vague',
reasoning: 'no detail',
reply: 'This issue will be closed as unclear.',
});
assert.equal(result.category, 'bug_needs_info');
assert.doesNotMatch(result.reply, /will be closed/i);
});
test('normalizeClassification keeps localized low-confidence bug reply', () => {
const result = auto.normalizeClassification({
category: 'bug_ready',
confidence: 0.5,
summary: 'maybe',
reasoning: 'unclear',
reply: '请补充复现步骤和日志。',
});
assert.equal(result.category, 'bug_needs_info');
assert.equal(result.reply, '请补充复现步骤和日志。');
});
test('isBotPrForIssue matches marker + Fixes', () => {
assert.equal(
auto.isBotPrForIssue(
@@ -269,6 +340,18 @@ test('pathsFromGitStatusPorcelain keeps both rename sides', () => {
assert.ok(paths.includes('scripts/evil.cjs'));
});
test('pathsFromGitDiffNameStatus keeps rename source and dest', () => {
const paths = auto.pathsFromGitDiffNameStatus(
'R100\t.github/workflows/x.yml\tunprotected.yml\nM\tsrc/a.ts\n',
);
assert.ok(paths.includes('.github/workflows/x.yml'));
assert.ok(paths.includes('unprotected.yml'));
assert.ok(paths.includes('src/a.ts'));
const hits = auto.hasProtectedChangesInSources({
nameStatusText: 'R100\t.github/workflows/x.yml\tunprotected.yml\n',
});
assert.deepEqual(hits, ['.github/workflows/x.yml']);
});
test('extractJsonObject reads fenced blocks', () => {
const obj = auto.extractJsonObject(
'Here you go:\n```json\n{"category":"unclear","confidence":0.9,"summary":"x","reasoning":"y","reply":"please clarify the steps"}\n```\n',