跳到正文
DEV Community· Qingfeng·· 4 小時前AI 評分58

作者以 Agent 執行前檢查大幅減少任務失敗

The Pre-Flight Check That Killed Most of My Failures

AI 導讀

作者用一段在 Agent 開始操作前執行的 Bash 腳本檢查六項環境狀態,並稱失敗次數大幅下降。檢查涵蓋 Git 分支與工作區狀態、佔用中的連接埠、上次執行的殘留、測試是否仍能重現問題、憑證與路徑是否符合預期,以及操作前快照。快照失敗時腳本會阻止任務開始;預檢輸出亦可貼入任務上下文,並作為事後檢討時的執行前狀態基準。

正文

For a month I tried to fix agent failures by improving instructions. It barely helped. Then I wrote a script that runs before the agent touches anything, and the failure count dropped hard.

The script is not clever. It checks six things.

1. What branch am I on, and is it clean?

git status plus the current branch, printed into the task context. Half my "the agent edited the wrong file" incidents were actually "the agent edited the right file on the wrong branch".

2. Which ports are already taken?

The agent starts a dev server, and it fails, or worse it silently attaches to something already listening. Print the ports in use before the task starts, and check again after. Twice I found a zombie process from my own earlier run holding the port, and the agent spent 20 minutes blaming the code.

3. Is anything left over from the last run?

Temp directories, lock files, half-written output, running background processes with names I recognise. Cleaning this is cheap; debugging around it is not.

4. Does the fixture still reproduce the problem?

If the task is "fix the failing test", the test must be failing before the agent starts. I have absolutely run a task where the bug was already gone and the agent invented a plausible cause for a failure that no longer existed.

5. Are the credentials and paths the ones I think they are?

Which database, which host, which token. Not the value - just an assertion that it is the expected one. One run pointed at a staging database because a shell profile had been changed somewhere else. Nothing broke, which is exactly why nobody noticed for two days.

6. Is the snapshot taken?

The one from my earlier post: hashes, key command outputs, and a copy of what is about to be modified. If this step fails, the task does not start. A task that cannot be undone should not begin.

Why a script and not a checklist

Because I skip checklists. Six checks in a script that refuses to proceed is 40 lines of bash and it never has a bad day. And it produces text I can paste into the task context, so the agent starts from facts instead of assumptions - the same facts I would have had to explain anyway.

The unexpected benefit: when something does go wrong, the pre-flight output is the first half of the postmortem. I can see the world as it was before the run, which is usually the exact state I need to compare against.

If you have one, what is in it that I am missing? Mine refuses to start on a dirty tree, which annoys me weekly and has saved me more than once.

來源:DEV Community · dev.to