peen 1.0.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.
Files changed (81) hide show
  1. peen/__init__.py +95 -0
  2. peen/__main__.py +7 -0
  3. peen/agents/__init__.py +28 -0
  4. peen/agents/default.py +215 -0
  5. peen/agents/interactive.py +218 -0
  6. peen/agents/utils/__init__.py +0 -0
  7. peen/agents/utils/prompt_user.py +26 -0
  8. peen/config/README.md +8 -0
  9. peen/config/__init__.py +64 -0
  10. peen/config/benchmarks/__init__.py +1 -0
  11. peen/config/benchmarks/programbench.yaml +277 -0
  12. peen/config/benchmarks/swebench.yaml +183 -0
  13. peen/config/benchmarks/swebench_backticks.yaml +247 -0
  14. peen/config/benchmarks/swebench_modal.yaml +46 -0
  15. peen/config/benchmarks/swebench_xml.yaml +229 -0
  16. peen/config/default.yaml +171 -0
  17. peen/config/inspector.tcss +56 -0
  18. peen/config/mini.yaml +151 -0
  19. peen/config/mini_textbased.yaml +171 -0
  20. peen/config/peen.yaml +35 -0
  21. peen/environments/__init__.py +33 -0
  22. peen/environments/docker.py +161 -0
  23. peen/environments/extra/__init__.py +0 -0
  24. peen/environments/extra/bubblewrap.py +151 -0
  25. peen/environments/extra/contree.py +153 -0
  26. peen/environments/extra/swerex_docker.py +80 -0
  27. peen/environments/extra/swerex_modal.py +124 -0
  28. peen/environments/local.py +103 -0
  29. peen/environments/singularity.py +139 -0
  30. peen/exceptions.py +26 -0
  31. peen/mcp/__init__.py +4 -0
  32. peen/mcp/config.py +11 -0
  33. peen/mcp/manager.py +181 -0
  34. peen/models/__init__.py +113 -0
  35. peen/models/extra/__init__.py +0 -0
  36. peen/models/extra/roulette.py +64 -0
  37. peen/models/litellm_model.py +170 -0
  38. peen/models/litellm_response_model.py +98 -0
  39. peen/models/litellm_textbased_model.py +48 -0
  40. peen/models/openrouter_model.py +180 -0
  41. peen/models/openrouter_response_model.py +133 -0
  42. peen/models/openrouter_textbased_model.py +79 -0
  43. peen/models/portkey_model.py +213 -0
  44. peen/models/portkey_response_model.py +182 -0
  45. peen/models/requesty_model.py +182 -0
  46. peen/models/test_models.py +269 -0
  47. peen/models/utils/__init__.py +0 -0
  48. peen/models/utils/actions_text.py +70 -0
  49. peen/models/utils/actions_toolcall.py +123 -0
  50. peen/models/utils/actions_toolcall_response.py +141 -0
  51. peen/models/utils/anthropic_utils.py +28 -0
  52. peen/models/utils/cache_control.py +67 -0
  53. peen/models/utils/content_string.py +92 -0
  54. peen/models/utils/openai_multimodal.py +50 -0
  55. peen/models/utils/retry.py +25 -0
  56. peen/py.typed +0 -0
  57. peen/run/__init__.py +1 -0
  58. peen/run/benchmarks/__init__.py +1 -0
  59. peen/run/benchmarks/programbench.py +193 -0
  60. peen/run/benchmarks/swebench.py +275 -0
  61. peen/run/benchmarks/swebench_single.py +100 -0
  62. peen/run/benchmarks/utils/__init__.py +0 -0
  63. peen/run/benchmarks/utils/batch_progress.py +178 -0
  64. peen/run/benchmarks/utils/common.py +17 -0
  65. peen/run/extra/__init__.py +0 -0
  66. peen/run/hello_world.py +42 -0
  67. peen/run/mini.py +118 -0
  68. peen/run/peen.py +117 -0
  69. peen/run/utilities/__init__.py +1 -0
  70. peen/run/utilities/config.py +132 -0
  71. peen/run/utilities/inspector.py +316 -0
  72. peen/run/utilities/mini_extra.py +51 -0
  73. peen/utils/__init__.py +0 -0
  74. peen/utils/log.py +36 -0
  75. peen/utils/serialize.py +29 -0
  76. peen-1.0.0.dist-info/METADATA +273 -0
  77. peen-1.0.0.dist-info/RECORD +81 -0
  78. peen-1.0.0.dist-info/WHEEL +5 -0
  79. peen-1.0.0.dist-info/entry_points.txt +9 -0
  80. peen-1.0.0.dist-info/licenses/LICENSE.md +21 -0
  81. peen-1.0.0.dist-info/top_level.txt +1 -0
