@dtt_siye/atool 1.3.0 → 1.4.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 (47) hide show
  1. package/VERSION +1 -1
  2. package/hooks/doc-sync-reminder +4 -4
  3. package/hooks/hooks-cursor.json +20 -0
  4. package/hooks/hooks.json +21 -1
  5. package/hooks/pre-commit +191 -0
  6. package/hooks/prompt-guard +84 -35
  7. package/hooks/session-start +34 -12
  8. package/hooks/task-state-tracker +145 -0
  9. package/lib/common.sh +36 -23
  10. package/lib/compute-importance.sh +73 -0
  11. package/lib/install-cursor.sh +2 -2
  12. package/lib/install-hooks.sh +64 -0
  13. package/lib/install-skills.sh +19 -0
  14. package/lib/knowledge-graph.sh +483 -81
  15. package/lib/pre-scan.sh +81 -6
  16. package/package.json +1 -1
  17. package/skills/agent-audit/SKILL.md +180 -0
  18. package/skills/architecture-guard/SKILL.md +164 -0
  19. package/skills/architecture-guard/rules/violation-detection.md +90 -0
  20. package/skills/ci-feedback/SKILL.md +165 -0
  21. package/skills/project-analyze/SKILL.md +131 -23
  22. package/skills/project-analyze/phases/phase1-setup.md +15 -1
  23. package/skills/project-analyze/phases/phase2-understand.md +17 -2
  24. package/skills/project-analyze/phases/phase2.5-refine.md +293 -0
  25. package/skills/project-analyze/phases/phase3-graph.md +7 -1
  26. package/skills/project-analyze/phases/phase4-synthesize.md +117 -120
  27. package/skills/project-analyze/phases/phase5-export.md +117 -33
  28. package/skills/project-analyze/prompts/understand-agent.md +17 -0
  29. package/skills/project-analyze/rules/android.md +61 -260
  30. package/skills/project-analyze/rules/devops.md +61 -421
  31. package/skills/project-analyze/rules/generic.md +53 -221
  32. package/skills/project-analyze/rules/go.md +60 -275
  33. package/skills/project-analyze/rules/harmony.md +64 -237
  34. package/skills/project-analyze/rules/java.md +47 -485
  35. package/skills/project-analyze/rules/mobile-flutter.md +57 -292
  36. package/skills/project-analyze/rules/mobile-react-native.md +65 -262
  37. package/skills/project-analyze/rules/mobile-swift.md +58 -303
  38. package/skills/project-analyze/rules/python.md +50 -296
  39. package/skills/project-analyze/rules/rust-tauri.md +51 -217
  40. package/skills/project-analyze/rules/rust.md +50 -274
  41. package/skills/project-analyze/rules/web-nextjs.md +61 -335
  42. package/skills/project-analyze/rules/web-react.md +50 -272
  43. package/skills/project-analyze/rules/web-vue.md +58 -352
  44. package/skills/project-analyze/rules/web.md +55 -347
  45. package/skills/project-query/SKILL.md +681 -120
  46. package/skills/requirements-writer/SKILL.md +48 -1
  47. package/skills/software-architecture/SKILL.md +73 -3
@@ -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.2.0
5
5
  category: quality
6
6
  ---
7
7
 
@@ -9,216 +9,777 @@ 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)、丰富的可视化输出(Mermaid 图、ASCII 树、结构化表格)。
13
+
14
+ **核心价值**:快速回答关于代码结构的复杂问题,无需手工扫描源码。
13
15
 
14
16
  ## 前置条件
15
17
 
16
- - 项目已通过 `/project-analyze` 完成分析
17
- - 存在 `.atool-docs/knowledge-graph.json`(v4.0+ 或 v5.0)
18
- - 如果需要数据流和调用链查询,需要 L2+ 深度的分析结果
18
+ - 项目已通过 `/project-analyze` 完成分析(L1-L5 任意深度)
19
+ - 存在 `.atool-docs/knowledge-graph.json`(v5.0+ 格式)
20
+ - L2+ 深度的分析(推荐)— 更多节点和边信息,查询结果更丰富
21
+ - ⚠️ L1 深度可用但结果只包含模块级节点(无函数、API 等细粒度)
22
+
23
+ ## 工作原理
24
+
25
+ ```
26
+ user query (自然语言或 /command)
27
+
28
+ parse query intent (识别查询类型 + 参数)
29
+
30
+ load knowledge-graph.json
31
+
32
+ apply jq template (执行对应的图查询模板)
33
+
34
+ generate output (结构化摘要 + Mermaid 图 + ASCII 视图 + 表格)
35
+
36
+ display result (支持 Claude Code / Cursor / 终端)
37
+ ```
19
38
 
20
39
  ## 查询类别
21
40
 
22
41
  ### 1. 节点查找 (Node Lookup)
23
42
 
24
- 查找特定类型的节点。
43
+ 快速查找特定的函数、类、模块、API 端点等。
25
44
 
45
+ **用户指令**:
26
46
  ```
27
- /find all functions named createUser
28
- /find all classes in auth module
47
+ /find functions named createUser
48
+ /find classes in auth module
29
49
  /find all API endpoints
30
- /find modules with highest coupling
50
+ /find modules with importance > 0.7
51
+ /find data_entity named User
31
52
  ```
