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
122 changes: 122 additions & 0 deletions .github/scripts/read-d270-android-artifact.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
'use strict';
// One finite metadata projection; no native worker, release authority or selector input.
const assert = require('node:assert/strict');
const crypto = require('node:crypto');
const fs = require('node:fs');
const path = require('node:path');
const { execFileSync } = require('node:child_process');
const OWNER = 'KeeperCommunity', REPO = 'bitcoin-keeper';
const RUN = 40065599904, JOB = 120796675691;
const SOURCE = 'd27007078808617a97ee460b592eefb44e7e5325';
const WORKFLOW = 'f5f20e6d959109cdc2e0096099992d72307255bf';
// Exact terminal Android job/artifact API and upload facts.
const ARTIFACT = 12551461839, ZIP_SIZE = 9596, DIGEST = 'b9182e02a33b7e0568d73654b42d61f66bce8e425d65a6960aa0f4f2194ee13a';
const NATIVE_CONCLUSION = 'failure', STEP_CONCLUSIONS = ['success', 'failure'];
const STEPS = ['Build development app', 'Run Android startup and native persistence probes'];
const CONCLUSIONS = ['success', 'failure', 'skipped'];
const NAME = `keeper-diagnostic-native-ANDROID-${SOURCE}-${RUN}-1`;

