Make the conformance gap index bite in every runner, and catch URLSession's cancellation

Merged
JE
4mo ago
+969-114

Three review findings arrived on #563 after it had already merged. All three are on code #563 added, and all three are the same failure mode: an assertion or a classification that looks like it covers something and does not.

An omitted delayBetweenRequests index was documented as "every gap" and implemented as "gap 0" in three runners. #563 taught the assertion to take a gap index and updated conformance/schema.json to say that omitting it "require[s] the minimum on every gap". Ruby and Python did that. Go, TypeScript and Kotlin kept measuring requestTimes[1] - requestTimes[0] unconditionally, so every un-indexed timing fixture — retry.json's multi-retry cases included — validated only its first backoff there. All five now iterate.

A delay assertion could pass while checking nothing. Two ways. A named gap that did not exist passed silently in all five, because the bounds check sat inside each runner's "did we record at least two requests" guard — so a fully dropped retry left one request, skipped the guard, and made the assertion evaporate rather than fire. And an omitted index over zero gaps passed everywhere, because "every gap must clear the minimum" is vacuously true when there are no gaps. Both are backwards: the case a timing pin most needs to catch is the one where the delay never happened. Neither can pass now.

Swift only recognised Swift-concurrency cancellation. #563 made cancellation terminal in both retry loops via error is CancellationError, tested with a MockTransport that throws exactly that. URLSession does not: a cancelled task surfaces as URLError(.cancelled). So the shape that actually occurs in production still fell through to the generic catch, got classified a retryable network blip, and spent the entire attempt budget re-issuing a request the caller had abandoned — while firing onRetry for each one. Both loops now test for either shape through a shared isCancellation.

The runners had no tests of their own

Every bounds branch above was written blind. The runners are test harnesses, but their assertion logic is code, and its bounds branches never execute against a fixture that passes — which is exactly how #563 shipped an assertion that vacuously passed, through a fully green five-runner conformance run.

So the contract is now one function per runner — checkDelayGaps / DelayGaps.check / check_delay_gaps — with a committed unit test beside it, wired into make check and into the four per-language CI jobs through a new conformance-runner-tests target. Eleven cases per runner, each named for a behavior that regressed here or on #563:

CaseWhat it kills
omitted index catches a later failing gapthe gap-0-only bug
omitted index with zero gaps failsthe vacuous "every gap" pass
a named gap the run never produced failsthe dropped-retry evaporation
a negative index is rejectedwrapping to the end, which has no meaning for a gap
Int.MAX_VALUE / MaxInt fails without overflowgap + 1 wrapping past the guard
a zero minimum still requires the gap to existthe truthiness gate
a zero-request run reports 0, not 1len(delays) + 1 misattributing the failure

Proofs

The Swift pin fails five ways against the parent commit (91f62074f), with git show 91f62074f:swift/Sources/Basecamp/HTTP/HTTPClient.swift swapped in:

DownloadTests.swift:857: failed - Expected URLError(.cancelled) raw, got network(message: "Network error", cause: Optional(Error Domain=NSURLErrorDomain Code=-999 "(null)"))
DownloadTests.swift:860: XCTAssertEqual failed: ("3") is not equal to ("1") - A cancelled URLSession task must not spend another attempt
DownloadTests.swift:861: XCTAssertEqual failed: ("[2, 3]") is not equal to ("[]") - A cancelled URLSession task must not announce a retry
DownloadTests.swift:862: XCTAssertEqual failed: ("[1, 2, 3]") is not equal to ("[1]")
DownloadTests.swift:863: XCTAssertEqual failed: ("[1, 2, 3]") is not equal to ("[1]")
Executed 1 test, with 5 failures (0 unexpected)

Three attempts, two onRetrys, three start/end pairs — the #508/#509 lifecycle-imbalance class, on the transport shape that actually occurs.

The runner bounds tests fail three ways against the logic as committed at 07c356693, lifted verbatim into the extracted signature:

--- FAIL: TestCheckDelayGaps/omitted_index_fails_when_there_are_no_gaps_at_all
    delay_gaps_test.go:120: expected a failure message, got a pass
--- FAIL: TestCheckDelayGaps/negative_gap_index_is_rejected
    delay_gaps_test.go:123: expected message containing "must be non-negative", got "Expected a delay at gap -1, but only 3 request(s) were made"
--- FAIL: TestCheckDelayGaps/MaxInt_gap_index_fails_without_overflowing
panic: runtime error: index out of range [-9223372036854775808]

The MaxInt case is a real panic, not a failed assertion — gap + 1 wraps negative and sails through the guard into an out-of-range read.

The five-runner behavior was proven before the extraction with three probe fixtures. Expected-fail in all five; actual, on merged main:

ProbeGoTSKotlinPythonRuby
omitted index, a later gap below the minimumPASS ✗PASS ✗PASS ✗FAIL ✓FAIL ✓
named gap 9 on a 3-request flowFAIL ✓FAIL ✓FAIL ✓PASS ✗PASS ✗
delay assertion on a single-request flowPASS ✗PASS ✗PASS ✗PASS ✗PASS ✗

Every cell now fails. The probes were scaffolding and are not committed; the unit tests above are their permanent form.

Review rounds

Three rounds, ten threads, all resolved (Copilot errored on all three; cubic and Codex covered).

Round 2 — cubic ×5: the Kotlin Int.MAX_VALUE overflow (Go had the identical shape); TypeScript never rejecting a negative index (toBeGreaterThan(index + 1) is trivially satisfied, then times[-1] produced NaN and a message about a delay); a Swift test whose catch-all asserted nothing about the error it caught; an orphaned doc comment left claiming isCancellation clamps Retry-After; and Ruby/Python collapsing two different failures into one misleading message.

Round 2, B4 agent ×2 — a min: 0 truthiness bug in Python and Ruby, which is this PR's own bug shape one layer up: 0 is falsy in Python and an absent min is nil in both, so a min: 0 assertion silently degraded to no assertion at all. The default now lands inside the checked function, where no caller can gate it away. And the nullable Kotlin index regressing requestPath/requestMethod/requestBody diagnostics to index null.

Round 3 — cubic ×1 and Codex ×2: the zero-request diagnostic; the helper tests not being invoked by required CI (an unrun test is not a guard — now wired into all four per-language jobs); and bundle exec running before anything installed the Ruby runner's gems.

Verification

make check green end to end (REAL_EXIT=0): Go ok, TypeScript 1051, Python 763, Ruby 1011 runs / 2285 assertions, Kotlin BUILD SUCCESSFUL, Swift 278.

Conformance, from the same run — Go 131/0/2, Kotlin 132/0/1, TypeScript 164 passed / 2 skipped, Ruby 122/0/11, Python 133/0/0. Runner unit tests: Go 11, Python 12, Ruby 11 runs / 20 assertions, Kotlin 10, TypeScript 11.

Follow-ups from the #563 review that remain open and are deliberately not in here: #564 (Retry-After: HTTP-date form, the > 0 guard, and which statuses honor it) and #567 (a Transport throwing BasecampError.network is classified terminal). #565 is no longer among them — the 401 refresh-replay budget is settled and lands in its own PR, since it changes SDK behavior in Python and Ruby rather than runner or Swift classification logic.