Skip to main content

ReDSL — Refactor + DSL + Self-Learning. LLM-powered autonomous code refactoring.

Project description

ReDSL

Refactor + DSL + Self-Learning — autonomous code refactoring with LLM, memory, and DSL.

ReDSL is a code refactoring system that combines static analysis, DSL rules, and LLM intelligence to automatically improve Python code quality.

Features

  • 🔍 Static Analysis - Integration with popular linters and metrics tools
  • 🧠 LLM with Reflection - Generate refactoring proposals with self-reflection loop
  • Hybrid Engine - Direct refactorings for simple changes, LLM for complex ones
  • 📊 DSL Engine - Define refactoring rules in readable YAML format
  • 💾 Memory System - Learn from refactoring history
  • 🚀 Scalability - Process multiple projects simultaneously

Installation

pip install redsl

Quick Start

Basic CLI Usage

# Refactor a single project (dry run)
redsl refactor ./my-project --max-actions 5 --dry-run

# Refactor without dry run (apply changes)
redsl refactor ./my-project --max-actions 10

# Get output in YAML format (for integration)
redsl refactor ./my-project --format yaml

# Get output in JSON format (for APIs)
redsl refactor ./my-project --format json

Batch Processing

# Process semcod projects with LLM
redsl batch semcod /path/to/semcod --max-actions 10

# Hybrid refactoring (no LLM) for semcod projects
redsl batch hybrid /path/to/semcod --max-changes 30

# Batch processing with JSON output
redsl batch semcod /path/to/semcod --format json

Python Code Quality Analysis

# Analyze code quality
redsl pyqual analyze ./my-project

# Analyze with custom config
redsl pyqual analyze ./my-project --config pyqual.yaml

# Get analysis in JSON format
redsl pyqual analyze ./my-project --format json

# Apply automatic fixes
redsl pyqual fix ./my-project

Debugging

# Check configuration
redsl debug config --show-env

# View DSL decisions for a project
redsl debug decisions ./my-project --limit 20

Advanced Usage

Using with CI/CD

# GitHub Actions example
- name: Run reDSL analysis
  run: |
    redsl refactor ./ --max-actions 5 --dry-run --format yaml > refactor-plan.yaml
    
- name: Upload refactoring plan
  uses: actions/upload-artifact@v3
  with:
    name: refactor-plan
    path: refactor-plan.yaml

Integration with Other Tools

# Use with jq for JSON processing
redsl refactor ./ --format json | jq '.refactoring_plan.decisions[] | select(.score > 1.0)'

# Pipe to file for review
redsl refactor ./ --format yaml > review-plan.yaml

# Extract only high-impact decisions
redsl refactor ./ --format yaml | yq '.refactoring_plan.decisions[] | select(.score > 1.5)'

Environment Configuration

Create .env file:

# LLM Configuration
OPENAI_API_KEY=your-api-key
REFACTOR_LLM_MODEL=openai/gpt-4
REFACTOR_DRY_RUN=false

# Custom settings
REFACTOR_MAX_ACTIONS=20
REFACTOR_REFLECTION_ROUNDS=2

Available Refactoring Actions

Simple Actions (no LLM)

  • REMOVE_UNUSED_IMPORTS - Remove unused imports
  • FIX_MODULE_EXECUTION_BLOCK - Fix module execution blocks
  • EXTRACT_CONSTANTS - Extract magic numbers to constants
  • ADD_RETURN_TYPES - Add return type annotations

Complex Actions (with LLM)

  • EXTRACT_FUNCTIONS - Extract high-complexity functions
  • SPLIT_MODULE - Split large modules
  • REDUCE_COMPLEXITY - Reduce cyclomatic complexity

REST API

Start the API server:

# Using uvicorn directly
uvicorn redsl.api:app --reload --host 0.0.0.0 --port 8000

# Using the CLI
redsl api --host 0.0.0.0 --port 8000

API Endpoints

Refactor a Project

curl -X POST "http://localhost:8000/refactor" \
  -H "Content-Type: application/json" \
  -d '{
    "project_path": "./my-project",
    "max_actions": 5,
    "dry_run": true,
    "format": "json"
  }'

Batch Processing

