Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pr-governance/policy.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@
"require_clearing_label_on_r2": {
"type": "boolean",
"description": "Whether R2 changes require an owner-approved or agent-approved clearing label. Set to false on relaxed repositories to allow AI-reviewed R2 merges without manual labels. Defaults to true."
},
"require_owner_approval_on_r2": {
"type": "boolean",
"description": "When true (strict repositories), R2 changes require owner-approved specifically and reject agent-approved alone. Defaults to false."
}
}
}
23 changes: 17 additions & 6 deletions pr-governance/scripts/gate.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const POLICY_FIELDS = ['version', 'mode', 'r0_paths', 'r2_paths',
'dependabot_template_exemption', 'max_evidence_age_hours'];
const POLICY_OPTIONAL_FIELDS = ['owner', 'reviewer', 'reviewers', 'review_routing',
'max_r1_diff_lines', 'blocking_severity', 'block_unresolved_threads',
'require_clearing_label_on_r2'];
'require_clearing_label_on_r2', 'require_owner_approval_on_r2'];
// A policy file may leave the reviewer identity to protected configuration, so
// a missing reviewer names every accepted source instead of surfacing as a
// generic field error. The three legacy variables remain a valid explicit
Expand Down Expand Up @@ -262,6 +262,11 @@ function validatePolicy(policy) {
if (policy.require_clearing_label_on_r2 !== undefined) {
assert(typeof policy.require_clearing_label_on_r2 === 'boolean', 'Invalid require_clearing_label_on_r2');
}
if (policy.require_owner_approval_on_r2 !== undefined) {
assert(typeof policy.require_owner_approval_on_r2 === 'boolean', 'Invalid require_owner_approval_on_r2');
}
assert(!(policy.require_owner_approval_on_r2 === true && policy.require_clearing_label_on_r2 === false),
'Conflicting R2 clearing policy flags');
if (policy.max_r1_diff_lines !== undefined) {
assert(Number.isSafeInteger(policy.max_r1_diff_lines) && policy.max_r1_diff_lines >= 50
&& policy.max_r1_diff_lines <= 5000, 'Invalid max_r1_diff_lines');
Expand Down Expand Up @@ -729,8 +734,9 @@ function evaluate(snapshot, policy, policySha, now = Math.floor(Date.now() / 100
}
const blocking = policy.block_unresolved_threads === false ? [] : threads.map(id => `thread:${id}`);
if (review.reason === 'review_findings') blocking.push('marker:unresolved_findings');
const needsClearingLabel = risk.tier === 'R2' && policy.require_clearing_label_on_r2 !== false;
const tierOK = !needsClearingLabel || labels.applied !== null;
const needsOwnerOnly = risk.tier === 'R2' && policy.require_owner_approval_on_r2 === true;
const needsClearingLabel = risk.tier === 'R2' && (needsOwnerOnly || policy.require_clearing_label_on_r2 !== false);
const tierOK = !needsClearingLabel || (needsOwnerOnly ? labels.applied === 'owner' : labels.applied !== null);
const allowed = review.state === 'success' && tierOK && template.ok;
let reason;
if (review.state === 'failure') reason = review.reason;
Expand All @@ -745,9 +751,14 @@ function evaluate(snapshot, policy, policySha, now = Math.floor(Date.now() / 100
// so an agent-cleared merge is visible instead of passing as a human review.
const cleared = labels.applied === 'owner' ? 'cleared by owner-approved'
: labels.applied === 'agent' ? 'cleared by agent-approved, NOT confirmed by the owner' : null;
const gateReasonText = reason === 'clearing_label_required' && risk.volumeEscalated
? 'diff volume exceeded; needs owner-approved or agent-approved'
: (GATE_TEXT[reason] || REVIEW_TEXT[reason] || reason);
const ownerRequiredText = risk.volumeEscalated
? 'diff volume exceeded; needs owner-approved'
: 'needs owner-approved';
const gateReasonText = reason === 'clearing_label_required' && needsOwnerOnly
? ownerRequiredText
: reason === 'clearing_label_required' && risk.volumeEscalated
? 'diff volume exceeded; needs owner-approved or agent-approved'
: (GATE_TEXT[reason] || REVIEW_TEXT[reason] || reason);
const advisory = review.reason === 'review_advisory'
? `advisory findings (${Object.keys(SEVERITY_LEVELS).find(key => SEVERITY_LEVELS[key] === review.severity)})`
: null;
Expand Down
26 changes: 25 additions & 1 deletion pr-governance/scripts/test-gate.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ test('the schema file lists exactly the fields the evaluator enforces', () => {
'dependabot_template_exemption', 'max_evidence_age_hours'].sort();
assert.deepEqual(declared, [...validated, 'owner', 'reviewer', 'reviewers', 'review_routing',
'max_r1_diff_lines', 'blocking_severity', 'block_unresolved_threads',
'require_clearing_label_on_r2'].sort());
'require_clearing_label_on_r2', 'require_owner_approval_on_r2'].sort());
assert.deepEqual([...schema.required].sort(), validated);
assert.deepEqual([...schema.properties.mode.enum].sort(), ['enforce', 'local', 'report-only']);
assert.deepEqual(schema.properties.reviewer.required.sort(), ['app_id', 'login', 'provider', 'user_id']);
Expand Down Expand Up @@ -678,3 +678,27 @@ test('require_clearing_label_on_r2: false allows AI-reviewed R2 PR without label
const strictBlocked = evaluate(strict);
assert.equal(strictBlocked.statuses.find(s => s.context === 'risk-gate').state, 'failure');
});

test('require_owner_approval_on_r2: true rejects agent-approved and requires owner-approved', () => {
const agentOnly = receiptMarker(fixture({ path: 'config/app.json' }));
agentOnly.policy.require_owner_approval_on_r2 = true;
clearRecord(agentOnly, labelEvent(agentOnly, { name: 'agent-approved', actor: { id: 999, login: 'agent' } }));
const denied = evaluate(agentOnly);
assert.equal(denied.allowed, false);
const deniedGate = denied.statuses.find(s => s.context === 'risk-gate');
assert.equal(deniedGate.state, 'failure');
assert.equal(deniedGate.description, 'R2: needs owner-approved');

const ownerBound = receiptMarker(fixture({ path: 'config/app.json' }));
ownerBound.policy.require_owner_approval_on_r2 = true;
clearRecord(ownerBound, labelEvent(ownerBound));
const accepted = evaluate(ownerBound);
assert.equal(accepted.allowed, true);
assert.equal(accepted.labels.applied, 'owner');
assert.match(accepted.statuses.find(s => s.context === 'risk-gate').description, /cleared by owner-approved/);

const conflict = fixture();
conflict.policy.require_owner_approval_on_r2 = true;
conflict.policy.require_clearing_label_on_r2 = false;
assert.throws(() => gate.validatePolicy(conflict.policy), /Conflicting R2 clearing policy flags/);
});
9 changes: 6 additions & 3 deletions scripts/policy-tiers.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
"strict": {
"blocking_severity": "P2",
"block_unresolved_threads": true,
"require_clearing_label_on_r2": true
"require_clearing_label_on_r2": true,
"require_owner_approval_on_r2": true
},
"standard": {
"blocking_severity": "P1",
"block_unresolved_threads": false,
"require_clearing_label_on_r2": true
"require_clearing_label_on_r2": true,
"require_owner_approval_on_r2": false
},
"relaxed": {
"blocking_severity": "P0",
"block_unresolved_threads": false,
"require_clearing_label_on_r2": false
"require_clearing_label_on_r2": false,
"require_owner_approval_on_r2": false
}
},
"repositories": {
Expand Down
7 changes: 4 additions & 3 deletions scripts/sync-policies.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const POLICY = '.github/governance/policy.json';
const BRANCH = 'chore/policy-tier';

// The fields this script owns. Anything else in a policy file is left alone.
export const FIELDS = ['blocking_severity', 'block_unresolved_threads', 'require_clearing_label_on_r2'];
export const FIELDS = ['blocking_severity', 'block_unresolved_threads', 'require_clearing_label_on_r2', 'require_owner_approval_on_r2'];

export function applyTier(policy, tier) {
const next = { ...policy };
Expand Down Expand Up @@ -119,8 +119,9 @@ function main(argv) {
'## AI assistance', '', 'DeepSeek Harness prepared the policy change and this body.', '',
'## What changed', '',
`\`${POLICY}\` now sets \`blocking_severity: ${tier.blocking_severity}\`,`,
`\`block_unresolved_threads: ${tier.block_unresolved_threads}\`, and`,
`\`require_clearing_label_on_r2: ${tier.require_clearing_label_on_r2}\`.`, '',
`\`block_unresolved_threads: ${tier.block_unresolved_threads}\`,`,
`\`require_clearing_label_on_r2: ${tier.require_clearing_label_on_r2}\`, and`,
`\`require_owner_approval_on_r2: ${tier.require_owner_approval_on_r2}\`.`, '',
'## How verified', '',
'The fields are validated by the gate schema; the action validates the policy at',
'load time, and this change touches no other field.', '',
Expand Down
10 changes: 9 additions & 1 deletion scripts/test-sync-policies.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,26 @@ test('applying a tier keeps every field the script does not own', async () => {
blocking_severity: 'P2',
block_unresolved_threads: true,
require_clearing_label_on_r2: true,
require_owner_approval_on_r2: true,
});
assert.deepEqual(next, {
...policy,
blocking_severity: 'P2',
block_unresolved_threads: true,
require_clearing_label_on_r2: true,
require_owner_approval_on_r2: true,
});
assert.deepEqual(policy.r2_paths, ['config/**'], 'the input is not mutated');
});

test('a repository already on its tier is recognised', async () => {
const { sameTier } = await load();
const tier = { blocking_severity: 'P1', block_unresolved_threads: false, require_clearing_label_on_r2: true };
const tier = {
blocking_severity: 'P1',
block_unresolved_threads: false,
require_clearing_label_on_r2: true,
require_owner_approval_on_r2: false,
};
assert.equal(sameTier({ ...tier }, tier), true);
assert.equal(sameTier({ ...tier, blocking_severity: 'P2' }, tier), false);
assert.equal(sameTier({}, tier), false);
Expand All @@ -43,6 +50,7 @@ test('the manifest gives every repository a documented tier', () => {
assert.match(manifest.tiers[tier].blocking_severity, /^P[0-3]$/);
assert.equal(typeof manifest.tiers[tier].block_unresolved_threads, 'boolean');
assert.equal(typeof manifest.tiers[tier].require_clearing_label_on_r2, 'boolean');
assert.equal(typeof manifest.tiers[tier].require_owner_approval_on_r2, 'boolean');
}
assert.ok(Object.keys(manifest.repositories).length >= 40);
});