Repository navigation
Expand file tree
/
Copy pathvalidate-workflow-contract.cjs
More file actions
1612 lines (1538 loc) · 89.4 KB
/
Copy pathvalidate-workflow-contract.cjs
File metadata and controls
1612 lines (1538 loc) · 89.4 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 node
const replacementContract = require('./deployment-replacement-contract.cjs');
const { readFileSync, readdirSync } = require('node:fs');
const path = require('node:path');
const yaml = require('js-yaml');
const commentContract = require('./generate-native-verification-workflows.cjs');
const { assertCommentWorkflowCaller, assertCommentWorkflowRuntime } = require('./validate-comment-workflows.cjs');
const repositoryRoot = path.resolve(__dirname, '..');
const workflowDirectories = [
path.join(repositoryRoot, '.github', 'workflows'),
path.join(repositoryRoot, 'setup', 'workflows'),
];
const QUEUE_WAIT_MINUTES = 90;
const DEPLOYMENT_VALIDATION_TIMEOUT_MINUTES = 5;
const PREPARE_VERSION_TIMEOUT_MINUTES = 15;
const PREPARE_COMPILED_TIMEOUT_MINUTES = 30;
const TAG_TIMEOUT_MINUTES = 10;
const NPM_PUBLISH_TIMEOUT_MINUTES = 20;
const FINALIZE_RELEASE_TIMEOUT_MINUTES = 15;
const FAILURE_REPORT_TIMEOUT_MINUTES = 5;
const MIN_QUEUE_JOB_TIMEOUT_MINUTES = 120;
const FAILURE_REPORT_CONDITION = "${{ failure() && needs.validate.result == 'success' && inputs.mode != 'replace' }}";
const DEPLOYMENT_CONCURRENCY_GROUP = 'copilot-deployment-${{ github.repository_id }}-${{ inputs.issue }}';
const DEPLOYMENT_CONTINUATION_CONCURRENCY_GROUP = 'copilot-deployment-${{ github.repository_id }}-${{ needs.resolve-operation.outputs.issue }}';
const DISTRIBUTED_COPILOT_ACTION = 'vypdev/copilot@v3';
const CHECKOUT_ACTION = 'actions/checkout@v5';
const ISOLATED_PNPM_DEST = '${{ runner.temp }}/setup-pnpm-${{ github.run_id }}-${{ github.run_attempt }}-${{ github.job }}';
const QUALITY_RUNNER_BY_TRUST = '${{ fromJSON((github.event_name == \'merge_group\' || (github.event_name == \'pull_request\' && github.event.pull_request.head.repo.full_name != github.repository)) && \'["ubuntu-latest"]\' || \'["self-hosted","codex","Linux"]\') }}';
const ISOLATED_REPOWISE_VENV_NAME = 'repowise-venv-${{ github.run_id }}-${{ github.run_attempt }}-code-health';
const ISOLATED_REPOWISE_REPORT_NAME = 'repowise-report-${{ github.run_id }}-${{ github.run_attempt }}-code-health';
const CRLF_WHITESPACE = 'core.whitespace=blank-at-eol,blank-at-eof,space-before-tab,cr-at-eol';
const PUSH_BRANCH_CONCURRENCY_GROUP = 'copilot-push-${{ github.repository }}-${{ github.ref_name }}';
const PULL_REQUEST_ANALYSIS_CONCURRENCY_GROUP = 'copilot-pr-${{ github.repository }}-${{ github.event.pull_request.number }}-analysis';
const PULL_REQUEST_REVIEW_STATE_CONCURRENCY_GROUP = 'copilot-pr-${{ github.repository }}-${{ github.event.pull_request.number }}-review-state';
const BUGBOT_CONCURRENCY_JOBS = Object.freeze({
'copilot_commit.yml': Object.freeze({
jobId: 'copilot-commits',
group: PUSH_BRANCH_CONCURRENCY_GROUP,
cancelInProgress: true,
}),
'copilot_pull_request.yml': Object.freeze({
jobId: 'copilot-pull-requests',
group: PULL_REQUEST_ANALYSIS_CONCURRENCY_GROUP,
cancelInProgress: true,
}),
'copilot_pull_request_review_state.yml': Object.freeze({
jobId: 'copilot-pull-request-review-state',
group: PULL_REQUEST_REVIEW_STATE_CONCURRENCY_GROUP,
cancelInProgress: true,
}),
});
function assertQueueBudget(queueWaitMinutes, minimumJobTimeoutMinutes) {
if (!Number.isFinite(queueWaitMinutes)
|| !Number.isFinite(minimumJobTimeoutMinutes)
|| minimumJobTimeoutMinutes < queueWaitMinutes) {
throw new Error(`minimum job timeout must be >= queue wait (${queueWaitMinutes}m).`);
}
}
assertQueueBudget(QUEUE_WAIT_MINUTES, MIN_QUEUE_JOB_TIMEOUT_MINUTES);
const QUEUE_WORKFLOW_MANIFEST = Object.freeze([
['copilot_commit.yml', 'Copilot - Commit', 'copilot-commits'],
['copilot_branch_sync.yml', 'Copilot - Branch Sync', 'branch-sync'],
['copilot_issue.yml', 'Copilot - Issue', 'copilot-issues'],
['copilot_issue_comment.yml', 'Copilot - Issue Comment', 'copilot-issues'],
['copilot_pull_request.yml', 'Copilot - Pull Request', 'copilot-pull-requests'],
['copilot_pull_request_review_state.yml', 'Copilot - Pull Request Review State', 'copilot-pull-request-review-state'],
['copilot_pull_request_comment.yml', 'Copilot - Pull Request Comment', 'copilot-pull-requests'],
['copilot_close_inactive_issues.yml', 'Copilot - Close Inactive Issues', 'copilot-inactive-issues'],
].map(([file, workflowName, jobId]) => ({ file, workflowName, jobId })));
const MUTATION_WORKFLOW_MANIFEST = Object.freeze([
{ file: 'release_workflow.yml', workflowName: 'Task - Release' },
{ file: 'hotfix_workflow.yml', workflowName: 'Task - Hotfix' },
]);
const BOT_GATED_WORKFLOW_FILES = new Set([
'copilot_commit.yml',
'copilot_issue.yml',
'copilot_issue_comment.yml',
'copilot_pull_request.yml',
'copilot_pull_request_review_state.yml',
'copilot_pull_request_comment.yml',
]);
const BOT_GATE_EXPRESSION = "${{ vars.COPILOT_BOT_LOGIN == '' || github.actor != vars.COPILOT_BOT_LOGIN }}";
const FORK_SAFE_BOT_GATE_EXPRESSION = "${{ (vars.COPILOT_BOT_LOGIN == '' || github.actor != vars.COPILOT_BOT_LOGIN) && github.event.pull_request.head.repo.full_name == github.repository }}";
const BOT_PR_ANALYSIS_GATE_EXPRESSION = "${{ github.event.pull_request.head.repo.full_name == github.repository && (vars.COPILOT_BOT_LOGIN == '' || github.actor != vars.COPILOT_BOT_LOGIN || ((github.event.action == 'opened' || github.event.action == 'reopened' || github.event.action == 'synchronize') && github.event.pull_request.user.login == github.actor)) }}";
const FORK_GATED_WORKFLOW_FILES = new Set([
'copilot_pull_request.yml',
'copilot_pull_request_review_state.yml',
'copilot_pull_request_comment.yml',
]);
const ISSUE_WORKFLOW_PROFILE_FILES = new Set([
'copilot_issue.yml',
'copilot_issue_comment.yml',
'copilot_commit.yml',
'copilot_pull_request.yml',
'copilot_pull_request_review_state.yml',
'copilot_pull_request_comment.yml',
'copilot_deployment_orchestration.yml',
'release_workflow.yml',
'hotfix_workflow.yml',
]);
const ZERO_OBJECT_ID = '0000000000000000000000000000000000000000';
const MAJOR_ACTION_REFERENCE = /^[^/\s]+\/[^@\s]+@v[1-9]\d*$/;
const currentCopilotManifest = yaml.load(readFileSync(path.join(repositoryRoot, 'action.yml'), 'utf8'));
const BASE_AGENT_INPUTS = ['agent-provider', 'agent-model-provider', 'agent-model', 'agent-effort', 'agent-executable'];
const AGENT_ROLE_INPUTS = Object.freeze(Object.fromEntries(
['findings', 'fixer', 'planner', 'reviewer', 'tester'].map(role => [role, [
`${role}-provider`, `${role}-model-provider`, `${role}-model`, `${role}-effort`, `${role}-executable`,
]]),
));
const WORKFLOW_AGENT_ROLES = Object.freeze({
'copilot_comment_runtime.yml': ['findings', 'fixer', 'planner', 'reviewer', 'tester'],
'copilot_issue.yml': ['planner'],
'copilot_pull_request.yml': ['planner', 'reviewer'],
'copilot_pull_request_review_state.yml': ['planner', 'reviewer'],
'copilot_commit.yml': ['findings'],
'copilot_issue_comment.yml': ['findings', 'fixer', 'planner', 'reviewer', 'tester'],
'copilot_pull_request_comment.yml': ['findings', 'fixer', 'planner', 'reviewer', 'tester'],
});
const REPOSITORY_LOCALE_INPUTS = Object.freeze({
'repository-locale': "${{ vars.REPOSITORY_LOCALE || 'en-US' }}",
'issues-locale': "${{ vars.ISSUES_LOCALE || '' }}",
'pull-requests-locale': "${{ vars.PULL_REQUESTS_LOCALE || '' }}",
});
const REPOSITORY_BUGBOT_CONTEXT_EXCLUSIONS = 'build/*,specs/CATALOG.md';
const REPOSITORY_BUGBOT_WORKFLOW_FILES = new Set([
'copilot_issue.yml',
'copilot_issue_comment.yml',
'copilot_pull_request.yml',
'copilot_pull_request_review_state.yml',
'copilot_pull_request_comment.yml',
]);
function workflowFiles(directory) {
return readdirSync(directory)
.filter(name => name.endsWith('.yml') || name.endsWith('.yaml'))
.map(name => path.join(directory, name));
}
function relativeWorkflow(file) {
return path.relative(repositoryRoot, file).replaceAll(path.sep, '/');
}
function expectedCopilotAction(relativeFile) {
if (path.basename(relativeFile) === commentContract.RUNTIME) return commentContract.SELF_ACTION;
if (relativeFile.startsWith('setup/workflows/')) return DISTRIBUTED_COPILOT_ACTION;
const native = require('./generate-native-verification-workflows.cjs');
return native.CALLBACKS.includes(path.basename(relativeFile)) ? native.SOURCE_ACTION : './';
}
function isCopilotAction(step) {
return typeof step?.uses === 'string'
&& (step.uses === '$/' || step.uses === './' || step.uses === './../.copilot-native-runtime' || /(?:^|\/)copilot@/.test(step.uses));
}
function shouldPersistCheckoutCredentials(relativeFile, jobId) {
const isMutationWorkflow = MUTATION_WORKFLOW_MANIFEST.some(
entry => relativeFile.endsWith(`/${entry.file}`),
);
if (!isMutationWorkflow) return false;
if (jobId === 'prepare-version-files') return true;
return !relativeFile.startsWith('setup/workflows/') && jobId === 'prepare-compiled-files';
}
function assertMajorActionReferences(file, workflow) {
const relativeFile = relativeWorkflow(file);
for (const [jobId, job] of Object.entries(workflow.jobs ?? {})) {
const actionUses = [job.uses, ...(job.steps ?? []).map(step => step?.uses)]
.filter(value => typeof value === 'string');
for (const uses of actionUses) {
if (isCopilotAction({ uses })) {
const expected = expectedCopilotAction(relativeFile);
if (uses !== expected) {
throw new Error(`${relativeFile} job ${jobId} must invoke Copilot with ${expected}.`);
}
continue;
}
if (uses.startsWith('actions/checkout@')) {
if (uses !== CHECKOUT_ACTION) {
throw new Error(`${relativeFile} job ${jobId} checkout must use ${CHECKOUT_ACTION}.`);
}
continue;
}
if (uses.startsWith('./') || uses.startsWith('$/') || uses.startsWith('docker://')) continue;
if (!MAJOR_ACTION_REFERENCE.test(uses)) {
throw new Error(`${relativeFile} job ${jobId} action ${uses} must use a major version tag such as owner/action@v1.`);
}
}
for (const [stepIndex, step] of (job.steps ?? []).entries()) {
if (!/^actions\/checkout@/.test(step?.uses ?? '')) continue;
const expectedPersistence = shouldPersistCheckoutCredentials(relativeFile, jobId);
if (step.with?.['persist-credentials'] !== expectedPersistence) {
throw new Error(`${relativeFile} job ${jobId} step ${stepIndex + 1} checkout must set persist-credentials: ${expectedPersistence}.`);
}
}
}
}
function assertCopilotActionInputs(file, workflow) {
const relativeFile = relativeWorkflow(file);
for (const [jobId, job] of Object.entries(workflow.jobs ?? {})) {
for (const [stepIndex, step] of (job.steps ?? []).entries()) {
if (!isCopilotAction(step)) continue;
const supportedInputs = new Set(Object.keys(currentCopilotManifest.inputs ?? {}));
const unsupported = Object.keys(step.with ?? {}).filter(input => !supportedInputs.has(input));
if (unsupported.length > 0) {
throw new Error(`${relativeFile} job ${jobId} step ${stepIndex + 1} passes inputs unsupported by ${step.uses}: ${unsupported.join(', ')}.`);
}
}
}
}
function runnerLabels(value) {
if (value !== null && typeof value === 'object' && !Array.isArray(value)) {
const literal = text => typeof text === 'string' && text.length > 0 && text.length <= 100
&& text === text.trim() && !/[\p{Cc}\p{Cf}${}<>]/u.test(text);
const labels = Array.isArray(value.labels) ? value.labels : [value.labels];
if (Object.keys(value).some(key => !['group', 'labels'].includes(key))
|| ('group' in value && !literal(value.group))
|| labels.length < 1 || labels.length > 10 || !labels.every(literal)
|| new Set(labels.map(label => label.toLowerCase())).size !== labels.length
|| !labels.includes('self-hosted')) {
throw new Error('runs-on object must contain only an optional literal group and 1–10 unique literal self-hosted labels.');
}
return labels;
}
return Array.isArray(value) ? value.map(String) : [String(value)];
}
function assertIsolatedPnpm(relativeFile, jobId, job) {
const labels = runnerLabels(job['runs-on']);
const selfHostedCodex = labels.includes('self-hosted') && labels.includes('codex');
const dynamicQualityRunner = relativeFile === '.github/workflows/ci_check.yml'
&& labels.includes(QUALITY_RUNNER_BY_TRUST);
if (!selfHostedCodex && !dynamicQualityRunner) return;
for (const step of job.steps ?? []) {
if (typeof step?.uses === 'string' && step.uses.startsWith('pnpm/action-setup@')
&& step.with?.dest !== ISOLATED_PNPM_DEST) {
throw new Error(`${relativeFile} job ${jobId} must isolate pnpm/action-setup by runner, run, attempt and job.`);
}
}
}
function assertRepoWiseTemporaryCleanup(relativeFile, job) {
if (relativeFile !== '.github/workflows/repowise.yml') return;
const python = (job.steps ?? []).find(step => step?.name === 'Set up Python 3.12 for RepoWise');
const pythonCheck = (job.steps ?? []).find(step => step?.name === 'Verify isolated Python toolchain');
const environment = (job.steps ?? []).find(step => step?.name === 'Create isolated RepoWise environment');
const cleanup = (job.steps ?? []).find(step => step?.name === 'Remove RepoWise temporary files');
const upload = (job.steps ?? []).find(step => step?.name === 'Upload RepoWise reports');
if (python?.uses !== 'actions/setup-python@v7'
|| python.id !== 'python' || python.with?.['python-version'] !== '3.12'
|| pythonCheck?.env?.PYTHON_BIN !== '${{ steps.python.outputs.python-path }}'
|| !pythonCheck.run?.includes('"$PYTHON_BIN" -m ensurepip --version')
|| environment?.env?.PYTHON_BIN !== '${{ steps.python.outputs.python-path }}'
|| job.steps.indexOf(python) >= job.steps.indexOf(environment)
|| job.env?.REPOWISE_VENV_NAME !== ISOLATED_REPOWISE_VENV_NAME
|| job.env?.REPOWISE_REPORT_NAME !== ISOLATED_REPOWISE_REPORT_NAME
|| upload?.with?.path !== '${{ runner.temp }}/${{ env.REPOWISE_REPORT_NAME }}/'
|| cleanup?.if !== '${{ always() }}'
|| cleanup?.run !== 'rm -rf -- "$RUNNER_TEMP/$REPOWISE_VENV_NAME" "$RUNNER_TEMP/$REPOWISE_REPORT_NAME"'
|| job.steps.indexOf(cleanup) <= job.steps.indexOf(upload)) {
throw new Error(`${relativeFile} must select complete Python, isolate RepoWise files, and remove them after artifact upload.`);
}
const reports = (job.steps ?? []).find(step => step?.name === 'Generate RepoWise reports');
const install = (job.steps ?? []).find(step => step?.name === 'Install RepoWise');
if (!install?.run?.includes("'repowise==0.58.0'")
|| !reports?.run?.includes('"$python_bin" scripts/repowise_reports.py --report-dir "$report_dir"')
|| !reports.run.includes('"$python_bin" -m unittest discover -s scripts/tests -p \'test_repowise*.py\' -v')
|| !reports.run.includes('trap cleanup EXIT')
|| reports.run.replace('rmdir .vscode 2>/dev/null || true', '').includes('|| true')
|| upload.if !== '${{ always() }}'
|| upload.with?.['retention-days'] !== 14) {
throw new Error(`${relativeFile} must preserve pinned bounded reports, compatibility tests, cleanup and unconditional artifacts.`);
}
}
function assertManualRepoWiseWorkflow(file, workflow) {
const relativeFile = relativeWorkflow(file);
if (relativeFile !== '.github/workflows/repowise.yml') return;
if (Object.keys(workflow.on ?? {}).join(',') !== 'workflow_dispatch') {
throw new Error(`${relativeFile} must run only through workflow_dispatch.`);
}
const job = workflow.jobs?.['code-health'];
const checkout = (job?.steps ?? []).find(step => step?.uses === CHECKOUT_ACTION);
if (job?.['timeout-minutes'] !== 40
|| JSON.stringify(workflow.permissions) !== JSON.stringify({ contents: 'read' })
|| job.permissions !== undefined
|| checkout?.with?.['fetch-depth'] !== 0
|| checkout.with?.ref !== undefined
|| workflow.concurrency?.group !== 'repowise-${{ github.ref }}'
|| workflow.concurrency?.['cancel-in-progress'] !== true) {
throw new Error(`${relativeFile} must retain its manual job budget, read-only permissions, selected revision and per-ref concurrency.`);
}
}
function assertPlatformCoverageAndValidators(relativeFile, job) {
const steps = job.steps ?? [];
const coverageStep = steps.find(step => step?.name === 'Full platform coverage and acceptance budgets');
if (coverageStep?.run !== 'pnpm run test:coverage' || coverageStep.if !== undefined) {
throw new Error(`${relativeFile} must retain full platform coverage and acceptance budgets.`);
}
const validationStep = steps.find(step => step?.name === 'Full platform documentation and contract validators');
const requiredValidations = [
'validate:agent-docs', 'validate:docs-page', 'validate:documentation',
'validate:workflows', 'validate:specifications', 'validate:setup-acceptance',
];
if (!validationStep?.run || requiredValidations.some(command => !validationStep.run.includes(`pnpm run ${command}`))
|| steps.indexOf(validationStep) <= steps.indexOf(coverageStep)
|| validationStep.if !== undefined) {
throw new Error(`${relativeFile} must retain full platform documentation and contract validators after coverage.`);
}
if (!steps.some(step => step?.run === 'pnpm run validate:npm-package && pnpm run smoke:npm-package')
|| !steps.some(step => step?.name === 'Local setup fixtures only')
|| !steps.some(step => step?.name === 'Isolated agent runtime fixtures')
|| !steps.some(step => step?.run === 'pnpm run build')
|| !steps.some(step => step?.run === 'pnpm run typecheck')) {
throw new Error(`${relativeFile} must retain build, typecheck, setup, agent and packaged npm fixtures on every platform.`);
}
}
function assertWindowsJobNpm(relativeFile, job) {
const steps = job.steps ?? [];
const npmCheck = steps.find(step => step?.name === 'Verify Windows job Node npm CLI');
if (npmCheck?.run !== 'node scripts/verify-windows-job-npm.cjs'
|| npmCheck.if !== "runner.os == 'Windows'"
|| steps.indexOf(npmCheck) >= steps.findIndex(step => step?.run === 'pnpm install --frozen-lockfile')) {
throw new Error(`${relativeFile} must verify the Windows job Node npm CLI before installation.`);
}
}
function assertPlatformHistoryCheckout(relativeFile, job) {
const checkout = (job.steps ?? []).find(step => step?.uses === 'actions/checkout@v5');
if (checkout?.with?.['fetch-depth'] !== 0 || checkout.with?.['persist-credentials'] !== false) {
throw new Error(`${relativeFile} must fetch full platform-test history without persisted checkout credentials.`);
}
}
function assertRunner(file, workflow) {
const relativeFile = relativeWorkflow(file);
if (path.basename(file) === commentContract.RUNTIME) return assertCommentWorkflowRuntime(file, workflow);
if (commentContract.CALLBACKS.includes(path.basename(file))) return assertCommentWorkflowCaller(file, workflow);
if (relativeFile === '.github/workflows/setup_platform_smoke.yml') {
const jobIds = Object.keys(workflow.jobs ?? {}).sort();
if (JSON.stringify(jobIds) !== JSON.stringify(['setup-platform-smoke', 'setup-self-hosted-codex-smoke', 'upload-windows-coverage', 'windows-startup-probe'])) {
throw new Error(`${relativeFile} must retain hosted and manually dispatched self-hosted platform fixture jobs and Windows coverage upload.`);
}
const uploadInput = workflow.on?.workflow_dispatch?.inputs?.upload_windows_coverage;
if (uploadInput?.type !== 'boolean' || uploadInput.default !== false) {
throw new Error(`${relativeFile} must default Windows Codecov export to disabled on manual fixture runs.`);
}
const probeInput = workflow.on?.workflow_dispatch?.inputs?.windows_startup_probe;
if (probeInput?.type !== 'boolean' || probeInput.default !== false) {
throw new Error(`${relativeFile} must default Windows startup probes to disabled.`);
}
}
if (relativeFile === '.github/workflows/copilot_runner_diagnostics.yml'
&& JSON.stringify(Object.keys(workflow.jobs ?? {})) !== JSON.stringify(['linux'])) {
throw new Error(`${relativeFile} must retain only the supported Linux Codex diagnostic job.`);
}
for (const [jobId, job] of Object.entries(workflow.jobs ?? {})) {
if (Object.values(job.env ?? {}).some(value => typeof value === 'string' && /\$\{\{\s*runner\./u.test(value))) {
throw new Error(`${relativeFile} job ${jobId} cannot use runner context in job-level env.`);
}
if (relativeFile === '.github/workflows/setup_platform_smoke.yml') {
if (jobId === 'windows-startup-probe') {
const steps = job.steps ?? [];
if (job['runs-on'] !== 'windows-latest' || job['timeout-minutes'] !== 5
|| job.if !== "${{ github.event_name == 'workflow_dispatch' && inputs.windows_startup_probe == true }}"
|| job.needs || steps.length !== 3
|| steps[0]?.uses !== 'actions/checkout@v5' || steps[0]?.with?.['persist-credentials'] !== false
|| steps[1]?.uses !== 'actions/setup-node@v7' || steps[1]?.with?.['node-version'] !== '24.x'
|| steps[2]?.run !== 'node scripts/diagnose-windows-powershell-startup.cjs') {
throw new Error(`${relativeFile} must retain its bounded manual hosted Windows startup diagnostic without credentials or export.`);
}
continue;
}
if (jobId === 'upload-windows-coverage') {
const steps = job.steps ?? [];
const checkout = steps[0];
const download = steps[1];
const upload = steps[2];
if (job['runs-on'] !== 'ubuntu-latest' || job.needs !== 'setup-platform-smoke'
|| job.if !== "${{ github.event_name == 'workflow_dispatch' && inputs.upload_windows_coverage == true }}"
|| steps.length !== 3
|| checkout?.uses !== 'actions/checkout@v5'
|| checkout.with?.['persist-credentials'] !== false || checkout.with?.['fetch-depth'] !== 0
|| download?.uses !== 'actions/download-artifact@v4'
|| download.with?.name !== 'hosted-windows-lcov' || download.with?.path !== 'coverage-windows'
|| upload?.uses !== 'codecov/codecov-action@v6'
|| upload.with?.files !== './coverage-windows/lcov.info'
|| upload.with?.disable_search !== true || upload.with?.fail_ci_if_error !== true
|| upload.with?.flags !== 'windows' || upload.with?.token !== '${{ secrets.CODECOV_TOKEN }}') {
throw new Error(`${relativeFile} must upload hosted Windows coverage from a dependent Ubuntu job only after explicit manual opt-in.`);
}
continue;
}
if (jobId === 'setup-platform-smoke') {
const platforms = job.strategy?.matrix?.os;
if (job.if !== "${{ github.event_name == 'pull_request' || (github.event_name == 'workflow_dispatch' && (inputs.windows_startup_probe != true || inputs.upload_windows_coverage == true)) }}"
|| job['runs-on'] !== '${{ matrix.os }}'
|| JSON.stringify(platforms) !== JSON.stringify(['ubuntu-latest', 'windows-latest', 'macos-latest'])) {
throw new Error(`${relativeFile} must use the PR or ordinary manual Ubuntu, Windows and macOS setup fixture matrix, independently of coverage export.`);
}
assertPlatformCoverageAndValidators(relativeFile, job);
const hostedCoverage = (job.steps ?? []).find(step => step?.name === 'Full platform coverage and acceptance budgets');
if (hostedCoverage?.env?.COPILOT_JEST_WINDOWS_SERVICE_COVERAGE !== undefined) {
throw new Error(`${relativeFile} must not recycle hosted Windows Jest workers at the service-runner limit.`);
}
assertWindowsJobNpm(relativeFile, job);
assertPlatformHistoryCheckout(relativeFile, job);
const artifact = (job.steps ?? []).find(step => step?.name === 'Preserve hosted Windows coverage');
if (artifact?.uses !== 'actions/upload-artifact@v4'
|| artifact.if !== "runner.os == 'Windows'"
|| artifact.with?.name !== 'hosted-windows-lcov'
|| artifact.with?.path !== 'coverage/lcov.info'
|| artifact.with?.['if-no-files-found'] !== 'error'
|| artifact.with?.['retention-days'] !== 1
|| (job.steps ?? []).indexOf(artifact) <= (job.steps ?? []).findIndex(step => step?.name === 'Full platform coverage and acceptance budgets')) {
throw new Error(`${relativeFile} must preserve hosted Windows coverage for dependent upload.`);
}
} else {
const expectedMatrix = [
{ platform: 'macOS', labels: ['self-hosted', 'codex', 'macOS'] },
{ platform: 'Ubuntu', labels: ['self-hosted', 'codex', 'Linux'] },
];
if (job['runs-on'] !== '${{ matrix.labels }}'
|| job.name !== 'Setup package and local session (self-hosted ${{ matrix.platform }} codex)'
|| ![expectedMatrix, [expectedMatrix[1]]].some(matrix =>
JSON.stringify(job.strategy?.matrix?.include) === JSON.stringify(matrix))
|| job.strategy?.['fail-fast'] !== false) {
throw new Error(`${relativeFile} must target self-hosted macOS and Ubuntu codex runners or the authorized temporary Ubuntu-only selection; Windows services are unsupported.`);
}
if (job.if !== "${{ github.event_name == 'workflow_dispatch' && github.ref == 'refs/heads/master' && inputs.windows_startup_probe != true }}") {
throw new Error(`${relativeFile} must reserve self-hosted codex runners for manual dispatch from protected master.`);
}
const steps = job.steps ?? [];
if (!steps.some(step => step?.name === 'Verify Ubuntu distribution'
&& step.if === "runner.os == 'Linux'" && step.run === '. /etc/os-release && test "$ID" = ubuntu')) {
throw new Error(`${relativeFile} must verify Ubuntu on self-hosted Codex runners.`);
}
const pnpmSetup = steps.find(step => typeof step?.uses === 'string' && step.uses.startsWith('pnpm/action-setup@'));
if (pnpmSetup?.with?.dest !== ISOLATED_PNPM_DEST) {
throw new Error(`${relativeFile} must isolate self-hosted pnpm installation by job.`);
}
const nodeSetup = steps.find(step => typeof step?.uses === 'string' && step.uses.startsWith('actions/setup-node@'));
if (nodeSetup?.with?.cache !== undefined || nodeSetup?.with?.['package-manager-cache'] !== false) {
throw new Error(`${relativeFile} must disable dependency-cache backup on persistent self-hosted smoke runners.`);
}
assertPlatformCoverageAndValidators(relativeFile, job);
const serviceCoverage = steps.find(step => step?.name === 'Full platform coverage and acceptance budgets');
if (serviceCoverage?.env?.COPILOT_JEST_WINDOWS_SERVICE_COVERAGE !== undefined) {
throw new Error(`${relativeFile} must not apply Windows service-runner settings to supported Codex fixtures.`);
}
assertPlatformHistoryCheckout(relativeFile, job);
const guidanceStep = steps.find(step => step?.name === 'Validate generated guidance checkout on codex runner');
if (!guidanceStep?.run?.includes('pnpm run validate:agent-docs')
|| !guidanceStep.run.includes('git ls-files --eol')) {
throw new Error(`${relativeFile} must verify generated guidance at self-hosted checkout.`);
}
}
assertIsolatedPnpm(relativeFile, jobId, job);
const validationStep = (job.steps ?? []).find(step => step?.name === 'Full platform documentation and contract validators');
if (!validationStep?.run?.includes(`git -c ${CRLF_WHITESPACE} diff --check -- . ':(exclude)build/**'`)) {
throw new Error(`${relativeFile} must retain default whitespace checks alongside CRLF support.`);
}
continue;
}
let expected = ['self-hosted', 'codex'];
if ((path.basename(file) === 'copilot_issue.yml' && jobId === 'issue-admission')
|| relativeFile.startsWith('setup/workflows/')
|| relativeFile === '.github/workflows/copilot_credential_health.yml'
|| (/^\.github\/workflows\/(?:release|hotfix)_workflow\.yml$/.test(relativeFile) && jobId === 'publish-npm')) {
expected = ['ubuntu-latest'];
} else if (relativeFile === '.github/workflows/copilot_pull_request_approval.yml') {
expected = ['self-hosted', 'codex', 'Linux'];
} else if (relativeFile === '.github/workflows/copilot_runner_diagnostics.yml') {
if (jobId !== 'linux') throw new Error(`${relativeFile} contains an unsupported Codex diagnostic job.`);
expected = ['self-hosted', 'codex', 'Linux'];
} else if (relativeFile === '.github/workflows/repowise.yml') {
expected = ['self-hosted', 'codex', 'Linux'];
} else if (relativeFile === '.github/workflows/ci_check.yml') {
expected = [QUALITY_RUNNER_BY_TRUST];
}
if (jobId === 'candidate-preflight' && MUTATION_WORKFLOW_MANIFEST.some(entry => relativeFile.endsWith(`/${entry.file}`))) expected = ['ubuntu-latest'];
const labels = runnerLabels(job['runs-on']);
const linuxOnly = JSON.stringify(expected) === JSON.stringify(['self-hosted', 'codex'])
&& JSON.stringify(labels) === JSON.stringify(['self-hosted', 'codex', 'Linux']);
if (JSON.stringify(labels) !== JSON.stringify(expected) && !linuxOnly) {
throw new Error(`${relativeFile} job ${jobId} must use runs-on ${expected.join(', ')}.`);
}
assertIsolatedPnpm(relativeFile, jobId, job);
assertRepoWiseTemporaryCleanup(relativeFile, job);
if (relativeFile === '.github/workflows/ci_check.yml'
|| relativeFile === '.github/workflows/repowise.yml') {
const ubuntuCheck = (job.steps ?? []).find(step => step?.name === 'Verify Ubuntu runner');
if (ubuntuCheck?.run !== '. /etc/os-release && test "$ID" = ubuntu') {
throw new Error(`${relativeFile} must verify Ubuntu on the selected runner.`);
}
}
if (relativeFile === '.github/workflows/ci_check.yml') {
const checkout = (job.steps ?? []).find(step => step?.uses === 'actions/checkout@v5');
if (checkout?.with?.['fetch-depth'] !== 0 || checkout.with?.['persist-credentials'] !== false) {
throw new Error(`${relativeFile} must fetch full history without stored checkout credentials for the event-base diff check.`);
}
const diffStep = (job.steps ?? []).find(step => step?.name === 'Validate Git diff');
if (!diffStep?.run?.includes(`git -c ${CRLF_WHITESPACE} diff --check "$base" HEAD -- . ':(exclude)build/**'`)) {
throw new Error(`${relativeFile} must retain default whitespace checks alongside CRLF support.`);
}
}
}
}
function assertAgentInputs(file, workflow) {
const relativeFile = relativeWorkflow(file);
const roles = WORKFLOW_AGENT_ROLES[path.basename(file)];
if (!roles) return;
const requiredAgentInputs = [
...BASE_AGENT_INPUTS,
...roles.flatMap(role => AGENT_ROLE_INPUTS[role]),
];
for (const [jobId, job] of Object.entries(workflow.jobs ?? {})) {
for (const [stepIndex, step] of (job.steps ?? []).entries()) {
if (!isCopilotAction(step)) continue;
const missing = requiredAgentInputs.filter(input => !(input in (step.with ?? {})));
if (missing.length > 0) {
throw new Error(`${relativeFile} job ${jobId} step ${stepIndex + 1} is missing agent inputs: ${missing.join(', ')}.`);
}
}
}
}
function assertRepositoryLocaleInputs(file, workflow) {
const relativeFile = relativeWorkflow(file);
if (!WORKFLOW_AGENT_ROLES[path.basename(file)]) return;
for (const [jobId, job] of Object.entries(workflow.jobs ?? {})) {
for (const [stepIndex, step] of (job.steps ?? []).entries()) {
if (!isCopilotAction(step)) continue;
const invalid = Object.entries(REPOSITORY_LOCALE_INPUTS)
.filter(([input, value]) => step.with?.[input] !== value)
.map(([input]) => input);
if (invalid.length > 0) {
throw new Error(`${relativeFile} job ${jobId} step ${stepIndex + 1} must pass the repository locale and inheriting empty scope overrides exactly; invalid: ${invalid.join(', ')}.`);
}
}
}
}
function assertRepositoryBugbotContextExclusions(file, workflow) {
const relativeFile = relativeWorkflow(file);
if (!relativeFile.startsWith('.github/workflows/')
|| !REPOSITORY_BUGBOT_WORKFLOW_FILES.has(path.basename(file))) return;
for (const [jobId, job] of Object.entries(workflow.jobs ?? {})) {
for (const [stepIndex, step] of (job.steps ?? []).entries()) {
if (!isCopilotAction(step)) continue;
if (step.with?.['ai-ignore-files'] !== REPOSITORY_BUGBOT_CONTEXT_EXCLUSIONS) {
throw new Error(`${relativeFile} job ${jobId} step ${stepIndex + 1} must exclude generated build and specification catalog artifacts from Bugbot context exactly.`);
}
}
}
}
function assertAgentInstallationPrerequisites(file, workflow) {
const manifestFile = path.basename(file);
if (!WORKFLOW_AGENT_ROLES[manifestFile] && manifestFile !== 'agent-cli-provisioning.yml') return;
const relativeFile = relativeWorkflow(file);
for (const [jobId, job] of Object.entries(workflow.jobs ?? {})) {
const steps = job.steps ?? [];
for (const step of steps) {
const repositoryVerificationStep = ['.github/workflows/copilot_issue_comment.yml', '.github/workflows/copilot_pull_request_comment.yml', '.github/workflows/copilot_comment_runtime.yml'].includes(relativeFile)
&& step.if === "env.COPILOT_PREPARE_VERIFICATION == 'true'"
&& ((step.name === 'Prepare repository Node' && step.uses === 'actions/setup-node@v7' && step.with?.['node-version'] === '24.x')
|| (step.name === 'Prepare repository pnpm' && step.uses === 'pnpm/action-setup@v5' && step.with?.version === '10.12.4')
|| (step.name === 'Seed trusted source build tools' && step.run === 'pnpm install --frozen-lockfile --ignore-scripts --ignore-pnpmfile --config.manage-package-manager-versions=false --prod=false'));
if (repositoryVerificationStep) continue;
if (/^(?:actions\/setup-node|pnpm\/action-setup)@/u.test(String(step?.uses ?? ''))
|| /\b(?:npm|pnpm|npx)\s+(?:install|add|ci|i)\b/u.test(String(step?.run ?? ''))
|| Object.hasOwn(step?.env ?? {}, 'AGENT_PROVISIONING')) {
throw new Error(`${relativeFile} job ${jobId} must use standalone official agent installation without Node/package-manager setup.`);
}
}
}
}
function assertNoJobLevelSecrets(file, workflow) {
const relativeFile = relativeWorkflow(file);
for (const [jobId, job] of Object.entries(workflow.jobs ?? {})) {
const exposed = Object.entries(job.env ?? {})
.filter(([, value]) => String(value).includes('secrets.'))
.map(([name]) => name);
if (exposed.length > 0) {
throw new Error(`${relativeFile} job ${jobId} must scope Secrets to the exact step that consumes them: ${exposed.join(', ')}.`);
}
}
}
function assertAgentWorkflowPermissions(file, workflow) {
const relativeFile = relativeWorkflow(file);
if (!WORKFLOW_AGENT_ROLES[path.basename(file)]) return;
const nativeEvidenceWorkflow = ['copilot_commit.yml', 'copilot_pull_request.yml', 'copilot_pull_request_review_state.yml',
'copilot_issue_comment.yml', 'copilot_pull_request_comment.yml', 'copilot_comment_runtime.yml'].includes(path.basename(file));
const ordinaryPermissions = nativeEvidenceWorkflow
? { checks: 'write', contents: 'read' }
: { contents: 'read' };
for (const [jobId, job] of Object.entries(workflow.jobs ?? {})) {
const expectedPermissions = path.basename(file) === 'copilot_issue.yml' && jobId === 'issue-admission'
? { actions: 'read', contents: 'read' } : ordinaryPermissions;
const permissions = job.permissions ?? {};
const permissionKeys = Object.keys(permissions).sort();
const expectedKeys = Object.keys(expectedPermissions).sort();
if (permissionKeys.length !== expectedKeys.length
|| permissionKeys.some((key, index) => key !== expectedKeys[index])
|| expectedKeys.some(key => permissions[key] !== expectedPermissions[key])) {
throw new Error(`${relativeFile} job ${jobId} must grant GITHUB_TOKEN exactly ${expectedKeys.map(key => `${key}: ${expectedPermissions[key]}`).join(', ')}; mutations use the explicit PAT.`);
}
if (commentContract.CALLBACKS.includes(path.basename(file)) && job.uses) {
assertCommentWorkflowCaller(file, workflow);
continue;
}
if (nativeEvidenceWorkflow && !(job.steps ?? []).some(step =>
isCopilotAction(step) && step?.env?.COPILOT_EVIDENCE_TOKEN === '${{ github.token }}')) {
throw new Error(`${relativeFile} job ${jobId} must scope COPILOT_EVIDENCE_TOKEN to the Action step for exact-head native Checks.`);
}
}
}
function assertLightweightBranchSyncWorkflow(file, workflow) {
if (path.basename(file) !== 'copilot_branch_sync.yml') return;
const relativeFile = relativeWorkflow(file);
const branches = workflow.on?.push?.branches;
if (JSON.stringify(branches) !== JSON.stringify(['**', '!copilot-permission-test-*'])) {
throw new Error(`${relativeFile} must observe pushes on every ordinary branch exactly once and exclude temporary permission-probe branches.`);
}
const job = workflow.jobs?.['branch-sync'];
if (!job || Object.keys(workflow.jobs ?? {}).length !== 1 || job.if !== undefined) {
throw new Error(`${relativeFile} must have one ungated branch-sync job so bot pushes can propagate.`);
}
if (job.env !== undefined) {
throw new Error(`${relativeFile} branch-sync must not define an agent environment.`);
}
const permissions = job.permissions ?? {};
if (Object.keys(permissions).join(',') !== 'contents' || permissions.contents !== 'read') {
throw new Error(`${relativeFile} branch-sync must grant only contents: read to GITHUB_TOKEN.`);
}
const actionSteps = (job.steps ?? []).filter(isCopilotAction);
const inputs = actionSteps[0]?.with ?? {};
if (actionSteps.length !== 1
|| inputs['single-action'] !== 'check_branch_sync_action'
|| inputs.token !== '${{ secrets.PAT }}'
|| Object.keys(inputs).some(key => key !== 'single-action' && key !== 'token')) {
throw new Error(`${relativeFile} must invoke only the lightweight branch-sync single action and PAT input.`);
}
if (/\b(AGENT_|CODEX_|OPENCODE_|CURSOR_|API_KEY|findings-|fixer-|planner-|reviewer-|tester-)/i.test(JSON.stringify(job))) {
throw new Error(`${relativeFile} branch-sync must not expose agent configuration or provider credentials.`);
}
}
function needsFor(job) {
if (job?.needs === undefined) return [];
return (Array.isArray(job.needs) ? job.needs : [job.needs]).map(String);
}
function assertNoUnsafeCondition(relativeFile, jobId, job) {
if (typeof job.if === 'string' && /\b(always|failure|cancelled)\s*\(/i.test(
MUTATION_WORKFLOW_MANIFEST.some(entry => relativeFile.endsWith(`/${entry.file}`))
&& ['prepare-version-files', 'prepare-compiled-files', 'promote'].includes(jobId)
? job.if.replace('!cancelled()', '') : job.if)) {
throw new Error(`${relativeFile} job ${jobId} has a bypass-capable if condition.`);
}
for (const [stepIndex, step] of (job.steps ?? []).entries()) {
if (typeof step.if === 'string' && /\b(always|failure|cancelled)\s*\(/i.test(step.if)) {
throw new Error(`${relativeFile} job ${jobId} step ${stepIndex + 1} has a bypass-capable if condition.`);
}
}
}
function assertNoConcurrency(relativeFile, workflow) {
if (workflow.concurrency !== undefined) {
throw new Error(`${relativeFile} must not define GitHub concurrency.`);
}
for (const [jobId, job] of Object.entries(workflow.jobs ?? {})) {
if (job.concurrency !== undefined) {
throw new Error(`${relativeFile} job ${jobId} must not define GitHub concurrency.`);
}
}
}
function assertReviewConcurrency(relativeFile, workflow) {
if (path.basename(relativeFile) === 'copilot_issue.yml') {
if (workflow.concurrency?.group !== 'copilot-issue-${{ github.repository }}-${{ github.event.issue.number }}'
|| workflow.concurrency?.['cancel-in-progress'] !== false || workflow.concurrency?.queue !== 'max'
|| Object.values(workflow.jobs ?? {}).some(job => job.concurrency !== undefined)) {
throw new Error(`${relativeFile} must retain every pending issue event without canceling active work.`);
}
return;
}
const contract = BUGBOT_CONCURRENCY_JOBS[path.basename(relativeFile)];
if (!contract) {
assertNoConcurrency(relativeFile, workflow);
return;
}
const { jobId: targetJobId, group, cancelInProgress } = contract;
if (workflow.concurrency !== undefined) {
throw new Error(`${relativeFile} must scope Bugbot branch concurrency to job ${targetJobId}.`);
}
for (const [jobId, job] of Object.entries(workflow.jobs ?? {})) {
if (jobId !== targetJobId) {
if (job.concurrency !== undefined) {
throw new Error(`${relativeFile} job ${jobId} must not define GitHub concurrency.`);
}
continue;
}
if (job.concurrency?.group !== group
|| job.concurrency?.['cancel-in-progress'] !== cancelInProgress) {
throw new Error(`${relativeFile} job ${jobId} must use its workflow- and event-specific group (PR number for pull requests), avoid cross-canceling another event owner, and cancel superseded runs within its lane.`);
}
}
}
function assertDirectEventTriggers(file, workflow) {
const relativeFile = relativeWorkflow(file);
const triggers = workflow.on ?? {};
if (relativeFile.endsWith('/copilot_pull_request_approval.yml')) return;
if (triggers && typeof triggers === 'object' && Object.prototype.hasOwnProperty.call(triggers, 'workflow_run')) {
throw new Error(`${relativeFile} must use direct event triggers and must not define workflow_run.`);
}
if (relativeFile.endsWith('/copilot_pull_request.yml')) {
if (!triggers.pull_request || triggers.pull_request_review) {
throw new Error(`${relativeFile} must define only direct pull_request code/lifecycle triggers.`);
}
const pullRequestTypes = triggers.pull_request.types;
if (!Array.isArray(pullRequestTypes)
|| pullRequestTypes.includes('edited')
|| ['opened', 'reopened', 'closed', 'synchronize', 'ready_for_review'].some(type => !pullRequestTypes.includes(type))) {
throw new Error(`${relativeFile} must handle code/lifecycle PR events without subscribing to metadata-only edited events.`);
}
if (triggers.merge_group || workflow.jobs?.['copilot-merge-group']) {
throw new Error(`${relativeFile} must keep merge-group compatibility in the dedicated pull-request merge-queue workflow so normal PR runs do not show a skipped duplicate check.`);
}
if (workflow.jobs?.['copilot-pull-requests']?.name !== 'Copilot - Pull Request') {
throw new Error(`${relativeFile} must preserve the exact normal PR analysis check identity.`);
}
} else if (relativeFile.endsWith('/copilot_pull_request_review_state.yml')) {
const reviewTypes = triggers.pull_request_review?.types;
if (triggers.pull_request
|| !Array.isArray(reviewTypes)
|| ['submitted', 'edited', 'dismissed'].some(type => !reviewTypes.includes(type))) {
throw new Error(`${relativeFile} must define only direct pull_request_review state triggers.`);
}
if (workflow.name !== 'Copilot - Pull Request Review State'
|| workflow.jobs?.['copilot-pull-request-review-state']?.name !== 'Copilot - Pull Request Review State') {
throw new Error(`${relativeFile} must preserve the exact review-state workflow and check identity.`);
}
} else {
return;
}
if (typeof workflow['run-name'] !== 'string'
|| !workflow['run-name'].includes('github.event_name')
|| !workflow['run-name'].includes('github.event.action')) {
throw new Error(`${relativeFile} must expose the event kind and action in its run identity.`);
}
}
function assertApprovalObserverWorkflow(file, workflow) {
const relativeFile = relativeWorkflow(file);
if (!relativeFile.endsWith('/copilot_pull_request_approval.yml')) return;
const sourceRepositoryWorkflow = relativeFile.startsWith('.github/');
const triggers = workflow.on ?? {};
if (Object.keys(triggers).sort().join(',') !== 'workflow_dispatch,workflow_run'
|| JSON.stringify(triggers.workflow_run?.types) !== JSON.stringify(['completed'])
|| (sourceRepositoryWorkflow
? JSON.stringify(triggers.workflow_run?.workflows) !== JSON.stringify(['Copilot - Pull Request', 'CI Check'])
: triggers.workflow_run?.workflows !== '__PR_APPROVAL_WORKFLOW_NAMES__')
|| triggers.workflow_dispatch?.inputs?.pr?.required !== true
|| Object.keys(workflow.jobs ?? {}).join(',') !== 'observe-approval') {
throw new Error(`${relativeFile} must use only exact completed workflow_run producers and an explicit manual PR number.`);
}
const job = workflow.jobs['observe-approval'];
const permissions = job.permissions ?? {};
const observerStep = sourceRepositoryWorkflow ? job.steps?.[1] : job.steps?.[0];
const trustedSourceCheckout = sourceRepositoryWorkflow
&& job.steps?.length === 2
&& job.steps[0]?.uses === CHECKOUT_ACTION
&& job.steps[0]?.with?.ref === '${{ github.sha }}'
&& job.steps[0]?.with?.['persist-credentials'] === false
&& observerStep?.uses === './';
if (JSON.stringify(permissions) !== JSON.stringify({ actions: 'read', checks: 'write', contents: 'read', 'pull-requests': 'read' })
|| job.concurrency?.group !== 'copilot-approval-${{ github.repository }}'
|| job.concurrency?.['cancel-in-progress'] !== false
|| (sourceRepositoryWorkflow ? !trustedSourceCheckout
: job.steps?.length !== 1 || observerStep?.uses !== DISTRIBUTED_COPILOT_ACTION)
|| observerStep?.with?.['pr-approval-observer'] !== 'true'
|| observerStep?.with?.token !== '${{ secrets.PAT }}'
|| observerStep?.with?.['pr-approval-policy'] !== "${{ vars.PR_APPROVAL_POLICY || '' }}"
|| observerStep?.env?.COPILOT_APPROVAL_CHECK_TOKEN !== '${{ github.token }}'
|| /actions\/cache|\brun\s*:/u.test(JSON.stringify(job))) {
throw new Error(`${relativeFile} must keep the privileged observer isolated from PR-head code and use narrow permissions.`);
}
}
function assertPullRequestMergeQueueWorkflow(file, workflow) {
const relativeFile = relativeWorkflow(file);
if (!relativeFile.endsWith('/copilot_pull_request_merge_queue.yml')) return;
const triggers = workflow.on ?? {};
const job = workflow.jobs?.['copilot-pull-request-required-check'];
if (workflow.name !== 'Copilot - Pull Request Merge Queue'
|| triggers.pull_request
|| triggers.pull_request_review
|| !Array.isArray(triggers.merge_group?.types)
|| !triggers.merge_group.types.includes('checks_requested')) {
throw new Error(`${relativeFile} must support merge_group checks_requested in a dedicated workflow.`);
}
if (typeof workflow['run-name'] !== 'string'
|| !workflow['run-name'].includes('github.event_name')
|| !workflow['run-name'].includes('github.event.action')) {
throw new Error(`${relativeFile} must expose the merge-group event and action in its run identity.`);
}
if (!job || job.name !== 'Copilot - Pull Request') {
throw new Error(`${relativeFile} must preserve the Copilot - Pull Request required-check identity for merge-group runs.`);
}
if (Object.keys(workflow.jobs ?? {}).length !== 1
|| job['timeout-minutes'] !== 10
|| job.permissions?.checks !== 'write'
|| job.permissions?.contents !== 'read'
|| (job.steps ?? []).length !== 1
|| job.steps[0]?.name !== 'Confirm merge-group compatibility'
|| typeof job.steps[0]?.run !== 'string'
|| (job.steps ?? []).some(isCopilotAction)) {
throw new Error(`${relativeFile} must remain a single lightweight, least-privilege merge-group check.`);
}
}
function assertExactNeeds(relativeFile, jobId, job, expected) {
const actual = needsFor(job);
if (actual.length !== expected.length || actual.some((value, index) => value !== expected[index])) {
throw new Error(`${relativeFile} job ${jobId} must need exactly ${expected.join(', ') || 'no jobs'}.`);
}
}
function assertExactTimeout(relativeFile, jobId, job, expected) {
if (job?.['timeout-minutes'] !== expected) {
throw new Error(`${relativeFile} job ${jobId} must have timeout-minutes ${expected}.`);
}
}
function assertTagPermissions(relativeFile, job) {
const permissions = job?.permissions ?? {};
const permissionKeys = Object.keys(permissions).sort();
if (permissionKeys.length !== 1 || permissionKeys[0] !== 'contents' || permissions.contents !== 'read') {
throw new Error(`${relativeFile} tag must have only contents: read permissions.`);
}
}
function assertNpmPublishJob(relativeFile, job) {
if (!job) throw new Error(`${relativeFile} must define publish-npm.`);
assertExactTimeout(relativeFile, 'publish-npm', job, NPM_PUBLISH_TIMEOUT_MINUTES);
assertExactNeeds(relativeFile, 'publish-npm', job, ['tag']);
if (job.if !== "${{ inputs.mode == 'publish' }}") {
throw new Error(`${relativeFile} publish-npm must run only in publish mode.`);
}
if (job.environment !== 'npm') {
throw new Error(`${relativeFile} publish-npm must use the npm environment.`);
}
const permissions = job.permissions ?? {};
const permissionKeys = Object.keys(permissions).sort();
if (permissionKeys.join(',') !== 'contents,id-token'
|| permissions.contents !== 'read'
|| permissions['id-token'] !== 'write') {
throw new Error(`${relativeFile} publish-npm must grant only contents: read and id-token: write permissions.`);
}
const steps = job.steps ?? [];
const checkout = steps.find(step => step?.uses === CHECKOUT_ACTION);
const setupNode = steps.find(step => step?.uses === 'actions/setup-node@v7');
const install = steps.find(step => step?.run === 'pnpm install --frozen-lockfile');
const validation = steps.find(step => step?.name === 'Validate immutable package');
const publish = steps.find(step => step?.run === 'npm publish --access public');
const availability = steps.find(step => step?.name === 'Wait for npm registry visibility');
const identity = steps.find(step => step?.name === 'Verify published package identity');
const detection = steps.find(step => step?.name === 'Detect an existing publication');
const visibilityScript = String(availability?.with?.script ?? '');
if (checkout?.with?.ref !== 'v${{ inputs.version }}'
|| checkout.with?.['fetch-depth'] !== 1
|| setupNode?.with?.['node-version'] !== '24.x'
|| setupNode.with?.['registry-url'] !== 'https://registry.npmjs.org'
|| setupNode.with?.['package-manager-cache'] !== false
|| !install
|| validation?.env?.RELEASE_VERSION !== '${{ inputs.version }}'
|| !String(validation?.run ?? '').includes('pnpm run validate:npm-package')
|| !String(validation?.run ?? '').includes('pnpm run smoke:npm-package')
|| !publish
|| publish.if !== "${{ steps.registry.outputs.publish == 'true' }}"
|| !String(detection?.run ?? '').includes('npm view "$PACKAGE_NAME@$RELEASE_VERSION" version')
|| availability?.env?.PACKAGE_NAME !== '@vypdev/copilot'
|| availability.env?.RELEASE_VERSION !== '${{ inputs.version }}'
|| availability?.uses !== 'actions/github-script@v9'
|| Object.keys(availability.with ?? {}).join(',') !== 'script'
|| availability.run !== undefined
|| availability.env?.POLL_INTERVAL !== "${{ vars.NPM_VISIBILITY_POLL_INTERVAL_SECONDS || '20' }}"
|| availability.env?.POLL_TIMEOUT !== "${{ vars.NPM_VISIBILITY_TIMEOUT_SECONDS || '600' }}"
|| !visibilityScript.includes("spawnSync('npm'")
|| !visibilityScript.includes('timeout: remaining')
|| !visibilityScript.includes("killSignal: 'SIGKILL'")
|| !visibilityScript.includes('completedAt > deadline')
|| !visibilityScript.includes('Math.min(interval, timeLeft)')
|| identity?.env?.PACKAGE_NAME !== '@vypdev/copilot'
|| identity.env?.RELEASE_VERSION !== '${{ inputs.version }}'
|| !String(identity.run ?? '').includes('npm view "$PACKAGE_NAME@$RELEASE_VERSION" gitHead')
|| !String(identity.run ?? '').includes('git rev-parse HEAD')
|| !String(identity.run ?? '').includes('"$registry_git_head" = "$local_git_head"')) {
throw new Error(`${relativeFile} publish-npm must install, validate, and publish the exact release tag.`);
}
if (JSON.stringify(job).includes('NPM_TOKEN') || JSON.stringify(job).includes('NODE_AUTH_TOKEN')) {
throw new Error(`${relativeFile} publish-npm must authenticate only through OIDC.`);
}
}
function assertFinalizeReleaseJob(relativeFile, job) {
if (!job) throw new Error(`${relativeFile} must define finalize-release.`);
assertExactTimeout(relativeFile, 'finalize-release', job, FINALIZE_RELEASE_TIMEOUT_MINUTES);
assertExactNeeds(relativeFile, 'finalize-release', job, ['publish-npm']);
const permissions = job.permissions ?? {};
if (Object.keys(permissions).join(',') !== 'contents,issues,pull-requests'
|| permissions.contents !== 'write'
|| permissions.issues !== 'write'
|| permissions['pull-requests'] !== 'write') {
throw new Error(`${relativeFile} finalize-release must have only contents, issues, and pull-requests write permissions.`);
}
const actions = (job.steps ?? [])
.filter(isCopilotAction)
.map(step => step.with?.['single-action']);
if (actions.join(',') !== 'create_release,publish_github_action,published_deployment_action') {
throw new Error(`${relativeFile} finalize-release must create the release, publish the action, and start reconciliation in order.`);
}
}
function assertTransitiveValidationAncestry(file, workflow, validationJobId, allowedFailureJobs = new Set()) {
const relativeFile = relativeWorkflow(file);
const jobs = workflow.jobs ?? {};
const ancestry = new Map();
const visiting = new Set();
const reachesGate = (jobId) => {
if (jobId === validationJobId) return true;
if (ancestry.has(jobId)) return ancestry.get(jobId);
if (visiting.has(jobId)) throw new Error(`${relativeFile} has a cycle in job needs.`);
const job = jobs[jobId];
if (!job) throw new Error(`${relativeFile} references missing job ${jobId}.`);
visiting.add(jobId);
const result = needsFor(job).some(parent => reachesGate(parent));
visiting.delete(jobId);
ancestry.set(jobId, result);
return result;
};
for (const [jobId, job] of Object.entries(jobs)) {
if (!allowedFailureJobs.has(jobId)) assertNoUnsafeCondition(relativeFile, jobId, job);
if (jobId !== validationJobId && !reachesGate(jobId)) {
throw new Error(`${relativeFile} job ${jobId} is not a transitive descendant of ${validationJobId}.`);
}
}
}
function assertFailureReportingJob(relativeFile, job, expectedNeeds, expectedKind) {
if (!job) throw new Error(`${relativeFile} must define report-failure.`);
assertExactTimeout(relativeFile, 'report-failure', job, FAILURE_REPORT_TIMEOUT_MINUTES);
assertExactNeeds(relativeFile, 'report-failure', job, expectedNeeds);
if (job.if !== FAILURE_REPORT_CONDITION) {
throw new Error(`${relativeFile} report-failure must run only for a failed deployment with a launcher issue.`);
}
const permissions = job.permissions ?? {};
const permissionKeys = Object.keys(permissions).sort();
const setup = relativeFile.startsWith('setup/workflows/');
const expectedPermissions = setup
? { issues: 'write' }
: { contents: 'read', issues: 'write' };
if (permissionKeys.join(',') !== Object.keys(expectedPermissions).sort().join(',')
|| Object.entries(expectedPermissions).some(([key, value]) => permissions[key] !== value)) {
throw new Error(`${relativeFile} report-failure must use its exact least-privilege permissions.`);
}
if (job.env !== undefined || Object.prototype.hasOwnProperty.call(job, 'continue-on-error')) {
throw new Error(`${relativeFile} report-failure must not define job-level secrets or bypasses.`);
}
const steps = job.steps ?? [];
if (setup) {
assertSetupFailureReporter(relativeFile, steps, expectedKind);
} else {
assertActiveFailureReporter(relativeFile, steps, expectedKind);
}
}
function assertActiveFailureReporter(relativeFile, steps, expectedKind) {