From 8c8360c942ccb5ff360dd2de4ecff0a4787a9adb Mon Sep 17 00:00:00 2001 From: cflmy Date: Fri, 27 Nov 2026 22:05:58 +0800 Subject: [PATCH] ci: read verified Knowledge upstream tasks by their issue number --- .woodpecker/upstream.yml | 10 +++++ docs/ci.md | 5 +++ tools/ci/verify-upstream.py | 89 ++++++++++++++++++++++++++++++++----- 3 files changed, 92 insertions(+), 12 deletions(-) create mode 100644 .woodpecker/upstream.yml diff --git a/.woodpecker/upstream.yml b/.woodpecker/upstream.yml new file mode 100644 index 0000000..a97a98a --- /dev/null +++ b/.woodpecker/upstream.yml @@ -0,0 +1,10 @@ +when: + - event: manual + branch: master + evaluate: 'MARQDO_UPSTREAM_ISSUE != nil && MARQDO_UPSTREAM_ISSUE != ""' + +steps: + verify: + image: git.cflmy.cn/cflmy/marqdo-ci-rust:3.0.0-rc.5@sha256:73481a35339157b1a3f3b23076517e96e302fbb5158e6ffcb1582acf22c6af94 + commands: + - python3 tools/ci/verify-upstream.py --issue "$MARQDO_UPSTREAM_ISSUE" diff --git a/docs/ci.md b/docs/ci.md index 4fa5b66..7d6d457 100644 --- a/docs/ci.md +++ b/docs/ci.md @@ -41,6 +41,11 @@ Pass `--archive` to reuse locally staged bytes. The gate checks the Gitea tag, artifact digest and installed Bundle before running the existing `core` lane. Its output records the actual consumer commit and CLI identities for review. +The `upstream` Woodpecker workflow accepts `MARQDO_UPSTREAM_ISSUE` as the +Knowledge issue number and uses the protected root baseline, recorded input +digests and authorized forge account to resolve the candidate. This workflow +needs read access to public issues and release assets. + ## Gates executed by CI | Gate | Command | diff --git a/tools/ci/verify-upstream.py b/tools/ci/verify-upstream.py index 190fb6e..eb0c480 100644 --- a/tools/ci/verify-upstream.py +++ b/tools/ci/verify-upstream.py @@ -17,6 +17,9 @@ from pathlib import Path, PurePosixPath ROOT = Path(__file__).resolve().parents[2] FORGE = "https://git.cflmy.cn" REPO = "cflmy/marqdo-cli" +CONSUMER = "cflmy/marqdo-knowledge" +COORDINATOR = "marqdo-reconcile" +ROOT_REPO = "cflmy/marqdo" TARGET = "x86_64-unknown-linux-gnu" MAX_ARCHIVE = 64 * 1024 * 1024 MAX_EXPANDED = 256 * 1024 * 1024 @@ -36,6 +39,64 @@ def require(condition, message): raise VerificationError(message) +def forge_json(opener, path): + with opener.open(FORGE + "/api/v1" + path, timeout=30) as response: + body = response.read(2 * 1024 * 1024 + 1) + require(len(body) <= 2 * 1024 * 1024, "forge response exceeds the size limit") + return json.loads(body) + + +def coordinator_notes(opener, path): + issue = forge_json(opener, path) + require(issue["state"] == "open" and issue["user"]["login"] == COORDINATOR, + "expected an open task from the reconcile account") + notes = [issue["body"]] + for page in range(1, 11): + batch = forge_json(opener, f"{path}/comments?limit=50&page={page}") + notes.extend(item["body"] for item in batch if item["user"]["login"] == COORDINATOR) + if len(batch) < 50: + return notes + raise VerificationError("too many task comments to verify safely") + + +def issue_input(opener, number, consumer_sha): + notes = coordinator_notes(opener, f"/repos/{CONSUMER}/issues/{number}") + marker = notes[0].splitlines()[0] + require(re.fullmatch(r"", marker), + "task does not have a verified release marker") + body = next(note for note in reversed(notes) if note.splitlines()[:1] == [marker]) + start = "\n```json\n" + require(body.count(start) == 1 and body.endswith("\n```"), "task has no candidate input data") + header, _, encoded = body.partition(start) + inputs = json.loads(encoded.removesuffix("\n```")) + digest = hashlib.sha256(json.dumps(inputs, sort_keys=True, separators=(",", ":")).encode()).hexdigest() + require(f"- Input digest: {digest}" in header.splitlines() + and "- State: pending" in header.splitlines(), "task input digest or state differs") + require(inputs["consumer"] == {"id": "knowledge", "source_sha": consumer_sha}, + "task targets a different consumer commit") + release = inputs["release"] + tag, sha = release["tag"], release["source_sha"] + require(release["repository"] == REPO and release["index_sha256"], "task does not name a CLI release") + key = hashlib.sha256(f"{REPO}\n{tag}\n{sha}".encode()).hexdigest() + task_key = hashlib.sha256(f"{key}\nknowledge\n{digest}".encode()).hexdigest() + require(marker == f"" + and f"" in header.splitlines(), + "task identity differs from the candidate input") + root_id = re.search(rf"(?m)^- Root tracking issue: {re.escape(FORGE)}/{ROOT_REPO}/issues/([1-9][0-9]*)$", header) + base = re.search(r"(?m)^- Root candidate: ([0-9a-f]{40})$", header) + require(root_id and base, "task lacks its root tracker or accepted baseline") + branch = forge_json(opener, f"/repos/{ROOT_REPO}/branches/main") + require(branch["protected"] and branch["commit"]["id"] == base[1], "task root baseline is stale") + tracker = coordinator_notes(opener, f"/repos/{ROOT_REPO}/issues/{root_id[1]}") + require(tracker[0].splitlines()[:1] == [f""] + and any(f"" in note.splitlines() + for note in tracker), "root tracker does not verify this producer release") + name = f"marqdo-{tag[1:]}-{TARGET}.tar.gz" + assets = [asset for asset in release["assets"] if asset["name"] == name and asset["target"] == TARGET] + require(len(assets) == 1, "task does not specify one Linux CLI archive") + return tag, sha, assets[0]["sha256"] + + def copy_verified(stream, output, digest): actual = hashlib.sha256() size = 0 @@ -66,24 +127,28 @@ def unpack(archive, output, top): def main(): parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("--tag", required=True) - parser.add_argument("--source-sha", required=True) - parser.add_argument("--archive-sha256", required=True) + parser.add_argument("--issue", type=int, help="read an authoritative Knowledge propagation task") + parser.add_argument("--tag") + parser.add_argument("--source-sha") + parser.add_argument("--archive-sha256") parser.add_argument("--archive", type=Path, help="use already downloaded bytes") args = parser.parse_args() - if not (re.fullmatch(r"v[0-9]+\.[0-9]+\.[0-9]+(?:-[0-9A-Za-z.-]+)?", args.tag) - and re.fullmatch(r"[0-9a-f]{40}", args.source_sha) - and re.fullmatch(r"[0-9a-f]{64}", args.archive_sha256)): - parser.error("expected an exact tag, source SHA and Linux archive SHA-256") + if args.issue is not None and (args.issue < 1 or args.tag or args.source_sha or args.archive_sha256): + parser.error("--issue requires a positive number and supplies all candidate identities") opener = urllib.request.build_opener(NoRedirect()) - top = f"marqdo-{args.tag[1:]}-{TARGET}" try: consumer = subprocess.check_output(["git", "-C", ROOT, "rev-parse", "HEAD"], text=True).strip() require(not subprocess.check_output(["git", "-C", ROOT, "status", "--porcelain"], text=True), "consumer checkout has uncommitted changes") - with opener.open(f"{FORGE}/api/v1/repos/{REPO}/tags/{args.tag}", timeout=30) as response: - tag = json.loads(response.read(1024 * 1024)) + if args.issue is not None: + args.tag, args.source_sha, args.archive_sha256 = issue_input(opener, args.issue, consumer) + require(re.fullmatch(r"v[0-9]+\.[0-9]+\.[0-9]+(?:-[0-9A-Za-z.-]+)?", args.tag or "") + and re.fullmatch(r"[0-9a-f]{40}", args.source_sha or "") + and re.fullmatch(r"[0-9a-f]{64}", args.archive_sha256 or ""), + "expected an exact tag, source SHA and Linux archive SHA-256") + top = f"marqdo-{args.tag[1:]}-{TARGET}" + tag = forge_json(opener, f"/repos/{REPO}/tags/{args.tag}") require(tag["name"] == args.tag and tag["commit"]["sha"] == args.source_sha, "authoritative CLI tag does not match the candidate") with tempfile.TemporaryDirectory(prefix="marqdo-knowledge-upstream-") as temporary: @@ -106,10 +171,10 @@ def main(): subprocess.run([ROOT / "scripts/ci.sh", "core"], cwd=ROOT, env=env, check=True) require(subprocess.check_output(["git", "-C", ROOT, "rev-parse", "HEAD"], text=True).strip() == consumer, "consumer identity changed during validation") - print(json.dumps({"consumer_sha": consumer, + print(json.dumps({"consumer_sha": consumer, "task_issue": args.issue, "cli_tag": args.tag, "cli_source_sha": sha, "archive_sha256": args.archive_sha256, "target": TARGET, "status": "passed"}, sort_keys=True)) - except (VerificationError, OSError, ValueError, KeyError, TypeError, + except (VerificationError, OSError, ValueError, KeyError, IndexError, TypeError, tarfile.TarError, subprocess.CalledProcessError) as error: print(f"upstream verification: {error}", file=sys.stderr) return 1