@dtt_siye/atool 1.3.1 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/README.md +97 -214
  2. package/README.md.atool-backup.20260410_114701 +299 -0
  3. package/VERSION +1 -1
  4. package/bin/atool.js +55 -9
  5. package/hooks/doc-sync-reminder +4 -4
  6. package/hooks/hooks-cursor.json +20 -0
  7. package/hooks/hooks.json +21 -1
  8. package/hooks/pre-commit +191 -0
  9. package/hooks/prompt-guard +84 -35
  10. package/hooks/session-start +34 -12
  11. package/hooks/task-state-tracker +145 -0
  12. package/install.sh +14 -4
  13. package/lib/common.sh +36 -23
  14. package/lib/compute-importance.sh +73 -0
  15. package/lib/install-cursor.sh +24 -2
  16. package/lib/install-hooks.sh +64 -0
  17. package/lib/install-kiro.sh +26 -2
  18. package/lib/install-skills.sh +5 -2
  19. package/lib/pre-scan.sh +13 -1
  20. package/lib/project-init.sh +28 -9
  21. package/package.json +1 -1
  22. package/skills/agent-audit/SKILL.md +180 -0
  23. package/skills/ai-project-architecture/SKILL.md +33 -534
  24. package/skills/ai-project-architecture/rules/architecture-validation.md +200 -0
  25. package/skills/ai-project-architecture/rules/compliance-check.md +83 -0
  26. package/skills/ai-project-architecture/rules/iron-laws.md +188 -0
  27. package/skills/ai-project-architecture/rules/migration.md +94 -0
  28. package/skills/ai-project-architecture/rules/refactoring.md +91 -0
  29. package/skills/ai-project-architecture/rules/testing.md +249 -0
  30. package/skills/ai-project-architecture/rules/verification.md +111 -0
  31. package/skills/architecture-guard/SKILL.md +164 -0
  32. package/skills/architecture-guard/rules/violation-detection.md +90 -0
  33. package/skills/atool-init/SKILL.md +24 -4
  34. package/skills/ci-feedback/SKILL.md +165 -0
  35. package/skills/project-analyze/SKILL.md +129 -19
  36. package/skills/project-analyze/phases/phase1-setup.md +76 -5
  37. package/skills/project-analyze/phases/phase2-understand.md +137 -26
  38. package/skills/project-analyze/phases/phase2.5-refine.md +32 -23
  39. package/skills/project-analyze/phases/phase3-graph.md +39 -5
  40. package/skills/project-analyze/phases/phase4-synthesize.md +17 -1
  41. package/skills/project-analyze/phases/phase5-export.md +42 -4
  42. package/skills/project-analyze/prompts/understand-agent.md +156 -298
  43. package/skills/project-analyze/rules/java.md +69 -1
  44. package/skills/project-query/SKILL.md +91 -200
  45. package/skills/project-query/rules/aggregate-stats.md +301 -0
  46. package/skills/project-query/rules/data-lineage.md +228 -0
  47. package/skills/project-query/rules/impact-analysis.md +218 -0
  48. package/skills/project-query/rules/neighborhood.md +234 -0
  49. package/skills/project-query/rules/node-lookup.md +97 -0
  50. package/skills/project-query/rules/path-query.md +135 -0
  51. package/skills/software-architecture/SKILL.md +39 -501
  52. package/skills/software-architecture/rules/concurrency-ha.md +346 -0
  53. package/skills/software-architecture/rules/ddd.md +450 -0
  54. package/skills/software-architecture/rules/decision-workflow.md +155 -0
  55. package/skills/software-architecture/rules/deployment.md +508 -0
  56. package/skills/software-architecture/rules/styles.md +232 -0
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  name: project-query
3
3
  description: 在需要查询项目分析结果时使用 — 支持节点查找、调用链追踪、影响分析、数据血缘、邻域查询、聚合统计,支持 G1-G5 五级粒度缩放。Use when querying analysis results — supports node lookup, call chain tracing, impact analysis, data lineage, neighborhood queries, aggregate statistics with G1-G5 granularity zooming.
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  category: quality
6
6
  ---
7
7
 
