kenaz 0.3.17 → 0.3.19

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 (316) hide show
  1. package/core/.claude/commands/agent-worker.md +1 -1
  2. package/core/.claude/commands/architect.md +1 -2
  3. package/core/.claude/commands/spec.md +1 -1
  4. package/core/.claude/commands//347/224/242/345/223/201.md +1 -1
  5. package/core/.claude/hooks/__tests__/task-1977-dedupe-guard.test.js +8 -8
  6. package/core/.claude/hooks/synapse-first-investigation.js +15 -0
  7. package/core/.claude/skills/architect_agent/SKILL.md +11 -92
  8. package/core/.claude/skills/assessment_agent/SKILL.md +0 -1
  9. package/core/.claude/skills/backend_engineer/SKILL.md +3 -6
  10. package/core/.claude/skills/code_reviewer/SKILL.md +1 -1
  11. package/core/.claude/skills/code_reviewer/patterns.yaml +1 -1
  12. package/core/.claude/skills/designer/SKILL.md +3 -6
  13. package/core/.claude/skills/dev_agent/SKILL.md +30 -491
  14. package/core/.claude/skills/developer/SKILL.md +3 -6
  15. package/core/.claude/skills/feature_agent/SKILL.md +3 -17
  16. package/core/.claude/skills/product_manager/SKILL.md +29 -1064
  17. package/core/.claude/skills/qa_engineer/SKILL.md +12 -17
  18. package/core/.claude/skills/spec_optimizer/SKILL.md +25 -275
  19. package/core/.claude/skills/sre_agent/SKILL.md +3 -6
  20. package/core/.collaboration/README.md +2 -3
  21. package/core/.collaboration/core/task-api.js +5 -138
  22. package/core/.collaboration/docs/CLI.md +0 -2
  23. package/core/.failure/INDEX.yaml +6 -40
  24. package/core/.failure/agent-execution/FAIL-013-search-cwd-drift-and-case-mismatch.yaml +1 -1
  25. package/core/.failure/tools/README.md +9 -227
  26. package/core/.formation/bifrost/exec.js +29 -4
  27. package/core/.formation/bifrost/launch.js +1 -6
  28. package/core/.formation/bifrost/launch.test.js +1 -1
  29. package/core/.formation/bifrost/provider-launch.js +18 -11
  30. package/core/.formation/bifrost/provider-launch.test.js +26 -2
  31. package/core/.formation/bifrost/shared/ansuz-boot-policy.js +3 -3
  32. package/core/.formation/bifrost/shared/ansuz-boot-policy.test.js +1 -2
  33. package/core/.formation/tools/__tests__/dispatch-cwd-project.test.js +42 -0
  34. package/core/.formation/tools/__tests__/m1-hmap-convid.test.js +1 -124
  35. package/core/.formation/tools/__tests__/subagent-executor.test.js +3 -2
  36. package/core/.formation/tools/dispatch.js +15 -2
  37. package/core/.formation/tools/loader.js +17 -140
  38. package/core/.formation/tools/post-complete.js +0 -31
  39. package/core/.rule/INDEX.yaml +0 -18
  40. package/core/.rule/general/agent-workflow.yaml +14 -164
  41. package/core/.rule/general/project-self-containment.yaml +8 -19
  42. package/core/.rule/general/rust-verify-target.yaml +6 -5
  43. package/core/.rule/general/synapse-code-intelligence.yaml +3 -3
  44. package/core/.specialist/Amelia/capability.yaml +12 -12
  45. package/core/.specialist/Amelia/workflow.yaml +2 -2
  46. package/core/.specialist/Ansuz/SYSTEM_DOC.md +20 -578
  47. package/core/.specialist/Ansuz/cognitive_principles.yaml +2 -3
  48. package/core/.specialist/Ansuz/concepts/boot_rules_summary.yaml +0 -1
  49. package/core/.specialist/Ansuz/concepts/deep_knowledge_pointers.yaml +1 -2
  50. package/core/.specialist/Ansuz/core.yaml +6 -108
  51. package/core/.specialist/Ansuz/guards/lesson_guards.yaml +0 -3
  52. package/core/.specialist/Ansuz/identity_contract.yaml +0 -1
  53. package/core/.specialist/Ansuz/knowledge.yaml +0 -4
  54. package/core/.specialist/Ansuz/rule.yaml +0 -1
  55. package/core/.specialist/Ansuz/tools/handoff-session-identity.test.js +6 -150
  56. package/core/.specialist/tests/runtime-discovery/README.md +1 -1
  57. package/core/.specialist/tools/__tests__/task-2374-usage-counter.test.js +1 -12
  58. package/core/.specialist/tools/prompt-files.test.js +1 -0
  59. package/core/.specialist/tools/task-bridge.js +1 -66
  60. package/core/.system/README.md +11 -552
  61. package/core/.system/collaboration/README.md +10 -174
  62. package/core/CLAUDE.md +1 -6
  63. package/core/KENAZ_CORE_VERSION +1 -1
  64. package/core/dev/scripts/js-yaml-resolution.test.js +1 -1
  65. package/core/dev/scripts/run-rust-tests.js +30 -46
  66. package/core/dev/scripts/run-rust-tests.test.js +17 -0
  67. package/core/dev/scripts/rust-verify-worktree.js +4 -4
  68. package/core/dev/scripts/verify-target-dir.js +6 -6
  69. package/core/plugins/kenaz/INVENTORY.md +8 -8
  70. package/core/plugins/kenaz/commands/architect.md +1 -2
  71. package/core/plugins/kenaz/commands/spec.md +1 -1
  72. package/core/plugins/kenaz/commands//347/224/242/345/223/201.md +1 -1
  73. package/core/plugins/kenaz/guidance.md +1 -1
  74. package/core/plugins/kenaz/scripts/sync.js +1 -1
  75. package/core/plugins/kenaz/scripts/ygg.js +6 -0
  76. package/core/plugins/kenaz/skills/architect_agent/SKILL.md +11 -92
  77. package/core/plugins/kenaz/skills/assessment_agent/SKILL.md +0 -1
  78. package/core/plugins/kenaz/skills/backend_engineer/SKILL.md +3 -6
  79. package/core/plugins/kenaz/skills/code_reviewer/SKILL.md +1 -1
  80. package/core/plugins/kenaz/skills/code_reviewer/patterns.yaml +1 -1
  81. package/core/plugins/kenaz/skills/designer/SKILL.md +3 -6
  82. package/core/plugins/kenaz/skills/dev_agent/SKILL.md +30 -491
  83. package/core/plugins/kenaz/skills/developer/SKILL.md +3 -6
  84. package/core/plugins/kenaz/skills/feature_agent/SKILL.md +3 -17
  85. package/core/plugins/kenaz/skills/product_manager/SKILL.md +29 -1064
  86. package/core/plugins/kenaz/skills/qa_engineer/SKILL.md +12 -17
  87. package/core/plugins/kenaz/skills/spec_optimizer/SKILL.md +25 -275
  88. package/core/plugins/kenaz/skills/sre_agent/SKILL.md +3 -6
  89. package/package.json +6 -6
  90. package/core/.claude/commands/archive.md +0 -24
  91. package/core/.claude/commands/init.md +0 -112
  92. package/core/.claude/commands/legacy_dev.md +0 -18
  93. package/core/.claude/commands/product-review.md +0 -131
  94. package/core/.claude/commands/strategy.md +0 -141
  95. package/core/.claude/commands/sunny.md +0 -1
  96. package/core/.claude/commands//347/224/242/345/223/201/346/252/242/350/246/226.md +0 -129
  97. package/core/.claude/commands//351/200/262/345/272/246.md +0 -15
  98. package/core/.claude/hooks/__tests__/substrate.test.js +0 -122
  99. package/core/.claude/hooks/_substrate-paths.js +0 -95
  100. package/core/.claude/hooks/context-check.js +0 -51
  101. package/core/.claude/hooks/fatigue-check.js +0 -52
  102. package/core/.claude/hooks/handoff-snapshot-on-commit.js +0 -154
  103. package/core/.claude/hooks/handoff-snapshot-on-question.js +0 -158
  104. package/core/.claude/hooks/handoff-staleness.js +0 -197
  105. package/core/.claude/hooks/jev-thalamus-context.js +0 -17
  106. package/core/.claude/hooks/jev-thalamus-context.test.js +0 -41
  107. package/core/.claude/hooks/lease-release.js +0 -119
  108. package/core/.claude/hooks/lease-touch.js +0 -96
  109. package/core/.claude/hooks/reflex-feedback.js +0 -5
  110. package/core/.claude/hooks/session-init.ps1 +0 -49
  111. package/core/.claude/hooks/sound-event.js +0 -70
  112. package/core/.claude/hooks/sound-event.sh +0 -37
  113. package/core/.claude/hooks/stream-delta.js +0 -169
  114. package/core/.claude/no-hooks.settings.json +0 -3
  115. package/core/.claude/skills/legacy_dev/SKILL.md +0 -202
  116. package/core/.claude/skills/sunny/SKILL.md +0 -74
  117. package/core/.collaboration/cli/commands/assign.js +0 -39
  118. package/core/.collaboration/cli/commands/claim.js +0 -33
  119. package/core/.collaboration/cli/commands/complete.js +0 -44
  120. package/core/.collaboration/cli/commands/create.js +0 -110
  121. package/core/.collaboration/cli/commands/get.js +0 -83
  122. package/core/.collaboration/cli/commands/index.js +0 -52
  123. package/core/.collaboration/cli/commands/list.js +0 -51
  124. package/core/.collaboration/cli/commands/lock.js +0 -45
  125. package/core/.collaboration/cli/commands/rebuild.js +0 -39
  126. package/core/.collaboration/cli/commands/search.js +0 -61
  127. package/core/.collaboration/cli/commands/stats.js +0 -61
  128. package/core/.collaboration/cli/commands/transfer.js +0 -41
  129. package/core/.collaboration/cli/commands/unlock.js +0 -48
  130. package/core/.collaboration/cli/formatters/help-formatter.js +0 -70
  131. package/core/.collaboration/cli/formatters/index.js +0 -7
  132. package/core/.collaboration/cli/task-cli.js +0 -46
  133. package/core/.collaboration/cli/utils/arg-parser.js +0 -74
  134. package/core/.collaboration/cli/utils/git-user.js +0 -25
  135. package/core/.collaboration/cli/utils/index.js +0 -9
  136. package/core/.collaboration/cli/utils/output.js +0 -10
  137. package/core/.collaboration/config/default.yaml +0 -189
  138. package/core/.collaboration/core/conflict-resolver.js +0 -461
  139. package/core/.collaboration/core/conflict-resolver.test.js +0 -389
  140. package/core/.collaboration/core/migrate-legacy.js +0 -260
  141. package/core/.collaboration/docs/API.md +0 -787
  142. package/core/.collaboration/docs/ARCHITECTURE.md +0 -596
  143. package/core/.collaboration/docs/FAQ.md +0 -695
  144. package/core/.collaboration/docs/MIGRATION.md +0 -804
  145. package/core/.collaboration/docs/examples/README.md +0 -320
  146. package/core/.collaboration/docs/examples/advanced-queries.js +0 -261
  147. package/core/.collaboration/docs/examples/basic-usage.js +0 -184
  148. package/core/.collaboration/docs/examples/team-workflow.js +0 -352
  149. package/core/.failure/agent-execution/FAIL-001-agent-no-output.yaml +0 -391
  150. package/core/.failure/agent-execution/FAIL-007-pm-lead-wrong-team-invocation.yaml +0 -0
  151. package/core/.failure/agent-execution/FAIL-010-phase0-poem-format-skipped.yaml +0 -28
  152. package/core/.failure/agent-execution/FAIL-010-skipping-cognitive-checkpoint-after-verification.yaml +0 -59
  153. package/core/.failure/agent-execution/FAIL-012-grep-shortcut-during-investigation.yaml +0 -85
  154. package/core/.failure/architecture/FAIL-006-python-version-compatibility.yaml +0 -445
  155. package/core/.failure/pipeline/FAIL-003-queue-pollution.yaml +0 -335
  156. package/core/.failure/pipeline/README.md +0 -95
  157. package/core/.failure/retired/FAIL-009-skipping-synapse-pulse-before-code-change.yaml +0 -49
  158. package/core/.failure/tools/USAGE_GUIDE.md +0 -323
  159. package/core/.failure/tools/check_failures.ps1 +0 -387
  160. package/core/.failure/tools/log_failure.ps1 +0 -474
  161. package/core/.formation/.episode-input.json +0 -10
  162. package/core/.formation/.merge-lock +0 -1
  163. package/core/.formation/lead.yaml +0 -118
  164. package/core/.formation/legacy.bak/designer.yaml +0 -57
  165. package/core/.formation/legacy.bak/pm.yaml +0 -57
  166. package/core/.formation/legacy.bak/qa.yaml +0 -57
  167. package/core/.formation/legacy.bak/review.yaml +0 -62
  168. package/core/.formation/legacy.bak/scheduler.yaml +0 -129
  169. package/core/.formation/legacy.bak/sre.yaml +0 -57
  170. package/core/.formation/role-map.yaml +0 -274
  171. package/core/.formation/roles/_shared/modules/code_review.yaml +0 -98
  172. package/core/.formation/roles/_shared/modules/security.yaml +0 -103
  173. package/core/.formation/roles/_shared/modules/testing.yaml +0 -14
  174. package/core/.formation/roles/backend/role.yaml +0 -93
  175. package/core/.formation/roles/code_reviewer/role.yaml +0 -102
  176. package/core/.formation/roles/database_optimizer/role.yaml +0 -107
  177. package/core/.formation/roles/designer/role.yaml +0 -66
  178. package/core/.formation/roles/developer/role.yaml +0 -75
  179. package/core/.formation/roles/frontend/role.yaml +0 -134
  180. package/core/.formation/roles/incident_response/role.yaml +0 -89
  181. package/core/.formation/roles/qa/role.yaml +0 -89
  182. package/core/.formation/roles/security/role.yaml +0 -87
  183. package/core/.formation/roles/sre/role.yaml +0 -66
  184. package/core/.formation/skills/coordinator.yaml +0 -52
  185. package/core/.formation/solo/diagnosis.yaml +0 -78
  186. package/core/.formation/solo/strategy.yaml +0 -121
  187. package/core/.formation/tests/hello.txt +0 -2
  188. package/core/.formation/tools/coordinator.js +0 -350
  189. package/core/.formation/tools/jev-correction.js +0 -214
  190. package/core/.formation/tools/jev-correction.test.js +0 -178
  191. package/core/.formation/tools/log-reader.js +0 -232
  192. package/core/.formation/tools/revert-daemon-state.js +0 -162
  193. package/core/.formation/tools/test-drift.js +0 -148
  194. package/core/.formation/unit/heimdall.yaml +0 -96
  195. package/core/.formation/unit/plan.yaml +0 -67
  196. package/core/.formation/unit/think.yaml +0 -58
  197. package/core/.kenaz/MUNINN_SNAPSHOT9_EPISODE.json +0 -32
  198. package/core/.kenaz/MUNINN_SNAPSHOT9_RUNE.json +0 -23
  199. package/core/.kenaz/MUNINN_SNAPSHOT9_SUMMARY.md +0 -89
  200. package/core/.kenaz/ash_TASK_1428-20260507.json +0 -14
  201. package/core/.kenaz/muninn_episode_write.ps1 +0 -17
  202. package/core/.kenaz/muninn_snapshot11_rune_candidates.md +0 -63
  203. package/core/.kenaz/muninn_snapshot1_rune_candidates.md +0 -49
  204. package/core/.kenaz/runes_TASK_1428.json +0 -57
  205. package/core/.kenaz/tmp_heimdall_proposal_8d3e392.json +0 -13
  206. package/core/.kenaz/tmp_props.json +0 -1
  207. package/core/.kenaz/tmp_sounddrop_t1_prompt.txt +0 -371
  208. package/core/.rule/MECHANIZED.md +0 -61
  209. package/core/.rule/general/cortex-memory-recall.yaml +0 -446
  210. package/core/.rule/general/pipeline.yaml +0 -246
  211. package/core/.rule/general/sunny-access-control.yaml +0 -60
  212. package/core/.rule/general/system-invariants.yaml +0 -231
  213. package/core/.rule/pipeline/PPL-002-completed-task-archival.yaml +0 -528
  214. package/core/.rule/pipeline/auto-update.yaml +0 -282
  215. package/core/.rule/pipeline/task-execution.yaml +0 -365
  216. package/core/.specialist/Ansuz/concepts/phase_protocol.yaml +0 -65
  217. package/core/.specialist/Ansuz/experiments/boot_ablation_replay.js +0 -197
  218. package/core/.specialist/Ansuz/tools/bootstrap-anchors.js +0 -289
  219. package/core/.specialist/Ansuz/workflow.yaml +0 -1225
  220. package/core/.specialist/experiments/rg_benchmark.js +0 -523
  221. package/core/.specialist/experiments/rg_benchmark_results.json +0 -27
  222. package/core/.specialist/tools/jev-optional-context.js +0 -107
  223. package/core/.specialist/tools/jev-optional-context.test.js +0 -152
  224. package/core/.specialist/tools/memory-action-log.js +0 -839
  225. package/core/.specialist/tools/phase-gate.js +0 -3534
  226. package/core/.specialist/tools/phase-gate.test.js +0 -85
  227. package/core/.specialist/tools/precompute-runner.js +0 -444
  228. package/core/.specialist/tools/rule-engine.js +0 -621
  229. package/core/.specialist/tools/task-bridge-jev.test.js +0 -170
  230. package/core/.synapse/memory-shared.json.sync +0 -1
  231. package/core/.system/agent-skills/README.md +0 -724
  232. package/core/.system/cli-tools/README.md +0 -724
  233. package/core/.system/cortex/MODULES.md +0 -1046
  234. package/core/.system/dashboard/FRONTEND.md +0 -812
  235. package/core/.system/dashboard/README.md +0 -737
  236. package/core/.system/failure/README.md +0 -723
  237. package/core/.system/formation/LEGACY_SKILLS.md +0 -974
  238. package/core/.system/formation/POST_COMPLETE.md +0 -602
  239. package/core/.system/formation/README.md +0 -1094
  240. package/core/.system/formation/TOOLS_REFERENCE.md +0 -1145
  241. package/core/.system/kenaz/README.md +0 -787
  242. package/core/.system/memory/README.md +0 -719
  243. package/core/.system/pipeline/README.md +0 -712
  244. package/core/.system/rule/README.md +0 -794
  245. package/core/.system/sunny/README.md +0 -476
  246. package/core/.system/sunny/RULES.json +0 -89
  247. package/core/.system/sunny/knowledge/INDEX.md +0 -37
  248. package/core/.system/sunny/knowledge/agent-teams-api.md +0 -0
  249. package/core/.system/sunny/knowledge/ansuz-specialist.md +0 -500
  250. package/core/.system/sunny/knowledge/claude-cli-spawn.md +0 -177
  251. package/core/.system/sunny/knowledge/collaboration-api.md +0 -925
  252. package/core/.system/sunny/teammates/README.md +0 -107
  253. package/core/.system/sunny/teammates/how_to_use.yaml +0 -101
  254. package/core/.system/sunny/teammates/tools/headless/launcher.js +0 -318
  255. package/core/.system/sunny/teammates/tools/powershell/launcher.js +0 -241
  256. package/core/.system/sunny/teammates/tools/powershell/start.bat +0 -50
  257. package/core/.system/sunny/teammates/tools/utils/env.js +0 -96
  258. package/core/.system/sunny/teammates/tools/utils/format-output.js +0 -329
  259. package/core/.system/sunny/tools.json +0 -101
  260. package/core/dev/scripts/cleanup-queue.js +0 -184
  261. package/core/dev/scripts/cleanup_queue.js +0 -86
  262. package/core/dev/scripts/legacy/README.md +0 -39
  263. package/core/dev/scripts/legacy/dev_agent_scheduler.ps1.bak +0 -536
  264. package/core/dev/scripts/legacy/multi_agent_scheduler.ps1.bak +0 -1978
  265. package/core/dev/scripts/matrix/CLI_README.md +0 -214
  266. package/core/dev/scripts/matrix/README.md +0 -310
  267. package/core/dev/scripts/matrix/package-lock.json +0 -4266
  268. package/core/dev/scripts/matrix/package.json +0 -55
  269. package/core/dev/scripts/matrix/src/abbreviations.ts +0 -295
  270. package/core/dev/scripts/matrix/src/cli/commands/convert.ts +0 -211
  271. package/core/dev/scripts/matrix/src/cli/commands/create.ts +0 -95
  272. package/core/dev/scripts/matrix/src/cli/commands/delete.ts +0 -102
  273. package/core/dev/scripts/matrix/src/cli/commands/list-tags.ts.wip +0 -140
  274. package/core/dev/scripts/matrix/src/cli/commands/manage-tags.ts.wip +0 -173
  275. package/core/dev/scripts/matrix/src/cli/commands/query.ts +0 -261
  276. package/core/dev/scripts/matrix/src/cli/commands/read.ts +0 -77
  277. package/core/dev/scripts/matrix/src/cli/commands/stats.ts.wip +0 -168
  278. package/core/dev/scripts/matrix/src/cli/commands/tokens.ts.wip +0 -126
  279. package/core/dev/scripts/matrix/src/cli/commands/update.ts +0 -147
  280. package/core/dev/scripts/matrix/src/cli/commands/validate.ts +0 -275
  281. package/core/dev/scripts/matrix/src/cli/index.ts +0 -350
  282. package/core/dev/scripts/matrix/src/cli/utils/file-handler.ts +0 -72
  283. package/core/dev/scripts/matrix/src/cli/utils/logger.ts +0 -65
  284. package/core/dev/scripts/matrix/src/index.ts +0 -62
  285. package/core/dev/scripts/matrix/src/indexer/tag-indexer.ts +0 -165
  286. package/core/dev/scripts/matrix/src/parser.ts +0 -281
  287. package/core/dev/scripts/matrix/src/serializer.ts +0 -151
  288. package/core/dev/scripts/matrix/src/validator.ts +0 -301
  289. package/core/dev/scripts/matrix/tests/abbreviations.test.ts +0 -122
  290. package/core/dev/scripts/matrix/tests/cli.test.ts +0 -177
  291. package/core/dev/scripts/matrix/tests/convert.test.ts +0 -143
  292. package/core/dev/scripts/matrix/tests/parser.test.ts +0 -203
  293. package/core/dev/scripts/matrix/tests/query.test.ts +0 -85
  294. package/core/dev/scripts/matrix/tests/serializer.test.ts +0 -197
  295. package/core/dev/scripts/matrix/tests/validate.test.ts +0 -141
  296. package/core/dev/scripts/matrix/tests/validator.test.ts +0 -167
  297. package/core/dev/scripts/matrix/tsconfig.json +0 -20
  298. package/core/dev/scripts/matrix/types/index.d.ts +0 -111
  299. package/core/dev/scripts/matrix/types/index.d.ts.map +0 -1
  300. package/core/dev/scripts/matrix/types/index.js +0 -8
  301. package/core/dev/scripts/matrix/types/index.js.map +0 -1
  302. package/core/dev/scripts/matrix/types/index.ts +0 -124
  303. package/core/dev/scripts/memory-usage-report.js +0 -152
  304. package/core/dev/scripts/probe/ansuz-scaffold-ablation.js +0 -579
  305. package/core/dev/scripts/probe/ansuz-scaffold-ablation.test.js +0 -244
  306. package/core/dev/scripts/probe/hook-latency-probe.js +0 -315
  307. package/core/dev/scripts/probe/hook-latency-probe.test.js +0 -105
  308. package/core/dev/scripts/probe/prediction-signal-auc.js +0 -483
  309. package/core/dev/scripts/probe/prediction-signal-auc.test.js +0 -200
  310. package/core/dev/scripts/test-pty-ws-tunnel.js +0 -229
  311. package/core/dev/scripts/validate_pipeline.js +0 -197
  312. package/core/dev/spikes/task2145-evidence-20261004/backend-log-excerpt.txt +0 -56
  313. package/core/dev/spikes/task2145-evidence-20261004/graph.shadow +0 -0
  314. package/core/dev/spikes/task2145-evidence-20261004/graph.wal +0 -0
  315. package/core/dev/spikes/task2145-evidence-20261004/graph_corrupt_suspect.marker +0 -1
  316. package/core/plugins/kenaz/commands/strategy.md +0 -141