32
53
 
33
54
  **执行步骤**:
34
- 1. 读取 `knowledge-graph.json` `nodes` 数组
35
- 2. `type` `label` 过滤(支持子串匹配)
36
- 3. 输出匹配节点的列表(ID、名称、路径、重要度)
37
- 4. 如果结果 > 20 个,按 importance 降序排列,仅展示 top 20
55
+ 1. 解析查询中的 **type** **label** 约束
56
+ 2. 读取 `knowledge-graph.json` `nodes` 数组
57
+ 3. 按 `type`(精确或模糊)和 `label`(子串匹配,不分大小写)过滤
58
+ 4. `importance` 降序排序,限制 top 20(避免过多结果)
59
+ 5. 生成输出表格
60
+
61
+ **输出示例**:
62
+ ```
63
+ ## Query Result: Node Lookup
64
+
65
+ Found 5 nodes matching: type=function, label contains "create"
66
+
67
+ | ID | Label | Path | Type | Importance | Metrics |
68
+ |----|-------|------|------|------------|---------|
69
+ | func:UserService.createUser | createUser | src/services/UserService.ts | function | 0.85 | CC: 3, Lines: 42 |
70
+ | func:OrderService.createOrder | createOrder | src/services/OrderService.ts | function | 0.72 | CC: 5, Lines: 68 |
71
+ | ... | | | | | |
72
+ ```
73
+
74
+ **jq 实现**:参考 `rules/query-templates.md` Template 1
38
75
 
39
76
  ### 2. 路径查询 (Path Query)
40
77
 
41
- 查找两个节点之间的调用路径。
78
+ 追踪两个节点之间的调用链或依赖链。
42
79
 
80
+ **用户指令**:
43
81
  ```
44
- /show call path from UserController.createUser to UserRepository.save
45
- /show dependency path from module-a to module-b
82
+ /show call path from UserController.createUser to Database.save
83
+ /show dependency path from module-auth to module-api
84
+ /show import chain from A to B
46
85
  ```
47
86
 
48
87
  **执行步骤**:
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 条)
88
+ 1. 解析 source 和 target 节点标识符
89
+ 2. knowledge-graph 中定位两个节点 ID
90
+ 3. 根据查询类型选择遍历的边类型:
91
+ - `call path` → 沿 `calls` + `calls_async`
92
+ - `dependency path` 沿 `depends_on`
93
+ - 其他 → 沿所有相关边类型
94
+ 4. 执行有向 BFS(广度优先搜索)找到最短路径
95
+ 5. 如有多条路径,返回所有(最多 5 条,按路径长度排序)
96
+ 6. 生成 Mermaid `graph LR` 有向图
97
+
98
+ **输出示例**:
99
+ ```
100
+ ## Query Result: Call Path
101
+
102
+ Source: UserController.createUser
103
+ Target: Database.save
104
+
105
+ ### Path 1 (3 hops) ✅ SHORTEST
106
+
107
+ UserController.createUser
108
+ --calls--> UserService.validateData
109
+ --calls--> Validator.checkEmail
110
+ --calls--> Database.save
111
+
112
+ ### Mermaid Diagram
113
+
114
+ graph LR
115
+ A["UserController.createUser"] -->|calls| B["UserService.validateData"]
116
+ B -->|calls| C["Validator.checkEmail"]
117
+ C -->|calls| D["Database.save"]
118
+ style A fill:#90EE90
119
+ style D fill:#FFB6C1
120
+ ```
121
+
122
+ **jq 实现**:参考 `rules/query-templates.md` Template 2
57
123
 
58
124
  ### 3. 影响分析 (Impact Analysis)
59
125
 
60
- 分析修改某个节点后的影响范围。
126
+ 分析修改某个节点可能影响的其他部分,以及风险等级。
61
127
 
128
+ **用户指令**:
62
129
  ```
63
- /what changes if I modify User entity
130
+ /what if I modify User entity
64
131
  /impact of changing AuthService
132
+ /change analysis for database schema
65
133
  ```
66
134
 
67
135
  **执行步骤**:
68
136
  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. 输出风险摘要 + 建议的测试范围
