from pathlib import Path


def existing_work_directories(root: str | Path) -> set[Path]:
    """Return all calcjob working directories under 'root' that exist on disk.

    Notes
    -----
    As Paths are returned without any information about what computer
    the path refers to, this function is only useful in environments
    where the Paths are globally unique.

    Examples
    --------
    >>> work_directories("/path/to/my-user/.aiida_run")
    {PosixPath('/path/to/my-user/.aiida_run/00/24/ab.c2-899c-4106-8c8e-74638dbdd71c')}
    """
    root = Path(root).absolute()
    # aiiDA shards working directory paths like '/path/to/.aiida_run/ab/cd/1234-...'
    # so we add glob 3 subdirectories onto the root to get to the working directories.
    return {Path(p) for p in root.glob("*/*/*")}