Skip to content

Commit 9eacc0f

Browse files
draft
1 parent 980e79e commit 9eacc0f

19 files changed

Lines changed: 2297 additions & 1389 deletions

CHANGELOG.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,48 @@
1+
## [0.3.1] - 2026-03-01
2+
3+
### Summary
4+
5+
feat(flow): AST-based type inference + side-effect detection for enhanced CONTRACTS and DATA_TYPES
6+
7+
### Added
8+
9+
- **TypeInferenceEngine** (`analysis/type_inference.py`)
10+
- Parses `->` return annotations from source AST
11+
- Extracts argument type hints (`arg: Type`)
12+
- Fallback: infers types from function name patterns (`parse_*` → str→dict)
13+
- Batch mode: `extract_all_types()` for all project functions
14+
15+
- **SideEffectDetector** (`analysis/side_effects.py`)
16+
- AST scan: detects `open()`, `write()`, `self.x = ...`, `global`, `del`
17+
- Classification: IO / Cache / Mutation / Pure
18+
- `SideEffectInfo` with detailed breakdown and summary
19+
- Heuristic fallback when source files unavailable
20+
21+
- **26 new tests** (`tests/test_sprint2_flow.py`)
22+
- TypeInferenceEngine: annotation extraction, defaults, signatures, batch
23+
- SideEffectDetector: IO, pure, mutation, summary, batch, heuristic
24+
- FlowExporter integration: contracts IN/OUT, data types, edge cases
25+
26+
### Changed
27+
28+
- **Enhanced CONTRACTS section** in `flow.toon`
29+
- Per-stage: IN types, OUT type, SIDE-EFFECT summary
30+
- INVARIANT inference (normalize → `len(output) <= len(input)`)
31+
- SMELL markers for CC ≥ 15
32+
33+
- **Enhanced DATA_TYPES section** in `flow.toon`
34+
- Source counts: `[N annotated, M inferred / T functions]`
35+
- Hub-type split recommendations with named sub-interfaces
36+
- e.g. `AnalysisResult → split into: StructureResult, MetricsResult, FlowResult`
37+
38+
- **FlowExporter** now uses `TypeInferenceEngine` and `SideEffectDetector`
39+
- Typed signatures from AST (not just arg names)
40+
- Purity scoring from AST body scan (not just name heuristics)
41+
42+
- **Version bump** to 0.3.1
43+
44+
---
45+
146
## [0.3.0] - 2026-03-01
247

348
### Summary

README.md

Lines changed: 31 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,32 @@
11
# code2flow
22

3-
**Python Code Flow Analysis Tool** Static analysis for control flow graphs (CFG), data flow graphs (DFG), and call graph extraction with 4 purpose-built output formats.
3+
**Python Code Flow Analysis Tool** - Static analysis for control flow graphs (CFG), data flow graphs (DFG), and call graph extraction with optimized TOON format.
44

55
![img.png](img.png)
66

7-
## 🚀 New in v0.3.0: Format Taxonomy
7+
## 🚀 New: TOON Format v2
88

9-
**4 files, 4 purposes** — each format answers a different question:
9+
**TOON v2** is the default output format - scannable, severity-sorted, prompt-ready:
1010

11-
| Format | File | Purpose | Answers |
12-
|--------|------|---------|---------|
13-
| **Map** | `project.map` | Structure | "What exists and how it's connected?" |
14-
| **Toon** | `analysis.toon` | Health diagnostics | "What's broken and how to fix it?" |
15-
| **Flow** | `flow.toon` | Data flow | "How do data flow through the system?" |
16-
| **Context** | `context.md` | LLM narrative | "Understand the system to rebuild it" |
11+
- **🎯 Health-first design** - issues sorted by severity (🔴/🟡)
12+
- **📊 Coupling matrix** - fan-in/fan-out analysis
13+
- **🔍 Duplicate detection** - find identical classes
14+
- **📈 Layered architecture** - package-level metrics
15+
- **⚡ Inline markers** - `!!` (CC≥15), `!` (CC≥10), `×DUP`
16+
- **🚫 Smart filtering** - excludes venv, site-packages
17+
- **📋 Actionable REFACTOR** - concrete steps, not just problems
1718

