Commit d708072
committed
fix(middleware): skip idempotency cache for mutable error codes (BUG-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>1 parent 61faeb1 commit d708072
2 files changed
Lines changed: 208 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
159 | 159 | | |
160 | 160 | | |
161 | 161 | | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
162 | 241 | | |
163 | 242 | | |
164 | 243 | | |
| |||
341 | 420 | | |
342 | 421 | | |
343 | 422 | | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
344 | 437 | | |
345 | 438 | | |
346 | 439 | | |
| |||
454 | 547 | | |
455 | 548 | | |
456 | 549 | | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
457 | 564 | | |
458 | 565 | | |
459 | 566 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
0 commit comments