"""Per-attempt record schema (machine-validated) and append-only storage.""" from __future__ import annotations import json, uuid from pathlib import Path from jsonschema import Draft202012Validator from .util import ROOT, utc_now, append_jsonl, sha256_json, git_commit STATUSES = ["success", "invalid_output", "timeout", "oom", "incompatible", "implementation_error", "unavailable", "budget_exhausted"] SCHEMA = { "$schema": "https://json-schema.org/draft/2020-12/schema", "type": "object", "required": ["schema_version", "study_id", "experiment_id", "trial_id", "attempt_id", "run_group_id", "protocol_hash", "config_hash", "git_commit", "timestamp_utc", "task_family", "document_id", "document_hash", "question_id", "split", "cluster_id", "sender_model_revision", "receiver_model_revision", "tokenizer_hashes", "backend_versions", "hardware_id", "device_state", "weight_precision", "method_id", "implementation_fidelity", "compression_config", "question_access", "topology", "handoff_index", "reuse_count", "cache_hit", "cache_compatibility_hash", "random_seed", "source_tokens_sender", "source_tokens_receiver", "query_tokens", "message_tokens", "message_utf8_bytes", "cache_tensor_bytes", "serialized_payload_bytes", "side_information_bytes", "fetched_bytes", "transport_bytes_if_measured", "timing_spans", "total_wall_ms", "ttft_ms", "input_usage", "output_usage", "provider_cached_usage", "process_memory", "backend_memory", "peak_prefill_memory", "peak_decode_memory", "mapper_memory", "cost_with_source", "prediction_artifact", "evidence_artifact", "reference_key_id", "task_scores", "status", "error_code", "retry_of", "log_artifact", "payload_hash", "output_hash"], "properties": { "schema_version": {"const": "wp30-attempt-1"}, "status": {"enum": STATUSES}, "question_access": {"enum": ["blind", "aware", "none"]}, "implementation_fidelity": {"enum": ["official", "faithful_reimplementation", "adapted", "inspired", "custom", "n/a"]}, "timing_spans": {"type": "object"}, "task_scores": {"type": ["object", "null"]}, "handoff_index": {"type": "integer"}, "reuse_count": {"type": "integer"}, }, } _validator = Draft202012Validator(SCHEMA) def new_record(**kw) -> dict: rec = {k: None for k in SCHEMA["required"]} rec.update({"schema_version": "wp30-attempt-1", "study_id": "WP30", "attempt_id": uuid.uuid4().hex[:12], "timestamp_utc": utc_now(), "git_commit": git_commit(), "handoff_index": 0, "reuse_count": 1, "cache_hit": False, "timing_spans": {}, "fetched_bytes": 0, "side_information_bytes": 0, "transport_bytes_if_measured": None, "retry_of": None}) rec.update(kw) return rec def validate_record(rec: dict) -> list[str]: return [e.message for e in _validator.iter_errors(rec)] def write_record(rec: dict, path: str | Path) -> None: errs = validate_record(rec) if errs: raise ValueError("invalid record: " + "; ".join(errs[:5])) append_jsonl(path, rec) def trial_key(experiment_id: str, document_id: str, question_id: str, method_id: str, compression_config: dict, sender: str, receiver: str, question_access: str, protocol_hash: str, seed: int, topology: str = "single", handoff_index: int = 0, extra: dict | None = None) -> str: return sha256_json({"e": experiment_id, "d": document_id, "q": question_id, "m": method_id, "c": compression_config, "s": sender, "r": receiver, "a": question_access, "p": protocol_hash, "seed": seed, "t": topology, "h": handoff_index, "x": extra or {}})[:24]