diff --git a/src/automation/mergeStewardGuards.test.ts b/src/automation/mergeStewardGuards.test.ts index 2c12826a..e46b02ef 100644 --- a/src/automation/mergeStewardGuards.test.ts +++ b/src/automation/mergeStewardGuards.test.ts @@ -22,6 +22,9 @@ const OLD = 'b'.repeat(40); function baseline(overrides: Partial = {}): StewardSnapshot { return { prNumber: 101, + author: 'swarm-publisher', + prReviews: [], + automationAccounts: ['swarm-publisher', 'swarm-worker', 'swarm-steward'], headSha: HEAD, title: 'fix(api): handle empty payload (AX-1)', files: ['src/api.ts', 'src/api.test.ts'], @@ -121,7 +124,7 @@ describe('guard: required checks green on the head SHA', () => { }); describe('guard: risk is low', () => { - it('a medium-risk PR is load-bearing', () => { + it('a medium-risk PR without a human approval is load-bearing', () => { expectLoadBearing('risk', baseline({ risk: { level: 'medium', headSha: HEAD } })); }); @@ -220,3 +223,78 @@ describe('dry-run only', () => { expect(source).not.toMatch(/child_process|execFile|spawn\(|fetch\(|octokit|gh pr merge|\/merge\b/i); }); }); + +// AGT-4881 — medium and above merge only after one human approval on the head SHA. +describe('risk guard: medium and above need a human approval on the head', () => { + const at = (minute: number) => `2026-11-15T00:${String(minute).padStart(2, '0')}:00Z`; + const medium = (prReviews: StewardSnapshot['prReviews'], extra: Partial = {}) => + baseline({ risk: { level: 'medium', headSha: HEAD }, prReviews, ...extra }); + + it('no approval: refused', () => { + expectLoadBearing('risk', medium([])); + }); + + it('a bot approval: refused', () => { + expectLoadBearing('risk', medium([ + { login: 'ci-helper[bot]', userType: 'Bot', state: 'APPROVED', commitId: HEAD, submittedAt: at(1) }, + ])); + }); + + it('an approval on an earlier SHA: refused', () => { + expectLoadBearing('risk', medium([ + { login: 'alice', userType: 'User', state: 'APPROVED', commitId: OLD, submittedAt: at(1) }, + ])); + }); + + it('a valid human approval on the head: passes', () => { + const decision = evaluateMergeSteward(medium([ + { login: 'alice', userType: 'User', state: 'APPROVED', commitId: HEAD, submittedAt: at(1) }, + ])); + expect(decision.mergeable).toBe(true); + expect(decision.results.find((r) => r.guard === 'risk')?.reason).toContain('approved on the head by alice'); + }); + + it('high risk follows the same rule', () => { + const human = { login: 'alice', userType: 'User', state: 'APPROVED' as const, commitId: HEAD, submittedAt: at(1) }; + expectLoadBearing('risk', baseline({ risk: { level: 'high', headSha: HEAD } })); + expect(evaluateMergeSteward(baseline({ risk: { level: 'high', headSha: HEAD }, prReviews: [human] })).mergeable).toBe(true); + }); + + it('factory accounts and the PR author do not count, even with a User type', () => { + expectLoadBearing('risk', medium([ + { login: 'swarm-worker', userType: 'User', state: 'APPROVED', commitId: HEAD, submittedAt: at(1) }, + { login: 'Swarm-Steward', userType: 'User', state: 'APPROVED', commitId: HEAD, submittedAt: at(2) }, + ])); + expectLoadBearing('risk', medium( + [{ login: 'carol', userType: 'User', state: 'APPROVED', commitId: HEAD, submittedAt: at(1) }], + { author: 'carol' }, + )); + }); + + it('a missing user type is not human', () => { + expectLoadBearing('risk', medium([{ login: 'alice', state: 'APPROVED', commitId: HEAD, submittedAt: at(1) }])); + }); + + it('a later change request or dismissal withdraws the approval; a comment does not', () => { + const approve = { login: 'alice', userType: 'User', state: 'APPROVED' as const, commitId: HEAD, submittedAt: at(1) }; + expectLoadBearing('risk', medium([ + { ...approve, state: 'CHANGES_REQUESTED', submittedAt: at(2) }, + approve, + ])); + expectLoadBearing('risk', medium([approve, { ...approve, state: 'DISMISSED', submittedAt: at(3) }])); + expect(evaluateMergeSteward(medium([approve, { ...approve, state: 'COMMENTED', submittedAt: at(4) }])).mergeable).toBe(true); + }); + + it('without a configured automation account list no approval is trusted', () => { + expectLoadBearing('risk', medium( + [{ login: 'alice', userType: 'User', state: 'APPROVED', commitId: HEAD, submittedAt: at(1) }], + { automationAccounts: [] }, + )); + }); + + it('the other guards still apply to an approved medium PR', () => { + const approved = [{ login: 'alice', userType: 'User', state: 'APPROVED' as const, commitId: HEAD, submittedAt: at(1) }]; + expectLoadBearing('required-checks', medium(approved, { checks: [] })); + expectLoadBearing('review-completeness', medium(approved, { review: null })); + }); +}); diff --git a/src/automation/mergeStewardGuards.ts b/src/automation/mergeStewardGuards.ts index 9f7725d5..fa8f7162 100644 --- a/src/automation/mergeStewardGuards.ts +++ b/src/automation/mergeStewardGuards.ts @@ -63,8 +63,30 @@ export interface StewardRisk { headSha: string; } +/** One submitted GitHub PR review. */ +export interface StewardPrReview { + login: string; + /** GitHub user type: `User`, `Bot`, `Organization`, … Missing is treated as not human. */ + userType?: string; + state: 'APPROVED' | 'CHANGES_REQUESTED' | 'COMMENTED' | 'DISMISSED' | 'PENDING'; + /** Commit the review was submitted against. */ + commitId: string; + /** ISO timestamp; reviews are ordered by it. */ + submittedAt: string; +} + export interface StewardSnapshot extends StewardFileList { prNumber: number; + /** Login that opened the PR. Its own approval never counts. */ + author: string; + /** Submitted reviews on the PR (any order). */ + prReviews: readonly StewardPrReview[]; + /** + * Logins the factory itself acts as (steward, worker, publisher). Agents here can act + * under an account whose GitHub type is `User`, so the type alone proves nothing. + * Empty means "not configured", and then no approval can be trusted. + */ + automationAccounts: readonly string[]; /** The PR's current head commit. All evidence must be for this SHA. */ headSha: string; title: string; @@ -134,6 +156,42 @@ const requiredChecksGuard: StewardGuard = { }, }; +/** + * The human approval a medium/high change needs (AGT-4881): at least one reviewer + * whose latest decisive review is APPROVED on the head SHA, who is a `User`, is not + * the PR author and is not one of the factory's own accounts. COMMENTED reviews do + * not change a reviewer's standing (GitHub semantics); a later CHANGES_REQUESTED or + * DISMISSED withdraws an earlier approval. + */ +export function humanApprovalOnHead(s: StewardSnapshot): { approvedBy: string[]; reason: string } { + if (s.automationAccounts.length === 0) { + return { approvedBy: [], reason: 'automation account list is not configured, so no approval can be trusted' }; + } + const automation = new Set(s.automationAccounts.map((login) => login.toLowerCase())); + const author = s.author.toLowerCase(); + const latest = new Map(); + const ordered = [...s.prReviews].sort((a, b) => Date.parse(a.submittedAt) - Date.parse(b.submittedAt)); + for (const review of ordered) { + if (review.state === 'COMMENTED' || review.state === 'PENDING') continue; + latest.set(review.login.toLowerCase(), review); + } + const approvedBy: string[] = []; + const ignored: string[] = []; + for (const [login, review] of latest) { + if (review.state !== 'APPROVED') continue; + if (review.commitId !== s.headSha) ignored.push(`${review.login} approved ${short(review.commitId)}, not the head`); + else if (review.userType !== 'User') ignored.push(`${review.login} is ${review.userType ?? 'of unknown type'}`); + else if (login === author) ignored.push(`${review.login} is the PR author`); + else if (automation.has(login)) ignored.push(`${review.login} is a factory account`); + else approvedBy.push(review.login); + } + if (approvedBy.length > 0) return { approvedBy, reason: `approved on the head by ${approvedBy.join(', ')}` }; + return { + approvedBy, + reason: ignored.length > 0 ? `no valid human approval (${ignored.join('; ')})` : 'no human approval', + }; +} + const riskGuard: StewardGuard = { name: 'risk', check(s) { @@ -142,8 +200,13 @@ const riskGuard: StewardGuard = { if (s.risk.headSha !== s.headSha) { return fail(name, `risk was classified on ${short(s.risk.headSha)}, not the head ${short(s.headSha)}`); } - if (s.risk.level !== 'low') return fail(name, `risk is ${s.risk.level}; only low merges without a person`); - return pass(name, 'risk is low'); + if (s.risk.level === 'low') return pass(name, 'risk is low'); + // medium and high: the steward merges only after a person approved this exact head. + const approval = humanApprovalOnHead(s); + if (approval.approvedBy.length === 0) { + return fail(name, `risk is ${s.risk.level} and needs one human approval on the head: ${approval.reason}`); + } + return pass(name, `risk is ${s.risk.level}; ${approval.reason}`); }, };