From 1b024481d9d86cb4d4b10eb7f8569f9c8e2f0981 Mon Sep 17 00:00:00 2001 From: Efra Espada Date: Thu, 19 Nov 2026 19:55:15 +0100 Subject: [PATCH] bugfix-976-recover-a-uniquely-identifiable-accepted-bugbot-status-dispatch-without-a-returned-run-id: recover correlated accepted status dispatch acknowledgments (#976) --- .../workflows/copilot_bugbot_status_card.yml | 6 +- build/github_action/index.js | 158 ++++++++++++++++-- docs/bugbot/failure-scenarios.mdx | 21 +++ quality/bugbot/evidence/issue-976/README.md | 35 ++++ .../evidence/issue-976/controls-2.log.gz | Bin 0 -> 450 bytes .../evidence/issue-976/controls-3.log.gz | Bin 0 -> 414 bytes .../evidence/issue-976/controls-4.log.gz | Bin 0 -> 162 bytes .../evidence/issue-976/controls-5.log.gz | Bin 0 -> 234 bytes .../evidence/issue-976/controls-6.log.gz | Bin 0 -> 237 bytes .../evidence/issue-976/controls-7.log.gz | Bin 0 -> 246 bytes .../evidence/issue-976/final-controls.log.gz | Bin 0 -> 246 bytes .../issue-976/initial-controls.log.gz | Bin 0 -> 450 bytes specs/CATALOG.md | 4 +- specs/bugbot-review-state-reconciliation.md | 86 ++++++++++ specs/catalog.json | 4 +- src/actions/github_action.ts | 2 +- ...github_bugbot_status_card_dispatch.test.ts | 155 ++++++++++++++++- ...github_bugbot_status_card_workflow.test.ts | 13 +- ...ithub_bugbot_status_card_acknowledgment.ts | 89 ++++++++++ .../github_bugbot_status_card_dispatch.ts | 26 ++- .../github_bugbot_status_card_handoff.ts | 7 + .../github_bugbot_status_card_workflow.ts | 12 +- 22 files changed, 574 insertions(+), 44 deletions(-) create mode 100644 quality/bugbot/evidence/issue-976/README.md create mode 100644 quality/bugbot/evidence/issue-976/controls-2.log.gz create mode 100644 quality/bugbot/evidence/issue-976/controls-3.log.gz create mode 100644 quality/bugbot/evidence/issue-976/controls-4.log.gz create mode 100644 quality/bugbot/evidence/issue-976/controls-5.log.gz create mode 100644 quality/bugbot/evidence/issue-976/controls-6.log.gz create mode 100644 quality/bugbot/evidence/issue-976/controls-7.log.gz create mode 100644 quality/bugbot/evidence/issue-976/final-controls.log.gz create mode 100644 quality/bugbot/evidence/issue-976/initial-controls.log.gz create mode 100644 src/infrastructure/github/github_bugbot_status_card_acknowledgment.ts diff --git a/.github/workflows/copilot_bugbot_status_card.yml b/.github/workflows/copilot_bugbot_status_card.yml index 7a1f6a2d9..3df17b369 100644 --- a/.github/workflows/copilot_bugbot_status_card.yml +++ b/.github/workflows/copilot_bugbot_status_card.yml @@ -1,5 +1,5 @@ name: Copilot - Bugbot Status Card -run-name: Bugbot status · PR ${{ inputs.pull_number }} +run-name: Bugbot status · PR ${{ inputs.pull_number }} · ${{ inputs.operation }} on: workflow_dispatch: @@ -8,6 +8,10 @@ on: description: Pull request number required: true type: number + operation: + description: SHA-256 correlation of the versioned handoff; not publication authority + required: true + type: string work: description: Versioned publication handoff required: true diff --git a/build/github_action/index.js b/build/github_action/index.js index 0ee3642c3..f9b17b351 100644 --- a/build/github_action/index.js +++ b/build/github_action/index.js @@ -96336,7 +96336,7 @@ async function runGitHubAction(verificationAuthority) { event: eventName, ref, actor, server: serverUrl, workflowRef: process.env.GITHUB_WORKFLOW_REF ?? '', locale: (0, github_action_locale_inputs_1.readGithubActionLocaleInputs)(github_action_input_1.getGithubActionInput).pullRequest, policy: verificationAuthority?.policyDigest ?? 'default', - inputs: { pullNumber: payload.inputs?.pull_number, work: payload.inputs?.work }, + inputs: { pullNumber: payload.inputs?.pull_number, work: payload.inputs?.work, operation: payload.inputs?.operation }, }); if (effect === 'skipped') core.setFailed('No current Bugbot status card publication was authorized.'); @@ -164262,6 +164262,122 @@ function githubBugbotReviewProgress(context, summary, current, observeCheck, wak } +/***/ }), + +/***/ 86730: +/***/ ((__unused_webpack_module, exports, __nccwpck_require__) => { + +"use strict"; + +Object.defineProperty(exports, "__esModule", ({ value: true })); +exports.statusCardUnacknowledged = void 0; +exports.createGithubBugbotStatusCardAcknowledgment = createGithubBugbotStatusCardAcknowledgment; +const github_bugbot_status_card_handoff_1 = __nccwpck_require__(83620); +const positive_integer_policy_1 = __nccwpck_require__(19879); +const github_bugbot_status_card_installation_1 = __nccwpck_require__(31666); +class StatusCardAcknowledgmentError extends Error { +} +const statusCardUnacknowledged = () => new StatusCardAcknowledgmentError('The shared Bugbot status card writer did not acknowledge publication. Inspect its native run and retry acknowledgment; preserve the last verified card and Check fallback.'); +exports.statusCardUnacknowledged = statusCardUnacknowledged; +/** Correlation is data provenance, never independent producer-code attestation. */ +function createGithubBugbotStatusCardAcknowledgment(binding) { + const { read, write, scope, ref, runId, attempt, repositoryId } = binding; + const workflow = { ...scope, workflow_id: github_bugbot_status_card_installation_1.bugbotStatusCardWorkflow }; + const unresolved = exports.statusCardUnacknowledged; + const acknowledgments = new Map(); + return async (inputs, sequence) => { + const operation = (0, github_bugbot_status_card_handoff_1.githubBugbotStatusCardOperation)(inputs.work, sequence); + const title = 'Bugbot status · PR ' + inputs.pull_number + ' · ' + operation; + let acknowledgment = acknowledgments.get(operation); + if (!acknowledgment) { + acknowledgment = { issued: false }; + acknowledgments.set(operation, acknowledgment); + } + if (acknowledgment.id) + return { id: acknowledgment.id, title }; + if (acknowledgment.pending) + return { id: await acknowledgment.pending, title }; + const state = acknowledgment; + state.pending = (async () => { + if (!state.issued) { + // A lost response can still mean accepted: retry reads, never this POST. + state.issued = true; + const { data } = await write.request('POST /repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches', { + ...workflow, ref, inputs: { ...inputs, operation }, return_run_details: true, + }); + const direct = (0, positive_integer_policy_1.parsePositiveSafeInteger)(data?.workflow_run_id); + if (direct) + return state.id = direct; + } + for (let lookup = 0; lookup < 6; lookup += 1) { + try { + const expected = await read.rest.actions.getWorkflow(workflow); + const origin = await read.rest.actions.getWorkflowRun({ ...scope, run_id: runId }); + const created = origin.data.created_at; + if (!(0, positive_integer_policy_1.parsePositiveSafeInteger)(expected.data.id) || expected.data.path !== github_bugbot_status_card_installation_1.bugbotStatusCardPath + || origin.data.id !== runId || origin.data.repository.id !== repositoryId || origin.data.run_attempt !== attempt + || typeof created !== 'string' || !Number.isFinite(Date.parse(created))) + throw unresolved(); + const matches = new Set(); + const seen = new Set(); + let complete = false; + let total; + for (let page = 1; page <= 10; page += 1) { + const response = await read.rest.actions.listWorkflowRuns({ ...workflow, + branch: ref, event: 'workflow_dispatch', created: '>=' + created, per_page: 100, page }); + const data = response.data; + if (!Array.isArray(data.workflow_runs) || data.workflow_runs.length > 100 + || !Number.isSafeInteger(data.total_count) || data.total_count < 0 + || total !== undefined && total !== data.total_count) + throw unresolved(); + total = data.total_count; + for (const run of data.workflow_runs) { + const id = (0, positive_integer_policy_1.parsePositiveSafeInteger)(run.id); + if (!id || seen.has(id)) + throw unresolved(); + seen.add(id); + if (run.repository.id === repositoryId && run.workflow_id === expected.data.id + && run.path === github_bugbot_status_card_installation_1.bugbotStatusCardPath && run.event === 'workflow_dispatch' + && run.head_branch === ref && run.display_title === title && run.run_attempt === 1 + && Number.isFinite(Date.parse(run.created_at)) && Date.parse(run.created_at) >= Date.parse(created)) + matches.add(id); + } + if (seen.size === total || data.workflow_runs.length < 100) { + if (data.total_count !== seen.size) + throw unresolved(); + complete = true; + break; + } + } + if (!complete || matches.size > 1) + throw unresolved(); + if (matches.size === 1) + return state.id = [...matches][0]; + } + catch (error) { + if ([401, 403].includes(Number((0, github_bugbot_status_card_installation_1.statusCardGithubErrorStatus)(error))) || error instanceof StatusCardAcknowledgmentError) + throw unresolved(); + if (lookup === 5) + throw unresolved(); + } + if (lookup < 5) + await new Promise(resolve => setTimeout(resolve, 500)); + } + throw unresolved(); + })().catch(() => { throw unresolved(); }); + try { + return { id: await state.pending, title }; + } + catch { + throw unresolved(); + } + finally { + state.pending = undefined; + } + }; +} + + /***/ }), /***/ 42190: @@ -164278,7 +164394,8 @@ const positive_integer_policy_1 = __nccwpck_require__(19879); const github_pull_request_client_factory_1 = __nccwpck_require__(9068); const github_bugbot_status_card_handoff_1 = __nccwpck_require__(83620); const github_bugbot_status_card_installation_1 = __nccwpck_require__(31666); -const unresolved = () => new Error('The shared Bugbot status card writer did not acknowledge publication.'); +const github_bugbot_status_card_acknowledgment_1 = __nccwpck_require__(86730); +const unresolved = github_bugbot_status_card_acknowledgment_1.statusCardUnacknowledged; async function createGithubBugbotStatusCardDispatcher(binding, environment = process.env) { const runId = (0, positive_integer_policy_1.parsePositiveSafeInteger)(environment.GITHUB_RUN_ID); const attempt = (0, positive_integer_policy_1.parsePositiveSafeInteger)(environment.GITHUB_RUN_ATTEMPT); @@ -164291,23 +164408,16 @@ async function createGithubBugbotStatusCardDispatcher(binding, environment = pro const read = (0, github_1.getOctokit)(binding.evidenceToken); const write = (0, github_1.getOctokit)(binding.token); const scope = { owner: binding.owner, repo: binding.repository }; - const workflow = { ...scope, workflow_id: github_bugbot_status_card_installation_1.bugbotStatusCardWorkflow }; const installation = await (0, github_bugbot_status_card_installation_1.readGithubBugbotStatusCardInstallation)(read, scope, ref); if (installation === 'absent') return undefined; const pullRequests = new pull_request_lifecycle_repository_1.PullRequestLifecycleRepository((0, github_pull_request_client_factory_1.createPullRequestLifecycleClient)()); - const dispatch = async (request) => { + const acknowledge = (0, github_bugbot_status_card_acknowledgment_1.createGithubBugbotStatusCardAcknowledgment)({ read, write, scope, ref, runId, attempt, repositoryId }); + const dispatch = async (request, sequence) => { const inputs = (0, github_bugbot_status_card_handoff_1.encodeGithubBugbotStatusCardHandoff)(request); if (installation !== 'installed' || !inputs) throw unresolved(); - const { data } = await write.request('POST /repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches', { - ...workflow, ref, inputs, return_run_details: true, - }); - const published = data; - const operation = (0, positive_integer_policy_1.parsePositiveSafeInteger)(published?.workflow_run_id); - if (!operation) - throw unresolved(); - return operation; + return acknowledge(inputs, sequence); }; const terminal = { async publish(input) { @@ -164325,7 +164435,7 @@ async function createGithubBugbotStatusCardDispatcher(binding, environment = pro // This deadline is only for acknowledging the short comment job. const until = Date.now() + 7 * 60000; while (Date.now() < until) { - const observed = await read.rest.actions.getWorkflowRun({ ...scope, run_id: effect }).catch((error) => { + const observed = await read.rest.actions.getWorkflowRun({ ...scope, run_id: effect.id }).catch((error) => { if ((0, github_bugbot_status_card_installation_1.statusCardGithubErrorStatus)(error) === 404) return undefined; throw unresolved(); @@ -164335,13 +164445,13 @@ async function createGithubBugbotStatusCardDispatcher(binding, environment = pro continue; } const { data } = observed; - if (data.id !== effect || data.repository.id !== repositoryId || data.path !== github_bugbot_status_card_installation_1.bugbotStatusCardPath + if (data.id !== effect.id || data.display_title !== effect.title || data.run_attempt !== 1 || data.repository.id !== repositoryId || data.path !== github_bugbot_status_card_installation_1.bugbotStatusCardPath || data.event !== 'workflow_dispatch' || data.head_branch !== ref) throw unresolved(); if (data.status === 'completed') { if (data.conclusion !== 'success') throw unresolved(); - const { data: jobs } = await read.rest.actions.listJobsForWorkflowRun({ ...scope, run_id: effect, per_page: 20, filter: 'latest' }); + const { data: jobs } = await read.rest.actions.listJobsForWorkflowRun({ ...scope, run_id: effect.id, per_page: 20, filter: 'latest' }); if (jobs.total_count === 1 && jobs.jobs.length === 1 && jobs.jobs[0].name === 'Publish Bugbot status card' && jobs.jobs[0].head_sha === data.head_sha && jobs.jobs[0].conclusion === 'success' && jobs.jobs[0].steps?.some((step) => step.name === 'Publish canonical comment' && step.conclusion === 'success')) @@ -164359,7 +164469,7 @@ async function createGithubBugbotStatusCardDispatcher(binding, environment = pro if (installation !== 'installed' || !observation.baseSha) return; await dispatch({ kind: 'progress', runId, attempt, source: { repositoryId, pullNumber: observation.pullNumber, - base: observation.baseSha, baseRef: observation.baseRefSha ?? observation.baseSha, head: observation.headSha, policy: binding.policy } }); + base: observation.baseSha, baseRef: observation.baseRefSha ?? observation.baseSha, head: observation.headSha, policy: binding.policy } }, observation.sequence); }, }; } @@ -164375,6 +164485,8 @@ async function createGithubBugbotStatusCardDispatcher(binding, environment = pro Object.defineProperty(exports, "__esModule", ({ value: true })); exports.decodeGithubBugbotStatusCardHandoff = decodeGithubBugbotStatusCardHandoff; exports.encodeGithubBugbotStatusCardHandoff = encodeGithubBugbotStatusCardHandoff; +exports.githubBugbotStatusCardOperation = githubBugbotStatusCardOperation; +const node_crypto_1 = __nccwpck_require__(6005); const bugbot_status_card_publication_policy_1 = __nccwpck_require__(4809); const positive_integer_policy_1 = __nccwpck_require__(19879); const maxHandoffBytes = 64000; @@ -164425,6 +164537,12 @@ function encodeGithubBugbotStatusCardHandoff(request) { const work = JSON.stringify({ version: 1, ...request }); return decodeGithubBugbotStatusCardHandoff({ pullNumber, work }) ? { pull_number: String(pullNumber), work } : undefined; } +/** A correlation key binds bytes; the worker still validates publication authority. */ +function githubBugbotStatusCardOperation(work, sequence) { + if (sequence !== undefined && (!Number.isSafeInteger(sequence) || sequence < 0)) + throw new Error('Invalid progress correlation sequence.'); + return (sequence === undefined ? '' : String(sequence) + ':') + (0, node_crypto_1.createHash)('sha256').update(work).digest('hex'); +} /***/ }), @@ -164611,9 +164729,15 @@ const sourceEvents = new Map([ ]); async function runGithubBugbotStatusCardWorkflow(binding) { const request = (0, github_bugbot_status_card_handoff_1.decodeGithubBugbotStatusCardHandoff)(binding.inputs); + const correlation = typeof binding.inputs.operation === 'string' + ? binding.inputs.operation.match(/^(0|[1-9]\d*):[a-f0-9]{64}$/u)?.[1] : undefined; + const sequence = correlation === undefined ? undefined : Number(correlation); const repository = binding.owner + '/' + binding.repository; if (binding.event !== 'workflow_dispatch' || binding.workflowRef !== repository + '/' + github_bugbot_status_card_installation_1.bugbotStatusCardPath + '@' + binding.ref - || !/^https:\/\/[a-z0-9.-]+(?::\d+)?$/iu.test(binding.server) || !request) + || !/^https:\/\/[a-z0-9.-]+(?::\d+)?$/iu.test(binding.server) || !request + || request.kind === 'progress' && (sequence === undefined || !Number.isSafeInteger(sequence)) + || typeof binding.inputs.work !== 'string' + || binding.inputs.operation !== (0, github_bugbot_status_card_handoff_1.githubBugbotStatusCardOperation)(binding.inputs.work, request.kind === 'progress' ? sequence : undefined)) return 'skipped'; const api = (0, github_1.getOctokit)(binding.evidenceToken); const scope = { owner: binding.owner, repo: binding.repository }; diff --git a/docs/bugbot/failure-scenarios.mdx b/docs/bugbot/failure-scenarios.mdx index cbe25394f..455fa079a 100644 --- a/docs/bugbot/failure-scenarios.mdx +++ b/docs/bugbot/failure-scenarios.mdx @@ -193,3 +193,24 @@ confirmation. Correct the named provider/response cause and recheck from the current source; preserve identity and reconciliation checks. Completed writes remain in place and must not be repeated blindly. See the [structured error reference](/security-operations/operations/error-reference#conflict-eligibility-and-review-freshness). + +## Shared writer acknowledgment + +An accepted status-writer dispatch can omit its run ID. The caller recovers only +one native run that matches the exact handoff operation, repository, workflow, +default ref and producer attempt. A recent run on the same PR is insufficient. +The matching source workflow must be installed on trusted default; preparing a +PR or merging develop does not establish installation. + +If acknowledgment remains unavailable, inspect the native writer run and retry +acknowledgment. The previous verified card and Review check remain authoritative; +no completed publication receipt or unqueued card fallback is issued. A retry in +the same producer adapter reads the accepted operation instead of repeating its +POST. A native rerun creates a new producer attempt and retains the existing +source/head/policy fences. This is not a durable exactly-once guarantee across +arbitrary process restarts. + +Missing permission, foreign or ambiguous runs and incomplete listings fail +closed. The bounded lookup applies to this short publication acknowledgment; +it imposes no model investigation deadline or token/turn/read limit. Operation +correlation establishes data/run identity, not independent code attestation. diff --git a/quality/bugbot/evidence/issue-976/README.md b/quality/bugbot/evidence/issue-976/README.md new file mode 100644 index 000000000..67fc25812 --- /dev/null +++ b/quality/bugbot/evidence/issue-976/README.md @@ -0,0 +1,35 @@ +# Accepted dispatch acknowledgment — #976 + +The five focused dispatch, worker, handoff, publication-workflow and policy +suites pass **62 tests** with fake providers. The dispatch matrix includes the +direct ID, unique accepted 204, absent/ambiguous match, foreign workflow/ref/ +repository/operation, denied lookup permission, transient workflow/origin/list +reads, acknowledgment replay, a foreign direct ID, later/incomplete pages, +lost POST response, concurrent retries, replaced origin and progress sequences. +The actual worker rejects a missing/forged correlation before provider reads. + +The caller hashes the actual encoded handoff. Progress includes its native +observation sequence in the correlation so subsequent progress still wakes the +writer. A replay shares the accepted operation rather than blindly sending +another POST. The worker independently recomputes the hash before applying its +existing source, actor, head, policy, attempt and serialized-publication checks. +Run/data correlation is not independent attestation of executable producer code. + +The first three control attempts could not compile: two TypeScript inference +errors, followed by a stale numeric run-ID use after introducing a typed effect. +They executed zero tests. Their logs are preserved. Subsequent controls passed +17, 47, 59 and 60 tests as receipt binding, worker validation and progress cases +were completed; `final-controls.log.gz` records all 62. These are implementation +controls, not model qualification campaigns or live installation evidence. + +Run the five Jest files named in the final log. Source workflow validation, +typecheck, lint, generated-bundle and specification/docs checks accompany the +PR's delivery SHA. The owning SDD and catalog register this adapter and its +fixed acknowledgment bounds. There is no model investigation deadline or +new user configuration. Consumer installation templates are unchanged. + +No model or real GitHub call occurs in these controls. A completed handoff +receipt is unavailable on failure; existing publication controls preserve the +last verified card and Check fallback. Production verification stays disabled. +The corresponding workflow still needs explicit reviewed installation on the +trusted default branch under #977. A develop PR cannot establish installation. diff --git a/quality/bugbot/evidence/issue-976/controls-2.log.gz b/quality/bugbot/evidence/issue-976/controls-2.log.gz new file mode 100644 index 0000000000000000000000000000000000000000..61c471d8a7b4caf93d220743f447acb79dd8c23e GIT binary patch literal 450 zcmV;z0X_a7iwFP!00002|IJj*Zqq;zzWWt=`EDsv**HzrsF65uK|ncHL=&p z3VS!&854yFUV$U;$CEG{JE50Cg+O91p7~}pf1hXHyng!*9LurubtIP9mC{8!UOU-z zt8r0?W#OV2fMV6HS3wFEl8%d#VpUX*wa1O*&qt5|h@(7-9m{QgpeP<=5A9BxF>P&*VIV?zz1i+$S5&X(_W|6g(wm@VeFmQt$YTSD+nff6-E9V%k5IsUbFv zV|gFqhq?*7`_A*_0mhi!fLxvH;kb)_S@?~7QAyGNg29_d4BkGMLH(WnKLT%lNnow% zE_)WWdxZK(<(Zg5L4JY5AMw*ubYv(yp5G#!AI~uvPVh=K(UF^@TOGaR1GwJ$-(>7WZvlzd`Lj_BD&iymWxG!gzUce8ur`oV8idEvQ&BE1Gkv@;!QZU%F0SN&TCpnd z)t7`LZlIo&VFaye5z+=##U^G7YluI^q&pwJ`0%Z<{^?{;0{90Mntox!bBJ?;G|Z~=Fw)0afdS7N~V IN<;$y0RExL?*IS* literal 0 HcmV?d00001 diff --git a/quality/bugbot/evidence/issue-976/controls-4.log.gz b/quality/bugbot/evidence/issue-976/controls-4.log.gz new file mode 100644 index 0000000000000000000000000000000000000000..932ca66b546e5c8ed731c7ae412d1df6b347ffef GIT binary patch literal 162 zcmV;T0A2qdiwFP!00002|3!^K4udcZ1n<1U52ylERgwCP!rd|aT5#2wmfT9NPIkY zLue1Mr_s*L)0`}Xj>U<^8$D5Cz>d4BBntcPRPAc4_k8&Et6T%ezzK6)Ll0ZaEp{u> zp?}%G5_VKkZeg(_J?;tJI|pL#N!kKj7f`j$?7~FIM!9g#49Uev$yQBH<}xU)TLD`C zJoHeeA&Q2u6llYF)T2Xcq`pOa!B@j^O5Pb37lSIWhGC%~HbI%VEcJUs-@{TFww}Jk k{Bid2@-H~WDqRKNSbyxhM_wDB8?f)b0sEIDUy%U-0O+!CGynhq literal 0 HcmV?d00001 diff --git a/quality/bugbot/evidence/issue-976/controls-6.log.gz b/quality/bugbot/evidence/issue-976/controls-6.log.gz new file mode 100644 index 0000000000000000000000000000000000000000..9b0cc307defa8fef08cb0d13be0c8d96565a11d7 GIT binary patch literal 237 zcmVIC zh>}X&+C8myX5Q?@fWwQ+70S zNPKSVugzYdI*}LToZzVvk4iBT>%sb7AZclRmk6A)=A4Y|Ef2YoN=*SM^*X2~_f230 zCeBcrQma~vIFahfS~GtBhP#IJ-MMi6C)_~34PEz(Mlm|8iEZ#Q!)Stm1e*w{!#I}f nLSD0CZsy$kH`m}8%4!REVZC{8jeI0-GqA4irstV}9|8aXxIb|C literal 0 HcmV?d00001 diff --git a/quality/bugbot/evidence/issue-976/controls-7.log.gz b/quality/bugbot/evidence/issue-976/controls-7.log.gz new file mode 100644 index 0000000000000000000000000000000000000000..d464a4b71c4993c7a764786044aeb0555a2c7e82 GIT binary patch literal 246 zcmVV}Wxaar_D|kdWEgLsb$5ebZID8tWY&e*7xez&T*zDXyXI+0pE_ z5}exS;|cfx6-6_Ov+(!?+`M4`^d4kcz}E#(Z8K3AfwNH*hKww^7%ADQkz^{p(t0Vd z*541^m$8qcSx^dVgV}0JoEB2ML|fs{zu_)1zYjiM{s}i>uyGiE(J&=X28Ci!g`Gim wtXN!PW#T;7?~S}?rP6P``!o086sx2PzOnAi+as@yj}6#1UoOF=Z!Q7=09V9!SO5S3 literal 0 HcmV?d00001 diff --git a/quality/bugbot/evidence/issue-976/final-controls.log.gz b/quality/bugbot/evidence/issue-976/final-controls.log.gz new file mode 100644 index 0000000000000000000000000000000000000000..466f30382a7fed4c080938f7fa573bc5a2b743e0 GIT binary patch literal 246 zcmV|cR*P*F6YI16vU%?AcR??L7Pd>a7OH6w)qI2%Qw&&ZOCk&>MnNXF7Bt>*%3 z{r%8g8M-K%1*Nbyn60+NX(qLEv=zSm4R?vpjfr*Rgi+kNXvyau`EjBtr5G)Q)n)n0pGuU#ZoPBOX5%&s$B^Qh^^1m z4_rLlO`FH-KBXF8JdB#TCK@-h(NBFmPC8zu64#B3F~kI@rRZ`o8{OQ@k&r=Y#0)^q zwkAtb@3Aoq*>4>N#_tDMIcr*WAVC=%!w1ojj}E4fGF}lQ6uhb>h!I`oqVraVEh##i zl7y%y9=cuh%fcVzi%N?A7Yt5*VQ~6X2K9IP{|KD?mcUBW zUG^+$_Xt%-<(Zg5L4JY5v-s&L>KMwp^IN3z<2kO6CwQfr=*V^FR!1+RBe?Kp%bU P[One dispatch POST] + P --> D{Run ID returned?} + D -->|yes| V[Validate exact worker and publishing step] + D -->|no| L[Read complete correlated run listings] + L -->|one exact match| V + L -->|none, foreign, ambiguous or unavailable| K[Keep verified card and Check fallback] + V -->|successful publication| R[Existing receipt may proceed] +``` + +In words, the direct ID remains the ordinary path. Missing-ID recovery reads the +workflow identity and the authenticated producer's creation time/attempt, then +searches that workflow on the same repository/default ref. A match requires the +full operation title, repository, workflow ID/path, event, branch, first worker +attempt and a creation time no earlier than the origin. Enumerate up to ten pages +of 100 unique runs and require a consistent complete count before adopting one +match. A truncated, repeated, inconsistent or ambiguous listing is unresolved. +Six lookup passes with 500 ms between passes cover eventual visibility and +transient reads; 401/403 or invalid identity fails closed. These fixed bounds +apply only to short acknowledgment reads. They impose no verifier research +cutoff, token budget, turn count or read quota. + +The adapter remembers a submitted operation within its producing attempt and +shares an in-flight acknowledgment between duplicate callers. A lost POST +response or a subsequent lookup failure never permits another POST for that +operation in that adapter. Retrying acknowledgment repeats reads. This is not a +durable exactly-once protocol across arbitrary process restarts. A native rerun +has a different producer attempt and therefore a different handoff identity; +ordinary worker source/attempt fences continue to apply. + +Even a directly returned ID MUST match the expected native title, repository, +workflow path/ref/event and first worker attempt before its one successful +publishing job/step can acknowledge completion. Failure leaves the last verified +comment and existing Check/Job Summary authoritative, performs no unqueued card +fallback and issues no completed publication receipt. Operator guidance is: +“Publication acknowledgment unavailable. Inspect the native writer run and retry +acknowledgment; the previous verified review remains available.” This adds no +locale-specific status-card text; diagnostics remain technical detail. + +Rollout requires reviewing the matching adapter and source workflow together and +then installing that exact workflow on trusted default under #977. An old writer +that lacks operation correlation cannot satisfy this recovery contract. A +prepared PR, develop merge or fake-provider control does not demonstrate an +installed writer. Keep production verification disabled throughout. + +The test budget is **13 new dispatch declarations / 18 controlled-provider +rows**, plus one worker declaration covering missing/forged correlation (two rows), plus the seven existing dispatch controls and existing publication/policy +integration suites. Distribution: unique accepted 204, absent match, ambiguity, +four foreign-identity dimensions, missing permission, transient workflow/origin/run-list read failures, +read-only retry after acceptance, foreign direct ID, later-page match, incomplete +listing, lost-response/concurrent retry, replaced origin attempt and distinct/replayed +progress sequences. Assert POST +counts, selected run, absent acknowledgment on failures, and the existing +preserved-card/no-receipt behavior through the actual publication workflow. +All endpoints are fake; no real secrets or GitHub writes occur. + +Acceptance requires that matrix, targeted publication integration, workflow, +typecheck, build/bundle, architecture, coverage, documentation and specification +checks at the delivered SHA. Register the adapter in the existing capability +catalog, regenerate its view, and update failure/retry guidance. Keep historical +pilot artifacts and all qualification gates unchanged. Primary API behavior is +specified by [GitHub workflow dispatch](https://docs.github.com/en/rest/actions/workflows#create-a-workflow-dispatch-event), +[workflow runs](https://docs.github.com/en/rest/actions/workflow-runs#list-workflow-runs-for-a-workflow) +and [native run names](https://docs.github.com/en/actions/reference/workflows-and-actions/workflow-syntax#run-name). diff --git a/specs/catalog.json b/specs/catalog.json index 328a4386d..5cb089f3a 100644 --- a/specs/catalog.json +++ b/specs/catalog.json @@ -727,7 +727,9 @@ "src/application/policies/bugbot_status_card_publication_policy.ts", "src/infrastructure/github/github_bugbot_status_card_workflow.ts", "src/infrastructure/github/github_bugbot_status_card_dispatch.ts", - "src/infrastructure/github/native_list_pagination.ts" + "src/infrastructure/github/native_list_pagination.ts", + "src/infrastructure/github/github_bugbot_status_card_acknowledgment.ts", + "src/infrastructure/github/github_bugbot_status_card_handoff.ts" ], "tests": [ "src/domain/bugbot/__tests__/review_state.test.ts", diff --git a/src/actions/github_action.ts b/src/actions/github_action.ts index 6158e4d7f..eb9982c84 100644 --- a/src/actions/github_action.ts +++ b/src/actions/github_action.ts @@ -59,7 +59,7 @@ export async function runGitHubAction(verificationAuthority?: BugbotVerification event: eventName, ref, actor, server: serverUrl, workflowRef: process.env.GITHUB_WORKFLOW_REF ?? '', locale: readGithubActionLocaleInputs(getGithubActionInput).pullRequest, policy: verificationAuthority?.policyDigest ?? 'default', - inputs: { pullNumber: payload.inputs?.pull_number, work: payload.inputs?.work }, + inputs: { pullNumber: payload.inputs?.pull_number, work: payload.inputs?.work, operation: payload.inputs?.operation }, }); if (effect === 'skipped') core.setFailed('No current Bugbot status card publication was authorized.'); else logInfo('Bugbot status card publication ' + effect + '.'); diff --git a/src/infrastructure/github/__tests__/github_bugbot_status_card_dispatch.test.ts b/src/infrastructure/github/__tests__/github_bugbot_status_card_dispatch.test.ts index d3d466f59..d7cba14c7 100644 --- a/src/infrastructure/github/__tests__/github_bugbot_status_card_dispatch.test.ts +++ b/src/infrastructure/github/__tests__/github_bugbot_status_card_dispatch.test.ts @@ -1,10 +1,11 @@ +import { createHash } from 'node:crypto'; import { createGithubBugbotStatusCardDispatcher } from '../github_bugbot_status_card_dispatch'; import { buildBugbotReviewProjection } from '../../../domain/bugbot/review_projection'; import { renderBugbotStatusCard } from '../../../application/policies/bugbot_review_presentation_policy'; const mockRead = { rest: { repos: { getContent: jest.fn() }, - actions: { getWorkflowRun: jest.fn(), listJobsForWorkflowRun: jest.fn() }, + actions: { getWorkflowRun: jest.fn(), listJobsForWorkflowRun: jest.fn(), getWorkflow: jest.fn(), listWorkflowRuns: jest.fn() }, } }; const mockWrite = { request: jest.fn() }; const mockCurrent = jest.fn(); @@ -39,9 +40,10 @@ beforeEach(() => { headRepositoryOwner: 'vypdev', headRepositoryName: 'copilot', state: 'open' }); mockWrite.request.mockResolvedValue({ data: { workflow_run_id: 42 } }); mockRead.rest.actions.getWorkflowRun.mockResolvedValue({ data: { - id: 42, repository: { id: 19 }, head_sha: applied.head_sha, head_branch: 'develop', + id: 42, repository: { id: 19 }, display_title: acceptedRun().display_title, run_attempt: 1, head_sha: applied.head_sha, head_branch: 'develop', event: 'workflow_dispatch', path: '.github/workflows/copilot_bugbot_status_card.yml', status: 'completed', conclusion: 'success', } }); + mockRead.rest.actions.getWorkflow.mockResolvedValue({ data: { id: 8, path: '.github/workflows/copilot_bugbot_status_card.yml' } }); mockRead.rest.actions.listJobsForWorkflowRun.mockResolvedValue({ data: { jobs: [applied], total_count: 1 } }); }); @@ -110,3 +112,152 @@ it('wakes progress with identifiers only and never waits for a comment from the expect(JSON.parse(payload.inputs.work)).not.toHaveProperty('verifiedBody'); expect(mockRead.rest.actions.getWorkflowRun).not.toHaveBeenCalled(); }); + +const createdAt = '2026-11-19T17:00:00Z'; +function acceptedRun(id = 42, override: Record = {}) { + const work = JSON.stringify({ version: 1, kind: 'analysis-result', runId: 21, attempt: 2, + source: { repositoryId: 19, pullNumber: 11, base: baseSha, baseRef: baseRefSha, head: headSha, policy: 'default' }, + verifiedBody: body }); + return { id, repository: { id: 19 }, workflow_id: 8, path: '.github/workflows/copilot_bugbot_status_card.yml', + event: 'workflow_dispatch', head_branch: 'develop', run_attempt: 1, created_at: createdAt, + display_title: 'Bugbot status · PR 11 · ' + createHash('sha256').update(work).digest('hex'), ...override }; +} +function acceptedWithoutId() { + mockWrite.request.mockResolvedValue({ status: 204, data: undefined }); + const effect = mockRead.rest.actions.getWorkflowRun.getMockImplementation(); + mockRead.rest.actions.getWorkflowRun.mockImplementation((input: { run_id: number }) => input.run_id === 21 + ? Promise.resolve({ data: { id: 21, repository: { id: 19 }, run_attempt: 2, created_at: createdAt } }) + : effect!(input)); + mockRead.rest.actions.listWorkflowRuns.mockResolvedValue({ data: { total_count: 1, workflow_runs: [acceptedRun()] } }); +} +async function failAcknowledgment(dispatcher: NonNullable>>) { + const result = expect(dispatcher.terminal.publish(request)).rejects.toThrow('retry acknowledgment'); + await jest.runAllTimersAsync(); + await result; + expect(mockWrite.request).toHaveBeenCalledTimes(1); + expect(mockRead.rest.actions.listJobsForWorkflowRun).not.toHaveBeenCalled(); +} + +afterEach(() => jest.useRealTimers()); + +it('recovers a unique accepted 204 run bound to the exact handoff operation', async () => { + acceptedWithoutId(); + const dispatcher = await createGithubBugbotStatusCardDispatcher(binding, environment); + await expect(dispatcher!.terminal.publish(request)).resolves.toBe('published'); + const sent = mockWrite.request.mock.calls[0][1].inputs; + expect(sent.operation).toBe(createHash('sha256').update(sent.work).digest('hex')); + expect(mockWrite.request).toHaveBeenCalledTimes(1); + expect(mockRead.rest.actions.getWorkflowRun).toHaveBeenLastCalledWith(expect.objectContaining({ run_id: 42 })); +}); + +it('fails safely when an accepted dispatch has no matching run', async () => { + jest.useFakeTimers(); acceptedWithoutId(); + mockRead.rest.actions.listWorkflowRuns.mockResolvedValue({ data: { total_count: 0, workflow_runs: [] } }); + const dispatcher = await createGithubBugbotStatusCardDispatcher(binding, environment); + await failAcknowledgment(dispatcher!); + expect(mockRead.rest.actions.listWorkflowRuns).toHaveBeenCalledTimes(6); +}); + +it('never adopts ambiguous accepted operations', async () => { + jest.useFakeTimers(); acceptedWithoutId(); + mockRead.rest.actions.listWorkflowRuns.mockResolvedValue({ data: { total_count: 2, workflow_runs: [acceptedRun(), acceptedRun(43)] } }); + const dispatcher = await createGithubBugbotStatusCardDispatcher(binding, environment); + await failAcknowledgment(dispatcher!); +}); + +it.each([ + ['workflow', { workflow_id: 9 }], ['ref', { head_branch: 'other' }], + ['repository', { repository: { id: 20 } }], ['operation', { display_title: 'Bugbot status · PR 11' }], +])('rejects a merely recent run with foreign %s', async (_name, override) => { + jest.useFakeTimers(); acceptedWithoutId(); + mockRead.rest.actions.listWorkflowRuns.mockResolvedValue({ data: { total_count: 1, workflow_runs: [acceptedRun(42, override)] } }); + const dispatcher = await createGithubBugbotStatusCardDispatcher(binding, environment); + await failAcknowledgment(dispatcher!); +}); + +it('retains accepted dispatch state when lookup permissions are unavailable', async () => { + jest.useFakeTimers(); acceptedWithoutId(); + mockRead.rest.actions.listWorkflowRuns.mockRejectedValue({ status: 403 }); + const dispatcher = await createGithubBugbotStatusCardDispatcher(binding, environment); + await failAcknowledgment(dispatcher!); + expect(mockRead.rest.actions.listWorkflowRuns).toHaveBeenCalledTimes(1); +}); + +it.each(['getWorkflow', 'getWorkflowRun', 'listWorkflowRuns'] as const)('retries a transient %s failure without repeating the accepted POST', async method => { + jest.useFakeTimers(); acceptedWithoutId(); + const port = mockRead.rest.actions[method]; + port.mockRejectedValueOnce({ status: 503 }); + const dispatcher = await createGithubBugbotStatusCardDispatcher(binding, environment); + const result = expect(dispatcher!.terminal.publish(request)).resolves.toBe('published'); + await jest.runAllTimersAsync(); await result; + expect(port.mock.calls.length).toBeGreaterThanOrEqual(2); + expect(mockWrite.request).toHaveBeenCalledTimes(1); +}); + +it('replays acknowledgment after accepted dispatch without another POST or a false receipt', async () => { + jest.useFakeTimers(); acceptedWithoutId(); + mockRead.rest.actions.listWorkflowRuns.mockRejectedValueOnce({ status: 403 }); + const dispatcher = await createGithubBugbotStatusCardDispatcher(binding, environment); + await failAcknowledgment(dispatcher!); + await expect(dispatcher!.terminal.publish(request)).resolves.toBe('published'); + expect(mockWrite.request).toHaveBeenCalledTimes(1); +}); + +it('rejects a direct ID for a different handoff even when workflow and ref match', async () => { + const dispatcher = await createGithubBugbotStatusCardDispatcher(binding, environment); + mockRead.rest.actions.getWorkflowRun.mockResolvedValue({ data: { ...acceptedRun(), display_title: 'foreign', status: 'completed', conclusion: 'success' } }); + await expect(dispatcher!.terminal.publish(request)).rejects.toThrow('retry acknowledgment'); + expect(mockRead.rest.actions.listJobsForWorkflowRun).not.toHaveBeenCalled(); +}); + +it('recovers the one matching operation on a later complete page', async () => { + acceptedWithoutId(); + const other = Array.from({ length: 100 }, (_, index) => acceptedRun(100 + index, { display_title: 'unrelated' })); + mockRead.rest.actions.listWorkflowRuns.mockImplementation(({ page }: { page: number }) => Promise.resolve({ data: { + total_count: 101, workflow_runs: page === 1 ? other : [acceptedRun()], + } })); + const dispatcher = await createGithubBugbotStatusCardDispatcher(binding, environment); + await expect(dispatcher!.terminal.publish(request)).resolves.toBe('published'); + expect(mockRead.rest.actions.listWorkflowRuns).toHaveBeenCalledTimes(2); +}); + +it('refuses a matching operation in an incomplete listing', async () => { + jest.useFakeTimers(); acceptedWithoutId(); + mockRead.rest.actions.listWorkflowRuns.mockResolvedValue({ data: { total_count: 2, workflow_runs: [acceptedRun()] } }); + const dispatcher = await createGithubBugbotStatusCardDispatcher(binding, environment); + await failAcknowledgment(dispatcher!); +}); + +it('does not redispatch after a lost POST response and concurrent acknowledgment retries', async () => { + acceptedWithoutId(); + mockWrite.request.mockRejectedValueOnce({ status: 502 }); + const dispatcher = await createGithubBugbotStatusCardDispatcher(binding, environment); + await expect(dispatcher!.terminal.publish(request)).rejects.toThrow('retry acknowledgment'); + await expect(Promise.all([dispatcher!.terminal.publish(request), dispatcher!.terminal.publish(request)])) + .resolves.toEqual(['published', 'published']); + expect(mockWrite.request).toHaveBeenCalledTimes(1); + expect(mockRead.rest.actions.listWorkflowRuns).toHaveBeenCalledTimes(1); +}); + +it('refuses recovery when the origin attempt is replaced', async () => { + jest.useFakeTimers(); acceptedWithoutId(); + mockRead.rest.actions.getWorkflowRun.mockResolvedValue({ data: { id: 21, repository: { id: 19 }, run_attempt: 3, created_at: createdAt } }); + const dispatcher = await createGithubBugbotStatusCardDispatcher(binding, environment); + await failAcknowledgment(dispatcher!); + expect(mockRead.rest.actions.listWorkflowRuns).not.toHaveBeenCalled(); +}); + +it('dispatches distinct progress sequences while acknowledging a replay without another wake', async () => { + const dispatcher = await createGithubBugbotStatusCardDispatcher(binding, environment); + const observation = { stage: 'analyzing' as const, pullNumber: 11, baseSha, baseRefSha, headSha, + observedAt: '2026-11-19T17:00:00Z', elapsedMs: 1, sequence: 2 }; + await dispatcher!.wake(observation); + await dispatcher!.wake({ ...observation, sequence: 3 }); + await dispatcher!.wake({ ...observation, sequence: 3 }); + expect(mockWrite.request).toHaveBeenCalledTimes(2); + const first = mockWrite.request.mock.calls[0][1].inputs; + const second = mockWrite.request.mock.calls[1][1].inputs; + expect(first.work).toBe(second.work); + expect(first.operation).not.toBe(second.operation); + expect(first.operation).toBe('2:' + createHash('sha256').update(first.work).digest('hex')); +}); diff --git a/src/infrastructure/github/__tests__/github_bugbot_status_card_workflow.test.ts b/src/infrastructure/github/__tests__/github_bugbot_status_card_workflow.test.ts index 3e8b13309..ce0a08702 100644 --- a/src/infrastructure/github/__tests__/github_bugbot_status_card_workflow.test.ts +++ b/src/infrastructure/github/__tests__/github_bugbot_status_card_workflow.test.ts @@ -1,5 +1,5 @@ import { runGithubBugbotStatusCardWorkflow } from '../github_bugbot_status_card_workflow'; -import { encodeGithubBugbotStatusCardHandoff } from '../github_bugbot_status_card_handoff'; +import { encodeGithubBugbotStatusCardHandoff, githubBugbotStatusCardOperation } from '../github_bugbot_status_card_handoff'; import { buildBugbotProgressCheckMarker } from '../../../application/policies/bugbot_progress_check_observation_policy'; import { buildBugbotReviewProjection } from '../../../domain/bugbot/review_projection'; import { renderBugbotStatusCard } from '../../../application/policies/bugbot_review_presentation_policy'; @@ -54,7 +54,7 @@ const progress = { source, runId: 23, attempt: 2, kind: 'progress' as const }; function invoke(request: Parameters[0] = progress, overrides: Partial = {}) { const wire = encodeGithubBugbotStatusCardHandoff(request)!; - return runGithubBugbotStatusCardWorkflow({ ...base, ...overrides, inputs: { pullNumber: wire.pull_number, work: wire.work } }); + return runGithubBugbotStatusCardWorkflow({ ...base, ...overrides, inputs: { pullNumber: wire.pull_number, work: wire.work, operation: githubBugbotStatusCardOperation(wire.work, request.kind === 'progress' ? 0 : undefined) } }); } beforeEach(() => { @@ -144,3 +144,12 @@ it('preserves active native progress when an independent authorized recheck refr expect(mockUpdate.mock.calls[0][2]).toContain('18/50 partitions validated'); expect(mockUpdate.mock.calls[0][2].endsWith(verifiedBody)).toBe(true); }); + +it.each([undefined, 'f'.repeat(64)])('refuses a missing or forged operation correlation before provider reads (%s)', async operation => { + const wire = encodeGithubBugbotStatusCardHandoff(progress)!; + await expect(runGithubBugbotStatusCardWorkflow({ ...base, inputs: { pullNumber: wire.pull_number, work: wire.work, operation } })) + .resolves.toBe('skipped'); + expect(mockApi.rest.repos.get).not.toHaveBeenCalled(); + expect(mockUpdate).not.toHaveBeenCalled(); + expect(mockAdd).not.toHaveBeenCalled(); +}); diff --git a/src/infrastructure/github/github_bugbot_status_card_acknowledgment.ts b/src/infrastructure/github/github_bugbot_status_card_acknowledgment.ts new file mode 100644 index 000000000..b706b4927 --- /dev/null +++ b/src/infrastructure/github/github_bugbot_status_card_acknowledgment.ts @@ -0,0 +1,89 @@ +import type { getOctokit } from '@actions/github'; +import { githubBugbotStatusCardOperation } from './github_bugbot_status_card_handoff'; +import { parsePositiveSafeInteger } from '../../domain/positive_integer_policy'; +import { bugbotStatusCardPath, bugbotStatusCardWorkflow, statusCardGithubErrorStatus } from './github_bugbot_status_card_installation'; + +class StatusCardAcknowledgmentError extends Error {} + +export const statusCardUnacknowledged = () => new StatusCardAcknowledgmentError('The shared Bugbot status card writer did not acknowledge publication. Inspect its native run and retry acknowledgment; preserve the last verified card and Check fallback.'); + +/** Correlation is data provenance, never independent producer-code attestation. */ +export function createGithubBugbotStatusCardAcknowledgment(binding: { + read: ReturnType; write: ReturnType; + scope: { owner: string; repo: string }; ref: string; runId: number; attempt: number; repositoryId: number; +}) { + const { read, write, scope, ref, runId, attempt, repositoryId } = binding; + const workflow = { ...scope, workflow_id: bugbotStatusCardWorkflow }; + const unresolved = statusCardUnacknowledged; + const acknowledgments = new Map }>(); + return async (inputs: { pull_number: string; work: string }, sequence?: number): Promise<{ id: number; title: string }> => { + const operation = githubBugbotStatusCardOperation(inputs.work, sequence); + const title = 'Bugbot status · PR ' + inputs.pull_number + ' · ' + operation; + let acknowledgment = acknowledgments.get(operation); + if (!acknowledgment) { + acknowledgment = { issued: false }; + acknowledgments.set(operation, acknowledgment); + } + if (acknowledgment.id) return { id: acknowledgment.id, title }; + if (acknowledgment.pending) return { id: await acknowledgment.pending, title }; + const state = acknowledgment; + state.pending = (async (): Promise => { + if (!state.issued) { + // A lost response can still mean accepted: retry reads, never this POST. + state.issued = true; + const { data } = await write.request('POST /repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches', { + ...workflow, ref, inputs: { ...inputs, operation }, return_run_details: true, + }); + const direct = parsePositiveSafeInteger((data as { workflow_run_id?: number } | undefined)?.workflow_run_id); + if (direct) return state.id = direct; + } + for (let lookup = 0; lookup < 6; lookup += 1) { + try { + const expected = await read.rest.actions.getWorkflow(workflow); + const origin = await read.rest.actions.getWorkflowRun({ ...scope, run_id: runId }); + const created = origin.data.created_at; + if (!parsePositiveSafeInteger(expected.data.id) || expected.data.path !== bugbotStatusCardPath + || origin.data.id !== runId || origin.data.repository.id !== repositoryId || origin.data.run_attempt !== attempt + || typeof created !== 'string' || !Number.isFinite(Date.parse(created))) throw unresolved(); + const matches = new Set(); + const seen = new Set(); + let complete = false; + let total: number | undefined; + for (let page = 1; page <= 10; page += 1) { + const response: Awaited> = await read.rest.actions.listWorkflowRuns({ ...workflow, + branch: ref, event: 'workflow_dispatch', created: '>=' + created, per_page: 100, page }); + const data = response.data; + if (!Array.isArray(data.workflow_runs) || data.workflow_runs.length > 100 + || !Number.isSafeInteger(data.total_count) || data.total_count < 0 + || total !== undefined && total !== data.total_count) throw unresolved(); + total = data.total_count; + for (const run of data.workflow_runs) { + const id = parsePositiveSafeInteger(run.id); + if (!id || seen.has(id)) throw unresolved(); + seen.add(id); + if (run.repository.id === repositoryId && run.workflow_id === expected.data.id + && run.path === bugbotStatusCardPath && run.event === 'workflow_dispatch' + && run.head_branch === ref && run.display_title === title && run.run_attempt === 1 + && Number.isFinite(Date.parse(run.created_at)) && Date.parse(run.created_at) >= Date.parse(created)) matches.add(id); + } + if (seen.size === total || data.workflow_runs.length < 100) { + if (data.total_count !== seen.size) throw unresolved(); + complete = true; + break; + } + } + if (!complete || matches.size > 1) throw unresolved(); + if (matches.size === 1) return state.id = [...matches][0]; + } catch (error) { + if ([401, 403].includes(Number(statusCardGithubErrorStatus(error))) || error instanceof StatusCardAcknowledgmentError) throw unresolved(); + if (lookup === 5) throw unresolved(); + } + if (lookup < 5) await new Promise(resolve => setTimeout(resolve, 500)); + } + throw unresolved(); + })().catch(() => { throw unresolved(); }); + try { return { id: await state.pending, title }; } + catch { throw unresolved(); } + finally { state.pending = undefined; } + }; +} diff --git a/src/infrastructure/github/github_bugbot_status_card_dispatch.ts b/src/infrastructure/github/github_bugbot_status_card_dispatch.ts index 4f7b03fde..87ac63cfa 100644 --- a/src/infrastructure/github/github_bugbot_status_card_dispatch.ts +++ b/src/infrastructure/github/github_bugbot_status_card_dispatch.ts @@ -7,10 +7,12 @@ import { parsePositiveSafeInteger } from '../../domain/positive_integer_policy'; import { createPullRequestLifecycleClient } from '../composition/github_pull_request_client_factory'; import { encodeGithubBugbotStatusCardHandoff } from './github_bugbot_status_card_handoff'; import { - bugbotStatusCardPath, bugbotStatusCardWorkflow, readGithubBugbotStatusCardInstallation, statusCardGithubErrorStatus, + bugbotStatusCardPath, readGithubBugbotStatusCardInstallation, statusCardGithubErrorStatus, } from './github_bugbot_status_card_installation'; -const unresolved = () => new Error('The shared Bugbot status card writer did not acknowledge publication.'); +import { createGithubBugbotStatusCardAcknowledgment, statusCardUnacknowledged } from './github_bugbot_status_card_acknowledgment'; + +const unresolved = statusCardUnacknowledged; export async function createGithubBugbotStatusCardDispatcher(binding: { owner: string; repository: string; repositoryId: unknown; defaultBranch: unknown; @@ -26,20 +28,14 @@ export async function createGithubBugbotStatusCardDispatcher(binding: { const read = getOctokit(binding.evidenceToken); const write = getOctokit(binding.token); const scope = { owner: binding.owner, repo: binding.repository }; - const workflow = { ...scope, workflow_id: bugbotStatusCardWorkflow }; const installation = await readGithubBugbotStatusCardInstallation(read, scope, ref); if (installation === 'absent') return undefined; const pullRequests = new PullRequestLifecycleRepository(createPullRequestLifecycleClient()); - const dispatch = async (request: Parameters[0]): Promise => { + const acknowledge = createGithubBugbotStatusCardAcknowledgment({ read, write, scope, ref, runId, attempt, repositoryId }); + const dispatch = async (request: Parameters[0], sequence?: number) => { const inputs = encodeGithubBugbotStatusCardHandoff(request); if (installation !== 'installed' || !inputs) throw unresolved(); - const { data } = await write.request('POST /repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches', { - ...workflow, ref, inputs, return_run_details: true, - }); - const published = data as { workflow_run_id?: number } | undefined; - const operation = parsePositiveSafeInteger(published?.workflow_run_id); - if (!operation) throw unresolved(); - return operation; + return acknowledge(inputs, sequence); }; const terminal: BugbotStatusCardTerminalPort = { async publish(input) { @@ -57,7 +53,7 @@ export async function createGithubBugbotStatusCardDispatcher(binding: { // This deadline is only for acknowledging the short comment job. const until = Date.now() + 7 * 60_000; while (Date.now() < until) { - const observed = await read.rest.actions.getWorkflowRun({ ...scope, run_id: effect }).catch((error: unknown) => { + const observed = await read.rest.actions.getWorkflowRun({ ...scope, run_id: effect.id }).catch((error: unknown) => { if (statusCardGithubErrorStatus(error) === 404) return undefined; throw unresolved(); }); @@ -66,11 +62,11 @@ export async function createGithubBugbotStatusCardDispatcher(binding: { continue; } const { data } = observed; - if (data.id !== effect || data.repository.id !== repositoryId || data.path !== bugbotStatusCardPath + if (data.id !== effect.id || data.display_title !== effect.title || data.run_attempt !== 1 || data.repository.id !== repositoryId || data.path !== bugbotStatusCardPath || data.event !== 'workflow_dispatch' || data.head_branch !== ref) throw unresolved(); if (data.status === 'completed') { if (data.conclusion !== 'success') throw unresolved(); - const { data: jobs } = await read.rest.actions.listJobsForWorkflowRun({ ...scope, run_id: effect, per_page: 20, filter: 'latest' }); + const { data: jobs } = await read.rest.actions.listJobsForWorkflowRun({ ...scope, run_id: effect.id, per_page: 20, filter: 'latest' }); if (jobs.total_count === 1 && jobs.jobs.length === 1 && jobs.jobs[0].name === 'Publish Bugbot status card' && jobs.jobs[0].head_sha === data.head_sha && jobs.jobs[0].conclusion === 'success' && jobs.jobs[0].steps?.some((step: { name: string; conclusion?: string | null }) => @@ -87,7 +83,7 @@ export async function createGithubBugbotStatusCardDispatcher(binding: { async wake(observation: BugbotReviewProgressObservation) { if (installation !== 'installed' || !observation.baseSha) return; await dispatch({ kind: 'progress', runId, attempt, source: { repositoryId, pullNumber: observation.pullNumber, - base: observation.baseSha, baseRef: observation.baseRefSha ?? observation.baseSha, head: observation.headSha, policy: binding.policy } }); + base: observation.baseSha, baseRef: observation.baseRefSha ?? observation.baseSha, head: observation.headSha, policy: binding.policy } }, observation.sequence); }, }; } diff --git a/src/infrastructure/github/github_bugbot_status_card_handoff.ts b/src/infrastructure/github/github_bugbot_status_card_handoff.ts index bc3e1edb1..20b295b83 100644 --- a/src/infrastructure/github/github_bugbot_status_card_handoff.ts +++ b/src/infrastructure/github/github_bugbot_status_card_handoff.ts @@ -1,3 +1,4 @@ +import { createHash } from 'node:crypto'; import { isBugbotStatusCardSource, readBugbotStatusCardHead, type BugbotStatusCardWakeup, } from '../../application/policies/bugbot_status_card_publication_policy'; @@ -48,3 +49,9 @@ export function encodeGithubBugbotStatusCardHandoff(request: CardRequest): { pul const work = JSON.stringify({ version: 1, ...request }); return decodeGithubBugbotStatusCardHandoff({ pullNumber, work }) ? { pull_number: String(pullNumber), work } : undefined; } + +/** A correlation key binds bytes; the worker still validates publication authority. */ +export function githubBugbotStatusCardOperation(work: string, sequence?: number): string { + if (sequence !== undefined && (!Number.isSafeInteger(sequence) || sequence < 0)) throw new Error('Invalid progress correlation sequence.'); + return (sequence === undefined ? '' : String(sequence) + ':') + createHash('sha256').update(work).digest('hex'); +} diff --git a/src/infrastructure/github/github_bugbot_status_card_workflow.ts b/src/infrastructure/github/github_bugbot_status_card_workflow.ts index 2a659eafc..5ad296819 100644 --- a/src/infrastructure/github/github_bugbot_status_card_workflow.ts +++ b/src/infrastructure/github/github_bugbot_status_card_workflow.ts @@ -5,7 +5,7 @@ import { canonicalGitObjectId } from '../../domain/git_object_id'; import { PullRequestLifecycleRepository } from '../../data/repository/pull_request/pull_request_lifecycle_repository'; import { createPullRequestLifecycleClient } from '../composition/github_pull_request_client_factory'; import { createBugbotCompositionRoot } from '../composition/bugbot_composition_root'; -import { decodeGithubBugbotStatusCardHandoff } from './github_bugbot_status_card_handoff'; +import { decodeGithubBugbotStatusCardHandoff, githubBugbotStatusCardOperation } from './github_bugbot_status_card_handoff'; import { runAdmittedGithubBugbotStatusCardJob } from './github_bugbot_status_card_lane'; import { nativeBugbotAnalysisPath, readNativeBugbotStatusCardReview, type NativeGithubRun, type NativeGithubCheck } from './github_bugbot_status_card_observation'; import { bugbotStatusCardPath } from './github_bugbot_status_card_installation'; @@ -21,12 +21,18 @@ const sourceEvents = new Map([ export async function runGithubBugbotStatusCardWorkflow(binding: { owner: string; repository: string; event: string; ref: string; workflowRef: string; actor: string; token: string; evidenceToken: string; server: string; - locale: string; policy: string; inputs: { pullNumber: unknown; work: unknown }; + locale: string; policy: string; inputs: { pullNumber: unknown; work: unknown; operation: unknown }; }) { const request = decodeGithubBugbotStatusCardHandoff(binding.inputs); + const correlation = typeof binding.inputs.operation === 'string' + ? binding.inputs.operation.match(/^(0|[1-9]\d*):[a-f0-9]{64}$/u)?.[1] : undefined; + const sequence = correlation === undefined ? undefined : Number(correlation); const repository = binding.owner + '/' + binding.repository; if (binding.event !== 'workflow_dispatch' || binding.workflowRef !== repository + '/' + bugbotStatusCardPath + '@' + binding.ref - || !/^https:\/\/[a-z0-9.-]+(?::\d+)?$/iu.test(binding.server) || !request) return 'skipped'; + || !/^https:\/\/[a-z0-9.-]+(?::\d+)?$/iu.test(binding.server) || !request + || request.kind === 'progress' && (sequence === undefined || !Number.isSafeInteger(sequence)) + || typeof binding.inputs.work !== 'string' + || binding.inputs.operation !== githubBugbotStatusCardOperation(binding.inputs.work, request.kind === 'progress' ? sequence : undefined)) return 'skipped'; const api = getOctokit(binding.evidenceToken); const scope = { owner: binding.owner, repo: binding.repository }; const [authenticated, repo] = await Promise.all([