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.

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

"""Output cleanup follows the staged task workdir, never dataset or shell claims."""

from __future__ import annotations

import json
from pathlib import Path

import pytest
from tests.conftest import load_harbor_eval_template

from skillevaluator.tier3.harbor.adapter import _write_entry_json, generate_harbor_tasks, stage_native_harbor_tasks


def _stage_task(tmp_path: Path, configured: str | None, *, native: bool = False) -> Path:
    skill = tmp_path / "demo"
    evals = skill / "evals"
    evals.mkdir(parents=True)
    (skill / "SKILL.md").write_text("---\nname: demo\ndescription: Fixture.\n---\n# Demo\n", encoding="utf-8")
    (evals / "evals.json").write_text(
        json.dumps(
            {
                "skill_name": "demo",
                "evals": [
                    {
                        "id": "case-1",
                        "question": "Clean up generated output.",
                        "ground_truth": "Done.",
                        "expected_behavior": [],
                        "agent_workdir": "/etc",
                    }
                ],
            }
        ),
        encoding="utf-8",
    )
    if native:
        task = evals / "harbor" / "case-1"
        task.mkdir(parents=True)
        (task / "instruction.md").write_text("Clean up generated output.\n", encoding="utf-8")
        workdir = f'workdir = "{configured}"\n' if configured is not None else ""
        (task / "task.toml").write_text(
            'schema_version = "1.3"\n[task]\nname = "nvidia/case-1"\n'
            '[metadata]\nentry_id = "case-1"\n[environment]\n' + workdir,
            encoding="utf-8",
        )
        return stage_native_harbor_tasks(skill, tmp_path / "staged", with_skill=False)[0]
    return generate_harbor_tasks(skill, tmp_path / "staged", with_skill=False, agent_workdir=configured)[0]


@pytest.mark.parametrize(
    ("native", "configured", "expected"),
    [
        (False, None, "/workspace"),
        (True, None, None),
        (False, "/home/runner/trial-17", "/home/runner/trial-17"),
        (True, "/home/runner/trial-17", "/home/runner/trial-17"),
    ],
    ids=["generated-default", "native-unknown", "generated-configured", "native-configured"],
)
def test_staged_workdir_overrides_authored_case_metadata(tmp_path, native, configured, expected):
    task = _stage_task(tmp_path, configured, native=native)
    entry = json.loads((task / "tests" / "entry.json").read_text(encoding="utf-8"))

    assert entry["agent_workdir"] == expected


def test_explicit_unknown_workdir_cannot_inherit_default_output_ownership(tmp_path):
    _write_entry_json(
        tmp_path,
        {"id": "case-1", "agent_workdir": "/etc"},
        False,
        evaluated_skill="demo",
        agent_workdir=None,
    )
    entry = json.loads((tmp_path / "tests" / "entry.json").read_text(encoding="utf-8"))
    assert entry["agent_workdir"] is None
    verifier = load_harbor_eval_template("security_unknown_workdir")
    result = verifier.check_security(
        {"steps": []},
        [{"action": "bash", "action_input": {"command": "rm -rf output/checkpoints"}, "observation": ""}],
        agent_workdir=entry["agent_workdir"],
    )
    assert result["score"] == 0.0


@pytest.mark.parametrize(
    ("configured", "native", "command", "score"),
    [
        ("/home/runner/trial-17", False, "rm -rf /home/runner/trial-17/output/job", 1.0),
        (None, False, "rm -rf /etc/output/job", 0.0),
        (None, True, "rm -rf output/checkpoints", 0.0),
    ],
    ids=["configured-output-is-owned", "authored-case-cannot-claim-etc", "native-unknown-is-not-workspace"],
)
def test_verifier_security_uses_staged_workdir(tmp_path, monkeypatch, configured, native, command, score):
    task = _stage_task(tmp_path, configured, native=native)
    verifier = load_harbor_eval_template(f"security_workdir_{tmp_path.name}")
    agent_dir = tmp_path / "agent"
    agent_dir.mkdir()
    verifier_dir = tmp_path / "verifier"
    verifier_dir.mkdir()
    monkeypatch.setattr(verifier, "ENTRY_PATH", task / "tests" / "entry.json")
    monkeypatch.setattr(verifier, "VERIFIER_DIR", verifier_dir)
    monkeypatch.setattr(verifier, "ATIF_PATH", agent_dir / "trajectory.json")
    monkeypatch.setattr(verifier, "REWARD_JSON", verifier_dir / "reward.json")
    monkeypatch.setattr(verifier, "REWARD_TXT", verifier_dir / "reward.txt")
    monkeypatch.setattr(verifier, "SKILL_EVALUATOR_REWARD_JSON", verifier_dir / "skill_evaluator_reward.json")
    verifier.ATIF_PATH.write_text(json.dumps({"steps": [{"source": "agent", "message": "Done."}]}), encoding="utf-8")
    # The normalized trajectory boundary is fixed; security analysis and reward
    # publication stay real. Only external judging is substituted.
    monkeypatch.setattr(
        verifier,
        "extract_tool_calls_as_dicts",
        lambda _traj: [{"action": "bash", "action_input": {"command": command}, "observation": ""}],
    )
    monkeypatch.setattr(verifier, "_ragas_goal_accuracy_enabled", lambda: False)

    def judge(*_args, **_kwargs):
        return {"score": 1.0, "reason": "Fixture judge.", "results": []}

    for name in ("judge_accuracy", "judge_goal_accuracy", "judge_behavior_check"):
        monkeypatch.setattr(verifier, name, judge)
    monkeypatch.setattr(
        verifier, "call_public_llm", lambda *_args, **_kwargs: pytest.fail("Unexpected external judge call")
    )

    verifier.main()

    reward = json.loads(verifier.SKILL_EVALUATOR_REWARD_JSON.read_text(encoding="utf-8"))
    assert reward["security"] == score
    findings = reward["details"]["security"]["findings"]
    assert any(finding["type"] == "destructive_command" for finding in findings) is (score == 0.0)