Swift transport: gate retries on operation idempotency (#411)
Fixes #411.
Problem
The Swift SDK's HTTP transport retried any request whose response status was in retry_on (429/503) — and retried on network errors — without checking operation idempotency. So non-idempotent POSTs were retried, contrary to the SDK contract (SPEC §7). A 503 returned after the backend created a resource issues a second create; for CreateWormhole a double-create consumes one of the scarce four-per-board slots (surfaced by #402). Documented known bug at SPEC.md:407/:1568.
Exposure: 41 POST operations — 3 idempotent (CompleteTodo, PauseQuestion, SubscribeToCardColumn), 38 non-idempotent (including all 30 Create*). The 3 idempotent POSTs must keep retrying, so a method-only gate is insufficient.
Fix — three-gate algorithm (mirrors Kotlin, metadata-driven)
An operation is retry-eligible iff the method is naturally idempotent (GET/HEAD/PUT/DELETE) or behavior-model.idempotent == true. Non-idempotent POST → single attempt; everything else retries per config on both the status path and the network-error path.
- Generator (
BehaviorModel.swift,MetadataEmitter.swift): parse the always-presentidempotentflag and emit a sortedidempotentOperationsset +Metadata.isIdempotent(for:). No service files change — onlyMetadata.swiftregenerates. - Transport (
HTTPClient.performRequest): new defaultedidempotent: Bool = falseparam (fail-safe). The gate allowlists the naturally-idempotent methods (so PATCH/OPTIONS/future methods stay fail-closed) and folds intomaxAttempts, covering both retry paths at once. BaseService(single caller ofperformRequest): computesMetadata.isIdempotent(for: info.operation)and threads it through all four request variants. Generated services and hand-written extensions inherit the gate unchanged.- SPEC.md: replaced the Swift over-retry known-bug entries with the three-gate behavior and corrected the stale "Swift surfaces network errors immediately" claim (Swift already retried network errors; that retry is now correctly gated).
No public API break
OperationInfo unchanged; performRequest gains a defaulted param (source-compatible); BaseService request signatures unchanged (idempotency computed internally); Metadata.isIdempotent internal. behavior-model.json unmodified (already carried the flag) → behavior-model-check stays green. No cross-SDK change.
Tests (swift/Tests/BasecampTests)
- Layer A (transport): non-idempotent POST not retried on 503/429/network-error → 1 attempt; idempotent POST retried on 503 → 2 attempts; PATCH not retried (method gate fail-closed); GET+503 regression still retries.
- Layer B (BaseService → Metadata → transport):
projects.create(non-idempotent POST) against 503 → 1 request;todos.complete(idempotent POSTCompleteTodo) against 503-then-204 → 2 requests, proving the metadata lookup wires through. - Generator parser/emitter (
IdempotencyMetadataTests,@testable import BasecampGenerator): flag parses true/false/absent; emitted set is deterministically sorted and contains exactly the true ops.
Verification
make swift-generate→ onlyMetadata.swiftchanges;Services/diff empty. Post-commit regengit diff --exit-codeclean (drift proof).Metadata.swift:idempotentOperationscontainsCompleteTodo/PauseQuestion/SubscribeToCardColumn, excludes everyCreate*, sorted.make swift-checkgreen (216 tests).make checkgreen.
Swift is macOS-only and not in the conformance matrix, so the XCTest suite is the acceptance gate.
Summary by cubic
Gate Swift transport retries on operation idempotency to stop reattempting non‑idempotent POSTs and align with SPEC §7. Non‑idempotent POSTs run once; naturally idempotent methods and POSTs marked idempotent: true still retry on 429/503 and network errors.
- Bug Fixes
- Implemented the three‑gate in
HTTPClient: retry only forGET/HEAD/PUT/DELETEor whenidempotentis true; gate folded intomaxAttemptsto cover both status and network‑error retries. HoistedretryableMethodsto a private static set to avoid per‑request allocations. - Generator reads
idempotentfrombehavior-model.jsonand emitsidempotentOperationsplusMetadata.isIdempotent(for:); emitter comment clarified to note it lists all idempotent‑flagged ops (any method). BaseServicelooks upMetadata.isIdempotent(for: operation)and threads it to the transport across all request paths.SPEC.mdupdated to document Swift’s three‑gate behavior and that Swift retries network errors, gated by idempotency.- Tests added for metadata parsing/emission and retry gating at both transport and service layers.
- No public API break; service method signatures unchanged.
- Implemented the three‑gate in