File size: 5,406 Bytes
eca5751
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
"""
Code Complexity Tool - Tính cyclomatic complexity cho Python functions.
Author: Hieu Louis (2026)

Cyclomatic complexity = 1 + số decision points:
  if/elif (each elif), for, while, except, with, assert,
  boolean and/or (n values → n-1), ternary if-exp, comprehension clauses.

Reference: McCabe (1976) — complexity ≥ 10 cần refactor.
"""
from __future__ import annotations

import ast
import json
from typing import Any, Dict, List, Optional

from .base import Tool, ToolResult, ToolContext, ToolCategory, ToolSafety


class _ComplexityVisitor(ast.NodeVisitor):
    """Đếm decision points trong một function body để tính cyclomatic complexity."""

    def __init__(self) -> None:
        self.complexity: int = 1  # baseline path

    def visit_If(self, n: ast.If) -> None:
        self.complexity += 1
        self.generic_visit(n)

    def visit_For(self, n: ast.For) -> None:
        self.complexity += 1
        self.generic_visit(n)

    def visit_AsyncFor(self, n: ast.AsyncFor) -> None:
        self.complexity += 1
        self.generic_visit(n)

    def visit_While(self, n: ast.While) -> None:
        self.complexity += 1
        self.generic_visit(n)

    def visit_ExceptHandler(self, n: ast.ExceptHandler) -> None:
        self.complexity += 1
        self.generic_visit(n)

    def visit_With(self, n: ast.With) -> None:
        self.complexity += 1
        self.generic_visit(n)

    def visit_AsyncWith(self, n: ast.AsyncWith) -> None:
        self.complexity += 1
        self.generic_visit(n)

    def visit_Assert(self, n: ast.Assert) -> None:
        self.complexity += 1
        self.generic_visit(n)

    def visit_BoolOp(self, n: ast.BoolOp) -> None:
        # `a and b and c` = 2 decision points
        self.complexity += max(0, len(n.values) - 1)
        self.generic_visit(n)

    def visit_IfExp(self, n: ast.IfExp) -> None:
        self.complexity += 1
        self.generic_visit(n)

    def visit_comprehension(self, n: ast.comprehension) -> None:
        # mỗi clause +1, mỗi if-condition +1
        self.complexity += 1 + len(n.ifs)
        self.generic_visit(n)


def _risk(c: int) -> str:
    """Phân loại risk theo complexity (McCabe thresholds)."""
    if c <= 5:
        return "low"
    if c <= 10:
        return "moderate"
    if c <= 20:
        return "high"
    return "very_high"


class CodeComplexityTool(Tool):
    """Tính cyclomatic complexity của các function trong Python file."""

    category = ToolCategory.CODE
    safety = ToolSafety.SAFE  # read-only analysis

    @property
    def name(self) -> str:
        return "code_complexity"

    @property
    def description(self) -> str:
        return (
            "Tính cyclomatic complexity của Python functions trong file. "
            "Complexity = 1 + số decision points (if/for/while/except/and/or...)."
        )

    @property
    def parameters(self) -> Dict[str, Any]:
        return {
            "type": "object",
            "properties": {
                "path": {"type": "string", "description": "File Python (.py) để phân tích"},
                "function": {
                    "type": "string",
                    "description": "Tên function cụ thể (optional). Mặc định tính tất cả.",
                },
            },
            "required": ["path"],
        }

    def validate_args(self, args: Dict[str, Any]) -> Optional[str]:
        if not args.get("path"):
            return "Missing required arg: path"
        return None

    def execute(self, args: Dict[str, Any], context: ToolContext) -> ToolResult:
        path: str = args["path"]
        target_fn: Optional[str] = args.get("function")

        try:
            with open(path, "r", encoding="utf-8") as f:
                source = f.read()
        except Exception as e:
            return ToolResult(success=False, error=f"Không đọc được file: {e}", return_code=1)

        try:
            tree = ast.parse(source)
        except SyntaxError as e:
            return ToolResult(
                success=False,
                error=f"SyntaxError line {e.lineno}: {e.msg}",
                return_code=1,
            )

        results: List[Dict[str, Any]] = []
        for node in ast.walk(tree):
            if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
                continue
            if target_fn and node.name != target_fn:
                continue
            visitor = _ComplexityVisitor()
            visitor.visit(node)
            results.append({
                "function": node.name,
                "line": node.lineno,
                "end_line": getattr(node, "end_lineno", node.lineno),
                "complexity": visitor.complexity,
                "risk": _risk(visitor.complexity),
            })

        if target_fn and not results:
            return ToolResult(
                success=False,
                error=f"Không tìm thấy function '{target_fn}' trong {path}",
                return_code=1,
            )

        return ToolResult(
            success=True,
            output=json.dumps(results, indent=2, ensure_ascii=False),
            metadata={
                "path": path,
                "function": target_fn,
                "count": len(results),
                "max_complexity": max((r["complexity"] for r in results), default=0),
            },
        )