SWE-agent
[TOC]
1. Code Analysis
1.1 Whole Picture
Orchestrating、Agent Logic、Tool Parsing、Env Execution、LLM Call ——each layer has its own class,assembled by YAML。

File Structure:
1 | sweagent/ |
1.2 Client Layer
1.3 RunSingle
1.3.1 RunSingle Main
1 | class RunSingle: |
1.3.2 Hook
1.3.3 Log System
1 | # run_single.py:141-147 |
- .info.log: Key decisions made by the Agent(STEP, THOUGHT, ACTION, submit)
- .debug.log: Docker commands, file uploads, and details of tool installation
- .trace.log: Every command executed within the container and its corresponding output
1.4 Agent Layer
SWE-bench designs a two-layer agent system.
1 | RetryAgent (Outer Layer — multiple attempts) |
1.4.1 Default Agent
Setup: Assemble 5 parts of prompt,
- system_template
- Inject demonstrations
- Render instance_template
- Install tools
- Get the initial state。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20# agents.py:561-606
def setup(...) -> None:
# apply template configuration to multimodal problem statements
if hasattr(problem_statement, "type") and problem_statement.type == "swe_bench_multimodal":
from sweagent.agent.problem_statement import SWEBenchMultimodalProblemStatement
if isinstance(problem_statement, SWEBenchMultimodalProblemStatement):
# apply the global disable_image_processing setting if it's not explicitly set
if not problem_statement.disable_image_processing and self.templates.disable_image_processing:
problem_statement.disable_image_processing = True
# 1. install tools (upload bundle to docker, execute install.sh)
self.tools.install(self._env)
# 2. add system message to history
self.add_system_message_to_history()
# 3. add demonstrations to history (optional few-shot)
self.add_demonstrations_to_history()
# 4. add instance to history (including {{problem_statement}})
self.add_instance_template_to_history(state=self.tools.get_state(self._env))step(): Single-step execution
1
2
3
4
5
6
7
8# agents.py:1235-1263
def step(self):
n_step = len(self.trajectory) + 1
self.logger.info("=" * 25 + f" STEP {n_step} " + "=" * 25)
step_output = self.forward_with_handling(self.messages) # Think + Act
self.add_step_to_history(step_output) # add output to history,
self.add_step_to_trajectory(step_output) # add to trajectory
return step_outputforward(): Think-Act
1
2
3
4
5
6
7
8
9
10
11# agents.py:1006-1060
def forward(self, history: list[dict[str, str]]) -> StepOutput:
step = StepOutput()
step.query = copy.deepcopy(history)
output = self.model.query(history) # Think: Call LLM (line 1042)
step.output = output["message"]
step.thought, step.action = \
self.tools.parse_actions(output) # Parse: extract thought + action from output of LLM
return self.handle_action(step) # Act: execution actionforward_with_handling
1
2
3
4
5
6
7
8
9
10
11
12
13def forward_with_handling(self, history: list[dict[str, str]]) -> StepOutput:
def handle_error_with_autosubmission(exit_status: str, message: str) -> StepOutput:
...
def handle_error_with_retry(exception: Exception, template: str, n_requeries: int) -> list[dict[str, str]]:
...
n_format_fails = 0
while n_format_fails < self.max_requeries:
try:
return self.forward(history)
...run(): main loop
1
2
3
4
5
6
7
8# agents.py:1265-1294
def run(self, env, problem_statement, output_dir):
self.setup(env, problem_statement, output_dir)
step_output = StepOutput()
while not step_output.done:
step_output = self.step()
self.save_trajectory() # save trajectory
return AgentRunResult(...)
1.4.2 Retry Agent(Actor-Critic)
Actor(agent_encapsulation):
1 | # agents.py:303-319 |
Critic (Reviewer):
1 | # reviewer.py:416-449 |
1.5 Env Layer
…
2.
2.1 Plan & Execute
Plan:
config/default.yaml
1 | # config/default.yaml |
2.2 Actor-Critic
1 | RetryAgent (Orchestrator) |
When Actor finished, package all outputs of Actor:
1 | if step_output.done: |
Reviewer to get a score:
1 | RetryAgent.run() |
ScoreRetryLoop:
Attempt 1: Actor Finish → Reviewer: Review trajectory + patch → “0.3 points” → fail Attempt 2: Actor Finish → Reviewer: Review new trajectory + new patch → “0.7 points” → not enough Attempt 3: Actor Finish → Reviewer: Review new trajectory + new patch → “0.85 points” → Pass and Stop
ChooserRetryLoop:
Attempt 1 → save
Attempt 1 → save
….
Attempt 10 → save
Chooser Receive 10 patches + edited_files: Submission 0: (patch_0) Submission 1: (patch_1) … “The third is best” → Return index 3
Critic Prompt:
config/benchmarks/250212_sweagent_heavy_sbl.yaml:
1 | retry_loop: |
The final edited file with 30 lines of context:
1
{{edited_files30}}
```