187 lines
10 KiB
Python
187 lines
10 KiB
Python
#!/usr/bin/env python3
|
|
"""Offline checkpoint readiness and strict original/native acceptance.
|
|
|
|
Human findings are declarations bound to exact receipts, never inferred from
|
|
successful tools. --allow-open is structural inspection, not acceptance.
|
|
"""
|
|
import argparse
|
|
import hashlib
|
|
from pathlib import Path
|
|
import sys
|
|
|
|
from fidelity_checkpoint import MATERIALS, PROJECT, _package, preflight
|
|
from fidelity_receipts import (audit_scenarios, canonical_bytes, contained_path,
|
|
identify_file, identify_tree, read_json, read_receipt, validate_receipt,
|
|
validate_scenario_contract)
|
|
|
|
CHECKS = ('A1', 'A2', 'EVID-02-unclassified', 'EVID-04-unclassified',
|
|
'P1', 'P2', 'P3', 'browser', 'paired_review', 'physical_input')
|
|
|
|
|
|
def ready(checkpoint):
|
|
root = Path(checkpoint)
|
|
checked = preflight(root)
|
|
manifest = read_json(root / 'manifest.json')
|
|
if manifest['tooling_only']:
|
|
raise ValueError('Synthetic checkpoint is tooling-only, not final materials')
|
|
names = {entry['path'] for entry in manifest['files']}
|
|
required = set(MATERIALS) | {'protocol/scenarios.json', 'protocol/capabilities.json',
|
|
'protocol/agenda.json', 'protocol/settings-template.json',
|
|
'protocol/annotation-template.json', 'review/index.html', 'review/viewer.mjs',
|
|
'review/review.json'}
|
|
if required - names:
|
|
raise ValueError('Missing final session materials: ' + ', '.join(sorted(required - names)))
|
|
# A source checkout must not hand off an older tooling/checklist copy after
|
|
# later edits. On a transferred checkpoint PROJECT is the checkpoint itself.
|
|
for name in MATERIALS:
|
|
if identify_file(PROJECT / name) != identify_file(root / name):
|
|
raise ValueError('Final material differs from current source; assemble a fresh checkpoint: ' + name)
|
|
protocol = read_json(root / 'protocol/scenarios.json')
|
|
validate_scenario_contract(protocol)
|
|
for rid, identity in manifest['packages'].items():
|
|
if _package(root / 'port' / rid, rid, False) != identity:
|
|
raise ValueError('Package/observer identity inconsistent: ' + rid)
|
|
agenda = read_json(root / 'protocol/agenda.json')
|
|
expected = {(s, c['id'], o) for s in ('original-windows-dxwnd', 'port-windows-native')
|
|
for c in protocol['cells'] for o in c['obligations']}
|
|
actual = {(r['side'], r['case_id'], r['obligation_id']) for r in agenda['runs']}
|
|
if actual != expected or len(agenda['runs']) != 80:
|
|
raise ValueError('Incomplete or duplicate Windows session agenda')
|
|
# Exported ledger links are already normalized by the exporter. Recheck
|
|
# every active link against the sealed actual materials at final handoff.
|
|
ledger = read_json(root / 'review/review.json')['ledger']
|
|
for capability in ledger['capabilities']:
|
|
for link in capability.get('source_links', []):
|
|
if link.get('href'):
|
|
identify_file(contained_path(root / 'review', link['href']))
|
|
return {'schema_version': 1, 'materials_ready': checked['status'] == 'ready',
|
|
'checked_files': checked['checked_files'], 'acceptance': 'human_needed',
|
|
'required': 120, 'pending': list(CHECKS),
|
|
'meaning': 'Identified offline materials only; all execution/review remains separate'}
|
|
|
|
|
|
def _bind(receipt, root, manifest, original_inventory=None):
|
|
identity = receipt['identity']
|
|
if receipt['evidence_kind'] == 'native_port':
|
|
rid = {'linux': 'linux-x64', 'windows': 'win-x64'}[receipt['platform']['os']]
|
|
expected = manifest['packages'][rid]
|
|
if identity['executable']['sha256'] != expected['executable']['sha256']:
|
|
raise ValueError('Wrong current executable identity')
|
|
if identity['source']['sha256'] not in (expected['build_info']['sha256'], expected['seal']['sha256']):
|
|
raise ValueError('Wrong current source identity')
|
|
if any(identity['source'].get(k) != expected['build']['source'].get(k)
|
|
for k in ('commit', 'dirty', 'diff_sha256')):
|
|
raise ValueError('Wrong source revision/dirty/diff provenance')
|
|
package = read_json(contained_path(root, identity['package']['path']))
|
|
resources = read_json(contained_path(root, identity['resources']['path']))
|
|
if (package.get('source') != expected['build']['source'] or
|
|
identity['package']['sha256'] != expected['seal']['sha256']):
|
|
raise ValueError('Wrong current package seal/source')
|
|
if resources != expected['resources'] and identity['resources']['sha256'] != expected['seal']['sha256']:
|
|
raise ValueError('Wrong current resources')
|
|
elif receipt['evidence_kind'] == 'original_dxwnd':
|
|
files = (original_inventory or {}).get('files')
|
|
if not isinstance(files, list) or not files:
|
|
raise ValueError('Missing authoritative original file inventory')
|
|
digest = hashlib.sha256(canonical_bytes(files)).hexdigest()
|
|
if (digest != manifest['original_pre']['tree_sha256'] or
|
|
len(files) != manifest['original_pre']['count']):
|
|
raise ValueError('Wrong authoritative original inventory')
|
|
for key in ('original_pre', 'original_post', 'package', 'resources'):
|
|
inventory = read_json(contained_path(root, identity[key]['path']))
|
|
if inventory.get('files') != files or inventory.get('tree_sha256') != digest:
|
|
raise ValueError('Wrong authoritative original inventory: ' + key)
|
|
executable = identity['executable']
|
|
member = executable.get('original_member')
|
|
expected = next((item for item in files if item['path'] == member), None)
|
|
if not expected or any(executable.get(k) != expected[k] for k in ('sha256', 'size', 'header')):
|
|
raise ValueError('Wrong selected original executable member identity')
|
|
|
|
|
|
def audit_records(protocol, manifest, records, review, original_inventory=None):
|
|
errors, usable, seen = [], [], set()
|
|
if manifest.get('tooling_only'):
|
|
errors.append('Synthetic checkpoint cannot qualify as runtime evidence')
|
|
for receipt, root in records:
|
|
label = str(receipt.get('receipt_id', '<missing>'))
|
|
try:
|
|
if label in seen:
|
|
raise ValueError('Duplicate/conflicting receipt ID; retain history outside current selection')
|
|
seen.add(label)
|
|
valid = validate_receipt(receipt, root)
|
|
if receipt.get('superseded_by') or receipt.get('historical'):
|
|
raise ValueError('Superseded/historical receipt is not current applicability')
|
|
if valid['accepted']:
|
|
_bind(receipt, root, manifest, original_inventory)
|
|
usable.append((receipt, root))
|
|
except (KeyError, TypeError, ValueError, OSError) as error:
|
|
errors.append(label + ': ' + str(error))
|
|
matrix = audit_scenarios(protocol, usable)
|
|
errors.extend(matrix['invalid'])
|
|
checks = review.get('checks', {})
|
|
human_needed = [key for key in CHECKS if not (isinstance(checks.get(key), dict)
|
|
and checks[key].get('observed') is True and checks[key].get('reviewer')
|
|
and checks[key].get('notes'))]
|
|
if set(review.get('receipt_ids', [])) != seen:
|
|
errors.append('Human review must bind the exact selected receipt IDs')
|
|
passed = matrix['complete'] and not errors and not human_needed
|
|
return {'schema_version': 1, 'materials_ready': True,
|
|
'acceptance': 'passed' if passed else 'human_needed', 'matrix': matrix,
|
|
'errors': errors, 'human_needed': human_needed,
|
|
'meaning': 'Declared reviewed evidence; no automatic physical/audible truth or universal tolerance'}
|
|
|
|
|
|
def audit(checkpoint, results):
|
|
root, results = Path(checkpoint), Path(results)
|
|
ready(root)
|
|
manifest = read_json(root / 'manifest.json')
|
|
for rid, package in manifest['packages'].items():
|
|
package['seal'] = identify_file(root / 'port' / rid / 'fidelity-build.json')
|
|
# A strict, explicit selection prevents newest-date selection and preserves
|
|
# failed/superseded histories. The tree walk rejects symlinks/case aliases.
|
|
identify_tree(results)
|
|
selection = results / 'current.json'
|
|
paths = read_json(selection).get('receipts', []) if selection.exists() else []
|
|
if not isinstance(paths, list) or len(paths) > 120 or len(set(paths)) != len(paths):
|
|
raise ValueError('Current receipt selection must be a bounded unique array')
|
|
records, selected = [], []
|
|
for name in paths:
|
|
path = contained_path(results, name)
|
|
records.append((read_receipt(path), path.parent))
|
|
selected.append(path)
|
|
review_path = results / 'human-review.json'
|
|
review = read_json(review_path) if review_path.exists() else {}
|
|
# Bind review to actual receipt bytes, not just reusable labels.
|
|
for (receipt, _), path in zip(records, selected):
|
|
expected = review.get('receipt_sha256', {}).get(receipt['receipt_id'])
|
|
if expected != identify_file(path)['sha256']:
|
|
review = dict(review, checks={})
|
|
break
|
|
return audit_records(read_json(root / 'protocol/scenarios.json'), manifest, records, review,
|
|
read_json(root / 'protocol/original-manifest.json'))
|
|
|
|
|
|
def main(argv=None):
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
sub = parser.add_subparsers(dest='command', required=True)
|
|
prepare = sub.add_parser('ready')
|
|
prepare.add_argument('--checkpoint', type=Path, required=True)
|
|
check = sub.add_parser('audit')
|
|
check.add_argument('--checkpoint', type=Path, required=True)
|
|
check.add_argument('--results', type=Path, required=True)
|
|
mode = check.add_mutually_exclusive_group(required=True)
|
|
mode.add_argument('--require-complete', action='store_true')
|
|
mode.add_argument('--allow-open', action='store_true')
|
|
args = parser.parse_args(argv)
|
|
try:
|
|
report = ready(args.checkpoint) if args.command == 'ready' else audit(args.checkpoint, args.results)
|
|
print(canonical_bytes(report).decode())
|
|
return 0 if args.command == 'ready' or args.allow_open or report['acceptance'] == 'passed' else 1
|
|
except (ValueError, OSError, KeyError, TypeError) as error:
|
|
print('Fidelity audit failed; acceptance OPEN: ' + str(error), file=sys.stderr)
|
|
return 2
|
|
|
|
|
|
if __name__ == '__main__':
|
|
sys.exit(main())
|