openenv-multidomain / validate_dataset.py
bochencs's picture
Publish reviewed bochencs-media-ledger-v14
e61e854 verified
Raw History Blame Contribute Delete
12.2 kB
"""Verify the dataset inventory and pinned source records offline."""
import hashlib
import json
import re
from pathlib import Path, PurePosixPath
SOURCE_MANIFEST_SHA256 = 'd322ada277050ecf9c3521ab749c61b0b58606b820dc1985f82986d0324b1e3c'
SCHEMA_SHA256 = 'c486d87e05b69ab133dd04cba072d5a39de8fda53c9a2f6fd5995570384724b0'
TASK_ROWS_SHA256 = 'd365647a20bbfcda6a418643f30e94827dbe2db1187d3376eb7032b71fbe7ac3'
EXPECTED_FILES = ('README.md', 'SOURCE_LOCK.json', 'components/qefiles/.dockerignore', 'components/qefiles/ADAPTER_NOTICE', 'components/qefiles/Dockerfile', 'components/qefiles/LICENSE', 'components/qefiles/SOURCE_MANIFEST.json', 'components/qefiles/THIRD_PARTY_NOTICES', 'components/qefiles/runtime/qefiles_compat/__init__.py', 'components/qefiles/runtime/qefiles_compat/environment.py', 'components/qefiles/runtime/qefiles_compat/server.py', 'components/qefiles/upstream/qe_arena/__init__.py', 'components/qefiles/upstream/qe_arena/app.py', 'components/qefiles/upstream/qe_arena/corpus/__init__.py', 'components/qefiles/upstream/qe_arena/corpus/aggregate_risk_factors.py', 'components/qefiles/upstream/qe_arena/corpus/alternate_chat_roles.py', 'components/qefiles/upstream/qe_arena/corpus/amortization_schedule.py', 'components/qefiles/upstream/qe_arena/corpus/api_complexity_level.py', 'components/qefiles/upstream/qe_arena/corpus/artifact_retention_keep.py', 'components/qefiles/upstream/qe_arena/corpus/backoff_schedule.py', 'components/qefiles/upstream/qe_arena/corpus/badge_dimensions.py', 'components/qefiles/upstream/qe_arena/corpus/baseline_verdict.py', 'components/qefiles/upstream/qe_arena/corpus/booking_conflicts.py', 'components/qefiles/upstream/qe_arena/corpus/boundary_violations.py', 'components/qefiles/upstream/qe_arena/corpus/brace_delta.py', 'components/qefiles/upstream/qe_arena/corpus/calibration_fit.py', 'components/qefiles/upstream/qe_arena/corpus/chat_completion_result.py', 'components/qefiles/upstream/qe_arena/corpus/claim_steal_priority.py', 'components/qefiles/upstream/qe_arena/corpus/compare_versions.py', 'components/qefiles/upstream/qe_arena/corpus/complexity_bucket.py', 'components/qefiles/upstream/qe_arena/corpus/compound_interest.py', 'components/qefiles/upstream/qe_arena/corpus/convert_timecode.py', 'components/qefiles/upstream/qe_arena/corpus/coverage_gap_severity.py', 'components/qefiles/upstream/qe_arena/corpus/crop_to_aspect.py', 'components/qefiles/upstream/qe_arena/corpus/debounce_switch.py', 'components/qefiles/upstream/qe_arena/corpus/decay_table.py', 'components/qefiles/upstream/qe_arena/corpus/efficiency_reward.py', 'components/qefiles/upstream/qe_arena/corpus/expense_violations.py', 'components/qefiles/upstream/qe_arena/corpus/find_stop.py', 'components/qefiles/upstream/qe_arena/corpus/flakiness_pattern.py', 'components/qefiles/upstream/qe_arena/corpus/flaky_hunter_reward.py', 'components/qefiles/upstream/qe_arena/corpus/flaky_run_summary.py', 'components/qefiles/upstream/qe_arena/corpus/format_duration.py', 'components/qefiles/upstream/qe_arena/corpus/gguf_loader.py', 'components/qefiles/upstream/qe_arena/corpus/gguf_special_ids.py', 'components/qefiles/upstream/qe_arena/corpus/glob_match.py', 'components/qefiles/upstream/qe_arena/corpus/income_tax.py', 'components/qefiles/upstream/qe_arena/corpus/integer_edge_values.py', 'components/qefiles/upstream/qe_arena/corpus/ip_allowlist_match.py', 'components/qefiles/upstream/qe_arena/corpus/is_collection_endpoint.py', 'components/qefiles/upstream/qe_arena/corpus/latency_percentiles.py', 'components/qefiles/upstream/qe_arena/corpus/login_rate_limit.py', 'components/qefiles/upstream/qe_arena/corpus/loudness_gain.py', 'components/qefiles/upstream/qe_arena/corpus/mail_merge.py', 'components/qefiles/upstream/qe_arena/corpus/maintainability_index.py', 'components/qefiles/upstream/qe_arena/corpus/maintenance_status.py', 'components/qefiles/upstream/qe_arena/corpus/merge_intervals.py', 'components/qefiles/upstream/qe_arena/corpus/mod_inverse.py', 'components/qefiles/upstream/qe_arena/corpus/mod_pow.py', 'components/qefiles/upstream/qe_arena/corpus/networkdays.py', 'components/qefiles/upstream/qe_arena/corpus/normalize_rational.py', 'components/qefiles/upstream/qe_arena/corpus/parse_cron_field.py', 'components/qefiles/upstream/qe_arena/corpus/parse_max_age.py', 'components/qefiles/upstream/qe_arena/corpus/parse_signature_header.py', 'components/qefiles/upstream/qe_arena/corpus/password_violations.py', 'components/qefiles/upstream/qe_arena/corpus/pattern_effectiveness.py', 'components/qefiles/upstream/qe_arena/corpus/pressure_check.py', 'components/qefiles/upstream/qe_arena/corpus/quality_gain_reward.py', 'components/qefiles/upstream/qe_arena/corpus/quality_grade.py', 'components/qefiles/upstream/qe_arena/corpus/reconcile_invoices.py', 'components/qefiles/upstream/qe_arena/corpus/round_sig_figs.py', 'components/qefiles/upstream/qe_arena/corpus/safe_join.py', 'components/qefiles/upstream/qe_arena/corpus/sample_variance.py', 'components/qefiles/upstream/qe_arena/corpus/select_bitrate_rung.py', 'components/qefiles/upstream/qe_arena/corpus/shift_cue.py', 'components/qefiles/upstream/qe_arena/corpus/slew_limit.py', 'components/qefiles/upstream/qe_arena/corpus/spec_readiness_score.py', 'components/qefiles/upstream/qe_arena/corpus/split_generic_params.py', 'components/qefiles/upstream/qe_arena/corpus/streamable_len.py', 'components/qefiles/upstream/qe_arena/corpus/string_length_boundaries.py', 'components/qefiles/upstream/qe_arena/corpus/threshold_compliance.py', 'components/qefiles/upstream/qe_arena/corpus/token_claims_check.py', 'components/qefiles/upstream/qe_arena/corpus/token_jaccard.py', 'components/qefiles/upstream/qe_arena/corpus/trace_sampling_decision.py', 'components/qefiles/upstream/qe_arena/corpus/truncate_to_token_budget.py', 'components/qefiles/upstream/qe_arena/corpus/uncovered_line_ranges.py', 'components/qefiles/upstream/qe_arena/corpus/validate_csv_row.py', 'components/qefiles/upstream/qe_arena/corpus/vuln_priority_bucket.py', 'components/qefiles/upstream/qe_arena/corpus/wilson_interval.py', 'components/qefiles/upstream/qe_arena/domains.py', 'components/qefiles/upstream/qe_arena/edit_budget.py', 'components/qefiles/upstream/qe_arena/environment.py', 'components/qefiles/upstream/qe_arena/flaky.py', 'components/qefiles/upstream/qe_arena/flaky_gen.py', 'components/qefiles/upstream/qe_arena/flaky_templates.py', 'components/qefiles/upstream/qe_arena/guest_harness.py', 'components/qefiles/upstream/qe_arena/keys/manifest_ed25519.pub', 'components/qefiles/upstream/qe_arena/manifest.py', 'components/qefiles/upstream/qe_arena/mutation.py', 'components/qefiles/upstream/qe_arena/runtime_pins.json', 'components/qefiles/upstream/qe_arena/sandbox.py', 'components/qefiles/upstream/qe_arena/shell.py', 'components/qefiles/upstream/qe_arena/skills/__init__.py', 'components/qefiles/upstream/qe_arena/skills/_common.py', 'components/qefiles/upstream/qe_arena/skills/desk_authlog.py', 'components/qefiles/upstream/qe_arena/skills/desk_finance.py', 'components/qefiles/upstream/qe_arena/skills/desk_ghg.py', 'components/qefiles/upstream/qe_arena/skills/desk_industrial.py', 'components/qefiles/upstream/qe_arena/skills/desk_math.py', 'components/qefiles/upstream/qe_arena/skills/desk_office.py', 'components/qefiles/upstream/qe_arena/skills/desk_science.py', 'components/qefiles/upstream/qe_arena/skills/desk_security.py', 'components/qefiles/upstream/qe_arena/skills/formal.py', 'components/qefiles/upstream/qe_arena/skills/media.py', 'components/qefiles/upstream/qe_arena/skills/science.py', 'components/qefiles/upstream/qe_arena/skills/subtitles.py', 'components/qefiles/upstream/qe_arena/tasks.py', 'components/qefiles/upstream/qe_arena/terminal_app.py', 'components/qefiles/upstream/qe_arena/terminal_env.py', 'components/qefiles/upstream/qe_arena/wasi_fs.py', 'components/qefiles/upstream/qe_arena/wasi_worker.py', 'schema.json', 'tasks.jsonl', 'validate_dataset.py')
TASK_IDS = ('qe_skill-media-d2', 'qe_skill-math-d4')
SECRET = re.compile(rb'(?<![A-Za-z0-9])(?:hf_[A-Za-z0-9]{25,}|ghp_[A-Za-z0-9]{25,}|github_pat_[A-Za-z0-9_]{25,}|sk-or-v1-[A-Za-z0-9]{20,}|-----BEGIN (?:RSA |EC |OPENSSH )?PRIVATE KEY-----)')
def require(condition, message):
if not condition:
raise ValueError(message)
def sha(path):
return hashlib.sha256(Path(path).read_bytes()).hexdigest()
def member(root, relative):
require(type(relative) is str, 'Invalid inventory path')
part = PurePosixPath(relative)
require(not part.is_absolute() and '..' not in part.parts, 'Unsafe inventory path')
path = Path(root) / relative
require(path.resolve().is_relative_to(Path(root).resolve()) and path.is_file()
and not path.is_symlink(), 'Missing or unsafe member: ' + relative)
return path
def validate(root):
root = Path(root)
require(root.is_dir() and not root.is_symlink(), 'Invalid dataset directory')
require(all(not p.is_symlink() for p in root.rglob('*')), 'Dataset contains a symlink')
files = json.loads(member(root, 'MANIFEST.json').read_text())['files']
require(set(files) == set(EXPECTED_FILES), 'Noncanonical dataset inventory')
actual = {p.relative_to(root).as_posix() for p in root.rglob('*') if p.is_file()
and p.relative_to(root).as_posix() not in {'MANIFEST.json', 'SHA256SUMS'}}
require(actual == set(files), 'Dataset inventory mismatch')
for relative, record in files.items():
path = member(root, relative)
require(path.stat().st_size == record['bytes'] and sha(path) == record['sha256'],
'Dataset member mismatch: ' + relative)
require(path.suffix not in {'.pyc', '.pem', '.key'} and path.name not in {'.env', 'token'},
'Forbidden file type: ' + relative)
require(not SECRET.search(path.read_bytes()), 'Credential-shaped content: ' + relative)
sums = ''.join(f"{record['sha256']} {name}\n" for name, record in files.items())
require(member(root, 'SHA256SUMS').read_text() == sums, 'Checksum listing mismatch')
require(sha(member(root, 'schema.json')) == SCHEMA_SHA256, 'Native schema changed')
require(sha(member(root, 'tasks.jsonl')) == TASK_ROWS_SHA256, 'Task metadata changed')
manifest_path = member(root, 'components/qefiles/SOURCE_MANIFEST.json')
require(sha(manifest_path) == SOURCE_MANIFEST_SHA256, 'Source record changed')
source = json.loads(manifest_path.read_text())
require(tuple(source['selected_tasks']) == TASK_IDS, 'Task order changed')
for rel, digest in source['original_files'].items():
require(sha(member(root, 'components/qefiles/upstream/' + rel)) == digest,
'Original source changed: ' + rel)
source_root = root / 'components/qefiles/upstream'
require({p.relative_to(source_root).as_posix() for p in source_root.rglob('*') if p.is_file()}
== set(source['original_files']), 'Unexpected original source inventory')
rows = [json.loads(line) for line in (root / 'tasks.jsonl').read_text().splitlines()]
require(tuple(row['task_id'] for row in rows) == TASK_IDS, 'Task registry changed')
for row in rows:
require(row['component'] == 'qefiles' and row['source_revision'] == source['revision'],
'Task source mismatch')
for definition in row['original_task_definitions']:
require(sha(member(root, definition['path'])) == definition['sha256'],
'Task definition mismatch')
lock = json.loads(member(root, 'SOURCE_LOCK.json').read_text())
expected_lock = {'components': {'qefiles': {
'repository': 'proffesor-for-testing/openenv-qe-arena',
'revision': 'a570c251784f7860f7c2e18e8dce5132fe25cedc',
'license': 'MIT', 'source_file': 'SOURCE_MANIFEST.json',
'record_sha256': SOURCE_MANIFEST_SHA256}}}
require(lock == expected_lock, 'Source lock identity, path, license or hash mismatch')
return {'passed': True, 'scope': 'offline_dataset_inventory_and_provenance',
'tasks': len(rows), 'components': 1, 'original_files': len(source['original_files']),
'files': len(files)}
if __name__ == '__main__':
print(json.dumps(validate(Path(__file__).resolve().parent)))