-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_workflows.py
More file actions
1822 lines (1732 loc) · 74.6 KB
/
Copy pathcheck_workflows.py
File metadata and controls
1822 lines (1732 loc) · 74.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""Enforce release-workflow trust invariants that actionlint cannot prove."""
from __future__ import annotations
import argparse
import json
import re
from collections.abc import Iterator
from pathlib import Path
from typing import cast
import yaml
try:
from ._checks import PROJECT_ROOT, CheckError
except ImportError: # Direct execution from the repository root.
from _checks import PROJECT_ROOT, CheckError
RELEASE_PLEASE_BASELINE_SHA = "31b68904141489ca04932edbf305ccf88af09372"
RELEASE_PLEASE_LOCK_JSONPATH = "$.package[?(@.name.value == 'cometapi')].version"
RELEASE_PLEASE_ACTION_SHA = "5c625bfb5d1ff62eadeeb3772007f7f66fdcf071"
RELEASE_PLEASE_BRIDGE_VERSION = "0.1.0-alpha.1"
RELEASE_PLEASE_STABLE_VERSION_PATTERN = re.compile(r"0\.1\.(?:0|[1-9][0-9]*)")
RELEASE_PLEASE_VERIFY_COMMAND = """\
test -n "$EXPECTED_TAG"
test -n "$EXPECTED_SHA"
release=""
for attempt in $(seq 1 12); do
release=$(gh api "repos/${{ github.repository }}/releases/tags/$EXPECTED_TAG") || true
if test -n "$release" && test "$(jq -r .immutable <<<"$release")" = "true"; then
break
fi
if test "$attempt" -ge 12; then
echo "release did not become immutable" >&2
exit 1
fi
sleep 5
done
test "$(jq -r .tag_name <<<"$release")" = "$EXPECTED_TAG"
test "$(jq -r .draft <<<"$release")" = "false"
test "$(jq -r .prerelease <<<"$release")" = "false"
test "$(jq -r .immutable <<<"$release")" = "true"
ref=$(gh api "repos/${{ github.repository }}/git/ref/tags/$EXPECTED_TAG")
tag_type=$(jq -r .object.type <<<"$ref")
tag_sha=$(jq -r .object.sha <<<"$ref")
if test "$tag_type" = "tag"; then
tag_sha=$(gh api "repos/${{ github.repository }}/git/tags/$tag_sha" --jq .object.sha)
else
test "$tag_type" = "commit"
fi
test "$tag_sha" = "$EXPECTED_SHA"
{
echo "release-tag=$EXPECTED_TAG"
echo "release-sha=$EXPECTED_SHA"
echo "release-verified=true"
} >> "$GITHUB_OUTPUT"
"""
PUBLISH_JOB_NAMES = {
"release-please",
"verify-recovery",
"select-release",
"build",
"release-live-smoke",
"publish",
"verify-registry",
}
RELEASE_PLEASE_JOB_CONDITION = (
"github.run_attempt == 1 && github.event_name == 'push' && "
"vars.RELEASE_PLEASE_ENABLED == 'true'"
)
RECOVERY_JOB_CONDITION = (
"github.run_attempt == 1 && github.event_name == 'workflow_dispatch' && "
"github.ref == format('refs/heads/{0}', github.event.repository.default_branch) && "
"vars.RELEASE_RECOVERY_TAG == inputs.release-tag && "
"vars.RELEASE_RECOVERY_SHA == inputs.release-sha"
)
SELECT_RELEASE_CONDITION = (
"always() && !cancelled() && github.run_attempt == 1 && "
"( ( github.event_name == 'push' && needs.release-please.result == 'success' && "
"needs.release-please.outputs.release-created == 'true' && "
"needs.release-please.outputs.release-verified == 'true' ) || "
"( github.event_name == 'workflow_dispatch' && "
"needs.verify-recovery.result == 'success' ) )"
)
BUILD_JOB_CONDITION = (
"always() && !cancelled() && github.run_attempt == 1 && "
"needs.select-release.result == 'success'"
)
RELEASE_LIVE_JOB_CONDITION = (
"always() && !cancelled() && github.run_attempt == 1 && needs.build.result == 'success'"
)
PUBLISH_JOB_CONDITION = (
"always() && !cancelled() && github.run_attempt == 1 && "
"needs.build.result == 'success' && "
"needs.release-live-smoke.result == 'success'"
)
REGISTRY_JOB_CONDITION = (
"always() && !cancelled() && github.run_attempt == 1 && "
"needs.build.result == 'success' && needs.publish.result == 'success'"
)
SELECT_RELEASE_COMMAND = """\
case "$EVENT_NAME" in
push)
release_sha=$RELEASE_PLEASE_SHA
release_tag=$RELEASE_PLEASE_TAG
;;
workflow_dispatch)
release_sha=$RECOVERY_SHA
release_tag=$RECOVERY_TAG
;;
*)
exit 1
;;
esac
test -n "$release_sha"
test -n "$release_tag"
{
echo "release-sha=$release_sha"
echo "release-tag=$release_tag"
} >> "$GITHUB_OUTPUT"
"""
def _mapping(value: object, label: str) -> dict[str, object]:
if not isinstance(value, dict):
raise CheckError(f"{label} must be a mapping")
raw = cast(dict[object, object], value)
if not all(isinstance(key, str) for key in raw):
raise CheckError(f"{label} must use scalar string keys")
mapping = {cast(str, key): item for key, item in raw.items()}
if "<<" in mapping:
raise CheckError(f"{label} must not use YAML merge keys")
return mapping
def _sequence(value: object, label: str) -> list[object]:
if not isinstance(value, list):
raise CheckError(f"{label} must be a sequence")
return cast(list[object], value)
def _scalar(value: object, label: str) -> str:
if not isinstance(value, str):
raise CheckError(f"{label} must be a scalar string")
return value
def _require_exact_keys(mapping: dict[str, object], expected: set[str], label: str) -> None:
actual = set(mapping)
if actual != expected:
missing = ", ".join(sorted(expected - actual)) or "none"
unexpected = ", ".join(sorted(actual - expected)) or "none"
raise CheckError(
f"{label} keys do not match the reviewed contract "
f"(missing: {missing}; unexpected: {unexpected})"
)
def _load_workflow(text: str, source: str) -> dict[str, object]:
try:
loaded: object = yaml.load(text, Loader=yaml.BaseLoader)
except yaml.YAMLError as error:
raise CheckError(f"{source} is not valid YAML: {error}") from error
return _mapping(loaded, source)
def _workflow_job(workflow: dict[str, object], name: str, source: str) -> dict[str, object]:
jobs = _mapping(workflow.get("jobs"), f"{source} jobs")
if name not in jobs:
raise CheckError(f"{source} has no {name!r} job")
return _mapping(jobs[name], f"{source} {name!r} job")
def _workflow_steps(job: dict[str, object], label: str) -> list[dict[str, object]]:
return [
_mapping(item, f"{label} step {index}")
for index, item in enumerate(_sequence(job.get("steps"), f"{label} steps"))
]
def _walk_mappings(value: object, label: str) -> Iterator[dict[str, object]]:
if isinstance(value, dict):
mapping = _mapping(cast(dict[object, object], value), label)
yield mapping
for key, child in mapping.items():
yield from _walk_mappings(child, f"{label}.{key}")
elif isinstance(value, list):
for index, child in enumerate(cast(list[object], value)):
yield from _walk_mappings(child, f"{label}[{index}]")
def _walk_scalars(value: object) -> Iterator[str]:
if isinstance(value, str):
yield value
elif isinstance(value, dict):
for child in cast(dict[object, object], value).values():
yield from _walk_scalars(child)
elif isinstance(value, list):
for child in cast(list[object], value):
yield from _walk_scalars(child)
def _secret_references(value: object) -> list[str]:
pattern = re.compile(r"\$\{\{[^}]*\bsecrets\b[^}]*\}\}", flags=re.IGNORECASE)
return [scalar for scalar in _walk_scalars(value) if pattern.search(scalar)]
def _require_unconditional(mapping: dict[str, object], label: str) -> None:
if "if" in mapping:
raise CheckError(f"{label} must not be conditional")
if "continue-on-error" in mapping:
raise CheckError(f"{label} must not allow failure")
if "defaults" in mapping:
raise CheckError(f"{label} must not override command defaults")
if "shell" in mapping:
raise CheckError(f"{label} must not override the command shell")
def _require_blocking_job(job: dict[str, object], label: str) -> None:
_require_unconditional(job, label)
if "permissions" in job:
raise CheckError(f"{label} must not override credential-free workflow permissions")
if "env" in job:
raise CheckError(f"{label} must not override the reviewed CI environment")
for index, step in enumerate(_workflow_steps(job, label)):
_require_unconditional(step, f"{label} step {index}")
if "env" in step:
raise CheckError(f"{label} step {index} must not override the CI environment")
def _run_step(job: dict[str, object], command: str, label: str) -> tuple[int, dict[str, object]]:
matches = [
(index, step)
for index, step in enumerate(_workflow_steps(job, label))
if step.get("run") == command
]
if len(matches) != 1:
raise CheckError(f"{label} must contain exactly one active run step: {command}")
index, step = matches[0]
_require_unconditional(step, f"{label} run step {command!r}")
return index, step
def _action_step(job: dict[str, object], action: str, label: str) -> tuple[int, dict[str, object]]:
matches: list[tuple[int, dict[str, object]]] = []
for index, step in enumerate(_workflow_steps(job, label)):
uses = step.get("uses")
if isinstance(uses, str) and uses.rpartition("@")[0] == action:
matches.append((index, step))
if len(matches) != 1:
raise CheckError(f"{label} must contain exactly one {action} step")
index, step = matches[0]
_require_unconditional(step, f"{label} {action} step")
return index, step
def _named_step(job: dict[str, object], name: str, label: str) -> tuple[int, dict[str, object]]:
matches = [
(index, step)
for index, step in enumerate(_workflow_steps(job, label))
if step.get("name") == name
]
if len(matches) != 1:
raise CheckError(f"{label} must contain exactly one {name!r} step")
index, step = matches[0]
_require_unconditional(step, f"{label} {name!r} step")
return index, step
def _named_run_step(
job: dict[str, object], name: str, command: str, label: str
) -> tuple[int, dict[str, object]]:
index, step = _named_step(job, name, label)
if step.get("run") != command or "uses" in step:
raise CheckError(f"{label} {name!r} step must run exactly: {command}")
return index, step
def _named_action_step(
job: dict[str, object], name: str, action: str, label: str
) -> tuple[int, dict[str, object]]:
index, step = _action_step(job, action, label)
if step.get("name") != name or "run" in step:
raise CheckError(f"{label} must use {action} in its {name!r} step")
return index, step
def _require_step_names(job: dict[str, object], expected: list[str], label: str) -> None:
names = [step.get("name") for step in _workflow_steps(job, label)]
if names != expected:
raise CheckError(f"{label} steps must match the reviewed sequence")
def _require_needs(job: dict[str, object], expected: list[str], label: str) -> None:
value = job.get("needs")
if isinstance(value, str):
actual = [value]
else:
actual = [
_scalar(item, f"{label} dependency")
for item in _sequence(value, f"{label} dependencies")
]
if actual != expected:
raise CheckError(f"{label} must depend on {', '.join(expected)}")
def _require_permissions(mapping: dict[str, object], expected: dict[str, str], label: str) -> None:
permissions = _mapping(mapping.get("permissions"), f"{label} permissions")
if permissions != expected:
raise CheckError(f"{label} permissions do not match the reviewed least-privilege map")
def _require_options(step: dict[str, object], expected: dict[str, str], label: str) -> None:
options = _mapping(step.get("with"), f"{label} options")
if options != expected:
raise CheckError(f"{label} options do not match the reviewed contract")
def _require_step_environments(
job: dict[str, object], expected: dict[str, dict[str, str]], label: str
) -> None:
for index, step in enumerate(_workflow_steps(job, label)):
name = _scalar(step.get("name"), f"{label} step {index} name")
if name in expected:
environment = _mapping(step.get("env"), f"{label} {name!r} environment")
if environment != expected[name]:
raise CheckError(f"{label} {name!r} environment does not match the contract")
elif "env" in step:
raise CheckError(f"{label} {name!r} step must not override the environment")
def _require_step_working_directories(
job: dict[str, object], expected: dict[str, str], label: str
) -> None:
matched: set[str] = set()
for index, step in enumerate(_workflow_steps(job, label)):
name = _scalar(step.get("name"), f"{label} step {index} name")
if name in expected:
if step.get("working-directory") != expected[name]:
raise CheckError(f"{label} {name!r} step must use its reviewed working directory")
matched.add(name)
elif "working-directory" in step:
raise CheckError(f"{label} {name!r} step must run from the checked-out repository root")
if matched != set(expected):
raise CheckError(f"{label} reviewed working-directory steps are missing")
def _action_references(workflow: dict[str, object], source: str) -> Iterator[tuple[str, str]]:
jobs = _mapping(workflow.get("jobs"), f"{source} jobs")
for job_name, value in jobs.items():
job = _mapping(value, f"{source} {job_name!r} job")
if "uses" in job:
yield (
f"{source} {job_name!r} job",
_scalar(job["uses"], f"{source} {job_name!r} job uses"),
)
if "steps" not in job:
continue
for index, step in enumerate(_workflow_steps(job, f"{source} {job_name!r} job")):
if "uses" in step:
yield (
f"{source} {job_name!r} step {index}",
_scalar(step["uses"], f"{source} {job_name!r} step {index} uses"),
)
def check_action_pins(text: str, source: str) -> None:
workflow = _load_workflow(text, source)
for label, value in _action_references(workflow, source):
if value.startswith("./"):
continue
if value.startswith("docker://"):
raise CheckError(f"{label}: Docker action references are not permitted")
action, separator, reference = value.rpartition("@")
if not separator or not action:
raise CheckError(f"{label}: external action reference {value!r} has no ref")
if re.fullmatch(r"[0-9a-f]{40}", reference) is None:
raise CheckError(f"{label}: {action} must be pinned to a full commit SHA")
def check_ci_workflow(text: str) -> None:
"""Require credential-free CI to cover every private-validation evidence layer."""
workflow = _load_workflow(text, "CI workflow")
_require_permissions(workflow, {"contents": "read"}, "credential-free CI")
if "defaults" in workflow:
raise CheckError("credential-free CI must not override command defaults")
if _mapping(workflow.get("env"), "CI workflow environment") != {"UV_VERSION": "0.11.8"}:
raise CheckError("credential-free CI must retain only its pinned uv frontend version")
triggers = _mapping(workflow.get("on"), "CI workflow triggers")
if set(triggers) != {"pull_request", "push", "schedule"}:
raise CheckError("CI triggers must equal pull requests, main pushes, and weekly schedule")
if triggers["pull_request"] != "":
raise CheckError("CI pull-request validation must not use activity or path filters")
push = _mapping(triggers["push"], "CI push trigger")
if set(push) != {"branches"}:
raise CheckError("CI main-push validation must not use path, tag, or activity filters")
branches = [
_scalar(item, "CI push branch")
for item in _sequence(push.get("branches"), "CI push branches")
]
if branches != ["main"]:
raise CheckError("CI must run only for default-branch pushes")
schedule = _sequence(triggers["schedule"], "CI schedule trigger")
if schedule != [{"cron": "23 4 * * 1"}]:
raise CheckError("CI latest-OpenAI canary must run on the reviewed weekly schedule")
concurrency = _mapping(workflow.get("concurrency"), "CI workflow concurrency")
if concurrency != {
"group": "ci-${{ github.workflow }}-${{ github.ref }}",
"cancel-in-progress": "true",
}:
raise CheckError("CI must retain reviewed per-ref cancellation")
_require_exact_keys(
workflow,
{"name", "on", "permissions", "concurrency", "env", "jobs"},
"CI workflow",
)
if _secret_references(workflow):
raise CheckError("credential-free CI must not reference repository secrets")
jobs = _mapping(workflow.get("jobs"), "CI workflow jobs")
expected_jobs = {
"quality",
"locked-runtime",
"minimum-openai",
"latest-openai",
"package",
"standalone",
}
if set(jobs) != expected_jobs:
raise CheckError("CI jobs must match the reviewed validation chain")
quality = _workflow_job(workflow, "quality", "CI workflow")
locked_runtime = _workflow_job(workflow, "locked-runtime", "CI workflow")
minimum_openai = _workflow_job(workflow, "minimum-openai", "CI workflow")
latest_openai = _workflow_job(workflow, "latest-openai", "CI workflow")
package = _workflow_job(workflow, "package", "CI workflow")
standalone = _workflow_job(workflow, "standalone", "CI workflow")
expected_job_keys = {
"quality": {"name", "runs-on", "timeout-minutes", "steps"},
"locked-runtime": {"name", "runs-on", "timeout-minutes", "strategy", "steps"},
"minimum-openai": {"name", "runs-on", "timeout-minutes", "steps"},
"latest-openai": {"name", "if", "runs-on", "timeout-minutes", "steps"},
"package": {"name", "needs", "runs-on", "timeout-minutes", "steps"},
"standalone": {"name", "needs", "runs-on", "timeout-minutes", "steps"},
}
for name, job in (
("quality", quality),
("locked-runtime", locked_runtime),
("minimum-openai", minimum_openai),
("package", package),
("standalone", standalone),
):
_require_blocking_job(job, f"CI {name!r} job")
if latest_openai.get("if") != (
"github.event_name == 'schedule' || github.actor == 'dependabot[bot]'"
):
raise CheckError(
"CI latest-OpenAI canary must run only for the weekly schedule or Dependabot"
)
if any(key in latest_openai for key in ("continue-on-error", "defaults", "env", "shell")):
raise CheckError("CI latest-OpenAI canary must retain blocking command execution")
for index, step in enumerate(_workflow_steps(latest_openai, "CI latest-openai job")):
_require_unconditional(step, f"CI latest-openai step {index}")
if "env" in step:
raise CheckError("CI latest-OpenAI steps must not override the CI environment")
for name, value in jobs.items():
job = _mapping(value, f"CI {name!r} job")
_require_exact_keys(job, expected_job_keys[name], f"CI {name!r} job")
_require_needs(
package,
["quality", "locked-runtime", "minimum-openai"],
"CI package job",
)
_require_needs(standalone, ["package"], "CI standalone job")
required_commands = {
"quality": (
"uv lock --check",
"uv sync --locked",
"uv run ruff check src tests scripts",
"uv run ruff format --check src tests scripts",
"uv run pyright",
'uv run pytest -m "not live"',
"uv run python scripts/check_version.py --require-changelog",
"uv run python scripts/check_version.py --require-public-preview-docs",
"uv run python scripts/check_secrets.py",
"uv run python scripts/run_actionlint.py",
"uv run python scripts/check_workflows.py",
),
"locked-runtime": ("uv sync --locked", 'uv run pytest -m "not live"'),
"minimum-openai": (
"uv sync --locked",
'uv pip install --python .venv/bin/python "openai==2.45.0"',
'uv run --no-sync pytest -m "not live"',
),
"latest-openai": (
"uv sync --locked",
'uv pip install --python .venv/bin/python --upgrade "openai>=2.45.0,<3.0.0"',
'uv run --no-sync pytest -m "not live"',
),
"package": (
"uv sync --locked",
"uv build",
"uv run twine check dist/*",
"uv run python scripts/check_artifacts.py dist/*",
"uv run python scripts/check_clean_install.py dist/*",
"sha256sum dist/* > artifact-sha256.txt",
),
"standalone": (
"python scripts/check_repository_independence.py",
"sha256sum --check artifact-sha256.txt",
),
}
required_jobs = {
"quality": quality,
"locked-runtime": locked_runtime,
"minimum-openai": minimum_openai,
"latest-openai": latest_openai,
"package": package,
"standalone": standalone,
}
expected_step_names = {
"quality": [
"Check out the candidate",
"Set up Python",
"Install the pinned uv frontend",
"Check lock consistency",
"Reproduce the locked environment",
"Lint",
"Check formatting",
"Type check",
"Run offline unit and contract tests",
"Check release version agreement",
"Check canonical public content and identity",
"Scan for credentials and scope mistakes",
"Validate workflow syntax with checksum-pinned actionlint",
"Verify release-workflow trust semantics",
],
"locked-runtime": [
"Check out the candidate",
"Set up Python",
"Install the pinned uv frontend",
"Reproduce the locked environment",
"Run offline tests",
],
"minimum-openai": [
"Check out the candidate",
"Set up Python",
"Install the pinned uv frontend",
"Create the development environment",
"Select the minimum supported OpenAI dependency",
"Run offline tests without resyncing the lock",
],
"latest-openai": [
"Check out the candidate",
"Set up Python",
"Install the pinned uv frontend",
"Create the development environment",
"Select latest OpenAI within the supported major",
"Run canary tests without resyncing the lock",
],
"package": [
"Check out the candidate",
"Set up Python",
"Install the pinned uv frontend",
"Reproduce the locked environment",
"Build wheel and source distribution",
"Check package metadata rendering",
"Inspect artifact identity and shape",
"Install and smoke-test each exact artifact",
"Record immutable artifact digests",
"Retain verified artifacts",
],
"standalone": [
"Check out the candidate",
"Set up Python",
"Install the pinned uv frontend",
"Verify from a copied standalone repository",
"Download the verified package artifacts",
"Recheck retained artifact digests",
],
}
expected_timeouts = {
"quality": "20",
"locked-runtime": "20",
"minimum-openai": "20",
"latest-openai": "20",
"package": "25",
"standalone": "35",
}
expected_python = {
"quality": "3.14",
"locked-runtime": "${{ matrix.python-version }}",
"minimum-openai": "3.10",
"latest-openai": "3.14",
"package": "3.14",
"standalone": "3.14",
}
for name, job in required_jobs.items():
if job.get("runs-on") != "ubuntu-latest":
raise CheckError(f"CI {name} job must use the reviewed GitHub-hosted runner")
if job.get("timeout-minutes") != expected_timeouts[name]:
raise CheckError(f"CI {name} job must retain its reviewed timeout")
_, setup_step = _named_action_step(
job, "Set up Python", "actions/setup-python", f"CI {name} job"
)
_, checkout_step = _named_action_step(
job, "Check out the candidate", "actions/checkout", f"CI {name} job"
)
if "with" in checkout_step:
raise CheckError(f"CI {name} checkout must use the triggering candidate defaults")
_require_options(
setup_step,
{"python-version": expected_python[name]},
f"CI {name} Python setup",
)
for name, commands in required_commands.items():
for command in commands:
_run_step(required_jobs[name], command, f"CI {name} job")
strategy = _mapping(locked_runtime.get("strategy"), "CI locked-runtime strategy")
if set(strategy) != {"fail-fast", "matrix"} or strategy["fail-fast"] != "false":
raise CheckError("CI runtime matrix must retain fail-fast: false")
matrix = _mapping(strategy.get("matrix"), "CI locked-runtime matrix")
if set(matrix) != {"python-version"}:
raise CheckError("CI runtime matrix must vary only the supported Python version")
python_versions = [
_scalar(item, "CI locked-runtime Python version")
for item in _sequence(matrix.get("python-version"), "CI locked-runtime Python versions")
]
if python_versions != ["3.10", "3.11", "3.12", "3.13", "3.14"]:
raise CheckError("CI must block on every supported Python runtime")
package_digest, _ = _run_step(
package, "sha256sum dist/* > artifact-sha256.txt", "CI package job"
)
package_upload, upload_step = _named_action_step(
package, "Retain verified artifacts", "actions/upload-artifact", "CI package job"
)
_require_options(
upload_step,
{
"name": "python-distributions",
"path": "dist/*\nartifact-sha256.txt\n",
"if-no-files-found": "error",
"retention-days": "7",
},
"CI package artifact upload",
)
if package_digest >= package_upload:
raise CheckError("CI package job must digest artifacts before retaining them")
copied_checkout, _ = _run_step(
standalone,
"python scripts/check_repository_independence.py",
"CI standalone job",
)
artifact_download, download_step = _action_step(
standalone, "actions/download-artifact", "CI standalone job"
)
_require_options(
download_step,
{"name": "python-distributions", "path": "verified-artifacts"},
"CI artifact download",
)
digest_check, digest_step = _run_step(
standalone,
"sha256sum --check artifact-sha256.txt",
"CI standalone job",
)
if digest_step.get("working-directory") != "verified-artifacts":
raise CheckError("CI must recheck retained artifact digests after download")
if not copied_checkout < artifact_download < digest_check:
raise CheckError(
"CI must finish copied-checkout verification before downloading and "
"rechecking retained artifacts"
)
for name, job in required_jobs.items():
_require_step_names(job, expected_step_names[name], f"CI {name} job")
_require_step_working_directories(
job,
(
{"Recheck retained artifact digests": "verified-artifacts"}
if name == "standalone"
else {}
),
f"CI {name} job",
)
def _check_publish_envelope(workflow: dict[str, object], source: str) -> None:
"""Require one top-level workflow identity for release creation and publication."""
_require_exact_keys(
workflow,
{"name", "on", "permissions", "concurrency", "env", "jobs"},
source,
)
if workflow.get("name") != "Publish immutable release":
raise CheckError("publication must retain its canonical top-level workflow identity")
_require_permissions(workflow, {"contents": "read"}, source)
if "defaults" in workflow:
raise CheckError("publication workflow must not override command defaults")
triggers = _mapping(workflow.get("on"), f"{source} triggers")
if set(triggers) != {"push", "workflow_dispatch"}:
raise CheckError("publication must run only for main pushes or explicit recovery dispatch")
push = _mapping(triggers["push"], f"{source} push trigger")
if set(push) != {"branches"}:
raise CheckError("publication push trigger must not use path or tag filters")
branches = [
_scalar(item, "publication push branch")
for item in _sequence(push.get("branches"), "publication push branches")
]
if branches != ["main"]:
raise CheckError("publication push trigger must use only main")
dispatch = _mapping(triggers["workflow_dispatch"], f"{source} recovery dispatch")
_require_exact_keys(dispatch, {"inputs"}, "publication recovery dispatch")
inputs = _mapping(dispatch["inputs"], "publication recovery inputs")
expected_descriptions = {
"release-tag": "Exact immutable GitHub release tag",
"release-sha": "Exact commit resolved by the release tag",
}
if set(inputs) != set(expected_descriptions):
raise CheckError("publication recovery must accept only the exact release identity")
for name, description in expected_descriptions.items():
if _mapping(inputs[name], f"publication recovery input {name}") != {
"description": description,
"required": "true",
"type": "string",
}:
raise CheckError(f"publication recovery input {name} must be an exact required string")
concurrency = _mapping(workflow.get("concurrency"), f"{source} concurrency")
if concurrency != {"group": "pypi-publish", "cancel-in-progress": "false"}:
raise CheckError("publication must serialize the complete release workflow")
if _mapping(workflow.get("env"), f"{source} environment") != {"UV_VERSION": "0.11.8"}:
raise CheckError("publication must retain its pinned uv frontend version")
jobs = _mapping(workflow.get("jobs"), f"{source} jobs")
if set(jobs) != PUBLISH_JOB_NAMES:
raise CheckError("publication jobs must match the reviewed top-level release chain")
def check_release_please_workflow(text: str) -> None:
"""Require Release Please to remain explicitly disabled by default."""
workflow = _load_workflow(text, "Release Please workflow")
_check_publish_envelope(workflow, "Release Please workflow")
release_job = _workflow_job(workflow, "release-please", "Release Please workflow")
_require_exact_keys(
release_job,
{"name", "if", "runs-on", "timeout-minutes", "outputs", "permissions", "steps"},
"Release Please job",
)
release_condition = " ".join(_scalar(release_job.get("if"), "Release Please condition").split())
if release_condition != RELEASE_PLEASE_JOB_CONDITION:
raise CheckError(
"Release Please must require a first-attempt main push and RELEASE_PLEASE_ENABLED=true"
)
if release_job.get("runs-on") != "ubuntu-latest":
raise CheckError("Release Please must use the reviewed GitHub-hosted runner")
if release_job.get("timeout-minutes") != "15":
raise CheckError("Release Please must retain its fifteen-minute timeout")
if "continue-on-error" in release_job:
raise CheckError("Release Please must not allow its job to fail")
if "env" in release_job:
raise CheckError("Release Please job must not override the action environment")
_require_permissions(
release_job,
{"contents": "write", "pull-requests": "write"},
"Release Please job",
)
if "defaults" in workflow or "defaults" in release_job:
raise CheckError("Release Please must not override command defaults")
_require_step_names(
release_job,
[
"Open or update the release PR, or create its approved release",
"Verify the immutable release created by Release Please",
],
"Release Please job",
)
outputs = _mapping(release_job["outputs"], "Release Please job outputs")
if outputs != {
"release-created": "${{ steps.release.outputs.release_created }}",
"release-sha": "${{ steps.verify-release.outputs.release-sha }}",
"release-tag": "${{ steps.verify-release.outputs.release-tag }}",
"release-verified": "${{ steps.verify-release.outputs.release-verified }}",
}:
raise CheckError("Release Please must expose only the verified release identity")
_require_step_environments(
release_job,
{
"Verify the immutable release created by Release Please": {
"EXPECTED_SHA": "${{ steps.release.outputs.sha }}",
"EXPECTED_TAG": "${{ steps.release.outputs.tag_name }}",
"GH_TOKEN": "${{ github.token }}",
}
},
"Release Please job",
)
_require_step_working_directories(release_job, {}, "Release Please job")
_, release_step = _named_action_step(
release_job,
"Open or update the release PR, or create its approved release",
"googleapis/release-please-action",
"Release Please job",
)
_require_options(
release_step,
{
"config-file": "release-please-config.json",
"manifest-file": ".release-please-manifest.json",
},
"Release Please action",
)
if release_step.get("id") != "release":
raise CheckError("Release Please action must expose its reviewed release outputs")
if release_step.get("uses") != (
f"googleapis/release-please-action@{RELEASE_PLEASE_ACTION_SHA}"
):
raise CheckError("Release Please must retain the release-please 17.3.0 action pin")
verify_matches = [
(index, step)
for index, step in enumerate(_workflow_steps(release_job, "Release Please job"))
if step.get("name") == "Verify the immutable release created by Release Please"
]
if len(verify_matches) != 1:
raise CheckError("Release Please job must contain its immutable-release verification")
_, verify_step = verify_matches[0]
if verify_step.get("run") != RELEASE_PLEASE_VERIFY_COMMAND or "uses" in verify_step:
raise CheckError("Release Please immutable-release verification is not exact")
if verify_step.get("id") != "verify-release" or verify_step.get("if") != (
"steps.release.outputs.release_created == 'true'"
):
raise CheckError("Release Please must verify only the release it just created")
if any(key in verify_step for key in ("continue-on-error", "shell", "working-directory")):
raise CheckError("Release Please immutable-release verification must fail closed")
if _secret_references(release_job):
raise CheckError("Release Please must not depend on repository credentials")
def check_release_recovery_workflow(text: str) -> None:
"""Require a default-branch-only, explicitly enabled immutable release recovery."""
workflow = _load_workflow(text, "release recovery workflow")
_check_publish_envelope(workflow, "release recovery workflow")
verify = _workflow_job(workflow, "verify-recovery", "release recovery workflow")
_require_exact_keys(
verify,
{
"name",
"if",
"runs-on",
"timeout-minutes",
"outputs",
"permissions",
"steps",
},
"release recovery verification job",
)
if " ".join(_scalar(verify["if"], "release recovery condition").split()) != (
RECOVERY_JOB_CONDITION
):
raise CheckError(
"release recovery must require an explicit first-attempt dispatch from the "
"protected default branch and the exact authorized release tag and commit"
)
if verify["runs-on"] != "ubuntu-latest" or verify["timeout-minutes"] != "5":
raise CheckError("release recovery verification must use the reviewed bounded runner")
_require_permissions(verify, {"contents": "read"}, "release recovery verification job")
outputs = _mapping(verify["outputs"], "release recovery outputs")
if outputs != {
"release-sha": "${{ steps.verify-release.outputs.release-sha }}",
"release-tag": "${{ steps.verify-release.outputs.release-tag }}",
}:
raise CheckError("release recovery must expose only the verified release identity")
_require_step_names(
verify,
["Verify the immutable release selected for recovery"],
"release recovery verification job",
)
_require_step_environments(
verify,
{
"Verify the immutable release selected for recovery": {
"EXPECTED_SHA": "${{ inputs.release-sha }}",
"EXPECTED_TAG": "${{ inputs.release-tag }}",
"GH_TOKEN": "${{ github.token }}",
}
},
"release recovery verification job",
)
_require_step_working_directories(verify, {}, "release recovery verification job")
_, verify_step = _named_run_step(
verify,
"Verify the immutable release selected for recovery",
RELEASE_PLEASE_VERIFY_COMMAND,
"release recovery verification job",
)
if verify_step.get("id") != "verify-release":
raise CheckError("release recovery verification must expose its exact outputs")
selector = _workflow_job(workflow, "select-release", "release recovery workflow")
_require_exact_keys(
selector,
{"name", "needs", "if", "runs-on", "timeout-minutes", "outputs", "permissions", "steps"},
"release identity selector",
)
_require_needs(
selector,
["release-please", "verify-recovery"],
"release identity selector",
)
selector_condition = " ".join(
_scalar(selector["if"], "release identity selector condition").split()
)
if selector_condition != SELECT_RELEASE_CONDITION:
raise CheckError(
"release identity selector must accept only one successfully verified path"
)
if selector["runs-on"] != "ubuntu-latest" or selector["timeout-minutes"] != "5":
raise CheckError("release identity selector must use the reviewed bounded runner")
_require_permissions(selector, {"contents": "read"}, "release identity selector")
if _mapping(selector["outputs"], "release identity selector outputs") != {
"release-sha": "${{ steps.select.outputs.release-sha }}",
"release-tag": "${{ steps.select.outputs.release-tag }}",
}:
raise CheckError("release identity selector must expose only the selected tag and commit")
_require_step_names(
selector,
["Select the independently verified release identity"],
"release identity selector",
)
_require_step_environments(
selector,
{
"Select the independently verified release identity": {
"EVENT_NAME": "${{ github.event_name }}",
"RECOVERY_SHA": "${{ needs.verify-recovery.outputs.release-sha }}",
"RECOVERY_TAG": "${{ needs.verify-recovery.outputs.release-tag }}",
"RELEASE_PLEASE_SHA": "${{ needs.release-please.outputs.release-sha }}",
"RELEASE_PLEASE_TAG": "${{ needs.release-please.outputs.release-tag }}",
}
},
"release identity selector",
)
_require_step_working_directories(selector, {}, "release identity selector")
_, selector_step = _named_run_step(
selector,
"Select the independently verified release identity",
SELECT_RELEASE_COMMAND,
"release identity selector",
)
if selector_step.get("id") != "select":
raise CheckError("release identity selector must expose its exact selected outputs")
if _secret_references(verify) or _secret_references(selector):
raise CheckError("release identity verification must not reference credentials")
def check_release_please_config(text: str, manifest_text: str) -> None:
"""Require either the reviewed bridge or a stable 0.1.x cleanup state."""
try:
value = cast(object, json.loads(text))
except json.JSONDecodeError as error:
raise CheckError(f"Release Please config is not valid JSON: {error}") from error
config = _mapping(value, "Release Please config")
try:
manifest_value = cast(object, json.loads(manifest_text))
except json.JSONDecodeError as error:
raise CheckError(f"Release Please manifest is not valid JSON: {error}") from error
manifest = _mapping(manifest_value, "Release Please manifest")
_require_exact_keys(manifest, {"."}, "Release Please manifest")
version = manifest["."]
stable_version = (
isinstance(version, str)
and RELEASE_PLEASE_STABLE_VERSION_PATTERN.fullmatch(version) is not None
)
if version != RELEASE_PLEASE_BRIDGE_VERSION and not stable_version:
raise CheckError(
"Release Please manifest must be the reviewed bridge or a stable 0.1.x version"
)
common_keys = {
"$schema",
"release-type",
"include-component-in-tag",
"include-v-in-tag",
"packages",
}
bridge_keys = {"last-release-sha", "prerelease", "versioning"}
bridge_enabled = set(config) == common_keys | bridge_keys
if bridge_enabled:
_require_exact_keys(
config,
common_keys | bridge_keys,
"Release Please bridge config",
)
if config["last-release-sha"] != RELEASE_PLEASE_BASELINE_SHA:
raise CheckError("Release Please must stop history at the recovery alpha commit")
if config["versioning"] != "prerelease" or config["prerelease"] is not False:
raise CheckError(
"Release Please must make the reviewed prerelease-to-stable transition"
)
if version != RELEASE_PLEASE_BRIDGE_VERSION:
raise CheckError(