homunculus-code 0.1.0 → 0.2.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.
@@ -0,0 +1,502 @@
1
+ # ch09: 外部整合(監控/Chrome/IDE/企業政策/GitLab/LLM Gateway/Plugin/Cloud/PR Review)
2
+
3
+ ## 21. 監控與 OTel
4
+
5
+ ```bash
6
+ export CLAUDE_CODE_ENABLE_TELEMETRY=1
7
+ export OTEL_METRICS_EXPORTER=console # 本地 debug
8
+ export OTEL_METRICS_EXPORTER=prometheus # Prometheus 整合
9
+ export OTEL_METRICS_EXPORTER=otlp # 完整 OTLP(Grafana/Datadog)
10
+ ```
11
+
12
+ **8 個 Metrics**:session count、LOC(added/removed)、PR count、commit count、**cost(USD)**、**tokens**(input/output/cache)、code edit decisions、active time
13
+
14
+ **5 個 Events**(OTel Logs):user_prompt、api_request(含 cost_usd、speed)、api_error、tool_result(含 duration_ms、bash_command)、tool_decision
15
+
16
+ **`prompt.id`**:UUID 追蹤單一 prompt 觸發的所有 API calls + tool results
17
+
18
+ **Analytics Dashboard(Teams/Enterprise)**:https://claude.ai/analytics/claude-code
19
+
20
+ ### PR 歸因(Analytics)
21
+
22
+ Merge 後自動標記 **`claude-code-assisted`** label(GitHub PR):
23
+ - 歸因窗口:merge 前 **21 天** + merge 後 **2 天**的 session activity
24
+ - 自動過濾:lock files、build artifacts、minified(>1000 chars/line)
25
+ - 保守估計:只計「高確信度」貢獻
26
+
27
+ ```bash
28
+ # 程式化查詢
29
+ gh pr list --label "claude-code-assisted" --state merged | wc -l
30
+ ```
31
+
32
+ > ⚠️ 只支援 GitHub(不支援 GitLab)
33
+
34
+ ---
35
+
36
+ ## 22. Chrome 瀏覽器整合
37
+
38
+ ```bash
39
+ claude --chrome # 啟用(單次)
40
+ /chrome # session 中切換 / 查看狀態 / 重連
41
+ ```
42
+
43
+ **前置條件**:
44
+ - Chrome 或 Edge(非 Brave/Arc)+ [Claude in Chrome extension](https://chromewebstore.google.com/detail/claude/fcoeoabgfenejglbffodgkkbkcdhcgfn) >= v1.0.36
45
+ - **必須直接 Anthropic 帳號**(❌ 不支援 Bedrock/Vertex/Foundry)
46
+
47
+ **核心能力**:
48
+ - Live debugging(讀 console errors + DOM,立即修代碼)
49
+ - 共享 browser login state(可存取 Gmail/Notion/任何已登入網站)
50
+ - GIF 錄製(`Record a GIF showing...`)
51
+ - 資料抽取(網頁 → CSV/JSON)
52
+ - 自動填表(從本地 CSV 讀取)
53
+
54
+ ```text
55
+ # 典型用法
56
+ > Open localhost:3000 and check the console for errors
57
+ > Record a GIF showing the checkout flow
58
+ > Fill the form with data from contacts.csv
59
+ ```
60
+
61
+ **對比 Playwright MCP**:
62
+ - `--chrome` = 簡單設定,共享登入,適合本地測試
63
+ - Playwright MCP = 精確控制,適合 CI/CD、headless
64
+
65
+ → **quest-board 本地開發優先 `--chrome`,CI/CD 用 Playwright**
66
+
67
+ ⚠️ 預設啟用會增加 context(browser tools 常駐),按需使用
68
+
69
+ ---
70
+
71
+ ## 23. IDE 整合:VS Code 與 JetBrains
72
+
73
+ ### VS Code Extension 快捷鍵
74
+
75
+ | 快捷鍵 | 功能 |
76
+ |--------|------|
77
+ | `Cmd+Esc` / `Ctrl+Esc` | Toggle focus:editor ↔ Claude |
78
+ | `Option+K` / `Alt+K` | Insert @-mention with file path + line numbers |
79
+ | `Cmd+Shift+Esc` | Open in new tab |
80
+
81
+ **@-mentions**(VS Code 專有):
82
+ ```text
83
+ @app.ts#5-10 ← Option+K 自動插入(含行號)
84
+ @terminal:name ← 引用 terminal 輸出
85
+ @browser ← Chrome 整合
86
+ ```
87
+
88
+ **Permission Modes**(設定 `claudeCode.initialPermissionMode`):
89
+ - `default`:每次詢問
90
+ - `plan`:先出計劃 markdown,你 inline comment 後再執行
91
+ - `acceptEdits`:自動接受修改
92
+ - `bypassPermissions`:全自動(devcontainer 環境才推薦)
93
+
94
+ **Remote Sessions**:Past Conversations → Remote tab → 繼續 claude.ai web session
95
+
96
+ **URI Handler**(外部開啟 session):
97
+ ```
98
+ vscode://anthropic.claude-code/open?prompt=<encoded>&session=<id>
99
+ ```
100
+ 從其他 app(Slack bot / CLI / 網頁)直接在 VS Code 觸發 Claude Code。
101
+
102
+ **Checkpoint(Rewind)**:hover 任何 message → rewind 按鈕 → Fork/Rewind code/Both
103
+
104
+ ### JetBrains Plugin(IntelliJ、PyCharm、WebStorm、GoLand 等)
105
+
106
+ **安裝**:JetBrains Marketplace 搜尋 "Claude Code Beta"
107
+
108
+ **快捷鍵**(JetBrains 特有):
109
+
110
+ | 快捷鍵 | 功能 |
111
+ |--------|------|
112
+ | `Cmd+Esc` / `Ctrl+Esc` | 開啟 Claude Code 面板 |
113
+ | `Cmd+Option+K` / `Alt+Ctrl+K` | 插入 file reference(@File#L1-99)|
114
+
115
+ **JetBrains 獨特功能**:
116
+ - **Diagnostic sharing**:IDE 的 lint/syntax 錯誤自動傳給 Claude
117
+ - **Selection context**:當前選取/tab 自動共享給 Claude
118
+ - **Diff viewer**:程式碼變更在 IDE 的 diff viewer 顯示(非 terminal)
119
+
120
+ **Remote Development 注意**:
121
+ - 必須在遠端 host 安裝 plugin(Settings → Plugin (Host))
122
+ - 不是安裝在本機 client
123
+
124
+ **ESC 鍵設定**(JetBrains terminal 問題):
125
+ ```
126
+ Settings → Tools → Terminal
127
+ → 取消勾選 "Move focus to the editor with Escape"
128
+ (否則 ESC 被 IDE 攔截,無法中斷 Claude)
129
+ ```
130
+
131
+ **安全注意**:auto-edit 模式可能修改 IDE config 檔案(自動執行風險)。
132
+
133
+ ### 共用(VS Code + JetBrains)
134
+
135
+ ```bash
136
+ claude --resume # IDE session 繼續到 CLI
137
+ /ide # Terminal Claude 連接到 IDE(啟用 diff viewer)
138
+ ```
139
+
140
+ ### Terminal 最佳化
141
+
142
+ - **Shift+Enter**(換行):iTerm2/WezTerm/Ghostty/Kitty 原生支援;其他用 `/terminal-setup`
143
+ - **Vim 模式**:`/vim` 或 `/config` 啟用(支援 hjkl/w/e/b/dd/yy 等常用指令)
144
+ - **通知**:完成後觸發 notification event(Kitty/Ghostty 原生;iTerm2 需設定)
145
+ - VS Code terminal 對超長貼上有截斷問題 → 改用 file-based 工作流
146
+
147
+ ---
148
+
149
+ ## 24. 企業政策管理(Server-Managed Settings)
150
+
151
+ **需求**:Teams/Enterprise 方案 + Claude Code >= 2.1.38
152
+
153
+ ### 兩種集中管理方式
154
+
155
+ | 方式 | 最適合 | 強度 |
156
+ |------|--------|------|
157
+ | Server-managed | 無 MDM / 非管理裝置 | 中(client-side) |
158
+ | Endpoint-managed | 有 MDM 的組織 | 高(OS 層級保護) |
159
+
160
+ 同時存在時:server-managed 優先;兩者都凌駕所有用戶設定和 CLI args。
161
+
162
+ ### 設定方式
163
+ Claude.ai → Admin Settings → Claude Code → Managed settings → 填 JSON
164
+
165
+ ```json
166
+ {
167
+ "permissions": {
168
+ "deny": ["Bash(curl *)", "Read(./.env)", "Read(./secrets/**)"]
169
+ },
170
+ "disableBypassPermissionsMode": "disable"
171
+ }
172
+ ```
173
+
174
+ ### 快取與推送
175
+ - 啟動時 + 每小時 polling 更新
176
+ - 有快取:立即套用,背景刷新
177
+ - 無快取(首次):非同步拉取(有短暫空窗期)
178
+
179
+ ### 安全性對話框
180
+ 以下設定需用戶明確批准:
181
+ - Shell command settings
182
+ - 自訂環境變數(非 allowlist)
183
+ - Hook 設定
184
+
185
+ 用戶拒絕 → Claude Code 退出;`-p` headless 跳過對話框。
186
+
187
+ ### 不支援 Server-Managed 的情況
188
+ Bedrock / Vertex AI / Foundry / `ANTHROPIC_BASE_URL` / LLM Gateway 時繞過。
189
+
190
+ ### ConfigChange Hook
191
+ ```json
192
+ "ConfigChange": [{ "type": "command", "command": "log_config_change.sh" }]
193
+ ```
194
+ 用於偵測/記錄設定被更改。
195
+
196
+ ### 資料保留政策
197
+
198
+ | 用戶類型 | 保留期 |
199
+ |---------|--------|
200
+ | Consumer(允許訓練)| 5 年 |
201
+ | Consumer(不允許)| 30 天 |
202
+ | Commercial(Team/Enterprise/API)| 30 天 |
203
+ | Enterprise + ZDR | 0 天 |
204
+
205
+ **訓練政策**:Commercial 用戶預設不訓練;Bedrock/Vertex 不支援 Developer Partner Program。
206
+
207
+ ### 隱私 Env Vars
208
+
209
+ ```bash
210
+ DISABLE_TELEMETRY=1 # 停用 Statsig 指標
211
+ DISABLE_ERROR_REPORTING=1 # 停用 Sentry 錯誤
212
+ DISABLE_BUG_COMMAND=1 # 停用 /bug 指令(送完整 transcript)
213
+ CLAUDE_CODE_DISABLE_FEEDBACK_SURVEY=1 # 停用問卷(Bedrock/Vertex 預設 off)
214
+ CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1 # 一次停用所有非必要流量 ⭐
215
+ CLAUDE_CODE_DISABLE_CRON=1 # 立即停用 session 內的 /loop 排程(v2.1.72)⭐
216
+ CLAUDE_CODE_DISABLE_GIT_INSTRUCTIONS=1 # 從 system prompt 移除內建 git 工作流指令(v2.1.69)
217
+ CLAUDE_CODE_SESSIONEND_HOOKS_TIMEOUT_MS=5000 # SessionEnd hook 超時(預設 1500ms,常導致 session-end.js 被中止)(v2.1.74)
218
+ ```
219
+ Bedrock/Vertex/Foundry:以上非必要流量**預設全部停用**。
220
+
221
+ ---
222
+
223
+ ## 25. GitLab CI/CD 整合(Beta)
224
+
225
+ Beta 功能,由 GitLab 維護(非 Anthropic)。
226
+
227
+ ### 最小設定
228
+
229
+ 1. Settings → CI/CD → Variables → 加入 `ANTHROPIC_API_KEY`(masked)
230
+ 2. 在 `.gitlab-ci.yml` 加入 claude job:
231
+
232
+ ```yaml
233
+ claude:
234
+ stage: ai
235
+ image: node:24-alpine3.21
236
+ rules:
237
+ - if: '$CI_PIPELINE_SOURCE == "web"'
238
+ - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
239
+ before_script:
240
+ - apk add --no-cache git curl bash
241
+ - curl -fsSL https://claude.ai/install.sh | bash
242
+ script:
243
+ - /bin/gitlab-mcp-server || true
244
+ - >
245
+ claude -p "${AI_FLOW_INPUT:-'Review and implement changes'}"
246
+ --permission-mode acceptEdits
247
+ --allowedTools "Bash Read Edit Write mcp__gitlab"
248
+ ```
249
+
250
+ ### AI_FLOW_* 變數(webhook 觸發時)
251
+ - `AI_FLOW_INPUT` — 用戶留言(`@claude` 後的文字)
252
+ - `AI_FLOW_CONTEXT` — issue/MR 上下文
253
+ - `AI_FLOW_EVENT` — 事件類型
254
+
255
+ ### 與 GitHub Actions 差異
256
+ | | GitHub Actions | GitLab CI/CD |
257
+ |--|--|--|
258
+ | 狀態 | GA | Beta |
259
+ | 工具 | `mcp__github` | `mcp__gitlab` |
260
+ | 安裝 | `/install-github-app` | 手動 YAML |
261
+ | 觸發 | PR comment | issue/MR comment(需 webhook)|
262
+
263
+ ### 企業提供者 OIDC
264
+ - **AWS Bedrock**:GitLab OIDC → `assume-role-with-web-identity`(無靜態 key)
265
+ - **Vertex AI**:Workload Identity Federation(無下載 key)
266
+
267
+ ### 進階參數
268
+ - `max_turns` — 限制回合數
269
+ - `timeout_minutes` — 限制執行時間
270
+ - `--debug` — 輸出詳細 log
271
+
272
+
273
+ ---
274
+
275
+ ## 26. LLM Gateway 整合(企業代理)
276
+
277
+ LLM gateway(如 LiteLLM)提供集中認證、cost tracking、audit log、model routing。
278
+
279
+ ### 環境變數設定
280
+
281
+ ```bash
282
+ # 統一端點(LiteLLM Anthropic 格式,推薦)
283
+ export ANTHROPIC_BASE_URL=https://litellm-server:4000
284
+
285
+ # 靜態 API key(作為 Authorization header)
286
+ export ANTHROPIC_AUTH_TOKEN=sk-litellm-static-key
287
+
288
+ # 動態 key helper(JWT 或 vault)
289
+ # settings.json 中設定:
290
+ # { "apiKeyHelper": "~/bin/get-litellm-key.sh" }
291
+
292
+ # key 刷新間隔(毫秒)
293
+ export CLAUDE_CODE_API_KEY_HELPER_TTL_MS=3600000
294
+
295
+ # Bedrock through LiteLLM
296
+ export ANTHROPIC_BEDROCK_BASE_URL=https://litellm-server:4000/bedrock
297
+ export CLAUDE_CODE_SKIP_BEDROCK_AUTH=1
298
+ export CLAUDE_CODE_USE_BEDROCK=1
299
+
300
+ # Vertex through LiteLLM
301
+ export ANTHROPIC_VERTEX_BASE_URL=https://litellm-server:4000/vertex_ai/v1
302
+ export CLAUDE_CODE_SKIP_VERTEX_AUTH=1
303
+ export CLAUDE_CODE_USE_VERTEX=1
304
+
305
+ # 當用 Anthropic Messages 格式連 Bedrock/Vertex 時
306
+ export CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1
307
+ ```
308
+
309
+ ### Gateway 需求
310
+
311
+ Gateway 必須支援以下 API 格式之一:
312
+ - **Anthropic Messages**:`/v1/messages`(需轉發 `anthropic-beta`、`anthropic-version` headers)
313
+ - **Bedrock InvokeModel**:`/invoke`(需保留 body 的 `anthropic_beta`、`anthropic_version`)
314
+ - **Vertex rawPredict**:`:rawPredict`
315
+
316
+ ---
317
+
318
+ ## 27. Plugin Marketplace 管理
319
+
320
+ ### 企業管理員設定
321
+
322
+ ```json
323
+ // .claude/settings.json — 推薦用戶安裝的 marketplace
324
+ {
325
+ "extraKnownMarketplaces": {
326
+ "company-tools": {
327
+ "source": { "source": "github", "repo": "your-org/claude-plugins" }
328
+ }
329
+ },
330
+ "enabledPlugins": {
331
+ "code-formatter@company-tools": true
332
+ }
333
+ }
334
+ ```
335
+
336
+ ```json
337
+ // managed settings — 限制只允許特定 marketplace(企業管控)
338
+ {
339
+ "strictKnownMarketplaces": [
340
+ { "source": "github", "repo": "acme-corp/approved-plugins" },
341
+ { "source": "hostPattern", "hostPattern": "^github\\.example\\.com$" }
342
+ ]
343
+ }
344
+ ```
345
+
346
+ `[]` = 完全鎖定(禁止新增任何 marketplace)
347
+
348
+ ### Marketplace 建立
349
+
350
+ ```
351
+ .claude-plugin/marketplace.json ← 目錄結構
352
+ ```
353
+
354
+ Plugin sources:`relative path`、`github`、`url`、`git-subdir`(sparse clone)、`npm`、`pip`
355
+
356
+ ```bash
357
+ # 驗證 marketplace
358
+ claude plugin validate .
359
+ /plugin validate .
360
+
361
+ # 增加 git timeout(慢速網路)
362
+ export CLAUDE_CODE_PLUGIN_GIT_TIMEOUT_MS=300000
363
+ ```
364
+
365
+ `${CLAUDE_PLUGIN_ROOT}` — 在 hooks/MCP 設定中引用 plugin 內的檔案。
366
+
367
+ ### Release Channels
368
+ - 建立兩個 marketplace 指向同一 repo 的不同 git ref(stable/latest)
369
+ - 透過 managed settings 的 `extraKnownMarketplaces` 分配給不同用戶群
370
+
371
+
372
+ ---
373
+
374
+ ## 28. Cloud Provider 設定(Bedrock / Vertex AI)
375
+
376
+ ### Amazon Bedrock
377
+
378
+ ```bash
379
+ export CLAUDE_CODE_USE_BEDROCK=1
380
+ export AWS_REGION=us-east-1 # 必須!不讀 .aws config
381
+
382
+ # 認證(選其一)
383
+ aws configure # CLI 設定
384
+ export AWS_BEARER_TOKEN_BEDROCK=your-api-key # 最簡單
385
+ aws sso login --profile p && export AWS_PROFILE=p # SSO
386
+
387
+ # 自動 SSO 刷新(settings.json)
388
+ # "awsAuthRefresh": "aws sso login --profile myprofile"
389
+ ```
390
+
391
+ **Bedrock 特有功能**:
392
+ ```bash
393
+ # 指定 Haiku 使用不同 region
394
+ export ANTHROPIC_SMALL_FAST_MODEL_AWS_REGION=us-west-2
395
+
396
+ # AWS Guardrails(settings.json)
397
+ # "ANTHROPIC_CUSTOM_HEADERS": "X-Amzn-Bedrock-GuardrailIdentifier: id\nX-Amzn-Bedrock-GuardrailVersion: 1"
398
+ ```
399
+
400
+ IAM 最小權限:`bedrock:InvokeModel` + `bedrock:InvokeModelWithResponseStream` + `bedrock:ListInferenceProfiles`
401
+
402
+ ### Google Vertex AI
403
+
404
+ ```bash
405
+ export CLAUDE_CODE_USE_VERTEX=1
406
+ export CLOUD_ML_REGION=global # 推薦(可用性最佳)
407
+ export ANTHROPIC_VERTEX_PROJECT_ID=YOUR-PROJECT-ID
408
+
409
+ # Haiku 不支援 global → 指定 region
410
+ export VERTEX_REGION_CLAUDE_3_5_HAIKU=us-east5
411
+ export VERTEX_REGION_CLAUDE_4_0_OPUS=europe-west1 # Opus 用 EU
412
+ ```
413
+
414
+ **1M Token Context Window**(Vertex beta):需 header `context-1m-2025-08-07`
415
+
416
+ IAM:`roles/aiplatform.user`(包含 `aiplatform.endpoints.predict`)
417
+
418
+ ### 共同重要設定
419
+
420
+ ```bash
421
+ # ⚠️ 部署必做:固定模型版本(防止新版上線破壞帳號)
422
+ # Bedrock
423
+ ANTHROPIC_DEFAULT_SONNET_MODEL='us.anthropic.claude-sonnet-4-6'
424
+ ANTHROPIC_DEFAULT_HAIKU_MODEL='us.anthropic.claude-haiku-4-5-20251001-v1:0'
425
+ # Vertex
426
+ ANTHROPIC_DEFAULT_SONNET_MODEL='claude-sonnet-4-6'
427
+ ANTHROPIC_DEFAULT_HAIKU_MODEL='claude-haiku-4-5@20251001'
428
+
429
+ # 某些 region 不支援 prompt caching
430
+ export DISABLE_PROMPT_CACHING=1
431
+ ```
432
+
433
+ ### Microsoft Foundry(Azure AI Foundry)
434
+
435
+ ```bash
436
+ export CLAUDE_CODE_USE_FOUNDRY=1
437
+ export ANTHROPIC_FOUNDRY_RESOURCE=your-resource-name
438
+ # 認證(API key 或 Entra ID 自動)
439
+ export ANTHROPIC_FOUNDRY_API_KEY=your-key # 或 az login
440
+ ```
441
+ RBAC:`Azure AI User` 或 `Cognitive Services User`
442
+
443
+ ### 三者共同限制
444
+ - `/login` `/logout` 停用
445
+ - server-managed settings 繞過
446
+ - 非必要遙測預設全部停用
447
+ - ⚠️ **固定模型版本**(不固定 → 新版上線可能破壞帳號)
448
+ - 建議建立**獨立帳號/專案**追蹤費用
449
+
450
+ ### LLM Gateway Skip-Auth(三者通用)
451
+ ```bash
452
+ export ANTHROPIC_BEDROCK_BASE_URL='https://gateway.example.com/bedrock'
453
+ export CLAUDE_CODE_SKIP_BEDROCK_AUTH=1 # Gateway 處理 AWS auth
454
+
455
+ export ANTHROPIC_VERTEX_BASE_URL='https://gateway.example.com/vertex'
456
+ export CLAUDE_CODE_SKIP_VERTEX_AUTH=1 # Gateway 處理 GCP auth
457
+
458
+ export ANTHROPIC_FOUNDRY_BASE_URL='https://gateway.example.com'
459
+ export CLAUDE_CODE_SKIP_FOUNDRY_AUTH=1 # Gateway 處理 Azure auth
460
+ ```
461
+ 用 `/status` 確認 gateway 設定。
462
+
463
+ ---
464
+
465
+
466
+ ---
467
+
468
+ ## 31. 自動 PR Code Review(Teams/Enterprise)
469
+
470
+ Teams 和 Enterprise 功能。PR 開啟或 commit 更新時,自動觸發多 agent 並行分析。
471
+
472
+ ### 啟用步驟
473
+
474
+ 1. Admin 進入 `claude.ai/admin-settings/claude-code`
475
+ 2. 安裝 Claude GitHub App(需要 **Contents read + Pull requests write** 權限)
476
+ 3. 選擇要啟用的 repositories
477
+ 4. 設定觸發方式:
478
+ - **After PR creation**:每 PR 執行一次(較省費)
479
+ - **After every push**:每次 commit 都執行(較即時,費用較高)
480
+
481
+ ### 分析特點
482
+
483
+ - **多 agent 並行分析**:邏輯錯誤、安全問題、regression
484
+ - **Inline comments 嚴重度標籤**:
485
+ - 🔴 Normal — 需要修改
486
+ - 🟡 Nit — 建議改善(非必要)
487
+ - 🟣 Pre-existing — 既有問題,非此 PR 引入
488
+ - 完成時間:約 20 分鐘
489
+ - 費用:約 **$15–25 / review**(多 agent 並行)
490
+ - **不阻塞 merge**:pure advisory
491
+
492
+ ### 自訂 Review 規則
493
+
494
+ | 檔案 | 用途 |
495
+ |------|------|
496
+ | `CLAUDE.md` | 專案共用規則(違反 → nit 等級 finding) |
497
+ | `REVIEW.md` | **review 專用**規則(風格指引、必查清單、跳過模式)|
498
+
499
+ `REVIEW.md` 是獨立於 `CLAUDE.md` 的 review-only 規則檔。建議內容:
500
+ ```markdown
501
+ # Review Rules
502
+