|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import argparse |
| 4 | +from typing import Callable, Sequence |
| 5 | + |
| 6 | +from agentlab.agents.claude_code import ClaudeCodeAdapter, ClaudeCodeConfig |
| 7 | +from agentlab.agents.codex_cli import CodexCliAdapter, CodexCliConfig |
| 8 | +from agentlab.agents.manual import ManualAgentAdapter |
| 9 | + |
| 10 | + |
| 11 | +AGENT_CHOICES = ("manual", "codex", "claude") |
| 12 | +PREFLIGHT_AGENT_CHOICES = ("codex", "claude") |
| 13 | + |
| 14 | + |
| 15 | +def add_agent_argument( |
| 16 | + parser: argparse.ArgumentParser, |
| 17 | + *, |
| 18 | + default: str, |
| 19 | + choices: Sequence[str], |
| 20 | + help: str, |
| 21 | +) -> None: |
| 22 | + parser.add_argument( |
| 23 | + "--agent", |
| 24 | + default=default, |
| 25 | + choices=list(choices), |
| 26 | + help=help, |
| 27 | + ) |
| 28 | + |
| 29 | + |
| 30 | +def add_agent_adapter_options( |
| 31 | + parser: argparse.ArgumentParser, |
| 32 | + *, |
| 33 | + codex_timeout_default: int, |
| 34 | + codex_timeout_help: str = "Maximum wall time for `codex exec`.", |
| 35 | + claude_timeout_default: int, |
| 36 | +) -> None: |
| 37 | + add_codex_options( |
| 38 | + parser, |
| 39 | + timeout_default=codex_timeout_default, |
| 40 | + timeout_help=codex_timeout_help, |
| 41 | + ) |
| 42 | + add_claude_options(parser, timeout_default=claude_timeout_default) |
| 43 | + |
| 44 | + |
| 45 | +def add_codex_options( |
| 46 | + parser: argparse.ArgumentParser, |
| 47 | + *, |
| 48 | + timeout_default: int, |
| 49 | + timeout_help: str, |
| 50 | +) -> None: |
| 51 | + parser.add_argument( |
| 52 | + "--codex-command", |
| 53 | + default="codex", |
| 54 | + help="Codex CLI executable to use when --agent codex.", |
| 55 | + ) |
| 56 | + parser.add_argument( |
| 57 | + "--codex-model", |
| 58 | + default=None, |
| 59 | + help="Optional model passed to `codex exec --model`.", |
| 60 | + ) |
| 61 | + parser.add_argument( |
| 62 | + "--codex-profile", |
| 63 | + default=None, |
| 64 | + help="Optional profile passed to `codex exec --profile`.", |
| 65 | + ) |
| 66 | + parser.add_argument( |
| 67 | + "--codex-sandbox", |
| 68 | + default="workspace-write", |
| 69 | + choices=["read-only", "workspace-write", "danger-full-access"], |
| 70 | + help="Sandbox mode passed to `codex exec --sandbox`.", |
| 71 | + ) |
| 72 | + parser.add_argument( |
| 73 | + "--codex-approval", |
| 74 | + default="never", |
| 75 | + choices=["untrusted", "on-failure", "on-request", "never"], |
| 76 | + help="Approval policy passed to `codex exec --ask-for-approval`.", |
| 77 | + ) |
| 78 | + parser.add_argument( |
| 79 | + "--codex-timeout-seconds", |
| 80 | + type=int, |
| 81 | + default=timeout_default, |
| 82 | + help=timeout_help, |
| 83 | + ) |
| 84 | + |
| 85 | + |
| 86 | +def add_claude_options( |
| 87 | + parser: argparse.ArgumentParser, |
| 88 | + *, |
| 89 | + timeout_default: int, |
| 90 | +) -> None: |
| 91 | + parser.add_argument( |
| 92 | + "--claude-command", |
| 93 | + default="claude", |
| 94 | + help="Claude Code CLI executable to use when --agent claude.", |
| 95 | + ) |
| 96 | + parser.add_argument( |
| 97 | + "--claude-model", |
| 98 | + default=None, |
| 99 | + help="Optional model passed to `claude --model`.", |
| 100 | + ) |
| 101 | + parser.add_argument( |
| 102 | + "--claude-permission-mode", |
| 103 | + default="acceptEdits", |
| 104 | + choices=[ |
| 105 | + "default", |
| 106 | + "acceptEdits", |
| 107 | + "plan", |
| 108 | + "auto", |
| 109 | + "dontAsk", |
| 110 | + "bypassPermissions", |
| 111 | + ], |
| 112 | + help="Permission mode passed to Claude Code.", |
| 113 | + ) |
| 114 | + parser.add_argument( |
| 115 | + "--claude-output-format", |
| 116 | + default="stream-json", |
| 117 | + choices=["text", "json", "stream-json"], |
| 118 | + help="Print-mode output format passed to Claude Code.", |
| 119 | + ) |
| 120 | + parser.add_argument( |
| 121 | + "--claude-max-turns", |
| 122 | + type=int, |
| 123 | + default=None, |
| 124 | + help="Optional maximum agentic turns for Claude Code print mode.", |
| 125 | + ) |
| 126 | + parser.add_argument( |
| 127 | + "--claude-allowed-tool", |
| 128 | + action="append", |
| 129 | + default=[], |
| 130 | + help=( |
| 131 | + "Tool permission rule passed as `--allowedTools`. Can be repeated, " |
| 132 | + "for example `Read` or `Bash(pytest *)`." |
| 133 | + ), |
| 134 | + ) |
| 135 | + parser.add_argument( |
| 136 | + "--claude-disallowed-tool", |
| 137 | + action="append", |
| 138 | + default=[], |
| 139 | + help=( |
| 140 | + "Tool permission rule passed as `--disallowedTools`. Can be " |
| 141 | + "repeated, for example `Bash(git push *)`." |
| 142 | + ), |
| 143 | + ) |
| 144 | + parser.add_argument( |
| 145 | + "--claude-timeout-seconds", |
| 146 | + type=int, |
| 147 | + default=timeout_default, |
| 148 | + help="Maximum wall time for `claude -p`.", |
| 149 | + ) |
| 150 | + parser.add_argument( |
| 151 | + "--claude-session-persistence", |
| 152 | + action="store_true", |
| 153 | + help=( |
| 154 | + "Allow Claude Code to persist its local session. By default " |
| 155 | + "agentlab passes `--no-session-persistence` for isolated trials." |
| 156 | + ), |
| 157 | + ) |
| 158 | + |
| 159 | + |
| 160 | +_add_claude_options = add_claude_options |
| 161 | + |
| 162 | + |
| 163 | +def _build_agent(args: argparse.Namespace, show_progress: bool = True) -> object: |
| 164 | + if args.agent == "manual": |
| 165 | + return ManualAgentAdapter(pause=not args.no_pause) |
| 166 | + if args.agent == "codex": |
| 167 | + return CodexCliAdapter(_codex_config_from_args(args, show_progress)) |
| 168 | + if args.agent == "claude": |
| 169 | + return ClaudeCodeAdapter(_claude_code_config_from_args(args, show_progress)) |
| 170 | + raise RuntimeError(f"unknown agent: {args.agent}") |
| 171 | + |
| 172 | + |
| 173 | +def _codex_config_from_args( |
| 174 | + args: argparse.Namespace, |
| 175 | + show_progress: bool = True, |
| 176 | +) -> CodexCliConfig: |
| 177 | + return CodexCliConfig( |
| 178 | + command=args.codex_command, |
| 179 | + model=args.codex_model, |
| 180 | + profile=args.codex_profile, |
| 181 | + sandbox=args.codex_sandbox, |
| 182 | + approval_policy=args.codex_approval, |
| 183 | + timeout_seconds=args.codex_timeout_seconds, |
| 184 | + show_progress=show_progress, |
| 185 | + ) |
| 186 | + |
| 187 | + |
| 188 | +def _claude_code_config_from_args( |
| 189 | + args: argparse.Namespace, |
| 190 | + show_progress: bool = True, |
| 191 | +) -> ClaudeCodeConfig: |
| 192 | + return ClaudeCodeConfig( |
| 193 | + command=args.claude_command, |
| 194 | + model=args.claude_model, |
| 195 | + permission_mode=args.claude_permission_mode, |
| 196 | + output_format=args.claude_output_format, |
| 197 | + max_turns=args.claude_max_turns, |
| 198 | + allowed_tools=tuple(args.claude_allowed_tool), |
| 199 | + disallowed_tools=tuple(args.claude_disallowed_tool), |
| 200 | + timeout_seconds=args.claude_timeout_seconds, |
| 201 | + show_progress=show_progress, |
| 202 | + no_session_persistence=not args.claude_session_persistence, |
| 203 | + ) |
| 204 | + |
| 205 | + |
| 206 | +def _agent_factory(args: argparse.Namespace) -> Callable[[bool], object]: |
| 207 | + def build(show_progress: bool) -> object: |
| 208 | + return _build_agent(args, show_progress=show_progress) |
| 209 | + |
| 210 | + return build |
0 commit comments