🐛 Bug Report: v1 Idempotency-Key release/finalize not scoped to claiming request, allowing duplicate agent runs after stale-claim takeover
Summary
The Idempotency-Key claim lifecycle for POST /v1/chat/completions (docsgpt/api/v1/idempotency.py) never scopes release/finalize to the request that owns the claim. When a stale-claim takeover creates two claimants on one key, the older request can delete or finalize the newer request's live claim — allowing duplicate agent runs (duplicate answer rows + double token billing), which is exactly what this module exists to prevent.
Affected code
-
docsgpt/api/v1/idempotency.py:147—predetermined_id = str(uuid.uuid4())is generated per claim but discarded;claim_or_replay()returns only(claimed, response), so the caller never learns thetask_idof the claim it now owns. -
docsgpt/api/v1/idempotency.py:217-233—release()runs an owner-unscoped delete:DELETE FROM task_dedup WHERE idempotency_key = :k AND status = 'pending'It matches on key + status only, never
task_id. -
docsgpt/storage/db/repositories/idempotency.py:304-337—finalize_task()has the same shape:UPDATE task_dedup ... WHERE idempotency_key = :key AND status = 'pending', again with notask_idpredicate. -
docsgpt/api/v1/idempotency.py:102-120—_release_stale_pending()deletes apendingrow older thanSTALE_PENDING_SECONDS(300s) so a retry can re-claim it. Nothing prevents the original request from still being alive when that happens. -
docsgpt/api/v1/routes.py:588-679— claim/finalize andrelease()on every error path (ResumeInProgressError,ContinuationNotPendingError,ContextOverflowError,ValueError, bareException).
Reproduction
- Request A sends
POST /v1/chat/completionswithIdempotency-Key: K. It claims K (task_id=uuidA,status='pending') and starts a long agent run (>300s — slow provider, LLM retries, etc.). - A b2b client retries with the same key K. Request B hits the fresh-pending conflict, then
_release_stale_pending(K)deletes A's "stale" claim and B re-claims (task_id=uuidB). Two agents are now running under one key. - A finishes with a non-2xx result → the route's error handler calls
release(K)→ the unscopedDELETE ... WHERE idempotency_key = 'K' AND status = 'pending'removes B's live claim. - A third retry C arrives while B is still running → no row exists → C claims and starts a third agent run instead of receiving HTTP 409.
Success-path variant: if A finishes 2xx after B's takeover, finalize(K, response) marks B's row completed with A's body while B is still running; B's own finalize then no-ops.
Impact
- Duplicate answer rows in
conversation_messagesand double/triple token billing — the exact regression the module's docstring (docsgpt/api/v1/idempotency.py:1-29) says it was added to prevent. - The documented OpenAI-style contract ("first attempt still in flight → HTTP 409",
docs/content/Agents/openai-compatible.mdx) is broken for third-party retries.
Notes
- The sibling Celery-side path does this correctly:
docsgpt/api/user/idempotency.py:239-245releases viaIdempotencyRepository.release_lease(key, owner_id), which is scopedAND lease_owner_id = :owner(docsgpt/storage/db/repositories/idempotency.py:281-302). The HTTP path has no equivalent, even though it is the only path with a stale-takeover window that can produce two claimants. tests/api/v1/test_v1_idempotency.pypins each state in isolation (fresh pending → 409, stale pending → re-claim, non-2xx → release) but never exercises the interleaving where the original claimant outlives its stale takeover and then releases/finalizes.
Suggested fix
Have claim_or_replay() return the predetermined_id it claimed and thread it into release(key, task_id) / finalize(key, task_id, response), adding AND task_id = :tid to both statements (and to finalize_task in the repository).
@arc53-machine assign this to me @yashascoding
@yashascoding, you're assigned.
- Open a PR or post progress within 14 days; otherwise the issue will be available to others again.
- Read
CONTRIBUTING.mdbefore starting. - Include a screenshot or short recording showing the change in your PR.
Automated triage. A maintainer will follow up.