diff --git a/.github/native/maestro_failure_snapshot.py b/.github/native/maestro_failure_snapshot.py new file mode 100644 index 000000000..08d22a009 --- /dev/null +++ b/.github/native/maestro_failure_snapshot.py @@ -0,0 +1,266 @@ +"""Project one Maestro 2.11 failed-assertion outcome; never tap-time evidence. + +Format inspected at upstream c8c361be1cc89b2e742f185466a302a22778be6b: +TestOutputWriter, FlowDebugOutput, ArtifactsGenerator, ArtifactCollector, +TreeNode and AndroidDriver. commands.json carries per-execution sequenceNumber +and artifacts-folder-relative SCREEN_HIERARCHY paths. Passing taps have no +hierarchy; neither commands nor the outcome hierarchy records resolved taps. +""" +import json +import math +import os +from pathlib import Path, PurePosixPath +import re +import stat + +FILE_LIMIT = 1024 * 1024 +ENTRY_LIMIT = 512 +STEP_LIMIT = 2048 +NODE_LIMIT = 10000 +COORD_LIMIT = 100000 +FLAGS = ('clickable', 'enabled', 'focused', 'selected') +SCOPE = 'POST_FAILED_ASSERT_OUTCOME_NOT_TAP_TIME' +# TestRunner/TestSuiteInteractor use the checked-in YAML config.name for the +# direct per-flow folder. These commands use no shard/retry CLI option. +FLOWS = {'product_first_wallet.yaml': 'Full app startup first wallet and cold persistence', + 'product_retry.yaml': 'Full app controlled setup disconnect Retry and cold persistence'} +STATUSES = ('PENDING', 'RUNNING', 'COMPLETED', 'FAILED', 'WARNED', 'SKIPPED') + + +def unknown(reason='UNAVAILABLE'): + return {'schema': 1, 'binding_state': 'UNKNOWN', 'reason': reason, + 'scope': SCOPE, 'failed_assertion': None, 'node_count': None, + 'skip': {'state': 'UNKNOWN', 'exact_resource_id_count': None, + 'bounds': None, **dict.fromkeys(FLAGS), + 'conflicts': dict.fromkeys(FLAGS)}, + 'tap_coordinates': 'NOT_RECORDED', 'visible_to_user': 'NOT_RECORDED'} + + +def reject(): + raise ValueError() + + +def pairs(items): + result = {} + for key, value in items: + if key in result: + reject() + result[key] = value + return result + + +def read_json(path, root): + if root.is_symlink() or not root.is_dir() or not path.resolve().is_relative_to(root.resolve()): + reject() + for item in (path, *path.parents): + if item.is_symlink(): + reject() + if item == root: + break + before = path.stat() + if not stat.S_ISREG(before.st_mode) or before.st_size > FILE_LIMIT: + reject() + with os.fdopen(os.open(path, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK), 'rb') as stream: + actual = os.fstat(stream.fileno()) + if (actual.st_dev, actual.st_ino, actual.st_size) != (before.st_dev, before.st_ino, before.st_size): + reject() + payload = stream.read(FILE_LIMIT + 1) + after = os.fstat(stream.fileno()) + if (actual.st_size, actual.st_mtime_ns) != (after.st_size, after.st_mtime_ns): + reject() + if len(payload) > FILE_LIMIT: + reject() + data = json.loads(payload.decode('utf-8'), object_pairs_hook=pairs, parse_constant=lambda _: reject()) + # Bound even ignored command payloads, including embedded scripts/errors. + pending, count = [(data, 0)], 0 + while pending: + item, depth = pending.pop() + count += 1 + if depth > 128 or count > 50000: + reject() + if type(item) is dict: + if len(item) > 256 or any(type(key) is not str for key in item): + reject() + pending.extend((value, depth + 1) for value in item.values()) + elif type(item) is list: + if len(item) > NODE_LIMIT: + reject() + pending.extend((value, depth + 1) for value in item) + elif type(item) is str and len(item) > 65536: + reject() + elif type(item) in (int, float) and (not math.isfinite(item) or abs(item) > 2 ** 53 - 1): + reject() + return data + + +def commands_file(root): + if root.is_symlink() or not root.is_dir(): + reject() + pending, found, count = [(root, 0)], [], 0 + while pending: + directory, depth = pending.pop() + if depth > 6: + reject() + with os.scandir(directory) as entries: + for entry in entries: + count += 1 + if count > ENTRY_LIMIT or entry.is_symlink(): + reject() + if entry.is_dir(follow_symlinks=False): + pending.append((Path(entry.path), depth + 1)) + elif entry.name == 'commands.json': + found.append(Path(entry.path)) + if len(found) > 1: + reject() + if len(found) != 1: + reject() + return found[0] + + +def target(command): + if type(command) is not dict or set(command) != {'assertConditionCommand'}: + return False + assertion = command['assertConditionCommand'] + if (type(assertion) is not dict or + not set(assertion) <= {'condition', 'timeout', 'label', 'optional'} or + assertion.get('optional', False) is not False or assertion.get('label') is not None): + return False + timeout = assertion.get('timeout') + if timeout is not None and (type(timeout) is not str or not re.fullmatch(r'[0-9]{1,10}', timeout)): + return False + condition = assertion.get('condition') + if type(condition) is not dict or set(condition) != {'visible'}: + return False + visible = condition['visible'] + return (type(visible) is dict and set(visible) <= {'idRegex', 'optional'} and + visible.get('idRegex') == 'view_startNewTile' and visible.get('optional', False) is False) + + +def failed_step(rows): + if type(rows) is not list or not 1 <= len(rows) <= STEP_LIMIT: + reject() + candidates, previous, artifact_paths = [], -1, set() + metadata_fields = {'status', 'timestamp', 'duration', 'error', 'sequenceNumber', + 'depth', 'evaluatedCommand', 'artifacts'} + for row in rows: + if type(row) is not dict or set(row) != {'command', 'metadata'}: + reject() + metadata, command = row['metadata'], row['command'] + if (type(command) is not dict or len(command) > 1 or type(metadata) is not dict or + not set(metadata) <= metadata_fields or metadata.get('status') not in STATUSES): + reject() + sequence = metadata.get('sequenceNumber') + if type(sequence) is not int or not previous < sequence < STEP_LIMIT: + reject() + previous = sequence + if type(metadata.get('depth', 0)) is not int or not 0 <= metadata.get('depth', 0) <= 128: + reject() + for field in ('timestamp', 'duration'): + if field in metadata and (type(metadata[field]) is not int or not 0 <= metadata[field] <= 10 ** 14): + reject() + if 'error' in metadata and (type(metadata['error']) is not dict or + not set(metadata['error']) <= {'message', 'debugMessage'} or + any(type(value) is not str for value in metadata['error'].values())): + reject() + if 'artifacts' in metadata and type(metadata['artifacts']) is not list: + reject() + artifacts = metadata.get('artifacts', []) + if len(artifacts) > 32: + reject() + for artifact in artifacts: + if (type(artifact) is not dict or set(artifact) != {'type', 'path'} or + type(artifact['type']) is not str or type(artifact['path']) is not str): + reject() + if artifact['path'] in artifact_paths: + reject() + artifact_paths.add(artifact['path']) + if metadata['status'] == 'FAILED' and target(command): + if 'evaluatedCommand' in metadata and not target(metadata['evaluatedCommand']): + reject() + candidates.append((sequence, artifacts)) + if len(candidates) != 1: + reject() + sequence, artifacts = candidates[0] + snapshots = [item['path'] for item in artifacts if item['type'] == 'SCREEN_HIERARCHY'] + if len(snapshots) != 1: + reject() + path = snapshots[0] + parts = PurePosixPath(path).parts + match = re.fullmatch(r'screen-hierarchy/step-([0-9]{3,6})(?:-[A-Za-z0-9_.-]{1,40})?\.json', path) + if (not match or len(parts) != 2 or any(part in ('.', '..') for part in parts) or + int(match[1]) != sequence + 1): + reject() + return sequence, path + + +def snapshot(tree): + pending, count, matches = [(tree, 0)], 0, [] + while pending: + node, depth = pending.pop() + count += 1 + if (count > NODE_LIMIT or depth > 128 or type(node) is not dict or + not set(node) <= {'attributes', 'children', 'checked', *FLAGS}): + reject() + attrs, children = node.get('attributes', {}), node.get('children', []) + if (type(attrs) is not dict or type(children) is not list or len(attrs) > 256 or + any(type(key) is not str or type(value) is not str for key, value in attrs.items()) or + any(node.get(flag) is not None and type(node[flag]) is not bool for flag in (*FLAGS, 'checked'))): + reject() + if attrs.get('resource-id') in ('btn_skip', 'io.hexawallet.keeper.development:id/btn_skip'): + matches.append(node) + pending.extend((child, depth + 1) for child in children) + return count, matches + + +def project_skip(nodes): + result = unknown()['skip'] + result['exact_resource_id_count'] = len(nodes) + if not nodes: + result['state'] = 'ABSENT' + return result + if len(nodes) != 1: + return result + node, attrs = nodes[0], nodes[0].get('attributes', {}) + number = r'(0|-?[1-9][0-9]{0,5})' + bounds = re.fullmatch(r'\[' + number + ',' + number + r'\]\[' + number + ',' + number + r'\]', attrs.get('bounds', '')) + if not bounds: + return result + left, top, right, bottom = map(int, bounds.groups()) + if (any(abs(value) > COORD_LIMIT for value in (left, top, right, bottom)) or + right <= left or bottom <= top): + return result + for flag in FLAGS: + if flag in attrs and attrs[flag] not in ('true', 'false'): + return result + result.update(state='UNIQUE_VALID_BOUNDS', bounds={'left': left, 'top': top, 'right': right, 'bottom': bottom}) + for flag in FLAGS: + direct = node.get(flag) + attribute = attrs.get(flag) + attribute = attribute == 'true' if attribute is not None else None + conflict = direct is not None and attribute is not None and direct != attribute + result['conflicts'][flag] = conflict + result[flag] = None if conflict else direct if direct is not None else attribute + return result + + +def collect(directory, flow, failure): + result = unknown('UNBOUND_FAILED_ASSERTION') + if (flow not in FLOWS or type(failure) is not dict or + failure.get('state') != 'ERROR_RECORDED' or + failure.get('failure_kind') != 'ASSERT_VISIBLE' or + failure.get('failed_selector') != 'start_new_tile'): + return result + try: + root = Path(directory).absolute() + commands = commands_file(root) + if commands.parent != root / FLOWS[flow]: + reject() + sequence, relative = failed_step(read_json(commands, root)) + tree = read_json(commands.parent / relative, root) + count, nodes = snapshot(tree) + result.update(binding_state='BOUND', reason='FAILED_ASSERTION_OUTCOME', node_count=count, + failed_assertion={'kind': 'ASSERT_VISIBLE', 'selector': 'start_new_tile', + 'sequence_number': sequence}, skip=project_skip(nodes)) + except Exception: + pass + return result diff --git a/.github/native/product_failure_diagnosis.py b/.github/native/product_failure_diagnosis.py index 1cda3f244..f08a748e7 100644 --- a/.github/native/product_failure_diagnosis.py +++ b/.github/native/product_failure_diagnosis.py @@ -1,5 +1,6 @@ """Read existing failed-sample files; export fixed observations, never raw text.""" from itertools import islice +import importlib.util import json import os from pathlib import Path @@ -257,6 +258,18 @@ def metro_window(metro, binding): return result +def failed_assertion_snapshot(directory, flow, failure): + try: + spec = importlib.util.spec_from_file_location('maestro_failure_snapshot', + Path(__file__).with_name('maestro_failure_snapshot.py')) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module.collect(directory, flow, failure) + except Exception: + return {'schema': 1, 'binding_state': 'UNKNOWN', 'reason': 'COLLECTOR_UNAVAILABLE', + 'scope': 'POST_FAILED_ASSERT_OUTCOME_NOT_TAP_TIME'} + + def collect(directory, flow, metro, binding=None): directory = Path(directory) return {'scope': 'Failed-sample files and bound owned Metro only; observations do not prove current visibility, route, tap delivery or cause.', diff --git a/.github/native/product_measurements.py b/.github/native/product_measurements.py index e2950aca0..fe33b847e 100644 --- a/.github/native/product_measurements.py +++ b/.github/native/product_measurements.py @@ -82,6 +82,17 @@ def sample_failure_observation(private, flow, metro, observer): return {'status': 'UNKNOWN', 'scope': 'Failed-sample observation unavailable; original failure retained.'} +def sample_failed_assertion_snapshot(private, flow, failure_observation, observer): + """Optional adjacent receipt; keep the existing four-field observation intact.""" + try: + failure = failure_observation.get('junit') + module, _ = observer or (native_module('product_failure_diagnosis'), None) + return module.failed_assertion_snapshot(private, flow, failure) + except Exception: + return {'schema': 1, 'binding_state': 'UNKNOWN', 'reason': 'COLLECTOR_UNAVAILABLE', + 'scope': 'POST_FAILED_ASSERT_OUTCOME_NOT_TAP_TIME'} + + class Receipt: """Fixed vocabulary and numbers only; never store a request body/identifier.""" def __init__(self): @@ -422,6 +433,8 @@ def save(): except Exception: outcome = receipt.snapshot() outcome['failure_observation'] = sample_failure_observation(output, flow, metro, observer) + outcome['failed_assertion_snapshot'] = sample_failed_assertion_snapshot( + output, flow, outcome['failure_observation'], observer) if h.PLATFORM == 'ANDROID': outcome['android_runtime_diagnostic'] = android_failure_observation(output, device) if retry_fixture: diff --git a/.github/tests/test_maestro_failure_snapshot.py b/.github/tests/test_maestro_failure_snapshot.py new file mode 100644 index 000000000..f061eece5 --- /dev/null +++ b/.github/tests/test_maestro_failure_snapshot.py @@ -0,0 +1,344 @@ +"""Fabricated pinned-Maestro format controls; no native execution or raw export.""" +import copy +import contextlib +import hashlib +import importlib.util +import json +from pathlib import Path +import re +import tempfile +import types +import unittest +from unittest import mock + +NATIVE = Path(__file__).parents[1] / 'native' + + +def load(name): + spec = importlib.util.spec_from_file_location(name, NATIVE / (name + '.py')) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +projection = load('maestro_failure_snapshot') +diagnosis = load('product_failure_diagnosis') +product = load('product_measurements') +FAILURE = {'state': 'ERROR_RECORDED', 'failed_selector': 'start_new_tile', 'failure_kind': 'ASSERT_VISIBLE'} + + +def assertion(sequence=2, path=None): + return {'command': {'assertConditionCommand': {'condition': {'visible': { + 'idRegex': 'view_startNewTile', 'optional': False}}, 'timeout': '30000', 'optional': False}}, + 'metadata': {'status': 'FAILED', 'sequenceNumber': sequence, 'depth': 1, + 'timestamp': 1792710000000, 'duration': 30000, + 'error': {'message': 'PRIVATE_SENTINEL', 'debugMessage': 'PRIVATE_SENTINEL'}, + 'artifacts': [{'type': 'SCREEN_HIERARCHY', 'path': path or + 'screen-hierarchy/step-%03d-assertCondition-view_startNewTile.json' % (sequence + 1)}]}} + + +def skip(**changes): + node = {'attributes': {'resource-id': 'btn_skip', 'bounds': '[4,8][104,58]', + 'text': 'PRIVATE_SENTINEL', 'clickable': 'true', 'enabled': 'true'}, + 'clickable': True, 'enabled': True, 'focused': False, 'selected': False} + node.update(changes) + return node + + +class FailedSnapshot(unittest.TestCase): + def setUp(self): + self.temp = tempfile.TemporaryDirectory() + self.addCleanup(self.temp.cleanup) + self.root = Path(self.temp.name) + # TestRunner creates a folder from the flow's public configured name. + self.bundle = self.root / 'Full app startup first wallet and cold persistence' + self.bundle.mkdir() + self.commands = self.bundle / 'commands.json' + + def write(self, rows=None, tree=None): + rows = [assertion()] if rows is None else rows + self.commands.write_text(json.dumps(rows)) + path = self.bundle / 'screen-hierarchy/step-003-assertCondition-view_startNewTile.json' + path.parent.mkdir(exist_ok=True) + path.write_text(json.dumps({'children': [skip()]} if tree is None else tree)) + return path + + def collect(self, flow='product_first_wallet.yaml', failure=None): + value = projection.collect(self.root, flow, FAILURE if failure is None else failure) + rendered = json.dumps(value) + for private in ('PRIVATE_SENTINEL', str(self.root), 'screen-hierarchy', 'resource-id'): + self.assertNotIn(private, rendered) + return value + + def test_unique_failed_outcome_projects_fixed_count_bounds_flags_not_visibility_or_tap(self): + self.write() + result = self.collect() + self.assertEqual(result['binding_state'], 'BOUND') + self.assertEqual(result['scope'], 'POST_FAILED_ASSERT_OUTCOME_NOT_TAP_TIME') + self.assertEqual(result['failed_assertion'], {'kind': 'ASSERT_VISIBLE', + 'selector': 'start_new_tile', 'sequence_number': 2}) + self.assertEqual(result['skip'], {'state': 'UNIQUE_VALID_BOUNDS', + 'exact_resource_id_count': 1, 'bounds': {'left': 4, 'top': 8, 'right': 104, 'bottom': 58}, + 'clickable': True, 'enabled': True, 'focused': False, 'selected': False, + 'conflicts': dict.fromkeys(projection.FLAGS, False)}) + self.assertEqual(result['tap_coordinates'], 'NOT_RECORDED') + self.assertEqual(result['visible_to_user'], 'NOT_RECORDED') + + def test_only_artifact_bound_to_exact_failed_assertion_is_used_not_other_snapshot(self): + self.write(tree={}) + (self.bundle / 'screen-hierarchy/step-001.json').write_text(json.dumps(skip())) + self.assertEqual(self.collect()['skip']['exact_resource_id_count'], 0) + self.write(tree={'attributes': {'resource-id': 'foreign:id/btn_skip'}}) + self.assertEqual(self.collect()['skip']['exact_resource_id_count'], 0) + node = skip(); node['attributes']['resource-id'] = 'io.hexawallet.keeper.development:id/btn_skip' + self.write(tree=node) + self.assertEqual(self.collect()['skip']['exact_resource_id_count'], 1) + + def test_no_snapshot_is_inferred_for_completed_skip_or_unrelated_junit_failure(self): + row = {'command': {'tapOnElement': {'selector': {'idRegex': 'btn_skip'}}}, + 'metadata': {'status': 'COMPLETED', 'sequenceNumber': 1}} + self.write(rows=[row]) + self.assertEqual(self.collect()['binding_state'], 'UNKNOWN') + self.write() + for changes in ({'state': 'UNKNOWN'}, {'failed_selector': 'onboarding_skip'}, + {'failure_kind': 'ELEMENT_NOT_FOUND'}): + self.assertEqual(self.collect(failure={**FAILURE, **changes})['binding_state'], 'UNKNOWN') + self.assertEqual(self.collect(flow='private.yaml')['binding_state'], 'UNKNOWN') + + def test_failed_command_and_evaluated_selector_must_be_unique_exact_and_nonoptional(self): + for mutation in ('completed', 'warned', 'other_id', 'text', 'index', 'optional', + 'label', 'timeout', 'condition', 'other_command', 'evaluated', 'duplicate'): + row = assertion() + command = row['command']['assertConditionCommand'] + visible = command['condition']['visible'] + rows = [row] + if mutation in ('completed', 'warned'): row['metadata']['status'] = mutation.upper() + elif mutation == 'other_id': visible['idRegex'] = 'btn_skip' + elif mutation == 'text': visible['textRegex'] = 'PRIVATE_SENTINEL' + elif mutation == 'index': visible['index'] = '0' + elif mutation == 'optional': command['optional'] = True + elif mutation == 'label': command['label'] = 'PRIVATE_SENTINEL' + elif mutation == 'timeout': command['timeout'] = [] + elif mutation == 'condition': command['condition']['notVisible'] = {'idRegex': 'btn_skip'} + elif mutation == 'other_command': row['command']['tapOnElement'] = {} + elif mutation == 'evaluated': row['metadata']['evaluatedCommand'] = {'tapOnElement': {}} + else: rows.append(assertion(3)) + self.write(rows=rows) + with self.subTest(mutation=mutation): self.assertEqual(self.collect()['binding_state'], 'UNKNOWN') + + def test_sequence_metadata_shape_order_and_duplicates_fail_closed(self): + for sequence in (None, True, -1, 1.0, '2', projection.STEP_LIMIT): + row = assertion(); row['metadata']['sequenceNumber'] = sequence + self.write(rows=[row]) + self.assertEqual(self.collect()['binding_state'], 'UNKNOWN') + for mutation in ('duplicate_sequence', 'wrong_status', 'duration', 'depth', 'error', 'wrapper'): + row = assertion(); rows = [row] + if mutation == 'duplicate_sequence': rows.insert(0, copy.deepcopy(row)) + elif mutation == 'wrong_status': row['metadata']['status'] = 'ERROR' + elif mutation == 'duration': row['metadata']['duration'] = False + elif mutation == 'depth': row['metadata']['depth'] = -1 + elif mutation == 'error': row['metadata']['error'] = 'PRIVATE_SENTINEL' + else: row['extra'] = 'PRIVATE_SENTINEL' + self.write(rows=rows) + self.assertEqual(self.collect()['binding_state'], 'UNKNOWN') + + def test_path_type_sequence_and_duplicate_artifact_ownership_are_required(self): + for path in ('../outside.json', '/tmp/private.json', 'screen-hierarchy/../step-003.json', + 'screen-hierarchy\\step-003.json', 'screen-hierarchy/step-004.json', + 'https://private.example/step-003.json', 'screen-hierarchy/step-003.json\n'): + self.write(rows=[assertion(path=path)]) + self.assertEqual(self.collect()['binding_state'], 'UNKNOWN') + for change in ('missing', 'wrong_type', 'duplicate', 'other_owner'): + row = assertion(); rows = [row] + artifact = row['metadata']['artifacts'][0] + if change == 'missing': row['metadata'].pop('artifacts') + elif change == 'wrong_type': artifact['type'] = 'SCREENSHOT' + elif change == 'duplicate': row['metadata']['artifacts'].append(copy.deepcopy(artifact)) + else: rows.insert(0, {'command': {}, 'metadata': {'status': 'FAILED', + 'sequenceNumber': 1, 'artifacts': [copy.deepcopy(artifact)]}}) + self.write(rows=rows) + self.assertEqual(self.collect()['binding_state'], 'UNKNOWN') + + def test_commands_file_candidates_and_symlink_root_file_parent_or_fifo_are_unknown(self): + snapshot = self.write() + other = self.root / 'commands.json'; other.write_text(self.commands.read_text()) + self.assertEqual(self.collect()['binding_state'], 'UNKNOWN'); other.unlink() + for path in (self.commands, snapshot): + saved = path.read_bytes(); path.unlink() + outside = self.root / 'outside.json'; outside.write_bytes(saved) + path.symlink_to(outside) + self.assertEqual(self.collect()['binding_state'], 'UNKNOWN') + path.unlink(); path.write_bytes(saved) + alias = self.root / 'alias'; alias.symlink_to(self.bundle, target_is_directory=True) + self.assertEqual(self.collect()['binding_state'], 'UNKNOWN'); alias.unlink() + root_alias = self.root.parent / (self.root.name + '-alias') + root_alias.symlink_to(self.root, target_is_directory=True) + try: self.assertEqual(projection.collect(root_alias, 'product_first_wallet.yaml', FAILURE)['binding_state'], 'UNKNOWN') + finally: root_alias.unlink() + snapshot.unlink(); projection.os.mkfifo(snapshot) + self.assertEqual(self.collect()['binding_state'], 'UNKNOWN') + + def test_bundle_must_match_the_source_defined_flow_name_without_other_flow_or_shard_adoption(self): + for flow, name in projection.FLOWS.items(): + content = (NATIVE / flow).read_text() + self.assertEqual(re.search(r'^name: (.+)$', content, re.MULTILINE)[1], name) + self.write() + other = self.root / 'Other flow' + self.bundle.rename(other) + self.assertEqual(self.collect()['binding_state'], 'UNKNOWN') + retry = self.root / projection.FLOWS['product_retry.yaml'] + other.rename(retry) + self.assertEqual(self.collect(flow='product_retry.yaml')['binding_state'], 'BOUND') + retry.rename(self.root / (retry.name + '-shard-1')) + self.assertEqual(self.collect(flow='product_retry.yaml')['binding_state'], 'UNKNOWN') + + def test_malformed_duplicate_json_and_wrong_node_shapes_never_export_partial_nodes(self): + snapshot = self.write() + for raw in (b'{"attributes":{},"attributes":{}}', b'\xffPRIVATE_SENTINEL', b'NaN', + json.dumps(skip()).encode('utf-16')): + snapshot.write_bytes(raw) + self.assertEqual(self.collect()['binding_state'], 'UNKNOWN') + for tree in ({'children': [skip(), 42]}, {'attributes': []}, {'children': None}, + {'clickable': 1}, {'attributes': {'bounds': 42}}, {'unexpected': 'PRIVATE_SENTINEL'}): + self.write(tree=tree) + self.assertEqual(self.collect()['binding_state'], 'UNKNOWN') + self.write(); self.commands.write_text('[{"command":{},"command":{},"metadata":{}}]') + self.assertEqual(self.collect()['binding_state'], 'UNKNOWN') + self.write(); self.commands.write_text(self.commands.read_text().replace('30000,', '1e999,')) + self.assertEqual(self.collect()['binding_state'], 'UNKNOWN') + + def test_file_scan_step_and_node_bounds_fail_closed(self): + snapshot = self.write() + snapshot.write_bytes(b' ' * (projection.FILE_LIMIT + 1)) + self.assertEqual(self.collect()['binding_state'], 'UNKNOWN') + self.write(); self.commands.write_bytes(b' ' * (projection.FILE_LIMIT + 1)) + self.assertEqual(self.collect()['binding_state'], 'UNKNOWN') + self.write() + for constant, value in (('ENTRY_LIMIT', 1), ('STEP_LIMIT', 2), ('NODE_LIMIT', 1)): + with mock.patch.object(projection, constant, value): + self.assertEqual(self.collect()['binding_state'], 'UNKNOWN') + + def test_multiple_or_invalid_bounds_never_select_one_skip_or_project_its_flags(self): + self.write(tree={'children': [skip(), skip()]}) + result = self.collect()['skip'] + self.assertEqual((result['state'], result['exact_resource_id_count']), ('UNKNOWN', 2)) + self.assertIsNone(result['bounds']); self.assertIsNone(result['clickable']) + for bounds in ('[4,8][4,58]', '[104,8][4,58]', '[4,8][104,8]', '[0,0][100001,1]', + '[01,0][2,2]', '[0,0][2,2]\n', 'PRIVATE_SENTINEL'): + node = skip(); node['attributes']['bounds'] = bounds + self.write(tree=node) + result = self.collect()['skip'] + self.assertEqual(result['state'], 'UNKNOWN') + self.assertIsNone(result['bounds']); self.assertIsNone(result['enabled']) + + def test_boolean_sources_are_nullable_and_disagreements_are_fixed_conflict_facts(self): + node = skip(clickable=False); node.pop('focused'); node.pop('selected') + self.write(tree=node) + result = self.collect()['skip'] + self.assertIsNone(result['clickable']); self.assertIs(result['conflicts']['clickable'], True) + self.assertIs(result['enabled'], True); self.assertIsNone(result['focused']) + node['attributes']['enabled'] = 'PRIVATE_SENTINEL' + self.write(tree=node) + self.assertEqual(self.collect()['skip']['state'], 'UNKNOWN') + + def test_integration_reads_junit_once_and_optional_projection_failure_preserves_other_facts(self): + self.write() + fixed = {'state': 'COMPLETE'} + with mock.patch.object(diagnosis, 'junit', return_value=FAILURE) as junit, \ + mock.patch.object(diagnosis, 'hierarchy', return_value=fixed), \ + mock.patch.object(diagnosis, 'metro_window', return_value=fixed): + result = diagnosis.collect(self.root, 'product_first_wallet.yaml', None) + junit.assert_called_once() + self.assertEqual(set(result), {'scope', 'junit', 'hierarchy', 'product_metro'}) + original = copy.deepcopy(result) + snapshot = product.sample_failed_assertion_snapshot( + self.root, 'product_first_wallet.yaml', result, (diagnosis, None)) + self.assertEqual(snapshot['binding_state'], 'BOUND') + self.assertEqual(result, original) + with mock.patch.object(diagnosis.importlib.util, 'spec_from_file_location', side_effect=ValueError('PRIVATE_SENTINEL')): + result = diagnosis.failed_assertion_snapshot(self.root, 'product_first_wallet.yaml', FAILURE) + self.assertEqual(result['binding_state'], 'UNKNOWN') + self.assertNotIn('PRIVATE_SENTINEL', json.dumps(result)) + + def test_secondary_receipt_failure_preserves_old_observation_original_error_and_purge(self): + root, order = self.root, [] + private = root / 'private'; private.mkdir() + (root / 'release').mkdir() + manifest = b'{"name":"owned"}' + (root / 'package.json').write_bytes(manifest) + (root / 'release/version.json').write_text(json.dumps( + {'version': '2.6.4', 'androidVersionCode': 627, 'iosBuildNumber': 620})) + (root / '.env.ci').write_text('\n'.join([ + 'ENVIRONMENT=DEVELOPMENT', 'RELAY=http://127.0.0.1:8765/', + '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'])) + sha, artifact = 'a' * 40, {'sha256': 'b' * 64} + digest = hashlib.sha256(manifest).hexdigest() + state = {'build': 'PASS', 'source_sha': sha, 'built_artifact': artifact, + 'commands': [], 'maestro': {'binary': 'fake-maestro'}, 'nodeify_overlay': { + 'path': 'package.json', 'allowed_fields': ['browser', 'react-native', 'browserify'], + 'before_sha256': digest, 'after_sha256': digest}} + original_error = RuntimeError('PRIVATE_SENTINEL original') + old = {'scope': 'fixed', 'junit': dict(FAILURE), 'hierarchy': {'state': 'UNKNOWN'}, + 'product_metro': {'state': 'UNKNOWN'}} + + def command(args, *_args, **_kwargs): + state['commands'].append({}) + if args == ['adb', 'get-serialno']: + return 'emulator-5554' + if args[0] == 'fake-maestro': + output = Path(args[args.index('--debug-output') + 1]) + (output / 'junit.xml').write_text('PRIVATE_SENTINEL') + raise original_error + return 'image' + + def collect(output, *_args): + self.assertTrue((output / 'junit.xml').exists()) + order.append('old') + return copy.deepcopy(old) + + def secondary(output, flow, parsed_junit): + self.assertTrue((output / 'junit.xml').exists()) + self.assertEqual(flow, 'product_first_wallet.yaml') + self.assertEqual(parsed_junit, old['junit']) + order.append('secondary') + raise ValueError('PRIVATE_SENTINEL secondary') + + observer = types.SimpleNamespace(bind_metro=lambda _: None, collect=collect, + failed_assertion_snapshot=secondary) + manager = types.SimpleNamespace(start=lambda *_a, **_k: None, + close=lambda: order.append('close'), events=[]) + server = types.SimpleNamespace(serve_forever=lambda: None, shutdown=lambda: None, server_close=lambda: None) + + def installed(_device): + proof = {'method': 'SINGLE_INSTALLED_BASE_APK_SHA256', 'sha256': 'c' * 64} + state.update(installed_artifact_verified=True, installed_artifact=proof) + return proof + + h = types.SimpleNamespace(ROOT=root, PRIVATE=private, PLATFORM='ANDROID', APP_IDS={'ANDROID': 'owned.app'}, + read=lambda: copy.deepcopy(state), save=lambda value: state.update(copy.deepcopy(value)), + artifact_identity=lambda: artifact, command=command, verify_installed_artifact=installed) + with contextlib.ExitStack() as stack: + stack.enter_context(mock.patch.dict(product.os.environ, {'GITHUB_ACTIONS': 'true', + 'RUNNER_ENVIRONMENT': 'github-hosted', 'KEEPER_SOURCE_SHA': sha})) + stack.enter_context(mock.patch.object(product.subprocess, 'check_output', side_effect=[sha, manifest])) + stack.enter_context(mock.patch.object(product.subprocess, 'run', return_value=types.SimpleNamespace(returncode=0))) + stack.enter_context(mock.patch.object(product, 'native_module', side_effect=lambda name: + observer if name == 'product_failure_diagnosis' else types.SimpleNamespace(OwnedMetro=lambda *_: manager))) + stack.enter_context(mock.patch.object(product, 'make_server', return_value=server)) + stack.enter_context(mock.patch.object(product, 'android_failure_observation', return_value={'status': 'UNAVAILABLE'})) + with self.assertRaises(RuntimeError) as failure: + product.run(h) + self.assertIs(failure.exception, original_error) + outcome = state['product_ui']['samples'][0] + self.assertEqual(outcome['failure_observation'], old) + self.assertEqual(set(outcome['failure_observation']), {'scope', 'junit', 'hierarchy', 'product_metro'}) + self.assertEqual(outcome['failed_assertion_snapshot']['binding_state'], 'UNKNOWN') + self.assertEqual(order, ['old', 'secondary', 'close']) + self.assertEqual(list(private.iterdir()), []) + self.assertNotIn('PRIVATE_SENTINEL', json.dumps(state['product_ui'])) + + +if __name__ == '__main__': + unittest.main() diff --git a/docs/r5-onboarding-boundary-observation.md b/docs/r5-onboarding-boundary-observation.md index b27c6fb3c..eee7d788f 100644 --- a/docs/r5-onboarding-boundary-observation.md +++ b/docs/r5-onboarding-boundary-observation.md @@ -78,9 +78,19 @@ Optional parser failure leaves the receipt UNKNOWN and preserves existing fault classification and private-output cleanup. No arbitrary log text, tag, PID, timestamp, path, URL or error payload is exported. -The separate Maestro failed-assertion geometry projection is successor C2. -C1 alone adds no snapshot claim and does not change the existing four-key -`failure_observation` contract. Missing marker stages remain UNKNOWN. +A separate pure helper binds the failed assertion's existing Maestro +`commands.json` outcome to its `SCREEN_HIERARCHY` artifact using the pinned +flow folder, sequence number and artifact path. Pinned Maestro 2.11.0 source +`c8c361be1cc89b2e742f185466a302a22778be6b` defines that layout in TestRunner, +TestSuiteInteractor, TestDebugReporter and ArtifactsGenerator. The helper projects only fixed Skip identity count, +bounded numeric bounds and public native booleans. Its scope is +POST_FAILED_ASSERT_OUTCOME_NOT_TAP_TIME. It does not inspect tap-time coordinates, +infer what was visible to the user, or add any native query. Missing, malformed, +ambiguous or incorrectly bound input remains UNKNOWN. The older metadata-only +artifact cannot retroactively gain these new observations. +The optional receipt is adjacent at each failed outcome's +`failed_assertion_snapshot`; the existing four-key `failure_observation` remains +unchanged for both ordinary samples and the controlled Retry sample. Host controls execute the actual TypeScript handler/effect closures, including original return/throw priority, listener/timer cleanup, release guards and quiet @@ -90,19 +100,21 @@ unchanged four-command query. Relevant React review checks cover existing hook lifecycles and unchanged props/accessibility only. No UI-library refactor or unrelated optimization is included. These controls are not native acceptance. -## Physical source-review split C1 - -This marker package descends from fixed join `8940d8601d65245ffbd1d42ad0095e8426579978`, whose -parents are frozen B642fc14d1f2c3bd3451fe257642b01c6d1c07c95 and external -044ebb6ea2d465f6a70e9116e3feac5a604cd696; its tree is exactly frozen B. -The join is a source-review baseline, not a native/release candidate. -The external thirteen-file addition is exposed fully across this marker package -and successor C2. Neither producer nor the earlier A/B packages are rewritten. -The complete new external source is approximately16.3k changed code/test tokens -(chars/4), with exact per-package estimates in their handoffs. - -React review: existing mount effect/dependency counts, listener registration, -cleanup and timer behavior are preserved. The Skip handler calls the original -reset once with the same receiver/arguments and returns/rethrows immediately. -No state update, render condition, layout, accessibility prop or testID changes. -No UI-library or unrelated performance refactor is included. +## Reconciliation packages C1 and C2 + +C1 `69f64b16afc5bcfb98b6d14c2ff63db0eb9474b9` exposes the Android marker +producer/parser/unchanged-query integration. Its mechanical source-review base +`8940d8601d65245ffbd1d42ad0095e8426579978` has frozen B642fc14 and external +044ebb6 as parents and exactly the frozen B tree; it is not a native candidate. +This successor C2 separately exposes the failed-assertion snapshot projection, +its existing product-failure adapter and all geometry/privacy/file-bound controls. +The original thirteen-file external work is preserved through ancestry, producer +links and both complete package diffs, approximately16.3k code/test tokens in +total (changed characters/4). Exact counts and checks belong in the handoffs. + +The app source uses one iOS failure marker plus this separately scoped Android +onboarding marker, each fixed and development-only. Existing A/B credential +current-attempt guards remain intact. React review confirms unchanged render +conditions, hook/listener/timer lifecycles, reset receiver/arguments/count, +accessibility props and test IDs. No unrelated UI or performance change. +No producer rewrite, app-cause correction or native/release acceptance is claimed.