Theme / v0.5.0

CatDesk

把 ChatGPT Web 變成本地 Coding Agent

實戰範例

修一個失敗的測試

用 search 定位失敗的測試、read 讀懂程式碼、run_command 跑測試、edit 修復後重跑直到全綠。重點認識 run_command 的 120 秒上限。

修一個失敗的測試

這是 CatDesk 最日常的使用場景:CI 紅了,測試失敗,你把整個「跑測試、看錯誤、改程式、再跑一次」的迴圈交給 ChatGPT。這篇範例完整走一遍,並且說清楚 run_command 的一個關鍵限制:它最多等 120 秒。


情境

order-service 專案的測試 tests/discount.test.ts 挂了。錯誤訊息是 expected 90, received 100,看起來折扣沒被套用。你在專案根目錄跑著 CatDesk(multi-tools 模式),對 ChatGPT 說:

目標

找出折扣沒套用的原因,修好它,並且用同一個對話把測試跑到全綠。

步驟:給 ChatGPT 的 prompt

The test tests/discount.test.ts is failing with
"expected 90, received 100".

1. Read the test file and the implementation it exercises.
2. Run the single failing test with npm test to see the output.
3. Fix the bug with the smallest possible edit.
4. Re-run the test to confirm it passes, then run the full suite.
Do not touch anything unrelated to the discount logic.

CatDesk 工具呼叫序列

1. read(paths: ["tests/discount.test.ts"])
2. search(pattern: "applyDiscount")
3. read(paths: ["src/orders/discount.ts"])
4. run_command(command: "npm test -- discount")
5. edit(path: "src/orders/discount.ts",
       old: "if (isVip) total = total", new: "if (isVip) total -= total * 0.1")
6. run_command(command: "npm test -- discount")
7. run_command(command: "npm test")

第 4 步的輸出確認了問題:applyDiscount() 判斷 VIP 之後算完折扣卻忘了回寫。第 5 步 edit 以「防護式取代」的方式原子性修改,找不到 old 內容就會失敗,不會改錯地方。第 6、7 步先跑單一測試再跑全套,確認沒有波及別的案例。

run_command 的 120 秒上限

這裡必須講清楚:run_command 是「跑一個短指令,等它跑完再回傳」,但它的等待上限是 120 秒。超時就會被切斷。

  • 單一測試檔、lint、簡單的建置檢查:用 run_command 剛好。
  • 完整測試套件動輒跑好幾分鐘的專案:不要用 run_command,改用 start_command 起背景工作、用 poll_command 收增量輸出,詳見範例 005。

上面第 7 步能這樣跑,是因為這個專案全套測試在 90 秒內會結束。如果 ChatGPT 選錯了工具、指令超時,你會看到逾時回傳,這時提醒它改用 start_command 即可。

結果與重點

測試全綠。ChatGPT 回報:VIP 折扣算出來後沒有回寫到總價,已在 src/orders/discount.ts 修正,全套 214 個測試通過。

三個重點帶走:

  • 迴圈是「search 找線索 → read 讀懂 → run_command 驗證 → edit 修改 → 再驗證」,這個節奏幾乎適用所有小型修復。
  • edit 是防護式編輯,替換目標不存在時會失敗而不是硬改,這是安全設計。
  • run_command 有 120 秒上限,這是它和 start_command 的分界線,長工一律交給背景工作。