Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix(release): bind PyPI monitor to its dispatch on main (#7134)

LearningCircuit525920cfbc71
diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml
index 3733cff21b..728ff2f64a 100644
--- a/.github/workflows/publish.yml
+++ b/.github/workflows/publish.yml
@@ -1,5 +1,5 @@
name: Publish to PyPI
-run-name: PyPI release ${{ github.event.client_payload.run_id }} artifact ${{ github.event.client_payload.artifact_id }}
+run-name: PyPI release ${{ github.event.client_payload.run_id }} artifact ${{ github.event.client_payload.artifact_id }} dispatch ${{ github.event.client_payload.dispatch_id }}
# Keep this top-level workflow for PyPI Trusted Publishing. The release gate
# builds and tests the distributions; this workflow verifies and publishes
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index 301aed9799..eae0cf2eaa 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -1565,7 +1565,7 @@ jobs:
contents: read
outputs:
dispatch_time: ${{ steps.dispatch-time.outputs.timestamp }}
- publish_title: PyPI release ${{ github.run_id }} artifact ${{ needs.release-gate.outputs.package_artifact_id }}
+ publish_title: ${{ steps.dispatch.outputs.publish_title }}
steps:
- name: Harden the runner (Audit all outbound calls)
@@ -1580,6 +1580,7 @@ jobs:
run: echo "timestamp=$(date -u +%Y-%m-%dT%H:%M:%SZ)" >> "$GITHUB_OUTPUT"
- name: Trigger PyPI publish workflow
+ id: dispatch
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
env:
RELEASE_TAG: ${{ needs.build.outputs.tag }}
@@ -1607,6 +1608,10 @@ jobs:
!/^[0-9a-f]{64}$/.test(process.env.PACKAGE_MANIFEST_SHA256 || '')) {
throw new Error('Release gate did not return verified package outputs');
}
+ // A rerun of this job must identify its own dispatch, even when
+ // the release run and tested artifact are unchanged.
+ const dispatchId = require('node:crypto').randomUUID();
+ const publishTitle = `PyPI release ${context.runId} artifact ${process.env.PACKAGE_ARTIFACT_ID} dispatch ${dispatchId}`;
await github.rest.repos.createDispatchEvent({
owner: context.repo.owner,
repo: context.repo.repo,
@@ -1616,10 +1621,12 @@ jobs:
sha: process.env.RELEASE_SHA,
run_id: String(context.runId),
artifact_id: process.env.PACKAGE_ARTIFACT_ID,
+ dispatch_id: dispatchId,
manifest_sha256: process.env.PACKAGE_MANIFEST_SHA256,
prerelease: false
}
});
+ core.setOutput('publish_title', publishTitle);
console.log('Triggered PyPI publish workflow');
# Block on the dispatched publish.yml run so create-release downstream
@@ -1638,7 +1645,7 @@ jobs:
timeout-minutes: 120
permissions:
contents: read
- actions: read # Required for gh run list
+ actions: read # Required for gh run list and gh run view
issues: write
steps:
@@ -1660,11 +1667,7 @@ jobs:
DISPATCH_TIME: ${{ needs.trigger-pypi.outputs.dispatch_time }}
PUBLISH_TITLE: ${{ needs.trigger-pypi.outputs.publish_title }}
run: |
- # pipefail is essential here: the poll loop pipes `gh run list`
- # into `jq`, and without pipefail a transient `gh` failure
- # (network, auth, rate limit) is swallowed by `jq` returning
- # empty input, causing the loop to spin silently for the full
- # poll budget rather than surfacing the error immediately.
+ # pipefail makes discovery fail promptly if gh or jq fails.
set -euo pipefail
echo "Monitoring publish.yml for tag $RELEASE_TAG (dispatched at $DISPATCH_TIME)..."
@@ -1686,30 +1689,51 @@ jobs:
max_wait=6300
start=$SECONDS
conclusion=""
+ publish_run_id=""
while [ $((SECONDS - start)) -lt "$max_wait" ]; do
- # Compare timestamps numerically — lexicographic comparison
- # breaks when GitHub returns sub-second precision (e.g.
- # "...:56.500Z" sorts before "...:56Z"). fromdateiso8601
- # doesn't accept fractional seconds, so strip them first. The
- # 60s buffer tolerates minor clock skew between the runner and
- # GitHub's API. Stderr is intentionally NOT redirected so gh
- # failures stay visible in the runner log.
- run=$(gh run list --workflow=publish.yml --limit=20 --json status,conclusion,createdAt,displayTitle \
- | jq -r --arg since "$DISPATCH_TIME" --arg title "$PUBLISH_TITLE" '
- def to_epoch: sub("\\.[0-9]+Z$"; "Z") | fromdateiso8601;
- (($since | to_epoch) - 60) as $s
- | [.[] | select(.displayTitle == $title and (.createdAt | to_epoch) >= $s)] | .[0]
- ')
-
- if [ "$run" = "null" ] || [ -z "$run" ]; then
- echo "publish.yml: waiting for run to appear..." >&2
- sleep 30
- continue
+ if [ -z "$publish_run_id" ]; then
+ # The title contains a fresh UUID from this dispatch, so a
+ # prior attempt (or another release) cannot satisfy it. Keep
+ # the time bound for stale/replayed dispatches; compare epochs
+ # because GitHub may return fractional seconds.
+ matches=$(gh run list --workflow=publish.yml --limit=100 --json databaseId,createdAt,displayTitle \
+ | jq -c --arg since "$DISPATCH_TIME" --arg title "$PUBLISH_TITLE" '
+ def to_epoch: sub("\\.[0-9]+Z$"; "Z") | fromdateiso8601;
+ (($since | to_epoch) - 60) as $s
+ | [.[] | select(.displayTitle == $title and (.createdAt | to_epoch) >= $s)]
+ ')
+ match_count=$(jq 'length' <<< "$matches")
+ if [ "$match_count" -gt 1 ]; then
+ echo "::error::Multiple publish.yml runs matched this dispatch" >&2
+ echo "pypi_result=ambiguous" >> "$GITHUB_OUTPUT"
+ exit 1
+ fi
+ if [ "$match_count" -eq 0 ]; then
+ echo "publish.yml: waiting for run to appear..." >&2
+ sleep 30
+ continue
+ fi
+ publish_run_id=$(jq -r '.[0].databaseId' <<< "$matches")
+ if ! [[ "$publish_run_id" =~ ^[1-9][0-9]*$ ]]; then
+ echo "::error::Matched publish.yml run has an invalid ID" >&2
+ echo "pypi_result=invalid_run_id" >> "$GITHUB_OUTPUT"
+ exit 1
+ fi
+ echo "Monitoring publish.yml run $publish_run_id" >&2
fi
- status=$(echo "$run" | jq -r '.status')
- conclusion=$(echo "$run" | jq -r '.conclusion')
+ # Once selected, follow this run ID only. A new or reordered
+ # run in the list cannot change the publication outcome.
+ run=$(gh run view "$publish_run_id" --json databaseId,displayTitle,status,conclusion)
+ if [ "$(jq -r '.databaseId' <<< "$run")" != "$publish_run_id" ] ||
+ [ "$(jq -r '.displayTitle' <<< "$run")" != "$PUBLISH_TITLE" ]; then
+ echo "::error::Selected publish.yml run identity changed" >&2
+ echo "pypi_result=identity_mismatch" >> "$GITHUB_OUTPUT"
+ exit 1
+ fi
+ status=$(jq -r '.status' <<< "$run")
+ conclusion=$(jq -r '.conclusion // empty' <<< "$run")
if [ "$status" = "completed" ]; then
echo "publish.yml: $conclusion" >&2
diff --git a/changelog.d/6976.bugfix.md b/changelog.d/6976.bugfix.md
new file mode 100644
index 0000000000..44a418cecd
--- /dev/null
+++ b/changelog.d/6976.bugfix.md
@@ -0,0 +1 @@
+Match each PyPI publish run to its unique release dispatch and monitor that exact run, so unrelated or earlier successful runs cannot authorize a GitHub Release.
diff --git a/tests/ci/test_pypi_monitor_correlation.py b/tests/ci/test_pypi_monitor_correlation.py
new file mode 100644
index 0000000000..0918490b6c
--- /dev/null
+++ b/tests/ci/test_pypi_monitor_correlation.py
@@ -0,0 +1,297 @@
+"""Exercise the release workflow's PyPI dispatch and monitor contract."""
+
+# allow: no-sut-import — these tests execute workflow shell with a stub gh CLI.
+
+from __future__ import annotations
+
+import json
+import os
+import shutil
+import subprocess
+from uuid import UUID
+from pathlib import Path
+
+import pytest
+import yaml
+
+
+ROOT = Path(__file__).resolve().parents[2]
+RELEASE = ROOT / ".github" / "workflows" / "release.yml"
+PUBLISH = ROOT / ".github" / "workflows" / "publish.yml"
+TITLE = "PyPI release 123 artifact 456 dispatch 11111111-2222-4333-8444-555555555555"
+OLDER_TITLE = "PyPI release 123 artifact 456 dispatch aaaaaaaa-bbbb-4ccc-8ddd-eeeeeeeeeeee"
+
+
+def _jobs(path: Path) -> dict:
+ return yaml.safe_load(path.read_text(encoding="utf-8"))["jobs"]
+
+
+def _run(title: str, run_id: int, conclusion: str | None = None) -> dict:
+ return {
+ "databaseId": run_id,
+ "displayTitle": title,
+ "createdAt": "2026-09-29T12:00:10Z",
+ "status": "completed" if conclusion is not None else "in_progress",
+ "conclusion": conclusion,
+ }
+
+
+def _monitor(
+ tmp_path: Path, lists: list[list[dict]], views: dict[int, list[dict]]
+):
+ step = next(
+ step
+ for step in _jobs(RELEASE)["monitor-pypi"]["steps"]
+ if step.get("name") == "Wait for PyPI publish workflow to complete"
+ )
+ script = step["run"]
+ assert script.count("max_wait=6300") == 1
+ # Advance Bash's wall clock without waiting for the real 30s sleeps.
+ script = "sleep() { SECONDS=$((SECONDS + 1)); }\n" + script.replace(
+ "max_wait=6300", "max_wait=3"
+ )
+
+ fixture = tmp_path / "fixture.json"
+ fixture.write_text(
+ json.dumps({"lists": lists, "views": views}), encoding="utf-8"
+ )
+ state = tmp_path / "state.json"
+ state.write_text(
+ json.dumps({"list_calls": 0, "view_ids": []}), encoding="utf-8"
+ )
+ gh = tmp_path / "gh"
+ gh.write_text(
+ """#!/usr/bin/env python3
+import json
+import os
+import sys
+from pathlib import Path
+
+fixture = json.loads(Path(os.environ['GH_FIXTURE']).read_text())
+path = Path(os.environ['GH_STATE'])
+state = json.loads(path.read_text())
+if sys.argv[1:3] == ['run', 'list']:
+ index = state['list_calls']
+ state['list_calls'] += 1
+ result = fixture['lists'][min(index, len(fixture['lists']) - 1)]
+elif sys.argv[1:3] == ['run', 'view']:
+ run_id = sys.argv[3]
+ state['view_ids'].append(run_id)
+ attempts = fixture['views'][run_id]
+ index = state['view_ids'].count(run_id) - 1
+ result = attempts[min(index, len(attempts) - 1)]
+else:
+ raise SystemExit(f'unexpected gh arguments: {sys.argv[1:]}')
+path.write_text(json.dumps(state))
+print(json.dumps(result))
+""",
+ encoding="utf-8",
+ )
+ gh.chmod(0o755)
+
+ output = tmp_path / "github-output"
+ env = {
+ **os.environ,
+ "PATH": f"{tmp_path}{os.pathsep}{os.environ['PATH']}",
+ "GH_FIXTURE": str(fixture),
+ "GH_STATE": str(state),
+ "GITHUB_OUTPUT": str(output),
+ "DISPATCH_TIME": "2026-09-29T12:00:00Z",
+ "PUBLISH_TITLE": TITLE,
+ "RELEASE_TAG": "v2.0.0",
+ }
+ result = subprocess.run(
+ ["bash", "-c", script],
+ check=False,
+ capture_output=True,
+ text=True,
+ env=env,
+ timeout=10,
+ )
+ return (
+ result,
+ output.read_text(encoding="utf-8"),
+ json.loads(state.read_text()),
+ )
+
+
+def test_dispatch_has_unique_title_bound_to_payload_and_monitor():
+ release = _jobs(RELEASE)
+ trigger = release["trigger-pypi"]
+ dispatch = next(
+ step for step in trigger["steps"] if step.get("id") == "dispatch"
+ )
+ script = dispatch["with"]["script"]
+ publish_name = yaml.safe_load(PUBLISH.read_text(encoding="utf-8"))[
+ "run-name"
+ ]
+
+ assert "require('node:crypto').randomUUID()" in script
+ assert "dispatch_id: dispatchId" in script
+ assert "core.setOutput('publish_title', publishTitle)" in script
+ assert "await github.rest.repos.createDispatchEvent" in script
+ assert trigger["outputs"]["publish_title"] == (
+ "${{ steps.dispatch.outputs.publish_title }}"
+ )
+ assert "${{ github.event.client_payload.dispatch_id }}" in publish_name
+ assert "${{ github.event.client_payload.run_id }}" in publish_name
+ assert "${{ github.event.client_payload.artifact_id }}" in publish_name
+ monitor_step = next(
+ step
+ for step in release["monitor-pypi"]["steps"]
+ if step.get("name") == "Wait for PyPI publish workflow to complete"
+ )
+ assert monitor_step["env"]["PUBLISH_TITLE"] == (
+ "${{ needs.trigger-pypi.outputs.publish_title }}"
+ )
+
+
+ shutil.which("node") is None, reason="Node.js is unavailable"
+)
+def test_each_trigger_attempt_emits_a_distinct_matching_dispatch_title():
+ trigger = _jobs(RELEASE)["trigger-pypi"]
+ script = next(
+ step["with"]["script"]
+ for step in trigger["steps"]
+ if step.get("id") == "dispatch"
+ )
+ harness = """
+const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor;
+const dispatches = [];
+const outputs = {};
+const github = {
+ rest: { repos: { createDispatchEvent: async (payload) => dispatches.push(payload) } }
+};
+const context = { repo: { owner: 'LearningCircuit', repo: 'local-deep-research' }, runId: 123 };
+const core = { setOutput: (key, value) => { outputs[key] = value; } };
+(async () => {
+ await new AsyncFunction('github', 'context', 'core', 'require', process.env.DISPATCH_SCRIPT)(
+ github, context, core, require
+ );
+ console.log(JSON.stringify({dispatches, outputs}));
+})().catch((error) => { console.error(error); process.exitCode = 1; });
+"""
+ env = {
+ **os.environ,
+ "DISPATCH_SCRIPT": script,
+ "RELEASE_TAG": "v2.0.0",
+ "RELEASE_SHA": "a" * 40,
+ "PACKAGE_ARTIFACT_ID": "456",
+ "PACKAGE_MANIFEST_SHA256": "b" * 64,
+ }
+ attempts = []
+ for _ in range(2):
+ result = subprocess.run(
+ ["node", "-e", harness],
+ check=True,
+ capture_output=True,
+ text=True,
+ env=env,
+ timeout=10,
+ )
+ attempts.append(json.loads(result.stdout.splitlines()[-1]))
+
+ ids = []
+ for attempt in attempts:
+ payload = attempt["dispatches"][0]["client_payload"]
+ dispatch_id = payload["dispatch_id"]
+ assert UUID(dispatch_id).version == 4
+ assert attempt["outputs"]["publish_title"] == (
+ f"PyPI release 123 artifact 456 dispatch {dispatch_id}"
+ )
+ ids.append(dispatch_id)
+ assert ids[0] != ids[1]
+
+
+ shutil.which("jq") is None, reason="workflow shell needs jq"
+)
+def test_unrelated_newer_success_cannot_hide_intended_failure(tmp_path):
+ unrelated = _run(OLDER_TITLE, 900, "success")
+ intended = _run(TITLE, 901, "failure")
+
+ result, output, state = _monitor(
+ tmp_path, [[unrelated, intended]], {901: [intended]}
+ )
+
+ assert result.returncode == 1, result.stderr
+ assert output == "pypi_result=failure\n"
+ assert state["view_ids"] == ["901"]
+
+
+ shutil.which("jq") is None, reason="workflow shell needs jq"
+)
+def test_monitor_waits_for_its_dispatch_while_other_run_succeeds(tmp_path):
+ unrelated = _run(OLDER_TITLE, 900, "success")
+
+ result, output, state = _monitor(tmp_path, [[unrelated]], {})
+
+ assert result.returncode == 1, result.stderr
+ assert output == "pypi_result=timed_out\n"
+ assert state["view_ids"] == []
+
+
+ shutil.which("jq") is None, reason="workflow shell needs jq"
+)
+def test_selected_run_stays_pinned_across_polls(tmp_path):
+ pending = _run(TITLE, 901)
+ failed = _run(TITLE, 901, "failure")
+ unrelated = _run(OLDER_TITLE, 902, "success")
+
+ result, output, state = _monitor(
+ tmp_path,
+ [[pending], [unrelated]],
+ {901: [pending, failed]},
+ )
+
+ assert result.returncode == 1, result.stderr
+ assert output == "pypi_result=failure\n"
+ assert state["list_calls"] == 1
+ assert state["view_ids"] == ["901", "901"]
+
+
+ shutil.which("jq") is None, reason="workflow shell needs jq"
+)
+def test_matching_success_ignores_unrelated_failure(tmp_path):
+ intended = _run(TITLE, 901, "success")
+ unrelated = _run(OLDER_TITLE, 900, "failure")
+
+ result, output, state = _monitor(
+ tmp_path, [[unrelated, intended]], {901: [intended]}
+ )
+
+ assert result.returncode == 0, result.stderr
+ assert output == "pypi_result=success\n"
+ assert state["view_ids"] == ["901"]
+
+
+ shutil.which("jq") is None, reason="workflow shell needs jq"
+)
+def test_ambiguous_dispatch_identity_fails_closed(tmp_path):
+ first = _run(TITLE, 901, "success")
+ replay = _run(TITLE, 902, "success")
+
+ result, output, state = _monitor(tmp_path, [[first, replay]], {})
+
+ assert result.returncode == 1, result.stderr
+ assert output == "pypi_result=ambiguous\n"
+ assert state["view_ids"] == []
+
+
+ shutil.which("jq") is None, reason="workflow shell needs jq"
+)
+def test_run_view_identity_mismatch_fails_closed(tmp_path):
+ intended = _run(TITLE, 901)
+ wrong = _run(TITLE, 902, "success")
+
+ result, output, state = _monitor(tmp_path, [[intended]], {901: [wrong]})
+
+ assert result.returncode == 1, result.stderr
+ assert output == "pypi_result=identity_mismatch\n"
+ assert state["view_ids"] == ["901"]