Agent Type
Coding Agent with OpenClaw
A coding agent automates the most time-consuming parts of software development: reviewing pull requests, detecting bugs, suggesting refactors, writing tests, and generating documentation — all from the command line or a CI/CD pipeline.
What a Coding Agent Can Do
- Review pull requests for bugs, style violations, and security issues
- Detect and explain bugs in existing code
- Suggest and apply refactoring improvements
- Generate unit tests for existing functions
- Write inline documentation (docstrings, JSDoc)
- Explain unfamiliar code in plain English
When NOT to use: Don't let a coding agent push directly to
main. Always route its output through human code review. The agent is a highly capable drafter, not a final approver.
Required Tools
| Tool | Purpose |
|---|---|
code_reader | Read source files from filesystem or GitHub |
code_executor | Run code snippets in a sandboxed environment |
file_writer | Write refactored or generated files |
linter | Run flake8/eslint and return violations |
github_api | Fetch PR diffs, post review comments |
Code Review Agent Setup
# agents/coding.yaml
name: coding-agent
model: gpt-4o
temperature: 0.1
max_tokens: 4096
tools:
- code_reader
- linter
- github_api
system: |
You are a senior software engineer conducting a code review.
For each file changed:
1. Check for correctness and logic errors.
2. Identify security vulnerabilities (SQL injection, XSS, secrets in code).
3. Note style and readability issues.
4. Suggest improvements with concrete code snippets.
Format output as Markdown with severity labels: 🔴 Critical, 🟡 Warning, 🟢 Suggestion.from openclaw import Agent
agent = Agent.from_config("agents/coding.yaml")
# Review a PR diff
diff = open("pr_42.diff").read()
review = agent.run(f"Review this pull request diff:\n\n{diff}")
print(review)Bug Detection and Fix Agent
import subprocess
from openclaw import Agent
bug_agent = Agent.from_config("agents/coding.yaml")
def find_and_explain_bug(filepath: str) -> str:
code = open(filepath).read()
# Also run linter for static analysis context
lint = subprocess.run(
["flake8", "--max-line-length=120", filepath],
capture_output=True, text=True
).stdout
return bug_agent.run(
f"Analyse this Python file for bugs.\n\nLinter output:\n{lint}\n\nCode:\n```python\n{code}\n```"
)
report = find_and_explain_bug("src/payment_processor.py")
print(report)Test Generation Agent
# agents/test-writer.yaml
name: test-writer
model: gpt-4o
temperature: 0.2
max_tokens: 4096
tools:
- code_reader
- file_writer
system: |
You are a Python test engineer. Given source code, write pytest unit tests.
- Test each public function with at least 3 cases: happy path, edge case, error case.
- Use pytest fixtures and parametrize where appropriate.
- Do not import the module under test with sys.path tricks; assume it is installed.
- Return ONLY the test file content, no explanation.agent = Agent.from_config("agents/test-writer.yaml")
source = open("src/utils.py").read()
tests = agent.run(f"Write tests for:\n```python\n{source}\n```")
# Save to test file
with open("tests/test_utils.py", "w") as f:
f.write(tests)
print("Tests written to tests/test_utils.py")Refactoring Agent
refactor_agent = Agent.from_config("agents/coding.yaml")
def refactor_file(filepath: str, goal: str) -> str:
"""Return refactored code for the given file."""
code = open(filepath).read()
result = refactor_agent.run(
f"Refactor the following code. Goal: {goal}\n\n"
f"Return ONLY the refactored code, no explanation.\n\n"
f"```python\n{code}\n```"
)
# Strip markdown fences if present
result = result.strip()
if result.startswith("```"):
result = "\n".join(result.split("\n")[1:-1])
return result
new_code = refactor_file("src/legacy.py", "Extract repeated DB calls into a repository class")
open("src/legacy_refactored.py", "w").write(new_code)IDE Integration
You can hook the coding agent into your editor workflow via a simple shell alias or VS Code task:
# ~/.bashrc / ~/.zshrc
alias oc-review='openclaw run --agent agents/coding.yaml --task "Review the staged diff" < <(git diff --cached)'
alias oc-tests='openclaw run --agent agents/test-writer.yaml --task "Write tests for this file: $1"'// .vscode/tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "OpenClaw: Review current file",
"type": "shell",
"command": "openclaw run --agent agents/coding.yaml --task 'Review ${file}'",
"group": "test",
"presentation": { "reveal": "always", "panel": "new" }
}
]
}Guardrails for Coding Agents
Never allow the coding agent to
git push, delete files, or modify production configs directly. Set allow_destructive: false in your tool config and always review output before applying.
# Safe coding agent tool config
tools:
code_reader:
read_only: true
allowed_extensions: [.py, .js, .ts, .go, .rs, .yaml, .json]
file_writer:
output_dir: ./agent-output/ # write suggestions here, not in-place
create_only: true # cannot overwrite existing files