From 333f51d9d08bf04e458194a528cc019214107d61 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 31 Oct 2026 03:33:21 +0000 Subject: [PATCH 1/2] test(cbz): G1 run() launches no scan after the wrong-owner preflight DCL's review 5719770173 on #724 found the wrong-owner test's no-launch assertion vacuous: its Popen spy wrapped a direct _scan_execution_identity call, which never launches the registered G1 scan. - The direct test keeps its check that the preflight refuses with the owner message, and drops the vacuous spy. - A new test drives run() under the same wrong-owner lstat mock, for the HOME and the XDG directory. It asserts that the spy saw the other eleven gates launch, that no launch matches the registered G1 argv, that G1's row is INCOMPLETE with no exit code and empty logs, and that inspect() reads G1 back as INCOMPLETE. - The new test joins the nested optimized focused-control list. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SHCrxvZuVwzVNC7SDC9TAj Co-authored-by: Dylan Roy --- tests/test_cbz_baseline.py | 589 +++++++++++++++++++++++++++++++++++++ 1 file changed, 589 insertions(+) create mode 100644 tests/test_cbz_baseline.py diff --git a/tests/test_cbz_baseline.py b/tests/test_cbz_baseline.py new file mode 100644 index 000000000..779de93e0 --- /dev/null +++ b/tests/test_cbz_baseline.py @@ -0,0 +1,589 @@ +"""Portable baseline validator controls; local fixture inputs and processes only.""" +import hashlib +import importlib.util +import json +import os +import shlex +from pathlib import Path +import subprocess +import sys +import tempfile +import unittest +from unittest import mock + +TOOL = Path(__file__).resolve().parents[1] / 'tools/cbz_baseline.py' + + +def sha(path): + return hashlib.sha256(Path(path).read_bytes()).hexdigest() + + +def write(path, value): + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(json.dumps(value, sort_keys=True, indent=2) + '\n') + + +class BaselineTests(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.tool = None + if TOOL.exists(): + spec = importlib.util.spec_from_file_location('cbz_baseline', TOOL) + cls.tool = importlib.util.module_from_spec(spec) + spec.loader.exec_module(cls.tool) + + def setUp(self): + self.assertIsNotNone(self.tool, 'baseline validator is missing') + self.tmp = tempfile.TemporaryDirectory() + self.addCleanup(self.tmp.cleanup) + self.base = Path(self.tmp.name) + self.root = self.base / 'repo' + self.root.mkdir() + subprocess.run(['git', 'init', '-q', '-b', 'main', str(self.root)], check=True) + subprocess.run(['git', '-C', str(self.root), 'config', 'user.name', 'Fixture'], check=True) + subprocess.run(['git', '-C', str(self.root), 'config', 'user.email', 'fixture@invalid'], check=True) + (self.root / 'tools').mkdir() + self.gate = self.root / 'tools/check.py' + self.gate.write_text('print("observed fixture execution")\n') + self.audit = self.root / 'tools/cbz_audit.py' + self.audit.write_bytes(TOOL.with_name('cbz_audit.py').read_bytes()) + proof = self.root / 'proof.lean' + proof.write_text('theorem fixture : True := True.intro\n') + self.proof = {'schema': 'cbz.proof-index.v1', 'records': [ + {'id': 'fixture', 'status': 'QUARANTINED', 'source_hashes': [{'path': 'proof.lean', 'sha256': sha(proof)}], + 'dependencies': [], 'exact_boundary': 'fixture declaration', 'scientific_closure_limits': 'No kernel claim', + 'reason': 'not kernel audited', 'attempts': ['fixture inspection'], 'isolation': 'No CLOSED dependents', 'owner': 'none'}]} + write(self.root / 'cbz/proof-index.json', self.proof) + write(self.root / 'cbz/tool-registry.json', {'schema': 'cbz.tools.v1', 'tools': [ + {'path': 'tools/check.py', 'self_test': [sys.executable, 'tools/check.py']}, + {'path': 'tools/cbz_audit.py', 'self_test': [sys.executable, '-B', '-S', 'tools/cbz_audit.py', 'selftest']}]}) + statuses = {f'G{i}': 'INCOMPLETE' for i in range(12)} + self.doc = '# Baseline state\n# Scientific boundaries\n# Cold-start commands\npython tools/check.py\n# Evidence and invariants\n# Unmet criteria\n' + self.doc += '\n' + (self.root / 'STATUS.md').write_text(self.doc) + self.config = {'schema': 'cbz.baseline.v1', 'proof_index': 'cbz/proof-index.json', + 'tool_registry': 'cbz/tool-registry.json', 'docs': ['STATUS.md'], + 'allowed_axioms': ['propext', 'Classical.choice', 'Quot.sound'], 'pins': [], + 'cold_start': {'status': 'STATUS.md', 'required_answers': [ + 'Baseline state', 'Scientific boundaries', 'Cold-start commands', + 'Evidence and invariants', 'Unmet criteria'], 'commands': [['python', 'tools/check.py']]}, + 'gates': [{'id': f'G{i}', 'argv': [sys.executable, 'tools/check.py'], + 'sources': ['tools/check.py'], 'timeout_seconds': 3} for i in range(12)]} + self.config['gates'][1] = dict( + self.config['gates'][1], + argv=['python', '-B', '-S', 'tools/cbz_audit.py', 'scan', '--root', '.'], + sources=['tools/cbz_audit.py'], + timeout_seconds=120, + ) + write(self.root / self.tool.DEFAULT_CONFIG, self.config) + self.commit() + self.out = self.base / 'receipts' + + def commit(self): + subprocess.run(['git', '-C', str(self.root), 'add', '.'], check=True) + subprocess.run(['git', '-C', str(self.root), 'commit', '-qm', 'fixture'], check=True) + + def inspect(self, receipts=None): + return self.tool.inspect(self.root, receipts=receipts) + + def test_missing_receipts_are_incomplete_never_green(self): + report = self.inspect() + self.assertEqual(report['status'], 'INCOMPLETE') + self.assertEqual(report['gate_statuses'], {f'G{i}': 'INCOMPLETE' for i in range(12)}) + self.assertFalse(report['certifies_cbz0']) + + def test_live_proof_tamper_fails(self): + (self.root / 'proof.lean').write_text('axiom injected : False\n') + self.assertEqual(self.inspect()['status'], 'FAIL') + + def test_closed_cannot_depend_transitively_on_quarantine(self): + record = dict(self.proof['records'][0], id='closed', status='CLOSED', dependencies=['fixture'], + axioms=[], kernel_evidence=[]) + self.proof['records'].append(record) + write(self.root / 'cbz/proof-index.json', self.proof) + report = self.inspect() + self.assertTrue(any('quarant' in x.lower() for x in report['failures'])) + + def test_custom_axiom_cannot_be_closed(self): + record = self.proof['records'][0] + record.update(status='CLOSED', axioms=['Custom.fake'], kernel_evidence=[]) + write(self.root / 'cbz/proof-index.json', self.proof) + self.assertTrue(any('axiom' in x.lower() for x in self.inspect()['failures'])) + + def test_unknown_proof_status_refused(self): + self.proof['records'][0]['status'] = 'PASS' + write(self.root / 'cbz/proof-index.json', self.proof) + self.assertEqual(self.inspect()['status'], 'FAIL') + + def test_unlisted_mission_tool_refused(self): + (self.root / 'tools/cbz_hidden.py').write_text('print("hidden")\n') + self.assertTrue(any('unlisted' in x.lower() for x in self.inspect()['failures'])) + + def test_receipts_bind_actual_execution_and_stale_source_is_refused(self): + run = self.tool.run(self.root, self.out) + self.assertTrue((self.out / 'receipts.json').exists()) + statuses = {row['id']: row['status'] for row in run['gates']} + self.assertEqual(statuses['G1'], 'INCOMPLETE') # scanner result requires semantic receipts + self.assertEqual([statuses[f'G{i}'] for i in range(12) if i != 1], ['PASS'] * 11) + observed = self.inspect(self.out / 'receipts.json') + self.assertEqual(observed['gate_statuses']['G1'], 'INCOMPLETE') + self.assertEqual([observed['gate_statuses'][f'G{i}'] for i in range(12) if i != 1], ['PASS'] * 11) + self.assertEqual(observed['status'], 'FAIL') # docs still say INCOMPLETE + self.gate.write_text('print("changed source")\n') + stale = self.inspect(self.out / 'receipts.json') + self.assertEqual(set(stale['gate_statuses'].values()), {'INCOMPLETE'}) + self.assertTrue(any('stale' in x.lower() for x in stale['unmet'])) + + def test_g1_snapshot_is_canonical_and_binds_exit_code(self): + execution = self.tool._scan_execution_identity(self.root, self.config['gates'][1]) + compact = self.tool._scan_snapshot(b'{"a":1,"b":2}\n', 0, execution) + spaced = self.tool._scan_snapshot(b'{ "b": 2, "a": 1 }\n', 0, execution) + self.assertEqual(compact, spaced) + self.assertNotEqual(compact, self.tool._scan_snapshot(b'{"a":1,"b":2}\n', 1, execution)) + + def test_g1_legacy_or_malformed_snapshot_is_incomplete(self): + self.tool.run(self.root, self.out) + for malformed in (None, 'not-a-sha256'): + receipt = json.loads((self.out / 'receipts.json').read_text()) + row = next(item for item in receipt['gates'] if item['id'] == 'G1') + if malformed is None: + row.pop('scan_snapshot_sha256', None) + else: + row['scan_snapshot_sha256'] = malformed + write(self.out / 'receipts.json', receipt) + observed = self.inspect(self.out / 'receipts.json') + self.assertEqual(observed['gate_statuses']['G1'], 'INCOMPLETE') + self.assertTrue(any('missing or invalid scan snapshot' in item for item in observed['unmet'])) + + def test_g1_receipt_recheck_failure_is_incomplete(self): + self.tool.run(self.root, self.out) + with mock.patch.object( + self.tool, '_recompute_scan_snapshot', + side_effect=self.tool.ValidationError('recheck unavailable'), + ): + observed = self.inspect(self.out / 'receipts.json') + self.assertEqual(observed['gate_statuses']['G1'], 'INCOMPLETE') + self.assertTrue(any('current registered scan snapshot could not be reproduced' in item for item in observed['unmet'])) + + def test_g1_receipt_rejects_new_committed_tracked_axiom(self): + run = self.tool.run(self.root, self.out) + row = next(item for item in run['gates'] if item['id'] == 'G1') + self.assertEqual(row['status'], 'INCOMPLETE') # scanner output is not semantic closure + self.assertRegex(row['scan_snapshot_sha256'], r'^[0-9a-f]{64}$') + self.assertIn('tools/cbz_baseline.py', row['source_sha256']) + self.assertEqual(row['source_sha256']['tools/cbz_baseline.py'], sha(TOOL)) + self.assertEqual(row['scan_execution']['argv'][0], row['scan_execution']['executable_path']) + late = self.root / 'late-proof.lean' + late.write_text('axiom injected : False\n') + self.commit() + observed = self.inspect(self.out / 'receipts.json') + self.assertEqual(observed['gate_statuses']['G1'], 'INCOMPLETE') + self.assertTrue(any('current scan snapshot differs from receipt' in item for item in observed['unmet'])) + execution = self.tool._scan_execution_identity(self.root, self.config['gates'][1]) + fresh = subprocess.run( + execution['argv'], cwd=self.root, env=execution['environment'], + capture_output=True, + ) + self.assertEqual(fresh.returncode, 1) + payload = json.loads(fresh.stdout) + self.assertEqual(payload['result'], 'FAIL') + self.assertTrue(any(item['path'] == 'late-proof.lean' for item in payload['findings'])) + + def test_g1_receipt_rejects_new_untracked_proof_candidate(self): + self.tool.run(self.root, self.out) + late = self.root / 'proofs/new-candidate.lean' + late.parent.mkdir(parents=True) + late.write_text('theorem candidate : True := True.intro\n') + observed = self.inspect(self.out / 'receipts.json') + self.assertEqual(observed['gate_statuses']['G1'], 'INCOMPLETE') + self.assertTrue(any('current scan snapshot differs from receipt' in item for item in observed['unmet'])) + execution = self.tool._scan_execution_identity(self.root, self.config['gates'][1]) + fresh = subprocess.run( + execution['argv'], cwd=self.root, env=execution['environment'], + capture_output=True, + ) + self.assertEqual(fresh.returncode, 2) + payload = json.loads(fresh.stdout) + self.assertIn('proofs/new-candidate.lean', payload['untracked']) + self.assertTrue(any( + item['code'] == 'file.untracked_proof' and item['path'] == 'proofs/new-candidate.lean' + for item in payload['coverage_gaps'] + )) + + def test_changed_command_invalidates_receipt(self): + self.tool.run(self.root, self.out) + self.config['gates'][0]['argv'].append('new-argument') + write(self.root / self.tool.DEFAULT_CONFIG, self.config) + self.assertEqual(set(self.inspect(self.out / 'receipts.json')['gate_statuses'].values()), {'INCOMPLETE'}) + + def test_output_inside_source_root_refused(self): + with self.assertRaises(self.tool.ValidationError): + self.tool.run(self.root, self.root / 'out') + + def test_timeout_is_recorded_incomplete(self): + self.gate.write_text('import time\ntime.sleep(10)\n') + self.config['gates'] = [dict(self.config['gates'][0], timeout_seconds=1)] + write(self.root / self.tool.DEFAULT_CONFIG, self.config) + result = self.tool.run(self.root, self.out) + self.assertEqual(result['gates'][0]['status'], 'INCOMPLETE') + self.assertTrue(result['gates'][0]['timed_out']) + + def test_offline_pin_object_and_manifest_checked(self): + head = subprocess.check_output(['git', '-C', str(self.root), 'rev-parse', 'HEAD'], text=True).strip() + write(self.root / 'lake-manifest.json', {'packages': [{'name': 'Math', 'rev': head}]}) + self.config['pins'] = [{'repo': '.', 'commit': head, 'object_path': 'proof.lean', + 'sha256': sha(self.root / 'proof.lean'), 'pin_file': 'lake-manifest.json', 'package': 'Math'}] + write(self.root / self.tool.DEFAULT_CONFIG, self.config) + report = self.inspect() + self.assertEqual(report['pins'][0]['status'], 'PASS') + write(self.root / 'lake-manifest.json', {'packages': [{'name': 'Math', 'rev': 'f' * 40}]}) + self.assertEqual(self.inspect()['pins'][0]['status'], 'FAIL') + + def test_missing_offline_dependency_is_incomplete(self): + self.config['pins'] = [{'repo': '../missing', 'commit': 'f' * 40, 'object_path': 'x', 'sha256': '0' * 64}] + write(self.root / self.tool.DEFAULT_CONFIG, self.config) + self.assertEqual(self.inspect()['pins'][0]['status'], 'INCOMPLETE') + + def test_cold_start_checks_executable_entrypoints_without_theorem_claim(self): + result = self.tool.cold_start(self.root) + self.assertEqual(result['status'], 'PASS') + self.assertFalse(result['certifies_theorems']) + (self.root / 'STATUS.md').write_text('PASS\n') + self.assertEqual(self.tool.cold_start(self.root)['status'], 'FAIL') + + def test_exit_two_is_incomplete_rather_than_an_observed_failure(self): + self.gate.write_text('import sys\nsys.exit(2)\n') + self.config['gates'] = [self.config['gates'][0]] + write(self.root / self.tool.DEFAULT_CONFIG, self.config) + self.assertEqual(self.tool.run(self.root, self.out)['gates'][0]['status'], 'INCOMPLETE') + + def test_receipt_log_tampering_is_refused(self): + self.tool.run(self.root, self.out) + (self.out / 'G0.stdout').write_text('forged PASS') + self.assertEqual(self.inspect(self.out / 'receipts.json')['gate_statuses']['G0'], 'INCOMPLETE') + + def test_declared_allowed_axioms_cannot_admit_custom_axioms(self): + self.config['allowed_axioms'].append('Custom.fake') + write(self.root / self.tool.DEFAULT_CONFIG, self.config) + with self.assertRaises(self.tool.ValidationError): + self.inspect() + + def test_duplicate_json_key_is_refused(self): + (self.root / self.tool.DEFAULT_CONFIG).write_text('{"schema":"cbz.baseline.v1","schema":"forged"}') + with self.assertRaises(self.tool.ValidationError): + self.inspect() + + + def test_malformed_receipt_shapes_are_incomplete(self): + fake = self.base / 'malformed.json' + payloads = ( + [], + None, + {'schema': 'cbz.receipts.v1', + 'config_sha256': sha(self.root / self.tool.DEFAULT_CONFIG), + 'gates': [None]}, + ) + for payload in payloads: + write(fake, payload) + observed = self.inspect(fake) + self.assertEqual(set(observed['gate_statuses'].values()), {'INCOMPLETE'}) + + def test_g1_receipt_recheck_brackets_source_binding(self): + run = self.tool.run(self.root, self.out) + row = next(item for item in run['gates'] if item['id'] == 'G1') + def mutate_during_recheck(root, gate, expected_execution): + (root / 'tools/cbz_audit.py').write_text('print("changed during recheck")\n') + return row['scan_snapshot_sha256'] + with mock.patch.object(self.tool, '_recompute_scan_snapshot', side_effect=mutate_during_recheck): + observed = self.inspect(self.out / 'receipts.json') + self.assertEqual(observed['gate_statuses']['G1'], 'INCOMPLETE') + self.assertTrue(any( + 'source or execution identity changed during receipt recheck' in item + for item in observed['unmet'] + )) + + def test_g1_execution_identity_binds_resolved_executable(self): + # Exercise the registered PATH-resolved command, not only an absolute fixture interpreter. + self.config['gates'][1]['argv'][0] = 'python' + write(self.root / self.tool.DEFAULT_CONFIG, self.config) + run = self.tool.run(self.root, self.out) + row = next(item for item in run['gates'] if item['id'] == 'G1') + identity = row['scan_execution'] + self.assertEqual(identity['argv'][0], identity['executable_path']) + self.assertRegex(identity['executable_sha256'], r'^[0-9a-f]{64}$') + shadow_dir = self.base / 'shadow-bin' + shadow_dir.mkdir() + shadow = shadow_dir / 'python' + shadow.write_text('#!/bin/sh\nexit 0\n') + shadow.chmod(0o755) + original_path = os.environ.get('PATH', os.defpath) + with mock.patch.dict(os.environ, {'PATH': str(shadow_dir) + os.pathsep + original_path}): + observed = self.inspect(self.out / 'receipts.json') + self.assertEqual(observed['gate_statuses']['G1'], 'INCOMPLETE') + self.assertTrue(any('execution identity is missing or changed' in item for item in observed['unmet'])) + + def test_g1_execution_identity_binds_git_executable(self): + real_git = self.tool.shutil.which('git') + self.assertIsNotNone(real_git) + shadow_dir = self.base / 'shadow-git' + shadow_dir.mkdir() + shadow_git = shadow_dir / 'git' + + def write_wrapper(marker): + shadow_git.write_text( + '#!/bin/sh\n# ' + marker + '\nexec ' + shlex.quote(real_git) + ' "$@"\n' + ) + shadow_git.chmod(0o755) + + original_defpath = os.defpath + shadow_path = str(shadow_dir) + os.pathsep + original_defpath + write_wrapper('first identity') + with mock.patch.object(os, 'defpath', shadow_path): + first = self.tool._scan_execution_identity(self.root, self.config['gates'][1]) + write_wrapper('second identity') + second = self.tool._scan_execution_identity(self.root, self.config['gates'][1]) + self.assertEqual(first['git']['executable_path'], str(shadow_git.resolve())) + self.assertRegex(first['git']['executable_sha256'], r'^[0-9a-f]{64}$') + self.assertNotEqual(first['git']['executable_sha256'], second['git']['executable_sha256']) + + def test_g1_execution_identity_binds_git_configuration(self): + gate = self.config['gates'][1] + first = self.tool._scan_execution_identity(self.root, gate) + info_exclude = self.root / '.git/info/exclude' + info_exclude.parent.mkdir(parents=True, exist_ok=True) + info_exclude.write_text('# first exclude state\n') + second = self.tool._scan_execution_identity(self.root, gate) + self.assertNotEqual( + first['git']['info_exclude_sha256'], + second['git']['info_exclude_sha256'], + ) + + core_excludes = self.base / 'core-excludes' + core_excludes.write_text('*.candidate\n') + subprocess.run( + ['git', '-C', str(self.root), 'config', 'core.excludesfile', str(core_excludes)], + check=True, + ) + third = self.tool._scan_execution_identity(self.root, gate) + self.assertNotEqual(second['git']['config_sha256'], third['git']['config_sha256']) + self.assertEqual(len(third['git']['core_excludesfiles']), 1) + original_excludes_hash = third['git']['core_excludesfiles'][0]['sha256'] + core_excludes.write_text('*.candidate\n*.archive\n') + fourth = self.tool._scan_execution_identity(self.root, gate) + self.assertNotEqual(original_excludes_hash, fourth['git']['core_excludesfiles'][0]['sha256']) + + def test_g1_execution_identity_rejects_default_xdg_home_ignore_files(self): + gate = self.config['gates'][1] + identity = self.tool._scan_execution_identity(self.root, gate) + home = Path(identity['environment']['HOME']) + xdg = Path(identity['environment']['XDG_CONFIG_HOME']) + self.assertIsNone(identity['git']['default_excludes']['xdg_ignore_sha256']) + self.assertIsNone(identity['git']['default_excludes']['home_fallback_ignore_sha256']) + + candidate = self.root / 'untracked-proof-candidate.lean' + candidate.write_text('theorem hidden : True := True.intro\n') + git = identity['git']['executable_path'] + + def untracked(): + result = subprocess.run( + [git, '-C', str(self.root), 'ls-files', '--others', '--exclude-standard'], + cwd=self.root, capture_output=True, env=identity['environment'], check=True, + ) + return result.stdout.decode().splitlines() + + self.assertIn('untracked-proof-candidate.lean', untracked()) + + # XDG_CONFIG_HOME takes precedence over the HOME fallback, but both + # implicit paths must remain absent in the captured G1 identity. + home_ignore = home / '.config' / 'git' / 'ignore' + home_ignore.parent.mkdir(parents=True) + home_ignore.write_text('untracked-proof-candidate.lean\n') + self.assertIn('untracked-proof-candidate.lean', untracked()) + with self.assertRaises(self.tool.ValidationError): + self.tool._scan_execution_identity(self.root, gate) + home_ignore.unlink() + + # The effective XDG default ignore file can hide the same candidate; + # the identity preflight must reject it before a G1 scan is launched. + xdg_ignore = xdg / 'git' / 'ignore' + xdg_ignore.parent.mkdir(parents=True) + xdg_ignore.write_text('untracked-proof-candidate.lean\n') + self.assertNotIn('untracked-proof-candidate.lean', untracked()) + with self.assertRaises(self.tool.ValidationError): + self.tool._scan_execution_identity(self.root, gate) + + def test_g1_execution_identity_rejects_preexisting_permissive_directories(self): + gate = self.config['gates'][1] + identity = self.tool._scan_execution_identity(self.root, gate) + default = identity['git']['default_excludes'] + effective_uid = os.geteuid() + self.assertEqual(default['home_directory_uid'], effective_uid) + self.assertEqual(default['xdg_directory_uid'], effective_uid) + self.assertEqual(default['home_directory_mode'], 0o700) + self.assertEqual(default['xdg_directory_mode'], 0o700) + + git_dir = self.root / '.git' + directories = (git_dir / 'cbz-g1-home', git_dir / 'cbz-g1-xdg') + for directory in directories: + if directory.exists(): + directory.rmdir() + for directory in directories: + with self.subTest(directory=directory.name): + directory.mkdir(mode=0o700) + directory.chmod(0o777) + try: + with self.assertRaisesRegex(self.tool.ValidationError, 'mode must be 0700'): + self.tool._scan_execution_identity(self.root, gate) + finally: + for cleanup in directories: + if cleanup.exists(): + cleanup.chmod(0o700) + cleanup.rmdir() + + def test_g1_execution_identity_rejects_wrong_owner_directories(self): + gate = self.config['gates'][1] + git_dir = self.root / '.git' + directories = (git_dir / 'cbz-g1-home', git_dir / 'cbz-g1-xdg') + path_type = type(self.root) + original_lstat = path_type.lstat + effective_uid = os.geteuid() + + for directory in directories: + with self.subTest(directory=directory.name): + for cleanup in directories: + if cleanup.exists(): + cleanup.chmod(0o700) + cleanup.rmdir() + + def wrong_owner(path): + metadata = original_lstat(path) + if path == directory: + fields = list(metadata) + fields[4] = effective_uid + 1 + return os.stat_result(fields) + return metadata + + # The preflight itself never launches the G1 scan; the launch + # boundary is exercised through run() in the next test. + with mock.patch.object(path_type, 'lstat', new=wrong_owner): + with self.assertRaisesRegex( + self.tool.ValidationError, + 'owner is not the effective user', + ): + self.tool._scan_execution_identity(self.root, gate) + + def test_g1_run_does_not_launch_after_wrong_owner_preflight(self): + gate = self.config['gates'][1] + git_dir = self.root.resolve() / '.git' + directories = (git_dir / 'cbz-g1-home', git_dir / 'cbz-g1-xdg') + path_type = type(self.root) + original_lstat = path_type.lstat + effective_uid = os.geteuid() + check_argv = [sys.executable, 'tools/check.py'] + + for directory in directories: + with self.subTest(directory=directory.name): + for cleanup in directories: + if cleanup.exists(): + cleanup.chmod(0o700) + cleanup.rmdir() + + def wrong_owner(path): + metadata = original_lstat(path) + if path == directory: + fields = list(metadata) + fields[4] = effective_uid + 1 + return os.stat_result(fields) + return metadata + + output = self.base / ('receipts-' + directory.name) + with mock.patch.object(path_type, 'lstat', new=wrong_owner): + with mock.patch.object(subprocess, 'Popen', wraps=subprocess.Popen) as popen: + result = self.tool.run(self.root, output) + + launched = [list(call.args[0] if call.args else call.kwargs['args']) + for call in popen.call_args_list] + # The spy sees run()'s own launches: the other eleven gates ran. + self.assertEqual(launched.count(check_argv), 11) + # No launch matches the registered G1 command, by its + # registered or its resolved executable. + self.assertFalse(any(argv[1:] == gate['argv'][1:] for argv in launched)) + rows = {row['id']: row for row in result['gates']} + self.assertEqual(rows['G1']['status'], 'INCOMPLETE') + self.assertEqual(rows['G1']['error'], 'command/input unavailable; no passing execution claimed') + self.assertIsNone(rows['G1']['exit_code']) + self.assertNotIn('scan_execution', rows['G1']) + self.assertEqual((output / 'G1.stdout').read_bytes(), b'') + self.assertEqual((output / 'G1.stderr').read_bytes(), b'') + self.assertEqual([rows[f'G{i}']['status'] for i in range(12) if i != 1], ['PASS'] * 11) + self.assertEqual(self.inspect(output / 'receipts.json')['gate_statuses']['G1'], 'INCOMPLETE') + + def test_g1_snapshot_requires_private_directory_identity_fields(self): + identity = self.tool._scan_execution_identity(self.root, self.config['gates'][1]) + effective_uid = os.geteuid() + fields = ( + 'home_directory_uid', + 'home_directory_mode', + 'xdg_directory_uid', + 'xdg_directory_mode', + ) + for field in fields: + altered = json.loads(json.dumps(identity)) + del altered['git']['default_excludes'][field] + with self.subTest(field=field): + with self.assertRaises(self.tool.ValidationError): + self.tool._scan_snapshot(b'{}', 0, altered) + invalid = ( + ('home_directory_uid', True), + ('home_directory_uid', effective_uid + 1), + ('home_directory_mode', 0o777), + ('xdg_directory_uid', True), + ('xdg_directory_uid', effective_uid + 1), + ('xdg_directory_mode', 0o777), + ) + for field, value in invalid: + altered = json.loads(json.dumps(identity)) + altered['git']['default_excludes'][field] = value + with self.subTest(field=field, value=value): + with self.assertRaises(self.tool.ValidationError): + self.tool._scan_snapshot(b'{}', 0, altered) + + def test_g1_focused_controls_under_optimized_python(self): + names = ( + 'test_g1_snapshot_is_canonical_and_binds_exit_code', + 'test_g1_legacy_or_malformed_snapshot_is_incomplete', + 'test_g1_receipt_recheck_failure_is_incomplete', + 'test_g1_receipt_recheck_brackets_source_binding', + 'test_g1_receipt_rejects_new_committed_tracked_axiom', + 'test_g1_receipt_rejects_new_untracked_proof_candidate', + 'test_g1_execution_identity_binds_resolved_executable', + 'test_g1_execution_identity_binds_git_executable', + 'test_g1_execution_identity_binds_git_configuration', + 'test_g1_execution_identity_rejects_default_xdg_home_ignore_files', + 'test_g1_execution_identity_rejects_preexisting_permissive_directories', + 'test_g1_execution_identity_rejects_wrong_owner_directories', + 'test_g1_run_does_not_launch_after_wrong_owner_preflight', + 'test_g1_snapshot_requires_private_directory_identity_fields', + 'test_malformed_receipt_shapes_are_incomplete', + ) + command = [ + sys.executable, '-O', str(Path(__file__).resolve()), + *[f'BaselineTests.{name}' for name in names], + ] + completed = subprocess.run( + command, cwd=Path(__file__).resolve().parents[1], + capture_output=True, text=True, timeout=300, + ) + self.assertEqual(completed.returncode, 0, msg=(completed.stdout + completed.stderr)[-12000:]) + + def test_forged_pass_without_process_logs_is_refused(self): + fake = self.base / 'forged.json' + write(fake, {'schema': 'cbz.receipts.v1', 'config_sha256': sha(self.root / self.tool.DEFAULT_CONFIG), + 'gates': [{'id': f'G{i}', 'status': 'PASS'} for i in range(12)]}) + report = self.inspect(fake) + self.assertEqual(set(report['gate_statuses'].values()), {'INCOMPLETE'}) + self.assertNotEqual(report['status'], 'PASS') + + +if __name__ == '__main__': + unittest.main() From f80c30380087f3b2f33ddf84349332ebb880056e Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 31 Oct 2026 23:07:10 +0000 Subject: [PATCH 2/2] fix(cbz): add baseline+audit tools so tip-based 8aa6a582 tests can run Source SHA 41743c1d454f8ef3fd515bd9aaa0487540537b0d. tools/cbz_baseline.py and tools/cbz_audit.py are the minimal dependencies for the orphan test module on tip: the G1 wrong-owner run() test targets cbz_baseline, and tests/test_cbz_baseline.py setUp copies cbz_audit.py into the fixture. Co-authored-by: Dylan Roy --- tools/cbz_audit.py | 400 ++++++++++++++++++++++ tools/cbz_baseline.py | 761 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 1161 insertions(+) create mode 100644 tools/cbz_audit.py create mode 100644 tools/cbz_baseline.py diff --git a/tools/cbz_audit.py b/tools/cbz_audit.py new file mode 100644 index 000000000..43e08460f --- /dev/null +++ b/tools/cbz_audit.py @@ -0,0 +1,400 @@ +#!/usr/bin/env python3 +"""CBZ-0 finite static auditor (Python standard library only). + +Usage: + python -B tools/cbz_audit.py selftest + python -B tools/cbz_audit.py scan --root . [--registry FILE] [--quarantine FILE] + +Both commands print JSON. Scan exits 1 on findings, 2 on incomplete coverage or +missing semantic work. It NEVER certifies overall Layer A or mathematical +soundness. static_result=PASS means only the declared finite static checks pass. + +Registry schema: {"version":1,"files":[{"path":"bounds.py","sha256":"...", +"role":"certification"}]}. Roles: certification, noncertifying, proof-source, +other. Exact bytes must match; registry completeness is a separate semantic duty. +Certification detection currently supports Python float AST nodes/calls and Lean +Float tokens only. Aliasing, FFI, dependencies and dynamic code need semantic work. + +Quarantine schema: {"version":1,"entries":[{"path":"Q.lean","sha256":"...", +"statement":"...","reason":"...","attempts":["..."],"owner":"none"}]}. +A valid entry annotates findings; it NEVER suppresses them or proves isolation. + +Inventory is the entire Git tracked working tree, including missing files, stage +conflicts, symlinks and submodules. Archives are explicitly unsupported coverage +gaps; their contents are not claimed scanned. Untracked proof/archive candidates +are reported as gaps. No archive extraction or third-party execution occurs. +""" +from __future__ import annotations +import argparse +import ast +import hashlib +import json +import os +from pathlib import Path, PurePosixPath +import re +import stat +import subprocess +import sys +import tempfile +import unicodedata + +VERSION = 'cbz-static-1' +PROOF_SUFFIXES = {'.lean', '.olean', '.ilean', '.v', '.agda', '.thy', '.tex', '.typ', '.ipynb', '.smt2', '.pvs', '.dfy', '.fst', '.fsti', '.idr', '.lagda'} +ARCHIVE_SUFFIXES = ('.zip', '.tar', '.tgz', '.tar.gz', '.tar.bz2', '.tar.xz', '.gz', '.bz2', '.xz', '.7z', '.rar', '.zst') +LEAN_FORBIDDEN = {'sorry', 'admit', 'axiom', 'native_decide'} +LEAN_TRUST = {'sorryAx', 'unsafe', 'implemented_by', 'extern', 'ofReduceBool', 'ofReduceNat', 'trustCompiler', 'skipKernelTC'} +REQUIRED_SEMANTICS = ['kernel_and_declared_axioms', 'status_index_and_nonvacuity', 'statement_alignment_and_unweakened_bounds', 'test_preservation_and_regression', 'certification_scope', 'dependency_and_dynamic_syntax_review'] + + +def digest(raw): + return hashlib.sha256(raw).hexdigest() + + +def is_archive(path, raw=b''): + return (path.lower().endswith(ARCHIVE_SUFFIXES) or raw.startswith((b'PK\x03\x04', b'PK\x05\x06', b'\x1f\x8b', b'BZh', b'\xfd7zXZ\x00', b'7z\xbc\xaf\x27\x1c', b'Rar!', b'\x28\xb5\x2f\xfd')) or raw[257:262] == b'ustar') + + +def proof_candidate(path): + return PurePosixPath(path).suffix.lower() in PROOF_SUFFIXES + + +def ident_char(c): + return c in "_'" or c.isalnum() or unicodedata.category(c).startswith(('L', 'M')) + + +def lean_tokens(source): + """Return (kind, value, line) tokens and conservative lexical gaps. + + Nested comments, ordinary/raw strings and quoted identifiers are recognized. + Interpolated strings are gaps because embedded Lean expressions need parsing. + This is not a Lean parser or a semantic dependency/axiom checker. + """ + tokens, gaps = [], [] + i, line, size = 0, 1, len(source) + while i < size: + if source.startswith('--', i): + end = source.find('\n', i) + i = size if end < 0 else end + elif source.startswith('/-', i): + start, depth = line, 1 + i += 2 + while i < size and depth: + if source.startswith('/-', i): + depth += 1 + i += 2 + elif source.startswith('-/', i): + depth -= 1 + i += 2 + else: + line += source[i] == '\n' + i += 1 + if depth: + gaps.append((start, 'unterminated nested comment')) + elif source[i] == '«': + start = line + end = source.find('»', i + 1) + if end < 0: + gaps.append((start, 'unterminated quoted identifier')) + break + tokens.append(('quoted', source[i + 1:end], line)) + line += source[i:end + 1].count('\n') + i = end + 1 + elif source.startswith('s!"', i): + gaps.append((line, 'interpolated string requires Lean parser')) + i += 2 + elif source[i] == 'r' and (match := re.match(r'r(#+)?"', source[i:])): + start = line + marker = '"' + (match.group(1) or '') + end = source.find(marker, i + len(match.group())) + if end < 0: + gaps.append((start, 'unterminated raw string')) + break + line += source[i:end + len(marker)].count('\n') + i = end + len(marker) + elif source[i] == '"': + start = line + i += 1 + closed = False + while i < size: + if source[i] == '\\': + line += i + 1 < size and source[i + 1] == '\n' + i += 2 + elif source[i] == '"': + i += 1 + closed = True + break + else: + line += source[i] == '\n' + i += 1 + if not closed: + gaps.append((start, 'unterminated string')) + elif source[i] == "'" and (match := re.match(r"'(?:[^'\\\n]|\\(?:u[0-9a-fA-F]{4}|x[0-9a-fA-F]{2}|.))'", source[i:])): + i += len(match.group()) + elif ident_char(source[i]): + start = i + while i < size and ident_char(source[i]): + i += 1 + tokens.append(('identifier', source[start:i], line)) + else: + line += source[i] == '\n' + i += 1 + return tokens, gaps + + +def lean_findings(tokens): + return [('lean.' + value, line, 'lexical trust/placeholder token requires review') + for kind, value, line in tokens + if (kind == 'identifier' and value in LEAN_FORBIDDEN) or value in LEAN_TRUST] + + +def certification_findings(path, source, tokens): + suffix = PurePosixPath(path).suffix.lower() + if suffix == '.lean': + return ([('certification.float', line, 'Float token in explicitly classified certifier') + for _, value, line in tokens if value in {'Float', 'Float32', 'Float64'}], []) + if suffix != '.py': + return [], [('certification.unsupported', 0, 'certifier language is outside finite detector scope')] + try: + tree = ast.parse(source, filename=path) + except (SyntaxError, ValueError) as error: + return [], [('certification.parse', 0, str(error))] + findings = [] + for node in ast.walk(tree): + literal = isinstance(node, ast.Constant) and type(node.value) in {float, complex} + call = isinstance(node, ast.Call) and ((isinstance(node.func, ast.Name) and node.func.id == 'float') or (isinstance(node.func, ast.Attribute) and node.func.attr in {'float', 'float16', 'float32', 'float64'})) + if literal or call: + findings.append(('certification.float', node.lineno, 'floating point in explicitly classified certifier')) + return findings, [] + + +def validate_manifest(value, kind, files): + if value is None: + return {} + key = 'files' if kind == 'registry' else 'entries' + if type(value) is not dict or set(value) != {'version', key} or type(value['version']) is not int or value['version'] != 1 or type(value[key]) is not list: + raise ValueError('invalid manifest envelope') + entries = {} + for row in value[key]: + fields = {'path', 'sha256', 'role'} if kind == 'registry' else {'path', 'sha256', 'statement', 'reason', 'attempts', 'owner'} + if type(row) is not dict or set(row) != fields: + raise ValueError('invalid manifest row fields') + path = row['path'] + if type(path) is not str or not path or PurePosixPath(path).is_absolute() or '..' in PurePosixPath(path).parts or path in entries or path not in files: + raise ValueError('unknown, unsafe or duplicate manifest path') + if type(row['sha256']) is not str or not re.fullmatch('[0-9a-f]{64}', row['sha256']) or row['sha256'] != files[path].get('sha256'): + raise ValueError('manifest bytes do not match tracked working file') + if kind == 'registry': + if row['role'] not in {'certification', 'noncertifying', 'proof-source', 'other'}: + raise ValueError('invalid classification role') + elif row['owner'] != 'none' or any(type(row[k]) is not str or not row[k].strip() for k in ('statement', 'reason')) or type(row['attempts']) is not list or not row['attempts'] or any(type(a) is not str or not a.strip() for a in row['attempts']): + raise ValueError('quarantine requires statement, reason, attempts and owner:none') + entries[path] = row + return entries + + +def scan(root, registry=None, quarantine=None): + root = Path(root).resolve() + result = {'schema_version': 1, 'tool': VERSION, 'scanner_sha256': digest(Path(__file__).read_bytes()), 'root': str(root), 'scope': 'Git tracked working-tree bytes, not commit or kernel semantics', 'files': [], 'tracked_count': 0, 'read_count': 0, 'proof_candidates': [], 'untracked': [], 'findings': [], 'coverage_gaps': [], 'quarantine': [], 'semantic_receipts_required': list(REQUIRED_SEMANTICS), 'mathematical_soundness_certified': False} + def gap(code, path, detail, line=0): + result['coverage_gaps'].append({'code': code, 'path': path, 'line': line, 'detail': detail}) + raw_files, by_path = {}, {} + try: + top = subprocess.run(['git', '-C', str(root), 'rev-parse', '--show-toplevel'], check=True, capture_output=True) + root = Path(os.fsdecode(top.stdout.removesuffix(b'\n'))).resolve() + result['root'] = str(root) + proc = subprocess.run(['git', '-C', str(root), 'ls-files', '--stage', '-z'], check=True, capture_output=True) + records = proc.stdout.split(b'\0') + for record in records: + if not record: + continue + metadata, raw_path = record.split(b'\t', 1) + mode, oid, stage = metadata.decode('ascii').split() + path = raw_path.decode('utf-8', errors='surrogateescape') + if path in by_path: + gap('git.conflict', path, 'multiple index stages') + continue + entry = {'path': path, 'index_mode': mode, 'index_object': oid, 'index_stage': stage, 'read': False} + by_path[path] = entry + result['files'].append(entry) + if stage != '0': + gap('git.conflict', path, 'unmerged index entry') + if mode == '160000': + gap('file.submodule', path, 'submodule requires independent complete scan') + continue + if mode == '120000' or any((root / parent).is_symlink() for parent in (PurePosixPath(path), *PurePosixPath(path).parents) if str(parent) != '.'): + gap('file.symlink', path, 'symlink not followed') + continue + try: + if not stat.S_ISREG((root / path).stat().st_mode): + gap('file.non_regular', path, 'tracked file replaced by non-regular file') + continue + raw = (root / path).read_bytes() + except OSError as error: + gap('file.unreadable', path, type(error).__name__) + continue + entry.update(read=True, sha256=digest(raw), bytes=len(raw)) + raw_files[path] = raw + proc = subprocess.run(['git', '-C', str(root), 'ls-files', '--others', '--exclude-standard', '-z'], check=True, capture_output=True) + result['untracked'] = [p.decode('utf-8', errors='surrogateescape') for p in proc.stdout.split(b'\0') if p] + for path in result['untracked']: + if proof_candidate(path) or is_archive(path): + gap('file.untracked_proof', path, 'untracked proof/archive candidate is outside tracked scan') + except (OSError, ValueError, subprocess.CalledProcessError) as error: + gap('git.enumeration', '', type(error).__name__) + result['tracked_count'] = len(by_path) + result['read_count'] = len(raw_files) + manifests = {} + for kind, value in [('registry', registry), ('quarantine', quarantine)]: + try: + manifests[kind] = validate_manifest(value, kind, by_path) + except (ValueError, TypeError) as error: + gap(kind + '.invalid', '', str(error)) + manifests[kind] = {} + result['quarantine'] = list(manifests['quarantine'].values()) + if quarantine is not None: + result['semantic_receipts_required'].append('quarantine_isolation_and_closed_dependencies') + candidates = {p for p in by_path if proof_candidate(p)} + candidates.update(p for p, row in manifests['registry'].items() if row['role'] == 'proof-source') + result['proof_candidates'] = sorted(candidates) + for path, raw in raw_files.items(): + suffix = PurePosixPath(path).suffix.lower() + classified = manifests['registry'].get(path, {}).get('role') + by_path[path]['classification'] = classified or 'unclassified' + if raw.startswith(b'version https://git-lfs.github.com/spec/v1\n'): + gap('file.lfs_pointer', path, 'LFS pointer bytes do not include payload') + continue + if is_archive(path, raw): + gap('archive.unsupported', path, 'archive contents not scanned; independent extraction inventory required') + continue + if path in candidates and suffix != '.lean': + gap('proof.unsupported', path, 'proof/document language is outside Lean lexical scope') + if suffix != '.lean' and classified != 'certification': + continue + try: + source = raw.decode('utf-8') + except UnicodeDecodeError: + gap('file.encoding', path, 'source is not strict UTF-8') + continue + tokens = [] + findings = [] + if suffix == '.lean': + tokens, lex_gaps = lean_tokens(source) + for line, detail in lex_gaps: + gap('lean.lexical_gap', path, detail, line) + findings.extend(lean_findings(tokens)) + if classified == 'certification': + cert_findings, cert_gaps = certification_findings(path, source, tokens) + findings.extend(cert_findings) + for code, line, detail in cert_gaps: + gap(code, path, detail, line) + for code, line, detail in findings: + result['findings'].append({'code': code, 'path': path, 'line': line, 'detail': detail, 'sha256': by_path[path]['sha256'], 'quarantine_requested': path in manifests['quarantine']}) + for item in result['coverage_gaps']: + entry = by_path.get(item['path'], {}) + item.update(sha256=entry.get('sha256'), index_object=entry.get('index_object'), kind=item['code'].split('.', 1)[0]) + result['inventory_sha256'] = digest(json.dumps(result['files'], sort_keys=True, ensure_ascii=True, separators=(',', ':')).encode()) + result['static_result'] = 'FAIL' if result['findings'] else ('INCOMPLETE' if result['coverage_gaps'] else 'PASS') + result['result'] = 'FAIL' if result['findings'] else 'INCOMPLETE' + return result + + +# Each mutation changes the actual scanner source. A killed mutant must complete +# the whole test process with assertion failures, not syntax/import errors. +MUTATIONS = [ + ('disable_placeholder_detector', "LEAN_FORBIDDEN = {'sorry', 'admit', 'axiom', 'native_decide'}", 'LEAN_FORBIDDEN = set()'), + ('disable_trust_detector', "LEAN_TRUST = {'sorryAx', 'unsafe', 'implemented_by', 'extern', 'ofReduceBool', 'ofReduceNat', 'trustCompiler', 'skipKernelTC'}", 'LEAN_TRUST = set()'), + ('ignore_hidden_files', "if not record:\n continue", "if not record or b'\\t.' in record:\n continue"), + ('suppress_coverage_failures', "result['coverage_gaps'].append({'code': code, 'path': path, 'line': line, 'detail': detail})", 'pass'), + ('suppress_active_findings', "for code, line, detail in findings:", 'for code, line, detail in []:'), + ('disable_float_detector', 'if literal or call:', 'if False:'), + ('ignore_quarantine_hash', "or row['sha256'] != files[path].get('sha256')", ''), + ('accept_empty_semantic_closure', "result['result'] = 'FAIL' if result['findings'] else 'INCOMPLETE'", "result['result'] = 'FAIL' if result['findings'] else 'PASS'"), + ('disable_archive_detection', 'if is_archive(path, raw):', 'if False:'), + ('ignore_untracked_proofs', 'if proof_candidate(path) or is_archive(path):', 'if False:'), + ('misclassify_all_python_as_certification', "classified = manifests['registry'].get(path, {}).get('role')", "classified = 'certification' if suffix == '.py' else manifests['registry'].get(path, {}).get('role')"), + ('hide_quarantined_findings', 'for code, line, detail in findings:', "for code, line, detail in ([] if path in manifests['quarantine'] else findings):"), +] + + +def selftest(tests): + source_path = Path(__file__).resolve() + source = source_path.read_text() + helper = source_path.with_name('cbz_mutation.py') + helper_before = digest(helper.read_bytes()) + tests_before = digest(tests.read_bytes()) + optimization = ['-OO'] if sys.flags.optimize == 2 else ['-O'] if sys.flags.optimize == 1 else [] + def run(path, expected=None): + env = dict(os.environ, CBZ_AUDIT_UNDER_TEST=str(path), PYTHONDONTWRITEBYTECODE='1') + command = [sys.executable, '-B', *optimization, str(helper), '--tests', str(tests)] + with tempfile.TemporaryDirectory(prefix='cbz-audit-population-') as temp: + if expected is not None: + population = Path(temp) / 'expected.json' + population.write_text(json.dumps(expected)) + command.extend(['--expected', str(population)]) + proc = subprocess.run(command, capture_output=True, text=True, env=env) + try: + evidence = json.loads(proc.stdout) + except (ValueError, TypeError): + evidence = {'body_kill': False, 'baseline_success': False, 'output': proc.stdout + proc.stderr} + return proc.returncode, evidence + code, baseline = run(source_path) + result = {'schema_version': 1, 'tool': VERSION, 'scope': 'finite behavioral and source-mutation suite; no mathematical soundness claim', 'scanner_sha256': digest(source.encode()), 'tests_sha256': tests_before, 'lifecycle_helper_sha256': helper_before, 'optimization': sys.flags.optimize, 'baseline': {'exit_code': code, 'output': baseline.get('output', ''), 'lifecycle': baseline}, 'mutants': [], 'threshold': {'numerator': 1, 'denominator': 1}, 'result': 'FAIL'} + if code != 0 or not baseline.get('baseline_success') or baseline.get('optimization') != sys.flags.optimize: + return result + with tempfile.TemporaryDirectory(prefix='cbz-audit-mutants-') as directory: + for name, before, after in MUTATIONS: + # Restrict replacement to production source before mutation definitions. + head, tail = source.split('\nMUTATIONS = [', 1) + if head.count(before) != 1: + result['mutants'].append({'name': name, 'killed': False, 'error': 'mutation target not unique'}) + continue + path = Path(directory) / (name + '.py') + path.write_text(head.replace(before, after, 1) + '\nMUTATIONS = [' + tail) + code, evidence = run(path, baseline['population']) + killed = code == 0 and evidence.get('body_kill') is True and evidence.get('optimization') == sys.flags.optimize + result['mutants'].append({'name': name, 'killed': killed, 'exit_code': code, 'output': evidence.get('output', ''), 'lifecycle': evidence}) + result['killed'] = sum(item['killed'] for item in result['mutants']) + result['total'] = len(MUTATIONS) + result['inputs_unchanged'] = (digest(source_path.read_bytes()) == result['scanner_sha256'] + and digest(tests.read_bytes()) == tests_before + and digest(helper.read_bytes()) == helper_before) + result['result'] = 'PASS' if result['inputs_unchanged'] and result['total'] > 0 and result['killed'] * result['threshold']['denominator'] >= result['total'] * result['threshold']['numerator'] else 'FAIL' + return result + + +def load_json(path): + def pairs(items): + out = {} + for key, value in items: + if key in out: + raise ValueError('duplicate JSON key') + out[key] = value + return out + def bad_number(value): + raise ValueError('non-integer JSON number forbidden: ' + value) + return json.loads(path.read_text(), object_pairs_hook=pairs, parse_float=bad_number, parse_constant=bad_number) + + +def main(argv=None): + parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) + commands = parser.add_subparsers(dest='command', required=True) + scan_args = commands.add_parser('scan') + scan_args.add_argument('--root', type=Path, default=Path.cwd()) + scan_args.add_argument('--registry', type=Path) + scan_args.add_argument('--quarantine', type=Path) + self_args = commands.add_parser('selftest') + self_args.add_argument('--tests', type=Path, default=Path(__file__).resolve().parents[1] / 'tests/test_cbz_audit.py') + args = parser.parse_args(argv) + try: + if args.command == 'selftest': + result = selftest(args.tests.resolve()) + else: + result = scan(args.root, registry=load_json(args.registry) if args.registry else None, quarantine=load_json(args.quarantine) if args.quarantine else None) + except (OSError, ValueError, TypeError, subprocess.SubprocessError) as error: + result = {'result': 'FAIL', 'error': type(error).__name__ + ': ' + str(error), 'mathematical_soundness_certified': False} + print(json.dumps(result, sort_keys=True, indent=2, ensure_ascii=True, allow_nan=False)) + return 0 if result['result'] == 'PASS' else (2 if result['result'] == 'INCOMPLETE' else 1) + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/tools/cbz_baseline.py b/tools/cbz_baseline.py new file mode 100644 index 000000000..a51bcffb0 --- /dev/null +++ b/tools/cbz_baseline.py @@ -0,0 +1,761 @@ +#!/usr/bin/env python3 +"""Bounded local CBZ evidence validator. No network or Git-ref writes. + +PASS means only that the explicitly reported mechanical checks passed. This tool +cannot certify mathematical closure, complete Layer A, remote hygiene or CBZ-0. +Gate PASS means an observed, source-bound registered command exited zero. +""" +from __future__ import annotations +import argparse +import hashlib +import json +import os +from pathlib import Path +import re +import shutil +import signal +import stat +import subprocess +import sys +import tempfile +import time + +DEFAULT_CONFIG = 'cbz/evidence/website-foundation-20261029/baseline-config.json' +GATES = tuple(f'G{i}' for i in range(12)) +STATES = {'PASS', 'FAIL', 'INCOMPLETE'} + + +class ValidationError(RuntimeError): + pass + + +def _pairs(items): + result = {} + for key, value in items: + if key in result: + raise ValidationError('duplicate JSON key: ' + key) + result[key] = value + return result + + +def _read(path): + try: + return json.loads(Path(path).read_text(), object_pairs_hook=_pairs) + except (OSError, ValueError) as error: + raise ValidationError('unreadable or invalid JSON: ' + str(path)) from error + + +def _sha(path): + return hashlib.sha256(Path(path).read_bytes()).hexdigest() + + +def _inside(root, relative): + if not isinstance(relative, str) or not relative or Path(relative).is_absolute(): + raise ValidationError('expected relative source path') + path = (root / relative).resolve() + if not path.is_relative_to(root) or '.git' in Path(relative).parts: + raise ValidationError('source path escapes root or enters Git internals') + return path + + +def _config(root, config_path=None): + selected = config_path or DEFAULT_CONFIG + path = _inside(root, selected) + config = _read(path) + if config.get('schema') != 'cbz.baseline.v1': + raise ValidationError('unsupported baseline config schema') + if not set(config.get('allowed_axioms', [])).issubset({'propext', 'Classical.choice', 'Quot.sound'}): + raise ValidationError('custom axioms cannot be admitted by config') + gates = config.get('gates', []) + if not isinstance(gates, list) or len({g.get('id') for g in gates}) != len(gates): + raise ValidationError('duplicate or invalid gate declarations') + for gate in gates: + if gate.get('id') not in GATES or not isinstance(gate.get('argv'), list) or not gate['argv']: + raise ValidationError('invalid registered gate') + if any(not isinstance(x, str) or not x or '\x00' in x for x in gate['argv']): + raise ValidationError('invalid gate argv') + if not isinstance(gate.get('sources'), list) or not gate['sources']: + raise ValidationError('gate requires exact source inputs') + for source in gate['sources']: + _inside(root, source) + timeout = gate.get('timeout_seconds', 60) + memory = gate.get('memory_mb', 1024) + if type(timeout) is not int or not 1 <= timeout <= 3600 or type(memory) is not int or not 64 <= memory <= 8192: + raise ValidationError('gate resource bounds invalid') + config['_config_path'] = selected + return config + + +def _status(failures, unmet): + return 'FAIL' if failures else ('INCOMPLETE' if unmet else 'PASS') + + +def _hash_entries(root, entries, label, failures, unmet): + if not isinstance(entries, list) or not entries: + unmet.append(label + ': no exact source/evidence hashes') + return + seen = set() + for entry in entries: + if not isinstance(entry, dict) or not re.fullmatch(r'[0-9a-f]{64}', str(entry.get('sha256', ''))): + failures.append(label + ': invalid SHA-256 entry') + continue + path = _inside(root, entry.get('path')) + if entry['path'] in seen: + failures.append(label + ': duplicate source path') + seen.add(entry['path']) + if not path.is_file(): + unmet.append(label + ': missing ' + entry['path']) + elif _sha(path) != entry['sha256']: + failures.append(label + ': source/evidence hash mismatch ' + entry['path']) + + +def _proofs(root, config, failures, unmet): + index = _read(_inside(root, config['proof_index'])) + records = index.get('records') + if index.get('schema') != 'cbz.proof-index.v1' or not isinstance(records, list) or not records: + failures.append('proof index schema/records invalid') + return + ids = [r.get('id') for r in records] + if any(not isinstance(x, str) or not x for x in ids) or len(set(ids)) != len(ids): + failures.append('proof index IDs missing or duplicated') + return + by_id = dict(zip(ids, records)) + allowed = set(config.get('allowed_axioms', [])) + for record in records: + label = 'proof ' + record['id'] + status = record.get('status') + if status not in ('CLOSED', 'QUARANTINED'): + failures.append(label + ': invalid status') + _hash_entries(root, record.get('source_hashes'), label, failures, unmet) + if not record.get('exact_boundary') or not record.get('scientific_closure_limits'): + unmet.append(label + ': missing exact boundary or scientific limits') + deps = record.get('dependencies') + if not isinstance(deps, list) or any(dep not in by_id for dep in deps): + failures.append(label + ': dependency list missing or unresolved') + continue + if status == 'QUARANTINED': + if record.get('owner') != 'none' or any(not record.get(x) for x in ('reason', 'attempts', 'isolation')): + failures.append(label + ': quarantine reason/attempts/isolation/owner incomplete') + if status == 'CLOSED': + axioms = record.get('axioms') + if not isinstance(axioms, list): + unmet.append(label + ': exact observed axiom list unavailable') + elif any(axiom not in allowed for axiom in axioms): + failures.append(label + ': custom or undeclared axiom in CLOSED record') + _hash_entries(root, record.get('kernel_evidence'), label + ' kernel evidence', failures, unmet) + todo, seen = list(deps), set() + while todo: + dep = todo.pop() + if dep in seen: + continue + seen.add(dep) + if by_id[dep].get('status') == 'QUARANTINED': + failures.append(label + ': transitively depends on quarantined ' + dep) + next_deps = by_id[dep].get('dependencies', []) + if isinstance(next_deps, list): + todo.extend(x for x in next_deps if x in by_id) + # Cycles cannot supply independent closure evidence. + visiting, done = set(), set() + def visit(name): + if name in visiting: + failures.append('proof dependency cycle: ' + name) + return + if name in done: + return + visiting.add(name) + for dep in by_id[name].get('dependencies', []): + if dep in by_id: + visit(dep) + visiting.remove(name) + done.add(name) + for name in ids: + visit(name) + + +def _tools(root, config, failures, unmet): + registry = _read(_inside(root, config['tool_registry'])) + if registry.get('schema') != 'cbz.tools.v1' or not isinstance(registry.get('tools'), list): + failures.append('tool registry schema invalid') + return + listed = set() + for tool in registry['tools']: + path = tool.get('path') + if path in listed: + failures.append('duplicate registered tool: ' + str(path)) + listed.add(path) + if not _inside(root, path).is_file(): + unmet.append('registered tool missing: ' + str(path)) + command = tool.get('self_test') + if not isinstance(command, list) or not command or any(not isinstance(x, str) for x in command): + unmet.append('registered tool has no executable self-test: ' + str(path)) + if 'source_hashes' in tool: + _hash_entries(root, tool['source_hashes'], 'tool ' + str(path), failures, unmet) + for path in (root / 'tools').glob('cbz_*.py'): + if path.relative_to(root).as_posix() not in listed: + failures.append('unlisted mission tool: ' + path.name) + + +def _git(root, *args): + env = {k: v for k, v in os.environ.items() if not k.startswith('GIT_')} + env.update(GIT_TERMINAL_PROMPT='0', GIT_NO_REPLACE_OBJECTS='1') + p = subprocess.run(['git', '-C', str(root), *args], capture_output=True, env=env, timeout=30) + if p.returncode: + raise ValidationError('local Git observation unavailable') + return p.stdout + + +def _pins(root, config, failures, unmet): + result = [] + pins = config.get('pins') + if not isinstance(pins, list) or not pins: + unmet.append('cross-repository pin inventory unavailable or empty') + return result + for pin in pins: + local_f, local_u = [], [] + try: + commit = pin['commit'] + if not re.fullmatch(r'[0-9a-f]{40}(?:[0-9a-f]{24})?', commit): + local_f.append('pin commit must be exact full object ID') + repo = (root / pin['repo']).resolve() + if not repo.is_dir(): + local_u.append('offline dependency repository unavailable') + elif not local_f: + _git(repo, 'cat-file', '-e', commit + '^{commit}') + objpath = pin['object_path'] + if not isinstance(objpath, str) or not objpath or objpath.startswith(('/', '-')) or '..' in Path(objpath).parts: + raise ValidationError('invalid pinned object path') + content = _git(repo, 'show', commit + ':' + objpath) + if hashlib.sha256(content).hexdigest() != pin['sha256']: + local_f.append('pinned object bytes mismatch') + if 'pin_file' in pin: + manifest = _read(_inside(root, pin['pin_file'])) + matches = [p for p in manifest.get('packages', []) if p.get('name') == pin.get('package')] + if len(matches) != 1 or matches[0].get('rev') != commit: + local_f.append('package manifest pinned commit mismatch') + else: + local_u.append('no consuming package manifest binding') + except (KeyError, TypeError, ValidationError, OSError, subprocess.TimeoutExpired): + local_u.append('offline pin object/config evidence incomplete') + failures.extend('pin: ' + x for x in local_f) + unmet.extend('pin: ' + x for x in local_u) + result.append({'repo': pin.get('repo'), 'status': _status(local_f, local_u), 'failures': local_f, 'unmet': local_u}) + return result + + +def _binding(root, config, gate): + paths = [config['_config_path'], config['proof_index'], config['tool_registry'], *gate['sources']] + binding = {name: _sha(_inside(root, name)) for name in sorted(set(paths))} + if gate['id'] == 'G1': + # G1 receipt semantics are implemented by this consumer; bind that code too. + binding['tools/cbz_baseline.py'] = _sha(Path(__file__).resolve()) + return binding + + +def _scan_execution_identity(root, gate): + argv = gate.get('argv') + if not isinstance(argv, list) or not argv or any( + not isinstance(item, str) or not item or '\x00' in item for item in argv + ): + raise ValidationError('invalid registered G1 command') + executable = shutil.which(argv[0]) + if not executable: + raise ValidationError('registered G1 executable unavailable') + path = Path(executable).resolve() + if not path.is_file(): + raise ValidationError('registered G1 executable is not a file') + environment = { + 'PATH': os.defpath, + 'LANG': 'C', + 'LC_ALL': 'C', + 'GIT_CONFIG_NOSYSTEM': '1', + 'GIT_CONFIG_SYSTEM': os.devnull, + 'GIT_CONFIG_GLOBAL': os.devnull, + 'GIT_TERMINAL_PROMPT': '0', + 'GIT_NO_REPLACE_OBJECTS': '1', + 'GIT_OPTIONAL_LOCKS': '0', + 'PYTHONDONTWRITEBYTECODE': '1', + 'PYTHONHASHSEED': '0', + } + git_executable = shutil.which('git', path=environment['PATH']) + if not git_executable: + raise ValidationError('registered G1 Git executable unavailable') + git_path = Path(git_executable).resolve() + if not git_path.is_file(): + raise ValidationError('registered G1 Git executable is not a file') + try: + git_sha256 = _sha(git_path) + exclude_output = subprocess.run( + [str(git_path), '-C', str(root), 'rev-parse', '--git-path', 'info/exclude'], + cwd=root, capture_output=True, env=environment, timeout=10, check=True, + ).stdout + exclude_name = os.fsdecode(exclude_output.removesuffix(b'\n')) + if not exclude_name: + raise ValidationError('G1 Git info/exclude path unavailable') + info_exclude = Path(exclude_name) + if not info_exclude.is_absolute(): + info_exclude = (Path(root) / info_exclude).resolve() + else: + info_exclude = info_exclude.resolve() + info_exclude_sha256 = _sha(info_exclude) if info_exclude.is_file() else None + + # Pin Git's implicit global-ignore lookup to private, repository-local + # empty directories. Reject either default ignore file if it appears. + git_dir = info_exclude.parent.parent + home_dir = git_dir / 'cbz-g1-home' + xdg_dir = git_dir / 'cbz-g1-xdg' + def private_directory_identity(directory): + if directory.is_symlink(): + raise ValidationError('G1 isolated Git home/config path is a symlink') + if directory.exists() and not directory.is_dir(): + raise ValidationError('G1 isolated Git home/config path is not a directory') + directory.mkdir(mode=0o700, parents=True, exist_ok=True) + metadata = directory.lstat() + if not stat.S_ISDIR(metadata.st_mode): + raise ValidationError('G1 isolated Git home/config path is not a real directory') + if not hasattr(os, 'geteuid') or metadata.st_uid != os.geteuid(): + raise ValidationError('G1 isolated Git home/config directory owner is not the effective user') + mode = stat.S_IMODE(metadata.st_mode) + if mode != 0o700: + raise ValidationError('G1 isolated Git home/config directory mode must be 0700') + return {'uid': metadata.st_uid, 'mode': mode} + + home_directory = private_directory_identity(home_dir) + xdg_directory = private_directory_identity(xdg_dir) + environment['HOME'] = str(home_dir) + environment['XDG_CONFIG_HOME'] = str(xdg_dir) + + xdg_ignore = xdg_dir / 'git' / 'ignore' + home_ignore = home_dir / '.config' / 'git' / 'ignore' + for directory in (xdg_dir / 'git', home_dir / '.config', home_dir / '.config' / 'git'): + if directory.is_symlink(): + raise ValidationError('G1 default Git ignore parent is a symlink') + if directory.exists() and not directory.is_dir(): + raise ValidationError('G1 default Git ignore parent is not a directory') + if xdg_ignore.is_symlink() or xdg_ignore.exists(): + raise ValidationError('G1 default XDG Git ignore file must be absent') + if home_ignore.is_symlink() or home_ignore.exists(): + raise ValidationError('G1 default HOME Git ignore file must be absent') + default_excludes = { + 'home_directory_uid': home_directory['uid'], + 'home_directory_mode': home_directory['mode'], + 'xdg_directory_uid': xdg_directory['uid'], + 'xdg_directory_mode': xdg_directory['mode'], + 'home_path_sha256': hashlib.sha256(os.fsencode(str(home_dir))).hexdigest(), + 'xdg_config_home_path_sha256': hashlib.sha256(os.fsencode(str(xdg_dir))).hexdigest(), + 'xdg_ignore_path_sha256': hashlib.sha256(os.fsencode(str(xdg_ignore))).hexdigest(), + 'xdg_ignore_sha256': None, + 'home_fallback_ignore_path_sha256': hashlib.sha256(os.fsencode(str(home_ignore))).hexdigest(), + 'home_fallback_ignore_sha256': None, + } + git_config = subprocess.run( + [str(git_path), '-C', str(root), 'config', '--null', '--show-origin', '--list'], + cwd=root, capture_output=True, env=environment, timeout=10, check=True, + ).stdout + core_excludes_output = subprocess.run( + [str(git_path), '-C', str(root), 'config', '--path', '--null', '--get-all', 'core.excludesfile'], + cwd=root, capture_output=True, env=environment, timeout=10, + ) + if core_excludes_output.returncode not in (0, 1): + raise ValidationError('G1 Git core.excludesfile configuration unavailable') + core_excludesfiles = [] + for raw_path in core_excludes_output.stdout.split(b'\0'): + if not raw_path: + continue + configured_path = Path(os.fsdecode(raw_path)).expanduser() + if not configured_path.is_absolute(): + configured_path = (Path(root) / configured_path).resolve() + else: + configured_path = configured_path.resolve() + core_excludesfiles.append({ + 'path_sha256': hashlib.sha256(os.fsencode(str(configured_path))).hexdigest(), + 'sha256': _sha(configured_path) if configured_path.is_file() else None, + }) + version = subprocess.run( + [str(git_path), '--version'], cwd=root, capture_output=True, + env=environment, timeout=10, check=True, + ).stdout + if _sha(git_path) != git_sha256: + raise ValidationError('registered G1 Git executable changed during identity capture') + except (OSError, subprocess.SubprocessError) as error: + raise ValidationError('registered G1 Git execution identity unavailable') from error + return { + 'argv': [str(path), *argv[1:]], + 'executable_path': str(path), + 'executable_sha256': _sha(path), + 'environment': environment, + 'git': { + 'executable_path': str(git_path), + 'executable_sha256': git_sha256, + 'version': os.fsdecode(version).strip(), + 'config_sha256': hashlib.sha256(git_config).hexdigest(), + 'info_exclude_sha256': info_exclude_sha256, + 'core_excludesfiles': core_excludesfiles, + 'default_excludes': default_excludes, + }, + } + +def _scan_snapshot(raw_output, exit_code, execution_identity): + if type(exit_code) is not int: + raise ValidationError('G1 scan exit code unavailable') + git_identity = execution_identity.get('git') if isinstance(execution_identity, dict) else None + def valid_hash(value): + return isinstance(value, str) and re.fullmatch(r'[0-9a-f]{64}', value) is not None + core_excludesfiles = git_identity.get('core_excludesfiles') if isinstance(git_identity, dict) else None + default_excludes = git_identity.get('default_excludes') if isinstance(git_identity, dict) else None + environment = execution_identity.get('environment') if isinstance(execution_identity, dict) else None + if ( + not isinstance(execution_identity, dict) + or not valid_hash(execution_identity.get('executable_sha256')) + or not isinstance(git_identity, dict) + or not isinstance(git_identity.get('executable_path'), str) + or not valid_hash(git_identity.get('executable_sha256')) + or not valid_hash(git_identity.get('config_sha256')) + or not isinstance(git_identity.get('version'), str) + or not git_identity.get('version') + or (git_identity.get('info_exclude_sha256') is not None and not valid_hash(git_identity.get('info_exclude_sha256'))) + or not isinstance(environment, dict) + or not isinstance(environment.get('HOME'), str) + or not Path(environment.get('HOME', '')).is_absolute() + or not isinstance(environment.get('XDG_CONFIG_HOME'), str) + or not Path(environment.get('XDG_CONFIG_HOME', '')).is_absolute() + or not isinstance(default_excludes, dict) + or not hasattr(os, 'geteuid') + or type(default_excludes.get('home_directory_uid')) is not int + or default_excludes.get('home_directory_uid') < 0 + or default_excludes.get('home_directory_uid') != os.geteuid() + or type(default_excludes.get('home_directory_mode')) is not int + or default_excludes.get('home_directory_mode') != 0o700 + or type(default_excludes.get('xdg_directory_uid')) is not int + or default_excludes.get('xdg_directory_uid') < 0 + or default_excludes.get('xdg_directory_uid') != os.geteuid() + or type(default_excludes.get('xdg_directory_mode')) is not int + or default_excludes.get('xdg_directory_mode') != 0o700 + or default_excludes.get('home_directory_uid') != default_excludes.get('xdg_directory_uid') + or not valid_hash(default_excludes.get('home_path_sha256')) + or not valid_hash(default_excludes.get('xdg_config_home_path_sha256')) + or not valid_hash(default_excludes.get('xdg_ignore_path_sha256')) + or default_excludes.get('xdg_ignore_sha256') is not None + or not valid_hash(default_excludes.get('home_fallback_ignore_path_sha256')) + or default_excludes.get('home_fallback_ignore_sha256') is not None + or default_excludes.get('home_path_sha256') != hashlib.sha256(os.fsencode(environment['HOME'])).hexdigest() + or default_excludes.get('xdg_config_home_path_sha256') != hashlib.sha256(os.fsencode(environment['XDG_CONFIG_HOME'])).hexdigest() + or default_excludes.get('xdg_ignore_path_sha256') != hashlib.sha256(os.fsencode(str(Path(environment['XDG_CONFIG_HOME']) / 'git' / 'ignore'))).hexdigest() + or default_excludes.get('home_fallback_ignore_path_sha256') != hashlib.sha256(os.fsencode(str(Path(environment['HOME']) / '.config' / 'git' / 'ignore'))).hexdigest() + or not isinstance(core_excludesfiles, list) + or any( + not isinstance(item, dict) + or not valid_hash(item.get('path_sha256')) + or (item.get('sha256') is not None and not valid_hash(item.get('sha256'))) + for item in core_excludesfiles + ) + ): + raise ValidationError('G1 scan executable or Git identity unavailable') + try: + parsed = json.loads(raw_output.decode('utf-8'), object_pairs_hook=_pairs) + if not isinstance(parsed, dict): + raise ValidationError('G1 scan output is not a JSON object') + canonical = json.dumps( + {'exit_code': exit_code, 'execution': execution_identity, 'output': parsed}, + sort_keys=True, ensure_ascii=True, separators=(',', ':'), allow_nan=False, + ).encode('utf-8') + except (UnicodeDecodeError, ValueError, TypeError) as error: + raise ValidationError('G1 scan output unavailable or invalid') from error + return hashlib.sha256(canonical).hexdigest() + + +def _recompute_scan_snapshot(root, gate, expected_execution): + execution = _scan_execution_identity(root, gate) + if execution != expected_execution: + raise ValidationError('G1 registered execution identity changed') + timeout = gate.get('timeout_seconds', 60) + memory = gate.get('memory_mb', 1024) * 1024 * 1024 + + def limits(): + import resource + resource.setrlimit(resource.RLIMIT_CPU, (timeout + 1, timeout + 2)) + resource.setrlimit(resource.RLIMIT_AS, (memory, memory)) + resource.setrlimit(resource.RLIMIT_FSIZE, (64 * 1024 * 1024, 64 * 1024 * 1024)) + + with tempfile.TemporaryDirectory(prefix='cbz-g1-recheck-') as directory: + stdout_path = Path(directory) / 'stdout.json' + stderr_path = Path(directory) / 'stderr.log' + with stdout_path.open('wb') as stdout, stderr_path.open('wb') as stderr: + process = subprocess.Popen( + execution['argv'], cwd=root, stdout=stdout, stderr=stderr, + env=execution['environment'], start_new_session=True, preexec_fn=limits, + ) + try: + exit_code = process.wait(timeout=timeout) + except subprocess.TimeoutExpired as error: + try: + os.killpg(process.pid, signal.SIGKILL) + except ProcessLookupError: + pass + process.wait() + raise ValidationError('G1 scan timed out during receipt verification') from error + if _scan_execution_identity(root, gate) != execution: + raise ValidationError('G1 registered execution identity changed during recheck') + return _scan_snapshot(stdout_path.read_bytes(), exit_code, execution) + +def _receipt_statuses(root, config, receipt_path, failures, unmet): + statuses = dict.fromkeys(GATES, 'INCOMPLETE') + declared = {gate['id']: gate for gate in config.get('gates', [])} + if receipt_path is None: + unmet.append('no observed source-bound gate receipts supplied') + return statuses + try: + receipt_path = Path(receipt_path).resolve() + receipts = _read(receipt_path) + if not isinstance(receipts, dict): + raise ValidationError('receipt must be a JSON object') + if receipts.get('schema') != 'cbz.receipts.v1' or receipts.get('config_sha256') != _sha(_inside(root, config['_config_path'])): + raise ValidationError('stale or invalid receipt configuration binding') + rows = receipts.get('gates') + if not isinstance(rows, list) or any(not isinstance(row, dict) for row in rows): + raise ValidationError('receipt gates must be a list of objects') + names = [row.get('id') for row in rows] + if any(not isinstance(name, str) for name in names) or len(set(names)) != len(names): + raise ValidationError('duplicate or invalid gate receipts') + for row in rows: + name = row['id'] + if name not in declared: + raise ValidationError('receipt for unregistered gate') + gate = declared[name] + binding_before = _binding(root, config, gate) + if row.get('argv') != gate['argv'] or row.get('source_sha256') != binding_before: + unmet.append(name + ': stale receipt command/source identity') + continue + valid = True + log_paths = {} + for stream in ('stdout', 'stderr'): + relative = row.get(stream + '_path') + if not isinstance(relative, str): + valid = False + break + log = (receipt_path.parent / relative).resolve() + if not log.is_relative_to(receipt_path.parent) or not log.is_file() or _sha(log) != row.get(stream + '_sha256'): + valid = False + break + log_paths[stream] = log + exit_code = row.get('exit_code') + observed = 'INCOMPLETE' if row.get('timed_out') or row.get('source_changed') else ('PASS' if exit_code == 0 else ('INCOMPLETE' if exit_code == 2 else 'FAIL')) + if not valid or row.get('status') != observed or type(exit_code) not in (int, type(None)) or not isinstance(row.get('started_utc'), str) or not row.get('started_utc'): + unmet.append(name + ': unverified/forged receipt or process logs') + continue + if name == 'G1': + snapshot = row.get('scan_snapshot_sha256') + execution = row.get('scan_execution') + if not isinstance(snapshot, str) or not re.fullmatch(r'[0-9a-f]{64}', snapshot): + unmet.append('G1: missing or invalid scan snapshot; legacy receipt is INCOMPLETE') + continue + try: + current_execution = _scan_execution_identity(root, gate) + if execution != current_execution: + unmet.append('G1: registered execution identity is missing or changed') + continue + stored_snapshot = _scan_snapshot(log_paths['stdout'].read_bytes(), exit_code, execution) + if stored_snapshot != snapshot: + unmet.append('G1: stored scan snapshot does not match preserved JSON output') + continue + current_snapshot = _recompute_scan_snapshot(root, gate, execution) + binding_after = _binding(root, config, gate) + execution_after = _scan_execution_identity(root, gate) + except (ValidationError, OSError, subprocess.SubprocessError, TypeError, KeyError, AttributeError): + unmet.append('G1: current registered scan snapshot could not be reproduced') + continue + if binding_after != binding_before or execution_after != execution: + unmet.append('G1: source or execution identity changed during receipt recheck') + continue + if current_snapshot != snapshot: + unmet.append('G1: current scan snapshot differs from receipt') + continue + statuses[name] = observed + except (ValidationError, OSError, TypeError, KeyError, AttributeError): + unmet.append('stale/invalid receipt or missing exact execution evidence') + return dict.fromkeys(GATES, 'INCOMPLETE') + for name, status in statuses.items(): + if status == 'INCOMPLETE': + unmet.append(name + ': not observed passing on current registered inputs') + elif status == 'FAIL': + failures.append(name + ': observed command failed') + return statuses + +def cold_start(root, *, config_path=None): + root = Path(root).resolve() + config = _config(root, config_path) + cold = config.get('cold_start', {}) + failures, unmet = [], [] + try: + text = _inside(root, cold['status']).read_text() + for answer in cold.get('required_answers', []): + if answer not in text: + failures.append('STATUS missing required answer heading: ' + answer) + commands = cold.get('commands', []) + if not commands: + unmet.append('no cold-start commands registered') + for command in commands: + if not isinstance(command, list) or not command or not all(isinstance(x, str) for x in command): + failures.append('invalid cold-start command') + continue + if ' '.join(command) not in text: + failures.append('registered cold-start argv absent from STATUS') + if not shutil.which(command[0]) and not (root / command[0]).is_file(): + unmet.append('cold-start executable unavailable: ' + command[0]) + for arg in command[1:]: + if arg.startswith(('tools/', 'tests/')) and not _inside(root, arg).is_file(): + unmet.append('cold-start entrypoint missing: ' + arg) + except (KeyError, OSError): + unmet.append('STATUS cold-start entrypoint unavailable') + return {'schema': 'cbz.cold-start.v1', 'status': _status(failures, unmet), 'failures': failures, + 'unmet': unmet, 'certifies_theorems': False, + 'scope': 'required answer headings, documented argv, and executable entrypoint availability only'} + + +def inspect(root, *, receipts=None, config_path=None): + root = Path(root).resolve() + config = _config(root, config_path) + failures, unmet = [], [] + for operation in (_proofs, _tools): + try: + operation(root, config, failures, unmet) + except (ValidationError, OSError, TypeError, KeyError): + unmet.append(operation.__name__ + ': input schema/files incomplete') + pins = _pins(root, config, failures, unmet) + statuses = _receipt_statuses(root, config, receipts, failures, unmet) + for name in GATES: + if name not in {gate['id'] for gate in config.get('gates', [])}: + unmet.append(name + ': no registered gate command') + for doc in config.get('docs', []): + try: + text = _inside(root, doc).read_text() + markers = re.findall(r'', text, re.S) + if len(markers) != 1: + unmet.append(doc + ': missing/duplicate machine gate claims') + continue + claims = json.loads(markers[0], object_pairs_hook=_pairs) + if set(claims) != set(GATES) or any(value not in STATES for value in claims.values()): + failures.append(doc + ': invalid gate claims') + elif claims != statuses: + failures.append(doc + ': documented gate statuses disagree with observed current receipts') + except (OSError, ValidationError, ValueError): + unmet.append(doc + ': unavailable/invalid document claims') + cold = cold_start(root, config_path=config_path) + failures.extend(cold['failures']) + unmet.extend(cold['unmet']) + try: + dirty = _git(root, 'status', '--porcelain=v1', '--untracked-files=all').decode().splitlines() + stashes = _git(root, 'stash', 'list').decode().splitlines() + heads = _git(root, 'for-each-ref', '--format=%(refname)', 'refs/heads').decode().splitlines() + worktrees = _git(root, 'worktree', 'list', '--porcelain').decode().count('worktree ') + if dirty: + unmet.append('local Git worktree/index not clean') + if stashes: + unmet.append('local Git stashes remain') + if len(heads) != 1: + unmet.append('local Git branch disposition incomplete') + if worktrees != 1: + unmet.append('additional local Git worktrees require disposition') + git = {'dirty_paths': len(dirty), 'stashes': len(stashes), 'local_heads': heads, 'worktrees': worktrees} + except (ValidationError, subprocess.TimeoutExpired): + unmet.append('local Git hygiene unavailable') + git = {'status': 'INCOMPLETE'} + return {'schema': 'cbz.inspect.v1', 'status': _status(failures, unmet), 'gate_statuses': statuses, + 'failures': failures, 'unmet': unmet, 'pins': pins, 'local_git': git, + 'certifies_cbz0': False, 'baseline_state': 'Partial Baseline', + 'scope': 'mechanical local evidence consistency; command exits do not establish full Layer A or theorem acceptance'} + + +def run(root, output, *, config_path=None): + root, output = Path(root).resolve(), Path(output).resolve() + if output.is_relative_to(root) or output.exists(): + raise ValidationError('run output must be new and outside source root') + config = _config(root, config_path) + output.mkdir(parents=True) + result = {'schema': 'cbz.receipts.v1', 'config_sha256': _sha(_inside(root, config['_config_path'])), + 'scope': 'observed registered command exits with exact source/log binding; not scientific certification', 'gates': []} + for gate in sorted(config.get('gates', []), key=lambda g: int(g['id'][1:])): + name = gate['id'] + row = {'id': name, 'argv': gate['argv'], 'status': 'INCOMPLETE', 'exit_code': None, + 'timed_out': False, 'source_changed': False, 'started_utc': time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime()), + 'stdout_path': name + '.stdout', 'stderr_path': name + '.stderr'} + execution = None + try: + row['source_sha256'] = _binding(root, config, gate) + if name == 'G1': + execution = _scan_execution_identity(root, gate) + row['scan_execution'] = execution + timeout = gate.get('timeout_seconds', 60) + def limits(): + import resource + resource.setrlimit(resource.RLIMIT_CPU, (timeout + 1, timeout + 2)) + memory = gate.get('memory_mb', 1024) * 1024 * 1024 + resource.setrlimit(resource.RLIMIT_AS, (memory, memory)) + resource.setrlimit(resource.RLIMIT_FSIZE, (64 * 1024 * 1024, 64 * 1024 * 1024)) + command = execution['argv'] if execution is not None else gate['argv'] + environment = execution['environment'] if execution is not None else None + with (output / row['stdout_path']).open('wb') as stdout, (output / row['stderr_path']).open('wb') as stderr: + process = subprocess.Popen(command, cwd=root, stdout=stdout, stderr=stderr, + env=environment, start_new_session=True, preexec_fn=limits) + try: + row['exit_code'] = process.wait(timeout=timeout) + except subprocess.TimeoutExpired: + row['timed_out'] = True + try: + os.killpg(process.pid, signal.SIGKILL) + except ProcessLookupError: + pass + row['exit_code'] = process.wait() + if name == 'G1' and _scan_execution_identity(root, gate) != execution: + row['source_changed'] = True + row['source_changed'] = row['source_changed'] or row['source_sha256'] != _binding(root, config, gate) + row['status'] = 'INCOMPLETE' if row['timed_out'] or row['source_changed'] else ('PASS' if row['exit_code'] == 0 else ('INCOMPLETE' if row['exit_code'] == 2 else 'FAIL')) + except (OSError, ValidationError, subprocess.SubprocessError): + row['status'] = 'INCOMPLETE' + row['error'] = 'command/input unavailable; no passing execution claimed' + for stream in ('stdout', 'stderr'): + path = output / row[stream + '_path'] + path.touch(exist_ok=True) + row[stream + '_sha256'] = _sha(path) + if name == 'G1': + try: + if execution is None: + raise ValidationError('G1 executable identity unavailable') + row['scan_snapshot_sha256'] = _scan_snapshot( + (output / row['stdout_path']).read_bytes(), row['exit_code'], execution) + except (ValidationError, OSError): + row['status'] = 'INCOMPLETE' + row['scan_snapshot_error'] = 'G1 JSON output, execution identity or actual exit code unavailable' + result['gates'].append(row) + temp = output / 'receipts.json.tmp' + temp.write_text(json.dumps(result, sort_keys=True, indent=2) + '\n') + temp.replace(output / 'receipts.json') + return result + +def main(argv=None): + parser = argparse.ArgumentParser(description=__doc__) + sub = parser.add_subparsers(dest='command', required=True) + for name in ('inspect', 'cold-start', 'run'): + command = sub.add_parser(name) + command.add_argument('--root', default='.') + command.add_argument('--config', default=DEFAULT_CONFIG, help='root-relative namespaced config') + if name == 'inspect': + command.add_argument('--receipts') + if name == 'run': + command.add_argument('--output', required=True) + args = parser.parse_args(argv) + try: + if args.command == 'run': + result = run(args.root, args.output, config_path=args.config) + code = 0 if len(result['gates']) == 12 and all(r['status'] == 'PASS' for r in result['gates']) else 2 + else: + result = inspect(args.root, receipts=args.receipts, config_path=args.config) if args.command == 'inspect' else cold_start(args.root, config_path=args.config) + code = {'PASS': 0, 'FAIL': 1, 'INCOMPLETE': 2}[result['status']] + print(json.dumps(result, sort_keys=True, indent=2)) + return code + except (ValidationError, OSError, KeyError, TypeError) as error: + print(json.dumps({'status': 'INCOMPLETE', 'error': str(error), 'certifies_cbz0': False})) + return 2 + + +if __name__ == '__main__': + sys.exit(main())