Security Remediation Agent for Python codebases
Project description
cat << 'EOF' > README.md
Refactoring Agent 🛡️
Autonomous Technical Debt & Security Remediation Agent.
Refactoring Agent goes beyond simple linting. It is an infrastructure layer for Continuous Modernization that:
- Scans legacy code for technical debt and Critical Security Vulnerabilities.
- Fixes issues automatically using a deterministic Rule Engine and LLMs (OpenAI/Ollama).
- Protects your codebase with strict CI/CD gates.
"Snyk finds the vulnerability. Refactoring Agent fixes it."
🚀 Key Features
🛡️ Security Scanning (New in v3.11)
Proactively hunts for critical vulnerabilities that static linters often miss:
- 🚫 Arbitrary Code Execution: Detects and blocks
eval()andexec(). - 🚫 Shell Injection: Flags dangerous
os.system()calls. - 🚫 Safe I/O: Enforces modern
input()over insecure legacy alternatives.
Example Output: ```text [ERROR] src/payment_gateway.py:12 Security Risk: usage of eval() detected. This allows arbitrary code execution. [ERROR] src/utils.py:45 Security Risk: usage of os.system() detected. Vulnerable to shell injection. ```
Installation
From PyPI (recommended)
```bash pip install "refactoring-agent>=3.11.1" ```
From source
```bash git clone https://github.com/StasLee1982/refactoring-agent.git cd refactoring-agent
python -m venv .venv source .venv/bin/activate # On Windows: .venv\Scripts\activate
pip install . ```
If you want to hack on the project itself: ```bash pip install -e . pytest -v ```
CLI usage
The main entrypoint is the `refactor-agent` CLI.
1) Basic scan (no AI)
Scan the current repository and fail on critical issues:
```bash refactor-agent check . ```
Typical findings:
- Legacy `raw_input` usage in Python 2 style code.
- Dangerous `eval()`, `exec()` and `os.system()` calls.
- Legacy `print` statements in production code.
- Syntax errors in broken / half‑migrated files.
Exclude additional directories: ```bash refactor-agent check . --exclude "venv,node_modules,dist" ```
Generate an HTML report: ```bash refactor-agent check . --html-report refactoring-report.html ```
2) AI‑powered fixes with OpenAI
Use OpenAI to propose automatic fixes without touching files:
```bash
export OPENAI_API_KEY="sk-..." # your key
refactor-agent check demo_legacy
--provider openai
--model gpt-4o-mini
--ai-fix
--dry-run
```
What happens:
- `--ai-fix` – ask the LLM to propose patches.
- `--dry-run` – print unified diffs, do not modify files on disk.
- Without `--dry-run`, the patches are applied in place.
You can also pass API key explicitly (useful in CI):
```bash
refactor-agent check demo_legacy
--provider openai
--model gpt-4o-mini
--api-key "$OPENAI_API_KEY"
--ai-fix --dry-run
```
3) AI‑powered fixes with Ollama (local LLM)
Run against a local Ollama model:
```bash
Example: llama3 pulled in Ollama beforehand
refactor-agent check demo_legacy
--provider ollama
--model llama3
--ai-fix
--dry-run
```
If your Ollama endpoint is not the default, you can override it:
```bash
refactor-agent check demo_legacy
--provider ollama
--model llama3
--base-url http://localhost:11434
--ai-fix --dry-run
```
4) Mock provider for demos and tests
Use the built‑in mock provider to demonstrate the workflow or run tests without calling any real LLM:
```bash
refactor-agent check demo_legacy
--provider mock
--ai-fix
--dry-run
```
The mock provider returns a deterministic, safe patch so you can see the end‑to‑end flow of Refactoring Agent in CI and docs.
Configuration
You can configure Refactoring Agent via `pyproject.toml`. Run `refactor-agent init` to generate a config interactively, or add this manually:
```toml [tool.refactoring-agent]
Security strictness: "strict" (fail on risk) or "relaxed" (only warn)
security_level = "strict"
How to handle legacy print statements: "error", "warn", or "ignore"
legacy_print = "warn"
AI Provider settings (optional, can also be set via env vars)
ai_provider = "openai" ai_model = "gpt-4o-mini" ```
| Option | Values | Description |
|---|---|---|
| `security_level` | `strict`, `relaxed` | `strict` makes CI fail if dangerous code (e.g., `eval`, `os.system`) is found. |
| `legacy_print` | `error`, `warn`, `ignore` | Controls whether Python 2 style prints fail the build or just emit a warning. |
GitHub Action example
You can run Refactoring Agent on every push / pull request as part of your security and modernization gate.
Option A – plain CLI in a workflow
```yaml name: Security & Legacy Scan on: push: branches: [ main ] pull_request:
jobs: refactoring-agent: runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Install Refactoring Agent
run: |
python -m pip install --upgrade pip
pip install "refactoring-agent>=3.11.1"
- name: Run Refactoring Agent (AI dry-run)
env:
OPENAI_API_KEY: \${{ secrets.OPENAI_API_KEY }}
run: |
refactor-agent check . \
--provider openai \
--model gpt-4o-mini \
--ai-fix \
--dry-run \
--html-report refactoring-report.html
- name: Upload HTML report
uses: actions/upload-artifact@v4
with:
name: refactoring-agent-report
path: refactoring-report.html
```
Option B – using the bundled Action (this repo)
If you publish `action.yml` in this repository, you can also wire it like this:
```yaml jobs: refactoring-agent: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4
- name: Run Refactoring Agent Action
uses: StasLee1982/refactoring-agent@v3.11.1
with:
# Adapt these inputs to match your action.yml
path: .
args: "--html-report refactoring-report.html --provider openai --ai-fix --dry-run"
api_key: \${{ secrets.OPENAI_API_KEY }}
``` EOF
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file refactoring_agent-3.12.1.tar.gz.
File metadata
- Download URL: refactoring_agent-3.12.1.tar.gz
- Upload date:
- Size: 20.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4be382a0d6c882433d0e4058adc2426a5726b1e43572d1914e180cced4d5aac5
|
|
| MD5 |
f7aa0b8efb9dcd2b46be527b0e622a45
|
|
| BLAKE2b-256 |
2fb8b75ff5ca6af76db27bba08415b1329a768ab4f18bf15a2bf8e3091f28d7e
|
File details
Details for the file refactoring_agent-3.12.1-py3-none-any.whl.
File metadata
- Download URL: refactoring_agent-3.12.1-py3-none-any.whl
- Upload date:
- Size: 16.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c562ed525491f8d1e2dc276aac5ba12a5167642b30ac1eb63c501e95e1dc4984
|
|
| MD5 |
f8c6313fb7b14de5ef98384334ab8a74
|
|
| BLAKE2b-256 |
e680780d085912aef5cc9b95f4f502dee791e561c813081945c8dd8b0c922b78
|