Skip to content

feat(api): Idempotency-Key support on POST routes (P3-G)#80

Merged
CryptoJones merged 1 commit into
masterfrom
feat/idempotency-key
May 18, 2026
Merged

feat(api): Idempotency-Key support on POST routes (P3-G)#80
CryptoJones merged 1 commit into
masterfrom
feat/idempotency-key

Conversation

@CryptoJones
Copy link
Copy Markdown
Owner

Part of architect audit issue #73 — iteration P3-G.

Summary

  • Clients may send Idempotency-Key: <printable-ASCII, 1-255> on any POST under /v1/*. First response is cached 24h, keyed by sha256(authKey:method:path) + the raw key.
  • Same body → replay (with Idempotency-Replay: true header).
  • Same key, different body → 409 { code: "idempotency_key_reused" }.
  • 5xx responses are not cached — real failures must remain retriable.
  • Canonical-JSON body hashing so benign field-reordering doesn't trip the body-mismatch path.
  • New dbo.IdempotencyKey table + migration; opportunistic TTL prune on each request — no sweeper job.
  • No-op when header is absent — legacy clients unaffected.

Test plan

  • tests/unit/idempotency.test.js (14 cases): canonical-JSON stability, scope isolation, KEY_PATTERN accept/reject.
  • tests/api/idempotency.test.js (4 cases): HTTP-level mount verification.
  • Full suite: 327 pass / 4 skip (was 309/4).
  • First-write→replay round-trip with a real DB lands once P5-M unlocks injectable DB in tests.

This code proudly made in Nebraska. GO BIG RED! 🌽 https://xkcd.com/2347/

Architect audit P3-G. Stripe-style at-most-once semantics for create
operations: clients that opt in by sending `Idempotency-Key: <key>` on
a POST get duplicate-suppression for free.

How it works:
- Cache scope is sha256(authKey || ':' || method || ':' || path),
  so two operators (or two routes) cannot collide on the same key.
- Cache key is the raw `Idempotency-Key` header value (printable
  ASCII, 1-255 chars, validated). Garbage headers return 400.
- The first response (status + JSON body) is persisted in the new
  `dbo.IdempotencyKey` table with a 24h TTL.
- A retry with the SAME (scope, key, request body hash) replays the
  cached response verbatim and carries `Idempotency-Replay: true`.
- A retry with the SAME (scope, key) but DIFFERENT body hash returns
  `409 { code: "idempotency_key_reused" }` so the client knows it
  reused a key for a semantically different request.
- 5xx responses are NOT cached — a real failure must be retriable.
- Request body hashing is canonical-JSON (key-sorted, recursive) so
  benign field-order differences don't trip the body-mismatch path.

Migration: new `dbo.IdempotencyKey` table (ikScope, ikKey,
ikRequestHash, ikResponseStatus, ikResponseBody, ikCreatedAt,
ikExpiresAt) with a `UNIQUE (ikScope, ikKey)` constraint that
also handles the concurrent-first-write race via
`ON CONFLICT DO NOTHING`. Expired rows are pruned opportunistically
on each idempotent request — no background sweeper.

Mounted globally on `/v1/*` POSTs after `attachAuth` runs (so the
scope hash can include the resolved auth key). No-op when the
header is absent, so legacy clients are entirely unaffected.

Tests:
- `tests/unit/idempotency.test.js` (14 cases): canonical JSON
  stability across key reordering, tenant/path isolation in the
  scope hash, `KEY_PATTERN` accept/reject grid.
- `tests/api/idempotency.test.js` (4 cases): HTTP-level mount
  verification — no header passes through, malformed header
  400s before the controller runs, valid header reaches handler,
  GETs are never gated.
- Full suite: 327 pass / 4 skip (was 309/4).

Full first-write→replay round-trip coverage requires a real DB and
lands in the integration suite once P5-M unlocks injectable DB.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@CryptoJones CryptoJones merged commit e055c88 into master May 18, 2026
3 checks passed
@CryptoJones CryptoJones deleted the feat/idempotency-key branch May 18, 2026 04:45
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