1819
```bash
19-
# Default: health diagnostics only
20+
# Default: TOON format only
2021
code2flow /path/to/project
2122

22-
# Generate all 4 core formats
23-
code2flow /path/to/project -f toon,map,flow,context
24-
25-
# Generate everything (all 8 formats)
23+
# Generate all formats
2624
code2flow /path/to/project -f all
2725

28-
# Just the structural map
29-
code2flow /path/to/project -f map
30-
```
31-
32-
### When to Use Which Format
33-
34-
```
35-
"What's in the project?" → project.map
36-
"What's broken?" → analysis.toon (HEALTH)
37-
"How to fix it?" → analysis.toon (REFACTOR)
38-
"How do data flow through the system?"→ flow.toon (PIPELINES)
39-
"Where to split a type?" → flow.toon (DATA_TYPES)
40-
"Is the pipeline pure?" → flow.toon (CONTRACTS)
41-
"How to rebuild in another language?" → context.md
42-
"What depends on this module?" → analysis.toon (COUPLING)
43-
"What type does a function return?" → project.map (signatures)
26+
# TOON + YAML (for comparison)
27+
code2flow /path/to/project -f toon,yaml
4428
```
4529

46-
## 🎯 TOON v2 — Health Diagnostics
47-
48-
**TOON v2** (`analysis.toon`) is the default output — scannable, severity-sorted, prompt-ready:
49-
50-
- **🎯 Health-first design** — issues sorted by severity (🔴/🟡)
51-
- **📊 Coupling matrix** — fan-in/fan-out analysis
52-
- **🔍 Duplicate detection** — find identical classes
53-
- **📈 Layered architecture** — package-level metrics
54-
- **⚡ Inline markers**`!!` (CC≥15), `!` (CC≥10), `×DUP`
55-
- **🚫 Smart filtering** — excludes venv, site-packages
56-
- **📋 Actionable REFACTOR** — concrete steps, not just problems
57-
5830
## Performance Optimization
5931

6032
For large projects (>1000 functions), use **Fast Mode**:
@@ -171,26 +143,17 @@ code2flow /path/to/project -o my_analysis
171143

172144
## Output Files
173145

174-
### Core Formats (4 purpose-built files)
175-
176-
| File | Format | Purpose | Size |
177-
|------|--------|---------|------|
178-
| `analysis.toon` | **Toon** | Health diagnostics (HEALTH, REFACTOR, COUPLING) | ~25KB |
179-
| `project.map` | **Map** | Structural map (modules, imports, signatures) | ~23KB |
180-
| `flow.toon` | **Flow** | Data-flow analysis (PIPELINES, CONTRACTS, DATA_TYPES) | ~10KB |
181-
| `context.md` | **Context** | LLM narrative (architecture, patterns, API) | ~30KB |
182-
183-
### Additional Formats
184-
185146
| File | Description | Size |
186147
|------|-------------|------|
148+
| `analysis.toon` | **🎯 Optimized TOON format** (default) | ~200KB |
187149
| `analysis.yaml` | Complete structured analysis data | ~2.5MB |
188150
| `analysis.json` | JSON format for programmatic use | ~2.6MB |
189151
| `flow.mmd` | Full Mermaid flowchart (all nodes) | ~9KB |
190-
| `compact_flow.mmd` | Compact flowchart deduplicated nodes | ~9KB |
152+
| `compact_flow.mmd` | Compact flowchart - deduplicated nodes | ~9KB |
191153
| `calls.mmd` | Function call graph | ~9KB |
192154
| `cfg.png` | Control flow visualization | ~7MB |
193155
| `call_graph.png` | Call graph visualization | ~3.7MB |
156+
| `llm_prompt.md` | LLM-ready analysis summary | ~35KB |
194157

195158
## 🎯 TOON v2 Format Structure
196159