# Batch semcod processing
curl -X POST "http://localhost:8000/batch/semcod" \
  -H "Content-Type: application/json" \
  -d '{
    "semcod_root": "/path/to/semcod",
    "max_actions": 10,
    "format": "yaml"
  }'

# Hybrid batch processing
curl -X POST "http://localhost:8000/batch/hybrid" \
  -H "Content-Type: application/json" \
  -d '{
    "semcod_root": "/path/to/semcod",
    "max_changes": 30
  }'

Debug Endpoints

# Get configuration
curl "http://localhost:8000/debug/config?show_env=true"

# Get decisions for a project
curl "http://localhost:8000/debug/decisions?project_path=./my-project&limit=10"

Python Quality Analysis

# Analyze code quality
curl -X POST "http://localhost:8000/pyqual/analyze" \
  -H "Content-Type: application/json" \
  -d '{
    "project_path": "./my-project",
    "format": "json"
  }'

# Apply fixes
curl -X POST "http://localhost:8000/pyqual/fix" \
  -H "Content-Type: application/json" \
  -d '{
    "project_path": "./my-project"
  }'

Interactive API Documentation

Once the server is running, visit:

Architecture

┌─────────────────────────────────────────────────────┐
│                  ORCHESTRATOR                       │
│   (loop: analyze → decide → refactor → reflect)    │
├─────────────┬──────────────┬────────────────────────┤
│  ANALYZER   │  DSL ENGINE  │   REFACTOR ENGINE      │
│  ─ toon.yaml│  ─ rules     │   ─ patch generation   │
│  ─ linters  │  ─ scoring   │   ─ validation         │
│  ─ metrics  │  ─ planning  │   ─ application        │
├─────────────┴──────────────┴────────────────────────┤
│            HYBRID REFACTOR ENGINES                  │
│  ─ DirectRefactorEngine (no LLM)                   │
│  ─ LLM RefactorEngine (with reflection)            │
├─────────────────────────────────────────────────────┤
│                  LLM LAYER (LiteLLM)                │
│   ─ code generation  ─ reflection  ─ self-critique  │
├─────────────────────────────────────────────────────┤
│                 MEMORY SYSTEM                       │
│   ─ episodic (refactoring history)                 │
│   ─ semantic (patterns, rules)                     │
│   ─ procedural (strategies, plans)                 │
└─────────────────────────────────────────────────────┘

Configuration

Environment variables:

  • OPENAI_API_KEY or OPENROUTER_API_KEY — API key
  • REFACTOR_LLM_MODEL — LLM model (e.g., openrouter/openai/gpt-5.4-mini)
  • REFACTOR_DRY_RUN — test mode (true/false)

Examples

Directory Description
examples/01-basic-analysis/ Project analysis from toon.yaml files
examples/02-custom-rules/ Define custom DSL rules
examples/03-full-pipeline/ Full cycle: analyze → decide → refactor → reflect
examples/04-memory-learning/ Memory system: episodic, semantic, procedural

License

Apache License 2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

redsl-1.2.9.tar.gz (157.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

redsl-1.2.9-py3-none-any.whl (161.1 kB view details)

Uploaded Python 3

File details

Details for the file redsl-1.2.9.tar.gz.

File metadata

  • Download URL: redsl-1.2.9.tar.gz
  • Upload date:
  • Size: 157.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for redsl-1.2.9.tar.gz
Algorithm Hash digest
SHA256 9248cf790c9ebd4db8c4dbf55ac2093c0c4dada579a428a4bc463d97bb23f243
MD5 0e9d6ba93a34f09da42aae3520d03a58
BLAKE2b-256 b4f2c0a8d04c3a8497b4fea83e5e44582801cd03e4c92b601cb73465e437249a

See more details on using hashes here.

File details

Details for the file redsl-1.2.9-py3-none-any.whl.

File metadata

  • Download URL: redsl-1.2.9-py3-none-any.whl
  • Upload date:
  • Size: 161.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for redsl-1.2.9-py3-none-any.whl
Algorithm Hash digest
SHA256 78d47dfe6f900c66f314253fe336e929834fee5e658339cbc3a3e040f0a72ab0
MD5 572fdf3675594d0be1555a06094565c8
BLAKE2b-256 ea8b2d99fa98fc760dccc845fc37d452af0f74fd2cb81b03831376450787a7cd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page