Swift transport: gate retries on operation idempotency (#411)

Merged
JE
4mo ago
+400-11

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-present idempotent flag and emit a sorted idempotentOperations set + Metadata.isIdempotent(for:). No service files change — only Metadata.swift regenerates.
  • Transport (HTTPClient.performRequest): new defaulted idempotent: Bool = false param (fail-safe). The gate allowlists the naturally-idempotent methods (so PATCH/OPTIONS/future methods stay fail-closed) and folds into maxAttempts, covering both retry paths at once.
  • BaseService (single caller of performRequest): computes Metadata.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 POST CompleteTodo) 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 → only Metadata.swift changes; Services/ diff empty. Post-commit regen git diff --exit-code clean (drift proof).
  • Metadata.swift: idempotentOperations contains CompleteTodo/PauseQuestion/SubscribeToCardColumn, excludes every Create*, sorted.
  • make swift-check green (216 tests). make check green.

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 for GET/HEAD/PUT/DELETE or when idempotent is true; gate folded into maxAttempts to cover both status and network‑error retries. Hoisted retryableMethods to a private static set to avoid per‑request allocations.
    • Generator reads idempotent from behavior-model.json and emits idempotentOperations plus Metadata.isIdempotent(for:); emitter comment clarified to note it lists all idempotent‑flagged ops (any method).
    • BaseService looks up Metadata.isIdempotent(for: operation) and threads it to the transport across all request paths.
    • SPEC.md updated 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.