@@ -210,10 +173,10 @@ REFACTOR[4]:
210173
211174
COUPLING:
212175
┌─────────────┬──────────────────────────────────────┐
213-
│ Package │ fan-in fan-out status │
176+
│ Package │ fan-in fan-out status
214177
├─────────────┼──────────────────────────────────────┤
215178
│ core │ 12 45 !! split needed │
216-
│ exporters │ 5 28 hub │
179+
│ exporters │ 5 28 hub
217180
└─────────────┴──────────────────────────────────────┘
218181
219182
LAYERS:
@@ -360,12 +323,9 @@ The analyzer is designed to be extensible. Key areas for enhancement:
360323

361324
| Command | Output | Use Case |
362325
|---------|--------|----------|
363-
| `code2flow ./project` | `analysis.toon` | Quick health diagnostics (default) |
364-
| `code2flow ./project -f all` | All 8 formats | Complete analysis |
365-
| `code2flow ./project -f toon,map,flow,context` | 4 core formats | Full taxonomy |
366-
| `code2flow ./project -f map` | `project.map` | Structural map |
367-
| `code2flow ./project -f flow` | `flow.toon` | Data-flow analysis |
368-
| `code2flow ./project -f context` | `context.md` | LLM narrative |
326+
| `code2flow ./project` | `analysis.toon` | Quick analysis (default) |
327+
| `code2flow ./project -f all` | All formats | Complete analysis |
328+
| `code2flow ./project -f toon,yaml` | TOON + YAML | Comparison |
369329
| `code2flow ./project -m hybrid -v` | TOON + verbose | Detailed analysis |
370330
| `python validate_toon.py analysis.toon` | Validation | Quality check |
371331

