"""Audited third-party microphones, current HFD motion construction. No imports from the donor repository. Its frozen target *timing* is checked, not its old poses: listen-hold and root40 remain owned by the current pipeline. Original sessions are retained once; approved third-party sessions are appended once and shuffled/refilled by the normal sampler (no extra task loss weight). """ from __future__ import annotations import copy import hashlib import json import random import wave from collections import Counter from functools import lru_cache from pathlib import Path import numpy as np from torch.utils.data import Dataset PROFILES = {"single_short", "multi_single", "multi_babble", "mixed_short", "continuous", "same_voice_aside"} def file_hash(path): h = hashlib.sha256() with Path(path).open("rb") as f: for chunk in iter(lambda: f.read(1024 * 1024), b""): h.update(chunk) return h.hexdigest() def contained(root, relative): path = (root / relative).resolve() if not path.is_relative_to(root): raise ValueError(f"Third-party path escapes production root: {relative}") return path @lru_cache(maxsize=1024) def verify_primitive(directory, expected_hash, stamps): """Hash each loaded primitive once per file revision, not every window.""" digest = hashlib.sha256() for filename in ("joint_pos.csv", "body_pos.csv", "body_quat.csv"): digest.update(filename.encode() + b"\0") digest.update(file_hash(Path(directory) / filename).encode() + b"\0") if digest.hexdigest() != expected_hash: raise ValueError(f"Third-party primitive motion changed: {directory}") def read_recipes(root, selection_path, split): """Compact metadata only; never put audio or augmented poses in the index.""" from psi.data.seed.startup_index_cache import cached_index root = Path(root).resolve() selection = json.loads(Path(selection_path).read_text()) expected = "train" if split == "train" else "validation" expected_format = ('hfd_third_party_training_selection_v1' if split == 'train' else 'hfd_third_party_scale_validation_v1') if (selection.get('format') != expected_format or selection.get('scaleup') is not True or selection.get('baseline_semantics_version') != 24): raise ValueError('Expected approved full-scale third-party selection') if selection.get("split") != expected: raise ValueError("Third-party selection split mismatch") if Path(selection["production_root"]).resolve() != root: raise ValueError("Third-party selection production root mismatch") manifest = root / "manifests/session_recipes.jsonl" if file_hash(manifest) != selection["recipe_manifest_sha256"]: raise ValueError("Third-party recipe hash mismatch") if file_hash(root / "manifests/source_contract.json") != selection["source_contract_sha256"]: raise ValueError("Third-party source contract hash mismatch") ids = selection["session_ids"] if not ids or len(set(ids)) != len(ids): raise ValueError("Empty/duplicate third-party selection") def build(): wanted = set(ids) found = {} with manifest.open() as f: for line in f: row = json.loads(line) sid = row["session_id"] if sid not in wanted: continue if sid in found or row["split"] != expected: raise ValueError(f"Duplicate or wrong-split third-party session: {sid}") if row["session_type"] not in PROFILES or row["interaction_mode"] != "turn-taking": raise ValueError(f"Unsupported third-party recipe: {sid}") mix = row["mix_metadata"] if mix.get("status") != "calibrated" or not mix.get("background_applied"): raise ValueError(f"Unaudited background: {sid}") if not np.isfinite(mix.get('measured_background_sir_db', np.nan)): raise ValueError(f"Unmeasurable third-party background: {sid}") for event in row['interference_events']: if event.get('type') == 'reference_clone' or event.get('voice_relation') == 'reference_clone': if event.get('boundary_unmeasurable') or event.get('active_sample_count',0) < 160: raise ValueError(f"Rejected same-voice boundary: {sid}") if not 2 <= len(row["turns"]) <= 4: raise ValueError(f"Unexpected donor turn count: {sid}") if len({t["voice_name"] for t in row["turns"]}) != 1: raise ValueError(f"Speaker changes within third-party session: {sid}") for section in ("audio", "timeline"): if not contained(root, row[section]["path"]).is_file(): raise FileNotFoundError(row[section]["path"]) keys = ("session_id", "session_type", "split", "timeline_seed", "turns", "audio", "timeline", "base_background", "supervision_targets", "composition_variant_id", "composition_salt", "session_voice_name", "target_speech_events") compact = {k: row[k] for k in keys} compact["background_language"] = row["background_assignment"]["language"] compact["interference_intervals"] = [ {"start_seconds": e["start_sample"] / 16000., "end_seconds": e["end_sample"] / 16000.} for e in row["interference_events"]] found[sid] = compact if set(found) != wanted: raise ValueError("Selected third-party recipes missing") return tuple(found[sid] for sid in ids) return cached_index(f"third-party-{split}", selection, build, code_paths=(Path(__file__),)) def replace_microphone(item, recipe, root): """Fail closed on timing changes; no old-pose checks and no re-mixing.""" sid = recipe["session_id"] audio = recipe["audio"] if tuple(audio[k] for k in ("sample_rate", "channels", "sample_width_bytes", "window_samples", "stride_samples")) != (16000, 1, 2, 9600, 6400): raise ValueError("Invalid third-party waveform geometry") timeline = contained(root, recipe["timeline"]["path"]) if file_hash(timeline) != recipe["timeline"]["file_sha256"]: raise ValueError(f"Third-party timeline corrupted: {sid}") steps = item["steps"] with np.load(timeline, allow_pickle=False) as stored: for key in stored.files: if key == "states": # Intentionally regenerated by latest listen-hold construction. continue actual = np.asarray([s[key] for s in steps], dtype=stored[key].dtype) if actual.shape != stored[key].shape or not np.array_equal(actual, stored[key]): raise ValueError(f"Third-party timing/supervision drift: {sid}/{key}") if len(steps) != recipe["timeline"]["action_steps"]: raise ValueError(f"Third-party action timeline length drift: {sid}") for expected in recipe["supervision_targets"]: step = steps[expected["step_index"]] for key, value in expected.items(): if isinstance(value, float): equal = np.isclose(step[key], value, rtol=0, atol=1e-6) else: equal = step[key] == value if not equal: raise ValueError(f"Third-party sparse target drift: {sid}/{key}") actual_speech = item["microphone_speech_intervals"] if len(actual_speech) != len(recipe["target_speech_events"]): raise ValueError("Third-party target speech event count changed") for actual, expected in zip(actual_speech, recipe["target_speech_events"]): if any(abs(round(actual[k] * 16000) - expected[e]) > 1 for k, e in (("start_seconds", "start_sample"), ("end_seconds", "end_sample"))): raise ValueError(f"Third-party target speech shifted: {sid}") path = contained(root, audio["path"]) if file_hash(path) != audio["sha256"]: raise ValueError(f"Third-party audio corrupted: {sid}") with wave.open(str(path), "rb") as f: if (f.getframerate(), f.getnchannels(), f.getsampwidth()) != (16000, 1, 2): raise ValueError("Third-party WAV header mismatch") waveform = np.frombuffer(f.readframes(f.getnframes()), "