Skip to content

Add idempotency key support for task deduplication#88

Open
AntoineToussaint wants to merge 1 commit intomainfrom
feat/idempotency-key
Open

Add idempotency key support for task deduplication#88
AntoineToussaint wants to merge 1 commit intomainfrom
feat/idempotency-key

Conversation

@AntoineToussaint
Copy link
Copy Markdown
Member

Summary

Two new fields on SpawnOptions for controlling task deduplication:

  • only_once: bool — auto-derives idempotency key from hash(task_name, params). First spawn creates the task, duplicates are no-ops.
  • idempotency_key: Option<String> — explicit key for when params differ but the operation is the same. Takes precedence over only_once.

Default behavior (only_once: false, no key) is unchanged — every spawn creates a new task.

Motivation

In multi-instance deployments, multiple clients may observe the same event and independently try to spawn the same logical task. Without dedup, N instances create N tasks that all execute. This is wasteful for expensive operations and incorrect for non-idempotent ones.

DB Changes

  • New nullable idempotency_key TEXT column on task tables
  • Partial unique index WHERE idempotency_key IS NOT NULL
  • spawn_task checks for existing non-terminal task with same key before inserting
  • Migration adds column/index to all existing queues
  • ensure_queue_tables updated for new queues

Design doc

See docs/design/durable-idempotency-key.md in the autopilot repo.

Test plan

  • Spawn with only_once: true — second spawn returns existing task
  • Spawn with explicit idempotency_key — same behavior
  • Spawn without key — creates new task (backward compatible)
  • Completed/failed/cancelled task with same key — new spawn succeeds
  • Existing tests pass

🤖 Generated with Claude Code

@AntoineToussaint AntoineToussaint force-pushed the feat/idempotency-key branch 3 times, most recently from 4b21848 to 42e29c1 Compare April 9, 2026 21:45
Two new fields on SpawnOptions:
- `only_once: bool` — auto-derives key from hash(task_name, params)
- `idempotency_key: Option<String>` — explicit key (takes precedence)

When a key is set, spawn_task checks for an existing non-terminal task
with the same key. If found, returns the existing task instead of
creating a duplicate. This enables first-served semantics where multiple
clients can safely try to spawn the same logical task.

DB changes:
- New nullable `idempotency_key` column on task tables
- Partial unique index on idempotency_key WHERE NOT NULL
- Migration adds column/index to all existing queues
- ensure_queue_tables updated for new queues

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant