"""Collect and analyze pinned declarations; does not execute benchmark tasks."""
import argparse
from collections import Counter
import concurrent.futures
import hashlib
import json
from pathlib import Path
import tomllib
import urllib.request

ROOT = Path(__file__).resolve().parent
REPOSITORY = "harbor-framework/terminal-bench"
COMMIT = "4070d6cef56a7d20cb5dc98d5e10ada88a6b9440"
REGISTRY_URL = "https://huggingface.co/datasets/harborframework/terminal-bench/resolve/v4.1.0/registry.json"
REGISTRY_SHA256 = "8809dc185dcb431e3a92ea1eaeb8f82ec63aa98050051815d0d6005ce2c99262"
RESOURCE_FIELDS = ("cpus", "memory_mb", "storage_mb", "gpus", "gpu_types", "tpu", "build_timeout_sec")
LEGACY_FIELDS = ("memory", "storage", "cpu_count", "gpu_count")

def get(url, limit):
    request = urllib.request.Request(url, headers={"User-Agent": "Sapien1Research/1.0"})
    with urllib.request.urlopen(request, timeout=20) as response:
        raw = response.read(limit + 1)
    if len(raw) > limit:
        raise ValueError("Bounded retrieval exceeded")
    return raw

def resource_block(block):
    assert isinstance(block, dict)
    return {
        "explicit": {k: block[k] for k in RESOURCE_FIELDS if k in block},
        "omitted": [k for k in RESOURCE_FIELDS if k not in block],
        "legacy": {k: block[k] for k in LEGACY_FIELDS if k in block},
        "all_keys": sorted(block),
    }

def extra_blocks(value, path=""):
    found = []
    if isinstance(value, dict):
        for key, item in value.items():
            next_path = f"{path}.{key}" if path else key
            if key == "environment" and isinstance(item, dict):
                found.append({"path": next_path, **resource_block(item)})
            found.extend(extra_blocks(item, next_path))
    elif isinstance(value, list):
        for i, item in enumerate(value):
            found.extend(extra_blocks(item, f"{path}[{i}]"))
    return found

def read_task(task):
    path = task["path"] + "/task.toml"
    url = f"https://raw.githubusercontent.com/{REPOSITORY}/{COMMIT}/{path}"
    raw = get(url, 100_000)
    data = tomllib.loads(raw.decode())
    env = data.get("environment", {})
    verifier = data.get("verifier", {})
    agent = data.get("agent", {})
    steps = data.get("steps", [])
    assert isinstance(steps, list)
    return {
        "task": task["name"], "path": task["path"], "source_url": url,
        "source_sha256": hashlib.sha256(raw).hexdigest(), "source_bytes": len(raw),
        "schema_version": data.get("schema_version"),
        "legacy_version": data.get("version"), "task_version": data.get("task", {}).get("version"),
        "top_level_keys": sorted(data),
        "environment_present": "environment" in data,
        "environment": resource_block(env),
        "agent_timeout_present": "timeout_sec" in agent,
        "agent_timeout_sec": agent.get("timeout_sec"),
        "verifier_timeout_present": "timeout_sec" in verifier,
        "verifier_timeout_sec": verifier.get("timeout_sec"),
        "verifier_mode_present": "environment_mode" in verifier,
        "verifier_mode": verifier.get("environment_mode"),
        "verifier_environment_present": "environment" in verifier,
        "verifier_environment": resource_block(verifier["environment"]) if "environment" in verifier else None,
        "steps_present": "steps" in data, "step_count": len(steps),
        "step_keys": [sorted(step) for step in steps],
        "additional_environment_blocks": [b for b in extra_blocks(data) if b["path"] not in {"environment", "verifier.environment"}],
    }