@@ -1,1046 +0,0 @@
1
- # kenaz-cortex RAG 引擎模組詳解
2
-
3
- > **版本**: 2.0.0
4
- > **最後更新**: 2026-02-20
5
- > **模組總數**: 58 個 Python 檔案
6
-
7
- ## 目錄
8
-
9
- - [系統架構](#系統架構)
10
- - [核心模組](#核心模組)
11
- - [chunker - 語義切片器](#chunker---語義切片器)
12
- - [vectordb - 向量資料庫](#vectordb---向量資料庫)
13
- - [sync - Git 增量同步](#sync---git-增量同步)
14
- - [api - 語義檢索 API](#api---語義檢索-api)
15
- - [context - 混合 Context 策略](#context---混合-context-策略)
16
- - [mcp - MCP Server](#mcp---mcp-server)
17
- - [symbols - 符號索引](#symbols---符號索引)
18
- - [retrieval - 混合檢索 V2](#retrieval---混合檢索-v2)
19
- - [輔助模組](#輔助模組)
20
- - [decomposition - 任務分解](#decomposition---任務分解)
21
- - [optimization - 代碼優化](#optimization---代碼優化)
22
- - [cli - 命令列介面](#cli---命令列介面)
23
- - [模組間依賴圖](#模組間依賴圖)
24
-
25
- ---
26
-
27
- ## 系統架構
28
-
29
- kenaz-cortex 是一個**智能 RAG 引擎**,模擬人腦的知識管理與檢索系統。主要功能包括:
30
-
31
- 1. **語義切片** - 按語義單位切分代碼和文檔
32
- 2. **向量存儲** - 使用 ChromaDB 進行本地向量存儲
33
- 3. **增量同步** - 與 Git commit 同步,自動更新索引
34
- 4. **智能檢索** - 四層混合檢索架構
35
- 5. **Context 管理** - 根據專案規模自動切換策略
36
-
37
- ```
38
- ┌─────────────────────────────────────────────────────────────────┐
39
- │ kenaz-cortex 架構 │
40
- │ │
41
- │ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
42
- │ │ 輸入層 │ │ 處理層 │ │ 輸出層 │ │
43
- │ │ │ │ │ │ │ │
44
- │ │ • chunker │ │ • vectordb │ │ • api │ │
45
- │ │ • sync │ │ • symbols │ │ • mcp │ │
46
- │ │ │ │ • retrieval │ │ • context │ │
47
- │ └──────────────┘ └──────────────┘ └──────────────┘ │
48
- │ │
49
- │ ┌──────────────────────────────────────────────────────────┐ │
50
- │ │ Git 增量同步(sync/) │ │
51
- │ │ Git Commit → 文件變更 → AST 切片 → 向量化 → 索引 │ │
52
- │ └──────────────────────────────────────────────────────────┘ │
53
- └─────────────────────────────────────────────────────────────────┘
54
- ```
55
-
56
- ---
57
-
58
- ## 核心模組
59
-
60
- ### chunker - 語義切片器
61
-
62
- **路徑**: `kenaz-cortex/src/cortex/chunker/`
63
-
64
- **職責**: 按語義單位切分代碼和文檔,確保邏輯完整性。
65
-
66
- #### 子模組
67
-
68
- | 模組 | 說明 | 支援特性 |
69
- |------|------|---------|
70
- | `python_chunker.py` | Python AST 切片器 | 函式、類別、模組頭部、Docstring、裝飾器、類型註解 |
71
- | `typescript_chunker.py` | TypeScript/JS 切片器 | 函式、類別、介面、型別別名、React Component |
72
- | `yaml_chunker.py` | YAML 語義切片器 | 頂層鍵值對、嵌套結構、任務定義 |
73
- | `markdown_chunker.py` | Markdown 切片器 | 章節、代碼塊、列表、表格 |
74
- | `__init__.py` | 模組匯出 | 統一介面 |
75
-
76
- #### 核心類別
77
-
78
- ```python
79
- @dataclass
80
- class Chunk:
81
- """代碼切片"""
82
- id: str # 唯一標識(SHA256 hash)
83
- file_path: str # 檔案路徑
84
- chunk_type: ChunkType # 切片類型(function, class, module_header)
85
- name: str # 名稱(函式名、類別名)
86
- content: str # 原始內容
87
- start_line: int # 起始行
88
- end_line: int # 結束行
89
- metadata: ChunkMetadata # 元數據(裝飾器、Docstring、依賴等)
90
- ```
91
-
92
- #### 使用範例
93
-
94
- ```python
95
- from cortex.chunker import PythonChunker
96
-
97
- chunker = PythonChunker()
98
- chunks = chunker.chunk_file("src/auth/login.py")
99
-
100
- for chunk in chunks:
101
- print(f"{chunk.name} ({chunk.chunk_type})")
102
- print(f" Lines: {chunk.start_line}-{chunk.end_line}")
103
- print(f" Decorators: {chunk.metadata.decorators}")
104
- ```
105
-
106
- #### 設計特點
107
-
108
- 1. **語義完整性** - 每個切片都是完整的邏輯單位
109
- 2. **元數據保留** - Docstring、裝飾器、類型註解不丟失
110
- 3. **多語言支援** - Python、TypeScript、YAML、Markdown
111
- 4. **可配置** - 支援最小切片長度、模組頭部包含等選項
112
-
113
- ---
114
-
115
- ### vectordb - 向量資料庫
116
-
117
- **路徑**: `kenaz-cortex/src/cortex/vectordb/`
118
-
119
- **職責**: 使用 ChromaDB 進行本地向量存儲,支援專案隔離。
120
-
121
- #### 子模組
122
-
123
- | 模組 | 說明 |
124
- |------|------|
125
- | `chroma.py` | ChromaDB 管理器 |
126
- | `__init__.py` | 模組匯出 |
127
-
128
- #### 核心類別
129
-
130
- ```python
131
- class ChromaManager:
132
- """
133
- ChromaDB 管理器
134
-
135
- 負責管理 ChromaDB 的連接、Collection 操作和資料 CRUD。
136
- 支援專案隔離,每個專案使用獨立的 Collection。
137
- """
138
-
139
- def __init__(self, config: CortexConfig | None = None, project_path: Path | str | None = None):
140
- """初始化,支援專案隔離"""
141
-
142
- def add_chunks(self, chunks: list[Chunk]) -> None:
143
- """批次新增切片"""
144
-
145
- def query(self, query_text: str, top_k: int = 5, where: dict | None = None) -> list[QueryResult]:
146
- """語義搜尋"""
147
-
148
- def delete_by_file(self, file_path: str) -> None:
149
- """刪除指定檔案的所有切片"""
150
- ```
151
-
152
- #### 專案隔離機制
153
-
154
- ```python
155
- # 不同專案使用不同 Collection
156
- manager_A = ChromaManager(project_path="/path/to/projectA")
157
- manager_B = ChromaManager(project_path="/path/to/projectB")
158
-
159
- # Collection 名稱:kenaz_<project_hash>
160
- # projectA → kenaz_abc123
161
- # projectB → kenaz_def456
162
- ```
163
-
164
- #### 向量化模型
165
-
166
- - **預設模型**: `all-MiniLM-L6-v2` (Sentence Transformers)
167
- - **維度**: 384
168
- - **速度**: 快速(本地推理)
169
- - **精度**: 中等(適合代碼檢索)
170
-
171
- #### 資料庫路徑
172
-
173
- - **預設位置**: `.cortex/db/chroma.sqlite3`
174
- - **可配置**: 透過 `CortexConfig.db_path` 設定
175
-
176
- ---
177
-
178
- ### sync - Git 增量同步
179
-
180
- **路徑**: `kenaz-cortex/src/cortex/sync/`
181
-
182
- **職責**: 與開發節奏同步,透過 Git hook 觸發增量索引。
183
-
184
- #### 子模組
185
-
186
- | 模組 | 說明 |
187
- |------|------|
188
- | `git_sync.py` | Git 增量同步管理器 |
189
- | `__init__.py` | 模組匯出 |
190
-
191
- #### 核心類別
192
-
193
- ```python
194
- class GitSyncManager:
195
- """
196
- Git 增量同步管理器
197
-
198
- 負責偵測 Git 變更並同步到向量資料庫。
199
- 支援增量更新、批次處理和同步日誌記錄。
200
- """
201
-
202
- def sync_from_last_commit(self) -> SyncStats:
203
- """從上次同步的 commit 開始增量同步"""
204
-
205
- def sync_commits(self, from_commit: str, to_commit: str) -> SyncStats:
206
- """同步指定 commit 範圍的變更"""
207
-
208
- def full_reindex(self) -> SyncStats:
209
- """全量重新索引"""
210
- ```
211
-
212
- #### 變更類型處理
213
-
214
- ```python
215
- class ChangeType(str, Enum):
216
- ADDED = "A" # 新增 → 切片 + 索引
217
- MODIFIED = "M" # 修改 → 刪除舊切片 + 索引新切片
218
- DELETED = "D" # 刪除 → 移除索引
219
- RENAMED = "R" # 重命名 → 移除舊索引 + 索引新檔案
220
- ```
221
-
222
- #### 支援的檔案類型
223
-
224
- | 副檔名 | 語言 | Chunker |
225
- |--------|------|---------|
226
- | `.py` | Python | PythonChunker |
227
- | `.ts`, `.tsx` | TypeScript | TypeScriptChunker |
228
- | `.js`, `.jsx` | JavaScript | TypeScriptChunker |
229
- | `.yaml`, `.yml` | YAML | YamlChunker |
230
- | `.md` | Markdown | MarkdownChunker |
231
-
232
- #### Git Hook 整合
233
-
234
- ```bash
235
- # .git/hooks/post-commit
236
- #!/bin/bash
237
- cortex sync --incremental
238
- ```
239
-
240
- #### 符號索引整合
241
-
242
- 如果 Universal Ctags 可用,會自動整合符號索引:
243
-
244
- ```python
245
- if is_ctags_available():
246
- symbol_index = SymbolIndex(project_root)
247
- symbol_index.update(changed_files)
248
- ```
249
-
250
- ---
251
-
252
- ### api - 語義檢索 API
253
-
254
- **路徑**: `kenaz-cortex/src/cortex/api/`
255
-
256
- **職責**: 供 Agent 查詢相關代碼的高階介面。
257
-
258
- #### 子模組
259
-
260
- | 模組 | 說明 |
261
- |------|------|
262
- | `search.py` | 語義檢索 API |
263
- | `__init__.py` | 模組匯出 |
264
-
265
- #### 核心類別
266
-
267
- ```python
268
- class CortexSearch:
269
- """
270
- Cortex 語義檢索 API
271
-
272
- 提供語義搜尋、名稱查找、檔案切片獲取等功能。
273
- """
274
-
275
- def query(self, query: str, top_k: int = 5, filters: SearchFilters | None = None) -> list[SearchResult]:
276
- """語義搜尋"""
277
-
278
- def find_by_name(self, name: str, chunk_type: str | None = None) -> list[SearchResult]:
279
- """按名稱搜尋"""
280
-
281
- def get_file_chunks(self, file_path: str) -> list[SearchResult]:
282
- """獲取檔案的所有 chunks"""
283
- ```
284
-
285
- #### 跨專案搜尋
286
-
287
- ```python
288
- class CrossProjectSearch:
289
- """跨專案語義搜尋"""
290
-
291
- def query(self, query: str, top_k: int = 5, project_filter: list[str] | None = None) -> list[CrossSearchResult]:
292
- """在多個專案中搜尋"""
293
- ```
294
-
295
- #### 搜尋過濾器
296
-
297
- ```python
298
- @dataclass
299
- class SearchFilters:
300
- language: str | None = None # 語言過濾(python, typescript)
301
- chunk_type: list[str] | str | None # 類型過濾(function, class)
302
- file_path: str | None = None # 路徑過濾
303
- min_relevance: float | None = None # 最低相關性分數
304
- ```
305
-
306
- #### 使用範例
307
-
308
- ```python
309
- from cortex.api import CortexSearch
310
-
311
- search = CortexSearch(project_path="/path/to/project")
312
-
313
- # 語義搜尋
314
- results = search.query(
315
- query="用戶登入驗證邏輯",
316
- top_k=5,
317
- filters=SearchFilters(language="python", chunk_type=["function", "class"])
318
- )
319
-
320
- # 按名稱搜尋
321
- results = search.find_by_name("authenticate_user")
322
-
323
- # 獲取檔案的所有 chunks
324
- chunks = search.get_file_chunks("src/auth/login.py")
325
- ```
326
-
327
- ---
328
-
329
- ### context - 混合 Context 策略
330
-
331
- **路徑**: `kenaz-cortex/src/cortex/context/`
332
-
333
- **職責**: 提供混合 Context 策略的實作,根據專案規模自動選擇最佳策略。
334
-
335
- #### 子模組
336
-
337
- | 模組 | 說明 |
338
- |------|------|
339
- | `structure_extractor.py` | 結構提取器,生成 Always In Context |
340
- | `smart_switch.py` | 智能切換器,根據專案規模自動選擇策略 |
341
- | `map_reduce.py` | Map-Reduce 聚合器,生成專案級概覽 |
342
- | `__init__.py` | 模組匯出 |
343
-
344
- #### 核心類別
345
-
346
- ```python
347
- class SmartContextSwitch:
348
- """
349
- 智能 Context 切換器
350
-
351
- 根據專案規模自動選擇最佳 Context 策略:
352
- - 初期:全量 Context + Prompt Caching(tokens < 40k AND files < 20)
353
- - 成長期:精簡 Context + RAG 檢索(tokens > 40k OR files > 20)
354
- """
355
-
356
- def get_context(self, mode: str = "auto") -> str:
357
- """
358
- 獲取 Context
359
-
360
- Args:
361
- mode: "auto" (自動), "full" (全量), "hybrid" (混合)
362
- """
363
- ```
364
-
365
- #### 雙閥值判斷
366
-
367
- ```python
368
- @dataclass
369
- class SmartSwitchConfig:
370
- # 切換閥值(任一超過即啟用 RAG)
371
- threshold_tokens: int = 40000 # Token 閥值
372
- threshold_files: int = 20 # 檔案數閥值
373
-
374
- # 全量 Context 配置
375
- prompt_caching: bool = True # 啟用 Prompt Caching
376
- exclude_patterns: list[str] # 排除模式(node_modules, tests 等)
377
-
378
- # 精簡 Context 配置
379
- project_tree_depth: int = 3 # 目錄樹深度
380
- essential_max_tokens: int = 2000
381
-
382
- # RAG 檢索配置
383
- rag_top_k: int = 5
384
- rag_min_relevance: float = 0.6
385
- ```
386
-
387
- #### Context 模式
388
-
389
- | 模式 | 條件 | 策略 |
390
- |------|------|------|
391
- | **Full** | tokens < 40k AND files < 20 | 全量 Context + Prompt Caching |
392
- | **Hybrid** | tokens > 40k OR files > 20 | 精簡 Context + RAG 檢索 |
393
-
394
- #### 結構提取器
395
-
396
- ```python
397
- class StructureExtractor:
398
- """
399
- 結構提取器
400
-
401
- 生成專案的 Always In Context(固定注入的結構概覽)。
402
- """
403
-
404
- def extract(self) -> str:
405
- """
406
- 提取專案結構
407
-
408
- Returns:
409
- 專案目錄樹 + 類型定義 + README + SPEC
410
- """
411
- ```
412
-
413
- #### Map-Reduce 聚合器
414
-
415
- ```python
416
- class MapReduceContext:
417
- """
418
- Map-Reduce 聚合器
419
-
420
- 生成專案級概覽,適用於大型專案。
421
- """
422
-
423
- def generate_overview(self) -> ProjectOverview:
424
- """
425
- 生成專案概覽
426
-
427
- Returns:
428
- 專案摘要、架構、技術棧、關鍵模組
429
- """
430
- ```
431
-
432
- ---
433
-
434
- ### mcp - MCP Server
435
-
436
- **路徑**: `kenaz-cortex/src/cortex/mcp/`
437
-
438
- **職責**: 提供 Model Context Protocol (MCP) 整合,讓 Claude Code 可以自動使用 RAG 功能。
439
-
440
- #### 子模組
441
-
442
- | 模組 | 說明 |
443
- |------|------|
444
- | `server.py` | MCP Server 實作 |
445
- | `__init__.py` | 模組匯出 |
446
-
447
- #### 提供的 MCP 工具
448
-
449
- | 工具 | 說明 | 參數 |
450
- |------|------|------|
451
- | `cortex_search` | 語義代碼搜索 | query, top_k, language, chunk_type |
452
- | `cortex_context` | 專案結構 Context(自動切換全量/RAG) | mode (auto, full, hybrid) |
453
- | `cortex_file_chunks` | 文件切片查詢 | file_path |
454
- | `cortex_symbols` | 符號索引搜尋 | name, kind, file_path |
455
- | `cortex_status` | RAG 引擎狀態 | - |
456
- | `cortex_reindex` | 重新索引 | - |
457
-
458
- #### 自動切換策略
459
-
460
- ```python
461
- def cortex_context(mode: str = "auto") -> str:
462
- """
463
- 專案結構 Context
464
-
465
- 自動切換策略:
466
- - 小型專案(<40k tokens, <20 files)→ 全量 Context
467
- - 大型專案(>40k tokens 或 >20 files)→ RAG 模式
468
- """
469
- switch = get_smart_switch()
470
- return switch.get_context(mode=mode)
471
- ```
472
-
473
- #### MCP Server 配置
474
-
475
- 設定檔:`~/.config/claude-code/config.json`
476
-
477
- ```json
478
- {
479
- "mcpServers": {
480
- "kenaz-cortex": {
481
- "command": "python",
482
- "args": ["-m", "cortex.mcp"],
483
- "env": {
484
- "CORTEX_PROJECT_PATH": "${workspaceFolder}"
485
- }
486
- }
487
- }
488
- }
489
- ```
490
-
491
- #### 啟動方式
492
-
493
- ```bash
494
- # 方式 1:透過 CLI
495
- cortex mcp
496
-
497
- # 方式 2:透過 Python 模組
498
- python -m cortex.mcp
499
-
500
- # 方式 3:程式碼調用
501
- from cortex.mcp import run_server
502
- run_server()
503
- ```
504
-
505
- ---
506
-
507
- ### symbols - 符號索引
508
-
509
- **路徑**: `kenaz-cortex/src/cortex/symbols/`
510
-
511
- **職責**: 整合 Universal Ctags 工具,為專案建立結構化的符號索引。
512
-
513
- #### 子模組
514
-
515
- | 模組 | 說明 |
516
- |------|------|
517
- | `ctags_runner.py` | Ctags 執行器 |
518
- | `symbol_index.py` | 符號索引管理器 |
519
- | `__init__.py` | 模組匯出 |
520
-
521
- #### 核心類別
522
-
523
- ```python
524
- class SymbolIndex:
525
- """
526
- 符號索引管理器
527
-
528
- 讓 Agent 能不經向量搜索就精確定位函數/類/介面定義。
529
- """
530
-
531
- def build(self) -> SymbolIndexStats:
532
- """建立完整符號索引"""
533
-
534
- def update(self, changed_files: list[str]) -> SymbolIndexStats:
535
- """增量更新索引"""
536
-
537
- def find_by_name(self, name: str) -> list[SymbolEntry]:
538
- """按名稱搜尋符號"""
539
-
540
- def find_by_kind(self, kind: SymbolKind) -> list[SymbolEntry]:
541
- """按類型搜尋符號"""
542
- ```
543
-
544
- #### 支援的符號類型
545
-
546
- ```python
547
- SymbolKind = Literal[
548
- "function", # 函式
549
- "class", # 類別
550
- "method", # 方法
551
- "interface", # 介面
552
- "type", # 型別
553
- "variable", # 變數
554
- "constant", # 常數
555
- "property", # 屬性
556
- "module", # 模組
557
- "namespace", # 命名空間
558
- "enum", # 枚舉
559
- "member", # 成員
560
- "parameter", # 參數
561
- ]
562
- ```
563
-
564
- #### Ctags 整合
565
-
566
- ```python
567
- def is_ctags_available() -> bool:
568
- """檢查 Universal Ctags 是否可用"""
569
-
570
- def run_ctags(project_root: Path) -> list[SymbolEntry]:
571
- """執行 ctags 並解析輸出"""
572
- ```
573
-
574
- #### 索引持久化
575
-
576
- - **檔案**: `.cortex/symbols.json`
577
- - **格式**: JSON
578
- - **更新時機**: Git commit 時自動增量更新
579
-
580
- ---
581
-
582
- ### retrieval - 混合檢索 V2
583
-
584
- **路徑**: `kenaz-cortex/src/cortex/retrieval/`
585
-
586
- **職責**: 四層混合檢索架構,根據任務特徵自動選擇最佳檢索策略。
587
-
588
- #### 子模組
589
-
590
- | 模組 | 說明 |
591
- |------|------|
592
- | `strategy.py` | 策略選擇器 |
593
- | `rule_first.py` | Layer 1 - 規則優先層 |
594
- | `architecture.py` | Layer 2 - 架構視角層 |
595
- | `dependency_expander.py` | Layer 3 - 依賴擴展 |
596
- | `similar_finder.py` | Layer 3 - 相似檢索 |
597
- | `__init__.py` | 模組匯出 |
598
-
599
- #### 四層架構
600
-
601
- ```
602
- ┌────────────────────────────────────────────────────────────┐
603
- │ Layer 1: 規則優先層 (Rule-First Layer) │
604
- │ 確保 Agent 理解專案規範 │
605
- │ • .rule/ → CLAUDE.md → .failure/ │
606
- └────────────────────────────────────────────────────────────┘
607
- ↓
608
- ┌────────────────────────────────────────────────────────────┐
609
- │ Layer 2: 架構視角層 (Architecture Layer) │
610
- │ 提供系統架構鳥瞰視角 │
611
- │ • full: 完整專案結構 │
612
- │ • simplified: 精簡版 │
613
- │ • disabled: 禁用(符號查找等簡單任務) │
614
- └────────────────────────────────────────────────────────────┘
615
- ↓
616
- ┌────────────────────────────────────────────────────────────┐
617
- │ Layer 3: 智能檢索層 (Smart Retrieval Layer) │
618
- │ • RAG: 語義檢索 │
619
- │ • Dependency: 依賴擴展 │
620
- │ • Similar: 相似特徵查找 │
621
- └────────────────────────────────────────────────────────────┘
622
- ↓
623
- ┌────────────────────────────────────────────────────────────┐
624
- │ Layer 4: 全局驗證層 (Global Validation Layer) │
625
- │ 重構/架構變更的全局驗證 │
626
- │ • 影響分析 │
627
- │ • 依賴檢查 │
628
- │ • 測試覆蓋 │
629
- └────────────────────────────────────────────────────────────┘
630
- ```
631
-
632
- #### 策略選擇器
633
-
634
- ```python
635
- class StrategySelector:
636
- """
637
- 策略選擇器
638
-
639
- 根據任務描述、複雜度、優先級、路徑範圍等多維度特徵,
640
- 自動選擇最佳的檢索策略。
641
- """
642
-
643
- def select_strategy(self, task: Task) -> RetrievalStrategy:
644
- """
645
- 選擇檢索策略
646
-
647
- 流程:
648
- 1. 關鍵字分析 → 確定基礎任務類型
649
- 2. 複雜度分析 → 計算複雜度分數
650
- 3. 優先級覆蓋 → 根據 P0/P1/P2/P3 調整
651
- 4. 路徑範圍分析 → 根據影響範圍微調
652
- 5. 特殊情況處理 → 處理邊界 case
653
- """
654
- ```
655
-
656
- #### 支援的任務類型
657
-
658
- ```python
659
- class TaskType(Enum):
660
- NEW_FEATURE = "new_feature" # 新功能開發
661
- BUG_FIX = "bug_fix" # Bug 修復
662
- REFACTOR = "refactor" # 重構
663
- ARCHITECTURE = "architecture" # 架構變更
664
- TEST = "test" # 測試撰寫
665
- DOCUMENTATION = "documentation" # 文檔更新
666
- PERFORMANCE = "performance" # 性能優化
667
- SECURITY = "security" # 安全修復
668
- DEPENDENCY_UPDATE = "dependency_update" # 依賴更新
669
- UI_UX = "ui_ux" # UI/UX 調整
670
- DATA_MIGRATION = "data_migration" # 數據遷移
671
- SYMBOL_LOOKUP = "symbol_lookup" # 符號查找
672
- ```
673
-
674
- #### 檢索策略配置
675
-
676
- ```python
677
- @dataclass
678
- class RetrievalStrategy:
679
- task_type: TaskType
680
- layer1_enabled: bool = True # Layer 1 啟用
681
- layer2_mode: str = "full" # Layer 2 模式(full, simplified, disabled)
682
- layer3_components: list[str] # Layer 3 組件(rag, dependency, similar)
683
- layer4_enabled: bool = False # Layer 4 啟用
684
- ```
685
-
686
- #### 策略範例
687
-
688
- | 任務類型 | Layer 1 | Layer 2 | Layer 3 | Layer 4 |
689
- |---------|---------|---------|---------|---------|
690
- | 新功能開發 | ✅ | full | [rag, similar] | ❌ |
691
- | Bug 修復 | ✅ | simplified | [rag, dependency] | ❌ |
692
- | 架構變更 | ✅ | full | [rag, dependency] | ✅ |
693
- | 符號查找 | ❌ | disabled | [] | ❌ |
694
-
695
- ---
696
-
697
- ## 輔助模組
698
-
699
- ### decomposition - 任務分解
700
-
701
- **路徑**: `kenaz-cortex/src/cortex/decomposition/`
702
-
703
- **職責**: 自動將 PRODUCT_SPEC 特性分解為可執行的開發任務。
704
-
705
- #### 子模組
706
-
707
- | 模組 | 說明 |
708
- |------|------|
709
- | `engine.py` | 分解引擎 |
710
- | `graph.py` | 任務圖 |
711
- | `rules.py` | 分解規則 |
712
- | `__init__.py` | 模組匯出 |
713
-
714
- #### 核心類別
715
-
716
- ```python
717
- class DecompositionEngine:
718
- """
719
- 任務分解引擎
720
-
721
- 自動分解 PRODUCT_SPEC 特性為開發任務。
722
- """
723
-
724
- def decompose(self, spec: ProductSpec) -> TaskGraph:
725
- """
726
- 分解規格為任務圖
727
-
728
- Returns:
729
- 包含任務、依賴關係、並行組的任務圖
730
- """
731
- ```
732
-
733
- #### 任務圖
734
-
735
- ```python
736
- class TaskGraph:
737
- """
738
- 任務圖
739
-
740
- 表示任務間的依賴關係和並行執行策略。
741
- """
742
-
743
- def to_mermaid(self) -> str:
744
- """輸出 Mermaid 格式的依賴圖"""
745
-
746
- def write_to_pipeline(self, output_dir: str) -> None:
747
- """寫入 Pipeline 任務目錄"""
748
- ```
749
-
750
- #### 使用範例
751
-
752
- ```python
753
- from cortex.spec import SpecParser
754
- from cortex.decomposition import DecompositionEngine
755
-
756
- parser = SpecParser()
757
- spec = parser.parse("dev/PRODUCT_SPEC.yaml")
758
-
759
- engine = DecompositionEngine()
760
- task_graph = engine.decompose(spec)
761
-
762
- # 輸出任務列表
763
- for task in task_graph.tasks:
764
- print(f"{task.id}: {task.title}")
765
- print(f" Agent: {task.agent_type}")
766
- print(f" Depends on: {task.dependencies}")
767
-
768
- # 輸出依賴圖(Mermaid 格式)
769
- print(task_graph.to_mermaid())
770
- ```
771
-
772
- ---
773
-
774
- ### optimization - 代碼優化
775
-
776
- **路徑**: `kenaz-cortex/src/cortex/optimization/`
777
-
778
- **職責**: 提供代碼品質監控和優化建議生成功能。
779
-
780
- #### 子模組
781
-
782
- | 模組 | 說明 |
783
- |------|------|
784
- | `agent.py` | 優化 Agent |
785
- | `analyzer.py` | 代碼分析器 |
786
- | `suggestion.py` | 建議生成器 |
787
- | `__init__.py` | 模組匯出 |
788
-
789
- #### 核心類別
790
-
791
- ```python
792
- class OptimizationAgent:
793
- """
794
- 優化 Agent
795
-
796
- 主動監控代碼品質,生成優化建議。
797
- """
798
-
799
- def analyze(self, file_path: str) -> SuggestionReport:
800
- """分析檔案並生成建議"""
801
- ```
802
-
803
- #### 建議類型
804
-
805
- ```python
806
- class SuggestionType(Enum):
807
- PERFORMANCE = "performance" # 性能優化
808
- SECURITY = "security" # 安全修復
809
- CODE_SMELL = "code_smell" # 代碼異味
810
- BEST_PRACTICE = "best_practice" # 最佳實踐
811
- REFACTOR = "refactor" # 重構
812
- ```
813
-
814
- #### 嚴重程度
815
-
816
- ```python
817
- class SeverityLevel(Enum):
818
- CRITICAL = "critical" # 嚴重(必須修復)
819
- HIGH = "high" # 高(優先修復)
820
- MEDIUM = "medium" # 中(計劃修復)
821
- LOW = "low" # 低(可選修復)
822
- ```
823
-
824
- ---
825
-
826
- ### cli - 命令列介面
827
-
828
- **路徑**: `kenaz-cortex/src/cortex/cli/`
829
-
830
- **職責**: 提供 cortex 命令列工具。
831
-
832
- #### 子模組
833
-
834
- | 模組 | 說明 |
835
- |------|------|
836
- | `main.py` | CLI 主程式 |
837
- | `__init__.py` | 模組匯出 |
838
-
839
- #### 可用命令
840
-
841
- ```bash
842
- # 語義搜尋
843
- cortex search "用戶登入驗證"
844
-
845
- # 查看狀態
846
- cortex status
847
-
848
- # 重新索引
849
- cortex reindex
850
-
851
- # 增量同步
852
- cortex sync --incremental
853
-
854
- # 啟動 MCP Server
855
- cortex mcp
856
-
857
- # 符號查找
858
- cortex symbols --name authenticate_user
859
-
860
- # 專案概覽
861
- cortex overview
862
- ```
863
-
864
- #### CLI 實作
865
-
866
- 基於 **Typer** 框架,提供:
867
-
868
- - 彩色輸出(rich)
869
- - 進度條
870
- - 自動補全
871
- - 參數驗證
872
-
873
- ---
874
-
875
- ## 模組間依賴圖
876
-
877
- ```mermaid
878
- graph TD
879
- A[config.py] -->|配置| B[chunker/]
880
- A -->|配置| C[vectordb/]
881
- A -->|配置| D[sync/]
882
- A -->|配置| E[api/]
883
-
884
- B -->|切片| C
885
- B -->|切片| D
886
-
887
- C -->|存儲| E
888
-
889
- D -->|同步| C
890
- D -->|Git 變更| B
891
- D -->|符號| F[symbols/]
892
-
893
- E -->|檢索| G[retrieval/]
894
- E -->|Context| H[context/]
895
-
896
- F -->|索引| E
897
-
898
- G -->|策略| H
899
-
900
- H -->|智能切換| I[mcp/]
901
- E -->|API| I
902
-
903
- J[decomposition/] -->|分解| K[spec/]
904
-
905
- L[optimization/] -->|分析| E
906
-
907
- M[cli/] -->|CLI| E
908
- M -->|CLI| D
909
- M -->|CLI| I
910
-
911
- classDef core fill:#f9f,stroke:#333,stroke-width:2px
912
- classDef aux fill:#bbf,stroke:#333,stroke-width:1px
913
- classDef interface fill:#bfb,stroke:#333,stroke-width:2px
914
-
915
- class B,C,D,E,F,G,H core
916
- class J,L aux
917
- class I,M interface
918
- ```
919
-
920
- ### 依賴層級
921
-
922
- #### Layer 0: 基礎層
923
- - `config.py` - 配置管理
924
-
925
- #### Layer 1: 輸入層
926
- - `chunker/` - 語義切片
927
- - `symbols/` - 符號索引
928
-
929
- #### Layer 2: 存儲層
930
- - `vectordb/` - 向量資料庫
931
- - `sync/` - Git 增量同步
932
-
933
- #### Layer 3: 檢索層
934
- - `api/` - 語義檢索 API
935
- - `retrieval/` - 混合檢索 V2
936
- - `context/` - 混合 Context 策略
937
-
938
- #### Layer 4: 介面層
939
- - `mcp/` - MCP Server
940
- - `cli/` - 命令列介面
941
-
942
- #### Layer 5: 輔助層
943
- - `decomposition/` - 任務分解
944
- - `optimization/` - 代碼優化
945
- - `spec/` - 規格解析
946
- - `integration/` - Agent 整合
947
- - `summary/` - 摘要生成
948
-
949
- ---
950
-
951
- ## 關鍵設計決策
952
-
953
- ### 1. 為何選擇 ChromaDB?
954
-
955
- - **本地優先** - 不依賴雲端服務
956
- - **易於整合** - Python 原生支援
957
- - **持久化存儲** - 支援 SQLite 後端
958
- - **專案隔離** - 每個專案獨立 Collection
959
-
960
- ### 2. 為何選擇 AST 切片?
961
-
962
- - **語義完整性** - 確保邏輯單位不被切斷
963
- - **元數據保留** - Docstring、裝飾器、類型註解
964
- - **依賴分析** - 可分析函式間依賴關係
965
-
966
- ### 3. 為何選擇 Git 同步?
967
-
968
- - **與開發節奏同步** - 不需要手動觸發
969
- - **增量更新** - 只處理變更檔案,效率高
970
- - **歷史記錄** - 可回溯任意 commit 的索引狀態
971
-
972
- ### 4. 為何選擇四層檢索架構?
973
-
974
- - **Layer 1(規則優先)** - 確保 Agent 遵守專案規範
975
- - **Layer 2(架構視角)** - 提供全局視野,避免局部優化
976
- - **Layer 3(智能檢索)** - 根據任務特徵動態調整檢索策略
977
- - **Layer 4(全局驗證)** - 防止架構變更破壞系統穩定性
978
-
979
- ### 5. 為何選擇 MCP?
980
-
981
- - **標準化介面** - Claude Code 官方支援
982
- - **自動整合** - 無需手動配置
983
- - **工具豐富** - 支援多種檢索模式
984
-
985
- ---
986
-
987
- ## 性能指標
988
-
989
- ### 索引速度
990
-
991
- | 專案規模 | 檔案數 | 索引時間 | 記憶體使用 |
992
- |---------|-------|---------|-----------|
993
- | 小型 | < 50 | 2-5 秒 | < 100 MB |
994
- | 中型 | 50-500 | 10-30 秒 | 100-500 MB |
995
- | 大型 | > 500 | 1-3 分鐘 | 500 MB - 1 GB |
996
-
997
- ### 檢索速度
998
-
999
- | 檢索類型 | 延遲 | Top-K |
1000
- |---------|------|-------|
1001
- | 語義搜尋 | 50-200 ms | 5-10 |
1002
- | 符號查找 | 10-50 ms | - |
1003
- | 檔案切片 | 5-20 ms | - |
1004
-
1005
- ### Context 生成速度
1006
-
1007
- | 模式 | 延遲 | Token 數 |
1008
- |------|------|---------|
1009
- | Full | 100-500 ms | 5k-40k |
1010
- | Hybrid | 200-800 ms | 2k-10k |
1011
-
1012
- ---
1013
-
1014
- ## 未來規劃
1015
-
1016
- ### Phase 1: 增強檢索能力
1017
- - [ ] 支援更多語言(Go, Rust, Java)
1018
- - [ ] 混合檢索優化(BM25 + Dense)
1019
- - [ ] 跨專案檢索增強
1020
-
1021
- ### Phase 2: 智能 Context 管理
1022
- - [ ] 動態 Token 預算分配
1023
- - [ ] Context 緩存與復用
1024
- - [ ] 多模態 Context(圖片、圖表)
1025
-
1026
- ### Phase 3: Agent 整合深化
1027
- - [ ] 任務自動分解與規劃
1028
- - [ ] 代碼自動優化建議
1029
- - [ ] 持續學習與改進
1030
-
1031
- ---
1032
-
1033
- ## 相關文檔
1034
-
1035
- | 文檔 | 路徑 |
1036
- |------|------|
1037
- | Cortex 整合指南 | `.memory/CORTEX_GUIDE.md` |
1038
- | 完整規格 | `docs/specs/MEMORY_SYSTEM_SPEC.md` |
1039
- | MCP 設置指南 | `dev/scripts/setup_mcp.ps1` |
1040
- | RAG 引擎設置 | `dev/scripts/SETUP_RAG.bat` |
1041
-
1042
- ---
1043
-
1044
- **版本**: 2.0.0
1045
- **最後更新**: 2026-02-20
1046
- **維護者**: Kenaz AI Team