@@ -475,19 +435,19 @@ code2flow ./project -f toon,yaml
475435
# Both formats available for comparison
476436
```
477437

478-
## 📋 TOON v2 Format Specification
438+
## 📋 TOON Format Specification
479439

480440
### File Structure
481441
```
482442
analysis.toon
483-
├── Header lines # Project summary + key metrics
484-
├── HEALTH # Highest-severity issues (🔴/🟡)
485-
├── REFACTOR # Actionable refactoring steps
486-
├── COUPLING # Package-level fan-in/fan-out summary
487-
├── LAYERS # Package hierarchy + inline markers
488-
├── FUNCTIONS # CC-filtered function list (focus on CC≥10)
489-
├── HOTSPOTS # Top fan-out functions
490-
└── CLASSES # Class-level complexity summary
443+
├── meta # Metadata (project, mode, timestamp)
444+
├── stats # Analysis statistics
445+
├── functions # Function analysis with complexity
446+
├── classes # Class information from function grouping
447+
├── modules # Module-level statistics
448+
├── patterns # Detected design patterns
449+
├── call_graph # Top 50 most important functions
450+
└── insights # Recommendations and summaries
491451
```
492452

493453
### Complexity Scoring

ROADMAP.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This document outlines planned features, improvements, and milestones for the code2flow project.
44

5-
## Current Status (v0.3.0)
5+
## Current Status (v0.3.1)
66

77
**Completed:**
88
- Core analysis engine with caching and parallel processing
@@ -15,9 +15,14 @@ This document outlines planned features, improvements, and milestones for the co
1515
- **Format Taxonomy (v0.3.0)** — 4 purpose-built output formats:
1616
- `project.map` — structural map (modules, imports, signatures, types)
1717
- `analysis.toon` — health diagnostics (HEALTH, REFACTOR, COUPLING, LAYERS)
18-
- `flow.toon`**NEW** data-flow analysis (PIPELINES, TRANSFORMS, CONTRACTS, DATA_TYPES)
18+
- `flow.toon` — data-flow analysis (PIPELINES, TRANSFORMS, CONTRACTS, DATA_TYPES)
1919
- `context.md` — LLM narrative (architecture, patterns, API surface)
2020
- CLI: `--format map,toon,flow,context,all`
21+
- **AST-based type inference + side-effect detection (v0.3.1)**:
22+
- `TypeInferenceEngine` — parses return annotations, argument types, name-based fallback
23+
- `SideEffectDetector` — AST scan for IO, cache, mutation, pure classification
24+
- Enhanced CONTRACTS: IN/OUT types, SIDE-EFFECT, INVARIANT, SMELL markers
25+
- Enhanced DATA_TYPES: source counts, hub-type split recommendations
2126

2227
---
2328

@@ -305,7 +310,7 @@ pattern:
305310
|---------|-------------|-------|--------|
306311
| v0.2.5 | Mar 2026 | TOON v2 format implementation | ✅ Done |
307312
| v0.3.0 | Mar 2026 | Format taxonomy (map, toon, flow, context) | ✅ Done |
308-
| v0.3.1 | Q2 2026 | CONTRACTS + DATA_TYPES enhancement | 📋 Planned |
313+
| v0.3.1 | Mar 2026 | CONTRACTS + DATA_TYPES enhancement (AST type inference, side-effect detection) | ✅ Done |
309314
| v0.4.0 | Q3 2026 | IDE integration, real-time analysis | 📋 Planned |
310315
| v0.5.0 | Q4 2026 | JS/TS support | 📋 Planned |
311316
| v0.6.0 | Q1 2027 | Enterprise features | 📋 Planned |

TODO.md

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,33 @@
99
- [x] Update CLI: `--format map,toon,flow,context,all`
1010
- [x] 4 files, 4 purposes: map (structure), toon (health), flow (data-flow), context (LLM)
1111

12-
## 🎯 Sprint 2 — CONTRACTS + DATA_TYPES (v0.3.1)
13-
14-
### High Priority
15-
16-
- [ ] **Type inference from AST**
17-
- Parse `->` return annotations
18-
- Parse arguments with type hints
19-
- Fallback: infer from names (`parse_*` → str input, `to_dict` → dict output)
20-
21-
- [ ] **CONTRACTS section enhancement**
22-
- Per-pipeline: input→output for each stage
23-
- Side-effect detection: `self.`, `.write`, `.save`, `cache`
24-
- Purity scoring: pure / IO / cache / mutation
25-
26-
- [ ] **DATA_TYPES section enhancement**
27-
- Count consumed/produced per type
28-
- Auto-detect hub-types (consumed ≥ 10)
29-
- Recommend split for hub-types
12+
## ✅ Completed — Sprint 2 (v0.3.1)
13+
14+
- [x] **Type inference from AST** (`analysis/type_inference.py`)
15+
- [x] Parse `->` return annotations
16+
- [x] Parse arguments with type hints
17+
- [x] Fallback: infer from names (`parse_*` → str input, `to_dict` → dict output)
18+
- [x] Batch mode: `extract_all_types()` for all project functions
19+
20+
- [x] **CONTRACTS section enhancement**
21+
- [x] Per-pipeline: IN types, OUT type for each stage
22+
- [x] Side-effect detection via AST: `self.`, `.write`, `open()`, `global`, `cache`
23+
- [x] Purity scoring: pure / IO / cache / mutation
24+
- [x] INVARIANT inference (normalize → `len(output) <= len(input)`)
25+
- [x] SMELL markers for CC ≥ 15
26+
27+
- [x] **DATA_TYPES section enhancement**
28+
- [x] Count consumed/produced per type (AST-based)
29+
- [x] Auto-detect hub-types (consumed ≥ 10)
30+
- [x] Hub-type split recommendations with named sub-interfaces
31+
- [x] Source counts: `[N annotated, M inferred / T functions]`
32+
33+
- [x] **SideEffectDetector** (`analysis/side_effects.py`)
34+
- [x] AST scan: `open()`, `write()`, `self.x = ...`, `global`, `del`
35+
- [x] Classification: IO / Cache / Mutation / Pure
36+
- [x] Heuristic fallback when source unavailable
37+
38+
- [x] **26 new tests** (`tests/test_sprint2_flow.py`)
3039

3140
## 🎯 Sprint 3 — PIPELINES auto-detection + SIDE_EFFECTS (v0.3.2)
3241

0 commit comments

Comments
 (0)