DocsGPT icon indicating copy to clipboard operation
DocsGPT copied to clipboard

🐛 Bug Report: v1 Idempotency-Key release/finalize not scoped to claiming request, allowing duplicate agent runs after stale-claim takeover

Open yashascoding opened this issue 1 month ago • 2 comments

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 the task_id of 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 no task_id predicate.

  • docsgpt/api/v1/idempotency.py:102-120 — _release_stale_pending() deletes a pending row older than STALE_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 and release() on every error path (ResumeInProgressError, ContinuationNotPendingError, ContextOverflowError, ValueError, bare Exception).

Reproduction

  1. Request A sends POST /v1/chat/completions with Idempotency-Key: K. It claims K (task_id=uuidA, status='pending') and starts a long agent run (>300s — slow provider, LLM retries, etc.).
  2. 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.
  3. A finishes with a non-2xx result → the route's error handler calls release(K) → the unscoped DELETE ... WHERE idempotency_key = 'K' AND status = 'pending' removes B's live claim.
  4. 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_messages and 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-245 releases via IdempotencyRepository.release_lease(key, owner_id), which is scoped AND 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.py pins 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).

yashascoding avatar Oct 18 '26 11:10 yashascoding

@arc53-machine assign this to me @yashascoding

yashascoding avatar Oct 18 '26 11:10 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.md before starting.
  • Include a screenshot or short recording showing the change in your PR.

Automated triage. A maintainer will follow up.

arc53-machine avatar Oct 18 '26 11:10 arc53-machine