@@ -9,216 +9,107 @@ category: quality
9
9
 
10
10
  ## 概述
11
11
 
12
- 本 skill 提供对已完成 `project-analyze` 分析的项目进行交互式查询。基于 `.atool-docs/knowledge-graph.json` 和 `.atool-docs/multi-dimensional-analysis.json`,支持 6 类查询和 5 级粒度缩放。
12
+ 本 skill 提供 `/project-analyze` 完成分析后的 **深度交互式查询**。基于 knowledge-graph.json,支持 6 类查询能力、5 级粒度缩放(G1-G5),丰富的可视化输出。
13
+
14
+ **核心价值**:快速回答关于代码结构的复杂问题,无需手工扫描源码。
13
15
 
14
16
  ## 前置条件
15
17
 
16
- - 项目已通过 `/project-analyze` 完成分析
17
- - 存在 `.atool-docs/knowledge-graph.json`(v4.0+ 或 v5.0)
18
- - 如果需要数据流和调用链查询,需要 L2+ 深度的分析结果
19
-
20
- ## 查询类别
21
-
22
- ### 1. 节点查找 (Node Lookup)
23
-
24
- 查找特定类型的节点。
25
-
26
- ```
27
- /find all functions named createUser
28
- /find all classes in auth module
29
- /find all API endpoints
30
- /find modules with highest coupling
31
- ```
32
-
33
- **执行步骤**:
34
- 1. 读取 `knowledge-graph.json` `nodes` 数组
35
- 2. `type` `label` 过滤(支持子串匹配)
36
- 3. 输出匹配节点的列表(ID、名称、路径、重要度)
37
- 4. 如果结果 > 20 个,按 importance 降序排列,仅展示 top 20
38
-
39
- ### 2. 路径查询 (Path Query)
40
-
41
- 查找两个节点之间的调用路径。
42
-
43
- ```
44
- /show call path from UserController.createUser to UserRepository.save
45
- /show dependency path from module-a to module-b
46
- ```
47
-
48
- **执行步骤**:
49
- 1. 在 knowledge-graph 中定位 source 和 target 节点
50
- 2. 根据 edge 类型选择子图:
51
- - `call path` → `calls` + `calls_async` 边
52
- - `dependency path` → `depends_on` 边
53
- 3. 执行 BFS 从 source 到 target
54
- 4. 输出路径(每步包含:节点名称、边类型、行号)
55
- 5. 生成 Mermaid `graph LR`
56
- 6. 如果有多条路径,展示所有(最多 5 条)
57
-
58
- ### 3. 影响分析 (Impact Analysis)
59
-
60
- 分析修改某个节点后的影响范围。
61
-
62
- ```
63
- /what changes if I modify User entity
64
- /impact of changing AuthService
65
- ```
66
-
67
- **执行步骤**:
68
- 1. 定位目标节点 X
69
- 2. 查找所有反向边(其他节点 → X):`depends_on`, `calls`, `reads_from`, `reads_state`, `implements`, `extends`
70
- 3. 对每个依赖节点 D,递归查找 D 的依赖者(最多 3 层)
71
- 4. 计算风险评分:
72
- - `risk(X) = Σ importance(D) × weight(D→X)` 对所有直接依赖者 D
73
- - risk > 0.7 = **HIGH**(红色标记)
74
- - 0.3-0.7 = **MEDIUM**(橙色标记)
75
- - < 0.3 = **LOW**(绿色标记)
76
- 5. 统计受影响的:模块数、函数数、API 端点数、数据实体数
77
- 6. 生成影响范围 Mermaid 图(G1 视角 = 模块级,G3 视角 = 函数级)
78
- 7. 输出风险摘要 + 建议的测试范围
79
-
80
- ### 4. 数据血缘 (Data Lineage)
81
-
82
- 追踪数据的来源和去向。
83
-
84
- ```
85
- /trace data lineage of Order entity
86
- /where does userData come from and where does it flow
87
- ```
88
-
89
- **执行步骤**:
90
- 1. 定位数据实体节点 D
91
- 2. **反向追踪**(来源):沿 `writes_to`, `writes_state`, `transforms` 边反向 BFS
92
- - 找到所有写入 D 的函数/服务
93
- - 继续追踪这些函数的数据来源
94
- - 直到找到外部输入(API、用户输入、定时任务)
95
- 3. **正向追踪**(去向):沿 `reads_from`, `reads_state`, `transforms`, `persists_to` 边正向 BFS
96
- - 找到所有读取 D 的函数/服务
97
- - 继续追踪这些函数的输出去向
98
- - 直到找到最终汇聚点(数据库、外部 API、日志)
99
- 4. 生成数据血缘图:
100
- - Mermaid `graph TD` 图,数据节点高亮
101
- - 每步标注操作类型(read/write/transform/persist)
102
- 5. 输出数据流描述文本
103
-
104
- ### 5. 邻域查询 (Neighborhood)
105
-
106
- 查看某个节点周围 N 跳的所有关联。
107
-
108
- ```
109
- /show everything connected to AuthService within 2 hops
110
- /neighbors of UserController.createUser
111
- ```
112
-
113
- **执行步骤**:
114
- 1. 定位焦点节点 F
115
- 2. 无向 BFS(不区分边的方向)展开 N 跳(默认 2,最大 5)
116
- 3. 按当前粒度级别过滤节点/边类型
117
- 4. 输出:
118
- - 邻域图(Mermaid)
119
- - 每个邻居节点与焦点的关系说明
120
- - 按关系类型分组统计
121
-
122
- ### 6. 聚合查询 (Aggregate)
123
-
124
- 按度量值排序和筛选节点。
125
-
126
- ```
127
- /which module has highest coupling
128
- /top 10 most complex functions
129
- /modules ranked by importance
130
- /show all nodes with Critical risk level
131
- ```
132
-
133
- **支持的度量**:
134
- - `coupling` → instability (I), efferent coupling (Ce), afferent coupling (Ca)
135
- - `complexity` → cyclomatic_complexity, halstead_volume
136
- - `importance` → composite importance score
137
- - `quality` → maintainability_index, technical_debt_ratio
138
- - `centrality` → betweenness, in_degree, out_degree
139
- - `data_flow` → flow_rate, schema_coupling
140
-
141
- **执行步骤**:
142
- 1. 解析查询的度量和排序方向
143
- 2. 从 knowledge-graph.json 读取所有节点的 metrics
144
- 3. 按指定度量排序
145
- 4. 输出 top N 结果(默认 10,最大 50)
146
- 5. 生成柱状图 ASCII 或 Mermaid
147
-
148
- ## 粒度缩放 (Zoom Level)
149
-
150
- 查询时通过 `zoom` 参数控制结果粒度:
151
-
152
- | 缩放级别 | 可见节点类型 | 可见边类型 | 适用场景 |
153
- |---------|------------|-----------|---------|
154
- | G1 (System) | module, layer | depends_on | 架构全景 |
155
- | G2 (Module) | + class, service, store, interface | + implements, extends, belongs_to | 模块内部结构 |
156
- | G3 (Function) | + function, api, route, hook | + calls, calls_async, returns, sends_http | 调用链追踪 |
157
- | G4 (Data) | + data_entity | + reads_from, writes_to, transforms, validates, reads_state, writes_state | 数据流分析 |
158
- | G5 (Cross-cutting) | 所有类型 | 所有边类型 | 全景视图 |
159
-
160
- **缩放实现**:
161
- - 读取 knowledge-graph.json
162
- - 按级别过滤节点类型 → 按级别过滤边类型
163
- - 从焦点节点 BFS 展开(如有焦点)
164
- - 返回过滤后的子图
18
+ - 项目已通过 `/project-analyze` 完成分析(L1-L5 任意深度)
19
+ - 存在 `.atool-docs/knowledge-graph.json`(v5.0+ 格式)
20
+ - L2+ 深度的分析(推荐)— 更多节点和边信息,查询结果更丰富
21
+ - ⚠️ L1 深度可用但结果只包含模块级节点(无函数、API 等细粒度)
22
+
23
+ ## When to Use
24
+
25
+ - **节点查找**:寻找特定函数、类、模块、API 端点
26
+ - `/find functions named createUser`
27
+ - `/find classes in auth module`
28
+ - `/find modules with importance > 0.7`
29
+
30
+ - **路径查询**:追踪两个节点之间的调用链或依赖链
31
+ - `/show call path from A to B`
32
+ - `/show dependency path from module-auth to module-api`
33
+
34
+ - **影响分析**:评估修改某个节点的风险和影响范围
35
+ - `/what if I modify User entity`
36
+ - `/impact of changing AuthService`
37
+ - `/change analysis for database schema`
38
+
39
+ - **数据血缘**:追踪数据实体从源头到终点的完整流向
40
+ - `/trace data lineage of Order entity`
41
+ - `/where does User data flow`
42
+
43
+ - **邻域查询**:查看某个节点周围 N 跳的所有直接和间接关联
44
+ - `/neighbors of AuthService within 2 hops`
45
+ - `/what's connected to UserController`
46
+
47
+ - **聚合查询**:按各种度量指标排序和统计节点
48
+ - `/top 10 most complex functions`
49
+ - `/modules ranked by importance`
50
+ - `/functions with cyclomatic_complexity > 10`
51
+
52
+ ## Quick Workflow
53
+
54
+ 1. **识别查询类型** 确定要执行的查询:节点、路径、影响、数据流、邻域或聚合
55
+ 2. **准备输入参数** 确定查询的源节点、目标节点、过滤条件、粒度级别(G1-G5)
56
+ 3. **执行查询** — 读取 knowledge-graph.json,应用对应查询模板
57
+ 4. **生成结果** — 返回结构化摘要 + Mermaid + ASCII 视图 + 详细表格
58
+ 5. **解释洞察** 提供分析建议和下一步操作
59
+
60
+ ## File Reference Map
61
+
62
+ | File | When to Read | Content |
63
+ |------|-------------|---------|
64
+ | `rules/node-lookup.md` | 查找特定代码元素时 | 节点查找的详细步骤、jq 实现、用户指令示例 |
65
+ | `rules/path-query.md` | 追踪调用链或依赖链时 | 路径查询算法、边类型支持、调用链可视化 |
66
+ | `rules/impact-analysis.md` | 评估修改风险时 | 风险评分公式、影响范围统计、测试建议 |
67
+ | `rules/data-lineage.md` | 理解数据流向时 | 数据流追踪算法、流向图生成、流量计算 |
68
+ | `rules/neighborhood.md` | 分析节点关联时 | BFS 邻域算法、粒度级别过滤、关系统计 |
69
+ | `rules/aggregate-stats.md` | 按指标排序统计时 | 支持的度量指标、过滤条件、风险等级判断 |
70
+
71
+ ## Constraints
72
+
73
+ - 必须先运行 `/project-analyze` 生成 knowledge-graph.json
74
+ - 查询失败时使用 `/find` 确认节点存在
75
+ - 复杂查询(如 G5)可能需要长时间,建议降低粒度
76
+ - 某些度量需要 L2+ 深度,如结果缺失请重新运行更深的分析
77
+
78
+ > IMPORTANT: When this skill is invoked, you MUST read the relevant
79
+ > subdirectory files based on the user's query type. Do NOT attempt to execute
80
+ > this skill using only this SKILL.md content.
165
81
 
