### Is there an existing issue for this? - [x] I have searched the existing issues ### Current Behavior Sometimes, when you stop a task workspace, the operation fails with this error: ``` internal provisionerserver error: complete job: execute transaction: update workspace build ai tasks flag: pq: insert or update on table "workspace_builds" violates foreign key constraint "workspace_builds_ai_task_sidebar_app_id_fkey" ``` <img width="1920" alt="Image" src="https://github.com/user-attachments/assets/da341260-366d-43fc-b974-f059381da726" /> Changing the workspace's version to an older version and then back again lets you work around the bug and stop the workspace. ### Relevant Log Output ```shell ``` ### Expected Behavior I'd expect to be able to stop the workspace without any problems. ### Steps to Reproduce I'm not sure how to reproduce this error. I've seen it happen when both the sidebar app and the coder_ai_task resource are created within a terraform module, that module is sourced from a git branch, and that branch gets updated between workspace start and stop. I don't know if these conditions are sufficient to reproduce the error. ### Environment - Host OS: Linux - Coder version: 2.24.1 ### Additional Context _No response_