Gateway auth: adaptive refresh margin, stale-token fallback, replay-safe retry#145
Merged
Conversation
The refresh-ahead margin was a fixed 60 seconds. Against an auth server issuing 60-second access tokens, a freshly issued token always sits inside the margin, so EVERY box request became a refresh round-trip. Each refresh rotates the server-side refresh-token family, so rapid request sequences hammered the rotation machinery and any hiccup (a timeout, an out-of-order rotation, replay detection) killed the whole session mid-command with a bare "Box requires sign-in." Three fixes in cli/gateway_auth.py: - The margin now scales with the token's issued lifetime, min(60, lifetime/4), so short-TTL servers cannot push the CLI into refreshing on every request. - A refresh that fails while the stored token is still valid falls back to that token and lets the gateway judge it, instead of hard-failing the command. - A refresh whose connection never reached the server is retried once. Ambiguous failures (read timeouts) are deliberately NOT retried: the server may have already rotated the refresh token, and replaying would trip its replay detection and burn the session. New unit tests pin all three behaviors (test_gateway_auth_refresh.py); gateway_auth previously had no unit coverage. Found by the box-lifecycle CI's first supervised runs, which lost sessions mid-run against a short-TTL auth server.
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
Merged
2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a class of spurious mid-command "Box requires sign-in" failures on gated boxes, found by the box-lifecycle CI's first supervised runs (#144).
The bug:
EXPIRY_MARGIN_SECONDS = 60is compared against the token's remaining life on every request. Against an auth server issuing short-lived access tokens (60 s observed in the field), a freshly issued token is always inside the margin — so every box request triggered a refresh round-trip. Each refresh rotates the server-side refresh-token family, so rapid sequences of requests (e.g.lager nets add-all, a verboselager update) hammered the rotation machinery, and any hiccup — a timeout, out-of-order rotation, replay detection — killed the session mid-command.Changes (
cli/gateway_auth.py)min(60, token_lifetime / 4)(lifetime read from the JWT'siat/exp). A 15-minute token keeps the old 60 s margin; a 60 s token gets a 15 s margin and is actually usable from cache.ConnectionError(request never reached the server). Read timeouts are deliberately not retried — the server may have already rotated the refresh token, and replaying can trip its replay detection and burn the whole session.Tests
test/unit/cli/test_gateway_auth_refresh.py(7 tests) pins all three behaviors plus rotation-cookie persistence.gateway_authpreviously had no unit coverage.test/unit/cli/suite green in a clean python:3.11 container.Checklist