@mars167/git-ai 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (122) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +364 -0
  3. package/README.zh-CN.md +361 -0
  4. package/assets/hooks/post-checkout +28 -0
  5. package/assets/hooks/post-merge +28 -0
  6. package/assets/hooks/pre-commit +17 -0
  7. package/assets/hooks/pre-push +29 -0
  8. package/dist/bin/git-ai.js +62 -0
  9. package/dist/src/commands/ai.js +30 -0
  10. package/dist/src/commands/checkIndex.js +19 -0
  11. package/dist/src/commands/dsr.js +156 -0
  12. package/dist/src/commands/graph.js +203 -0
  13. package/dist/src/commands/hooks.js +125 -0
  14. package/dist/src/commands/index.js +92 -0
  15. package/dist/src/commands/pack.js +31 -0
  16. package/dist/src/commands/query.js +139 -0
  17. package/dist/src/commands/semantic.js +134 -0
  18. package/dist/src/commands/serve.js +14 -0
  19. package/dist/src/commands/status.js +78 -0
  20. package/dist/src/commands/trae.js +75 -0
  21. package/dist/src/commands/unpack.js +28 -0
  22. package/dist/src/core/archive.js +91 -0
  23. package/dist/src/core/astGraph.js +127 -0
  24. package/dist/src/core/astGraphQuery.js +142 -0
  25. package/dist/src/core/cozo.js +266 -0
  26. package/dist/src/core/cpg/astLayer.js +56 -0
  27. package/dist/src/core/cpg/callGraph.js +483 -0
  28. package/dist/src/core/cpg/cfgLayer.js +490 -0
  29. package/dist/src/core/cpg/dfgLayer.js +237 -0
  30. package/dist/src/core/cpg/index.js +80 -0
  31. package/dist/src/core/cpg/types.js +108 -0
  32. package/dist/src/core/crypto.js +10 -0
  33. package/dist/src/core/dsr/generate.js +308 -0
  34. package/dist/src/core/dsr/gitContext.js +74 -0
  35. package/dist/src/core/dsr/indexMaterialize.js +106 -0
  36. package/dist/src/core/dsr/paths.js +26 -0
  37. package/dist/src/core/dsr/query.js +73 -0
  38. package/dist/src/core/dsr/snapshotParser.js +73 -0
  39. package/dist/src/core/dsr/state.js +27 -0
  40. package/dist/src/core/dsr/types.js +2 -0
  41. package/dist/src/core/embedding/fusion.js +52 -0
  42. package/dist/src/core/embedding/index.js +43 -0
  43. package/dist/src/core/embedding/parser.js +14 -0
  44. package/dist/src/core/embedding/semantic.js +254 -0
  45. package/dist/src/core/embedding/structural.js +97 -0
  46. package/dist/src/core/embedding/symbolic.js +117 -0
  47. package/dist/src/core/embedding/tokenizer.js +91 -0
  48. package/dist/src/core/embedding/types.js +2 -0
  49. package/dist/src/core/embedding.js +36 -0
  50. package/dist/src/core/git.js +49 -0
  51. package/dist/src/core/gitDiff.js +73 -0
  52. package/dist/src/core/indexCheck.js +131 -0
  53. package/dist/src/core/indexer.js +185 -0
  54. package/dist/src/core/indexerIncremental.js +303 -0
  55. package/dist/src/core/indexing/config.js +51 -0
  56. package/dist/src/core/indexing/hnsw.js +568 -0
  57. package/dist/src/core/indexing/index.js +17 -0
  58. package/dist/src/core/indexing/monitor.js +82 -0
  59. package/dist/src/core/indexing/parallel.js +252 -0
  60. package/dist/src/core/lancedb.js +111 -0
  61. package/dist/src/core/lfs.js +27 -0
  62. package/dist/src/core/log.js +62 -0
  63. package/dist/src/core/manifest.js +88 -0
  64. package/dist/src/core/parser/adapter.js +2 -0
  65. package/dist/src/core/parser/c.js +93 -0
  66. package/dist/src/core/parser/chunkRelations.js +178 -0
  67. package/dist/src/core/parser/chunker.js +274 -0
  68. package/dist/src/core/parser/go.js +98 -0
  69. package/dist/src/core/parser/java.js +80 -0
  70. package/dist/src/core/parser/markdown.js +76 -0
  71. package/dist/src/core/parser/python.js +81 -0
  72. package/dist/src/core/parser/rust.js +103 -0
  73. package/dist/src/core/parser/typescript.js +98 -0
  74. package/dist/src/core/parser/utils.js +62 -0
  75. package/dist/src/core/parser/yaml.js +53 -0
  76. package/dist/src/core/parser.js +75 -0
  77. package/dist/src/core/paths.js +10 -0
  78. package/dist/src/core/repoMap.js +164 -0
  79. package/dist/src/core/retrieval/cache.js +31 -0
  80. package/dist/src/core/retrieval/classifier.js +74 -0
  81. package/dist/src/core/retrieval/expander.js +80 -0
  82. package/dist/src/core/retrieval/fuser.js +40 -0
  83. package/dist/src/core/retrieval/index.js +32 -0
  84. package/dist/src/core/retrieval/reranker.js +304 -0
  85. package/dist/src/core/retrieval/types.js +2 -0
  86. package/dist/src/core/retrieval/weights.js +42 -0
  87. package/dist/src/core/search.js +41 -0
  88. package/dist/src/core/sq8.js +65 -0
  89. package/dist/src/core/symbolSearch.js +143 -0
  90. package/dist/src/core/types.js +2 -0
  91. package/dist/src/core/workspace.js +116 -0
  92. package/dist/src/mcp/server.js +794 -0
  93. package/docs/README.md +44 -0
  94. package/docs/cross-encoder.md +157 -0
  95. package/docs/embedding.md +158 -0
  96. package/docs/logo.png +0 -0
  97. package/docs/windows-setup.md +67 -0
  98. package/docs/zh-CN/DESIGN.md +102 -0
  99. package/docs/zh-CN/README.md +46 -0
  100. package/docs/zh-CN/advanced.md +26 -0
  101. package/docs/zh-CN/architecture_explained.md +116 -0
  102. package/docs/zh-CN/cli.md +109 -0
  103. package/docs/zh-CN/dsr.md +91 -0
  104. package/docs/zh-CN/graph_scenarios.md +173 -0
  105. package/docs/zh-CN/hooks.md +14 -0
  106. package/docs/zh-CN/manifests.md +136 -0
  107. package/docs/zh-CN/mcp.md +205 -0
  108. package/docs/zh-CN/quickstart.md +35 -0
  109. package/docs/zh-CN/rules.md +7 -0
  110. package/docs/zh-CN/technical-details.md +454 -0
  111. package/docs/zh-CN/troubleshooting.md +19 -0
  112. package/docs/zh-CN/windows-setup.md +67 -0
  113. package/install.sh +183 -0
  114. package/package.json +97 -0
  115. package/skills/git-ai-mcp/SKILL.md +86 -0
  116. package/skills/git-ai-mcp/references/constraints.md +143 -0
  117. package/skills/git-ai-mcp/references/tools.md +263 -0
  118. package/templates/agents/common/documents/Fix EISDIR error and enable multi-language indexing.md +14 -0
  119. package/templates/agents/common/documents/Fix git-ai index error in CodaGraph directory.md +13 -0
  120. package/templates/agents/common/skills/git-ai-mcp/SKILL.md +86 -0
  121. package/templates/agents/common/skills/git-ai-mcp/references/constraints.md +143 -0
  122. package/templates/agents/common/skills/git-ai-mcp/references/tools.md +263 -0
@@ -0,0 +1,205 @@
1
+ # MCP Server 接入
2
+
3
+ `git-ai` 提供了一个基于 MCP (Model Context Protocol) 的 stdio Server,供 Agent (如 Claude Desktop, Trae 等) 调用,赋予 Agent "理解代码库"的能力。
4
+
5
+ ## 启动
6
+
7
+ 在任意目录执行:
8
+
9
+ ```bash
10
+ git-ai ai serve
11
+ ```
12
+
13
+ 该进程是 stdio 模式(会等待客户端连接)。你可以把它配置到支持 MCP 的客户端里。
14
+
15
+ ## 工具列表
16
+
17
+ ### 仓库管理
18
+ - `get_repo({ path })`:返回指定 `path` 对应的仓库根目录与扫描根目录(调试用)
19
+
20
+ ### 索引管理
21
+ - `check_index({ path })`:检查索引结构是否与当前版本一致(不一致需重建索引)
22
+ - `rebuild_index({ path, dim?, overwrite? })`:重建全量索引(写入 `.git-ai/`;Risk: high)
23
+ - `pack_index({ path, lfs? })`:打包索引为 `.git-ai/lancedb.tar.gz`(可选启用 git-lfs track)
24
+ - `unpack_index({ path })`:解包索引归档
25
+
26
+ ### 检索
27
+ - `search_symbols({ path, query, mode?, case_insensitive?, max_candidates?, limit?, lang?, with_repo_map?, repo_map_max_files?, repo_map_max_symbols?, wiki_dir? })`:符号检索(lang: auto/all/java/ts/python/go/rust/c/markdown/yaml;可选附带 repo_map)
28
+ - `semantic_search({ path, query, topk?, lang?, with_repo_map?, repo_map_max_files?, repo_map_max_symbols?, wiki_dir? })`:基于 LanceDB + SQ8 的语义检索(lang: auto/all/java/ts/python/go/rust/c/markdown/yaml;可选附带 repo_map)
29
+ - `repo_map({ path, max_files?, max_symbols?, wiki_dir? })`:生成 repo map(重要文件/符号排名、引导 Wiki 阅读)
30
+ - `ast_graph_find({ path, prefix, limit?, lang? })`:按名字前缀查找符号定义(大小写不敏感;lang: auto/all/java/ts)
31
+ - `ast_graph_children({ path, id, as_file? })`:列出包含关系的直接子节点(文件→顶层符号、类→方法等)
32
+ - `ast_graph_refs({ path, name, limit?, lang? })`:按名字查引用位置(call/new/type;lang: auto/all/java/ts)
33
+ - `ast_graph_callers({ path, name, limit?, lang? })`:按名字查调用者(callee name;lang: auto/all/java/ts)
34
+ - `ast_graph_callees({ path, name, limit?, lang? })`:按名字查被调用者(caller name;lang: auto/all/java/ts)
35
+ - `ast_graph_chain({ path, name, direction?, max_depth?, limit?, lang? })`:按名字查调用链路(upstream/downstream,最大深度;lang: auto/all/java/ts)
36
+ - `ast_graph_query({ path, query, params? })`:对 AST 图数据库执行 CozoScript 查询(进阶)
37
+
38
+ ### 文件读取
39
+ - `list_files({ path, pattern?, limit? })`:按 glob 列文件(默认忽略 node_modules, .git 等)
40
+ - `read_file({ path, file, start_line?, end_line? })`:按行读取文件片段
41
+
42
+ ### DSR (Deterministic Semantic Record)
43
+ - `dsr_context({ path })`:获取仓库 Git 上下文和 DSR 目录状态
44
+ - 返回:commit_hash, repo_root, branch, detached, dsr_directory_state
45
+ - `dsr_generate({ path, commit })`:为指定提交生成 DSR
46
+ - 返回:commit_hash, file_path, existed, counts, semantic_change_type, risk_level
47
+ - `dsr_rebuild_index({ path })`:从 DSR 文件重建索引,加速查询
48
+ - 返回:enabled, engine, dbPath, counts
49
+ - `dsr_symbol_evolution({ path, symbol, start?, all?, limit?, contains? })`:追溯符号变更历史
50
+ - 返回:ok, hits(包含 commit_hash, semantic_change_type, risk_level, operations)
51
+
52
+ ## DSR 使用示例
53
+
54
+ 获取仓库 Git 状态和 DSR 情况:
55
+
56
+ ```js
57
+ dsr_context({ path: "/ABS/PATH/TO/REPO" })
58
+ ```
59
+
60
+ 为最近几次提交生成 DSR:
61
+
62
+ ```js
63
+ dsr_generate({ path: "/ABS/PATH/TO/REPO", commit: "HEAD" })
64
+ dsr_generate({ path: "/ABS/PATH/TO/REPO", commit: "HEAD~1" })
65
+ ```
66
+
67
+ 查询某个函数的变更历史:
68
+
69
+ ```js
70
+ dsr_symbol_evolution({ path: "/ABS/PATH/TO/REPO", symbol: "handleRequest", limit: 50 })
71
+ ```
72
+
73
+ 模糊匹配符号名称:
74
+
75
+ ```js
76
+ dsr_symbol_evolution({ path: "/ABS/PATH/TO/REPO", symbol: "Request", contains: true, limit: 100 })
77
+ ```
78
+
79
+ ## AST 图查询示例
80
+
81
+ 列出指定文件里的顶层符号(推荐:无需手动算 file_id):
82
+
83
+ ```js
84
+ ast_graph_children({ path: "/ABS/PATH/TO/REPO", id: "src/mcp/server.ts", as_file: true })
85
+ ```
86
+
87
+ 查询某个方法/函数的调用者(推荐:用 callers/callees/chain,不用手写 CozoScript):
88
+
89
+ ```js
90
+ ast_graph_callers({ path: "/ABS/PATH/TO/REPO", name: "greet", limit: 50 })
91
+ ast_graph_chain({ path: "/ABS/PATH/TO/REPO", name: "greet", direction: "upstream", max_depth: 3 })
92
+ ```
93
+
94
+ 列出指定文件里的顶层符号(进阶:直接写 CozoScript,需要 file_id):
95
+
96
+ ```cozo
97
+ ?[file_id] <- [[$file_id]]
98
+ ?[child_id, name, kind, start_line, end_line] :=
99
+ *ast_contains{parent_id: file_id, child_id},
100
+ *ast_symbol{ref_id: child_id, file, name, kind, signature, start_line, end_line}
101
+ ```
102
+
103
+ ## 推荐调用方式(让 Agent 自动传对路径)
104
+ - MCP tools 的 `path` 为必传:每次工具调用都必须显式传 `path: "/ABS/PATH/TO/REPO"`(保证调用原子性)
105
+
106
+ ## RepoMap 使用建议
107
+
108
+ repo map 用于给 Agent 一个"全局鸟瞰 + 导航入口"(重要文件/符号 + Wiki 关联),建议作为分析前置步骤:
109
+
110
+ ```js
111
+ repo_map({ path: "/ABS/PATH/TO/REPO", max_files: 20, max_symbols: 5 })
112
+ ```
113
+
114
+ 如果你希望在一次检索结果里顺带附加 repo map(默认关闭,避免输出膨胀):
115
+
116
+ ```js
117
+ search_symbols({ path: "/ABS/PATH/TO/REPO", query: "Foo", limit: 20, with_repo_map: true, repo_map_max_files: 20, repo_map_max_symbols: 5 })
118
+ semantic_search({ path: "/ABS/PATH/TO/REPO", query: "where is auth handled", topk: 5, with_repo_map: true })
119
+ ```
120
+
121
+ ## Agent Skills / Rules
122
+
123
+ 本仓库提供了 Agent 可直接复用的 Skill/Rule 模板,旨在让 Agent 能够遵循最佳实践来使用上述工具。
124
+
125
+ 这些模板文档(Markdown/YAML)会被索引,便于 Agent 通过 `semantic_search` 检索 MCP 规则与技能说明。
126
+
127
+ ### YAML 格式模板
128
+
129
+ - **Skill**: [`templates/agents/common/skills/git-ai/skill.yaml`](../../templates/agents/common/skills/git-ai/skill.yaml) - 指导 Agent 如何使用 git-ai 的 Git-native 语义体系(包含 DSR 约束)与 MCP 工具
130
+ - 包含:触发条件、工作流步骤、工具定义、输出要求、常见陷阱
131
+
132
+ - **Rule**: [`templates/agents/common/rules/git-ai.yaml`](../../templates/agents/common/rules/git-ai.yaml) - 约束 Agent 使用 git-ai MCP 的行为
133
+ - 包含:必须遵守的规则、推荐策略、禁止事项、Git Hooks 规则、Manifest Workspace 规则
134
+
135
+ ### Markdown 模版(便于直接阅读/复制)
136
+
137
+ - **Skill**: [`templates/agents/common/skills/git-ai-mcp/SKILL.md`](../../templates/agents/common/skills/git-ai-mcp/SKILL.md)
138
+ - **Rule**: [`templates/agents/common/rules/git-ai-mcp/RULE.md`](../../templates/agents/common/rules/git-ai-mcp/RULE.md)
139
+
140
+ ### 安装到 Trae
141
+
142
+ 将本仓库的 Skills 和 Rules 安装到当前项目的 `.agents` 目录(默认):
143
+
144
+ ```bash
145
+ cd /path/to/your-repo
146
+ git-ai ai agent install
147
+ git-ai ai agent install --overwrite
148
+ git-ai ai agent install --to /custom/location/.agents
149
+ ```
150
+
151
+ 如果你希望安装到 Trae 的 `.trae` 目录:
152
+
153
+ ```bash
154
+ git-ai ai agent install --agent trae
155
+ ```
156
+
157
+ ### Skill 工作流概览
158
+
159
+ 根据 `skill.yaml`,推荐的工作流程:
160
+
161
+ 1. **首次接触仓库** - 使用 `repo_map` 获取全局视图
162
+ 2. **检查索引状态** - 使用 `check_index`,必要时 `rebuild_index`
163
+ 3. **定位目标代码** - 使用 `search_symbols` 或 `semantic_search`
164
+ 4. **理解代码关系** - 使用 `ast_graph_callers/callees/chain`
165
+ 5. **追溯变更历史** - 使用 `dsr_symbol_evolution`
166
+ 6. **精读代码** - 使用 `read_file` 读取关键片段
167
+ 7. **提供建议** - 基于完整理解给出修改建议
168
+
169
+ ### Rule 约束概览
170
+
171
+ 根据 `rule.yaml`,Agent 必须遵守:
172
+
173
+ - **explicit_path**: 每次调用必须显式传 `path` 参数
174
+ - **check_index_first**: 符号搜索前必须检查索引
175
+ - **understand_before_modify**: 修改前必须先理解实现
176
+ - **use_dsr_for_history**: 追溯历史必须使用 DSR 工具
177
+ - **respect_dsr_risk**: 重视 DSR 报告的 high 风险变更
178
+
179
+ 文档/规则检索建议:
180
+ - 当问题涉及 MCP/Skill/Rule 时,先用 `semantic_search` 定位对应文档片段,再给出结论。
181
+
182
+ 禁止事项包括:
183
+ - 假设符号位置而不搜索
184
+ - 直接修改未读过的文件
185
+ - 手动解析 git 历史
186
+ - 在索引缺失时进行符号搜索
187
+ - 省略 `path` 参数
188
+
189
+ ## DSR 与 MCP 的关系
190
+
191
+ - **MCP tools** 主要覆盖"索引(.git-ai)构建与检索",用于让 Agent 低成本定位证据
192
+ - **DSR** 是"按提交的语义工件(.git-ai/dsr)",用于语义历史/演化类查询
193
+ - DSR 是 per-commit、immutable、deterministic 的,可用于精确追溯符号变更
194
+ - 任何历史遍历都必须从 Git DAG 出发(DSR 只 enrich 节点,不定义边)
195
+
196
+ DSR 相关命令见:[DSR 文档](./dsr.md)
197
+
198
+ ## 输出要求
199
+
200
+ Agent 使用 git-ai MCP 工具时应遵循:
201
+
202
+ 1. **先给结论,再给证据** - 先总结发现,再提供详细位置
203
+ 2. **使用 IDE 可点击链接** - 格式:`file:///path/to/file#L10-L20`
204
+ 3. **最小改动原则** - 建议修改时避免引入新依赖
205
+ 4. **证据必须基于 read_file** - 不要基于假设或猜测
@@ -0,0 +1,35 @@
1
+ # 安装与快速开始
2
+
3
+ ## 全局安装
4
+
5
+ ```bash
6
+ npm i -g git-ai
7
+ ```
8
+
9
+ ## 在任意仓库生成索引
10
+
11
+ ```bash
12
+ cd /path/to/repo
13
+ git-ai ai status
14
+ git-ai ai index --overwrite
15
+ ```
16
+
17
+ 后续如果你只想对变更文件做快速更新(例如配合 git hooks 在提交前重建索引),可以使用增量模式:
18
+
19
+ ```bash
20
+ git-ai ai index --incremental --staged
21
+ ```
22
+
23
+ ## 语义检索/符号检索
24
+
25
+ ```bash
26
+ git-ai ai query Indexer --limit 10
27
+ git-ai ai semantic "where do we open lancedb" --topk 5
28
+ ```
29
+
30
+ ## 归档与分享
31
+
32
+ ```bash
33
+ git-ai ai pack
34
+ git-ai ai unpack
35
+ ```
@@ -0,0 +1,7 @@
1
+ # Rules
2
+
3
+ ## 规则
4
+ - 优先用 git-ai MCP;若 `index_status` 可用先检查;不可用才用传统命令。
5
+ - 索引只针对当前 HEAD;不在查询接口显式建模版本/分支。
6
+ - 索引数据可被 Git 管理;不写入敏感信息。
7
+ - TypeScript strict;不引入外部 embedding;CLI 输出尽量 JSON。
@@ -0,0 +1,454 @@
1
+ # 技术细节
2
+
3
+ 本文档包含 git-ai 的详细技术说明,适合需要深入了解实现细节的开发者。
4
+
5
+ ## Git 代理模式
6
+
7
+ `git-ai` 默认行为与 `git` 保持一致,可以作为 `git` 的直接替代品:
8
+
9
+ ```bash
10
+ git-ai init
11
+ git-ai status
12
+ git-ai add -A
13
+ git-ai commit -m "msg"
14
+ git-ai push -u origin main
15
+ ```
16
+
17
+ 所有不包含 `ai` 子命令的调用都会被转发到系统 `git`。
18
+
19
+ ## AI 子命令完整列表
20
+
21
+ 所有 AI 相关能力放在 `git-ai ai` 下:
22
+
23
+ ### 索引管理
24
+
25
+ ```bash
26
+ # 查看索引状态
27
+ git-ai ai status
28
+
29
+ # 重建索引(覆盖模式)
30
+ git-ai ai index --overwrite
31
+
32
+ # 增量索引(仅索引暂存区文件)
33
+ git-ai ai index --incremental --staged
34
+ ```
35
+
36
+ ### 查询操作
37
+
38
+ ```bash
39
+ # 符号检索(支持多种模式)
40
+ git-ai ai query Indexer --limit 10
41
+
42
+ # 语义搜索
43
+ git-ai ai semantic "semantic search" --topk 5
44
+
45
+ # AST 图查询
46
+ git-ai ai graph find GitAIV2MCPServer
47
+ git-ai ai graph callers functionName
48
+ git-ai ai graph callees functionName
49
+ git-ai ai graph chain functionName --max-depth 3
50
+ ```
51
+
52
+ ### DSR 操作
53
+
54
+ ```bash
55
+ # 获取 Git 上下文和 DSR 状态
56
+ git-ai ai dsr context --json
57
+
58
+ # 为指定提交生成 DSR
59
+ git-ai ai dsr generate HEAD
60
+
61
+ # 从 DSR 文件重建索引
62
+ git-ai ai dsr rebuild-index
63
+
64
+ # 查询符号演变历史
65
+ git-ai ai dsr query symbol-evolution GitAIV2MCPServer --limit 200 --json
66
+ ```
67
+
68
+ ### 索引打包
69
+
70
+ ```bash
71
+ # 打包索引归档
72
+ git-ai ai pack
73
+
74
+ # 使用 Git LFS 打包
75
+ git-ai ai pack --lfs
76
+
77
+ # 解包索引归档
78
+ git-ai ai unpack
79
+ ```
80
+
81
+ ### MCP Server
82
+
83
+ ```bash
84
+ # 启动 MCP Server(stdio 模式)
85
+ git-ai ai serve
86
+ ```
87
+
88
+ ## DSR(Deterministic Semantic Record)
89
+
90
+ DSR 是按提交(per-commit)、不可变、确定性的语义工件。
91
+
92
+ ### 核心特性
93
+
94
+ - **按提交存储**:每个 Git 提交对应一个 DSR 文件
95
+ - **不可变性**:DSR 文件一旦生成永不修改
96
+ - **确定性**:相同的代码和提交总是生成相同的 DSR
97
+ - **可重建**:数据库/索引仅为可删缓存,必须可由 DSR + Git 重建
98
+
99
+ ### 文件结构
100
+
101
+ ```
102
+ .git-ai/
103
+ ├── dsr/
104
+ │ ├── <commit_hash_1>.json
105
+ │ ├── <commit_hash_2>.json
106
+ │ └── ...
107
+ ├── lancedb/ # 向量数据库(可删缓存)
108
+ ├── cozodb/ # 图数据库(可删缓存)
109
+ ├── lancedb.tar.gz # 索引归档
110
+ └── meta.json # 元数据
111
+ ```
112
+
113
+ ### DSR 文件格式
114
+
115
+ 每个 DSR 文件包含该提交的完整语义信息:
116
+
117
+ ```json
118
+ {
119
+ "commit_hash": "abc123...",
120
+ "timestamp": "2024-01-01T00:00:00Z",
121
+ "files": [
122
+ {
123
+ "path": "src/main.ts",
124
+ "symbols": [
125
+ {
126
+ "name": "functionName",
127
+ "kind": "function",
128
+ "location": {"line": 10, "column": 0},
129
+ "signature": "functionName(arg1: string): void"
130
+ }
131
+ ]
132
+ }
133
+ ]
134
+ }
135
+ ```
136
+
137
+ ## MCP Server 详细说明
138
+
139
+ ### 工具列表
140
+
141
+ `git-ai ai serve` 提供以下 MCP 工具:
142
+
143
+ #### 1. `check_index`
144
+
145
+ 检查索引是否就绪。
146
+
147
+ **参数**:
148
+ - `path` (string, required): 仓库根路径
149
+
150
+ **返回**:
151
+ ```json
152
+ {
153
+ "ok": true,
154
+ "indexed": true,
155
+ "commit_hash": "abc123...",
156
+ "file_count": 1234,
157
+ "symbol_count": 5678
158
+ }
159
+ ```
160
+
161
+ #### 2. `repo_map`
162
+
163
+ 获取仓库全局视图。
164
+
165
+ **参数**:
166
+ - `path` (string, required): 仓库根路径
167
+ - `max_files` (number, optional): 最大文件数,默认 20
168
+ - `max_symbols` (number, optional): 每个文件最大符号数,默认 5
169
+
170
+ #### 3. `search_symbols`
171
+
172
+ 符号检索(支持多种模式)。
173
+
174
+ **参数**:
175
+ - `path` (string, required): 仓库根路径
176
+ - `query` (string, required): 查询字符串
177
+ - `mode` (string, optional): 搜索模式,可选值:`substring`、`prefix`、`wildcard`、`regex`、`fuzzy`,默认 `substring`
178
+ - `limit` (number, optional): 返回结果数,默认 50
179
+
180
+ #### 4. `semantic_search`
181
+
182
+ 基于 LanceDB + SQ8 的语义检索。
183
+
184
+ **参数**:
185
+ - `path` (string, required): 仓库根路径
186
+ - `query` (string, required): 自然语言查询
187
+ - `topk` (number, optional): 返回结果数,默认 10
188
+
189
+ #### 5. `ast_graph_query`
190
+
191
+ 基于 CozoDB 的 AST 图查询(CozoScript)。
192
+
193
+ **参数**:
194
+ - `path` (string, required): 仓库根路径
195
+ - `query` (string, required): CozoScript 查询语句
196
+ - `params` (object, optional): 查询参数
197
+
198
+ #### 6. `ast_graph_find`
199
+
200
+ 按名称前缀查找符号。
201
+
202
+ **参数**:
203
+ - `path` (string, required): 仓库根路径
204
+ - `prefix` (string, required): 符号名前缀
205
+ - `limit` (number, optional): 返回结果数,默认 50
206
+
207
+ #### 7. `ast_graph_callers`
208
+
209
+ 查找函数调用者。
210
+
211
+ **参数**:
212
+ - `path` (string, required): 仓库根路径
213
+ - `name` (string, required): 函数名
214
+ - `limit` (number, optional): 返回结果数,默认 200
215
+
216
+ #### 8. `ast_graph_callees`
217
+
218
+ 查找函数调用的其他函数。
219
+
220
+ **参数**:
221
+ - `path` (string, required): 仓库根路径
222
+ - `name` (string, required): 函数名
223
+ - `limit` (number, optional): 返回结果数,默认 200
224
+
225
+ #### 9. `ast_graph_chain`
226
+
227
+ 追踪完整调用链。
228
+
229
+ **参数**:
230
+ - `path` (string, required): 仓库根路径
231
+ - `name` (string, required): 函数名
232
+ - `direction` (string, optional): 追踪方向,可选值:`downstream`、`upstream`,默认 `downstream`
233
+ - `max_depth` (number, optional): 最大深度,默认 3
234
+ - `limit` (number, optional): 返回结果数,默认 500
235
+
236
+ #### 10. `read_file`
237
+
238
+ 读取文件内容。
239
+
240
+ **参数**:
241
+ - `path` (string, required): 仓库根路径
242
+ - `file` (string, required): 文件相对路径
243
+ - `start_line` (number, optional): 起始行号,默认 1
244
+ - `end_line` (number, optional): 结束行号,默认 200
245
+
246
+ #### 11. `dsr_context`
247
+
248
+ 获取仓库 Git 上下文和 DSR 目录状态。
249
+
250
+ **参数**:
251
+ - `path` (string, required): 仓库根路径
252
+
253
+ **返回**:
254
+ ```json
255
+ {
256
+ "ok": true,
257
+ "commit_hash": "abc123...",
258
+ "repo_root": "/path/to/repo",
259
+ "branch": "main",
260
+ "detached": false,
261
+ "dsr_directory_state": {
262
+ "total_commits": 100,
263
+ "indexed_commits": 95,
264
+ "missing_commits": ["def456...", "ghi789..."]
265
+ }
266
+ }
267
+ ```
268
+
269
+ #### 12. `dsr_generate`
270
+
271
+ 为指定提交生成 DSR。
272
+
273
+ **参数**:
274
+ - `path` (string, required): 仓库根路径
275
+ - `commit` (string, optional): 提交哈希,默认 HEAD
276
+
277
+ #### 13. `dsr_rebuild_index`
278
+
279
+ 从 DSR 文件重建索引。
280
+
281
+ **参数**:
282
+ - `path` (string, required): 仓库根路径
283
+
284
+ #### 14. `dsr_symbol_evolution`
285
+
286
+ 追溯符号变更历史。
287
+
288
+ **参数**:
289
+ - `path` (string, required): 仓库根路径
290
+ - `symbol` (string, required): 符号名
291
+ - `start` (string, optional): 起始提交哈希
292
+ - `all` (boolean, optional): 是否返回所有历史,默认 false
293
+ - `limit` (number, optional): 返回结果数,默认 200
294
+ - `contains` (string, optional): 仅返回包含此提交的分支
295
+
296
+ ### 调用约定
297
+
298
+ **重要**:所有 MCP 工具调用必须显式传递 `path` 参数。
299
+
300
+ ```json
301
+ {
302
+ "name": "search_symbols",
303
+ "arguments": {
304
+ "path": "/absolute/path/to/repo",
305
+ "query": "functionName"
306
+ }
307
+ }
308
+ ```
309
+
310
+ 禁止依赖进程状态或工作目录隐式推断仓库位置。
311
+
312
+ ## Git Hooks 详细说明
313
+
314
+ ### 安装 Hooks
315
+
316
+ ```bash
317
+ git-ai ai hooks install
318
+ ```
319
+
320
+ ### Hook 行为
321
+
322
+ #### pre-commit
323
+
324
+ 自动执行以下操作:
325
+ 1. 增量索引暂存区文件:`index --incremental --staged`
326
+ 2. 打包索引:`pack`
327
+ 3. 将 `.git-ai/meta.json` 与 `.git-ai/lancedb.tar.gz` 加入暂存区
328
+
329
+ **注意**:索引内容以暂存区为准,确保提交的索引与代码一致。
330
+
331
+ #### pre-push
332
+
333
+ 再次执行 `pack`,若归档发生变化则阻止 push,提示先提交归档文件。
334
+
335
+ 这确保了每次 push 都包含最新的索引归档。
336
+
337
+ #### post-checkout / post-merge
338
+
339
+ 若存在 `.git-ai/lancedb.tar.gz` 则自动执行 `unpack`。
340
+
341
+ 这确保了切换分支或合并后,索引自动更新。
342
+
343
+ ### 查看 Hook 状态
344
+
345
+ ```bash
346
+ git-ai ai hooks status
347
+ ```
348
+
349
+ ## Git LFS 集成
350
+
351
+ ### 为什么需要 Git LFS
352
+
353
+ 索引归档 `.git-ai/lancedb.tar.gz` 可能较大(取决于项目规模),直接存入 Git 历史会导致:
354
+ - 仓库体积膨胀
355
+ - 克隆速度变慢
356
+ - Git 操作性能下降
357
+
358
+ 使用 Git LFS 可以有效管理这些大文件。
359
+
360
+ ### 开启 Git LFS(一次性)
361
+
362
+ ```bash
363
+ # 安装 Git LFS(如果未安装)
364
+ git lfs install
365
+
366
+ # 跟踪索引归档
367
+ git lfs track ".git-ai/lancedb.tar.gz"
368
+
369
+ # 提交 .gitattributes
370
+ git add .gitattributes
371
+ git commit -m "chore: track lancedb archive via git-lfs"
372
+ ```
373
+
374
+ ### 使用 git-ai 触发 LFS
375
+
376
+ ```bash
377
+ # 打包并自动使用 LFS(如果已安装 git-lfs)
378
+ git-ai ai pack --lfs
379
+ ```
380
+
381
+ ### 克隆/切分支后
382
+
383
+ 如果你环境设置了 `GIT_LFS_SKIP_SMUDGE=1`,或发现 `.git-ai/lancedb.tar.gz` 不是有效的 gzip 文件:
384
+
385
+ ```bash
386
+ git lfs pull
387
+ ```
388
+
389
+ ## Agent 模版详细说明
390
+
391
+ ### Skill 模版
392
+
393
+ Skill 模版定义了 Agent 如何使用 git-ai 工具的最佳实践。
394
+
395
+ **位置**:`templates/agents/common/skills/git-ai-mcp/SKILL.md`
396
+
397
+ **核心内容**:
398
+ - 推荐工作流程(7 步)
399
+ - 工具选择指南
400
+ - 最佳实践
401
+ - 常见陷阱
402
+
403
+ ### Rule 模版
404
+
405
+ Rule 模版定义了 Agent 使用 git-ai 工具时的行为约束。
406
+
407
+ **位置**:`templates/agents/common/rules/git-ai-mcp/RULE.md`
408
+
409
+ **核心内容**:
410
+ - 必须遵守的规则(must_follow)
411
+ - 推荐的做法(recommended)
412
+ - 禁止的行为(prohibited)
413
+ - 工具使用约束(tool_usage_constraints)
414
+
415
+ ### 安装 Agent 模版
416
+
417
+ ```bash
418
+ # 安装到当前仓库的默认位置
419
+ git-ai ai agent install
420
+
421
+ # 覆盖已存在的模版
422
+ git-ai ai agent install --overwrite
423
+
424
+ # 安装到自定义位置
425
+ git-ai ai agent install --to /custom/location/.agents
426
+
427
+ # 为特定 Agent 安装(如 Trae)
428
+ git-ai ai agent install --agent trae
429
+ ```
430
+
431
+ ## 性能指标
432
+
433
+ ### 索引速度
434
+
435
+ - 1k 文件:< 5 秒
436
+ - 10k 文件:< 30 秒
437
+ - 100k 文件:< 5 分钟
438
+
439
+ ### 查询速度
440
+
441
+ - 符号检索:< 10ms
442
+ - 语义搜索:< 100ms
443
+ - AST 图查询:< 50ms
444
+
445
+ ### 存储占用
446
+
447
+ - DSR 文件:约 1-5 MB / 1k 文件
448
+ - LanceDB 索引:约 10-50 MB / 1k 文件
449
+ - CozoDB 索引:约 5-20 MB / 1k 文件
450
+ - 打包归档:约 15-75 MB / 1k 文件
451
+
452
+ ## 故障排查
453
+
454
+ 详见 [故障排查指南](./troubleshooting.md)。