166
82
  ## 输出格式
167
83
 
168
- 每个查询结果包含以下部分:
169
-
170
- ### 1. 结构化摘要
171
- ```
172
- ## Query Result: Impact Analysis on User entity
173
-
174
- **Risk Level**: HIGH (0.82)
175
- **Affected Modules**: 5
176
- **Affected Functions**: 23
177
- **Affected APIs**: 3
178
- ```
179
-
180
- ### 2. Mermaid 可视化
181
- 根据查询类型生成对应的 Mermaid 图:
182
- - 路径查询 → `graph LR`(从左到右的有向图)
183
- - 影响分析 → `graph TD`(从上到下的辐射图)
184
- - 数据血缘 → `graph TD`(数据流图)
185
- - 邻域查询 → `graph TD`(星形图)
186
- - 聚合查询 → 柱状图或表格
187
-
188
- ### 3. ASCII 视图(终端兼容)
189
- ```
190
- UserController.create() --calls--> UserService.createUser()
191
- └── UserService.createUser() --calls--> UserRepository.save()
192
- └── UserRepository.save() --persists_to--> [DB: users]
193
- ```
194
-
195
- ### 4. 明细表格
196
- | 节点 | 类型 | 度量 | 关系 |
197
- |------|------|------|------|
198
-
199
- ## 多维分析查询
200
-
201
- 如果 `.atool-docs/multi-dimensional-analysis.json` 存在,还可以查询:
202
-
203
- ```
204
- /show structural analysis results
205
- /show call graph hotspots
206
- /show data flow hotspots
207
- /show quality metrics summary
208
- /show domain vocabulary
209
- ```
84
+ 每个查询结果包含:
85
+ - **结构化摘要**:查询类型、关键指标、数量统计
86
+ - **Mermaid 可视化**:根据查询类型生成相应图表
87
+ - **ASCII 视图**:终端兼容的文本视图
88
+ - **详细表格**:完整数据表格
89
+ - **建议和洞察**:风险提示、优化建议、下一步行动
90
+
91
+ ## 粒度缩放
92
+
93
+ 通过 `zoom` 或 `G` 参数控制结果粒度:
94
+ - **G1** (System):模块、层的高层架构视图
95
+ - **G2** (Module):模块内部结构,包含类、服务
96
+ - **G3** (Function):调用链,包含函数、API 端点
97
+ - **G4** (Data):数据流,包含数据实体
98
+ - **G5** (Cross-cutting):全部分析,所有节点和边类型
210
99
 
