fix(middleware): skip idempotency cache for mutable error codes (BUG-API-238)#180
Merged
Merged
Conversation
…API-238)
BUG-API-238 — the free-tier recycle gate emits 402
`free_tier_recycle_requires_claim` with a claim_url the user can act on in
30 seconds. Pre-fix the 402 was written to the explicit-key 24h cache, so
this sequence stranded the agent on a stale failure:
1. Agent POST /db/new with Idempotency-Key K1 → 402 (cached against K1).
2. User follows claim_url, claims with email (clears the gate state).
3. Agent retries POST /db/new with K1 → replays the cached 402.
The 24h cache TTL is the Stripe contract — same key replays the same
response — but it presumes a STABLE outcome. A 402 that flips when the
user takes a 30-second action is not stable.
Fix:
- mutableErrorCodes registry (currently { free_tier_recycle_requires_claim })
listing error codes whose 4xx resolution can flip inside the cache TTL.
- shouldCacheResponse(status, body, ct) helper that defers to caching for
success + non-JSON + stable 4xx, but skips for body.error ∈ mutable map.
- Wired into BOTH explicit-key (24h TTL) and fingerprint-fallback (120s)
cache-write paths so the bypass is symmetric.
What did NOT change:
- Stripe-shape contract for stable outcomes (success + quota_exceeded +
upgrade_required + provision_limit_reached etc.) — those still cache.
- 409 idempotency_key_conflict envelope (BUG-013/406) unchanged.
- No new endpoints, no new fields, no auth changes.
Surface checklist (rule 22):
- api/internal/middleware/idempotency.go helper + 2 wires
- api/internal/middleware/idempotency_mutable_cache_test.go new regression
- dashboard / marketing / OpenAPI no surface change
(Stripe contract preserved;
response shape unchanged)
Coverage block:
Symptom: Idempotent retry replays stale 402 after user clears recycle gate
Enumeration: rg -F 'rdb.Set(context.Background(), cacheKey' internal/middleware/idempotency.go
Sites found: 2 cache-write paths (explicit + fingerprint)
Sites touched: 2 / 2
Coverage test: TestShouldCacheResponse_MutableErrorsSkipCache iterates the
live mutableErrorCodes map (rule 18 — registry-iterating, not
hand-typed); TestIdempotency_RecycleGate402_SourceAssertion
static-grep that BOTH branches invoke shouldCacheResponse (rule 16).
Live verified: pre-merge curl evidence pending — see PR body
Local gate:
- go build ./... PASS
- go vet ./... PASS
- go test ./internal/middleware/ PASS (full suite)
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The static-source assertions in idempotency_mutable_cache_test.go pin
the registry invariants; this file proves both cache-write skip branches
fire end-to-end. Patch-coverage gate flagged 0% on idempotency.go
lines 429-435 (explicit-key skip) and 556-562 (fingerprint skip) because
the previous test only exercised the helper in isolation.
Adds 3 cases:
- explicit-key 402 free_tier_recycle_requires_claim → second call
misses cache (no X-Idempotent-Replay)
- fingerprint-fallback path → same invariant
- negative control: 402 quota_exceeded (stable) → second call DOES
replay (Stripe-shape contract preserved for non-mutable errors)
shouldCacheResponse now reports 100% coverage.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
29814a1 to
ad5ac42
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
BUG-API-238 — the free-tier recycle gate emits 402 `free_tier_recycle_requires_claim` with a `claim_url` the user can act on in 30 seconds. Pre-fix the 402 was written to the explicit-key 24h cache, so this sequence stranded the agent on a stale failure:
The 24h cache TTL is the Stripe contract — same key replays the same response — but it presumes a STABLE outcome. A 402 that flips when the user takes a 30-second action is not stable.
Fix
What did NOT change
Surface checklist (rule 22)
Coverage block
```
Symptom: Idempotent retry replays stale 402 after user clears recycle gate
Enumeration: rg -F 'rdb.Set(context.Background(), cacheKey' internal/middleware/idempotency.go
Sites found: 2 cache-write paths (explicit + fingerprint)
Sites touched: 2 / 2
Coverage test: TestShouldCacheResponse_MutableErrorsSkipCache iterates the LIVE
mutableErrorCodes map (rule 18 — registry-iterating, not hand-typed);
TestIdempotency_RecycleGate402_SourceAssertion static-grep that BOTH
branches invoke shouldCacheResponse (rule 16 — two emitters of one bug)
Live verified: post-merge — see verify-plan below
```
Local gate
Live verify plan (post-merge)
```bash
curl https://api.instanode.dev/healthz | jq .commit_id # must match merge SHA
1. POST /db/new with explicit Idempotency-Key as anon user that's already triggered the gate
2. Confirm 402; confirm X-Idempotent-Replay header NOT present on subsequent retry
3. Successful claim, retry again — confirm fresh 201 instead of stale 402
```
🤖 Generated with Claude Code