137
+ 2. 反向查询所有指向 X 的边(反向依赖):
138
+ - `depends_on`、`calls`、`reads_from`、`reads_state`、`implements`、`extends`
139
+ 3. 对找到的每个直接依赖节点 D,继续反向追踪(递归,最多 3 层)
140
+ 4. 计算风险评分公式:
141
+ ```
142
+ risk_score = Σ (importance(D) × edge_weight(D→X)) 对所有直接依赖者
143
+ ```
144
+ 5. 风险等级判断:
145
+ - `risk_score > 0.7` **🔴 HIGH RISK**(红色)
146
+ - `0.3 < risk_score ≤ 0.7` → **🟠 MEDIUM RISK**(橙色)
147
+ - `risk_score ≤ 0.3` → **🟢 LOW RISK**(绿色)
148
+ 6. 统计影响范围:受影响的模块数、函数数、API 端点数、数据实体数
149
+ 7. 按粒度级别生成 Mermaid 图
150
+ 8. 输出建议的测试范围(涉及的单元测试、集成测试、E2E 测试)
151
+
152
+ **输出示例**:
153
+ ```
154
+ ## Query Result: Impact Analysis
155
+
156
+ Target: data_entity:User
157
+
158
+ **Risk Assessment**: 🔴 HIGH (0.84)
159
+
160
+ ### Direct Dependents (1-hop)
161
+ - func:AuthService.validateUser (importance: 0.9, risk: HIGH)
162
+ - func:OrderService.processOrder (importance: 0.8, risk: HIGH)
163
+
164
+ ### Transitive Dependents (2-hop)
165
+ - module:api (affected functions: 4)
166
+ - module:payment (affected functions: 2)
167
+
168
+ ### Impact Summary
169
+ | Category | Count | Risk Level |
170
+ |----------|-------|-----------|
171
+ | Modules | 5 | HIGH |
172
+ | Functions | 23 | HIGH |
173
+ | API Endpoints | 3 | MEDIUM |
174
+ | Data Entities | 2 | LOW |
175
+
176
+ ### Recommended Testing
177
+ - [ ] Unit tests for AuthService, OrderService
178
+ - [ ] Integration tests for api module
179
+ - [ ] E2E tests for user creation, order flow
180
+ ```
181
+
182
+ **jq 实现**:参考 `rules/query-templates.md` Template 3
79
183
 
80
184
  ### 4. 数据血缘 (Data Lineage)
81
185
 
82
- 追踪数据的来源和去向。
186
+ 追踪某个数据实体从源头到终点的完整流向。
83
187
 
188
+ **用户指令**:
84
189
  ```
85
190
  /trace data lineage of Order entity
86
- /where does userData come from and where does it flow
191
+ /where does User data flow
192
+ /data flow for Payment status
87
193
  ```
88
194
 
89
195
  **执行步骤**:
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. 输出数据流描述文本
196
+ 1. 定位数据实体节点 D(type = `data_entity`)
197
+ 2. **反向追踪**(来源 Upstream):
198
+ - 沿 `writes_to`、`writes_state` 边反向查找所有写入 D 的函数
199
+ - 继续追踪这些函数的输入来源
200
+ - 直到找到边界节点(API、user_input、scheduled_task)
201
+ 3. **正向追踪**(去向 Downstream):
202
+ - 沿 `reads_from`、`reads_state`、`transforms` 边正向查找所有读取 D 的函数
203
+ - 继续追踪这些函数的输出流向
204
+ - 直到找到最终汇聚点(database、external_api、log)
205
+ 4. 计算数据流速率(flow_rate):
206
+ ```
207
+ flow_rate = count(writers) × count(readers) × avg_transformation_steps
208
+ ```
209
+ 5. 生成 Mermaid `graph TD` 数据流图,包括:
210
+ - 数据节点(蓝色高亮)
211
+ - 处理函数(绿色)
212
+ - 外部系统(灰色)
213
+ - 每条边标注操作类型(read/write/transform/persist)
214
+
215
+ **输出示例**:
216
+ ```
217
+ ## Query Result: Data Lineage
218
+
219
+ Target: data_entity:Order
220
+
221
+ ### Upstream (Data Sources)
222
+ api:POST/orders
223
+ --creates--> func:OrderService.createOrder
224
+ --writes_to--> data_entity:Order
225
+
226
+ ### Processing (Transformations)
227
+ func:OrderService.createOrder
228
+ --transforms--> func:PaymentService.calculateTotal
229
+ --transforms--> func:ShippingService.estimateDelivery
230
+
231
+ ### Downstream (Data Sinks)
232
+ data_entity:Order
233
+ --reads_from--> func:OrderService.getOrders
234
+ --returns--> api:GET/orders/{id}
235
+
236
+ --persists_to--> database:PostgreSQL
237
+
238
+ ### Data Flow Metrics
239
+ - Writers: 2 functions
240
+ - Readers: 5 functions
241
+ - Transformations: 3 steps
242
+ - Flow Rate: 30 (very high activity)
243
+ ```
244
+
245
+ **jq 实现**:参考 `rules/query-templates.md` Template 4
103
246
 
104
247
  ### 5. 邻域查询 (Neighborhood)
105
248
 
106
- 查看某个节点周围 N 跳的所有关联。
249
+ 查看某个节点周围 N 跳的所有直接和间接关联。
107
250
 
251
+ **用户指令**:
108
252
  ```
109
- /show everything connected to AuthService within 2 hops
110
- /neighbors of UserController.createUser
253
+ /neighbors of AuthService within 2 hops
254
+ /what's connected to UserController
255
+ /neighborhood of Order module (depth=3)
111
256
  ```
112
257
 
113
258
  **执行步骤**:
114
259
  1. 定位焦点节点 F