211
100
  ## Skill 协作
212
101
 
213
102
  | 协作 Skill | 触发条件 | 交互方式 |
214
103
  |-----------|---------|---------|
215
- | project-analyze | 需要生成 knowledge-graph.json | 前置依赖 |
216
- | code-review | 需要更详细的代码质量分析 | 建议运行 |
217
- | software-architecture | 影响分析显示架构问题 | 建议运行 |
104
+ | **project-analyze** | 需要生成 knowledge-graph.json | 前置依赖 — 必须先运行 `/project-analyze` 生成数据 |
105
+ | **code-review** | 影响分析显示代码质量问题 | 建议运行 — 用于深度代码质量评分 |
106
+ | **software-architecture** | 影响分析显示架构违反 | 建议运行 — 用于架构重构或规范化 |
218
107
 
219
108
  ## 错误处理
220
109
 
221
- - 如果 `knowledge-graph.json` 不存在 提示用户先运行 `/project-analyze`
222
- - 如果查询的节点不存在 → 列出最接近的匹配建议(模糊搜索)
223
- - 如果查询的度量不存在 列出可用的度量列表
224
- - 如果结果为空 建议更高的分析深度(L2+ 推荐用于路径查询)
110
+ | 错误 | 原因 | 解决方案 |
111
+ |------|------|--------|
112
+ | `knowledge-graph.json not found` | 项目未分析 | 运行 `/project-analyze` |
113
+ | `node not found` | 查询的节点不存在 | 使用 `/find` 查询节点确切名称 |
114
+ | `no results` | 查询条件过严格 | 放松过滤条件,或提升分析深度(L3+) |
115
+ | `timeout` | 查询太复杂 | 降低粒度级别(G5 → G3),限制查询范围 |
@@ -0,0 +1,301 @@
1
+ # Aggregate Query - 聚合查询
2
+
3
+ ## 支持的度量指标
4
+
5
+ ### 复杂度指标
6
+ | 度量 | 解释 | 高风险阈值 | 数据来源 |
7
+ |------|------|-----------|---------|
8
+ | `cyclomatic_complexity` | 圈复杂度 | > 10 | Static Analysis |
9
+ | `halstead_volume` | 代码体积 | > 8 | Static Analysis |
10
+ | `lines_of_code` | 函数行数 | > 200 | Static Analysis |
11
+
12
+ ### 耦合指标
13
+ | 度量 | 解释 | 高风险阈值 | 数据来源 |
14
+ |------|------|-----------|---------|
15
+ | `coupling (Ca)` | 传入耦合(有多少依赖我) | > 0.7 | Dependency Graph |
16
+ | `coupling (Ce)` | 传出耦合(我依赖多少) | > 0.7 | Dependency Graph |
17
+ | `instability (I)` | 不稳定性 Ce/(Ca+Ce) | > 0.8 | Dependency Graph |
18
+
19
+ ### 重要性指标
20
+ | 度量 | 解释 | 高风险阈值 | 数据来源 |
21
+ |------|------|-----------|---------|
22
+ | `importance` | 综合重要性评分 | > 0.8 | Weighted Metrics |
23
+ | `betweenness_centrality` | 介数中心性(关键路由节点) | > 0.5 | Network Analysis |
24
+ | `in_degree` | 入度(被依赖次数) | > 20 | Dependency Graph |
25
+ | `out_degree` | 出度(依赖他人次数) | > 20 | Dependency Graph |
26
+
27
+ ### 质量指标
28
+ | 度量 | 解释 | 高风险阈值 | 数据来源 |
29
+ |------|------|-----------|---------|
30
+ | `maintainability_index` | 可维护性指数 | < 70 | Static Analysis |
31
+ | `technical_debt_ratio` | 技术债占比 | > 0.2 | Code Analysis |
32
+ | `code_duplication_ratio` | 重复代码占比 | > 0.1 | Duplication Analysis |
33
+
34
+ ### 数据流指标
35
+ | 度量 | 解释 | 高风险阈值 | 数据来源 |
36
+ |------|------|-----------|---------|
37
+ | `flow_rate` | 数据流量 | > 100 | Data Flow Analysis |
38
+ | `schema_coupling` | 数据耦合度 | > 0.5 | Schema Analysis |
39
+
40
+ ## 执行步骤
41
+
42
+ ### 1. 解析查询条件
43
+ ```bash
44
+ /top 10 most complex functions
45
+ # metric=cyclomatic_complexity, direction=desc, limit=10, filter=
46
+
47
+ /modules ranked by importance
48
+ # metric=importance, direction=desc, limit=*, filter=type=module
49
+
50
+ /which module has highest coupling
51
+ # metric=coupling (Ce), direction=desc, limit=1, filter=type=module
52
+
53
+ /functions with cyclomatic_complexity > 10
54
+ # metric=cyclomatic_complexity, direction=desc, limit=*, filter=>10
55
+
56
+ /show all nodes with criticality CRITICAL
57
+ # metric=criticality, direction=desc, limit=*, filter=CRITICAL
58
+ ```
59
+
60
+ ### 2. 从 knowledge-graph.json 加载所有节点
61
+ ```json
62
+ {
63
+ "nodes": [
64
+ {
65
+ "id": "func:UserService.createUser",
66
+ "label": "createUser",
67
+ "type": "function",
68
+ "importance": 0.85,
69
+ "cyclomatic_complexity": 8,
70
+ "lines_of_code": 42,
71
+ "maintainability_index": 75,
72
+ ...
73
+ }
74
+ ]
75
+ }
76
+ ```
77
+
78
+ ### 3. 应用过滤条件
79
+ - 数值过滤:`cyclomatic_complexity > 10`
80
+ - 类型过滤:`type=module`
81
+ - 组合过滤:`importance > 0.7 AND type=function`
82
+
83
+ ### 4. 按指定度量排序
84
+ - 降序:`cyclomatic_complexity DESC`
85
+ - 升序:`lines_of_code ASC`
86
+ - 多级排序:`importance DESC, cyclomatic_complexity ASC`
87
+
88
+ ### 5. 取 top N(默认 10,最大 50)
89
+ ```
90
+ top 10: 前 10 个结果
91
+ top 50: 前 50 个结果
92
+ *:全部结果(谨慎使用)
93
+ ```
94
+
95
+ ### 6. 生成柱状图和排序表格
96
+ - ASCII 柱状图:终端友好
97
+ - Mermaid 柱状图:可视化展示
98
+ - 详细表格:完整数据展示
99
+
100
+ ## jq 实现
101
+
102
+ Template 6 (aggregate):
103
+ ```jq
104
+ # 应用过滤条件
105
+ def apply_filters($metric, $filter, $node_type):
106
+ $nodes | map(select(
107
+ .type == $node_type and
108
+ ($metric != "" and (.[$metric] | tonumber) >= $filter)
109
+ ));
110
+ end;
111
+
112
+ # 按度量排序
113
+ def sort_by_metric($metric, $direction="desc"):
114
+ if $direction == "desc" then
115
+ sort_by(.[$metric] | tonumber) | reverse
116
+ else
117
+ sort_by(.[$metric] | tonumber)
118
+ end;
119
+ end;
120
+
121
+ # 生成 ASCII 柱状图
122
+ def generate_ascii_chart($nodes, $metric, $max_length=40):
123
+ $max_value = $nodes | map(.[$metric] | tonumber) | max;
124
+ $nodes | map({
125
+ label: (.label | sub(".+:"; "") | .[:15] + "..."), # 截断标签
126
+ value: .[$metric] | tonumber,
127
+ bar_length: (.[ $metric] | tonumber * $max_length / $max_value | floor)
128
+ }) | map("[\(.label)] \(.bar_length * "█" + ($max_length - .bar_length) * " ") \(.value)");
129
+ end;
130
+
131
+ # 生成结果表格
132
+ def generate_result_table($nodes, $metric, $title):
133
+ if ($nodes | length) == 0 then
134
+ "No results found."
135
+ else
136
+ header = ["Rank", "Label", "Type", $metric | ascii_upcase, "Risk Level"];
137
+ rows = $nodes | map(. as $node | [
138
+ $rank,
139
+ $node.label,
140
+ $node.type,
141
+ $node[$metric] | tostring,
142
+ get_risk_level($node[$metric], $metric)
143
+ ]);
144
+
145
+ header + [""] + rows
146
+ end;
147
+ end;
148
+
149
+ # 获取风险等级
150
+ def get_risk_level($value, $metric):
151
+ case $metric
152
+ when "cyclomatic_complexity" then
153
+ if $value > 10 then "🔴 CRITICAL"
154
+ elif $value > 7 then "🟠 HIGH"
155
+ elif $value > 3 then "🟢 MEDIUM"
156
+ else "🔵 LOW" end
157
+ when "importance" then
158
+ if $value > 0.8 then "🔴 CRITICAL"
159
+ elif $value > 0.6 then "🟠 HIGH"
160
+ elif $value > 0.3 then "🟢 MEDIUM"
161
+ else "🔵 LOW" end
162
+ else "🔵 LOW"
163
+ end;
164
+ end;
165
+ ```
166
+
167
+ ## 用户指令示例
168
+
169
+ ### 复杂度相关
170
+ ```bash
171
+ # 查找最复杂的函数
172
+ /top 20 functions by cyclomatic_complexity
173
+
174
+ # 查找代码体积大的函数
175
+ /top 10 functions by halstead_volume
176
+
177
+ # 查找行数过多的函数
178
+ /functions with lines_of_code > 200
179
+ ```
180
+
181
+ ### 耦合相关
182
+ ```bash
183
+ # 查找传出耦合最高的模块
184
+ /modules ranked by coupling (Ce)
185
+
186
+ # 查找不稳定的模块
187
+ /modules with instability > 0.8
188
+
189
+ # 查找传入耦合高的模块
190
+ /modules with coupling (Ca) > 0.7
191
+ ```
192
+
193
+ ### 重要性相关
194
+ ```bash
195
+ # 查找最重要的函数/top 15 functions by importance
196
+ # 查找枢纽节点
197
+ /top 10 nodes by betweenness_centrality
198
+ # 查找被依赖最多的函数
199
+ /functions with in_degree > 10
200
+ ```
201
+
202
+ ### 质量相关
203
+ ```bash
204
+ # 查找技术债占比高的
205
+ /nodes with technical_debt_ratio > 0.3
206
+
207
+ # 查找可维护性差的
208
+ /functions with maintainability_index < 60
209
+
210
+ # 查找重复代码多的
211
+ /modules with code_duplication_ratio > 0.15
212
+ ```
213
+
214
+ ## 输出示例
215
+
216
+ ```
217
+ ## Query Result: Aggregate Metrics
218
+
219
+ Query: Top 10 Functions by Cyclomatic Complexity
220
+
221
+ ### Results (Sorted by cyclomatic_complexity DESC)
222
+
223
+ | Rank | Function | CC | Lines | Risk Level |
224
+ |------|----------|----|----|------------|
225
+ | 1 | OrderService.processOrder | 23 | 342 | 🔴 CRITICAL |
226
+ | 2 | PaymentService.validatePayment | 18 | 267 | 🔴 HIGH |
227
+ | 3 | AuthService.authenticate | 14 | 198 | 🟠 MEDIUM |
228
+ | 4 | UserService.createUser | 9 | 156 | 🟢 LOW |
229
+ | 5 | ShippingService.estimateDelivery | 8 | 124 | 🟢 LOW |
230
+
231
+ ### ASCII Chart
232
+
233
+ cyclomatic_complexity distribution:
234
+ OrderService.processOrder |██████████████████████ 23
235
+ PaymentService.validatePayment |██████████████████ 18
236
+ AuthService.authenticate |██████████████ 14
237
+ UserService.createUser |█████████ 9
238
+ ShippingService.estimateDelivery |████████ 8
239
+
240
+ ### Statistics
241
+ - Mean CC: 8.2
242
+ - Median CC: 7
243
+ - Max CC: 23 (OrderService.processOrder)
244
+ - Functions with CC > 10: 3 (HIGH RISK)
245
+
246
+ ### Recommendations
247
+ - 🔴 CRITICAL: Refactor OrderService.processOrder (consider splitting into 3-4 functions)
248
+ - 🔴 HIGH: Add comprehensive tests for PaymentService.validatePayment
249
+ - 🟠 MEDIUM: Monitor AuthService.authenticate for future complexity growth
250
+ ```
251
+
252
+ ## 聚合查询最佳实践
253
+
254
+ ### 1. 分层分析
255
+ ```bash
256
+ # 系统级:顶层模块
257
+ /top 10 modules by importance (G1)
258
+
259
+ # 模块级:内部结构
260
+ /top 20 functions by cyclomatic_complexity (G3)
261
+
262
+ # 实现级:具体代码
263
+ /functions with technical_debt_ratio > 0.2 (G5)
264
+ ```
265
+
266
+ ### 2. 组合查询
267
+ ```bash
268
+ # 高复杂度 + 高耦合
269
+ /functions with cyclomatic_complexity > 10 AND coupling (Ce) > 0.7
270
+
271
+ # 高重要性 + 低质量
272
+ /functions with importance > 0.8 AND maintainability_index < 70
273
+
274
+ # 全局质量概览
275
+ /show quality metrics summary
276
+ ```
277
+
278
+ ### 3. 趋势分析
279
+ ```bash
280
+ # 对比两个版本
281
+ /compare v1 vs v2 (metric=cyclomatic_complexity)
282
+
283
+ # 查看历史趋势
284
+ /show trend cyclomatic_complexity (last=5)
285
+ ```
286
+
287
+ ## 风险等级颜色编码
288
+
289
+ | 颜色 | 风险等级 | 阈值 | 行动建议 |
290
+ |------|---------|------|---------|
291
+ | 🔴 | CRITICAL | 最严重 | 立即重构,优先级最高 |
292
+ | 🟠 | HIGH | 严重 | 近期处理,安排重构 |
293
+ | 🟢 | MEDIUM | 中等 | 计划内优化,持续监控 |
294
+ | 🔵 | LOW | 轻微 | 正常维护,无需特别关注 |
295
+
296
+ ## 故障排除
297
+
298
+ 1. **度量不存在**:某些度量需要 L2+ 分析深度,重新运行更深分析
299
+ 2. **结果为空**:检查过滤条件是否过于严格,调整参数
300
+ 3. **性能问题**:大量数据时使用 limit 限制数量
301
+ 4. **排序错误**:确认 metric 名称和排序方向是否正确