AI 編寫的背景工作程序如何通過「提交後崩潰」測試
Make AI-Written Workers Pass the Crash-After-Commit Test
AI 編寫的背景工作程序可透過三種故障邊界測試,驗證外部操作已提交而回應尚未記錄時,重啟後能否安全恢復。測試以本地伺服器獨立記錄目的地接受的請求,並核對重啟後的持久狀態及回執中的外部發票 ID;中間故障應由測試訊號精確觸發。重播是否安全取決於接收 API 的冪等行為;若既無安全重播方式,也無可靠查詢途徑,便保留結果未知並供操作人員核對。
Disclosure: This article was drafted by an AI agent and is based on the sources linked below. The example is illustrative.
Ask a coding agent to build a background worker that sends a message, and the first test will usually prove the message gets sent. The follow-up question is more revealing: what happens if the destination accepts the message and the worker dies before recording the answer?
METR's November capability update looks at how performance changes when tasks are harder to construct or evaluate. A takeaway for application teams is to specify the part of a task that is expensive to verify. For a background worker, that usually means checking behavior across a real failure boundary, with durable state on both sides.
Write three tests around one side effect
Consider a job that creates an invoice through an external API. Give the job a stable ID, use a local test server as the API, and have the server record accepted requests independently of the worker's database.
| Where the worker stops | What the restarted worker should prove |
|---|---|
| Before sending | The destination recorded no request. The job can run after restart. |
| After the destination commits, before the response is recorded | Look up the operation or replay the same destination-supported idempotency key. Verify that the destination still has one invoice. If the result cannot be established safely, hold the job for reconciliation. |
| After storing the response, before acknowledging the queue | Recover the stored receipt, acknowledge the job, and leave the destination's request count unchanged. |
For the middle case, make the test server commit the invoice, signal the test harness, and hold the response. Stop the actual worker when the signal arrives. The replacement worker should read the same database. Use the harness signal to place the crash precisely. A sleep can miss the boundary and leave a test that passes without exercising recovery.
Give the agent a concrete acceptance criterion
Specify what you want to inspect: the destination's count, the durable state after restart, and the external invoice ID in the receipt. A green test that asserts only job.status === 'done' says nothing about duplicate invoices. Similarly, an outbox records the intention to send and makes that intention recoverable. Whether a replay is safe still depends on what the receiving API promises.
Before merging, check the API's documented idempotency behavior, including key lifetime and what happens when the same key is used with different parameters. Where the destination offers neither a safe replay nor a reliable lookup, keeping the result unknown is a useful outcome. It gives an operator a specific record to investigate before another side effect is attempted.
This small test matrix works for email notifications, deployment triggers, and other agent-written automation. It gives the reviewer something concrete to ask for: show what happened at the destination when the worker disappeared halfway through the job.
來源:DEV Community · dev.to