async function run({ github, context, core }) {
let owned, primary = false, output, stage = 'SELECTION';
try {
assert.ok(Number.isSafeInteger(ARTIFACT) && ARTIFACT > 0);
assert.ok(Number.isSafeInteger(ZIP_SIZE) && ZIP_SIZE > 0 && ZIP_SIZE <= 1024 * 1024 + 4096);
assert.match(DIGEST ?? '', /^[a-f0-9]{64}$/);
assert.ok(['success', 'failure'].includes(NATIVE_CONCLUSION));
assert.ok(Array.isArray(STEP_CONCLUSIONS) && STEP_CONCLUSIONS.length === STEPS.length &&
STEPS.every((_, index) => CONCLUSIONS.includes(STEP_CONCLUSIONS[index])));
assert.equal(context.repo.owner, OWNER); assert.equal(context.repo.repo, REPO);
assert.equal(context.eventName, 'push');
assert.equal(context.ref, 'refs/heads/codex/r5-d270-android-artifact-readout-20261024');
const args = { owner: OWNER, repo: REPO, request: { timeout: 30000 } };
const admit = async () => {
stage = 'API_LOOKUP';
const [r, j, a] = await Promise.all([
github.rest.actions.getWorkflowRun({ ...args, run_id: RUN }),
github.rest.actions.getJobForWorkflowRun({ ...args, job_id: JOB }),
github.rest.actions.getArtifact({ ...args, artifact_id: ARTIFACT }),
]);
stage = 'RUN_BINDING';
assert.equal(r.data.id, RUN); assert.equal(r.data.run_attempt, 1);
assert.equal(r.data.repository.id, 470904030); assert.equal(r.data.head_repository.id, 470904030);
assert.equal(r.data.head_sha, SOURCE); assert.equal(r.data.event, 'pull_request');
assert.equal(r.data.head_branch, 'codex/r5-native-acceptance-20261024');
assert.equal(r.data.path, '.github/workflows/keeper-native-diagnostic.yml');
assert.equal(r.data.status, 'completed'); assert.equal(r.data.conclusion, 'failure');
assert.deepEqual(r.data.referenced_workflows, [{
path: `${OWNER}/${REPO}/.github/workflows/keeper-native-platform.yml@${WORKFLOW}`,
ref: 'refs/pull/7149/merge', sha: WORKFLOW,
}]);
stage = 'JOB_BINDING';
assert.equal(j.data.id, JOB); assert.equal(j.data.run_id, RUN); assert.equal(j.data.run_attempt, 1);
assert.equal(j.data.head_sha, SOURCE); assert.equal(j.data.status, 'completed');
assert.equal(j.data.conclusion, NATIVE_CONCLUSION);
for (const [index, name] of STEPS.entries()) {
const steps = j.data.steps.filter(step => step.name === name);
assert.equal(steps.length, 1); assert.equal(steps[0].status, 'completed');
assert.equal(steps[0].conclusion, STEP_CONCLUSIONS[index]);
}
const upload = j.data.steps.filter(step => step.name === 'Retain safe Phase A evidence');
assert.equal(upload.length, 1); assert.equal(upload[0].status, 'completed');
assert.equal(upload[0].conclusion, 'success');
stage = 'ARTIFACT_BINDING';
assert.equal(a.data.id, ARTIFACT); assert.equal(a.data.name, NAME);
assert.equal(a.data.expired, false); assert.equal(a.data.size_in_bytes, ZIP_SIZE);
assert.equal(a.data.digest, `sha256:${DIGEST}`);
assert.equal(a.data.workflow_run.id, RUN); assert.equal(a.data.workflow_run.head_sha, SOURCE);
assert.equal(a.data.workflow_run.repository_id, 470904030);
assert.equal(a.data.workflow_run.head_repository_id, 470904030);
};
await admit();
stage = 'DOWNLOAD';
const download = await github.rest.actions.downloadArtifact({ ...args, artifact_id: ARTIFACT, archive_format: 'zip' });
const bytes = Buffer.from(download.data);
assert.equal(bytes.length, ZIP_SIZE);
assert.equal(crypto.createHash('sha256').update(bytes).digest('hex'), DIGEST);
stage = 'PRIVATE_FILE';
const temp = process.env.RUNNER_TEMP;
assert.ok(temp && path.isAbsolute(temp)); assert.equal(fs.realpathSync(temp), path.resolve(temp));
const filename = path.join(temp, 'r5-d270-android-fixed.zip');
const descriptor = fs.openSync(filename, 'wx', 0o600); owned = filename;
try { fs.writeFileSync(descriptor, bytes); }
finally { fs.closeSync(descriptor); }
stage = 'DECODE';
try {
output = execFileSync('python3', ['-B', '.github/scripts/read-d270-android-artifact.py'],
{ encoding: 'utf8', timeout: 30000, maxBuffer: 2 * 1024 * 1024, stdio: ['ignore', 'pipe', 'pipe'] });
} catch (error) {
const match = /^D270_ANDROID_OBSERVATION_READOUT_FAILED:(PRIVATE_FILE|KERNEL|ZIP_IDENTITY|ZIP_LAYOUT|JSON|ROOT_KEYS|PRODUCT|PROJECTED_METADATA)\n$/.exec(String(error.stdout ?? ''));
if (match) stage = 'DECODE_' + match[1];
throw error;
}
stage = 'PROJECTION_FRAME';
const lines = output.trim().split('\n');
assert.equal(lines.length, 3);
assert.equal(lines[0], 'D270_ANDROID_OBSERVATIONS_BEGIN');
assert.equal(lines[2], 'D270_ANDROID_OBSERVATIONS_END');
const projection = JSON.parse(lines[1]);
assert.deepEqual(Object.keys(projection).sort(), ['schema', 'scope', 'original_zip_sha256', 'metadata_sha256', 'observations'].sort());
assert.equal(projection.schema, 1);
assert.equal(projection.scope, 'OBSERVATIONS_ONLY_NOT_NATIVE_ACCEPTANCE_OR_CAUSE');
assert.equal(projection.original_zip_sha256, DIGEST);
assert.match(projection.metadata_sha256, /^[a-f0-9]{64}$/);
assert.equal(projection.observations.source_sha, SOURCE);
assert.equal(projection.observations.run_id, String(RUN));
assert.equal(projection.observations.run_attempt, '1');
await admit(); // Recheck official immutable source/workflow/job/attempt/artifact before releasing output.
} catch (_) {
primary = true;
throw new Error('FIXED_NATIVE_ARTIFACT_READOUT_FAILED_' + stage);
} finally {
if (owned) {
try { fs.unlinkSync(owned); }
catch (_) {
try { core.warning('FIXED_NATIVE_ARTIFACT_PRIVATE_CLEANUP_FAILED'); } catch (_) { /* retain primary */ }
if (!primary) throw new Error('FIXED_NATIVE_ARTIFACT_PRIVATE_CLEANUP_FAILED');
}
}
}
try { core.info(output.trim()); }
catch (_) { throw new Error('FIXED_NATIVE_ARTIFACT_READOUT_FAILED'); }
}
module.exports = { run };
102 changes: 102 additions & 0 deletions .github/scripts/read-d270-android-artifact.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/usr/bin/env python3
"""Project fixed d270 Android failure facts using the unchanged 044 kernel."""
import hashlib
import importlib.util
import io
import json
import os
from pathlib import Path
import stat
import zipfile

ROOT = Path(__file__).parent
KERNEL_SHA256 = '1e9fc4370d733041fb5a48dc8c022e3461decafbf970ba030e90784208aed1d9'
SOURCE = 'd27007078808617a97ee460b592eefb44e7e5325'
WORKFLOW = 'f5f20e6d959109cdc2e0096099992d72307255bf'
WORKFLOW_REF = 'KeeperCommunity/bitcoin-keeper/.github/workflows/keeper-native-diagnostic.yml@refs/pull/7149/merge'
RUN, ATTEMPT = '40065599904', '1'
ZIP_SIZE = 9596
DIGEST = 'b9182e02a33b7e0568d73654b42d61f66bce8e425d65a6960aa0f4f2194ee13a'
SCOPE = 'OBSERVATIONS_ONLY_NOT_NATIVE_ACCEPTANCE_OR_CAUSE'
PHASE = 'PRIVATE_FILE'
FIELDS = frozenset(('schema', 'source_sha', 'workflow_sha', 'workflow_ref', 'run_id', 'run_attempt',
'platform', 'diagnostic_only', 'build', 'smoke', 'native_probe', 'r5_features', 'hardware', 'signing',
'app_id', 'install_succeeded', 'installed_artifact_verified'))
SAMPLE_FIELDS = frozenset(('sample', 'status', 'completed_stages', 'pin_onboarding_progress',
'create_new_app_calls', 'other_backend_calls_rejected', 'failure_observation',
'failed_assertion_snapshot', 'android_runtime_diagnostic'))


