Skip to content

fix(client): bound + guard the collab-token retry policy (wiki-v2#385) - #40

Merged
PMQ9 merged 2 commits into
wiki-v2from
fix/385-collab-token-retry
Sep 19, 2026
Merged

PMQ9 merged 2 commits into
wiki-v2from
fix/385-collab-token-retry

Conversation

@PMQ9

@PMQ9 PMQ9 commented Sep 19, 2026

Copy link
Copy Markdown

Fork half of cccvu/wiki-v2#385. Fork-first: merge this, then the super-repo PR re-pins the gitlink to this merge SHA.

Problem

useCollabToken is the only query that overrides the app-wide retry: false, and its override does the opposite of a correct client:

  1. Throws on the case it exists for. error.response.status is read unguarded; a network error is an AxiosError with no response, so .status throws a TypeError inside the retry callback — the exact cold-start/rolling-deploy race the override was written to survive. (The //@ts-ignore was hiding this possibly-undefined read.)
  2. Amplifies edge controls. Everything except a 404 is retried, including the WAF's deliberate 403 (origin-guard / malformed path) and 429 (rate limit). Retrying a rate limit is the one client-side amplifier in the app (found while investigating feat(#485): page-lifecycle facts, trash roots, DB cycle guard, and snapshotBefore (wiki-v2 PR-B, fork half) #53's WAF-403 burst).
  3. Unbounded, not "10×". Because retry is a function, return 10 is coerced to boolean true (query-core retryer.js), so it retries forever (until success/unmount) with an uncapped, unjittered backoff.

Fix

Replace it with a bounded, standards-aligned (RFC 9110) policy, extracted into two exported pure functions so the branch logic is unit-tested directly:

  • shouldRetryCollabToken — retry only transient failures: a network error (no response) or a 5xx, plus a 429; every other 4xx (403/404/…) is terminal. Bounded by COLLAB_TOKEN_MAX_RETRIES (4 — one above React Query's default of 3, for the cold-start race).
  • collabTokenRetryDelay — honor Retry-After exactly on a 429/503 (it's a minimum: jitter is added on top, never a down-clamp, so we never retry early), else React Query's own default capped-exponential backoff. Jitter only where a rate-limited fleet would otherwise rebound in one synchronized wave.
  • useQuery is pinned to <ICollabToken, Error> so the error: unknown predicates don't widen TError; the //@ts-ignore is gone.

Recovery backstops for anything longer than the cap are unchanged: refetchOnMount and the editor's onAuthenticationFailed refetch.

Tests

New Vitest suite auth-query.test.tsx (runs in the docmost-client CI job): network-error retries without throwing (the core regression), 403/404 terminal, 429 retries + honors Retry-After as a minimum (a large value is not down-clamped), 5xx retries up to the cap then stops (the unbounded-retry lock), fallback backoff capped, and a hook-level wiring check. Math.random is pinned for determinism.

Local: full client suite green (308 tests), tsc --noEmit clean, check-upstream-boundary.sh green (documented as UPSTREAM_MODIFICATIONS.md §42(b) + §154 in the super-repo PR).

🤖 Generated with Claude Code

PMQ9 and others added 2 commits September 18, 2026 22:59
…v2#385)

useCollabToken was the only query overriding the app-wide `retry: false`, and its
override did the opposite of a correct client:

- read `error.response.status` unguarded -> threw a TypeError inside the retry
  callback on a network error (no `response`), the very cold-start race it existed
  to survive;
- retried every non-404, including deliberate edge refusals (WAF 403 / 429) -- the
  only client-side retry amplifier in the app;
- returned a truthy `10` from a retry *function*, which coerces to `true` -> retried
  UNBOUNDEDLY (not 10x) with an uncapped, unjittered backoff.

Replace it with a bounded, standards-aligned policy, extracted into two exported pure
functions so the branch logic is unit-tested directly:
- retry only transient failures -- a network error (no response) or 5xx -- and a 429,
  capped at COLLAB_TOKEN_MAX_RETRIES (4);
- deliberate 4xx refusals (403/404/...) are terminal;
- honor Retry-After exactly on 429/503 (a minimum; jitter is added on top, never a
  down-clamp, so we never retry early), else React Query's default capped backoff.

Adds a Vitest regression suite (runs in the docmost-client CI job).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…ccvu/wiki-v2#385)

Review round 1 (Testing, non-blocking Level 3): the `retryAfterSec > 0` sub-condition in
collabTokenRetryDelay was deletable without any test failing — no fixture exercised a finite
non-positive Retry-After, so a refactor could silently drop it and let a "0"/negative header
produce a zero/negative (immediate) delay. Add cases that a non-positive or HTTP-date Retry-After
falls back to the capped backoff (mutation-verified: removing `> 0` reds the new test). Also resolve
the wiring-test mock to a valid token so React Query doesn't error on undefined + background-retry.

Production code unchanged (test-only).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@PMQ9
PMQ9 merged commit 245a909 into wiki-v2 Sep 19, 2026
@PMQ9
PMQ9 deleted the fix/385-collab-token-retry branch September 19, 2026 05:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant