跳到正文
DEV Community· Jeff·· 2 小時前AI 評分58

智能體把 202 Accepted 當作完成,未輪詢背景工作

The agent treated the 202 Accepted response as done and never polled the job

AI 導讀

智能體把月度銷售報告回填請求的 202 Accepted 當作完成,未輪詢工作,該工作因缺少欄位而失敗,令報告一週後仍是空白。文章建議記錄 job id,輪詢至 completed、failed 或 canceled,並核對預期資料或錯誤,確認完成證據後才結束任務。

正文

The team asked the agent to backfill the monthly sales report by reprocessing last quarter's invoices through the existing ingestion endpoint. The call returned 202 Accepted with a job id in the body. The agent logged the response, wrote a short summary saying the backfill was submitted, marked the task done, and moved on. No one polled the job. The worker hit a missing column partway through, the job failed in the background, and when a human opened the report a week later it was empty.

The agent answered "did the server accept my request?" instead of "did the work actually finish?" Those are different questions on any endpoint that returns before the job runs.

A 202 Accepted means received, not finished

The acknowledgment proves transport, not outcome. A 202 tells the caller that the request was queued and will be processed later. It says nothing about whether the worker succeeds, retries, or errors out after the response left.

The job id is a handle, not a result. Having the id in the log lets you look up the outcome; treating that same id as proof of completion skips the actual lookup. The backfill can still fail, partially apply, or hang without changing the status code that already came back.

Fire-and-forget gives you no completion signal by default

Nothing comes back unless you ask. An accepted job does not push a result to the original caller. Without a status endpoint to poll or a webhook/callback to subscribe to, the response has already returned, so there is no automatic second message showing success or failure.

The summary paragraph hides the gap. "Submitted the backfill" reads like "the backfill completed," but it describes only the first hop. An agent that closes on the acknowledgment reports a healthy-looking story while the real work is still unproven.

Poll the terminal state, not the initial response

Record the job id and check where it landed. Re-request the job status or read the row the job wrote to, and wait for a terminal state: completed, failed, or canceled. A queued or running state means the task is not done yet.

Require a completion artifact. Before closing the task, point to something that proves the work ran end to end: the expected row count, a generated file, or a reported error. A logged request body does not qualify.

Separate accepted from done in the report. Mark the request accepted separately from the job completed, so a reviewer sees that the terminal state was actually verified rather than assumed from the status code.

Handle the failure branch on purpose. A poll that returns failed should surface the error, retry only idempotent stages, and report the partial state. Closing on 202 leaves the failure branch unvisited, the same way a happy-path-only test does.

An accepted request that goes unmonitored looks finished in the chat log and quietly fails in the worker. Where has a background job been closed on the first response, with no one ever checking the terminal state?

來源:DEV Community · dev.to