Skip to content

feat: idempotency keys at all execution trigger boundaries (RELIABILITY-006) #525

Closed

Description

@vybe

Summary

Add an Idempotency-Key contract at every producer boundary that creates executions: /api/internal/... (scheduler), /api/webhooks/{token} (#291), MCP chat_with_agent and fan_out, sync /chat, async /task, event subscriptions, and self-execute. Backend stores (key → execution_id) for 24 h and short-circuits duplicates with the original execution result instead of dispatching twice.

Context

From the orchestration architecture review (2026-04-26):

No idempotency at the producer boundary. Webhook re-deliveries, MCP client retries, scheduler-to-backend network blips — all create duplicate executions. Unifying every trigger through one funnel (TaskExecutionService) actually makes this worse, because the funnel doesn't dedupe — it just trusts the producer.

This is most acute for webhooks (external systems retry on 5xx and on perceived timeouts) and scheduler dispatch (network blip between scheduler container and backend creates phantom retries). Today the only mitigation is "be a well-behaved producer," which external webhook senders are not.

Acceptance Criteria

  • Idempotency-Key HTTP header accepted on: /api/agents/{name}/chat, /api/agents/{name}/task, /api/internal/agents/{name}/execute-task, /api/webhooks/{token}, MCP-originated executions (chat_with_agent, fan_out), event-subscription dispatch.
  • New idempotency_keys table: (key, scope, execution_id, created_at, response_snapshot) with 24 h TTL and a unique constraint on (scope, key).
  • On duplicate key: return the original execution_id and (if execution finished) the original result, with HTTP 200 + X-Idempotent-Replay: true header. Do not dispatch a second execution.
  • Webhook trigger auto-derives a key from (webhook_token, body_hash) if the caller doesn't supply one — covers naive webhook senders that retry without idempotency awareness.
  • Scheduler dispatch supplies a deterministic key derived from (schedule_id, fire_time) so a transient backend 5xx + APScheduler retry never produces duplicates.
  • Audit log entry for every idempotent replay (so duplicate-storms are observable).
  • Documented in architectural invariants — every new trigger type must accept idempotency keys before merging.

Technical Notes

  • Place enforcement in TaskExecutionService.execute_task() (or a thin pre-check ahead of it) so all trigger paths inherit it for free — consistent with the "single funnel" architecture.
  • Key scoping: per-tenant or per-agent to prevent cross-tenant collision.
  • Body hashing for webhooks: SHA-256 of canonicalized body, header-independent.
  • Related: pairs naturally with retry-in-funnel (currently only the scheduler retries, see ORCHESTRATION_RELIABILITY plan).

Activity

  1. added
    priority-p1
    type-feature
    on Apr 26, 2026
  2. vybe commented on Jun 2, 2026

    @vybe
    ContributorAuthor

    Closed by #1019 (squash-merged to dev after resolving append-collisions in database.py + migrations.py against current dev — keep-both, no migration renumber since the codebase uses named migrations; CI incl. schema-parity + backend-boots + multi-seed pytest re-ran green on the merge commit).

    Implements RELIABILITY-006: an Idempotency-Key contract at every execution trigger boundary (chat/task/internal/webhook/fan-out/scheduler/MCP). Atomic claim via PRIMARY KEY (scope, idempotency_key) (INSERT-or-conflict, cross-process safe), 24h TTL, fail-open dedup layer, replay short-circuit (200/202 + X-Idempotent-Replay, in-flight → 409). New idempotency_keys table, db/idempotency.py + services/idempotency_service.py, and architectural Invariant #18. Ships to main on next release.

  3. added a commit that references this issue on Jun 2, 2026
  4. added
    complexity-medium
    theme-reliability
    on Jun 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    complexity-mediumpriority-p1theme-reliabilitytype-feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions