Justin-lee commited on
Commit
ecbfaff
·
verified ·
1 Parent(s): 58db331

Add Claude Code vs CodePilot gap analysis

Browse files
Files changed (1) hide show
  1. GAP_ANALYSIS.md +253 -0
GAP_ANALYSIS.md ADDED
@@ -0,0 +1,253 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # CodePilot vs Claude Code — 差距分析報告
2
+
3
+ > 基於論文 "Dive into Claude Code" (arXiv:2604.14228) 的完整架構分析
4
+
5
+ ## 現狀:CodePilot 已有的功能 ✅ (37/37)
6
+
7
+ ### 核心工具
8
+ - ✅ read_file(帶行號、offset/limit)
9
+ - ✅ edit_file(精確字串替換、diff 顯示)
10
+ - ✅ write_file(建立/覆寫)
11
+ - ✅ run_command(超時保護、安全檢查)
12
+ - ✅ search_files(ripgrep/grep)
13
+ - ✅ list_files(遞迴搜尋、排除 .git 等)
14
+ - ✅ git_status(branch、status、log)
15
+
16
+ ### 記憶系統
17
+ - ✅ L1 CODEPILOT.md 指令層級(遞迴搜尋)
18
+ - ✅ L2 MEMORY.md 跨 session 記憶
19
+ - ✅ L3 Session JSONL 持久化
20
+ - ✅ L4 自動壓縮(9段摘要)
21
+ - ✅ FileStateCache(read-before-edit 強制)
22
+ - ✅ 文件未變更去重(UNCHANGED_STUB)
23
+ - ✅ HTML 註解移除
24
+
25
+ ### 模型/訓練
26
+ - ✅ 6 種模型後端(local、codex、openrouter、anthropic、openai、ollama)
27
+ - ✅ Duel 模式 + DPO 自動配對
28
+ - ✅ 蒸餾模式
29
+ - ✅ LeetCode 自動刷題
30
+ - ✅ KTO / SFT / DPO 訓練
31
+
32
+ ---
33
+
34
+ ## 差距:Claude Code 有但 CodePilot 缺少的 ❌
35
+
36
+ ### 優先級 P0(影響最大,應該先做)
37
+
38
+ #### 1. ❌ /init 初始化指令
39
+ Claude Code 有 `claude init` 指令,自動分析專案結構並產生 CLAUDE.md。
40
+
41
+ **缺少的**:CodePilot 需要用戶手動建立 CODEPILOT.md,新用戶不知道該寫什麼。
42
+
43
+ **實作建議**:
44
+ ```python
45
+ # /init 指令:讀取專案結構,用模型自動產生 CODEPILOT.md
46
+ def cmd_init(tools, model):
47
+ files = tools.list_files("*", max_depth=2)
48
+ git = tools.git_context()
49
+ # 讀取幾個關鍵檔案
50
+ readme = tools.read_file("README.md") if exists("README.md") else ""
51
+ pkg = tools.read_file("package.json") if exists("package.json") else ""
52
+ req = tools.read_file("requirements.txt") if exists("requirements.txt") else ""
53
+
54
+ prompt = f"""Analyze this project and generate a CODEPILOT.md file.
55
+ Files: {files}
56
+ Git: {git}
57
+ README: {readme[:2000]}
58
+ Config: {pkg or req}
59
+
60
+ Generate markdown with: tech stack, coding conventions, test commands, key files."""
61
+
62
+ codepilot_md = model.chat([{"role":"user","content":prompt}])
63
+ tools.write_file("CODEPILOT.md", codepilot_md)
64
+ ```
65
+
66
+ #### 2. ❌ 子代理(Sub-agents)
67
+ Claude Code 有 6 種內建子代理:Explore、Plan、Verification、General、Guide、Statusline。
68
+
69
+ **缺少的**:CodePilot 只有單一主循環,無法平行處理或分工。
70
+
71
+ **最重要的子代理**:
72
+ - **Explore agent**:只能讀/搜尋,不能寫。用於深入調查問題。
73
+ - **Verification agent**:完成任務後自動跑測試驗證。
74
+ - **Plan agent**:先產生計劃,用戶確認後才執行。
75
+
76
+ **實作建議**:
77
+ ```python
78
+ def run_subagent(model, task, tools, allowed_tools=None, deny_tools=None):
79
+ """在隔離的 context 中執行子任務"""
80
+ sub_messages = [
81
+ {"role": "system", "content": f"You are a sub-agent. Task: {task}"},
82
+ ]
83
+ # 跑獨立的工具循環,只回傳摘要
84
+ result = agent_loop(model, sub_messages, tools, max_rounds=5,
85
+ allowed_tools=allowed_tools)
86
+ return summarize(result)
87
+ ```
88
+
89
+ #### 3. ❌ 5 層漸進壓縮(Graduated Compaction)
90
+ Claude Code 有 5 層壓縮,從輕到重依序觸發:
91
+ 1. Budget reduction(每個工具結果限制大小)
92
+ 2. Snip(裁剪舊歷史)
93
+ 3. Microcompact(細粒度快取壓縮)
94
+ 4. Context collapse(讀取時虛擬投影)
95
+ 5. Auto-compact(完整摘要)
96
+
97
+ **缺少的**:CodePilot 只有第 5 層(直接摘要),前 4 層都沒有。
98
+
99
+ **最該先加的**:Budget reduction — 限制每個工具結果的長度。
100
+
101
+ **實作建議**:
102
+ ```python
103
+ # 在 execute_tool 後加入
104
+ MAX_TOOL_RESULT_TOKENS = 3000 # ~12KB
105
+
106
+ def truncate_tool_result(result, max_chars=12000):
107
+ if len(result) > max_chars:
108
+ return result[:max_chars//2] + f"\n\n... ({len(result)} chars total, truncated) ...\n\n" + result[-max_chars//4:]
109
+ return result
110
+ ```
111
+
112
+ #### 4. ❌ 錯誤恢復機制
113
+ Claude Code 有:
114
+ - Max output token 自動升級(最多重試 3 次)
115
+ - Prompt-too-long 自動壓縮重試
116
+ - Streaming fallback
117
+ - Fallback model 切換
118
+
119
+ **缺少的**:CodePilot 工具失敗或模型錯誤時只顯示錯誤訊息,沒有自動恢復。
120
+
121
+ **實作建議**:
122
+ ```python
123
+ for attempt in range(3):
124
+ try:
125
+ response = model.chat(messages)
126
+ break
127
+ except ContextTooLong:
128
+ messages = compact_messages(messages, model.chat) # 壓縮重試
129
+ except ModelError:
130
+ if fallback_model:
131
+ model = fallback_model # 切換備用模型
132
+ ```
133
+
134
+ ### 優先級 P1(重要但可以稍後做)
135
+
136
+ #### 5. ❌ 權限/審批系統
137
+ Claude Code 有 7 種權限模式:plan、default、auto-edit、full-auto、auto、deny、bubble。
138
+
139
+ **缺少的**:CodePilot 的 edit_file 和 run_command 都是直接執行,沒有確認步驟。
140
+
141
+ **實作建議**:
142
+ ```python
143
+ APPROVAL_MODES = {
144
+ "ask": "每次工具��叫都問用戶", # 最安全
145
+ "auto-edit": "文件編輯自動,指令要問",
146
+ "auto": "全部自動(危險指令除外)",
147
+ }
148
+ # 加入 --approval ask|auto-edit|auto 參數
149
+ ```
150
+
151
+ #### 6. ❌ 背景任務管理
152
+ Claude Code 的 BashTool 支援 `run_in_background`,長時間指令在背景跑。
153
+
154
+ **缺少的**:CodePilot 的 run_command 是阻塞的,超時就斷。
155
+
156
+ **實作建議**:
157
+ ```python
158
+ import threading
159
+
160
+ class BackgroundTask:
161
+ def __init__(self, command, cwd):
162
+ self.id = str(uuid.uuid4())[:8]
163
+ self.process = subprocess.Popen(command, shell=True, cwd=cwd,
164
+ stdout=subprocess.PIPE, stderr=subprocess.PIPE)
165
+
166
+ def check(self):
167
+ if self.process.poll() is not None:
168
+ return {"status": "done", "output": self.process.stdout.read().decode()}
169
+ return {"status": "running"}
170
+ ```
171
+
172
+ #### 7. ❌ Hooks 系統(27 種事件鉤子)
173
+ Claude Code 支援 pre/post tool execution hooks,可以:
174
+ - 在工具執行前/後注入自訂邏輯
175
+ - 自動 lint/format 修改後的文件
176
+ - 自動跑測試
177
+
178
+ **實作建議**:
179
+ ```python
180
+ # .codepilot/hooks.json
181
+ {
182
+ "post_edit_file": "black {file} && isort {file}",
183
+ "post_write_file": "black {file}",
184
+ "post_all_tools": "python -m pytest tests/ -x --tb=short"
185
+ }
186
+ ```
187
+
188
+ #### 8. ❌ 自訂代理(.codepilot/agents/*.md)
189
+ Claude Code 可以用 markdown 定義自訂代理,包含自己的工具集、模型、權限。
190
+
191
+ **實作建議**:
192
+ ```markdown
193
+ # .codepilot/agents/reviewer.md
194
+ ---
195
+ description: Code review agent
196
+ tools: [read_file, search_files, git_status]
197
+ disallowedTools: [write_file, edit_file, run_command]
198
+ model: anthropic/claude-sonnet-4
199
+ ---
200
+ You are a code reviewer. Read the code and provide feedback.
201
+ Never modify files directly.
202
+ ```
203
+
204
+ #### 9. ❌ Session Fork/Resume
205
+ Claude Code 支援 `--resume` 恢復之前的 session,以及 fork 分支對話。
206
+
207
+ **缺少的**:CodePilot 只能恢復最後一個 session。
208
+
209
+ #### 10. ❌ MCP(Model Context Protocol)整合
210
+ Claude Code 可以連接外部 MCP 伺服器(資料庫、API 等)。
211
+
212
+ ### 優先級 P2(錦上添花)
213
+
214
+ #### 11. ❌ WebFetch / WebSearch 工具
215
+ 讀取網頁或搜尋網路。
216
+
217
+ #### 12. ❌ 多模態支援
218
+ 讀取圖片、PDF、.ipynb notebook。
219
+
220
+ #### 13. ❌ Streaming 輸出
221
+ 逐字顯示模型回應,而不是等全部生成完才顯示。
222
+
223
+ #### 14. ❌ Shell 沙盒
224
+ OS 層級的 filesystem/network 隔離。
225
+
226
+ #### 15. ❌ ML 分類器判斷指令安全性
227
+ Claude Code 用 ML 模型判斷 bash 指令是否安全。
228
+
229
+ #### 16. ❌ 自動 Git Commit
230
+ 完成一組修改後自動 commit。
231
+
232
+ ---
233
+
234
+ ## 實作優先級排序
235
+
236
+ | 優先級 | 功能 | 工作量 | 價值 |
237
+ |--------|------|--------|------|
238
+ | **🔴 P0** | /init 自動產生 CODEPILOT.md | 小 | ⭐⭐⭐⭐⭐ |
239
+ | **🔴 P0** | 工具結果截斷(Budget reduction) | 小 | ⭐⭐⭐⭐⭐ |
240
+ | **🔴 P0** | 錯誤恢復(重試 + 壓縮 + fallback) | 中 | ⭐⭐⭐⭐⭐ |
241
+ | **🔴 P0** | Verification 子代理(自動跑測試) | 中 | ⭐⭐⭐⭐ |
242
+ | **🟡 P1** | 權限/審批系統 | 中 | ⭐⭐⭐⭐ |
243
+ | **🟡 P1** | Hooks(post-edit 自動 lint) | 小 | ⭐⭐⭐⭐ |
244
+ | **🟡 P1** | 背景任務 | 中 | ⭐⭐⭐ |
245
+ | **🟡 P1** | 自訂代理 (.codepilot/agents/) | 中 | ⭐⭐⭐ |
246
+ | **🟡 P1** | Session fork/resume | 小 | ⭐⭐⭐ |
247
+ | **🟡 P1** | 自動 Git Commit | 小 | ⭐⭐⭐ |
248
+ | **🟢 P2** | Streaming 輸出 | 中 | ⭐⭐⭐ |
249
+ | **🟢 P2** | WebFetch / WebSearch | 中 | ⭐⭐ |
250
+ | **🟢 P2** | MCP 整合 | 大 | ⭐⭐ |
251
+ | **🟢 P2** | 多模態(圖片/PDF) | 大 | ⭐⭐ |
252
+ | **🟢 P2** | Shell 沙盒 | 大 | ⭐ |
253
+ | **🟢 P2** | ML 安全分類器 | 大 | ⭐ |