homunculus-code 0.1.0 → 0.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.
- package/ARCHITECTURE.md +211 -0
- package/README.md +57 -17
- package/bin/cli.js +36 -0
- package/bin/init.js +28 -203
- package/bin/night.js +292 -0
- package/commands/hm-night.md +86 -0
- package/commands/hm-setup.md +65 -0
- package/commands/hm-status.md +47 -0
- package/examples/reference/README.md +13 -1
- package/examples/reference/evolved-skills/claude-code-reference/ch01-extensions.md +98 -0
- package/examples/reference/evolved-skills/claude-code-reference/ch02-agents.md +90 -0
- package/examples/reference/evolved-skills/claude-code-reference/ch03-hooks.md +104 -0
- package/examples/reference/evolved-skills/claude-code-reference/ch04-mcp.md +78 -0
- package/examples/reference/evolved-skills/claude-code-reference/ch05-ui.md +161 -0
- package/examples/reference/evolved-skills/claude-code-reference/ch06-rules-security.md +301 -0
- package/examples/reference/evolved-skills/claude-code-reference/ch07-model-memory.md +203 -0
- package/examples/reference/evolved-skills/claude-code-reference/ch08-workflows.md +148 -0
- package/examples/reference/evolved-skills/claude-code-reference/ch09-integrations.md +502 -0
- package/examples/reference/evolved-skills/claude-code-reference/ch10-cli-sdk.md +269 -0
- package/examples/reference/evolved-skills/claude-code-reference/ch11-output-versions.md +172 -0
- package/examples/reference/evolved-skills/claude-code-reference/index.md +134 -0
- package/package.json +2 -2
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
# ch06: Rules / Plugins / 權限系統 / Sandboxing / 安全
|
|
2
|
+
|
|
3
|
+
## 9. Path-Scoped Rules
|
|
4
|
+
|
|
5
|
+
```markdown
|
|
6
|
+
<!-- CLAUDE.md -->
|
|
7
|
+
<!-- 只在特定目錄生效 -->
|
|
8
|
+
<path-rules>
|
|
9
|
+
<rule path="src/legacy/**">
|
|
10
|
+
這個目錄是舊代碼,改動要保守。
|
|
11
|
+
</rule>
|
|
12
|
+
<rule path="scripts/**">
|
|
13
|
+
這裡的腳本要用 zsh,不用 bash。
|
|
14
|
+
</rule>
|
|
15
|
+
</path-rules>
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## 10. Plugins 系統
|
|
21
|
+
|
|
22
|
+
```
|
|
23
|
+
my-plugin/
|
|
24
|
+
├── .claude-plugin/plugin.json ← name, version, description
|
|
25
|
+
├── skills/<name>/SKILL.md
|
|
26
|
+
├── agents/<name>.md
|
|
27
|
+
├── hooks/hooks.json
|
|
28
|
+
└── .mcp.json
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
用 `claude --plugin-dir ./my-plugin` 測試。
|
|
32
|
+
Standalone (`.claude/`) 先用,確定後打包成 plugin 分享。
|
|
33
|
+
|
|
34
|
+
### 官方 Marketplace 安裝(用戶端)
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
/plugin # Discover/Installed/Marketplaces/Errors 四個 tab
|
|
38
|
+
|
|
39
|
+
# LSP plugins(前置:export ENABLE_LSP_TOOL=1)
|
|
40
|
+
# ⚠️ typescript-lsp 有 bug #15235,改用 vtsls!
|
|
41
|
+
/plugin install vtsls@claude-code-lsps # TypeScript/JS
|
|
42
|
+
/plugin install gopls@claude-code-lsps # Go
|
|
43
|
+
/plugin install pyright-lsp@claude-code-lsps # Python
|
|
44
|
+
/plugin install rust-analyzer-lsp@claude-code-lsps # Rust
|
|
45
|
+
|
|
46
|
+
# 指令有 namespace
|
|
47
|
+
/commit-commands:commit # 不是直接 /commit
|
|
48
|
+
|
|
49
|
+
/reload-plugins # 不重啟就套用(LSP 需重啟)
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
**🌟 Code Intelligence Plugins(最高價值)**
|
|
53
|
+
安裝對應 Language Server 後,Claude 獲得:
|
|
54
|
+
- **自動診斷**:每次 edit 後 LSP 自動報告型別錯誤 → Claude 即時修復(不需手動跑 compiler)
|
|
55
|
+
- **程式碼導航**:go-to-definition、find-references、type info
|
|
56
|
+
|
|
57
|
+
| 語言 | Plugin 安裝名 | 需要 binary |
|
|
58
|
+
|------|-------------|-------------|
|
|
59
|
+
| TypeScript | `vtsls` ⚠️(非 typescript-lsp) | `typescript-language-server` |
|
|
60
|
+
| Python | `pyright-lsp` | `pyright-langserver` |
|
|
61
|
+
| Rust | `rust-analyzer-lsp` | `rust-analyzer` |
|
|
62
|
+
| Go | `gopls` | `gopls` |
|
|
63
|
+
|
|
64
|
+
> `Ctrl+O` 在 "diagnostics found" 時顯示 inline 錯誤
|
|
65
|
+
|
|
66
|
+
**常用整合**:`github`、`linear`、`sentry`、`figma`、`slack`
|
|
67
|
+
**工作流**:`commit-commands`(git commit/push/PR)、`pr-review-toolkit`
|
|
68
|
+
|
|
69
|
+
---
|
|
70
|
+
|
|
71
|
+
## 決策樹:選什麼擴展方式?
|
|
72
|
+
|
|
73
|
+
```
|
|
74
|
+
要在 Claude 工作時自動觸發某個行為?
|
|
75
|
+
└── Hooks(確定性、每次都跑)
|
|
76
|
+
|
|
77
|
+
要封裝可重用的工作流程?
|
|
78
|
+
├── 互動式 → Skills(用戶 /invoke 或 Claude 自動用)
|
|
79
|
+
└── 隔離執行 → Subagents
|
|
80
|
+
|
|
81
|
+
要連接外部工具(DB、API、Slack)?
|
|
82
|
+
└── MCP servers
|
|
83
|
+
|
|
84
|
+
要跨專案分享一組 skills + agents + hooks?
|
|
85
|
+
└── Plugin
|
|
86
|
+
|
|
87
|
+
要定期在 session 內執行某任務?
|
|
88
|
+
└── /loop 或 CronCreate
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
---
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
## 13. 權限系統
|
|
97
|
+
|
|
98
|
+
```json
|
|
99
|
+
// settings.json
|
|
100
|
+
{
|
|
101
|
+
"permissions": {
|
|
102
|
+
"allow": [
|
|
103
|
+
"Bash(npm run *)", "Bash(git commit *)", "Bash(* --version)",
|
|
104
|
+
"WebFetch(domain:github.com)",
|
|
105
|
+
"mcp__playwright"
|
|
106
|
+
],
|
|
107
|
+
"deny": [
|
|
108
|
+
"Bash(rm -rf *)", "Bash(git push --force *)",
|
|
109
|
+
"Agent(Explore)"
|
|
110
|
+
]
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
**Permission Modes**:`default` / `acceptEdits` / `plan` / `dontAsk` / `bypassPermissions`
|
|
116
|
+
|
|
117
|
+
**路徑規則**:`//` = 絕對路徑,`~/` = home,`/` = 專案根目錄,無前綴 = 相對 cwd
|
|
118
|
+
|
|
119
|
+
**優先順序(高到低)**:Managed → CLI args → Local project → Shared project → User
|
|
120
|
+
|
|
121
|
+
**互動管理**:`/permissions` — 查看/新增/移除 allow/deny 規則(無需手動編輯 JSON)
|
|
122
|
+
|
|
123
|
+
**Bash auto-approval 白名單(v2.1.72 擴充)**:`lsof`, `pgrep`, `tput`, `ss`, `fd`, `fdfind` 加入常見唯讀指令白名單,減少確認提示。
|
|
124
|
+
|
|
125
|
+
---
|
|
126
|
+
|
|
127
|
+
## 14. Sandboxing(OS 層級隔離)
|
|
128
|
+
|
|
129
|
+
OS 層級限制 Bash 的 filesystem 和 network 存取,解決 permission 批准疲勞。
|
|
130
|
+
- **macOS**:Seatbelt;**Linux/WSL2**:bubblewrap(需 `apt install bubblewrap socat`)
|
|
131
|
+
- 所有 subprocess 繼承相同安全邊界
|
|
132
|
+
|
|
133
|
+
```
|
|
134
|
+
/sandbox # 開啟模式選擇(Auto-allow / Regular permissions)
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Auto-allow 模式下,sandbox 內的 Bash 自動執行不需確認(含檔案修改)。
|
|
138
|
+
|
|
139
|
+
### 設定
|
|
140
|
+
|
|
141
|
+
```json
|
|
142
|
+
{
|
|
143
|
+
"sandbox": {
|
|
144
|
+
"enabled": true,
|
|
145
|
+
"filesystem": {
|
|
146
|
+
"allowWrite": ["~/.kube", "//tmp/build"],
|
|
147
|
+
"denyWrite": ["~/.bashrc", "~/.zshrc"],
|
|
148
|
+
"denyRead": ["//Users/jinx/.ssh"]
|
|
149
|
+
},
|
|
150
|
+
"network": {
|
|
151
|
+
"allowedDomains": ["github.com", "npmjs.com"]
|
|
152
|
+
}
|
|
153
|
+
},
|
|
154
|
+
"allowUnsandboxedCommands": false
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### 路徑前綴規則
|
|
159
|
+
|
|
160
|
+
| 前綴 | 含義 | 範例 |
|
|
161
|
+
|------|------|------|
|
|
162
|
+
| `//` | 絕對路徑(不展開 `~`)| `//tmp/build` → `/tmp/build` |
|
|
163
|
+
| `~/` | home 相對 | `~/.kube` → `$HOME/.kube` |
|
|
164
|
+
| `/` | settings 檔案目錄相對 | `/build` |
|
|
165
|
+
| `./` 或無 | 相對於 cwd | `./output` |
|
|
166
|
+
|
|
167
|
+
多個 settings scope 的 `allowWrite` 會**合併**,不是覆蓋。
|
|
168
|
+
|
|
169
|
+
### 預設行為
|
|
170
|
+
|
|
171
|
+
- **寫入**:只允許 cwd 及子目錄
|
|
172
|
+
- **讀取**:整個系統(除 denyRead)
|
|
173
|
+
- **Network**:只允許明確列出的 domains
|
|
174
|
+
|
|
175
|
+
### 弱化網路隔離(開發用途)
|
|
176
|
+
|
|
177
|
+
```json
|
|
178
|
+
{ "sandboxEnableWeakerNetworkIsolation": true }
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
讓 sandbox 內可存取 localhost(docker daemon、local DB、dev server)。
|
|
182
|
+
⚠️ 降低網路安全邊界,僅在受信任環境使用。
|
|
183
|
+
|
|
184
|
+
### 安全優勢 — Prompt Injection 防護
|
|
185
|
+
|
|
186
|
+
Sandbox + Permissions = **Defense-in-depth**:
|
|
187
|
+
- **Permissions**:控制 Claude 能用哪些工具(所有工具)
|
|
188
|
+
- **Sandbox**:OS 層級強制限制 Bash 能存取的資源(只針對 Bash)
|
|
189
|
+
|
|
190
|
+
即使攻擊者操控 Claude,sandbox 仍阻止修改 `~/.bashrc`、向外部洩漏資料、下載惡意腳本。
|
|
191
|
+
|
|
192
|
+
已知限制:廣域 domain(如 `github.com`)可被利用做 data exfiltration;Unix sockets 可能 bypass sandbox。
|
|
193
|
+
|
|
194
|
+
### 逃生出口
|
|
195
|
+
|
|
196
|
+
sandbox 限制導致命令失敗時,Claude 可能嘗試在 sandbox 外重試(需用戶確認)。
|
|
197
|
+
禁止此行為:`"allowUnsandboxedCommands": false`
|
|
198
|
+
|
|
199
|
+
### DevContainer(完整隔離替代方案)
|
|
200
|
+
|
|
201
|
+
比 `/sandbox` 更強的 container 層級隔離,適合 CI/CD 或不信任的程式碼:
|
|
202
|
+
- 內建 firewall:只允許 npm/GitHub/Claude API
|
|
203
|
+
- 適合搭配 `--dangerously-skip-permissions`
|
|
204
|
+
- 組成:`devcontainer.json` + `Dockerfile` + `init-firewall.sh`
|
|
205
|
+
- ⚠️ 無法防止惡意 repo 竊取 container 內的 credentials
|
|
206
|
+
|
|
207
|
+
### 內建 Write 邊界(不需 /sandbox)
|
|
208
|
+
|
|
209
|
+
即使未啟用 sandbox,Claude Code 有預設規則:
|
|
210
|
+
- **寫入**只能到啟動目錄及子目錄 + `allowedDirectories`
|
|
211
|
+
- **讀取**可存取工作目錄外(系統函式庫、shared configs)
|
|
212
|
+
|
|
213
|
+
### 已知限制
|
|
214
|
+
|
|
215
|
+
- 不相容:`watchman`(jest 用 `--no-watchman`)、`docker`(加入 `excludedCommands`)
|
|
216
|
+
- **macOS crontab**:`crontab -e/-r` 在 sandbox 內會掛起(系統 crontab DB 寫入被限制)→ 改用 launchd
|
|
217
|
+
|
|
218
|
+
---
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
---
|
|
222
|
+
|
|
223
|
+
## 33. 安全最佳實踐速查
|
|
224
|
+
|
|
225
|
+
### 防護機制(自動啟用)
|
|
226
|
+
|
|
227
|
+
- 寫入限制:只能寫 project 啟動目錄及子目錄
|
|
228
|
+
- WebFetch 獨立 context,防 prompt injection
|
|
229
|
+
- 指令黑名單:curl/wget 需明確批准
|
|
230
|
+
- Suspicious Bash Detection:已批准的指令若行為可疑,仍需手動批准
|
|
231
|
+
- Git push 限制在當前分支
|
|
232
|
+
|
|
233
|
+
### ConfigChange Hook(審計設定變更)
|
|
234
|
+
|
|
235
|
+
```json
|
|
236
|
+
{
|
|
237
|
+
"hooks": {
|
|
238
|
+
"ConfigChange": [{
|
|
239
|
+
"matcher": "permissions.allow",
|
|
240
|
+
"hooks": [{"type": "command", "command": "logger 'Permission added: '"}]
|
|
241
|
+
}]
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
### Hooks vs CLAUDE.md
|
|
247
|
+
|
|
248
|
+
- **CLAUDE.md** = 建議(Claude 可能在 context 不足時忽略)
|
|
249
|
+
- **Hooks** = 保證(每次觸發一定執行)
|
|
250
|
+
- → lint/format/block migrations:用 Hook,不要只寫 CLAUDE.md
|
|
251
|
+
|
|
252
|
+
### Context 80% 效能懸崖
|
|
253
|
+
|
|
254
|
+
LLM 在 context 80%+ 時智能**明顯下降**(不只是大小限制)。
|
|
255
|
+
→ 不要等到撞上限才 `/clear`,80% 就應處理。
|
|
256
|
+
|
|
257
|
+
### CLAUDE.md 精修法
|
|
258
|
+
|
|
259
|
+
每行問:**「移除這條,Claude 會出錯嗎?」** 不會 → 刪掉。
|
|
260
|
+
目標 < 200 行;API docs 用 link 而非直接貼入。
|
|
261
|
+
|
|
262
|
+
### Fan-out 安全模式
|
|
263
|
+
|
|
264
|
+
```bash
|
|
265
|
+
for file in $(cat files.txt); do
|
|
266
|
+
claude -p "Migrate $file" \
|
|
267
|
+
--allowedTools "Edit,Bash(git commit *)" # 鎖定工具防 injection
|
|
268
|
+
done
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
### 資料安全注意事項
|
|
272
|
+
|
|
273
|
+
- **傳輸**:TLS 加密(prompt、code、output 傳送過程安全)
|
|
274
|
+
- **靜態儲存**:⚠️ **不加密**(at rest NOT encrypted)— 企業合規/HIPAA 需注意
|
|
275
|
+
- **本地 session 快取**:30 天(可配置)
|
|
276
|
+
- 禁用遙測:`CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1`
|
|
277
|
+
|
|
278
|
+
### /sandbox 隔離
|
|
279
|
+
|
|
280
|
+
```bash
|
|
281
|
+
/sandbox # macOS Seatbelt,限制 fs/network
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
Sandbox 配置(settings.json):
|
|
285
|
+
```json
|
|
286
|
+
{
|
|
287
|
+
"sandbox": {
|
|
288
|
+
"filesystem": {
|
|
289
|
+
"allowWrite": ["//tmp/build"],
|
|
290
|
+
"denyRead": ["~/.aws/credentials"]
|
|
291
|
+
},
|
|
292
|
+
"network": { "allowedDomains": ["github.com"] }
|
|
293
|
+
},
|
|
294
|
+
"sandboxEnableWeakerNetworkIsolation": true, // 允許存取 localhost(開發用)
|
|
295
|
+
// v2.1.69:macOS only,讓 gh/gcloud/terraform 等 Go 工具在自訂 MITM proxy 下驗證 TLS
|
|
296
|
+
"sandbox": { "enableWeakerNetworkIsolation": true } // 同上,新格式
|
|
297
|
+
}
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
---
|
|
301
|
+
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
# ch07: Model 設定 / 記憶系統進階 / 成本管理
|
|
2
|
+
|
|
3
|
+
## 15. Model 設定與 opusplan
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
/model opusplan # Plan Mode 用 Opus,執行自動切 Sonnet
|
|
7
|
+
/model sonnet[1m] # 1M token context window
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
| 別名 | 行為 |
|
|
11
|
+
|------|------|
|
|
12
|
+
| `sonnet` | 日常任務(預設推薦)|
|
|
13
|
+
| `opus` | 複雜推理 |
|
|
14
|
+
| `haiku` | 快速簡單任務 |
|
|
15
|
+
| **`opusplan`** | **Plan=Opus,Execute=Sonnet(最佳 cost/quality)** |
|
|
16
|
+
| `sonnet[1m]` | 超大 context(>200K tokens 按 extra 計費)|
|
|
17
|
+
|
|
18
|
+
**Effort Level**(Opus 4.6 / Sonnet 4.6;v2.1.72 起 `max` 移除):
|
|
19
|
+
```bash
|
|
20
|
+
CLAUDE_CODE_EFFORT_LEVEL=low # ○ 快速便宜
|
|
21
|
+
CLAUDE_CODE_EFFORT_LEVEL=medium # ◐ 預設(Opus 4.6 Max/Team)
|
|
22
|
+
CLAUDE_CODE_EFFORT_LEVEL=high # ● 深度推理
|
|
23
|
+
/model # 用方向鍵調整,顯示 ○◐● 圖示
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
**Fast Mode + Low Effort = 最大速度**(適合快速迭代)
|
|
27
|
+
|
|
28
|
+
**Cloud Provider 模型固定**(Bedrock/Vertex/Foundry 必須設定,否則新版本上線可能 break):
|
|
29
|
+
```bash
|
|
30
|
+
export ANTHROPIC_DEFAULT_OPUS_MODEL="us.anthropic.claude-opus-4-5"
|
|
31
|
+
export ANTHROPIC_DEFAULT_SONNET_MODEL="us.anthropic.claude-sonnet-4-6"
|
|
32
|
+
export ANTHROPIC_DEFAULT_HAIKU_MODEL="us.anthropic.claude-haiku-3-5"
|
|
33
|
+
```
|
|
34
|
+
不設定則 alias 解析到最新版,Anthropic 上線新 model 時可能破壞未啟用的帳號。
|
|
35
|
+
|
|
36
|
+
**`modelOverrides` setting**(v2.1.73):將 model picker 項目對應到自訂 provider model ID:
|
|
37
|
+
```json
|
|
38
|
+
{
|
|
39
|
+
"modelOverrides": {
|
|
40
|
+
"opus": "arn:aws:bedrock:us-east-1::foundation-model/anthropic.claude-opus-4-6-v1:0"
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
用途:Bedrock inference profile ARN、自訂 gateway endpoint 等。
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
## 17. 記憶系統進階
|
|
52
|
+
|
|
53
|
+
| 層級 | 路徑 | 說明 |
|
|
54
|
+
|------|------|------|
|
|
55
|
+
| Managed | `/Library/Application Support/ClaudeCode/CLAUDE.md` | 組織全體(不可排除)|
|
|
56
|
+
| User | `~/.claude/CLAUDE.md` | 個人偏好(所有專案)|
|
|
57
|
+
| Project | `./CLAUDE.md` | 團隊共享(commit 進 git)|
|
|
58
|
+
| **Local** | **`./CLAUDE.local.md`** | **個人私有(自動加入 .gitignore)** |
|
|
59
|
+
| Auto | `~/.claude/projects/<repo>/memory/` | Claude 自動寫(前 200 行);`/memory` 管理;`"autoMemoryEnabled": false` 停用;`"autoMemoryDirectory": "path"` 自訂路徑(v2.1.74)|
|
|
60
|
+
|
|
61
|
+
**Path-Scoped Rules**(節省 context 的關鍵):
|
|
62
|
+
```markdown
|
|
63
|
+
---
|
|
64
|
+
paths:
|
|
65
|
+
- "src/api/**/*.ts"
|
|
66
|
+
- "tests/**/*.test.ts"
|
|
67
|
+
---
|
|
68
|
+
# 只在處理這些檔案時載入的規則
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
**Monorepo 排除**:
|
|
72
|
+
```json
|
|
73
|
+
{"claudeMdExcludes": ["**/other-team/CLAUDE.md"]}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
**InstructionsLoaded hook**:診斷哪些 instruction 在何時載入(偵錯用)
|
|
77
|
+
|
|
78
|
+
**Rules symlink**:`ln -s ~/shared-rules .claude/rules/shared`(多專案共享)
|
|
79
|
+
|
|
80
|
+
**CLAUDE.md Import**(組合多個檔案):
|
|
81
|
+
```markdown
|
|
82
|
+
See @README.md for project overview and @package.json for npm commands.
|
|
83
|
+
|
|
84
|
+
# Additional Instructions
|
|
85
|
+
- Git workflow: @docs/git-instructions.md
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
**`/init`**:自動從現有 codebase 生成 starter CLAUDE.md(偵測 build system/test framework/patterns)
|
|
89
|
+
|
|
90
|
+
**CLAUDE.md 應包含 vs 不應包含**:
|
|
91
|
+
- ✅ 包含:Claude 無法從代碼猜到的 bash commands、不同於 default 的 code style、測試指令、PR 規範
|
|
92
|
+
- ❌ 排除:Claude 已知的標準慣例、頻繁變動的資訊、代碼庫的逐檔說明
|
|
93
|
+
|
|
94
|
+
**強調語法**:加 "IMPORTANT" 或 "YOU MUST" 改善 adherence(CLAUDE.md 太長時規則容易被忽略)
|
|
95
|
+
|
|
96
|
+
**HTML 注解隱藏(v2.1.72)**:`<!-- 這段說明只給人看,不消耗 token -->` 不會注入 model context,但 `@CLAUDE.md` 讀取時仍可見。適合在 CLAUDE.md 中加人類閱讀的說明而不浪費 token。
|
|
97
|
+
|
|
98
|
+
**Compaction 客製化**(在 CLAUDE.md 中):
|
|
99
|
+
```markdown
|
|
100
|
+
When compacting, always preserve the full list of modified files and any test commands.
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
**`/compact <instructions>`**:指定 focus 壓縮,如 `/compact Focus on the API changes`
|
|
104
|
+
|
|
105
|
+
---
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
---
|
|
109
|
+
|
|
110
|
+
## 18. 成本管理
|
|
111
|
+
|
|
112
|
+
**平均費用**:$6/開發者/天(90% 用戶 < $12)
|
|
113
|
+
|
|
114
|
+
**最有效的成本控制**:
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
# 任務切換:先命名再清,之後可復原
|
|
118
|
+
/rename "feature-auth"
|
|
119
|
+
/clear
|
|
120
|
+
/resume feature-auth # 需要時復原
|
|
121
|
+
|
|
122
|
+
# MCP 工具自動延遲載入(超過 5% context 就 defer)
|
|
123
|
+
# settings.json: {"env": {"ENABLE_TOOL_SEARCH": "auto:5"}}
|
|
124
|
+
|
|
125
|
+
# 降低 extended thinking 費用(Sonnet/pre-Opus-4.6 適用)
|
|
126
|
+
MAX_THINKING_TOKENS=8000 # 預設 31999
|
|
127
|
+
MAX_THINKING_TOKENS=0 # 完全停用思考(Opus 4.6 已改用 adaptive,此設仍有效)
|
|
128
|
+
|
|
129
|
+
# CI/CD 成本上限(超過就中止,不要用 --no-session-persistence)
|
|
130
|
+
claude -p "..." --max-budget-usd 2.00 # 超過 $2 自動停止
|
|
131
|
+
|
|
132
|
+
# Hook 預處理:10000 行 log → grep ERROR → 幾百行
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
**Haiku 適用場景**(比 Sonnet 便宜 ~80%):
|
|
136
|
+
- Subagent 重複性任務:格式轉換、資料萃取、分類
|
|
137
|
+
- Eval 評估(/eval-skill 明確用 Haiku)
|
|
138
|
+
- 批次處理(指令明確、輸出格式固定)
|
|
139
|
+
- 設定:skill frontmatter 的 `model: haiku`,或 `ANTHROPIC_DEFAULT_CLAUDE_MODEL=claude-haiku-4-5`
|
|
140
|
+
|
|
141
|
+
**高成本陷阱**:
|
|
142
|
+
- Agent Teams plan mode ≈ 7x tokens
|
|
143
|
+
- MCP server 即使不用也佔 context
|
|
144
|
+
- `/compact` 指令要加 focus(保留關鍵內容)
|
|
145
|
+
|
|
146
|
+
**CLAUDE.md → Skills**:詳細工作流移到 skills(按需載入),CLAUDE.md 保持 < 200 行
|
|
147
|
+
|
|
148
|
+
**Team Rate Limit 建議(TPM/人)**:
|
|
149
|
+
| 人數 | TPM/人 | RPM/人 |
|
|
150
|
+
|------|--------|--------|
|
|
151
|
+
| 1-5 | 200k-300k | 5-7 |
|
|
152
|
+
| 5-20 | 100k-150k | 2.5-3.5 |
|
|
153
|
+
| 20-50 | 50k-75k | 1.25-1.75 |
|
|
154
|
+
| 50-100 | 25k-35k | 0.62-0.87 |
|
|
155
|
+
| 100+ | 10k-20k | 0.25-0.47 |
|
|
156
|
+
|
|
157
|
+
Bedrock/Vertex/Foundry 無原生費用追蹤 → 用 **LiteLLM Proxy** 按 key 追蹤費用。
|
|
158
|
+
|
|
159
|
+
---
|
|
160
|
+
|
|
161
|
+
## 決策樹:選什麼擴展方式?
|
|
162
|
+
|
|
163
|
+
```
|
|
164
|
+
要在 Claude 工作時自動觸發某個行為?
|
|
165
|
+
└── Hooks(確定性、每次都跑)
|
|
166
|
+
|
|
167
|
+
要封裝可重用的工作流程?
|
|
168
|
+
├── 互動式 → Skills(用戶 /invoke 或 Claude 自動用)
|
|
169
|
+
└── 隔離執行 → Subagents
|
|
170
|
+
|
|
171
|
+
要多個 agent 互相討論/協作?
|
|
172
|
+
└── Agent Teams(實驗性,需啟用)
|
|
173
|
+
|
|
174
|
+
要連接外部工具(DB、API、Slack)?
|
|
175
|
+
└── MCP servers
|
|
176
|
+
|
|
177
|
+
要跨專案分享一組 skills + agents + hooks?
|
|
178
|
+
└── Plugin
|
|
179
|
+
|
|
180
|
+
要定期在 session 內執行某任務?
|
|
181
|
+
└── /loop 或 CronCreate
|
|
182
|
+
|
|
183
|
+
要快速 Opus 回應(犧牲費用)?
|
|
184
|
+
└── /fast(Fast Mode)
|
|
185
|
+
|
|
186
|
+
要安全的自主執行環境?
|
|
187
|
+
└── /sandbox(Sandboxing)
|
|
188
|
+
|
|
189
|
+
要超大規模平行批次變更(50+ 檔)?
|
|
190
|
+
└── /batch(每個任務獨立 worktree + PR)
|
|
191
|
+
|
|
192
|
+
完成功能後要清理代碼品質?
|
|
193
|
+
└── /simplify(平行 3 agents 審查)
|
|
194
|
+
|
|
195
|
+
要自訂互動快捷鍵(stash/editor/mode 切換)?
|
|
196
|
+
└── Keybindings(~/.claude/keybindings.json,/keybindings 管理)
|
|
197
|
+
|
|
198
|
+
要在瀏覽器測試 web app / 抽取資料 / 錄 GIF?
|
|
199
|
+
└── Chrome 整合(claude --chrome,共享登入狀態)
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
---
|
|
203
|
+
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
# ch08: 進階工作流模式 + Best Practices
|
|
2
|
+
|
|
3
|
+
## 16. 進階工作流模式
|
|
4
|
+
|
|
5
|
+
**Interview Pattern**(大型功能前):
|
|
6
|
+
```text
|
|
7
|
+
I want to build X. Interview me using the AskUserQuestion tool.
|
|
8
|
+
Keep interviewing until covered, then write a spec to SPEC.md.
|
|
9
|
+
```
|
|
10
|
+
→ 收到 spec 後開新 session 執行(乾淨 context)
|
|
11
|
+
|
|
12
|
+
**Writer/Reviewer 雙 Session**:
|
|
13
|
+
- Session A 實作,Session B 用 `@file` 引用做 review
|
|
14
|
+
- 或 A 寫 tests,B 寫 passing code
|
|
15
|
+
|
|
16
|
+
**Fan-out 批次**:
|
|
17
|
+
```bash
|
|
18
|
+
for file in $(cat files.txt); do
|
|
19
|
+
claude -p "Migrate $file" --allowedTools "Edit,Bash(git commit *)"
|
|
20
|
+
done
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
**Pipe 資料**:`cat error.log | claude`、`claude -p "..." --output-format json | jq`
|
|
24
|
+
|
|
25
|
+
**@ 引用**:`Look at @src/auth.ts and @docs/policy.md, then implement...`
|
|
26
|
+
|
|
27
|
+
**Session 命名**(`/rename`):
|
|
28
|
+
```text
|
|
29
|
+
/rename "oauth-migration" ← 像 git branch 一樣命名,方便 --resume 找
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
**Claude Code 除錯配置確認**:
|
|
33
|
+
```text
|
|
34
|
+
/status ← 確認 proxy/gateway 設定是否正確套用
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
## 決策樹:選什麼擴展方式?
|
|
40
|
+
|
|
41
|
+
```
|
|
42
|
+
要在 Claude 工作時自動觸發某個行為?
|
|
43
|
+
└── Hooks(確定性、每次都跑)
|
|
44
|
+
|
|
45
|
+
要封裝可重用的工作流程?
|
|
46
|
+
├── 互動式 → Skills(用戶 /invoke 或 Claude 自動用)
|
|
47
|
+
└── 隔離執行 → Subagents
|
|
48
|
+
|
|
49
|
+
要多個 agent 互相討論/協作?
|
|
50
|
+
└── Agent Teams(實驗性,需啟用)
|
|
51
|
+
|
|
52
|
+
要連接外部工具(DB、API、Slack)?
|
|
53
|
+
└── MCP servers
|
|
54
|
+
|
|
55
|
+
要跨專案分享一組 skills + agents + hooks?
|
|
56
|
+
└── Plugin
|
|
57
|
+
|
|
58
|
+
要定期在 session 內執行某任務?
|
|
59
|
+
└── /loop 或 CronCreate
|
|
60
|
+
|
|
61
|
+
要快速 Opus 回應(犧牲費用)?
|
|
62
|
+
└── /fast(Fast Mode)
|
|
63
|
+
|
|
64
|
+
要安全的自主執行環境?
|
|
65
|
+
└── /sandbox(Sandboxing)
|
|
66
|
+
|
|
67
|
+
要超大規模平行批次變更(50+ 檔)?
|
|
68
|
+
└── /batch(每個任務獨立 worktree + PR)
|
|
69
|
+
|
|
70
|
+
完成功能後要清理代碼品質?
|
|
71
|
+
└── /simplify(平行 3 agents 審查)
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
---
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
---
|
|
78
|
+
|
|
79
|
+
## 最佳實踐摘要
|
|
80
|
+
|
|
81
|
+
1. **CLAUDE.md 保持精簡**:只放 Claude 不能從代碼猜到的規則,多餘的規則會被忽略
|
|
82
|
+
2. **提供驗證手段**:測試/截圖/輸出讓 Claude 自我校驗(最高槓桿)
|
|
83
|
+
3. **Context 是最貴資源**:用 `/clear` 清理、用 subagent 隔離探索、用 Summarize from here 壓縮
|
|
84
|
+
4. **Explore → Plan → Code → Commit**:Plan Mode 分離探索與實作
|
|
85
|
+
5. **大型 migration 用 fan-out**:`claude -p "..." --allowedTools` 批次處理
|
|
86
|
+
6. **安全性雙層**:Permissions(工具層)+ Sandboxing(OS 層)同時使用
|
|
87
|
+
7. **Path-scoped rules**:詳細指令用 `paths:` frontmatter 按需載入,節省 context
|
|
88
|
+
8. **先命名後清理**:`/rename` → `/clear` → `/resume`,不丟失 session 歷史
|
|
89
|
+
9. **自訂快捷鍵**:`chat:stash`(Ctrl+S)暫存 prompt、`chat:externalEditor`(Ctrl+E)編輯長 prompt
|
|
90
|
+
10. **MCP scope 注意**:`local`(個人本專案)、`project`(.mcp.json 共享)、`user`(跨專案)
|
|
91
|
+
11. **診斷優先**:`/doctor` 全面檢查(安裝/MCP/keybindings/context 警告),`/bug` 回報問題
|
|
92
|
+
12. **CLAUDE.md 精簡**:`/init` 生成起點,只保留 Claude 無法從代碼猜到的規則;太長時規則被忽略
|
|
93
|
+
13. **Context 是最貴資源(二)**:2 次糾正後 `/clear` 重來;subagent 做探索(保護主 context);`/compact Focus on X` 聚焦壓縮
|
|
94
|
+
14. **`ultrathink`** keyword:當次 turn 使用 high effort 思考(不改全局),用於 one-off 複雜問題
|
|
95
|
+
|
|
96
|
+
---
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
---
|
|
100
|
+
|
|
101
|
+
## 29. 工作流最佳實踐(Best Practices)
|
|
102
|
+
|
|
103
|
+
### 5 個常見反模式
|
|
104
|
+
|
|
105
|
+
| 反模式 | 症狀 | 修法 |
|
|
106
|
+
|--------|------|------|
|
|
107
|
+
| 廚房水槽 Session | 多個不相關任務堆在同一 context | `/clear` 分隔任務 |
|
|
108
|
+
| 無限糾正迴圈 | 糾正兩次仍錯 | `/clear` + 重寫更精確的 prompt |
|
|
109
|
+
| 臃腫 CLAUDE.md | 太長導致 Claude 忽略規則 | 每行問「刪掉會犯錯嗎?」 |
|
|
110
|
+
| 信任後驗證落差 | 實作看似對但邊界 case 未處理 | 永遠提供驗證(tests/screenshots)|
|
|
111
|
+
| 無限探索 | Claude 讀幾百個檔案,context 爆 | 用 subagent 做調查 |
|
|
112
|
+
|
|
113
|
+
### 驗證策略(最高槓桿)
|
|
114
|
+
```
|
|
115
|
+
❌ "implement email validation"
|
|
116
|
+
✅ "write validateEmail. test cases: user@example.com=true, invalid=false. run tests after"
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### Session 管理
|
|
120
|
+
```bash
|
|
121
|
+
claude --continue # 繼續最近 session
|
|
122
|
+
claude --resume # 選擇 session(picker)
|
|
123
|
+
claude --resume oauth-migration # 指定名稱
|
|
124
|
+
/rename oauth-migration # 給當前 session 命名
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
**v2.1.70 改進**:修復 skill listing 重複注入;`--resume` 省 ~600 tokens(不重複載入已在 context 的 skills)。
|
|
128
|
+
|
|
129
|
+
### Worktree 工作流(v2.1.72)
|
|
130
|
+
```bash
|
|
131
|
+
claude --worktree feature-a # 建立 git worktree 並進入
|
|
132
|
+
# 完成後在 session 內使用 ExitWorktree 工具離開
|
|
133
|
+
# 或 /plan fix the auth bug ← plan mode 可直接帶描述引數
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### CLAUDE.md 精要
|
|
137
|
+
|
|
138
|
+
| ✅ 放進去 | ❌ 不要放 |
|
|
139
|
+
|-----------|----------|
|
|
140
|
+
| Claude 無法猜到的 bash 命令 | 讀程式碼就知道的事 |
|
|
141
|
+
| 非預設的 code style | 標準語言慣例 |
|
|
142
|
+
| Test runner 和測試指令 | 詳細 API 文件(改用連結)|
|
|
143
|
+
| 開發環境 quirks | 「寫乾淨的程式碼」等自明慣例 |
|
|
144
|
+
|
|
145
|
+
CLAUDE.md 太長時 Claude 會忽略重要規則。`IMPORTANT`/`YOU MUST` 強化關鍵規則。
|
|
146
|
+
|
|
147
|
+
---
|
|
148
|
+
|