@haaaiawd/anws 1.0.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.
- package/README.md +96 -0
- package/bin/cli.js +76 -0
- package/lib/copy.js +38 -0
- package/lib/index.js +8 -0
- package/lib/init.js +139 -0
- package/lib/manifest.js +53 -0
- package/lib/output.js +74 -0
- package/lib/update.js +85 -0
- package/package.json +36 -0
- package/templates/.agent/rules/agents.md +90 -0
- package/templates/.agent/skills/build-inspector/SKILL.md +83 -0
- package/templates/.agent/skills/complexity-guard/SKILL.md +71 -0
- package/templates/.agent/skills/complexity-guard/references/anti_patterns.md +21 -0
- package/templates/.agent/skills/concept-modeler/SKILL.md +112 -0
- package/templates/.agent/skills/concept-modeler/prompts/GLOSSARY_PROMPT.md +40 -0
- package/templates/.agent/skills/concept-modeler/references/ENTITY_EXTRACTION_PROMPT.md +299 -0
- package/templates/.agent/skills/concept-modeler/scripts/glossary_gen.py +66 -0
- package/templates/.agent/skills/git-forensics/SKILL.md +74 -0
- package/templates/.agent/skills/git-forensics/references/ANALYSIS_METHODOLOGY.md +193 -0
- package/templates/.agent/skills/git-forensics/scripts/git_forensics.py +615 -0
- package/templates/.agent/skills/git-forensics/scripts/git_hotspots.py +118 -0
- package/templates/.agent/skills/report-template/SKILL.md +88 -0
- package/templates/.agent/skills/report-template/references/REPORT_TEMPLATE.md +100 -0
- package/templates/.agent/skills/runtime-inspector/SKILL.md +93 -0
- package/templates/.agent/skills/spec-writer/SKILL.md +58 -0
- package/templates/.agent/skills/spec-writer/references/prd_template.md +174 -0
- package/templates/.agent/skills/system-architect/SKILL.md +620 -0
- package/templates/.agent/skills/system-architect/references/rfc_template.md +59 -0
- package/templates/.agent/skills/system-designer/SKILL.md +439 -0
- package/templates/.agent/skills/system-designer/references/system-design-template.md +533 -0
- package/templates/.agent/skills/task-planner/SKILL.md +474 -0
- package/templates/.agent/skills/task-planner/references/TASK_TEMPLATE.md +133 -0
- package/templates/.agent/skills/tech-evaluator/SKILL.md +135 -0
- package/templates/.agent/skills/tech-evaluator/references/ADR_TEMPLATE.md +68 -0
- package/templates/.agent/workflows/blueprint.md +185 -0
- package/templates/.agent/workflows/challenge.md +467 -0
- package/templates/.agent/workflows/change.md +294 -0
- package/templates/.agent/workflows/craft.md +626 -0
- package/templates/.agent/workflows/design-system.md +497 -0
- package/templates/.agent/workflows/explore.md +307 -0
- package/templates/.agent/workflows/forge.md +354 -0
- package/templates/.agent/workflows/genesis.md +265 -0
- package/templates/.agent/workflows/scout.md +130 -0
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
# 实体提取 Prompt 模板 - 完整版
|
|
2
|
+
|
|
3
|
+
## 概述
|
|
4
|
+
|
|
5
|
+
此 Prompt 用于从用户的自然语言描述中提取系统的逻辑模型。基于 2024 年 LLM Prompt 工程最佳实践设计。
|
|
6
|
+
|
|
7
|
+
## 设计原则
|
|
8
|
+
|
|
9
|
+
基于调研的最佳实践:
|
|
10
|
+
|
|
11
|
+
1. **明确指令**:清晰陈述任务和期望的实体类型
|
|
12
|
+
2. **Few-shot 示例**:提供示例帮助模型理解格式
|
|
13
|
+
3. **Chain-of-Thought**:引导模型逐步推理
|
|
14
|
+
4. **Role Prompting**:赋予模型专家身份
|
|
15
|
+
5. **JSON 输出**:明确指定结构化输出格式
|
|
16
|
+
6. **"Give LLM an Out"**:允许模型表示不确定性
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## 主 Prompt 模板
|
|
21
|
+
|
|
22
|
+
```markdown
|
|
23
|
+
# 角色定义
|
|
24
|
+
|
|
25
|
+
你是一名拥有 15 年经验的资深系统架构师。你的专长是:
|
|
26
|
+
- 从模糊需求中提取精确的系统模型
|
|
27
|
+
- 识别用户遗漏的关键组件
|
|
28
|
+
- 预判潜在的架构风险和耦合问题
|
|
29
|
+
- 设计可扩展、可维护的系统架构
|
|
30
|
+
|
|
31
|
+
# 任务
|
|
32
|
+
|
|
33
|
+
用户想要构建以下系统/功能:
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
{{USER_INPUT}}
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
请执行以下 **结构化分析**,按步骤推理:
|
|
40
|
+
|
|
41
|
+
## 第 1 步:实体提取 (Entity Extraction)
|
|
42
|
+
|
|
43
|
+
列出这个系统中必须存在的所有核心"名词"。
|
|
44
|
+
|
|
45
|
+
思考方向:
|
|
46
|
+
- 用户/角色类:谁会使用这个系统?
|
|
47
|
+
- 数据/资源类:系统处理什么数据?
|
|
48
|
+
- 服务/组件类:需要哪些独立的服务模块?
|
|
49
|
+
- 外部依赖类:需要对接哪些外部 API 或服务?
|
|
50
|
+
- 基础设施类:需要什么存储、缓存、队列?
|
|
51
|
+
|
|
52
|
+
对于每个实体,评估其 **必要性**:
|
|
53
|
+
- [必须] = 没有这个系统无法运行
|
|
54
|
+
- [应该] = 没有这个会严重影响体验
|
|
55
|
+
- [可选] = 增强功能,可后续添加
|
|
56
|
+
|
|
57
|
+
## 第 2 步:数据流分析 (Data Flow Analysis)
|
|
58
|
+
|
|
59
|
+
描述数据如何在这些实体之间流转。
|
|
60
|
+
|
|
61
|
+
使用格式:
|
|
62
|
+
```
|
|
63
|
+
[源实体] --[动作/数据]--> [目标实体]
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
重点考虑:
|
|
67
|
+
- 主流程(Happy Path)是什么?
|
|
68
|
+
- 数据在哪些点会被转换或处理?
|
|
69
|
+
- 哪些是同步操作,哪些应该异步?
|
|
70
|
+
|
|
71
|
+
## 第 3 步:缺失检测 (Missing Component Detection)
|
|
72
|
+
|
|
73
|
+
**这是最关键的一步**。根据你的经验,主动识别用户描述中 **遗漏** 的关键组件:
|
|
74
|
+
|
|
75
|
+
检查清单:
|
|
76
|
+
- [ ] **错误处理**:如果 X 失败了怎么办?重试?回滚?
|
|
77
|
+
- [ ] **数据持久化**:数据存在哪里?需要备份吗?
|
|
78
|
+
- [ ] **认证授权**:谁能访问什么?如何验证身份?
|
|
79
|
+
- [ ] **日志监控**:如何知道系统状态?如何调试问题?
|
|
80
|
+
- [ ] **队列/缓存**:是否需要异步处理?是否需要缓存热点数据?
|
|
81
|
+
- [ ] **配置管理**:hardcode 还是外部配置?环境差异如何处理?
|
|
82
|
+
- [ ] **限流/熔断**:高并发时如何保护系统?
|
|
83
|
+
- [ ] **幂等性**:重复请求会怎样?
|
|
84
|
+
|
|
85
|
+
对于每个缺失项,解释 **为什么需要** 以及 **如果没有会怎样**。
|
|
86
|
+
|
|
87
|
+
## 第 4 步:风险识别 (Risk Identification)
|
|
88
|
+
|
|
89
|
+
预判潜在的问题(即使用户没问):
|
|
90
|
+
|
|
91
|
+
风险类型:
|
|
92
|
+
- **耦合风险**:哪些组件之间可能产生不健康的依赖?
|
|
93
|
+
- **性能风险**:哪里可能成为瓶颈?
|
|
94
|
+
- **可靠性风险**:哪里可能导致级联故障?
|
|
95
|
+
- **安全风险**:哪里可能有安全漏洞?
|
|
96
|
+
- **扩展性风险**:当规模增长 10 倍时,哪里会先出问题?
|
|
97
|
+
|
|
98
|
+
## 第 5 步:边界定义 (Boundary Definition)
|
|
99
|
+
|
|
100
|
+
建议如何划分模块边界以降低耦合:
|
|
101
|
+
|
|
102
|
+
- 哪些组件应该放在一起(高内聚)?
|
|
103
|
+
- 哪些组件应该分离(低耦合)?
|
|
104
|
+
- 接口应该如何设计?
|
|
105
|
+
|
|
106
|
+
---
|
|
107
|
+
|
|
108
|
+
# 输出格式
|
|
109
|
+
|
|
110
|
+
严格按照以下 JSON 格式输出:
|
|
111
|
+
|
|
112
|
+
```json
|
|
113
|
+
{
|
|
114
|
+
"entities": [
|
|
115
|
+
{
|
|
116
|
+
"name": "实体名称",
|
|
117
|
+
"type": "用户|数据|服务|外部|基础设施",
|
|
118
|
+
"necessity": "必须|应该|可选",
|
|
119
|
+
"description": "简要描述"
|
|
120
|
+
}
|
|
121
|
+
],
|
|
122
|
+
"data_flows": [
|
|
123
|
+
{
|
|
124
|
+
"from": "源实体",
|
|
125
|
+
"action": "动作描述",
|
|
126
|
+
"to": "目标实体",
|
|
127
|
+
"data": "传递的数据",
|
|
128
|
+
"sync": true
|
|
129
|
+
}
|
|
130
|
+
],
|
|
131
|
+
"missing_components": [
|
|
132
|
+
{
|
|
133
|
+
"component": "缺失组件名称",
|
|
134
|
+
"category": "错误处理|持久化|安全|监控|性能|配置",
|
|
135
|
+
"reason": "为什么需要",
|
|
136
|
+
"impact_if_missing": "如果没有会怎样",
|
|
137
|
+
"priority": "高|中|低"
|
|
138
|
+
}
|
|
139
|
+
],
|
|
140
|
+
"potential_risks": [
|
|
141
|
+
{
|
|
142
|
+
"risk_type": "耦合|性能|可靠性|安全|扩展性",
|
|
143
|
+
"description": "风险描述",
|
|
144
|
+
"affected_entities": ["相关实体"],
|
|
145
|
+
"mitigation": "建议的缓解措施"
|
|
146
|
+
}
|
|
147
|
+
],
|
|
148
|
+
"boundary_recommendations": [
|
|
149
|
+
{
|
|
150
|
+
"module_name": "模块名称",
|
|
151
|
+
"contains": ["包含的实体"],
|
|
152
|
+
"rationale": "为什么这样划分"
|
|
153
|
+
}
|
|
154
|
+
],
|
|
155
|
+
"questions_for_user": [
|
|
156
|
+
"如果有不确定的地方,列出需要用户澄清的问题"
|
|
157
|
+
]
|
|
158
|
+
}
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
# 重要提示
|
|
162
|
+
|
|
163
|
+
1. **不要编造**:如果信息不足,在 `questions_for_user` 中列出问题
|
|
164
|
+
2. **保守估计**:宁可多识别缺失组件,也不要遗漏
|
|
165
|
+
3. **解释推理**:对每个判断提供理由
|
|
166
|
+
4. **实际经验**:基于真实系统设计经验,而不是理论
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
---
|
|
170
|
+
|
|
171
|
+
## Few-Shot 示例
|
|
172
|
+
|
|
173
|
+
### 示例输入
|
|
174
|
+
|
|
175
|
+
```
|
|
176
|
+
我想做一个"视频摘要生成器":用户上传视频,系统用 Whisper 转文字,
|
|
177
|
+
然后用 GPT 生成摘要,最后返回给用户。
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
### 示例输出
|
|
181
|
+
|
|
182
|
+
```json
|
|
183
|
+
{
|
|
184
|
+
"entities": [
|
|
185
|
+
{"name": "User", "type": "用户", "necessity": "必须", "description": "上传视频并获取摘要的用户"},
|
|
186
|
+
{"name": "VideoFile", "type": "数据", "necessity": "必须", "description": "用户上传的视频文件"},
|
|
187
|
+
{"name": "VideoUploader", "type": "服务", "necessity": "必须", "description": "处理视频上传的服务"},
|
|
188
|
+
{"name": "TranscriptionService", "type": "服务", "necessity": "必须", "description": "调用 Whisper API 转文字"},
|
|
189
|
+
{"name": "SummaryService", "type": "服务", "necessity": "必须", "description": "调用 GPT 生成摘要"},
|
|
190
|
+
{"name": "WhisperAPI", "type": "外部", "necessity": "必须", "description": "OpenAI Whisper 语音识别"},
|
|
191
|
+
{"name": "GPT_API", "type": "外部", "necessity": "必须", "description": "OpenAI GPT 文本生成"},
|
|
192
|
+
{"name": "FileStorage", "type": "基础设施", "necessity": "必须", "description": "存储上传的视频和生成的摘要"},
|
|
193
|
+
{"name": "TaskQueue", "type": "基础设施", "necessity": "应该", "description": "异步处理视频任务"}
|
|
194
|
+
],
|
|
195
|
+
"data_flows": [
|
|
196
|
+
{"from": "User", "action": "上传", "to": "VideoUploader", "data": "视频文件", "sync": true},
|
|
197
|
+
{"from": "VideoUploader", "action": "存储", "to": "FileStorage", "data": "视频文件", "sync": true},
|
|
198
|
+
{"from": "VideoUploader", "action": "创建任务", "to": "TaskQueue", "data": "任务ID", "sync": true},
|
|
199
|
+
{"from": "TaskQueue", "action": "触发", "to": "TranscriptionService", "data": "视频路径", "sync": false},
|
|
200
|
+
{"from": "TranscriptionService", "action": "调用", "to": "WhisperAPI", "data": "音频数据", "sync": true},
|
|
201
|
+
{"from": "TranscriptionService", "action": "传递", "to": "SummaryService", "data": "文字内容", "sync": true},
|
|
202
|
+
{"from": "SummaryService", "action": "调用", "to": "GPT_API", "data": "文字内容", "sync": true},
|
|
203
|
+
{"from": "SummaryService", "action": "存储", "to": "FileStorage", "data": "摘要结果", "sync": true}
|
|
204
|
+
],
|
|
205
|
+
"missing_components": [
|
|
206
|
+
{
|
|
207
|
+
"component": "任务状态追踪",
|
|
208
|
+
"category": "监控",
|
|
209
|
+
"reason": "用户上传后需要知道处理进度",
|
|
210
|
+
"impact_if_missing": "用户无法知道视频是否处理完成,体验极差",
|
|
211
|
+
"priority": "高"
|
|
212
|
+
},
|
|
213
|
+
{
|
|
214
|
+
"component": "API 调用重试机制",
|
|
215
|
+
"category": "错误处理",
|
|
216
|
+
"reason": "Whisper/GPT API 可能超时或失败",
|
|
217
|
+
"impact_if_missing": "单次失败导致整个任务失败,浪费用户时间",
|
|
218
|
+
"priority": "高"
|
|
219
|
+
},
|
|
220
|
+
{
|
|
221
|
+
"component": "视频大小/时长限制",
|
|
222
|
+
"category": "性能",
|
|
223
|
+
"reason": "大视频会导致处理时间过长和成本过高",
|
|
224
|
+
"impact_if_missing": "用户可能上传几GB的视频导致系统崩溃",
|
|
225
|
+
"priority": "高"
|
|
226
|
+
},
|
|
227
|
+
{
|
|
228
|
+
"component": "费用追踪/计费",
|
|
229
|
+
"category": "配置",
|
|
230
|
+
"reason": "API 调用有成本,需要追踪",
|
|
231
|
+
"impact_if_missing": "无法控制成本,可能产生意外账单",
|
|
232
|
+
"priority": "中"
|
|
233
|
+
}
|
|
234
|
+
],
|
|
235
|
+
"potential_risks": [
|
|
236
|
+
{
|
|
237
|
+
"risk_type": "性能",
|
|
238
|
+
"description": "视频处理是 CPU/内存密集型,大视频可能导致 OOM",
|
|
239
|
+
"affected_entities": ["TranscriptionService"],
|
|
240
|
+
"mitigation": "限制视频大小,使用流式处理"
|
|
241
|
+
},
|
|
242
|
+
{
|
|
243
|
+
"risk_type": "可靠性",
|
|
244
|
+
"description": "如果 TaskQueue 宕机,所有待处理任务丢失",
|
|
245
|
+
"affected_entities": ["TaskQueue"],
|
|
246
|
+
"mitigation": "使用持久化队列(如 Redis + 持久化)"
|
|
247
|
+
},
|
|
248
|
+
{
|
|
249
|
+
"risk_type": "耦合",
|
|
250
|
+
"description": "TranscriptionService 和 SummaryService 如果同步调用,一个慢会拖慢整个流程",
|
|
251
|
+
"affected_entities": ["TranscriptionService", "SummaryService"],
|
|
252
|
+
"mitigation": "考虑将两个服务通过队列解耦"
|
|
253
|
+
}
|
|
254
|
+
],
|
|
255
|
+
"boundary_recommendations": [
|
|
256
|
+
{
|
|
257
|
+
"module_name": "上传模块",
|
|
258
|
+
"contains": ["VideoUploader", "FileStorage"],
|
|
259
|
+
"rationale": "上传和存储紧密相关,应该放在一起"
|
|
260
|
+
},
|
|
261
|
+
{
|
|
262
|
+
"module_name": "处理模块",
|
|
263
|
+
"contains": ["TranscriptionService", "SummaryService", "TaskQueue"],
|
|
264
|
+
"rationale": "视频处理流程,通过队列异步执行"
|
|
265
|
+
},
|
|
266
|
+
{
|
|
267
|
+
"module_name": "外部集成层",
|
|
268
|
+
"contains": ["WhisperAPI", "GPT_API"],
|
|
269
|
+
"rationale": "所有外部 API 调用应该通过统一的适配层,方便切换供应商"
|
|
270
|
+
}
|
|
271
|
+
],
|
|
272
|
+
"questions_for_user": [
|
|
273
|
+
"视频支持的最大时长/大小是多少?",
|
|
274
|
+
"用户是否需要实时查看处理进度?",
|
|
275
|
+
"摘要结果需要保留多久?"
|
|
276
|
+
]
|
|
277
|
+
}
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
---
|
|
281
|
+
|
|
282
|
+
## 追问策略
|
|
283
|
+
|
|
284
|
+
如果初始分析不够深入,使用以下追问:
|
|
285
|
+
|
|
286
|
+
1. **边界追问**:"如果 [实体A] 出错了,[实体B] 会怎样?"
|
|
287
|
+
2. **规模追问**:"如果有 10000 个用户同时使用,哪里会先崩?"
|
|
288
|
+
3. **演化追问**:"6 个月后如果要加 [新功能],需要改动哪些组件?"
|
|
289
|
+
4. **成本追问**:"这个架构的运维成本主要在哪里?"
|
|
290
|
+
5. **安全追问**:"如果有恶意用户尝试攻击,最薄弱的环节是哪里?"
|
|
291
|
+
|
|
292
|
+
---
|
|
293
|
+
|
|
294
|
+
## 与其他 Skills 的配合
|
|
295
|
+
|
|
296
|
+
- 对于 **Brownfield** 模式:先用 `build-inspector` 分析构建边界,用 `runtime-inspector` 分析 IPC,再用此 Prompt 分析新功能
|
|
297
|
+
- 分析结果应传递给 Scout 的功能冲突分析(Step 6)和最终报告生成
|
|
298
|
+
- 识别的缺失组件应在后续 Blueprint 阶段详细设计
|
|
299
|
+
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import argparse
|
|
3
|
+
import json
|
|
4
|
+
import re
|
|
5
|
+
|
|
6
|
+
# Simple regex for finding potential domain terms (Classes, Structs)
|
|
7
|
+
# This is a heuristic. For better results, use the tree-sitter slice we built earlier.
|
|
8
|
+
# But for "Concept Modeling" of a whole repo, a fast scan is often enough to start.
|
|
9
|
+
|
|
10
|
+
PATTERNS = {
|
|
11
|
+
'python': [r'class\s+([A-Z][a-zA-Z0-9_]*)'],
|
|
12
|
+
'rust': [r'struct\s+([A-Z][a-zA-Z0-9_]*)', r'enum\s+([A-Z][a-zA-Z0-9_]*)'],
|
|
13
|
+
'javascript': [r'class\s+([A-Z][a-zA-Z0-9_]*)', r'interface\s+([A-Z][a-zA-Z0-9_]*)'],
|
|
14
|
+
'typescript': [r'class\s+([A-Z][a-zA-Z0-9_]*)', r'interface\s+([A-Z][a-zA-Z0-9_]*)', r'type\s+([A-Z][a-zA-Z0-9_]*)']
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
def scan_identifiers(root_dir: str):
|
|
18
|
+
terms = set()
|
|
19
|
+
|
|
20
|
+
for root, _, files in os.walk(root_dir):
|
|
21
|
+
for file in files:
|
|
22
|
+
ext = os.path.splitext(file)[1]
|
|
23
|
+
lang = None
|
|
24
|
+
if ext == '.py': lang = 'python'
|
|
25
|
+
elif ext == '.rs': lang = 'rust'
|
|
26
|
+
elif ext in ['.js', '.jsx']: lang = 'javascript'
|
|
27
|
+
elif ext in ['.ts', '.tsx']: lang = 'typescript'
|
|
28
|
+
|
|
29
|
+
if lang:
|
|
30
|
+
try:
|
|
31
|
+
with open(os.path.join(root, file), 'r', encoding='utf-8') as f:
|
|
32
|
+
content = f.read()
|
|
33
|
+
|
|
34
|
+
for pat in PATTERNS[lang]:
|
|
35
|
+
matches = re.finditer(pat, content)
|
|
36
|
+
for m in matches:
|
|
37
|
+
terms.add(m.group(1))
|
|
38
|
+
except Exception:
|
|
39
|
+
pass
|
|
40
|
+
|
|
41
|
+
return sorted(list(terms))
|
|
42
|
+
|
|
43
|
+
def main():
|
|
44
|
+
parser = argparse.ArgumentParser(description="Generate Glossary Context")
|
|
45
|
+
parser.add_argument("--path", required=True, help="Repo path")
|
|
46
|
+
parser.add_argument("--output", "-o", help="Output JSON file for LLM")
|
|
47
|
+
|
|
48
|
+
args = parser.parse_args()
|
|
49
|
+
|
|
50
|
+
print(f"Scanning for domain terms in {args.path}...")
|
|
51
|
+
terms = scan_identifiers(args.path)
|
|
52
|
+
|
|
53
|
+
result = {
|
|
54
|
+
"candidate_terms": terms,
|
|
55
|
+
"instruction": "The above terms were extracted from the codebase. Please filter out technical terms (like 'Config', 'Factory') and keep only Domain Terms."
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if args.output:
|
|
59
|
+
with open(args.output, 'w') as f:
|
|
60
|
+
json.dump(result, f, indent=2)
|
|
61
|
+
print(f"Context saved to {args.output}")
|
|
62
|
+
else:
|
|
63
|
+
print(json.dumps(result, indent=2))
|
|
64
|
+
|
|
65
|
+
if __name__ == "__main__":
|
|
66
|
+
main()
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: git-forensics
|
|
3
|
+
description: 分析 Git 历史,发现"逻辑耦合"(总一起改的文件)和"热点"(高频修改的复杂模块)。基于 Adam Tornhill 的《Your Code as a Crime Scene》方法论。
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# 考古学家手册 (The Archaeologist's Field Notes)
|
|
7
|
+
|
|
8
|
+
> "历史不会重复,但会押韵。静态分析告诉你结构,Git 取证告诉你痛苦的真相。" —— Adam Tornhill
|
|
9
|
+
|
|
10
|
+
本技能基于 **Adam Tornhill 的《Your Code as a Crime Scene》** 方法论。
|
|
11
|
+
核心思想:代码的**演化历史**比代码本身更能揭示设计问题。
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## ⚠️ 强制深度思考
|
|
16
|
+
|
|
17
|
+
> [!IMPORTANT]
|
|
18
|
+
> 在执行任何分析之前,你**必须**调用 `sequential thinking` 工具,视情况进行 **2—3 步**推理。
|
|
19
|
+
> 思考内容例如:
|
|
20
|
+
> 1. "这个项目的 Git 历史有多深?是否需要 `git fetch --unshallow`?"
|
|
21
|
+
> 2. "我应该关注哪个时间范围?(最近 3 个月?1 年?)"
|
|
22
|
+
> 3. "有没有明显的'噪音文件'(如 `package-lock.json`)需要排除?"
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## ⚡ 快速启动
|
|
27
|
+
1. **耦合分析**: `python scripts/git_forensics.py --repo . --threshold 0.3`
|
|
28
|
+
2. **热点探测**: `python scripts/git_hotspots.py --repo . --days 180`
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
## 🧭 探索流程 (The Dig)
|
|
33
|
+
|
|
34
|
+
### 第一步:感知时间流 (The Tornhill Method)
|
|
35
|
+
* **老师傅箴言**: "代码的价值不是它是什么,而是它是如何变成这样的。"
|
|
36
|
+
* 运行 `git log --oneline -n 100`,快速感知项目近期活跃度。
|
|
37
|
+
* **关键推断**: 如果最近 50 次提交都只涉及一两个目录,那就是"震中"——最可能藏有风险的地方。
|
|
38
|
+
|
|
39
|
+
### 第二步:发现隐性耦合 (Temporal Coupling / Change Coupling)
|
|
40
|
+
* **核心问题**: "有没有两个文件,它们在代码里**没有** `import`/`use` 关系,但 **70%** 的提交都一起出现?"
|
|
41
|
+
* **老师傅警报 (来自 Adam Tornhill)**:
|
|
42
|
+
* ⚠️ **逻辑耦合但物理分离** -> 这是"架构腐烂 (Architectural Decay)"的强信号!
|
|
43
|
+
* ⚠️ **跨构建根的耦合** -> 如果 `service/ipc.rs` 总和 `gui/api.ts` 一起改,但它们属于不同构建根,这是**版本漂移**的温床!
|
|
44
|
+
* **处方**: 要么合并它们到同一模块,要么抽象出共享的 Schema/契约层。
|
|
45
|
+
|
|
46
|
+
### 第三步:定位热点 (Hotspot Analysis - CodeScene Methodology)
|
|
47
|
+
* **公式**: 热点 = 高变更频率 (Churn) × 高复杂度 (Complexity)
|
|
48
|
+
* **老师傅策略矩阵 (来自 CodeScene)**:
|
|
49
|
+
|
|
50
|
+
| | 低复杂度 | 高复杂度 |
|
|
51
|
+
| :---: | :---: | :---: |
|
|
52
|
+
| **高 Churn** | 配置/生成代码,可忽略 | 🔴 **优先重构!Bug 温床,ROI 最高** |
|
|
53
|
+
| **低 Churn** | 稳定模块,别动 | 🟡 遗留雷区,小心翼翼 |
|
|
54
|
+
|
|
55
|
+
* **老师傅建议**: 重构资源有限时,**只攻高 Churn + 高 Complexity 的象限**。这是投资回报率最高的地方。
|
|
56
|
+
|
|
57
|
+
---
|
|
58
|
+
|
|
59
|
+
## 🛡️ 老师傅守则
|
|
60
|
+
|
|
61
|
+
1. **先取消浅克隆**: `git fetch --unshallow`。没有历史 = 没有数据 = 你是盲人。
|
|
62
|
+
2. **过滤噪音**: 忽略 `package-lock.json`, `Cargo.lock`, `*.min.js`, `dist/` 等生成物。它们会污染你的分析结果。
|
|
63
|
+
3. **警惕大规模重命名**: 重命名 (`git mv`) 会干扰追踪。如果发现诡异的耦合数据,手动检查是否是重命名导致。
|
|
64
|
+
4. **关联构建信息**: 在输出耦合对时,**标注它们分别属于哪个构建根**。这是连接 Git 分析和 Build 分析的关键桥梁。
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
## 📤 输出清单
|
|
69
|
+
|
|
70
|
+
1. **Coupling Pairs (耦合对)**: Score > 0.7 的文件对。**标注它们的构建根**。
|
|
71
|
+
2. **Cross-Root Couplings (跨根耦合)**: 重点标记!如果两个高耦合文件属于不同构建根,这是**头号风险**。
|
|
72
|
+
3. **Hotspots (热点)**: 高风险模块列表 (高 Churn + 高 Complexity)。
|
|
73
|
+
4. **Orphans (孤儿)**: 超过 1 年无人触碰的文件(知识腐烂警告)。
|
|
74
|
+
5. **Refactoring Priority**: 根据 Churn/Complexity 矩阵给出建议的重构优先级。
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
# Git Forensics 分析方法论
|
|
2
|
+
|
|
3
|
+
## 概述
|
|
4
|
+
|
|
5
|
+
Git Forensics 的核心价值不仅是数据抓取,更重要的是**分析**和**可操作建议**。本文档定义了分析的方法论和决策规则。
|
|
6
|
+
|
|
7
|
+
## 核心指标
|
|
8
|
+
|
|
9
|
+
### 1. 共改频率 (Co-change Frequency)
|
|
10
|
+
|
|
11
|
+
**定义**:
|
|
12
|
+
```
|
|
13
|
+
frequency = 共改次数 / 目标文件总提交次数
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
**风险等级判定**:
|
|
17
|
+
|
|
18
|
+
| 频率范围 | 风险等级 | 标记 | 解读 |
|
|
19
|
+
|---|---|---|---|
|
|
20
|
+
| ≥ 0.70 | 🔴 HIGH | `HIGH_COUPLING` | 强逻辑耦合,修改一个必须考虑另一个 |
|
|
21
|
+
| 0.40 - 0.69 | 🟡 MEDIUM | `MEDIUM_COUPLING` | 中等耦合,建议关注 |
|
|
22
|
+
| < 0.40 | 🟢 LOW | (无标记) | 偶发性共改,可能是巧合 |
|
|
23
|
+
|
|
24
|
+
### 2. 文件类型分类
|
|
25
|
+
|
|
26
|
+
不同类型的文件有不同的耦合性质:
|
|
27
|
+
|
|
28
|
+
| 类型 | 匹配模式 | 耦合性质 | 处理方式 |
|
|
29
|
+
|---|---|---|---|
|
|
30
|
+
| 测试文件 | `*test*`, `*spec*`, `__tests__/` | ✅ 合理耦合 | 正常,测试应跟随代码变化 |
|
|
31
|
+
| 配置文件 | `*config*`, `.env*`, `*.yaml`, `*.json` | ⚠️ 需警惕 | 可能存在硬编码或配置管理问题 |
|
|
32
|
+
| 文档文件 | `*.md`, `README*`, `CHANGELOG*` | ✅ 合理耦合 | 正常,文档应随代码更新 |
|
|
33
|
+
| 生产代码 | 其他 | ❓ 需深入分析 | 高频率 + 无物理依赖 = 隐性耦合 |
|
|
34
|
+
|
|
35
|
+
### 3. 时间衰减因素
|
|
36
|
+
|
|
37
|
+
近期的共改比远期的更有参考价值:
|
|
38
|
+
|
|
39
|
+
| 时间范围 | 权重建议 | 说明 |
|
|
40
|
+
|---|---|---|
|
|
41
|
+
| 最近 30 天 | 1.5x | 最近的修改模式更能反映当前状态 |
|
|
42
|
+
| 30-90 天 | 1.0x | 标准权重 |
|
|
43
|
+
| 90-180 天 | 0.7x | 历史参考,权重降低 |
|
|
44
|
+
| > 180 天 | 0.5x | 仅作参考 |
|
|
45
|
+
|
|
46
|
+
**注意**:当前脚本实现尚未包含时间衰减,作为后续优化项。
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
## 分析建议生成规则
|
|
51
|
+
|
|
52
|
+
### 规则 1:高频生产代码耦合
|
|
53
|
+
|
|
54
|
+
**条件**:
|
|
55
|
+
- 两个生产代码文件
|
|
56
|
+
- 共改频率 ≥ 0.70
|
|
57
|
+
- 无物理依赖(即 A 不 import B)
|
|
58
|
+
|
|
59
|
+
**建议**:
|
|
60
|
+
- "考虑将 A 和 B 合并为同一模块"
|
|
61
|
+
- "或者提取公共接口,明确依赖关系"
|
|
62
|
+
- "检查是否存在隐性的数据依赖"
|
|
63
|
+
|
|
64
|
+
### 规则 2:跨模块高频耦合
|
|
65
|
+
|
|
66
|
+
**条件**:
|
|
67
|
+
- 两个文件属于不同目录/模块
|
|
68
|
+
- 共改频率 ≥ 0.50
|
|
69
|
+
|
|
70
|
+
**建议**:
|
|
71
|
+
- "可能需要重新划分模块边界"
|
|
72
|
+
- "考虑使用事件/消息解耦"
|
|
73
|
+
- "评估是否应该合并为同一模块"
|
|
74
|
+
|
|
75
|
+
### 规则 3:配置文件高频耦合
|
|
76
|
+
|
|
77
|
+
**条件**:
|
|
78
|
+
- 生产代码与配置文件共改频率 ≥ 0.30
|
|
79
|
+
|
|
80
|
+
**建议**:
|
|
81
|
+
- "检查是否存在硬编码的配置值"
|
|
82
|
+
- "考虑使用环境变量或配置中心"
|
|
83
|
+
- "评估配置管理策略"
|
|
84
|
+
|
|
85
|
+
### 规则 4:测试覆盖率信号
|
|
86
|
+
|
|
87
|
+
**条件**:
|
|
88
|
+
- 生产代码没有对应的测试文件共改
|
|
89
|
+
- 或测试文件共改频率 < 0.30
|
|
90
|
+
|
|
91
|
+
**建议**:
|
|
92
|
+
- "该文件可能缺少测试覆盖"
|
|
93
|
+
- "修改时未同步更新测试可能有风险"
|
|
94
|
+
|
|
95
|
+
### 规则 5:单点责任人风险
|
|
96
|
+
|
|
97
|
+
**条件**:
|
|
98
|
+
- 某个文件 80% 以上的修改来自同一作者
|
|
99
|
+
|
|
100
|
+
**建议**:
|
|
101
|
+
- "存在知识单点风险"
|
|
102
|
+
- "建议进行知识转移或代码评审"
|
|
103
|
+
|
|
104
|
+
---
|
|
105
|
+
|
|
106
|
+
## 输出结构
|
|
107
|
+
|
|
108
|
+
### 标准输出字段
|
|
109
|
+
|
|
110
|
+
```json
|
|
111
|
+
{
|
|
112
|
+
"target_file": "分析的目标文件",
|
|
113
|
+
"analysis_period_days": 180,
|
|
114
|
+
"total_commits_modifying_target": 25,
|
|
115
|
+
"co_changed_files": [
|
|
116
|
+
{
|
|
117
|
+
"file": "共改文件路径",
|
|
118
|
+
"co_change_count": 20,
|
|
119
|
+
"frequency": 0.80,
|
|
120
|
+
"warning": "HIGH_COUPLING|MEDIUM_COUPLING",
|
|
121
|
+
"category": "PRODUCTION|TEST_FILE|CONFIG_FILE|DOC_FILE"
|
|
122
|
+
}
|
|
123
|
+
],
|
|
124
|
+
"last_modified_date": "2024-12-20",
|
|
125
|
+
"primary_authors": ["Alice", "Bob"],
|
|
126
|
+
"analysis": {
|
|
127
|
+
"high_risk_files": ["高风险文件列表"],
|
|
128
|
+
"recommendations": ["可操作的建议列表"]
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### 建议的格式
|
|
134
|
+
|
|
135
|
+
建议应该是**可操作的**,而不是泛泛的描述:
|
|
136
|
+
|
|
137
|
+
✅ **好的建议**:
|
|
138
|
+
- "考虑将 `login.ts` 和 `session.ts` 合并,因为它们在 80% 的提交中一起修改"
|
|
139
|
+
- "文件 `config.ts` 与多个生产代码共改,检查是否应该使用环境变量"
|
|
140
|
+
|
|
141
|
+
❌ **差的建议**:
|
|
142
|
+
- "存在耦合"(太模糊)
|
|
143
|
+
- "需要注意"(没有行动指导)
|
|
144
|
+
|
|
145
|
+
---
|
|
146
|
+
|
|
147
|
+
## 边界情况处理
|
|
148
|
+
|
|
149
|
+
### 大规模重构 Commit
|
|
150
|
+
|
|
151
|
+
**问题**:一次重构可能修改几十个文件,干扰分析。
|
|
152
|
+
|
|
153
|
+
**解决方案**:
|
|
154
|
+
- 识别修改文件数 > 20 的 commit
|
|
155
|
+
- 可选择忽略这类 commit
|
|
156
|
+
- 或降低其权重
|
|
157
|
+
|
|
158
|
+
**当前实现**:未处理,作为后续优化项。
|
|
159
|
+
|
|
160
|
+
### 新文件
|
|
161
|
+
|
|
162
|
+
**问题**:新创建的文件历史太短。
|
|
163
|
+
|
|
164
|
+
**解决方案**:
|
|
165
|
+
- 如果文件存在时间 < 30 天,标记分析结果为"数据不足"
|
|
166
|
+
- 建议等待更多历史数据
|
|
167
|
+
|
|
168
|
+
### 删除的文件
|
|
169
|
+
|
|
170
|
+
**问题**:历史中引用了已删除的文件。
|
|
171
|
+
|
|
172
|
+
**解决方案**:
|
|
173
|
+
- 在输出中标记 `[DELETED]`
|
|
174
|
+
- 仍然报告历史耦合关系
|
|
175
|
+
|
|
176
|
+
---
|
|
177
|
+
|
|
178
|
+
## 与其他 Skills 的配合
|
|
179
|
+
|
|
180
|
+
1. **与 `build-inspector` 对比**:
|
|
181
|
+
- build-inspector 提供构建边界和版本漂移风险
|
|
182
|
+
- git-forensics 提供逻辑耦合
|
|
183
|
+
- 两者交叉 = 跨构建根的耦合(最高风险)
|
|
184
|
+
|
|
185
|
+
2. **与 `runtime-inspector` 结合**:
|
|
186
|
+
- 在高耦合文件中检查 IPC 相关代码
|
|
187
|
+
- 这些文件的协议漂移风险更高
|
|
188
|
+
|
|
189
|
+
3. **输出给 Scout 功能冲突分析**:
|
|
190
|
+
- 高风险文件列表
|
|
191
|
+
- 生成的建议
|
|
192
|
+
- 耦合关系图数据
|
|
193
|
+
|