Repository navigation
Expand file tree
/
Copy patheffect_executor.py
More file actions
413 lines (378 loc) · 15.5 KB
/
Copy patheffect_executor.py
File metadata and controls
413 lines (378 loc) · 15.5 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
"""Deferred effect boundary for CI Retry Gate.
The composite Action persists the Decision Evidence Record first, then invokes this
executor. The executor re-verifies every persisted contract, re-reads current GitHub
state, and only then dispatches a rerun.
"""
from __future__ import annotations
import json
import os
from datetime import datetime, timezone
from pathlib import Path
from ci_retry_gate import GitHubAPI, revalidate_rerun_subject_binding, redact
from decision_record import DecisionRecordError, read_decision_record
from effect_plan import EffectPlanError, read_effect_plan
from execution_lease import (
ExecutionLeaseError,
lease_digest,
load_registry,
read_execution_lease,
verify_effect_ownership,
)
from linearizable_lease import LinearizableLeaseError, verify_fencing_token
from external_monotonic_root import (
DEFAULT_PREDICATE_TYPE,
ExternalMonotonicRootError,
verify_external_monotonic_root,
)
from eba_integration_contract import (
ASSUMPTION_KIND,
AUTHORITY_KIND,
DECISION_KIND,
ContractViolation,
build_execution_receipt,
ensure_decision_allows_request,
read_contract_artifact,
write_contract_artifact,
)
def _now_iso() -> str:
return datetime.now(timezone.utc).isoformat(timespec="seconds").replace("+00:00", "Z")
def _write_output(name: str, value: str) -> None:
path = os.environ.get("GITHUB_OUTPUT")
if path:
safe_value = " ".join(str(value).splitlines())
with open(path, "a", encoding="utf-8") as handle:
handle.write(f"{name}={safe_value}\n")
def _required(name: str) -> str:
value = str(os.environ.get(name) or "").strip()
if not value:
raise RuntimeError(f"{name} is required")
return value
def _load_decision_experience() -> dict:
raw = str(os.environ.get("INPUT_DECISION_EXPERIENCE_JSON") or "").strip()
if not raw:
return {}
try:
value = json.loads(raw)
except json.JSONDecodeError:
return {}
return value if isinstance(value, dict) else {}
def _emit_summary(*, outcome: str, reason: str) -> None:
summary_path = os.environ.get("GITHUB_STEP_SUMMARY")
if not summary_path:
return
with open(summary_path, "a", encoding="utf-8") as handle:
handle.write("\n### Effect boundary\n\n")
handle.write(f"- Outcome: `{outcome}`\n")
handle.write(f"- Reason: {reason}\n")
def main() -> int:
try:
token = _required("INPUT_GITHUB_TOKEN")
plan_path = _required("INPUT_EFFECT_PLAN_PATH")
plan_sha = _required("INPUT_EFFECT_PLAN_SHA256")
decision_record_path = _required("INPUT_DECISION_RECORD_PATH")
decision_record_sha = _required("INPUT_DECISION_RECORD_SHA256")
request_path = _required("INPUT_EBA_REQUEST_PATH")
request_sha = _required("INPUT_EBA_REQUEST_SHA256")
assumption_path = _required("INPUT_EBA_ASSUMPTION_PATH")
assumption_sha = _required("INPUT_EBA_ASSUMPTION_SHA256")
authority_path = _required("INPUT_EBA_AUTHORITY_PATH")
authority_sha = _required("INPUT_EBA_AUTHORITY_SHA256")
decision_path = _required("INPUT_EBA_DECISION_PATH")
decision_sha = _required("INPUT_EBA_DECISION_SHA256")
receipt_path = _required("INPUT_EBA_RECEIPT_PATH")
plan = read_effect_plan(plan_path, expected_sha256=plan_sha)
repo = str(plan["repository"])
decision_record = read_decision_record(
decision_record_path,
expected_sha256=decision_record_sha,
)
if plan["decision_record_sha256"] != decision_record["record_sha256"]:
raise RuntimeError("effect plan/Decision Evidence Record digest mismatch")
action_request = read_contract_artifact(
request_path,
expected_sha256=request_sha,
expected_kind="ActionRequest",
)
assumption_state = read_contract_artifact(
assumption_path,
expected_sha256=assumption_sha,
expected_kind=ASSUMPTION_KIND,
)
authority_grant = read_contract_artifact(
authority_path,
expected_sha256=authority_sha,
expected_kind=AUTHORITY_KIND,
)
contract_decision = read_contract_artifact(
decision_path,
expected_sha256=decision_sha,
expected_kind=DECISION_KIND,
)
except (
RuntimeError,
EffectPlanError,
DecisionRecordError,
ContractViolation,
) as exc:
print(f"::error::deferred effect boundary could not verify persisted state: {redact(str(exc))}")
return 2
mutation_admitted = bool(plan["mutation_admitted"])
rerun_triggered = False
receipt_outcome = "NOT_EXECUTED"
effect_reason = "MUTATION_NOT_ADMITTED"
admitted_at: str | None = None
api = GitHubAPI(token, os.environ.get("GITHUB_API_URL", "https://api.github.com"))
lease = None
lease_block_reason: str | None = None
lease_path = str(os.environ.get("INPUT_EXECUTION_LEASE_PATH") or "").strip()
lease_registry_dir = str(
os.environ.get("INPUT_EXECUTION_LEASE_REGISTRY_DIR") or ""
).strip()
executor_id = str(os.environ.get("INPUT_EXECUTOR_ID") or "").strip()
lease_guard_requested = bool(lease_path or lease_registry_dir or executor_id)
if lease_guard_requested:
if not lease_path or not lease_registry_dir or not executor_id:
lease_block_reason = (
"EXECUTION_LEASE_BLOCK: lease path, registry directory, and executor ID "
"must be supplied together"
)
else:
try:
lease = read_execution_lease(lease_path)
verify_effect_ownership(
candidate_lease=lease,
registry_leases=load_registry(lease_registry_dir),
expected_owner_id=executor_id,
decision_record_sha256=decision_record_sha,
effect_plan_sha256=plan_sha,
repository=repo,
run_id=int(plan["run_id"]),
now=_now_iso(),
)
except ExecutionLeaseError as exc:
lease_block_reason = f"EXECUTION_LEASE_BLOCK: {redact(str(exc))}"
coordination_ref = str(
os.environ.get("INPUT_LINEARIZABLE_COORDINATION_REF") or ""
).strip()
fencing_token_sha = str(
os.environ.get("INPUT_LINEARIZABLE_FENCING_TOKEN_SHA") or ""
).strip()
witness_ref = str(
os.environ.get("INPUT_LINEARIZABLE_WITNESS_REF") or ""
).strip()
linearizable_guard_requested = bool(
coordination_ref or fencing_token_sha or witness_ref
)
linearizable_block_reason: str | None = None
if linearizable_guard_requested:
if not coordination_ref or not fencing_token_sha:
linearizable_block_reason = (
"LINEARIZABLE_FENCE_BLOCK: coordination ref and fencing token "
"must be supplied together"
)
else:
try:
verify_fencing_token(
api,
repo,
coordination_ref=coordination_ref,
fencing_token_sha=fencing_token_sha,
witness_ref=witness_ref or None,
expected_decision_record_sha256=decision_record_sha,
expected_effect_plan_sha256=plan_sha,
)
except (LinearizableLeaseError, RuntimeError) as exc:
linearizable_block_reason = (
f"LINEARIZABLE_FENCE_BLOCK: {redact(str(exc))}"
)
external_root_record_path = str(
os.environ.get("INPUT_EXTERNAL_MONOTONIC_ROOT_RECORD_PATH") or ""
).strip()
external_root_anchor_path = str(
os.environ.get("INPUT_EXTERNAL_MONOTONIC_ROOT_ANCHOR_PATH") or ""
).strip()
external_root_bundle_path = str(
os.environ.get("INPUT_EXTERNAL_MONOTONIC_ROOT_BUNDLE_PATH") or ""
).strip()
external_root_signer_workflow = str(
os.environ.get("INPUT_EXTERNAL_MONOTONIC_ROOT_SIGNER_WORKFLOW") or ""
).strip()
external_root_predicate_type = str(
os.environ.get("INPUT_EXTERNAL_MONOTONIC_ROOT_PREDICATE_TYPE")
or DEFAULT_PREDICATE_TYPE
).strip()
external_root_guard_requested = bool(
external_root_record_path
or external_root_anchor_path
or external_root_bundle_path
or external_root_signer_workflow
)
external_root_block_reason: str | None = None
external_root_record = None
if external_root_guard_requested:
if (
not external_root_record_path
or not external_root_anchor_path
or not external_root_bundle_path
or not external_root_signer_workflow
or not fencing_token_sha
):
external_root_block_reason = (
"EXTERNAL_MONOTONIC_ROOT_BLOCK: root record, stable anchor, "
"attestation bundle, signer workflow, and presented fencing token "
"must be supplied together"
)
elif linearizable_block_reason is None:
try:
external_root_record = verify_external_monotonic_root(
api,
repo,
root_record_path=external_root_record_path,
anchor_path=external_root_anchor_path,
attestation_bundle_path=external_root_bundle_path,
signer_workflow=external_root_signer_workflow,
predicate_type=external_root_predicate_type,
presented_token_sha=fencing_token_sha,
run_id=int(plan["run_id"]),
decision_record_sha256=decision_record_sha,
effect_plan_sha256=plan_sha,
)
except (ExternalMonotonicRootError, RuntimeError) as exc:
detail = " ".join(redact(str(exc)).split())
external_root_block_reason = (
f"EXTERNAL_MONOTONIC_ROOT_BLOCK: {detail}"
)
if mutation_admitted:
if linearizable_block_reason is not None:
effect_reason = linearizable_block_reason
elif external_root_block_reason is not None:
effect_reason = external_root_block_reason
elif lease_block_reason is not None:
effect_reason = lease_block_reason
elif decision_record.get("decision") != "ALLOW":
effect_reason = "DECISION_RECORD_NOT_ALLOW"
else:
evidence_decision = {
"scope": dict(plan["scope"]),
}
expected_failed_jobs = list(plan["failed_jobs"])
try:
binding_valid, binding_reason = revalidate_rerun_subject_binding(
api,
repo,
int(plan["run_id"]),
evidence_decision,
expected_failed_jobs,
)
except RuntimeError as exc:
binding_valid = False
binding_reason = (
"RERUN_SCOPE_UNAVAILABLE: could not re-read current workflow state "
f"before deferred rerun: {redact(str(exc))}"
)
if not binding_valid:
effect_reason = binding_reason
else:
admitted_at = _now_iso()
try:
ensure_decision_allows_request(
contract_decision,
action_request,
now=admitted_at,
assumption_states=[assumption_state],
authority_grant=authority_grant,
)
except ContractViolation as exc:
effect_reason = f"INTEGRATION_CONTRACT_BLOCK: {exc}"
else:
try:
api.rerun_failed_jobs(repo, int(plan["run_id"]))
except RuntimeError as exc:
# A transport failure can occur after GitHub accepted the POST.
# Do not claim NOT_EXECUTED when the effect is epistemically unknown.
receipt_outcome = "UNKNOWN"
effect_reason = f"RERUN_DISPATCH_UNKNOWN: {redact(str(exc))}"
else:
rerun_triggered = True
receipt_outcome = "SUCCEEDED"
effect_reason = "RERUN_DISPATCH_ACCEPTED"
receipt = build_execution_receipt(
action_request=action_request,
decision_artifact=contract_decision,
assumption_states=[assumption_state],
authority_grant=authority_grant,
rerun_triggered=rerun_triggered,
admitted_at=admitted_at,
execution_reason=effect_reason,
execution_outcome=receipt_outcome,
)
receipt_sha = write_contract_artifact(receipt_path, receipt)
experience = _load_decision_experience()
execution = experience.get("execution")
if isinstance(execution, dict):
execution["rerun_triggered"] = rerun_triggered
execution["effect_outcome"] = receipt_outcome
execution["effect_reason"] = effect_reason
_write_output("rerun-triggered", "true" if rerun_triggered else "false")
_write_output("effect-outcome", receipt_outcome)
_write_output("effect-reason", effect_reason)
_write_output(
"linearizable-fence-enforced",
"true" if linearizable_guard_requested else "false",
)
_write_output("linearizable-coordination-ref", coordination_ref)
_write_output("linearizable-fencing-token-sha", fencing_token_sha)
_write_output("linearizable-witness-ref", witness_ref)
_write_output(
"external-monotonic-root-enforced",
"true" if external_root_guard_requested else "false",
)
_write_output("external-monotonic-root-anchor-path", external_root_anchor_path)
_write_output(
"external-monotonic-root-token-sha",
str(external_root_record.get("fencing_token_sha") or "")
if isinstance(external_root_record, dict)
else "",
)
_write_output(
"external-monotonic-root-epoch",
str(external_root_record.get("epoch") or "")
if isinstance(external_root_record, dict)
else "",
)
_write_output(
"external-monotonic-root-state",
str(external_root_record.get("lifecycle_state") or "")
if isinstance(external_root_record, dict)
else "",
)
_write_output("execution-lease-enforced", "true" if lease_guard_requested else "false")
_write_output(
"execution-lease-owner",
str(lease.get("owner_id") or "") if isinstance(lease, dict) else "",
)
_write_output(
"execution-lease-epoch",
str(lease.get("epoch") or "") if isinstance(lease, dict) else "",
)
_write_output(
"execution-lease-sha256",
lease_digest(lease) if isinstance(lease, dict) else "",
)
_write_output("eba-receipt-path", str(Path(receipt_path)))
_write_output("eba-receipt-sha256", receipt_sha)
_write_output(
"eba-receipt-json",
json.dumps(receipt, separators=(",", ":"), sort_keys=True),
)
if experience:
_write_output(
"decision-experience-json",
json.dumps(experience, separators=(",", ":"), sort_keys=True),
)
_emit_summary(outcome=receipt_outcome, reason=effect_reason)
# UNKNOWN is not retried here. The caller receives the receipt and a successful
# process exit so reconciliation can determine what GitHub actually did later.
return 0
if __name__ == "__main__":
raise SystemExit(main())