Repository navigation
Expand file tree
/
Copy pathrun_simulator.py
More file actions
187 lines (147 loc) · 6.09 KB
/
Copy pathrun_simulator.py
File metadata and controls
187 lines (147 loc) · 6.09 KB
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
"""
Sandbox-only pipeline: execute each script in the Docker sandbox and save the
raw output. No world-model simulation is performed.
Skip-existing is ON by default; pass --overwrite to re-run scripts that
already have a result file.
Usage:
python run_simulator.py [--scripts scripts/] [--results results/] [--config config/settings.yaml]
"""
from __future__ import annotations
import argparse
import json
import logging
import sys
import threading
from concurrent.futures import ThreadPoolExecutor, as_completed
from pathlib import Path
import yaml
from sandbox import runner as sandbox_runner
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)-8s [%(threadName)s] %(name)s: %(message)s",
)
log = logging.getLogger("simulator")
_log_lock = threading.Lock()
def _log(level: str, msg: str, *args) -> None:
with _log_lock:
getattr(log, level)(msg, *args)
def parse_args() -> argparse.Namespace:
p = argparse.ArgumentParser(description="Sandbox-only script runner (no simulation)")
p.add_argument("--scripts", default="scripts", help="Root directory or single script file")
p.add_argument("--results", default="results", help="Output directory for results")
p.add_argument("--config", default="config/settings.yaml")
p.add_argument("--categories", nargs="*", help="Limit to specific categories")
p.add_argument("--build-sandbox", action="store_true", help="(Re)build sandbox Docker image first")
p.add_argument("--workers", type=int, default=None,
help="Override sandbox.max_workers from config")
p.add_argument("--debug", action="store_true",
help="Set log level to DEBUG")
p.add_argument("--overwrite", action="store_true",
help="Re-run scripts that already have a result file (default: skip existing)")
return p.parse_args()
def collect_scripts(scripts_dir: Path, categories: list[str] | None) -> list[Path]:
if scripts_dir.is_file():
return [scripts_dir]
scripts = sorted(scripts_dir.glob("**/*.sh"))
if categories:
scripts = [s for s in scripts if s.parent.name in categories]
return scripts
def _result_path(results_dir: Path, script_path: Path) -> Path:
return results_dir / "raw" / f"{script_path.parent.stem}_{script_path.stem}.json"
def _process_one(
script_path: Path,
cfg: dict,
results_dir: Path,
) -> dict:
"""Run one script in the sandbox and save raw output. Returns summary entry."""
threading.current_thread().name = script_path.stem
category = script_path.parent.name
content = script_path.read_text()
_log("info", "START %s", script_path)
actual: dict = {}
try:
actual = sandbox_runner.run(content, cfg)
_log("debug", " sandbox done %s", script_path.name)
except Exception as exc:
_log("error", " sandbox FAILED %s: %s", script_path.name, exc)
actual = {"error": str(exc)}
# Save raw sandbox output directly (no comparison)
out = results_dir / "raw"
out.mkdir(parents=True, exist_ok=True)
dest = out / f"{category}_{script_path.stem}.json"
dest.write_text(json.dumps(actual, indent=2))
_log("info", "Saved raw output → %s", dest)
stdout = actual.get("stdout", "")
exit_code = actual.get("exit_code")
has_error = "error" in actual
_log(
"info",
"DONE %-45s exit_code=%s error=%s",
str(script_path),
exit_code,
has_error,
)
return {
"script": str(script_path),
"category": category,
"exit_code": exit_code,
"stdout_length": len(stdout),
"has_error": has_error,
}
def main() -> int:
args = parse_args()
if args.debug:
logging.getLogger().setLevel(logging.DEBUG)
for noisy in ("httpx", "httpcore", "urllib3"):
logging.getLogger(noisy).setLevel(logging.WARNING)
with open(args.config) as f:
cfg = yaml.safe_load(f)
if args.build_sandbox:
log.info("Building sandbox Docker image...")
sandbox_runner.build_sandbox_image(cfg)
scripts_dir = Path(args.scripts)
results_dir = Path(args.results)
results_dir.mkdir(parents=True, exist_ok=True)
scripts = collect_scripts(scripts_dir, args.categories)
if not scripts:
log.error("No scripts found in %s", scripts_dir)
return 1
if not args.overwrite:
raw_dir = results_dir / "raw"
before = len(scripts)
scripts = [s for s in scripts if not _result_path(results_dir, s).exists()]
log.info("Skip-existing: skipped %d already-run script(s), %d remaining",
before - len(scripts), len(scripts))
if not scripts:
log.info("All scripts already have results; nothing to do.")
return 0
else:
log.info("--overwrite: re-running all scripts")
max_workers = args.workers or cfg.get("sandbox", {}).get("max_workers",
cfg.get("simulation", {}).get("max_workers", 3))
log.info("Found %d script(s), running with max_workers=%d", len(scripts), max_workers)
summary: list[dict] = []
with ThreadPoolExecutor(max_workers=max_workers) as pool:
futures = {
pool.submit(_process_one, sp, cfg, results_dir): sp
for sp in scripts
}
for future in as_completed(futures):
script_path = futures[future]
try:
entry = future.result()
summary.append(entry)
except Exception as exc:
_log("error", "Unhandled error for %s: %s", script_path, exc)
summary.sort(key=lambda e: e["script"])
summary_path = results_dir / "summary.json"
summary_path.write_text(json.dumps(summary, indent=2))
log.info("Summary written to %s", summary_path)
if summary:
total = len(summary)
errors = sum(1 for e in summary if e["has_error"])
avg_exit = sum(e["exit_code"] for e in summary if e["exit_code"] is not None) / max(1, total - errors)
log.info("Total: %d Errors: %d Avg exit_code: %.1f", total, errors, avg_exit)
return 0
if __name__ == "__main__":
sys.exit(main())