mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-11-29 01:08:56 +01:00
Bind demo native proof to the consuming workflow attempt
Stable convergence run 41389692448 refused the demo transaction before mutation because no main-push native proof was available. Make the existing secret-free native workflow an explicit update/recovery prerequisite instead of relying on path-filtered history. Admit only the fixed consumer's exact source, run, attempt and successful native job; keep verification-only non-mutating and preserve the full native watches, cleanup, signed-runtime admission, production lease and retained rollback. Add executable source/attempt/refusal controls and consuming workflow-graph regression checks with the matching contracts. Change-source: pulse-maintainer
This commit is contained in:
94
.github/scripts/dispatch-demo-runtime.py
vendored
94
.github/scripts/dispatch-demo-runtime.py
vendored
@@ -6,7 +6,6 @@ import base64
|
||||
import hashlib
|
||||
import importlib.util
|
||||
import json
|
||||
import datetime
|
||||
import re
|
||||
import os
|
||||
from pathlib import Path
|
||||
@@ -131,61 +130,68 @@ def payload(mode, environ=os.environ):
|
||||
'binary': base64.b64encode(binary).decode(), 'version_file': base64.b64encode(version_file).decode()}
|
||||
|
||||
|
||||
NATIVE_PATHS = ('.github/scripts/demo-runtime-transaction.py',
|
||||
'.github/scripts/dispatch-demo-runtime.py',
|
||||
'.github/scripts/tests/demo_runtime_native.py',
|
||||
'.github/workflows/demo-runtime-native.yml')
|
||||
NATIVE_JOBS = {
|
||||
'.github/workflows/release-convergence.yml':
|
||||
'Converge stable demo / Prove demo transaction / Native demo systemd/Caddy proof',
|
||||
'.github/workflows/recover-demo-server.yml':
|
||||
'Prove demo transaction / Native demo systemd/Caddy proof',
|
||||
}
|
||||
|
||||
|
||||
def require_native(environ=os.environ):
|
||||
"""Fail closed before SSH; a metadata/component pass is not native proof."""
|
||||
"""Require the same attempt's secret-free native prerequisite before SSH.
|
||||
|
||||
Path-filtered main history can be empty and is not a production prerequisite
|
||||
scheduler. Both consumers now run the reviewed native workflow themselves;
|
||||
a previous attempt, another source or a component pass cannot admit mutation.
|
||||
"""
|
||||
sha, repository = environ['GITHUB_SHA'], environ['GITHUB_REPOSITORY']
|
||||
if not re.fullmatch(r'[0-9a-f]{40}', sha) or repository != 'rcourtman/Pulse':
|
||||
run_id, attempt = environ['GITHUB_RUN_ID'], environ['GITHUB_RUN_ATTEMPT']
|
||||
if (not re.fullmatch(r'[0-9a-f]{40}', sha) or repository != 'rcourtman/Pulse'
|
||||
or not re.fullmatch(r'[1-9][0-9]*', run_id)
|
||||
or not re.fullmatch(r'[1-9][0-9]*', attempt)):
|
||||
raise ValueError('native-source-identity')
|
||||
since = (datetime.datetime.now(datetime.timezone.utc).date() - datetime.timedelta(days=6)).isoformat()
|
||||
base = f'repos/{repository}/actions/workflows/demo-runtime-native.yml/runs?branch=main&event=push&per_page=5'
|
||||
|
||||
def read(query):
|
||||
result = subprocess.run(['gh', 'api', query], capture_output=True, timeout=30, check=False)
|
||||
if result.returncode:
|
||||
raise ValueError('native-evidence-unavailable')
|
||||
rows = json.loads(result.stdout)['workflow_runs']
|
||||
if not isinstance(rows, list) or any(not isinstance(row, dict) for row in rows):
|
||||
value = json.loads(result.stdout)
|
||||
if not isinstance(value, dict):
|
||||
raise ValueError('native-evidence-malformed')
|
||||
for row in rows:
|
||||
if (type(row.get('id')) is not int or not re.fullmatch(r'[0-9a-f]{40}', str(row.get('head_sha')))
|
||||
or type(row.get('run_attempt')) is not int or not isinstance(row.get('updated_at'), str)
|
||||
or not isinstance(row.get('created_at'), str)):
|
||||
raise ValueError('native-evidence-malformed')
|
||||
return rows
|
||||
return value
|
||||
|
||||
rows = read(base + '&created=' + since + '..*')
|
||||
if len(rows) < 5:
|
||||
rows += read(base)
|
||||
merged = {}
|
||||
for row in rows:
|
||||
old = merged.get(row['id'])
|
||||
if old is None or (row['run_attempt'], row['updated_at']) > (old['run_attempt'], old['updated_at']):
|
||||
merged[row['id']] = row
|
||||
for row in sorted(merged.values(), key=lambda r: (r['created_at'], r['id']), reverse=True):
|
||||
head = row['head_sha']
|
||||
if (row.get('event') != 'push' or row.get('head_branch') != 'main'
|
||||
or row.get('path') != '.github/workflows/demo-runtime-native.yml'):
|
||||
raise ValueError('native-run-identity')
|
||||
ancestor = subprocess.run(['git', 'merge-base', '--is-ancestor', head, sha], capture_output=True)
|
||||
if ancestor.returncode != 0:
|
||||
continue
|
||||
matching = subprocess.run(['git', 'diff', '--quiet', head, sha, '--', *NATIVE_PATHS], capture_output=True)
|
||||
if matching.returncode == 1:
|
||||
continue
|
||||
if matching.returncode != 0:
|
||||
raise ValueError('native-source-unavailable')
|
||||
# Never select an older green sample over a newer matching failure or
|
||||
# pending run. The native driver exits success only with full cleanup.
|
||||
if row.get('status') != 'completed' or row.get('conclusion') != 'success':
|
||||
raise ValueError('native-acceptance-not-passed')
|
||||
return {'run_id': row['id'], 'run_attempt': row['run_attempt'], 'source_sha': head}
|
||||
raise ValueError('native-acceptance-missing')
|
||||
base = f'repos/{repository}/actions/runs/{run_id}'
|
||||
run = read(base)
|
||||
if (type(run.get('id')) is not int or run['id'] != int(run_id)
|
||||
or type(run.get('run_attempt')) is not int or run['run_attempt'] != int(attempt)
|
||||
or run.get('head_sha') != sha or run.get('head_branch') != 'main'
|
||||
or run.get('event') != 'workflow_dispatch'
|
||||
or not isinstance(run.get('path'), str) or run['path'] not in NATIVE_JOBS
|
||||
or not isinstance(run.get('repository'), dict)
|
||||
or run['repository'].get('full_name') != repository):
|
||||
raise ValueError('native-run-identity')
|
||||
# The attempt-specific endpoint cannot borrow a successful job from a prior
|
||||
# rerun. Refuse incomplete pagination rather than infer an absent proof.
|
||||
result = read(base + f'/attempts/{attempt}/jobs?per_page=100')
|
||||
jobs = result.get('jobs')
|
||||
if (not isinstance(jobs, list) or type(result.get('total_count')) is not int
|
||||
or result['total_count'] != len(jobs) or len(jobs) > 100
|
||||
or any(not isinstance(job, dict) for job in jobs)):
|
||||
raise ValueError('native-evidence-malformed')
|
||||
matches = [job for job in jobs if job.get('name') == NATIVE_JOBS[run['path']]]
|
||||
if len(matches) != 1:
|
||||
raise ValueError('native-acceptance-missing')
|
||||
job = matches[0]
|
||||
if (type(job.get('id')) is not int or job['id'] <= 0
|
||||
or type(job.get('run_id')) is not int or job['run_id'] != int(run_id)
|
||||
or type(job.get('run_attempt')) is not int or job['run_attempt'] != int(attempt)
|
||||
or job.get('head_sha') != sha):
|
||||
raise ValueError('native-job-identity')
|
||||
if job.get('status') != 'completed' or job.get('conclusion') != 'success':
|
||||
raise ValueError('native-acceptance-not-passed')
|
||||
return {'run_id': int(run_id), 'run_attempt': int(attempt), 'source_sha': sha,
|
||||
'job_id': job['id']}
|
||||
|
||||
|
||||
def prepare_runtime():
|
||||
|
||||
@@ -7,6 +7,7 @@ import os
|
||||
from pathlib import Path
|
||||
import signal
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
@@ -948,54 +949,119 @@ class PortableTransactionFixtureTest(unittest.TestCase):
|
||||
|
||||
|
||||
class NativeGateTest(unittest.TestCase):
|
||||
def row(self, run_id=10, **fields):
|
||||
return {'id': run_id, 'head_sha': 'a' * 40, 'run_attempt': 1,
|
||||
'created_at': '2026-10-03T01:00:00Z', 'updated_at': '2026-10-03T01:10:00Z',
|
||||
'path': '.github/workflows/demo-runtime-native.yml', 'event': 'push', 'head_branch': 'main',
|
||||
def environment(self):
|
||||
return {'GITHUB_SHA': 'b' * 40, 'GITHUB_REPOSITORY': 'rcourtman/Pulse',
|
||||
'GITHUB_RUN_ID': '123', 'GITHUB_RUN_ATTEMPT': '2'}
|
||||
|
||||
def run_record(self, **fields):
|
||||
return {'id': 123, 'run_attempt': 2, 'head_sha': 'b' * 40,
|
||||
'head_branch': 'main', 'event': 'workflow_dispatch',
|
||||
'path': '.github/workflows/release-convergence.yml',
|
||||
'repository': {'full_name': 'rcourtman/Pulse'}, **fields}
|
||||
|
||||
def job(self, **fields):
|
||||
return {'id': 10, 'run_id': 123, 'run_attempt': 2, 'head_sha': 'b' * 40,
|
||||
'name': dispatcher.NATIVE_JOBS['.github/workflows/release-convergence.yml'],
|
||||
'status': 'completed', 'conclusion': 'success', **fields}
|
||||
|
||||
def check(self, rows, history=None, read_exit=0, same=True):
|
||||
def check(self, *, run=None, jobs=None, listing=None, read_exit=0, environ=None):
|
||||
calls = []
|
||||
run = self.run_record() if run is None else run
|
||||
jobs = [self.job()] if jobs is None else jobs
|
||||
listing = {'total_count': len(jobs), 'jobs': jobs} if listing is None else listing
|
||||
def command(argv, **kwargs):
|
||||
calls.append(argv)
|
||||
if argv[0] == 'gh':
|
||||
payload = rows if 'created=' in argv[-1] else (history if history is not None else rows)
|
||||
return subprocess.CompletedProcess(argv, read_exit, json.dumps({'workflow_runs': payload}).encode())
|
||||
return subprocess.CompletedProcess(argv, 0 if argv[1] == 'merge-base' or same else 1)
|
||||
self.assertEqual(argv[:2], ['gh', 'api'])
|
||||
self.assertEqual(kwargs, {'capture_output': True, 'timeout': 30, 'check': False})
|
||||
body = run if len(calls) == 1 else listing
|
||||
return subprocess.CompletedProcess(argv, read_exit, json.dumps(body).encode())
|
||||
with patch.object(dispatcher.subprocess, 'run', side_effect=command):
|
||||
result = dispatcher.require_native({'GITHUB_SHA': 'b' * 40, 'GITHUB_REPOSITORY': 'rcourtman/Pulse'})
|
||||
result = dispatcher.require_native(self.environment() if environ is None else environ)
|
||||
return result, calls
|
||||
|
||||
def test_matching_reviewed_native_source_passes_with_current_first_inventory(self):
|
||||
result, calls = self.check([self.row()])
|
||||
self.assertEqual(result['run_id'], 10)
|
||||
reads = [c for c in calls if c[0] == 'gh']
|
||||
self.assertEqual(len(reads), 2)
|
||||
self.assertIn('created=', reads[0][-1]); self.assertNotIn('created=', reads[1][-1])
|
||||
self.assertEqual(set(calls[-1][6:]), set(dispatcher.NATIVE_PATHS))
|
||||
def test_same_run_current_attempt_native_job_passes_without_history_or_git(self):
|
||||
result, calls = self.check()
|
||||
self.assertEqual(result, {'run_id': 123, 'run_attempt': 2, 'source_sha': 'b' * 40, 'job_id': 10})
|
||||
self.assertEqual([call[-1] for call in calls], [
|
||||
'repos/rcourtman/Pulse/actions/runs/123',
|
||||
'repos/rcourtman/Pulse/actions/runs/123/attempts/2/jobs?per_page=100'])
|
||||
|
||||
def test_newer_matching_failure_or_pending_run_cannot_use_older_green(self):
|
||||
for fields in ({'conclusion': 'failure'}, {'status': 'in_progress', 'conclusion': None}):
|
||||
def test_fixed_recovery_consumer_requires_its_own_native_job(self):
|
||||
path = '.github/workflows/recover-demo-server.yml'
|
||||
result, _ = self.check(run=self.run_record(path=path),
|
||||
jobs=[self.job(name=dispatcher.NATIVE_JOBS[path])])
|
||||
self.assertEqual(result['job_id'], 10)
|
||||
with self.assertRaisesRegex(ValueError, 'missing'):
|
||||
self.check(run=self.run_record(path=path))
|
||||
|
||||
def test_prior_attempt_or_other_source_run_cannot_enable_mutation(self):
|
||||
for fields in ({'id': 122}, {'run_attempt': 1}, {'head_sha': 'a' * 40},
|
||||
{'head_branch': 'release-candidate/fixture'}, {'event': 'pull_request'},
|
||||
{'path': '.github/workflows/build-and-test.yml'}, {'path': []},
|
||||
{'repository': {'full_name': 'rcourtman/pulse-pro'}},
|
||||
{'repository': []}, {'id': True}):
|
||||
with self.subTest(fields=fields), self.assertRaisesRegex(ValueError, 'run-identity'):
|
||||
self.check(run=self.run_record(**fields))
|
||||
|
||||
def test_prior_attempt_or_other_source_job_cannot_enable_mutation(self):
|
||||
for fields in ({'run_id': 122}, {'run_attempt': 1}, {'head_sha': 'a' * 40},
|
||||
{'id': True}, {'id': 0}, {'run_attempt': True}, {'run_id': True}):
|
||||
with self.subTest(fields=fields), self.assertRaisesRegex(ValueError, 'job-identity'):
|
||||
self.check(jobs=[self.job(**fields)])
|
||||
|
||||
def test_failed_pending_or_skipped_native_proof_never_admits_mutation(self):
|
||||
for fields in ({'conclusion': 'failure'}, {'status': 'in_progress', 'conclusion': None},
|
||||
{'conclusion': 'skipped'}, {'conclusion': 'cancelled'},
|
||||
{'conclusion': 'timed_out'}, {'conclusion': 'neutral'}):
|
||||
with self.subTest(fields=fields), self.assertRaisesRegex(ValueError, 'not-passed'):
|
||||
self.check([self.row(11, **fields), self.row(10)])
|
||||
self.check(jobs=[self.job(**fields)])
|
||||
|
||||
def test_latest_duplicate_attempt_retains_failure(self):
|
||||
with self.assertRaisesRegex(ValueError, 'not-passed'):
|
||||
self.check([self.row()], [self.row(run_attempt=2, conclusion='failure')])
|
||||
def test_missing_component_only_or_duplicate_proof_never_admits_mutation(self):
|
||||
for jobs in ([], [self.job(name='Script smoke tests & backend build')],
|
||||
[self.job(), self.job(id=11)]):
|
||||
with self.subTest(jobs=jobs), self.assertRaisesRegex(ValueError, 'missing'):
|
||||
self.check(jobs=jobs)
|
||||
|
||||
def test_missing_changed_or_misidentified_proof_cannot_enable_mutation(self):
|
||||
for rows, same in (([], True), ([self.row()], False), ([self.row(event='workflow_dispatch')], True)):
|
||||
with self.subTest(rows=rows, same=same), self.assertRaises(ValueError):
|
||||
self.check(rows, same=same)
|
||||
def test_truncated_or_malformed_job_collection_is_not_proof(self):
|
||||
for listing in ({'total_count': 101, 'jobs': [self.job()]},
|
||||
{'total_count': 0, 'jobs': {}},
|
||||
{'total_count': True, 'jobs': [self.job()]},
|
||||
{'total_count': 1, 'jobs': [None]}, {}):
|
||||
with self.subTest(listing=listing), self.assertRaisesRegex(ValueError, 'malformed'):
|
||||
self.check(listing=listing)
|
||||
|
||||
def test_invalid_launch_identity_stops_before_any_read(self):
|
||||
for fields in ({'GITHUB_SHA': 'main'}, {'GITHUB_REPOSITORY': 'rcourtman/pulse-pro'},
|
||||
{'GITHUB_RUN_ID': '0'}, {'GITHUB_RUN_ATTEMPT': '../1'}):
|
||||
with self.subTest(fields=fields), patch.object(dispatcher.subprocess, 'run') as child:
|
||||
with self.assertRaisesRegex(ValueError, 'source-identity'):
|
||||
dispatcher.require_native({**self.environment(), **fields})
|
||||
child.assert_not_called()
|
||||
|
||||
def test_refused_read_has_no_history_fallback_or_ssh(self):
|
||||
calls = []
|
||||
def denied(argv, **kwargs):
|
||||
calls.append(argv); return subprocess.CompletedProcess(argv, 1, b'')
|
||||
with patch.object(dispatcher.subprocess, 'run', side_effect=denied), self.assertRaises(ValueError):
|
||||
dispatcher.require_native({'GITHUB_SHA': 'b' * 40, 'GITHUB_REPOSITORY': 'rcourtman/Pulse'})
|
||||
self.assertEqual(len(calls), 1)
|
||||
self.assertEqual(calls[0][0:2], ['gh', 'api'])
|
||||
for fail_on in (1, 2):
|
||||
calls = []
|
||||
def denied(argv, **kwargs):
|
||||
calls.append(argv)
|
||||
return subprocess.CompletedProcess(argv, 1 if len(calls) == fail_on else 0,
|
||||
json.dumps(self.run_record()).encode())
|
||||
with self.subTest(fail_on=fail_on), patch.object(dispatcher.subprocess, 'run', side_effect=denied):
|
||||
with self.assertRaisesRegex(ValueError, 'evidence-unavailable'):
|
||||
dispatcher.require_native(self.environment())
|
||||
self.assertEqual(len(calls), fail_on)
|
||||
self.assertTrue(all(call[:2] == ['gh', 'api'] for call in calls))
|
||||
|
||||
def test_main_refuses_before_payload_or_production_ssh(self):
|
||||
with patch.object(dispatcher, 'require_native', side_effect=ValueError('native-acceptance-missing')), \
|
||||
patch.object(dispatcher, 'payload') as payload, \
|
||||
patch.object(dispatcher.subprocess, 'run') as child, \
|
||||
patch.object(sys, 'argv', ['dispatch-demo-runtime.py', 'update']), \
|
||||
patch('sys.stdout', new_callable=io.StringIO) as output:
|
||||
self.assertEqual(dispatcher.main(), 1)
|
||||
self.assertEqual(json.loads(output.getvalue()), {
|
||||
'schema_version': 2, 'status': 'refused', 'mutated': False,
|
||||
'failure': 'native-acceptance-unavailable'})
|
||||
payload.assert_not_called(); child.assert_not_called()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user