Files
WoW-Hybrid-gateway/ops/owned_candidate_policy.py
2026-11-23 03:28:32 +02:00

76 lines
3.6 KiB
Python

"""Preflight checks, decisions and guarded compensation for an owned-client candidate."""
def verify_reused_artifacts(artifacts, *, digest, record):
"""Report which installed package members differ from the requested fingerprints."""
mismatches = {}
for path, expected in artifacts:
try:
actual = digest(path)
except OSError as error:
mismatches[path.name] = {'expected_sha256': expected, 'actual_sha256': None,
'read_error': type(error).__name__}
else:
if actual != expected:
mismatches[path.name] = {'expected_sha256': expected, 'actual_sha256': actual}
record['reuse_installed_mismatches'] = mismatches
if mismatches:
details = ', '.join(
f"{name} expected {entry['expected_sha256']}, actual {entry['actual_sha256'] or entry.get('read_error')}"
for name, entry in mismatches.items()
)
raise ValueError(f"Installed package differs from the requested selection ({details})")
def candidate_decision(explicit, record):
"""Return a decision; incomplete staging/setup can never be retained.
An explicit decision is authoritative for a successful run. Missing flags
in an explicit decision retain the historical meaning of rejection.
"""
staged = record.get('candidate_staging_complete') is True
started = record.get('candidate_test_started') is True
complete = (record.get('state') == 'complete-review-pending'
and record.get('runner_exit') == 0
and record.get('first_auth_accepted') is True
and not record.get('error'))
if not staged or not started:
return {'accept_store': False, 'accept_wire': False,
'reason': 'Candidate staging or runner setup did not complete.'}, 'incomplete-setup'
if explicit is None:
return {'accept_store': True, 'accept_wire': True,
'reason': 'Staged candidate retained by default; gameplay acceptance is not implied.'}, ('default-completed' if complete else 'default-unverified-after-test-failure')
if (not isinstance(explicit, dict)
or any(type(explicit.get(k, False)) is not bool for k in ('accept_store', 'accept_wire'))):
return {'accept_store': False, 'accept_wire': False,
'reason': 'Invalid explicit decision; candidate rejected.'}, 'invalid-explicit'
return dict(explicit), 'explicit'
def compensate_cleanup_failure(record, *, can_rollback, rollback):
"""Attempt rollback after cleanup failure without touching an unsafe owner.
A blocked or failed rollback is explicitly not accepted and must leave the
lock/recovery evidence intact; it is not reported as a successful restore.
"""
record['candidate_accepted'] = False
record['candidate_store_retained'] = None
record['candidate_wire_retained'] = None
try:
safe = can_rollback()
except BaseException as error:
record['rollback_error'] = type(error).__name__ + ': ' + str(error)
safe = False
if not safe:
record['candidate_state'] = 'rollback-blocked-not-accepted'
return
try:
rollback({'accept_store': False, 'accept_wire': False})
except BaseException as error:
record['rollback_error'] = type(error).__name__ + ': ' + str(error)
record['candidate_state'] = 'rollback-incomplete-not-accepted'
else:
record['candidate_store_retained'] = False
record['candidate_wire_retained'] = False
record['candidate_state'] = 'rolled-back-after-cleanup-error'