diff --git a/.github/native/android_startup_diagnosis.py b/.github/native/android_startup_diagnosis.py index 487cb040b..d8c286e6a 100644 --- a/.github/native/android_startup_diagnosis.py +++ b/.github/native/android_startup_diagnosis.py @@ -1,5 +1,6 @@ """Read-only Android startup facts. No raw device or application text escapes.""" from pathlib import Path +import importlib.util import re import shutil import subprocess @@ -130,11 +131,20 @@ def exit_history(payload, usable=True): def android_log(payload, owned): result = {'parsed_error_records': 0, 'own_error_records': 0, 'unclassified_own_error_records': 0, + 'diagnostic_marker_records': 0, 'unparsed_nonempty_lines': 0, 'exception_types': [], 'fault_categories': [], 'attribution': 'CURRENT_OWN_PID_MATCH_HISTORY_FRESHNESS_UNKNOWN' if owned else 'UNKNOWN_NO_CURRENT_OWN_PROCESS'} pattern = r'^\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+\s+(\d+)\s+\d+\s+[EF]\s+[^:]+:\s*(.*)$' for line in payload.splitlines(): + # Reserved development diagnostics are not runtime error records. Keep + # this message-start filter independent of the optional receipt parser; + # an unavailable observer cannot turn its own markers into app faults. + if re.fullmatch(r'[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{3}' + r' +[1-9][0-9]{0,9} +[1-9][0-9]{0,9} +E +ReactNativeJS: ' + r'KEEPER_ONBOARDING_BOUNDARY\|[^\n]*', line): + result['diagnostic_marker_records'] += 1 + continue match = re.match(pattern, line) if not match: if line.strip() and not line.startswith('--------- beginning of '): @@ -152,6 +162,18 @@ def android_log(payload, owned): return result +def onboarding_receipt(payload, owned, *, usable, full_input): + try: + spec = importlib.util.spec_from_file_location('onboarding_failure_receipts', + Path(__file__).with_name('onboarding_failure_receipts.py')) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module.parse(payload, owned, usable=usable, full_input=full_input) + except Exception: + return {'state': 'UNKNOWN', 'scope': 'OPTIONAL_DEV_MARKER_PARSER_UNAVAILABLE', + 'cause': 'UNDETERMINED', 'freshness': 'GUEST_CLOCK_UNVERIFIED'} + + def metro(payload): selected = [] headers, remaining = 0, 0 @@ -260,6 +282,9 @@ def capture(label, arguments): result['runtime_errors'].update( lookup_status=result['commands'][-1]['lookup_status'], input_truncated=result['commands'][-1]['input_truncated']) + result['onboarding_boundary_observation'] = onboarding_receipt(payload, owned, + usable=result['commands'][-1]['lookup_status'] == 'SUCCESS', + full_input=result['commands'][-1]['input_truncated'] is False) try: payload, observed = bounded(private / 'metro.log', private) result['metro'] = {'lookup_status': 'SUCCESS', **observed, **metro(payload)} diff --git a/.github/native/onboarding_failure_receipts.py b/.github/native/onboarding_failure_receipts.py new file mode 100644 index 000000000..79c5936be --- /dev/null +++ b/.github/native/onboarding_failure_receipts.py @@ -0,0 +1,149 @@ +"""Pure, bounded projection of reserved development-only Android receipts.""" +from datetime import date +import re + +LIMIT = 512 * 1024 +MAX_RECORDS = 10000 +MAX_MARKERS = 64 +PREFIX = 'KEEPER_ONBOARDING_BOUNDARY|' +STAGES = ('ONBOARDING_EFFECT', 'SKIP_PRESS', 'SKIP_RESET_RETURNED', + 'SKIP_RESET_THROWN', 'NEW_KEEPER_EFFECT') +_FRAME = re.compile( + r'(?:0[1-9]|1[0-2])-(?:0[1-9]|[12][0-9]|3[01]) ' + r'(?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]\.[0-9]{3}' + r' +([1-9][0-9]{0,9}) +([1-9][0-9]{0,9}) +E +ReactNativeJS: (.*)') +_FORM = re.compile(re.escape(PREFIX) + r'(v[0-9]{1,4})\|([A-Z][A-Z0-9_]{0,63})') + + +def _frame(line): + if not isinstance(line, str) or len(line) > LIMIT or '\n' in line: + return None + match = _FRAME.fullmatch(line) + if not match or any(int(match[index]) > 2147483647 for index in (1, 2)): + return None + try: + date(2000, int(line[:2]), int(line[3:5])) # Year is absent; permit February 29. + except ValueError: + return None + return match + + +def has_diagnostic_marker_prefix(line): + """Reserve diagnostic frames for separate parsing, including rejected suffixes. + + This predicate establishes neither a valid receipt nor process attribution. + It must not exclude ordinary errors that merely mention the marker later. + """ + match = _frame(line) + return bool(match and match[3].startswith(PREFIX)) + + +def _clean(line): + # Only LF is a record delimiter; CR is accepted solely as part of CRLF by + # parse(). Reject replacement, C0/C1 controls and Unicode line separators. + return not any(ord(char) < 32 or 127 <= ord(char) <= 159 or + char in '\ufffd\u2028\u2029' for char in line) + + +def _receipt(line): + match = _frame(line) + if not match or not _clean(line): + return None + form = _FORM.fullmatch(match[3]) + if form and form[1] == 'v1' and form[2] in STAGES: + return int(match[1]), form[2] + return None + + +def is_diagnostic_marker_record(line): + """Recognize an exact valid enum record, independently of process ownership.""" + return _receipt(line) is not None + + +def parse(payload, owned_pids, *, usable=True, full_input=True): + """Return positive receipts only; missing stages never establish absence. + + Inputs are an already-collected threadtime E/F payload and the existing + current-process inventory. No guest-clock freshness, route, touch delivery, + causal relationship or application success is inferred from these records. + """ + result = { + 'schema': 1, 'scope': 'DEV_MARKERS_NOT_ROUTE_TAP_DELIVERY_OR_CAUSE_PROOF', + 'state': 'UNKNOWN', 'cause': 'UNDETERMINED', + 'freshness': 'GUEST_CLOCK_UNVERIFIED', + 'coverage': 'POSITIVE_ONLY_MISSING_STAGES_UNKNOWN', + 'producer_per_stage_limit': 4, 'producer_total_limit': 20, + 'consumer_marker_limit': MAX_MARKERS, 'input_byte_limit': LIMIT, + 'scan_record_limit': MAX_RECORDS, + 'input_usable': usable is True, 'full_input': full_input is True, + 'ownership_usable': False, 'stages': {stage: None for stage in STAGES}, + 'scanned_records': 0, 'marker_records': 0, 'owned_marker_records': 0, + 'foreign_pid_records': 0, 'duplicate_records': 0, + 'malformed_marker_records': 0, 'unsupported_marker_records': 0, + 'invalid_text_records': 0, 'partial_records': 0, + 'input_limit_exceeded': False, 'record_limit_reached': False, + 'marker_limit_reached': False, + } + if not isinstance(owned_pids, (set, frozenset)) or any( + type(pid) is not int or not 0 < pid <= 2147483647 for pid in owned_pids): + owned_pids = set() + result['ownership_usable'] = bool(owned_pids) + if usable is not True or not isinstance(payload, str): + return result + # Reject over-budget input rather than accepting a potentially split prefix. + if len(payload) > LIMIT: + result['input_limit_exceeded'] = True + return result + try: + if len(payload.encode('utf-8')) > LIMIT: + result['input_limit_exceeded'] = True + return result + except UnicodeEncodeError: + result['invalid_text_records'] = 1 + return result + lines = payload.split('\n') + if lines[-1]: + result['partial_records'] = 1 + lines.pop() # Never accept the terminal non-LF fragment as a complete record. + for line in lines: + if result['scanned_records'] >= MAX_RECORDS: + result['record_limit_reached'] = True + break + result['scanned_records'] += 1 + if line.endswith('\r'): + line = line[:-1] + if PREFIX not in line: + continue + if result['marker_records'] >= MAX_MARKERS: + result['marker_limit_reached'] = True + break + result['marker_records'] += 1 + if not _clean(line): + result['invalid_text_records'] += 1 + result['malformed_marker_records'] += 1 + continue + match = _frame(line) + if not match or not match[3].startswith(PREFIX): + result['malformed_marker_records'] += 1 + continue + form = _FORM.fullmatch(match[3]) + if not form: + result['malformed_marker_records'] += 1 + continue + if form[1] != 'v1' or form[2] not in STAGES: + result['unsupported_marker_records'] += 1 + continue + if int(match[1]) not in owned_pids: + result['foreign_pid_records'] += 1 + continue + result['owned_marker_records'] += 1 + stage = form[2] + if result['stages'][stage] is True: + result['duplicate_records'] += 1 + result['stages'][stage] = True + if result['owned_marker_records']: + partial = (full_input is not True or result['partial_records'] or + result['record_limit_reached'] or result['marker_limit_reached'] or + result['malformed_marker_records'] or result['unsupported_marker_records']) + result['state'] = 'PARTIAL' if partial else 'OBSERVED' + return result diff --git a/.github/tests/onboarding-boundary-observer.test.cjs b/.github/tests/onboarding-boundary-observer.test.cjs new file mode 100644 index 000000000..00f35a07c --- /dev/null +++ b/.github/tests/onboarding-boundary-observer.test.cjs @@ -0,0 +1,137 @@ +'use strict'; +// Execute the actual TS handler/effect closures with synthetic host inputs. +const assert = require('node:assert/strict'); +const test = require('node:test'); +const fs = require('node:fs'); +const path = require('node:path'); +const vm = require('node:vm'); +const ts = require('typescript'); +const root = path.resolve(__dirname, '../..'); +const onboarding = 'src/screens/Splash/OnBoardingSlides.tsx'; +const keeper = 'src/screens/NewKeeperAppScreen/NewKeeperAppScreen.tsx'; +const helper = 'src/utils/onboarding-boundary-observer.ts'; +const stages = ['ONBOARDING_EFFECT', 'SKIP_PRESS', 'SKIP_RESET_RETURNED', 'SKIP_RESET_THROWN', 'NEW_KEEPER_EFFECT']; +const plain = value => JSON.parse(JSON.stringify(value)); +function compile(source, extra = {}) { + const context = { exports: {}, global: {}, __DEV__: true, + require(name) { assert.equal(name, 'react-native'); return { Platform: { OS: 'android' } }; }, + console: new Proxy({}, { get() { throw new Error('No console/LogBox'); } }), + Date: new Proxy({}, { get() { throw new Error('No clock'); } }), + setTimeout() { throw new Error('No new timer'); }, fetch() { throw new Error('No network'); }, ...extra }; + vm.runInNewContext(ts.transpileModule(source, { compilerOptions: { + module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020, + } }).outputText, context); + return context; +} +function observer(extra = {}) { + const markers = []; + const context = compile(fs.readFileSync(path.join(root, helper), 'utf8'), { + global: { nativeLoggingHook: (...args) => markers.push(args) }, ...extra }); + return { run: context.exports.observeOnboardingBoundary, markers }; +} +function tree(file) { + return ts.createSourceFile(file, fs.readFileSync(path.join(root, file), 'utf8'), + ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX); +} +function findAll(source, predicate) { + const found = []; + function visit(node) { if (predicate(node)) found.push(node); ts.forEachChild(node, visit); } + visit(source); return found; +} +function skipHandler(extra) { + const source = tree(onboarding); + const elements = findAll(source, node => (ts.isJsxOpeningElement(node) || ts.isJsxSelfClosingElement(node)) && + node.attributes.properties.some(prop => ts.isJsxAttribute(prop) && prop.name.text === 'testID' && + ts.isStringLiteral(prop.initializer) && prop.initializer.text === 'btn_skip')); + assert.equal(elements.length, 1); + assert.equal(elements[0].tagName.getText(source), 'TouchableOpacity'); + const prop = elements[0].attributes.properties.find(prop => prop.name?.text === 'onPress'); + return compile('exports.run = ' + prop.initializer.expression.getText(source), extra).exports.run; +} +function effect(file, anchor, extra) { + const source = tree(file); + const all = findAll(source, node => ts.isCallExpression(node) && node.expression.getText(source) === 'useEffect'); + assert.equal(all.length, file === onboarding ? 1 : 6, 'observer must not add effects'); + const found = all.filter(node => node.arguments[0].getText(source).includes(anchor)); + assert.equal(found.length, 1); + assert.equal(found[0].arguments[1].getText(source), '[]', 'existing mount dependency list is unchanged'); + return compile('exports.run = ' + found[0].arguments[0].getText(source), extra).exports.run; +} + +test('Android dev hook emits bounded fixed stages at level3 with no payload/console/timer/network', () => { + const h = observer(); + for (const stage of stages) for (let n = 0; n < 9; n++) h.run(stage); + assert.equal(h.markers.length, 20); + for (const stage of stages) assert.equal(h.markers.filter(row => row[0] === 'KEEPER_ONBOARDING_BOUNDARY|v1|' + stage).length, 4); + assert.ok(h.markers.every(row => row.length === 2 && row[1] === 3 && row[0].length < 100)); + for (const bad of ['PRIVATE_SENTINEL', 'SKIP_PRESS\nPRIVATE_SENTINEL', {}, null]) h.run(bad); + assert.equal(h.markers.length, 20); + assert.equal(JSON.stringify(h.markers).includes('PRIVATE_SENTINEL'), false); +}); + +test('release guard precedes platform/hook inspection; iOS and broken hooks stay quiet', () => { + let inspected = 0; + const platform = new Proxy({}, { get() { inspected++; throw new Error('PRIVATE_SENTINEL'); } }); + const global = new Proxy({}, { get() { inspected++; throw new Error('PRIVATE_SENTINEL'); } }); + const release = observer({ __DEV__: false, global, require: () => platform }); + assert.doesNotThrow(() => release.run('SKIP_PRESS')); + assert.equal(inspected, 0); + const ios = observer({ global, require: () => ({ Platform: { OS: 'ios' } }) }); + assert.doesNotThrow(() => ios.run('SKIP_PRESS')); + assert.equal(inspected, 0); + for (const input of [{ global }, { global: { nativeLoggingHook() { throw new Error('PRIVATE_SENTINEL'); } } }, + { require: () => ({ get Platform() { throw new Error('PRIVATE_SENTINEL'); } }) }]) { + assert.doesNotThrow(() => observer(input).run('SKIP_PRESS')); + } +}); + +test('actual Skip handler preserves reset receiver/arguments/count and immediate return identity', () => { + const order = [], result = { get then() { throw new Error('Must not await/inspect return'); } }; + const h = observer({ global: { nativeLoggingHook: text => order.push(text.split('|')[2]) } }); + const navigation = { reset(...args) { + assert.equal(this, navigation); order.push('RESET'); + assert.deepEqual(plain(args), [{ index: 0, routes: [{ name: 'NewKeeperApp' }] }]); + return result; + } }; + const run = skipHandler({ navigation, observeOnboardingBoundary: h.run }); + assert.equal(run(), result); + assert.deepEqual(order, ['SKIP_PRESS', 'RESET', 'SKIP_RESET_RETURNED']); +}); + +test('actual Skip original throw wins and no error property is read even when the native hook throws', () => { + let reads = 0, calls = 0; + const original = new Proxy({}, { get() { reads++; throw new Error('PRIVATE_SENTINEL'); } }); + const hook = observer({ global: { nativeLoggingHook() { throw new Error('PRIVATE_SENTINEL observer'); } } }); + const run = skipHandler({ navigation: { reset() { calls++; throw original; } }, observeOnboardingBoundary: hook.run }); + assert.throws(run, caught => caught === original); + assert.equal(calls, 1); assert.equal(reads, 0); + const seen = observer(); + const failed = skipHandler({ navigation: { reset() { throw original; } }, observeOnboardingBoundary: seen.run }); + assert.throws(failed, caught => caught === original); + assert.deepEqual(seen.markers.map(row => row[0].split('|')[2]), ['SKIP_PRESS', 'SKIP_RESET_THROWN']); +}); + +test('actual existing Onboarding effect keeps one original listener and cleanup without adding effects', () => { + const order = [], h = observer(); + const cleanup = effect(onboarding, 'BackHandler.addEventListener', { + observeOnboardingBoundary: h.run, + BackHandler: { addEventListener(name, callback) { + order.push(name); assert.equal(callback(), true); + return { remove() { order.push('remove'); } }; + } }, + })(); + assert.deepEqual(order, ['hardwareBackPress']); cleanup(); + assert.deepEqual(order, ['hardwareBackPress', 'remove']); + assert.deepEqual(h.markers.map(row => row[0].split('|')[2]), ['ONBOARDING_EFFECT']); +}); + +test('actual existing NewKeeper mount effect keeps navigation ref and original timer cleanup', () => { + const h = observer(), navigationActive = { current: false }, timer = {}, cleared = []; + const cleanup = effect(keeper, 'navigationActive.current = true', { + observeOnboardingBoundary: h.run, navigationActive, nextNavigation: { current: timer }, + clearTimeout(value) { cleared.push(value); }, + })(); + assert.equal(navigationActive.current, true); assert.deepEqual(cleared, []); + cleanup(); assert.equal(navigationActive.current, false); assert.deepEqual(cleared, [timer]); + assert.deepEqual(h.markers.map(row => row[0].split('|')[2]), ['NEW_KEEPER_EFFECT']); +}); diff --git a/.github/tests/test_onboarding_failure_integration.py b/.github/tests/test_onboarding_failure_integration.py new file mode 100644 index 000000000..459ecba41 --- /dev/null +++ b/.github/tests/test_onboarding_failure_integration.py @@ -0,0 +1,120 @@ +"""Synthetic existing-query integration; no Android process/device is started.""" +import importlib.util +import json +from pathlib import Path +import subprocess +import tempfile +import types +import unittest +from unittest import mock + +NATIVE = Path(__file__).resolve().parents[1] / 'native' +spec = importlib.util.spec_from_file_location('onboarding_android_test', NATIVE / 'android_startup_diagnosis.py') +diag = importlib.util.module_from_spec(spec) +spec.loader.exec_module(diag) +MARKER = 'KEEPER_ONBOARDING_BOUNDARY|v1|SKIP_PRESS' + + +def line(message=MARKER, pid=100, tag='ReactNativeJS'): + return '10-22 12:00:00.123 %s 101 E %s: %s\n' % (pid, tag, message) + + +class IntegrationTests(unittest.TestCase): + def setUp(self): + self.temp = tempfile.TemporaryDirectory() + self.addCleanup(self.temp.cleanup) + self.private = Path(self.temp.name) + (self.private / 'metro.log').write_text('') + + def collect(self, payload, *, failed=False): + expected = [ + ['shell', 'ps', '-A', '-o', 'PID,NAME'], + ['shell', 'dumpsys', 'activity', 'activities'], + ['shell', 'dumpsys', 'activity', 'exit-info', diag.APP], + ['logcat', '-d', '-b', 'main', '-b', 'crash', '-t', '1200', '-v', 'threadtime', '*:E'], + ] + calls = [] + def query(args, stdout, **kwargs): + self.assertEqual(args[:3], ['adb', '-s', 'emulator-5554']) + self.assertEqual(args[3:], expected[len(calls)]) + self.assertEqual(kwargs, {'stderr': subprocess.STDOUT, 'timeout': 15, 'check': False}) + calls.append(args) + content = ['PID NAME\n100 ' + diag.APP + '\n', + 'topResumedActivity=ActivityRecord{a u0 ' + diag.APP + '/.MainActivity t1}\n', + '', payload][len(calls) - 1] + stdout.write(content.encode()) + return subprocess.CompletedProcess(args, 1 if failed and len(calls) == 4 else 0) + with mock.patch.object(diag.subprocess, 'run', side_effect=query): + result = diag.collect(self.private, 'emulator-5554') + self.assertEqual(len(calls), 4, 'observer must not add a native query') + self.assertEqual(result['cleanup_status'], 'DELETED') + self.assertFalse((self.private / 'android-startup-diagnosis').exists()) + return result + + def test_existing_four_queries_supply_separate_positive_receipt_not_runtime_faults(self): + result = self.collect(line() + line('TypeError: PRIVATE_SENTINEL') + + line('KEEPER_ONBOARDING_BOUNDARY|v999|PRIVATE_SENTINEL')) + receipt = result['onboarding_boundary_observation'] + self.assertEqual(receipt['state'], 'PARTIAL') + self.assertIs(receipt['stages']['SKIP_PRESS'], True) + self.assertIsNone(receipt['stages']['NEW_KEEPER_EFFECT']) + self.assertEqual(receipt['freshness'], 'GUEST_CLOCK_UNVERIFIED') + self.assertEqual(receipt['unsupported_marker_records'], 1) + errors = result['runtime_errors'] + self.assertEqual(errors['diagnostic_marker_records'], 2) + self.assertEqual(errors['own_error_records'], 1) + self.assertEqual(errors['parsed_error_records'], 1) + self.assertEqual(errors['fault_categories'], ['JS_TYPE_ERROR']) + self.assertNotIn('PRIVATE_SENTINEL', json.dumps(result)) + self.assertNotIn(diag.APP, json.dumps(result)) + + def test_prefix_must_start_native_message_with_exact_error_tag(self): + payload = (line('TypeError: ' + MARKER) + line(MARKER, tag='OtherTag') + + line('KEEPER_ONBOARDING_BOUNDARY|broken TypeError: PRIVATE_SENTINEL')) + errors = diag.android_log(payload, {100}) + self.assertEqual(errors['diagnostic_marker_records'], 1) + self.assertEqual(errors['own_error_records'], 2) + self.assertEqual(errors['fault_categories'], ['JS_TYPE_ERROR']) + self.assertNotIn('PRIVATE_SENTINEL', json.dumps(errors)) + + def test_failed_query_foreign_pid_and_control_text_never_claim_current_stage(self): + cases = [(line(), {'failed': True}), (line(pid=200), {}), + ('INFO PRIVATE_SENTINEL\r' + line(), {})] + for payload, options in cases: + with self.subTest(options=options, size=len(payload)): + receipt = self.collect(payload, **options)['onboarding_boundary_observation'] + self.assertEqual(receipt['state'], 'UNKNOWN') + self.assertTrue(all(value is None for value in receipt['stages'].values())) + self.assertNotIn('PRIVATE_SENTINEL', json.dumps(receipt)) + + def test_partial_input_retains_only_positive_complete_records_without_absence_claim(self): + original = diag.bounded + def partial(path, private): + payload, observed = original(path, private) + if path.name == 'runtime-errors.log': + observed.update(input_truncated=True, read_scope='PREFIX_AND_TAIL') + return payload, observed + with mock.patch.object(diag, 'bounded', side_effect=partial): + result = self.collect(line()) + receipt = result['onboarding_boundary_observation'] + self.assertEqual(receipt['state'], 'PARTIAL') + self.assertIs(receipt['stages']['SKIP_PRESS'], True) + self.assertIsNone(receipt['stages']['SKIP_RESET_RETURNED']) + + def test_optional_import_or_parser_failure_preserves_original_fault_facts_and_purge(self): + for importing in (True, False): + module = types.SimpleNamespace(parse=mock.Mock(side_effect=ValueError('PRIVATE_SENTINEL'))) + source = types.SimpleNamespace(loader=types.SimpleNamespace(exec_module=lambda _: None)) + with self.subTest(importing=importing), \ + mock.patch.object(diag.importlib.util, 'spec_from_file_location', + side_effect=OSError('PRIVATE_SENTINEL') if importing else None, return_value=source), \ + mock.patch.object(diag.importlib.util, 'module_from_spec', return_value=module): + result = self.collect(line() + line('TypeError: PRIVATE_SENTINEL')) + self.assertEqual(result['onboarding_boundary_observation']['state'], 'UNKNOWN') + self.assertEqual(result['runtime_errors']['fault_categories'], ['JS_TYPE_ERROR']) + self.assertEqual(result['runtime_errors']['diagnostic_marker_records'], 1) + self.assertNotIn('PRIVATE_SENTINEL', json.dumps(result)) + + +if __name__ == '__main__': + unittest.main() diff --git a/.github/tests/test_onboarding_failure_receipts.py b/.github/tests/test_onboarding_failure_receipts.py new file mode 100644 index 000000000..2bd631d59 --- /dev/null +++ b/.github/tests/test_onboarding_failure_receipts.py @@ -0,0 +1,149 @@ +import importlib.util +import json +from pathlib import Path +import unittest +from unittest import mock + +SPEC = importlib.util.spec_from_file_location( + 'onboarding_failure_receipts', + Path(__file__).resolve().parents[1] / 'native' / 'onboarding_failure_receipts.py') +receipts = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(receipts) + + +def record(stage='SKIP_PRESS', pid=100, *, version='v1', severity='E', tag='ReactNativeJS'): + return ('10-22 23:01:02.003 %d 101 %s %s: %s%s|%s' % + (pid, severity, tag, receipts.PREFIX, version, stage)) + + +class ReceiptsTests(unittest.TestCase): + def test_exact_owned_records_are_positive_only_and_have_unverified_freshness(self): + payload = '\n'.join(record(stage) for stage in receipts.STAGES) + '\n' + result = receipts.parse(payload, {100}) + self.assertEqual(result['state'], 'OBSERVED') + self.assertTrue(all(value is True for value in result['stages'].values())) + self.assertEqual(result['owned_marker_records'], 5) + self.assertEqual(result['freshness'], 'GUEST_CLOCK_UNVERIFIED') + self.assertEqual(result['cause'], 'UNDETERMINED') + self.assertEqual(result['coverage'], 'POSITIVE_ONLY_MISSING_STAGES_UNKNOWN') + self.assertEqual(result['producer_per_stage_limit'], 4) + self.assertEqual(result['producer_total_limit'], 20) + self.assertEqual(result['consumer_marker_limit'], 64) + self.assertEqual(result['input_byte_limit'], 512 * 1024) + self.assertEqual(result['scan_record_limit'], 10000) + missing = receipts.parse(record() + '\n', {100}) + self.assertIsNone(missing['stages']['NEW_KEEPER_EFFECT']) + self.assertNotIn(False, missing['stages'].values()) + + def test_foreign_and_duplicate_records_have_separate_bounded_counts(self): + payload = record(pid=200) + '\n' + (record() + '\n') * 2 + result = receipts.parse(payload, {100}) + self.assertEqual(result['foreign_pid_records'], 1) + self.assertEqual(result['duplicate_records'], 1) + self.assertEqual(result['owned_marker_records'], 2) + self.assertEqual(result['marker_records'], 3) + self.assertTrue(receipts.is_diagnostic_marker_record(record(pid=200))) + self.assertTrue(receipts.has_diagnostic_marker_prefix(record(pid=200))) + + def test_calendar_validation_rejects_impossible_dates_without_using_a_clock(self): + for calendar in ('02-30', '02-31', '04-31', '06-31', '09-31', '11-31'): + line = record().replace('10-22', calendar) + with self.subTest(calendar=calendar): + self.assertFalse(receipts.is_diagnostic_marker_record(line)) + self.assertFalse(receipts.has_diagnostic_marker_prefix(line)) + result = receipts.parse(line + '\n', {100}) + self.assertEqual(result['state'], 'UNKNOWN') + self.assertEqual(result['malformed_marker_records'], 1) + for calendar in ('02-29', '04-30', '12-31'): + self.assertTrue(receipts.is_diagnostic_marker_record(record().replace('10-22', calendar))) + + def test_unusable_or_invalid_ownership_never_accepts_receipts(self): + for owned in (set(), {True}, {'100'}, [100], {0}, {-1}, {2147483648}): + with self.subTest(owned=owned): + result = receipts.parse(record() + '\n', owned) + self.assertEqual(result['state'], 'UNKNOWN') + self.assertTrue(all(value is None for value in result['stages'].values())) + result = receipts.parse(record() + '\n', {100}, usable=False) + self.assertEqual(result['scanned_records'], 0) + self.assertEqual(result['state'], 'UNKNOWN') + + def test_incomplete_input_keeps_positives_but_missing_stages_unknown(self): + complete = record() + '\n' + for payload, full_input in ((complete, False), (complete + record('NEW_KEEPER_EFFECT'), True)): + with self.subTest(full_input=full_input): + result = receipts.parse(payload, {100}, full_input=full_input) + self.assertEqual(result['state'], 'PARTIAL') + self.assertTrue(result['stages']['SKIP_PRESS']) + self.assertIsNone(result['stages']['NEW_KEEPER_EFFECT']) + self.assertEqual(receipts.parse(record(), {100})['state'], 'UNKNOWN') + self.assertEqual(receipts.parse(record() + '\r\n', {100})['state'], 'OBSERVED') + + def test_strict_frame_and_complete_marker_reject_substrings_controls_and_unicode(self): + original = record() + malformed = [ + original + ' PRIVATE_SENTINEL', original + '|PRIVATE_SENTINEL', + original.replace(': KEEPER_', ': TypeError PRIVATE_SENTINEL KEEPER_'), + original.replace('ReactNativeJS:', 'PrivateTag:'), + original.replace(' E ', ' F '), original.replace(' E ', ' I '), + original.replace('23:01:02.003', '25:01:02.003'), + original.replace('.003', '.0030'), original.replace('100 101', '0 101'), + original.replace('100 101', '2147483648 101'), + original.replace(' E ', '\tE '), original + '\x00', + original.replace('|v1|', '|v1|\r'), + original + '\ufffd', original + '\u2028', original + '\u2029', + original + '\x85', original + '\x1b', original + '\t', + ] + for line in malformed: + with self.subTest(line=repr(line)): + self.assertFalse(receipts.is_diagnostic_marker_record(line)) + result = receipts.parse(line + '\n', {100}) + self.assertEqual(result['state'], 'UNKNOWN') + self.assertTrue(all(value is None for value in result['stages'].values())) + self.assertEqual(result['malformed_marker_records'], 1) + self.assertNotIn('PRIVATE_SENTINEL', json.dumps(result)) + + def test_reserved_prefix_is_separate_from_strict_success_and_ordinary_faults(self): + for line in (record(stage='UNKNOWN_STAGE'), record(version='v2'), + record() + '|PRIVATE_SENTINEL', record() + '\x00'): + self.assertTrue(receipts.has_diagnostic_marker_prefix(line)) + self.assertFalse(receipts.is_diagnostic_marker_record(line)) + for line in ('TypeError: PRIVATE_SENTINEL', + record().replace(': KEEPER_', ': TypeError KEEPER_'), + record(tag='PrivateTag'), record(severity='F')): + self.assertFalse(receipts.has_diagnostic_marker_prefix(line)) + result = receipts.parse(record(stage='UNKNOWN_STAGE') + '\n' + record(version='v2') + '\n', {100}) + self.assertEqual(result['unsupported_marker_records'], 2) + self.assertEqual(result['malformed_marker_records'], 0) + + def test_input_record_and_marker_budgets_fail_closed_and_never_echo_input(self): + oversized = record() + '\n' + 'PRIVATE_SENTINEL' * (receipts.LIMIT // 10) + result = receipts.parse(oversized, {100}) + self.assertTrue(result['input_limit_exceeded']) + self.assertEqual(result['state'], 'UNKNOWN') + with mock.patch.object(receipts, 'LIMIT', 100): + result = receipts.parse('\u20ac' * 34, {100}) + self.assertTrue(result['input_limit_exceeded']) + with mock.patch.object(receipts, 'MAX_RECORDS', 2): + result = receipts.parse('private\nprivate\n' + record() + '\n', {100}) + self.assertTrue(result['record_limit_reached']) + self.assertEqual(result['scanned_records'], 2) + self.assertEqual(result['state'], 'UNKNOWN') + with mock.patch.object(receipts, 'MAX_MARKERS', 2): + result = receipts.parse((record() + '\n') * 3, {100}) + self.assertTrue(result['marker_limit_reached']) + self.assertEqual(result['marker_records'], 2) + self.assertEqual(result['state'], 'PARTIAL') + self.assertNotIn('PRIVATE_SENTINEL', json.dumps(result)) + + def test_private_content_surrogates_and_terminal_fragments_never_escape(self): + for payload in ('PRIVATE_SENTINEL\n', record() + ' PRIVATE_SENTINEL', + '\ud800PRIVATE_SENTINEL\n', None): + result = receipts.parse(payload, {100}) + serialized = json.dumps(result) + self.assertNotIn('PRIVATE_SENTINEL', serialized) + self.assertNotIn('ReactNativeJS', serialized) + self.assertEqual(result['state'], 'UNKNOWN') + + +if __name__ == '__main__': + unittest.main() diff --git a/docs/r5-onboarding-boundary-observation.md b/docs/r5-onboarding-boundary-observation.md new file mode 100644 index 000000000..b27c6fb3c --- /dev/null +++ b/docs/r5-onboarding-boundary-observation.md @@ -0,0 +1,108 @@ +# Android onboarding boundary observation + +This isolated preparation starts from PR #7131 source +`e34331083959c94b5f0a17603d8c1f578305c474`. It implements observation only and has +not run on a native host. It makes no app, dependency, selector, tap, timing or +navigation correction, and selects no new hosted window. It preserves the +existing credential-failure preparation and does not add an acceptance predicate. + +The actual Android artifact `12466091444` from run `39858617273`, attempt `1`, +source `1bc34890cb8618b2fffd446a63924974e0fe15b5`, identifies a failed +ASSERT_VISIBLE `start_new_tile` after the host acknowledged `skip_tapped`. +Its one retained hierarchy contains Skip; Start New IDs/public text are absent. +The current-owned product Metro window contains five valid lines and no fixed +error header. Those observations do not identify why the transition failed. +They do not justify another tap, a timeout change, a new selector or an app fix. + +The observer emits only these fixed stages: + +| Stage | Code boundary observed | +| --- | --- | +| `ONBOARDING_EFFECT` | Entry to the existing OnBoardingSlides mount effect | +| `SKIP_PRESS` | Entry to the original Skip handler | +| `SKIP_RESET_RETURNED` | The original synchronous `navigation.reset` call returned | +| `SKIP_RESET_THROWN` | That original call threw; the same object is rethrown | +| `NEW_KEEPER_EFFECT` | Entry to NewKeeperApp's existing mount effect | + +The original inline Skip handler still calls +`navigation.reset({ index: 0, routes: [{ name: 'NewKeeperApp' }] })` exactly once. +It returns the same value immediately, without awaiting or inspecting it. +Neither the original error nor any route, state, account, event, message or +credential payload is inspected or logged. An immediate return does not prove +that navigation handled the action or a new screen committed. Existing effect +dependencies, registration, cleanup and timer cleanup are unchanged. No effects, +listeners, timers, native queries, file writes or network operations are added. +Layout, props, test IDs and accessibility behavior are unchanged. + +`observeOnboardingBoundary` first checks `__DEV__`, then Android's static +`Platform.OS`, before inspecting the native logging hook. Release and iOS calls +emit nothing. In development Android it writes the fixed string +`KEEPER_ONBOARDING_BOUNDARY|v1|` directly through `nativeLoggingHook` at +level 3, bypassing console/ExceptionsManager/LogBox. Hook/getter failures are +quiet. Each stage is limited to four emissions per JS process, twenty total; +missing records cannot establish that a boundary did not execute. + +Pinned React Native 0.83.9 source verifies this Android sink: + +- `ReactAndroid/src/main/jni/react/runtime/jni/JReactInstance.cpp` binds + `reactAndroidLoggingHook` through `bindNativeLogger`. +- `ReactCommon/jsiexecutor/jsireact/JSIExecutor.cpp` passes the message and numeric + level directly to that logger. +- `ReactAndroid/src/main/jni/react/jni/JSLogging.cpp` writes under `ReactNativeJS` + at `level + ANDROID_LOG_DEBUG`. Android's DEBUG value is 3, so level 3 reaches + ERROR (6), already included by the existing `*:E` query. + +The cached RN tarball SHA1 is `61d2a4ccd89e06b98992c9b9781856ad88a09d39`. +The three respective source SHA256 values are +`251c8d866c84f606c9e1fab3e75fefe67318954df447fb0bfd2e441ce234c598`, +`ab696163e3bd3408bbc5c9a401b9e49d3b85f775884e883c47494084b83b36c0`, +and `fa2c24d2ad0434d2534cc0073efa487c75ee98d2d75071e4ec14b4162eb43623`. +This source proof does not claim that the exact hosted sink has been exercised. + +The existing Android post-failure collector still runs its original four +commands with the same arguments and 15-second deadlines. The new pure parser +receives that already-collected raw threadtime payload and current owned PID +inventory. Only complete LF/CRLF records with the fixed Error-level +`ReactNativeJS` framing, valid date/time fields and exact allowed marker are +positive observations. Missing stages remain null/UNKNOWN. Foreign PIDs, +duplicates, malformed text, partial records and exhausted limits have separate +bounded counters. The consumer discloses its 512 KiB, 10,000-record and 64-marker +limits. The receipt remains diagnostic, with cause UNDETERMINED and guest-clock +freshness UNVERIFIED; it cannot bind a record to this particular Maestro tap, +prove a route outcome or establish current screen visibility. + +Reserved marker messages beginning exactly at the native message start remain +separate from runtime error counts, including rejected diagnostic suffixes. +Ordinary errors merely mentioning the marker later remain ordinary error input. +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. + +Host controls execute the actual TypeScript handler/effect closures, including +original return/throw priority, listener/timer cleanup, release guards and quiet +observer failures. Parser/integration controls exercise foreign PIDs, malformed +framing, reserved-prefix placement, limits, privacy, optional failures and the +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. diff --git a/src/screens/NewKeeperAppScreen/NewKeeperAppScreen.tsx b/src/screens/NewKeeperAppScreen/NewKeeperAppScreen.tsx index 6b2f9b489..25fea1aba 100644 --- a/src/screens/NewKeeperAppScreen/NewKeeperAppScreen.tsx +++ b/src/screens/NewKeeperAppScreen/NewKeeperAppScreen.tsx @@ -30,6 +30,7 @@ import { useIsFocused } from '@react-navigation/native'; import Fonts from 'src/constants/Fonts'; import ThemedSvg from 'src/components/ThemedSvg.tsx/ThemedSvg'; import { currentBackupAccountSession } from 'src/services/backup/transport'; +import { observeOnboardingBoundary } from 'src/utils/onboarding-boundary-observer'; export function Tile({ title, subTitle, onPress, Icon = null, loading = false }) { const { colorMode } = useColorMode(); @@ -166,6 +167,7 @@ function NewKeeperApp({ navigation }: { navigation }) { }, [appRecreated]); useEffect(() => { + observeOnboardingBoundary('NEW_KEEPER_EFFECT'); navigationActive.current = true; return () => { navigationActive.current = false; diff --git a/src/screens/Splash/OnBoardingSlides.tsx b/src/screens/Splash/OnBoardingSlides.tsx index 234fcc321..cf47528c9 100644 --- a/src/screens/Splash/OnBoardingSlides.tsx +++ b/src/screens/Splash/OnBoardingSlides.tsx @@ -18,6 +18,7 @@ import { KEEPER_KNOWLEDGEBASE } from 'src/utils/service-utilities/config'; import useIsSmallDevices from 'src/hooks/useSmallDevices'; import ThemedColor from 'src/components/ThemedColor/ThemedColor'; import ThemedSvg from 'src/components/ThemedSvg.tsx/ThemedSvg'; +import { observeOnboardingBoundary } from 'src/utils/onboarding-boundary-observer'; const { width } = Dimensions.get('window'); @@ -50,6 +51,7 @@ function OnBoardingSlides({ navigation }) { ]); useEffect(() => { + observeOnboardingBoundary('ONBOARDING_EFFECT'); const backAction = () => true; const backHandler = BackHandler.addEventListener('hardwareBackPress', backAction); return () => backHandler.remove(); @@ -70,7 +72,17 @@ function OnBoardingSlides({ navigation }) { 715 ? 5 : 2} height={10}> {currentPosition !== 2 && ( navigation.reset({ index: 0, routes: [{ name: 'NewKeeperApp' }] })} + onPress={() => { + observeOnboardingBoundary('SKIP_PRESS'); + try { + const result = navigation.reset({ index: 0, routes: [{ name: 'NewKeeperApp' }] }); + observeOnboardingBoundary('SKIP_RESET_RETURNED'); + return result; + } catch (error) { + observeOnboardingBoundary('SKIP_RESET_THROWN'); + throw error; + } + }} style={styles.skipTextWrapper} testID="btn_skip" > diff --git a/src/utils/onboarding-boundary-observer.ts b/src/utils/onboarding-boundary-observer.ts new file mode 100644 index 000000000..9da583cac --- /dev/null +++ b/src/utils/onboarding-boundary-observer.ts @@ -0,0 +1,33 @@ +import { Platform } from 'react-native'; + +type OnboardingBoundaryStage = + | 'ONBOARDING_EFFECT' + | 'SKIP_PRESS' + | 'SKIP_RESET_RETURNED' + | 'SKIP_RESET_THROWN' + | 'NEW_KEEPER_EFFECT'; + +const STAGES = [ + 'ONBOARDING_EFFECT', + 'SKIP_PRESS', + 'SKIP_RESET_RETURNED', + 'SKIP_RESET_THROWN', + 'NEW_KEEPER_EFFECT', +]; +const counts: Partial> = {}; + +// Fixed development observations only. No route, state, event or error payload. +export const observeOnboardingBoundary = (stage: OnboardingBoundaryStage) => { + try { + if (typeof __DEV__ === 'undefined' || !__DEV__) return; + if (Platform.OS !== 'android' || !STAGES.includes(stage) || (counts[stage] || 0) >= 4) return; + const hook = (global as unknown as { nativeLoggingHook?: unknown }).nativeLoggingHook; + if (typeof hook !== 'function') return; + counts[stage] = (counts[stage] || 0) + 1; + // RN0.83.9's direct native hook maps level3 to ReactNativeJS Error on + // Android. This bypasses console/LogBox and does not create an exception. + hook(`KEEPER_ONBOARDING_BOUNDARY|v1|${stage}`, 3); + } catch { + /* Observation is optional and cannot replace navigation results. */ + } +};