from __future__ import annotations

import json
import os
from pathlib import Path
import sys


def main() -> None:
    if os.name == "nt" or not Path("/proc").is_dir():
        result = {
            "status": "PASS",
            "reason": "Windows or non-/proc platform uses a different identity strategy",
            "pid": os.getpid(),
        }
        print(json.dumps(result, indent=2))
        return

    pid = os.getpid()
    proc_self_pid = int((Path("/proc") / "self" / "stat").read_text(encoding="utf-8").split()[0])
    proc_entry = Path("/proc") / str(pid) / "stat"
    consistent = proc_self_pid == pid and proc_entry.is_file()
    result = {
        "status": "PASS" if consistent else "UNSUPPORTED",
        "reason": (
            "Python PID and /proc PID namespace are consistent"
            if consistent
            else "Python PID is remapped while /proc exposes another namespace; detached-child cleanup evidence is not valid here"
        ),
        "pid": pid,
        "proc_self_pid": proc_self_pid,
        "proc_entry_visible": consistent,
    }
    print(json.dumps(result, indent=2))
    if not consistent:
        sys.exit(2)


if __name__ == "__main__":
    main()