115
- 2. 无向 BFS(不区分边的方向)展开 N 跳(默认 2,最大 5)
116
- 3. 按当前粒度级别过滤节点/边类型
117
- 4. 输出:
118
- - 邻域图(Mermaid)
119
- - 每个邻居节点与焦点的关系说明
120
- - 按关系类型分组统计
260
+ 2. 构建双向邻接表(边无向化,忽略方向)
261
+ 3. 迭代 BFS 展开 N 跳(默认 2,最大 5):
262
+ - Hop 0:焦点节点 F 本身
263
+ - Hop 1:F 的直接邻居
264
+ - Hop 2:邻居的邻居(排除已访问)
265
+ - Hop 3:再往外一层
266
+ - ...
267
+ 4. 按粒度级别过滤节点/边类型(G1-G5)
268
+ 5. 按关系类型分组统计邻域结构
269
+
270
+ **输出示例**:
271
+ ```
272
+ ## Query Result: Neighborhood Query
273
+
274
+ Focus: module:auth
275
+ Depth: 2 hops
276
+
277
+ ### Hop 1 - Direct Neighbors (4 nodes)
278
+ - module:core (depends_on, depends_on)
279
+ - module:user (implements, depends_on)
280
+ - service:jwt (uses)
281
+
282
+ ### Hop 2 - Secondary Neighbors (6 nodes)
283
+ - module:api (depends_on via core)
284
+ - module:database (depends_on via core)
285
+ - function:validateToken (calls via jwt)
286
+
287
+ ### Relationship Summary
288
+ | Relationship Type | Count | Nodes Affected |
289
+ |------------------|-------|----------------|
290
+ | depends_on | 3 | 4 |
291
+ | calls | 2 | 3 |
292
+ | implements | 1 | 2 |
293
+ | uses | 1 | 1 |
294
+
295
+ ### Neighborhood Graph (Mermaid)
296
+ graph TD
297
+ auth[("🎯 module:auth")]
298
+ core["module:core"]
299
+ user["module:user"]
300
+ jwt["service:jwt"]
301
+
302
+ auth -->|depends_on| core
303
+ auth -->|implements| user
304
+ auth -->|uses| jwt
305
+
306
+ core -->|depends_on| api["module:api"]
307
+ core -->|depends_on| db["module:database"]
308
+ ```
309
+
310
+ **jq 实现**:参考 `rules/query-templates.md` Template 5
121
311
 
122
312
  ### 6. 聚合查询 (Aggregate)
123
313
 
124
- 按度量值排序和筛选节点。
314
+ 按各种度量指标排序和统计节点。用于快速识别高风险、高复杂度、高耦合等关键问题点。
125
315
 
316
+ **用户指令**:
126
317
  ```
127
- /which module has highest coupling
128
318
  /top 10 most complex functions
129
319
  /modules ranked by importance
130
- /show all nodes with Critical risk level
320
+ /which module has highest coupling
321
+ /functions with cyclomatic_complexity > 10
322
+ /show all nodes with criticality CRITICAL
131
323
  ```
132
324
 
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
325
+ **支持的度量指标**:
326
+
327
+ | 度量类别 | 可用指标 | 解释 |
328
+ |---------|---------|------|
329
+ | **复杂度** | cyclomatic_complexity | 圈复杂度(>10 为高风险) |
330
+ | | halstead_volume | 代码体积(>8 为大) |
331
+ | | lines_of_code | 函数行数 |
332
+ | **耦合** | coupling (Ca) | 传入耦合(有多少依赖我) |
333
+ | | coupling (Ce) | 传出耦合(我依赖多少) |
334
+ | | instability (I) | 不稳定性 (Ce/(Ca+Ce)) |
335
+ | **重要性** | importance | 综合重要性评分 (0-1) |
336
+ | **质量** | maintainability_index | 可维护性指数 |
337
+ | | technical_debt_ratio | 技术债占比 |
338
+ | **中心性** | betweenness_centrality | 介数中心性(关键路由节点) |
339
+ | | in_degree | 入度(被依赖次数) |
340
+ | | out_degree | 出度(依赖他人次数) |
341
+ | **数据流** | flow_rate | 数据流量 |
342
+ | | schema_coupling | 数据耦合度 |
140
343
 
