Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(ci): a permission failure must never red-X a contributor's PR (live incident)

Garry Tan3c26f2eaf241
diff --git a/scripts/pr-gate.d.mts b/scripts/pr-gate.d.mts
index b5bad976f5..5a40186bdb 100644
--- a/scripts/pr-gate.d.mts
+++ b/scripts/pr-gate.d.mts
@@ -92,3 +92,6 @@ export declare function runGate(
env?: Record<string, string | undefined>,
fetchImpl?: typeof fetch,
): Promise<number>;
+
+export declare function isPermissionFailure(err: unknown): boolean;
+export declare const PERMISSION_HELP: (msg: string) => string;
diff --git a/scripts/pr-gate.mjs b/scripts/pr-gate.mjs
index c92576d5f3..b1386095d4 100644
--- a/scripts/pr-gate.mjs
+++ b/scripts/pr-gate.mjs
@@ -1171,6 +1171,28 @@ export async function runGate(dir, env = process.env, fetchImpl = fetch) {
return lane === 'close-lane' ? 1 : 0;
}
+/**
+ * A missing WRITE permission (or a token that cannot see the resource) is an
+ * operator condition, never a statement about the PR under review.
+ *
+ * Observed on this gate's first live run: the repository's GITHUB_TOKEN was
+ * read-only, so every comment and label call returned 403, the throw reached
+ * the top-level handler as exit 2, and the gate put a red X on every open PR —
+ * including an outside contributor's — with no comment saying why. The gate is
+ * advisory. It must never fail a contributor's check because it could not talk
+ * to the API. 401/403/404 from the GitHub side warn loudly and exit 0; a real
+ * outage or a bug in here still fails visibly.
+ */
+export function isPermissionFailure(err) {
+ return /\b(401|403|404)\b/.test(String(err?.message ?? err));
+}
+
+export const PERMISSION_HELP = (msg) =>
+ `PR gate could not post its verdict: ${msg}. This is a repository permission ` +
+ 'problem, not a finding about this PR. Operator: Settings → Actions → General → ' +
+ 'Workflow permissions must allow read and write, and ANTHROPIC_API_KEY must be ' +
+ 'set for the usefulness verdict to run.';
+
// Import side-effect guard: only run when executed directly (node/bun),
// never when the exports are imported by tests.
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
@@ -1182,7 +1204,11 @@ if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href)
runGate(dir).then(
(code) => process.exit(code),
(err) => {
- // Infrastructure failure (GitHub API down, bad inputs): fail visibly.
+ if (isPermissionFailure(err)) {
+ console.log(`::warning::${PERMISSION_HELP(String(err?.message ?? err))}`);
+ process.exit(0);
+ }
+ // Anything else (GitHub down, malformed inputs, a bug here): fail visibly.
console.error(`::error::PR gate crashed: ${err?.stack ?? err}`);
process.exit(2);
},
diff --git a/test/pr-gate-workflow.test.ts b/test/pr-gate-workflow.test.ts
index 2b804a1e04..5e0d35c0b1 100644
--- a/test/pr-gate-workflow.test.ts
+++ b/test/pr-gate-workflow.test.ts
@@ -39,6 +39,8 @@ import { tmpdir } from 'node:os';
import { join } from 'node:path';
import {
checkTitle,
+ isPermissionFailure,
+ PERMISSION_HELP,
detectRedFlags,
detectPolicyMisses,
hasScreenshot,
@@ -324,6 +326,39 @@ describe('pr-gate script rubric pins', () => {
});
});
+describe('permission failures never red-X a PR (live incident, 2026-08-04)', () => {
+ // The gate's first live run: the repo's GITHUB_TOKEN was read-only, every
+ // comment/label call 403'd, the throw became exit 2, and an outside
+ // contributor's PR got a red X with no comment explaining it. The gate is
+ // advisory — it must degrade, not accuse.
+ test('GitHub permission/visibility failures are not the PR\'s fault', () => {
+ expect(isPermissionFailure(new Error('comment upsert failed: 403'))).toBe(true);
+ expect(isPermissionFailure(new Error('label add failed: 403'))).toBe(true);
+ expect(isPermissionFailure(new Error('label remove failed: 401'))).toBe(true);
+ expect(isPermissionFailure(new Error('pr fetch failed: 404'))).toBe(true);
+ });
+
+ test('real outages and bugs still fail visibly', () => {
+ expect(isPermissionFailure(new Error('comment upsert failed: 500'))).toBe(false);
+ expect(isPermissionFailure(new Error('comment upsert failed: 502'))).toBe(false);
+ expect(isPermissionFailure(new TypeError('x is not a function'))).toBe(false);
+ expect(isPermissionFailure(undefined)).toBe(false);
+ });
+
+ test('the operator, not the contributor, is told what to fix', () => {
+ const help = PERMISSION_HELP('comment upsert failed: 403');
+ expect(help).toContain('not a finding about this PR');
+ expect(help).toContain('Workflow permissions');
+ expect(help).toContain('ANTHROPIC_API_KEY');
+ });
+
+ test('the entry handler routes permission failures to exit 0', () => {
+ const handler = SCRIPT.slice(SCRIPT.indexOf('runGate(dir).then'));
+ expect(handler).toMatch(/isPermissionFailure\(err\)[\s\S]*process\.exit\(0\)/);
+ expect(handler).toMatch(/process\.exit\(2\)/);
+ });
+});
+
describe('checkTitle (version-first rule)', () => {
test('accepts version-first titles', () => {
expect(