Code / benchmarks/harness.py

benchmarks/harness.py 62 lines
#!/usr/bin/env python3
# =============================================================================
#  Project   : modelmap
#  File      : benchmarks/harness.py
#  Purpose   : Benchmark harness skeleton — runs experiments, embeds manifests
#  Author    : Simon-Pierre Boucher
#  Contact   : contact@spboucher.ai
#  Website   : https://modelmap.io
#  Created   : 2026-08-12
#  Modified  : 2026-08-12
#  Platform  : macOS / Apple Silicon (arm64)
#  License   : All rights reserved (research code)
# =============================================================================
"""Experiment harness (skeleton — finalized during Phase 5).

Contract established by the charter: every result JSON written under
results/<experiment_id>/<UTC timestamp>/ embeds the hardware manifest, the
git commit, the config, and the seed(s) used, so any map is reproducible from
commit + config + model hash + seed + manifest.
"""

from __future__ import annotations

import datetime
import json
import subprocess
from pathlib import Path

from hardware_manifest import manifest

ROOT = Path(__file__).resolve().parent.parent


def git_commit() -> str:
    try:
        return subprocess.run(
            ["git", "rev-parse", "HEAD"], cwd=ROOT, capture_output=True, text=True, check=True
        ).stdout.strip()
    except subprocess.CalledProcessError:
        return "unknown"


def result_dir(experiment_id: str) -> Path:
    ts = datetime.datetime.now(datetime.UTC).strftime("%Y%m%dT%H%M%SZ")
    out = ROOT / "results" / experiment_id / ts
    out.mkdir(parents=True)
    return out


def write_result(experiment_id: str, payload: dict, config: dict | None = None) -> Path:
    out = result_dir(experiment_id)
    doc = {
        "experiment": experiment_id,
        "commit": git_commit(),
        "config": config or {},
        "manifest": manifest(),
        **payload,
    }
    path = out / "results.json"
    path.write_text(json.dumps(doc, indent=2) + "\n")
    return path