The agent harness performance optimization system. Skills, instincts, memory, security, and research-first development for Claude Code, Codex, Opencode, Cursor and beyond.
fix: address release gate review findings
Viggo Phillips5b05a3f0063d
diff --git a/scripts/ci/verify-release-gates.js b/scripts/ci/verify-release-gates.jsindex 698c302c5..d3c28c5f6 100644--- a/scripts/ci/verify-release-gates.js+++ b/scripts/ci/verify-release-gates.js@@ -1,8 +1,67 @@'use strict';+const Ajv = require('ajv');+const API_VERSION = '2022-11-28';const DEFAULT_ATTEMPTS = 20;const DEFAULT_DELAY_MS = 30_000;+const ajv = new Ajv({ allErrors: true });++const referenceSchema = {+ type: 'object',+ required: ['object'],+ properties: {+ object: {+ type: 'object',+ required: ['type', 'sha'],+ properties: { type: { type: 'string' }, sha: { type: 'string' } },+ },+ },+};+const tagSchema = {+ type: 'object',+ required: ['verification', 'object'],+ properties: {+ verification: {+ type: 'object',+ required: ['verified'],+ properties: {+ verified: { type: 'boolean' },+ reason: { anyOf: [{ type: 'string' }, { type: 'null' }] },+ },+ },+ object: {+ type: 'object',+ required: ['type', 'sha'],+ properties: { type: { type: 'string' }, sha: { type: 'string' } },+ },+ },+};+const workflowRunsSchema = collectionSchema('workflow_runs');+const checkRunsSchema = collectionSchema('check_runs');++function collectionSchema(property) {+ return {+ type: 'object',+ required: [property],+ properties: {+ [property]: {+ type: 'array',+ items: {+ type: 'object',+ required: ['id', 'name', 'head_sha', 'status', 'conclusion'],+ properties: {+ id: { type: 'integer' },+ name: { type: 'string' },+ head_sha: { type: 'string' },+ status: { type: 'string' },+ conclusion: { anyOf: [{ type: 'string' }, { type: 'null' }] },+ },+ },+ },+ },+ };+}function requiredEnvironment(env = process.env) {const values = {@@ -23,8 +82,14 @@ function requiredEnvironment(env = process.env) {return values;}-async function githubApi(path, { repository, token }, fetchImpl = fetch) {- const response = await fetchImpl(`https://api.github.com/repos/${repository}${path}`, {+async function githubApi(path, inputs, fetchImpl = fetch, schema) {+ const { payload } = await githubApiPage(path, inputs, fetchImpl, schema);+ return payload;+}++async function githubApiPage(pathOrUrl, { repository, token }, fetchImpl, schema) {+ const url = githubApiUrl(pathOrUrl, repository);+ const response = await fetchImpl(url, {headers: {Accept: 'application/vnd.github+json',Authorization: `Bearer ${token}`,@@ -32,21 +97,64 @@ async function githubApi(path, { repository, token }, fetchImpl = fetch) {},});if (!response.ok) {- throw new Error(`GitHub API ${path} failed with status ${response.status}`);+ throw new Error(`GitHub API ${url} failed with status ${response.status}`);+ }+ const payload = await response.json();+ const validate = ajv.compile(schema);+ if (!validate(payload)) {+ throw new Error(`GitHub API response validation failed: ${ajv.errorsText(validate.errors)}`);}- return response.json();+ return { payload, next: nextPageUrl(response.headers?.get?.('link'), repository) };+}++function githubApiUrl(pathOrUrl, repository) {+ if (!pathOrUrl.startsWith('https://')) {+ return `https://api.github.com/repos/${repository}${pathOrUrl}`;+ }+ const url = new URL(pathOrUrl);+ if (url.origin !== 'https://api.github.com' || !url.pathname.startsWith(`/repos/${repository}/`)) {+ throw new Error('GitHub API pagination link escaped the release repository');+ }+ return url.toString();+}++function nextPageUrl(linkHeader, repository) {+ if (!linkHeader) return null;+ const next = linkHeader+ .split(',')+ .map(value => value.trim().match(/^<([^>]+)>;\s*rel="([^"]+)"$/))+ .find(match => match?.[2] === 'next');+ if (!next) return null;+ return githubApiUrl(next[1], repository);+}++async function githubApiPages(path, itemsKey, inputs, fetchImpl, schema) {+ const items = [];+ let next = path;+ while (next) {+ const page = await githubApiPage(next, inputs, fetchImpl, schema);+ items.push(...page.payload[itemsKey]);+ next = page.next;+ }+ return items;}async function verifySignedAnnotatedTag(inputs, fetchImpl = fetch) {const reference = await githubApi(`/git/ref/tags/${encodeURIComponent(inputs.releaseTag)}`,inputs,- fetchImpl+ fetchImpl,+ referenceSchema);if (reference.object.type !== 'tag') {throw new Error('Release tag must be annotated; lightweight tags are rejected');}- const tagObject = await githubApi(`/git/tags/${reference.object.sha}`, inputs, fetchImpl);+ const tagObject = await githubApi(+ `/git/tags/${reference.object.sha}`,+ inputs,+ fetchImpl,+ tagSchema+ );if (tagObject.verification.verified !== true) {const reason = tagObject.verification.reason || 'unknown';throw new Error(`Release tag signature is not verified: ${reason}`);@@ -96,19 +204,23 @@ async function waitForExactShaGates(inputs, fetchImpl = fetch, sleep = defaultSlconst attempts = positiveInteger(process.env.RELEASE_GATE_ATTEMPTS, DEFAULT_ATTEMPTS);const delayMs = positiveInteger(process.env.RELEASE_GATE_DELAY_MS, DEFAULT_DELAY_MS);for (let attempt = 1; attempt <= attempts; attempt += 1) {- const [workflowPayload, checkPayload] = await Promise.all([- githubApi(+ const [runs, checks] = await Promise.all([+ githubApiPages(`/actions/runs?head_sha=${inputs.releaseSha}&event=push&per_page=100`,+ 'workflow_runs',inputs,- fetchImpl+ fetchImpl,+ workflowRunsSchema+ ),+ githubApiPages(+ `/commits/${inputs.releaseSha}/check-runs?per_page=100`,+ 'check_runs',+ inputs,+ fetchImpl,+ checkRunsSchema),- githubApi(`/commits/${inputs.releaseSha}/check-runs?per_page=100`, inputs, fetchImpl),]);- const assessment = assessExactShaGates(- workflowPayload.workflow_runs || [],- checkPayload.check_runs || [],- inputs.releaseSha- );+ const assessment = assessExactShaGates(runs, checks, inputs.releaseSha);if (assessment.state === 'passed') return;if (assessment.state === 'failed') throw new Error(assessment.reason);if (attempt < attempts) await sleep(delayMs);@@ -145,6 +257,8 @@ if (require.main === module) {module.exports = {assessExactShaGates,+ githubApi,+ githubApiPages,requiredEnvironment,verifySignedAnnotatedTag,waitForExactShaGates,diff --git a/tests/ci/release-packed-artifact-workflow.test.js b/tests/ci/release-packed-artifact-workflow.test.jsindex 3659ff51a..1a6533832 100644--- a/tests/ci/release-packed-artifact-workflow.test.js+++ b/tests/ci/release-packed-artifact-workflow.test.js@@ -3,30 +3,48 @@const assert = require('assert');const fs = require('fs');const path = require('path');+const yaml = require('js-yaml');const repoRoot = path.resolve(__dirname, '..', '..');const workflowPaths = ['.github/workflows/release.yml','.github/workflows/reusable-release.yml',];-const { assessExactShaGates } = require('../../scripts/ci/verify-release-gates.js');+const {+ assessExactShaGates,+ verifySignedAnnotatedTag,+ waitForExactShaGates,+} = require('../../scripts/ci/verify-release-gates.js');const lifecycleRunnerSource = load('tests/ci/packed-artifact-lifecycle.js');let passed = 0;let failed = 0;+const pendingTests = [];function test(name, fn) {try {- fn();- console.log(` ✓ ${name}`);- passed += 1;+ const result = fn();+ if (result && typeof result.then === 'function') {+ pendingTests.push(result.then(() => pass(name), error => fail(name, error)));+ } else {+ pass(name);+ }} catch (error) {- console.log(` ✗ ${name}`);- console.log(` Error: ${error.message}`);- failed += 1;+ fail(name, error);}}+function pass(name) {+ console.log(` ✓ ${name}`);+ passed += 1;+}++function fail(name, error) {+ console.log(` ✗ ${name}`);+ console.log(` Error: ${error.message}`);+ failed += 1;+}+function load(relativePath) {return fs.readFileSync(path.join(repoRoot, relativePath), 'utf8').replace(/\r\n/g, '\n');}@@ -52,16 +70,23 @@ for (const workflowPath of workflowPaths) {test(`${workflowPath} verifies signed tags and exact-SHA CI gates before building`, () => {const verify = jobBlock(source, 'verify', 'lifecycle');+ const workflow = yaml.load(source);+ const verifyJob = workflow.jobs.verify;+ const gateStep = verifyJob.steps.find(+ step => step.name === 'Verify signed tag and exact-SHA CI gates'+ );const gateIndex = verify.indexOf('name: Verify signed tag and exact-SHA CI gates');const installIndex = verify.indexOf('name: Install dependencies');+ const effectivePermissions = verifyJob.permissions || workflow.permissions || {};assert.ok(gateIndex >= 0, 'missing release provenance gate');assert.ok(installIndex > gateIndex, 'release provenance must be verified before dependencies run');- assert.match(verify, /node scripts\/ci\/verify-release-gates\.js/);- assert.match(verify, /RELEASE_SHA(?:=|:)/);- assert.match(verify, /RELEASE_TAG:/);- assert.match(source, /actions:\s*read/);- assert.match(source, /checks:\s*read/);+ assert.ok(gateStep, 'missing named release provenance gate step');+ assert.match(gateStep.run, /node scripts\/ci\/verify-release-gates\.js/);+ assert.match(gateStep.run, /RELEASE_SHA=/);+ assert.ok(gateStep.env?.RELEASE_TAG, 'gate step must receive RELEASE_TAG');+ assert.strictEqual(effectivePermissions.actions, 'read');+ assert.strictEqual(effectivePermissions.checks, 'read');});test(`${workflowPath} packs once and exports the package name and SHA-256`, () => {@@ -200,6 +225,86 @@ test('release gate verifier accepts only successful checks for the exact SHA', (assert.strictEqual(failedCodeql.state, 'failed');});+test('release gate verifier validates GitHub response shapes before use', async () => {+ const inputs = {+ repository: 'affaan-m/ECC',+ releaseSha: 'a'.repeat(40),+ releaseTag: 'v1.2.3',+ token: 'test-token',+ };+ const malformedResponse = async () => ({+ ok: true,+ status: 200,+ headers: { get: () => null },+ json: async () => ({ object: { type: 'tag' } }),+ });++ await assert.rejects(+ verifySignedAnnotatedTag(inputs, malformedResponse),+ /GitHub API response validation failed/+ );+});++test('release gate verifier evaluates checks from every GitHub result page', async () => {+ const releaseSha = 'a'.repeat(40);+ const inputs = {+ repository: 'affaan-m/ECC',+ releaseSha,+ releaseTag: 'v1.2.3',+ token: 'test-token',+ };+ const pageTwo =+ `https://api.github.com/repos/${inputs.repository}/commits/${releaseSha}/check-runs` ++ '?per_page=100&page=2';+ const response = (payload, link = null) => ({+ ok: true,+ status: 200,+ headers: { get: name => (name.toLowerCase() === 'link' ? link : null) },+ json: async () => payload,+ });+ const fetchImpl = async url => {+ if (url.includes('/actions/runs?')) {+ return response({+ workflow_runs: [+ { id: 1, name: 'CI', head_sha: releaseSha, status: 'completed', conclusion: 'success' },+ ],+ });+ }+ if (url === pageTwo) {+ return response({+ check_runs: [+ {+ id: 2,+ name: 'CodeQL JavaScript',+ head_sha: releaseSha,+ status: 'completed',+ conclusion: 'failure',+ },+ ],+ });+ }+ return response(+ {+ check_runs: [+ {+ id: 1,+ name: 'CodeQL Actions',+ head_sha: releaseSha,+ status: 'completed',+ conclusion: 'success',+ },+ ],+ },+ `<${pageTwo}>; rel="next"`+ );+ };++ await assert.rejects(+ waitForExactShaGates(inputs, fetchImpl),+ /CodeQL JavaScript concluded failure/+ );+});+test('reusable release requires its input to resolve through the tag namespace', () => {const source = load('.github/workflows/reusable-release.yml');const verify = jobBlock(source, 'verify', 'lifecycle');@@ -327,6 +432,8 @@ test('packed lifecycle installs and verifies the opt-in Ito distribution surfaceassert.match(lifecycleRunnerSource, /packed Itô bridge executed a PATH collision/);});-console.log(`\nPassed: ${passed}`);-console.log(`Failed: ${failed}`);-process.exit(failed > 0 ? 1 : 0);+Promise.all(pendingTests).then(() => {+ console.log(`\nPassed: ${passed}`);+ console.log(`Failed: ${failed}`);+ process.exitCode = failed > 0 ? 1 : 0;+});