Scenario 18: Interactive Code Optimization¶
Developer or tech lead improving code quality with AI-powered suggestions and approval workflows.
Table of Contents¶
- Quick Start
- Overview
- Optimization Categories
- Approval Workflow
- Composite Mode (Orchestrator)
- CLI Commands
- TUI Commands
- Example Questions
- Related Scenarios
Quick Start¶
/select 18
Overview¶
Scenario 18 provides AI-powered code optimization with an interactive approval workflow. It analyzes code for performance, security, and readability improvements, then presents suggestions for review before applying changes.
User Query → Analysis Engine → Suggestions → Approval Workflow → Apply Edits
↓
┌──────────┬──────────┐
↓ ↓ ↓
Performance Security Readability
Optimization Categories¶
Performance Optimization¶
Identifies performance bottlenecks and inefficiencies:
> Analyze src/core/ for performance issues
╭─────────────── Performance Analysis ──────────────────────╮
│ │
│ Summary by Category: │
│ - Performance: 12 │
│ │
│ Suggestions: │
│ │
│ 1. [Warning] Loop-invariant computation │
│ File: `src/core/processor.py:45` │
│ ID: `opt_001` │
│ Move `len(data)` outside the loop │
│ │
│ 2. [Warning] Inefficient string concatenation │
│ File: `src/core/formatter.py:89` │
│ ID: `opt_002` │
│ Use list join instead of += in loop │
│ │
│ 3. [Suggestion] Missing cache decorator │
│ File: `src/core/calculator.py:34` │
│ ID: `opt_003` │
│ Add @lru_cache for pure function │
│ │
│ Use `/optimize approve --id <id>` to approve. │
│ │
╰────────────────────────────────────────────────────────────╯
Security Optimization¶
Identifies security vulnerabilities and hardening opportunities:
> Analyze src/api/ for security issues
╭─────────────── Security Analysis ─────────────────────────╮
│ │
│ Summary by Category: │
│ - Security: 8 │
│ │
│ Suggestions: │
│ │
│ 1. [Critical] SQL Injection Risk │
│ File: `src/api/user_handler.py:67` │
│ ID: `sec_001` │
│ Use parameterized queries │
│ │
│ 2. [Warning] Missing input validation │
│ File: `src/api/data_handler.py:23` │
│ ID: `sec_002` │
│ Add length check for user input │
│ │
│ 3. [Warning] Hardcoded timeout │
│ File: `src/api/client.py:45` │
│ ID: `sec_003` │
│ Use configurable timeout value │
│ │
│ Use `/optimize approve --id <id>` to approve. │
│ │
╰────────────────────────────────────────────────────────────╯
Readability Optimization¶
Identifies code smells and maintainability issues:
> Analyze src/utils/ for readability
╭─────────────── Readability Analysis ──────────────────────╮
│ │
│ Summary by Category: │
│ - Readability: 15 │
│ │
│ Suggestions: │
│ │
│ 1. [Suggestion] Long function (52 lines) │
│ File: `src/utils/parser.py:89` │
│ ID: `read_001` │
│ Extract helper functions │
│ │
│ 2. [Suggestion] Deeply nested conditionals │
│ File: `src/utils/validator.py:34` │
│ ID: `read_002` │
│ Use early returns to flatten logic │
│ │
│ 3. [Info] Missing docstring │
│ File: `src/utils/helpers.py:12` │
│ ID: `read_003` │
│ Add function documentation │
│ │
╰────────────────────────────────────────────────────────────╯
Approval Workflow¶
Review Suggestions¶
View detailed information about a suggestion:
> Show details for opt_001
╭─────────────── Suggestion Details ────────────────────────╮
│ │
│ ID: opt_001 │
│ Category: Performance │
│ Severity: Warning │
│ │
│ Title: Loop-invariant computation │
│ File: src/core/processor.py:45 │
│ │
│ Current code: │
│ ```python │
│ for i in range(len(data)): │
│ if i < len(data) - 1: │
│ process(data[i]) │
│ ``` │
│ │
│ Suggested fix: │
│ ```python │
│ data_len = len(data) │
│ for i in range(data_len): │
│ if i < data_len - 1: │
│ process(data[i]) │
│ ``` │
│ │
│ Impact: ~15% improvement in loop iterations │
│ Confidence: 0.95 │
│ │
╰────────────────────────────────────────────────────────────╯
Approve/Reject/Defer¶
> /optimize approve --id opt_001
╭─────────────── Suggestion Approved ───────────────────────╮
│ │
│ ID: opt_001 │
│ Status: APPROVED │
│ │
│ Edit queued. Use `/optimize apply` to apply all │
│ approved changes. │
│ │
╰────────────────────────────────────────────────────────────╯
> /optimize reject --id opt_002 --reason "Not applicable"
> /optimize defer --id opt_003 --until "2024-02-01"
Apply Approved Changes¶
> /optimize apply
╭─────────────── Applying Changes ──────────────────────────╮
│ │
│ Approved suggestions: 5 │
│ │
│ Applying opt_001... Done │
│ Applying sec_001... Done │
│ Applying read_001... Done │
│ Applying read_002... Done │
│ Applying sec_003... Done │
│ │
│ Summary: │
│ Successfully applied: 5 │
│ Failed: 0 │
│ Files modified: 4 │
│ │
│ Backups created in: .codegraph/backups/ │
│ │
╰────────────────────────────────────────────────────────────╯
Undo Applied Changes¶
> /optimize undo
╭─────────────── Undo Changes ──────────────────────────────╮
│ │
│ Last batch: 5 changes applied at 14:35:21 │
│ │
│ Reverting opt_001... Done │
│ Reverting sec_001... Done │
│ Reverting read_001... Done │
│ Reverting read_002... Done │
│ Reverting sec_003... Done │
│ │
│ All changes reverted successfully. │
│ │
╰────────────────────────────────────────────────────────────╯
Composite Mode (Orchestrator)¶
Scenario 18 can operate as a composite orchestrator, invoking multiple sub-scenarios for comprehensive analysis:
┌─────────────────────────────────────────────────────────────┐
│ S18 COMPOSITE ORCHESTRATOR │
│ │
│ ┌────────────────────────────────────────────────────────┐ │
│ │ SCENARIO INVOCATION │ │
│ │ ┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐ │ │
│ │ │ S02 │ │ S05 │ │ S06 │ │ S11 │ │ S12 │ │ │
│ │ │Sec. │ │Refac│ │Perf │ │Arch │ │Debt │ │ │
│ │ └──┬──┘ └──┬──┘ └──┬──┘ └──┬──┘ └──┬──┘ │ │
│ └─────┼────────┼───────┼────────┼────────┼──────────────┘ │
│ │ │ │ │ │ │
│ └────────┴───────┴────────┴────────┘ │
│ ↓ │
│ ┌────────────────────────────────────────────────────────┐ │
│ │ RESULT MERGER │ │
│ │ (deduplication, conflict resolution) │ │
│ └────────────────────────────────────────────────────────┘ │
│ ↓ │
│ ┌────────────────────────────────────────────────────────┐ │
│ │ PRIORITY CALCULATOR │ │
│ │ (severity × impact × roi × confidence × consensus) │ │
│ └────────────────────────────────────────────────────────┘ │
│ ↓ │
│ UNIFIED FINDINGS │
└─────────────────────────────────────────────────────────────┘
Sub-Scenarios¶
| Scenario | Weight | Contribution |
|---|---|---|
| S02 (Security Audit) | 1.5 | Security vulnerabilities |
| S05 (Refactoring) | 1.0 | Code smells, dead code |
| S06 (Performance) | 1.2 | Performance bottlenecks |
| S11 (Architecture) | 1.1 | Architecture violations |
| S12 (Tech Debt) | 0.9 | Technical debt items |
Running Composite Mode¶
# CLI
codegraph composition run "Optimize src/" -o s18
# API
POST /api/v1/composition/query
{
"query": "Optimize src/",
"orchestrator": "scenario_18"
}
Merge Strategies¶
| Strategy | Description |
|---|---|
union |
Include all findings (default) |
intersection |
Only findings from 2+ scenarios |
weighted |
Weight by scenario priority |
consensus |
Only findings with agreement |
CLI Commands¶
# Analyze for all categories
codegraph optimize analyze src/
# Analyze for specific category
codegraph optimize analyze src/ --performance
codegraph optimize analyze src/ --security
codegraph optimize analyze src/ --readability
# List pending suggestions
codegraph optimize list
# Show suggestion details
codegraph optimize show --id opt_001
# Approve suggestion
codegraph optimize approve --id opt_001
# Reject suggestion
codegraph optimize reject --id opt_002 --reason "Not needed"
# Apply all approved
codegraph optimize apply
# Undo last batch
codegraph optimize undo
TUI Commands¶
| Command | Description |
|---|---|
/optimize analyze <path> |
Analyze for optimizations |
/optimize list |
List pending suggestions |
/optimize show --id <id> |
Show suggestion details |
/optimize approve --id <id> |
Approve suggestion |
/optimize reject --id <id> |
Reject suggestion |
/optimize defer --id <id> |
Defer for later |
/optimize apply |
Apply approved changes |
/optimize undo |
Undo last batch |
Example Questions¶
- “Optimize src/core/ for performance”
- “Find security improvements in src/api/”
- “Analyze src/utils/ for readability issues”
- “Show all pending optimization suggestions”
- “Approve suggestion opt_001”
- “Apply all approved optimizations”
- “What would be the impact of fixing sec_001?”
Configuration¶
Configure optimization behavior in config.yaml:
scenarios:
code_optimization:
categories:
- performance
- security
- readability
severity_threshold: info # info, warning, critical
max_suggestions: 50
auto_backup: true
composite:
enabled: true
sub_scenarios:
- scenario_02 # Security
- scenario_05 # Refactoring
- scenario_06 # Performance
- scenario_11 # Architecture
- scenario_12 # Tech Debt
parallel_execution: true
merge_strategy: weighted
Related Scenarios¶
- Security Audit - Security vulnerability scanning
- Refactoring - Code cleanup
- Performance - Performance analysis
- File Editing - Apply code changes
- Composite Workflows - Orchestration details