"""检查 reference compile 的 pass、source map、缓存标识和来源记录。

公开 compiler 会返回确定的 reference IR。本例查看 pass record、源码路径、
Target/Calibration hash 和 cache key，同时确认流程没有生成私有 payload 或生产调度。
"""

from __future__ import annotations

import json

from cascaqit import AHSProgram, AtomRegister, MockNeutralAtomTarget, Waveform
from cascaqit.compiler import CompilerPipeline


def main() -> None:
    """编译一个 global Analog 程序并检查 provenance 字段。"""
    target = MockNeutralAtomTarget.v0_1()
    program = (
        AHSProgram(
            AtomRegister.line(count=1, spacing=5.0),
            program_id="lesson.compiler.reference",
        )
        .drive(
            rabi=Waveform.linear(0.0, 0.8, duration=0.2),
            detuning=Waveform.constant(-0.2, duration=0.2),
            phase=0.0,
        )
        .measure()
    )
    validated = program.validate(target, shots=16)
    discretized, _ = validated.discretize(target)
    snapshot = target.to_snapshot(
        snapshot_id="snapshot.lesson.compiler",
        source="offline_lesson",
        status="available",
        effective_at="2026-07-20T00:00:00Z",
    )
    compiled = CompilerPipeline().compile(
        discretized.program_ir,
        target_snapshot=snapshot,
    )
    repeated = CompilerPipeline().compile(
        discretized.program_ir, target_snapshot=snapshot
    )
    cache_report = compiled.metadata["compile_cache_report"]
    contract = compiled.metadata["compiler_pipeline_contract"]
    pass_pipeline = compiled.metadata["compiler_pass_pipeline"]

    payload = {
        "track": "compiler_engineer",
        "level": "expert",
        "lesson": "reference_compile_provenance",
        "facts": {
            "pass_names": [item["pass_name"] for item in pass_pipeline["records"]],
            "source_map_keys": sorted(compiled.source_map),
            "same_cache_key": repeated.compile_cache_key == compiled.compile_cache_key,
            "same_compiled_hash": repeated.stable_hash() == compiled.stable_hash(),
            "target_hash_matches": compiled.target_snapshot_hash
            == snapshot.target_snapshot_hash,
            "cache_status": cache_report["status"],
            "filesystem_cache_used": cache_report["filesystem_cache_used"],
            "cache_key_present": len(compiled.compile_cache_key) == 64,
            "target_hash_present": bool(compiled.target_snapshot_hash),
            "compilation_scope": compiled.compilation_scope,
            "private_calibration_present": "calibration_snapshot_hash"
            in compiled.to_dict(),
            "hardware_payload_emitted": contract["hardware_payload_emitted"],
            "production_scheduling_performed": compiled.channel_schedule[
                "production_channel_allocation_performed"
            ],
        },
        "boundaries": {
            "hardware_execution": False,
            "cloud_execution": False,
            "network_accessed": False,
            "credentials_loaded": False,
        },
    }
    print(json.dumps(payload, sort_keys=True))


if __name__ == "__main__":
    main()
