summaryrefslogtreecommitdiff
path: root/evals/tests/test_sweep.py
blob: ea9250a498d31cac7b956461bd4d59a739fab90c (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
"""Tests for Sweep orchestrator."""

from __future__ import annotations

import json
from dataclasses import asdict
from pathlib import Path
from unittest.mock import patch

from drill.engine import Engine, RunResult
from drill.sweep import RunGroup, RunStatus, Sweep, write_run_group


class TestRunStatus:
    def test_pass_status(self) -> None:
        rs = RunStatus(index=0, status="pass", duration=10.5)
        assert rs.error is None
        assert rs.status == "pass"

    def test_error_status(self) -> None:
        rs = RunStatus(index=2, status="error", duration=1.2, error="tmux crashed")
        assert rs.error == "tmux crashed"

    def test_serializes_to_dict(self) -> None:
        rs = RunStatus(index=0, status="pass", duration=10.5)
        d = asdict(rs)
        assert d["index"] == 0
        assert d["status"] == "pass"
        assert d["duration"] == 10.5
        assert d["error"] is None


class TestRunGroup:
    def test_creates_with_defaults(self) -> None:
        rg = RunGroup(
            scenario="test",
            backend="claude",
            n=3,
            timestamp="2026-04-20T14-30-00",
            sweep_id="abc12345",
            runs=[],
        )
        assert rg.partial is False

    def test_partial_flag(self) -> None:
        rg = RunGroup(
            scenario="test",
            backend="claude",
            n=3,
            timestamp="2026-04-20T14-30-00",
            sweep_id="abc12345",
            runs=[RunStatus(index=0, status="pass", duration=10.0)],
            partial=True,
        )
        assert rg.partial is True
        assert len(rg.runs) == 1


class TestWriteRunGroup:
    def test_writes_json(self, tmp_path: Path) -> None:
        rg = RunGroup(
            scenario="test-scenario",
            backend="claude",
            n=2,
            timestamp="2026-04-20T14-30-00",
            sweep_id="abc12345",
            runs=[
                RunStatus(index=0, status="pass", duration=100.0),
                RunStatus(index=1, status="fail", duration=95.0),
            ],
        )
        write_run_group(rg, tmp_path)
        path = tmp_path / "run-group.json"
        assert path.exists()
        data = json.loads(path.read_text())
        assert data["scenario"] == "test-scenario"
        assert data["sweep_id"] == "abc12345"
        assert data["partial"] is False
        assert len(data["runs"]) == 2
        assert data["runs"][0]["status"] == "pass"
        assert data["runs"][1]["status"] == "fail"

    def test_writes_partial(self, tmp_path: Path) -> None:
        rg = RunGroup(
            scenario="test",
            backend="claude",
            n=5,
            timestamp="2026-04-20T14-30-00",
            sweep_id="abc12345",
            runs=[RunStatus(index=0, status="pass", duration=100.0)],
            partial=True,
        )
        write_run_group(rg, tmp_path)
        data = json.loads((tmp_path / "run-group.json").read_text())
        assert data["partial"] is True
        assert len(data["runs"]) == 1

    def test_omits_null_errors(self, tmp_path: Path) -> None:
        rg = RunGroup(
            scenario="test",
            backend="claude",
            n=1,
            timestamp="2026-04-20T14-30-00",
            sweep_id="abc12345",
            runs=[RunStatus(index=0, status="pass", duration=50.0)],
        )
        write_run_group(rg, tmp_path)
        data = json.loads((tmp_path / "run-group.json").read_text())
        run_data = data["runs"][0]
        assert "error" not in run_data


class TestSweepIntegration:
    def test_full_sweep_writes_run_group(self, tmp_path: Path) -> None:
        """Test that Sweep creates run dirs and writes run-group.json."""
        scenario_file = tmp_path / "scenarios" / "test.yaml"
        scenario_file.parent.mkdir(parents=True)
        scenario_file.write_text(
            "scenario: test-scenario\n"
            "description: test\n"
            "user_posture: naive\n"
            "setup: {}\n"
            "turns:\n  - intent: do the thing\n"
            "limits:\n  max_turns: 5\n"
            "verify:\n  criteria:\n    - thing was done\n"
        )

        backends_dir = tmp_path / "backends"
        backends_dir.mkdir()
        (backends_dir / "mock-backend.yaml").write_text(
            "name: mock-backend\n"
            "cli: echo\n"
            "args: []\n"
            "required_env: []\n"
            "hooks:\n  pre_run: []\n  post_run: []\n"
            "shutdown: /exit\n"
            "idle:\n  quiescence_seconds: 1\n  ready_pattern: '.'\n"
            "startup_timeout: 5\n"
            "terminal:\n  cols: 80\n  rows: 24\n"
            "session_logs: {}\n"
        )

        results_dir = tmp_path / "results"
        fixtures_dir = tmp_path / "fixtures"
        fixtures_dir.mkdir()

        fake_verdict = json.dumps(
            {
                "criteria": [
                    {
                        "criterion": "thing was done",
                        "verdict": "pass",
                        "evidence": "yes",
                        "rationale": "it was done",
                    }
                ],
                "observations": [],
                "summary": "ok",
            }
        )

        fake_result = RunResult(
            scenario="test-scenario",
            backend="mock-backend",
            timestamp="2026-04-20T14-30-00",
            session_log="log",
            filesystem_json='{"files": []}',
            tool_calls_jsonl='{"tool": "Bash"}',
            verdict_json=fake_verdict,
            meta={"actor_turns": 3},
        )

        sweep = Sweep(
            scenario_path=scenario_file,
            backend_names=["mock-backend"],
            backends_dir=backends_dir,
            fixtures_dir=fixtures_dir,
            results_dir=results_dir,
            n=3,
            sweep_id="test1234",
        )

        with patch.object(Engine, "run", return_value=fake_result):
            groups = sweep.run_all()

        assert len(groups) == 1
        group = groups[0]
        assert group.scenario == "test-scenario"
        assert len(group.runs) == 3
        assert all(r.status == "pass" for r in group.runs)
        assert group.partial is False

        # Verify run-group.json was written
        scenario_results = results_dir / "test-scenario" / "mock-backend"
        assert scenario_results.exists()
        group_dirs = list(scenario_results.iterdir())
        assert len(group_dirs) == 1
        rg_path = group_dirs[0] / "run-group.json"
        assert rg_path.exists()
        rg_data = json.loads(rg_path.read_text())
        assert rg_data["sweep_id"] == "test1234"
        assert len(rg_data["runs"]) == 3