Skip to main content

AI-powered Python code auditor for environmental efficiency

Project description

EcoCode: AI-Powered Python Code Auditor for Energy Efficiency

๐Ÿ† Mastering the Kiro Platform

This project was built entirely using Kiro's spec-driven development workflow, demonstrating how AI-assisted development can produce high-quality, well-tested software with formal correctness guarantees.

How We Used Kiro

1. Spec-Driven Development

We leveraged Kiro's structured spec workflow to transform a rough idea into a complete implementation:

  • Requirements Phase: Generated EARS-compliant requirements with INCOSE quality rules
  • Design Phase: Created comprehensive design documents with architecture diagrams, component interfaces, and data models
  • Tasks Phase: Broke down the design into incremental, actionable coding tasks

2. Property-Based Testing Integration

Kiro guided us through formal correctness properties:

  • 14 Correctness Properties defined in the design document
  • 21 Property-Based Tests using Hypothesis library
  • Each property validates specific requirements traceability

3. Iterative Implementation

Using Kiro's task execution workflow:

  • Tasks were implemented incrementally with checkpoints
  • Each component was tested before integration
  • Property tests caught edge cases that unit tests would miss

Project Structure

.kiro/
โ”œโ”€โ”€ hooks/
โ”‚   โ””โ”€โ”€ ecocode-green-audit.kiro.hook  # Auto-audit on file save
โ”œโ”€โ”€ steering/
โ”‚   โ””โ”€โ”€ green-coding.md                # Green coding best practices
โ”œโ”€โ”€ specs/
โ”‚   โ””โ”€โ”€ ecocode/
โ”‚       โ”œโ”€โ”€ requirements.md   # EARS-compliant requirements
โ”‚       โ”œโ”€โ”€ design.md         # Architecture & correctness properties
โ”‚       โ””โ”€โ”€ tasks.md          # Implementation checklist (all โœ…)
src/
โ””โ”€โ”€ ecocode/
    โ”œโ”€โ”€ analysis.py           # Pattern detection (AST-based)
    โ”œโ”€โ”€ auditor.py            # Main orchestration
    โ”œโ”€โ”€ models.py             # Data models
    โ”œโ”€โ”€ refactoring.py        # Refactor plan generation
    โ”œโ”€โ”€ reporter.py           # JSON/Console output
    โ”œโ”€โ”€ scoring.py            # Green Score calculation
    โ””โ”€โ”€ watcher.py            # File monitoring
tests/
โ”œโ”€โ”€ property/                 # Property-based tests (Hypothesis)
โ”œโ”€โ”€ unit/                     # Unit tests
โ””โ”€โ”€ fixtures/                 # Sample code for testing

Key Features Demonstrated

Feature Kiro Capability Used
Pattern Detection Spec-driven design with AST analysis
Green Score Property-based testing for score invariants
Refactoring Plans Requirements traceability in tasks
JSON Serialization Round-trip property testing
CLI Interface Incremental task implementation

๐Ÿค– Kiro Agent Integration

EcoCode is designed to be an agentic extension of the Kiro IDE:

  • Agent Hooks: Includes a pre-configured on_file_save hook in the .kiro/hooks/ folder. This triggers an automated audit every time a developer saves a Python file, delivering results directly to the Kiro Agent.

  • MCP Integration: The Kiro Agent can utilize MCP servers when interpreting EcoCode's JSON reports. This allows the AI to provide real-world carbon and cost savings estimates during the refactoring conversation.

  • Steering Files: Custom steering rules in .kiro/steering/ guide the agent to prioritize energy efficiency suggestions and follow green coding best practices.

Running the Project

# Install dependencies
pip install -e .

# Analyze a Python file
python -m ecocode --analyze your_file.py

# Output as JSON
python -m ecocode --analyze your_file.py --json

# Run all tests (21 property-based tests)
python -m pytest tests/ -v

Correctness Properties Validated

  1. Python File Filtering - Only .py files trigger analysis
  2. Issue Structure Completeness - All issues have required fields
  3. Green Score Range Invariant - Score always in [0, 100]
  4. Perfect Score for Clean Code - Empty issues gives 100
  5. Score Determinism - Same issues produce same score
  6. Severity Impact Ordering - Higher severity = higher penalty
  7. Score Breakdown Consistency - Penalties + score = 100
  8. RefactorPlan Completeness - All plans have required fields
  9. RefactorPlan Priority Ordering - Plans sorted by priority
  10. JSON Round-Trip Consistency - Serialize/deserialize preserves data
  11. Nested Loop Detection - Nested array loops are detected
  12. Redundant Computation Detection - Loop-invariant code detected
  13. Model Loading in Loop Detection - ML model loads in loops detected
  14. One RefactorPlan Per Issue - Plan count equals issue count

What Makes This Special

  • 100% Test Pass Rate: All 21 property-based tests pass
  • Formal Correctness: Properties derived from EARS requirements
  • Full Traceability: Every test maps to specific requirements
  • Clean Architecture: Modular design following spec-driven patterns

Built with Kiro - Where AI meets formal software engineering

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

ecocode-0.1.0.tar.gz (28.3 kB view details)

Uploaded Source

Built Distribution

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

ecocode-0.1.0-py3-none-any.whl (29.8 kB view details)

Uploaded Python 3

File details

Details for the file ecocode-0.1.0.tar.gz.

File metadata

  • Download URL: ecocode-0.1.0.tar.gz
  • Upload date:
  • Size: 28.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for ecocode-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0094e05e0a13c8ff08e20d96de9a5569da7dd19dbac8d189ab7ed27fa5fe9652
MD5 d53a322e6a1912273570f2051c443f20
BLAKE2b-256 9e6d76a0d9bf433d40eba5188fb2136c256bcf42b68e7beecdeab55bd245764f

See more details on using hashes here.

File details

Details for the file ecocode-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ecocode-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 29.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for ecocode-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0de16820058076c6b96349d4033be9949bb225184ece0321d74703171f362b84
MD5 bd0cbd9b7a121ff0025413bab4ceda36
BLAKE2b-256 bffac8e162ad58a46f88b262eafa9e9e1ecf53f07c59ce4301d05007014d7cb8

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