Fix the WaitGroup close check in a worker pool

Review the AI's codeMedium15 min

Your task

An AI agent wrote a Go worker pool. A producer sends jobs on one channel, workers send results on a second, and the caller ranges over the results. It only finishes cleanly when two things happen. The jobs channel gets closed. And one goroutine waits on the WaitGroup, until every worker is done, and then closes the results channel. You check a trace of the pool's channel operations against those rules.

An assistant wrote diagnose_worker_pool(events). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • events: the operations in the order they happen, each "<goroutine>:<action>".
  • The actions that matter are send_jobs, close_jobs, wg_wait, close_results and range_results. Any other action is ignored.
  • Return one of "deadlock:jobs-never-closed", "deadlock:results-never-closed", "deadlock:close-after-range", "panic:send-on-closed-results" or "ok".

Rules the tests check:

  • Split each event at its first :. The part before is the goroutine and the part after is the action. An event with no : is all goroutine, with an empty action.
  • Check in this order and return the first result that applies:
    1. No event has the action close_jobs: "deadlock:jobs-never-closed".
    2. No event has the action close_results: "deadlock:results-never-closed".
    3. Take the first close_results event. If an earlier event in the same goroutine has the action range_results: "deadlock:close-after-range".
    4. If an earlier event in that same goroutine has the action wg_wait: "ok".
    5. Otherwise: "panic:send-on-closed-results".
  • Goroutine names are compared exactly. A wg_wait in any other goroutine does not count for step 4.

Examples

diagnose_worker_pool(events: list[str]) -> str
Example 1
Inputdiagnose_worker_pool(["producer:send_jobs", "producer:close_jobs", "main:range_results", "main:wg_wait"])
Output"deadlock:results-never-closed"
Example 2
Inputdiagnose_worker_pool(["producer:send_jobs", "producer:close_jobs", "closer:wg_wait", "closer:close_results", "main:range_results"])
Output"ok"
Example 3
Inputdiagnose_worker_pool(["producer:close_jobs", "main:range_results", "main:wg_wait", "main:close_results"])
Output"deadlock:close-after-range"