peen/__init__.py ADDED
@@ -0,0 +1,95 @@
1
+ """
2
+ This file provides:
3
+
4
+ - Path settings for global config file & relative directories
5
+ - Version numbering
6
+ - Protocols for the core components of peen.
7
+ By the magic of protocols & duck typing, you can pretty much ignore them,
8
+ unless you want the static type checking.
9
+ """
10
+
11
+ __version__ = "1.0.0"
12
+
13
+ import os
14
+ from pathlib import Path
15
+ from typing import Any, Protocol
16
+
17
+ import dotenv
18
+ from platformdirs import user_config_dir
19
+ from rich.console import Console
20
+
21
+ from peen.utils.log import logger
22
+
23
+ package_dir = Path(__file__).resolve().parent
24
+
25
+
26
+ global_config_dir = Path(
27
+ os.getenv("PEEN_GLOBAL_CONFIG_DIR")
28
+ or os.getenv("MSWEA_GLOBAL_CONFIG_DIR")
29
+ or (user_config_dir("peen") if Path(user_config_dir("peen")).exists() else user_config_dir("peen"))
30
+ )
31
+ global_config_dir.mkdir(parents=True, exist_ok=True)
32
+ global_config_file = Path(global_config_dir) / ".env"
33
+
34
+ if not (os.getenv("PEEN_SILENT_STARTUP") or os.getenv("MSWEA_SILENT_STARTUP")):
35
+ Console().print(
36
+ f"This is [bold green]Peen-SWE[/bold green] version [bold green]{__version__}[/bold green].\n"
37
+ f"Loading global config from [bold green]'{global_config_file}'[/bold green]",
38
+ )
39
+ dotenv.load_dotenv(dotenv_path=global_config_file)
40
+
41
+
42
+ # === Protocols ===
43
+ # You can ignore them unless you want static type checking.
44
+
45
+
46
+ class Model(Protocol):
47
+ """Protocol for language models."""
48
+
49
+ config: Any
50
+
51
+ def query(self, messages: list[dict[str, str]], **kwargs) -> dict: ...
52
+
53
+ def format_message(self, **kwargs) -> dict: ...
54
+
55
+ def format_observation_messages(
56
+ self, message: dict, outputs: list[dict], template_vars: dict | None = None
57
+ ) -> list[dict]: ...
58
+
59
+ def get_template_vars(self, **kwargs) -> dict[str, Any]: ...
60
+
61
+ def serialize(self) -> dict: ...
62
+
63
+
64
+ class Environment(Protocol):
65
+ """Protocol for execution environments."""
66
+
67
+ config: Any
68
+
69
+ def execute(self, action: dict, cwd: str = "") -> dict[str, Any]: ...
70
+
71
+ def get_template_vars(self, **kwargs) -> dict[str, Any]: ...
72
+
73
+ def serialize(self) -> dict: ...
74
+
75
+
76
+ class Agent(Protocol):
77
+ """Protocol for agents."""
78
+
79
+ config: Any
80
+
81
+ def run(self, task: str, **kwargs) -> dict: ...
82
+
83
+ def save(self, path: Path | None, *extra_dicts) -> dict: ...
84
+
85
+
86
+ __all__ = [
87
+ "Agent",
88
+ "Model",
89
+ "Environment",
90
+ "package_dir",
91
+ "__version__",
92
+ "global_config_file",
93
+ "global_config_dir",
94
+ "logger",
95
+ ]
peen/__main__.py ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env python3
2
+ """This is what happens when you do `python -m peen` or `uvx peen`."""
3
+
4
+ from peen.run.peen import app
5
+
6
+ if __name__ == "__main__":
7
+ app()
@@ -0,0 +1,28 @@
1
+ """Agent implementations for mini-SWE-agent."""
2
+
3
+ import copy
4
+ import importlib
5
+
6
+ from peen import Agent, Environment, Model
7
+
8
+ _AGENT_MAPPING = {
9
+ "default": "peen.agents.default.DefaultAgent",
10
+ "interactive": "peen.agents.interactive.InteractiveAgent",
11
+ }
12
+
13
+
14
+ def get_agent_class(spec: str) -> type[Agent]:
15
+ full_path = _AGENT_MAPPING.get(spec, spec)
16
+ try:
17
+ module_name, class_name = full_path.rsplit(".", 1)
18
+ module = importlib.import_module(module_name)
19
+ return getattr(module, class_name)
20
+ except (ValueError, ImportError, AttributeError):
21
+ msg = f"Unknown agent type: {spec} (resolved to {full_path}, available: {_AGENT_MAPPING})"
22
+ raise ValueError(msg)
23
+
24
+
25
+ def get_agent(model: Model, env: Environment, config: dict, *, default_type: str = "") -> Agent:
26
+ config = copy.deepcopy(config)
27
+ agent_class = get_agent_class(config.pop("agent_class", default_type))
28
+ return agent_class(model, env, **config)
peen/agents/default.py ADDED
@@ -0,0 +1,215 @@
1
+ """Basic agent class. See https://sobhit-singh.github.io/peen-swe/latest/advanced/control_flow/ for visual explanation
2
+ or https://minimal-agent.com for a tutorial on the basic building principles.
3
+ """
4
+
5
+ import json
6
+ import logging
7
+ import time
8
+ import traceback
9
+ from pathlib import Path
10
+ from typing import Any
11
+
12
+ from jinja2 import StrictUndefined, Template
13
+ from pydantic import BaseModel
14
+
15
+ from peen import Environment, Model, __version__
16
+ from peen.exceptions import FormatError, InterruptAgentFlow, LimitsExceeded, TimeExceeded
17
+ from peen.utils.serialize import recursive_merge
18
+
19
+
20
+ class AgentConfig(BaseModel):
21
+ """Check the config files in peen/config for example settings."""
22
+
23
+ system_template: str
24
+ """Template for the system message (the first message)."""
25
+ instance_template: str
26
+ """Template for the first user message specifying the task (the second message overall)."""
27
+ step_limit: int = 0
28
+ """Maximum number of steps the agent can take."""
29
+ cost_limit: float = 3.0
30
+ """Stop agent after exceeding (!) this cost."""
31
+ wall_time_limit_seconds: int = 0
32
+ """Stop agent after this many seconds of wall-clock time. 0 means no limit."""
33
+ max_consecutive_format_errors: int = 3
34
+ """Exit after this many format errors in a row (0 = no limit)."""
35
+ output_path: Path | None = None
36
+ """Save the trajectory to this path."""
37
+
38
+
39
+ class DefaultAgent:
40
+ def __init__(
41
+ self,
42
+ model: Model,
43
+ env: Environment,
44
+ *,
45
+ config_class: type = AgentConfig,
46
+ mcp_manager: Any = None,
47
+ **kwargs,
48
+ ):
49
+ """See the `AgentConfig` class for permitted keyword arguments."""
50
+ self.config = config_class(**kwargs)
51
+ self.messages: list[dict] = []
52
+ self.model = model
53
+ self.env = env
54
+ self.mcp_manager = mcp_manager
55
+ self.extra_template_vars = {}
56
+ self.logger = logging.getLogger("agent")
57
+ self.cost = 0.0
58
+ self.n_calls = 0
59
+ self.n_consecutive_format_errors = 0
60
+ self._start_time = time.time()
61
+
62
+ def get_template_vars(self, **kwargs) -> dict:
63
+ return recursive_merge(
64
+ self.config.model_dump(),
65
+ self.env.get_template_vars(),
66
+ self.model.get_template_vars(),
67
+ {
68
+ "n_model_calls": self.n_calls,
69
+ "model_cost": self.cost,
70
+ "elapsed_seconds": int(time.time() - self._start_time),
71
+ },
72
+ self.extra_template_vars,
73
+ kwargs,
74
+ )
75
+
76
+ def _render_template(self, template: str) -> str:
77
+ return Template(template, undefined=StrictUndefined).render(**self.get_template_vars())
78
+
79
+ def add_messages(self, *messages: dict) -> list[dict]:
80
+ self.logger.debug(messages) # set log level to debug to see
81
+ self.messages.extend(messages)
82
+ return list(messages)
83
+
84
+ def handle_uncaught_exception(self, e: Exception) -> list[dict]:
85
+ return self.add_messages(
86
+ self.model.format_message(
87
+ role="exit",
88
+ content=str(e),
89
+ extra={
90
+ "exit_status": type(e).__name__,
91
+ "submission": "",
92
+ "exception_str": str(e),
93
+ "traceback": traceback.format_exc(),
94
+ },
95
+ )
96
+ )
97
+
98
+ def run(self, task: str = "", **kwargs) -> dict:
99
+ """Run step() until agent is finished. Returns dictionary with exit_status, submission keys."""
100
+ try:
101
+ if self.mcp_manager:
102
+ self.mcp_manager.start()
103
+ if hasattr(self.model, "set_tools"):
104
+ from peen.models.utils.actions_toolcall import BASH_TOOL
105
+
106
+ self.model.set_tools([BASH_TOOL, *self.mcp_manager.get_tools()])
107
+ self.extra_template_vars |= {"task": task, **kwargs}
108
+ self.messages = []
109
+ self.add_messages(
110
+ self.model.format_message(role="system", content=self._render_template(self.config.system_template)),
111
+ self.model.format_message(role="user", content=self._render_template(self.config.instance_template)),
112
+ )
113
+ while True:
114
+ try:
115
+ self.step()
116
+ self.n_consecutive_format_errors = 0 # reset on any clean step
117
+ except FormatError as e:
118
+ # The call was billed before parsing failed, so query() never got to charge it.
119
+ self.cost += e.messages[0].get("extra", {}).get("cost", 0.0)
120
+ self.n_consecutive_format_errors += 1
121
+ if 0 < self.config.max_consecutive_format_errors <= self.n_consecutive_format_errors:
122
+ self.add_messages(
123
+ *e.messages,
124
+ {
125
+ "role": "exit",
126
+ "content": "RepeatedFormatError",
127
+ "extra": {"exit_status": "RepeatedFormatError", "submission": ""},
128
+ },
129
+ )
130
+ else:
131
+ self.add_messages(*e.messages)
132
+ except InterruptAgentFlow as e:
133
+ self.add_messages(*e.messages)
134
+ except Exception as e:
135
+ self.handle_uncaught_exception(e)
136
+ raise
137
+ finally:
138
+ self.save(self.config.output_path)
139
+ if self.messages[-1].get("role") == "exit":
140
+ break
141
+ return self.messages[-1].get("extra", {})
142
+ finally:
143
+ if self.mcp_manager:
144
+ self.mcp_manager.close()
145
+
146
+ def step(self) -> list[dict]:
147
+ """Query the LM, execute actions."""
148
+ return self.execute_actions(self.query())
149
+
150
+ def query(self) -> dict:
151
+ """Query the model and return model messages. Override to add hooks."""
152
+ if 0 < self.config.step_limit <= self.n_calls or 0 < self.config.cost_limit <= self.cost:
153
+ raise LimitsExceeded(
154
+ {
155
+ "role": "exit",
156
+ "content": "LimitsExceeded",
157
+ "extra": {"exit_status": "LimitsExceeded", "submission": ""},
158
+ }
159
+ )
160
+ if 0 < self.config.wall_time_limit_seconds <= int(time.time() - self._start_time):
161
+ raise TimeExceeded(
162
+ {
163
+ "role": "exit",
164
+ "content": "TimeExceeded",
165
+ "extra": {"exit_status": "TimeExceeded", "submission": ""},
166
+ }
167
+ )
168
+ self.n_calls += 1
169
+ message = self.model.query(self.messages)
170
+ self.cost += message.get("extra", {}).get("cost", 0.0)
171
+ self.add_messages(message)
172
+ return message
173
+
174
+ def execute_actions(self, message: dict) -> list[dict]:
175
+ """Execute actions in message, add observation messages, return them."""
176
+ outputs = []
177
+ for action in message.get("extra", {}).get("actions", []):
178
+ if action.get("tool_name", "bash") == "bash" or not self.mcp_manager:
179
+ outputs.append(self.env.execute(action))
180
+ else:
181
+ outputs.append(self.mcp_manager.execute(action))
182
+ return self.add_messages(*self.model.format_observation_messages(message, outputs, self.get_template_vars()))
183
+
184
+ def serialize(self, *extra_dicts) -> dict:
185
+ """Serialize agent state to a json-compatible nested dictionary for saving."""
186
+ last_message = self.messages[-1] if self.messages else {}
187
+ last_extra = last_message.get("extra", {})
188
+ agent_data = {
189
+ "info": {
190
+ "model_stats": {
191
+ "instance_cost": self.cost,
192
+ "api_calls": self.n_calls,
193
+ },
194
+ "config": {
195
+ "agent": self.config.model_dump(mode="json"),
196
+ "agent_type": f"{self.__class__.__module__}.{self.__class__.__name__}",
197
+ },
198
+ "mini_version": __version__,
199
+ "exit_status": last_extra.get("exit_status", ""),
200
+ "submission": last_extra.get("submission", ""),
201
+ },
202
+ "messages": self.messages,
203
+ "trajectory_format": "peen-1.0",
204
+ }
205
+ return recursive_merge(agent_data, self.model.serialize(), self.env.serialize(), *extra_dicts)
206
+
207
+ def save(self, path: Path | None, *extra_dicts) -> dict:
208
+ """Save the trajectory of the agent to a file if path is given. Returns full serialized data.
209
+ You can pass additional dictionaries with extra data to be (recursively) merged into the output data.
210
+ """
211
+ data = self.serialize(*extra_dicts)
212
+ if path:
213
+ path.parent.mkdir(parents=True, exist_ok=True)
214
+ path.write_text(json.dumps(data, indent=2))
215
+ return data
@@ -0,0 +1,218 @@
1
+ """A small generalization of the default agent that puts the user in the loop.
2
+
3
+ There are three modes:
4
+ - human: commands issued by the user are executed immediately
5
+ - confirm: commands issued by the LM but not whitelisted are confirmed by the user
6
+ - yolo: commands issued by the LM are executed immediately without confirmation
7
+ """
8
+
9
+ import json
10
+ import re
11
+ import sys
12
+ from typing import Literal, NoReturn
13
+
14
+ from rich.console import Console
15
+ from rich.rule import Rule
16
+
17
+ from peen.agents.default import AgentConfig, DefaultAgent
18
+ from peen.agents.utils.prompt_user import _multiline_prompt, prompt_session
19
+ from peen.exceptions import LimitsExceeded, Submitted, TimeExceeded, UserInterruption
20
+ from peen.models.utils.content_string import get_content_string
21
+
22
+ console = Console(highlight=False)
23
+
24
+
25
+ class InteractiveAgentConfig(AgentConfig):
26
+ mode: Literal["human", "confirm", "yolo"] = "confirm"
27
+ """Whether to confirm actions."""
28
+ whitelist_actions: list[str] = []
29
+ """Never confirm actions that match these regular expressions."""
30
+ confirm_exit: bool = True
31
+ """If the agent wants to finish, do we ask for confirmation from user?"""
32
+
33
+
34
+ class InteractiveAgent(DefaultAgent):
35
+ _MODE_COMMANDS_MAPPING = {"/u": "human", "/c": "confirm", "/y": "yolo"}
36
+
37
+ def __init__(self, *args, config_class=InteractiveAgentConfig, **kwargs):
38
+ super().__init__(*args, config_class=config_class, **kwargs)
39
+ self.cost_last_confirmed = 0.0
40
+
41
+ def _interrupt(self, content: str, *, itype: str = "UserInterruption") -> NoReturn:
42
+ raise UserInterruption({"role": "user", "content": content, "extra": {"interrupt_type": itype}})
43
+
44
+ def add_messages(self, *messages: dict) -> list[dict]:
45
+ # Extend supermethod to print messages
46
+ for msg in messages:
47
+ role, content = msg.get("role") or msg.get("type", "unknown"), get_content_string(msg)
48
+ if role == "assistant":
49
+ console.print(
50
+ f"\n[red][bold]peen[/bold] (step [bold]{self.n_calls}[/bold], [bold]${self.cost:.2f}[/bold]):[/red]\n",
51
+ end="",
52
+ highlight=False,
53
+ )
54
+ else:
55
+ console.print(f"\n[bold green]{role.capitalize()}[/bold green]:\n", end="", highlight=False)
56
+ console.print(content, highlight=False, markup=False)
57
+ return super().add_messages(*messages)
58
+
59
+ def query(self) -> dict:
60
+ # Extend supermethod to handle human mode
61
+ if self.config.mode == "human":
62
+ match command := self._prompt_and_handle_slash_commands("[bold yellow]>[/bold yellow] "):
63
+ case "/y" | "/c":
64
+ pass
65
+ case _:
66
+ msg = {
67
+ "role": "user",
68
+ "content": f"User command: \n```bash\n{command}\n```",
69
+ "extra": {"actions": [{"command": command}]},
70
+ }
71
+ self.add_messages(msg)
72
+ return msg
73
+ try:
74
+ with console.status("Waiting for the LM to respond..."):
75
+ return super().query()
76
+ except TimeExceeded:
77
+ # A wall-clock limit can't be lifted by raising the step/cost limits
78
+ # (the next query re-checks the clock and raises again), so prompting
79
+ # would loop forever. Always stop cleanly instead.
80
+ raise
81
+ except LimitsExceeded:
82
+ if not self._stdin_is_interactive():
83
+ # No terminal to prompt for new limits -- e.g. an unattended
84
+ # `--yolo` run, or any run with stdin redirected from /dev/null
85
+ # (CI, a sandbox). Stop cleanly so the trajectory is saved with a
86
+ # LimitsExceeded exit status, instead of crashing on EOFError when
87
+ # reading input.
88
+ raise
89
+ console.print(
90
+ f"Limits exceeded. Limits: {self.config.step_limit} steps, ${self.config.cost_limit}.\n"
91
+ f"Current spend: {self.n_calls} steps, ${self.cost:.2f}."
92
+ )
93
+ self.config.step_limit = int(input("New step limit: "))
94
+ self.config.cost_limit = float(input("New cost limit: "))
95
+ return super().query()
96
+
97
+ @staticmethod
98
+ def _stdin_is_interactive() -> bool:
99
+ """Whether an interactive terminal is available to prompt the user.
100
+
101
+ Returns False for unattended runs (e.g. `--yolo` in CI, or inside a
102
+ sandbox with stdin redirected from /dev/null), where calling ``input()``
103
+ would raise ``EOFError`` and crash the run.
104
+ """
105
+ try:
106
+ return sys.stdin is not None and sys.stdin.isatty()
107
+ except (ValueError, OSError):
108
+ return False
109
+
110
+ def step(self) -> list[dict]:
111
+ # Override the step method to handle user interruption
112
+ try:
113
+ console.print(Rule())
114
+ return super().step()
115
+ except KeyboardInterrupt:
116
+ interruption_message = self._prompt_and_handle_slash_commands(
117
+ "\n\n[bold yellow]Interrupted.[/bold yellow] "
118
+ "[green]Type a comment/command[/green] (/h for available commands)"
119
+ "\n[bold yellow]>[/bold yellow] "
120
+ ).strip()
121
+ if not interruption_message or interruption_message in self._MODE_COMMANDS_MAPPING:
122
+ interruption_message = "Temporary interruption caught."
123
+ self._interrupt(f"Interrupted by user: {interruption_message}")
124
+
125
+ def execute_actions(self, message: dict) -> list[dict]:
126
+ # Override to handle user confirmation and confirm_exit, with try/finally to preserve partial outputs
127
+ actions = message.get("extra", {}).get("actions", [])
128
+ commands = [
129
+ action["command"]
130
+ if action.get("tool_name", "bash") == "bash" and "command" in action
131
+ else f"[mcp:{action.get('tool_name')}] {json.dumps(action.get('arguments', {}))}"
132
+ for action in actions
133
+ ]
134
+ outputs = []
135
+ try:
136
+ self._ask_confirmation_or_interrupt(commands)
137
+ for action in actions:
138
+ if action.get("tool_name", "bash") == "bash" or not self.mcp_manager:
139
+ outputs.append(self.env.execute(action))
140
+ else:
141
+ outputs.append(self.mcp_manager.execute(action))
142
+ except Submitted as e:
143
+ self._check_for_new_task_or_submit(e)
144
+ finally:
145
+ result = self.add_messages(
146
+ *self.model.format_observation_messages(message, outputs, self.get_template_vars())
147
+ )
148
+ return result
149
+
150
+ def _add_observation_messages(self, message: dict, outputs: list[dict]) -> list[dict]:
151
+ return self.add_messages(*self.model.format_observation_messages(message, outputs, self.get_template_vars()))
152
+
153
+ def _check_for_new_task_or_submit(self, e: Submitted) -> NoReturn:
154
+ """Check if user wants to add a new task or submit."""
155
+ if self.config.confirm_exit:
156
+ message = (
157
+ "[bold yellow]Agent wants to finish.[/bold yellow] "
158
+ "[bold green]Type new task[/bold green] or [bold]Enter[/bold] to quit "
159
+ "([bold]/h[/bold] for commands)\n"
160
+ "[bold yellow]>[/bold yellow] "
161
+ )
162
+ user_input = self._prompt_and_handle_slash_commands(message).strip()
163
+ if user_input == "/u": # directly continue
164
+ self._interrupt("Switched to human mode.")
165
+ elif user_input in self._MODE_COMMANDS_MAPPING: # ask again
166
+ return self._check_for_new_task_or_submit(e)
167
+ elif user_input:
168
+ self._interrupt(f"The user added a new task: {user_input}", itype="UserNewTask")
169
+ raise e
170
+
171
+ def _should_ask_confirmation(self, action: str) -> bool:
172
+ return self.config.mode == "confirm" and not any(re.match(r, action) for r in self.config.whitelist_actions)
173
+
174
+ def _ask_confirmation_or_interrupt(self, commands: list[str]) -> None:
175
+ if not any(self._should_ask_confirmation(c) for c in commands):
176
+ return
177
+ prompt = (
178
+ f"[bold yellow]Execute {len(commands)} action(s)?[/] [green][bold]Enter[/] to confirm[/], "
179
+ "[red]type [bold]comment[/] to reject[/], or [blue][bold]/h[/] to show available commands[/]\n"
180
+ "[bold yellow]>[/bold yellow] "
181
+ )
182
+ match user_input := self._prompt_and_handle_slash_commands(prompt).strip():
183
+ case "" | "/y":
184
+ pass # confirmed, do nothing
185
+ case "/u": # Skip execution action and get back to query
186
+ self._interrupt("Commands not executed. Switching to human mode", itype="UserRejection")
187
+ case _:
188
+ self._interrupt(
189
+ f"Commands not executed. The user rejected your commands with the following message: {user_input}",
190
+ itype="UserRejection",
191
+ )
192
+
193
+ def _prompt_and_handle_slash_commands(self, prompt: str, *, _multiline: bool = False) -> str:
194
+ """Prompts the user, takes care of /h (followed by requery) and sets the mode. Returns the user input."""
195
+ console.print(prompt, end="")
196
+ if _multiline:
197
+ return _multiline_prompt()
198
+ user_input = prompt_session.prompt("")
199
+ if user_input == "/m":
200
+ return self._prompt_and_handle_slash_commands(prompt, _multiline=True)
201
+ if user_input == "/h":
202
+ console.print(
203
+ f"Current mode: [bold green]{self.config.mode}[/bold green]\n"
204
+ f"[bold green]/y[/bold green] to switch to [bold yellow]yolo[/bold yellow] mode (execute LM commands without confirmation)\n"
205
+ f"[bold green]/c[/bold green] to switch to [bold yellow]confirmation[/bold yellow] mode (ask for confirmation before executing LM commands)\n"
206
+ f"[bold green]/u[/bold green] to switch to [bold yellow]human[/bold yellow] mode (execute commands issued by the user)\n"
207
+ f"[bold green]/m[/bold green] to enter multiline comment",
208
+ )
209
+ return self._prompt_and_handle_slash_commands(prompt)
210
+ if user_input in self._MODE_COMMANDS_MAPPING:
211
+ if self.config.mode == self._MODE_COMMANDS_MAPPING[user_input]:
212
+ return self._prompt_and_handle_slash_commands(
213
+ f"[bold red]Already in {self.config.mode} mode.[/bold red]\n{prompt}"
214
+ )
215
+ self.config.mode = self._MODE_COMMANDS_MAPPING[user_input]
216
+ console.print(f"Switched to [bold green]{self.config.mode}[/bold green] mode.")
217
+ return user_input
218
+ return user_input
File without changes
@@ -0,0 +1,26 @@
1
+ from prompt_toolkit.formatted_text.html import HTML
2
+ from prompt_toolkit.history import FileHistory
3
+ from prompt_toolkit.shortcuts import PromptSession
4
+
5
+ from peen import global_config_dir
6
+
7
+ _history = FileHistory(global_config_dir / "interactive_history.txt")
8
+ try:
9
+ prompt_session = PromptSession(history=_history)
10
+ _multiline_prompt_session = PromptSession(history=_history, multiline=True)
11
+ except Exception:
12
+ from prompt_toolkit.output import DummyOutput
13
+
14
+ prompt_session = PromptSession(history=_history, output=DummyOutput())
15
+ _multiline_prompt_session = PromptSession(history=_history, multiline=True, output=DummyOutput())
16
+
17
+
18
+ def _multiline_prompt() -> str:
19
+ return _multiline_prompt_session.prompt(
20
+ "",
21
+ bottom_toolbar=HTML(
22
+ "Submit message: <b fg='yellow' bg='black'>Esc, then Enter</b> | "
23
+ "Navigate history: <b fg='yellow' bg='black'>Arrow Up/Down</b> | "
24
+ "Search history: <b fg='yellow' bg='black'>Ctrl+R</b>"
25
+ ),
26
+ )
peen/config/README.md ADDED
@@ -0,0 +1,8 @@
1
+ # Configs
2
+
3
+ * `mini.yaml` - Default config for `mini`/`agents/interactive.py` agent.
4
+ * `default.yaml` - Default config for the `default.py` agent.
5
+
6
+ ## Benchmarks
7
+
8
+ * `benchmarks/swebench.yaml` - Config for the `run/benchmarks/swebench.py` entry point.
@@ -0,0 +1,64 @@
1
+ """Configuration files and utilities for mini-SWE-agent."""
2
+
3
+ import json
4
+ import os
5
+ from pathlib import Path
6
+
7
+ import yaml
8
+
9
+ builtin_config_dir = Path(__file__).parent
10
+
11
+
12
+ def get_config_path(config_spec: str | Path) -> Path:
13
+ """Get the path to a config file."""
14
+ config_spec = Path(config_spec)
15
+ if config_spec.suffix != ".yaml":
16
+ config_spec = config_spec.with_suffix(".yaml")
17
+ candidates = [
18
+ Path(config_spec),
19
+ Path(os.getenv("MSWEA_CONFIG_DIR", ".")) / config_spec,
20
+ builtin_config_dir / config_spec,
21
+ builtin_config_dir / "extra" / config_spec,
22
+ builtin_config_dir / "benchmarks" / config_spec,
23
+ ]
24
+ for candidate in candidates:
25
+ if candidate.exists():
26
+ return candidate
27
+
28
+ raise FileNotFoundError(f"Could not find config file for {config_spec} (tried: {candidates})")
29
+
30
+
31
+ def _key_value_spec_to_nested_dict(config_spec: str) -> dict:
32
+ """Interpret key-value specs from the command line.
33
+
34
+ Example:
35
+
36
+ "model.model_name=anthropic/claude-sonnet-4-5-20250929" ->
37
+ {"model": {"model_name": "anthropic/claude-sonnet-4-5-20250929"}}
38
+ """
39
+ key, value = config_spec.split("=", 1)
40
+ try:
41
+ value = json.loads(value)
42
+ except json.JSONDecodeError:
43
+ pass
44
+ keys = key.split(".")
45
+ if any(k == "" for k in keys):
46
+ raise ValueError(f"Invalid config spec {config_spec!r}: empty config key")
47
+ result = {}
48
+ current = result
49
+ for k in keys[:-1]:
50
+ current[k] = {}
51
+ current = current[k]
52
+ current[keys[-1]] = value
53
+ return result
54
+
55
+
56
+ def get_config_from_spec(config_spec: str | Path) -> dict:
57
+ """Get a config from a config spec."""
58
+ if isinstance(config_spec, str) and "=" in config_spec:
59
+ return _key_value_spec_to_nested_dict(config_spec)
60
+ path = get_config_path(config_spec)
61
+ return yaml.safe_load(path.read_text(encoding="utf-8"))
62
+
63
+
64
+ __all__ = ["builtin_config_dir", "get_config_path", "get_config_from_spec", "_key_value_spec_to_nested_dict"]
@@ -0,0 +1 @@
1
+ """Extra configuration files for mini-SWE-agent."""