An open source design system that's fully customizable and agent ready
TypeScript
ci(spec-owner): decide a head without knowledge records once, then read the decision back (#9231)
Cindy Zhang52b8f559207a
diff --git a/.github/scripts/spec-owner-reconcile.cjs b/.github/scripts/spec-owner-reconcile.cjsindex f430008e3d..47cc437dde 100644--- a/.github/scripts/spec-owner-reconcile.cjs+++ b/.github/scripts/spec-owner-reconcile.cjs@@ -578,7 +578,89 @@ async function reconcileSpecOwnerGate({};}- const initialPr = await getPullRequest();+ /**+ * A head that changes no knowledge record has one decision, whatever any+ * owner says: "No knowledge records changed." It depends only on the head's+ * files, so it is written once per head and later events read it back+ * instead of reconciling (two reads when the event carries the full pull+ * request). Returns whether it decided the head, and the live pull request+ * when it had to read one, so the full reconcile does not read it again.+ */+ async function decideHeadWithoutRecords() {+ const eventPr = context.payload.pull_request;+ const livePr =+ eventPr?.changed_files == null ? await getPullRequest() : null;+ const pr = livePr ?? eventPr;+ const undecided = {done: false, livePr};+ if (isSettled(pr)) return undecided;+ const files = await github.paginate(github.rest.pulls.listFiles, {+ owner,+ repo,+ pull_number: pullNumber,+ per_page: 100,+ });+ const scope = classifyChanges(files, {expectedCount: pr.changed_files});+ if (!scope.complete || scope.touchesKnowledgeRecords) return undecided;++ const headSha = pr.head.sha;+ const decided = {satisfied: true, text: null};+ const statuses = await listStatuses(headSha);+ const spec = readSpecGate(statuses);+ const isDecided =+ spec.known &&+ spec.satisfied &&+ newestGateRun(statuses, repository)?.status.description ===+ mirrorDescription({state: 'success'});+ const code = readCodeGate(statuses);+ const gate =+ code &&+ composeReviewGate({+ codeGated: code.gated,+ codeDescription: code.description,+ spec: decided,+ });+ const current = statuses.find(+ status => status.context === REVIEW_GATE_CONTEXT,+ );+ const isGateCurrent =+ !gate ||+ (current?.state === gate.state &&+ current?.description === gate.description);+ if (labelNames(pr).has(env.REVIEW_LABEL)) {+ await removeLabel(env.REVIEW_LABEL);+ }+ if (isDecided && isGateCurrent) {+ core.info('This head already carries its decision: no records changed.');+ return {done: true};+ }+ if (!(await isLiveHeadWritable(headSha))) return {done: true};+ if (!isDecided) {+ await createStatus({+ sha: headSha,+ context: GATE_STATUS_CONTEXT,+ state: 'success',+ description: mirrorDescription({state: 'success'}),+ });+ }+ // The details link names the run that decided this head.+ await publishReviewGate(+ headSha,+ isDecided ? {...decided, url: spec.url} : decided,+ );+ return {done: true};+ }++ // A failed fast path falls through to the full reconcile, which writes the+ // waiting review gate before anything else can fail.+ let fastPath = {done: false, livePr: null};+ try {+ fastPath = await decideHeadWithoutRecords();+ } catch (error) {+ core.warning(`Falling back to the full reconcile: ${error.message}`);+ }+ if (fastPath.done) return;++ const initialPr = fastPath.livePr ?? (await getPullRequest());// Nothing about a merged or closed head is still being decided. Reconciling// one can only publish a decision that arrives after the merge it claims to// gate, so stop before writing any status.diff --git a/.github/scripts/spec-owner-reconcile.test.mjs b/.github/scripts/spec-owner-reconcile.test.mjsindex e81be3885b..6b2c5de975 100644--- a/.github/scripts/spec-owner-reconcile.test.mjs+++ b/.github/scripts/spec-owner-reconcile.test.mjs@@ -303,6 +303,33 @@ function published(state) {);}+/** Count every GitHub API call a run makes (paginated lists count once). */+function countApiCalls(harness) {+ const counter = {total: 0};+ for (const namespace of Object.values(harness.github.rest)) {+ for (const [name, method] of Object.entries(namespace)) {+ if (typeof method !== 'function') continue;+ namespace[name] = async (...args) => {+ counter.total += 1;+ return method(...args);+ };+ }+ }+ return counter;+}++const codeOnlyFile = {+ filename: 'packages/core/src/Button/Button.tsx',+ status: 'modified',+};++/** A pull_request_target event that carries the full pull request. */+function fullPrContext(harness, runId) {+ const ctx = context({runId, action: 'synchronize'});+ ctx.payload.pull_request = structuredClone(harness.state.pr);+ return ctx;+}+function latestReviewGate(state) {return state.statuses.find(status => status.context === 'review-required');}@@ -1454,3 +1481,80 @@ describe('spec owner workflow reconciliation', () => {});});});++describe('a head that changes no knowledge record', () => {+ it('is decided once, then read back in two calls with nothing written', async () => {+ const harness = createHarness({changedFile: codeOnlyFile});++ await run(harness, fullPrContext(harness, 100n));+ expect(latestGateStatus(harness.state)).toMatchObject({+ state: 'success',+ description: 'Mirrors review-required · spec ok',+ });+ expect(latestReviewGate(harness.state)).toMatchObject({+ state: 'success',+ description: 'No code review required.',+ });++ const statusCount = harness.state.statuses.length;+ const counter = countApiCalls(harness);+ await run(harness, fullPrContext(harness, 101n));++ // The file list and the head's statuses; no status is written again.+ expect(counter.total).toBe(2);+ expect(harness.state.statuses).toHaveLength(statusCount);+ });++ it('decides a new head in a handful of calls, not a full reconcile', async () => {+ const harness = createHarness({changedFile: codeOnlyFile});+ const counter = countApiCalls(harness);++ await run(harness, fullPrContext(harness, 100n));++ // Was 27 (pull ×3, files, reviews, comments, timeline, statuses ×6,+ // mirror and gate writes) before this head path.+ expect(counter.total).toBeLessThanOrEqual(8);+ });++ it('answers an owner review on a decided head in three calls', async () => {+ const harness = createHarness({changedFile: codeOnlyFile});+ await run(harness, fullPrContext(harness, 100n));+ const counter = countApiCalls(harness);++ await run(+ harness,+ context({+ runId: 101n,+ eventName: 'pull_request_review',+ action: 'submitted',+ review: {state: 'approved', user: {login: 'cixzhang'}},+ }),+ );++ // The review event carries no file count, so the pull request is read.+ expect(counter.total).toBe(3);+ });++ it('falls back to the full reconcile when its file read fails', async () => {+ const harness = createHarness({changedFile: codeOnlyFile});+ let failed = false;+ const listFiles = harness.github.rest.pulls.listFiles;+ harness.github.rest.pulls.listFiles = async options => {+ if (!failed) {+ failed = true;+ throw new Error('listFiles failed');+ }+ return listFiles(options);+ };++ await run(harness, fullPrContext(harness, 100n));++ expect(harness.state.calls).toContain(+ 'warning:Falling back to the full reconcile: listFiles failed',+ );+ expect(latestGateStatus(harness.state)).toMatchObject({+ state: 'success',+ description: 'Mirrors review-required · spec ok',+ });+ });+});