Download source/tests/fuzz_tools.py from rca-umb/five-desks-control: direct link, hf CLI and curl.
- Browser
- Download file 4.46 kB
-
https://huggingface.co/datasets/rca-umb/five-desks-control/resolve/main/source/tests/fuzz_tools.py
- Command line
-
hf download hf://datasets/rca-umb/five-desks-control/source/tests/fuzz_tools.py
-
curl -L -o fuzz_tools.py https://huggingface.co/datasets/rca-umb/five-desks-control/resolve/main/source/tests/fuzz_tools.py
4.46 kB
| """Fuzz every tool of every task with hostile arguments; no call may raise, and every reward must be in [0, 1]. | |
| Usage: .venv/bin/python tests/fuzz_tools.py [--seeds 4] [--rounds 60] | |
| """ | |
| import argparse | |
| import math | |
| import random | |
| import sys | |
| import time | |
| import traceback | |
| from pathlib import Path | |
| sys.path.insert(0, str(Path(__file__).resolve().parents[1])) | |
| from diligence.environment import TASK_IDS, DiligenceEnvironment # noqa: E402 | |
| from diligence.models import ToolAction # noqa: E402 | |
| STRINGS = ["", " ", "\n", "None", "null", "-1", "0", "1e309", "nan", "../../etc/passwd", "/etc/passwd", "./", ".", | |
| "x" * 20000, "\x00", "é漢字🙂", "'; DROP TABLE invoices; --", "{{}}", "%s%s%n", "\\", "e1", "E1", | |
| "2026-10-05", "2026-02-30", "25:99", "09:00", "9:00", "09:00:00", "-09:00", "README.md", "DEPLOYMENT.md", | |
| "Dana Whitfield", "CWE-89", "cwe-89 sql injection", "SELECT 1", "print(1)", "import os; os._exit(0)"] | |
| INTS = [0, 1, -1, 2, 7, 13, 99, 10 ** 18, -(10 ** 18), 2 ** 63] | |
| FLOATS = [0.0, -0.0, 1.5, float("inf"), float("-inf"), float("nan"), 1e308] | |
| def value(rng, depth=0): | |
| kind = rng.random() | |
| if kind < 0.35: | |
| return rng.choice(STRINGS) | |
| if kind < 0.5: | |
| return rng.choice(INTS) | |
| if kind < 0.6: | |
| return rng.choice(FLOATS) | |
| if kind < 0.65: | |
| return rng.choice([True, False, None]) | |
| if depth > 2: | |
| return rng.choice(STRINGS) | |
| if kind < 0.82: | |
| return [value(rng, depth + 1) for _ in range(rng.randint(0, 4))] | |
| keys = STRINGS[:8] + ["answer", "value", "file", "line", "cwe", "total_usd", "top_customer", "date", "start"] | |
| return {str(rng.choice(keys)): value(rng, depth + 1) for _ in range(rng.randint(0, 4))} | |
| def args_for(rng, tool): | |
| """Mostly well-typed arguments (so the tool body runs), sometimes extra, missing or mistyped ones.""" | |
| out = {} | |
| for name, typ in tool.args.items(): | |
| if rng.random() < 0.08: | |
| continue | |
| v = value(rng) | |
| if rng.random() < 0.8: | |
| for _ in range(50): | |
| if isinstance(v, typ) and not (typ in (int, float) and isinstance(v, bool)): | |
| break | |
| v = value(rng) | |
| out[name] = v | |
| if rng.random() < 0.05: | |
| out[rng.choice(STRINGS[:5])] = value(rng) | |
| return out | |
| def check(obs, where): | |
| assert isinstance(obs.output, str), where | |
| assert isinstance(obs.done, bool), where | |
| r = obs.reward | |
| assert r is None or (isinstance(r, (int, float)) and math.isfinite(r) and 0 <= r <= 1), (where, r) | |
| assert len(obs.output) <= 6100, (where, len(obs.output)) | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--seeds", type=int, default=4) | |
| ap.add_argument("--rounds", type=int, default=60) | |
| args = ap.parse_args() | |
| rng = random.Random(12345) | |
| calls = failures = 0 | |
| slowest = (0.0, "") | |
| for task_id in TASK_IDS: | |
| for seed in range(args.seeds): | |
| env = DiligenceEnvironment() | |
| for _ in range(args.rounds): | |
| if env._done: | |
| check(env.reset(task_id=task_id, seed=seed, canonical_names=True), task_id) | |
| tool = rng.choice(list(env._tools.values())) | |
| if tool.name == "python" and rng.random() < 0.7: | |
| continue # python is slow; it gets its own adversarial tests | |
| action = {"tool": tool.name, "args": args_for(rng, tool)} | |
| if rng.random() < 0.03: | |
| action["tool"] = rng.choice(STRINGS[:6]) or "x" | |
| where = f"{task_id} seed {seed} {str(action)[:300]}" | |
| t0 = time.monotonic() | |
| try: | |
| check(env.step(ToolAction(**action)), where) | |
| except Exception: # noqa: BLE001 | |
| failures += 1 | |
| print("FAIL", where) | |
| traceback.print_exc(limit=3) | |
| took = time.monotonic() - t0 | |
| slowest = max(slowest, (took, where[:160])) | |
| calls += 1 | |
| # stepping after the episode is over must be harmless too | |
| env._done = True | |
| check(env.step(ToolAction(tool="submit", args={})), task_id) | |
| print(f"{task_id}: done ({calls} calls so far, {failures} failures)", flush=True) | |
| print(f"{calls} calls, {failures} failures; slowest {slowest[0]:.1f}s: {slowest[1]}") | |
| sys.exit(1 if failures else 0) | |
| if __name__ == "__main__": | |
| main() | |