summaryrefslogtreecommitdiff
path: root/evals/drill/assertions.py
blob: 6d061b95173b52c913317f4416fdbe7756b0ec3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""Post-session deterministic assertions for drill scenarios."""

from __future__ import annotations

import os
import subprocess
from dataclasses import dataclass
from pathlib import Path

from drill.verifier import CriterionResult


@dataclass
class AssertionResult:
    command: str
    passed: bool
    exit_code: int
    stdout: str
    stderr: str

    def to_criterion_result(self) -> CriterionResult:
        evidence = f"exit code {self.exit_code}"
        if self.stdout:
            evidence += f"\nstdout: {self.stdout}"
        if self.stderr:
            evidence += f"\nstderr: {self.stderr}"
        return CriterionResult(
            criterion=f"[assertion] {self.command}",
            verdict="pass" if self.passed else "fail",
            evidence=evidence,
            rationale="Deterministic assertion " + ("passed" if self.passed else "failed"),
            source="assertion",
        )


def run_verify_assertions(
    assertions: list[str],
    results_dir: Path,
    workdir: Path,
    *,
    timeout_seconds: int = 10,
) -> list[AssertionResult]:
    bin_dir = Path(__file__).parent.parent / "bin"
    env = {
        **os.environ,
        "DRILL_WORKDIR": str(workdir),
        "PATH": f"{bin_dir}:{os.environ.get('PATH', '')}",
    }
    results: list[AssertionResult] = []
    for cmd in assertions:
        try:
            proc = subprocess.run(
                ["bash", "-c", cmd],
                cwd=results_dir,
                capture_output=True,
                text=True,
                env=env,
                timeout=timeout_seconds,
            )
            results.append(
                AssertionResult(
                    command=cmd,
                    passed=proc.returncode == 0,
                    exit_code=proc.returncode,
                    stdout=proc.stdout.strip(),
                    stderr=proc.stderr.strip(),
                )
            )
        except subprocess.TimeoutExpired:
            results.append(
                AssertionResult(
                    command=cmd,
                    passed=False,
                    exit_code=124,
                    stdout="",
                    stderr=f"Timed out after {timeout_seconds}s",
                )
            )
        except Exception as e:
            results.append(
                AssertionResult(
                    command=cmd,
                    passed=False,
                    exit_code=-1,
                    stdout="",
                    stderr=str(e),
                )
            )
    return results