diff --git a/.github/workflows/pineforge-release.yml b/.github/workflows/pineforge-release.yml index a15302b..5b8eb12 100644 --- a/.github/workflows/pineforge-release.yml +++ b/.github/workflows/pineforge-release.yml @@ -131,7 +131,9 @@ jobs: if: always() with: name: release-stage-${{ github.run_attempt }}-10-adapter - path: ${{ runner.temp }}/release-receipt.json + path: | + ${{ runner.temp }}/release-receipt.json + ${{ runner.temp }}/producer-outcome.json if-no-files-found: warn report: @@ -145,6 +147,7 @@ jobs: issues: write with: hub: true + orchestration: ${{ toJSON(needs) }} failed: ${{ needs.reconcile.result != 'success' }} stage: reconcile secrets: inherit diff --git a/.github/workflows/release-report.yml b/.github/workflows/release-report.yml index 023f773..a5db48b 100644 --- a/.github/workflows/release-report.yml +++ b/.github/workflows/release-report.yml @@ -9,6 +9,9 @@ on: stage: type: string required: true + orchestration: + type: string + required: true hub: type: boolean default: false @@ -21,6 +24,10 @@ permissions: jobs: report: runs-on: ubuntu-24.04 + env: + # Supplied by the caller's real needs context; the runner retains the + # original triggering event in GITHUB_EVENT_PATH for this reusable call. + RELEASE_NEEDS: ${{ inputs.orchestration }} steps: - uses: actions/checkout@v7 with: @@ -29,6 +36,10 @@ jobs: - uses: actions/setup-python@v6 with: python-version: '3.13' + - name: Capture the original workflow event and actual needs before transport + env: + PYTHONPATH: scripts + run: python -m release_chain.reporting context - uses: actions/download-artifact@v6 with: pattern: release-stage-${{ github.run_attempt }}-* @@ -42,6 +53,14 @@ jobs: STAGE: ${{ inputs.stage }} RELEASE_ALERT_OWNER: ${{ vars.RELEASE_ALERT_OWNER }} run: python -m release_chain.reporting collect + - name: Retain private triggering event and orchestration observations + if: always() + uses: actions/upload-artifact@v5 + with: + name: release-run-context-${{ github.run_attempt }} + path: run-context.json + if-no-files-found: error + retention-days: 90 - name: Upload the single hub receipt snapshot id: upload if: inputs.hub && steps.receipt.outputs.has_event == 'true' @@ -90,7 +109,7 @@ jobs: env: PYTHONPATH: scripts GH_TOKEN: ${{ github.token }} - FAILED: 'true' - STAGE: receipt-transport + FAILED: ${{ inputs.failed }} + STAGE: ${{ inputs.failed && inputs.stage || 'receipt-transport' }} RELEASE_ALERT_OWNER: ${{ vars.RELEASE_ALERT_OWNER }} - run: python -m release_chain.reporting collect --receipt incident-release-receipt.json + run: python -m release_chain.reporting incident --receipt incident-release-receipt.json diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 2f766b0..0895a0d 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -249,7 +249,9 @@ jobs: if: always() with: name: release-stage-${{ github.run_attempt }}-10-manual - path: ${{ runner.temp }}/release-receipt.json + path: | + ${{ runner.temp }}/release-receipt.json + ${{ runner.temp }}/producer-outcome.json if-no-files-found: warn prepare: @@ -303,7 +305,9 @@ jobs: if: always() with: name: release-stage-${{ github.run_attempt }}-20-prepare - path: ${{ runner.temp }}/release-receipt.json + path: | + ${{ runner.temp }}/release-receipt.json + ${{ runner.temp }}/producer-outcome.json if-no-files-found: warn build: @@ -357,7 +361,6 @@ jobs: name: validated-release-bundle-${{ github.run_attempt }} path: bundle/ - uses: actions/download-artifact@v6 - if: needs.prepare.outputs.has_target == 'true' with: name: release-stage-${{ github.run_attempt }}-20-prepare path: report/ @@ -369,7 +372,9 @@ jobs: if: always() with: name: release-stage-${{ github.run_attempt }}-30-github - path: report/release-receipt.json + path: | + report/release-receipt.json + report/producer-outcome.json if-no-files-found: warn publish: @@ -397,7 +402,6 @@ jobs: name: validated-release-bundle-${{ github.run_attempt }} path: bundle/ - uses: actions/download-artifact@v6 - if: needs.prepare.outputs.has_target == 'true' with: name: release-stage-${{ github.run_attempt }}-30-github path: report/ @@ -413,7 +417,9 @@ jobs: if: always() with: name: release-stage-${{ github.run_attempt }}-40-publish - path: report/release-receipt.json + path: | + report/release-receipt.json + report/producer-outcome.json if-no-files-found: warn verify: @@ -446,7 +452,7 @@ jobs: name: validated-release-bundle-${{ github.run_attempt }} path: bundle/ - uses: actions/download-artifact@v6 - if: needs.authz.result == 'success' && needs.prepare.outputs.has_target == 'true' + if: needs.authz.result == 'success' with: name: release-stage-${{ github.run_attempt }}-30-github path: report/ @@ -459,19 +465,22 @@ jobs: if: always() with: name: release-stage-${{ github.run_attempt }}-50-verify - path: report/release-receipt.json + path: | + report/release-receipt.json + report/producer-outcome.json if-no-files-found: warn report: if: >- - !cancelled() && needs.authz.result == 'success' && !inputs.claim_placeholder - needs: [authz, manual-pr, prepare, build, github-release, publish, verify] + !cancelled() && needs.authz.result == 'success' + needs: [authz, claim-name, manual-pr, prepare, build, github-release, publish, verify] uses: ./.github/workflows/release-report.yml permissions: contents: read actions: read issues: write with: + orchestration: ${{ toJSON(needs) }} failed: ${{ contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled') }} - stage: ${{ needs.publish.result == 'failure' && 'pypi-upload' || needs.verify.result == 'failure' && 'verify-publication' || needs.github-release.result == 'failure' && 'github-release' || needs.build.result == 'failure' && 'distribution-transfer' || needs.prepare.result == 'failure' && 'prepare-publication' || needs.manual-pr.result == 'failure' && 'prepare-pr' || 'publication' }} + stage: ${{ needs.publish.result == 'failure' && 'pypi-upload' || needs.verify.result == 'failure' && 'verify-publication' || needs.github-release.result == 'failure' && 'github-release' || needs.build.result == 'failure' && 'distribution-transfer' || needs.prepare.result == 'failure' && 'prepare-publication' || needs.manual-pr.result == 'failure' && 'prepare-pr' || needs.claim-name.result == 'failure' && 'claim-placeholder' || 'publication' }} secrets: inherit diff --git a/docs/release-receipt-report.md b/docs/release-receipt-report.md index 222f7ae..c91fb03 100644 --- a/docs/release-receipt-report.md +++ b/docs/release-receipt-report.md @@ -1,9 +1,96 @@ # Data consumer receipt v1 delivery report -Implemented in `/Users/haoliangwen/code/pineforge-data-wt-rc-data` on -`rc/data-adapter`. HEAD remains `63c66863272f4b362c1efa503d50dda0e34ced3c`. -All changes remain in the working tree. No commits, pushes, file deletions, -release publications or callbacks were performed locally. +The public receipt and callback formats below describe the delivery contract. +The historical integration record applies only to its recorded source revision; +it does not establish validation of subsequent changes. + +Current stage artifacts may carry private `producer-outcome.json` alongside +the unchanged receipt contract. Actual producer decisions distinguish a +targetless no-op, a stored verified replay and current targeted output. +The reporter binds the witness to the API-observed run/attempt/head, current +artifact inventory and actual producer job, then consumes its decision inputs. +Missing or invalid evidence never grants no-op success. Stored replay is +compared with the immutable asset, installed lock and served package proof; +its original identities remain unchanged and it is not re-emitted. + +The public `release-receipt-data` archive still contains exactly one +`release-receipt.json`. The internal witness never enters the immutable public +completion asset or callback. The reporting workflow separately retains private +`run-context.json` as `release-run-context-`, capturing the runner's +original triggering event and the caller's actual `needs` data before transport. +It includes the original upstream run/attempt/event or Release dispatch inputs, +bound to the repository and current run/attempt/head. + +Both the same-run collector and the completed-run adapter check this context +against independent current run, job and artifact observations; a workflow-run +source is re-read at its recorded run ID and attempt. A later running attempt +does not invalidate that completed original trigger. Missing or conflicting +specific-attempt data still refuses. Manual **CI** and **Native feed** dispatches on +main and successful placeholder claims can therefore finish silently when all +release producers were skipped. Empty inventory alone cannot grant this outcome. +Executed producers still require their evidence. Missing, stale or conflicting +context and incomplete inventories refuse collection. + +One narrow case can be quiet without a context that had no chance to be written: +the provider independently proves that the exact main-branch Release attempt +completed with conclusion `cancelled`, and its complete attempt-job response +explicitly declares `total_count: 0` and `jobs: []`. Repository, workflow, +source and attempt must match, and no stage, context, bundle or receipt evidence +may exist. Downloaded stage evidence also refuses this outcome. The adapter +records `publisher-cancelled-before-execution` in the existing internal witness, +bound to publisher ID, attempt and head, and rechecks the provider facts when +collecting it. + +The adapter's own authenticated context remains required. +For a `workflow_run` report, the witness tuple must first match the authenticated +original event. The collector then reads that event's exact independent API +attempt and complete job inventory, without reading the latest publisher run. +Advancing only latest to a running or successful attempt therefore preserves +the original cancelled-attempt outcome. Missing or conflicting historical facts +still refuse. A manual `workflow_dispatch` request's original `publication_run` +continues to select the latest publisher; an older cancelled witness cannot +override that current target. Publisher evidence from any attempt still refuses +the quiet cancellation outcome. Empty lists without complete metadata, any +executed or refused authz job, or another publisher conclusion cannot authorize +this outcome. +This proves no execution; it does not establish why cancellation happened. +No public receipt or callback is produced for that publisher. + +The context is an ordinary run artifact with the repository's artifact access +and 90-day retention. It contains the full original event payload; "private" +here means outside the public receipt contract, not a separate access boundary. + +Same-run completion comes from actual workflow `needs`; lagging REST status does +not withhold a valid producer snapshot. The API still supplies independent job +names and run/attempt/source bindings, and conflicting observations refuse. +Incident fallback is diagnostic only and preserves upstream failure stages. +Delivery failures use the collected event/PR when available, while API or +collection failures reach a run-scoped incident. It never writes or dispatches +a diagnostic receipt, and incident-delivery errors remain visible failures. +Collection has one strict path for production and library callers; there is no +permissive missing-snapshot default. Current stages must agree on the decision. +Package stages bind the carried decision to the actual bundle's CI run, +attempt, source and version. Repair handoffs check the completed publisher's +current verification artifact and independently observed PR version/source. + +The control families exercise actual `prepare`, `manual`, `reconcile`, +`github-release`, `preflight`, `verify`, `publication`, `collect` and `incident` +commands using their existing arguments. Stored replay controls retain the +original run 300 receipt while varying the current adapter envelope. The +recovery-handoff control executes real main and pre-merge gate parsing over +fixture transports. Existing full-main-proof controls stop at an explicit +candidate boundary; the synthetic completion fixture replaces later proof +boundaries. Its handoff controls test orchestration only. Historically, 14 rows +were named as full publication checks over those fixed boundaries; those rows +are invalid evidence of full-gate verification. Their scope is now named +explicitly, without promoting the historical results. Neither fixture execution +nor a skipped live-PyPI control establishes provider or native acceptance. + +## Historical integration record + +The original integration recorded source revision +`63c66863272f4b362c1efa503d50dda0e34ced3c`. The following inventory and static +check transcript belong to that historical integration. The integration review's sections 1 and 3, the entire hub **Consumer receipt contract v1** section, its reference builder and `release_receipts.py`'s @@ -14,7 +101,7 @@ spelling follows the hub's actual code: Dockerfile line 74 consumes `RELEASE_VERSION`, `publish.yml` passes the v-prefixed Git tag, and `release_smoke.py` explicitly expects `v` + the bare release version. -## Files changed +## Historical file inventory - `.github/workflows/pineforge-release.yml` - `.github/workflows/release-report.yml` @@ -77,9 +164,9 @@ those issues and Actions logs. send the four-key signal. Upload failure prevents dispatch. A transport error creates a local incident without overwriting an uploaded completion snapshot. -All intermediate `release-stage--` snapshots also serialize -exactly this same contract; none use a home-grown receipt/report shape. Only -the literal coordinator artifact is selected by the hub policy. +Intermediate receipt snapshots serialize the same public contract. Stages also +carry a separate private producer witness for targetless decisions and stored +replay. Only the literal coordinator artifact is selected by the hub policy. For `verified`, both CI/E2E reference the complete successful pre-merge PR CI attempt, including release/ready. Main CI independently authorizes the exact @@ -299,11 +386,13 @@ in verify. Only publish can skip, after proving matching bytes already exist. ## Validation and remaining rollout requirement -`tests/test_consumer_receipt.py` builds the sample through the actual completed -publication coordinator, validates it with the hub's own validate_receipt / +`tests/test_consumer_receipt.py` builds the sample through a synthetic completed +publication coordinator fixture, validates it with the hub's own validate_receipt / check_run / identity logic, checks the artifact archive and exact signal with the hub checks, rejects schema/gate/source drift, and covers immutable-source repair, approved publication replay skips and verified-state non-regression. +The fixture replaces candidate, main, local-lock, OCI, served and previous +validators with fixed answers; those checks are outside its demonstrated scope. The sibling checkout cannot be imported in standalone consumer CI, so the minimal validator/check/helper snapshot is vendored in `tests/hub_receipt_validator.py`, not used by production. Its field-check @@ -315,9 +404,9 @@ including their formatting, rather than rewritten to this repo's lint style. Upstream `scripts/release_receipts.py` SHA-256: `6fe7b0afcb7a34de9e28bce9d8372e5a4eaa6695398536697f59ed705b0cc23a`. -Static checks only were run locally, per the owner rule. No pytest, Docker, -database, distribution exercise, publication or live callback was run. Both the -new hub-validator test and all existing suites remain CI work. +Only static checks were recorded for that historical integration. The transcript +contains no pytest, Docker, database, distribution exercise, publication or live +callback evidence, and must not be used as validation of the current source. ```text $ ruff check . diff --git a/docs/releasing.md b/docs/releasing.md index 2937d2f..cd9147d 100644 --- a/docs/releasing.md +++ b/docs/releasing.md @@ -76,10 +76,6 @@ expected head. No admin merge is used. Strict up-to-date required checks and an App without branch bypass are essential to close the final base-movement race. Merge queue is not enabled by this implementation. -All tests run in GitHub CI or cloud runners. On the maintainer's Mac run only -static checks, for example Ruff, mypy, Python compilation, actionlint and -`git diff --check`. - ## Publication, completion and repair Successful PR CI wakes the reconciler through `workflow_run`. The App's merge @@ -104,12 +100,95 @@ The only receipt format is the hub's **Consumer receipt contract v1**, runs checked-out `main` tooling. Its `report` job calls `release-report.yml`'s `report` job, which uploads exactly **`release-receipt-data`** containing one root file **`release-receipt.json`**. Sorted, compact JSON with a final LF is used. -Reruns replace that run's one live artifact and advance `sequence` using the +Full producer reruns replace that run's one live artifact and advance `sequence` using the Actions run attempt. `attempt` is the reporting Actions run ID. Stage artifacts -named `release-stage--` use the same receipt contract; they -are private inputs, never separate hub-facing formats. Target-less operations -produce no receipt; ordinary manual package publication still uses the existing -landed CI evidence and gates, without requiring a product receipt artifact. CI/PR-head code never builds the hub artifact. +named `release-stage--` contain a receipt for a current target +or an internal `producer-outcome.json` witness for an actual targetless decision +or stored replay. The witness is private and never enters the hub artifact, +retained receipt or callback. Targetless operations produce no public receipt. +Ordinary manual package publication still uses the existing landed CI evidence +and gates. CI/PR-head code never builds the hub artifact. + +The producer records the actual decision and its inputs, bound to its Actions +run, attempt and head. The reporter independently reads the current run, complete +artifact inventory and producer jobs. Ordinary main pushes, pending main proofs, +package bumps and repair handoffs can therefore finish quietly with a valid +current witness. Missing files alone never authorize that result. Missing, +expired, unregistered, invalid or wrong-attempt evidence fails collection. +Re-running only the report job cannot borrow an earlier attempt's decision. +Every downloaded current stage must agree on the target and producer decision; +a later targetless stage cannot erase an earlier target. Package publication +carries prepare's witness through GitHub Release, preflight and verification, +checking its CI run/attempt, source and version against the bundle at each step. +An absent targeted receipt cannot select the package-only path. + +The reporting workflow also retains private `run-context.json` in the separate +`release-run-context-` artifact. Before downloading stage artifacts it +captures the runner's original event, including the upstream run and attempt for +`workflow_run` or the original Release dispatch inputs, and the caller's actual +`needs` observations. Consumers bind those facts to the repository, run, attempt +and head, re-read the upstream run's exact recorded attempt, and check the +complete current job and stage inventories. A newer running attempt does not +invalidate the original completed trigger; missing or conflicting exact-attempt +data still fails. This context stays outside the public receipt, immutable +completion asset and four-key callback. It is an ordinary repository run artifact +holding the full event payload for 90 days, with the repository's artifact access. + +A manual dispatch of **CI** or **Native feed** on main can complete a Release +run with every release producer skipped. A successful `claim_placeholder` run +also has no release producer. These runs finish quietly only with that complete +context and matching independent observations. They create no product receipt +or failed incident. An empty artifact list, a caller boolean, a missing context, +or an executed producer with missing evidence cannot establish this outcome. +Report-only retries cannot borrow context or stage evidence from another attempt; +use **Re-run all jobs** when evidence needs regeneration. + +The sole context-free empty-publisher exception requires independent provider +proof that the exact Release run/attempt on main completed as `cancelled` before +any job executed. The selected run and independent specific-attempt record must +match repository, workflow, main branch, head, run ID and attempt. The complete attempt-jobs response +must explicitly declare integer `total_count: 0` and `jobs: []`, with no stage, +context, bundle or receipt evidence and no downloaded stage. An executed job, +including a refused authz job, incomplete inventory metadata, or a noncancelled +publisher stays on the strict evidence paths. The adapter emits only an internal +`publisher-cancelled-before-execution` witness bound to that publisher ID, +attempt and head. + +Collection still requires the adapter's own authenticated +reporting context. For a `workflow_run` trigger, collection first matches the +witness's publisher ID, attempt and head to the authenticated original event, +then selects that event's exact independent API attempt and complete jobs. +A newer running or successful latest attempt cannot replace the original +cancelled attempt; this consumer path does not read the latest publisher run. +For manual `workflow_dispatch`, the original `publication_run` input still +selects the latest publisher, so an older cancelled witness cannot suppress +the current target. Publisher stage, context, bundle or receipt evidence, +including evidence from a later attempt, still refuses this quiet outcome. +It emits no public receipt or callback and makes no claim about the cause of +cancellation. + +For jobs in the reporting run, the workflow's actual `needs` results establish +completion. The attempt-jobs API independently supplies names and run/source +bindings, but its status can still be queued, running or null after completion. +Conflicting terminal conclusions, missing or unexpected jobs, and mismatched +attempts or sources refuse collection. Historical runs without this context +still require terminal API proof for their existing producer artifacts; they +cannot use an empty inventory alone to establish a quiet no-target result. + +The internal witness has `schema`, `run_id`, `run_attempt`, `head_sha`, `outcome`, +`reason`, `inputs` and `receipt_sha256`. Actual no-target decisions use a null +receipt digest; replay binds the unchanged original receipt's canonical bytes. +The decision comes from producer execution, with no caller override. The +reporter also checks the relevant PR, input run or completed publisher facts. +Pending proof inputs record the observed wait; a later counterpart completion +does not turn that earlier valid decision into an incident. + +Stored verified replay keeps the original receipt's `attempt`, `sequence`, +timestamp, event and all pins. The current adapter envelope is checked separately. +The reporter compares the snapshot with the immutable stored receipt, reads the +installed lock at the producer's recorded source revision and rechecks the served +package proof. Replay produces no new hub artifact, retained asset, callback or +incident. A stale targeted receipt without this replay proof still fails. CI and E2E both reference the complete successful **pre-merge** `ci.yml` run, including `release/ready`. The landed `ci-post.yml` run authorizes the exact @@ -183,9 +262,21 @@ successful artifact upload, a separate hub-only App token POSTs or publication run. The event type is fixed by the hub; no configurable legacy callback format remains. Upload failure prevents callback. Callback failures alert locally; hub polling can still collect the immutable artifact. Reporting -errors write a separate local incident snapshot without replacing a previously -uploaded completion snapshot. A previously verified target never emits a later +errors use the diagnostic-only `incident` command, which cannot write a receipt +or enable transport. Upstream failures retain their actual stage; delivery +failures use `receipt-transport`. A previously verified target never emits a later failed/superseded/progress receipt; local incidents still record those errors. +Delivery diagnostics keep the collected event and PR when the evidence supports +them. API or collection errors instead reach a run-scoped incident with the +collection error recorded in the Actions summary. Failure to deliver the incident +itself remains an error. + +Offline CLI controls cover manual upstream workflows, placeholder claims, +ordinary main pushes, pending proof pairs, package bumps/publication, repair +handoffs, stored replay, delayed job status and evidence refusals. GitHub, +PyPI, GHCR and git observations use fixtures. Such controls demonstrate only +the validators they actually execute; they do not establish hosted workflow, +native, provider, publication or callback acceptance. The required hub `RELEASE_RECEIPT_POLICY.data` entry is checked into [`receipt-policy.json`](https://github.com/pineforge-4pass/pineforge-data/blob/main/tests/fixtures/release_chain/receipt-policy.json). @@ -195,13 +286,15 @@ skips. The reporting workflow is `pineforge-release.yml`, artifact `release-receipt-data`, base `main`; deploy uses `release.yml` and the inventory above. A complete synthetic verified receipt is in [`sample-release-receipt.json`](https://github.com/pineforge-4pass/pineforge-data/blob/main/tests/fixtures/release_chain/sample-release-receipt.json). -`tests/test_consumer_receipt.py` builds it through the real coordinator builder -and validates it with the hub's field, GitHub-check, identity, artifact and -signal checks vendored in `tests/hub_receipt_validator.py`. The sibling hub repo -is unavailable in standalone CI, so the snapshot records its source hash. These -unit tests run in CI only, together with the existing suites. - -The 15-minute reconciler repairs dropped ready events and missing publication +`tests/test_consumer_receipt.py` builds it through the coordinator with fixed +candidate, main, local-lock, OCI, served and previous proof boundaries. That +fixture tests orchestration and the hub's field, GitHub-check, identity, artifact +and signal checks vendored in `tests/hub_receipt_validator.py`; it cannot prove +the replaced publication gates. The sibling hub repo is unavailable in +standalone CI, so the snapshot records its source hash. Controls that stop at a +candidate boundary likewise prove only the path up to that boundary. + +The event-driven or manually dispatched reconciler repairs dropped ready events and missing publication transitions, reuses verified receipts without republishing, and refuses to replace the tested dependency with a moving image alias. Actions write access is used only on this consumer to replay its existing `Release` workflow after @@ -214,6 +307,7 @@ publish an ordinary data change, leave `ci_run` empty and choose a semantic `bump`; the App opens a version PR under the same gates. Use **Re-run all jobs** for a CI or publication retry: evidence from earlier attempts cannot fill a missing job in the current attempt. +The periodic schedule remains disabled in `pineforge-release.yml`. ## Rollback diff --git a/scripts/release_chain/automation.py b/scripts/release_chain/automation.py index beb858a..d2a32d9 100644 --- a/scripts/release_chain/automation.py +++ b/scripts/release_chain/automation.py @@ -35,7 +35,16 @@ version_key, ) from .evidence import gate_for_run -from .receipts import FIELDS, candidate, canonical, receipt, timestamp +from .receipts import ( + FIELDS, + OUTCOME_FILE, + candidate, + canonical, + observe_outcome, + producer_outcome, + receipt, + timestamp, +) MARKER = re.compile(r"") @@ -91,6 +100,10 @@ def body(metadata: dict[str, Any]) -> str: def finish(report: dict[str, Any], path: Path) -> None: require(set(report) == FIELDS, "receipt fields differ from the hub contract") + outcome = producer_outcome(report) + if outcome is not None: + path.parent.mkdir(parents=True, exist_ok=True) + (path.parent / OUTCOME_FILE).write_bytes(canonical(outcome)) if report["event_id"] is None: return # No target means no receipt, including ordinary non-release main pushes. path.parent.mkdir(parents=True, exist_ok=True) @@ -441,7 +454,7 @@ def reconcile( from .publishing import recover_applied report.update(receipt(installed["event"], "reconcile-rollback")) - recover_applied(repo, source, report) + recover_applied(repo, source, report, installed_source=base) return report["state"] = "superseded" return @@ -454,7 +467,7 @@ def reconcile( report["state"] = "merged" from .publishing import recover_applied - recover_applied(repo, source, report) + recover_applied(repo, source, report, installed_source=base) return installed_version = runtime_pin(source).split(":")[-2].split("@", 1)[0] if version_key(event["release_version"]) <= version_key(installed_version): @@ -493,6 +506,7 @@ def reconcile( for pr in prs: if marker(pr)["kind"] == "package": maybe_merge(repo, pr, report, bot, None, None) + observe_outcome(report, "no-target", "no-ready-release", base=base) return same = [ pr @@ -526,3 +540,8 @@ def reconcile( # The retained PR comment is the human-facing supersession record. # One reporting run owns one target and one immutable hub receipt. maybe_merge(repo, pr, report, bot, event if not bump else None, manifest) + if bump and report["event_id"] is None: + observe_outcome( + report, "no-target", "package-bump", base=base, bump=bump, version=version, + pr=report["pr"]["number"], + ) diff --git a/scripts/release_chain/publishing.py b/scripts/release_chain/publishing.py index dea3400..f79a4da 100644 --- a/scripts/release_chain/publishing.py +++ b/scripts/release_chain/publishing.py @@ -16,6 +16,7 @@ from .automation import api, finish, git, inventory, marker, pr_identity, snapshot from .contract import ( + HEX40, LOCK, MANIFEST, RUNTIME, @@ -39,8 +40,17 @@ matching_main_runs, ) from .main_proofs import NATIVE_WORKFLOW +from .receipts import ( + OUTCOME_FILE, + OUTCOME_SCHEMA, + ProducerReceipt, + canonical, + observe_outcome, + receipt, + served, + timestamp, +) from .receipts import candidate as record_candidate -from .receipts import canonical, receipt, served, timestamp # TEMPORARY first-adopter exemption (TX-DATA-BASELINE2, L6 10-19): # remove the data exemption and the hub form together, only after 0.3.1 is verified AND @@ -284,7 +294,9 @@ def stored_receipt(repo: str, version: str) -> dict[str, Any] | None: ) -def recover_applied(repo: str, source: Path, report: dict[str, Any]) -> None: +def recover_applied( + repo: str, source: Path, report: dict[str, Any], *, installed_source: str | None = None +) -> None: """Replay completed receipts or repair a dropped publication transition.""" version = package_version(source) saved = stored_receipt(repo, version) @@ -301,7 +313,20 @@ def recover_applied(repo: str, source: Path, report: dict[str, Any]) -> None: image_digest == lock["event"]["image"]["digest"], "stored publication does not match installed defaults", ) + require( + saved["release_version"] == lock["event"]["release_version"] + and saved["event_id"] == lock["event"]["event_id"], + "stored publication does not match installed target", + ) + require( + isinstance(installed_source, str) and HEX40.fullmatch(installed_source), + "stored publication installed source missing", + ) report.update(saved) + observe_outcome( + report, "replay", "stored-verified", version=version, + installed_source=installed_source, installed_lock_sha256=sha256(canonical(lock)), + ) return prs = inventory(repo, "pulls?state=closed&base=main&sort=updated&direction=desc") candidates = [ @@ -369,6 +394,10 @@ def recover_applied(repo: str, source: Path, report: dict[str, Any]) -> None: }, ) report.update(receipt(None)) # The pinned reporting run owns the next snapshot. + observe_outcome( + report, "no-target", "publication-handoff", version=version, + publication_run=max(completed, key=lambda row: row["id"])["id"], + ) return # App token needs Actions:write only on this consumer for lost-event repair. api( @@ -450,6 +479,10 @@ def prepare( and marker(pr) ] if not matches: + observe_outcome( + report, "no-target", "ordinary-main", ci_run=run_id, + ci_attempt=run["run_attempt"], source=source, + ) return {"eligible": "false"} # Ordinary main pushes are not release requests. require(len(matches) == 1, "ambiguous merged release PR") pr = api(repo, f"pulls/{matches[0]['number']}") @@ -473,7 +506,11 @@ def prepare( "counterpart is not exact-head main CI", ) evidence = gate_for_run(repo, run_id, head=source) - except ProofPending: + except ProofPending as pending: + observe_outcome( + report, "no-target", "main-proofs-pending", ci_run=run_id, + ci_attempt=run["run_attempt"], source=source, pending=str(pending), + ) return {"eligible": "false", "waiting": "main-proofs-pending"} git("fetch", "origin", source) # Keep this process and scripts at the trusted default branch. Read source as data. @@ -533,6 +570,11 @@ def prepare( if published is not None: require(published == evidence["distributions"], "PyPI version exists with different bytes") report["state"] = "deploying" if published is None else "merged" + if event is None: + observe_outcome( + report, "no-target", "package-publication", ci_run=run_id, + ci_attempt=run["run_attempt"], source=source, version=version, + ) return { "eligible": "true", "published": str(published is not None).lower(), @@ -636,12 +678,41 @@ def main() -> None: args = parser.parse_args() repo = os.environ["GITHUB_REPOSITORY"] report = ( - object_json(args.receipt.read_bytes()) + ProducerReceipt(object_json(args.receipt.read_bytes())) if args.receipt.exists() else receipt(None, "publish") ) try: args.directory.mkdir(parents=True, exist_ok=True) + if args.command != "prepare" and report["event_id"] is None: + # Target-less package publication must carry prepare's actual decision. + # A missing targeted receipt can never select this path by absence. + observed = object_json((args.receipt.parent / OUTCOME_FILE).read_bytes()) + gate = object_json((args.directory / "evidence.json").read_bytes()) + require( + set(observed) == { + "schema", "run_id", "run_attempt", "head_sha", "outcome", + "reason", "inputs", "receipt_sha256", + } + and observed.get("schema") == OUTCOME_SCHEMA + and type(observed.get("run_id")) is int + and observed["run_id"] == int(os.environ["GITHUB_RUN_ID"]) + and type(observed.get("run_attempt")) is int + and observed["run_attempt"] == int(os.environ["GITHUB_RUN_ATTEMPT"]) + and observed.get("head_sha") == os.environ["GITHUB_SHA"] + and observed.get("outcome") == "no-target" + and observed.get("reason") == "package-publication" + and observed.get("receipt_sha256") is None + and observed.get("inputs") == { + "ci_run": gate["run_id"], "ci_attempt": gate["attempt"], + "source": gate["head"], "version": package_version(args.directory / "source"), + } + and verify_local(args.directory / "source") is None, + "missing or invalid package publication outcome", + ) + observe_outcome( + report, "no-target", "package-publication", **observed["inputs"] + ) if args.command == "prepare": outputs = prepare(Path.cwd(), repo, args.run, args.directory, report) with Path(os.environ["GITHUB_OUTPUT"]).open("a") as handle: diff --git a/scripts/release_chain/receipts.py b/scripts/release_chain/receipts.py index 0f50db5..5bc9a85 100644 --- a/scripts/release_chain/receipts.py +++ b/scripts/release_chain/receipts.py @@ -33,6 +33,36 @@ "deployed", "previous", } +OUTCOME_FILE = "producer-outcome.json" +OUTCOME_SCHEMA = "pineforge/internal-producer-outcome/v1" + + +class ProducerReceipt(dict[str, Any]): + """Private decision state; attributes never enter the public receipt JSON.""" + + outcome: dict[str, Any] | None = None + + +def observe_outcome( + report: dict[str, Any], outcome: str, reason: str, **inputs: Any +) -> None: + require(isinstance(report, ProducerReceipt), "producer decision needs a working receipt") + assert isinstance(report, ProducerReceipt) + report.outcome = {"outcome": outcome, "reason": reason, "inputs": inputs} + + +def producer_outcome(report: dict[str, Any]) -> dict[str, Any] | None: + observed = getattr(report, "outcome", None) + if observed is None: + return None + return { + "schema": OUTCOME_SCHEMA, + "run_id": int(os.environ["GITHUB_RUN_ID"]), + "run_attempt": int(os.environ["GITHUB_RUN_ATTEMPT"]), + "head_sha": os.environ["GITHUB_SHA"], + **observed, + "receipt_sha256": sha256(canonical(report)) if report["event_id"] else None, + } def canonical(value: object) -> bytes: @@ -48,9 +78,9 @@ def timestamp() -> str: return datetime.now(UTC).strftime("%Y-%m-%dT%H:%M:%SZ") -def receipt(event: dict[str, Any] | None, _stage: str = "") -> dict[str, Any]: +def receipt(event: dict[str, Any] | None, _stage: str = "") -> ProducerReceipt: # A target-less working object is never written or uploaded as a receipt. - return { + return ProducerReceipt({ "schema": "pineforge/consumer-receipt/v1", "consumer": "data", "event_id": event["event_id"] if event else None, @@ -60,7 +90,7 @@ def receipt(event: dict[str, Any] | None, _stage: str = "") -> dict[str, Any]: "state": "pending", "timestamp": timestamp(), **dict.fromkeys(("pr", "ci", "e2e", "deploy", "preview", "deployed", "previous")), - } + }) def checks(gate: dict[str, Any]) -> dict[str, Any]: diff --git a/scripts/release_chain/reporting.py b/scripts/release_chain/reporting.py index 77b3f8e..cc6a607 100644 --- a/scripts/release_chain/reporting.py +++ b/scripts/release_chain/reporting.py @@ -4,16 +4,19 @@ import argparse import copy +import io import os import re import subprocess import tempfile +import zipfile from pathlib import Path from typing import Any from .automation import api, finish, inventory, marker, pr_identity from .contract import ( HUB, + LOCK, gh_bytes, hub_manifest, json_bytes, @@ -22,27 +25,841 @@ sha256, verify_local, ) -from .evidence import artifact_json, gate_for_run, semantic_evidence_identity +from .evidence import gate_for_run, semantic_evidence_identity from .publishing import candidate_gate, stored_receipt, verified_package from .receipts import ( DEPLOY_JOBS, DEPLOY_SKIPS, FIELDS, + OUTCOME_FILE, + OUTCOME_SCHEMA, WORKFLOW, + ProducerReceipt, candidate, + canonical, + observe_outcome, receipt, signal, timestamp, ) +REPORT_STAGES = {"10-adapter", "10-manual", "20-prepare", "30-github", "40-publish", "50-verify"} +STAGE_JOBS = { + "10-adapter": "reconcile", "10-manual": "manual-pr", "20-prepare": "prepare", + "30-github": "github-release", "40-publish": "publish", "50-verify": "verify", +} +RUN_CONTEXT_FILE = "run-context.json" +RUN_CONTEXT_PREFIX = "release-run-context-" +RELEASE_NEEDS = {"authz", "claim-name", "manual-pr", *DEPLOY_JOBS} +ADAPTER_NEEDS = {"authz", "reconcile"} +NO_TARGET_STAGES = { + "ordinary-main": {"20-prepare"}, + "main-proofs-pending": {"20-prepare"}, + "package-publication": {"20-prepare", "30-github", "40-publish", "50-verify"}, + "package-bump": {"10-manual"}, + "no-ready-release": {"10-adapter"}, + "publication-handoff": {"10-adapter"}, + "publisher-handoff": {"10-adapter"}, + "reporter-handoff": {"10-adapter"}, + "publisher-no-target": {"10-adapter"}, + "publisher-no-producer": {"10-adapter"}, + "publisher-cancelled-before-execution": {"10-adapter"}, +} +NO_TARGET_INPUTS = { + "ordinary-main": {"ci_run", "ci_attempt", "source"}, + "main-proofs-pending": {"ci_run", "ci_attempt", "source", "pending"}, + "package-publication": {"ci_run", "ci_attempt", "source", "version"}, + "package-bump": {"base", "bump", "version", "pr"}, + "no-ready-release": {"base"}, + "publication-handoff": {"publication_run", "version"}, + "publisher-handoff": {"publication_run", "ci_run", "version", "source"}, + "reporter-handoff": {"publication_run", "version", "source"}, + "publisher-no-target": {"publication_run", "publication_attempt", "outcome_sha256"}, + "publisher-no-producer": {"publication_run", "publication_attempt", "context_sha256"}, + "publisher-cancelled-before-execution": {"publication_run", "publication_attempt", "source"}, +} -def collect(directory: Path, *, failed: bool, stage: str) -> dict[str, Any]: - paths = sorted(directory.glob("release-stage-*/release-receipt.json")) - report = object_json(paths[-1].read_bytes()) if paths else receipt(None, stage) - require(set(report) == FIELDS, "receipt fields differ from the hub contract") + +def current_run(repo: str) -> dict[str, Any]: + run_id, attempt = int(os.environ["GITHUB_RUN_ID"]), int(os.environ["GITHUB_RUN_ATTEMPT"]) + run = api(repo, f"actions/runs/{run_id}") + require( + isinstance(run, dict) and type(run.get("id")) is int and run["id"] == run_id > 0 + and type(run.get("run_attempt")) is int and run["run_attempt"] == attempt > 0 + and run.get("path") in { + ".github/workflows/release.yml", ".github/workflows/" + WORKFLOW, + } + and run.get("event") in { + "repository_dispatch", "workflow_run", "workflow_dispatch", "schedule", + } + and isinstance(run.get("head_sha"), str) and re.fullmatch(r"[0-9a-f]{40}", run["head_sha"]), + "release-producer-run-mismatch: untrusted current run/attempt", + ) + return run + + +def stage_rows(artifacts: list[Any], run: dict[str, Any]) -> list[dict[str, Any]]: + require( + all(isinstance(row, dict) and isinstance(row.get("name"), str) for row in artifacts), + "release-snapshot-invalid: malformed artifact inventory", + ) + prefix = f"release-stage-{run['run_attempt']}-" + rows = [row for row in artifacts if row["name"].startswith(prefix)] + require( + rows or not any(row["name"].startswith("release-stage-") for row in artifacts), + f"release-snapshot-attempt-mismatch: run {run['id']} attempt {run['run_attempt']}", + ) + allowed = REPORT_STAGES - {"10-adapter"} + if run["path"] == ".github/workflows/" + WORKFLOW: + allowed = {"10-adapter"} + require( + all(row["name"][len(prefix):] in allowed for row in rows) + and len({row["name"] for row in rows}) == len(rows), + "release-snapshot-invalid: unregistered or duplicate stage", + ) + require( + all(row["expired"] is False for row in rows), + "release-snapshot-missing: current stage expired", + ) + return sorted(rows, key=lambda row: row["name"]) + + +def checked_context( + repo: str, run: dict[str, Any], jobs: list[Any], context: dict[str, Any], +) -> dict[str, Any]: + """Check private workflow observations, never an expected product verdict.""" + expected = ADAPTER_NEEDS if run["path"].endswith("/" + WORKFLOW) else RELEASE_NEEDS + require( + set(context) == { + "repository", "run_id", "run_attempt", "head_sha", "event_name", "event", "needs", + } + and context["repository"] == repo + and type(context["run_id"]) is int and context["run_id"] == run["id"] + and type(context["run_attempt"]) is int and context["run_attempt"] == run["run_attempt"] + and isinstance(context["head_sha"], str) + and re.fullmatch(r"[0-9a-f]{40}", context["head_sha"]) + and context["head_sha"] == run["head_sha"] + and context["event_name"] == run["event"] + and isinstance(context["event"], dict) and isinstance(context["needs"], dict) + and (run.get("repository") or {}).get("full_name") == repo + and (run.get("head_repository") or {}).get("full_name") == repo, + "release-run-context-invalid: run binding differs", + ) + event, needs = context["event"], context["needs"] + require( + (event.get("repository") or {}).get("full_name") == repo and set(needs) == expected, + "release-run-context-invalid: repository or needs inventory differs", + ) + for _name, observation in needs.items(): + require( + isinstance(observation, dict) and set(observation) == {"result", "outputs"} + and isinstance(observation["result"], str) + and observation["result"] in {"success", "failure", "cancelled", "skipped"} + and isinstance(observation["outputs"], dict) + and all(isinstance(key, str) and isinstance(value, str) + for key, value in observation["outputs"].items()), + "release-run-context-invalid: malformed needs observation", + ) + require( + needs["authz"]["result"] == "success", + "release-run-context-invalid: authz did not pass", + ) + require( + all(isinstance(job, dict) and isinstance(job.get("name"), str) for job in jobs) + and len({job["name"] for job in jobs}) == len(jobs) + and {job["name"] for job in jobs} <= expected | {"report", "report / report"} + and expected <= {job["name"] for job in jobs}, + "release-producer-job-mismatch: current job names differ from needs", + ) + for job in jobs: + require( + "status" in job and "conclusion" in job + and type(job.get("run_id")) is int and job["run_id"] == run["id"] + and type(job.get("run_attempt")) is int and job["run_attempt"] == run["run_attempt"] + and job.get("head_sha") == run["head_sha"], + "release-producer-job-mismatch: job run/attempt/source differs", + ) + if job["name"] not in needs: + continue # The reporting job is still running. + result = needs[job["name"]]["result"] + status = job.get("status") + require( + (status == "completed" and job.get("conclusion") == result) + or ( + ( + status is None + or (isinstance(status, str) and status in {"queued", "in_progress"}) + ) + and job.get("conclusion") is None + ), + "release-producer-job-mismatch: API contradicts workflow needs", + ) + if run["event"] == "workflow_run": + source = event.get("workflow_run") + require( + event.get("action") == "completed" and isinstance(source, dict) + and type(source.get("id")) is int and source["id"] > 0 + and type(source.get("run_attempt")) is int and source["run_attempt"] > 0, + "release-run-context-invalid: original upstream identity missing", + ) + # A later re-run does not change the completed attempt that invoked us. + # This is an independent attempt record, never the event as its own proof. + upstream = api(repo, f"actions/runs/{source['id']}/attempts/{source['run_attempt']}") + require( + isinstance(upstream, dict) and type(upstream.get("id")) is int + and type(upstream.get("run_attempt")) is int, + "release-run-context-invalid: malformed checked upstream", + ) + fields = { + "id", "run_attempt", "path", "name", "event", "head_branch", "head_sha", + "status", "conclusion", + } + allowed = ( + {".github/workflows/ci.yml": "CI", ".github/workflows/release.yml": "Release"} + if expected == ADAPTER_NEEDS else { + ".github/workflows/ci-post.yml": "CI", + ".github/workflows/native-feed-post.yml": "Native feed", + } + ) + require( + all(field in source and source[field] == upstream.get(field) for field in fields) + and upstream.get("status") == "completed" + and isinstance(upstream.get("conclusion"), str) + and upstream["conclusion"] in { + "success", "failure", "cancelled", "skipped", "neutral", + "timed_out", "action_required", "stale", + } + and upstream.get("path") in allowed + and upstream.get("name") == allowed[upstream["path"]] + and isinstance(upstream.get("head_sha"), str) + and re.fullmatch(r"[0-9a-f]{40}", upstream["head_sha"]) + and (source.get("head_repository") or {}).get("full_name") == repo + and (upstream.get("head_repository") or {}).get("full_name") == repo + and (upstream.get("repository") or {}).get("full_name") == repo, + "release-run-context-invalid: checked upstream differs", + ) + require( + (expected == RELEASE_NEEDS and upstream["head_branch"] == "main") + or (expected == ADAPTER_NEEDS and ( + (upstream["path"] == ".github/workflows/ci.yml" + and upstream["event"] == "pull_request") + or (upstream["path"] == ".github/workflows/release.yml" + and upstream["head_branch"] == "main") + )), + "release-run-context-invalid: upstream cannot invoke this workflow", + ) + elif run["event"] == "workflow_dispatch" and expected == RELEASE_NEEDS: + inputs = event.get("inputs") + require( + isinstance(inputs, dict) + and set(inputs) == {"bump", "ci_run", "rollback_package", "claim_placeholder"} + and isinstance(inputs["bump"], str) and inputs["bump"] in {"patch", "minor", "major"} + and all(inputs[name] is None or isinstance(inputs[name], str) + for name in ("ci_run", "rollback_package")) + and isinstance(inputs["claim_placeholder"], str) + and inputs["claim_placeholder"] in {"true", "false"}, + "release-run-context-invalid: original Release inputs missing", + ) + return context + + +def workflow_context(repo: str, directory: Path) -> dict[str, Any] | None: + # Only the trusted workflow call supplies needs. The event is the runner's + # original event file, not a flag or a product-generated expected decision. + if "RELEASE_NEEDS" not in os.environ: + return None # Historical executed producers still need terminal API proof. + try: + context = { + "repository": repo, "run_id": int(os.environ["GITHUB_RUN_ID"]), + "run_attempt": int(os.environ["GITHUB_RUN_ATTEMPT"]), + "head_sha": os.environ["GITHUB_SHA"], "event_name": os.environ["GITHUB_EVENT_NAME"], + "event": object_json(Path(os.environ["GITHUB_EVENT_PATH"]).read_bytes()), + "needs": object_json(os.environ["RELEASE_NEEDS"].encode()), + } + except (OSError, ValueError, KeyError) as error: + raise ValueError("release-run-context-invalid: unreadable workflow event/needs") from error + # Capture data before any transport/API read. Consumers check it against + # independent observations; retaining this file alone grants no decision. + directory.parent.mkdir(parents=True, exist_ok=True) + (directory.parent / RUN_CONTEXT_FILE).write_bytes(canonical(context)) + return context + + +def publisher_context( + repo: str, run: dict[str, Any], jobs: list[Any], artifacts: list[Any], +) -> dict[str, Any] | None: + name = RUN_CONTEXT_PREFIX + str(run["run_attempt"]) + rows = [row for row in artifacts if row["name"] == name] + require( + rows or not any(row["name"].startswith(RUN_CONTEXT_PREFIX) for row in artifacts), + "release-run-context-invalid: context attempt differs", + ) + if not rows: + return None + require( + len(rows) == 1 and rows[0]["expired"] is False, + "release-run-context-invalid: duplicate or expired context", + ) + raw = gh_bytes(f"repos/{repo}/actions/artifacts/{rows[0]['id']}/zip") + try: + with zipfile.ZipFile(io.BytesIO(raw)) as zipped: + require( + zipped.namelist() == [RUN_CONTEXT_FILE], + "release-run-context-invalid: context archive layout differs", + ) + context = object_json(zipped.read(RUN_CONTEXT_FILE)) + except (OSError, zipfile.BadZipFile, ValueError) as error: + raise ValueError("release-run-context-invalid: unreadable context archive") from error + return checked_context(repo, run, jobs, context) + + +def stage_inventory( + rows: list[Any], run: dict[str, Any], context: dict[str, Any] | None, +) -> None: + if context is None: + require(rows, f"release-snapshot-missing: run {run['id']} attempt {run['run_attempt']}") + return + expected = { + stage for stage, name in STAGE_JOBS.items() + if name in context["needs"] and context["needs"][name]["result"] != "skipped" + } + prefix = f"release-stage-{run['run_attempt']}-" + require( + {row["name"][len(prefix):] for row in rows} == expected, + "release-snapshot-missing: stage inventory differs from executed producers", + ) + + +def checked_no_producer(run: dict[str, Any], context: dict[str, Any] | None) -> None: + require( + context is not None and run["path"] == ".github/workflows/release.yml" + and run.get("head_branch") == "main", + "release-snapshot-missing: no bound no-producer context", + ) + assert context is not None + needs, event = context["needs"], context["event"] + require( + all(needs[name]["result"] == "skipped" for name in {"manual-pr", *DEPLOY_JOBS}), + "release-snapshot-missing: an executed producer requires evidence", + ) + if run["event"] == "workflow_run": + source = event["workflow_run"] # Already re-read and matched in checked_context. + require( + source["event"] == "workflow_dispatch" and source["head_branch"] == "main" + and needs["claim-name"]["result"] == "skipped", + "release-run-context-invalid: upstream event requires a producer", + ) + else: + require( + run["event"] == "workflow_dispatch" and event.get("ref") == "refs/heads/main" + and event["inputs"]["claim_placeholder"] == "true" + and needs["claim-name"]["result"] == "success", + "release-run-context-invalid: dispatch requires a producer", + ) + + +def checked_cancelled_before_execution( + repo: str, run: dict[str, Any], jobs: list[Any], artifacts: list[Any], +) -> None: + """The provider must explicitly prove that this cancelled attempt ran no jobs.""" + require( + type(run.get("id")) is int and run["id"] > 0 + and type(run.get("run_attempt")) is int and run["run_attempt"] > 0 + and run.get("path") == ".github/workflows/release.yml" + and run.get("name") == "Release" and run.get("head_branch") == "main" + and run.get("event") in {"workflow_run", "workflow_dispatch"} + and isinstance(run.get("head_sha"), str) + and re.fullmatch(r"[0-9a-f]{40}", run["head_sha"]) + and run.get("status") == "completed" and run.get("conclusion") == "cancelled" + and all(isinstance(run.get(key), dict) and run[key].get("full_name") == repo + for key in ("repository", "head_repository")), + "release-producer-outcome-invalid: cancelled publisher binding differs", + ) + endpoint = f"actions/runs/{run['id']}/attempts/{run['run_attempt']}" + exact = api(repo, endpoint) + fields = { + "id", "run_attempt", "path", "name", "event", "head_branch", "head_sha", + "status", "conclusion", + } + require( + isinstance(exact, dict) and type(exact.get("id")) is int + and type(exact.get("run_attempt")) is int + and all(field in exact and exact[field] == run[field] for field in fields) + and all(isinstance(exact.get(key), dict) and exact[key].get("full_name") == repo + for key in ("repository", "head_repository")), + "release-producer-outcome-invalid: cancelled publisher attempt differs", + ) + # inventory() deliberately returns rows only. This exceptional zero-job + # decision additionally needs the provider's complete explicit count. + page = api(repo, endpoint + "/jobs?per_page=100&page=1") + require( + isinstance(page, dict) and type(page.get("total_count")) is int + and page["total_count"] == 0 and isinstance(page.get("jobs"), list) + and page["jobs"] == [] and jobs == [], + "release-producer-outcome-invalid: cancelled publisher lacks complete zero-job inventory", + ) + require( + not any(row["name"].startswith(( + "release-stage-", RUN_CONTEXT_PREFIX, "validated-release-bundle-", + )) or row["name"] in {"release-receipt-data", "release-receipt.json", OUTCOME_FILE} + for row in artifacts), + "release-producer-outcome-invalid: cancelled publisher has evidence", + ) + + +def producer_job( + stage: str, jobs: list[Any], context: dict[str, Any] | None = None, +) -> dict[str, Any]: + matches = [job for job in jobs if job["name"] == STAGE_JOBS[stage]] + if context is not None: + result = context["needs"][STAGE_JOBS[stage]]["result"] + require( + len(matches) == 1 and result in {"success", "failure", "cancelled"}, + "release-producer-job-mismatch: producer did not execute in this attempt", + ) + # checked_context rejects conflicts and binds every API name/run/source. + # needs is the completion authority when same-run REST status is lagging. + return {**matches[0], "conclusion": result} + require( + len(matches) == 1 and matches[0]["status"] == "completed" + and matches[0]["conclusion"] in {"success", "failure", "cancelled"}, + "release-producer-job-mismatch: producer did not execute in this attempt", + ) + return matches[0] + + +def publication_stage( + repo: str, row: dict[str, Any] +) -> tuple[dict[str, Any] | None, dict[str, Any] | None]: + raw = gh_bytes(f"repos/{repo}/actions/artifacts/{row['id']}/zip") + try: + with zipfile.ZipFile(io.BytesIO(raw)) as zipped: + names = zipped.namelist() + require( + bool(names) and len(names) == len(set(names)) + and set(names) <= {"release-receipt.json", OUTCOME_FILE}, + "release-snapshot-invalid: unexpected stage archive contents", + ) + return ( + object_json(zipped.read("release-receipt.json")) + if "release-receipt.json" in names else None, + object_json(zipped.read(OUTCOME_FILE)) if OUTCOME_FILE in names else None, + ) + except (OSError, zipfile.BadZipFile, ValueError) as error: + raise ValueError("release-snapshot-invalid: unreadable stage archive") from error + + +def checked_no_target( + repo: str, run: dict[str, Any], outcome: dict[str, Any], + context: dict[str, Any] | None = None, +) -> None: + reason, inputs = outcome["reason"], outcome["inputs"] + require( + set(inputs) == NO_TARGET_INPUTS[reason], + "release-producer-outcome-invalid: decision inputs differ", + ) + for name, value in inputs.items(): + if name in {"ci_run", "ci_attempt", "publication_run", "publication_attempt", "pr"}: + valid = type(value) is int and value > 0 + elif name in {"source", "base"}: + valid = isinstance(value, str) and bool(re.fullmatch(r"[0-9a-f]{40}", value)) + elif name in {"outcome_sha256", "context_sha256"}: + valid = isinstance(value, str) and bool(re.fullmatch(r"[0-9a-f]{64}", value)) + elif name == "version": + valid = isinstance(value, str) and bool(re.fullmatch(r"\d+\.\d+\.\d+", value)) + elif name == "bump": + valid = isinstance(value, str) and value in {"patch", "minor", "major"} + else: + valid = isinstance(value, str) and bool(value) # ProofPending's actual reason. + require(valid, f"release-producer-outcome-invalid: invalid {name}") + if reason in {"ordinary-main", "main-proofs-pending", "package-publication"}: + ci = api(repo, f"actions/runs/{inputs['ci_run']}") + require( + ci.get("id") == inputs["ci_run"] and ci.get("head_sha") == inputs["source"] + and ci.get("run_attempt") == inputs["ci_attempt"] + and ci.get("event") == "push" and ci.get("head_branch") == "main" + and ci.get("path") in { + ".github/workflows/ci-post.yml", ".github/workflows/native-feed-post.yml", + }, + "release-producer-outcome-invalid: observed main input differs", + ) + prs = api(repo, f"commits/{inputs['source']}/pulls?per_page=100") + matches = [ + pr for pr in prs + if pr.get("merged_at") and pr["base"]["ref"] == "main" + and pr["merge_commit_sha"] == inputs["source"] and marker(pr) + ] + if reason == "ordinary-main": + require( + not matches, + "release-producer-outcome-invalid: main input has a release target", + ) + else: + require( + len(matches) == 1, + "release-producer-outcome-invalid: release PR input differs", + ) + if reason == "package-publication": + meta = marker(api(repo, f"pulls/{matches[0]['number']}")) + require( + meta is not None and meta.get("kind") == "package" + and meta.get("event_id") is None and meta.get("version") == inputs["version"], + "release-producer-outcome-invalid: package publication PR differs", + ) + elif reason == "package-bump": + pr = api(repo, f"pulls/{inputs['pr']}") + meta = marker(pr) + require( + meta is not None and meta.get("kind") == "package" + and meta.get("base") == inputs["base"] and meta.get("bump") == inputs["bump"] + and meta.get("version") == inputs["version"] and meta.get("event_id") is None, + "release-producer-outcome-invalid: package PR differs", + ) + elif reason == "no-ready-release": + commit = api(repo, f"commits/{inputs['base']}") + require( + commit.get("sha") == inputs["base"], + "release-producer-outcome-invalid: base differs", + ) + else: + if reason == "publisher-cancelled-before-execution": + event = context["event"] if context is not None else {} + source = event.get("workflow_run") + dispatch = event.get("inputs") + require( + context is not None + and run["path"] == ".github/workflows/" + WORKFLOW + and ( + (run["event"] == "workflow_run" and isinstance(source, dict) + and source.get("id") == inputs["publication_run"] + and source.get("run_attempt") == inputs["publication_attempt"] + and source.get("head_sha") == inputs["source"]) + or (run["event"] == "workflow_dispatch" and isinstance(dispatch, dict) + and dispatch.get("publication_run") == str(inputs["publication_run"])) + ), + "release-producer-outcome-invalid: cancelled publisher trigger differs", + ) + if run["event"] == "workflow_run": + # checked_context authenticated this original event against + # its independent attempt record. The witness cannot choose + # another attempt, and a later re-run cannot replace this one. + publisher = api( + repo, f"actions/runs/{source['id']}/attempts/{source['run_attempt']}" + ) + else: + # A manual request still selects the current publisher. + publisher = api(repo, f"actions/runs/{dispatch['publication_run']}") + require( + isinstance(publisher, dict) and publisher.get("id") == inputs["publication_run"] + and publisher.get("run_attempt") == inputs["publication_attempt"] + and publisher.get("head_sha") == inputs["source"], + "release-producer-outcome-invalid: cancelled publisher inputs differ", + ) + artifacts = inventory(repo, f"actions/runs/{publisher['id']}/artifacts", "artifacts") + jobs = inventory( + repo, + f"actions/runs/{publisher['id']}/attempts/{publisher['run_attempt']}/jobs", + "jobs", + ) + checked_cancelled_before_execution(repo, publisher, jobs, artifacts) + return + publisher = api(repo, f"actions/runs/{inputs['publication_run']}") + require( + publisher.get("id") == inputs["publication_run"] + and publisher.get("path") == ".github/workflows/release.yml" + and publisher.get("status") == "completed" and publisher.get("conclusion") == "success" + and publisher.get("event") in {"workflow_run", "workflow_dispatch"}, + "release-producer-outcome-invalid: handoff publisher differs", + ) + if reason == "publisher-no-producer": + require( + publisher.get("run_attempt") == inputs["publication_attempt"], + "release-producer-outcome-invalid: publisher attempt moved", + ) + artifacts = inventory(repo, f"actions/runs/{publisher['id']}/artifacts", "artifacts") + rows = stage_rows(artifacts, publisher) + jobs = inventory( + repo, + f"actions/runs/{publisher['id']}/attempts/{publisher['run_attempt']}/jobs", + "jobs", + ) + context = publisher_context(repo, publisher, jobs, artifacts) + stage_inventory(rows, publisher, context) + require(not rows, "release-producer-outcome-invalid: publisher now has stage evidence") + checked_no_producer(publisher, context) + require( + sha256(canonical(context)) == inputs["context_sha256"], + "release-producer-outcome-invalid: publisher context differs", + ) + elif reason == "publisher-no-target": + require( + publisher.get("run_attempt") == inputs["publication_attempt"], + "release-producer-outcome-invalid: publisher attempt moved", + ) + artifacts = inventory(repo, f"actions/runs/{publisher['id']}/artifacts", "artifacts") + rows = stage_rows(artifacts, publisher) + jobs = inventory( + repo, + f"actions/runs/{publisher['id']}/attempts/{publisher['run_attempt']}/jobs", + "jobs", + ) + # Old witness-bearing runs remain readable without a run context; + # empty inventories never take that compatibility path. + context = publisher_context(repo, publisher, jobs, artifacts) + stage_inventory(rows, publisher, context) + checked = None + decision = None + for row in rows: + prior, decision = publication_stage(repo, row) + current = checked_stage( + repo, publisher, row["name"].split("-", 3)[3], jobs, prior, decision, context + ) + same_decision(checked, current) + checked = current + require( + checked is not None and decision is not None + and sha256(canonical(decision)) == inputs["outcome_sha256"], + "release-producer-outcome-invalid: publisher outcome differs", + ) + assert checked is not None + require(checked["event_id"] is None, "publisher had a required current target") + elif reason == "publisher-handoff": + checked_handoff_target(repo, publisher, inputs["version"], inputs["source"]) + require( + publisher["head_sha"] != inputs["source"], + "release-producer-outcome-invalid: publisher did not need a source repair", + ) + ci = api(repo, f"actions/runs/{inputs['ci_run']}") + require( + ci.get("id") == inputs["ci_run"] and ci.get("head_sha") == inputs["source"] + and ci.get("path") == ".github/workflows/ci-post.yml" + and ci.get("event") == "push" and ci.get("head_branch") == "main", + "handoff main source differs", + ) + elif reason == "reporter-handoff": + checked_handoff_target(repo, publisher, inputs["version"], inputs["source"]) + require( + publisher["head_sha"] == inputs["source"] + and run["head_sha"] != inputs["source"], + "reporter did not need a source repair", + ) + else: + checked_handoff_target(repo, publisher, inputs["version"]) + + +def checked_handoff_target( + repo: str, publisher: dict[str, Any], version: str, source: str | None = None, +) -> None: + """A repair decision must refer to the actual completed, targeted publisher.""" + rows = stage_rows(inventory( + repo, f"actions/runs/{publisher['id']}/artifacts", "artifacts" + ), publisher) + require( + rows and rows[-1]["name"] == f"release-stage-{publisher['run_attempt']}-50-verify", + "release-producer-outcome-invalid: handoff lacks publisher verification", + ) + loaded, outcome = publication_stage(repo, rows[-1]) + require( + loaded is not None and outcome is None, + "release-producer-outcome-invalid: handoff lacks a targeted snapshot", + ) + assert loaded is not None + snapshot_identity(loaded, publisher["id"], publisher["run_attempt"]) + job = producer_job("50-verify", inventory( + repo, f"actions/runs/{publisher['id']}/attempts/{publisher['run_attempt']}/jobs", "jobs" + )) + pr = api(repo, f"pulls/{loaded['pr']['number']}") + meta = marker(pr) + require( + job["conclusion"] == "success" + and meta is not None and meta.get("version") == version + and pr.get("merged") is True and pr["head"]["repo"]["full_name"] == repo + and pr["base"]["ref"] == "main" + and loaded["pr"] == pr_identity(pr, base=meta["base"]) + and pr["merge_commit_sha"] == (publisher["head_sha"] if source is None else source), + "release-producer-outcome-invalid: handoff version/source differs", + ) + + +def checked_stage( + repo: str, run: dict[str, Any], stage: str, jobs: list[Any], + loaded: dict[str, Any] | None, outcome: dict[str, Any] | None, + context: dict[str, Any] | None = None, +) -> dict[str, Any]: + job = producer_job(stage, jobs, context) + if outcome is None: + require(loaded is not None, "release-snapshot-missing: receipt and producer outcome absent") + assert loaded is not None + snapshot_identity(loaded, run["id"], run["run_attempt"]) + return ProducerReceipt(loaded) + require( + set(outcome) == { + "schema", "run_id", "run_attempt", "head_sha", "outcome", "reason", + "inputs", "receipt_sha256", + } + and outcome["schema"] == OUTCOME_SCHEMA + and type(outcome["run_id"]) is int and outcome["run_id"] == run["id"] + and type(outcome["run_attempt"]) is int and outcome["run_attempt"] == run["run_attempt"] + and outcome["head_sha"] == run["head_sha"] + and isinstance(outcome["outcome"], str) and isinstance(outcome["reason"], str) + and isinstance(outcome["inputs"], dict) and bool(outcome["inputs"]) + and job["conclusion"] == "success", + "release-producer-outcome-invalid: envelope or producer job differs", + ) + if outcome["outcome"] == "no-target": + require( + stage in NO_TARGET_STAGES.get(outcome["reason"], set()) + and loaded is None and outcome["receipt_sha256"] is None, + "release-producer-outcome-invalid: no-target decision conflicts with snapshot", + ) + checked_no_target(repo, run, outcome, context) + report = receipt(None) + else: + require( + outcome["outcome"] == "replay" and outcome["reason"] == "stored-verified" + and stage == "10-adapter" and loaded is not None + and outcome["receipt_sha256"] == sha256(canonical(loaded)), + "release-producer-outcome-invalid: missing replay proof", + ) + assert loaded is not None + inputs = outcome["inputs"] + require( + set(inputs) == {"version", "installed_source", "installed_lock_sha256"} + and isinstance(inputs.get("version"), str) + and re.fullmatch(r"\d+\.\d+\.\d+", inputs["version"]) + and isinstance(inputs.get("installed_source"), str) + and re.fullmatch(r"[0-9a-f]{40}", inputs["installed_source"]), + "release-replay-invalid: installed source/version missing", + ) + saved = stored_receipt(repo, inputs["version"]) + require( + saved is not None and saved == loaded and saved["state"] == "verified", + "release-replay-invalid: immutable stored receipt differs", + ) + snapshot_identity(loaded, loaded["attempt"], loaded["sequence"]) + installed = object_json(gh_bytes( + f"repos/{repo}/contents/{LOCK}?ref={inputs['installed_source']}", + accept="application/vnd.github.raw+json", + )) + require( + sha256(canonical(installed)) == inputs.get("installed_lock_sha256") + and installed["package_version"] == inputs["version"] + and installed["event"]["event_id"] == loaded["event_id"] + and installed["event"]["release_version"] == loaded["release_version"], + "release-replay-invalid: installed target differs", + ) + actual = verified_package(repo, inputs["version"], saved) + require( + actual == loaded["deployed"] + and actual["image_digest"] == installed["event"]["image"]["digest"], + "release-replay-invalid: installed/served identity differs", + ) + report = ProducerReceipt(loaded) + observe_outcome(report, outcome["outcome"], outcome["reason"], **outcome["inputs"]) + return report + + +def same_decision(previous: dict[str, Any] | None, current: dict[str, Any]) -> None: + """Later stages cannot erase a required target or substitute another decision.""" + if previous is None: + return + require( + (previous["event_id"], previous["release_version"]) + == (current["event_id"], current["release_version"]) + and getattr(previous, "outcome", None) == getattr(current, "outcome", None), + "release-producer-outcome-invalid: stage decisions disagree", + ) + + +def snapshot_identity(report: dict[str, Any], run_id: int, attempt: int) -> None: + """Check provenance before a snapshot can be relabelled or forwarded.""" + require(set(report) == FIELDS, "release-snapshot-invalid: receipt fields differ") + require( + report["schema"] == "pineforge/consumer-receipt/v1" + and report["consumer"] == "data" + and isinstance(report["event_id"], str) + and bool(report["event_id"]) + and isinstance(report["release_version"], str) + and bool(report["release_version"]), + "release-snapshot-invalid: expected a targeted data receipt", + ) + require( + type(run_id) is int and run_id > 0 + and type(attempt) is int and attempt > 0 + and type(report["attempt"]) is int + and report["attempt"] == run_id + and type(report["sequence"]) is int + and report["sequence"] == attempt, + f"release-snapshot-attempt-mismatch: expected run {run_id} attempt {attempt}", + ) + + +def collect(directory: Path, *, failed: bool) -> dict[str, Any]: + repo = os.environ["GITHUB_REPOSITORY"] + context = workflow_context(repo, directory) + run = current_run(repo) + rows = stage_rows(inventory(repo, f"actions/runs/{run['id']}/artifacts", "artifacts"), run) + jobs = inventory(repo, f"actions/runs/{run['id']}/attempts/{run['run_attempt']}/jobs", "jobs") + if context is not None: + checked_context(repo, run, jobs, context) + stage_inventory(rows, run, context) + upstream = {"reconcile"} if run["path"].endswith("/" + WORKFLOW) else { + "manual-pr", *DEPLOY_JOBS, + } + if context is None: + observed_failed = any( + job["name"] in upstream and job["conclusion"] in {"failure", "cancelled"} + for job in jobs + ) + else: + observed_failed = any( + observation["result"] in {"failure", "cancelled"} + for observation in context["needs"].values() + ) + require(failed == observed_failed, "release-producer-job-mismatch: failure input differs") + prefix = f"release-stage-{run['run_attempt']}-" + require( + {path.name for path in directory.glob(prefix + "*")} == {row["name"] for row in rows}, + "release-snapshot-missing: downloaded current inventory differs from GitHub", + ) + if not rows: + checked_no_producer(run, context) + return receipt(None) # No producer ran; never fabricate a producer receipt/outcome. + report = None + for row in rows: + directory_stage = directory / row["name"] + try: + require( + {path.name for path in directory_stage.iterdir()} + <= {"release-receipt.json", OUTCOME_FILE}, + "release-snapshot-invalid: unexpected stage contents", + ) + path = directory_stage / "release-receipt.json" + loaded = object_json(path.read_bytes()) if path.exists() else None + path = directory_stage / OUTCOME_FILE + outcome = object_json(path.read_bytes()) if path.exists() else None + except (OSError, ValueError) as error: + raise ValueError(f"release-snapshot-invalid: {row['name']}") from error + current = checked_stage( + repo, run, row["name"][len(prefix):], jobs, loaded, outcome, context + ) + if ( + (getattr(current, "outcome", None) or {}).get("reason") + == "publisher-cancelled-before-execution" + ): + require( + {path.name for path in directory.glob("release-stage-*")} == {row["name"]} + and not any((directory / name).exists() for name in { + "release-receipt.json", OUTCOME_FILE, RUN_CONTEXT_FILE, "evidence.json", + }), + "release-producer-outcome-invalid: cancelled publisher has downloaded evidence", + ) + same_decision(report, current) + report = current + assert report is not None if failed: - report["attempt"] = int(os.environ.get("GITHUB_RUN_ID", "1")) - report["sequence"] = int(os.environ.get("GITHUB_RUN_ATTEMPT", "1")) + if getattr(report, "outcome", None): + report = receipt(None) # A failed envelope never rewrites an original replay. report["state"] = "failed" report["timestamp"] = timestamp() return report @@ -144,29 +961,69 @@ def callback(report: dict[str, Any]) -> None: ) -def publication(repo: str, run_id: int, report: dict[str, Any]) -> None: +def publication( + repo: str, run_id: int, report: dict[str, Any], *, directory: Path = Path("receipts"), +) -> None: """Finalize a completed Release run using observed candidate, deploy and PyPI evidence.""" require(type(run_id) is int and run_id > 0, "invalid publication run ID") run = api(repo, f"actions/runs/{run_id}") require( - run["path"] == ".github/workflows/release.yml" + run.get("id") == run_id + and type(run.get("run_attempt")) is int + and run["run_attempt"] > 0 + and run["path"] == ".github/workflows/release.yml" and run["event"] in {"workflow_run", "workflow_dispatch"} and run["status"] == "completed", "untrusted or incomplete publisher", ) prefix = f"release-stage-{run['run_attempt']}-" artifacts = inventory(repo, f"actions/runs/{run_id}/artifacts", "artifacts") - stages = sorted( - row["name"] - for row in artifacts - if not row["expired"] - and row["name"].startswith(prefix) - and row["name"][len(prefix) : 2 + len(prefix)] in {"10", "20", "30", "40", "50"} - ) - if not stages: - return # An ordinary main push/manual pre-manifest package has no product target. - loaded = artifact_json(repo, run_id, stages[-1], "release-receipt.json") - require(set(loaded) == FIELDS, "publisher receipt fields differ") + rows = stage_rows(artifacts, run) + jobs = inventory(repo, f"actions/runs/{run_id}/attempts/{run['run_attempt']}/jobs", "jobs") + context = publisher_context(repo, run, jobs, artifacts) + if not rows and context is None and run["conclusion"] == "cancelled": + checked_cancelled_before_execution(repo, run, jobs, artifacts) + require( + report["event_id"] is None and report["release_version"] is None, + "release-producer-outcome-invalid: cancelled publisher conflicts with current target", + ) + require( + not any(directory.glob("release-stage-*")) + and not any((directory / name).exists() for name in { + "release-receipt.json", OUTCOME_FILE, RUN_CONTEXT_FILE, "evidence.json", + }), + "release-producer-outcome-invalid: cancelled publisher has downloaded evidence", + ) + observe_outcome( + report, "no-target", "publisher-cancelled-before-execution", + publication_run=run_id, publication_attempt=run["run_attempt"], source=run["head_sha"], + ) + return + stage_inventory(rows, run, context) + if not rows: + checked_no_producer(run, context) + require(run["conclusion"] == "success", "target-less publisher failed") + observe_outcome( + report, "no-target", "publisher-no-producer", publication_run=run_id, + publication_attempt=run["run_attempt"], context_sha256=sha256(canonical(context)), + ) + return + loaded = None + for row in rows: + snapshot, outcome = publication_stage(repo, row) + current = checked_stage( + repo, run, row["name"][len(prefix):], jobs, snapshot, outcome, context + ) + same_decision(loaded, current) + loaded = current + assert loaded is not None + if loaded["event_id"] is None: + require(run["conclusion"] == "success", "target-less publisher failed") + observe_outcome( + report, "no-target", "publisher-no-target", publication_run=run_id, + publication_attempt=run["run_attempt"], outcome_sha256=sha256(canonical(outcome)), + ) + return report.update(loaded) report.update( attempt=int(os.environ["GITHUB_RUN_ID"]), @@ -181,10 +1038,10 @@ def publication(repo: str, run_id: int, report: dict[str, Any]) -> None: if run["conclusion"] != "success": report["state"] = "failed" return - if stages[-1] == prefix + "10-manual": + if rows[-1]["name"] == prefix + "10-manual": return # A successful manual run opened/tested a PR; it did not deploy. require( - stages[-1] == prefix + "50-verify" and report["deployed"] is not None, + rows[-1]["name"] == prefix + "50-verify" and report["deployed"] is not None, "publisher did not verify served distributions", ) pr = api(repo, f"pulls/{report['pr']['number']}") @@ -208,22 +1065,31 @@ def publication(repo: str, run_id: int, report: dict[str, Any]) -> None: # Source remains data: never check out or execute the PR head or artifact scripts. with tempfile.TemporaryDirectory() as temporary: bundle = Path(temporary) - subprocess.run( - [ - "gh", - "run", - "download", - str(run_id), - "--repo", - repo, - "--name", - f"validated-release-bundle-{run['run_attempt']}", - "--dir", - str(bundle), - ], - check=True, - ) - main_gate = object_json((bundle / "evidence.json").read_bytes()) + bundle_name = f"validated-release-bundle-{run['run_attempt']}" + try: + subprocess.run( + [ + "gh", + "run", + "download", + str(run_id), + "--repo", + repo, + "--name", + bundle_name, + "--dir", + str(bundle), + ], + check=True, + ) + except subprocess.CalledProcessError as error: + raise ValueError( + f"release-bundle-unavailable: run {run_id} artifact {bundle_name}" + ) from error + try: + main_gate = object_json((bundle / "evidence.json").read_bytes()) + except (OSError, ValueError) as error: + raise ValueError(f"release-bundle-invalid: {bundle_name} evidence") from error fresh_gate = gate_for_run(repo, main_gate["run_id"], head=source) require( json_bytes(semantic_evidence_identity(fresh_gate)) @@ -256,10 +1122,17 @@ def publication(repo: str, run_id: int, report: dict[str, Any]) -> None: }, ) report.update(receipt(None)) + observe_outcome( + report, "no-target", "publisher-handoff", publication_run=run_id, + ci_run=main_gate["run_id"], version=version, source=source, + ) return reporting = api(repo, f"actions/runs/{os.environ['GITHUB_RUN_ID']}") require( - reporting["path"] == ".github/workflows/" + WORKFLOW + reporting.get("id") == int(os.environ["GITHUB_RUN_ID"]) + and type(reporting.get("run_attempt")) is int + and reporting["run_attempt"] == int(os.environ["GITHUB_RUN_ATTEMPT"]) + and reporting["path"] == ".github/workflows/" + WORKFLOW and reporting["event"] in {"workflow_run", "workflow_dispatch"}, "untrusted reporter", ) @@ -274,7 +1147,12 @@ def publication(repo: str, run_id: int, report: dict[str, Any]) -> None: }, ) report.update(receipt(None)) + observe_outcome( + report, "no-target", "reporter-handoff", publication_run=run_id, + version=version, source=source, + ) return + # Keep the final deployment observation after the full publication proof. jobs = inventory(repo, f"actions/runs/{run_id}/attempts/{run['run_attempt']}/jobs", "jobs") results = {} for name in DEPLOY_JOBS: @@ -330,29 +1208,63 @@ def retain(repo: str, report: dict[str, Any]) -> None: def main() -> None: parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("command", choices=("collect", "publication", "retain", "callback")) + parser.add_argument( + "command", choices=("collect", "publication", "retain", "callback", "incident", "context") + ) parser.add_argument("--directory", type=Path, default=Path("receipts")) parser.add_argument("--receipt", type=Path, default=Path("release-receipt.json")) parser.add_argument("--run", type=int) args = parser.parse_args() repo = os.environ["GITHUB_REPOSITORY"] - if args.command == "publication": + if args.command == "context": + require( + workflow_context(repo, args.directory) is not None, + "release-run-context-invalid: trusted workflow needs missing", + ) + elif args.command == "publication": report = receipt(None) try: - publication(repo, args.run, report) + publication(repo, args.run, report, directory=args.directory) except Exception: report["state"] = "failed" raise finally: finish(report, args.receipt) - elif args.command == "collect": - report = collect( - args.directory, failed=os.environ["FAILED"] == "true", stage=os.environ["STAGE"] + elif args.command in {"collect", "incident"}: + incident_only = args.command == "incident" + require( + os.environ["FAILED"] in {"true", "false"}, + "FAILED must be the actual workflow failure observation", ) - # Stored verified replays are checked, but do not re-emit an old snapshot - # under a new reporting source/run. Polling can recover the original artifact. - has_event = bool(report["event_id"]) and report["attempt"] == int( - os.environ["GITHUB_RUN_ID"] + diagnostic_error = None + try: + report = collect( + args.directory, + failed=os.environ["FAILED"] == "true", + ) + except ( + ValueError, OSError, subprocess.CalledProcessError, KeyError, TypeError, AttributeError, + ) as error: + if not incident_only: + raise + # Invalid release evidence must not prevent a run-scoped incident. + # This target-less object is never written or sent to the hub. + report = receipt(None) + diagnostic_error = f"{type(error).__name__}: {error}" + if incident_only: + # A delivery error is a real failure even if the upstream producers + # succeeded. Preserve any fully collected event/PR without emitting it. + report = ProducerReceipt(copy.deepcopy(report)) + report["state"] = "failed" + # A replay is checked against the stored asset and installed proof. Its + # original attempt, sequence, timestamp and pins remain untouched. + producer_decision = getattr(report, "outcome", None) + has_event = ( + not incident_only + and not producer_decision + and bool(report["event_id"]) + and report["attempt"] == int(os.environ["GITHUB_RUN_ID"]) + and report["sequence"] == int(os.environ["GITHUB_RUN_ATTEMPT"]) ) if ( has_event @@ -364,8 +1276,11 @@ def main() -> None: finish(report, args.receipt) with Path(os.environ["GITHUB_OUTPUT"]).open("a") as handle: handle.write(f"has_event={str(has_event).lower()}\n") - incident(repo, report, os.environ["RELEASE_ALERT_OWNER"], os.environ["STAGE"]) + if not producer_decision or incident_only: + incident(repo, report, os.environ["RELEASE_ALERT_OWNER"], os.environ["STAGE"]) with Path(os.environ["GITHUB_STEP_SUMMARY"]).open("a") as handle: + if diagnostic_error is not None: + handle.write(f"Receipt collection failed: {diagnostic_error}\n") handle.write( f"Data release state: **{report['state']}**; event `{report['event_id']}`.\n" ) diff --git a/tests/test_consumer_receipt.py b/tests/test_consumer_receipt.py index 9321eba..512e184 100644 --- a/tests/test_consumer_receipt.py +++ b/tests/test_consumer_receipt.py @@ -5,8 +5,10 @@ import io import json import os +import sys import zipfile from pathlib import Path +from urllib.error import HTTPError from urllib.parse import parse_qs, urlsplit from xml.etree import ElementTree @@ -283,15 +285,33 @@ def validate_waiting_python(proof, run, jobs, **kwargs): @pytest.fixture def incident_transport(monkeypatch): """Real incident/inventory logic, with mutable issue observations at the API.""" - state = {"issues": [], "reads": [], "writes": [], "comments": []} + state = { + "issues": [], "reads": [], "writes": [], "comments": [], "producer_reads": [], + "artifacts": [], "reports": [], "monkeypatch": monkeypatch, + "jobs": [{"name": "prepare", "status": "completed", "conclusion": "success"}], + } + monkeypatch.setenv("GITHUB_REPOSITORY", REPO) monkeypatch.setenv("GITHUB_RUN_ID", "930") monkeypatch.setenv("GITHUB_RUN_ATTEMPT", "2") + monkeypatch.setenv("GITHUB_SHA", MERGE) def api(repo, endpoint, method="GET", data=None): assert repo == REPO if method == "GET": - state["reads"].append(endpoint) assert data is None + if endpoint.startswith(("actions/runs/", "commits/", "pulls/")): + state["producer_reads"].append(endpoint) + if endpoint == "actions/runs/930": + return { + "id": 930, "run_attempt": 2, "head_sha": MERGE, + "path": ".github/workflows/release.yml", "event": "workflow_run", + } + if endpoint == "actions/runs/930/artifacts?per_page=100&page=1": + return {"artifacts": copy.deepcopy(state["artifacts"])} + if endpoint == "actions/runs/930/attempts/2/jobs?per_page=100&page=1": + return {"jobs": copy.deepcopy(state["jobs"])} + return publishing.api(repo, endpoint) + state["reads"].append(endpoint) assert endpoint == ( "issues?state=all&sort=updated&direction=desc&per_page=100&page=1" ) @@ -313,18 +333,273 @@ def api(repo, endpoint, method="GET", data=None): # inventory's original implementation resolves automation.api at call time. monkeypatch.setattr(automation, "api", api) monkeypatch.setattr(reporting, "api", api) + real_incident = reporting.incident + + def observe_incident(repo, report, owner, stage): + state["reports"].append(copy.deepcopy(report)) + return real_incident(repo, report, owner, stage) + + monkeypatch.setattr(reporting, "incident", observe_incident) return state +def callback_report_cli(directory, incidents, command, *, failed): + monkeypatch = incidents["monkeypatch"] + with monkeypatch.context() as cli: + cli.setenv("GITHUB_OUTPUT", str(directory.parent / "report-outputs")) + cli.setenv("GITHUB_STEP_SUMMARY", str(directory.parent / "report-summary")) + cli.setenv("RELEASE_ALERT_OWNER", "release-owner") + cli.setenv("STAGE", "prepare-publication") + cli.setenv("FAILED", str(failed).lower()) + cli.setattr(sys, "argv", [ + "release_chain.reporting", command, "--directory", str(directory), + "--receipt", str(directory.parent / "report-release-receipt.json"), + ]) + reporting.main() + assert not (directory.parent / "report-release-receipt.json").exists() + assert (directory.parent / "report-outputs").read_text() == "has_event=false\n" + + def collect_callback_without_incident(state, incidents): directory = state["callback_root"] / "downloaded-stages" - assert list(directory.glob("release-stage-*/release-receipt.json")) == [] - report = reporting.collect(directory, failed=False, stage="prepare-publication") - assert report["state"] == "pending" and report["event_id"] is None - assert report["pr"] is report["ci"] is report["deployed"] is None - reporting.incident(REPO, report, "release-owner", "prepare-publication") + stage = directory / "release-stage-2-20-prepare" + automation.finish(state["callback_report"], stage / "release-receipt.json") + # Match upload-artifact: no file means no artifact, including on the defective source. + if stage.exists() and any(stage.iterdir()): + incidents["artifacts"] = [{ + "id": 93020, "name": "release-stage-2-20-prepare", "expired": False, + }] + callback_report_cli(directory, incidents, "collect", failed=False) + assert "**pending**" in (directory.parent / "report-summary").read_text() assert incidents["reads"] == incidents["writes"] == [] - return report + + +@pytest.mark.parametrize("source", ["CI", "Native feed"]) +@pytest.mark.parametrize("slot", ["python", "native"]) +@pytest.mark.parametrize("readiness", ["missing", "queued", "in_progress"]) +def test_cli_prepare_and_report_preserve_real_proof_waiting( + main_callback_transport, incident_transport, monkeypatch, source, slot, readiness +): + state = main_callback_transport + run = state[f"{slot}_run"] + if readiness == "missing": + state[f"{slot}_runs"] = [] + else: + run.update(status=readiness, conclusion=None) + for job in state[f"{slot}_jobs"]: + job.update(status=readiness, conclusion=None) + directory = state["callback_root"] / "downloaded-stages" + producer = directory / "release-stage-2-20-prepare" + callback = state["python_run" if source == "CI" else "native_run"]["id"] + with monkeypatch.context() as cli: + cli.setenv("GITHUB_OUTPUT", str(state["callback_root"] / "producer-outputs")) + cli.setattr(sys, "argv", [ + "release_chain.publishing", "prepare", "--run", str(callback), + "--directory", str(state["output"]), + "--receipt", str(producer / "release-receipt.json"), + ]) + publishing.main() + if producer.exists() and any(producer.iterdir()): + incident_transport["artifacts"] = [{ + "id": 93020, "name": "release-stage-2-20-prepare", "expired": False, + }] + callback_report_cli(directory, incident_transport, "collect", failed=False) + assert incident_transport["issues"] == incident_transport["writes"] == [] + assert not (producer / "release-receipt.json").exists() + decision = contract.object_json((producer / "producer-outcome.json").read_bytes()) + assert decision["reason"] == "main-proofs-pending" + assert decision["inputs"]["source"] == state["python_run"]["head_sha"] + assert state["effects"] == [] + + +@pytest.mark.parametrize("fault", [ + None, "missing", "run", "attempt", "head", "schema", "fields", + "ci-run", "ci-attempt", "source", "version", +]) +def test_cli_targetless_package_carries_actual_outcome_through_every_publisher_stage( + main_callback_transport, incident_transport, monkeypatch, fault +): + state = main_callback_transport + source = state["python_run"]["head_sha"] + pr = state["callback_pr"] + pr["body"] = automation.body({ + "kind": "package", "event_id": None, "bump": "patch", "version": "0.3.1", + "generated_head": pr["head"]["sha"], "base": BASE, + }) + pin = ( + "ghcr.io/pineforge-4pass/pineforge-release:1.0.0@sha256:" + "b271873d0e91a9eeeb4595daaded9bdfbdbcd3925d3ec1300a833395e2004c86" + ) + files = { + contract.RUNTIME: f'DEFAULT_RELEASE_IMAGE = "{pin}"\n'.encode(), + contract.DOCKERFILE: f"ARG PINEFORGE_RELEASE_IMAGE={pin}\n".encode(), + "pyproject.toml": b'[project]\nname = "pineforge-data"\nversion = "0.3.1"\n', + } + distributions = { + "pineforge_data-0.3.1-py3-none-any.whl": b"independent tested wheel bytes", + "pineforge_data-0.3.1.tar.gz": b"independent tested sdist bytes", + } + hashes = {name: contract.sha256(raw) for name, raw in distributions.items()} + state["python_evidence"]["distributions"] = hashes + release = { + "tag_name": "v0.3.1", + "assets": [{"id": index, "name": name} for index, name in enumerate(distributions, 1)], + } + metadata = {"urls": [ + {"filename": name, "digests": {"sha256": hashes[name]}, + "url": f"https://files.pythonhosted.org/{name}"} + for name in distributions + ]} + publication = {"available": False, "commands": []} + original_api, report_api = publishing.api, automation.api + + def api(repo, endpoint, method="GET", data=None): + if endpoint == "": + return {"default_branch": "main"} + if endpoint == "releases/tags/v0.3.1": + return copy.deepcopy(release) + return original_api(repo, endpoint, method, data) + + def inventory_api(repo, endpoint, method="GET", data=None): + if endpoint == "releases?per_page=100&page=1": + return [copy.deepcopy(release)] + return report_api(repo, endpoint, method, data) + + def snapshot(revision, directory): + assert revision in {source, "origin/main"} + for name, raw in files.items(): + path = directory / name + path.parent.mkdir(parents=True, exist_ok=True) + path.write_bytes(raw) + + def git(*args): + if args[0] == "fetch": + return "" + if args[0] == "ls-remote": + return "retained tag" + assert args[0] == "rev-parse" + return { + f"{source}^{{tree}}": state["python_evidence"]["tree"], + f"{source}^1": BASE, "v0.3.1^{commit}": source, + }[args[1]] + + def fetch(url, headers=None): + assert headers is None + if url == "https://pypi.org/pypi/pineforge-data/0.3.1/json": + if not publication["available"]: + raise HTTPError(url, 404, "not yet published", {}, None) + return contract.json_bytes(metadata) + assert url.startswith("https://files.pythonhosted.org/") + return distributions[url.rsplit("/", 1)[1]] + + def asset_bytes(route, *, accept): + assert accept == "application/octet-stream" + asset = next(row for row in release["assets"] if route.endswith(f"/{row['id']}")) + return distributions[asset["name"]] + + def process(command, *, check): + assert check is True + publication["commands"].append(command) + if command[:3] == ["gh", "run", "download"]: + assert command[3] == str(state["python_run"]["id"]) + assert command[7] == ( + f"python-package-distributions-{state['python_run']['run_attempt']}" + ) + directory = Path(command[-1]) + directory.mkdir(parents=True) + for name, raw in distributions.items(): + (directory / name).write_bytes(raw) + else: + assert command == ["gh", "release", "edit", "v0.3.1", "--repo", REPO, "--latest"] + + monkeypatch.setattr(publishing, "api", api) + monkeypatch.setattr(automation, "api", inventory_api) + monkeypatch.setattr(publishing, "snapshot", snapshot) + monkeypatch.setattr(publishing, "git", git) + monkeypatch.setattr(publishing, "fetch", fetch) + monkeypatch.setattr(publishing, "gh_bytes", asset_bytes) + monkeypatch.setattr(publishing.subprocess, "run", process) + incident_transport["jobs"] = [ + {"name": name, "status": "completed", "conclusion": "success"} + for name in receipts.DEPLOY_JOBS + ] + directory = state["callback_root"] / "downloaded-stages" + previous = None + for command, stage_name in ( + ("prepare", "20-prepare"), ("github-release", "30-github"), + ("preflight", "40-publish"), ("verify", "50-verify"), + ): + stage = directory / ("release-stage-2-" + stage_name) + if command == "verify": + previous = directory / "release-stage-2-30-github" + if previous is not None: + for filename in ("release-receipt.json", "producer-outcome.json"): + path = previous / filename + if path.exists(): + stage.mkdir(parents=True, exist_ok=True) + (stage / filename).write_bytes(path.read_bytes()) + if command == "verify": + # Independently observed PyPI transition after the upload boundary. + publication["available"] = True + with monkeypatch.context() as cli: + cli.setenv("GITHUB_OUTPUT", str(state["callback_root"] / "producer-outputs")) + argv = [ + "release_chain.publishing", command, "--directory", str(state["output"]), + "--receipt", str(stage / "release-receipt.json"), + ] + if command == "prepare": + argv += ["--run", str(state["python_run"]["id"])] + cli.setattr(sys, "argv", argv) + if command == "preflight" and fault: + witness = stage / "producer-outcome.json" + assert witness.is_file() + if fault == "missing": + witness.rename(state["callback_root"] / "withheld-package-outcome.json") + else: + observed = contract.object_json(witness.read_bytes()) + if fault in {"run", "attempt"}: + observed["run_id" if fault == "run" else "run_attempt"] += 1 + elif fault == "head": + observed["head_sha"] = "9" * 40 + elif fault == "schema": + observed["schema"] = "pineforge/consumer-receipt/v1" + elif fault == "fields": + observed["ignored"] = True + elif fault in {"ci-run", "ci-attempt"}: + observed["inputs"]["ci_run" if fault == "ci-run" else "ci_attempt"] += 1 + elif fault == "source": + observed["inputs"]["source"] = "9" * 40 + else: + observed["inputs"]["version"] = "8.9.10" + witness.write_bytes(contract.json_bytes(observed)) + # A missing prerequisite is a setup failure, never causal RED. + error = FileNotFoundError if fault == "missing" else ValueError + cause = "producer-outcome.json" if fault == "missing" else ( + "^missing or invalid package publication outcome$" + ) + with pytest.raises(error, match=cause): + publishing.main() + assert not (stage / "release-receipt.json").exists() + assert incident_transport["writes"] == [] + return + publishing.main() + if stage.exists() and any(stage.iterdir()): + incident_transport["artifacts"].append({ + "id": 93000 + len(incident_transport["artifacts"]), + "name": stage.name, "expired": False, + }) + previous = stage + callback_report_cli(directory, incident_transport, "collect", failed=False) + assert incident_transport["issues"] == incident_transport["writes"] == [] + assert len(state["validated_pairs"]) == 1 + assert set(state["validated_pairs"][0]["main_proofs"]) == {"network", "native"} + assert len(publication["commands"]) == 2 + assert all(not (path / "release-receipt.json").exists() for path in directory.iterdir()) + + +def callback_failure_incident(directory, incidents): + callback_report_cli(directory, incidents, "incident", failed=True) + return incidents["reports"][-1] def test_main_gate_consumes_matching_native_transport_positive_twin(main_proof_transport): @@ -899,12 +1174,11 @@ def test_main_callbacks_refuse_invalid_python_while_native_pending( "number": 41, "state": "closed", "body": "\nolder observation", }) - report = reporting.collect( - state["callback_root"] / "downloaded-stages", failed=True, stage="prepare-publication" + report = callback_failure_incident( + state["callback_root"] / "downloaded-stages", incidents ) assert report["state"] == "failed" and report["event_id"] is None assert report["attempt"] == 930 and report["sequence"] == 2 - reporting.incident(REPO, report, "release-owner", "prepare-publication") assert len(incidents["reads"]) == 1 assert [(route, method) for route, method, _ in incidents["writes"]] == ( [("issues/41/comments", "POST"), ("issues/41", "PATCH")] if existing_issue else @@ -967,10 +1241,9 @@ def test_callback_reporting_failure_still_opens_or_updates_incident( "number": 41, "state": "closed", "body": "\nolder observation", }) - report = reporting.collect(tmp_path, failed=True, stage="prepare-publication") + report = callback_failure_incident(tmp_path / "downloaded-stages", incidents) assert report["state"] == "failed" and report["event_id"] is None assert report["attempt"] == 930 and report["sequence"] == 2 - reporting.incident(REPO, report, "release-owner", "prepare-publication") assert len(incidents["reads"]) == 1 assert [(route, method) for route, method, _ in incidents["writes"]] == ( [("issues/41/comments", "POST"), ("issues/41", "PATCH")] if existing_issue else @@ -1044,10 +1317,7 @@ def test_main_callbacks_keep_completed_and_malformed_proofs_decisive( if fault == "malformed-json": assert b'{"run_id":1,"run_id":2}' in state["parsed_bytes"] # A real refusal must still take the actionable failure path. - report = reporting.collect( - state["callback_root"], failed=True, stage="prepare-publication" - ) - reporting.incident(REPO, report, "release-owner", "prepare-publication") + callback_failure_incident(state["callback_root"] / "downloaded-stages", incident_transport) assert [(route, method) for route, method, _ in incident_transport["writes"]] == [ ("issues", "POST"), ("issues/41", "PATCH"), ] @@ -1306,8 +1576,14 @@ def publication_main_transport( saved = reporting.gate_for_run(REPO, python["id"], head=source) assert set(saved["main_proofs"]) == {"network", "native"} assert state["main_validations"][-1] == saved + publisher = { + "id": 200, "run_attempt": 1, "path": ".github/workflows/release.yml", + "event": "workflow_run", "head_sha": source, + "status": "completed", "conclusion": "success", + } staged = contract.object_json((FIXTURES / "sample-release-receipt.json").read_bytes()) staged.update( + attempt=publisher["id"], sequence=publisher["run_attempt"], pr=automation.pr_identity(pr), state="deploying", deployed=receipts.served(event, source, "0.3.1", saved["distributions"]), ) @@ -1341,15 +1617,16 @@ def api(repo, endpoint, method="GET", data=None): assert data is None state["publication_reads"].append(endpoint) if endpoint == "actions/runs/200": - return { - "id": 200, "run_attempt": 1, "path": ".github/workflows/release.yml", - "event": "workflow_run", "head_sha": source, - "status": "completed", "conclusion": "success", - } + return copy.deepcopy(publisher) if endpoint == "actions/runs/200/artifacts?per_page=100&page=1": return {"artifacts": [{ "id": 200001, "name": "release-stage-1-50-verify", "expired": False, }]} + if endpoint == "actions/runs/200/attempts/1/jobs?per_page=100&page=1": + return {"jobs": [ + {"name": name, "status": "completed", "conclusion": "success"} + for name in receipts.DEPLOY_JOBS + ]} if endpoint == "pulls/7": return copy.deepcopy(pr) if endpoint == f"commits/{source}": @@ -1425,6 +1702,7 @@ def verify_local(root): monkeypatch.setattr(automation, "api", api) monkeypatch.setattr(reporting, "api", api) monkeypatch.setattr(evidence, "gh_bytes", gh_bytes) + monkeypatch.setattr(reporting, "gh_bytes", gh_bytes) monkeypatch.setattr(contract, "gh_bytes", hub_bytes) monkeypatch.setattr(reporting.subprocess, "run", download) monkeypatch.setattr(reporting, "gate_for_run", observe_gate) @@ -1705,6 +1983,7 @@ class ObservedGitHub: def __init__(self): self.writes = [] self.archive = b"" + self.stage_artifacts = {} self.pr = { "number": 7, "merged": True, @@ -1735,6 +2014,7 @@ def __init__(self): }, 300: { "id": 300, + "run_attempt": 1, "path": ".github/workflows/pineforge-release.yml", "head_sha": MERGE, "event": "workflow_run", @@ -1773,6 +2053,8 @@ def api(self, route): if parts[-1] == "jobs": return {"jobs": copy.deepcopy(self.jobs[run_id])} if parts[-1] == "artifacts": + if run_id in self.stage_artifacts: + return {"artifacts": copy.deepcopy(self.stage_artifacts[run_id])} name = "release-stage-1-50-verify" if run_id == 200 else "release-receipt-data" return { "artifacts": [ @@ -1799,11 +2081,18 @@ def consumer_api(self, repo, endpoint, method="GET", data=None): @pytest.fixture -def completed_data_receipt(monkeypatch): +def completed_data_receipt(monkeypatch, tmp_path): + """Synthetic coordinator/contract wiring only. + + Candidate, main, local-lock, OCI, served and previous proof boundaries use + fixed answers below. This fixture cannot establish full publication proof. + """ event = contract.object_json((FIXTURES / "ready-event.json").read_bytes()) expected = contract.object_json((FIXTURES / "sample-release-receipt.json").read_bytes()) monkeypatch.setenv("GITHUB_RUN_ID", "300") monkeypatch.setenv("GITHUB_RUN_ATTEMPT", "1") + monkeypatch.setenv("GITHUB_REPOSITORY", REPO) + monkeypatch.setenv("GITHUB_SHA", MERGE) monkeypatch.setenv("RELEASE_RECEIPT_POLICY", (FIXTURES / "receipt-policy.json").read_text()) monkeypatch.setattr(receipts, "timestamp", lambda: "2020-01-01T00:07:00Z") monkeypatch.setattr(reporting, "timestamp", lambda: "2020-01-01T00:07:00Z") @@ -1832,7 +2121,20 @@ def completed_data_receipt(monkeypatch): monkeypatch.setattr( reporting, "inventory", lambda repo, route, key: client.api(f"repos/{repo}/{route}")[key] ) - monkeypatch.setattr(reporting, "artifact_json", lambda *args: copy.deepcopy(staged)) + original_bytes = evidence.gh_bytes + + def stage_bytes(route, **kwargs): + if route == f"repos/{REPO}/actions/artifacts/200/zip": + archive = io.BytesIO() + with zipfile.ZipFile(archive, "w") as zipped: + zipped.writestr("release-receipt.json", receipts.canonical(staged)) + return archive.getvalue() + if route.startswith(f"repos/{REPO}/actions/runs/200/artifacts?"): + return contract.json_bytes(client.api(route)) + return original_bytes(route, **kwargs) + + monkeypatch.setattr(reporting, "gh_bytes", stage_bytes) + monkeypatch.setattr(evidence, "gh_bytes", stage_bytes) monkeypatch.setattr( reporting, "hub_manifest", lambda version: (FIXTURES / "release-manifest.json").read_bytes() ) @@ -1872,8 +2174,13 @@ def download(command, *, check): (Path(command[-1]) / "evidence.json").write_bytes(contract.json_bytes(main_gate)) monkeypatch.setattr(reporting.subprocess, "run", download) - report = receipts.receipt(None) - reporting.publication(REPO, 200, report) + path = tmp_path / "coordinator" / "release-receipt.json" + with monkeypatch.context() as cli: + cli.setattr(sys, "argv", [ + "release_chain.reporting", "publication", "--run", "200", "--receipt", str(path), + ]) + reporting.main() + report = contract.object_json(path.read_bytes()) assert verified_versions == ["0.3.1", "0.3.0"] assert report == expected return report, event, client, hub.profile("data") @@ -1967,6 +2274,87 @@ def test_moved_reporting_source_queues_a_trusted_immutable_tag_run(completed_dat ] +@pytest.mark.parametrize("moved", ["publisher", "reporter"]) +@pytest.mark.parametrize("fault", [ + None, "version", "source", "publisher-attempt", "publisher-state", + "reporter-head", "missing-publisher-stage", +]) +def test_cli_source_repair_handoff_orchestration_with_stubbed_proof_boundaries( + completed_data_receipt, tmp_path, monkeypatch, moved, fault +): + _report, _event, client, _policy = completed_data_receipt + client.runs[200 if moved == "publisher" else 300]["head_sha"] = "9" * 40 + client.runs[80] = { + "id": 80, "run_attempt": 1, "head_sha": MERGE, "event": "push", + "head_branch": "main", "path": ".github/workflows/ci-post.yml", + } + client.jobs[300] = [{"name": "reconcile", "status": "completed", "conclusion": "success"}] + produced = tmp_path / "handoff" + monkeypatch.setenv("GITHUB_SHA", client.runs[300]["head_sha"]) + monkeypatch.setattr(sys, "argv", [ + "release_chain.reporting", "publication", "--run", "200", + "--receipt", str(produced / "release-receipt.json"), + ]) + reporting.main() + directory = tmp_path / "handoff-download" + witness = produced / "producer-outcome.json" + client.stage_artifacts[300] = [] + assert witness.is_file() + stage = directory / "release-stage-1-10-adapter" + stage.mkdir(parents=True) + (stage / witness.name).write_bytes(witness.read_bytes()) + client.stage_artifacts[300] = [{ + "id": 30010, "name": stage.name, "expired": False, + }] + if fault in {"version", "source"}: + decision = contract.object_json((stage / witness.name).read_bytes()) + decision["inputs"]["version" if fault == "version" else "source"] = ( + "8.9.10" if fault == "version" else "8" * 40 + ) + (stage / witness.name).write_bytes(contract.json_bytes(decision)) + if fault == "publisher-attempt": + client.runs[200]["run_attempt"] += 1 + elif fault == "publisher-state": + client.runs[200]["conclusion"] = "failure" + elif fault == "reporter-head": + client.runs[300]["head_sha"] = "8" * 40 + elif fault == "missing-publisher-stage": + client.stage_artifacts[200] = [] + monkeypatch.setenv("GITHUB_OUTPUT", str(tmp_path / "handoff-outputs")) + monkeypatch.setenv("GITHUB_STEP_SUMMARY", str(tmp_path / "handoff-summary")) + monkeypatch.setenv("FAILED", "false") + monkeypatch.setenv("STAGE", "reconcile") + monkeypatch.setenv("RELEASE_ALERT_OWNER", "release-owner") + monkeypatch.setattr(sys, "argv", [ + "release_chain.reporting", "collect", "--directory", str(directory), + "--receipt", str(tmp_path / "handoff-public-receipt.json"), + ]) + if fault: + cause = { + "version": "release-producer-outcome-invalid: handoff version/source differs", + "source": "release-producer-outcome-invalid: handoff version/source differs", + "publisher-attempt": "release-snapshot-attempt-mismatch:", + "publisher-state": "release-producer-outcome-invalid: handoff publisher differs", + "reporter-head": "release-producer-outcome-invalid: envelope or producer job differs", + "missing-publisher-stage": ( + "release-producer-outcome-invalid: handoff lacks publisher verification" + ), + }[fault] + with pytest.raises(ValueError, match="^" + cause): + reporting.main() + assert not (tmp_path / "handoff-outputs").exists() + else: + reporting.main() + assert (tmp_path / "handoff-outputs").read_text() == "has_event=false\n" + assert not (tmp_path / "handoff-public-receipt.json").exists() + assert not (produced / "release-receipt.json").exists() + assert len(client.writes) == 1 + assert client.writes[0][1] == ( + "actions/workflows/release.yml/dispatches" if moved == "publisher" + else "actions/workflows/pineforge-release.yml/dispatches" + ) + + def test_only_the_documented_replay_upload_can_skip(completed_data_receipt): report, event, client, policy = completed_data_receipt client.jobs[200][3]["conclusion"] = "skipped" @@ -2604,22 +2992,38 @@ def test_successful_manual_pr_run_reports_progress_without_claiming_deploy( completed_data_receipt, monkeypatch ): report, _event, client, _policy = completed_data_receipt + publisher = client.runs[200] progress = copy.deepcopy(report) progress.update( + attempt=publisher["id"], sequence=publisher["run_attempt"], state="pr_open", ci=None, e2e=None, deploy=None, preview=None, deployed=None, previous=None ) progress["pr"]["merge"] = None monkeypatch.setattr( reporting, "inventory", - lambda *args: [ - { - "name": "release-stage-1-10-manual", + lambda _repo, _endpoint, key: ( + client.jobs[200] if key == "jobs" else [{ + "id": 200, + "name": f"release-stage-{publisher['run_attempt']}-10-manual", "expired": False, - } - ], + }] + ), ) - monkeypatch.setattr(reporting, "artifact_json", lambda *args: copy.deepcopy(progress)) + client.jobs[200].append({ + "name": "manual-pr", "status": "completed", "conclusion": "success", + }) + archive = io.BytesIO() + with zipfile.ZipFile(archive, "w") as zipped: + zipped.writestr("release-receipt.json", receipts.canonical(progress)) + original_bytes = reporting.gh_bytes + + def stage_bytes(route, **kwargs): + if route == f"repos/{REPO}/actions/artifacts/200/zip": + return archive.getvalue() + return original_bytes(route, **kwargs) + + monkeypatch.setattr(reporting, "gh_bytes", stage_bytes) result = receipts.receipt(None) reporting.publication(REPO, 200, result) assert result["state"] == "pr_open" and result["deploy"] is None diff --git a/tests/test_release_adapter.py b/tests/test_release_adapter.py index 7d43bb3..83c0c12 100644 --- a/tests/test_release_adapter.py +++ b/tests/test_release_adapter.py @@ -4,6 +4,7 @@ import io import shutil import subprocess +import sys import time import zipfile from pathlib import Path @@ -11,6 +12,7 @@ from xml.etree import ElementTree import pytest +import release_adapter import yaml from release_chain import automation, contract, evidence, publishing, receipts, reporting @@ -1597,7 +1599,7 @@ def test_replayed_applied_event_creates_no_pr_or_merge(ready, baseline, monkeypa ), ) monkeypatch.setattr(automation, "api", lambda *args: pytest.fail("duplicate mutated GitHub")) - monkeypatch.setattr(publishing, "recover_applied", lambda *args: None) + monkeypatch.setattr(publishing, "recover_applied", lambda *args, **kwargs: None) report = automation.receipt(None, "test") automation.reconcile("owner/data", {"client_payload": event}, report, "release[bot]") assert report["state"] == "merged" @@ -1632,14 +1634,60 @@ def test_public_receipt_cannot_contain_homegrown_or_private_fields(ready) -> Non reporting.public_receipt(report) -def test_failed_downstream_job_overrides_earlier_green_receipt(tmp_path, ready) -> None: +def test_failed_downstream_job_overrides_earlier_green_receipt( + tmp_path, ready, monkeypatch +) -> None: + monkeypatch.setenv("GITHUB_REPOSITORY", "owner/data") + monkeypatch.setenv("GITHUB_RUN_ID", "200") + monkeypatch.setenv("GITHUB_RUN_ATTEMPT", "3") + monkeypatch.setenv("GITHUB_OUTPUT", str(tmp_path / "outputs")) + monkeypatch.setenv("GITHUB_STEP_SUMMARY", str(tmp_path / "summary")) + monkeypatch.setenv("FAILED", "true") + monkeypatch.setenv("STAGE", "distribution-transfer") + monkeypatch.setenv("RELEASE_ALERT_OWNER", "release-owner") event, _manifest = ready report = automation.receipt(event, "prepare") report["state"] = "deploying" - directory = tmp_path / "release-stage-1-20-prepare" + directory = tmp_path / f"release-stage-{report['sequence']}-20-prepare" directory.mkdir() (directory / "release-receipt.json").write_bytes(contract.json_bytes(report)) - result = reporting.collect(tmp_path, failed=True, stage="publication") + issues = [] + + def api(repo, endpoint, method="GET", data=None): + assert repo == "owner/data" + if endpoint == "actions/runs/200": + return { + "id": 200, "run_attempt": 3, "path": ".github/workflows/release.yml", + "head_sha": "c" * 40, "event": "workflow_run", + } + if endpoint == "actions/runs/200/artifacts?per_page=100&page=1": + return {"artifacts": [{ + "id": 201, "name": "release-stage-3-20-prepare", "expired": False, + }]} + if endpoint == "actions/runs/200/attempts/3/jobs?per_page=100&page=1": + return {"jobs": [ + {"name": "prepare", "status": "completed", "conclusion": "success"}, + {"name": "build", "status": "completed", "conclusion": "failure"}, + ]} + if endpoint == "releases?per_page=100&page=1": + return [] + if endpoint.startswith("issues?"): + return issues + if endpoint == "issues" and method == "POST": + issues.append({"number": 41, **data}) + return issues[0] + assert endpoint == "issues/41" and method == "PATCH" + issues[0].update(data) + return issues[0] + + monkeypatch.setattr(automation, "api", api) + monkeypatch.setattr(reporting, "api", api) + monkeypatch.setattr(sys, "argv", [ + "release_chain.reporting", "collect", "--directory", str(tmp_path), + "--receipt", str(tmp_path / "release-receipt.json"), + ]) + reporting.main() + result = contract.object_json((tmp_path / "release-receipt.json").read_bytes()) assert result["state"] == "failed" assert result["event_id"] == event["event_id"] assert result["deployed"] is None @@ -1784,7 +1832,7 @@ def test_completed_receipt_replay_does_not_dispatch_or_publish( monkeypatch.setattr(publishing, "verified_package", lambda *args: saved["deployed"]) monkeypatch.setattr(publishing, "api", lambda *args: pytest.fail("replayed publication")) report = automation.receipt(event, "reconcile") - publishing.recover_applied("owner/data", baseline, report) + publishing.recover_applied("owner/data", baseline, report, installed_source="b" * 40) assert report["state"] == "verified" assert report["deployed"] == saved["deployed"] @@ -1958,12 +2006,26 @@ def test_recovery_saved_receipt_replays_after_real_verification(saved_recovery_t state = saved_recovery_transport apply(state["root"], state["source_files"]) report = receipts.receipt(state["event"], "reconcile") - publishing.recover_applied("owner/data", state["root"], report) + publishing.recover_applied( + "owner/data", state["root"], report, installed_source="b" * 40, + ) assert_saved_recovery_verification_order(state) assert state["local_results"][1] == state["local_results"][0] assert report == state["saved"] +def test_recovery_saved_receipt_requires_observed_installed_revision(saved_recovery_transport): + state = saved_recovery_transport + apply(state["root"], state["source_files"]) + report = receipts.receipt(state["event"], "reconcile") + before = copy.deepcopy(report) + # Existing API shape reaches the actual old replay branch too. + with pytest.raises(ValueError, match=r"^stored publication installed source missing$"): + publishing.recover_applied("owner/data", state["root"], report) + assert_saved_recovery_verification_order(state) + assert report == before + + def test_recovery_saved_receipt_keeps_installed_image_mismatch_refusal(saved_recovery_transport): state = saved_recovery_transport manifest = contract.object_json(state["source_files"][contract.MANIFEST]) @@ -2006,6 +2068,409 @@ def test_recovery_verifies_package_before_requiring_local_lock(saved_recovery_tr assert report == before +@pytest.fixture(params=[(987, 1), (1987, 5)], ids=["replay-first", "replay-later"]) +def replay_cli_job(saved_recovery_transport, monkeypatch, request): + state = saved_recovery_transport + apply(state["root"], state["source_files"]) + run_id, attempt = request.param + # Independent adapter envelope and installed revision, unrelated to the stored run 300. + run = { + "id": run_id, "run_attempt": attempt, "head_sha": "e" * 40, + "path": ".github/workflows/pineforge-release.yml", "event": "workflow_run", + } + installed_source = "b" * 40 + stage_name = f"release-stage-{attempt}-10-adapter" + original_api = automation.api + calls = [] + state["installed_bytes"] = state["source_files"][contract.LOCK] + jobs = [{"name": "reconcile", "status": "completed", "conclusion": "success"}] + + def api(repo, endpoint, method="GET", data=None): + calls.append((endpoint, method)) + assert repo == "owner/data" and method == "GET" and data is None + if endpoint == f"actions/runs/{run_id}": + return copy.deepcopy(run) + if endpoint == f"actions/runs/{run_id}/artifacts?per_page=100&page=1": + return {"artifacts": [{"id": 98710, "name": stage_name, "expired": False}]} + if endpoint == f"actions/runs/{run_id}/attempts/{attempt}/jobs?per_page=100&page=1": + return {"jobs": copy.deepcopy(jobs)} + if endpoint == "pulls?state=open&base=main&per_page=100&page=1": + return [] + return original_api(repo, endpoint, method, data) + + def installed_bytes(route, *, accept): + assert route == ( + f"repos/owner/data/contents/{contract.LOCK}?ref={installed_source}" + ) and accept == "application/vnd.github.raw+json" + return state["installed_bytes"] + + def installed_snapshot(revision, destination): + assert revision == installed_source + apply(destination, state["source_files"]) + + monkeypatch.setattr(automation, "api", api) + monkeypatch.setattr(reporting, "api", api) + monkeypatch.setattr(reporting, "gh_bytes", installed_bytes) + monkeypatch.setattr(automation, "git", lambda *args: installed_source) + monkeypatch.setattr(automation, "snapshot", installed_snapshot) + monkeypatch.setattr(automation, "newest_ready", lambda: ( + state["event"], state["source_files"][contract.MANIFEST], + )) + for key, value in { + "GITHUB_REPOSITORY": "owner/data", "GITHUB_RUN_ID": str(run_id), + "GITHUB_RUN_ATTEMPT": str(attempt), "GITHUB_SHA": run["head_sha"], + "RELEASE_BOT_LOGIN": "release[bot]", "FAILED": "false", "STAGE": "reconcile", + "RELEASE_ALERT_OWNER": "release-owner", + "GITHUB_OUTPUT": str(state["root"] / "report-outputs"), + "GITHUB_STEP_SUMMARY": str(state["root"] / "report-summary"), + }.items(): + monkeypatch.setenv(key, value) + event_path = state["root"] / "event.json" + event_path.write_bytes(b"{}") + monkeypatch.setenv("GITHUB_EVENT_PATH", str(event_path)) + produced = state["root"] / "producer" + monkeypatch.setattr(sys, "argv", [ + "release_adapter.py", "reconcile", "--receipt", str(produced / "release-receipt.json"), + ]) + release_adapter.main() + destination = state["root"] / "downloaded-stages" / stage_name + destination.mkdir(parents=True) + for filename in ("release-receipt.json", "producer-outcome.json"): + path = produced / filename + if path.exists(): + (destination / filename).write_bytes(path.read_bytes()) + state.update( + cli_run=run, cli_jobs=jobs, cli_calls=calls, cli_stage=destination, cli_produced=produced, + ) + return state + + +def replay_collect_cli(state, monkeypatch): + monkeypatch.setattr(sys, "argv", [ + "release_chain.reporting", "collect", "--directory", str(state["cli_stage"].parent), + "--receipt", str(state["root"] / "report-release-receipt.json"), + ]) + reporting.main() + + +def test_cli_stored_replay_preserves_original_identity_after_real_verification( + replay_cli_job, monkeypatch +): + state = replay_cli_job + original = receipts.canonical(state["saved"]) + assert (state["cli_stage"] / "release-receipt.json").read_bytes() == original + replay_collect_cli(state, monkeypatch) + assert (state["cli_stage"] / "release-receipt.json").read_bytes() == original + assert receipts.canonical(state["saved"]) == original + assert (state["root"] / "report-outputs").read_text() == "has_event=false\n" + assert not (state["root"] / "report-release-receipt.json").exists() + assert state["reads"].count("https://pypi.org/pypi/pineforge-data/0.3.1/json") == 4 + assert state["processes"] == [] + assert all(method == "GET" for _, method in state["cli_calls"]) + assert "**verified**" in (state["root"] / "report-summary").read_text() + + +@pytest.mark.parametrize( + "status", ["queued", "in_progress", None], ids=["queued", "running", "null"] +) +def test_cli_stored_replay_uses_needs_completion_when_job_api_lags( + replay_cli_job, monkeypatch, status +): + state = replay_cli_job + run = state["cli_run"] + run.update( + event="workflow_dispatch", repository={"full_name": "owner/data"}, + head_repository={"full_name": "owner/data"}, + ) + job = state["cli_jobs"][0] + job.update( + status=status, conclusion=None, run_id=run["id"], + run_attempt=run["run_attempt"], head_sha=run["head_sha"], + ) + state["cli_jobs"].append({ + "name": "authz", "status": "completed", "conclusion": "success", + "run_id": run["id"], "run_attempt": run["run_attempt"], "head_sha": run["head_sha"], + }) + state["cli_jobs"].append({ + "name": "report / report", "status": "in_progress", "conclusion": None, + "run_id": run["id"], "run_attempt": run["run_attempt"], "head_sha": run["head_sha"], + }) + path = state["root"] / "lag-workflow-event.json" + path.write_bytes(contract.json_bytes({ + "repository": {"full_name": "owner/data"}, "ref": "refs/heads/main", + "inputs": {"publication_run": ""}, + })) + monkeypatch.setenv("GITHUB_EVENT_PATH", str(path)) + monkeypatch.setenv("GITHUB_EVENT_NAME", "workflow_dispatch") + monkeypatch.setenv("RELEASE_NEEDS", contract.json_bytes({ + "authz": {"result": "success", "outputs": {}}, + "reconcile": {"result": "success", "outputs": {}}, + }).decode()) + original = (state["cli_stage"] / "release-receipt.json").read_bytes() + replay_collect_cli(state, monkeypatch) + assert (state["cli_stage"] / "release-receipt.json").read_bytes() == original + assert original == receipts.canonical(state["saved"]) + assert (state["root"] / "report-outputs").read_text() == "has_event=false\n" + assert not (state["root"] / "report-release-receipt.json").exists() + assert state["reads"].count("https://pypi.org/pypi/pineforge-data/0.3.1/json") == 4 + assert all(method == "GET" for _, method in state["cli_calls"]) + assert state["processes"] == [] + + +@pytest.mark.parametrize("fault", [ + "missing-witness", "witness-json", "witness-run", "witness-attempt", "witness-head", + "witness-schema", "witness-reason", "witness-digest", "witness-extra", "input-extra", + "snapshot-attempt", "stored-pin", "stored-state", "installed-lock", "installed-version", + "served-bytes", "adapter-attempt", "producer-skipped", +]) +def test_cli_stored_replay_requires_independent_proof(replay_cli_job, monkeypatch, fault): + state = replay_cli_job + stage = state["cli_stage"] + assert (stage / "producer-outcome.json").is_file() + assert (stage / "release-receipt.json").is_file() + if fault == "missing-witness": + # Keep the withheld bytes outside the downloaded stage. + path = stage / "producer-outcome.json" + path.rename(state["root"] / "withheld-producer-outcome.json") + elif fault.startswith("witness-") or fault == "input-extra": + path = stage / "producer-outcome.json" + value = contract.object_json(path.read_bytes()) + if fault in {"witness-run", "witness-attempt"}: + value["run_id" if fault == "witness-run" else "run_attempt"] += 1 + elif fault == "witness-head": + value["head_sha"] = "9" * 40 + elif fault == "witness-schema": + value["schema"] = "pineforge/consumer-receipt/v1" + elif fault == "witness-reason": + value["reason"] = "ordinary-main" + elif fault == "witness-digest": + value["receipt_sha256"] = "0" * 64 + elif fault == "input-extra": + value["inputs"]["ignored"] = True + else: + value["ignored"] = True + path.write_bytes(b"{" if fault == "witness-json" else contract.json_bytes(value)) + elif fault == "snapshot-attempt": + path = stage / "release-receipt.json" + value = contract.object_json(path.read_bytes()) + value["sequence"] += 1 + path.write_bytes(contract.json_bytes(value)) + elif fault == "stored-pin": + state["saved"]["previous"]["facts_sha256"] = "0" * 64 + elif fault == "stored-state": + state["saved"]["state"] = "failed" + elif fault in {"installed-lock", "installed-version"}: + value = contract.object_json(state["installed_bytes"]) + if fault == "installed-lock": + value["event"]["event_id"] = "sha256:" + "0" * 64 + else: + value["package_version"] = "8.9.10" + state["installed_bytes"] = contract.json_bytes(value) + elif fault == "served-bytes": + state["downloads"]["pineforge_data-0.3.1-py3-none-any.whl"] += b"changed" + elif fault == "adapter-attempt": + state["cli_run"]["run_attempt"] += 1 + else: + state["cli_jobs"][0]["conclusion"] = "skipped" + cause = { + "missing-witness": "release-snapshot-attempt-mismatch:", + "witness-json": "release-snapshot-invalid:", + "witness-run": "release-producer-outcome-invalid: envelope or producer job differs", + "witness-attempt": "release-producer-outcome-invalid: envelope or producer job differs", + "witness-head": "release-producer-outcome-invalid: envelope or producer job differs", + "witness-schema": "release-producer-outcome-invalid: envelope or producer job differs", + "witness-reason": "release-producer-outcome-invalid: missing replay proof", + "witness-digest": "release-producer-outcome-invalid: missing replay proof", + "witness-extra": "release-producer-outcome-invalid: envelope or producer job differs", + "input-extra": "release-replay-invalid: installed source/version missing", + "snapshot-attempt": "release-producer-outcome-invalid: missing replay proof", + "stored-pin": "release-replay-invalid: immutable stored receipt differs", + "stored-state": "release-replay-invalid: immutable stored receipt differs", + "installed-lock": "release-replay-invalid: installed target differs", + "installed-version": "release-replay-invalid: installed target differs", + "served-bytes": "PyPI bytes differ", + "adapter-attempt": "release-producer-run-mismatch:", + "producer-skipped": "release-producer-job-mismatch: producer did not execute", + }[fault] + with pytest.raises(ValueError, match="^" + cause): + replay_collect_cli(state, monkeypatch) + assert not (state["root"] / "report-release-receipt.json").exists() + assert not (state["root"] / "report-outputs").exists() + assert not (state["root"] / "report-summary").exists() + assert state["processes"] == [] + assert all(method == "GET" for _, method in state["cli_calls"]) + + +def test_cli_recovery_handoff_uses_real_gates_and_emits_no_new_receipt( + recovery_main_transport, ready, monkeypatch +): + state = recovery_main_transport + event, manifest = ready + root, source = state["root"], state["python"]["head_sha"] + apply(root, contract.update_files(root, event, manifest)) + files = {name: (root / name).read_bytes() for name in contract.UPDATE_FILES} + pr = state["pr"] + pr.update(merged=True) + pr["head"] = {"sha": "a" * 40, "repo": {"full_name": "owner/data"}} + pr["base"] = {"ref": "main", "sha": "b" * 40} + pr["body"] = automation.body({ + "kind": "release", "version": "0.3.1", "event_id": event["event_id"], + "generated_head": "a" * 40, "base": "b" * 40, + }) + candidate, candidate_run, candidate_jobs = green_gate() + candidate["distributions"] = { + "pineforge_data-0.3.1-py3-none-any.whl": contract.sha256(b"candidate wheel"), + "pineforge_data-0.3.1.tar.gz": contract.sha256(b"candidate sdist"), + } + candidate_run["pull_requests"] = [{"number": 7}] + for job in candidate_jobs: + job["completed_at"] = "2026-11-07T11:59:00Z" + publisher = { + "id": 400, "run_attempt": 4, "path": ".github/workflows/release.yml", + "head_sha": source, "event": "workflow_run", "status": "completed", + "conclusion": "success", + } + adapter = { + "id": 987, "run_attempt": 3, "path": ".github/workflows/pineforge-release.yml", + "head_sha": source, "event": "workflow_run", + } + stage = "release-stage-3-10-adapter" + publisher_stage = {"id": 40050, "name": "release-stage-4-50-verify", "expired": False} + saved_stage = contract.object_json((FIXTURES / "sample-release-receipt.json").read_bytes()) + saved_stage.update( + attempt=400, sequence=4, state="deploying", + pr={"number": 7, "base": "main", "base_sha": "b" * 40, + "head": "a" * 40, "merge": source}, + ) + saved_stage["deployed"]["source"] = source + observed_api, proof_bytes = automation.api, evidence.gh_bytes + writes = [] + downloaded = [] + + def api(repo, endpoint, method="GET", data=None): + assert repo == "owner/data" + if method != "GET": + assert endpoint == "actions/workflows/pineforge-release.yml/dispatches" + assert method == "POST" and data == { + "ref": "v0.3.1", "inputs": {"publication_run": "400"}, + } + writes.append((endpoint, copy.deepcopy(data))) + return None + if endpoint == "pulls?state=open&base=main&per_page=100&page=1": + return [] + if endpoint == "pulls/7": + return copy.deepcopy(pr) + if endpoint == "actions/workflows/release.yml/runs?per_page=100": + return {"workflow_runs": [copy.deepcopy(publisher)]} + if endpoint.startswith("actions/workflows/ci.yml/runs?"): + return {"workflow_runs": [copy.deepcopy(candidate_run)]} + if endpoint == "actions/runs/70/attempts/2/jobs?per_page=100&page=1": + return {"jobs": copy.deepcopy(candidate_jobs)} + if endpoint == "actions/runs/400": + return copy.deepcopy(publisher) + if endpoint == "actions/runs/987": + return copy.deepcopy(adapter) + if endpoint == "actions/runs/400/artifacts?per_page=100&page=1": + return {"artifacts": [copy.deepcopy(publisher_stage)]} + if endpoint == "actions/runs/987/artifacts?per_page=100&page=1": + return {"artifacts": copy.deepcopy(downloaded)} + if endpoint == "actions/runs/400/attempts/4/jobs?per_page=100&page=1": + return {"jobs": [ + {"name": name, "status": "completed", "conclusion": "success"} + for name in receipts.DEPLOY_JOBS + ]} + if endpoint == "actions/runs/987/attempts/3/jobs?per_page=100&page=1": + return {"jobs": [{ + "name": "reconcile", "status": "completed", "conclusion": "success", + }]} + return observed_api(repo, endpoint, method, data) + + def archive(name, value): + raw = io.BytesIO() + with zipfile.ZipFile(raw, "w") as zipped: + zipped.writestr(name, contract.json_bytes(value)) + return raw.getvalue() + + def gh_bytes(route, **kwargs): + if route == "repos/owner/data/actions/artifacts/40050/zip": + return archive("release-receipt.json", saved_stage) + path = route.split("?", 1)[0] + if path == "repos/owner/data/actions/runs/70": + return contract.json_bytes(candidate_run) + if path == "repos/owner/data/actions/runs/70/attempts/2/jobs": + return contract.json_bytes({"jobs": candidate_jobs}) + if path == "repos/owner/data/actions/runs/70/artifacts": + return contract.json_bytes({"artifacts": [{ + "id": 7000, "name": "release-evidence-2", "expired": False, + }]}) + if path == "repos/owner/data/actions/artifacts/7000/zip": + return archive("evidence.json", candidate) + if path == f"repos/{contract.HUB}/releases": + return contract.json_bytes([{ + "tag_name": "v" + event["release_version"], "draft": False, + "prerelease": False, "assets": [{"name": "release-manifest.json"}], + }]) + if path == f"repos/{contract.HUB}/releases/tags/v{event['release_version']}": + return contract.json_bytes({ + "draft": False, "prerelease": False, + "assets": [{"id": 81, "name": "release-manifest.json"}], + }) + if path == f"repos/{contract.HUB}/releases/assets/81": + return manifest + return proof_bytes(route, **kwargs) + + def git(*args): + assert args in {("fetch", "origin", "main"), ("rev-parse", "origin/main")} + return source if args[0] == "rev-parse" else "" + + def snapshot(revision, destination): + assert revision == source + apply(destination, files) + + monkeypatch.setattr(automation, "api", api) + monkeypatch.setattr(publishing, "api", api) + monkeypatch.setattr(reporting, "api", api) + monkeypatch.setattr(contract, "gh_bytes", gh_bytes) + monkeypatch.setattr(evidence, "gh_bytes", gh_bytes) + monkeypatch.setattr(reporting, "gh_bytes", gh_bytes) + monkeypatch.setattr(automation, "git", git) + monkeypatch.setattr(automation, "snapshot", snapshot) + for key, value in { + "GITHUB_REPOSITORY": "owner/data", "GITHUB_RUN_ID": "987", + "GITHUB_RUN_ATTEMPT": "3", "GITHUB_SHA": source, "RELEASE_BOT_LOGIN": "release[bot]", + "GITHUB_OUTPUT": str(root / "report-outputs"), + "GITHUB_STEP_SUMMARY": str(root / "report-summary"), "FAILED": "false", + "STAGE": "reconcile", "RELEASE_ALERT_OWNER": "release-owner", + }.items(): + monkeypatch.setenv(key, value) + trigger = root / "trigger.json" + trigger.write_bytes(b"{}") + monkeypatch.setenv("GITHUB_EVENT_PATH", str(trigger)) + produced = root / "producer" + monkeypatch.setattr(sys, "argv", [ + "release_adapter.py", "reconcile", "--receipt", str(produced / "release-receipt.json"), + ]) + release_adapter.main() + destination = root / "downloaded-stages" / stage + for name in ("release-receipt.json", "producer-outcome.json"): + path = produced / name + if path.exists(): + destination.mkdir(parents=True, exist_ok=True) + (destination / name).write_bytes(path.read_bytes()) + if destination.exists(): + downloaded.append({"id": 98710, "name": stage, "expired": False}) + state["cli_stage"] = destination + replay_collect_cli(state, monkeypatch) + assert len(writes) == 1 + assert not (root / "report-release-receipt.json").exists() + assert not (produced / "release-receipt.json").exists() + assert (root / "report-outputs").read_text() == "has_event=false\n" + decision = contract.object_json((produced / "producer-outcome.json").read_bytes()) + assert decision["reason"] == "publication-handoff" + assert decision["inputs"] == {"version": "0.3.1", "publication_run": 400} + assert len(state["validated"]) == 2 # Independent main and pre-merge gates executed. + + def assert_pull_request_filter_policy(workflow: dict[str, Any], name: str) -> None: assert "pull_request" in workflow["on"] assert "pull_request_target" not in workflow["on"] @@ -2230,7 +2695,11 @@ def test_reconciliation_repairs_a_dropped_rollback_publication( ), ) - def recover(repo, source, report): + def recover(repo, source, report, *, installed_source=None): + # Keep this legacy orchestration control usable on c042 as well. + # The real CLI replay controls independently require the recorded revision. + if installed_source is not None: + assert installed_source == "b" * 40 recovered.append(report["event_id"]) report["state"] = "deploying" diff --git a/tests/test_release_reporting.py b/tests/test_release_reporting.py new file mode 100644 index 0000000..2bf2201 --- /dev/null +++ b/tests/test_release_reporting.py @@ -0,0 +1,2310 @@ +"""Offline report-job entry tests; replace transport, keep receipt validation real.""" + +from __future__ import annotations + +import copy +import io +import subprocess +import sys +import zipfile +from pathlib import Path + +import hub_receipt_validator as hub +import pytest +import release_adapter +import yaml +from release_chain import automation, contract, evidence, publishing, receipts, reporting + +FIXTURES = Path(__file__).parent / "fixtures/release_chain" +REPO = "owner/data" +HEAD, BASE, MERGE = "a" * 40, "b" * 40, "c" * 40 + + +def receipt_archive(report): + archive = io.BytesIO() + with zipfile.ZipFile(archive, "w") as zipped: + zipped.writestr("release-receipt.json", receipts.canonical(report)) + return archive.getvalue() + + +class ReportObservations: + """Mutable GitHub responses and retained bytes, never a validation result.""" + + def __init__(self, run_id, attempt): + # These observations are installed before the receipt is loaded or mutated. + # Never derive API answers from the receipt under test. + self.event = contract.object_json((FIXTURES / "ready-event.json").read_bytes()) + self.reads, self.writes, self.downloads = [], [], [] + self.issues, self.releases = [], [] + self.archives, self.assets, self.artifacts = {}, {}, {} + self.bundles = {} + self.open_prs = [] + self.hub_releases = [] + self.pr = { + "number": 7, "merged": True, + "merged_at": "2020-01-01T00:05:00Z", "merge_commit_sha": MERGE, + "base": {"ref": "main", "sha": BASE}, + "head": {"sha": HEAD, "repo": {"full_name": REPO}}, + "body": automation.body({ + "kind": "release", "version": "0.3.1", "generated_head": HEAD, "base": BASE, + }), + } + self.runs, self.jobs = {}, {} + self.attempts, self.job_pages = {}, {} + for observed_id, observed_attempt, workflow, source, names in ( + (70, 2, "ci.yml", HEAD, receipts.CI_JOBS), + (200, attempt + 2, "release.yml", MERGE, receipts.DEPLOY_JOBS), + ): + self.runs[observed_id] = { + "id": observed_id, "run_attempt": observed_attempt, + "path": ".github/workflows/" + workflow, "head_sha": source, + "event": "workflow_run" if observed_id == 200 else "pull_request", + "status": "completed", "conclusion": "success", + } + # Snapshot ordinary facts before receipt loading; later latest-run + # changes must not rewrite the independently installed attempt. + self.attempts[(observed_id, observed_attempt)] = copy.deepcopy(self.runs[observed_id]) + finished = "2020-01-01T00:06:00Z" if observed_id == 200 else "2020-01-01T00:04:00Z" + self.jobs[observed_id] = [ + {"name": name, "status": "completed", "conclusion": "success", + "completed_at": finished} + for name in names + ] + self.reporter = { + "id": run_id, "run_attempt": attempt, + "path": ".github/workflows/" + receipts.WORKFLOW, + "head_sha": MERGE, "event": "workflow_run", + } + self.runs[run_id] = self.reporter + self.jobs[run_id] = [{"name": "reconcile", "status": "completed", "conclusion": "success"}] + self.installed = {} + + def api(self, route): + self.reads.append(route) + path = route.split("?", 1)[0].removeprefix(f"repos/{REPO}/") + if path == "issues": + return copy.deepcopy(self.issues) + if path == "releases": + return copy.deepcopy(self.releases) + if path == "pulls": + return copy.deepcopy(self.open_prs) + if path == f"pulls/{self.pr['number']}": + return copy.deepcopy(self.pr) + if path == f"commits/{self.pr['merge_commit_sha']}": + return {"sha": MERGE, "parents": [{"sha": BASE}]} + if path == f"commits/{BASE}": + return {"sha": BASE, "parents": []} + if path == f"commits/{MERGE}/pulls": + return [] # Independent ordinary-main observation; tests may replace the PR route. + parts = path.split("/") + if path.startswith("actions/runs/"): + run_id = int(parts[2]) + if parts[-1] == "artifacts": + return {"artifacts": copy.deepcopy(self.artifacts.get(run_id, []))} + if "attempts" in parts: + attempt = int(parts[4]) + if parts[-1] != "jobs": + # No fallback to latest: the independently installed attempt + # record can be absent, malformed or older than that run. + return copy.deepcopy(self.attempts.get((run_id, attempt))) + if (run_id, attempt) in self.job_pages: + return copy.deepcopy(self.job_pages[(run_id, attempt)]) + # Explicit historical pages are independent of the latest run. + # Retain the legacy latest-only fallback for older fixtures. + assert attempt == self.runs[run_id]["run_attempt"] + if parts[-1] == "jobs": + return {"jobs": copy.deepcopy(self.jobs[run_id])} + return copy.deepcopy(self.runs[run_id]) + raise AssertionError(f"unregistered GitHub read: {route}") + + def consumer_api(self, repo, endpoint, method="GET", data=None): + assert repo == REPO + if method == "GET": + assert data is None + return self.api(f"repos/{repo}/{endpoint}") + self.writes.append((endpoint, method, copy.deepcopy(data))) + if endpoint == "issues" and method == "POST": + issue = {"number": 41, **copy.deepcopy(data)} + self.issues.append(issue) + return copy.deepcopy(issue) + assert self.issues and endpoint.startswith("issues/41") + if endpoint == "issues/41/comments" and method == "POST": + return {"id": len(self.writes)} + assert endpoint == "issues/41" and method == "PATCH" + self.issues[0].update(copy.deepcopy(data)) + return copy.deepcopy(self.issues[0]) + + def gh_bytes(self, route, **kwargs): + assert set(kwargs) <= {"accept"} + if route.startswith(f"repos/{contract.HUB}/releases?"): + return contract.json_bytes(self.hub_releases) + if route == f"repos/{contract.HUB}/releases/tags/v{self.event['release_version']}": + return contract.json_bytes({ + "draft": False, "prerelease": False, + "assets": [{"id": 81, "name": "release-manifest.json"}], + }) + if route == f"repos/{contract.HUB}/releases/assets/81": + return (FIXTURES / "release-manifest.json").read_bytes() + path = route.removeprefix(f"repos/{REPO}/") + if path.startswith("contents/"): + return self.installed[route] + if path.startswith("actions/artifacts/"): + return self.archives[int(path.split("/")[2])] + if path.startswith("releases/assets/"): + return self.assets[int(path.split("/")[2])] + return contract.json_bytes(self.api(route)) + + def request(self, url, **kwargs): + assert kwargs == {"github": True, "accept": "application/octet-stream"} + prefix = f"https://api.github.com/repos/{REPO}/actions/artifacts/" + assert url.startswith(prefix) and url.endswith("/zip") + return self.archives[int(url.removeprefix(prefix).split("/")[0])] + + def retain(self, report): + self.releases = [{ + "tag_name": "v0.3.1", + "assets": [{"id": 98, "name": "release-receipt.json"}], + }] + self.assets[98] = receipts.canonical(report) + + def download(self, command, *, check): + assert check is True and command[:3] == ["gh", "run", "download"] + publisher = 200 + assert command[3:7] == [str(publisher), "--repo", REPO, "--name"] + assert command[8] == "--dir" and len(command) == 10 + name = command[7] + self.downloads.append(name) + if name not in self.bundles: + raise subprocess.CalledProcessError(1, command, stderr=b"artifact not found") + directory = Path(command[9]) + for filename, raw in self.bundles[name].items(): + path = directory / filename + path.parent.mkdir(parents=True, exist_ok=True) + path.write_bytes(raw) + + +@pytest.fixture(params=[(930, 2), (1930, 11)], ids=["attempt-2", "attempt-11"]) +def report_job(tmp_path, monkeypatch, request): + run_id, attempt = request.param + observed = ReportObservations(run_id, attempt) + report = contract.object_json((FIXTURES / "sample-release-receipt.json").read_bytes()) + report.update(attempt=run_id, sequence=attempt) + report["deploy"]["run_attempt"] = attempt + 2 + for key, value in { + "GITHUB_REPOSITORY": REPO, "GITHUB_RUN_ID": str(run_id), + "GITHUB_RUN_ATTEMPT": str(attempt), "FAILED": "false", "STAGE": "publication", + "GITHUB_SHA": MERGE, + "RELEASE_ALERT_OWNER": "release-owner", + "GITHUB_OUTPUT": str(tmp_path / "outputs"), + "GITHUB_STEP_SUMMARY": str(tmp_path / "summary"), + }.items(): + monkeypatch.setenv(key, value) + monkeypatch.setattr(automation, "api", observed.consumer_api) + monkeypatch.setattr(publishing, "api", observed.consumer_api) + monkeypatch.setattr(reporting, "api", observed.consumer_api) + monkeypatch.setattr(contract, "gh_bytes", observed.gh_bytes) + monkeypatch.setattr(evidence, "gh_bytes", observed.gh_bytes) + monkeypatch.setattr(publishing, "gh_bytes", observed.gh_bytes) + monkeypatch.setattr(reporting, "gh_bytes", observed.gh_bytes) + monkeypatch.setattr(reporting.subprocess, "run", observed.download) + return { + "root": tmp_path, "report": report, "observed": observed, + "event": contract.object_json((FIXTURES / "ready-event.json").read_bytes()), + "policy": contract.object_json((FIXTURES / "receipt-policy.json").read_bytes())["data"], + } + + +def register_stage(job, attempt, suffix): + observed = job["observed"] + run_id = observed.reporter["id"] + name = f"release-stage-{attempt}-{suffix}" + rows = observed.artifacts.setdefault(run_id, []) + if not any(row["name"] == name for row in rows): + rows.append({"id": len(rows) + 50000, "name": name, "expired": False}) + return job["root"] / "receipts" / name + + +def write_stage(job, *, report=None, attempt=None, suffix="10-adapter"): + if report is None: + report = job["report"] + if attempt is None: + attempt = job["observed"].reporter["run_attempt"] + path = register_stage(job, attempt, suffix) / "release-receipt.json" + path.parent.mkdir(parents=True, exist_ok=True) + path.write_bytes(contract.json_bytes(report)) + return path + + +def run_report(job, monkeypatch, command="collect"): + monkeypatch.setattr(sys, "argv", [ + "release_chain.reporting", command, + "--directory", str(job["root"] / "receipts"), + "--receipt", str(job["root"] / "release-receipt.json"), + ]) + reporting.main() + + +def assert_no_delivery(job): + assert not (job["root"] / "release-receipt.json").exists() + assert not (job["root"] / "outputs").exists() + assert not (job["root"] / "summary").exists() + assert job["observed"].writes == job["observed"].downloads == [] + + +def ship_producer(job, directory, suffix): + """Model upload/download: an empty producer directory creates no artifact.""" + names = ("release-receipt.json", "producer-outcome.json") + files = [directory / name for name in names if (directory / name).exists()] + if not files: + return None + destination = register_stage(job, job["observed"].reporter["run_attempt"], suffix) + destination.mkdir(parents=True, exist_ok=True) + for path in files: + (destination / path.name).write_bytes(path.read_bytes()) + return destination + + +def archive_stage(directory): + archive = io.BytesIO() + with zipfile.ZipFile(archive, "w") as zipped: + for path in sorted(directory.iterdir()): + zipped.writestr(path.name, path.read_bytes()) + return archive.getvalue() + + +def ordinary_producer(job, monkeypatch, workflow="ci-post.yml"): + observed = job["observed"] + observed.reporter["path"] = ".github/workflows/release.yml" + observed.jobs[observed.reporter["id"]] = [ + {"name": "prepare", "status": "completed", "conclusion": "success"}, + ] + observed.runs[810] = { + "id": 810, "run_attempt": 3, "path": ".github/workflows/" + workflow, + "event": "push", "head_branch": "main", "head_sha": MERGE, + "status": "completed", "conclusion": "success", + } + directory = job["root"] / "producer" + with monkeypatch.context() as producer: + producer.setenv("GITHUB_EVENT_NAME", "workflow_run") + producer.setenv("GITHUB_OUTPUT", str(job["root"] / "producer-outputs")) + producer.setattr(sys, "argv", [ + "release_chain.publishing", "prepare", "--run", "810", + "--directory", str(job["root"] / "bundle"), + "--receipt", str(directory / "release-receipt.json"), + ]) + publishing.main() + return directory + + +def assert_silent_noop(job): + assert not (job["root"] / "release-receipt.json").exists() + assert (job["root"] / "outputs").read_text() == "has_event=false\n" + assert job["observed"].writes == job["observed"].downloads == [] + assert job["observed"].issues == [] + + +def bound_job_facts(run, facts): + """Raw attempt-job transport facts, independent of receipts and CLI results.""" + return [ + {"name": name, "status": "completed", "conclusion": result, + "run_id": run["id"], "run_attempt": run["run_attempt"], "head_sha": run["head_sha"]} + for name, result in facts + ] + + +def active_report_job(run): + return { + "name": "report / report", "status": "in_progress", "conclusion": None, + "run_id": run["id"], "run_attempt": run["run_attempt"], "head_sha": run["head_sha"], + } + + +def trusted_call(job, monkeypatch, needs, event): + run = job["observed"].reporter + path = job["root"] / f"workflow-event-{run['id']}.json" + path.write_bytes(contract.json_bytes(event)) + for key, value in { + "GITHUB_RUN_ID": str(run["id"]), "GITHUB_RUN_ATTEMPT": str(run["run_attempt"]), + "GITHUB_SHA": run["head_sha"], "GITHUB_EVENT_NAME": run["event"], + "GITHUB_EVENT_PATH": str(path), "RELEASE_NEEDS": contract.json_bytes(needs).decode(), + }.items(): + monkeypatch.setenv(key, value) + return { + "repository": REPO, "run_id": run["id"], "run_attempt": run["run_attempt"], + "head_sha": run["head_sha"], "event_name": run["event"], + "event": copy.deepcopy(event), "needs": copy.deepcopy(needs), + } + + +def no_producer_facts(job, monkeypatch, origin): + observed = job["observed"] + run = observed.reporter + run.update( + path=".github/workflows/release.yml", name="Release", head_branch="main", + event="workflow_dispatch" if origin == "claim" else "workflow_run", + repository={"full_name": REPO}, head_repository={"full_name": REPO}, + ) + needs = { + "authz": {"result": "success", "outputs": {}}, + "claim-name": {"result": "success" if origin == "claim" else "skipped", "outputs": {}}, + "manual-pr": {"result": "skipped", "outputs": {}}, + "prepare": {"result": "skipped", "outputs": {}}, + "build": {"result": "skipped", "outputs": {}}, + "github-release": {"result": "skipped", "outputs": {}}, + "publish": {"result": "skipped", "outputs": {}}, + "verify": {"result": "skipped", "outputs": {}}, + } + # These API facts are installed separately from needs; conflict controls + # mutate only one side. Nothing is copied from a production receipt/verdict. + observed.jobs[run["id"]] = [ + *bound_job_facts(run, [ + ("authz", "success"), ("claim-name", "success" if origin == "claim" else "skipped"), + ("manual-pr", "skipped"), ("prepare", "skipped"), ("build", "skipped"), + ("github-release", "skipped"), ("publish", "skipped"), ("verify", "skipped"), + ]), + active_report_job(run), + ] + if origin == "claim": + event = { + "repository": {"full_name": REPO}, "ref": "refs/heads/main", + "inputs": {"bump": "patch", "ci_run": "", "rollback_package": "", + "claim_placeholder": "true"}, + } + else: + upstream = { + "id": 810, "run_attempt": 3, "head_sha": MERGE, "head_branch": "main", + "name": "CI" if origin == "ci" else "Native feed", + "path": ".github/workflows/" + ( + "ci-post.yml" if origin == "ci" else "native-feed-post.yml" + ), + "event": "workflow_dispatch", "status": "completed", "conclusion": "success", + "repository": {"full_name": REPO}, "head_repository": {"full_name": REPO}, + } + observed.runs[810] = copy.deepcopy(upstream) + observed.attempts[(810, 3)] = copy.deepcopy(upstream) + event = {"repository": {"full_name": REPO}, "action": "completed", "workflow_run": upstream} + return trusted_call(job, monkeypatch, needs, event) + + +def retain_context_facts(job, context): + """Install raw workflow data at the real single-file artifact boundary.""" + run = job["observed"].reporter + name = f"release-run-context-{run['run_attempt']}" + row = {"id": 62000, "name": name, "expired": False} + job["observed"].artifacts.setdefault(run["id"], []).append(row) + archive = io.BytesIO() + with zipfile.ZipFile(archive, "w") as zipped: + zipped.writestr("run-context.json", contract.json_bytes(context)) + job["observed"].archives[row["id"]] = archive.getvalue() + return row + + +def adapter_call(job, monkeypatch, publisher=None, *, failed=False): + observed = job["observed"] + if publisher is not None: + observed.reporter = { + "id": 3100, "run_attempt": 7, "head_sha": MERGE, + "path": ".github/workflows/pineforge-release.yml", "event": "workflow_run", + } + observed.runs[3100] = observed.reporter + run = observed.reporter + run.update(repository={"full_name": REPO}, head_repository={"full_name": REPO}) + if publisher is None: + run["event"] = "workflow_dispatch" + event = {"repository": {"full_name": REPO}, "ref": "refs/heads/main", + "inputs": {"publication_run": ""}} + else: + event = {"repository": {"full_name": REPO}, "action": "completed", + "workflow_run": copy.deepcopy(publisher)} + observed.attempts[(publisher["id"], publisher["run_attempt"])] = copy.deepcopy(publisher) + needs = { + "authz": {"result": "success", "outputs": {}}, + "reconcile": {"result": "failure" if failed else "success", "outputs": {}}, + } + observed.jobs[run["id"]] = [ + *bound_job_facts(run, [ + ("authz", "success"), ("reconcile", "failure" if failed else "success"), + ]), + active_report_job(run), + ] + monkeypatch.setenv("FAILED", str(failed).lower()) + return trusted_call(job, monkeypatch, needs, event) + + +@pytest.mark.parametrize("origin", ["ci", "native", "claim"]) +@pytest.mark.parametrize("inventory", ["empty", "unrelated"]) +def test_cli_no_producer_current_event_is_silent(report_job, monkeypatch, origin, inventory): + context = no_producer_facts(report_job, monkeypatch, origin) + if inventory == "unrelated": + report_job["observed"].artifacts[context["run_id"]] = [ + {"id": 63000, "name": "unrelated-build-data", "expired": False}, + ] + run_report(report_job, monkeypatch) + assert_silent_noop(report_job) + assert contract.object_json((report_job["root"] / "run-context.json").read_bytes()) == context + assert not (report_job["root"] / "producer-outcome.json").exists() + + +def test_cli_claim_placeholder_preserves_unset_optional_inputs(report_job, monkeypatch): + job = report_job + context = no_producer_facts(job, monkeypatch, "claim") + context["event"]["inputs"].update(ci_run=None, rollback_package=None) + trusted_call(job, monkeypatch, context["needs"], context["event"]) + run_report(job, monkeypatch) + assert_silent_noop(job) + assert contract.object_json((job["root"] / "run-context.json").read_bytes()) == context + + +@pytest.mark.parametrize("origin", ["ci", "native", "claim"]) +def test_cli_no_producer_publication_and_adapter_recheck_raw_context( + report_job, monkeypatch, origin +): + job = report_job + context = no_producer_facts(job, monkeypatch, origin) + publisher = job["observed"].reporter + publisher.update(status="completed", conclusion="success") + retain_context_facts(job, context) + publisher_id = publisher["id"] + adapter_call(job, monkeypatch, publisher) + produced = job["root"] / "no-producer-adapter" + monkeypatch.setattr(sys, "argv", [ + "release_chain.reporting", "publication", "--run", str(publisher_id), + "--receipt", str(produced / "release-receipt.json"), + ]) + reporting.main() + witness = produced / "producer-outcome.json" + assert witness.is_file() + value = contract.object_json(witness.read_bytes()) + assert value["reason"] == "publisher-no-producer" + assert value["inputs"]["publication_run"] == publisher_id + assert value["inputs"]["context_sha256"] == contract.sha256(receipts.canonical(context)) + assert ship_producer(job, produced, "10-adapter") is not None + run_report(job, monkeypatch) + assert_silent_noop(job) + assert not (produced / "release-receipt.json").exists() + assert sum(f"/actions/runs/{publisher_id}/artifacts" in route + for route in job["observed"].reads) == 2 + + +def publication_cli(job, monkeypatch, publisher_id): + produced = job["root"] / "publication-producer" + monkeypatch.setattr(sys, "argv", [ + "release_chain.reporting", "publication", "--run", str(publisher_id), + "--directory", str(job["root"] / "receipts"), + "--receipt", str(produced / "release-receipt.json"), + ]) + reporting.main() + return produced + + +@pytest.mark.parametrize("origin", ["ci", "native"]) +@pytest.mark.parametrize("entry", ["report", "publisher-and-adapter"]) +def test_cli_original_trigger_uses_recorded_attempt_after_rerun( + report_job, monkeypatch, origin, entry +): + job = report_job + context = no_producer_facts(job, monkeypatch, origin) + observed = job["observed"] + original = context["event"]["workflow_run"] + original["run_attempt"] = 1 + observed.attempts[(810, 1)] = copy.deepcopy(original) + # Independent latest-run response: it is deliberately still readable by + # exact85, so its RED is the real "checked upstream differs" refusal. + observed.runs[810].update(run_attempt=2, status="in_progress", conclusion=None) + trusted_call(job, monkeypatch, context["needs"], context["event"]) + assert observed.attempts[(810, 1)]["status"] == "completed" + assert observed.runs[810]["run_attempt"] == 2 + if entry == "report": + run_report(job, monkeypatch) + else: + publisher = observed.reporter + publisher.update(status="completed", conclusion="success") + retain_context_facts(job, context) + adapter_call(job, monkeypatch, publisher) + produced = publication_cli(job, monkeypatch, publisher["id"]) + assert not (produced / "release-receipt.json").exists() + assert ship_producer(job, produced, "10-adapter") is not None + run_report(job, monkeypatch) + assert_silent_noop(job) + assert f"repos/{REPO}/actions/runs/810/attempts/1" in observed.reads + assert f"repos/{REPO}/actions/runs/810" not in observed.reads + + +@pytest.mark.parametrize("damage,cause", [ + ("missing", "malformed checked upstream"), + ("list", "malformed checked upstream"), + ("attempt-missing", "malformed checked upstream"), + ("attempt-type", "malformed checked upstream"), + ("id", "checked upstream differs"), + ("attempt", "checked upstream differs"), + ("source", "checked upstream differs"), + ("path", "checked upstream differs"), + ("name", "checked upstream differs"), + ("event", "checked upstream differs"), + ("branch", "checked upstream differs"), + ("repository", "checked upstream differs"), + ("head-repository", "checked upstream differs"), + ("incomplete", "checked upstream differs"), + ("conclusion", "checked upstream differs"), + ("partial", "checked upstream differs"), +]) +def test_cli_original_trigger_requires_independent_specific_attempt( + report_job, monkeypatch, damage, cause +): + job = report_job + no_producer_facts(job, monkeypatch, "ci") + observed = job["observed"] + exact = observed.attempts[(810, 3)] + assert exact == observed.runs[810] and exact is not observed.runs[810] + if damage in {"missing", "list"}: + observed.attempts[(810, 3)] = None if damage == "missing" else [] + elif damage in {"attempt-missing", "partial"}: + exact.pop("run_attempt" if damage == "attempt-missing" else "path") + elif damage == "attempt-type": + exact["run_attempt"] = "3" + elif damage == "incomplete": + exact.update(status="in_progress", conclusion=None) + elif damage in {"repository", "head-repository"}: + exact[damage.replace("-", "_")] = {"full_name": "outside/data"} + else: + field, value = { + "id": ("id", 811), "attempt": ("run_attempt", 4), "source": ("head_sha", HEAD), + "path": ("path", ".github/workflows/native-feed-post.yml"), + "name": ("name", "Native feed"), "event": ("event", "push"), + "branch": ("head_branch", "other"), "conclusion": ("conclusion", "failure"), + }[damage] + exact[field] = value + with pytest.raises(ValueError, match="^release-run-context-invalid: " + cause): + run_report(job, monkeypatch) + assert_no_delivery(job) + assert f"repos/{REPO}/actions/runs/810/attempts/3" in observed.reads + assert f"repos/{REPO}/actions/runs/810" not in observed.reads + + +@pytest.mark.parametrize("trigger", ["pr-ci", "hub-dispatch"]) +def test_cli_adapter_context_covers_real_trigger_and_running_report_row( + report_job, monkeypatch, trigger +): + job = report_job + context = adapter_call(job, monkeypatch) + observed, run = job["observed"], job["observed"].reporter + if trigger == "pr-ci": + upstream = { + "id": 70, "run_attempt": 1, "name": "CI", "path": ".github/workflows/ci.yml", + "event": "pull_request", "head_branch": "bot/pineforge-release/main/stable/2.0.0", + "head_sha": HEAD, "status": "completed", "conclusion": "success", + "repository": {"full_name": REPO}, "head_repository": {"full_name": REPO}, + } + observed.attempts[(70, 1)] = copy.deepcopy(upstream) + observed.runs[70] = { + **copy.deepcopy(upstream), "run_attempt": 2, "status": "in_progress", + "conclusion": None, + } + run["event"] = "workflow_run" + event = {"repository": {"full_name": REPO}, "action": "completed", "workflow_run": upstream} + else: + run["event"] = "repository_dispatch" + event = {"repository": {"full_name": REPO}, "action": "pineforge-release-ready", + "client_payload": copy.deepcopy(observed.event)} + trusted_call(job, monkeypatch, context["needs"], event) + assert observed.jobs[run["id"]][-1] == active_report_job(run) + write_stage(job) + original = receipts.canonical(job["report"]) + run_report(job, monkeypatch) + assert (job["root"] / "release-receipt.json").read_bytes() == original + assert (job["root"] / "outputs").read_text() == "has_event=true\n" + if trigger == "pr-ci": + assert f"repos/{REPO}/actions/runs/70/attempts/1" in observed.reads + assert f"repos/{REPO}/actions/runs/70" not in observed.reads + # Collection scope: the staged receipt is a fixed fixture. The real + # reconcile/gates, replay and repair controls remain separate obligations. + + +def cancelled_publisher_facts( + job, monkeypatch, trigger="workflow_run", report_trigger="workflow_run" +): + observed = job["observed"] + publisher = { + "id": 200, "run_attempt": observed.runs[200]["run_attempt"], "head_sha": MERGE, + "path": ".github/workflows/release.yml", "name": "Release", "head_branch": "main", + "event": trigger, "status": "completed", "conclusion": "cancelled", + "repository": {"full_name": REPO}, "head_repository": {"full_name": REPO}, + } + observed.runs[200] = publisher + observed.jobs[200] = [] + # The count is an explicit transport observation, never len(jobs), a + # missing-key default, or a product/expected-receipt decision. + observed.job_pages[(200, publisher["run_attempt"])] = {"total_count": 0, "jobs": []} + observed.artifacts[200] = [] + context = adapter_call(job, monkeypatch, publisher) + if report_trigger == "workflow_dispatch": + observed.reporter["event"] = report_trigger + trusted_call(job, monkeypatch, context["needs"], { + "repository": {"full_name": REPO}, "ref": "refs/heads/main", + "inputs": {"publication_run": "200"}, + }) + return publisher + + +@pytest.mark.parametrize("trigger", ["workflow_run", "workflow_dispatch"]) +@pytest.mark.parametrize("report_trigger", ["workflow_run", "workflow_dispatch"]) +def test_cli_cancelled_before_execution_is_quiet_only_after_provider_recheck( + report_job, monkeypatch, trigger, report_trigger +): + job = report_job + publisher = cancelled_publisher_facts(job, monkeypatch, trigger, report_trigger) + produced = publication_cli(job, monkeypatch, publisher["id"]) + path = produced / "producer-outcome.json" + assert path.is_file() and not (produced / "release-receipt.json").exists() + witness = contract.object_json(path.read_bytes()) + assert witness["reason"] == "publisher-cancelled-before-execution" + assert witness["inputs"] == { + "publication_run": 200, "publication_attempt": publisher["run_attempt"], "source": MERGE, + } + assert witness["receipt_sha256"] is None + assert witness["run_id"] == 3100 and witness["run_attempt"] == 7 + assert ship_producer(job, produced, "10-adapter") is not None + before = list(job["observed"].reads) + run_report(job, monkeypatch) + assert_silent_noop(job) + reads = job["observed"].reads[len(before):] + exact = f"repos/{REPO}/actions/runs/200/attempts/{publisher['run_attempt']}" + assert exact in before and exact in reads + assert exact + "/jobs?per_page=100&page=1" in reads + assert f"repos/{REPO}/actions/runs/200/artifacts?per_page=100&page=1" in reads + assert set(job["observed"].job_pages[(200, publisher["run_attempt"])]) == { + "total_count", "jobs", + } + assert not any("supersed" in str(value) for value in witness.values()) + + +def original_cancelled_attempt_witness(job, monkeypatch, *, report_trigger="workflow_run"): + """Produce a real witness over separately authored event/run/attempt facts.""" + observed = job["observed"] + original = { + "id": 200, "run_attempt": 1, "head_sha": MERGE, + "path": ".github/workflows/release.yml", "name": "Release", "head_branch": "main", + "event": "workflow_run", "status": "completed", "conclusion": "cancelled", + "repository": {"full_name": REPO}, "head_repository": {"full_name": REPO}, + } + historical = { + "id": 200, "run_attempt": 1, "head_sha": MERGE, + "path": ".github/workflows/release.yml", "name": "Release", "head_branch": "main", + "event": "workflow_run", "status": "completed", "conclusion": "cancelled", + "repository": {"full_name": REPO}, "head_repository": {"full_name": REPO}, + } + observed.runs[200] = { + "id": 200, "run_attempt": 1, "head_sha": MERGE, + "path": ".github/workflows/release.yml", "name": "Release", "head_branch": "main", + "event": "workflow_run", "status": "completed", "conclusion": "cancelled", + "repository": {"full_name": REPO}, "head_repository": {"full_name": REPO}, + } + context = adapter_call(job, monkeypatch, original) + # Replace adapter_call's convenience copy before any product call. This + # historical API response is authored independently of the event and latest. + observed.attempts[(200, 1)] = historical + observed.jobs[200] = [] + observed.job_pages[(200, 1)] = {"total_count": 0, "jobs": []} + observed.artifacts[200] = [] + if report_trigger == "workflow_dispatch": + observed.reporter["event"] = report_trigger + context = trusted_call(job, monkeypatch, context["needs"], { + "repository": {"full_name": REPO}, "ref": "refs/heads/main", + "inputs": {"publication_run": "200"}, + }) + assert original == historical == observed.runs[200] + assert original is not historical and historical is not observed.runs[200] + assert observed.job_pages[(200, 1)] == {"total_count": 0, "jobs": []} + assert type(observed.job_pages[(200, 1)]["total_count"]) is int + assert observed.jobs[200] == observed.artifacts[200] == [] + produced = publication_cli(job, monkeypatch, 200) + path = produced / "producer-outcome.json" + assert path.is_file() and not (produced / "release-receipt.json").exists() + witness = contract.object_json(path.read_bytes()) + assert witness["reason"] == "publisher-cancelled-before-execution" + assert witness["inputs"] == { + "publication_run": 200, "publication_attempt": 1, "source": MERGE, + } + assert witness["run_id"] == 3100 and witness["run_attempt"] == 7 + assert witness["receipt_sha256"] is None + stage = ship_producer(job, produced, "10-adapter") + assert stage is not None and (stage / "producer-outcome.json").is_file() + assert not (stage / "release-receipt.json").exists() + return context, historical, stage + + +def advance_publisher_to_attempt_two(job, status, conclusion): + """Install raw latest facts and populated jobs, independent of the witness.""" + observed = job["observed"] + latest = { + "id": 200, "run_attempt": 2, "head_sha": HEAD, + "path": ".github/workflows/release.yml", "name": "Release", "head_branch": "main", + "event": "workflow_run", "status": status, "conclusion": conclusion, + "repository": {"full_name": REPO}, "head_repository": {"full_name": REPO}, + } + jobs = bound_job_facts(latest, [ + ("authz", "success"), ("claim-name", "skipped"), ("manual-pr", "skipped"), + ("prepare", "success"), ("build", "success"), ("github-release", "success"), + ("publish", "success"), ("verify", "success"), ("report / report", "success"), + ]) + if status == "in_progress": + jobs[-1].update(status="in_progress", conclusion=None) + elif conclusion == "cancelled": + jobs[-1]["conclusion"] = "cancelled" + observed.runs[200] = latest + observed.jobs[200] = jobs + observed.job_pages[(200, 2)] = {"total_count": 9, "jobs": copy.deepcopy(jobs)} + assert observed.attempts[(200, 1)]["run_attempt"] == 1 + assert observed.attempts[(200, 1)]["conclusion"] == "cancelled" + assert observed.job_pages[(200, 1)] == {"total_count": 0, "jobs": []} + assert observed.job_pages[(200, 2)]["total_count"] == 9 and jobs + return latest + + +@pytest.mark.parametrize("latest_status,latest_conclusion", [ + ("in_progress", None), ("completed", "success"), +]) +def test_cli_original_cancelled_witness_survives_publisher_rerun( + report_job, monkeypatch, latest_status, latest_conclusion +): + job = report_job + context, historical, stage = original_cancelled_attempt_witness(job, monkeypatch) + observed = job["observed"] + event_before = receipts.canonical(context["event"]) + historical_before = receipts.canonical(historical) + witness_before = (stage / "producer-outcome.json").read_bytes() + latest = advance_publisher_to_attempt_two(job, latest_status, latest_conclusion) + assert context["event"]["workflow_run"]["run_attempt"] == 1 + assert historical is observed.attempts[(200, 1)] and historical is not latest + assert historical["head_sha"] == MERGE and latest["head_sha"] == HEAD + assert observed.artifacts[200] == [] + before = len(observed.reads) + run_report(job, monkeypatch) + assert_silent_noop(job) + reads = observed.reads[before:] + exact = f"repos/{REPO}/actions/runs/200/attempts/1" + assert exact in reads and exact + "/jobs?per_page=100&page=1" in reads + assert f"repos/{REPO}/actions/runs/200/artifacts?per_page=100&page=1" in reads + assert f"repos/{REPO}/actions/runs/200" not in reads + assert not any("/actions/runs/200/attempts/2" in route for route in reads) + assert (stage / "producer-outcome.json").read_bytes() == witness_before + assert receipts.canonical(context["event"]) == event_before + assert receipts.canonical(observed.attempts[(200, 1)]) == historical_before + assert contract.object_json((job["root"] / "run-context.json").read_bytes()) == context + + +@pytest.mark.parametrize("damage,cause", [ + ("missing-record", "release-run-context-invalid: malformed checked upstream"), + ("malformed-record", "release-run-context-invalid: malformed checked upstream"), + ("missing-attempt", "release-run-context-invalid: malformed checked upstream"), + ("wrong-id", "release-run-context-invalid: checked upstream differs"), + ("wrong-attempt", "release-run-context-invalid: checked upstream differs"), + ("wrong-source", "release-run-context-invalid: checked upstream differs"), + ("missing-path", "release-run-context-invalid: checked upstream differs"), + ("incomplete-record", "release-run-context-invalid: checked upstream differs"), + ("wrong-conclusion", "release-run-context-invalid: checked upstream differs"), + ("wrong-repository", "release-run-context-invalid: checked upstream differs"), + ( + "missing-count", + "release-producer-outcome-invalid: cancelled publisher lacks complete zero-job inventory", + ), + ( + "partial-count", + "release-producer-outcome-invalid: cancelled publisher lacks complete zero-job inventory", + ), + ( + "boolean-count", + "release-producer-outcome-invalid: cancelled publisher lacks complete zero-job inventory", + ), + ( + "executed", + "release-producer-outcome-invalid: cancelled publisher lacks complete zero-job inventory", + ), + ("later-stage", "release-producer-outcome-invalid: cancelled publisher has evidence"), +]) +def test_cli_original_cancelled_witness_requires_historical_proof_after_rerun( + report_job, monkeypatch, damage, cause +): + job = report_job + context, historical, stage = original_cancelled_attempt_witness(job, monkeypatch) + observed = job["observed"] + advance_publisher_to_attempt_two(job, "completed", "success") + assert context["event"]["workflow_run"] == historical + assert (stage / "producer-outcome.json").is_file() + page = observed.job_pages[(200, 1)] + assert page == {"total_count": 0, "jobs": []} + if damage in {"missing-record", "malformed-record"}: + observed.attempts[(200, 1)] = None if damage == "missing-record" else [] + elif damage in {"missing-attempt", "missing-path"}: + historical.pop("run_attempt" if damage == "missing-attempt" else "path") + elif damage in { + "wrong-id", "wrong-attempt", "wrong-source", "incomplete-record", + "wrong-conclusion", "wrong-repository", + }: + field, value = { + "wrong-id": ("id", 201), "wrong-attempt": ("run_attempt", 2), + "wrong-source": ("head_sha", HEAD), "incomplete-record": ("status", "in_progress"), + "wrong-conclusion": ("conclusion", "success"), + "wrong-repository": ("repository", {"full_name": "outside/data"}), + }[damage] + historical[field] = value + elif damage == "missing-count": + page.pop("total_count") + elif damage in {"partial-count", "boolean-count"}: + page["total_count"] = 1 if damage == "partial-count" else False + elif damage == "executed": + page.update(total_count=1, jobs=bound_job_facts(historical, [("authz", "failure")])) + else: + assert damage == "later-stage" + observed.artifacts[200].append({ + "id": 61003, "name": "release-stage-2-20-prepare", "expired": False, + }) + observed.archives[61003] = receipt_archive(job["report"]) + before = len(observed.reads) + with pytest.raises(ValueError, match="^" + cause + "$"): + run_report(job, monkeypatch) + assert_no_delivery(job) + reads = observed.reads[before:] + assert f"repos/{REPO}/actions/runs/200/attempts/1" in reads + assert f"repos/{REPO}/actions/runs/200" not in reads + assert not any("/actions/runs/200/attempts/2" in route for route in reads) + assert context["event"]["workflow_run"]["run_attempt"] == 1 + assert context["event"]["workflow_run"]["conclusion"] == "cancelled" + assert observed.runs[200]["run_attempt"] == 2 + assert not (job["root"] / "publication-producer" / "release-receipt.json").exists() + + +@pytest.mark.parametrize("movement", ["running", "successful", "cancelled", "source"]) +def test_cli_manual_cancelled_witness_still_selects_latest_publisher( + report_job, monkeypatch, movement +): + job = report_job + context, historical, stage = original_cancelled_attempt_witness( + job, monkeypatch, report_trigger="workflow_dispatch", + ) + observed = job["observed"] + historical_before = receipts.canonical(historical) + assert context["event"]["inputs"] == {"publication_run": "200"} + assert (stage / "producer-outcome.json").is_file() + if movement == "source": + observed.runs[200]["head_sha"] = HEAD + else: + status, conclusion = { + "running": ("in_progress", None), "successful": ("completed", "success"), + "cancelled": ("completed", "cancelled"), + }[movement] + advance_publisher_to_attempt_two(job, status, conclusion) + assert observed.job_pages[(200, 1)] == {"total_count": 0, "jobs": []} + assert historical["head_sha"] == MERGE and observed.runs[200]["head_sha"] == HEAD + before = len(observed.reads) + with pytest.raises( + ValueError, match=r"^release-producer-outcome-invalid: cancelled publisher inputs differ$", + ): + run_report(job, monkeypatch) + assert_no_delivery(job) + reads = observed.reads[before:] + assert f"repos/{REPO}/actions/runs/200" in reads + assert not any("/actions/runs/200/attempts/" in route for route in reads) + assert receipts.canonical(observed.attempts[(200, 1)]) == historical_before + + +@pytest.mark.parametrize("damage,cause", [ + ("missing-count", "cancelled publisher lacks complete zero-job inventory"), + ("partial-count", "cancelled publisher lacks complete zero-job inventory"), + ("string-count", "cancelled publisher lacks complete zero-job inventory"), + ("boolean-count", "cancelled publisher lacks complete zero-job inventory"), + ("executed", "cancelled publisher lacks complete zero-job inventory"), + ("authz-refused", "cancelled publisher lacks complete zero-job inventory"), + ("contradictory-jobs", "cancelled publisher lacks complete zero-job inventory"), + ("attempt-missing", "cancelled publisher attempt differs"), + ("attempt", "cancelled publisher attempt differs"), + ("attempt-source", "cancelled publisher attempt differs"), + ("attempt-repository", "cancelled publisher attempt differs"), + ("attempt-status", "cancelled publisher attempt differs"), + ("repository", "cancelled publisher binding differs"), + ("head-repository", "cancelled publisher binding differs"), + ("branch", "cancelled publisher binding differs"), + ("workflow-name", "cancelled publisher binding differs"), + ("bundle-evidence", "cancelled publisher has evidence"), + ("downloaded", "cancelled publisher has downloaded evidence"), + ("downloaded-receipt", "cancelled publisher has downloaded evidence"), +]) +@pytest.mark.parametrize("boundary", ["publication", "adapter-recheck"]) +def test_cli_cancelled_before_execution_refuses_missing_or_conflicting_proof( + report_job, monkeypatch, damage, cause, boundary +): + job = report_job + # A manual adapter input isolates the cancellation recheck. A workflow-run + # context would independently reject damaged attempt data even earlier. + publisher = cancelled_publisher_facts( + job, monkeypatch, + report_trigger="workflow_dispatch" if boundary == "adapter-recheck" else "workflow_run", + ) + observed, attempt = job["observed"], publisher["run_attempt"] + produced = job["root"] / "publication-producer" + if boundary == "adapter-recheck": + produced = publication_cli(job, monkeypatch, 200) + assert (produced / "producer-outcome.json").is_file() + assert ship_producer(job, produced, "10-adapter") is not None + page = observed.job_pages[(200, attempt)] + exact = observed.attempts[(200, attempt)] + assert page == {"total_count": 0, "jobs": []} + if damage == "missing-count": + page.pop("total_count") + elif damage in {"partial-count", "string-count", "boolean-count"}: + page["total_count"] = { + "partial-count": 1, "string-count": "0", "boolean-count": False, + }[damage] + elif damage in {"executed", "authz-refused", "contradictory-jobs"}: + name = "authz" if damage == "authz-refused" else "prepare" + observed.jobs[200] = bound_job_facts(publisher, [ + (name, "failure" if damage == "authz-refused" else "success"), + ]) + page["jobs"] = copy.deepcopy(observed.jobs[200]) + page["total_count"] = 0 if damage == "contradictory-jobs" else 1 + elif damage == "attempt-missing": + observed.attempts[(200, attempt)] = None + elif damage.startswith("attempt"): + field, value = { + "attempt": ("run_attempt", attempt + 1), "attempt-source": ("head_sha", HEAD), + "attempt-repository": ("repository", {"full_name": "outside/data"}), + "attempt-status": ("status", "in_progress"), + }[damage] + exact[field] = value + elif damage in {"repository", "head-repository", "branch", "workflow-name"}: + field, value = { + "repository": ("repository", {"full_name": "outside/data"}), + "head-repository": ("head_repository", {"full_name": "outside/data"}), + "branch": ("head_branch", "other"), "workflow-name": ("name", "Other"), + }[damage] + publisher[field] = value + elif damage == "bundle-evidence": + observed.artifacts[200].append({ + "id": 61001, "name": f"validated-release-bundle-{attempt}", "expired": False, + }) + elif damage == "downloaded": + (job["root"] / "receipts" / f"release-stage-{attempt}-20-prepare").mkdir(parents=True) + else: + directory = job["root"] / "receipts" + directory.mkdir(parents=True, exist_ok=True) + (directory / "release-receipt.json").write_bytes(b"{}") + with pytest.raises(ValueError, match="^release-producer-outcome-invalid: " + cause): + if boundary == "publication": + publication_cli(job, monkeypatch, 200) + else: + run_report(job, monkeypatch) + assert_no_delivery(job) + assert not (produced / "release-receipt.json").exists() + if boundary == "publication": + assert not (produced / "producer-outcome.json").exists() + + +@pytest.mark.parametrize("damage,cause", [ + ("input-attempt", "cancelled publisher trigger differs"), + ("input-source", "cancelled publisher trigger differs"), + ("input-other-publisher", "cancelled publisher trigger differs"), + ("input-other-cancelled-attempt", "cancelled publisher trigger differs"), + ("caller-boolean", "decision inputs differ"), + ("invented-cause", "no-target decision conflicts with snapshot"), + ("missing-current-context", "cancelled publisher trigger differs"), +]) +def test_cli_cancelled_witness_cannot_supply_its_own_authority( + report_job, monkeypatch, damage, cause +): + job = report_job + context, historical, stage = original_cancelled_attempt_witness(job, monkeypatch) + observed = job["observed"] + advance_publisher_to_attempt_two(job, "completed", "success") + assert context["event"]["workflow_run"] == historical + path = stage / "producer-outcome.json" + assert path.is_file() and not (stage / "release-receipt.json").exists() + value = contract.object_json(path.read_bytes()) + assert value["reason"] == "publisher-cancelled-before-execution" + if damage == "input-attempt": + value["inputs"]["publication_attempt"] += 1 + elif damage == "input-source": + value["inputs"]["source"] = HEAD + elif damage == "input-other-publisher": + # A different authentic cancelled run cannot stand in for the trigger. + other = { + "id": 201, "run_attempt": 1, "head_sha": MERGE, + "path": ".github/workflows/release.yml", "name": "Release", "head_branch": "main", + "event": "workflow_run", "status": "completed", "conclusion": "cancelled", + "repository": {"full_name": REPO}, "head_repository": {"full_name": REPO}, + } + observed.runs[201] = other + observed.attempts[(201, 1)] = copy.deepcopy(other) + observed.jobs[201] = [] + observed.job_pages[(201, 1)] = {"total_count": 0, "jobs": []} + observed.artifacts[201] = [] + value["inputs"]["publication_run"] = 201 + elif damage == "input-other-cancelled-attempt": + # Even an independently proven cancelled attempt of the same run + # cannot replace the original triggering attempt. + observed.attempts[(200, 2)] = { + "id": 200, "run_attempt": 2, "head_sha": HEAD, + "path": ".github/workflows/release.yml", "name": "Release", "head_branch": "main", + "event": "workflow_run", "status": "completed", "conclusion": "cancelled", + "repository": {"full_name": REPO}, "head_repository": {"full_name": REPO}, + } + observed.runs[200].update(status="completed", conclusion="cancelled") + observed.jobs[200] = [] + observed.job_pages[(200, 2)] = {"total_count": 0, "jobs": []} + assert observed.attempts[(200, 2)] == observed.runs[200] + assert observed.attempts[(200, 2)] is not observed.runs[200] + value["inputs"].update(publication_attempt=2, source=HEAD) + elif damage == "caller-boolean": + value["inputs"] = {"cancelled_before_execution": True} + elif damage == "invented-cause": + value["reason"] = "publisher-superseded" + else: + monkeypatch.delenv("RELEASE_NEEDS") + path.write_bytes(contract.json_bytes(value)) + before = len(observed.reads) + with pytest.raises(ValueError, match="^release-producer-outcome-invalid: " + cause): + run_report(job, monkeypatch) + assert_no_delivery(job) + reads = observed.reads[before:] + assert f"repos/{REPO}/actions/runs/200" not in reads + assert not any("/actions/runs/201" in route for route in reads) + assert not any("/actions/runs/200/attempts/2" in route for route in reads) + + +@pytest.mark.parametrize("conclusion", ["success", "failure", "skipped"]) +def test_cli_non_cancelled_empty_publisher_still_requires_evidence( + report_job, monkeypatch, conclusion +): + job = report_job + publisher = cancelled_publisher_facts(job, monkeypatch) + publisher["conclusion"] = conclusion + job["observed"].attempts[(200, publisher["run_attempt"])]["conclusion"] = conclusion + assert job["observed"].job_pages[(200, publisher["run_attempt"])] == { + "total_count": 0, "jobs": [], + } + with pytest.raises(ValueError, match=r"^release-snapshot-missing:"): + publication_cli(job, monkeypatch, 200) + assert_no_delivery(job) + assert not (job["root"] / "publication-producer" / "producer-outcome.json").exists() + + +def test_cancelled_publisher_cannot_erase_existing_target(report_job, monkeypatch): + job = report_job + cancelled_publisher_facts(job, monkeypatch) + report = receipts.ProducerReceipt(copy.deepcopy(job["report"])) + before = receipts.canonical(report) + with pytest.raises( + ValueError, + match=( + r"^release-producer-outcome-invalid: cancelled publisher conflicts with current target$" + ), + ): + reporting.publication(REPO, 200, report) + assert receipts.canonical(report) == before + assert getattr(report, "outcome", None) is None + assert_no_delivery(job) + + +@pytest.mark.parametrize("boundary", ["publication", "adapter-recheck"]) +def test_cli_cancelled_publisher_with_stage_evidence_cannot_be_quiet( + report_job, monkeypatch, boundary +): + job = report_job + publisher = cancelled_publisher_facts(job, monkeypatch) + if boundary == "adapter-recheck": + produced = publication_cli(job, monkeypatch, 200) + assert (produced / "producer-outcome.json").is_file() + assert ship_producer(job, produced, "10-adapter") is not None + job["observed"].artifacts[200].append({ + "id": 61002, "name": f"release-stage-{publisher['run_attempt']}-20-prepare", + "expired": False, + }) + job["observed"].archives[61002] = receipt_archive(job["report"]) + cause = ( + "^release-producer-job-mismatch: producer did not execute" + if boundary == "publication" + else "^release-producer-outcome-invalid: cancelled publisher has evidence" + ) + with pytest.raises(ValueError, match=cause): + if boundary == "publication": + publication_cli(job, monkeypatch, 200) + else: + run_report(job, monkeypatch) + assert_no_delivery(job) + if boundary == "publication": + assert not (job["root"] / "publication-producer" / "producer-outcome.json").exists() + + +@pytest.mark.parametrize("damage,error,cause", [ + ("missing-jobs", KeyError, "^'jobs'$"), + ("null-jobs", ValueError, "^unexpected GitHub inventory response$"), + ("null-page", TypeError, "'NoneType' object is not subscriptable"), +]) +def test_cli_cancelled_publisher_malformed_job_inventory_refuses( + report_job, monkeypatch, damage, error, cause +): + job = report_job + publisher = cancelled_publisher_facts(job, monkeypatch) + page = {"missing-jobs": {"total_count": 0}, + "null-jobs": {"total_count": 0, "jobs": None}, "null-page": None}[damage] + job["observed"].job_pages[(200, publisher["run_attempt"])] = page + with pytest.raises(error, match=cause): + publication_cli(job, monkeypatch, 200) + assert_no_delivery(job) + assert not (job["root"] / "publication-producer" / "producer-outcome.json").exists() + + +def test_cli_context_bearing_publisher_rechecks_actual_stage_evidence(report_job, monkeypatch): + job = report_job + produced = ordinary_producer(job, monkeypatch) + assert (produced / "producer-outcome.json").is_file() + context = no_producer_facts(job, monkeypatch, "ci") + context["event"]["workflow_run"]["event"] = "push" + observed = job["observed"] + observed.runs[810]["event"] = "push" + observed.attempts[(810, 3)]["event"] = "push" + context["needs"]["prepare"]["result"] = "success" + observed.jobs[observed.reporter["id"]][3]["conclusion"] = "success" + trusted_call(job, monkeypatch, context["needs"], context["event"]) + stage = ship_producer(job, produced, "20-prepare") + assert stage is not None + row = observed.artifacts[observed.reporter["id"]][0] + observed.archives[row["id"]] = archive_stage(stage) + (job["root"] / "receipts").rename(job["root"] / "publisher-downloads") + publisher = observed.reporter + publisher.update(status="completed", conclusion="success") + observed.jobs[publisher["id"]][-1].update(status="completed", conclusion="success") + retain_context_facts(job, context) + adapter_call(job, monkeypatch, publisher) + forwarded = publication_cli(job, monkeypatch, publisher["id"]) + assert not (forwarded / "release-receipt.json").exists() + value = contract.object_json((forwarded / "producer-outcome.json").read_bytes()) + assert value["reason"] == "publisher-no-target" + assert ship_producer(job, forwarded, "10-adapter") is not None + run_report(job, monkeypatch) + assert_silent_noop(job) + assert ( + sum(f"/actions/runs/{publisher['id']}/artifacts" in route for route in observed.reads) == 2 + ) + # The real prepare decision and context checks run; this ordinary-main + # case intentionally has no release PR, bundle, OCI or publication proof. + + +@pytest.mark.parametrize("conclusion", ["success", "cancelled"]) +def test_cli_context_bearing_manual_publisher_preserves_target_and_failure( + report_job, monkeypatch, conclusion +): + job = report_job + context = no_producer_facts(job, monkeypatch, "claim") + context["event"]["inputs"]["claim_placeholder"] = "false" + context["needs"]["claim-name"]["result"] = "skipped" + context["needs"]["manual-pr"]["result"] = conclusion + observed, publisher = job["observed"], job["observed"].reporter + # Independent provider job facts; no row is derived from the staged receipt. + observed.jobs[publisher["id"]][1]["conclusion"] = "skipped" + observed.jobs[publisher["id"]][2]["conclusion"] = conclusion + observed.jobs[publisher["id"]][-1].update(status="completed", conclusion="success") + trusted_call(job, monkeypatch, context["needs"], context["event"]) + progress = copy.deepcopy(job["report"]) + progress.update(state="pr_open", ci=None, e2e=None, deploy=None, + preview=None, deployed=None, previous=None) + progress["pr"]["merge"] = None + stage = write_stage(job, report=progress, suffix="10-manual").parent + row = observed.artifacts[publisher["id"]][0] + observed.archives[row["id"]] = archive_stage(stage) + (job["root"] / "receipts").rename(job["root"] / "publisher-downloads") + publisher.update(status="completed", conclusion=conclusion) + retain_context_facts(job, context) + adapter_call(job, monkeypatch, publisher) + produced = publication_cli(job, monkeypatch, publisher["id"]) + assert not (produced / "producer-outcome.json").exists() + actual = contract.object_json((produced / "release-receipt.json").read_bytes()) + assert actual == { + **progress, "attempt": 3100, "sequence": 7, "timestamp": actual["timestamp"], + "state": "pr_open" if conclusion == "success" else "failed", + } + assert ship_producer(job, produced, "10-adapter") is not None + run_report(job, monkeypatch) + assert contract.object_json((job["root"] / "release-receipt.json").read_bytes()) == actual + assert observed.downloads == [] + if conclusion == "success": + assert observed.writes == observed.issues == [] + else: + assert observed.issues[0]["title"] == f"Data release failed: {actual['event_id']}" + assert all(endpoint.startswith("issues") for endpoint, _method, _data in observed.writes) + # Fixed manual progress snapshot: no claim of full deployment validation. + + +def test_cli_empty_publisher_no_target_witness_reaches_diagnostic(report_job, monkeypatch): + job = report_job + context = no_producer_facts(job, monkeypatch, "claim") + observed, publisher = job["observed"], job["observed"].reporter + publisher.update(status="completed", conclusion="success") + retain_context_facts(job, context) + adapter_call(job, monkeypatch, publisher) + forged = receipts.receipt(None) + receipts.observe_outcome( + forged, "no-target", "publisher-no-target", publication_run=publisher["id"], + publication_attempt=publisher["run_attempt"], outcome_sha256="0" * 64, + ) + produced = job["root"] / "forged-producer" + automation.finish(forged, produced / "release-receipt.json") + stage = ship_producer(job, produced, "10-adapter") + assert stage is not None and (stage / "producer-outcome.json").is_file() + assert not (stage / "release-receipt.json").exists() + assert observed.artifacts[publisher["id"]][0]["name"].startswith("release-run-context-") + with pytest.raises( + ValueError, match=r"^release-producer-outcome-invalid: publisher outcome differs$", + ): + run_report(job, monkeypatch) + assert_no_delivery(job) + run_report(job, monkeypatch, "incident") + assert not (job["root"] / "release-receipt.json").exists() + assert (job["root"] / "outputs").read_text() == "has_event=false\n" + assert "ValueError: release-producer-outcome-invalid: publisher outcome differs" in ( + job["root"] / "summary" + ).read_text() + assert observed.issues[0]["title"] == "Data release failed: run-3100" + assert all(endpoint.startswith("issues") for endpoint, _method, _data in observed.writes) + # Exact85 reaches its UnboundLocalError after the valid empty context, + # using only the old publisher-no-target vocabulary; no missing new API. + + +@pytest.mark.parametrize("damage,cause", [ + ("missing-needs", "release-snapshot-missing:"), + ("caller-boolean", "release-run-context-invalid: repository or needs inventory differs"), + ("missing-event", "release-run-context-invalid: unreadable workflow event/needs"), + ("needs-missing", "release-run-context-invalid: repository or needs inventory differs"), + ("needs-malformed", "release-run-context-invalid: malformed needs observation"), + ("needs-conflict", "release-producer-job-mismatch: API contradicts workflow needs"), + ("upstream-attempt", "release-run-context-invalid: checked upstream differs"), + ("upstream-source", "release-run-context-invalid: checked upstream differs"), + ("upstream-event", "release-run-context-invalid: checked upstream differs"), + ("current-source", "release-run-context-invalid: run binding differs"), + ("push-requires-producer", "release-run-context-invalid: upstream event requires a producer"), + ("executed-missing", "release-snapshot-missing: stage inventory differs"), + ("unexpected-stage", "release-snapshot-invalid:"), + ("undownloaded-stage", "release-snapshot-missing: downloaded current inventory differs"), +], ids=[ + "missing-needs", "caller-boolean", "missing-event", "needs-missing", "needs-malformed", + "needs-conflict", "upstream-attempt", "upstream-source", "upstream-event", "current-source", + "push-requires-producer", "executed-missing", "unexpected-stage", "undownloaded-stage", +]) +def test_cli_empty_inventory_requires_complete_orchestration( + report_job, monkeypatch, damage, cause +): + job = report_job + context = no_producer_facts(job, monkeypatch, "ci") + observed, run = job["observed"], job["observed"].reporter + needs, event = context["needs"], context["event"] + if damage == "missing-needs": + monkeypatch.delenv("RELEASE_NEEDS") + elif damage == "caller-boolean": + monkeypatch.setenv("RELEASE_NEEDS", '{"no_target":true}') + elif damage == "missing-event": + monkeypatch.setenv("GITHUB_EVENT_PATH", str(job["root"] / "absent-event.json")) + elif damage == "needs-missing": + needs.pop("verify") + elif damage == "needs-malformed": + needs["prepare"]["result"] = ["skipped"] + elif damage == "needs-conflict": + needs["prepare"]["result"] = "success" + elif damage.startswith("upstream-"): + field = {"upstream-attempt": "run_attempt", "upstream-source": "head_sha", + "upstream-event": "event"}[damage] + event["workflow_run"][field] = ( + 99 if field == "run_attempt" else "push" if field == "event" else HEAD + ) + if damage == "upstream-attempt": + # A provider record at the requested endpoint with the wrong + # recorded attempt must fail the binding, rather than lookup setup. + observed.attempts[(810, 99)] = copy.deepcopy(observed.runs[810]) + elif damage == "current-source": + run["head_sha"] = HEAD + elif damage == "push-requires-producer": + event["workflow_run"]["event"] = "push" + observed.runs[810]["event"] = "push" + observed.attempts[(810, 3)]["event"] = "push" + elif damage == "executed-missing": + needs["prepare"]["result"] = "success" + observed.jobs[run["id"]][3]["conclusion"] = "success" + elif damage == "unexpected-stage": + register_stage(job, run["run_attempt"], "99-unregistered") + else: + (job["root"] / "receipts" / f"release-stage-{run['run_attempt']}-20-prepare").mkdir( + parents=True + ) + if damage not in {"missing-needs", "caller-boolean", "missing-event", "current-source"}: + trusted_call(job, monkeypatch, needs, event) + with pytest.raises(ValueError, match="^" + cause): + run_report(job, monkeypatch) + assert_no_delivery(job) + + +@pytest.mark.parametrize("damage,cause", [ + ("missing", "release-snapshot-missing:"), + ("expired", "release-run-context-invalid: duplicate or expired context"), + ("stale", "release-run-context-invalid: context attempt differs"), + ("duplicate", "release-run-context-invalid: duplicate or expired context"), + ("json", "release-run-context-invalid: unreadable context archive"), + ("layout", "release-run-context-invalid: unreadable context archive"), + ("repository", "release-run-context-invalid: run binding differs"), + ("run", "release-run-context-invalid: run binding differs"), + ("attempt", "release-run-context-invalid: run binding differs"), + ("head", "release-run-context-invalid: run binding differs"), + ("inputs", "release-run-context-invalid: original Release inputs missing"), + ("not-claim", "release-run-context-invalid: dispatch requires a producer"), + ("claim-failed", "release-producer-job-mismatch: API contradicts workflow needs"), +], ids=[ + "missing", "expired", "stale", "duplicate", "json", "layout", "repository", "run", + "attempt", "head", "inputs", "not-claim", "claim-failed", +]) +def test_cli_publication_refuses_unbound_no_producer_context( + report_job, monkeypatch, damage, cause +): + job = report_job + context = no_producer_facts(job, monkeypatch, "claim") + publisher = job["observed"].reporter + publisher.update(status="completed", conclusion="success") + if damage in {"repository", "run", "attempt", "head"}: + field = {"repository": "repository", "run": "run_id", "attempt": "run_attempt", + "head": "head_sha"}[damage] + context[field] = "outside/data" if field == "repository" else ( + HEAD if field == "head_sha" else context[field] + 1 + ) + elif damage == "inputs": + context["event"].pop("inputs") + elif damage == "not-claim": + context["event"]["inputs"]["claim_placeholder"] = "false" + row = retain_context_facts(job, context) + if damage == "missing": + job["observed"].artifacts[publisher["id"]].clear() + elif damage == "expired": + row["expired"] = True + elif damage == "stale": + row["name"] = f"release-run-context-{publisher['run_attempt'] - 1}" + elif damage == "duplicate": + job["observed"].artifacts[publisher["id"]].append(copy.deepcopy(row)) + elif damage in {"json", "layout"}: + archive = io.BytesIO() + with zipfile.ZipFile(archive, "w") as zipped: + zipped.writestr("nested/run-context.json" if damage == "layout" else "run-context.json", + b"{" if damage == "json" else contract.json_bytes(context)) + job["observed"].archives[row["id"]] = archive.getvalue() + elif damage == "claim-failed": + job["observed"].jobs[publisher["id"]][1]["conclusion"] = "failure" + publisher_id = publisher["id"] + adapter_call(job, monkeypatch, publisher) + monkeypatch.setattr(sys, "argv", [ + "release_chain.reporting", "publication", "--run", str(publisher_id), + "--receipt", str(job["root"] / "release-receipt.json"), + ]) + with pytest.raises(ValueError, match="^" + cause): + reporting.main() + assert_no_delivery(job) + + +@pytest.mark.parametrize( + "status", ["queued", "in_progress", None], ids=["queued", "running", "null"] +) +@pytest.mark.parametrize("failed", [False, True], ids=["targeted", "upstream-failure"]) +def test_cli_collect_uses_needs_completion_with_lagging_jobs( + report_job, monkeypatch, status, failed +): + job = report_job + adapter_call(job, monkeypatch, failed=failed) + raw_job = job["observed"].jobs[job["observed"].reporter["id"]][1] + raw_job.update(status=status, conclusion=None) + write_stage(job) + run_report(job, monkeypatch) + saved = contract.object_json((job["root"] / "release-receipt.json").read_bytes()) + assert saved["state"] == ("failed" if failed else "verified") + assert saved["pr"] == job["report"]["pr"] + assert (job["root"] / "outputs").read_text() == "has_event=true\n" + assert raw_job["status"] == status and raw_job["conclusion"] is None + + +@pytest.mark.parametrize( + "status", ["queued", "in_progress", None], ids=["queued", "running", "null"] +) +def test_cli_actual_no_target_producer_uses_needs_completion(report_job, monkeypatch, status): + job = report_job + produced = ordinary_producer(job, monkeypatch) + assert ship_producer(job, produced, "20-prepare") is not None + context = no_producer_facts(job, monkeypatch, "ci") + context["event"]["workflow_run"]["event"] = "push" + job["observed"].runs[810]["event"] = "push" + job["observed"].attempts[(810, 3)]["event"] = "push" + context["needs"]["prepare"]["result"] = "success" + raw_job = job["observed"].jobs[job["observed"].reporter["id"]][3] + raw_job.update(status=status, conclusion=None) + trusted_call(job, monkeypatch, context["needs"], context["event"]) + run_report(job, monkeypatch) + assert_silent_noop(job) + + +@pytest.mark.parametrize("damage,cause", [ + ("name", "current job names differ from needs"), + ("duplicate", "current job names differ from needs"), + ("extra", "current job names differ from needs"), + ("run", "job run/attempt/source differs"), + ("attempt", "job run/attempt/source differs"), + ("source", "job run/attempt/source differs"), + ("terminal-conflict", "API contradicts workflow needs"), + ("nonterminal-conflict", "API contradicts workflow needs"), + ("failure-flag", "failure input differs"), + ("needs-skipped", "API contradicts workflow needs"), +], ids=[ + "name", "duplicate", "extra", "run", "attempt", "source", "terminal-conflict", + "nonterminal-conflict", "failure-flag", "needs-skipped", +]) +def test_cli_needs_authority_refuses_conflicting_current_facts( + report_job, monkeypatch, damage, cause +): + job = report_job + context = adapter_call(job, monkeypatch) + write_stage(job) + jobs = job["observed"].jobs[job["observed"].reporter["id"]] + raw = jobs[1] + if damage == "name": + raw["name"] = "unexpected-producer" + elif damage in {"duplicate", "extra"}: + jobs.append({**raw, "name": "reconcile" if damage == "duplicate" else "unregistered"}) + elif damage in {"run", "attempt", "source"}: + field = {"run": "run_id", "attempt": "run_attempt", "source": "head_sha"}[damage] + raw[field] = HEAD if field == "head_sha" else raw[field] + 1 + elif damage in {"terminal-conflict", "nonterminal-conflict"}: + raw.update(status="completed" if damage == "terminal-conflict" else "queued", + conclusion="failure") + elif damage == "failure-flag": + monkeypatch.setenv("FAILED", "true") + else: + context["needs"]["reconcile"]["result"] = "skipped" + trusted_call(job, monkeypatch, context["needs"], context["event"]) + with pytest.raises(ValueError, match="^release-producer-job-mismatch: " + cause): + run_report(job, monkeypatch) + assert_no_delivery(job) + + +@pytest.mark.parametrize("boundary", ["run", "artifacts", "jobs"]) +def test_cli_transport_failure_reaches_run_scoped_diagnostic( + report_job, monkeypatch, boundary +): + job, observed = report_job, report_job["observed"] + original = observed.consumer_api + run_id = observed.reporter["id"] + write_stage(job) + observed.jobs[run_id][0]["conclusion"] = "failure" + monkeypatch.setenv("FAILED", "true") + endpoint_to_fail = { + "run": f"actions/runs/{run_id}", + "artifacts": f"actions/runs/{run_id}/artifacts", + "jobs": f"actions/runs/{run_id}/attempts/{observed.reporter['run_attempt']}/jobs", + }[boundary] + + def api(repo, endpoint, method="GET", data=None): + if method == "GET" and endpoint.split("?", 1)[0] == endpoint_to_fail: + raise subprocess.CalledProcessError( + 1, ["gh", "api", endpoint], stderr=b"API unavailable" + ) + return original(repo, endpoint, method, data) + + monkeypatch.setattr(reporting, "api", api) + monkeypatch.setattr(automation, "api", api) + with pytest.raises(subprocess.CalledProcessError): + run_report(job, monkeypatch) + assert_no_delivery(job) + run_report(job, monkeypatch, "incident") + assert not (job["root"] / "release-receipt.json").exists() + assert (job["root"] / "outputs").read_text() == "has_event=false\n" + assert "CalledProcessError" in (job["root"] / "summary").read_text() + assert observed.issues[0]["title"] == f"Data release failed: run-{run_id}" + assert all(endpoint.startswith("issues") for endpoint, _method, _data in observed.writes) + + +@pytest.mark.parametrize("boundary", ["run", "event", "upstream"]) +def test_cli_malformed_repository_metadata_reaches_run_scoped_diagnostic( + report_job, monkeypatch, boundary +): + job = report_job + context = no_producer_facts(job, monkeypatch, "ci") + observed = job["observed"] + if boundary == "run": + observed.reporter["repository"] = [REPO] + elif boundary == "event": + context["event"]["repository"] = [REPO] + trusted_call(job, monkeypatch, context["needs"], context["event"]) + else: + observed.runs[810]["head_repository"] = [REPO] + observed.attempts[(810, 3)]["head_repository"] = [REPO] + monkeypatch.setenv("STAGE", "receipt-transport") + # This is an intentionally malformed raw observation, not a valid-path RED. + # Normal collection must still propagate the original collection failure. + with pytest.raises(AttributeError, match=r"^'list' object has no attribute 'get'$"): + run_report(job, monkeypatch) + assert_no_delivery(job) + run_report(job, monkeypatch, "incident") + assert not (job["root"] / "release-receipt.json").exists() + assert not (job["root"] / "producer-outcome.json").exists() + assert (job["root"] / "outputs").read_text() == "has_event=false\n" + assert "AttributeError" in (job["root"] / "summary").read_text() + issue = observed.issues[0] + assert issue["title"] == f"Data release failed: run-{observed.reporter['id']}" + assert "Stage: `receipt-transport`" in issue["body"] + assert all(endpoint.startswith("issues") for endpoint, _method, _data in observed.writes) + + +@pytest.mark.parametrize( + "status", ["queued", "in_progress", None], ids=["queued", "running", "null"] +) +def test_cli_actual_no_target_downstream_failure_is_an_incident(report_job, monkeypatch, status): + job = report_job + produced = ordinary_producer(job, monkeypatch) + assert ship_producer(job, produced, "20-prepare") is not None + context = no_producer_facts(job, monkeypatch, "ci") + context["event"]["workflow_run"]["event"] = "push" + job["observed"].runs[810]["event"] = "push" + job["observed"].attempts[(810, 3)]["event"] = "push" + context["needs"]["prepare"]["result"] = "success" + context["needs"]["build"]["result"] = "failure" + jobs = job["observed"].jobs[job["observed"].reporter["id"]] + jobs[3]["conclusion"] = "success" + jobs[4].update(status=status, conclusion=None) + trusted_call(job, monkeypatch, context["needs"], context["event"]) + monkeypatch.setenv("FAILED", "true") + monkeypatch.setenv("STAGE", "distribution-transfer") + run_report(job, monkeypatch) + assert not (job["root"] / "release-receipt.json").exists() + assert (job["root"] / "outputs").read_text() == "has_event=false\n" + issue = job["observed"].issues[0] + assert issue["title"] == f"Data release failed: run-{job['observed'].reporter['id']}" + assert "Stage: `distribution-transfer`" in issue["body"] + + +def test_cli_diagnostic_delivery_error_remains_visible(report_job, monkeypatch): + job = report_job + write_stage(job) + original = job["observed"].consumer_api + + def api(repo, endpoint, method="GET", data=None): + if endpoint == "issues" and method == "POST": + raise subprocess.CalledProcessError( + 1, ["gh", "api", "issues"], stderr=b"delivery failed" + ) + return original(repo, endpoint, method, data) + + monkeypatch.setattr(reporting, "api", api) + with pytest.raises(subprocess.CalledProcessError): + run_report(job, monkeypatch, "incident") + assert not (job["root"] / "release-receipt.json").exists() + assert (job["root"] / "outputs").read_text() == "has_event=false\n" + assert job["observed"].issues == [] + + +def test_cli_delivery_failure_preserves_collected_event_and_pr(report_job, monkeypatch): + job = report_job + adapter_call(job, monkeypatch) + write_stage(job) + monkeypatch.setenv("STAGE", "receipt-transport") + run_report(job, monkeypatch, "incident") + issue = job["observed"].issues[0] + assert issue["title"] == f"Data release failed: {job['report']['event_id']}" + assert "Stage: `receipt-transport`" in issue["body"] + assert f"https://github.com/{REPO}/pull/7" in issue["body"] + assert not (job["root"] / "release-receipt.json").exists() + assert (job["root"] / "outputs").read_text() == "has_event=false\n" + assert all(endpoint.startswith("issues") for endpoint, _method, _data in job["observed"].writes) + + +@pytest.mark.parametrize("workflow", ["ci-post.yml", "native-feed-post.yml"]) +def test_cli_ordinary_main_actual_producer_is_silent(report_job, monkeypatch, workflow): + job = report_job + produced = ordinary_producer(job, monkeypatch, workflow) + ship_producer(job, produced, "20-prepare") + run_report(job, monkeypatch) + assert_silent_noop(job) + assert not (produced / "release-receipt.json").exists() + witness = contract.object_json((produced / "producer-outcome.json").read_bytes()) + assert witness["reason"] == "ordinary-main" + assert witness["inputs"] == {"ci_run": 810, "ci_attempt": 3, "source": MERGE} + assert any("/actions/runs/810" in route for route in job["observed"].reads) + + +def complete_no_target_publisher(job, monkeypatch, produced, suffix): + observed = job["observed"] + publisher_id = observed.reporter["id"] + stage = ship_producer(job, produced, suffix) + observed.reporter.update(status="completed", conclusion="success") + if stage is not None: + row = observed.artifacts[publisher_id][0] + observed.archives[row["id"]] = archive_stage(stage) + (job["root"] / "receipts").rename(job["root"] / "publisher-downloads") + observed.reporter = { + "id": 3100, "run_attempt": 7, "path": ".github/workflows/pineforge-release.yml", + "event": "workflow_run", "head_sha": MERGE, + } + observed.runs[3100] = observed.reporter + observed.jobs[3100] = [{ + "name": "reconcile", "status": "completed", "conclusion": "success", + }] + monkeypatch.setenv("GITHUB_RUN_ID", "3100") + monkeypatch.setenv("GITHUB_RUN_ATTEMPT", "7") + adapter_directory = job["root"] / "adapter-producer" + monkeypatch.setattr(sys, "argv", [ + "release_chain.reporting", "publication", "--run", str(publisher_id), + "--receipt", str(adapter_directory / "release-receipt.json"), + ]) + reporting.main() + ship_producer(job, adapter_directory, "10-adapter") + run_report(job, monkeypatch) + assert_silent_noop(job) + assert not (adapter_directory / "release-receipt.json").exists() + assert any(f"/actions/runs/{publisher_id}/artifacts" in route for route in observed.reads) + + +@pytest.mark.parametrize("workflow", ["ci-post.yml", "native-feed-post.yml"]) +def test_cli_publication_and_adapter_report_preserve_actual_no_target( + report_job, monkeypatch, workflow +): + produced = ordinary_producer(report_job, monkeypatch, workflow) + complete_no_target_publisher(report_job, monkeypatch, produced, "20-prepare") + + +@pytest.mark.parametrize("damage", [ + "missing", "json", "run", "attempt", "head", "reason", "inputs", "input-source", + "expired", "unregistered", "not-downloaded", "producer-skipped", "api-attempt", +]) +def test_cli_no_target_requires_current_bound_producer_evidence(report_job, monkeypatch, damage): + job = report_job + produced = ordinary_producer(job, monkeypatch) + # Do not depend on a new Python API: defective-first reaches the real collect command. + if damage == "missing": + register_stage(job, job["observed"].reporter["run_attempt"], "20-prepare").mkdir( + parents=True) + else: + stage = ship_producer(job, produced, "20-prepare") + assert stage is not None + path = stage / "producer-outcome.json" + assert path.is_file() + if damage == "json": + path.write_bytes(b"{") + elif damage in {"run", "attempt", "head", "reason", "inputs", "input-source"}: + value = contract.object_json(path.read_bytes()) + if damage == "run": + value["run_id"] += 1 + elif damage == "attempt": + value["run_attempt"] += 1 + elif damage == "head": + value["head_sha"] = "9" * 40 + elif damage == "reason": + value["reason"] = "unregistered" + elif damage == "inputs": + value["inputs"] = {"ignored": True} + else: + value["inputs"]["source"] = "9" * 40 + path.write_bytes(contract.json_bytes(value)) + elif damage == "not-downloaded": + # Download a different directory, preserving the API's current row. + stage.rename(stage.with_name("unregistered-download")) + rows = job["observed"].artifacts.get(job["observed"].reporter["id"], []) + assert rows + if damage == "expired": + rows[0]["expired"] = True + elif damage == "unregistered": + rows[0]["name"] += "-unknown" + elif damage == "producer-skipped": + job["observed"].jobs[job["observed"].reporter["id"]][0]["conclusion"] = "skipped" + elif damage == "api-attempt": + job["observed"].reporter["run_attempt"] += 1 + cause = { + "missing": "release-snapshot-missing: receipt and producer outcome absent", + "json": "release-snapshot-invalid:", + "run": "release-producer-outcome-invalid: envelope or producer job differs", + "attempt": "release-producer-outcome-invalid: envelope or producer job differs", + "head": "release-producer-outcome-invalid: envelope or producer job differs", + "reason": "release-producer-outcome-invalid: no-target decision conflicts with snapshot", + "inputs": "release-producer-outcome-invalid: decision inputs differ", + "input-source": "release-producer-outcome-invalid: observed main input differs", + "expired": "release-snapshot-missing: current stage expired", + "unregistered": "release-snapshot-invalid: unregistered or duplicate stage", + "not-downloaded": "release-snapshot-missing: downloaded current inventory differs", + "producer-skipped": "release-producer-job-mismatch: producer did not execute", + "api-attempt": "release-producer-run-mismatch:", + }[damage] + with pytest.raises(ValueError, match="^" + cause): + run_report(job, monkeypatch) + assert_no_delivery(job) + + +def test_cli_report_only_rerun_cannot_reuse_prior_no_target_decision(report_job, monkeypatch): + job = report_job + produced = ordinary_producer(job, monkeypatch) + ship_producer(job, produced, "20-prepare") + # The download action in the new attempt fetches no prior-attempt directories. + downloaded = job["root"] / "receipts" + if downloaded.exists(): + downloaded.rename(job["root"] / "prior-downloads") + old = job["observed"].reporter["run_attempt"] + job["observed"].reporter["run_attempt"] = old + 1 + monkeypatch.setenv("GITHUB_RUN_ATTEMPT", str(old + 1)) + with pytest.raises(ValueError, match=r"^release-snapshot-attempt-mismatch:"): + run_report(job, monkeypatch) + assert_no_delivery(job) + + +@pytest.mark.parametrize("entry", ["collect", "publication"]) +def test_cli_later_no_target_cannot_erase_a_current_target(report_job, monkeypatch, entry): + job = report_job + observed = job["observed"] + produced = ordinary_producer(job, monkeypatch) + ship_producer(job, produced, "20-prepare") + # Keep an earlier, independently registered targeted producer snapshot. + current = copy.deepcopy(job["report"]) + write_stage(job, report=current, suffix="10-manual") + observed.jobs[observed.reporter["id"]].append({ + "name": "manual-pr", "status": "completed", "conclusion": "success", + }) + if entry == "collect": + with pytest.raises(ValueError, match="stage decisions disagree"): + run_report(job, monkeypatch) + else: + publisher_id = observed.reporter["id"] + observed.reporter.update(status="completed", conclusion="success") + for row in observed.artifacts[publisher_id]: + observed.archives[row["id"]] = archive_stage( + job["root"] / "receipts" / row["name"] + ) + monkeypatch.setattr(sys, "argv", [ + "release_chain.reporting", "publication", "--run", str(publisher_id), + "--receipt", str(job["root"] / "release-receipt.json"), + ]) + with pytest.raises(ValueError, match="stage decisions disagree"): + reporting.main() + assert_no_delivery(job) + + +@pytest.mark.parametrize("failed", [False, True]) +def test_cli_failure_input_is_checked_against_producer_jobs(report_job, monkeypatch, failed): + job = report_job + write_stage(job) + monkeypatch.setenv("FAILED", str(failed).lower()) + observed = job["observed"] + observed.jobs[observed.reporter["id"]][0]["conclusion"] = "success" if failed else "failure" + with pytest.raises(ValueError, match="failure input differs"): + run_report(job, monkeypatch) + assert_no_delivery(job) + + +def test_cli_unreadable_download_still_reaches_diagnostic_incident(report_job, monkeypatch): + job = report_job + path = register_stage(job, job["observed"].reporter["run_attempt"], "10-adapter") + path.parent.mkdir(parents=True) + path.write_bytes(b"not a directory") + monkeypatch.setenv("FAILED", "true") + job["observed"].jobs[job["observed"].reporter["id"]][0]["conclusion"] = "failure" + run_report(job, monkeypatch, "incident") + assert not (job["root"] / "release-receipt.json").exists() + assert (job["root"] / "outputs").read_text() == "has_event=false\n" + assert job["observed"].issues[0]["state"] == "open" + + +def pre_manifest_files(): + pin = ( + "ghcr.io/pineforge-4pass/pineforge-release:1.0.0@sha256:" + "b271873d0e91a9eeeb4595daaded9bdfbdbcd3925d3ec1300a833395e2004c86" + ) + return { + contract.RUNTIME: f'DEFAULT_RELEASE_IMAGE = "{pin}"\n'.encode(), + contract.DOCKERFILE: f"ARG PINEFORGE_RELEASE_IMAGE={pin}\n".encode(), + "pyproject.toml": b'[project]\nname = "pineforge-data"\nversion = "0.3.0"\n', + } + + +def snapshot_files(files): + def snapshot(revision, directory): + assert revision == BASE + for name, raw in files.items(): + path = directory / name + path.parent.mkdir(parents=True, exist_ok=True) + path.write_bytes(raw) + return snapshot + + +@pytest.mark.parametrize("bump,forward", [ + (None, False), ("patch", False), ("minor", False), ("major", False), + ("patch", True), ("minor", True), ("major", True), +]) +def test_cli_reconcile_actual_targetless_decisions(report_job, monkeypatch, bump, forward): + job = report_job + observed = job["observed"] + expected_versions = {"patch": "0.3.1", "minor": "0.4.0", "major": "1.0.0"} + if bump: + observed.reporter["path"] = ".github/workflows/release.yml" + observed.reporter["event"] = "workflow_dispatch" + observed.jobs[observed.reporter["id"]] = [ + {"name": "manual-pr", "status": "completed", "conclusion": "success"}, + ] + created_patch = {} + + def git(*args): + if args[0] == "diff": + assert args == ("diff", "--name-only", BASE, HEAD) + return "\n".join(created_patch) + if args[0] == "fetch": + if args != ("fetch", "origin", "main"): + assert args == ( + "fetch", "origin", "main", "refs/heads/" + observed.pr["head"]["ref"], + ) + return "" + assert args in {("rev-parse", "origin/main"), ("merge-base", BASE, HEAD)} + return BASE + + def source_bytes(command): + assert command == ["git", "show", f"{HEAD}:pyproject.toml"] + return created_patch["pyproject.toml"] + + monkeypatch.setattr(automation, "git", git) + monkeypatch.setattr(automation.subprocess, "check_output", source_bytes) + monkeypatch.setattr(automation, "snapshot", snapshot_files(pre_manifest_files())) + + def create_pr(repo, base, patch, branch, meta, bot): + # Replace the remote creation boundary; consume the real deterministic patch. + assert repo == REPO and base == BASE and bot == "release[bot]" + assert patch["pyproject.toml"].count(expected_versions[bump].encode()) == 1 + created_patch.update(patch) + assert branch.startswith("bot/pineforge-package/main/") + meta.update(base=BASE, generated_head=HEAD) + observed.pr.update( + merged=False, merge_commit_sha=None, + user={"login": bot}, state="open", body=automation.body(meta), + ) + observed.pr["head"]["ref"] = branch + return copy.deepcopy(observed.pr) + + monkeypatch.setattr(automation, "create_pr", create_pr) + real_api = observed.consumer_api + + def api(repo, endpoint, method="GET", data=None): + if endpoint.startswith("actions/workflows/ci.yml/runs?"): + return {"workflow_runs": []} + return real_api(repo, endpoint, method, data) + + monkeypatch.setattr(automation, "api", api) + event_path = job["root"] / "event.json" + event_path.write_bytes(b"{}") + produced = job["root"] / "producer" + with monkeypatch.context() as producer: + producer.setenv("GITHUB_EVENT_PATH", str(event_path)) + producer.setenv("RELEASE_BOT_LOGIN", "release[bot]") + argv = ["release_adapter.py", "manual" if bump else "reconcile"] + if bump: + argv += ["--bump", bump] + producer.setattr(sys, "argv", [ + *argv, "--receipt", str(produced / "release-receipt.json"), + ]) + release_adapter.main() + if forward: + complete_no_target_publisher(job, monkeypatch, produced, "10-manual") + else: + ship_producer(job, produced, "10-manual" if bump else "10-adapter") + run_report(job, monkeypatch) + assert_silent_noop(job) + witness = contract.object_json((produced / "producer-outcome.json").read_bytes()) + assert witness["reason"] == ("package-bump" if bump else "no-ready-release") + if bump: + assert witness["inputs"]["version"] == expected_versions[bump] + + +def test_report_collect_refuses_absent_snapshot(report_job, monkeypatch): + with pytest.raises(ValueError, match=r"^release-snapshot-missing:"): + run_report(report_job, monkeypatch) + assert_no_delivery(report_job) + + +def test_report_collect_refuses_receiptless_stage(report_job, monkeypatch): + register_stage(report_job, report_job["observed"].reporter["run_attempt"], "10-adapter").mkdir( + parents=True) + with pytest.raises(ValueError, match=r"^release-snapshot-missing:"): + run_report(report_job, monkeypatch) + assert_no_delivery(report_job) + + +@pytest.mark.parametrize("offset", [-1, 1], ids=["previous", "later"]) +def test_report_collect_refuses_other_attempt_snapshot(report_job, monkeypatch, offset): + old = copy.deepcopy(report_job["report"]) + old["sequence"] += offset + path = write_stage(report_job, report=old, attempt=old["sequence"]) + path.parent.rename(report_job["root"] / "withheld-other-attempt") + with pytest.raises(ValueError, match=r"^release-snapshot-attempt-mismatch:"): + run_report(report_job, monkeypatch) + assert_no_delivery(report_job) + + +@pytest.mark.parametrize("field", ["attempt", "sequence"], ids=["wrong-run", "wrong-attempt"]) +@pytest.mark.parametrize("failed", [False, True], ids=["success-path", "failure-path"]) +def test_report_collect_refuses_relabelled_snapshot(report_job, monkeypatch, field, failed): + changed = copy.deepcopy(report_job["report"]) + changed[field] += 1 + write_stage(report_job, report=changed) + monkeypatch.setenv("FAILED", str(failed).lower()) + if failed: + report_job["observed"].jobs[report_job["observed"].reporter["id"]][0]["conclusion"] = ( + "failure" + ) + with pytest.raises(ValueError, match=r"^release-snapshot-attempt-mismatch:"): + run_report(report_job, monkeypatch) + assert_no_delivery(report_job) + + +@pytest.mark.parametrize("damage", ["json", "fields", "target", "schema", "consumer", "stage"]) +def test_report_collect_refuses_invalid_snapshot(report_job, monkeypatch, damage): + changed = copy.deepcopy(report_job["report"]) + if damage == "fields": + changed["extra"] = "unregistered" + elif damage == "target": + changed.update(event_id=None, release_version=None) + elif damage == "schema": + changed["schema"] = "pineforge/consumer-signal/v1" + elif damage == "consumer": + changed["consumer"] = "web" + path = write_stage( + report_job, report=changed, suffix="50-unregistered" if damage == "stage" else "10-adapter" + ) + if damage == "json": + path.write_bytes(b"{") + with pytest.raises(ValueError, match=r"^release-snapshot-invalid:"): + run_report(report_job, monkeypatch) + assert_no_delivery(report_job) + + +def test_report_collect_does_not_substitute_retained_receipt(report_job, monkeypatch): + report_job["observed"].retain(report_job["report"]) + with pytest.raises(ValueError, match=r"^release-snapshot-missing:"): + run_report(report_job, monkeypatch) + assert_no_delivery(report_job) + assert not any("/releases" in route for route in report_job["observed"].reads) + + +def test_report_collect_preserves_current_snapshot_and_all_pins(report_job, monkeypatch): + report = report_job["report"] + # Match the download action: only the current attempt's stage directory exists. + for offset in (-1, 1): + report_job["observed"].artifacts.setdefault(report["attempt"], []).append({ + "id": 60000 + offset, "name": f"release-stage-{report['sequence'] + offset}-10-adapter", + "expired": False, + }) + write_stage(report_job) + run_report(report_job, monkeypatch) + raw = (report_job["root"] / "release-receipt.json").read_bytes() + assert raw == receipts.canonical(report) + assert (report_job["root"] / "outputs").read_text() == "has_event=true\n" + assert "**verified**" in (report_job["root"] / "summary").read_text() + observed = report_job["observed"] + public = hub.validate_receipt( + observed, REPO, contract.object_json(raw), + report_job["event"], report_job["policy"], observed.reporter, + ) + assert public["state"] == "verified" + for name in ("pr", "preview", "deployed", "previous"): + assert public[name] == report[name] + artifact_id = report["attempt"] * 100 + observed.archives[artifact_id] = receipt_archive(contract.object_json(raw)) + observed.artifacts[report["attempt"]] = [{ + "id": artifact_id, "name": receipts.ARTIFACT, "expired": False, + "digest": "sha256:" + contract.sha256(observed.archives[artifact_id]), + }] + assert hub.fetch_artifact(observed, REPO, observed.reporter, report_job["policy"]) == report + + +def test_report_collect_preserves_retained_completion(report_job, monkeypatch): + observed, report = report_job["observed"], report_job["report"] + saved = copy.deepcopy(report) + saved.update(attempt=report["attempt"] - 100, sequence=1) + observed.retain(saved) + retained = observed.assets[98] + write_stage(report_job) + run_report(report_job, monkeypatch) + collected = contract.object_json( + (report_job["root"] / "release-receipt.json").read_bytes() + ) + assert collected == report + reporting.retain(REPO, collected) + assert observed.assets[98] == retained == receipts.canonical(saved) + public = hub.validate_receipt( + observed, REPO, collected, report_job["event"], report_job["policy"], observed.reporter + ) + assert public["state"] == "verified" + assert observed.downloads == [] + assert all(endpoint.startswith("issues") for endpoint, _method, _data in observed.writes) + + +def test_report_collect_failed_current_snapshot_preserves_pins(report_job, monkeypatch): + write_stage(report_job) + monkeypatch.setenv("FAILED", "true") + report_job["observed"].jobs[report_job["observed"].reporter["id"]][0]["conclusion"] = "failure" + run_report(report_job, monkeypatch) + report = contract.object_json((report_job["root"] / "release-receipt.json").read_bytes()) + assert report["state"] == "failed" + expected = {**report_job["report"], "state": "failed", "timestamp": report["timestamp"]} + assert report == expected + assert report_job["observed"].issues[0]["state"] == "open" + + +@pytest.mark.parametrize("slot", ["event_id", "pr", "deployed", "previous"]) +def test_retained_completion_rejects_pin_drift(report_job, slot): + changed = copy.deepcopy(report_job["report"]) + if slot == "event_id": + changed[slot] = "sha256:" + contract.sha256(b"other release") + elif slot == "pr": + changed[slot]["head"] = contract.sha256(b"other PR head")[:40] + else: + changed[slot]["facts_sha256"] = contract.sha256(slot.encode()) + observed = report_job["observed"] + observed.retain(changed) + with pytest.raises(ValueError, match=r"^immutable completion receipt differs$"): + reporting.retain(REPO, report_job["report"]) + assert observed.downloads == observed.writes == [] + + +@pytest.mark.parametrize( + ("slot", "cause"), + [ + ("event_id", "receipt event differs"), + ("pr", "PR base differs from policy"), + ("ci", "checks ran against a different source"), + ("e2e", "reported checks differ from GitHub"), + ("deploy", "required check inventory differs from owner policy"), + ("preview", "preview did not exercise the candidate source/image"), + ("deployed", "actual served source/image differs"), + ("previous", "malformed identity"), + ], + ids=["event", "pr", "ci", "e2e", "deploy", "preview", "deployed", "previous"], +) +def test_hub_validator_consumes_varied_receipt_pins(report_job, slot, cause): + changed = copy.deepcopy(report_job["report"]) + other_sha = contract.sha256(slot.encode()) + if slot == "event_id": + changed[slot] = "sha256:" + other_sha + elif slot == "pr": + changed[slot]["base"] = "dev" + elif slot == "ci": + changed[slot]["sha"] = other_sha[:40] + elif slot == "e2e": + changed[slot]["results"][changed[slot]["expected"][0]] = "failure" + elif slot == "deploy": + changed[slot]["approved_skips"] = [] + elif slot == "preview": + changed[slot]["source"] = other_sha[:40] + elif slot == "deployed": + changed[slot]["image_digest"] = "sha256:" + other_sha + else: + changed[slot]["facts_sha256"] = "invalid" + observed = report_job["observed"] + with pytest.raises(hub.Invalid, match=cause): + hub.validate_receipt( + observed, REPO, changed, report_job["event"], report_job["policy"], observed.reporter + ) + + +@pytest.mark.parametrize("snapshot", ["absent", "present", "invalid"]) +def test_incident_command_cannot_enable_receipt_transport(report_job, monkeypatch, snapshot): + if snapshot != "absent": + path = write_stage(report_job) + if snapshot == "invalid": + path.write_bytes(b"{") + monkeypatch.setenv("FAILED", "true") + run_report(report_job, monkeypatch, "incident") + assert not (report_job["root"] / "release-receipt.json").exists() + assert (report_job["root"] / "outputs").read_text() == "has_event=false\n" + assert "**failed**" in (report_job["root"] / "summary").read_text() + assert report_job["observed"].issues[0]["state"] == "open" + assert report_job["observed"].downloads == [] + + +@pytest.fixture +def publisher_job(report_job): + observed, report = report_job["observed"], report_job["report"] + publisher = observed.runs[200] + run_id, attempt = publisher["id"], publisher["run_attempt"] + staged = { + **copy.deepcopy(report), "attempt": run_id, "sequence": attempt, "state": "deploying", + } + artifact_id = run_id * 100 + observed.archives[artifact_id] = receipt_archive(staged) + observed.artifacts[run_id] = [{ + "id": artifact_id, "name": f"release-stage-{attempt}-50-verify", "expired": False, + }] + report_job.update(publisher=run_id, publisher_attempt=attempt, staged=staged) + return report_job + + +@pytest.mark.parametrize("inventory", ["absent", "previous", "later", "expired", "unregistered"]) +def test_publication_refuses_missing_attempt_snapshot(publisher_job, inventory): + job = publisher_job + observed = job["observed"] + rows = observed.artifacts[job["publisher"]] + if inventory == "absent": + rows.clear() + elif inventory in {"previous", "later"}: + offset = -1 if inventory == "previous" else 1 + rows[0]["name"] = f"release-stage-{job['publisher_attempt'] + offset}-50-verify" + elif inventory == "expired": + rows[0]["expired"] = True + else: + rows[0]["name"] = f"release-stage-{job['publisher_attempt']}-50-unregistered" + report = receipts.receipt(None) + before = copy.deepcopy(report) + cause = ( + "release-snapshot-attempt-mismatch:" if inventory in {"previous", "later"} + else "release-snapshot-invalid:" if inventory == "unregistered" + else "release-snapshot-missing:" + ) + with pytest.raises(ValueError, match="^" + cause): + reporting.publication(REPO, job["publisher"], report) + assert report == before + assert observed.downloads == observed.writes == [] + + +@pytest.mark.parametrize("field", ["attempt", "sequence"], ids=["wrong-run", "wrong-attempt"]) +def test_publication_refuses_relabelled_snapshot(publisher_job, field): + job = publisher_job + changed = copy.deepcopy(job["staged"]) + changed[field] += 1 + job["observed"].archives[job["publisher"] * 100] = receipt_archive(changed) + report = receipts.receipt(None) + before = copy.deepcopy(report) + with pytest.raises(ValueError, match=r"^release-snapshot-attempt-mismatch:"): + reporting.publication(REPO, job["publisher"], report) + assert report == before + assert job["observed"].downloads == job["observed"].writes == [] + + +@pytest.mark.parametrize("fault", ["absent", "wrong-run", "wrong-attempt", "receiptless"]) +def test_publication_cli_requires_current_target(publisher_job, monkeypatch, fault): + job = publisher_job + observed = job["observed"] + if fault == "absent": + observed.artifacts[job["publisher"]] = [] + else: + changed = copy.deepcopy(job["staged"]) + if fault == "wrong-run": + changed["attempt"] += 1 + elif fault == "wrong-attempt": + changed["sequence"] += 1 + archive = io.BytesIO() + with zipfile.ZipFile(archive, "w") as zipped: + if fault != "receiptless": + zipped.writestr("release-receipt.json", receipts.canonical(changed)) + observed.archives[job["publisher"] * 100] = archive.getvalue() + monkeypatch.setattr(sys, "argv", [ + "release_chain.reporting", "publication", "--run", str(job["publisher"]), + "--receipt", str(job["root"] / "release-receipt.json"), + ]) + cause = { + "absent": "release-snapshot-missing:", + "wrong-run": "release-snapshot-attempt-mismatch:", + "wrong-attempt": "release-snapshot-attempt-mismatch:", + "receiptless": "release-snapshot-invalid: unreadable stage archive", + }[fault] + with pytest.raises(ValueError, match="^" + cause): + reporting.main() + assert_no_delivery(job) + + +def test_publication_preserves_current_manual_progress(publisher_job): + job = publisher_job + observed = job["observed"] + progress = copy.deepcopy(job["staged"]) + progress.update( + state="pr_open", ci=None, e2e=None, deploy=None, + preview=None, deployed=None, previous=None, + ) + progress["pr"]["merge"] = None + observed.archives[job["publisher"] * 100] = receipt_archive(progress) + observed.artifacts[job["publisher"]][0]["name"] = ( + f"release-stage-{job['publisher_attempt']}-10-manual" + ) + observed.jobs[job["publisher"]].append({ + "name": "manual-pr", "status": "completed", "conclusion": "success", + }) + report = receipts.receipt(None) + reporting.publication(REPO, job["publisher"], report) + expected = { + **progress, "attempt": job["report"]["attempt"], "sequence": job["report"]["sequence"], + "timestamp": report["timestamp"], + } + assert report == expected + assert observed.downloads == observed.writes == [] + + +@pytest.mark.parametrize("inventory", ["absent", "previous", "later"]) +def test_publication_refuses_unavailable_attempt_bundle(publisher_job, inventory): + job = publisher_job + observed = job["observed"] + if inventory != "absent": + offset = -1 if inventory == "previous" else 1 + observed.bundles[f"validated-release-bundle-{job['publisher_attempt'] + offset}"] = { + "evidence.json": contract.json_bytes({"run_id": 80}), + } + with pytest.raises(ValueError, match=r"^release-bundle-unavailable:"): + reporting.publication(REPO, job["publisher"], receipts.receipt(None)) + assert observed.downloads == [f"validated-release-bundle-{job['publisher_attempt']}"] + assert observed.writes == [] + + +@pytest.mark.parametrize("contents", ["missing-evidence", "invalid-evidence"]) +def test_publication_refuses_invalid_bundle(publisher_job, contents): + job = publisher_job + observed = job["observed"] + observed.bundles[f"validated-release-bundle-{job['publisher_attempt']}"] = ( + {} if contents == "missing-evidence" else {"evidence.json": b"{"} + ) + with pytest.raises(ValueError, match=r"^release-bundle-invalid:"): + reporting.publication(REPO, job["publisher"], receipts.receipt(None)) + assert observed.writes == [] + + +class MainGateBoundary(RuntimeError): + """The bundle was read; full main-proof validation must run next.""" + + +def test_publication_current_bundle_reaches_main_gate(publisher_job, monkeypatch): + job = publisher_job + observed = job["observed"] + main = {"run_id": job["report"]["attempt"] - 1} + bundle = f"validated-release-bundle-{job['publisher_attempt']}" + observed.bundles[bundle] = {"evidence.json": contract.json_bytes(main)} + calls = [] + + def before_main_gate(repo, run_id, *, head): + calls.append((repo, run_id, head)) + raise MainGateBoundary("before full main-proof gate") + + monkeypatch.setattr(reporting, "gate_for_run", before_main_gate) + report = receipts.receipt(None) + with pytest.raises(MainGateBoundary, match=r"^before full main-proof gate$"): + reporting.publication(REPO, job["publisher"], report) + assert observed.downloads == [bundle] + assert calls == [(REPO, main["run_id"], job["report"]["pr"]["merge"])] + assert report["state"] == "deploying" + assert report["deploy"] == job["staged"]["deploy"] + assert observed.writes == [] + + +def test_reporting_workflow_requires_collection_before_transport(): + workflow = yaml.load( + (Path(__file__).parents[1] / ".github/workflows/release-report.yml").read_text(), + Loader=yaml.BaseLoader, + ) + steps = workflow["jobs"]["report"]["steps"] + collect = next(step for step in steps if step.get("id") == "receipt") + assert collect["run"] == "python -m release_chain.reporting collect" + assert "if" not in collect and "continue-on-error" not in collect + download = next(step for step in steps if "actions/download-artifact@" in step.get("uses", "")) + assert download["with"]["pattern"] == "release-stage-${{ github.run_attempt }}-*" + upload = next(step for step in steps if step.get("id") == "upload") + assert upload["if"] == "inputs.hub && steps.receipt.outputs.has_event == 'true'" + alert = steps[-1] + assert alert["if"] == "failure()" + assert alert["run"] == ( + "python -m release_chain.reporting incident --receipt incident-release-receipt.json" + ) diff --git a/tests/test_release_workflow_authz.py b/tests/test_release_workflow_authz.py index 332ef4f..c9ecdd8 100644 --- a/tests/test_release_workflow_authz.py +++ b/tests/test_release_workflow_authz.py @@ -145,15 +145,11 @@ def assert_workflow_authz(workflow: dict[str, Any]) -> None: needs = [needs] assert "authz" in needs, f"{job_id}: needs authz" - report_tail = ( - "needs.reconcile.result != 'skipped'" - if "reconcile" in jobs - else "!inputs.claim_placeholder" - ) report = jobs["report"] - assert report["if"] == ( - f"!cancelled() && {AUTHZ_PASSED} && {report_tail}" - ), "report: authz condition" + condition = f"!cancelled() && {AUTHZ_PASSED}" + if "reconcile" in jobs: + condition += " && needs.reconcile.result != 'skipped'" + assert report["if"] == condition, "report: authz condition" assert report["uses"] == "./.github/workflows/release-report.yml" assert report["secrets"] == "inherit" @@ -181,6 +177,103 @@ def test_release_workflows_use_the_canonical_authz(name: str) -> None: assert_workflow_authz(load(name)) +@pytest.mark.parametrize( + "workflow,job_id,stage", + [ + ("pineforge-release.yml", "reconcile", "10-adapter"), + ("release.yml", "manual-pr", "10-manual"), + ("release.yml", "prepare", "20-prepare"), + ("release.yml", "github-release", "30-github"), + ("release.yml", "publish", "40-publish"), + ("release.yml", "verify", "50-verify"), + ], +) +def test_actual_stage_transport_keeps_the_private_producer_outcome( + workflow: str, job_id: str, stage: str +) -> None: + job = load(workflow)["jobs"][job_id] + uploads = [ + step for step in job["steps"] + if step.get("uses", "").startswith("actions/upload-artifact@") + and step["with"]["name"] == "release-stage-${{ github.run_attempt }}-" + stage + ] + assert len(uploads) == 1 and uploads[0]["if"] == "always()" + paths = uploads[0]["with"]["path"].splitlines() + assert len(paths) == 2 + assert paths[0].endswith("/release-receipt.json") + assert paths[1].endswith("/producer-outcome.json") + assert paths[0].rsplit("/", 1)[0] == paths[1].rsplit("/", 1)[0] + for step in job["steps"]: + if ( + step.get("uses", "").startswith("actions/download-artifact@") + and step["with"].get("name", "").startswith("release-stage-") + ): + assert "has_target" not in step.get("if", "") + + +def test_diagnostics_preserve_the_failed_stage_without_enabling_transport() -> None: + workflow = load("release-report.yml") + steps = workflow["jobs"]["report"]["steps"] + fallback = steps[-1] + assert fallback["if"] == "failure()" + assert fallback["env"]["STAGE"] == "${{ inputs.failed && inputs.stage || 'receipt-transport' }}" + assert fallback["env"]["FAILED"] == "${{ inputs.failed }}" + assert fallback["run"] == ( + "python -m release_chain.reporting incident --receipt incident-release-receipt.json" + ) + upload = next(step for step in steps if step.get("id") == "upload") + assert upload["with"]["path"] == "release-receipt.json" + assert upload["if"] == "inputs.hub && steps.receipt.outputs.has_event == 'true'" + + +@pytest.mark.parametrize("name", RELEASE_WORKFLOWS) +def test_actual_report_call_passes_complete_needs_without_a_verdict_override(name: str) -> None: + report = load(name)["jobs"]["report"] + assert report["with"]["orchestration"] == "${{ toJSON(needs) }}" + assert set(report["needs"]) == ( + {"authz", "reconcile"} if name == "pineforge-release.yml" else { + "authz", "claim-name", "manual-pr", "prepare", "build", "github-release", + "publish", "verify", + } + ) + if name == "release.yml": + assert report["if"] == "!cancelled() && needs.authz.result == 'success'" + assert ( + "needs.claim-name.result == 'failure' && 'claim-placeholder'" in report["with"]["stage"] + ) + + +def test_run_context_is_private_data_captured_before_artifact_transport() -> None: + workflow = load("release-report.yml") + assert workflow["on"]["workflow_call"]["inputs"]["orchestration"] == { + "type": "string", "required": "true", + } + job = workflow["jobs"]["report"] + assert job["env"]["RELEASE_NEEDS"] == "${{ inputs.orchestration }}" + steps = job["steps"] + capture = next( + step for step in steps if step.get("run") == "python -m release_chain.reporting context" + ) + download = next( + step for step in steps if step.get("uses", "").startswith("actions/download-artifact@") + ) + collect = next(step for step in steps if step.get("id") == "receipt") + private = next( + step for step in steps + if step.get("with", {}).get("name") == "release-run-context-${{ github.run_attempt }}" + ) + assert ( + steps.index(capture) < steps.index(download) < steps.index(collect) < steps.index(private) + ) + assert "if" not in capture and "continue-on-error" not in capture + assert private["if"] == "always()" + assert private["with"]["path"] == "run-context.json" + assert private["with"]["if-no-files-found"] == "error" + public = next(step for step in steps if step.get("id") == "upload") + assert public["with"]["path"] == "release-receipt.json" + assert public["with"]["name"] == "release-receipt-data" + + @pytest.mark.parametrize("name", RELEASE_WORKFLOWS) @pytest.mark.parametrize("mutation", ("missing-dependency", "unguarded-report", "wrong-bots")) def test_structure_checker_rejects_unsafe_mutations(name: str, mutation: str) -> None: @@ -190,8 +283,8 @@ def test_structure_checker_rejects_unsafe_mutations(name: str, mutation: str) -> before, after = " needs: authz\n", "" finding = "needs authz" elif mutation == "unguarded-report": - before = f" report:\n if: >-\n !cancelled() && {AUTHZ_PASSED} && " - after = " report:\n if: >-\n !cancelled() && " + before = f" report:\n if: >-\n !cancelled() && {AUTHZ_PASSED}" + after = " report:\n if: >-\n !cancelled()" finding = "report: authz condition" else: before = f'ALLOWED_BOTS: "{ALLOWED_BOTS}"'