def project(blob):
global PHASE
PHASE = 'KERNEL'
source = ROOT / 'read-044-android-artifact.py'
if hashlib.sha256(source.read_bytes()).hexdigest() != KERNEL_SHA256:
raise ValueError('Fixed kernel changed.')
spec = importlib.util.spec_from_file_location('fixed_044_android_kernel', source)
kernel = importlib.util.module_from_spec(spec)
spec.loader.exec_module(kernel)
literals = kernel.public_literals() # Original hash-checked policy remains unchanged.
kernel.SOURCE, kernel.WORKFLOW, kernel.WORKFLOW_REF = SOURCE, WORKFLOW, WORKFLOW_REF
kernel.RUN, kernel.ATTEMPT = RUN, ATTEMPT
kernel.public_literals = lambda: literals | {SOURCE, WORKFLOW, WORKFLOW_REF, RUN, ATTEMPT}
PHASE = 'ZIP_IDENTITY'
if type(blob) is not bytes or len(blob) != ZIP_SIZE or hashlib.sha256(blob).hexdigest() != DIGEST:
kernel.rejected()
PHASE = 'ZIP_LAYOUT'
with zipfile.ZipFile(io.BytesIO(blob)) as archive:
members = archive.infolist()
if len(members) != 1:
kernel.rejected()
entry = members[0]
if (entry.filename != 'android/metadata.json' or entry.orig_filename != entry.filename or
entry.flag_bits & 1 or stat.S_IFMT(entry.external_attr >> 16) not in (0, stat.S_IFREG) or
entry.file_size > kernel.LIMIT or entry.compress_type not in (zipfile.ZIP_STORED, zipfile.ZIP_DEFLATED)):
kernel.rejected()
raw = kernel.canonical_payload(blob, entry, archive.start_dir, archive.comment)
PHASE = 'JSON'
data = json.loads(raw, object_pairs_hook=kernel.pairs, parse_float=kernel.finite,
parse_constant=lambda _: kernel.rejected())
PHASE = 'ROOT_KEYS'
if type(data) is not dict or not set(data) <= kernel.ROOT_KEYS:
kernel.rejected()
projected = {key: value for key, value in data.items() if key in FIELDS}
PHASE = 'PRODUCT'
product = data.get('product_ui')
if (type(product) is not dict or product.get('status') != 'FAIL' or
type(product.get('samples')) is not list or not 1 <= len(product['samples']) <= 6 or
any(type(sample) is not dict for sample in product['samples'])):
kernel.rejected()
projected['product_ui'] = {key: product[key] for key in ('status', 'failure_category') if key in product}
projected['product_ui']['samples'] = [{key: value for key, value in sample.items() if key in SAMPLE_FIELDS}
for sample in product['samples']]
PHASE = 'PROJECTED_METADATA'
kernel.validate_metadata(projected)
return {'schema': 1, 'scope': SCOPE, 'original_zip_sha256': DIGEST,
'metadata_sha256': hashlib.sha256(raw).hexdigest(), 'observations': projected}


def main(argv=None):
global PHASE
PHASE = 'PRIVATE_FILE'
try:
if argv:
raise ValueError('No selectors.')
path = Path(os.environ['RUNNER_TEMP']) / 'r5-d270-android-fixed.zip'
with os.fdopen(os.open(path, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK), 'rb') as source:
info = os.fstat(source.fileno())
if not stat.S_ISREG(info.st_mode) or stat.S_IMODE(info.st_mode) != 0o600 or info.st_size != ZIP_SIZE:
raise ValueError('Invalid private file.')
result = project(source.read(ZIP_SIZE + 1))
except Exception:
print('D270_ANDROID_OBSERVATION_READOUT_FAILED:' + PHASE)
return 1
print('D270_ANDROID_OBSERVATIONS_BEGIN')
print(json.dumps(result, sort_keys=True, separators=(',', ':')))
print('D270_ANDROID_OBSERVATIONS_END')
return 0


if __name__ == '__main__':
import sys
raise SystemExit(main(sys.argv[1:]))
Loading