sciworld 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
sciworld/cli.py ADDED
@@ -0,0 +1,1398 @@
1
+ """CLI entry point for SciWorld."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import argparse
6
+ import logging
7
+ import shutil
8
+ import sys
9
+ import tempfile
10
+ from pathlib import Path
11
+
12
+ from sciworld.config import (
13
+ config_dir, instructions_dir, load_config, save_config,
14
+ load_instruction, save_instruction, list_instructions,
15
+ is_initialized, DEFAULT_CONFIG,
16
+ )
17
+ from sciworld.github import DEFAULT_ORG, RepoInfo, clone_repo, list_repos, _get_github_token
18
+ from sciworld.milestones import MILESTONE_ORDER, meets_minimum
19
+ from sciworld.package import package_environment
20
+ from sciworld import style as S
21
+
22
+ # Sentinel: select() returned "Exit"
23
+ _EXIT = -1
24
+
25
+
26
+ # ── sw init ───────────────────────────────────────────────
27
+
28
+ def cmd_init(args: argparse.Namespace) -> int:
29
+ """Initialize ~/.sciworld with default config."""
30
+
31
+ S.intro(
32
+ "Scientist World",
33
+ prefix="Welcome to",
34
+ subtitle=f"Every released paper with code is a frozen research\n"
35
+ f"trajectory. {S.ITALIC}Scientist World{S.RESET}{S.DIM} converts papers into\n"
36
+ f"execution-grounded environments at scale — a benchmark\n"
37
+ f"and training gym for AI research agents.",
38
+ )
39
+
40
+ cfg_dir = config_dir()
41
+
42
+ if is_initialized() and not args.force:
43
+ print(f" {S.warn(f'Already initialized at {S.BOLD}{cfg_dir}{S.RESET}')}")
44
+ print(f" {S.DIM}Run {S.RESET}{S.BOLD}sw init --force{S.RESET}{S.DIM} to reconfigure.{S.RESET}")
45
+ print()
46
+ return 0
47
+
48
+ cfg = load_config() if is_initialized() else dict(DEFAULT_CONFIG)
49
+ if args.org:
50
+ cfg["org"] = args.org
51
+
52
+ # ── Step 1: Environments directory ──────────────────────
53
+
54
+ rc = _prompt_envs_dir(cfg)
55
+ if rc == _EXIT:
56
+ save_config(cfg)
57
+ _finish_init(cfg)
58
+ return 0
59
+
60
+ # ── Step 2: Compute ───────────────────────────────────
61
+
62
+ rc = _prompt_compute()
63
+ if rc == _EXIT:
64
+ save_config(cfg)
65
+ _finish_init(cfg)
66
+ return 0
67
+
68
+ # ── Step 3: Container runtime ─────────────────────────
69
+
70
+ rc = _prompt_container(cfg)
71
+ if rc == _EXIT:
72
+ save_config(cfg)
73
+ _finish_init(cfg)
74
+ return 0
75
+
76
+ # ── Step 4: Session manager ───────────────────────────
77
+
78
+ rc = _prompt_session(cfg)
79
+ if rc == _EXIT:
80
+ save_config(cfg)
81
+ _finish_init(cfg)
82
+ return 0
83
+
84
+ # ── Step 5: System prompt ─────────────────────────────
85
+
86
+ rc = _prompt_system_prompt()
87
+ if rc == _EXIT:
88
+ save_config(cfg)
89
+ _finish_init(cfg)
90
+ return 0
91
+
92
+ # ── Done ──────────────────────────────────────────────
93
+
94
+ save_config(cfg)
95
+
96
+ # Write default research prompts if not already customized
97
+ from sciworld.research_cmd import _DEFAULT_PROMPTS
98
+ for mode_name, default_text in _DEFAULT_PROMPTS.items():
99
+ if not load_instruction(f"research_{mode_name}"):
100
+ save_instruction(f"research_{mode_name}", default_text)
101
+
102
+ _finish_init(cfg)
103
+ return 0
104
+
105
+
106
+ def _finish_init(cfg: dict) -> None:
107
+ """Print save confirmation, summary, and next steps."""
108
+ S.bar()
109
+ S.step_done(f"Config saved to {S.BOLD}{config_dir()}{S.RESET}")
110
+
111
+ # Create data directory layout if data_dir was set
112
+ data_dir = cfg.get("data_dir", "")
113
+ if data_dir:
114
+ dd = Path(data_dir)
115
+ dd.mkdir(parents=True, exist_ok=True)
116
+ (dd / "envs").mkdir(exist_ok=True)
117
+ (dd / "containers").mkdir(exist_ok=True)
118
+ S.bar()
119
+ S.step_done(f"Data layout created at {S.BOLD}{dd}{S.RESET}")
120
+
121
+ # Build the agent container if a runtime is configured
122
+ runtime = cfg.get("container", "")
123
+ if runtime:
124
+ _build_agent_container(dd / "containers", runtime)
125
+
126
+ _print_config_summary(cfg)
127
+
128
+ S.bar()
129
+ S.step_end(f"{S.BOLD}Next steps{S.RESET}")
130
+ print(f" {S.CYAN}${S.RESET} sw select {S.DIM}Browse and select a paper{S.RESET}")
131
+ print(f" {S.CYAN}${S.RESET} sw research --id XXXX.XXXXX {S.DIM}Build a research environment{S.RESET}")
132
+ print(f" {S.CYAN}${S.RESET} sw config list {S.DIM}View your settings{S.RESET}")
133
+ print()
134
+
135
+
136
+ # ── Config prompt helpers ─────────────────────────────────
137
+ # Each returns _EXIT if user chose exit, 0 otherwise.
138
+ # Compute writes to instruction markdown files.
139
+ # Paths/values write to cfg dict (saved to config.json by caller).
140
+
141
+
142
+ def _prompt_compute() -> int:
143
+ """Interactive compute setup — writes to instruction files."""
144
+ S.bar()
145
+ S.step_start("Compute setup")
146
+ S.bar()
147
+ S.note([
148
+ "How the AI agent should launch GPU/CPU jobs on your system.",
149
+ f"This instruction is injected into the agent's prompt so it",
150
+ f"knows how to request compute (e.g. SLURM, local, cloud).",
151
+ ])
152
+ S.bar()
153
+
154
+ # Check for existing value
155
+ existing_combined = load_instruction("compute")
156
+ existing_gpu = load_instruction("compute_gpu")
157
+ existing_cpu = load_instruction("compute_cpu")
158
+ has_existing = bool(existing_combined or existing_gpu or existing_cpu)
159
+
160
+ if has_existing:
161
+ if existing_combined:
162
+ S.note([f"Current: {S.RESET}{existing_combined}"])
163
+ else:
164
+ if existing_gpu:
165
+ S.note([f"Current GPU: {S.RESET}{existing_gpu}"])
166
+ if existing_cpu:
167
+ S.note([f"Current CPU: {S.RESET}{existing_cpu}"])
168
+ S.bar()
169
+ options = [
170
+ "Keep existing",
171
+ "Same instruction for GPU and CPU",
172
+ "Separate GPU and CPU instructions",
173
+ "Exit setup",
174
+ ]
175
+ choice = S.select("Compute config", options, default=0)
176
+ if choice == 0:
177
+ S.bar()
178
+ S.step_done(f"{S.DIM}Kept existing{S.RESET}")
179
+ return 0
180
+ elif choice == 1:
181
+ choice = 0 # map to combined
182
+ elif choice == 2:
183
+ choice = 1 # map to separate
184
+ else:
185
+ S.bar()
186
+ S.step_done(f"{S.DIM}Exiting setup{S.RESET}")
187
+ return _EXIT
188
+ else:
189
+ S.note([
190
+ "How does your machine run GPU and CPU jobs?",
191
+ "",
192
+ "Examples:",
193
+ f" {S.RESET}srun --gres=gpu:1 --mem=32G --time=4:00:00",
194
+ f" {S.RESET}srun --cpus-per-task=8 --mem=16G --time=2:00:00",
195
+ f" {S.RESET}local{S.DIM} (run directly, no job scheduler)",
196
+ ])
197
+ S.bar()
198
+ choice = S.select("Configure compute?", [
199
+ "Same instruction for GPU and CPU",
200
+ "Separate GPU and CPU instructions",
201
+ "Skip",
202
+ "Exit setup",
203
+ ], default=1)
204
+
205
+ if choice == 2:
206
+ S.bar()
207
+ S.step_done(f"{S.DIM}Skipped — run {S.RESET}{S.BOLD}sw config compute{S.RESET}{S.DIM} later{S.RESET}")
208
+ return 0
209
+ elif choice == 3:
210
+ S.bar()
211
+ S.step_done(f"{S.DIM}Exiting setup{S.RESET}")
212
+ return _EXIT
213
+
214
+ if choice == 0:
215
+ S.bar()
216
+ cmd = S.prompt("Compute command")
217
+ save_instruction("compute", cmd)
218
+ _remove_instruction("compute_gpu")
219
+ _remove_instruction("compute_cpu")
220
+ S.bar()
221
+ S.step_done(S.success("Compute instruction saved"))
222
+ elif choice == 1:
223
+ S.bar()
224
+ gpu_cmd = S.prompt("GPU command")
225
+ cpu_cmd = S.prompt("CPU command")
226
+ save_instruction("compute_gpu", gpu_cmd)
227
+ save_instruction("compute_cpu", cpu_cmd)
228
+ _remove_instruction("compute")
229
+ S.bar()
230
+ S.step_done(S.success("Compute instructions saved"))
231
+
232
+ return 0
233
+
234
+
235
+ def _remove_instruction(name: str) -> None:
236
+ """Delete an instruction file if it exists."""
237
+ path = instructions_dir() / f"{name}.md"
238
+ if path.exists():
239
+ path.unlink()
240
+
241
+
242
+ # Supported container runtimes — display order
243
+ _SUPPORTED_CONTAINERS = ["apptainer", "docker", "podman", "singularity"]
244
+
245
+
246
+ def _detect_containers() -> dict[str, str]:
247
+ """Return {name: path} for all supported runtimes found in PATH."""
248
+ import shutil as _sh
249
+ return {name: _sh.which(name) for name in _SUPPORTED_CONTAINERS if _sh.which(name)}
250
+
251
+
252
+ def _verify_container(name: str) -> bool:
253
+ """Run a quick sanity check that the runtime works."""
254
+ import subprocess as _sp
255
+ try:
256
+ result = _sp.run([name, "--version"], capture_output=True, timeout=5)
257
+ return result.returncode == 0
258
+ except (FileNotFoundError, _sp.TimeoutExpired):
259
+ return False
260
+
261
+
262
+ def _agent_def_path() -> Path:
263
+ """Path to the bundled agent.def file (apptainer/singularity)."""
264
+ return Path(__file__).parent / "containers" / "agent.def"
265
+
266
+
267
+ def _agent_dockerfile_path() -> Path:
268
+ """Path to the bundled agent.Dockerfile (docker/podman)."""
269
+ return Path(__file__).parent / "containers" / "agent.Dockerfile"
270
+
271
+
272
+ _DOCKER_IMAGE_TAG = "sciworld-agent:latest"
273
+
274
+
275
+ def _build_agent_container(containers_dir: Path, runtime: str) -> bool:
276
+ """Build the agent container for the configured runtime.
277
+
278
+ - apptainer / singularity → ``<containers_dir>/agent.sif`` from agent.def
279
+ - docker / podman → image ``sciworld-agent:latest`` from agent.Dockerfile
280
+
281
+ Shows a live progress line and offers a skip option before starting.
282
+ Returns True on success or skip, False on failure.
283
+ """
284
+ import subprocess as _sp
285
+ import threading
286
+ import time
287
+
288
+ is_apptainer_family = runtime in ("apptainer", "singularity")
289
+ is_docker_family = runtime in ("docker", "podman")
290
+
291
+ if not (is_apptainer_family or is_docker_family):
292
+ S.bar()
293
+ S.step_done(S.error(
294
+ f"Unsupported container runtime: {runtime}. "
295
+ f"Supported: apptainer, singularity, docker, podman."
296
+ ))
297
+ return False
298
+
299
+ # Build file: .def for apptainer family, Dockerfile for docker family
300
+ if is_apptainer_family:
301
+ build_file = _agent_def_path()
302
+ sif_path = containers_dir / "agent.sif"
303
+ already_built = sif_path.exists()
304
+ artifact_label = f"{sif_path}"
305
+ else:
306
+ build_file = _agent_dockerfile_path()
307
+ sif_path = None
308
+ already_built = _docker_image_exists(runtime, _DOCKER_IMAGE_TAG)
309
+ artifact_label = f"{runtime} image {_DOCKER_IMAGE_TAG}"
310
+
311
+ if not build_file.exists():
312
+ S.bar()
313
+ S.step_done(S.error(f"Build file missing: {build_file}"))
314
+ return False
315
+
316
+ if already_built:
317
+ S.bar()
318
+ S.step_done(f"Agent container already built: {S.BOLD}{artifact_label}{S.RESET}")
319
+ return True
320
+
321
+ # Offer to skip
322
+ S.bar()
323
+ S.note([
324
+ f"About to build the agent container (~5-10 minutes, ~300MB).",
325
+ f"Required before running {S.RESET}{S.BOLD}sw research{S.RESET}{S.DIM}.",
326
+ ])
327
+ S.bar()
328
+ choice = S.select("Build agent container now?", ["Build now", "Skip (build on first sw research)"])
329
+ if choice == 1:
330
+ S.bar()
331
+ S.step_done(f"{S.DIM}Skipped — will build on first run{S.RESET}")
332
+ return True
333
+
334
+ # Build with live progress
335
+ S.bar()
336
+ S.step_start(f"Building agent container ({runtime})")
337
+ start = time.time()
338
+ done = threading.Event()
339
+
340
+ def _tick():
341
+ phases = ["pulling base", "installing apt packages", "installing node.js",
342
+ "installing claude cli", "installing codex cli", "finalizing"]
343
+ i = 0
344
+ while not done.is_set():
345
+ elapsed = int(time.time() - start)
346
+ mins, secs = divmod(elapsed, 60)
347
+ phase = phases[min(i // 20, len(phases) - 1)]
348
+ sys.stdout.write(f"{S.MOVE_UP}{S.CLEAR_LINE}")
349
+ S.step_start(f"Building agent container ({runtime}) "
350
+ f"{S.DIM}{mins:d}m{secs:02d}s · {phase}{S.RESET}")
351
+ sys.stdout.flush()
352
+ i += 1
353
+ if done.wait(1.0):
354
+ break
355
+
356
+ ticker = threading.Thread(target=_tick, daemon=True)
357
+ ticker.start()
358
+
359
+ if is_apptainer_family:
360
+ build_cmd = [runtime, "build", "--force", str(sif_path), str(build_file)]
361
+ else:
362
+ # docker/podman: tag + dockerfile + context dir
363
+ build_cmd = [
364
+ runtime, "build",
365
+ "--tag", _DOCKER_IMAGE_TAG,
366
+ "--file", str(build_file),
367
+ str(build_file.parent),
368
+ ]
369
+
370
+ try:
371
+ result = _sp.run(
372
+ build_cmd,
373
+ capture_output=True, text=True, timeout=2700,
374
+ )
375
+ except _sp.TimeoutExpired:
376
+ done.set()
377
+ ticker.join(timeout=2)
378
+ sys.stdout.write(f"{S.MOVE_UP}{S.CLEAR_LINE}")
379
+ S.step_done(S.error("Build timed out after 45 minutes"))
380
+ return False
381
+ except FileNotFoundError:
382
+ done.set()
383
+ ticker.join(timeout=2)
384
+ sys.stdout.write(f"{S.MOVE_UP}{S.CLEAR_LINE}")
385
+ S.step_done(S.error(f"{runtime} not found in PATH"))
386
+ return False
387
+ finally:
388
+ done.set()
389
+ ticker.join(timeout=2)
390
+
391
+ sys.stdout.write(f"{S.MOVE_UP}{S.CLEAR_LINE}")
392
+ if result.returncode != 0:
393
+ S.step_done(S.error(f"Build failed: {result.stderr.strip()[-400:]}"))
394
+ return False
395
+
396
+ elapsed = int(time.time() - start)
397
+ mins, secs = divmod(elapsed, 60)
398
+ S.step_done(S.success(f"Built {S.BOLD}{artifact_label}{S.RESET} {S.DIM}({mins:d}m{secs:02d}s){S.RESET}"))
399
+ return True
400
+
401
+
402
+ def _docker_image_exists(runtime: str, image_tag: str) -> bool:
403
+ """Return True if the named docker/podman image exists locally."""
404
+ import subprocess as _sp
405
+ try:
406
+ r = _sp.run(
407
+ [runtime, "image", "inspect", image_tag],
408
+ capture_output=True, timeout=10,
409
+ )
410
+ return r.returncode == 0
411
+ except (FileNotFoundError, _sp.TimeoutExpired):
412
+ return False
413
+
414
+
415
+ def _prepare_container_dir(cfg: dict) -> Path | None:
416
+ """Create <data_dir>/containers/ if data_dir is set."""
417
+ data_dir = cfg.get("data_dir", "")
418
+ if not data_dir:
419
+ return None
420
+ path = Path(data_dir) / "containers"
421
+ path.mkdir(parents=True, exist_ok=True)
422
+ return path
423
+
424
+
425
+ _CONTAINER_INSTALL_HELP = {
426
+ "apptainer": (
427
+ "# Linux (Debian/Ubuntu)\n"
428
+ "sudo add-apt-repository -y ppa:apptainer/ppa && sudo apt update\n"
429
+ "sudo apt install -y apptainer\n"
430
+ "\n"
431
+ "# Linux (RPM/Fedora/RHEL)\n"
432
+ "sudo dnf install -y apptainer\n"
433
+ "\n"
434
+ "# Docs: https://apptainer.org/docs/admin/main/installation.html"
435
+ ),
436
+ "docker": (
437
+ "# Linux: https://docs.docker.com/engine/install/\n"
438
+ "# macOS/Windows: https://docs.docker.com/desktop/"
439
+ ),
440
+ "podman": (
441
+ "# Linux (Debian/Ubuntu)\n"
442
+ "sudo apt install -y podman\n"
443
+ "\n"
444
+ "# Linux (RPM/Fedora/RHEL)\n"
445
+ "sudo dnf install -y podman\n"
446
+ "\n"
447
+ "# macOS: brew install podman\n"
448
+ "# Docs: https://podman.io/docs/installation"
449
+ ),
450
+ }
451
+
452
+
453
+ def _install_help_flow(name: str) -> bool:
454
+ """Show install instructions for *name* and loop with Check/Back.
455
+
456
+ Returns True if the runtime became available (user picked Check now and
457
+ it was found); False if the user went back.
458
+ """
459
+ import shutil as _sh
460
+ while True:
461
+ S.bar()
462
+ help_text = _CONTAINER_INSTALL_HELP.get(name, "(no instructions available)")
463
+ for line in help_text.split("\n"):
464
+ S.bar(f"{S.DIM}{line}{S.RESET}")
465
+ S.bar()
466
+ pick = S.select(f"After installing {name}", ["Check again", "Back"])
467
+ if pick == 1:
468
+ return False
469
+ if _sh.which(name):
470
+ S.bar()
471
+ S.step_done(S.success(f"{name} is now available"))
472
+ return True
473
+ S.bar()
474
+ S.step_done(S.error(f"{name} still not found in PATH"))
475
+
476
+
477
+ def _prompt_container(cfg: dict) -> int:
478
+ """List all supported runtimes with status and let the user choose one."""
479
+ S.bar()
480
+ S.step_start("Container runtime")
481
+ S.bar()
482
+ S.note([
483
+ "Agents run experiments inside a container for isolation.",
484
+ f"Supported: {S.RESET}apptainer{S.DIM} (HPC), {S.RESET}docker{S.DIM}, {S.RESET}podman{S.DIM}, {S.RESET}singularity{S.DIM}",
485
+ ])
486
+ S.bar()
487
+
488
+ existing = cfg.get("container", "")
489
+ if existing:
490
+ S.note([f"Current: {S.RESET}{existing}"])
491
+ S.bar()
492
+
493
+ while True:
494
+ found = _detect_containers() # {name: path}
495
+
496
+ options = []
497
+ runtime_names = []
498
+ if existing:
499
+ options.append("Keep existing")
500
+ runtime_names.append("__keep__")
501
+ for name in _SUPPORTED_CONTAINERS:
502
+ if name in found:
503
+ options.append(f"{name} {S.GREEN}exists in{S.RESET} {S.DIM}{found[name]}{S.RESET}")
504
+ else:
505
+ options.append(f"{name} {S.DIM}needs installation{S.RESET}")
506
+ runtime_names.append(name)
507
+ options.append("Skip")
508
+ runtime_names.append("__skip__")
509
+ options.append("Exit setup")
510
+ runtime_names.append("__exit__")
511
+
512
+ choice = S.select("Container runtime", options, default=0)
513
+ selected = runtime_names[choice]
514
+
515
+ if selected == "__keep__":
516
+ S.bar()
517
+ S.step_done(f"{S.DIM}Kept existing ({existing}){S.RESET}")
518
+ return 0
519
+ if selected == "__exit__":
520
+ S.bar()
521
+ S.step_done(f"{S.DIM}Exiting setup{S.RESET}")
522
+ return _EXIT
523
+ if selected == "__skip__":
524
+ S.bar()
525
+ S.step_done(f"{S.DIM}Skipped — run {S.RESET}{S.BOLD}sw config container{S.RESET}{S.DIM} later{S.RESET}")
526
+ return 0
527
+
528
+ # User picked an actual runtime
529
+ name = selected
530
+ if name not in found:
531
+ # Show install help and loop back on "Back"
532
+ if not _install_help_flow(name):
533
+ continue # user hit Back → show the runtime list again
534
+ # User picked "Will it work?" and it's now found — re-detect
535
+ found = _detect_containers()
536
+ if name not in found:
537
+ continue
538
+
539
+ # Verify the runtime actually works
540
+ S.bar()
541
+ S.step_start(f"Verifying {S.BOLD}{name}{S.RESET}")
542
+ if not _verify_container(name):
543
+ S.bar()
544
+ S.step_done(S.error(f"{name} found but failed to run --version"))
545
+ continue
546
+ cfg["container"] = name
547
+ S.step_done(S.success(f"Using {S.BOLD}{name}{S.RESET}"))
548
+ return 0
549
+
550
+
551
+ # ── Session manager ──────────────────────────────────────
552
+
553
+ _SUPPORTED_SESSIONS = ["tmux", "screen", "nohup"]
554
+
555
+ _SESSION_DESC = {
556
+ "tmux": "persistent session, attach/detach (preferred)",
557
+ "screen": "persistent session, attach/detach (older alternative)",
558
+ "nohup": "background + log file (always available, no reattach)",
559
+ }
560
+
561
+
562
+ def _detect_sessions() -> dict[str, str]:
563
+ """Return {name: path} for session tools found in PATH. nohup is always present."""
564
+ import shutil as _sh
565
+ found = {}
566
+ for name in ("tmux", "screen"):
567
+ p = _sh.which(name)
568
+ if p:
569
+ found[name] = p
570
+ found["nohup"] = _sh.which("nohup") or "/usr/bin/nohup"
571
+ return found
572
+
573
+
574
+ def _prompt_session(cfg: dict) -> int:
575
+ """Let the user pick a session manager for long-running agent runs."""
576
+ S.bar()
577
+ S.step_start("Session manager")
578
+ S.bar()
579
+ S.note([
580
+ "How agent runs are kept alive when you close the terminal.",
581
+ f"{S.RESET}tmux{S.DIM}/{S.RESET}screen{S.DIM} let you reattach; {S.RESET}nohup{S.DIM} just logs to a file.",
582
+ ])
583
+ S.bar()
584
+
585
+ existing = cfg.get("session", "")
586
+ if existing:
587
+ S.note([f"Current: {S.RESET}{existing}"])
588
+ S.bar()
589
+
590
+ found = _detect_sessions()
591
+
592
+ options = []
593
+ runtime_names = []
594
+ if existing:
595
+ options.append("Keep existing")
596
+ runtime_names.append("__keep__")
597
+ for name in _SUPPORTED_SESSIONS:
598
+ desc = _SESSION_DESC[name]
599
+ if name in found:
600
+ options.append(f"{name} {S.GREEN}exists in{S.RESET} {S.DIM}{found[name]}{S.RESET} {S.DIM}· {desc}{S.RESET}")
601
+ else:
602
+ options.append(f"{name} {S.DIM}needs installation · {desc}{S.RESET}")
603
+ runtime_names.append(name)
604
+ options.append("Skip")
605
+ runtime_names.append("__skip__")
606
+ options.append("Exit setup")
607
+ runtime_names.append("__exit__")
608
+
609
+ # Default to first available in preference order
610
+ default_idx = 0
611
+ if not existing:
612
+ for pref in ("tmux", "screen", "nohup"):
613
+ if pref in found:
614
+ default_idx = runtime_names.index(pref)
615
+ break
616
+
617
+ choice = S.select("Session manager", options, default=default_idx)
618
+ selected = runtime_names[choice]
619
+
620
+ if selected == "__keep__":
621
+ S.bar()
622
+ S.step_done(f"{S.DIM}Kept existing ({existing}){S.RESET}")
623
+ return 0
624
+ if selected == "__exit__":
625
+ S.bar()
626
+ S.step_done(f"{S.DIM}Exiting setup{S.RESET}")
627
+ return _EXIT
628
+ if selected == "__skip__":
629
+ S.bar()
630
+ S.step_done(f"{S.DIM}Skipped — run {S.RESET}{S.BOLD}sw config session{S.RESET}{S.DIM} later{S.RESET}")
631
+ return 0
632
+
633
+ name = selected
634
+ if name not in found:
635
+ S.bar()
636
+ S.step_done(S.error(f"{name} is not installed"))
637
+ S.bar()
638
+ S.note([f"Install {name} and re-run {S.RESET}{S.BOLD}sw config session{S.RESET}"])
639
+ return 0
640
+
641
+ cfg["session"] = name
642
+ S.bar()
643
+ S.step_done(S.success(f"Using {S.BOLD}{name}{S.RESET}"))
644
+ return 0
645
+
646
+
647
+ def _prompt_envs_dir(cfg: dict) -> int:
648
+ """Interactive data directory setup — writes to config.json."""
649
+ S.step_start("Data directory")
650
+ S.bar()
651
+ S.note([
652
+ "Root folder for all sciworld data. Contents:",
653
+ f" {S.RESET}<data_dir>/envs/<arxiv_id>/{S.DIM} cloned repo + agent workspaces",
654
+ f" {S.RESET}<data_dir>/containers/{S.DIM} built container images",
655
+ ])
656
+ S.bar()
657
+
658
+ existing = cfg.get("data_dir", "")
659
+
660
+ if existing:
661
+ S.note([f"Current: {S.RESET}{existing}"])
662
+ S.bar()
663
+ choice = S.select("Data directory", [
664
+ "Keep existing",
665
+ "Set new path",
666
+ "Exit setup",
667
+ ], default=0)
668
+
669
+ if choice == 0:
670
+ S.bar()
671
+ S.step_done(f"{S.DIM}Kept existing{S.RESET}")
672
+ return 0
673
+ elif choice == 1:
674
+ S.bar()
675
+ data_dir = str(Path(S.prompt_path("Data directory")).expanduser().resolve())
676
+ cfg["data_dir"] = data_dir
677
+ S.bar()
678
+ S.step_done(S.success(f"Set to {S.BOLD}{data_dir}{S.RESET}"))
679
+ else:
680
+ S.bar()
681
+ S.step_done(f"{S.DIM}Exiting setup{S.RESET}")
682
+ return _EXIT
683
+ else:
684
+ choice = S.select("Configure data directory?", [
685
+ "Set a path",
686
+ "Skip",
687
+ "Exit setup",
688
+ ], default=0)
689
+
690
+ if choice == 0:
691
+ S.bar()
692
+ data_dir = str(Path(S.prompt_path("Data directory")).expanduser().resolve())
693
+ cfg["data_dir"] = data_dir
694
+ S.bar()
695
+ S.step_done(S.success(f"Set to {S.BOLD}{data_dir}{S.RESET}"))
696
+ elif choice == 1:
697
+ S.bar()
698
+ S.step_done(f"{S.DIM}Skipped — run {S.RESET}{S.BOLD}sw config paths{S.RESET}{S.DIM} later{S.RESET}")
699
+ else:
700
+ S.bar()
701
+ S.step_done(f"{S.DIM}Exiting setup{S.RESET}")
702
+ return _EXIT
703
+
704
+ return 0
705
+
706
+
707
+ # Loaded from cli/sciworld/prompts/system_prompt.md at import time — the
708
+ # default is not hardcoded in Python.
709
+ from sciworld.research_cmd import _package_prompt as _pkg_prompt
710
+ _DEFAULT_SYSTEM_PROMPT = _pkg_prompt("system_prompt")
711
+
712
+
713
+ def _prompt_system_prompt() -> int:
714
+ """Interactive system prompt setup — writes to instruction file."""
715
+ S.bar()
716
+ S.step_start("System prompt")
717
+ S.bar()
718
+ S.note([
719
+ "The base instruction given to the AI agent before each run.",
720
+ f"Sets the agent's role, behavior, and autonomy level.",
721
+ f"Press {S.RESET}Tab{S.DIM} to fill the default, or type your own.",
722
+ ])
723
+ S.bar()
724
+
725
+ existing = load_instruction("system_prompt")
726
+
727
+ if existing:
728
+ import textwrap as _tw
729
+ S.note([f"Current:{S.RESET}"])
730
+ for line in _tw.wrap(existing, width=60):
731
+ S.note([f" {S.RESET}{line}"])
732
+ S.bar()
733
+ choice = S.select("System prompt", [
734
+ "Keep existing",
735
+ "Write new",
736
+ "Exit setup",
737
+ ], default=0)
738
+
739
+ if choice == 0:
740
+ S.bar()
741
+ S.step_done(f"{S.DIM}Kept existing{S.RESET}")
742
+ elif choice == 1:
743
+ S.bar()
744
+ value = S.prompt_ghost("System prompt", _DEFAULT_SYSTEM_PROMPT)
745
+ save_instruction("system_prompt", value)
746
+ S.bar()
747
+ S.step_done(S.success("System prompt saved"))
748
+ else:
749
+ S.bar()
750
+ S.step_done(f"{S.DIM}Exiting setup{S.RESET}")
751
+ return _EXIT
752
+ else:
753
+ value = S.prompt_ghost("System prompt", _DEFAULT_SYSTEM_PROMPT)
754
+ save_instruction("system_prompt", value)
755
+ S.bar()
756
+ S.step_done(S.success("System prompt saved"))
757
+
758
+ return 0
759
+
760
+
761
+ def _bar_labeled(label: str, value: str) -> None:
762
+ """Print 'label value' under the bar, wrapping long values.
763
+
764
+ First line: │ label first-part-of-value
765
+ Continuation: │ rest-of-value
766
+ """
767
+ import textwrap
768
+ # bar prefix " │ " = 6, label like "prompt " = 7, gap " " = 2 → 15
769
+ label_col = len(label) + 2 # label + " "
770
+ prefix_w = 6 + label_col
771
+ usable = max(S._term_cols() - prefix_w - 4, 20) # 4 right margin
772
+ lines = textwrap.wrap(value, width=usable) if value.strip() else [value]
773
+ # First line: label + value inline
774
+ S.bar(f"{S.DIM}{label}{S.RESET} {lines[0]}")
775
+ # Continuation lines aligned under value start
776
+ pad = " " * label_col
777
+ for ln in lines[1:]:
778
+ S.bar(f"{pad}{ln}")
779
+
780
+
781
+ def _print_config_summary(cfg: dict) -> None:
782
+ """Print all config values under the bar."""
783
+ data = cfg.get("data_dir", "") or f"{S.DIM}not set{S.RESET}"
784
+ container = cfg.get("container", "") or f"{S.DIM}not set{S.RESET}"
785
+ session = cfg.get("session", "") or f"{S.DIM}not set{S.RESET}"
786
+ S.bar()
787
+ S.bar(f"{S.DIM}org {S.RESET} {cfg['org']}")
788
+ S.bar(f"{S.DIM}data {S.RESET} {data}")
789
+ S.bar(f"{S.DIM}container{S.RESET} {container}")
790
+ S.bar(f"{S.DIM}session {S.RESET} {session}")
791
+
792
+ # Compute: combined or separate
793
+ combined = load_instruction("compute")
794
+ gpu = load_instruction("compute_gpu")
795
+ cpu = load_instruction("compute_cpu")
796
+
797
+ if combined:
798
+ _bar_labeled("compute", combined)
799
+ elif gpu or cpu:
800
+ _bar_labeled("gpu ", gpu or f"{S.DIM}not set{S.RESET}")
801
+ _bar_labeled("cpu ", cpu or f"{S.DIM}not set{S.RESET}")
802
+ else:
803
+ S.bar(f"{S.DIM}compute{S.RESET} {S.DIM}not set{S.RESET}")
804
+
805
+ # System prompt
806
+ sp = load_instruction("system_prompt")
807
+ if sp:
808
+ _bar_labeled("prompt ", sp)
809
+ else:
810
+ S.bar(f"{S.DIM}prompt {S.RESET} {S.DIM}not set{S.RESET}")
811
+
812
+
813
+ # ── sw config ─────────────────────────────────────────────
814
+
815
+ def _prompt_agent_prompt() -> int:
816
+ """Edit or reset mode-specific agent prompts."""
817
+ from sciworld.research_cmd import _DEFAULT_PROMPTS
818
+ S.bar()
819
+ S.step_start("Agent prompts")
820
+ S.bar()
821
+ S.note([
822
+ f"Mode-specific instructions given to the agent during {S.RESET}sw research{S.DIM}.",
823
+ f"Each mode (improve/scratch) has its own prompt.",
824
+ ])
825
+ S.bar()
826
+
827
+ for mode_name in ("improve", "scratch"):
828
+ custom = load_instruction(f"research_{mode_name}")
829
+ if custom:
830
+ S.bar(f"{S.BOLD}{mode_name}{S.RESET} {S.DIM}(customized){S.RESET}")
831
+ else:
832
+ S.bar(f"{S.BOLD}{mode_name}{S.RESET} {S.DIM}(default){S.RESET}")
833
+
834
+ S.bar()
835
+ choice = S.select("What to do?", [
836
+ "Edit improve prompt",
837
+ "Edit scratch prompt",
838
+ "Reset improve to default",
839
+ "Reset scratch to default",
840
+ "Skip",
841
+ ])
842
+
843
+ if choice == 0:
844
+ current = load_instruction("research_improve") or _DEFAULT_PROMPTS["improve"]
845
+ S.bar()
846
+ value = S.prompt_ghost("Improve prompt", current)
847
+ save_instruction("research_improve", value)
848
+ S.bar()
849
+ S.step_done(S.success("Improve prompt saved"))
850
+ elif choice == 1:
851
+ current = load_instruction("research_scratch") or _DEFAULT_PROMPTS["scratch"]
852
+ S.bar()
853
+ value = S.prompt_ghost("Scratch prompt", current)
854
+ save_instruction("research_scratch", value)
855
+ S.bar()
856
+ S.step_done(S.success("Scratch prompt saved"))
857
+ elif choice == 2:
858
+ save_instruction("research_improve", _DEFAULT_PROMPTS["improve"])
859
+ S.bar()
860
+ S.step_done(S.success("Improve prompt reset to default"))
861
+ elif choice == 3:
862
+ save_instruction("research_scratch", _DEFAULT_PROMPTS["scratch"])
863
+ S.bar()
864
+ S.step_done(S.success("Scratch prompt reset to default"))
865
+ else:
866
+ S.bar()
867
+ S.step_done(f"{S.DIM}Skipped{S.RESET}")
868
+
869
+ return 0
870
+
871
+
872
+ def _prompt_continue_prompt() -> int:
873
+ """Interactive editor for the --loop continuation prompt."""
874
+ from sciworld.research_cmd import _DEFAULT_CONTINUE_PROMPT
875
+ S.bar()
876
+ S.step_start("Continue prompt")
877
+ S.bar()
878
+ S.note([
879
+ f"Sent on every resumed iteration when {S.RESET}sw research --loop{S.DIM}",
880
+ f"is active. The agent's session is already resumed, so this can stay short.",
881
+ ])
882
+ S.bar()
883
+
884
+ existing = load_instruction("continue")
885
+ if existing:
886
+ S.bar(f"{S.DIM}(customized){S.RESET}")
887
+ else:
888
+ S.bar(f"{S.DIM}(default){S.RESET}")
889
+ S.bar()
890
+
891
+ choice = S.select("What to do?", [
892
+ "Edit continue prompt",
893
+ "Reset to default",
894
+ "Skip",
895
+ ])
896
+
897
+ if choice == 0:
898
+ current = existing or _DEFAULT_CONTINUE_PROMPT
899
+ S.bar()
900
+ value = S.prompt_ghost("Continue prompt", current)
901
+ save_instruction("continue", value)
902
+ S.bar()
903
+ S.step_done(S.success("Continue prompt saved"))
904
+ elif choice == 1:
905
+ save_instruction("continue", _DEFAULT_CONTINUE_PROMPT)
906
+ S.bar()
907
+ S.step_done(S.success("Continue prompt reset to default"))
908
+ else:
909
+ S.bar()
910
+ S.step_done(f"{S.DIM}Skipped{S.RESET}")
911
+
912
+ return 0
913
+
914
+
915
+ def _prompt_test_prompt() -> int:
916
+ """Interactive editor for the test-phase prompt."""
917
+ from sciworld.research_cmd import _DEFAULT_TEST_PROMPT
918
+ S.bar()
919
+ S.step_start("Test prompt")
920
+ S.bar()
921
+ S.note([
922
+ f"Sent once after the main phase ends with {S.RESET}done=true{S.DIM}.",
923
+ f"The agent runs the held-out test evaluator and writes {S.RESET}reflection.md{S.DIM}.",
924
+ ])
925
+ S.bar()
926
+
927
+ existing = load_instruction("test")
928
+ if existing:
929
+ S.bar(f"{S.DIM}(customized){S.RESET}")
930
+ else:
931
+ S.bar(f"{S.DIM}(default){S.RESET}")
932
+ S.bar()
933
+
934
+ choice = S.select("What to do?", [
935
+ "Edit test prompt",
936
+ "Reset to default",
937
+ "Skip",
938
+ ])
939
+
940
+ if choice == 0:
941
+ current = existing or _DEFAULT_TEST_PROMPT
942
+ S.bar()
943
+ value = S.prompt_ghost("Test prompt", current)
944
+ save_instruction("test", value)
945
+ S.bar()
946
+ S.step_done(S.success("Test prompt saved"))
947
+ elif choice == 1:
948
+ save_instruction("test", _DEFAULT_TEST_PROMPT)
949
+ S.bar()
950
+ S.step_done(S.success("Test prompt reset to default"))
951
+ else:
952
+ S.bar()
953
+ S.step_done(f"{S.DIM}Skipped{S.RESET}")
954
+
955
+ return 0
956
+
957
+
958
+ _CONFIG_SECTIONS = {
959
+ "paths": _prompt_envs_dir,
960
+ "compute": lambda cfg: _prompt_compute(),
961
+ "container": _prompt_container,
962
+ "session": _prompt_session,
963
+ "prompt": lambda cfg: _prompt_system_prompt(),
964
+ "agent-prompt": lambda cfg: _prompt_agent_prompt(),
965
+ "continue-prompt": lambda cfg: _prompt_continue_prompt(),
966
+ "test-prompt": lambda cfg: _prompt_test_prompt(),
967
+ }
968
+
969
+
970
+ def _cmd_config_reset() -> int:
971
+ """Delete all config and instruction files."""
972
+ import shutil as _shutil
973
+ cfg_dir = config_dir()
974
+ if not cfg_dir.exists():
975
+ print(f" {S.DIM}Nothing to reset.{S.RESET}")
976
+ print()
977
+ return 0
978
+ _shutil.rmtree(str(cfg_dir))
979
+ print()
980
+ print(f" {S.success(f'Cleared all config at {S.BOLD}{cfg_dir}{S.RESET}')}")
981
+ print(f" {S.DIM}Run {S.RESET}{S.BOLD}sw init{S.RESET}{S.DIM} to start fresh.{S.RESET}")
982
+ print()
983
+ return 0
984
+
985
+
986
+ def _print_wrapped(label: str, value: str, indent: int = 4) -> None:
987
+ """Print a labelled value, wrapping long text to fit the terminal."""
988
+ import textwrap
989
+ pad = " " * indent
990
+ # label width: e.g. "compute " = 9 visible chars
991
+ label_w = len(label) + 2 # label + 2 spaces
992
+ prefix_w = indent + label_w
993
+ cols = S._term_cols()
994
+ usable = max(cols - prefix_w - 4, 20) # 4 right margin
995
+ lines = textwrap.wrap(value, width=usable) if value.strip() else [value]
996
+ # First line: label + value
997
+ print(f"{pad}{S.DIM}{label}{S.RESET} {lines[0]}")
998
+ # Continuation lines: aligned under value
999
+ cont_pad = " " * prefix_w
1000
+ for ln in lines[1:]:
1001
+ print(f"{cont_pad}{ln}")
1002
+
1003
+
1004
+ def _cmd_config_list() -> int:
1005
+ """Print all config values and exit."""
1006
+ cfg = load_config()
1007
+ data = cfg.get("data_dir", "") or f"{S.DIM}not set{S.RESET}"
1008
+ container = cfg.get("container", "") or f"{S.DIM}not set{S.RESET}"
1009
+ session = cfg.get("session", "") or f"{S.DIM}not set{S.RESET}"
1010
+ print()
1011
+ print(f" {S.BOLD}Settings{S.RESET} {S.DIM}{config_dir() / 'config.json'}{S.RESET}")
1012
+ print(f" {S.DIM}org {S.RESET} {cfg['org']}")
1013
+ print(f" {S.DIM}data {S.RESET} {data}")
1014
+ print(f" {S.DIM}container{S.RESET} {container}")
1015
+ print(f" {S.DIM}session {S.RESET} {session}")
1016
+ print()
1017
+ print(f" {S.BOLD}Instructions{S.RESET} {S.DIM}{instructions_dir()}/{S.RESET}")
1018
+ combined = load_instruction("compute")
1019
+ gpu = load_instruction("compute_gpu")
1020
+ cpu = load_instruction("compute_cpu")
1021
+ if combined:
1022
+ _print_wrapped("compute", combined)
1023
+ elif gpu or cpu:
1024
+ _print_wrapped("gpu ", gpu or f"{S.DIM}not set{S.RESET}")
1025
+ _print_wrapped("cpu ", cpu or f"{S.DIM}not set{S.RESET}")
1026
+ else:
1027
+ print(f" {S.DIM}compute{S.RESET} {S.DIM}not set{S.RESET}")
1028
+ sp = load_instruction("system_prompt")
1029
+ if sp:
1030
+ _print_wrapped("prompt ", sp)
1031
+ else:
1032
+ print(f" {S.DIM}prompt {S.RESET} {S.DIM}not set{S.RESET}")
1033
+ # Research prompts
1034
+ print()
1035
+ print(f" {S.BOLD}Research prompts{S.RESET}")
1036
+ for mode_name in ("improve", "scratch"):
1037
+ custom = load_instruction(f"research_{mode_name}")
1038
+ if custom:
1039
+ print(f" {S.DIM}{mode_name}{S.RESET} customized")
1040
+ else:
1041
+ print(f" {S.DIM}{mode_name}{S.RESET} {S.DIM}default{S.RESET}")
1042
+ print()
1043
+ return 0
1044
+
1045
+
1046
+ def cmd_config(args: argparse.Namespace) -> int:
1047
+ """View or update configuration."""
1048
+
1049
+ if not is_initialized():
1050
+ print(f" {S.error(f'Not initialized. Run {S.BOLD}sw init{S.RESET} first.')}")
1051
+ print()
1052
+ return 1
1053
+
1054
+ section = args.section
1055
+
1056
+ if section == "list":
1057
+ return _cmd_config_list()
1058
+ if section == "reset":
1059
+ return _cmd_config_reset()
1060
+
1061
+ cfg = load_config()
1062
+
1063
+ S.intro("Scientist World", subtitle="Configuration")
1064
+
1065
+ # Show current values
1066
+ S.step_start("Current config")
1067
+ _print_config_summary(cfg)
1068
+ S.bar()
1069
+
1070
+ if section:
1071
+ fn = _CONFIG_SECTIONS.get(section)
1072
+ if not fn:
1073
+ S.step_end(S.error(f"Unknown section: {section}"))
1074
+ print(f" {S.DIM}Available: {', '.join(_CONFIG_SECTIONS)}{S.RESET}")
1075
+ print()
1076
+ return 1
1077
+ fn(cfg)
1078
+ else:
1079
+ for fn in _CONFIG_SECTIONS.values():
1080
+ rc = fn(cfg)
1081
+ if rc == _EXIT:
1082
+ break
1083
+
1084
+ save_config(cfg)
1085
+
1086
+ S.bar()
1087
+ S.step_end(f"Saved to {S.BOLD}{config_dir()}{S.RESET}")
1088
+ print()
1089
+ return 0
1090
+
1091
+
1092
+ # ── sw list ───────────────────────────────────────────────
1093
+
1094
+ def cmd_list(args: argparse.Namespace) -> int:
1095
+ """List repos at or above a milestone threshold."""
1096
+
1097
+ S.intro("Scientist World")
1098
+ S.step_start(f"Fetching from {S.BOLD}{args.org}{S.RESET}")
1099
+
1100
+ try:
1101
+ repos = list_repos(org=args.org)
1102
+ except RuntimeError as e:
1103
+ S.bar()
1104
+ S.step_end(S.error(str(e)))
1105
+ return 1
1106
+
1107
+ if args.min_milestone:
1108
+ repos = [r for r in repos if meets_minimum(r.milestone, args.min_milestone)]
1109
+
1110
+ repos.sort(key=lambda r: (
1111
+ -MILESTONE_ORDER.index(r.milestone) if r.milestone in MILESTONE_ORDER else 999,
1112
+ r.arxiv_id,
1113
+ ))
1114
+
1115
+ S.bar()
1116
+ S.step_done(f"Found {S.BOLD}{len(repos)}{S.RESET} repos")
1117
+
1118
+ if not repos:
1119
+ S.bar()
1120
+ S.step_end(f"{S.DIM}No repos match the criteria.{S.RESET}")
1121
+ print()
1122
+ return 0
1123
+
1124
+ print()
1125
+ S.table_header(("ARXIV ID", 16), ("MILESTONE", 24), ("TITLE", 50))
1126
+
1127
+ for r in repos:
1128
+ title = r.title[:48] + "…" if len(r.title) > 48 else r.title
1129
+ ms = S.colored_milestone(r.milestone)
1130
+ S.table_row([f"{S.BRIGHT_WHITE}{r.arxiv_id}{S.RESET}", ms, f"{S.DIM}{title}{S.RESET}"], [16, 24, 50])
1131
+
1132
+ print()
1133
+ print(f" {S.DIM}{len(repos)} repos{S.RESET}")
1134
+ print()
1135
+ return 0
1136
+
1137
+
1138
+ # ── sw create ─────────────────────────────────────────────
1139
+
1140
+ def cmd_create(args: argparse.Namespace) -> int:
1141
+ """Create an environment from a repo."""
1142
+ arxiv_id = args.arxiv_id
1143
+ mode = args.mode
1144
+ output = Path(args.output)
1145
+
1146
+ mode_color = S.BRIGHT_MAGENTA if mode == "improve" else S.BRIGHT_CYAN
1147
+
1148
+ S.intro("Scientist World")
1149
+ S.step_start("Create environment")
1150
+ S.bar(f"{S.DIM}paper{S.RESET} {arxiv_id}")
1151
+ S.bar(f"{S.DIM}mode{S.RESET} {mode_color}{mode}{S.RESET}")
1152
+ S.bar(f"{S.DIM}output{S.RESET} {output}")
1153
+
1154
+ if output.exists():
1155
+ S.bar()
1156
+ S.step_end(S.error(f"Output path already exists: {output}"))
1157
+ return 1
1158
+
1159
+ S.bar()
1160
+ S.step_start(f"Looking up {S.BOLD}{arxiv_id}{S.RESET} in {S.BOLD}{args.org}{S.RESET}")
1161
+
1162
+ try:
1163
+ repos = list_repos(org=args.org)
1164
+ except RuntimeError as e:
1165
+ S.bar()
1166
+ S.step_end(S.error(str(e)))
1167
+ return 1
1168
+
1169
+ matching = [r for r in repos if r.arxiv_id == arxiv_id]
1170
+ if not matching:
1171
+ S.bar()
1172
+ S.step_end(S.error(f"No repo found for {arxiv_id} in {args.org}"))
1173
+ return 1
1174
+
1175
+ repo = matching[0]
1176
+ S.bar()
1177
+ S.step_done(f"Found {S.BOLD}{repo.repo_name}{S.RESET} {S.colored_milestone(repo.milestone)}")
1178
+
1179
+ token = _get_github_token()
1180
+ tmp_dir = Path(tempfile.mkdtemp(prefix="sciworld_"))
1181
+ clone_dir = tmp_dir / "repo"
1182
+
1183
+ try:
1184
+ S.bar()
1185
+ S.step_start("Cloning")
1186
+ clone_repo(repo.clone_url, clone_dir, token=token)
1187
+ S.step_done("Cloned")
1188
+
1189
+ S.bar()
1190
+ S.step_start(f"Packaging {mode_color}{mode}{S.RESET} environment")
1191
+ package_environment(clone_dir, output, mode=mode)
1192
+ S.step_done("Packaged")
1193
+
1194
+ S.bar()
1195
+ S.step_end(S.success(f"Ready at {S.BOLD}{output}{S.RESET}"))
1196
+ print()
1197
+ return 0
1198
+
1199
+ except Exception as e:
1200
+ S.bar()
1201
+ S.step_end(S.error(str(e)))
1202
+ if output.exists():
1203
+ shutil.rmtree(str(output), ignore_errors=True)
1204
+ return 1
1205
+
1206
+ finally:
1207
+ shutil.rmtree(str(tmp_dir), ignore_errors=True)
1208
+
1209
+
1210
+ # ── main ──────────────────────────────────────────────────
1211
+
1212
+ def main(argv: list[str] | None = None) -> int:
1213
+ try:
1214
+ return _main(argv)
1215
+ except KeyboardInterrupt:
1216
+ print(f"\n {S.DIM}Interrupted.{S.RESET}\n")
1217
+ return 130
1218
+
1219
+
1220
+ def _main(argv: list[str] | None = None) -> int:
1221
+ cfg = load_config()
1222
+
1223
+ parser = argparse.ArgumentParser(
1224
+ prog="sciworld",
1225
+ description="Scientist World — research environments from papers at scale.",
1226
+ )
1227
+ parser.add_argument(
1228
+ "--org", default=cfg.get("org", DEFAULT_ORG),
1229
+ help=f"GitHub organization (default: {cfg.get('org', DEFAULT_ORG)})",
1230
+ )
1231
+ parser.add_argument(
1232
+ "-v", "--verbose", action="store_true",
1233
+ help="Enable debug logging",
1234
+ )
1235
+ sub = parser.add_subparsers(dest="command")
1236
+
1237
+ # --- init ---
1238
+ p_init = sub.add_parser(
1239
+ "init",
1240
+ help="Initialize Scientist World configuration",
1241
+ description="Set up ~/.sciworld with environments directory, compute instructions, and system prompt.",
1242
+ )
1243
+ p_init.add_argument("--force", action="store_true",
1244
+ help="Re-run setup even if already initialized (edit existing values)")
1245
+ p_init.add_argument("--org", default=None,
1246
+ help="Set the default GitHub organization (default: ScientistWorld)")
1247
+
1248
+ # --- config ---
1249
+ p_config = sub.add_parser(
1250
+ "config",
1251
+ help="View or update configuration",
1252
+ description="View all settings with 'list', clear with 'reset', or edit a section directly.",
1253
+ )
1254
+ p_config.add_argument(
1255
+ "section", nargs="?", default=None,
1256
+ choices=["list", "reset"] + list(_CONFIG_SECTIONS),
1257
+ help="Action: list (show all), reset (clear all), paths, compute, or prompt",
1258
+ )
1259
+
1260
+ # --- select ---
1261
+ p_select = sub.add_parser(
1262
+ "select",
1263
+ help="Select a paper environment to work on",
1264
+ description=(
1265
+ "Browse and clone a paper environment. "
1266
+ "Interactive by default (dice roll, search, or paginated list). "
1267
+ "Use --id for direct selection without the interactive flow."
1268
+ ),
1269
+ )
1270
+ p_select.add_argument("--id", dest="paper_id", metavar="ARXIV_ID",
1271
+ help="Select a paper directly by arXiv ID (e.g. 2505.19252)")
1272
+ p_select.add_argument("--path", dest="dest_path", metavar="DIR",
1273
+ help="Override the clone destination (default: <data_dir>/envs/<id>/source)")
1274
+ p_select.add_argument("--refresh", action="store_true",
1275
+ help="Bypass cache and fetch fresh data from GitHub")
1276
+
1277
+ # --- research ---
1278
+ p_research = sub.add_parser(
1279
+ "research",
1280
+ help="Set up a research environment for a paper",
1281
+ description=(
1282
+ "Select a paper, choose a mode (improve/scratch), and build a "
1283
+ "ready-to-go research workspace with template, config, and evaluation."
1284
+ ),
1285
+ )
1286
+ p_research.add_argument("--id", dest="paper_id", metavar="ARXIV_ID",
1287
+ help="Select a paper directly by arXiv ID (e.g. 2505.19252)")
1288
+ p_research.add_argument("--mode", choices=["improve", "scratch"],
1289
+ help="Research mode (default: interactive prompt)")
1290
+ p_research.add_argument("--agent", choices=["claude-code", "codex", "gemini", "kimi"],
1291
+ help="Agent to run (default: interactive prompt)")
1292
+ p_research.add_argument("--tag", metavar="TAG",
1293
+ help="Run tag for multiple runs of the same agent/mode (e.g. v2, ablation)")
1294
+ p_research.add_argument("--path", dest="dest_path", metavar="DIR",
1295
+ help="Override the full environment destination path")
1296
+ p_research.add_argument("--setup-only", action="store_true",
1297
+ help="Build the environment folder only — no launcher, no auto-launch")
1298
+ p_research.add_argument("--refresh", action="store_true",
1299
+ help="Bypass cache and fetch fresh data from GitHub")
1300
+ p_research.add_argument("--loop", action="store_true",
1301
+ help="Resume the agent session with a short follow-up prompt "
1302
+ "until status.json is marked done (or --max-loops is hit)")
1303
+ p_research.add_argument("--max-loops", type=int, default=10, metavar="N",
1304
+ help="Max iterations when --loop is set (default: 10)")
1305
+ # Hidden: debug/manual-run option. Writes a standalone launch.sh sibling
1306
+ # of the env folder (full pipeline, prompts inlined) but does not auto-
1307
+ # launch. Intentionally not advertised in --help.
1308
+ p_research.add_argument("--launcher", action="store_true",
1309
+ help=argparse.SUPPRESS)
1310
+
1311
+ # --- info ---
1312
+ p_info = sub.add_parser(
1313
+ "info",
1314
+ help="Show paper information (overview, problem, experiments)",
1315
+ description=(
1316
+ "Reads markdowns from <data_dir>/envs/<arxiv_id>/source/ and shows "
1317
+ "the paper's metadata, TL;DR, summary, problem, and experiments. "
1318
+ "Use --full to also show method, targets, constraints, and evaluation."
1319
+ ),
1320
+ )
1321
+ p_info.add_argument("--id", dest="arxiv_id", metavar="ARXIV_ID",
1322
+ required=True,
1323
+ help="arXiv paper ID (e.g. 2505.19252)")
1324
+ p_info.add_argument("--full", action="store_true",
1325
+ help="Show all sections (method, targets, constraints, evaluation)")
1326
+
1327
+ # --- evaluate ---
1328
+ p_eval = sub.add_parser(
1329
+ "evaluate",
1330
+ aliases=["eval"],
1331
+ help="Compare a paper's workspace scores to the paper's reference",
1332
+ description=(
1333
+ "Given an arXiv ID, find the workspace under <data_dir>/envs/<id>/ "
1334
+ "and compare its scoring/scores.json against the paper's "
1335
+ "scoring/reference.json. If multiple runs exist, prompts to pick one."
1336
+ ),
1337
+ )
1338
+ p_eval.add_argument("--id", dest="arxiv_id", metavar="ARXIV_ID",
1339
+ required=True,
1340
+ help="arXiv paper ID (e.g. 2505.19252)")
1341
+ p_eval.add_argument("--run", metavar="NAME",
1342
+ help="Run folder name (e.g. claude-code-improve). "
1343
+ "If omitted and multiple runs exist, prompts.")
1344
+
1345
+ # --- create (legacy) ---
1346
+ p_create = sub.add_parser("create", help="Create an environment from a repo")
1347
+ p_create.add_argument("arxiv_id", help="arXiv paper ID (e.g. 2512.01557)")
1348
+ p_create.add_argument(
1349
+ "--mode", required=True,
1350
+ choices=["improve", "scratch"],
1351
+ help="Environment mode",
1352
+ )
1353
+ p_create.add_argument(
1354
+ "--output", "-o", required=True,
1355
+ help="Output directory path",
1356
+ )
1357
+
1358
+ args = parser.parse_args(argv)
1359
+
1360
+ if args.verbose:
1361
+ logging.basicConfig(level=logging.DEBUG)
1362
+
1363
+ if args.command == "init":
1364
+ return cmd_init(args)
1365
+ elif args.command == "config":
1366
+ return cmd_config(args)
1367
+ # elif args.command == "list":
1368
+ # return cmd_list(args)
1369
+ elif args.command == "select":
1370
+ from sciworld.select_cmd import cmd_select
1371
+ return cmd_select(args)
1372
+ elif args.command == "research":
1373
+ from sciworld.research_cmd import cmd_research
1374
+ return cmd_research(args)
1375
+ elif args.command in ("evaluate", "eval"):
1376
+ from sciworld.evaluate_cmd import cmd_evaluate
1377
+ return cmd_evaluate(args)
1378
+ elif args.command == "info":
1379
+ from sciworld.info_cmd import cmd_info
1380
+ return cmd_info(args)
1381
+ elif args.command == "create":
1382
+ return cmd_create(args)
1383
+ else:
1384
+ S.intro(
1385
+ "Scientist World",
1386
+ "Research environments from papers at scale.",
1387
+ )
1388
+ parser.print_help()
1389
+ print()
1390
+ return 0
1391
+
1392
+
1393
+ if __name__ == "__main__":
1394
+ try:
1395
+ sys.exit(main())
1396
+ except KeyboardInterrupt:
1397
+ print(f"\n {S.DIM}Interrupted.{S.RESET}\n")
1398
+ sys.exit(130)