source-kb 0.2.2__py3-none-any.whl

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 (228) hide show
  1. cli/__init__.py +50 -0
  2. cli/__main__.py +5 -0
  3. cli/commands/__init__.py +1 -0
  4. cli/commands/anchor_fix.py +47 -0
  5. cli/commands/diff_doc.py +52 -0
  6. cli/commands/dispatch.py +77 -0
  7. cli/commands/extract.py +72 -0
  8. cli/commands/file_list.py +74 -0
  9. cli/commands/index.py +84 -0
  10. cli/commands/lock.py +89 -0
  11. cli/commands/merge.py +60 -0
  12. cli/commands/merge_delta.py +19 -0
  13. cli/commands/metadata.py +24 -0
  14. cli/commands/pipeline.py +45 -0
  15. cli/commands/post_merge.py +43 -0
  16. cli/commands/query.py +52 -0
  17. cli/commands/render.py +101 -0
  18. cli/commands/scan_repos.py +46 -0
  19. cli/commands/setup.py +94 -0
  20. cli/commands/split.py +196 -0
  21. cli/commands/stale_files.py +98 -0
  22. cli/commands/validate.py +191 -0
  23. core/__init__.py +32 -0
  24. core/config.py +261 -0
  25. core/docs/__init__.py +7 -0
  26. core/docs/section_updater.py +286 -0
  27. core/docs/shared.py +149 -0
  28. core/git.py +294 -0
  29. core/interfaces.py +249 -0
  30. core/monitor/__init__.py +5 -0
  31. core/monitor/progress.py +83 -0
  32. core/monitor/prompt_store.py +49 -0
  33. core/paths.py +141 -0
  34. core/preset.py +237 -0
  35. core/preset_accessors.py +202 -0
  36. core/preset_classify.py +132 -0
  37. core/preset_hooks.py +129 -0
  38. core/preset_profile.py +89 -0
  39. core/prompt/__init__.py +7 -0
  40. core/prompt/__main__.py +147 -0
  41. core/prompt/content.py +320 -0
  42. core/prompt/context_manager.py +164 -0
  43. core/prompt/renderer.py +236 -0
  44. core/prompt/response_parser.py +274 -0
  45. core/prompt/templates.py +357 -0
  46. core/prompt/validate_parity.py +162 -0
  47. core/prompt/variables.py +339 -0
  48. core/rag/__init__.py +22 -0
  49. core/rag/__main__.py +136 -0
  50. core/rag/bm25_index.py +268 -0
  51. core/rag/chunker.py +273 -0
  52. core/rag/embedder.py +151 -0
  53. core/rag/indexer.py +292 -0
  54. core/rag/loader.py +89 -0
  55. core/rag/retriever.py +82 -0
  56. core/skeleton/__init__.py +11 -0
  57. core/skeleton/__main__.py +934 -0
  58. core/skeleton/anchor_fix.py +250 -0
  59. core/skeleton/classify.py +331 -0
  60. core/skeleton/cmd_anchor_fix.py +43 -0
  61. core/skeleton/cmd_diff_doc.py +44 -0
  62. core/skeleton/cmd_lock.py +87 -0
  63. core/skeleton/cmd_merge_delta.py +41 -0
  64. core/skeleton/community.py +233 -0
  65. core/skeleton/dependency_graph.py +306 -0
  66. core/skeleton/diff_doc.py +248 -0
  67. core/skeleton/dispatch.py +273 -0
  68. core/skeleton/dispatch_render.py +319 -0
  69. core/skeleton/dispatch_source.py +111 -0
  70. core/skeleton/extract.py +218 -0
  71. core/skeleton/extract_methods.py +298 -0
  72. core/skeleton/file_list.py +239 -0
  73. core/skeleton/impact.py +278 -0
  74. core/skeleton/jar_download.py +177 -0
  75. core/skeleton/jar_resolver.py +186 -0
  76. core/skeleton/loader.py +162 -0
  77. core/skeleton/merge.py +278 -0
  78. core/skeleton/merge_delta.py +229 -0
  79. core/skeleton/metadata.py +96 -0
  80. core/skeleton/metadata_builders.py +264 -0
  81. core/skeleton/module_dag.py +330 -0
  82. core/skeleton/parsers/__init__.py +71 -0
  83. core/skeleton/parsers/jqassistant.py +300 -0
  84. core/skeleton/parsers/jqassistant_cypher.py +225 -0
  85. core/skeleton/parsers/regex.py +171 -0
  86. core/skeleton/parsers/treesitter.py +324 -0
  87. core/skeleton/parsers/treesitter_java.py +284 -0
  88. core/skeleton/parsers/treesitter_multi.py +289 -0
  89. core/skeleton/pom_parser.py +299 -0
  90. core/skeleton/post_merge.py +295 -0
  91. core/skeleton/post_merge_llm.py +82 -0
  92. core/skeleton/query.py +195 -0
  93. core/skeleton/shard_context.py +177 -0
  94. core/skeleton/split.py +180 -0
  95. core/skeleton/split_cache.py +107 -0
  96. core/skeleton/split_feedback.py +174 -0
  97. core/skeleton/split_plan.py +219 -0
  98. core/skeleton/split_plan_helpers.py +305 -0
  99. core/skeleton/split_plan_llm.py +274 -0
  100. core/utils.py +135 -0
  101. core/validators/__init__.py +65 -0
  102. core/validators/__main__.py +215 -0
  103. core/validators/consistency.py +203 -0
  104. core/validators/coverage.py +171 -0
  105. core/validators/duplicates.py +76 -0
  106. core/validators/engine.py +224 -0
  107. core/validators/links.py +76 -0
  108. core/validators/sampling.py +169 -0
  109. core/validators/structure.py +144 -0
  110. engine/__init__.py +7 -0
  111. engine/assembler.py +231 -0
  112. engine/confirm.py +65 -0
  113. engine/dedup.py +106 -0
  114. engine/main.py +211 -0
  115. engine/pipeline/__init__.py +163 -0
  116. engine/pipeline/recovery.py +250 -0
  117. engine/pipeline/steps/__init__.py +23 -0
  118. engine/pipeline/steps/audit.py +220 -0
  119. engine/pipeline/steps/audit_apply.py +195 -0
  120. engine/pipeline/steps/audit_helpers.py +155 -0
  121. engine/pipeline/steps/classify_llm.py +236 -0
  122. engine/pipeline/steps/classify_prompt.py +223 -0
  123. engine/pipeline/steps/finalize.py +160 -0
  124. engine/pipeline/steps/generate.py +169 -0
  125. engine/pipeline/steps/generate_batch.py +197 -0
  126. engine/pipeline/steps/generate_recovery.py +170 -0
  127. engine/pipeline/steps/llm_plan_split.py +253 -0
  128. engine/pipeline/steps/lock.py +64 -0
  129. engine/pipeline/steps/preflight.py +237 -0
  130. engine/pipeline/steps/preflight_adjust.py +147 -0
  131. engine/pipeline/steps/pregenerate.py +130 -0
  132. engine/pipeline/steps/quality.py +81 -0
  133. engine/pipeline/steps/skeleton.py +149 -0
  134. engine/pipeline/steps/source.py +163 -0
  135. engine/pipeline/steps/sync.py +117 -0
  136. engine/pipeline/steps/sync_finalize.py +237 -0
  137. engine/pipeline/steps/sync_update.py +341 -0
  138. engine/pipelines.py +91 -0
  139. engine/runner.py +335 -0
  140. engine/strategies/__init__.py +86 -0
  141. engine/strategies/api.py +128 -0
  142. engine/strategies/delegated.py +50 -0
  143. engine/strategies/dryrun.py +25 -0
  144. engine/two_phase.py +143 -0
  145. mcp_server/__init__.py +73 -0
  146. mcp_server/__main__.py +5 -0
  147. mcp_server/tools/__init__.py +1 -0
  148. mcp_server/tools/config.py +63 -0
  149. mcp_server/tools/discovery.py +276 -0
  150. mcp_server/tools/generation.py +184 -0
  151. mcp_server/tools/planning.py +144 -0
  152. mcp_server/tools/source.py +175 -0
  153. mcp_server/tools/validation.py +140 -0
  154. mcp_server/tools/workflow.py +166 -0
  155. mcp_server/workflow_loader.py +204 -0
  156. presets/generic/audit_dimensions.md +132 -0
  157. presets/generic/doc_types.yaml +152 -0
  158. presets/generic/preset.yaml +115 -0
  159. presets/java-spring/audit_dimensions.md +228 -0
  160. presets/java-spring/audit_dimensions.yaml +203 -0
  161. presets/java-spring/doc_types.yaml +269 -0
  162. presets/java-spring/hooks.py +122 -0
  163. presets/java-spring/preset.yaml +341 -0
  164. presets/java-spring/templates/README.md +34 -0
  165. presets/java-spring/templates/audit-system.md +15 -0
  166. presets/java-spring/templates/subagent-aop.md +105 -0
  167. presets/java-spring/templates/subagent-api.md +63 -0
  168. presets/java-spring/templates/subagent-architecture.md +111 -0
  169. presets/java-spring/templates/subagent-async-events.md +107 -0
  170. presets/java-spring/templates/subagent-audit-api-contracts.md +40 -0
  171. presets/java-spring/templates/subagent-audit-architecture.md +38 -0
  172. presets/java-spring/templates/subagent-audit-business.md +40 -0
  173. presets/java-spring/templates/subagent-audit-data-models.md +40 -0
  174. presets/java-spring/templates/subagent-business.md +129 -0
  175. presets/java-spring/templates/subagent-caching.md +75 -0
  176. presets/java-spring/templates/subagent-database-access.md +114 -0
  177. presets/java-spring/templates/subagent-enum.md +75 -0
  178. presets/java-spring/templates/subagent-error-handling.md +91 -0
  179. presets/java-spring/templates/subagent-external-integrations.md +80 -0
  180. presets/java-spring/templates/subagent-index.md +122 -0
  181. presets/java-spring/templates/subagent-messaging.md +97 -0
  182. presets/java-spring/templates/subagent-model.md +88 -0
  183. presets/java-spring/templates/subagent-observability.md +91 -0
  184. presets/java-spring/templates/subagent-scheduled.md +81 -0
  185. presets/java-spring/templates/subagent-security.md +102 -0
  186. presets/java-spring/templates/subagent-structure.md +101 -0
  187. presets/java-spring/templates/subagent-sync-section.md +34 -0
  188. presets/java-spring/templates/subagent-utils.md +73 -0
  189. presets/java-spring/templates/sync-system.md +8 -0
  190. presets/java-spring/workflow-extensions.md +112 -0
  191. skills/__init__.py +1 -0
  192. skills/_shared/README.md +30 -0
  193. skills/_shared/doc-coverage-shared.md +134 -0
  194. skills/_shared/doc-quality-standard.md +1058 -0
  195. skills/_shared/doc-subagent-rules.md +762 -0
  196. skills/_shared/windows-compat.md +89 -0
  197. skills/kb-audit/SKILL.md +52 -0
  198. skills/kb-audit/rules.md +88 -0
  199. skills/kb-audit/steps/step-01-prepare.md +75 -0
  200. skills/kb-audit/steps/step-02-audit.md +96 -0
  201. skills/kb-audit/steps/step-03-verify.md +65 -0
  202. skills/kb-audit/steps/step-04-report.md +64 -0
  203. skills/kb-init/SKILL.md +146 -0
  204. skills/kb-init/rules.md +187 -0
  205. skills/kb-init/steps/step-01-scope.md +62 -0
  206. skills/kb-init/steps/step-02-source.md +410 -0
  207. skills/kb-init/steps/step-03-generate.md +307 -0
  208. skills/kb-init/steps/step-04-quality.md +92 -0
  209. skills/kb-init/steps/step-05-finalize.md +132 -0
  210. skills/kb-init/templates/core/execution-modes.md +29 -0
  211. skills/kb-init/templates/core/output-only.md +4 -0
  212. skills/kb-init/templates/core/readwrite.md +33 -0
  213. skills/kb-search/SKILL.md +138 -0
  214. skills/kb-search/rules.md +64 -0
  215. skills/kb-sync/SKILL.md +43 -0
  216. skills/kb-sync/rules.md +70 -0
  217. skills/kb-sync/scripts/rebuild_module.py +91 -0
  218. skills/kb-sync/scripts/scan_repos.py +687 -0
  219. skills/kb-sync/steps/step-01-detect.md +72 -0
  220. skills/kb-sync/steps/step-02-update.md +71 -0
  221. skills/kb-sync/steps/step-03-verify.md +47 -0
  222. skills/kb-sync/steps/step-04-finalize.md +52 -0
  223. source_kb-0.2.2.dist-info/METADATA +194 -0
  224. source_kb-0.2.2.dist-info/RECORD +228 -0
  225. source_kb-0.2.2.dist-info/WHEEL +5 -0
  226. source_kb-0.2.2.dist-info/entry_points.txt +3 -0
  227. source_kb-0.2.2.dist-info/licenses/LICENSE +21 -0
  228. source_kb-0.2.2.dist-info/top_level.txt +6 -0
