File size: 3,312 Bytes
b199558
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Generate a negative control: a plausible but incomplete attempt per task.

Every task must score 0.0. This is the check that the verifier is measuring the
whole task rather than one easy assertion.

The controls are produced by mutating the oracle: the interesting failure mode
for an agent is not "did nothing" but "fixed the obvious half".

Run with:  python tools/make_controls.py
"""

import json
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parent.parent
DATASET = ROOT / "TerminalBench-DockerLess.jsonl"
OUTDIR = ROOT / "predictions"


def mutate(text, old, new, label):
    if old not in text:
        raise SystemExit("control %s: anchor not found" % label)
    return text.replace(old, new)


def build_controls(rows):
    by_id = {row["task_id"]: row for row in rows}
    controls = {}

    # 1. access-log: nearest-rank replaced by a 0-based index -> p95 is off by one
    #    rank, everything else is right.
    solution = by_id["tbdl-access-log-report"]["solution"]
    controls["tbdl-access-log-report"] = mutate(
        solution,
        "p95 = durations[math.ceil(0.95 * count) - 1] if count else 0",
        "p95 = durations[int(round(0.95 * count)) - 1] if count else 0",
        "access-log",
    )

    # 2. git-recovery: recovers the *wrong* historical revision and never
    #    rescues the unreachable commit.
    solution = by_id["tbdl-git-recovery"]["solution"]
    solution = mutate(solution, "/ Add config loader$/", "/ Initial import$/", "git-recovery")
    solution = mutate(
        solution, 'git branch recovered "$wip_commit"', "true  # gave up on the wip commit", "git-recovery-2"
    )
    controls["tbdl-git-recovery"] = solution

    # 3. csv-canon: forgets the BOM and forgets to sort.
    solution = by_id["tbdl-csv-canon"]["solution"]
    solution = mutate(solution, 'encoding="utf-8-sig"', 'encoding="utf-8"', "csv-canon")
    solution = mutate(
        solution,
        'rows.sort(key=lambda row: (row[0], row[1] if len(row) > 1 else ""))',
        "pass  # rows arrive sorted, surely",
        "csv-canon-2",
    )
    controls["tbdl-csv-canon"] = solution

    # 4. build-repair: fixes the shell bugs only, leaving both Python faults.
    controls["tbdl-build-repair"] = """#!/usr/bin/env bash
set -euo pipefail
cd "$TB_WORK"
python - <<'PYEOF'
from pathlib import Path
path = Path("build.sh")
text = path.read_text(encoding="utf-8")
text = text.replace('mkdir -p "$OUTDIR"', 'mkdir -p "$OUT" build')
path.write_text(text, encoding="utf-8", newline="\\n")
PYEOF
bash build.sh || true
"""
    return controls


def main():
    OUTDIR.mkdir(exist_ok=True)
    rows = [json.loads(line) for line in open(DATASET, encoding="utf-8") if line.strip()]
    controls = build_controls(rows)
    target = OUTDIR / "partial.jsonl"
    with open(target, "w", encoding="utf-8", newline="\n") as handle:
        for row in rows:
            handle.write(
                json.dumps(
                    {
                        "task_id": row["task_id"],
                        "agent_name": "control-partial",
                        "agent_script": controls[row["task_id"]],
                    }
                )
                + "\n"
            )
    print("wrote %s for %d tasks" % (target, len(rows)))


if __name__ == "__main__":
    main()