-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
1755 lines (1478 loc) · 65 KB
/
Copy pathcli.py
File metadata and controls
1755 lines (1478 loc) · 65 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""AI Coding Gym CLI - main entry point.
A command-line tool for the AI Coding Gym platform (https://aicodinggym.com).
Supports SWE-bench, MLE-bench, and Code Review challenges.
SETUP (required before any other command):
aicodinggym configure --user-id YOUR_USER_ID
SWE-BENCH WORKFLOW:
aicodinggym swe fetch django__django-10097
# ... edit code to fix the issue ...
aicodinggym swe submit django__django-10097
MLE-BENCH WORKFLOW:
aicodinggym mle download spaceship-titanic
# ... train model, generate predictions ...
aicodinggym mle submit spaceship-titanic -F submission.csv
CODE REVIEW WORKFLOW:
aicodinggym cr fetch sentry-0001
# ... review the diff and write your review ...
aicodinggym cr submit sentry-0001 -f review.md
"""
import json
import os
import platform
import re
import subprocess
import sys
import urllib.request
from datetime import datetime
from pathlib import Path
import click
from . import __version__
from . import entire_logging
from .api import (
APIError,
configure as api_configure,
cr_submit_review,
fetch_pr as api_fetch_pr,
fetch_problem as api_fetch_problem,
mlebench_download_file,
mlebench_download_open,
mlebench_submit_csv,
submit_notification,
)
from .config import (
get_logging_consent,
load_config,
load_credentials,
save_config,
save_credentials,
set_logging_consent,
)
from .git_ops import (
add_commit_push,
check_tool_installed,
clone_repo,
clone_repo_cr,
generate_ssh_key_pair,
reset_to_setup_commit,
restore_branch,
run_git_command,
)
def _hyperlink(url: str, text: str | None = None) -> str:
"""Return an OSC 8 terminal hyperlink. Falls back to plain URL on unsupported terminals."""
label = text or url
return f"\033]8;;{url}\033\\{label}\033]8;;\033\\"
def _error(msg: str) -> None:
"""Print an error message to stderr and exit."""
click.echo(f"Error: {msg}", err=True)
sys.exit(1)
def _warn(msg: str) -> None:
"""Print a warning message to stderr."""
click.echo(f"Warning: {msg}", err=True)
_GYM_ENV_API = "https://api.github.com/repos/AICodingGym/gym-environment/contents"
_GYM_ENV_SKIP = {"README.md"}
def _download_directory(api_url: str, dest_dir: Path) -> None:
"""Recursively download all files from a GitHub API directory listing."""
try:
req = urllib.request.Request(api_url, headers={"Accept": "application/vnd.github.v3+json"})
with urllib.request.urlopen(req, timeout=15) as r:
entries = json.loads(r.read())
except Exception as e:
_warn(f"Failed to list directory {dest_dir.name}: {e}")
return
dest_dir.mkdir(parents=True, exist_ok=True)
for entry in entries:
name = entry.get("name", "")
etype = entry.get("type")
if etype == "file":
url = entry.get("download_url")
if not url:
continue
try:
with urllib.request.urlopen(url, timeout=15) as r:
(dest_dir / name).write_bytes(r.read())
except Exception as e:
_warn(f"Failed to download {dest_dir.name}/{name}: {e}")
elif etype == "dir":
sub_url = entry.get("url")
if sub_url:
_download_directory(sub_url, dest_dir / name)
def _install_gym_environment(dest: Path) -> None:
"""Download gym-environment files into dest and add them to .gitignore."""
try:
req = urllib.request.Request(_GYM_ENV_API, headers={"Accept": "application/vnd.github.v3+json"})
with urllib.request.urlopen(req, timeout=15) as resp:
entries = json.loads(resp.read())
except Exception as e:
_warn(f"Could not fetch gym-environment file list: {e}")
return
downloaded: list[str] = []
for entry in entries:
name = entry.get("name", "")
if name in _GYM_ENV_SKIP:
continue
etype = entry.get("type")
if etype == "file":
url = entry.get("download_url")
if not url:
continue
try:
with urllib.request.urlopen(url, timeout=15) as r:
(dest / name).write_bytes(r.read())
downloaded.append(name)
except Exception as e:
_warn(f"Failed to download {name}: {e}")
elif etype == "dir":
sub_url = entry.get("url")
if sub_url:
_download_directory(sub_url, dest / name)
downloaded.append(name)
if not downloaded:
return
# Append to .gitignore
gitignore = dest / ".gitignore"
existing = gitignore.read_text(encoding="utf-8") if gitignore.exists() else ""
existing_lines = set(existing.splitlines())
new_entries = [f for f in downloaded if f not in existing_lines and f"/{f}" not in existing_lines]
if new_entries:
block = "\n# gym-environment\n" + "\n".join(new_entries) + "\n"
with open(gitignore, "a", encoding="utf-8", newline="\n") as fh:
fh.write(block)
def _resolve_user_id(config: dict, user_id: str | None) -> str:
"""Resolve user_id from argument or config, with helpful error."""
if user_id:
return user_id
uid = config.get("user_id")
if not uid:
_error(
"User ID is not configured.\n\n"
"Run 'aicodinggym configure --user-id YOUR_USER_ID' first.\n"
"This generates an SSH key and registers it with aicodinggym.com."
)
return uid
def _resolve_workspace(config: dict, workspace_dir: str | None) -> Path:
"""Resolve workspace directory from argument or config."""
if workspace_dir:
return Path(workspace_dir).resolve()
configured = config.get("workspace_dir")
if configured:
return Path(configured).resolve()
return Path.cwd().resolve()
def _print_test_summary(lines: list[str], problem_id: str, returncode: int,
elapsed: float = 0.0) -> None:
"""Parse act output and print a clear test results summary."""
steps: list[tuple[str, str]] = [] # (status, name)
test_results: list[str] = []
failures: list[str] = []
errors: list[str] = []
ran_line = ""
for raw in lines:
line = raw.rstrip()
# Capture step results (Success/Failure lines)
m = re.search(r"(✅\s+Success|❌\s+Failure)\s+-\s+(.+?)(?:\s+\[.*\])?$", line)
if m:
status = "PASS" if "Success" in m.group(1) else "FAIL"
steps.append((status, m.group(2).strip()))
# Capture individual test results (PASS/FAIL/ERROR/ok lines)
test_m = re.search(r"\|\s+([\w_]+\s+\([\w.]+\))\s+\.\.\.\s+(ok|FAIL|ERROR)", line)
if test_m:
test_results.append(f" {test_m.group(2):>5} {test_m.group(1)}")
# Capture "Ran N tests" line
ran_m = re.search(r"Ran (\d+) tests? in (.+)", line)
if ran_m:
ran_line = ran_m.group(0)
# Capture FAIL/ERROR blocks
fail_m = re.search(r"\|\s+(FAIL|ERROR): (.+)", line)
if fail_m:
if fail_m.group(1) == "FAIL":
failures.append(fail_m.group(2))
else:
errors.append(fail_m.group(2))
click.echo("\n" + "=" * 60)
click.echo(f" TEST SUMMARY — {problem_id}")
click.echo("=" * 60)
if steps:
click.echo("\nWorkflow steps:")
for status, name in steps:
icon = "PASS" if status == "PASS" else "FAIL"
click.echo(f" [{icon}] {name}")
if test_results:
click.echo("\nTest results:")
for tr in test_results:
click.echo(tr)
if failures:
click.echo(f"\nFailed tests ({len(failures)}):")
for f in failures:
click.echo(f" - {f}")
if errors:
click.echo(f"\nErrored tests ({len(errors)}):")
for e in errors:
click.echo(f" - {e}")
if ran_line:
click.echo(f"\n{ran_line}")
if returncode == 0:
click.echo(f"\nResult: ALL TESTS PASSED")
else:
click.echo(f"\nResult: TESTS FAILED (exit code {returncode})")
if elapsed > 0:
minutes, seconds = divmod(int(elapsed), 60)
click.echo(f"Elapsed: {minutes}m {seconds}s")
click.echo("=" * 60)
def _ensure_act_config() -> None:
"""Create act config file with medium image if it doesn't exist.
Prevents act from prompting interactively on first run.
"""
if sys.platform == "win32":
actrc = Path(os.environ.get("APPDATA", Path.home() / "AppData" / "Roaming")) / "act" / "actrc"
elif sys.platform == "darwin":
actrc = Path.home() / "Library" / "Application Support" / "act" / "actrc"
else:
xdg = Path(os.environ.get("XDG_CONFIG_HOME", Path.home() / ".config"))
actrc = xdg / "act" / "actrc"
if actrc.exists():
return
actrc.parent.mkdir(parents=True, exist_ok=True)
# Medium image: ~500MB, compatible with most actions
actrc.write_text(
"-P ubuntu-latest=catthehacker/ubuntu:act-latest\n"
"-P ubuntu-22.04=catthehacker/ubuntu:act-22.04\n"
"-P ubuntu-20.04=catthehacker/ubuntu:act-20.04\n"
)
click.echo(f"Created act config at {actrc} (using medium runner images).")
def _resolve_key_path(config: dict, creds: dict | None = None) -> Path:
"""Resolve SSH private key path from credentials or config."""
path_str = None
if creds:
path_str = creds.get("private_key_path")
if not path_str:
path_str = config.get("private_key_path")
if not path_str:
_error(
"SSH key path not found.\n\n"
"Run 'aicodinggym configure --user-id YOUR_USER_ID' to generate a key.\n"
"If you previously configured, your config may be corrupted.\n"
"Config location: ~/.aicodinggym/config.json"
)
key_path = Path(path_str)
if not key_path.exists():
_error(
f"SSH key file not found at: {key_path}\n\n"
"Run 'aicodinggym configure --user-id YOUR_USER_ID' to regenerate.\n"
"This will create a new SSH key pair and register it with the server."
)
return key_path
# ── AI-session logging (Entire integration) ──────────────────────────────────
_CONSENT_PROMPT = (
"AI Coding Gym can upload this AI coding session (prompts, responses, files "
"changed) for research only. Data is de-identified/anonymized before use.\n"
"Upload your session logs on submit?"
)
def _configure_hint(user_id: str | None) -> str:
"""A copy-pasteable 'configure' command (where Entire install is offered)."""
return f"aicodinggym configure --user-id {user_id}" if user_id else "aicodinggym configure"
def _setup_logging(problem_dir: Path, *, init_git: bool = False,
user_id: str | None = None) -> bool:
"""Best-effort: install Entire hooks so the session is captured locally.
Capture is local-only; nothing is uploaded until the user consents at
submit. If Entire isn't installed, point the user at ``configure`` (which
offers to install it) rather than silently skipping — unless they've already
opted out of logging. Returns True if AI-session capture is now active.
"""
if not entire_logging.is_available():
if get_logging_consent() is not False: # not explicitly opted out
click.echo(
" Logging: Not set up — the 'entire' CLI isn't installed.\n"
f" Run '{_configure_hint(user_id)}' to enable AI workflow logging."
)
return False
ok, msg = entire_logging.setup(problem_dir, init_git=init_git)
if ok:
click.echo(f" Logging: {msg} (uploaded only with your consent on submit)")
return True
# On setup failure (Entire present but enable errored) we stay quiet.
return False
def _launch_instruction(problem_dir: Path) -> str:
"""Reminder that AI-session capture only works for an agent launched here.
Claude Code (and Codex etc.) load their capture hooks from the directory
they're started in, fixed for the whole session — cd-ing in later does not
activate them. So the agent must be launched inside the problem folder.
"""
return (
f"Please start your agent inside {problem_dir}:\n"
f" cd {problem_dir}\n"
" claude # or codex / your AI agent\n"
"(Capture only works for an agent launched here — not one you cd into later.)"
)
def _safe_key_path(config: dict, creds: dict | None = None) -> Path | None:
"""Resolve the SSH key for a log push without exiting if it's missing.
Unlike :func:`_resolve_key_path`, this returns None instead of aborting, so
an optional log upload never kills a submit that already succeeded.
"""
path_str = (creds or {}).get("private_key_path") or config.get("private_key_path")
if not path_str:
return None
key_path = Path(path_str)
return key_path if key_path.exists() else None
def _resolve_log_upload_consent(flag: bool | None) -> bool:
"""Resolve whether to upload session logs, prompting once if needed.
Precedence: explicit --upload-logs/--no-upload-logs flag > stored consent >
first-time prompt. In non-interactive sessions with no recorded choice we
default to NOT uploading (privacy-safe) and print how to opt in.
"""
if flag is not None:
set_logging_consent(flag)
return flag
stored = get_logging_consent()
if stored is not None:
return stored
if not sys.stdin.isatty():
click.echo(
"AI session captured locally; upload skipped (no consent on record).\n"
" Opt in for research (de-identified): aicodinggym configure --upload-logs"
)
return False
answer = click.confirm("\n" + _CONSENT_PROMPT, default=False)
set_logging_consent(answer)
return answer
def _resolve_logs_remote(benchmark: str, creds: dict | None,
config: dict, override: str | None) -> str | None:
"""Resolve the writable git URL to push session logs to.
All benchmarks log to the user's single repo (one repo, many branches),
distinguished by the per-problem log branch name. SWE may fall back to its
own writable clone URL when the submission repo isn't recorded yet; CR's
cloned repo is the read-only PR and must never be used.
"""
if override:
return override
env = os.environ.get("AICODINGGYM_LOGS_REMOTE")
if env:
return env
if config.get("submission_repo_url"):
return config["submission_repo_url"]
if benchmark == "swe":
return (creds or {}).get("repo_url")
return None
def _logging_status(thunk) -> str:
"""Run a logging step before the success banner, never suppressing it.
The submission already succeeded by the time logging runs, so any unexpected
error here must not hide the banner — degrade to a warning + empty status.
"""
try:
return thunk()
except Exception as e: # noqa: BLE001
_warn(f"AI session logging skipped due to an error: {e}")
return ""
def _maybe_upload_logs(problem_dir: Path, *, benchmark: str, problem_id: str,
user_id: str, key_path: Path | None, config: dict,
creds: dict | None, upload_flag: bool | None,
logs_remote_override: str | None, tool: str | None = None,
flush: bool = False) -> str:
"""Consent-gated upload of the captured AI session at submit time.
Returns a status line (or "") to embed in the caller's success summary. Any
interactive consent prompt happens here, so callers should invoke this
*before* printing their banner. Hard failures are warned to stderr.
"""
if not entire_logging.is_available():
if get_logging_consent() is not False: # not explicitly opted out
return (
" Logs: Not captured — the 'entire' CLI isn't installed.\n"
f" Run '{_configure_hint(user_id)}' to enable logging next time."
)
return ""
if not entire_logging.is_enabled(problem_dir):
return "" # repo wasn't set up for capture (e.g. fetched before install)
if flush:
entire_logging.flush(problem_dir)
if not entire_logging.has_sessions(problem_dir):
return "" # nothing was captured — stay quiet
if not _resolve_log_upload_consent(upload_flag):
return " Logs: AI session captured locally; upload skipped."
remote = _resolve_logs_remote(benchmark, creds, config, logs_remote_override)
if not remote:
return (
" Logs: consented, but no logs repository is configured.\n"
" Re-run 'aicodinggym configure' or pass --logs-remote URL."
)
ok, info = entire_logging.upload(
problem_dir, remote_url=remote, benchmark=benchmark, problem_id=problem_id,
user_id=user_id, key_path=key_path, tool=tool, cli_version=__version__,
)
if ok:
return f" Logs: uploaded for research (branch {info})"
_warn(f"AI session log upload failed: {info}")
return ""
def _maybe_submit_mle_artifacts(workspace_repo: Path, *, competition_id: str,
user_id: str, config: dict, key_path: Path | None,
upload_flag: bool | None,
logs_remote_override: str | None) -> str:
"""MLE submit: always push the user's solution code to their submission repo,
and — only with consent — also upload the captured AI session logs.
Pushing the code is the whole point of ``submit`` and it goes to the user's
*own* repo, so it is NOT gated on research-log consent; only the
de-identified AI session upload is. The code lands on a stable branch named
after the competition (overwritten each submit, so ``mle restore`` has a
predictable name to pull); logs land on a unique per-submission branch so
prior sessions are never overwritten. The CSV itself already went to the
API. Returns a status block (or "").
"""
workspace_repo = Path(workspace_repo)
if not (workspace_repo / ".git").exists():
return "" # workspace was never initialised (e.g. downloaded pre-upgrade)
# Commit the solution code locally. With Entire enabled, this commit also
# captures the AI session checkpoint (commit_linking is always on) — no
# separate flush needed. The session is uploaded only with consent, below.
entire_logging.commit_workspace(workspace_repo, f"MLE submission: {competition_id}")
remote = _resolve_logs_remote("mle", None, config, logs_remote_override)
if not remote:
return (
" Code: committed locally, but no submission repository is configured.\n"
" Re-run 'aicodinggym configure' or pass --logs-remote URL."
)
lines: list[str] = []
# 1) Always push the solution code — it's the user's own repo and the point
# of submitting; independent of research-log consent.
code_ok, code_info = entire_logging.push_branch(
workspace_repo, remote_url=remote, dest_branch=competition_id,
key_path=key_path, force=True,
)
if code_ok:
lines.append(f" Code: pushed to branch '{code_info}'")
lines.append(f" Restore later with: aicodinggym mle restore {competition_id}")
else:
_warn(f"MLE code push failed: {code_info}")
# 2) Upload the de-identified AI session logs only with consent.
if not entire_logging.is_available():
lines.append(" Logs: AI session not captured ('entire' not installed).")
lines.append(f" Run '{_configure_hint(user_id)}' to enable logging next time.")
elif not (entire_logging.is_enabled(workspace_repo)
and entire_logging.has_sessions(workspace_repo)):
pass # no AI session was captured — stay quiet
elif not _resolve_log_upload_consent(upload_flag):
lines.append(" Logs: AI session captured locally; upload skipped.")
else:
ok, info = entire_logging.upload(
workspace_repo, remote_url=remote, benchmark="mle", problem_id=competition_id,
user_id=user_id, key_path=key_path, cli_version=__version__,
submission_stamp=entire_logging.new_stamp(),
)
if ok:
lines.append(f" Logs: uploaded for research (branch {info})")
else:
_warn(f"AI session log upload failed: {info}")
return "\n".join(lines)
def _configure_logging(upload_logs_flag: bool | None) -> None:
"""During configure: offer to install Entire, then record upload consent.
Consent is resolved here — where a human is reliably at the keyboard —
rather than only at submit. Later submits are often non-interactive (e.g.
driven by an AI agent, where stdin is not a TTY); recording the choice now
gives them an answer to act on instead of silently skipping the upload.
"""
if entire_logging.is_available():
ver = entire_logging.version()
click.echo(f" Logging: Entire detected ({ver or 'installed'})")
else:
click.echo(
"\nOptional — AI workflow logging:\n"
" AI Coding Gym can capture your AI coding sessions (via Entire,\n"
" https://entire.io) and, only with your consent, upload them for\n"
" research. Uploaded data is de-identified/anonymized. Needs the\n"
" 'entire' CLI."
)
if not sys.stdin.isatty():
click.echo(f" Install later with:\n {entire_logging.INSTALL_COMMAND}")
elif click.confirm("Install the Entire CLI now?", default=True):
click.echo("Installing Entire (downloading a binary; this may take a minute)...")
ok, msg = entire_logging.install()
if ok:
click.echo(f" Entire: {msg}")
else:
_warn(
f"Could not install Entire automatically: {msg}\n"
f" Install manually: {entire_logging.INSTALL_COMMAND}"
)
else:
click.echo(f" Skipped. Install later with:\n {entire_logging.INSTALL_COMMAND}")
# Record the standing upload consent now. An explicit flag wins; otherwise
# ask once (only with a TTY and no prior choice) so non-interactive submits
# have an answer. The submit-time prompt remains as a fallback for anyone who
# configured non-interactively.
if upload_logs_flag is not None:
set_logging_consent(upload_logs_flag)
elif get_logging_consent() is None and sys.stdin.isatty():
granted = click.confirm("\n" + _CONSENT_PROMPT, default=False)
set_logging_consent(granted)
click.echo(
" Consent recorded — change it anytime with\n"
" 'aicodinggym configure --upload-logs' (or --no-upload-logs)."
)
# ── Top-level group ──────────────────────────────────────────────────────────
@click.group(
epilog=(
"\b\n"
"SETUP (run once before using other commands):\n"
" aicodinggym configure --user-id YOUR_USER_ID\n"
" (user_id is required — get yours at https://aicodinggym.com)\n\n"
"\b\n"
"EXAMPLES:\n"
" aicodinggym swe fetch django__django-10097\n"
" aicodinggym swe submit django__django-10097 --message 'Fix auth bug'\n"
" aicodinggym mle download spaceship-titanic\n"
" aicodinggym mle submit spaceship-titanic -F predictions.csv\n"
" aicodinggym cr fetch sentry-0001\n"
" aicodinggym cr submit sentry-0001 -f review.md\n\n"
"\b\n"
"WEBSITE:\n"
" https://aicodinggym.com\n"
),
)
@click.version_option(__version__, prog_name="aicodinggym")
def main():
"""AI Coding Gym CLI.
A command-line interface for the AI Coding Gym platform
(https://aicodinggym.com). Provides tools to fetch coding problems,
download datasets, and submit solutions.
Designed for use by both humans and LLM/AI agents.
\b
QUICK START:
1. Configure: aicodinggym configure --user-id YOUR_USER_ID
2. Fetch: aicodinggym swe fetch PROBLEM_ID
3. Solve: (edit code in the cloned repository)
4. Submit: aicodinggym swe submit PROBLEM_ID
"""
pass
# ── configure ────────────────────────────────────────────────────────────────
@main.command()
@click.option(
"--user-id", required=True,
help="Your AI Coding Gym user ID. Get one at https://aicodinggym.com.",
)
@click.option(
"--workspace-dir", default=None, type=click.Path(),
help="Default workspace directory for cloning repositories. "
"Defaults to the current working directory.",
)
@click.option(
"--upload-logs/--no-upload-logs", "upload_logs", default=None,
help="Pre-set consent for uploading de-identified AI session logs for "
"research (otherwise you're asked once, on your first submit).",
)
def configure(user_id: str, workspace_dir: str | None, upload_logs: bool | None):
"""Configure credentials and register SSH key with aicodinggym.com.
Generates an SSH key pair locally (stored in ~/.aicodinggym/),
sends the public key to the server, and saves your configuration.
\b
This command must be run once before using any other commands.
If you've already configured, running again will reuse your existing key.
\b
WHAT IT DOES:
1. Generates SSH key pair in ~/.aicodinggym/
2. Registers your public key with the AI Coding Gym server
3. Receives your assigned repository name
4. Saves all settings to ~/.aicodinggym/config.json
\b
EXAMPLE:
aicodinggym configure --user-id alice123
aicodinggym configure --user-id alice123 --workspace-dir ~/gym-workspace
"""
try:
click.echo(f"Generating SSH key for user '{user_id}'...")
private_key_path, public_key = generate_ssh_key_pair(user_id)
click.echo("Registering public key with aicodinggym.com...")
existing = load_config()
submission_repo_url = existing.get("submission_repo_url")
try:
data = api_configure(user_id, public_key)
repo_name = data.get("repo_name")
if not repo_name:
_error("Server did not return a repository name. Please try again or contact support.")
# Writable repo we can push AI-session logs to (CR/MLE upload target).
submission_repo_url = data.get("repo_url") or submission_repo_url
except APIError as e:
if "409" in str(e):
click.echo("Key already registered, reusing existing configuration.")
repo_name = existing.get("repo_name", f"submission-{user_id}")
else:
raise
# Only change the workspace when --workspace-dir is explicitly given.
# On re-configure, preserve the previously configured workspace instead
# of silently relocating it to wherever 'configure' happened to run;
# fall back to the current directory only on first-time setup.
if workspace_dir:
resolved_workspace = str(Path(workspace_dir).resolve())
else:
resolved_workspace = existing.get("workspace_dir") or str(Path.cwd().resolve())
config = {
"user_id": user_id,
"repo_name": repo_name,
"private_key_path": str(private_key_path),
"workspace_dir": resolved_workspace,
}
if submission_repo_url:
config["submission_repo_url"] = submission_repo_url
save_config(config)
_install_gym_environment(Path(resolved_workspace))
click.echo(
f"\nConfiguration saved successfully!\n"
f"\n"
f" User ID: {user_id}\n"
f" Repository: {repo_name}\n"
f" Workspace: {resolved_workspace}\n"
f" SSH Key: {private_key_path}\n"
f" Config: ~/.aicodinggym/config.json"
)
_configure_logging(upload_logs)
click.echo(
"\nYou can now use 'aicodinggym swe', 'aicodinggym mle', and 'aicodinggym cr' commands."
)
except APIError as e:
_error(str(e))
except Exception as e:
_error(f"Configuration failed: {e}")
# ── swe group ────────────────────────────────────────────────────────────────
@main.group()
def swe():
"""SWE-bench coding challenges - fetch, solve, and submit bug fixes.
\b
PREREQUISITE:
Run 'aicodinggym configure --user-id YOUR_USER_ID' before using these commands.
\b
WORKFLOW:
1. aicodinggym swe fetch PROBLEM_ID # Clone the problem repo
2. (edit code to fix the issue) # Work on your solution
3. aicodinggym swe submit PROBLEM_ID # Submit your fix
4. aicodinggym swe reset PROBLEM_ID # (optional) Start over
\b
PROBLEM IDS:
Problem IDs follow the format: <project>__<repo>-<number>
Examples: django__django-10097, sympy__sympy-13043, scikit-learn__scikit-learn-11578
"""
pass
@swe.command("fetch")
@click.argument("problem_id")
@click.option("--user-id", default=None, help="Override configured user ID.")
@click.option(
"--workspace-dir", default=None, type=click.Path(),
help="Directory to clone into. Overrides configured workspace.",
)
def swe_fetch(problem_id: str, user_id: str | None, workspace_dir: str | None):
"""Fetch a SWE-bench problem and clone its repository locally.
Contacts the AI Coding Gym server to set up the problem branch,
then clones the repository into your workspace directory.
\b
PREREQUISITE:
You must run 'aicodinggym configure --user-id YOUR_USER_ID' first.
If you haven't configured yet, this command will fail with instructions.
\b
ARGUMENTS:
PROBLEM_ID The unique problem identifier (e.g., 'django__django-10097').
Get problem IDs from https://aicodinggym.com.
\b
WHAT IT DOES:
1. Requests the problem branch from the server
2. Clones the repository (shallow clone for efficiency)
3. Sets up your local workspace at <workspace>/<problem_id>/
\b
EXAMPLE:
aicodinggym swe fetch django__django-10097
aicodinggym swe fetch django__django-10097 --workspace-dir ~/projects
"""
config = load_config()
uid = _resolve_user_id(config, user_id)
workspace = _resolve_workspace(config, workspace_dir)
key_path = _resolve_key_path(config)
try:
click.echo(f"Fetching problem '{problem_id}' from server...")
data = api_fetch_problem(uid, problem_id)
except APIError as e:
_error(str(e))
branch = data.get("branch_name", problem_id)
repo_url = data.get("repo_url")
server_msg = data.get("message", "")
if not repo_url:
_error("Server did not return a repository URL. The problem may not exist.")
# Save credentials for later submit
credentials = load_credentials()
credentials[problem_id] = {
"repo_url": repo_url,
"branch": branch,
"user_id": uid,
"private_key_path": str(key_path),
"workspace_dir": str(workspace),
"benchmark": "swe",
}
save_credentials(credentials)
workspace.mkdir(parents=True, exist_ok=True)
click.echo(f"Cloning branch '{branch}' into {workspace / problem_id}...")
success, msg = clone_repo(repo_url, branch, problem_id, str(workspace), key_path)
if not success:
_error(msg)
# Record the user's writable repo (one repo, many branches) so CR/MLE logs
# can target it too. Only set if not already configured.
if repo_url and not config.get("submission_repo_url"):
config["submission_repo_url"] = repo_url
save_config(config)
_install_gym_environment(workspace / problem_id)
capture_active = _setup_logging(workspace / problem_id, user_id=uid)
click.echo(
f"\nSuccessfully fetched problem: {problem_id}\n"
f"\n"
f" {msg}\n"
)
if server_msg:
click.echo(f" Server: {server_msg}\n")
click.echo("You can now start working on the solution!")
if capture_active:
click.echo("\n" + _launch_instruction(workspace / problem_id))
@swe.command("submit")
@click.argument("problem_id")
@click.option("--user-id", default=None, help="Override configured user ID.")
@click.option(
"--message", "-m", default=None,
help="Commit message. Auto-generated if not provided.",
)
@click.option(
"--force", is_flag=True, default=False,
help="Force push (--force-with-lease). Use with caution.",
)
@click.option(
"--workspace-dir", default=None, type=click.Path(),
help="Workspace directory. Overrides configured/cached value.",
)
@click.option(
"--upload-logs/--no-upload-logs", "upload_logs", default=None,
help="Override consent for uploading de-identified AI session logs.",
)
@click.option(
"--logs-remote", default=None,
help="Git URL to push AI session logs to (defaults to this problem's repo).",
)
def swe_submit(problem_id: str, user_id: str | None, message: str | None,
force: bool, workspace_dir: str | None,
upload_logs: bool | None, logs_remote: str | None):
"""Submit your SWE-bench solution by committing and pushing changes.
Stages all changes, commits them, pushes to the remote, and notifies
the AI Coding Gym server that your submission is ready for evaluation.
\b
PREREQUISITE:
You must run 'aicodinggym swe fetch PROBLEM_ID' first.
The fetch command sets up the repository and caches credentials
needed for submission.
\b
ARGUMENTS:
PROBLEM_ID The problem identifier you fetched earlier.
\b
WHAT IT DOES:
1. Stages all changed files (git add)
2. Commits with your message (or auto-generated one)
3. Pushes to the remote branch
4. Notifies the backend for evaluation
\b
EXAMPLE:
aicodinggym swe submit django__django-10097
aicodinggym swe submit django__django-10097 -m "Fix auth validation bug"
aicodinggym swe submit django__django-10097 --force
"""
config = load_config()
uid = _resolve_user_id(config, user_id)
credentials = load_credentials()
if problem_id not in credentials:
_error(
f"No credentials found for '{problem_id}'.\n\n"
f"You must fetch the problem first:\n"
f" aicodinggym swe fetch {problem_id}\n\n"
f"This clones the repository and saves the credentials needed for submission."
)
creds = credentials[problem_id]
if creds.get("user_id") and creds["user_id"] != uid:
_error(
f"User ID mismatch. Problem was fetched by '{creds['user_id']}', not '{uid}'.\n"
f"Either use --user-id {creds['user_id']} or re-fetch the problem."
)
workspace = _resolve_workspace(config, workspace_dir or creds.get("workspace_dir"))
problem_dir = workspace / problem_id
if not problem_dir.exists():
_error(
f"Problem directory not found at: {problem_dir}\n\n"
f"You must fetch the problem first:\n"
f" aicodinggym swe fetch {problem_id}\n\n"
f"Or specify the correct workspace with --workspace-dir."
)
key_path = _resolve_key_path(config, creds)
branch = creds["branch"]
commit_msg = message or f"Solution submission for {problem_id} at {datetime.now().isoformat()}"
click.echo(f"Submitting solution for '{problem_id}'...")
success, msg, commit_hash = add_commit_push(str(problem_dir), branch, key_path, commit_msg, force)
if not success:
_error(msg)
# Notify backend
try:
submit_notification(
problem_id=problem_id,
user_id=uid,
commit_hash=commit_hash,
branch=branch,
commit_message=commit_msg,
timestamp=datetime.now().isoformat(),
)
except APIError as e:
_warn(f"Changes pushed, but failed to notify backend: {e}")
# Resolve logging (incl. any consent prompt) before the summary so the
# prompt never interrupts the success banner. The solution commit above
# already triggered Entire's checkpoint, so no flush is needed.
log_status = _logging_status(lambda: _maybe_upload_logs(
problem_dir, benchmark="swe", problem_id=problem_id, user_id=uid,
key_path=key_path, config=config, creds=creds, upload_flag=upload_logs,
logs_remote_override=logs_remote,
))
summary = [
f"\nSuccessfully submitted solution for {problem_id}\n",
f" Commit: {commit_hash[:8]}",
f" Branch: {branch}",
f" Status: Pushed and backend notified",
]
if log_status:
summary.append(log_status)
summary.append("")
summary.append(f"View results at: {_hyperlink(f'https://aicodinggym.com/challenges/swe/{problem_id}')}")
click.echo("\n".join(summary))