Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .github/native/android_startup_diagnosis.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 '):
Expand All @@ -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
Expand Down Expand Up @@ -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)}
Expand Down
149 changes: 149 additions & 0 deletions .github/native/onboarding_failure_receipts.py
Original file line number Diff line number Diff line change
@@ -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
137 changes: 137 additions & 0 deletions .github/tests/onboarding-boundary-observer.test.cjs
Original file line number Diff line number Diff line change
@@ -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']);
});
Loading