@@ -0,0 +1,307 @@
1
+ # Step 3: 文档生成(子代理编排)
2
+
3
+ ## 3.1 跨模块依赖排序
4
+
5
+ 多模块时从 pom.xml 构建依赖 DAG,拓扑排序生成(被依赖的先生成)。后生成模块可引用先生成模块的中立文档。
6
+
7
+ ## 3.2 批次编排
8
+
9
+ 对每个模块,按 `doc_types.yaml` 的 `batch` 字段分组生成文档:
10
+
11
+ - 同 batch 内的文档可并行生成(除非有 `depends_on` 约束)
12
+ - `conditional: true` 的文档需先判断是否触发(骨架中存在相关文件/注解)
13
+ - 跨 batch 必须串行(后续 batch 依赖前序 batch 的输出)
14
+
15
+ 获取当前模块的具体批次计划:
16
+ ```shell
17
+ source-kb dispatch \
18
+ --config kb-project.yaml --kb {kb} --module {module}
19
+ ```
20
+
21
+ ## 3.3 拆分决策
22
+
23
+ ### 3.3.1 智能拆分优先级链
24
+
25
+ 拆分决策按以下优先级链执行,前序成功则跳过后续:
26
+
27
+ | 优先级 | 策略 | 触发条件 | LLM 成本 |
28
+ |--------|------|---------|---------|
29
+ | 1 | 缓存命中 | 骨架 hash 未变 | 0 |
30
+ | 2 | 社区检测 | 骨架含 imports/fields 数据 | 0 |
31
+ | 3 | Agent 智能分组 | delegated 模式,社区检测未产出有效方案 | 0(Agent 自身能力) |
32
+ | 4 | 代码规则拆分 | Agent 分组未执行或失败 | 0 |
33
+ | 5 | 简单拆分 | 以上全部失败 | 0 |
34
+
35
+ **Agent 智能分组**(优先级 3,Agent 模式专用):
36
+
37
+ 当 `source-kb dispatch` 输出中出现"待 Agent 分组"时:
38
+
39
+ 1. 读取 `.meta/split-requests/{doc-type}-grouping-request.json`
40
+ 2. 根据文件列表和约束条件,按业务域将文件分组
41
+ 3. 输出分组结果为 JSON 文件(格式见 request 中的 `output_format`)
42
+ 4. 执行验证和回填:
43
+ ```shell
44
+ source-kb split-apply \
45
+ --module-dir "{module_dir}" --doc-type {doc_type} \
46
+ --groups "{groups_json_path}"
47
+ ```
48
+ 5. 验证通过 → 分片文件写入 `.meta/shards/`
49
+ 6. **重新执行 `source-kb dispatch`** 刷新 `dispatch-plan.md` 和 `dispatch-tasks.json`(Agent 模式专属,因为首次 preview 时分组尚未完成;CLI 模式内联调用 LLM,无需此步):
50
+ ```shell
51
+ source-kb dispatch \
52
+ --config kb-project.yaml --kb {kb} --module {module}
53
+ ```
54
+ 7. 继续 Gate 3A
55
+ 8. 验证失败 → 根据错误信息调整分组,重新执行 split-apply
56
+ 9. 连续 2 次失败 → fallback 到代码规则拆分(重新执行 `split-files`)
57
+
58
+ **约束(程序化校验,不通过则拒绝)**:
59
+ - 单分片 ≤ 80 文件
60
+ - 单分片行数 ≤ max_lines(Agent 模式 10000)
61
+ - 最大/最小分片比 < 3x
62
+ - 所有文件必须被分配(不能遗漏)
63
+ - 不能有重复分配
64
+
65
+ ### 3.3.2 拆分阈值
66
+
67
+ > **唯一规则源**:`core/skeleton/split.py`
68
+ > 拆分判断优先级:**字节数 > 行数 > 文件数**(文件数仅作上限保护)。
69
+
70
+ **Agent 模式(readwrite):**
71
+
72
+ | 业务代码行数 | 策略 |
73
+ |-------------|------|
74
+ | ≤ 6000 | 单代理 |
75
+ | 6001-12000 | 拆 2 个子代理 |
76
+ | 12001-18000 | 拆 3 个子代理 |
77
+ | > 18000 | 拆 4 个子代理 |
78
+
79
+ **CLI 模式(output-only):**
80
+
81
+ | 业务代码行数 | 策略 |
82
+ |-------------|------|
83
+ | ≤ 4000 | 单代理 |
84
+ | 4001-8000 | 拆 2 个子代理 |
85
+ | 8001-12000 | 拆 3 个子代理 |
86
+ | > 12000 | 拆 4 个子代理 |
87
+
88
+ **硬约束**:业务代码行数超过模式阈值时必须拆分,禁止因并发限制降级为单代理。
89
+
90
+ ### 3.3.3 架构/模型代理拆分
91
+
92
+ | 文档类型 | 拆分条件 | 策略 |
93
+ |---------|---------|------|
94
+ | architecture | 输入 > 20KB | 20-40KB 拆 2 个,> 40KB 拆 3 个 |
95
+ | data-models | 骨架 > 100KB | 100-200KB 拆 2 个,> 200KB 拆 3-4 个 |
96
+
97
+ ## 3.4 构造分派计划
98
+
99
+ ### 3.4.1 执行模式选择
100
+
101
+ | 场景 | 使用模式 | 说明 |
102
+ |------|---------|------|
103
+ | AI Agent 在执行 | `--mode readwrite` | 子代理通过工具读写文件 |
104
+ | 通过 CLI 执行 | `--mode output-only` | CLI 调用 LLM API |
105
+
106
+ **重要:严格按照指定的执行模式执行,不要串台!**
107
+
108
+ ### 3.4.2 Agent 模式构造步骤
109
+
110
+ > **⛔ 反模式警告:以下行为绝对禁止**
111
+ > - 主会话读取源码文件内容(.java/.xml/.yml)
112
+ > - 主会话读取骨架 JSON 的具体条目(只看统计数字)
113
+ > - 主会话自己写 knowledge/ 下的 .md 文档
114
+ > - 以"效率""精简""context限制"为由跳过子代理
115
+ > - 合并多个子代理为一个以"减少调用次数"
116
+ > - 在 Gate 3A 用户确认前执行任何子代理
117
+
118
+ **逐步执行:**
119
+
120
+ 1. **生成分派预览**:
121
+ ```shell
122
+ source-kb dispatch \
123
+ --config kb-project.yaml --kb {kb} --module {module}
124
+ ```
125
+
126
+ 2. **渲染每个子代理的 prompt**(必须 `--mode readwrite`):
127
+ ```shell
128
+ source-kb render \
129
+ --template subagent-{type}.md \
130
+ --module {module} --config kb-project.yaml --kb {kb} \
131
+ --doc-type {doc_type} --mode readwrite \
132
+ --extra high_methods="..." generated_docs="..."
133
+ ```
134
+
135
+ 3. **第三批业务代理额外传入**:
136
+ - `generated_docs`:已生成的第二批文档 `## ` 标题列表
137
+ - `high_methods`:从 step-02 覆盖率预检提取的 high 方法列表
138
+
139
+ 4. **分片场景的文件清单处理**:
140
+ 当 dispatch-preview 显示某个 doc-type 需要拆分(如 business-logic 拆 5 个)时:
141
+ - 工具链会在 `.meta/shards/` 下生成分片文件清单(如 `business-logic-shard-01.txt`)
142
+ - 渲染 prompt 时通过 `--extra file_list_override=".meta/shards/business-logic-shard-01.txt"` 传入
143
+ - 子代理只读取自己分片的文件清单,不读完整 file-list
144
+ - 每个分片子代理输出到独立文件(如 `business-logic-shard-01.md`),最后由 §3.7 合并
145
+
146
+ 4. **检查拆分需求**(按 §3.3 规则)
147
+
148
+ 5. **不立即执行** — 将 task 加入分派计划,等 Gate 3A 确认
149
+
150
+ ### 3.4.3 CLI 模式构造步骤
151
+
152
+ ```shell
153
+ source-kb pipeline init --kb {kb} --module {module}
154
+ ```
155
+
156
+ CLI 自动处理拆分决策、并发控制、超时、重试。需要 `LLM_BASE_URL`/`LLM_API_KEY`/`LLM_MODEL` 环境变量。
157
+
158
+ ## Gate 3A: 分派预览确认
159
+
160
+ 所有模块的子代理 task 构造完毕后,**禁止立即执行**。先向用户展示分派计划。
161
+
162
+ ### 展示格式
163
+
164
+ ```
165
+ 模块: {module_name}({source_file_count} 源码文件,{total_lines} 行)
166
+
167
+ | 批次 | 子代理 | 产出文档 | 输入文件数 | 输入总行数 | 拆分策略 |
168
+ |------|--------|---------|-----------|-----------|---------|
169
+ | 1 | structure | source-tree-analysis.md | 12 | 3200 | 单代理 |
170
+ | 2A | model | data-models.md | 8 | 2100 | 单代理 |
171
+ | 2B | architecture | architecture.md | 15 | 1200 | 单代理 |
172
+ | 3 | business-order | business-logic.md (shard 1) | 14 | 4100 | 社区检测 拆2 |
173
+ | 3 | business-payment | business-logic.md (shard 2) | 12 | 4025 | |
174
+ | 4 | index | index.md | — | — | 单代理 |
175
+ ```
176
+
177
+ ### 用户审查关注点
178
+
179
+ - 输入总字节 > 500KB(Agent)或 > 300KB(CLI)的分片是否已拆分
180
+ - 同一文档各分片字节数是否均衡(最大/最小比 < 3x)
181
+ - 业务代理行数 > 阈值是否已拆分
182
+ - 拆分策略是否合理(社区检测 > LLM > 代码规则)
183
+ - 单代理输入文件数是否 ≤ 80
184
+
185
+ ### 用户操作
186
+
187
+ - `继续` → 执行所有子代理任务,进入 §3.5
188
+ - 指出具体问题 → 调整分派计划后重新展示
189
+ - `跳过 {module}` → 标记 SKIPPED
190
+
191
+ ## 3.5 执行生成
192
+
193
+ ### Agent 模式
194
+
195
+ 按批次顺序执行子代理任务:
196
+
197
+ 1. 逐批创建子代理,每个子代理接收渲染后的 prompt
198
+ 2. 子代理作为文档生成第一责任人:读取源码 → 分析 → 生成文档 → 写入文件
199
+ 3. 当前批次所有子代理完成后,进入下一批次
200
+ 4. **主会话不直接生成文档**,只负责调度和进度监控
201
+
202
+ **禁止在 Agent 模式下调用 `source-kb pipeline --action init`**(CLI 模式用于无 Agent 环境)。
203
+
204
+ ### CLI 模式
205
+
206
+ ```shell
207
+ source-kb pipeline init --kb {kb} --module {module}
208
+ ```
209
+
210
+ ### 进度监控
211
+
212
+ 每批次子代理启动后执行:
213
+
214
+ ```shell
215
+ source-kb validate consistency --module-dir "{knowledge_dir}"
216
+ ```
217
+
218
+ **卡死检测**:心跳超过 30 秒无更新 → 判定卡死 → 按 §3.6 失败恢复处理。
219
+
220
+ ## 3.6 失败恢复
221
+
222
+ 每批子代理返回后:
223
+
224
+ 1. 扫描 `.meta/progress/` 目录,判定各文档状态
225
+ 2. **空转检测**:子代理返回但文档不存在或 < 1KB → 空转
226
+ 3. 空转处理:
227
+ - 分析原因(输入过大?模型不足?偶发?)
228
+ - 按拆分规则重新拆分 → 再次执行
229
+ - 连续 2 次空转 → 标记 `FAILED`,继续后续文档
230
+ 4. **禁止主会话降级补写**(R6)
231
+
232
+ | 状态 | 判定条件 | 处理 |
233
+ |------|---------|------|
234
+ | ✅ 完成 | progress 含 DONE + 文档 > 1KB | 跳过 |
235
+ | ⚠️ 空转 | 子代理返回但文档不存在或 < 1KB | 拆分重试 |
236
+ | ⚠️ 中断 | progress 含 START 无 DONE | 拆分重试 |
237
+ | ❌ 报错 | progress 含 ERROR | 分析原因后重试 |
238
+
239
+ ## 3.7 分片合并
240
+
241
+ 所有子代理完成后,合并分片文档:
242
+
243
+ ```shell
244
+ # 自动合并所有分片(CLI 模式由 pipeline 自动执行)
245
+ source-kb merge --dir "{module_dir}"
246
+
247
+ # 指定前缀和顺序(如 architecture)
248
+ source-kb merge --prefix architecture --order stack,infra --dir "{module_dir}"
249
+ ```
250
+
251
+ 合并成功后,**必须删除分片源文件**(避免索引污染和目录混乱):
252
+
253
+ ```shell
254
+ # 删除已合并的分片 .md 文件(保留 .meta/shards/ 下的文件清单作为审计记录)
255
+ rm -f "{module_dir}"/*-shard-*.md
256
+ ```
257
+
258
+ 验证:合并后的主文档存在且 > 1KB,分片文件已删除。
259
+
260
+ ## 3.8 合并后精炼
261
+
262
+ 所有文档生成完毕后(无论是否有分片),执行规则化精炼(去重 + 术语一致性 + 锚点补充):
263
+
264
+ ```shell
265
+ # Agent 模式:对所有生成的文档执行精炼
266
+ source-kb post-merge --module-dir "{module_dir}"
267
+ ```
268
+
269
+ 精炼内容:
270
+ - **去重**:移除合并时产生的重复段落(>80 字符的语义重复)
271
+ - **术语一致性**:将别名替换为标准术语(基于骨架 JavaDoc 提取的术语表)
272
+ - **锚点补充**:为二级标题添加 HTML 锚点,支持交叉引用跳转
273
+
274
+ > ⚠️ 即使所有文档都是单代理生成(无分片),精炼仍然有价值——锚点补充和术语一致性检查对单代理文档同样适用。
275
+
276
+ ## 3.9 质量反馈记录(可选)
277
+
278
+ 生成完成后记录拆分质量分数,供下次优化拆分参数:
279
+
280
+ ```shell
281
+ python -c "
282
+ from core.skeleton.split_feedback import record_split_result
283
+ from pathlib import Path
284
+ record_split_result(
285
+ module_dir=Path('{module_dir}'),
286
+ doc_type='business-logic',
287
+ strategy='community', # 或 'llm' / 'package-fallback'
288
+ resolution=1.0,
289
+ n_splits=3,
290
+ coverage_score=0.85, # 从 step-04 coverage_check 获取
291
+ quality_score=0.90, # 从 step-04 validate 获取
292
+ )
293
+ "
294
+ ```
295
+
296
+ ## Gate 3B 检查清单
297
+
298
+ 执行以下检查,全部通过后进入 step-04:
299
+
300
+ - [ ] 所有预期文档状态为 DONE 或 FAILED
301
+ - [ ] FAILED 文档已记录原因(将在最终报告中汇报用户)
302
+ - [ ] 分片文档已合并(§3.7,仅拆分场景)
303
+ - [ ] 合并后精炼已执行(§3.8,**所有文档都需要**,包括单代理生成的)
304
+ - [ ] 进度文件状态一致(无 START 无 DONE 的残留)
305
+ - [ ] 每个生成的 .md 文件 > 1KB
306
+
307
+ 全部通过 → 加载 `step-04-quality.md`
@@ -0,0 +1,92 @@
1
+ # Step 4: 质量校验
2
+
3
+ 所有文档生成完毕后,依次执行以下校验。任一校验不通过 → 修复后重新校验,不跳过(R5)。
4
+
5
+ ## 4.1 覆盖率校验
6
+
7
+ ```shell
8
+ # 单文件骨架
9
+ source-kb validate coverage check \
10
+ --skeleton {module_dir}/.skeleton.json --docs-dir {module_dir} --type {module_type}
11
+
12
+ # 分片目录骨架
13
+ source-kb validate coverage check \
14
+ --skeleton-dir {module_dir}/.skeleton/ --docs-dir {module_dir} --type {module_type}
15
+ ```
16
+
17
+ | 覆盖率 | 处理 |
18
+ |--------|------|
19
+ | ≥ 80% | ✅ 通过 |
20
+ | 60%-80% | 自动补充(最多 2 轮),仍不达标 → ⚠️ 询问用户 |
21
+ | < 60% | ❌ 询问用户 |
22
+
23
+ 注意:`coverage_check.py` 扫描 `--docs-dir` 下所有 .md 文件,`architecture.md` 中的配置类方法、`data-models.md` 中的枚举方法都计入覆盖率。
24
+
25
+ ## 4.1b 数值抽样校验
26
+
27
+ 覆盖率校验通过后,执行骨架感知的数值抽样:
28
+
29
+ ```shell
30
+ # 数值抽样校验(枚举值 + 字段名随机抽样验证)
31
+ source-kb validate sampling --module-dir "{module_dir}"
32
+ ```
33
+
34
+ 校验内容:
35
+ - 从骨架随机抽取 3-5 个枚举值,检查是否出现在 `enums-and-constants.md`
36
+ - 从骨架随机抽取 3-5 个实体字段名,检查是否出现在 `data-models.md`
37
+ - `api-contracts.md` 的 H2 章节数与骨架 Controller 数量比对(偏差 > 50% 报警)
38
+
39
+ | 抽样命中率 | 处理 |
40
+ |-----------|------|
41
+ | 100% | 通过 |
42
+ | 60%-100% | ⚠️ 警告,人工确认是否为文档遗漏 |
43
+ | < 60% | 定位遗漏章节,补充后重新校验 |
44
+
45
+ ## 4.2 引用完整性校验
46
+
47
+ 1. 提取 business-logic.md 中所有 `](./` 和 `](../` 格式的 Markdown 链接
48
+ 2. 验证目标文件存在
49
+ 3. 含 `#锚点` 的链接,验证目标文档中存在对应 `##`/`###` 标题
50
+ 4. 死链 > 0 → 修复锚点或降级为文件级链接,修复后重新校验
51
+
52
+ ## 4.3 重复内容检测
53
+
54
+ 当 business-logic.md > 50KB 时执行:
55
+
56
+ | 检测模式 | 阈值 | 疑似重复 |
57
+ |---------|------|---------|
58
+ | 缓存 Key 常量模式 | 出现 > 3 次 | 与 caching.md 重复 |
59
+ | JSON 示例代码块 | > 5 个 | 与 messaging.md 重复 |
60
+ | 字段表格头 `\| 字段 \| 类型 \|` | > 3 个 | 与 data-models.md 重复 |
61
+ | 中间件配置参数 | 出现 > 3 次 | 与 architecture.md 重复 |
62
+
63
+ 命中 → 定位重复段落,替换为引用链接。
64
+
65
+ ## 4.4 文档产出质量校验
66
+
67
+ ```shell
68
+ # 校验单个模块
69
+ source-kb validate structure \
70
+ --module-dir "{module_dir}"
71
+
72
+ # 校验整个知识库
73
+ source-kb validate structure \
74
+ --kb-dir "{knowledge_dir}"
75
+ ```
76
+
77
+ 校验维度:文件完整性、体量阈值、必需章节、跨文档引用、markdown 结构。
78
+
79
+ | 评分 | 处理 |
80
+ |------|------|
81
+ | A/B 级 | ✅ 通过 |
82
+ | C 级(1-2 错误) | ⚠️ 修复后重新校验 |
83
+ | D 级(≥ 3 错误) | ❌ 必须修复后才能进入 Step 5 |
84
+
85
+ ## Gate 4 检查清单
86
+
87
+ - [ ] 方法覆盖率 ≥ 80%(或用户已确认接受当前覆盖率)
88
+ - [ ] 引用完整性校验通过(无死链)
89
+ - [ ] 重复内容检测通过(或已替换为引用链接)
90
+ - [ ] 产出质量校验 A/B 级
91
+
92
+ 全部通过 → 加载 `step-05-finalize.md`
@@ -0,0 +1,132 @@
1
+ # Step 5: 收尾
2
+
3
+ ## 5.1 清理进度文件
4
+
5
+ 覆盖率校验通过后,先终止残留心跳进程,再删除进度标记文件:
6
+
7
+ ```shell
8
+ # 1. 终止残留心跳进程 + 标记过期进度(主会话统一执行,不依赖子代理自行清理)
9
+ source-kb validate consistency \
10
+ --module-dir "{module_dir}" --preset "{preset}" --cleanup
11
+
12
+ # 2. 删除已完成的进度文件和 PID 文件
13
+ source-kb validate consistency --module-dir "{module_dir}" --cleanup
14
+ ```
15
+
16
+ 保留 `.meta/progress/business-logic-{domain}` 作为业务代理分片的审计记录。
17
+
18
+ ## 5.1b 过期文件检测 [必须]
19
+
20
+ > ⚠️ **此步骤不可跳过。** 重新生成时,旧文件可能因 skip 规则变更或模块类型调整而不再适用,残留会导致索引污染和文档导航混乱。
21
+
22
+ 扫描 `{module_dir}` 下已有的 `.md` 文件,对比本次 dispatch 计划中预期生成的文档列表,找出"存在但不在计划中"的过期文件。
23
+
24
+ ```shell
25
+ source-kb stale-files \
26
+ --config kb-project.yaml --kb {kb} --module {module}
27
+ ```
28
+
29
+ ### 判定规则
30
+
31
+ | 文件状态 | 判定 | 处理 |
32
+ |---------|------|------|
33
+ | 在 dispatch 计划中且已生成 | 正常 | 保留 |
34
+ | 在 dispatch 计划中但未生成(FAILED) | 失败 | 已在 Gate 3B 记录 |
35
+ | **不在 dispatch 计划中但文件存在** | 过期/遗留 | → 提示用户 |
36
+ | `.meta/` 下的文件 | 元数据 | 始终保留 |
37
+ | `index.md` | 索引 | 始终保留(即使不在计划中) |
38
+
39
+ ### 用户交互
40
+
41
+ 当检测到过期文件时,向用户展示:
42
+
43
+ ```
44
+ ⚠️ 检测到以下文件不在本次生成计划中(可能是历史遗留或 skip 规则变更导致):
45
+
46
+ | 文件 | 大小 | 最后修改 | 可能原因 |
47
+ |------|------|---------|---------|
48
+ | api-contracts.md | 7.2KB | 2025-04-10 | base-lib 模块已跳过此文档类型 |
49
+ | messaging.md | 3.1KB | 2025-03-20 | 本次文件分类未触发消息队列文档 |
50
+
51
+ 操作选项:
52
+ 1. 全部删除(推荐:保持目录整洁,避免索引污染)
53
+ 2. 全部保留(文件不会被索引,但保留在目录中)
54
+ 3. 逐个确认
55
+ ```
56
+
57
+ ### 自动处理(无需用户确认)
58
+
59
+ 以下情况自动跳过,不提示用户:
60
+ - 文件在 `.meta/` 目录下
61
+ - 文件名以 `.` 开头(隐藏文件)
62
+ - 文件为 `README.md`
63
+
64
+ ## 5.2 共享文档生成
65
+
66
+ 所有模块文档生成完毕后,在 `{knowledge_dir}/_shared/` 下生成跨模块汇总文档:
67
+
68
+ | 文件 | 触发条件 |
69
+ |------|---------|
70
+ | project-overview.md | 始终生成 |
71
+ | cross-module-calls.md | 模块数 ≥ 2 |
72
+ | error-codes.md | 任一模块有错误码定义 |
73
+ | database-ddl.md | 任一模块有数据模型 |
74
+
75
+ **生成方式**:主会话串行生成,不委派子代理。原因:
76
+ 1. 主会话已持有所有模块文档的上下文
77
+ 2. 后台子代理写盘权限不稳定
78
+ 3. 共享文档数量少,串行效率足够
79
+
80
+ ## 5.3 失败回滚
81
+
82
+ 开始前如果 knowledge 目录已有 git 仓库且工作区 clean → `git commit` 当前状态作为快照。工作区有未提交变更 → 先 `git stash`。
83
+
84
+ - git clone 失败 → 跳过该模块
85
+ - 文档生成崩溃 → 已写盘的保留
86
+ - 失败后可 `git reset --hard` 或 `git stash pop` 回滚
87
+
88
+ ## 5.4 释放知识库锁
89
+
90
+ **无论成功还是失败,必须执行**(R10):
91
+
92
+ ```shell
93
+ rm -f "{knowledge_dir}/.kb-lock"
94
+ ```
95
+
96
+ ## 5.5 构建索引
97
+
98
+ ### Embedding 服务预检
99
+
100
+ ```shell
101
+ source-kb validate links --config kb-project.yaml --module-dir "{module_dir}"
102
+ ```
103
+
104
+ | 退出码 | 处理 |
105
+ |--------|------|
106
+ | 0 | ✅ 继续构建索引 |
107
+ | 1(模型未找到) | ⚠️ 提示 `ollama pull {model}` |
108
+ | 2(服务不可达) | ⚠️ 提示检查 `embedding.base_url` 和 Ollama 服务状态 |
109
+
110
+ 预检失败不阻塞文档生成,仅阻塞索引构建。
111
+
112
+ ### 构建索引
113
+
114
+ ```shell
115
+ # 构建/重建索引
116
+ source-kb index --kb {kb}
117
+ ```
118
+
119
+ ## 5.6 验证检索
120
+
121
+ ```shell
122
+ source-kb search --kb {kb} "查询词"
123
+ ```
124
+
125
+ ## 5.7 最终报告
126
+
127
+ 向用户汇报:
128
+ - 成功生成的模块和文档列表
129
+ - FAILED 的文档及原因
130
+ - 覆盖率数据
131
+ - 索引构建状态
132
+ - 需要用户手动处理的遗留项
@@ -0,0 +1,29 @@
1
+ # 执行模式指导
2
+
3
+ 本文件定义两种执行模式的声明式指导,由 `render_prompt.py --mode` 注入到子代理 prompt 中。
4
+ 模板本身只包含平台无关的内容规则,执行模式指导在渲染时按需注入。
5
+
6
+ ## 模式判定
7
+
8
+ | 条件 | 执行模式 | 说明 |
9
+ |------|---------|------|
10
+ | 通过 CLI pipeline 执行 | `output-only` | CLI 捕获输出并写入文件 |
11
+ | 通过 Agent 手动引导执行 | `readwrite` | Agent 通过工具读写文件 |
12
+
13
+ 使用 `render_prompt.py --mode` 显式指定,或由调用方自行判断。
14
+
15
+ ## 两种模式的区别
16
+
17
+ ### output-only(输出即文档)
18
+
19
+ 子代理只需要输出 markdown 文本,不需要任何文件操作。
20
+ 所有源码信息已内嵌在 prompt 中(骨架 + 源码内容)。
21
+
22
+ ### readwrite(读写文件)
23
+
24
+ 子代理需要通过工具读取源码、分块写入文档。
25
+ 写盘纪律:
26
+ - 每完成一个 `##` 章节就写盘一次
27
+ - 首次创建文件,后续追加
28
+ - 单次写入不超过 150 行
29
+ - 每次写盘后读取前 5 行确认写入成功
@@ -0,0 +1,4 @@
1
+ ## 执行方式
2
+
3
+ 直接输出完整的 markdown 文档内容,不要输出解释、确认或其他非文档内容。
4
+ 所有需要的源码信息已在下方「源码骨架」和「源码文件内容」章节中提供,禁止引用未提供的文件。
@@ -0,0 +1,33 @@
1
+ ## 执行方式
2
+
3
+ 你需要根据文件清单读取源码文件,分析内容并生成文档。
4
+
5
+ ### 文件读取
6
+
7
+ 源码缓存目录:`{source_cache_path}`
8
+ 文件清单:在「需要读取的源码文件」章节中提供
9
+
10
+ 读取规则:
11
+ 1. 根据文件清单逐个读取源码文件
12
+ 2. 分析文件内容,提取所需信息
13
+ 3. 禁止读取未在清单中的文件
14
+ 4. 直接分析源码,所有数值参数必须从源码确认
15
+
16
+ ### 关键原则
17
+
18
+ 1. **禁止编造**:所有内容必须有源码出处
19
+ 2. **直接分析**:基于原始源码文件内容
20
+ 3. **聚焦任务**:只生成当前文档类型的内容
21
+ 4. **验证数值**:配置参数必须从源码验证
22
+
23
+ ### 写盘纪律
24
+
25
+ - 每完成一个 `##` 章节就写盘一次
26
+ - 首次创建文件使用 Write,后续追加使用 Edit
27
+ - 单次写入不超过 150 行
28
+ - 每次写盘后读取前 5 行确认写入成功
29
+
30
+ ### 输出文件
31
+
32
+ `{output_path}/{doc_type}.md`(如果分片则覆盖为具体文件名)
33
+