141
344
  **执行步骤**:
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
- - 返回过滤后的子图
345
+ 1. 解析查询条件:目标度量、排序方向(升/降)、数量限制(top N)、过滤条件
346
+ 2. 从 knowledge-graph.json 加载所有节点及其 metrics
347
+ 3. 应用过滤条件(如 complexity > 10)
348
+ 4. 按指定度量排序
349
+ 5. top N(默认 10,最大 50)
350
+ 6. 生成柱状图(ASCII 或 Mermaid)和排序表格
351
+
352
+ **输出示例**:
353
+ ```
354
+ ## Query Result: Aggregate Metrics
355
+
356
+ Query: Top 10 Functions by Cyclomatic Complexity
357
+
358
+ ### Results (Sorted by cyclomatic_complexity DESC)
359
+
360
+ | Rank | Function | CC | Lines | Risk Level |
361
+ |------|----------|----|----|------------|
362
+ | 1 | OrderService.processOrder | 23 | 342 | 🔴 CRITICAL |
363
+ | 2 | PaymentService.validatePayment | 18 | 267 | 🔴 HIGH |
364
+ | 3 | AuthService.authenticate | 14 | 198 | 🟠 MEDIUM |
365
+ | 4 | UserService.createUser | 9 | 156 | 🟢 LOW |
366
+ | ... | | | | |
367
+
368
+ ### ASCII Chart
369
+
370
+ cyclomatic_complexity distribution:
371
+ OrderService.processOrder |██████████████████████ 23
372
+ PaymentService.validatePayment |██████████████████ 18
373
+ AuthService.authenticate |██████████████ 14
374
+ UserService.createUser |█████████ 9
375
+ ShippingService.estimateDelivery |████████ 8
376
+ ...
377
+
378
+ ### Statistics
379
+ - Mean CC: 8.2
380
+ - Median CC: 7
381
+ - Max CC: 23 (OrderService.processOrder)
382
+ - Functions with CC > 10: 3 (HIGH RISK)
383
+
384
+ ### Recommendations
385
+ - Refactor OrderService.processOrder (consider splitting into 3-4 functions)
386
+ - Add comprehensive tests for PaymentService.validatePayment
387
+ - Monitor AuthService.authenticate for future complexity growth
388
+ ```
389
+
390
+ **jq 实现**:参考 `rules/query-templates.md` Template 6
391
+
392
+ ## 粒度缩放 (Zoom Level G1-G5)
393
+
394
+ 查询时通过 `zoom` 或 `G` 参数控制结果粒度,从高层架构到低层细节:
395
+
396
+ | 缩放级别 | 可见节点类型 | 可见边类型 | 用途 | 典型查询 |
397
+ |---------|------------|-----------|------|---------|
398
+ | **G1** (System) | `module`、`layer` | `depends_on` | 高层架构视图 | /top modules by importance (G1) |
399
+ | **G2** (Module) | G1 + `class`、`service`、`store`、`interface` | G1 + `implements`、`extends`、`belongs_to` | 模块内部结构 | /neighborhood of auth module (G2) |
400
+ | **G3** (Function) | G2 + `function`、`api_endpoint`、`route`、`hook` | G2 + `calls`、`calls_async`、`returns`、`sends_http` | 调用链追踪 | /show call path from A to B (G3) |
401
+ | **G4** (Data) | G3 + `data_entity` | G3 + `reads_from`、`writes_to`、`transforms`、`validates`、`reads_state`、`writes_state` | 数据流分析 | /trace data lineage of User (G4) |
402
+ | **G5** (Cross-cutting) | 所有 14 种节点类型 | 所有 24 种边类型 | 全景分析 | /impact analysis of core module (G5) |
403
+
404
+ **缩放机制**:
405
+ 1. 用户指定粒度(默认 G2)
406
+ 2. 根据粒度级别过滤 nodes 类型集合
407
+ 3. 根据粒度级别过滤 edges 类型集合
408
+ 4. 应用 BFS 或 DFS 算法
409
+ 5. 返回过滤后的子图
410
+
411
+ **使用示例**:
412
+ ```bash
413
+ # 系统级概览(只看模块和层)
414
+ /top modules by importance (zoom=G1)
415
+
416
+ # 模块级细节(看类和接口)
417
+ /neighborhood of auth (zoom=G2)
418
+
419
+ # 函数级调用链
420
+ /show call path from UserController.create to UserService.save (zoom=G3)
421
+
422
+ # 数据流(包括数据实体)
423
+ /trace data lineage of Order (zoom=G4)
424
+
425
+ # 完整全景
426
+ /impact analysis of User entity (zoom=G5)
427
+ ```
165
428
 
166
429
  ## 输出格式
167
430
 
168
- 每个查询结果包含以下部分:
431
+ 每个查询结果包含以下部分(根据查询类型有所不同):
169
432
 
170
433
  ### 1. 结构化摘要
171
- ```
434
+ ```markdown
172
435
  ## Query Result: Impact Analysis on User entity
173
436
 
174
- **Risk Level**: HIGH (0.82)
437
+ **Risk Level**: 🔴 HIGH (0.82)
175
438
  **Affected Modules**: 5
176
439
  **Affected Functions**: 23
177
440
  **Affected APIs**: 3
441
+ **Affected Data Entities**: 2
442
+
443
+ ### Key Insights
444
+ - Most critical: AuthService, OrderService
445
+ - Highest risk path: User → AuthService → PaymentService
178
446
  ```
179
447
 
180
448
  ### 2. Mermaid 可视化
181
- 根据查询类型生成对应的 Mermaid 图:
182
- - 路径查询 → `graph LR`(从左到右的有向图)
183
- - 影响分析 → `graph TD`(从上到下的辐射图)
184
- - 数据血缘 → `graph TD`(数据流图)
185
- - 邻域查询 → `graph TD`(星形图)
186
- - 聚合查询柱状图或表格
449
+ 根据查询类型生成对应的图表:
450
+ - **路径查询** → `graph LR`(从左到右的有向链)
451
+ - **影响分析** → `graph TD`(从上到下的辐射树,颜色表示风险等级)
452
+ - **数据血缘** → `graph TD`(数据流向图,高亮数据节点)
453
+ - **邻域查询** → `graph TD`(星形或树形拓扑)
454
+ - **聚合查询**ASCII 柱状图 + 表格
187
455
 
188
456
  ### 3. ASCII 视图(终端兼容)
189
457
  ```
