Skip to content

Multi-tier framework for evaluating AI agent skills and plugins with quality gates, security scanning, harness compatibility checks, semantic overlap detection, synthetic evaluation dataset generation, and live agent evaluation that measures how skills affect agent behavior. Ships reusable CI templates that bring every check to any repository.

7.8 KB
Raw Blame
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

"""Path-safety and host-path redaction helpers for Tier 2 commands."""

from __future__ import annotations

import os
import stat
from collections.abc import Collection
from contextlib import suppress
from pathlib import Path, PurePosixPath
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from skillevaluator.models.result import ValidationResult


def safe_path_label(path: Path) -> str:
    """Return a useful path label without exposing its host directory."""
    return path.name or "."


def is_link_or_reparse(path: Path) -> bool:
    """Return whether *path* itself is a symlink, junction, or reparse point."""
    try:
        metadata = path.lstat()
    except OSError:
        return False

    reparse_flag = getattr(stat, "FILE_ATTRIBUTE_REPARSE_POINT", 0x400)
    file_attributes = getattr(metadata, "st_file_attributes", 0)
    is_junction = getattr(path, "is_junction", None)
    return bool(
        stat.S_ISLNK(metadata.st_mode) or file_attributes & reparse_flag or (callable(is_junction) and is_junction())
    )


def is_contained_compatibility_alias(path: Path, *, sibling_names: Collection[str]) -> bool:
    """Recognize only ``CLAUDE.md -> AGENTS.md`` with a regular sibling target.

    The alias is validated but never followed. Callers can skip it and process
    the independently discovered ``AGENTS.md`` target once. ``sibling_names``
    must come from the same traversal snapshot that produced ``path``.
    """
    if path.name != "CLAUDE.md" or "AGENTS.md" not in sibling_names:
        return False
    try:
        metadata = path.lstat()
        target_text = os.readlink(path)  # noqa: PTH115 - the raw target must match exactly
        target_metadata = path.with_name("AGENTS.md").lstat()
    except OSError:
        return False

    reparse_flag = getattr(stat, "FILE_ATTRIBUTE_REPARSE_POINT", 0x400)
    target_attributes = getattr(target_metadata, "st_file_attributes", 0)
    return bool(
        stat.S_ISLNK(metadata.st_mode)
        and getattr(metadata, "st_nlink", 1) == 1
        and target_text == "AGENTS.md"
        and stat.S_ISREG(target_metadata.st_mode)
        and getattr(target_metadata, "st_nlink", 1) == 1
        and not target_attributes & reparse_flag
    )


def contained_directory_alias_target(path: Path, relative: Path) -> str | None:
    """Return the in-root target that an unselected directory alias names, or ``None``.

    ``relative`` is the alias path below the scan root. Only the raw link text
    is read; the alias is never followed. The text must be relative, may climb
    only through the alias's own (already traversed) parent directories, and
    must then name a path below the root that is neither the root nor an
    ancestor of the alias. The returned POSIX-style relative path is an identity
    claim only: callers skip the alias and accept it only when the same walk
    independently enumerates that path as a regular directory.
    """
    try:
        metadata = path.lstat()
        target_text = os.readlink(path)  # noqa: PTH115 - validate the raw link text without following it
    except (OSError, ValueError):
        return None
    if getattr(metadata, "st_nlink", 1) != 1 or not target_text or "\x00" in target_text:
        return None
    target = Path(target_text)
    if target.anchor:
        return None

    parts = target.parts
    ascent = 0
    while ascent < len(parts) and parts[ascent] == "..":
        ascent += 1
    descent = parts[ascent:]
    base = relative.parent.parts
    # A ``..`` after a named component could climb out of a linked directory,
    # making the lexical claim differ from the physical target.
    if ascent > len(base) or not descent or ".." in descent:
        return None
    normalized = PurePosixPath(*base[: len(base) - ascent], *descent)
    if PurePosixPath(relative.as_posix()).is_relative_to(normalized):
        return None
    return normalized.as_posix()


def paths_refer_to_same_location(left: Path, right: Path) -> bool:
    """Compare output paths after resolving aliases, symlinks, and hard links."""
    with suppress(OSError):
        if left.exists() and right.exists() and left.samefile(right):
            return True

    def normalized(path: Path) -> str:
        expanded = path.expanduser()
        try:
            resolved = expanded.resolve(strict=False)
        except (OSError, RuntimeError):
            resolved = expanded.absolute()
        return os.path.normcase(os.path.normpath(str(resolved)))

    return normalized(left) == normalized(right)


def sanitize_path_text(message: str, paths: list[Path | None] | tuple[Path | None, ...]) -> str:
    """Replace absolute forms of supplied paths with stable, relative labels."""
    replacements: dict[str, str] = {}
    for path in paths:
        if path is None:
            continue
        label = safe_path_label(path)
        candidates = {str(path)}
        with suppress(OSError, RuntimeError):
            candidates.add(str(path.expanduser().absolute()))
        with suppress(OSError, RuntimeError):
            candidates.add(str(path.expanduser().resolve(strict=False)))
        for candidate in candidates:
            if candidate and Path(candidate).is_absolute():
                replacements[candidate] = label

    for candidate in sorted(replacements, key=len, reverse=True):
        message = message.replace(candidate, replacements[candidate])
    return message


def _sanitize_value(value, paths: tuple[Path | None, ...]):
    if isinstance(value, str):
        return sanitize_path_text(value, paths)
    if isinstance(value, Path):
        return sanitize_path_text(str(value), paths)
    if isinstance(value, dict):
        return {_sanitize_value(key, paths): _sanitize_value(item, paths) for key, item in value.items()}
    if isinstance(value, list):
        return [_sanitize_value(item, paths) for item in value]
    if isinstance(value, tuple):
        return tuple(_sanitize_value(item, paths) for item in value)
    return value


def sanitize_tier2_results(results: list[ValidationResult], *paths: Path | None) -> list[ValidationResult]:
    """Remove supplied host paths from every user-visible result field in place."""
    sanitized_paths = tuple(paths)
    for result in results:
        result.validator_name = sanitize_path_text(result.validator_name, sanitized_paths)
        result.validator_description = sanitize_path_text(result.validator_description, sanitized_paths)
        result.errors[:] = [sanitize_path_text(value, sanitized_paths) for value in result.errors]
        result.warnings[:] = [sanitize_path_text(value, sanitized_paths) for value in result.warnings]
        result.messages[:] = [sanitize_path_text(value, sanitized_paths) for value in result.messages]
        result.metadata = _sanitize_value(result.metadata, sanitized_paths)

        for detail in result.success_details:
            detail.message = sanitize_path_text(detail.message, sanitized_paths)
            detail.metadata = _sanitize_value(detail.metadata, sanitized_paths)

        for finding in result.findings:
            finding.message = sanitize_path_text(finding.message, sanitized_paths)
            finding.file_path = sanitize_path_text(finding.file_path, sanitized_paths)
            if finding.line_content is not None:
                finding.line_content = sanitize_path_text(finding.line_content, sanitized_paths)
            if finding.suggestion is not None:
                finding.suggestion = sanitize_path_text(finding.suggestion, sanitized_paths)
            finding.metadata = _sanitize_value(finding.metadata, sanitized_paths)
    return results


__all__ = [
    "contained_directory_alias_target",
    "is_contained_compatibility_alias",
    "is_link_or_reparse",
    "paths_refer_to_same_location",
    "safe_path_label",
    "sanitize_path_text",
    "sanitize_tier2_results",
]