From 0984c5b97e7e8326d6856ba662274ecf325e5e0f Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Sun, 18 Oct 2026 06:47:25 +0000 Subject: [PATCH] Bootstrap hosted native validation with private diagnostics and draft guards --- .github/native/hosted_native.py | 880 ++++++++++++++++++ .github/native/nodeify.cjs | 33 + .github/native/startup.yaml | 22 + .../tests/keeper-local-validation.test.cjs | 408 +++++++- .github/tests/native-action-pins.test.cjs | 182 ++++ .github/tests/native-draft-policy.test.cjs | 131 +++ .github/tests/nodeify.test.cjs | 38 + .github/tests/test_diagnostic_privacy.py | 136 +++ .github/tests/test_hosted_native.py | 494 ++++++++++ .github/workflows/keeper-hosted-native.yml | 150 +++ .github/workflows/keeper-local-validation.yml | 106 ++- .github/workflows/keeper-validation-tests.yml | 26 + .github/workflows/test.yml | 8 + AGENTS.md | 6 +- docs/cloud-bootstrap-preparation.md | 175 ++++ docs/hosted-native-validation.md | 198 ++++ 16 files changed, 2968 insertions(+), 25 deletions(-) create mode 100644 .github/native/hosted_native.py create mode 100644 .github/native/nodeify.cjs create mode 100644 .github/native/startup.yaml create mode 100644 .github/tests/native-action-pins.test.cjs create mode 100644 .github/tests/native-draft-policy.test.cjs create mode 100644 .github/tests/nodeify.test.cjs create mode 100644 .github/tests/test_diagnostic_privacy.py create mode 100644 .github/tests/test_hosted_native.py create mode 100644 .github/workflows/keeper-hosted-native.yml create mode 100644 .github/workflows/keeper-validation-tests.yml create mode 100644 docs/cloud-bootstrap-preparation.md create mode 100644 docs/hosted-native-validation.md diff --git a/.github/native/hosted_native.py b/.github/native/hosted_native.py new file mode 100644 index 000000000..6573d9595 --- /dev/null +++ b/.github/native/hosted_native.py @@ -0,0 +1,880 @@ +#!/usr/bin/env python3 +"""Disposable hosted smoke harness; only allowlisted metadata leaves the runner. + +Native and Maestro output stays under RUNNER_TEMP, never in an uploaded glob. +The gate trusts protected workflow/job provenance, never this writable artifact. +""" + +import hashlib +import json +import os +from pathlib import Path +import platform +import re +import shutil +import signal +import subprocess +import sys +import tempfile +import time +import urllib.request +import xml.etree.ElementTree as ET + + +ROOT = Path(__file__).resolve().parents[2] +PLATFORM = os.environ.get('KEEPER_NATIVE_PLATFORM', '') +PRIVATE = Path(os.environ.get('RUNNER_TEMP', tempfile.gettempdir())) / 'keeper-native-private' +EVIDENCE = ROOT / 'artifacts' / 'native' / PLATFORM.lower() / 'metadata.json' +MAESTRO_VERSION = 'cli-2.11.0' +MAESTRO_SHA256 = '5384593cb4e7a106489e75a821d157dd43f4e438df6bc308b72e82c685e1283a' +APP_IDS = {'IOS': 'io.hexawallet.hexakeeper.dev', 'ANDROID': 'io.hexawallet.keeper.development'} + + +def save(data): + EVIDENCE.parent.mkdir(parents=True, exist_ok=True) + EVIDENCE.write_text(json.dumps(data, indent=2) + '\n') + + +def read(): + return json.loads(EVIDENCE.read_text()) + + +def failure_details(log): + """Classify observed tool failures; never return any part of a log line. + + Redaction cannot recognize arbitrary wallet, user or application text. Each + result must come from this fixed vocabulary, including the fallback. + """ + categories = ( + (r'lockfile needs to be updated|could not find compatible versions|^The sandbox is not in sync', + 'DEPENDENCY_LOCK_MISMATCH'), + (r'no space left on device', 'DISK_SPACE_EXHAUSTED'), + (r'command not found', 'REQUIRED_TOOL_UNAVAILABLE'), + (r'requires a higher minimum deployment target', 'IOS_DEPLOYMENT_TARGET_UNSUPPORTED'), + (r'^e: ', 'KOTLIN_COMPILER_ERROR'), + (r'^\[!\]', 'COCOAPODS_ERROR'), + (r'^FAILURE:|^A problem occurred|^Execution failed', 'BUILD_FAILURE'), + (r'^Could not ', 'DEPENDENCY_OR_TOOL_FAILURE'), + (r'\berror:|^error |^> (?!Task\b)|^Caused by:', 'TOOL_ERROR'), + ) + selected = [] + for raw in log.splitlines(): + line = re.sub(r'\x1b\[[0-9;]*[A-Za-z]', '', raw).strip() + if re.match(r'^\d+\s*\|', line): + continue + for pattern, category in categories: + if re.search(pattern, line, re.IGNORECASE): + if category not in selected: + selected.append(category) + break + return selected + + +def maestro_failure_details(log): + """Retain only fixed command labels, selectors and tool failure categories.""" + selected = [] + for raw in log.splitlines(): + line = re.sub(r'\x1b\[[0-9;]*[A-Za-z]', '', raw).strip() + if line.endswith('FAILED'): + if line.startswith(('Launch app', 'Clear state', 'Stop app')) and any(app in line for app in APP_IDS.values()): + selected.append('Maestro app launch/stop command FAILED') + elif line.startswith(('Assert', 'Wait')) and 'Create a passcode' in line: + selected.append('Maestro assertion: Create a passcode visible FAILED') + elif line.startswith(('Assert', 'Wait')) and 'Confirm your passcode' in line: + selected.append('Maestro assertion: Confirm your passcode state FAILED') + elif line.startswith('Tap on'): + match = re.search(r'\bkey_[1-4]\b', line) + if match: + selected.append('Maestro tap %s FAILED' % match.group()) + elif line.startswith(('Assertion is false:', 'Element not found:')): + # Emit only a category and fixed selector; even a first-line suffix + # can contain a hierarchy or arbitrary device text. + category = 'assertion failed' if line.startswith('Assertion is false:') else 'element not found' + for target in ['Create a passcode', 'Confirm your passcode', 'key_1', 'key_2', 'key_3', 'key_4']: + if target in line: + selected.append('Maestro %s: %s' % (category, target)) + break + elif line.startswith(('Unable to launch app', 'Failed to launch app')): + selected.append('Maestro app launch failed') + elif line.startswith(('Unable to connect', 'Failed to connect')): + selected.append('Maestro connection failed') + elif line.startswith('Failed to start'): + selected.append('Maestro driver start failed') + elif line.startswith(('No devices found', 'No connected devices', 'Unable to find device')): + selected.append('Maestro device unavailable') + if len(selected) >= 10: + break + return list(dict.fromkeys(selected))[:10] + + +def android_startup_errors(log): + categories = {'AndroidRuntime': 'ANDROID_RUNTIME_ERROR', + 'ReactNativeJS': 'REACT_NATIVE_JS_ERROR', 'ReactNative': 'REACT_NATIVE_ERROR'} + selected = [] + for line in log.splitlines(): + match = re.match(r'^[EF]/(AndroidRuntime|ReactNativeJS|ReactNative)\s*\(\s*\d+\):\s*(.*)', line) + if not match: + continue + tag, message = match.groups() + if re.search(r'(?:FATAL EXCEPTION|Caused by:|\b[\w.$]*(?:Exception|Error)\b|' + r'Invariant Violation|Unable to load script|Could not)', message): + if categories[tag] not in selected: + selected.append(categories[tag]) + return selected + + +def metro_startup_errors(log): + selected = [] + for raw in log.splitlines(): + line = re.sub(r'\x1b\[[0-9;]*[A-Za-z]', '', raw).strip() + if re.match(r'^(?:error\b|(?:Syntax|Type|Reference)Error:|Unable to resolve module)', line, re.IGNORECASE): + category = 'METRO_ERROR' + for marker, known in [('Unable to resolve module', 'METRO_MODULE_RESOLUTION_ERROR'), + ('SyntaxError:', 'METRO_SYNTAX_ERROR'), + ('TypeError:', 'METRO_TYPE_ERROR'), + ('ReferenceError:', 'METRO_REFERENCE_ERROR')]: + if marker.lower() in line.lower(): + category = known + break + if category not in selected: + selected.append(category) + return selected + + +def metro_startup_progress(log): + # Metro's TerminalReporter uses the same bare BUNDLE line for done and + # failed builds. Count observed markers without inferring completion. + lines = [re.sub(r'\x1b\[[0-9;]*[A-Za-z]', '', line).strip() for line in log.splitlines()] + bundles = sum(bool(re.match(r'^BUNDLE\s', line)) for line in lines) + prebundles = sum(bool(re.match(r'^PREBUNDLE\s', line)) for line in lines) + return {'bundle_marker_observed': bundles > 0, 'bundle_marker_count': bundles, + 'prebundle_marker_count': prebundles} + + +def private_log_prefix(path): + """Read launch-time output, discarding a partially truncated final record.""" + limit = 1024 * 1024 + with path.open('rb') as source: + payload = source.read(limit + 1) + truncated = len(payload) > limit + payload = payload[:limit] + if truncated: + payload = payload.rsplit(b'\n', 1)[0] + b'\n' if b'\n' in payload else b'' + return payload.decode(errors='replace'), truncated + + +def ios_startup_diagnostics(payload, executable): + if not re.fullmatch(r'[A-Za-z0-9_][A-Za-z0-9_.-]*', executable): + raise ValueError('Unsafe iOS executable selector.') + # log(1) compact: ISO timestamp, abbreviated type, process[PID:TID], message. + # Only prefixed own-process records qualify; never retain continuation lines. + pattern = re.compile(r'^\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}:\d{2}(?:\.\d+)?' + r'(?:[+-]\d{2}:?\d{2}|Z)?\s+(\S+)\s+' + r'([A-Za-z0-9_.-]+)\[\d+:[A-Fa-f0-9x]+\]\s+(.*)$') + selected = [] + matched = 0 + for line in payload.splitlines(): + match = pattern.match(line) + if not match: + continue + severity, process, message = match.groups() + if process != executable: + continue + matched += 1 + if severity.lower() in ['e', 'f', 'er', 'ft', 'error', 'fault'] or re.search( + r'(?:\b(?:error|exception|fatal)\b|\b[\w.$]*(?:Exception|Error)\b)', + message, re.IGNORECASE): + if not selected: + selected.append('IOS_STARTUP_ERROR') + return {'format': 'compact', 'matching_process_records': matched, + 'errors': selected} + + +def startup_hierarchy_signals(directory): + """Classify fixed public startup selectors; never export hierarchy content.""" + # Maestro cli-2.11.0 writes standalone TreeNode snapshots in this layout. + flags = ['createPIN', 'confirmPIN', 'RNredbox', 'noScriptURL', + 'developmentServerError', 'unableLoadScript'] + result = {flag: False for flag in flags} + result.update({'snapshot_count': 0, 'parse_error_types': []}) + redbox_ids = {'redbox-error', 'redbox-dismiss', 'redbox-reload', 'rn_redbox_stack', + 'rn_redbox_dismiss_button', 'rn_redbox_reload_button', 'catalyst_redbox_title'} + fixed_messages = {'noScriptURL': 'No script URL provided.', + 'developmentServerError': 'Could not connect to development server.', + 'unableLoadScript': 'Unable to load script.'} + for index, path in enumerate(directory.glob('**/screen-hierarchy/step-*.json')): + try: + if index >= 8: + raise ValueError('Hierarchy snapshot count exceeded limit.') + result['snapshot_count'] += 1 + if path.is_symlink() or not path.resolve().is_relative_to(directory.resolve()): + raise ValueError('Hierarchy snapshot must remain in private output.') + with path.open('rb') as source: + payload = source.read(1024 * 1024 + 1) + if len(payload) > 1024 * 1024: + raise ValueError('Hierarchy snapshot exceeded byte limit.') + pending = [json.loads(payload)] + snapshot = {flag: False for flag in flags} + nodes = 0 + while pending: + node = pending.pop() + nodes += 1 + if nodes > 10000: + raise ValueError('Hierarchy snapshot exceeded node limit.') + if not isinstance(node, dict): + raise TypeError('Unexpected hierarchy node format.') + attributes = node.get('attributes') or {} + children = node.get('children') or [] + if not isinstance(attributes, dict) or not isinstance(children, list): + raise TypeError('Unexpected hierarchy attributes or children format.') + # iOS StaticText often exposes only accessibilityText. Android + # resource IDs include the package prefix before :id/. + identifier = attributes.get('resource-id', '') + if isinstance(identifier, str) and identifier.rsplit(':id/', 1)[-1] in redbox_ids: + snapshot['RNredbox'] = True + for key in ['accessibilityText', 'text', 'title', 'value']: + value = attributes.get(key) + if not isinstance(value, str): + continue + snapshot['createPIN'] |= value.strip() == 'Create a passcode' + snapshot['confirmPIN'] |= value.strip() == 'Confirm your passcode' + for flag, message in fixed_messages.items(): + snapshot[flag] |= message in value + if len(pending) + len(children) + nodes > 10000: + raise ValueError('Hierarchy snapshot exceeded node limit.') + pending.extend(children) + for flag in flags: + result[flag] |= snapshot[flag] + except Exception as error: + kind = type(error).__name__ + if kind not in result['parse_error_types']: + result['parse_error_types'].append(kind) + if index >= 8: + break + return result + + +def maestro_preflight_diagnostics(directory=None, command_label='Assert startup and disposable passcode transition'): + """Export fixed cli-2.11.0 driver markers, never subprocess error text.""" + markers = { + 'runner_install_started': '[Start] Install XCUITest runner on', + 'runner_install_launch_returned': '[Done] Install XCUITest runner on', + 'status_check_started': '[Start] Perform XCUITest driver status check on', + 'status_response_received': '[Done] Perform XCUITest driver status check on', + 'status_check_failed': '[Failed] Perform XCUITest driver status check on', + 'driver_startup_timeout': 'iOS driver not ready in time, consider increasing timeout by configuring MAESTRO_DRIVER_STARTUP_TIMEOUT env variable', + 'prebuilt_driver_selected': 'Installing pre built driver without xcodebuild', + 'xcodebuild_driver_selected': 'Installing driver with xcodebuild', + 'xctestrun_config_missing': 'xctestrun config does not exist', + 'ui_test_runner_missing': 'ui test runner does not exist', + 'xcode_test_execute_failed': '** TEST EXECUTE FAILED **', + 'xcode_test_failed': '** TEST FAILED **', + 'xcode_testing_failed': 'Testing failed:', + } + # The install/status "Done" markers precede readiness evaluation; they do + # not prove that the runner installed successfully or became ready. + result = {name: False for name in markers} + result.update({'host_port_unavailable': False, 'requested_device_disconnected': False, + 'exception_types': [], 'logs_read': 0, 'truncated_logs': 0, + 'log_limit_reached': False, 'collection_error_types': []}) + known_types = ['IOSDriverTimeoutException', 'FileNotFoundException', 'CliError', + 'IllegalStateException', 'IOException', 'ConnectException', + 'SocketTimeoutException', 'TimeoutException', 'RuntimeException', + 'UnknownHostException', 'SSLHandshakeException'] + paths = [] + for index, entry in enumerate(read()['commands'], 1): + if entry['label'] == command_label: + paths.append(PRIVATE / ('command-%03d.log' % index)) + directory = directory if directory is not None else PRIVATE / 'maestro-output' + if not directory.resolve().is_relative_to(PRIVATE.resolve()): + raise ValueError('Maestro diagnostic directory must remain private.') + for pattern in ['**/maestro.log', '**/xctest_runner_*.log']: + for path in directory.glob(pattern): + paths.append(path) + if len(paths) > 6: + break + if len(paths) > 6: + break + for path in paths[:6]: + try: + if path.is_symlink() or not path.resolve().is_relative_to(PRIVATE.resolve()): + raise ValueError('Maestro log must remain in private output.') + payload, truncated = private_log_prefix(path) + result['logs_read'] += 1 + result['truncated_logs'] += int(truncated) + for name, marker in markers.items(): + result[name] |= marker in payload + for line in payload.splitlines(): + result['host_port_unavailable'] |= ('Requested driver host port ' in line + and ' is not available' in line) + result['requested_device_disconnected'] |= (' was requested, but it is not connected.' in line) + for kind in known_types: + if re.search(r'(? 6 + return result + + +def record_startup_failure(report, device): + """Collect bounded diagnostics before destroying this job's fresh app state.""" + details = {'scope': 'Fresh startup only; no account or persisted PIN created.', + 'collection_errors': []} + + def collect(name, action): + try: + details[name] = action() + except Exception as error: + details['collection_errors'].append({'stage': name, 'error_type': type(error).__name__}) + + def private_text(path): + # No raw logs/reports are copied to the artifact directory. + with path.open('rb') as source: + source.seek(0, os.SEEK_END) + source.seek(max(0, source.tell() - 1024 * 1024)) + return source.read().decode(errors='replace') + + def maestro_details(): + result = {'failed_commands': [], 'testcase_failures': [], 'report': 'MISSING'} + for index, entry in enumerate(read()['commands'], 1): + if entry['label'] == 'Assert startup and disposable passcode transition': + result['failed_commands'] = maestro_failure_details(private_text(PRIVATE / ('command-%03d.log' % index))) + if report.exists(): + if report.stat().st_size > 1024 * 1024: + raise RuntimeError('Maestro report exceeded diagnostic size limit.') + suite = ET.parse(report).getroot() + result['report'] = 'PRESENT' + result['testcase_statuses'] = [case.get('status') if case.get('status') in + ['SUCCESS', 'ERROR', 'WARNING', 'SKIPPED'] else 'UNKNOWN' + for case in list(suite.iter('testcase'))[:10]] + for failure in list(suite.iter('failure'))[:10]: + # Pinned Maestro JUnit stores failure.message separately from UI hierarchy. + first_line = ''.join(failure.itertext()).splitlines()[:1] + result['testcase_failures'].extend(maestro_failure_details('\n'.join(first_line))) + return result + + collect('maestro', maestro_details) + collect('maestro_preflight', maestro_preflight_diagnostics) + collect('hierarchy_signals', lambda: startup_hierarchy_signals(PRIVATE / 'maestro-output')) + collect('metro_errors', lambda: metro_startup_errors(private_text(PRIVATE / 'metro.log'))) + collect('metro_progress', lambda: metro_startup_progress(private_log_prefix(PRIVATE / 'metro.log')[0])) + if PLATFORM == 'ANDROID' and device: + def android_details(): + args = ['adb', '-s', device, 'logcat', '-d', '-t', '300', '-v', 'brief', + 'AndroidRuntime:E', 'ReactNativeJS:E', 'ReactNative:E', '*:S'] + path = PRIVATE / 'android-startup-errors.log' + with path.open('wb') as output: + try: + result = subprocess.run(args, stdout=output, stderr=subprocess.STDOUT, + timeout=20, check=False) + code = result.returncode + except subprocess.TimeoutExpired: + code = 124 + return {'command': args, 'exit_code': code, + 'errors': android_startup_errors(private_text(path)) if code == 0 else []} + collect('android_errors', android_details) + elif PLATFORM == 'IOS' and device: + def ios_details(): + executable = read()['built_artifact']['executable'] + if not re.fullmatch(r'[A-Za-z0-9_][A-Za-z0-9_.-]*', executable): + raise ValueError('Unsafe iOS executable selector.') + predicate = ('process == "%s" AND (messageType == "error" OR messageType == "fault" ' + 'OR eventMessage CONTAINS[c] "error" OR eventMessage CONTAINS[c] "exception" ' + 'OR eventMessage CONTAINS[c] "fatal")') % executable + args = ['xcrun', 'simctl', 'spawn', device, 'log', 'show', '--style', 'compact', + '--last', '10m', '--predicate', predicate] + path = PRIVATE / 'ios-startup-errors.log' + # Keep log/xcrun notices private and separate from compact records. + with path.open('wb') as output, (PRIVATE / 'ios-startup-errors-stderr.log').open('wb') as stderr: + try: + result = subprocess.run(args, stdout=output, stderr=stderr, + timeout=25, check=False) + code = result.returncode + except subprocess.TimeoutExpired: + code = 124 + result = {'command': args, 'exit_code': code, 'errors': []} + if code == 0: + try: + payload, truncated = private_log_prefix(path) + result.update(ios_startup_diagnostics(payload, executable)) + result['input_truncated'] = truncated + except (ValueError, TypeError) as error: + result['parse_error'] = type(error).__name__ + return result + collect('ios_errors', ios_details) + data = read() + data['startup_diagnostics'] = details + save(data) + print('Bounded startup diagnostics: ' + json.dumps(details), flush=True) + + +def command(args, label, timeout=120, cwd=ROOT, capture=False, error_details=True): + """Record exact nonsecret argv and status; keep all process output private.""" + PRIVATE.mkdir(parents=True, exist_ok=True) + data = read() + entry = {'label': label, 'command': args, 'timeout_seconds': timeout} + data['commands'].append(entry) + save(data) + started = time.monotonic() + output_file = PRIVATE / ('command-%03d.log' % len(data['commands'])) + print(label, flush=True) + try: + with output_file.open('wb') as output: + proc = subprocess.Popen(args, cwd=cwd, stdout=output, stderr=subprocess.STDOUT, + start_new_session=True) + try: + code = proc.wait(timeout=timeout) + except subprocess.TimeoutExpired: + os.killpg(proc.pid, signal.SIGKILL) + proc.wait() + code = 124 + except OSError: + code = 127 + entry['exit_code'] = code + entry['duration_seconds'] = round(time.monotonic() - started, 1) + if code: + # Fixed diagnostics expose install blockers without publishing arbitrary logs. + log = output_file.read_text(errors='replace') if output_file.exists() else '' + known = { + 'lockfile needs to be updated': 'Manifest and yarn.lock are inconsistent; repair the lockfile separately.', + 'could not find compatible versions': 'The native dependency lock is incompatible with the manifest.', + 'no space left on device': 'The hosted runner ran out of disk space.', + 'command not found': 'A required native tool is unavailable.', + 'requires a higher minimum deployment target': 'A pod requires a higher iOS deployment target.', + } + entry['diagnostic'] = next((message for needle, message in known.items() + if needle in log.lower()), 'Inspect this command locally; raw output remains private to the runner.') + # Keep the schema-1 field name for consumers; values are now fixed + # categories, never redacted excerpts or arbitrary exception messages. + entry['redacted_tool_errors'] = failure_details(log) if error_details else [] + for line in entry['redacted_tool_errors']: + print(line, flush=True) + save(data) + raise RuntimeError('%s failed (exit %s). %s' % (label, code, entry['diagnostic'])) + save(data) + return output_file.read_text(errors='replace').strip() if capture else None + + +def record(): + source = subprocess.check_output(['git', 'rev-parse', 'HEAD'], cwd=ROOT, text=True).strip() + if source != os.environ.get('KEEPER_SOURCE_SHA'): + raise RuntimeError('Checkout does not match the exact requested PR head.') + save({ + 'schema': 1, 'platform': PLATFORM, 'source_sha': source, + 'workflow_ref': os.environ.get('GITHUB_WORKFLOW_REF'), + 'workflow_sha': os.environ.get('GITHUB_WORKFLOW_SHA'), + 'run_id': os.environ.get('GITHUB_RUN_ID'), 'run_attempt': os.environ.get('GITHUB_RUN_ATTEMPT'), + 'runner': {key: os.environ.get(key) for key in + ['RUNNER_OS', 'RUNNER_ARCH', 'ImageOS', 'ImageVersion']}, + 'os': platform.platform(), 'commands': [], 'build': 'NOT RUN', 'smoke': 'NOT RUN', + 'hardware': 'NOT RUN', 'signing': 'NOT RUN', 'app_id': APP_IDS[PLATFORM], + 'smoke_scope': 'Fresh install; render Create a passcode; type four disposable digits; assert Confirm your passcode; stop before persisting credentials.', + 'startup_timing_scope': 'Bounded cold development bundle/startup allowance; no release performance acceptance.', + 'artifact_policy': 'Metadata only. No raw logs, screenshots, app binaries, simulator data or credentials.', + }) + versions = { + 'node': command(['node', '--version'], 'Record Node version', capture=True), + 'java': command(['java', '-version'], 'Record Java version', capture=True), + } + if PLATFORM == 'IOS': + versions['xcode'] = command(['xcodebuild', '-version'], 'Record Xcode version', capture=True) + versions['sdk'] = command(['xcrun', '--sdk', 'iphonesimulator', '--show-sdk-version'], + 'Record iOS Simulator SDK', capture=True) + else: + versions['expected_sdk_build'] = 'compileSdk=36; buildTools=35.0.0; NDK=27.1.12297006' + versions['expected_emulator_target'] = 'Android API 35; google_apis; x86_64; pixel_6' + data = read() + data['toolchain'] = versions + save(data) + + +def fixtures(): + # Empty values would fall back to real development services in config.ts. + overlays = ['.env.ci', 'firebase.json'] + (ROOT / '.env.ci').write_text('''ENVIRONMENT=DEVELOPMENT +RELAY=https://127.0.0.1:9/ +SIGNING_SERVER_TESTNET=https://127.0.0.1:9/ +SIGNING_SERVER_MAINNET=https://127.0.0.1:9/ +CHANNEL_URL=https://127.0.0.1:9/ +SENTRY_DNS=https://public@127.0.0.1:9/1 +ENC_KEY_STORAGE_IDENTIFIER=KEEPER-HOSTED-DISPOSABLE +''') + (ROOT / 'firebase.json').write_text(json.dumps({'react-native': { + 'app_data_collection_default_enabled': False, + 'analytics_auto_collection_enabled': False, + 'analytics_collection_deactivated': True, + 'messaging_auto_init_enabled': False, + }}) + '\n') + if PLATFORM == 'IOS': + import plistlib + for path, bundle in [('ios/GoogleService-Info.plist', 'io.hexawallet.keeper'), + ('ios/GoogleServices/dev/GoogleService-Info.plist', APP_IDS['IOS'])]: + overlays.append(path) + with (ROOT / path).open('wb') as output: + plistlib.dump({'API_KEY': 'AIzaSy' + '0' * 33, 'GCM_SENDER_ID': '123456789012', + 'PLIST_VERSION': '1', 'BUNDLE_ID': bundle, + 'PROJECT_ID': 'keeper-disposable-ci', + 'GOOGLE_APP_ID': '1:123456789012:ios:0000000000000000000000', + 'IS_GCM_ENABLED': False, 'IS_ANALYTICS_ENABLED': False, + 'IS_ADS_ENABLED': False, 'IS_SIGNIN_ENABLED': False}, output) + else: + overlays.append('android/app/google-services.json') + (ROOT / 'android/app/google-services.json').write_text(json.dumps({ + 'project_info': {'project_number': '123456789012', 'project_id': 'keeper-disposable-ci'}, + 'client': [{'client_info': {'mobilesdk_app_id': '1:123456789012:android:0000000000000000', + 'android_client_info': {'package_name': APP_IDS['ANDROID']}}, + 'api_key': [{'current_key': 'AIzaSy' + '0' * 33}]}], + 'configuration_version': '1', + }) + '\n') + data = read() + data['configuration'] = 'Synthetic Firebase project; DEVELOPMENT/testnet; loopback-only configured services; no production secrets.' + data['configuration_overlays'] = [{'path': path, 'sha256': hashlib.sha256((ROOT / path).read_bytes()).hexdigest()} + for path in overlays] + save(data) + + +def install_maestro(): + archive = PRIVATE / 'maestro.zip' + url = 'https://github.com/mobile-dev-inc/Maestro/releases/download/%s/maestro.zip' % MAESTRO_VERSION + command(['curl', '--fail', '--location', '--silent', '--show-error', '--max-time', '120', + '--output', str(archive), url], 'Download pinned Maestro', timeout=130) + if hashlib.sha256(archive.read_bytes()).hexdigest() != MAESTRO_SHA256: + raise RuntimeError('Maestro archive checksum mismatch.') + # unzip preserves executable modes for the bundled native iOS driver; + # Python ZipFile extraction would silently strip those modes. + command(['unzip', '-q', str(archive), '-d', str(PRIVATE / 'maestro')], + 'Extract verified Maestro including native executable modes', timeout=120) + candidates = list((PRIVATE / 'maestro').rglob('bin/maestro')) + if len(candidates) != 1: + raise RuntimeError('Unexpected Maestro archive layout.') + data = read() + data['maestro'] = {'version': MAESTRO_VERSION, 'sha256': MAESTRO_SHA256, 'binary': str(candidates[0])} + save(data) + + +def android_sdk_metadata(sdk, component): + properties = (sdk / component / 'source.properties').read_text() + values = dict((key.strip(), value.strip()) for line in properties.splitlines() + if '=' in line for key, value in [line.split('=', 1)]) + if not values.get('Pkg.Revision'): + raise RuntimeError('Android SDK component %s has no Pkg.Revision metadata.' % component) + return {key: value for key, value in values.items() + if key in ['Pkg.Revision', 'AndroidVersion.ApiLevel']} + + +def prepare(): + command(['npm', 'install', '--global', 'yarn@1.22.22', '--ignore-scripts'], 'Install pinned Yarn', timeout=180) + command(['yarn', 'install', '--frozen-lockfile', '--ignore-scripts', '--non-interactive'], + 'Install immutable JavaScript dependencies', timeout=900) + command(['node_modules/.bin/patch-package', '--error-on-fail'], 'Apply repository dependency patches', timeout=180) + original_manifest = (ROOT / 'package.json').read_bytes() + command(['node', '.github/native/nodeify.cjs'], + 'Apply committed Node compatibility whitelist without installing dependencies', timeout=180) + installed_manifest = (ROOT / 'package.json').read_bytes() + original = json.loads(original_manifest) + installed = json.loads(installed_manifest) + for key in ['browser', 'react-native', 'browserify']: + original.pop(key, None) + installed.pop(key, None) + if original != installed: + raise RuntimeError('Node compatibility setup changed fields beyond module mappings.') + data = read() + data['nodeify_overlay'] = { + 'path': 'package.json', 'before_sha256': hashlib.sha256(original_manifest).hexdigest(), + 'after_sha256': hashlib.sha256(installed_manifest).hexdigest(), + 'diff_sha256': hashlib.sha256(subprocess.check_output(['git', 'diff', 'HEAD', '--', 'package.json'], cwd=ROOT)).hexdigest(), + 'allowed_fields': ['browser', 'react-native', 'browserify'], + } + save(data) + command(['git', 'diff', '--exit-code', 'HEAD', '--', 'yarn.lock', 'ios/Podfile.lock'], + 'Verify dependency locks remain immutable') + fixtures() + install_maestro() + if PLATFORM == 'IOS': + command(['gem', 'install', 'cocoapods', '--version', '1.16.2', '--no-document'], + 'Install pinned CocoaPods', timeout=300) + if os.environ.get('KEEPER_POD_LOCK_REPAIR') == '1': + # Diagnostic bootstrap only. A candidate lock is reviewed/committed, + # then a separate strict deployment run must build and test the app. + command(['pod', 'update', 'NitroIap', 'NitroModules', 'openiap', '--no-repo-update'], + 'Generate targeted CocoaPods lock candidate (not validation)', + timeout=900, cwd=ROOT / 'ios') + shutil.copyfile(ROOT / 'ios/Podfile.lock', EVIDENCE.parent / 'Podfile.lock.candidate') + raise RuntimeError('Pod lock candidate generated for review; no native validation performed.') + command(['pod', 'install', '--deployment'], 'Install immutable CocoaPods dependencies', + timeout=900, cwd=ROOT / 'ios') + # Existing PBX build phases hardcode this path. Adapt the disposable host, + # preserving the app project and the source commit being tested. + command(['sudo', 'mkdir', '-p', '/opt/homebrew/bin'], 'Prepare existing Xcode Node path') + command(['sudo', 'ln', '-sf', shutil.which('node'), '/opt/homebrew/bin/node'], + 'Connect existing Xcode Node path to pinned Node') + else: + command(['sdkmanager', 'platforms;android-36', 'build-tools;35.0.0', 'ndk;27.1.12297006'], + 'Install pinned Android build SDK and NDK', timeout=600) + sdk = Path(os.environ.get('ANDROID_HOME', os.environ.get('ANDROID_SDK_ROOT', ''))) + observed = {} + for component in ['platforms/android-36', 'build-tools/35.0.0', 'ndk/27.1.12297006']: + observed[component] = android_sdk_metadata(sdk, component) + data = read() + data['toolchain']['observed_android_sdk'] = observed + save(data) + if (ROOT / 'package.json').read_bytes() != installed_manifest: + raise RuntimeError('Native installation unexpectedly rewrote the installed manifest.') + command(['git', 'diff', '--exit-code', 'HEAD', '--', 'yarn.lock', 'ios/Podfile.lock'], + 'Verify native installation did not rewrite locks') + + +def artifact_identity(): + if PLATFORM == 'IOS': + import plistlib + bundle = PRIVATE / 'DerivedData/Build/Products/Debug-iphonesimulator/hexa_keeper_dev.app' + with (bundle / 'Info.plist').open('rb') as source: + executable = plistlib.load(source)['CFBundleExecutable'] + binary = bundle / executable + tree = hashlib.sha256() + for file in sorted(path for path in bundle.rglob('*') if path.is_file()): + tree.update(str(file.relative_to(bundle)).encode() + b'\0') + tree.update(hashlib.sha256(file.read_bytes()).digest()) + return {'path': str(bundle), 'executable': executable, + 'executable_sha256': hashlib.sha256(binary.read_bytes()).hexdigest(), + 'bundle_tree_sha256': tree.hexdigest()} + apk = ROOT / 'android/app/build/outputs/apk/development/debug/app-development-debug.apk' + return {'path': str(apk.relative_to(ROOT)), 'sha256': hashlib.sha256(apk.read_bytes()).hexdigest()} + + +def driver(): + """Fail early on iOS driver setup without installing or crediting the app.""" + if PLATFORM != 'IOS': + raise ValueError('The disposable driver preflight is iOS-only.') + state = PRIVATE / 'maestro-preflight-state' + os.environ['XDG_STATE_HOME'] = str(state) + details = {'scope': 'DRIVER PREFLIGHT/setup only; no app build, install or UI credit.', + 'result': 'FAIL', 'cleanup_errors': []} + device = None + original_error = None + label = 'Check disposable iOS driver readiness' + try: + created = command(['xcrun', 'simctl', 'create', 'Keeper-driver-preflight-ci', + 'com.apple.CoreSimulator.SimDeviceType.iPhone-16', + 'com.apple.CoreSimulator.SimRuntime.iOS-18-5'], + 'Create owned driver-preflight simulator', timeout=120, capture=True) + if not re.fullmatch(r'[A-Fa-f0-9]{8}(?:-[A-Fa-f0-9]{4}){3}-[A-Fa-f0-9]{12}', created): + raise ValueError('simctl create did not return an owned simulator UUID.') + device = created + command(['xcrun', 'simctl', 'boot', device], 'Boot owned driver-preflight simulator', timeout=120) + command(['xcrun', 'simctl', 'bootstatus', device, '-b'], + 'Wait for driver-preflight simulator boot', timeout=240) + command([read()['maestro']['binary'], '--platform', 'ios', '--device', device, 'hierarchy'], + label, timeout=300, error_details=False) + details['result'] = 'PASS' + except Exception as error: + original_error = error + details['error_type'] = type(error).__name__ + raise + finally: + if device: + for action in ['shutdown', 'delete']: + try: + subprocess.run(['xcrun', 'simctl', action, device], stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, timeout=30, check=True) + except Exception as error: + details['cleanup_errors'].append({'action': action, 'error_type': type(error).__name__}) + try: + details['diagnostics'] = maestro_preflight_diagnostics(state / 'maestro/tests', label) + except Exception as error: + details['diagnostics'] = {'collection_error_types': [type(error).__name__]} + if details['cleanup_errors']: + details['result'] = 'FAIL' + data = read() + data['driver_preflight'] = details + save(data) + if details['cleanup_errors'] and original_error is None: + raise RuntimeError('Owned driver-preflight simulator cleanup failed.') + + +def build(): + if PLATFORM == 'IOS': + architecture = platform.machine() + if architecture not in ['arm64', 'x86_64']: + raise RuntimeError('Unsupported hosted iOS simulator architecture: %s' % architecture) + data = read() + data.setdefault('toolchain', {})['ios_simulator_architecture'] = architecture + save(data) + command(['xcodebuild', '-workspace', 'ios/hexa_keeper.xcworkspace', '-scheme', 'hexa_keeper_dev', + '-configuration', 'Debug', '-sdk', 'iphonesimulator', '-destination', 'generic/platform=iOS Simulator', + '-derivedDataPath', str(PRIVATE / 'DerivedData'), 'CODE_SIGNING_ALLOWED=NO', + 'CODE_SIGNING_REQUIRED=NO', 'ARCHS=' + architecture, 'ONLY_ACTIVE_ARCH=YES', 'build'], + 'Build unsigned development simulator app', timeout=2400) + else: + observed = command(['./gradlew', '--version'], 'Record actual Gradle and JVM version', + capture=True, cwd=ROOT / 'android') + data = read() + data['toolchain']['gradle'] = observed + save(data) + command(['./gradlew', ':app:assembleDevelopmentDebug', '--no-daemon', '--max-workers=2', + '-PreactNativeArchitectures=x86_64'], 'Build debug-key-only development APK', + timeout=2100, cwd=ROOT / 'android') + data = read() + data['built_artifact'] = artifact_identity() + data['build'] = 'PASS' + save(data) + + +def warm_metro_bundle(): + """Bound cold development bundling before starting either disposable app.""" + bundle_platform = {'IOS': 'ios', 'ANDROID': 'android'}.get(PLATFORM) + if bundle_platform is None: + raise ValueError('Unsupported native bundle platform.') + url = 'http://127.0.0.1:8081/index.bundle?platform=%s&dev=true&minify=false' % bundle_platform + body = PRIVATE / ('%s-development-warmup.bundle' % bundle_platform) + details = {'scope': 'Development bundle setup only; not native or UI evidence.', + 'platform': bundle_platform, 'url': url, 'timeout_seconds': 240, + 'result': 'FAIL', 'http_status': None} + started = time.monotonic() + try: + status = command(['curl', '--fail', '--silent', '--show-error', '--max-time', '240', + '--output', str(body), '--write-out', '%{http_code}', url], + 'Warm %s development bundle (setup only)' % PLATFORM, timeout=250, capture=True) + if re.fullmatch(r'\d{3}', status): + details['http_status'] = int(status) + if status != '200' or not body.is_file() or body.stat().st_size == 0: + raise RuntimeError('Development bundle warmup did not return a nonempty HTTP 200 body.') + digest = hashlib.sha256() + with body.open('rb') as source: + for chunk in iter(lambda: source.read(1024 * 1024), b''): + digest.update(chunk) + details.update({'result': 'PASS', 'bytes': body.stat().st_size, 'sha256': digest.hexdigest()}) + finally: + details['duration_seconds'] = round(time.monotonic() - started, 1) + data = read() + data['metro_warmup'] = details + save(data) + + +def smoke(): + if read()['build'] != 'PASS': + raise RuntimeError('A successful native build is required before smoke testing.') + if artifact_identity() != read()['built_artifact']: + raise RuntimeError('Built artifact changed before the disposable startup smoke.') + metro_output = (PRIVATE / 'metro.log').open('wb') + metro = subprocess.Popen(['node', 'node_modules/react-native/cli.js', 'start', '--port', '8081'], + cwd=ROOT, stdout=metro_output, stderr=subprocess.STDOUT, start_new_session=True) + device = None + report = PRIVATE / 'smoke-junit.xml' + try: + for _ in range(60): + if metro.poll() is not None: + raise RuntimeError('Metro exited before the disposable smoke.') + try: + with urllib.request.urlopen('http://127.0.0.1:8081/status', timeout=2) as response: + if b'packager-status:running' in response.read(): + break + except OSError: + time.sleep(1) + else: + raise RuntimeError('Metro did not become ready within 60 attempts.') + warm_metro_bundle() + if PLATFORM == 'IOS': + device = command(['xcrun', 'simctl', 'create', 'Keeper-disposable-ci', + 'com.apple.CoreSimulator.SimDeviceType.iPhone-16', + 'com.apple.CoreSimulator.SimRuntime.iOS-18-5'], + 'Create fresh iPhone 16 iOS 18.5 simulator', capture=True) + command(['xcrun', 'simctl', 'boot', device], 'Boot disposable simulator') + command(['xcrun', 'simctl', 'bootstatus', device, '-b'], 'Wait for simulator boot', timeout=240) + command(['xcrun', 'simctl', 'install', device, + str(PRIVATE / 'DerivedData/Build/Products/Debug-iphonesimulator/hexa_keeper_dev.app')], + 'Install unsigned development app', timeout=120) + descriptor = 'iPhone 16; iOS 18.5' + else: + sdk = Path(os.environ.get('ANDROID_HOME', os.environ.get('ANDROID_SDK_ROOT', ''))) + # Query the installed SDK package without loading emulator GUI libraries + # on the headless host. The emulator runner already booted this image. + emulator_version = android_sdk_metadata(sdk, 'emulator') + adb_version = command(['adb', 'version'], 'Record actual adb version', capture=True) + data = read() + data['toolchain']['emulator'] = {'source': 'emulator/source.properties', **emulator_version} + data['toolchain']['adb'] = adb_version + save(data) + device = command(['adb', 'get-serialno'], 'Identify fresh emulator', capture=True) + command(['adb', '-s', device, 'reverse', 'tcp:8081', 'tcp:8081'], 'Connect emulator to Metro') + command(['adb', '-s', device, 'install', '-r', + 'android/app/build/outputs/apk/development/debug/app-development-debug.apk'], + 'Install development debug APK', timeout=120) + descriptor = command(['adb', '-s', device, 'shell', 'getprop', 'ro.build.fingerprint'], + 'Record Android emulator image', capture=True) + data = read() + data['device'] = {'id': device, 'image': descriptor, 'fresh_disposable_state': True} + data['installed_artifact_verified'] = True + save(data) + command([data['maestro']['binary'], '--device', device, 'test', '--env', 'APP_ID=' + APP_IDS[PLATFORM], + '--flatten-debug-output', '--debug-output', str(PRIVATE / 'maestro-output'), + '--format', 'junit', '--output', str(report), '--test-output-dir', str(PRIVATE / 'maestro-output'), + '.github/native/startup.yaml'], 'Assert startup and disposable passcode transition', timeout=420) + suite = ET.parse(report).getroot() + cases = list(suite.iter('testcase')) + if not cases or any(list(case.iter('failure')) or list(case.iter('error')) or list(case.iter('skipped')) + for case in cases): + raise RuntimeError('Maestro did not produce a complete successful smoke report.') + data = read() + data['smoke'] = 'PASS' + data['smoke_test_cases'] = len(cases) + save(data) + except Exception: + try: + record_startup_failure(report, device) + except Exception as error: + # Diagnostics never replace the original failed native/UI result. + data = read() + data['startup_diagnostics'] = {'collection_errors': [ + {'stage': 'collector', 'error_type': type(error).__name__}]} + save(data) + raise + finally: + if metro.poll() is None: + os.killpg(metro.pid, signal.SIGTERM) + try: + metro.wait(timeout=10) + except subprocess.TimeoutExpired: + os.killpg(metro.pid, signal.SIGKILL) + metro_output.close() + if device and PLATFORM == 'IOS': + subprocess.run(['xcrun', 'simctl', 'shutdown', device], stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, timeout=30, check=False) + subprocess.run(['xcrun', 'simctl', 'delete', device], stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, timeout=30, check=False) + elif device: + subprocess.run(['adb', '-s', device, 'uninstall', APP_IDS[PLATFORM]], stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, timeout=30, check=False) + + +def main(): + if PLATFORM not in APP_IDS or len(sys.argv) != 2 or sys.argv[1] not in ['record', 'prepare', 'driver', 'build', 'smoke']: + raise RuntimeError('Expected a hosted platform and record|prepare|driver|build|smoke.') + os.environ.update({'ENVFILE': '.env.ci', 'RCT_NO_LAUNCH_PACKAGER': '1', + 'RCT_NEW_ARCH_ENABLED': '1', 'MAESTRO_CLI_NO_ANALYTICS': '1'}) + run_phase(sys.argv[1]) + + +def run_phase(phase): + try: + globals()[phase]() + except Exception: + if EVIDENCE.exists(): + evidence = read() + evidence['failed_phase'] = phase + if phase in ['build', 'smoke']: + evidence[phase] = 'FAIL' + save(evidence) + raise + + +if __name__ == '__main__': + try: + main() + except Exception: + # Unexpected exceptions can contain private paths or process output too. + print('Hosted native validation failed; inspect phase status and fixed diagnostic categories.', file=sys.stderr) + sys.exit(1) diff --git a/.github/native/nodeify.cjs b/.github/native/nodeify.cjs new file mode 100644 index 000000000..d24127bf1 --- /dev/null +++ b/.github/native/nodeify.cjs @@ -0,0 +1,33 @@ +#!/usr/bin/env node +'use strict'; + +const path = require('node:path'); + +// Match setup.sh's existing polyfills without invoking its dependency installer. +// The unfiltered --hack command selects every core shim, including packages this +// app does not depend on, and makes valid imports such as constants unresolvable. +const SHIMS = Object.freeze([ + 'buffer', 'events', 'process', 'stream', 'inherits', 'path', 'assert', 'crypto', +]); + +function runNodeify(root = path.resolve(__dirname, '../..')) { + if (process.cwd() !== root) { + throw new Error('Run native Node compatibility preparation from the repository root.'); + } + const cli = require.resolve('rn-nodeify/cmd.js', { paths: [root] }); + const coreList = require.resolve(path.join(path.dirname(cli), 'coreList.js')); + require(coreList); + require.cache[coreList].exports = [...SHIMS]; + const originalArguments = process.argv; + try { + // No --install flag: the pinned upstream CLI can apply its existing module + // and file compatibility fixes, but cannot enter installShims(). + process.argv = [process.execPath, cli, '--hack']; + require(cli); + } finally { + process.argv = originalArguments; + } +} + +module.exports = { runNodeify, SHIMS }; +if (require.main === module) runNodeify(); diff --git a/.github/native/startup.yaml b/.github/native/startup.yaml new file mode 100644 index 000000000..1dc939637 --- /dev/null +++ b/.github/native/startup.yaml @@ -0,0 +1,22 @@ +# Intentionally stop before persisting a PIN or generating any recovery material. +appId: ${APP_ID} +name: Disposable startup and passcode input +--- +- launchApp: + clearState: true + permissions: + all: deny +- extendedWaitUntil: + visible: Create a passcode + timeout: 180000 +- assertNotVisible: Confirm your passcode +- tapOn: + id: key_1 +- tapOn: + id: key_2 +- tapOn: + id: key_3 +- tapOn: + id: key_4 +- assertVisible: Confirm your passcode +- stopApp diff --git a/.github/tests/keeper-local-validation.test.cjs b/.github/tests/keeper-local-validation.test.cjs index 3de7d7994..959b8a6a7 100644 --- a/.github/tests/keeper-local-validation.test.cjs +++ b/.github/tests/keeper-local-validation.test.cjs @@ -15,6 +15,12 @@ const runWorkflow = new (Object.getPrototypeOf(async function () {}).constructor ); const sha = 'b78b480eb4223390b2d17073387f9887d2e35f95'; +const baseSha = 'a'.repeat(40); +const runBaseSha = 'c'.repeat(40); +const repository = 'KeeperCommunity/bitcoin-keeper'; +const repositoryId = 470904030; +const headRef = 'codex/example-change'; +const nativeWorkflowPath = '.github/workflows/keeper-hosted-native.yml'; const prNumber = 7045; const comment = (body, login) => ({ body, user: { login } }); const handoff = (head = sha, checks = 'IOS, ANDROID, HARDWARE') => comment( @@ -41,27 +47,102 @@ Real devices/signers: ${overrides.hardware || 'NOT RUN / POST-RELEASE by owner d 'local-writer' ); +const hostedRun = (overrides = {}) => ({ + id: 90, + workflow_id: 123, + path: nativeWorkflowPath, + head_sha: sha, + head_branch: headRef, + head_repository: { full_name: repository, id: repositoryId }, + repository: { full_name: repository, id: repositoryId }, + event: 'pull_request', + status: 'completed', + conclusion: 'success', + run_attempt: 2, + html_url: `https://github.com/${repository}/actions/runs/90`, + pull_requests: [{ + number: prNumber, + head: { sha, ref: headRef, repo: { id: repositoryId } }, + base: { sha: runBaseSha, ref: 'sprint', repo: { id: repositoryId } } + }], + ...overrides +}); +const hostedJob = (platform, overrides = {}) => ({ + name: platform === 'IOS' ? 'Hosted iOS simulator' : 'Hosted Android emulator', + head_sha: sha, + run_id: 90, + run_attempt: 2, + status: 'completed', + conclusion: 'success', + steps: ['Build development app', 'Run disposable startup smoke'].map(name => ({ + name, status: 'completed', conclusion: 'success' + })), + ...overrides +}); +const trustedTree = () => ({ + truncated: false, + tree: [ + { path: nativeWorkflowPath, mode: '100644', type: 'blob', sha: '1'.repeat(40) }, + { path: '.github/native', mode: '040000', type: 'tree', sha: '2'.repeat(40) }, + { path: '.github/native/smoke.yaml', mode: '100644', type: 'blob', sha: '3'.repeat(40) }, + { path: '.github/native/build.sh', mode: '100755', type: 'blob', sha: '4'.repeat(40) }, + { path: 'src/App.tsx', mode: '100644', type: 'blob', sha: '5'.repeat(40) } + ] +}); +const hostedOptions = (overrides = {}) => ({ + runs: [hostedRun()], jobs: [hostedJob('IOS'), hostedJob('ANDROID')], ...overrides +}); + async function evaluate(comments, options = {}) { const filesRoute = () => {}; const commentsRoute = () => {}; + const runsRoute = () => {}; + const jobsRoute = () => {}; const statuses = []; const failures = []; + const warnings = []; + const calls = []; + const pullsRequested = []; + let pullReads = 0; const github = { - paginate: async route => { + paginate: async (route, args) => { + calls.push({ route, args }); if (route === filesRoute) return options.files || [{ filename: 'src/App.tsx' }]; if (route === commentsRoute) return comments; + if (route === runsRoute) { + if (options.runsError) throw new Error(options.runsError); + return [...(options.runs || [])]; + } + if (route === jobsRoute) { + if (options.jobsError) throw new Error(options.jobsError); + return options.jobsByRun?.[args.run_id] || options.jobs || []; + } throw new Error('Unexpected paginate route'); }, rest: { pulls: { - get: async () => ({ data: { - base: { ref: 'sprint' }, - head: { sha: options.head || sha }, - body: '', user: { login: 'cloud-writer' }, - html_url: 'https://github.com/KeeperCommunity/bitcoin-keeper/pull/7045' - } }), + get: async ({ pull_number }) => { + pullsRequested.push(pull_number); + pullReads += 1; + return { data: { + base: { ref: options.baseRef || 'sprint', sha: baseSha, + repo: { full_name: repository, id: repositoryId } }, + head: { sha: pullReads > 1 && options.afterHead ? options.afterHead : options.head || sha, + ref: headRef, repo: options.headRepository || { full_name: repository, id: repositoryId } }, + body: options.body || '', user: { login: 'cloud-writer' }, + draft: options.draft ?? false, + html_url: `https://github.com/${repository}/pull/${pull_number}` + } }; + }, listFiles: filesRoute }, + actions: { listWorkflowRuns: runsRoute, listJobsForWorkflowRunAttempt: jobsRoute }, + git: { + getTree: async ({ tree_sha }) => { + if (options.treeErrors?.includes(tree_sha)) throw new Error('Tree unavailable'); + return { data: options.trees?.[tree_sha] || trustedTree() }; + } + }, issues: { listComments: commentsRoute }, repos: { getCollaboratorPermissionLevel: async ({ username }) => ({ data: { @@ -74,11 +155,14 @@ async function evaluate(comments, options = {}) { } }; const context = { repo: { owner: 'KeeperCommunity', repo: 'bitcoin-keeper' }, - issue: { number: prNumber }, payload: {} }; - const core = { info: () => {}, warning: () => {}, setFailed: text => failures.push(text) }; + issue: { number: prNumber }, payload: {}, ...options.context }; + const core = { info: () => {}, warning: text => warnings.push(text), + setFailed: text => failures.push(text) }; await runWorkflow(github, context, core); - assert.equal(statuses.length, 1); - return { status: statuses[0], failures }; + assert.equal(statuses.length, options.afterHead || options.baseRef ? 0 : 1); + return { status: statuses[0], statuses, failures, warnings, calls, pullsRequested, + runsCalls: calls.filter(c => c.route === runsRoute), + jobsCalls: calls.filter(c => c.route === jobsRoute) }; } test('current requirements still fail without an owner decision', async () => { @@ -158,3 +242,305 @@ test('a new PR head invalidates both handoff and owner decision', async () => { assert.equal(status.state, 'failure'); assert.equal(status.description, 'Trusted Cloud handoff for current SHA required'); }); + +test('trusted exact-head hosted smoke passes both platforms without a local attestation', async () => { + const result = await evaluate([handoff(sha, 'IOS, ANDROID')], hostedOptions()); + assert.equal(result.status.state, 'success'); + assert.match(result.status.description, /hosted IOS, ANDROID/); + assert.equal(result.status.sha, sha); + assert.equal(result.failures.length, 0); + assert.deepEqual(result.runsCalls[0].args, { + owner: 'KeeperCommunity', repo: 'bitcoin-keeper', workflow_id: 'keeper-hosted-native.yml', + head_sha: sha, event: 'pull_request', per_page: 100 + }); + assert.equal(result.jobsCalls[0].args.attempt_number, 2); +}); + +test('successful hosted Android can combine with local iOS when hosted iOS fails', async () => { + const { status } = await evaluate([ + handoff(sha, 'IOS, ANDROID'), report({ android: 'NOT RUN' }) + ], hostedOptions({ + runs: [hostedRun({ conclusion: 'failure' })], + jobs: [hostedJob('IOS', { conclusion: 'failure' }), hostedJob('ANDROID')] + })); + assert.equal(status.state, 'success'); + assert.match(status.description, /hosted ANDROID/); +}); + +test('one platform success cannot satisfy the other platform', async () => { + const { status } = await evaluate([handoff(sha, 'IOS, ANDROID')], hostedOptions({ + jobs: [hostedJob('ANDROID')] + })); + assert.equal(status.state, 'failure'); + assert.match(status.description, /IOS/); +}); + +test('hosted platform passes do not satisfy physical hardware checks', async () => { + const { status } = await evaluate([handoff()], hostedOptions()); + assert.equal(status.state, 'failure'); + assert.equal(status.description, 'Missing local PASS: HARDWARE'); +}); + +test('hosted passes can fill unrun platforms while exact owner rules defer hardware', async () => { + const { status } = await evaluate([handoff(), decision(), + report({ ios: 'NOT RUN', android: 'NOT RUN' })], hostedOptions()); + assert.equal(status.state, 'success'); + assert.match(status.description, /hosted IOS, ANDROID; owner-deferred HARDWARE/); +}); + +test('hosted success cannot waive hardware failures or remove later-report requirements', async () => { + for (const comments of [ + [handoff(), decision()], + [handoff(), report(), decision()], + [handoff(), decision(), report({ hardware: 'FAIL: incorrect signer address' })], + [handoff(), decision({}, 'other-writer'), report()] + ]) { + const { status } = await evaluate(comments, hostedOptions()); + assert.equal(status.state, 'failure'); + assert.match(status.description, /HARDWARE/); + } +}); + +test('latest trusted local failure or blocked report cannot be hidden by hosted passes', async () => { + for (const state of ['LOCAL_FAILED', 'BLOCKED']) { + const { status } = await evaluate([handoff(sha, 'IOS, ANDROID'), + report(), report({ state })], hostedOptions()); + assert.equal(status.state, 'failure'); + assert.equal(status.description, 'Latest local validation is not LOCAL_PASSED'); + } +}); + +test('explicit local platform failures cannot be relabeled as passed by hosted results', async () => { + for (const failedField of ['ios', 'android']) { + const { status } = await evaluate([handoff(sha, 'IOS, ANDROID'), + report({ [failedField]: 'FAIL: reproducible startup crash' })], hostedOptions()); + assert.equal(status.state, 'failure', failedField); + } +}); + +test('hosted passes still require a trusted current-head Cloud handoff', async () => { + for (const comments of [[], [handoff('d'.repeat(40), 'IOS, ANDROID')], + [comment(handoff(sha, 'IOS, ANDROID').body, 'read-only-user')]]) { + const result = await evaluate(comments, hostedOptions()); + assert.equal(result.status.state, 'failure'); + assert.equal(result.runsCalls.length, 0); + } +}); + +test('reject hosted runs with stale, wrong, missing or untrusted identity metadata', async () => { + const badRuns = [ + { head_sha: 'd'.repeat(40) }, + { head_branch: 'other-branch' }, + { head_repository: { full_name: 'attacker/bitcoin-keeper', id: 1 } }, + { path: '.github/workflows/build-only.yml' }, + { event: 'push' }, + { event: 'pull_request_target' }, + { pull_requests: [] }, + { pull_requests: [{ ...hostedRun().pull_requests[0], number: prNumber + 1 }] }, + { pull_requests: [{ ...hostedRun().pull_requests[0], + head: { sha: 'd'.repeat(40), ref: headRef, repo: { id: repositoryId } } }] }, + { pull_requests: [{ ...hostedRun().pull_requests[0], + base: { ref: 'sprint', repo: { id: repositoryId } } }] }, + { run_attempt: 0 }, + { run_attempt: '2' } + ]; + for (const change of badRuns) { + const { status } = await evaluate([handoff(sha, 'IOS, ANDROID')], + hostedOptions({ runs: [hostedRun(change)] })); + assert.equal(status.state, 'failure', JSON.stringify(change)); + } +}); + +test('reject hosted evidence whose recorded PR association has wrong refs or repositories', async () => { + const original = hostedRun().pull_requests[0]; + for (const change of [ + { base: { ...original.base, ref: 'dev' } }, + { base: { ...original.base, repo: { id: 1 } } }, + { head: { ...original.head, ref: 'other-branch' } }, + { head: { ...original.head, repo: { id: 1 } } } + ]) { + const { status } = await evaluate([handoff(sha, 'IOS, ANDROID')], + hostedOptions({ runs: [hostedRun({ pull_requests: [{ ...original, ...change }] })] })); + assert.equal(status.state, 'failure', JSON.stringify(change)); + } +}); + +test('fork-hosted runs retain local fallback but cannot supply trusted hosted evidence', async () => { + const options = hostedOptions({ headRepository: { full_name: 'contributor/bitcoin-keeper', id: 1 } }); + const hostedOnly = await evaluate([handoff(sha, 'IOS, ANDROID')], options); + assert.equal(hostedOnly.status.state, 'failure'); + const local = await evaluate([handoff(sha, 'IOS, ANDROID'), report()], options); + assert.equal(local.status.state, 'success'); +}); + +test('hosted CI requires matching workflow and entire native harness in source and both bases', async () => { + for (const ref of [sha, baseSha, runBaseSha]) { + for (const mutation of ['workflow', 'smoke', 'add', 'delete', 'mode']) { + const tree = trustedTree(); + if (mutation === 'workflow') tree.tree[0].sha = 'f'.repeat(40); + if (mutation === 'smoke') tree.tree[2].sha = 'f'.repeat(40); + if (mutation === 'add') tree.tree.push({ + path: '.github/native/extra.sh', mode: '100755', type: 'blob', sha: 'f'.repeat(40) + }); + if (mutation === 'delete') tree.tree.splice(2, 1); + if (mutation === 'mode') tree.tree[3].mode = '100644'; + const { status } = await evaluate([handoff(sha, 'IOS, ANDROID')], + hostedOptions({ trees: { [ref]: tree } })); + assert.equal(status.state, 'failure', `${ref}: ${mutation}`); + } + } +}); + +test('unrelated source changes do not invalidate immutable hosted CI definitions', async () => { + const tree = trustedTree(); + tree.tree[4].sha = 'f'.repeat(40); + const { status } = await evaluate([handoff(sha, 'IOS, ANDROID')], + hostedOptions({ trees: { [sha]: tree } })); + assert.equal(status.state, 'success'); +}); + +test('bootstrap without trusted native infrastructure and truncated trees fail closed', async () => { + for (const tree of [{ truncated: false, tree: [] }, + { ...trustedTree(), truncated: true }, + { truncated: false, tree: trustedTree().tree.filter(e => e.path !== nativeWorkflowPath) }, + { truncated: false, tree: trustedTree().tree.filter(e => !e.path.startsWith('.github/native/')) }]) { + const { status } = await evaluate([handoff(sha, 'IOS, ANDROID')], + hostedOptions({ trees: { [baseSha]: tree } })); + assert.equal(status.state, 'failure'); + } +}); + +test('hosted API failures never become a pass and preserve genuine local fallback', async () => { + for (const failure of [{ runsError: 'Actions unavailable' }, + { jobsError: 'Jobs unavailable' }, { treeErrors: [runBaseSha] }]) { + const options = hostedOptions(failure); + const hostedOnly = await evaluate([handoff(sha, 'IOS, ANDROID')], options); + assert.equal(hostedOnly.status.state, 'failure'); + assert.ok(hostedOnly.warnings.length); + const local = await evaluate([handoff(sha, 'IOS, ANDROID'), report()], options); + assert.equal(local.status.state, 'success'); + } +}); + +test('cancelled, skipped, pending or incomplete hosted runs provide no platform evidence', async () => { + for (const change of [{ status: 'queued', conclusion: null }, + { status: 'in_progress', conclusion: null }, { conclusion: 'cancelled' }, + { conclusion: 'skipped' }, { conclusion: 'timed_out' }, { conclusion: 'action_required' }, + { conclusion: 'neutral' }, { conclusion: null }]) { + const { status } = await evaluate([handoff(sha, 'IOS, ANDROID')], + hostedOptions({ runs: [hostedRun(change)] })); + assert.equal(status.state, 'failure', JSON.stringify(change)); + } +}); + +test('a newer failed or pending run supersedes an older successful run regardless of API order', async () => { + for (const newest of [hostedRun({ id: 91, conclusion: 'cancelled' }), + hostedRun({ id: 91, status: 'in_progress', conclusion: null }), + hostedRun({ id: 91, conclusion: 'failure' })]) { + const { status, jobsCalls } = await evaluate([handoff(sha, 'IOS, ANDROID')], hostedOptions({ + runs: [hostedRun(), newest], jobsByRun: { 91: [] } + })); + assert.equal(status.state, 'failure'); + assert.ok(jobsCalls.every(call => call.args.run_id === 91)); + } +}); + +test('reruns inspect only the newest attempt and do not reuse earlier success', async () => { + const result = await evaluate([handoff(sha, 'IOS, ANDROID')], hostedOptions({ + runs: [hostedRun({ run_attempt: 3 })], jobs: [hostedJob('IOS'), hostedJob('ANDROID')] + })); + assert.equal(result.jobsCalls[0].args.attempt_number, 3); + assert.equal(result.status.state, 'failure'); +}); + +test('a successful draft workflow with skipped native jobs supplies no platform or hardware pass', async () => { + for (const required of ['IOS', 'ANDROID', 'IOS, ANDROID', 'IOS, ANDROID, HARDWARE']) { + const result = await evaluate([handoff(sha, required)], hostedOptions({ + draft: true, + jobs: ['IOS', 'ANDROID'].map(platform => hostedJob(platform, { conclusion: 'skipped', steps: [] })), + })); + assert.equal(result.status.state, 'failure', required); + } +}); + +test('a newer skipped draft run cannot reuse a ready PR run or an earlier attempt', async () => { + for (const run of [hostedRun({ id: 91 }), hostedRun({ id: 91, run_attempt: 3 })]) { + const result = await evaluate([handoff(sha, 'IOS, ANDROID')], hostedOptions({ + draft: true, runs: [hostedRun(), run], + jobsByRun: { 91: ['IOS', 'ANDROID'].map(platform => hostedJob(platform, + { run_id: 91, run_attempt: run.run_attempt, conclusion: 'skipped', steps: [] })) }, + })); + assert.equal(result.status.state, 'failure'); + assert.ok(result.jobsCalls.every(call => call.args.run_id === 91 && call.args.attempt_number === run.run_attempt)); + } +}); + +test('wrong run identity and stale source on jobs cannot satisfy current platform checks', async () => { + for (const change of [{ head_sha: 'd'.repeat(40) }, { run_id: 89 }, + { run_attempt: 1 }, { status: 'in_progress' }, { conclusion: 'cancelled' }, + { conclusion: 'skipped' }, { conclusion: 'failure' }]) { + const { status } = await evaluate([handoff(sha, 'IOS')], hostedOptions({ + jobs: [hostedJob('IOS', change)] + })); + assert.equal(status.state, 'failure', JSON.stringify(change)); + } +}); + +test('a build-only job or missing, skipped, cancelled or failed smoke is not native validation', async () => { + for (const steps of [[], [hostedJob('IOS').steps[0]], [hostedJob('IOS').steps[1]], + ...['skipped', 'cancelled', 'failure'].map(conclusion => [ + hostedJob('IOS').steps[0], { ...hostedJob('IOS').steps[1], conclusion } + ]), [hostedJob('IOS').steps[0], { ...hostedJob('IOS').steps[1], status: 'in_progress' }]]) { + const { status } = await evaluate([handoff(sha, 'IOS')], hostedOptions({ + jobs: [hostedJob('IOS', { steps })] + })); + assert.equal(status.state, 'failure', JSON.stringify(steps)); + } +}); + +test('duplicate platform jobs or required steps do not ambiguously satisfy validation', async () => { + for (const jobs of [[hostedJob('IOS'), hostedJob('IOS')], + [hostedJob('IOS', { steps: [...hostedJob('IOS').steps, hostedJob('IOS').steps[1]] })]]) { + const { status } = await evaluate([handoff(sha, 'IOS')], hostedOptions({ jobs })); + assert.equal(status.state, 'failure'); + } +}); + +test('workflow_run routing evaluates the associated PR and refreshes its current head', async () => { + const routedNumber = 7059; + const run = hostedRun({ pull_requests: [{ ...hostedRun().pull_requests[0], number: routedNumber }] }); + const result = await evaluate([handoff(sha, 'IOS, ANDROID')], hostedOptions({ + runs: [run], context: { issue: {}, eventName: 'workflow_run', payload: { workflow_run: run } } + })); + assert.equal(result.status.state, 'success'); + assert.deepEqual(result.pullsRequested, [routedNumber, routedNumber]); + assert.match(result.status.target_url, /pull\/7059$/); +}); + +test('a PR head change during validation prevents posting obsolete success', async () => { + const result = await evaluate([handoff(sha, 'IOS, ANDROID')], hostedOptions({ + afterHead: 'd'.repeat(40) + })); + assert.equal(result.statuses.length, 0); + assert.match(result.failures[0], /PR head changed/); +}); + +test('documentation and GitHub metadata exemption never consumes hosted CI assertions', async () => { + const result = await evaluate([], hostedOptions({ files: [ + { filename: 'AGENTS.md' }, { filename: '.github/workflows/keeper-hosted-native.yml' } + ] })); + assert.equal(result.status.state, 'success'); + assert.equal(result.runsCalls.length, 0); + assert.equal(result.jobsCalls.length, 0); +}); + +test('hosted workflow lifecycle events refresh the privileged gate under the exact workflow name', () => { + const nativeWorkflow = fs.readFileSync(path.join(path.dirname(workflowPath), + 'keeper-hosted-native.yml'), 'utf8'); + const name = nativeWorkflow.match(/^name:\s*(.+)$/m)?.[1]; + assert.ok(name, 'hosted workflow must have a name'); + assert.ok(lines.some(line => line.trim() === `workflows: [${name}]`), + 'workflow_run must name the actual hosted workflow'); + assert.ok(lines.some(line => line.trim() === 'types: [completed, requested, in_progress]'), + 'new attempts must invalidate old hosted evidence promptly'); +}); diff --git a/.github/tests/native-action-pins.test.cjs b/.github/tests/native-action-pins.test.cjs new file mode 100644 index 000000000..0f58e9857 --- /dev/null +++ b/.github/tests/native-action-pins.test.cjs @@ -0,0 +1,182 @@ +'use strict'; + +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const { spawnSync } = require('node:child_process'); +const test = require('node:test'); + +// Scope: workflows introduced by the bootstrap. Existing gate/action policy is +// unchanged; this does not silently repin unrelated workflows. +const workflows = ['keeper-hosted-native.yml', 'keeper-validation-tests.yml']; +// Verified upstream refs/tags/v2.34.0^{} on 2026-10-18, also used by #7062. +// The annotated tag object is 324029e2f414c084d8b15ba075288885e74aef9c. +const emulatorCommit = '1dcd0090116d15e7c562f8db72807de5e036a4ed'; +const emulatorAction = 'reactivecircus/android-emulator-runner'; +// Other pins match the upstream tags noted in the workflows, read-only checked +// on the same date. Updates require a new verified commit, not just SHA syntax. +const verifiedPins = new Map([ + ['actions/checkout', 'd23441a48e516b6c34aea4fa41551a30e30af803'], + ['actions/setup-node', '249970729cb0ef3589644e2896645e5dc5ba9c38'], + ['actions/setup-java', 'b6effb05e454b25005698d916606bdc6ffcbf961'], + ['ruby/setup-ruby', '675dd7ba1b06c8786a1480d89c384f5620a42647'], + ['actions/upload-artifact', 'b7c566a772e6b6bfb58ed0dc250532a479d7789f'], + ['android-actions/setup-android', 'be39fa834029ff78f1a44aa3bb0819b8fc2bd8fd'], + [emulatorAction, emulatorCommit], +]); + +// Use the host's existing Python/PyYAML parser, never app dependencies or a +// partial YAML regex. Missing tooling, ambiguous duplicate keys and unsupported +// YAML values fail the contract; there is no permissive fallback or install. +const yamlParser = ` +import json +import sys +import yaml + +class UniqueSafeLoader(yaml.SafeLoader): + pass + +def unique_mapping(loader, node, deep=False): + loader.flatten_mapping(node) + result = {} + for key_node, value_node in node.value: + key = loader.construct_object(key_node, deep=deep) + if key in result: + raise ValueError('Duplicate YAML mapping key') + result[key] = loader.construct_object(value_node, deep=deep) + return result + +UniqueSafeLoader.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, unique_mapping) +print(json.dumps(yaml.load(sys.stdin.read(), Loader=UniqueSafeLoader))) +`; + +function actionReferences(workflow, runParser = spawnSync) { + const result = runParser('python3', ['-B', '-c', yamlParser], { + input: workflow, encoding: 'utf8', timeout: 5000, maxBuffer: 1024 * 1024, + }); + assert.equal(result.status, 0, + `YAML parsing is required (Python 3 + PyYAML): ${result.error?.message || result.stderr || ''}`); + const document = JSON.parse(result.stdout); + const mapping = value => value !== null && typeof value === 'object' && !Array.isArray(value); + assert.ok(mapping(document) && mapping(document.jobs), 'workflow must have a jobs mapping'); + const references = []; + for (const job of Object.values(document.jobs)) { + assert.ok(mapping(job), 'each job must be a mapping'); + if (Object.hasOwn(job, 'uses')) references.push(job.uses); + if (Object.hasOwn(job, 'steps')) { + assert.ok(Array.isArray(job.steps), 'job steps must be a sequence'); + for (const step of job.steps) { + assert.ok(mapping(step), 'each step must be a mapping'); + if (Object.hasOwn(step, 'uses')) references.push(step.uses); + } + } + } + assert.ok(references.every(reference => typeof reference === 'string'), 'uses values must be strings'); + return references; +} + +function validateActionPins(workflow) { + const references = actionReferences(workflow); + assert.ok(references.length > 0, 'expected external actions in bootstrap workflow'); + for (const reference of references) { + assert.match(reference, /^[\w-]+\/[\w./-]+@[0-9a-f]{40}$/, + `external actions require a full immutable commit: ${reference}`); + const [action, revision] = reference.split('@'); + assert.ok(verifiedPins.has(action), `action needs a verified upstream commit: ${action}`); + assert.equal(revision, verifiedPins.get(action), + 'action must use its verified upstream commit, not an arbitrary SHA or tag object'); + } +} + +// Keep a valid companion action so a skipped mutable entry cannot pass merely +// because the parser found some other pinned reference in the same workflow. +function workflowWithStep(step) { + return `jobs: + contracts: + steps: + - uses: actions/setup-node@${verifiedPins.get('actions/setup-node')} + - ${step} +`; +} + +test('bootstrap external actions have full immutable pins and the verified emulator commit', () => { + for (const workflow of workflows) { + validateActionPins(fs.readFileSync(path.join(__dirname, '../workflows', workflow), 'utf8')); + } + const hosted = fs.readFileSync(path.join(__dirname, '../workflows', workflows[0]), 'utf8'); + assert.equal(actionReferences(hosted).filter(ref => ref.startsWith(`${emulatorAction}@`)).length, 1); +}); + +test('action pin validation rejects mutable, short, malformed and tag-object revisions', () => { + for (const revision of ['v2', 'v2.34.0', 'main', emulatorCommit.slice(0, 12), + '324029e2f414c084d8b15ba075288885e74aef9c38', + '324029e2f414c084d8b15ba075288885e74aef9c', 'a'.repeat(40), 'g'.repeat(40), + '${{ inputs.revision }}']) { + assert.throws(() => validateActionPins(workflowWithStep(`uses: ${emulatorAction}@${revision}`)), + /full immutable commit|verified upstream commit/, revision); + } + assert.throws(() => validateActionPins(workflowWithStep('uses: actions/checkout@v6')), /full immutable commit/); + assert.throws(() => validateActionPins(workflowWithStep(`uses: actions/checkout@${'a'.repeat(40)}`)), /verified upstream commit/); + assert.throws(() => validateActionPins(workflowWithStep(`uses: unknown/action@${emulatorCommit}`)), /verified upstream commit/); + assert.throws(() => validateActionPins('jobs: {}\n'), assert.AssertionError); +}); + +test('quoted keys, flow mappings and folded values cannot hide a mutable action beside pinned actions', () => { + const forms = [ + reference => `"uses": ${reference}`, + reference => `'uses': '${reference}'`, + reference => `"\\u0075ses": "${reference}"`, + reference => `!!str uses: ${reference}`, + reference => `{uses: ${reference}}`, + reference => `{"name": "checkout", "uses": "${reference}"}`, + reference => `uses: >-\n ${reference}`, + reference => `uses: |-\n ${reference}`, + ]; + for (const form of forms) { + const pinned = `actions/checkout@${verifiedPins.get('actions/checkout')}`; + const valid = workflowWithStep(form(pinned)); + assert.deepEqual(actionReferences(valid), [ + `actions/setup-node@${verifiedPins.get('actions/setup-node')}`, pinned, + ]); + validateActionPins(valid); + for (const mutable of ['actions/checkout@v6', 'actions/checkout@main']) { + const invalid = workflowWithStep(form(mutable)); + assert.ok(actionReferences(invalid).includes(mutable), 'mutable action must be inspected, not skipped'); + assert.throws(() => validateActionPins(invalid), /full immutable commit/, form(mutable)); + } + } +}); + +test('whole flow documents, aliases and job-level uses are inspected', () => { + const pinned = `actions/checkout@${verifiedPins.get('actions/checkout')}`; + const forms = [ + reference => JSON.stringify({ jobs: { contracts: { steps: [ + { uses: pinned }, { uses: reference }, + ] } } }), + reference => `jobs:\n contracts:\n steps:\n - uses: ${pinned}\n - &checkout {uses: ${reference}}\n - *checkout\n`, + reference => `jobs: {build: {steps: [{uses: ${pinned}}]}, reuse: {"uses": "${reference}"}}\n`, + ]; + for (const form of forms) { + validateActionPins(form(pinned)); + assert.ok(actionReferences(form('actions/checkout@v6')).includes('actions/checkout@v6')); + assert.throws(() => validateActionPins(form('actions/checkout@v6')), /full immutable commit/); + } +}); + +test('YAML inspection fails closed for unavailable parsers, invalid input and duplicate keys', () => { + for (const result of [ + { status: null, error: new Error('spawn python3 ENOENT') }, + { status: 1, stderr: "ModuleNotFoundError: No module named 'yaml'" }, + { status: null, error: new Error('parser timeout') }, + ]) { + assert.throws(() => actionReferences('jobs: {}', () => result), /YAML parsing is required/); + } + assert.throws(() => validateActionPins('jobs: ['), /YAML parsing is required/); + const pinned = `actions/checkout@${verifiedPins.get('actions/checkout')}`; + for (const references of [[pinned, 'actions/checkout@v6'], ['actions/checkout@v6', pinned]]) { + assert.throws(() => validateActionPins(workflowWithStep( + `uses: ${references[0]}\n "uses": ${references[1]}`)), /Duplicate YAML mapping key/); + } + assert.throws(() => validateActionPins(workflowWithStep('uses: {action: actions/checkout@v6}')), + /uses values must be strings/); +}); diff --git a/.github/tests/native-draft-policy.test.cjs b/.github/tests/native-draft-policy.test.cjs new file mode 100644 index 000000000..979034c2b --- /dev/null +++ b/.github/tests/native-draft-policy.test.cjs @@ -0,0 +1,131 @@ +'use strict'; + +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const { spawnSync } = require('node:child_process'); +const test = require('node:test'); + +const directory = path.resolve(__dirname, '../workflows'); +// BaseLoader keeps YAML's `on` key and scalar text intact for these policy +// checks. Parsing failure is fatal; this is not workflow semantic lint. +const parsed = spawnSync('python3', ['-B', '-c', ` +import json, pathlib, sys, yaml +print(json.dumps({p.name: yaml.load(p.read_text(), Loader=yaml.BaseLoader) + for p in pathlib.Path(sys.argv[1]).glob('*.yml')})) +`, directory], { encoding: 'utf8', timeout: 5000 }); +assert.equal(parsed.status, 0, parsed.stderr || parsed.error?.message); +const workflows = JSON.parse(parsed.stdout); +const native = workflows['keeper-hosted-native.yml']; +const ci = workflows['test.yml']; +const guard = "github.event_name == 'pull_request' && toJSON(github.event.pull_request.draft) == 'false'"; + +// Evaluate the actual checked-in expressions using only their two context +// paths and toJSON. Missing context is null, as in Actions. These conditions +// compare strings only, so JavaScript and Actions equality give the same result. +function allowed(condition, eventName, draft = null) { + assert.equal(typeof condition, 'string', 'an explicit condition is required'); + const expression = condition + .replaceAll('github.event_name', 'eventName') + .replaceAll('github.event.pull_request.draft', 'draft'); + return Boolean(new Function('eventName', 'draft', 'toJSON', `return (${expression});`)( + eventName, draft, JSON.stringify)); +} + +function triggered(workflow, event, action, branch) { + const config = workflow.on[event]; + if (config === undefined) return false; + if (config.branches && !config.branches.includes(branch)) return false; + const types = config.types || (event === 'pull_request' ? ['opened', 'synchronize', 'reopened'] : null); + return !types || types.includes(action); +} + +test('native opened, synchronize and reopened draft jobs are skipped before runner allocation', () => { + assert.deepEqual(Object.keys(native.jobs), ['ios', 'android']); + for (const action of ['opened', 'synchronize', 'reopened']) { + assert.ok(triggered(native, 'pull_request', action, 'sprint')); + for (const job of Object.values(native.jobs)) { + assert.equal(job.if, guard); + assert.equal(allowed(job.if, 'pull_request', true), false, action); + assert.equal(allowed(job.if, 'pull_request', false), true, action); + } + } +}); + +test('ready-for-review schedules native work and normal Tests; conversion back to draft skips native work', () => { + assert.ok(triggered(native, 'pull_request', 'ready_for_review', 'sprint')); + assert.ok(triggered(ci, 'pull_request', 'ready_for_review', 'sprint')); + assert.ok(triggered(native, 'pull_request', 'converted_to_draft', 'sprint')); + for (const job of Object.values(native.jobs)) { + assert.equal(allowed(job.if, 'pull_request', false), true); + assert.equal(allowed(job.if, 'pull_request', true), false); + } + assert.equal(native.concurrency.group, 'keeper-hosted-native-${{ github.event.pull_request.number }}'); + assert.equal(native.concurrency['cancel-in-progress'], 'true'); +}); + +test('native non-PR and missing or malformed draft contexts fail closed', () => { + assert.deepEqual(Object.keys(native.on), ['pull_request']); + for (const job of Object.values(native.jobs)) { + for (const event of ['push', 'workflow_dispatch', 'workflow_call', 'workflow_run', 'schedule', 'pull_request_target']) { + assert.equal(allowed(job.if, event, false), false, event); + } + for (const draft of [null, '', 'false', 'true', 0, 1]) { + assert.equal(allowed(job.if, 'pull_request', draft), false, JSON.stringify(draft)); + } + } +}); + +test('bootstrap has no reusable native caller, worker or dispatch entry point', () => { + assert.deepEqual(Object.keys(workflows).sort(), [ + 'codeql.yml', 'keeper-hosted-native.yml', 'keeper-local-validation.yml', + 'keeper-validation-tests.yml', 'rag-ingest-release.yml', 'test.yml', + ]); + for (const [filename, workflow] of Object.entries(workflows)) { + assert.ok(!Object.hasOwn(workflow.on, 'workflow_call'), filename); + assert.ok(!Object.hasOwn(workflow.on, 'workflow_dispatch'), filename); + for (const job of Object.values(workflow.jobs)) assert.ok(!job.uses, filename); + } + // #7062's reusable workflows are intentionally absent, not silently adopted. + // Any later addition requires an explicit extension of this entry-path review. +}); + +test('pushing the unpublished branch triggers no checked-in push workflow', () => { + for (const [filename, workflow] of Object.entries(workflows)) { + assert.equal(triggered(workflow, 'push', null, 'codex/cloud-bootstrap-privacy'), false, filename); + } +}); + +test('draft opening and updates retain the expected host checks without a release trigger', () => { + for (const action of ['opened', 'synchronize', 'reopened']) { + const triggeredFiles = Object.entries(workflows).filter(([, workflow]) => + triggered(workflow, 'pull_request', action, 'sprint')).map(([filename]) => filename).sort(); + assert.deepEqual(triggeredFiles, [ + 'codeql.yml', 'keeper-hosted-native.yml', 'keeper-validation-tests.yml', 'test.yml', + ]); + assert.ok(triggered(workflows['keeper-local-validation.yml'], 'pull_request_target', action, 'sprint')); + } +}); + +test('draft Tests install cannot invoke native prepare and does not waive install or test failure', () => { + const steps = ci.jobs.test.steps; + const installs = steps.filter(step => step.run?.startsWith('yarn install')); + assert.equal(installs.length, 2); + for (const [event, draft, expected] of [ + ['pull_request', true, 'yarn install --frozen-lockfile --ignore-scripts'], + ['pull_request', null, 'yarn install --frozen-lockfile --ignore-scripts'], + ['pull_request', 'false', 'yarn install --frozen-lockfile --ignore-scripts'], + ['pull_request', false, 'yarn install'], ['push', null, 'yarn install'], + ]) { + const active = installs.filter(step => allowed(step.if, event, draft)); + assert.equal(active.length, 1); + assert.equal(active[0].run, expected); + } + assert.ok(!ci.jobs.test.if && !ci.jobs.test['continue-on-error']); + for (const step of steps) assert.ok(!step['continue-on-error']); + const tests = steps.find(step => step.name === 'Run tests'); + assert.equal(tests.run, 'npm run test'); + assert.ok(!tests.if); + assert.equal(ci.jobs.lint.steps.find(step => step.name === 'Install dependencies').run, + 'yarn install --ignore-scripts'); +}); diff --git a/.github/tests/nodeify.test.cjs b/.github/tests/nodeify.test.cjs new file mode 100644 index 000000000..400c36534 --- /dev/null +++ b/.github/tests/nodeify.test.cjs @@ -0,0 +1,38 @@ +'use strict'; + +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const os = require('node:os'); +const path = require('node:path'); +const { spawnSync } = require('node:child_process'); +const test = require('node:test'); + +const wrapper = path.resolve(__dirname, '../native/nodeify.cjs'); + +test('native shim preparation filters upstream defaults without requesting installation', t => { + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'keeper-nodeify-')); + t.after(() => fs.rmSync(root, { recursive: true, force: true })); + const upstream = path.join(root, 'node_modules/rn-nodeify'); + fs.mkdirSync(upstream, { recursive: true }); + fs.writeFileSync(path.join(upstream, 'coreList.js'), "module.exports = ['constants', 'fs', 'net'];\n"); + fs.writeFileSync(path.join(upstream, 'cmd.js'), ` + process.stdout.write(JSON.stringify({ + shims: require('./coreList'), arguments: process.argv.slice(2) + })); + `); + const result = spawnSync(process.execPath, ['-e', + 'require(process.argv[1]).runNodeify(process.cwd())', wrapper], { cwd: root, encoding: 'utf8' }); + assert.equal(result.status, 0, result.stderr); + const output = JSON.parse(result.stdout); + assert.deepEqual(output.arguments, ['--hack']); + assert.deepEqual(output.shims, [ + 'buffer', 'events', 'process', 'stream', 'inherits', 'path', 'assert', 'crypto', + ]); + assert.ok(!output.shims.includes('constants')); +}); + +test('native shim preparation rejects an unrelated working directory', () => { + const { runNodeify } = require(wrapper); + assert.throws(() => runNodeify(path.join(os.tmpdir(), 'unrelated-keeper-checkout')), + /repository root/); +}); diff --git a/.github/tests/test_diagnostic_privacy.py b/.github/tests/test_diagnostic_privacy.py new file mode 100644 index 000000000..923d7562d --- /dev/null +++ b/.github/tests/test_diagnostic_privacy.py @@ -0,0 +1,136 @@ +"""Adversarial export contracts with synthetic data; no native tools or app state.""" + +from contextlib import redirect_stdout, redirect_stderr +import io +import json +from pathlib import Path +import runpy +import subprocess +import sys +import unittest +from unittest import mock + +import test_hosted_native as harness + +native = harness.native + + +# Reuse the isolated evidence fixture without running the inherited test suite. +class DiagnosticPrivacyTest(unittest.TestCase): + setUp = harness.HostedNativeHarnessTest.setUp + + def test_arbitrary_private_text_cannot_be_exported_by_any_log_classifier(self): + payloads = [ + 'PRIVATE_SENTINEL ordinary wallet name and recovery words with no secret label', + 'PRIVATE_SENTINEL xpubSyntheticExtendedKey bc1qsyntheticaddress user@example.invalid', + 'PRIVATE_SENTINEL token = "two words" password: two words Bearer synthetic', + 'PRIVATE_SENTINEL file:///Users/private-user/Documents/wallet.txt /home/private-user/backup', + 'PRIVATE_SENTINEL https://example.invalid/private?phrase=one%20two', + 'PRIVATE_SENTINEL cHJpdmF0ZSB3b3Jkcw== \\u0070rivate synthetic', + 'PRIVATE_SENTINEL \u79d8\u5bc6 \u03bc\u03c5\u03c3\u03c4\u03b9\u03ba\u03cc \u202esecret \x00\x1b[31mprivate\x1b[0m', + 'PRIVATE_SENTINEL first line\nprivate continuation\rprivate last line', + 'PRIVATE_SENTINEL ' + 'long arbitrary private text ' * 100, + ] + routes = [ + (native.failure_details, 'error: {}', ['TOOL_ERROR']), + (native.failure_details, 'e: {}', ['KOTLIN_COMPILER_ERROR']), + (native.failure_details, 'FAILURE: {}', ['BUILD_FAILURE']), + (native.failure_details, 'Caused by: {}', ['TOOL_ERROR']), + (native.maestro_failure_details, 'Unable to launch app {}', ['Maestro app launch failed']), + (native.maestro_failure_details, 'Failed to connect {}', ['Maestro connection failed']), + (native.maestro_failure_details, 'Failed to start {}', ['Maestro driver start failed']), + (native.maestro_failure_details, 'No devices found {}', ['Maestro device unavailable']), + (native.android_startup_errors, 'E/AndroidRuntime( 123): FATAL EXCEPTION: {}', ['ANDROID_RUNTIME_ERROR']), + (native.android_startup_errors, 'E/ReactNativeJS( 123): TypeError: {}', ['REACT_NATIVE_JS_ERROR']), + (native.android_startup_errors, 'F/ReactNative( 123): Could not {}', ['REACT_NATIVE_ERROR']), + (native.metro_startup_errors, 'ERROR {}', ['METRO_ERROR']), + (native.metro_startup_errors, 'SyntaxError: {}', ['METRO_SYNTAX_ERROR']), + (native.metro_startup_errors, 'TypeError: {}', ['METRO_TYPE_ERROR']), + (native.metro_startup_errors, 'ReferenceError: {}', ['METRO_REFERENCE_ERROR']), + (native.metro_startup_errors, 'Unable to resolve module {}', ['METRO_MODULE_RESOLUTION_ERROR']), + ] + for payload in payloads: + for classify, template, expected in routes: + with self.subTest(route=template, payload=payload[:70]): + self.assertEqual(classify(template.format(payload)), expected) + with self.subTest(route='ios', payload=payload[:70]): + diagnostics = native.ios_startup_diagnostics( + '2026-10-18 06:00:00.000 E hexa_keeper_dev[123:abc] ' + payload, 'hexa_keeper_dev') + self.assertEqual(diagnostics['errors'], ['IOS_STARTUP_ERROR']) + + def test_general_failure_categories_cover_install_blockers_without_excerpts(self): + messages = [ + ('error: lockfile needs to be updated', 'DEPENDENCY_LOCK_MISMATCH'), + ('[!] could not find compatible versions', 'DEPENDENCY_LOCK_MISMATCH'), + ('The sandbox is not in sync', 'DEPENDENCY_LOCK_MISMATCH'), + ('error: no space left on device', 'DISK_SPACE_EXHAUSTED'), + ('error: command not found', 'REQUIRED_TOOL_UNAVAILABLE'), + ('[!] requires a higher minimum deployment target', 'IOS_DEPLOYMENT_TARGET_UNSUPPORTED'), + ('[!] other pod failure', 'COCOAPODS_ERROR'), + ('A problem occurred', 'BUILD_FAILURE'), + ('Execution failed', 'BUILD_FAILURE'), + ('Could not find a dependency', 'DEPENDENCY_OR_TOOL_FAILURE'), + ('> compiler detail', 'TOOL_ERROR'), + ] + for message, category in messages: + with self.subTest(message=message): + self.assertEqual(native.failure_details(message + ' PRIVATE_SENTINEL'), [category]) + self.assertEqual(native.failure_details('ordinary PRIVATE_SENTINEL\n> Task :compile\n' + ' 12 | error: PRIVATE_SENTINEL'), []) + self.assertEqual(native.failure_details('error: PRIVATE_SENTINEL\n' * 1000), ['TOOL_ERROR']) + self.assertEqual(native.metro_startup_errors('ERROR PRIVATE_SENTINEL\n' * 1000), ['METRO_ERROR']) + self.assertEqual(native.android_startup_errors('E/OtherApp( 1): Error: PRIVATE_SENTINEL'), []) + + def test_failed_command_metadata_console_and_exception_do_not_export_private_text(self): + script = self.root / 'failed-command.py' + script.write_text('print("error: PRIVATE_SENTINEL arbitrary recovery text"); raise SystemExit(3)') + console = io.StringIO() + with redirect_stdout(console), self.assertRaises(RuntimeError) as failure: + native.command([sys.executable, str(script)], 'Build fixture') + record = native.read()['commands'][0] + self.assertEqual(record['exit_code'], 3) + self.assertEqual(record['redacted_tool_errors'], ['TOOL_ERROR']) + self.assertIn('TOOL_ERROR', console.getvalue()) + self.assertNotIn('PRIVATE_SENTINEL', native.EVIDENCE.read_text() + console.getvalue() + str(failure.exception)) + self.assertIn('PRIVATE_SENTINEL', (native.PRIVATE / 'command-001.log').read_text()) + + def test_startup_collector_exports_only_categories_and_preserves_failure(self): + native.PRIVATE.mkdir() + native.save({'commands': [{'label': 'Assert startup and disposable passcode transition'}], + 'build': 'PASS', 'smoke': 'FAIL'}) + (native.PRIVATE / 'command-001.log').write_text('Unable to launch app PRIVATE_SENTINEL wallet text\n') + (native.PRIVATE / 'metro.log').write_text('ERROR PRIVATE_SENTINEL wallet text\n') + report = native.PRIVATE / 'smoke-junit.xml' + report.write_text('' + 'Failed to connect PRIVATE_SENTINEL wallet text\nUI hierarchy: PRIVATE_SENTINEL' + '') + + def android_log(args, stdout, **kwargs): + stdout.write(b'E/AndroidRuntime( 123): FATAL EXCEPTION: PRIVATE_SENTINEL wallet text\n') + return subprocess.CompletedProcess(args, 0) + + console = io.StringIO() + with redirect_stdout(console), mock.patch.object(subprocess, 'run', side_effect=android_log): + native.record_startup_failure(report, 'fixture-emulator') + data = native.read() + details = data['startup_diagnostics'] + self.assertEqual((data['build'], data['smoke']), ('PASS', 'FAIL')) + self.assertEqual(details['maestro']['testcase_statuses'], ['ERROR']) + self.assertEqual(details['maestro']['failed_commands'], ['Maestro app launch failed']) + self.assertEqual(details['maestro']['testcase_failures'], ['Maestro connection failed']) + self.assertEqual(details['metro_errors'], ['METRO_ERROR']) + self.assertEqual(details['android_errors']['errors'], ['ANDROID_RUNTIME_ERROR']) + self.assertNotIn('PRIVATE_SENTINEL', json.dumps(data) + console.getvalue()) + + def test_cli_failure_never_prints_arbitrary_exception_text(self): + console = io.StringIO() + # The record phase fails before invoking a native tool. Exercise the real + # CLI exception boundary, including its nonzero exit, with a private error. + with mock.patch.dict(native.os.environ, {'KEEPER_NATIVE_PLATFORM': 'ANDROID'}), \ + mock.patch.object(sys, 'argv', [str(Path(native.__file__)), 'record']), \ + mock.patch.object(subprocess, 'check_output', side_effect=RuntimeError('PRIVATE_SENTINEL')), \ + redirect_stderr(console), self.assertRaises(SystemExit) as failure: + runpy.run_path(native.__file__, run_name='__main__') + self.assertEqual(failure.exception.code, 1) + self.assertIn('Hosted native validation failed', console.getvalue()) + self.assertNotIn('PRIVATE_SENTINEL', console.getvalue()) diff --git a/.github/tests/test_hosted_native.py b/.github/tests/test_hosted_native.py new file mode 100644 index 000000000..a1b9fed4f --- /dev/null +++ b/.github/tests/test_hosted_native.py @@ -0,0 +1,494 @@ +"""Pure hosted-harness checks: these do not claim native simulator evidence.""" + +import hashlib +import importlib.util +import json +from pathlib import Path +import subprocess +import sys +import tempfile +import unittest +from unittest import mock + + +SPEC = importlib.util.spec_from_file_location( + 'hosted_native', Path(__file__).resolve().parents[1] / 'native' / 'hosted_native.py') +native = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(native) + + +class HostedNativeHarnessTest(unittest.TestCase): + def setUp(self): + self.directory = tempfile.TemporaryDirectory() + self.addCleanup(self.directory.cleanup) + self.root = Path(self.directory.name) + self.patches = mock.patch.multiple(native, ROOT=self.root, PRIVATE=self.root / 'private', + EVIDENCE=self.root / 'evidence/metadata.json', PLATFORM='ANDROID') + self.patches.start() + self.addCleanup(self.patches.stop) + native.save({'commands': [], 'build': 'NOT RUN', 'smoke': 'NOT RUN'}) + + def test_success_records_command_and_duration_without_output(self): + script = self.root / 'output.py' + script.write_text('print("private output")') + result = native.command([sys.executable, str(script)], 'Success', capture=True) + self.assertEqual(result, 'private output') + record = native.read()['commands'][0] + self.assertEqual(record['exit_code'], 0) + self.assertGreaterEqual(record['duration_seconds'], 0) + self.assertNotIn('private output', native.EVIDENCE.read_text()) + + def test_failure_exposes_only_allowlisted_diagnostic(self): + script = self.root / 'failure.py' + script.write_text('print("private payload: lockfile needs to be updated"); raise SystemExit(3)') + with self.assertRaisesRegex(RuntimeError, 'Manifest and yarn.lock are inconsistent'): + native.command([sys.executable, str(script)], 'Install') + record = native.read()['commands'][0] + self.assertEqual(record['exit_code'], 3) + self.assertNotIn('private payload', native.EVIDENCE.read_text()) + + def test_hierarchy_command_failure_never_exports_arbitrary_error_lines(self): + script = self.root / 'hierarchy_failure.py' + script.write_text('print("error: PRIVATE_SENTINEL hierarchy data"); raise SystemExit(1)') + with self.assertRaisesRegex(RuntimeError, 'exit 1'): + native.command([sys.executable, str(script)], 'Driver hierarchy', error_details=False) + self.assertEqual(native.read()['commands'][0]['redacted_tool_errors'], []) + self.assertNotIn('PRIVATE_SENTINEL', native.EVIDENCE.read_text()) + + def test_tool_errors_are_deduplicated_fixed_categories(self): + output = 'private payload\nerror: missing module https://example.test/private token=secret\n' \ + + 'Caused by: password=private Bearer private\n' + 'error: detail\n' * 30 + details = native.failure_details(output) + self.assertEqual(details, ['TOOL_ERROR']) + kotlin = '> Task :compile\n' * 40 + 'e: file:///runner/App.kt:1 unresolved reference' + self.assertEqual(native.failure_details(kotlin), ['KOTLIN_COMPILER_ERROR']) + excerpts = ''.join(' %s | +(NSDictionary*)statWithError:(NSError**)error:\n' % line + for line in range(30)) + self.assertEqual(native.failure_details(excerpts + 'error: actual compiler failure'), + ['TOOL_ERROR']) + + def test_timeout_records_failure_and_terminates_process(self): + with self.assertRaisesRegex(RuntimeError, 'exit 124'): + native.command([sys.executable, '-c', 'import time; time.sleep(30)'], 'Timeout', timeout=0.05) + self.assertEqual(native.read()['commands'][0]['exit_code'], 124) + + def test_startup_diagnostics_keep_only_fixed_failed_steps_and_categories(self): + maestro = '\n'.join([ + 'Assert that "Create a passcode" is visible... FAILED', + 'Tap on id: key_2... FAILED', + 'Tap on "private recovery phrase"... FAILED', + 'UI hierarchy: private state', + 'Assertion is false: "Create a passcode" is visible; UI hierarchy: PRIVATE_SENTINEL', + 'Element not found: key_1; private arbitrary suffix', + ]) + messages = native.maestro_failure_details(maestro) + self.assertTrue(any('Create a passcode' in message for message in messages)) + self.assertIn('Maestro tap key_2 FAILED', messages) + self.assertNotIn('private', '\n'.join(messages)) + self.assertNotIn('PRIVATE_SENTINEL', '\n'.join(messages)) + self.assertNotIn('hierarchy', '\n'.join(messages)) + self.assertIn('Maestro assertion failed: Create a passcode', messages) + self.assertIn('Maestro element not found: key_1', messages) + metro = native.metro_startup_errors(' BUNDLE index.js\n ERROR Error: Unable to resolve module token=private https://example.test/private; View hierarchy: PRIVATE_SENTINEL') + self.assertEqual(metro, ['METRO_MODULE_RESOLUTION_ERROR']) + native_errors = native.android_startup_errors('\n'.join([ + 'E/AndroidRuntime( 123): FATAL EXCEPTION: main; credentials: PRIVATE_SENTINEL', + 'E/ReactNativeJS( 123): TypeError: Cannot load module token=private', + 'E/OtherApp( 124): Error: private unrelated data', + 'E/ReactNativeJS( 123): at unneeded stack frame', + ])) + self.assertEqual(native_errors, ['ANDROID_RUNTIME_ERROR', 'REACT_NATIVE_JS_ERROR']) + + def test_junit_failure_collection_omits_hierarchy_and_preserves_failed_result(self): + native.PRIVATE.mkdir() + native.save({'commands': [], 'build': 'PASS', 'smoke': 'FAIL'}) + (native.PRIVATE / 'metro.log').write_text('No bundler error\n') + report = native.PRIVATE / 'smoke-junit.xml' + report.write_text('' + 'Assertion is false: Create a passcode is visible\nUI hierarchy: private data' + '') + hierarchy = native.PRIVATE / 'maestro-output/2026-10-17_092233/Disposable startup and passcode input/screen-hierarchy' + hierarchy.mkdir(parents=True) + (hierarchy / 'step-002-assertCondition-Create_a_passcode.json').write_text(json.dumps({ + 'attributes': {'accessibilityText': 'PRIVATE_SENTINEL', 'resource-id': 'redbox-error'}, + 'children': [{'attributes': {'accessibilityText': 'No script URL provided. PRIVATE_SENTINEL'}}]})) + native.record_startup_failure(report, None) + data = native.read() + self.assertEqual(data['smoke'], 'FAIL') + self.assertEqual(data['startup_diagnostics']['maestro']['testcase_statuses'], ['ERROR']) + self.assertNotIn('private data', native.EVIDENCE.read_text()) + self.assertNotIn('PRIVATE_SENTINEL', native.EVIDENCE.read_text()) + self.assertTrue(data['startup_diagnostics']['hierarchy_signals']['RNredbox']) + self.assertTrue(data['startup_diagnostics']['hierarchy_signals']['noScriptURL']) + self.assertTrue(data['startup_diagnostics']['maestro']['testcase_failures']) + + def test_private_hierarchy_exports_only_fixed_signals_from_standalone_tree_nodes(self): + hierarchy = self.root / 'maestro-output/2026-10-17_092233/Disposable startup and passcode input/screen-hierarchy' + hierarchy.mkdir(parents=True) + (hierarchy / 'step-002-assertCondition-Create_a_passcode.json').write_text(json.dumps({ + 'attributes': {'accessibilityText': 'Create a passcode', 'value': 'PRIVATE_SENTINEL'}, + 'children': [ + {'attributes': {'accessibilityText': 'Confirm your passcode'}}, + {'attributes': {'resource-id': 'io.hexawallet.keeper.development:id/rn_redbox_stack', + 'text': 'Could not connect to development server. PRIVATE_SENTINEL'}}, + {'attributes': {'title': 'Unable to load script. PRIVATE_SENTINEL'}}, + {'attributes': {'text': 'SpringBoard PRIVATE_SENTINEL'}, 'children': None}, + ]})) + # Other JSON files are not hierarchy snapshots and must not be parsed. + (hierarchy.parent / 'commands.json').write_text('{ malformed PRIVATE_SENTINEL') + signals = native.startup_hierarchy_signals(self.root / 'maestro-output') + self.assertEqual(signals, {'createPIN': True, 'confirmPIN': True, 'RNredbox': True, + 'noScriptURL': False, 'developmentServerError': True, + 'unableLoadScript': True, 'snapshot_count': 1, 'parse_error_types': []}) + self.assertNotIn('PRIVATE_SENTINEL', json.dumps(signals)) + self.assertNotIn('SpringBoard', json.dumps(signals)) + + def test_private_hierarchy_bounds_bytes_and_nodes_and_records_only_parse_error_classes(self): + hierarchy = self.root / 'maestro-output/screen-hierarchy' + hierarchy.mkdir(parents=True) + (hierarchy / 'step-001-malformed.json').write_text('{ PRIVATE_SENTINEL') + (hierarchy / 'step-002-oversize.json').write_bytes(b'PRIVATE_SENTINEL' * (1024 * 1024 // 8)) + (hierarchy / 'step-003-too-many-nodes.json').write_text(json.dumps({ + 'attributes': {'accessibilityText': 'Create a passcode'}, 'children': [{}] * 10000})) + signals = native.startup_hierarchy_signals(self.root / 'maestro-output') + self.assertEqual(signals['snapshot_count'], 3) + self.assertEqual(set(signals['parse_error_types']), {'JSONDecodeError', 'ValueError'}) + self.assertFalse(signals['createPIN']) + self.assertNotIn('PRIVATE_SENTINEL', json.dumps(signals)) + + def test_maestro_preflight_retains_fixed_driver_markers_and_known_types_without_raw_messages(self): + output = native.PRIVATE / 'maestro-output' + output.mkdir(parents=True) + native.save({'commands': [{'label': 'Assert startup and disposable passcode transition'}], + 'smoke': 'FAIL'}) + (native.PRIVATE / 'command-001.log').write_text( + 'xcuitest.installer.LocalXCTestInstaller$IOSDriverTimeoutException: ' + 'iOS driver not ready in time, consider increasing timeout by configuring ' + 'MAESTRO_DRIVER_STARTUP_TIMEOUT env variable PRIVATE_SENTINEL\n' + 'PRIVATE_SENTINELException: arbitrary exception names must not escape\n') + (output / 'maestro.log').write_text( + '[Start] Install XCUITest runner on PRIVATE_SENTINEL\n' + '[Done] Install XCUITest runner on PRIVATE_SENTINEL\n' + '[Start] Perform XCUITest driver status check on PRIVATE_SENTINEL\n' + '[Done] Perform XCUITest driver status check on PRIVATE_SENTINEL\n' + '[Failed] Perform XCUITest driver status check on PRIVATE_SENTINEL\n' + 'Installing pre built driver without xcodebuild PRIVATE_SENTINEL\n' + 'Requested driver host port PRIVATE_SENTINEL is not available\n') + (output / 'xctest_runner_fixture.log').write_text( + 'java.io.FileNotFoundException: xctestrun config does not exist PRIVATE_SENTINEL\n' + 'ui test runner does not exist PRIVATE_SENTINEL\n' + '** TEST EXECUTE FAILED ** PRIVATE_SENTINEL\nTesting failed: PRIVATE_SENTINEL\n') + result = native.maestro_preflight_diagnostics() + self.assertTrue(result['driver_startup_timeout']) + self.assertTrue(result['runner_install_started']) + self.assertTrue(result['runner_install_launch_returned']) + self.assertTrue(result['status_response_received']) + self.assertTrue(result['status_check_failed']) + self.assertTrue(result['host_port_unavailable']) + self.assertTrue(result['xctestrun_config_missing']) + self.assertTrue(result['ui_test_runner_missing']) + self.assertTrue(result['xcode_test_execute_failed']) + self.assertTrue(result['xcode_testing_failed']) + self.assertEqual(set(result['exception_types']), {'IOSDriverTimeoutException', 'FileNotFoundException'}) + self.assertEqual(result['logs_read'], 3) + self.assertNotIn('PRIVATE_SENTINEL', json.dumps(result)) + self.assertNotIn(str(output), json.dumps(result)) + self.assertNotIn('driver_ready', result) + self.assertEqual(native.read()['smoke'], 'FAIL') + + def test_ios_driver_preflight_is_setup_only_and_cleans_only_its_created_simulator(self): + native.save({'commands': [], 'maestro': {'binary': '/private/maestro'}, + 'build': 'NOT RUN', 'smoke': 'NOT RUN'}) + owned = '12345678-1234-1234-1234-123456789abc' + with mock.patch.object(native, 'PLATFORM', 'IOS'), mock.patch.dict(native.os.environ), \ + mock.patch.object(native, 'command', side_effect=[owned, None, None, None]) as command, \ + mock.patch.object(subprocess, 'run', return_value=subprocess.CompletedProcess([], 0)) as cleanup: + native.driver() + self.assertEqual(native.os.environ['XDG_STATE_HOME'], str(native.PRIVATE / 'maestro-preflight-state')) + self.assertEqual(command.call_args.args[0], + ['/private/maestro', '--platform', 'ios', '--device', owned, 'hierarchy']) + self.assertFalse(command.call_args.kwargs['error_details']) + self.assertEqual([call.args[0] for call in cleanup.call_args_list], + [['xcrun', 'simctl', 'shutdown', owned], ['xcrun', 'simctl', 'delete', owned]]) + self.assertEqual(native.read()['driver_preflight']['result'], 'PASS') + self.assertEqual(native.read()['build'], 'NOT RUN') + self.assertEqual(native.read()['smoke'], 'NOT RUN') + + def test_ios_driver_failure_preserves_original_error_and_attempts_delete_after_cleanup_timeout(self): + native.save({'commands': [], 'maestro': {'binary': '/private/maestro'}, + 'build': 'NOT RUN', 'smoke': 'NOT RUN'}) + owned = '12345678-1234-1234-1234-123456789abc' + with mock.patch.object(native, 'PLATFORM', 'IOS'), mock.patch.dict(native.os.environ), \ + mock.patch.object(native, 'command', side_effect=[owned, None, None, RuntimeError('driver failed PRIVATE_SENTINEL')]), \ + mock.patch.object(subprocess, 'run', side_effect=[subprocess.TimeoutExpired('shutdown', 30), + subprocess.CompletedProcess([], 0)]) as cleanup, \ + self.assertRaisesRegex(RuntimeError, 'driver failed PRIVATE_SENTINEL'): + native.run_phase('driver') + self.assertEqual(cleanup.call_count, 2) + self.assertEqual(cleanup.call_args.args[0], ['xcrun', 'simctl', 'delete', owned]) + data = native.read() + self.assertEqual(data['driver_preflight']['result'], 'FAIL') + self.assertEqual(data['driver_preflight']['cleanup_errors'], [{'action': 'shutdown', 'error_type': 'TimeoutExpired'}]) + self.assertEqual(data['failed_phase'], 'driver') + self.assertEqual(data['build'], 'NOT RUN') + self.assertEqual(data['smoke'], 'NOT RUN') + self.assertNotIn('PRIVATE_SENTINEL', native.EVIDENCE.read_text()) + + def test_driver_preflight_diagnostics_cannot_contaminate_smoke_flags(self): + native.PRIVATE.mkdir() + driver_label = 'Check disposable iOS driver readiness' + native.save({'commands': [{'label': driver_label}, + {'label': 'Assert startup and disposable passcode transition'}]}) + (native.PRIVATE / 'command-001.log').write_text('IOSDriverTimeoutException PRIVATE_SENTINEL\n') + (native.PRIVATE / 'command-002.log').write_text('ordinary smoke output PRIVATE_SENTINEL\n') + state = native.PRIVATE / 'maestro-preflight-state/maestro/tests/timestamp' + state.mkdir(parents=True) + (state / 'maestro.log').write_text('** TEST EXECUTE FAILED ** PRIVATE_SENTINEL\n') + driver = native.maestro_preflight_diagnostics(state.parent, driver_label) + smoke = native.maestro_preflight_diagnostics() + self.assertTrue(driver['xcode_test_execute_failed']) + self.assertEqual(driver['exception_types'], ['IOSDriverTimeoutException']) + self.assertFalse(smoke['xcode_test_execute_failed']) + self.assertEqual(smoke['exception_types'], []) + self.assertNotIn('PRIVATE_SENTINEL', json.dumps(driver)) + + def test_maestro_preflight_bounds_files_bytes_and_collection_errors_without_changing_failure(self): + output = native.PRIVATE / 'maestro-output' + output.mkdir(parents=True) + native.save({'commands': [], 'smoke': 'FAIL'}) + (output / 'maestro.log').write_bytes( + b'PRIVATE_SENTINEL' * (1024 * 1024 // 8) + b'\nInstalling driver with xcodebuild\n') + for index in range(7): + (output / ('xctest_runner_%s.log' % index)).write_text('PRIVATE_SENTINEL\n') + result = native.maestro_preflight_diagnostics() + self.assertEqual(result['logs_read'], 6) + self.assertTrue(result['log_limit_reached']) + self.assertEqual(result['truncated_logs'], 1) + self.assertFalse(result['xcodebuild_driver_selected']) + self.assertNotIn('PRIVATE_SENTINEL', json.dumps(result)) + with mock.patch.object(native, 'private_log_prefix', side_effect=OSError('PRIVATE_SENTINEL')): + result = native.maestro_preflight_diagnostics() + self.assertEqual(result['collection_error_types'], ['OSError']) + self.assertEqual(native.read()['smoke'], 'FAIL') + + def test_ios_startup_errors_restrict_process_and_export_only_a_fixed_category(self): + payload = '\n'.join([ + 'Filtering the log data using the process predicate', + 'Timestamp Ty Process[PID:TID]', + '2026-10-17 09:22:33.123 E hexa_keeper_dev[123:abc] [com.facebook.react:javascript] NSException: module failed token=private; UI hierarchy: PRIVATE_SENTINEL', + 'private stack continuation', + '2026-10-17 09:22:34.123 E OtherApp[124:def] Error: hexa_keeper_dev private unrelated application', + '2026-10-17 09:22:35.123 Df hexa_keeper_dev[123:abc] normal startup message', + '2026-10-17 09:22:36.123 Df hexa_keeper_dev[123:abc] ReferenceError: missing startup module', + 'PRIVATE_SENTINEL multiline continuation', + '2026-10-17 09:22:37.123+0000 Df hexa_keeper_dev[123:abc] NSException: failed native startup', + 'PRIVATE_SENTINEL multiline continuation', + 'Error: hexa_keeper_dev[123:abc] missing timestamp must not be accepted', + ]) + diagnostics = native.ios_startup_diagnostics(payload, 'hexa_keeper_dev') + errors = diagnostics['errors'] + self.assertEqual(diagnostics['matching_process_records'], 4) + self.assertEqual(errors, ['IOS_STARTUP_ERROR']) + self.assertEqual(native.ios_startup_diagnostics('no compact records', 'hexa_keeper_dev')['matching_process_records'], 0) + + def test_ios_collector_keeps_launch_prefix_when_log_is_truncated_and_preserves_failure(self): + native.PRIVATE.mkdir() + native.save({'commands': [], 'smoke': 'FAIL', 'built_artifact': {'executable': 'hexa_keeper_dev'}}) + (native.PRIVATE / 'metro.log').write_text('\x1b[32m BUNDLE \x1b[0m private-path/index.js\n' + ' BUNDLE private-path/index.js\n PREBUNDLE private-path/index.js\n') + + def compact_log(args, stdout, **kwargs): + self.assertEqual(args[args.index('--style') + 1], 'compact') + self.assertEqual(args[args.index('--last') + 1], '10m') + stdout.write(b'2026-10-17 09:22:33.123 Df hexa_keeper_dev[123:abc] ReferenceError: launch module failed token=private\n') + stdout.write(b'2026-10-17 09:22:34.123 E OtherApp[124:def] Error: private unrelated data\n') + # The first MiB ends within a continuation, not a complete record. + stdout.write(b'PRIVATE_SENTINEL' * (1024 * 1024 // 8)) + stdout.write(b'\n2026-10-17 09:30:33.123 E hexa_keeper_dev[123:abc] Error: tail must not replace launch evidence\n') + return subprocess.CompletedProcess(args, 0) + + with mock.patch.object(native, 'PLATFORM', 'IOS'), \ + mock.patch.object(subprocess, 'run', side_effect=compact_log): + native.record_startup_failure(native.PRIVATE / 'missing-report.xml', 'owned-simulator') + data = native.read() + self.assertEqual(data['smoke'], 'FAIL') + diagnostics = data['startup_diagnostics']['ios_errors'] + self.assertTrue(diagnostics['input_truncated']) + self.assertEqual(diagnostics['matching_process_records'], 1) + self.assertEqual(diagnostics['errors'], ['IOS_STARTUP_ERROR']) + self.assertEqual(data['startup_diagnostics']['metro_progress'], + {'bundle_marker_observed': True, 'bundle_marker_count': 2, 'prebundle_marker_count': 1}) + self.assertNotIn('private unrelated', native.EVIDENCE.read_text()) + self.assertNotIn('PRIVATE_SENTINEL', native.EVIDENCE.read_text()) + self.assertNotIn('private-path', native.EVIDENCE.read_text()) + + def test_warmup_keeps_each_platform_body_private_and_does_not_supply_smoke_evidence(self): + native.PRIVATE.mkdir() + body = b'disposable development bundle PRIVATE_SENTINEL' + + def warmup(args, *unused, **kwargs): + self.assertEqual(args[-1], 'http://127.0.0.1:8081/index.bundle?platform=%s&dev=true&minify=false' + % platform.lower()) + Path(args[args.index('--output') + 1]).write_bytes(body) + return '200' + + for platform in ['IOS', 'ANDROID']: + with self.subTest(platform=platform), mock.patch.object(native, 'PLATFORM', platform), \ + mock.patch.object(native, 'command', side_effect=warmup): + native.warm_metro_bundle() + data = native.read() + self.assertEqual(data['smoke'], 'NOT RUN') + self.assertEqual(data['metro_warmup']['platform'], platform.lower()) + self.assertEqual(data['metro_warmup']['result'], 'PASS') + self.assertEqual(data['metro_warmup']['http_status'], 200) + self.assertEqual(data['metro_warmup']['bytes'], len(body)) + self.assertEqual(data['metro_warmup']['sha256'], hashlib.sha256(body).hexdigest()) + self.assertNotIn('PRIVATE_SENTINEL', native.EVIDENCE.read_text()) + with mock.patch.object(native, 'command', side_effect=RuntimeError('HTTP error')), \ + self.assertRaisesRegex(RuntimeError, 'HTTP error'): + native.warm_metro_bundle() + self.assertEqual(native.read()['metro_warmup']['result'], 'FAIL') + self.assertEqual(native.read()['smoke'], 'NOT RUN') + with mock.patch.object(native, 'PLATFORM', 'unsupported&private=value'), \ + mock.patch.object(native, 'command') as command, \ + self.assertRaisesRegex(ValueError, 'Unsupported native bundle platform'): + native.warm_metro_bundle() + command.assert_not_called() + + def test_ios_collector_rejects_unsafe_executable_without_running_command(self): + native.PRIVATE.mkdir() + native.save({'commands': [], 'smoke': 'FAIL', 'built_artifact': {'executable': '../unsafe"'}}) + (native.PRIVATE / 'metro.log').write_text('') + with mock.patch.object(native, 'PLATFORM', 'IOS'), mock.patch.object(subprocess, 'run') as run: + native.record_startup_failure(native.PRIVATE / 'missing-report.xml', 'owned-simulator') + run.assert_not_called() + self.assertEqual(native.read()['smoke'], 'FAIL') + self.assertIn({'stage': 'ios_errors', 'error_type': 'ValueError'}, + native.read()['startup_diagnostics']['collection_errors']) + + def test_diagnostic_failure_never_replaces_original_smoke_failure(self): + native.PRIVATE.mkdir() + identity = {'sha256': 'test artifact'} + native.save({'commands': [], 'build': 'PASS', 'smoke': 'NOT RUN', 'built_artifact': identity}) + metro = mock.Mock() + metro.poll.return_value = 1 + with mock.patch.object(native, 'artifact_identity', return_value=identity), \ + mock.patch.object(subprocess, 'Popen', return_value=metro), \ + mock.patch.object(native, 'record_startup_failure', side_effect=RuntimeError('collector failed')), \ + self.assertRaisesRegex(RuntimeError, 'Metro exited before the disposable smoke'): + native.run_phase('smoke') + self.assertEqual(native.read()['smoke'], 'FAIL') + self.assertEqual(native.read()['startup_diagnostics']['collection_errors'], + [{'stage': 'collector', 'error_type': 'RuntimeError'}]) + + def test_missing_executable_is_recorded(self): + with self.assertRaisesRegex(RuntimeError, 'exit 127'): + native.command([str(self.root / 'missing-command')], 'Missing') + self.assertEqual(native.read()['commands'][0]['exit_code'], 127) + + def test_record_rejects_wrong_source_sha_before_collecting_evidence(self): + before = native.EVIDENCE.read_bytes() + with mock.patch.dict(native.os.environ, {'KEEPER_SOURCE_SHA': 'b' * 40}), \ + mock.patch.object(subprocess, 'check_output', return_value='a' * 40 + '\n'), \ + self.assertRaisesRegex(RuntimeError, 'exact requested PR head'): + native.record() + self.assertEqual(native.EVIDENCE.read_bytes(), before) + + def test_android_fixtures_use_only_dummy_project_and_loopback_services(self): + (self.root / 'android/app').mkdir(parents=True) + native.fixtures() + environment = (self.root / '.env.ci').read_text() + self.assertIn('ENVIRONMENT=DEVELOPMENT', environment) + for line in environment.splitlines(): + if 'https://' in line: + self.assertIn('127.0.0.1:9', line) + firebase = json.loads((self.root / 'android/app/google-services.json').read_text()) + self.assertEqual(firebase['project_info']['project_id'], 'keeper-disposable-ci') + settings = json.loads((self.root / 'firebase.json').read_text())['react-native'] + self.assertFalse(settings['messaging_auto_init_enabled']) + self.assertFalse(settings['app_data_collection_default_enabled']) + for overlay in native.read()['configuration_overlays']: + self.assertEqual(overlay['sha256'], hashlib.sha256((self.root / overlay['path']).read_bytes()).hexdigest()) + self.assertEqual(len(native.read()['configuration_overlays']), 3) + + def test_ios_fixtures_cover_both_tracked_firebase_files(self): + (self.root / 'ios/GoogleServices/dev').mkdir(parents=True) + with mock.patch.object(native, 'PLATFORM', 'IOS'): + native.fixtures() + paths = {overlay['path'] for overlay in native.read()['configuration_overlays']} + self.assertEqual(paths, {'.env.ci', 'firebase.json', 'ios/GoogleService-Info.plist', + 'ios/GoogleServices/dev/GoogleService-Info.plist'}) + + def test_emulator_revision_comes_from_sdk_metadata_without_executing_binary(self): + emulator = self.root / 'emulator' + emulator.mkdir() + (emulator / 'source.properties').write_text('Pkg.Revision = 36.6.11\nUnrelated = ignored\n') + with mock.patch.object(subprocess, 'Popen') as popen: + self.assertEqual(native.android_sdk_metadata(self.root, 'emulator'), {'Pkg.Revision': '36.6.11'}) + popen.assert_not_called() + + def test_emulator_metadata_must_exist_and_contain_revision(self): + with self.assertRaises(FileNotFoundError): + native.android_sdk_metadata(self.root, 'emulator') + emulator = self.root / 'emulator' + emulator.mkdir() + for text in ['Pkg.Desc = Android Emulator\n', 'Pkg.Revision = \n']: + with self.subTest(properties=text): + (emulator / 'source.properties').write_text(text) + with self.assertRaisesRegex(RuntimeError, 'no Pkg.Revision metadata'): + native.android_sdk_metadata(self.root, 'emulator') + + def test_smoke_requires_successful_build(self): + with mock.patch.object(subprocess, 'Popen') as popen, \ + self.assertRaisesRegex(RuntimeError, 'successful native build'): + native.smoke() + popen.assert_not_called() + + def test_ios_build_rejects_unsupported_architecture_before_invoking_xcode(self): + with mock.patch.object(native, 'PLATFORM', 'IOS'), \ + mock.patch.object(native.platform, 'machine', return_value='riscv64'), \ + mock.patch.object(native, 'command') as command, \ + self.assertRaisesRegex(RuntimeError, 'Unsupported hosted iOS simulator architecture'): + native.build() + command.assert_not_called() + + def test_ios_build_uses_only_the_supported_host_simulator_architecture(self): + for architecture in ['arm64', 'x86_64']: + with self.subTest(architecture=architecture), \ + mock.patch.object(native, 'PLATFORM', 'IOS'), \ + mock.patch.object(native.platform, 'machine', return_value=architecture), \ + mock.patch.object(native, 'artifact_identity', return_value={'sha256': 'test artifact'}), \ + mock.patch.object(native, 'command') as command: + native.build() + arguments = command.call_args.args[0] + self.assertIn('ARCHS=' + architecture, arguments) + self.assertIn('ONLY_ACTIVE_ARCH=YES', arguments) + self.assertIn('CODE_SIGNING_ALLOWED=NO', arguments) + self.assertEqual(command.call_args.kwargs['timeout'], 2400) + self.assertEqual(native.read()['toolchain']['ios_simulator_architecture'], architecture) + + def test_artifact_hash_detects_changed_apk_before_smoke(self): + apk = self.root / 'android/app/build/outputs/apk/development/debug/app-development-debug.apk' + apk.parent.mkdir(parents=True) + apk.write_bytes(b'first disposable artifact') + identity = native.artifact_identity() + self.assertEqual(identity['sha256'], hashlib.sha256(apk.read_bytes()).hexdigest()) + native.save({'commands': [], 'build': 'PASS', 'smoke': 'NOT RUN', 'built_artifact': identity}) + apk.write_bytes(b'replacement artifact') + with mock.patch.object(subprocess, 'Popen') as popen, \ + self.assertRaisesRegex(RuntimeError, 'artifact changed'): + native.smoke() + popen.assert_not_called() + + def test_failed_build_and_smoke_are_never_recorded_as_pass(self): + for phase in ['build', 'smoke']: + with self.subTest(phase=phase), \ + mock.patch.object(native, phase, side_effect=RuntimeError('failure')), \ + self.assertRaisesRegex(RuntimeError, 'failure'): + native.run_phase(phase) + self.assertEqual(native.read()[phase], 'FAIL') + self.assertEqual(native.read()['failed_phase'], phase) + + +if __name__ == '__main__': + unittest.main() diff --git a/.github/workflows/keeper-hosted-native.yml b/.github/workflows/keeper-hosted-native.yml new file mode 100644 index 000000000..3d198b099 --- /dev/null +++ b/.github/workflows/keeper-hosted-native.yml @@ -0,0 +1,150 @@ +name: Keeper hosted native validation + +on: + pull_request: + branches: [sprint] + types: [opened, synchronize, reopened, ready_for_review, converted_to_draft] + +# PR code runs only in disposable, unprivileged jobs. No signing or service secrets. +permissions: + contents: read + +concurrency: + group: keeper-hosted-native-${{ github.event.pull_request.number }} + cancel-in-progress: true + +env: + CI: 'true' + HUSKY: '0' + KEEPER_SOURCE_SHA: ${{ github.event.pull_request.head.sha }} + MAESTRO_CLI_NO_ANALYTICS: '1' + +jobs: + ios: + name: Hosted iOS simulator + # Skip before allocating a runner; missing/non-boolean draft state fails closed. + if: github.event_name == 'pull_request' && toJSON(github.event.pull_request.draft) == 'false' + runs-on: macos-15 + timeout-minutes: 65 + env: + KEEPER_NATIVE_PLATFORM: IOS + DEVELOPER_DIR: /Applications/Xcode_16.4.app/Contents/Developer + steps: + - name: Check out exact app source + uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6 + with: + ref: ${{ github.event.pull_request.head.sha }} + persist-credentials: false + - name: Set up Node + uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6 + with: + node-version: '22.22.0' + cache: yarn + - name: Set up Java for Maestro + uses: actions/setup-java@b6effb05e454b25005698d916606bdc6ffcbf961 # v5 + with: + distribution: temurin + java-version: '17' + - name: Set up Ruby + uses: ruby/setup-ruby@675dd7ba1b06c8786a1480d89c384f5620a42647 # v1.281.0 + with: + ruby-version: '3.3.8' + - name: Record runner and toolchain + run: python3 .github/native/hosted_native.py record + - name: Install immutable native dependencies + run: python3 .github/native/hosted_native.py prepare + - name: Check disposable iOS driver readiness + run: | + test "$(git rev-parse HEAD)" = '${{ github.event.pull_request.head.sha }}' + git diff --exit-code HEAD -- .github/native .github/workflows/keeper-hosted-native.yml + python3 .github/native/hosted_native.py driver + - name: Build development app + run: | + test "$(git rev-parse HEAD)" = '${{ github.event.pull_request.head.sha }}' + git diff --exit-code HEAD -- .github/native .github/workflows/keeper-hosted-native.yml + python3 .github/native/hosted_native.py build + - name: Run disposable startup smoke + run: | + test "$(git rev-parse HEAD)" = '${{ github.event.pull_request.head.sha }}' + git diff --exit-code HEAD -- .github/native .github/workflows/keeper-hosted-native.yml + python3 .github/native/hosted_native.py smoke + - name: Retain safe native evidence + if: always() + uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6 + with: + name: keeper-native-ios-${{ github.event.pull_request.head.sha }} + path: | + artifacts/native/ios/metadata.json + artifacts/native/ios/Podfile.lock.candidate + if-no-files-found: warn + retention-days: 7 + + android: + name: Hosted Android emulator + if: github.event_name == 'pull_request' && toJSON(github.event.pull_request.draft) == 'false' + runs-on: ubuntu-24.04 + timeout-minutes: 60 + env: + KEEPER_NATIVE_PLATFORM: ANDROID + steps: + - name: Check out exact app source + uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6 + with: + ref: ${{ github.event.pull_request.head.sha }} + persist-credentials: false + - name: Set up Node + uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6 + with: + node-version: '22.22.0' + cache: yarn + - name: Set up Java + uses: actions/setup-java@b6effb05e454b25005698d916606bdc6ffcbf961 # v5 + with: + distribution: temurin + java-version: '17' + cache: gradle + - name: Set up Android command-line tools + uses: android-actions/setup-android@be39fa834029ff78f1a44aa3bb0819b8fc2bd8fd # v4 + with: + cmdline-tools-version: '13114758' + packages: platform-tools + log-accepted-android-sdk-licenses: false + - name: Record runner and toolchain + run: python3 .github/native/hosted_native.py record + - name: Install immutable native dependencies + run: python3 .github/native/hosted_native.py prepare + - name: Build development app + run: | + test "$(git rev-parse HEAD)" = '${{ github.event.pull_request.head.sha }}' + git diff --exit-code HEAD -- .github/native .github/workflows/keeper-hosted-native.yml + python3 .github/native/hosted_native.py build + - name: Enable emulator acceleration + run: | + echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-keeper-kvm.rules + sudo udevadm control --reload-rules + sudo udevadm trigger --name-match=kvm + - name: Run disposable startup smoke + uses: reactivecircus/android-emulator-runner@1dcd0090116d15e7c562f8db72807de5e036a4ed # v2.34.0 (peeled commit) + with: + api-level: 35 + target: google_apis + arch: x86_64 + profile: pixel_6 + cores: 2 + ram-size: 3072M + force-avd-creation: true + disable-animations: true + emulator-boot-timeout: 240 + emulator-options: -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -no-snapshot -camera-back none -camera-front none + script: | + test "$(git rev-parse HEAD)" = '${{ github.event.pull_request.head.sha }}' + git diff --exit-code HEAD -- .github/native .github/workflows/keeper-hosted-native.yml + python3 .github/native/hosted_native.py smoke + - name: Retain safe native evidence + if: always() + uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6 + with: + name: keeper-native-android-${{ github.event.pull_request.head.sha }} + path: artifacts/native/android/metadata.json + if-no-files-found: warn + retention-days: 7 diff --git a/.github/workflows/keeper-local-validation.yml b/.github/workflows/keeper-local-validation.yml index d66fc70f8..af64b8c8a 100644 --- a/.github/workflows/keeper-local-validation.yml +++ b/.github/workflows/keeper-local-validation.yml @@ -7,21 +7,25 @@ on: types: [opened, reopened, synchronize, edited, ready_for_review] issue_comment: types: [created, edited, deleted] + workflow_run: + workflows: [Keeper hosted native validation] + types: [completed, requested, in_progress] permissions: contents: read issues: read pull-requests: read + actions: read statuses: write concurrency: - group: keeper-local-validation-${{ github.event.pull_request.number || github.event.issue.number }} + group: keeper-local-validation-${{ github.event.pull_request.number || github.event.issue.number || github.event.workflow_run.pull_requests[0].number || github.run_id }} cancel-in-progress: false jobs: keeper-local-validation: name: Keeper local validation gate - if: github.event_name == 'pull_request_target' || github.event.issue.pull_request != null + if: github.event_name == 'pull_request_target' || github.event.issue.pull_request != null || github.event.workflow_run.event == 'pull_request' runs-on: ubuntu-latest steps: - name: Verify local validation for exact PR commit @@ -30,7 +34,9 @@ jobs: script: | const owner = context.repo.owner; const repo = context.repo.repo; - const pull_number = context.payload.pull_request?.number ?? context.issue.number; + const pull_number = context.payload.pull_request?.number ?? + context.payload.workflow_run?.pull_requests?.[0]?.number ?? context.issue.number; + if (!pull_number) return; const { data: pr } = await github.rest.pulls.get({ owner, repo, pull_number }); if (pr.base.ref !== 'sprint') { core.info('Only PRs targeting sprint use this validation gate.'); @@ -87,6 +93,75 @@ jobs: return allowed; }; + // PR workflows are editable by the PR author. A successful check name + // alone is NOT evidence. All executable CI files must match both the + // protected current base and the base used by that particular run. + // This intentionally rejects this infrastructure PR's bootstrap runs. + const hostedPasses = async () => { + const passed = new Set(); + try { + const workflowPath = '.github/workflows/keeper-hosted-native.yml'; + const repository = owner + '/' + repo; + if (pr.head.repo?.full_name !== repository) return passed; + const runs = await github.paginate(github.rest.actions.listWorkflowRuns, { + owner, repo, workflow_id: 'keeper-hosted-native.yml', + head_sha: sha, event: 'pull_request', per_page: 100 + }); + // Do not fall back to an older success after a failed/cancelled + // rerun or while newer evidence is pending. + const run = runs.sort((a, b) => b.id - a.id)[0]; + if (!run || run.head_sha !== sha || run.event !== 'pull_request' || + run.path !== workflowPath || run.head_branch !== pr.head.ref || + run.head_repository?.full_name !== repository || + run.status !== 'completed' || !['success', 'failure'].includes(run.conclusion)) { + return passed; + } + const linked = run.pull_requests?.find(p => p.number === pull_number); + if (!linked || linked.head?.sha !== sha || linked.head?.ref !== pr.head.ref || + linked.base?.ref !== 'sprint' || !linked.base?.sha || + linked.head?.repo?.id !== pr.head.repo.id || + linked.base?.repo?.id !== pr.base.repo?.id || + !pr.head.repo.id || !pr.base.repo?.id || + !Number.isSafeInteger(run.run_attempt) || run.run_attempt < 1) return passed; + const fingerprint = async ref => { + const { data } = await github.rest.git.getTree({ owner, repo, tree_sha: ref, recursive: '1' }); + if (data.truncated) throw new Error('Truncated trusted CI tree'); + const entries = data.tree.filter(e => e.path === workflowPath || + e.path.startsWith('.github/native/')); + if (!entries.some(e => e.path === workflowPath) || + !entries.some(e => e.path.startsWith('.github/native/') && e.type === 'blob')) { + throw new Error('Hosted native infrastructure not present in trusted base'); + } + return JSON.stringify(entries.map(e => [e.path, e.mode, e.type, e.sha]) + .sort((a, b) => a[0].localeCompare(b[0]))); + }; + const [headFiles, baseFiles, runBaseFiles] = await Promise.all([ + fingerprint(sha), fingerprint(pr.base.sha), fingerprint(linked.base.sha) + ]); + if (headFiles !== baseFiles || headFiles !== runBaseFiles) return passed; + const jobs = await github.paginate(github.rest.actions.listJobsForWorkflowRunAttempt, { + owner, repo, run_id: run.id, attempt_number: run.run_attempt, per_page: 100 + }); + for (const [platform, name] of [['IOS', 'Hosted iOS simulator'], + ['ANDROID', 'Hosted Android emulator']]) { + const matches = jobs.filter(j => j.name === name); + if (matches.length !== 1) continue; + const job = matches[0]; + if (job.head_sha !== sha || job.run_id !== run.id || job.run_attempt !== run.run_attempt || + job.status !== 'completed' || job.conclusion !== 'success') continue; + const steps = ['Build development app', 'Run disposable startup smoke']; + if (steps.every(name => job.steps?.filter(s => s.name === name && + s.status === 'completed' && s.conclusion === 'success').length === 1)) { + passed.add(platform); + core.info(platform + ' hosted evidence: ' + run.html_url + ', attempt ' + run.run_attempt); + } + } + } catch (err) { + core.warning('Hosted evidence unavailable; local fallback retained: ' + err.message); + } + return passed; + }; + let state = 'failure'; let description = 'Awaiting local Mac validation'; if (docsOnly) { @@ -129,6 +204,7 @@ jobs: if (malformed) { description = 'Specify Required local checks: IOS, ANDROID, HARDWARE'; } else { + const hosted = await hostedPasses(); // Only the release owner can defer unrun physical checks. Keep // every original handoff intact, and bind the newest owner // decision to this PR and this exact head commit. @@ -165,9 +241,7 @@ jobs: break; } } - if (!report) { - description = 'Awaiting trusted Mac validation of current SHA'; - } else if (field(report, 'State') !== 'LOCAL_PASSED') { + if (report && field(report, 'State') !== 'LOCAL_PASSED') { description = 'Latest local validation is not LOCAL_PASSED'; } else { const reportFields = { @@ -179,10 +253,12 @@ jobs: const hardwareDeferred = required.has('HARDWARE') && hardwareDeferral && reportIndex > ownerDecisionIndex && /^(not run|deferred)\b/i.test(hardwareResult); - const notPassed = [...required].filter(x => - !/^pass\b/i.test(field(report, reportFields[x])) && - !(x === 'HARDWARE' && hardwareDeferred) - ); + const notPassed = [...required].filter(x => { + const result = field(report, reportFields[x]); + if (/^fail(?:ed)?\b/i.test(result)) return true; + return !/^pass\b/i.test(result) && !hosted.has(x) && + !(x === 'HARDWARE' && hardwareDeferred); + }); if (hardwareDeferred && !required.has('IOS') && !required.has('ANDROID')) { description = 'IOS or ANDROID required before HARDWARE deferral'; } else if (notPassed.length) { @@ -190,9 +266,10 @@ jobs: } else { state = 'success'; const passed = [...required].filter(x => - /^pass\b/i.test(field(report, reportFields[x])) + /^pass\b/i.test(field(report, reportFields[x])) || hosted.has(x) ); description = 'Validated at current SHA: ' + passed.join(', ') + + (hosted.size ? '; hosted ' + [...hosted].join(', ') : '') + (hardwareDeferred ? '; owner-deferred HARDWARE' : ''); } } @@ -200,6 +277,13 @@ jobs: } } + // API reads above can take time. Never post a success for a head that + // changed while evidence was being collected. + const { data: current } = await github.rest.pulls.get({ owner, repo, pull_number }); + if (current.head.sha !== sha) { + core.setFailed('PR head changed during validation; awaiting new-head evaluation'); + return; + } await github.rest.repos.createCommitStatus({ owner, repo, sha, state, context: contextName, diff --git a/.github/workflows/keeper-validation-tests.yml b/.github/workflows/keeper-validation-tests.yml new file mode 100644 index 000000000..7bbc3b67d --- /dev/null +++ b/.github/workflows/keeper-validation-tests.yml @@ -0,0 +1,26 @@ +name: Native validation contracts + +on: + pull_request: + branches: [sprint] + push: + branches: [sprint] + paths: ['.github/**'] + +permissions: + contents: read + +jobs: + contracts: + name: Native validation contracts + runs-on: ubuntu-24.04 + timeout-minutes: 5 + steps: + - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6 + with: + persist-credentials: false + - uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6 + with: + node-version: '22' + - run: node --test .github/tests/*.test.cjs + - run: python3 -B -m unittest discover -s .github/tests -p 'test_*.py' diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5d6104f0a..17673bae8 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -11,6 +11,7 @@ on: - sprint - dev - main + types: [opened, synchronize, reopened, ready_for_review] permissions: contents: read @@ -49,7 +50,14 @@ jobs: with: node-version: '22' + # Draft PRs must not invoke package.json prepare -> setup.sh -> CocoaPods. + # Keep the normal required Tests job; install/test errors still fail it. + - name: Install draft dependencies without lifecycle scripts + if: github.event_name == 'pull_request' && toJSON(github.event.pull_request.draft) != 'false' + run: yarn install --frozen-lockfile --ignore-scripts + - name: Install dependencies + if: github.event_name != 'pull_request' || toJSON(github.event.pull_request.draft) == 'false' run: yarn install - name: Run tests diff --git a/AGENTS.md b/AGENTS.md index db639d581..871210bf4 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1,6 +1,6 @@ # Bitcoin Keeper: Codex workflow -This repository uses a **ChatGPT → Codex Cloud → Codex Local (Mac)** workflow for coding tasks. The GitHub pull request is the durable handoff between stages. Do **not** rely on one agent having access to another agent's conversation, unpushed changes, machine, or installed tools. +The prepared workflow is **Cloud code complete → hosted native validation → authorized physical checks → release review**. Until the hosted infrastructure is reviewed, installed and verified, retain the existing Local path below as the safe fallback; do not assume the owner's Mac is available. Signing remains a separate approved action. Follow the [bootstrap adoption order](docs/cloud-bootstrap-preparation.md), including the later restacking of Phase A #7062. The GitHub pull request is the durable handoff between stages. Do **not** rely on one agent having access to another agent's conversation, unpushed changes, machine, or installed tools. Repository: `KeeperCommunity/bitcoin-keeper`. Use `sprint` as the PR base unless the task explicitly specifies another base. @@ -26,7 +26,7 @@ Repository: `KeeperCommunity/bitcoin-keeper`. Use `sprint` as the PR base unless ``` 4. For app/code changes, `Required local checks` must list one or more of `IOS`, `ANDROID`, `HARDWARE` (comma-separated). Use both iOS and Android for cross-platform changes; add hardware for signer/device interactions. If no Mac tests are necessary, explain why, but the GitHub gate exempts only documentation and `.github/` changes. The handoff must name the actual PR head commit SHA after the final cloud push. -5. Return the PR URL/number and state `NEEDS_LOCAL` to the user so ChatGPT can generate the exact local handoff prompt. Continue unrelated cloud tasks while waiting for a Mac. +5. Return the PR URL/number and actual platform `PASS`/`FAIL`/`NOT RUN` results for the current head. Use `NEEDS_LOCAL` for remaining native or physical requirements; successful trusted hosted evidence may satisfy `IOS`/`ANDROID` without a Mac login. Continue unrelated cloud tasks while waiting for a Mac. Follow [hosted native validation](docs/hosted-native-validation.md) for trust, bootstrap, scope and artifact limitations. 6. If a task needs only code review, explicitly state why local testing is not required rather than silently skipping it. ## Codex Local (Mac): validate the PR @@ -84,6 +84,6 @@ A known critical defect is not eligible for post-release deferral. In particular ## GitHub merge gate -The `.github/workflows/keeper-local-validation.yml` workflow posts commit status `keeper/local-validation` for the current PR head SHA. For code changes, it remains failing until a repository collaborator posts a `Codex local validation` comment with `State: LOCAL_PASSED`, the **exact current commit SHA**, and `pass` for every platform named by `Required local checks`, except `HARDWARE` when the exact-SHA owner decision above defers unrun physical checks. New commits invalidate previous local sign-off and deferral. Documentation and `.github/`-only PRs are exempt from Mac tests. +The `.github/workflows/keeper-local-validation.yml` workflow posts commit status `keeper/local-validation` for the current PR head SHA. For code changes, it requires a trusted exact-head Cloud handoff. Each required `IOS`/`ANDROID` platform needs either a genuine successful hosted native build **and** disposable UI smoke from `.github/workflows/keeper-hosted-native.yml`, or a repository collaborator's exact-head `LOCAL_PASSED` report. A hosted run is trusted only when its workflow and complete `.github/native/` harness match the protected current base and the run's original base; PR-authored check names, comments and artifacts alone are not CI evidence. New or changed CI infrastructure must first be reviewed and merged before it can provide authoritative native evidence. The latest run/attempt must contain successful build and smoke steps for that platform; stale, skipped, cancelled, pending, build-only or untrusted evidence supplies no hosted pass. A latest explicit local failure remains blocking. `HARDWARE` still requires the existing local physical result or exact-SHA owner decision and subsequent honest local report above. New commits invalidate previous native evidence, local sign-off and deferral. Documentation and `.github/`-only PRs are exempt from Mac tests. This workflow **reports** a status; to **enforce** it, repository administrators must require the `keeper/local-validation` commit status for merges to `sprint` in GitHub Rulesets or branch protection. Require the existing `Tests` CI check as well. This is a review/test gate, not a guarantee that tests were actually executed; agents must report actual results truthfully. Release signing, TestFlight, Play Store, and production deployment remain separate and require explicit user approval. diff --git a/docs/cloud-bootstrap-preparation.md b/docs/cloud-bootstrap-preparation.md new file mode 100644 index 000000000..70a5b4559 --- /dev/null +++ b/docs/cloud-bootstrap-preparation.md @@ -0,0 +1,175 @@ +# Cloud bootstrap preparation + +This is an unpublished infrastructure-only preparation from known committed +sources. It carries no native pass, app acceptance, signing or release approval. +No unpublished files from the original task or the owner's Mac were migrated or +backed up. The original task's working-tree state remains unknown. + +## Source identities + +Fresh branch/PR refs matched these checkpoints on 18 October 2026: + +| Source | Commit | +| --- | --- | +| sprint / bootstrap base | `def0d6a90c80287edb4f58ae272cbfc340f00d73` | +| #7060 `codex/7059-hosted-native-validation` | `d865ca9c4f91532a1524443cc6da42d8d7b07e1a` | +| #7062 `codex/7061-cloud-release-phase-a` | `59c821ce195a5c063d99045fd97b5ac605433f62` | +| R5 #7058 | `625435ac26af0304670c993a639626b1117699ba` | +| R4 #7045, excluded | `be89c34473bb6aab253b98325f1fe746ca391c6f` | + +#7060 descends from the verified sprint commit; #7062 is stacked on #7060. +The preparation reuses #7060's `.github/` infrastructure and relevant Markdown, +with diagnostic privacy corrections, action-pin regressions and these adoption +notes. Its gate implementation is unchanged from #7060. Original gate contract +cases are retained, with additional skipped-draft/latest-attempt regressions. +Provenance, latest-attempt, explicit-failure and hardware requirements +remain intact. No live protection, permission or credential setting changes. + +`package.json`, `yarn.lock`, `ios/Podfile.lock` and +`ios/hexa_keeper/AppDelegate.mm` remain at sprint. R4's assessment document, +source, refs, private fixtures and Mac work are not imported or changed. Phase A +app probes/scenario matrices and Phase B signing/distribution are excluded. + +## Diagnostic and action-pin correction + +General tool failures and Maestro, Android, Metro and iOS startup collectors +export fixed categories or fixed public startup labels. They never return +arbitrary log excerpts, including unknown errors. The legacy metadata field +`redacted_tool_errors` now contains category identifiers only. Synthetic privacy +regressions exercise unlabelled private text, wallet-like data, spaced secrets, +URLs/paths, encoded text, Unicode/control characters, multiline input and repeated +messages. Collector tests check metadata and console output while retaining +failed command and smoke outcomes. Raw logs remain private to the disposable +runner. Categories indicate observed markers, not a diagnosis or a platform pass. + +The emulator action uses upstream commit +`1dcd0090116d15e7c562f8db72807de5e036a4ed`, matching #7062. Read-only verification +of `ReactiveCircus/android-emulator-runner` on 18 October 2026 returned: + +```text +refs/tags/v2.34.0 324029e2f414c084d8b15ba075288885e74aef9c +refs/tags/v2.34.0^{} 1dcd0090116d15e7c562f8db72807de5e036a4ed +``` + +The old #7060 value appended `38` to the tag object's SHA and was malformed. +Host contracts require full 40-character immutable action pins in the two newly +introduced workflows, matching the upstream revisions verified on the same date, +including this exact emulator commit. They reject mutable tags, short/malformed +revisions, unverified actions/SHAs and the annotated tag object. Existing action policy +in unrelated workflows and the inherited gate is unchanged. + +Pin inspection parses YAML with the host's existing Python 3 and PyYAML, then +checks every job-level and step-level `uses` value. Quoted/escaped keys, flow +mappings, folded scalars and aliases cannot hide mutable actions alongside valid +pins. Missing parser tooling, invalid YAML, duplicate mapping keys and non-string +action references fail the contract; there is no regex fallback or dependency +installation. Future host-contract environments must provide Python 3 and PyYAML. + +## Draft publication behavior + +Both hosted native jobs require a `pull_request` event with a literal boolean +`draft: false` before allocating a runner. Drafts and missing/malformed draft +state cannot start either native job. `ready_for_review` starts a new eligible +native run; marking a PR ready therefore requires separate native-run/budget +authorization. `converted_to_draft` triggers a skipped run in the same PR +concurrency group, configured to cancel an older native run. Cancellation is +best effort and cannot undo work already started while the PR was ready. + +The existing CI `Tests` install would otherwise invoke `prepare` and CocoaPods. +For draft PRs only, it now uses frozen Yarn with lifecycle scripts disabled. +The normal `Tests` job still runs and any install/Jest failure still fails it. +No app manifest, lock or test changes are included. Ready/non-PR installs retain +their existing behavior, and `ready_for_review` reruns CI so a draft installation +result cannot replace the normal ready-PR test run. Lint's existing install +already disables lifecycle scripts. The frozen dependency blocker is not waived. + +Automatic checks for this branch targeting sprint, from the checked-in triggers: + +| Event | Automatic behavior | +| --- | --- | +| Push `codex/cloud-bootstrap-privacy` with no PR | No checked-in workflow matches this branch's push event | +| Open/reopen a draft; push new commits to an open draft (`synchronize`) | Linux CodeQL; CI Lint and Tests; Node/Python Native validation contracts; existing trusted local-validation gate. Hosted iOS/Android jobs are skipped before runner allocation | +| Edit the draft PR description | Existing trusted local-validation gate refreshes; no native workflow trigger | +| Mark ready for review | Native jobs become eligible; CI reruns the normal install/tests; trusted gate refreshes. CodeQL and host contracts retain their existing default PR event types | +| Convert back to draft | Hosted-native concurrency cancellation/skip only; no new native runner. Previously started ready-PR CI is not retroactively undone | + +The trusted gate's existing docs/`.github` exemption can report success for this +infrastructure PR. That is not native acceptance or installation/merge approval. +For app PRs, skipped jobs/steps, even inside an overall successful workflow, supply +no native pass; a newer skipped run/attempt cannot reuse an older native success. +All provenance, explicit-failure and HARDWARE checks remain unchanged. + +This bootstrap contains no `workflow_call`, `workflow_dispatch` or job-level +reusable-workflow call. The entry-path contracts check that boundary. #7062's +`keeper-release-acceptance.yml` callers and `keeper-native-platform.yml` worker +remain outside this patch. Before their separate restack/adoption, add the same +draft guard at both caller jobs and the worker, retaining source/provenance/R4 +checks. The reusable worker receives the [caller's GitHub event context](https://docs.github.com/en/actions/reference/workflows-and-actions/reusing-workflow-configurations#github-context); a direct +non-PR caller must not bypass the guard. These future paths are not claimed fixed +or installed by this bootstrap. + +The remaining Linux checks consume hosted runner time. Available included minutes +and billing limits have not been verified, so this is not a zero-cost guarantee. +Confirm allowance before publishing under a no-paid-resources restriction; do not +disable required checks or change billing/security settings to bypass it. No +workflow has been dispatched or run as part of this source-only preparation. + +## Adoption order and remaining gates + +1. Independently review this infrastructure diff and its host contracts. Any + publication, PR change or merge requires separate owner authorization. Only a + reviewed installation on protected sprint can establish the workflow/harness + baseline. Candidate runs cannot authorize their own changed infrastructure. +2. Reconcile the app and dependency changes separately against the approved + source baseline. This bootstrap deliberately leaves frozen-install blockers + unresolved. Do not unlock dependency installation, weaken gates or treat host + contracts as native evidence. R4 work and the owner's Mac remain untouched. +3. Restack #7062's Phase A delta onto the adopted bootstrap in separately + authorized work. Do not merge its existing #7060 ancestry wholesale: that + would also import the four excluded app/dependency files. Reconcile + `hosted_native.py` changes while preserving the fixed-category exports and + privacy regressions; retain the corrected emulator pin and extend pin/draft + entry-path contracts and guards to Phase A's callers and worker. Update #7062's bootstrap references to + the actual adopted source. Phase A requires its own review and protected-base + installation before it can supply authoritative evidence. +4. After the source baseline and frozen dependencies are reconciled, obtain + authorization and budget for actual exact-source native runs using unchanged + reviewed infrastructure. Record source SHA, workflow/run/attempt, each real + build/UI outcome and safe artifact identities. New source commits invalidate + old passes. Missing, skipped, cancelled, untrusted or failed evidence does not + become a pass; explicit local failures and HARDWARE gates still apply. R5's + feature acceptance remains separate from this startup smoke. + +Signing, distribution, uploads, deployment and publication remain separately +approved operations. Phase B is not implemented or verified by this preparation. +The optional private signing follow-up note is not a Phase A prerequisite. + +## Validation scope + +Use `node --test .github/tests/*.test.cjs` and +`python3 -B -m unittest discover -s .github/tests -p 'test_*.py'`, plus available +workflow lint, syntax and diff checks. The host suite uses synthetic fixtures and +mocked native commands; it requires no app dependency installation, emulator, +native compilation or Actions dispatch. Report actual results in the private +handoff. A missing lint tool is a validation-tool blocker, distinct from a failed +host contract or the setup-reported frozen app dependency installation failure. + +Private preparation results on 18 October 2026: + +| Check | Result | +| --- | --- | +| Node host contracts | PASS: 53 tests, including gate, shim, action-pin and draft-entry contracts | +| Python host contracts | PASS: 35 tests, including 5 adversarial privacy tests with subcases | +| JavaScript and Python syntax | PASS | +| Changed YAML syntax | PASS: installed PyYAML, 5 files; not workflow semantic lint | +| Staged diff whitespace and scope | PASS: only `.github/` and relevant Markdown; excluded app files unchanged | +| Workflow semantic lint (`actionlint`) | NOT RUN: executable unavailable; nothing installed | +| App dependency installation / app tests | NOT RUN here; setup-reported frozen-install failure remains unresolved | +| Native builds, simulators/emulators, hardware | NOT RUN | +| Actions dispatch, signing/distribution, deployment | NOT RUN | + +The privacy regression suite was also applied to the original #7060 harness as a +negative control: all 5 tests failed (168 assertion/subcase failures, no execution +errors), confirming the tests detect the original exports. Those are intentional +baseline failures, not failures of this prepared diff. No live native evidence or +CI run from another SHA is credited to this preparation. diff --git a/docs/hosted-native-validation.md b/docs/hosted-native-validation.md new file mode 100644 index 000000000..019124473 --- /dev/null +++ b/docs/hosted-native-validation.md @@ -0,0 +1,198 @@ +# Hosted native validation + +This infrastructure-only bootstrap reuses issue #7059 / PR #7060's bounded hosted +development builds and disposable first-use UI smoke. It excludes that PR's app +and dependency changes. See [bootstrap preparation and adoption order](cloud-bootstrap-preparation.md) +for exact committed sources, exclusions and the relationship to Phase A #7062. +It does not change R4 #7045 or R5 #7058 app code, merge either branch, change branch +protection, or approve a release. Adopt it for R5 after R4's released baseline is +settled and R5 is reconciled against that baseline. + +## Handoff + +Draft publication skips both native jobs before runner allocation. The existing +host checks still run; the draft `Tests` dependency install disables lifecycle +scripts to avoid invoking native setup. See the [draft event table and budget +limits](cloud-bootstrap-preparation.md#draft-publication-behavior). Skipped native +jobs supply no platform pass. Marking ready for review enables native work and +requires separate authorization and budget; a draft is not a trust installation. + +1. **Cloud code complete:** push the final code, run suitable regression checks, + and record the full PR head in the existing `Codex local handoff`. Keep + `Required local checks: IOS, ANDROID` and add `HARDWARE` when applicable. +2. **Hosted native validation:** the PR workflow checks out that exact head with + read-only permissions and no persisted checkout credential. Both platform jobs + install locked dependencies, build the development app and run the disposable + startup smoke. Report each platform as `PASS`, `FAIL` or `NOT RUN` with the run + URL, attempt and tested SHA. A successful build without a successful UI smoke + is not a platform pass. Any new commit starts this step again. +3. **Remaining native and physical checks:** assign an authorized tester to the + remaining feature-specific cases, fallback builds/UI when hosted setup fails, + and physical-device cases; do not assume the owner's Mac is available. Real + biometrics, phones/cameras, hardware signers, NFC/USB and signing prompts remain + explicit. Preserve the existing owner-scoped HARDWARE deferral procedure. A + later local `LOCAL_FAILED`/`BLOCKED` report cannot be hidden by a hosted pass. +4. **Release review:** inspect the actual exact-head evidence and unresolved + issues, obtain independent review and explicit owner approval for any signing, + distribution or submission. CI success grants none of those approvals. + +Hosted coverage is deliberately narrow: cold startup to the passcode screen and +entry of a throwaway first passcode through the confirmation screen. Stop before +account/key generation, backup, wallet funding, live transactions or signer use. +Do not mistake this smoke for the R5 deletion matrix, recovery/upgrade validation, +startup performance acceptance or complete release readiness. + +## Trust and gate behavior + +The existing trusted base-branch gate never checks out or executes PR code and +retains its `keeper/local-validation` status name. The new native workflow uses +`pull_request`, never elevated `pull_request_target` execution. No signing, +environment secrets, release credentials or production configuration are passed +to its jobs. + +The gate reads Actions metadata itself. It selects the newest run at the current +source SHA, verifies the workflow path, event, source repository/branch, associated +PR and SHA, and compares every workflow/harness file (including additions and +deletions) against both the current base and the run's base. The latest attempt +must have exactly one successful platform job with successful `Build development +app` and `Run disposable startup smoke` steps. A cancelled/pending run supplies no +pass. A completed run whose other platform failed can still supply its genuinely +successful platform. Partial reruns may lack the other platform in the latest +attempt; use **Re-run all jobs** to establish both platforms together. + +Missing metadata, API failures, untrusted changed CI files and incomplete evidence +produce zero hosted passes. Existing local reports remain a fallback. HARDWARE +cannot be filled by simulator evidence. New commits also invalidate owner scope +decisions. Owner deferral still needs a later honest local report recording +physical checks as NOT RUN/DEFERRED; an actual failed physical test cannot be +deferred. No PR-author assertion of a hosted result is parsed as evidence. + +This verifies reviewed CI provenance and actual GitHub step results. The native +build still executes PR-controlled Gradle/Xcode/Metro code on its disposable +runner. Integrity checks detect tracked harness changes between phases, but do +not provide an adversarial sandbox against malicious same-user build scripts. +Review native source and CI changes before merging; branch review requirements +remain necessary. The gate does not make an unreviewed app trustworthy. + +Bootstrap is intentionally staged: this infrastructure PR can run diagnostics and +test the verifier, but its new workflow is not yet in the protected base, so its +hosted results cannot authorize app PRs. Review and merge the CI-only PR only when +the owner approves. Then a new same-repository PR run using unchanged reviewed CI +files can provide evidence. Fork runs and modified native CI need review/local +fallback. The workflow-run listener is also effective only after the trusted gate +is merged; pre-merge gate tests execute the candidate inline script with mocked +API responses, not a privileged checkout of the candidate. + +## Dependency and native-install failures + +Use Yarn 1's frozen lock install with lifecycle scripts disabled, followed by the +explicit CI preparation in `.github/native/`. The repository's normal `prepare` +unconditionally invokes CocoaPods and writes a Mac-specific Android SDK path; +running it on Linux is not supported. Do not silently regenerate `yarn.lock`, use +an unlocked install, skip patch failures or replace a failed native build with +Jest. CocoaPods deployment mode likewise exposes Podfile/lock drift. + +The pinned Node compatibility tool is restricted to setup.sh's existing shim +whitelist without invoking its installer. Unrestricted `--hack` rewrites imports +to optional packages that the manifest does not contain; the bounded wrapper +keeps both platform Metro graphs resolvable using committed dependencies. Android +command-line tools are provisioned explicitly before SDK/NDK installation. + +The #7060 source checkpoint documents dependency drift at sprint +`def0d6a90c80287edb4f58ae272cbfc340f00d73`: missing Yarn lock selectors and older +IAP/Nitro Pod-lock selections. Those app/dependency repairs are excluded here. +The private environment's setup reported a failed frozen app dependency install; +this preparation neither repeats nor repairs it. Host contracts do not validate +app installation or either native platform. + +The inherited native lock-repair path is diagnostic only: if separately +authorized, it fails without building/testing and retains a candidate lock for +review. It is not enabled or run by this bootstrap. Dependency repair belongs in +a separate reviewed source change; an authoritative native run must use frozen +Yarn and `pod install --deployment`. New source commits require new native results. +Do not import the ongoing R4 diagnostic/reproducibility branch or its private +fixtures. Its existing Android build diagnostics do not constitute emulator UI +evidence. + +## Evidence, runner budget and remaining work + +Each platform retains allowlisted source/toolchain/runner metadata, actual command +exit outcomes and fixed diagnostic categories. Diagnostic Pod-lock repair +may also retain its dependency-lock candidate; it cannot report a platform pass. +Development configuration points service integrations at +loopback and uses dummy Firebase identities. Only the bounded first-use scenario +runs on fresh hosted devices. Raw build/device/Metro/Maestro logs, screenshots, +app bundles, keystores and generated account data are not uploaded. Failed raw +command output stays private to the ephemeral runner; metadata records which +command failed. Keep artifacts for seven days. + +On startup failure, metadata may include fixed Maestro command/assertion labels, +Metro failure categories and Android/iOS error categories from this job's fresh +account-free device. No error message, path, arbitrary exception name or suffix is +copied from those logs. The schema-1 `redacted_tool_errors` field retains its name +for compatibility but now holds only fixed category identifiers. Unrecognized +text supplies no category; an empty diagnostic list never means success. CLI +failures also omit arbitrary exception text. Diagnostic collection cannot change +a failed result. Cold development +bundling has a three-minute initial-screen allowance and seven-minute Maestro +command limit, with all UI assertions still required. This is not release +performance acceptance. The iOS build command is limited to the hosted +simulator's architecture and 40 minutes; Android's build command is capped at +35 minutes. + +Before the iOS build, a separate disposable simulator checks Maestro's driver +with a bounded `hierarchy` command. This is setup only: it installs no Keeper app +and supplies no app build or UI credit. Its private XDG state and fixed diagnostic +flags remain separate from the later smoke. The command is capped at five +minutes, with Maestro's driver timeout unchanged; owned simulator cleanup runs +on success or failure. + +For both platforms, a bounded loopback request compiles the development bundle +before iOS simulator creation or Android app installation; only its HTTP status, +byte count and hash are retained. +This setup step supplies no native/UI pass. On failure, fixed startup-screen +flags may be derived from bounded private Maestro hierarchy snapshots, without +retaining their text or attributes. The iOS exception collector reads the first +MiB of compact own-process output over the launch window, preserving early +errors instead of parsing a truncated JSON array. Missing diagnostics remain +explicit and do not imply a clean runtime. + +The iOS job is capped at 65 minutes and Android at 60 minutes, with command and +emulator boot limits inside those budgets. The jobs have no automatic retries; +new pushes cancel only +this workflow's runs for that PR. Cold native compilation, SDK installation and +CocoaPods can consume substantial hosted minutes. Use job timestamps and the +Actions usage page to report observed runtime and cost; do not describe a failed +dependency preflight's short duration as native-build timing. Current pricing, +included minutes and spending limits depend on the repository's plan. +As checked on 2026-10-17, GitHub's [standard runner rates](https://docs.github.com/en/billing/reference/actions-runner-pricing) +are $0.062/minute for macOS and $0.006/minute for Linux x64. At those rates a +30-minute iOS plus 30-minute Android attempt is about $2.04; reaching both job +caps is about $4.39, before included minutes, artifact storage, rounding and the +existing CI jobs. These are planning estimates, not measured native timings or +an account bill. These rates were inherited from #7060 and were not rechecked in +this source-only preparation. Observed bootstrap results belong in the validation report. +Enable spending limits before routine use if needed. + +Any later validation report must record the actual source/run/attempt and each +platform's build/UI outcome. This preparation runs no native workflow. Until both +strict native builds and actual UI runs +work reliably, use the existing local fallback without interrupting an active Mac +session. A passing startup smoke does not transfer to another PR or prove its +feature matrix. R4 and its existing Local evidence remain outside this bootstrap. +Hardware and release approval remain separate. + +## Administrative configuration + +Require `keeper/local-validation` and existing `Tests` for merges to `sprint`, +and require review for changes to the gate/native harness. `Native validation +contracts` is a useful additional required check. Do not require both native jobs +unconditionally when local fallback is intended: the combined gate resolves +platform evidence. Administrators should verify the required status app/source, +default-branch workflow availability, Actions runner permissions and private-repo +minute budget. The inherited #7060 report states that on 2026-10-17 the active +branch-rules endpoint reported required +`keeper/local-validation` and `Tests` checks from GitHub Actions (integration +15368). The legacy branch-protection endpoint returned HTTP 403; broader +administrator and bypass settings remain unverified. These settings were not +rechecked or changed by this source-only preparation.