def collect():
    raw = get(REGISTRY_URL, 300_000)
    assert hashlib.sha256(raw).hexdigest() == REGISTRY_SHA256, "Registry changed"
    registry = json.loads(raw)[0]
    assert registry["name"] == "terminal-bench" and registry["version"] == "4.1.0"
    tasks = sorted(registry["tasks"], key=lambda t: t["path"])
    assert len(tasks) == len({t["path"] for t in tasks}) == 66
    assert all(t["path"].startswith("tasks/") for t in tasks)
    with concurrent.futures.ThreadPoolExecutor(max_workers=3) as pool:
        rows = list(pool.map(read_task, tasks))
    output = {
        "repository": REPOSITORY, "release": "v4.1.0", "commit": COMMIT,
        "observed_on": "2026-11-17", "registry_url": REGISTRY_URL,
        "registry_sha256": hashlib.sha256(raw).hexdigest(), "cases": rows,
    }
    (ROOT / "census.json").write_text(json.dumps(output, indent=2) + "\n")
    return output

def summarize(data):
    rows = data["cases"]
    def label(value):
        if isinstance(value, (int, float)) and not isinstance(value, bool):
            return str(int(value)) if value == int(value) else str(value)
        if isinstance(value, str):
            return value
        return json.dumps(value, sort_keys=True)
    def distribution(getter):
        return dict(sorted(Counter(label(getter(r)) for r in rows).items()))
    hardware_fields = ("cpus", "memory_mb", "storage_mb", "gpus", "gpu_types", "tpu")
    hardware = [{k: r["environment"]["explicit"][k] for k in hardware_fields if k in r["environment"]["explicit"]} for r in rows]
    normalized_gpu = [{**h, "gpus": h.get("gpus", 0)} for h in hardware]
    baseline = {"cpus": 2, "memory_mb": 4096, "storage_mb": 10240, "gpus": 0}
    above = [r["task"] for r in rows if any(k in r["environment"]["explicit"] and r["environment"]["explicit"][k] > v for k, v in baseline.items())]
    summary = {
        "n": len(rows), "source_bytes": sum(r["source_bytes"] for r in rows),
        "schema_versions": distribution(lambda r: r["schema_version"]),
        "legacy_versions": distribution(lambda r: r["legacy_version"]),
        "hardware_declaration_profiles": len({json.dumps(h, sort_keys=True) for h in hardware}),
        "profiles_if_omitted_gpu_is_zero": len({json.dumps(h, sort_keys=True) for h in normalized_gpu}),
        "resources": {k: distribution(lambda r: r["environment"]["explicit"].get(k, "omitted")) for k in RESOURCE_FIELDS},
        "agent_timeout_sec": distribution(lambda r: r["agent_timeout_sec"] if r["agent_timeout_present"] else "omitted"),
        "verifier_timeout_sec": distribution(lambda r: r["verifier_timeout_sec"] if r["verifier_timeout_present"] else "omitted"),
        "verifier_modes": distribution(lambda r: r["verifier_mode"] if r["verifier_mode_present"] else "omitted"),
        "explicit_verifier_environment_blocks": sum(r["verifier_environment_present"] for r in rows),
        "tasks_with_steps": [r["task"] for r in rows if r["steps_present"]],
        "tasks_with_extra_environment_blocks": [r["task"] for r in rows if r["additional_environment_blocks"]],
        "tasks_with_legacy_resource_fields": [r["task"] for r in rows if r["environment"]["legacy"] or (r["verifier_environment"] and r["verifier_environment"]["legacy"])],
        "modal_value_preset": baseline,
        "above_modal_preset_any_field": {"count": len(above), "tasks": above},
        "above_modal_preset_by_field": {k: [r["task"] for r in rows if k in r["environment"]["explicit"] and r["environment"]["explicit"][k] > v] for k, v in baseline.items()},
        "interpretation": "Complete release declarations, not measured usage, enforcement, minimum needs, costs or performance. Omitted is distinct from zero.",
    }
    (ROOT / "census-results.json").write_text(json.dumps(summary, indent=2) + "\n")
    print(json.dumps(summary, indent=2))

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--collect", action="store_true", help="Fetch the fixed 66 metadata files; never execute them")
    args = parser.parse_args()
    data = collect() if args.collect else json.loads((ROOT / "census.json").read_text())
    summarize(data)
