from typing import Union
import pathlib
import hashlib


def _resolve_experiment_buffer(
    buffer_root: Union[str, pathlib.Path],
    exp_id: int,
    experiment_name: str,
    filter_string: str,
) -> pathlib.Path:
    """Resolve the buffer root for a given experiment and filter string."""
    filter_hash = hashlib.sha512(filter_string.encode(encoding="utf8")).hexdigest()[:8]
    buffer_root = pathlib.Path(buffer_root) / f"{exp_id}_{experiment_name}" / filter_hash
    return buffer_root