190
- UserController.create() --calls--> UserService.createUser()
191
- └── UserService.createUser() --calls--> UserRepository.save()
192
- └── UserRepository.save() --persists_to--> [DB: users]
458
+ UserController.create()
459
+ --[calls]--> UserService.createUser()
460
+ --[calls]--> UserRepository.save()
461
+ --[persists_to]--> [DB: users]
462
+
463
+ Tree format:
464
+ ┌─ UserController.create() [importance: 0.8, CC: 5]
465
+ │ └─ UserService.createUser() [importance: 0.7, CC: 8]
466
+ │ └─ UserRepository.save() [importance: 0.6, CC: 3]
193
467
  ```
194
468
 
195
- ### 4. 明细表格
196
- | 节点 | 类型 | 度量 | 关系 |
197
- |------|------|------|------|
469
+ ### 4. 详细表格
470
+ ```markdown
471
+ | 节点 ID | 节点名 | 类型 | 重要性 | 度量值 | 关系 |
472
+ |--------|--------|------|--------|--------|------|
473
+ | func:A.create | A.create | function | 0.8 | CC: 5 | calls |
474
+ | func:B.validate | B.validate | function | 0.7 | CC: 8 | calls_async |
475
+ ```
476
+
477
+ ### 5. 建议和洞察
478
+ ```markdown
479
+ ### Recommendations
480
+ - 🔴 CRITICAL: Refactor OrderService (CC=23)
481
+ - 🟠 HIGH: Add integration tests for auth module
482
+ - 🟢 LOW: Monitor UserService for future changes
483
+ ```
198
484
 
199
- ## 多维分析查询
485
+ ## 多维分析查询(需要 Multi-Dimensional Analysis)
200
486
 
201
- 如果 `.atool-docs/multi-dimensional-analysis.json` 存在,还可以查询:
487
+ 如果 `.atool-docs/multi-dimensional-analysis.json` 存在(project-analyze Phase 3 输出),还可以进行高级多维分析查询:
202
488
 
203
- ```
489
+ ### 5 维分析框架
490
+
491
+ | 维度 | 关注点 | 查询示例 |
492
+ |------|--------|---------|
493
+ | **结构维** | 模块划分、依赖拓扑、分层遵循 | `/show structural analysis results` |
494
+ | **行为维** | 调用关系、数据流、控制流 | `/show call graph hotspots` |
495
+ | **数据维** | 数据流向、数据耦合、数据质量 | `/show data flow hotspots` |
496
+ | **质量维** | 复杂度、重复代码、技术债 | `/show quality metrics summary` |
497
+ | **语义维** | 业务术语、领域对象、业务规则 | `/show domain vocabulary` |
498
+
499
+ ### 查询命令
500
+
501
+ ```bash
502
+ # 结构分析
204
503
  /show structural analysis results
205
- /show call graph hotspots
504
+ /find layering violations
505
+ /detect circular dependencies (depth=3)
506
+
507
+ # 行为分析
508
+ /show call graph hotspots (top=10)
509
+ /trace control flow for OrderService
510
+ /find execution bottlenecks
511
+
512
+ # 数据分析
206
513
  /show data flow hotspots
514
+ /trace data dependencies across modules
515
+ /find schema coupling issues
516
+
517
+ # 质量分析
207
518
  /show quality metrics summary
519
+ /find code smell clusters
520
+ /show technical debt hotspots (sorted=desc, limit=20)
521
+
522
+ # 语义分析
208
523
  /show domain vocabulary
524
+ /extract business concepts
525
+ /map entity relationships
526
+ ```
527
+
528
+ ### 输出示例
529
+
530
+ ```markdown
531
+ ## Multi-Dimensional Analysis: Quality Metrics
532
+
533
+ ### 质量维 Quality Dimension
534
+ - Average Cyclomatic Complexity: 6.2
535
+ - Hotspots (CC > 10): 5 functions
536
+ - Duplicate Code Blocks: 23 instances (~12% code duplication)
537
+ - Technical Debt Ratio: 15.3% (MEDIUM)
538
+
539
+ ### 结构维 Structure Dimension
540
+ - Modules: 8 (avg 3.2 imports per module)
541
+ - Layering Violations: 0 ✅
542
+ - Circular Dependencies: 0 ✅
543
+ - Stability Index: 0.62 (MODERATE)
544
+
545
+ ### 行为维 Behavior Dimension
546
+ - Call Graph Diameter: 7 hops
547
+ - Hottest Call Paths: OrderService → PaymentService → Database
548
+ - Async Calls: 12% of total
549
+
550
+ ### 数据维 Data Dimension
551
+ - Data Entities: 14
552
+ - Schema Coupling: 0.41 (MODERATE)
553
+ - Data Flow Hotspots: User → Order → Payment (3-hop flow)
554
+
555
+ ### 语义维 Semantic Dimension
556
+ - Domain Concepts: auth, order, payment, shipping
557
+ - Entity Mapping: User → Order (1:many), Order → Payment (1:1)
558
+ - Business Rules: 23 detected
209
559
  ```
210
560
 
211
561
  ## Skill 协作
212
562
 
213
563
  | 协作 Skill | 触发条件 | 交互方式 |
214
564
  |-----------|---------|---------|
