diff --git a/.github/workflows/lm006-sign-coefficient.yml b/.github/workflows/lm006-sign-coefficient.yml new file mode 100644 index 000000000..21361d33a --- /dev/null +++ b/.github/workflows/lm006-sign-coefficient.yml @@ -0,0 +1,60 @@ +name: LM006 cubic sign coefficient exact replay + +'on': + pull_request: + paths: + - 'coefficients/lm006_wrong_sign_20261006_r20/**' + - 'tools/lm006_sign_coefficient_replay.py' + - 'tests/test_lm006_sign_coefficient_replay.py' + - '.github/workflows/lm006-sign-coefficient.yml' + - 'reviews/lm006_wrong_sign_asymptotic_20261006/NOTE.md' + - 'imports/lifetime_parent_20260925/UNIFORM_MATRIX_CAP_AND_LIFETIME.md' + push: + branches: [main] + paths: + - 'coefficients/lm006_wrong_sign_20261006_r20/**' + - 'tools/lm006_sign_coefficient_replay.py' + - 'tests/test_lm006_sign_coefficient_replay.py' + - '.github/workflows/lm006-sign-coefficient.yml' + - 'reviews/lm006_wrong_sign_asymptotic_20261006/NOTE.md' + - 'imports/lifetime_parent_20260925/UNIFORM_MATRIX_CAP_AND_LIFETIME.md' + workflow_dispatch: + +permissions: + contents: read + +jobs: + numerical-replay: + runs-on: ubuntu-latest + timeout-minutes: 30 + strategy: + fail-fast: false + matrix: + mode: [normal, optimized] + name: lm006-sign-coefficient (${{ matrix.mode }}) + steps: + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 + with: + persist-credentials: false + fetch-depth: 1 + - uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 + with: + python-version: '3.13.5' + - name: Replay protocol controls and the actual frozen numerical certificate + shell: bash + run: | + set -euo pipefail + mkdir -p "$RUNNER_TEMP/lm006-protocol" + FLAGS="-B -S" + if [ "${{ matrix.mode }}" = optimized ]; then FLAGS="-B -O -S"; fi + python $FLAGS -m unittest discover -s tests -p 'test_lm006_sign_coefficient_replay.py' -v 2>&1 | tee "$RUNNER_TEMP/lm006-protocol/${{ matrix.mode }}.log" + python -B -S tools/lm006_sign_coefficient_replay.py --mode "${{ matrix.mode }}" --output "$RUNNER_TEMP/lm006-certificate-${{ matrix.mode }}-${{ github.run_attempt }}" + - name: Preserve actual success or failure evidence + if: always() + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 + with: + name: lm006-certificate-${{ matrix.mode }}-${{ github.run_id }}-${{ github.run_attempt }} + path: | + ${{ runner.temp }}/lm006-certificate-${{ matrix.mode }}-${{ github.run_attempt }}/ + ${{ runner.temp }}/lm006-protocol/ + if-no-files-found: error diff --git a/tests/test_lm006_sign_coefficient_replay.py b/tests/test_lm006_sign_coefficient_replay.py new file mode 100644 index 000000000..6e6d5cead --- /dev/null +++ b/tests/test_lm006_sign_coefficient_replay.py @@ -0,0 +1,314 @@ +"""Real-process and source-custody controls; no new mathematical verdict.""" +import importlib.util +import json +import os +from pathlib import Path +import re +import signal +import subprocess +import sys +import tempfile +import time +import unittest + +DRIVER = Path(__file__).resolve().parents[1] / 'tools/lm006_sign_coefficient_replay.py' + +class ReplayTests(unittest.TestCase): + def setUp(self): + self.assertTrue(DRIVER.is_file(), 'replay implementation missing') + spec = importlib.util.spec_from_file_location('lm006_replay', DRIVER) + self.m = importlib.util.module_from_spec(spec) + spec.loader.exec_module(self.m) + self.tmp = tempfile.TemporaryDirectory() + self.addCleanup(self.tmp.cleanup) + self.root = Path(self.tmp.name) / 'repo' + self.root.mkdir() + self.logs = Path(self.tmp.name) / 'logs' + self.logs.mkdir() + + def fixture(self): + pins = {} + for name in self.m.PINS: + p = self.root / name + p.parent.mkdir(parents=True, exist_ok=True) + p.write_bytes(('fixture: ' + name + '\n').encode()) + pins[name] = self.m.git_blob(p.read_bytes()) + return pins + + def child(self, code, expected=b'ok\n', kind='exact', timeout=3, name='child'): + return self.m.run_step([sys.executable, '-B', '-S', '-c', code], self.root, + self.logs, name, expected, kind, timeout) + + def test_source_identity_and_exact_membership(self): + pins = self.fixture() + identities = self.m.validate_snapshot(self.root, pins) + self.assertEqual(set(identities), set(pins)) + for name, pin in pins.items(): + self.assertEqual(identities[name]['git_blob'], pin) + self.assertRegex(identities[name]['sha256'], '^[0-9a-f]{64}$') + extra = self.root / self.m.PACKET / 'extra.txt' + extra.write_text('extra') + with self.assertRaisesRegex(self.m.ReplayError, 'packet_membership'): + self.m.validate_snapshot(self.root, pins) + + def test_changed_or_missing_payload_and_upstream(self): + pins = self.fixture() + for name in pins: + p = self.root / name + old = p.read_bytes() + p.write_bytes(old+b'changed') + with self.assertRaisesRegex(self.m.ReplayError, 'source_identity'): + self.m.validate_snapshot(self.root, pins) + p.unlink() + with self.assertRaisesRegex(self.m.ReplayError, 'source_missing'): + self.m.validate_snapshot(self.root, pins) + p.write_bytes(old) + + def test_symlink_leaf_and_parent_rejected(self): + pins = self.fixture() + p = self.root / self.m.PACKET / 'RESULTS.json' + old = p.read_bytes() + outside = Path(self.tmp.name)/'outside' + outside.write_bytes(old) + p.unlink();p.symlink_to(outside) + with self.assertRaisesRegex(self.m.ReplayError, 'source_symlink'): + self.m.validate_snapshot(self.root,pins) + p.unlink();p.write_bytes(old) + directory = self.root / 'reviews' + moved = self.root / 'moved' + directory.rename(moved);directory.symlink_to(moved,target_is_directory=True) + with self.assertRaisesRegex(self.m.ReplayError, 'source_symlink'): + self.m.validate_snapshot(self.root,pins) + + def test_path_validation(self): + for name in ('../x','/x','a/../b','a//b','a\\b','a/./b',''): + with self.subTest(name=name),self.assertRaises(self.m.ReplayError): + self.m.source_file(self.root,name) + + def test_success_process_evidence(self): + row = self.child("print('ok')") + self.assertEqual(row['returncode'],0) + self.assertFalse(row['timed_out']) + self.assertEqual(row['status'],'PASS') + self.assertEqual((self.logs/'child.stdout').read_bytes(),b'ok\n') + self.assertEqual((self.logs/'child.stderr').read_bytes(),b'') + self.assertEqual(json.loads((self.logs/'child.process.json').read_bytes()),row) + + def test_wrong_exit_silent_garbage_and_stderr(self): + for i,(code,reason) in enumerate([ + ("raise RuntimeError('unrelated failure')",'unexpected_exit'), + ("pass",'stdout_mismatch'), + ("print('garbage')",'stdout_mismatch'), + ("import sys;print('ok');print('warning',file=sys.stderr)",'unexpected_stderr')]): + name='bad'+str(i) + with self.subTest(code=code),self.assertRaisesRegex(self.m.ReplayError,reason): + self.child(code,name=name) + rec=json.loads((self.logs/(name+'.process.json')).read_bytes()) + self.assertEqual(rec['status'],'FAIL') + self.assertEqual(rec['failure'],reason) + + def test_timeout_preserves_both_streams_and_process_record(self): + with self.assertRaisesRegex(self.m.ReplayError,'timeout'): + self.child("import sys,time;print('started',flush=True);print('partial',file=sys.stderr,flush=True);time.sleep(20)",timeout=0.3) + rec=json.loads((self.logs/'child.process.json').read_bytes()) + self.assertTrue(rec['timed_out']) + self.assertEqual(rec['failure'],'timeout') + self.assertEqual((self.logs/'child.stdout').read_bytes(),b'started\n') + self.assertEqual((self.logs/'child.stderr').read_bytes(),b'partial\n') + self.assertIsInstance(rec['returncode'],int) + + @unittest.skipUnless(os.name=='posix','workflow and process groups are POSIX') + def test_timeout_stops_child_process_group(self): + marker=Path(self.tmp.name)/'late-marker' + grandchild="import time;from pathlib import Path;time.sleep(1);Path("+repr(str(marker))+").write_text('bad')" + code="import subprocess,sys,time;subprocess.Popen([sys.executable,'-c',"+repr(grandchild)+"]);print('ready',flush=True);time.sleep(20)" + with self.assertRaisesRegex(self.m.ReplayError,'timeout'): + self.child(code,timeout=0.3) + time.sleep(1.1) + self.assertFalse(marker.exists()) + + def test_spawn_failure_has_record(self): + with self.assertRaisesRegex(self.m.ReplayError,'spawn_error'): + self.m.run_step(['/nonexistent/lm006-executable'],self.root,self.logs,'spawn',b'','exact',1) + row=json.loads((self.logs/'spawn.process.json').read_bytes()) + self.assertEqual(row['status'],'FAIL');self.assertIsNone(row['returncode']) + self.assertIn('FileNotFoundError',row['detail']) + + def test_existing_step_evidence_never_overwritten(self): + self.child("print('ok')") + old={p.name:p.read_bytes() for p in self.logs.iterdir()} + with self.assertRaisesRegex(self.m.ReplayError,'step_evidence_exists'): + self.child("print('changed')") + self.assertEqual(old,{p.name:p.read_bytes() for p in self.logs.iterdir()}) + + def test_unittest_contract_requires_all_named_passes(self): + good=('\n'.join(name+' (test_enclosure.EnclosureTests.'+name+') ... ok' for name in self.m.TEST_NAMES) + +'\n\n'+'-'*70+'\nRan 15 tests in 0.001s\n\nOK\n').encode() + self.m.check_output(b'',good,None,'tests') + altered=[good.replace(b' ... ok',b' ... skipped',1),good+b'noise', + good.replace(b'Ran 15',b'Ran 0'),good.replace(b' ... ok',b' ... FAIL',1), + good[good.index(b'\n\n')+2:],good.replace(self.m.TEST_NAMES[0].encode(),b'wrong_test',1)] + for bad in altered: + with self.assertRaisesRegex(self.m.ReplayError,'test_report_mismatch'): + self.m.check_output(b'',bad,None,'tests') + with self.assertRaisesRegex(self.m.ReplayError,'test_report_mismatch'): + self.m.check_output(b'noise',good,None,'tests') + + def test_exact_output_contract_keeps_json_types_and_scope(self): + want=b'{"independently_reviewed": false, "grid": 2048}\n' + self.m.check_output(want,b'',want,'exact') + for bad in (b'',want.replace(b'false',b'0'),want.replace(b'false',b'true'),want+b'\n'): + with self.assertRaisesRegex(self.m.ReplayError,'stdout_mismatch'): + self.m.check_output(bad,b'',want,'exact') + with self.assertRaisesRegex(self.m.ReplayError,'unknown_contract'): + self.m.check_output(b'',b'',b'','unknown') + + def test_host_identity_and_strict_run_identifiers(self): + head='1'*40 + env={'GITHUB_ACTIONS':'true','GITHUB_REPOSITORY':self.m.REPOSITORY, + 'GITHUB_SHA':head,'GITHUB_RUN_ID':'123','GITHUB_RUN_ATTEMPT':'1'} + self.m.host_identity(head,env) + for key,bad in [('GITHUB_SHA','2'*40),('GITHUB_REPOSITORY','elsewhere/repo'), + ('GITHUB_RUN_ID','0'),('GITHUB_RUN_ID','123'),('GITHUB_RUN_ATTEMPT','01')]: + changed=dict(env);changed[key]=bad + with self.assertRaisesRegex(self.m.ReplayError,'host_identity'): + self.m.host_identity(head,changed) + self.m.host_identity(head,{}) + + def test_execution_sources_are_committed_and_clean(self): + for name in self.m.EXECUTION_FILES: + p=self.root/name;p.parent.mkdir(parents=True,exist_ok=True);p.write_text('test fixture\n') + subprocess.run(['git','init','-q'],cwd=self.root,check=True) + subprocess.run(['git','add','.'],cwd=self.root,check=True) + subprocess.run(['git','-c','user.name=Fixture','-c','user.email=fixture@example.invalid', + '-c','commit.gpgsign=false','-c','maintenance.auto=false','commit','-qm','fixture'],cwd=self.root,check=True) + head=self.m.git_head(self.root) + self.m.execution_sources(self.root,head) + target=self.root/self.m.EXECUTION_FILES[0];target.write_text('changed') + with self.assertRaisesRegex(self.m.ReplayError,'execution_source_drift'): + self.m.execution_sources(self.root,head) + + def test_preflight_failure_writes_failed_receipt(self): + output=Path(self.tmp.name)/'preflight' + with self.assertRaises(self.m.ReplayError): + self.m.execute(self.root,output,'normal') + receipt=json.loads((output/'receipt.json').read_bytes()) + self.assertEqual(receipt['status'],'FAIL');self.assertFalse(receipt['scientific_acceptance']) + self.assertEqual(receipt['steps'],{}) + self.assertIn('failure',receipt) + + def test_rejects_existing_or_internal_evidence_directory(self): + out=Path(self.tmp.name)/'existing';out.mkdir();sentinel=out/'keep';sentinel.write_text('keep') + with self.assertRaisesRegex(self.m.ReplayError,'evidence_exists'): + self.m.execute(self.root,out,'normal') + self.assertEqual(sentinel.read_text(),'keep') + with self.assertRaisesRegex(self.m.ReplayError,'evidence_inside_source'): + self.m.execute(self.root,self.root/'logs','normal') + with self.assertRaisesRegex(self.m.ReplayError,'invalid_mode'): + self.m.execute(self.root,Path(self.tmp.name)/'other','wrong') + + def test_cli_rejects_invalid_arguments(self): + for args in ([],['--mode','wrong','--output','x'],['--mode','normal','--output','x','--grid','8']): + p=subprocess.run([sys.executable,'-B','-S',str(DRIVER),*args],capture_output=True) + self.assertEqual(p.returncode,2) + + def synthetic_project(self, coefficient_suffix='', coefficient_output='synthetic', bad_test=False): + """Real committed temporary source; alternate in-memory pins are TEST ONLY.""" + import shutil + # This disposable Git repository is not the Actions checkout. Keep the + # production host check intact; only this synthetic test's environment + # is isolated, then restored even when the test fails. + previous = os.environ.get('GITHUB_ACTIONS') + def restore(): + if previous is None: + os.environ.pop('GITHUB_ACTIONS', None) + else: + os.environ['GITHUB_ACTIONS'] = previous + self.addCleanup(restore) + os.environ['GITHUB_ACTIONS'] = 'false' + pins = self.fixture() + packet = self.root / self.m.PACKET + (packet / 'RESULTS.json').write_text('synthetic\n') + methods = '\n'.join(' def '+name+'(self): self.assertTrue('+('False' if bad_test and i==0 else 'True')+')' + for i,name in enumerate(self.m.TEST_NAMES)) + (packet / 'test_enclosure.py').write_text('import unittest\nclass EnclosureTests(unittest.TestCase):\n'+methods+'\n') + (packet / 'enclose.py').write_text("import sys\nassert sys.argv[1:]==['--grid','2048']\nprint("+repr(coefficient_output)+")\n"+coefficient_suffix+'\n') + for name in self.m.EXECUTION_FILES: + dest=self.root/name;dest.parent.mkdir(parents=True,exist_ok=True) + source=DRIVER.parents[1]/name + shutil.copyfile(source,dest) + (self.root/'other.txt').write_text('unchanged') + subprocess.run(['git','init','-q'],cwd=self.root,check=True) + subprocess.run(['git','add','.'],cwd=self.root,check=True) + subprocess.run(['git','-c','user.name=Fixture','-c','user.email=fixture@example.invalid', + '-c','commit.gpgsign=false','-c','maintenance.auto=false','commit','-qm','synthetic inputs'],cwd=self.root,check=True) + spec=importlib.util.spec_from_file_location('fixture_replay',self.root/self.m.EXECUTION_FILES[0]) + module=importlib.util.module_from_spec(spec);spec.loader.exec_module(module) + module.PINS={name:module.git_blob((self.root/name).read_bytes()) for name in pins} + return module + + def test_complete_orchestration_with_real_fixture(self): + module=self.synthetic_project() + out=Path(self.tmp.name)/'success' + receipt=module.execute(self.root,out,'optimized') + self.assertEqual(receipt['status'],'PASS') + self.assertEqual(set(receipt['steps']),{'tests','coefficient'}) + self.assertEqual(receipt['tested_commit'],module.git_head(self.root)) + self.assertEqual((out/'coefficient.stdout').read_bytes(),b'synthetic\n') + self.assertIn('-O',receipt['steps']['coefficient']['record']['command']) + + def test_failed_test_stops_before_coefficient(self): + module=self.synthetic_project(bad_test=True) + out=Path(self.tmp.name)/'failed-tests' + with self.assertRaisesRegex(module.ReplayError,'unexpected_exit'): + module.execute(self.root,out,'normal') + rec=json.loads((out/'receipt.json').read_bytes()) + self.assertEqual(rec['status'],'FAIL') + self.assertEqual(set(rec['steps']),{'tests'}) + self.assertFalse((out/'coefficient.stdout').exists()) + + def test_wrong_coefficient_cannot_produce_success_receipt(self): + module=self.synthetic_project(coefficient_output='wrong') + out=Path(self.tmp.name)/'wrong-coefficient' + with self.assertRaisesRegex(module.ReplayError,'stdout_mismatch'): + module.execute(self.root,out,'normal') + rec=json.loads((out/'receipt.json').read_bytes()) + self.assertEqual(rec['status'],'FAIL') + self.assertEqual(rec['steps']['tests']['record']['status'],'PASS') + self.assertEqual(rec['steps']['coefficient']['record']['status'],'FAIL') + + def test_valid_output_followed_by_packet_edit_is_rejected(self): + module=self.synthetic_project("from pathlib import Path;Path('NOTE.md').write_text('edited')") + out=Path(self.tmp.name)/'drift' + with self.assertRaisesRegex(module.ReplayError,'source_identity'): + module.execute(self.root,out,'normal') + rec=json.loads((out/'receipt.json').read_bytes()) + self.assertEqual(rec['status'],'FAIL') + self.assertEqual(rec['steps']['coefficient']['record']['status'],'PASS') + self.assertIn('source_identity',rec['failure']) + + def test_oversized_stream_is_retained_and_rejected(self): + with self.assertRaisesRegex(self.m.ReplayError,'output_too_large'): + self.child("import sys;sys.stdout.write('x'*"+str(self.m.MAX_STREAM_BYTES+1)+")") + self.assertEqual((self.logs/'child.stdout').stat().st_size,self.m.MAX_STREAM_BYTES+1) + row=json.loads((self.logs/'child.process.json').read_bytes()) + self.assertEqual(row['status'],'FAIL') + self.assertEqual(row['failure'],'output_too_large') + + def test_workflow_triggers_and_preserves_failure_artifacts(self): + workflow=DRIVER.parents[1]/'.github/workflows/lm006-sign-coefficient.yml' + self.assertTrue(workflow.is_file(),'dedicated workflow missing') + text=workflow.read_text() + for path in [self.m.PACKET+'/**',*self.m.EXECUTION_FILES,*self.m.DEPENDENCIES]: + self.assertEqual(text.count("- '"+path+"'"),2,path) + for phrase in ('permissions:\n contents: read','fail-fast: false', + 'mode: [normal, optimized]','if: always()', + 'persist-credentials: false',"python-version: '3.13.5'", + "-p 'test_lm006_sign_coefficient_replay.py'",'if-no-files-found: error'): + self.assertIn(phrase,text) + self.assertNotIn('continue-on-error:',text) + self.assertNotIn('pull_request_target:',text) + self.assertEqual(len(re.findall(r'uses: actions/[a-z-]+@[0-9a-f]{40}',text)),3) + +if __name__=='__main__': + unittest.main() diff --git a/tools/lm006_sign_coefficient_replay.py b/tools/lm006_sign_coefficient_replay.py new file mode 100644 index 000000000..93d35b650 --- /dev/null +++ b/tools/lm006_sign_coefficient_replay.py @@ -0,0 +1,273 @@ +#!/usr/bin/env python3 +"""Replay the frozen PR362 coefficient, preserving source and process evidence. + +Standard library / POSIX only. This is an execution interlock, not mathematical +acceptance or Lean formalization. The numerical files are immutable inputs. +""" +import argparse +import hashlib +import json +import os +from pathlib import Path, PurePosixPath +import re +import signal +import subprocess +import sys +import time + +REPOSITORY = 'd6g8k5htny-coder/Math-' +SOURCE_COMMIT = '1f64b8f8ee450262e0f56e6bdf9dfb37931f48c6' +PACKET = 'coefficients/lm006_wrong_sign_20261006_r20' +EXECUTION_FILES = ('tools/lm006_sign_coefficient_replay.py', + 'tests/test_lm006_sign_coefficient_replay.py', + '.github/workflows/lm006-sign-coefficient.yml') +PINS = {'coefficients/lm006_wrong_sign_20261006_r20/NOTE.md': 'a79f080851bba69a03ec4b8348dc93d04143bf62', + 'coefficients/lm006_wrong_sign_20261006_r20/RESULTS.json': '53b5921f40f95a2623795b6cfe9ec47420705ad3', + 'coefficients/lm006_wrong_sign_20261006_r20/SOURCES.json': 'e51a4dfb507c373baafc12ec20afed609092fd34', + 'coefficients/lm006_wrong_sign_20261006_r20/enclose.py': 'e680ddb7aa0abd4b6aab2cac6a54c1ff28efc782', + 'coefficients/lm006_wrong_sign_20261006_r20/test_enclosure.py': 'a05992a4e5d9845618aee6ea53d62e08ee776940', + 'reviews/lm006_wrong_sign_asymptotic_20261006/NOTE.md': 'ea914c1b8e0b8180b645f3f626e2081ab87700b9', + 'imports/lifetime_parent_20260925/UNIFORM_MATRIX_CAP_AND_LIFETIME.md': 'dfed3b8d318a3ab1950957f393307733a4bef3f2'} +DEPENDENCIES = ('reviews/lm006_wrong_sign_asymptotic_20261006/NOTE.md', + 'imports/lifetime_parent_20260925/UNIFORM_MATRIX_CAP_AND_LIFETIME.md') +TEST_NAMES = ('test_cli', + 'test_density_parameter_intervals', + 'test_exponential_against_unrounded_alternating_sums', + 'test_exponential_bounds', + 'test_full_rectangle_sum_against_fraction_reference', + 'test_grid_refines_and_tails_positive', + 'test_image_moment_bound', + 'test_independent_inner_primitive', + 'test_invalid_inputs', + 'test_j_exact', + 'test_normalizer', + 'test_pi_and_cdf', + 'test_rational_rounding', + 'test_rectangle_extrema', + 'test_square_root_enclosure') +MAX_STREAM_BYTES = 4 * 1024 * 1024 + + +class ReplayError(ValueError): + """A failure is not a successful negative control.""" + + +def require(ok, reason): + if not ok: + raise ReplayError(reason) + + +def sha256(raw): + return hashlib.sha256(raw).hexdigest() + + +def git_blob(raw): + return hashlib.sha1(b'blob %d\0' % len(raw) + raw).hexdigest() + + +def source_file(root, relative): + require(type(relative) is str and relative and '\\' not in relative + and not relative.startswith('/') + and all(s not in ('', '.', '..') for s in relative.split('/')), 'unsafe_path') + p = Path(root) + for part in PurePosixPath(relative).parts: + p = p / part + require(not p.is_symlink(), 'source_symlink') + require(p.is_file(), 'source_missing') + return p + + +def validate_snapshot(root, pins=None): + """Alternate pins are only for explicit synthetic tests; no CLI override exists.""" + pins = PINS if pins is None else pins + result = {} + for name, expected in pins.items(): + raw = source_file(root, name).read_bytes() + actual = git_blob(raw) + require(actual == expected, 'source_identity: ' + name) + result[name] = {'bytes': len(raw), 'sha256': sha256(raw), 'git_blob': actual} + names = sorted(Path(name).name for name in pins if name.startswith(PACKET + '/')) + require(sorted(p.name for p in (Path(root) / PACKET).iterdir()) == names, 'packet_membership') + return result + + +def check_output(stdout, stderr, expected, kind): + if kind == 'tests': + # Every named test must succeed: zero tests, skips, unknown tests, noise and + # a forged summary without individual pass lines are not accepted. + prefix = '\n'.join(name + ' (test_enclosure.EnclosureTests.' + name + ') ... ok' + for name in TEST_NAMES).encode() + pattern = re.escape(prefix + b'\n\n' + b'-' * 70) + pattern += rb'\nRan 15 tests in [0-9]+(?:\.[0-9]+)?s\n\nOK\n' + require(stdout == b'' and re.fullmatch(pattern, stderr) is not None, 'test_report_mismatch') + return + require(kind == 'exact', 'unknown_contract') + require(stderr == b'', 'unexpected_stderr') + # Compare all bytes, preserving numeric strings, JSON types, the grid and + # the immutable author-run independently_reviewed=false annotation. + require(stdout == expected, 'stdout_mismatch') + + +def write_json(path, value): + with path.open('x', encoding='utf-8', newline='\n') as stream: + json.dump(value, stream, sort_keys=True, indent=2, allow_nan=False) + stream.write('\n') + + +def run_step(argv, cwd, logs, name, expected, kind, timeout=900): + require(os.name == 'posix', 'posix_required') + require(re.fullmatch(r'[a-z][a-z0-9_-]*', name) is not None, 'unsafe_step_name') + logs = Path(logs) + out, err, process = [logs / (name + suffix) for suffix in ('.stdout', '.stderr', '.process.json')] + require(not any(p.exists() or p.is_symlink() for p in (out, err, process)), 'step_evidence_exists') + start = time.monotonic_ns() + record = {'command': list(map(str, argv)), 'returncode': None, 'timed_out': False, + 'status': 'FAIL', 'timeout_seconds': timeout} + failure = None + # Files, not PIPEs, retain partial output and avoid inherited-pipe deadlocks. + # Kill the isolated POSIX process group on timeout. This is not an OS sandbox: + # a hostile process deliberately leaving the group is outside this contract. + with out.open('xb') as stdout, err.open('xb') as stderr: + try: + child = subprocess.Popen(argv, cwd=cwd, stdout=stdout, stderr=stderr, + start_new_session=True, + env={**os.environ, 'PYTHONDONTWRITEBYTECODE': '1'}) + try: + record['returncode'] = child.wait(timeout=timeout) + except subprocess.TimeoutExpired: + record['timed_out'] = True + failure = 'timeout' + try: + os.killpg(child.pid, signal.SIGKILL) + except ProcessLookupError: + pass + try: + record['returncode'] = child.wait(timeout=5) + except subprocess.TimeoutExpired: + record['detail'] = 'process-group termination did not finish in 5 seconds' + except OSError as exc: + failure = 'spawn_error' + record['detail'] = type(exc).__name__ + ': ' + str(exc) + record['elapsed_ns'] = time.monotonic_ns() - start + # A bound applies before reading logs into memory. Full on-disk streams remain + # available for diagnosis even when the output contract is exceeded. + sizes = (out.stat().st_size, err.stat().st_size) + if max(sizes) > MAX_STREAM_BYTES: + failure = failure or 'output_too_large' + record['stdout_bytes'], record['stderr_bytes'] = sizes + else: + stdout, stderr = out.read_bytes(), err.read_bytes() + record.update(stdout_bytes=len(stdout), stderr_bytes=len(stderr), + stdout_sha256=sha256(stdout), stderr_sha256=sha256(stderr)) + if failure is None: + try: + require(record['returncode'] == 0, 'unexpected_exit') + check_output(stdout, stderr, expected, kind) + except ReplayError as exc: + failure = str(exc) + if failure is None: + record['status'] = 'PASS' + else: + record['failure'] = failure + write_json(process, record) + if failure is not None: + raise ReplayError(failure) + return record + + +def git(root, *args): + p = subprocess.run(['git', *args], cwd=root, capture_output=True, timeout=30) + require(p.returncode == 0, 'git_command_failed') + return p.stdout.decode('ascii').strip() + + +def git_head(root): + head = git(root, 'rev-parse', 'HEAD') + require(re.fullmatch('[0-9a-f]{40}', head) is not None, 'invalid_git_head') + return head + + +def execution_sources(root, head): + result = {} + for name in EXECUTION_FILES: + raw = source_file(root, name).read_bytes() + committed = git(root, 'rev-parse', head + ':' + name) + require(committed == git_blob(raw), 'execution_source_drift') + result[name] = {'git_blob': committed, 'sha256': sha256(raw), 'bytes': len(raw)} + return result + + +def host_identity(head, env): + if env.get('GITHUB_ACTIONS') == 'true': + require(env.get('GITHUB_REPOSITORY') == REPOSITORY and env.get('GITHUB_SHA') == head, + 'host_identity') + for key in ('GITHUB_RUN_ID', 'GITHUB_RUN_ATTEMPT'): + require(re.fullmatch('[1-9][0-9]*', env.get(key, '')) is not None, 'host_identity') + + +def execute(root, output, mode): + root, output = Path(root).resolve(), Path(output).resolve() + require(mode in ('normal', 'optimized'), 'invalid_mode') + require(not output.is_relative_to(root), 'evidence_inside_source') + require(not output.exists(), 'evidence_exists') + output.mkdir(parents=True) + receipt = {'schema_version': 1, 'object': 'LM006-SIGN-COEFFICIENT-REPLAY', + 'repository': REPOSITORY, 'source_commit': SOURCE_COMMIT, 'mode': mode, + 'scientific_acceptance': False, 'formal_verification': False, + 'status': 'FAIL', 'steps': {}, + 'run_id': os.environ.get('GITHUB_RUN_ID'), + 'run_attempt': os.environ.get('GITHUB_RUN_ATTEMPT'), + 'event': os.environ.get('GITHUB_EVENT_NAME')} + try: + before = validate_snapshot(root) + head = git_head(root) + receipt['tested_commit'] = head + host_identity(head, os.environ) + receipt['execution_sources'] = execution_sources(root, head) + receipt['sources'] = before + require(Path(__file__).resolve() == root / EXECUTION_FILES[0], 'driver_location') + require(git(root, 'status', '--porcelain', '--untracked-files=no') == '', 'tracked_source_dirty') + packet = root / PACKET + flags = ['-B'] + (['-O'] if mode == 'optimized' else []) + ['-S'] + specs = [('tests', ['-m', 'unittest', 'test_enclosure', '-v'], None, 'tests'), + ('coefficient', ['enclose.py', '--grid', '2048'], + (packet / 'RESULTS.json').read_bytes(), 'exact')] + for name, args, expected, kind in specs: + try: + run_step([sys.executable, *flags, *args], packet, output, name, expected, kind) + finally: + path = output / (name + '.process.json') + if path.is_file(): + raw = path.read_bytes() + receipt['steps'][name] = {'process_sha256': sha256(raw), + 'record': json.loads(raw)} + require(validate_snapshot(root) == before, 'source_changed_during_execution') + require(execution_sources(root, head) == receipt['execution_sources'], 'execution_source_drift') + require(git_head(root) == head, 'head_changed_during_execution') + require(git(root, 'status', '--porcelain', '--untracked-files=no') == '', 'tracked_source_dirty') + receipt['status'] = 'PASS' + except Exception as exc: + receipt['failure'] = type(exc).__name__ + ': ' + str(exc) + raise + finally: + write_json(output / 'receipt.json', receipt) + return receipt + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--root', type=Path, default=Path('.')) + parser.add_argument('--output', type=Path, required=True) + parser.add_argument('--mode', choices=('normal', 'optimized'), required=True) + args = parser.parse_args() + try: + result = execute(args.root, args.output, args.mode) + except Exception as exc: + print('LM006_REPLAY_FAIL: ' + type(exc).__name__ + ': ' + str(exc), file=sys.stderr) + return 1 + print(json.dumps(result, sort_keys=True, indent=2, allow_nan=False)) + return 0 + + +if __name__ == '__main__': + raise SystemExit(main())