"""Shared artifact serialization and path guards for PChome mapping."""

from __future__ import annotations

import json
from pathlib import Path
from typing import Any


def _canonical_retry_exception_artifact_bytes(payload: dict[str, Any]) -> bytes:
    return (
        json.dumps(payload, ensure_ascii=False, sort_keys=True, indent=2, default=str) + "\n"
    ).encode("utf-8")


def _resolve_retry_exception_artifact_path(root: Path, relative_path: str) -> Path:
    relative = Path(relative_path)
    if relative.is_absolute() or ".." in relative.parts:
        raise ValueError(f"unsafe artifact path: {relative_path}")
    return root / relative


__all__ = (
    "_canonical_retry_exception_artifact_bytes",
    "_resolve_retry_exception_artifact_path",
)