215
- | project-analyze | 需要生成 knowledge-graph.json | 前置依赖 |
216
- | code-review | 需要更详细的代码质量分析 | 建议运行 |
217
- | software-architecture | 影响分析显示架构问题 | 建议运行 |
565
+ | **project-analyze** | 需要生成 knowledge-graph.json | 前置依赖 — 必须先运行 `/project-analyze` 生成数据 |
566
+ | **code-review** | 影响分析显示代码质量问题 | 建议运行 — 用于深度代码质量评分 |
567
+ | **software-architecture** | 影响分析显示架构违反 | 建议运行 — 用于架构重构或规范化 |
568
+
569
+ ## 最佳实践和使用场景
570
+
571
+ ### 场景 1: 新人快速上手(30 分钟)
572
+ ```bash
573
+ # Step 1: 了解项目全貌
574
+ /top modules by importance (zoom=G1)
575
+
576
+ # Step 2: 深入核心模块
577
+ /neighborhood of core module (depth=2, zoom=G2)
578
+
579
+ # Step 3: 追踪关键数据流
580
+ /trace data lineage of User (zoom=G4)
581
+ ```
582
+
583
+ ### 场景 2: 评估修改风险
584
+ ```bash
585
+ # 修改某个函数前
586
+ /impact of changing UserService.updateProfile
587
+
588
+ # 修改某个数据模型前
589
+ /what if I modify Order entity
590
+
591
+ # 推荐的测试范围会自动生成
592
+ ```
593
+
594
+ ### 场景 3: 识别技术债
595
+ ```bash
596
+ # 查找最复杂的函数
597
+ /top 20 functions by cyclomatic_complexity
598
+
599
+ # 查找最高耦合的模块
600
+ /modules ranked by coupling (sorted=desc, limit=10)
601
+
602
+ # 查找技术债占比最高的
603
+ /nodes with technical_debt_ratio > 0.3
604
+ ```
605
+
606
+ ### 场景 4: 架构审查
607
+ ```bash
608
+ # 整个系统的依赖拓扑
609
+ /show all modules and dependencies (zoom=G1)
610
+
611
+ # 检测循环依赖
612
+ /find circular dependencies
613
+
614
+ # 验证分层原则
615
+ /check layering violations
616
+ ```
617
+
618
+ ## 错误处理和故障排除
619
+
620
+ | 错误 | 原因 | 解决方案 |
621
+ |------|------|--------|
622
+ | `knowledge-graph.json not found` | 项目未分析 | 运行 `/project-analyze` 完成初始分析 |
623
+ | `node not found` | 查询的节点不存在 | 使用 `/find` 查询节点确切名称,或用模糊搜索 |
624
+ | `no results` | 查询条件过严格 | 放松过滤条件,或提升分析深度(L3+) |
625
+ | `metric not available` | 该度量在此分析深度不存在 | 某些度量需要 L2+ 深度,重新运行更深的分析 |
626
+ | `timeout` | 查询太复杂(如 G5 的大图) | 降低粒度级别(G5 → G3),或限制查询范围 |
627
+
628
+ **快速故障排除流程**:
629
+ ```
630
+ Query fails?
631
+ ├─ Check if knowledge-graph.json exists
632
+ │ └─ No → Run /project-analyze
633
+ ├─ Check if node/metric names are correct
634
+ │ └─ No → Use /find to search
635
+ ├─ Check if analysis depth is sufficient
636
+ │ └─ L1 → Rerun with L2+ for more detail
637
+ └─ Check if zoom level is appropriate
638
+ └─ Too detailed → Lower granularity
639
+ ```
640
+
641
+ ## 高级技巧
642
+
643
+ ### 1. 导出查询结果
644
+ 所有查询结果都支持导出为 JSON/CSV:
645
+ ```bash
646
+ /find functions named "create*" (export=json)
647
+ # 输出标准 JSON,可用 jq 处理
648
+ ```
649
+
650
+ ### 2. 保存查询模板
651
+ 频繁使用的查询可保存为别名:
652
+ ```bash
653
+ /alias weekly-review = "top 10 modules by importance (G1) + top 20 functions by cyclomatic_complexity"
654
+ /weekly-review # 一键执行
655
+ ```
656
+
657
+ ### 3. 比较两个分析结果
658
+ ```bash
659
+ # 对比两个不同版本的项目
660
+ /compare analysis v1 vs v2 (metric=cyclomatic_complexity)
661
+ # 输出:哪些函数变得更复杂、哪些模块耦合度增加等
662
+ ```
663
+
664
+ ### 4. 生成质量报告
665
+ ```bash
666
+ /generate report (title="Project Quality Review", include=["complexity", "coupling", "risk"])
667
+ # 输出一份综合报告,含图表和建议
668
+ ```
669
+
670
+ ---
671
+
672
+ ## 快速参考表
673
+
674
+ ### 6 类查询速查
675
+
676
+ | 查询类型 | 命令示例 | 适用场景 | 输出 |
677
+ |---------|---------|---------|------|
678
+ | **节点查找** | `/find functions named "create*"` | 查找特定代码元素 | 表格 + 重要性排序 |
679
+ | **路径查询** | `/show call path from A to B` | 追踪调用链或依赖链 | Mermaid 图 + ASCII 树 |
680
+ | **影响分析** | `/what if I modify User entity` | 评估修改风险 | 风险评分 + 影响范围 + 测试建议 |
681
+ | **数据血缘** | `/trace data lineage of Order` | 理解数据流向 | 上游/处理/下游 + Mermaid 图 |
682
+ | **邻域查询** | `/neighbors of AuthService (depth=2)` | 理解节点关联 | 星形/树形拓扑 + 关系统计 |
683
+ | **聚合查询** | `/top 10 functions by cyclomatic_complexity` | 识别风险点 | 排序表格 + ASCII 柱状图 |
684
+
685
+ ### 粒度级别速查
686
+
687
+ | 级别 | 用途 | 典型查询 |
688
+ |------|------|---------|
689
+ | **G1** | 系统级架构 | `/top modules by importance (G1)` |
690
+ | **G2** | 模块结构 | `/neighborhood of core (G2)` |
691
+ | **G3** | 调用链 | `/show call path A→B (G3)` |
692
+ | **G4** | 数据流 | `/trace data lineage (G4)` |
693
+ | **G5** | 全景分析 | `/impact analysis (G5)` |
694
+
695
+ ### 常用度量指标速查
696
+
697
+ | 度量 | 含义 | 高风险阈值 |
698
+ |------|------|-----------|
699
+ | `cyclomatic_complexity` | 圈复杂度 | > 10 |
700
+ | `halstead_volume` | 代码体积 | > 8 |
701
+ | `lines_of_code` | 代码行数 | > 200 |
702
+ | `coupling (Ce)` | 传出耦合 | > 0.7 |
703
+ | `instability (I)` | 不稳定性 | > 0.8 |
704
+ | `importance` | 综合重要性 | > 0.8 |
705
+ | `technical_debt_ratio` | 技术债占比 | > 0.2 |
706
+ | `betweenness_centrality` | 关键度(枢纽) | > 0.5 |
707
+
708
+ ---
709
+
710
+ ## 完整工作流示例
711
+
712
+ ### 场景:重构一个遗留项目
713
+
714
+ ```bash
715
+ # Step 1: 了解全局架构(5分钟)
716
+ /top modules by importance (zoom=G1)
717
+ # 识别核心模块和边界模块
718
+
719
+ # Step 2: 深入高风险模块(10分钟)
720
+ /top 10 functions by cyclomatic_complexity
721
+ # 找出最复杂的函数,优先重构
722
+
723
+ # Step 3: 分析重构影响范围(5分钟)
724
+ /impact of changing OrderService.processOrder
725
+ # 评估修改风险,确定测试范围
726
+
727
+ # Step 4: 验证架构约束(3分钟)
728
+ /find circular dependencies
729
+ /find layering violations
730
+ # 确保重构后不引入新的架构问题
731
+
732
+ # Step 5: 生成交付物(2分钟)
733
+ /generate report (title="Refactoring Plan", include=["complexity", "risk", "dependencies"])
734
+ # 生成可交付的分析报告
735
+ ```
736
+
737
+ ### 场景:新人代码审查
738
+
739
+ ```bash
740
+ # 新人需要 review UserService,但不知道从何下手
741
+
742
+ # Step 1: 理解 UserService 的职责
743
+ /show definition of UserService
744
+ /neighborhood of UserService (depth=1, G2)
745
+ # 知道它依赖谁,谁依赖它
746
+
747
+ # Step 2: 追踪关键调用链
748
+ /show call path from UserController.createUser to UserRepository.save
749
+ # 理解整个流程
750
+
751
+ # Step 3: 确认数据流
752
+ /trace data lineage of User entity
753
+ # 知道 User 数据怎么来的、流向哪里
754
+
755
+ # Step 4: 评估修改风险
756
+ /impact of changing UserService.validateEmail
757
+ # 确定这个修改会影响哪些测试用例
758
+
759
+ # 现在新人可以自信地进行 review 了!
760
+ ```
761
+
762
+ ---
763
+
764
+ ## FAQ
765
+
766
+ **Q: 我的项目有 500+ 文件,是否支持?**
767
+ A: 完全支持。使用 L1 分析快速了解全貌(<2分钟),或用粒度缩放(G1)查看模块级视图。
768
+
769
+ **Q: Query Panel 和 code-review 有什么区别?**
770
+ A: code-review 专注代码质量分析(8维度评分),project-query 专注**结构和关系分析**(路径、影响、数据流)。通常配合使用。
771
+
772
+ **Q: 怎样处理查询结果太多的情况?**
773
+ A: 使用 `limit`、`filter`、`zoom` 参数缩小范围。例如 `/top 50 functions ... (limit=10, zoom=G2)`。
774
+
775
+ **Q: 支持导出为什么格式?**
776
+ A: 支持 JSON(标准格式,可用 jq 处理)、CSV(电子表格)、HTML(报告)、PDF(文档)。
777
+
778
+ ---
218
779
 
219
- ## 错误处理
780
+ ## 相关资源
220
781
 
221
- - 如果 `knowledge-graph.json` 不存在 → 提示用户先运行 `/project-analyze`
222
- - 如果查询的节点不存在 列出最接近的匹配建议(模糊搜索)
223
- - 如果查询的度量不存在 列出可用的度量列表
224
- - 如果结果为空 → 建议更高的分析深度(L2+ 推荐用于路径查询)
782
+ - **Project Analyze Guide**:`docs/14-project-analyze-guide.md`
783
+ - **Query Templates**:`rules/query-templates.md`(jq 实现细节)
784
+ - **Knowledge Graph Schema**:`lib/knowledge-graph.sh`(节点/边定义)
785
+ - **Multi-Dimensional Analysis**:`lib/multi-dimensional-analysis.sh`(五维分析算法)