嗨大家,我是 Debuguy。
前面我們建立了完整的 AI DevOps Squad:Grafana 看監控、Elasticsearch 查 Log、kubectl-ai 查資源、Playwright 確認使用者體驗。但還是會遇到一個尷尬的場景...
📱 凌晨 3:27
🚨 [CRITICAL] Payment Service Error
付款失敗率暴增,檢測到異常的狀態碼 418@on-call-engineer
你問 Bot:
@debuguy_bot 幫我看看為什麼會出現 418 狀態碼
Bot 的回應:
根據 Log 分析,系統回傳了 418 I'm a teapot 狀態碼。
這是一個來自 RFC 2324 的愚人節笑話狀態碼,通常不應該在生產環境出現...
「所以你是在跟我說我們的付款系統變成茶壺了?這什麼鬼!」
你真正想問的是:
「為什麼我們的程式碼會在這個時候回傳 418?」
「是哪段邏輯觸發的?」
「該不會是哪個工程師覺得好玩亂加的吧?」
Bot 很會查 Log、看 Metrics、檢查 K8s 資源,但它不知道你的程式碼長什麼樣子。
它可以告訴你:
但它沒辦法告訴你:
「如果 AI 能直接『看過』我的程式碼就好了...」
Repomix 是一個神奇的工具,它能把你的整個 Git repository 打包成一個單一檔案,而且:
.gitignore 的檔案傳統做法:一個一個檔案複製貼上
請你幫我看這段程式碼:
[貼上 api.ts]
還有這個 service:
[貼上 payment.service.ts]
喔對了還有這個 util:
[貼上 error-handler.ts]
「貼到手痠,而且 AI 的 context 還爆掉了...」
用 Repomix:一次搞定
repomix --output-show-line-numbers
產生一個 repomix-output.xml,裡面包含:
📦 Repository: payment-service
📂 src/
├─ api.ts (lines 1-150)
├─ services/
│ └─ payment.service.ts (lines 151-300)
└─ utils/
└─ error-handler.ts (lines 301-350)
==================== File: src/api.ts ====================
1: import express from 'express';
2: import { PaymentService } from './services/payment.service';
3:
4: const app = express();
...
然後直接塞到 System Prompt!
「1M tokens 大概是多少程式碼?」
粗估:
這代表:
你可能會想:「為什麼不用 RAG(Retrieval-Augmented Generation)?」
RAG 的問題:
直接塞進 System Prompt:
「就像一個新同事 onboarding,直接把整個 codebase 給他看一遍。」
# 全域安裝
npm install -g repomix
# 或是用 npx (不需安裝)
npx repomix
在你的專案目錄執行:
cd /path/to/your/project
# 產生帶行號的完整程式碼
repomix --output-show-line-numbers
# 預設會產生 repomix-output.xml
進階參數:
# 排除特定目錄
repomix --ignore "node_modules,dist,build"
# 只包含特定檔案類型
repomix --include "**/*.ts,**/*.js"
# 客製化輸出檔名
repomix --output codebase-snapshot.txt
更新你的 .prompt 檔案:
# prompts/chatbot.prompt
---
model: gemini-2.5-flash
config:
temperature: 0.7
input:
schema:
repomix: string
---
You are Debuguy Bot, an AI DevOps assistant with deep knowledge of our codebase.
## Complete Codebase Knowledge
You have access to the ENTIRE codebase with line numbers. The codebase structure is below:
{{repomix}}
When analyzing errors or issues:
1. Reference specific files and line numbers
2. Understand the full context of function calls
3. Trace execution flow across multiple files
4. Identify potential bugs based on actual implementation
## Your Capabilities
With access to:
- ✅ Grafana for metrics
- ✅ Elasticsearch for logs
- ✅ kubectl-ai for K8s resources
- ✅ **Complete codebase understanding**
You can now provide:
- Root cause analysis with code references
- Specific file and line number suggestions
- Impact assessment based on actual implementation
- Code-aware debugging strategies
工程師: @debuguy_bot 幫我看看為什麼會出現 418 狀態碼
Bot: 找到了!讓我來揭開這個「茶壺之謎」🫖
🔍 錯誤發生在 `src/api/payment.ts:67-72`
根據程式碼邏輯,418 是我們自訂的狀態碼(沒錯,就是那個茶壺),用來表示:
- 客戶端請求超時(payment.service.ts:145 設定的 30 秒 timeout)
- 第三方支付 API 回應時間過長
程式碼註解寫著:
// 當第三方 API timeout 時,回傳 418 "I'm a teapot"
// 因為我們不想沖咖啡(處理付款),我們是茶壺(超時了)
// - 某位有幽默感的工程師,2024-03-15
📊 從 Grafana 看到:
- 第三方 API 平均回應時間從 2s 飆到 28s
- 觸發我們的「茶壺防禦機制」
💡 建議:
1. 檢查第三方支付 API 是否有問題
2. 考慮調整 timeout 設定(目前 30s 可能太短)
3. 加入 retry 機制(目前 payment.service.ts:150 沒有 retry)
4. 順便考慮換個比較正經的狀態碼?😅
價值:
工程師: @debuguy_bot 昨天晚上 8 點到 9 點之間有很多 null pointer exception,幫我查一下
Bot: 找到問題了!
🐛 Bug 位置:`src/services/user.service.ts:89`
程式碼:
88: const user = await this.userRepository.findById(userId);
89: const userName = user.profile.name; // ❌ 這裡沒有檢查 null
90: return userName;
📋 Log 證據:
昨晚 20:15-21:00 有 237 筆 "Cannot read property 'name' of undefined"
🔎 根因:
- 新用戶註冊時 profile 可能是 undefined
- 昨晚剛好有一波新用戶註冊活動
💡 建議修復:
```typescript
const userName = user?.profile?.name ?? 'Anonymous';
或在 line 88 後加入:
if (!user.profile) {
throw new UserProfileNotFoundError(userId);
}
**價值:**
- ✅ 直接找到 bug 的位置
- ✅ 給出具體的修復建議
- ✅ 加碼提供更好的錯誤處理方式
### 3. 安全性考量
**敏感資訊過濾:**
```bash
# .repomixignore (類似 .gitignore)
.env
.env.*
*.key
*.pem
secrets/
config/production.yaml
Repomix 自動遵守 .gitignore:
建議:
今天我們完成了 AI DevOps Squad 的終極升級:
現在 AI 真的能做到:
你只需要根據這些證據,決定下一步行動。
當凌晨警報響起時,Bot 不只告訴你「發生了什麼」,還能告訴你「為什麼會發生」和「程式碼的哪裡有問題」。甚至還能發現某位工程師在 2024 年 3 月 15 日留下的茶壺梗。
這就是我們想要的 AI DevOps 助手。
完整的原始碼在這裡
AI 的發展變化很快,目前這個想法以及專案也還在實驗中。但也許透過這個過程大家可以有一些經驗和想法互相交流,歡迎大家追蹤這個系列。
也歡迎追蹤我的 Threads @debuguy.dev