Align generated Go + Python retry loops to total-attempt semantics (A4b1)

Merged
JE
4mo ago
+408-19

SPEC.md and behavior-model.json define max_retries / retry.max as a total attempt count (including the initial request): max_retries = 3 means at most 3 attempts. Ruby, Kotlin, and the hand-written Go client already honor this; generated Go (doWithRetry) and Python (sync + async _request_with_retry) treated it as additional retries (MaxRetries + 1), making one extra request.

This aligns both to total-attempts.

⚠️ Observable change

With the default of 3, requests now make up to 3 attempts, not 4. (max_retries was already documented as a total attempt count; the generated-Go and Python transports were the outliers.)

Effective-attempts algorithm (identical for generated Go and Python)

  • max_retries < 0 → configuration error. Generated Go returns a plain error from WithRetryConfig and re-checks inside doWithRetry (RetryConfig is publicly mutable after construction); Python already raises ValueError at Config construction (immutable thereafter).
  • max_retries == 0 → a single attempt, no retry — an explicit compatibility exception to SPEC's ≥ 1.
  • max_retries > 0 → max_retries attempts.

This also fixes a latent generated-Go bug: a negative MaxRetries previously made the loop body never execute and returned (nil, nil).

Docs / user-visible strings brought in line

  • SPEC.md validation note now enumerates the three distinct max_retries = 0 outcomes across four implementations (generated Go & Python accept → 1 request; Ruby makes 0 requests and raises; hand-written Go panics) and distinguishes generated from hand-written Go.
  • python/README.md: "3 retries, up to 4 total attempts" → "3 attempts"; env-var table "N+1 total attempts" → "Total attempts including the initial request".
  • Python / Ruby / hand-written-Go exhaustion strings: "retries" → "attempts" (values were already total-correct; only the wording lied).

Scope

A4b1 only. A4b2 (generated Go / Python honoring per-op retry.max) is deliberately deferred — that per-op divergence remains and is not addressed here.

Tests

  • Generated-Go retry counts {0→1, 1→1, 3→3}, negative rejected both at WithRetryConfig and via post-construction mutation into doWithRetry, and a deterministic no-sleep-after-final-attempt test (fake HttpRequestDoer + context cancellation — no wall-clock threshold). Lives in go/pkg/basecamp, outside pkg/generated per the repo rule.
  • Python sync + async attempt-count parity and negative-at-construction.

make check green.


Summary by cubic

Aligns generated Go and Python retry loops to total-attempt semantics so max_retries is the total number of attempts. With the default of 3, requests now make up to 3 attempts, not 4.

  • Bug Fixes

    • Generated Go (doWithRetry) and Python (sync + async) now use total-attempts: < 0 → configuration error; 0 → one attempt; > 0 → N attempts. Go validates in WithRetryConfig and re-validates in-call; fixes the negative-MaxRetries (nil, nil) bug.
    • Python Config rejects negatives and non-integers (including bool), preventing fractional or misleading attempt counts.
    • No sleep or OnRetry after the final attempt in both generated and hand-written Go; tests pin this behavior. Exhaustion messages now say “attempt(s)” in Go, Python, and Ruby; docs updated (SPEC.md, python/README.md).
  • Migration

    • Behavior change: default 3 means up to 3 attempts. Update tests/monitoring that assumed 4.
    • Scope: A4b1 only. Per-operation retry.max (A4b2) is unchanged.