Repository navigation
Conversation
…ant hint
The OAuth access-token lifetime was changed to track the II grant
expiration (matching the session duration the user picks). But that
expiration arrives on the connect-completion POST — a best-effort,
unauthenticated message that races the token exchange — and it was used
verbatim for BOTH the client's `expires_in` AND the server's internal
token-validity gate (`TokenInfo.ttl`), with no floor:
Some(exp_ns) => Duration::from_nanos(exp_ns.saturating_sub(now_ns))
When that value lands at/near/behind `now` (clock skew, or a stale/dropped
POST that won the race), `saturating_sub` yields ~0, so the token is born
invalid. The client's first authenticated MCP call then 401s and the OAuth
flow never leaves the callback page ("Redirecting back to the agent…").
When the completion POST instead lost the race, the TTL fell back to the
default 1h and the connect succeeded — the intermittent "worked on the 2nd
attempt" the production (id.ai) connector showed.
Make the grant expiration asymmetric: it may EXTEND the token beyond the
default (a week-long grant still mints a week-long token — no fixed 1h cap)
but never SHORTEN it below the default. An at/near/behind-`now` expiration
now falls back to the default instead of zero, so a token is never born
invalid. The grant remains the hard ceiling at II (a signed call 401s once
it truly lapses), which drives the clean re-auth the shortening was meant
to achieve.
Only the production-shared token path changes; the Phase-2 flow and per-
instance gating are untouched. Adds a regression test covering the
at/behind-`now` and short-grant cases.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_014T9N8USDfNK5yzznPGg7Ym
There was a problem hiding this comment.
Pull request overview
Fixes an intermittent OAuth completion failure in the production connector by preventing “born-invalid” access tokens when the grant-expiration hint arrives at/behind server now (race/clock-skew). The change floors the computed token TTL at the default value, ensuring the first authenticated MCP call doesn’t immediately 401 and stall on the OAuth callback page.
Changes:
- Update
token_ttlto apply an asymmetric rule: grant expiration may extend TTL beyond the default, but may not shorten it below the default. - Expand/rename the unit test to cover long-grant extension, short-grant flooring, and at/behind-
nowfallback behavior.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| /// The access-token lifetime. The II grant is the hard ceiling at II (a signed | ||
| /// call 401s once it lapses), so this governs only the CLIENT's view, and it | ||
| /// does so asymmetrically: a grant that outlasts the default may **extend** the |
|
Closing this. Investigation of the production connector incident showed the "born-invalid token" scenario this PR guards against is not what was happening: every access token actually issued during the incident window had a healthy lifetime ( On top of not fixing the incident, this PR's If a guard against a zero-TTL token is ever wanted (e.g. against clock skew reporting Generated by Claude Code Generated by Claude Code |
Incident
The production connector (id.ai, on v1) intermittently failed to complete OAuth: the browser reached ChatGPT's callback (
…/connector/oauth/…?code=mcp-code-…&state=…) but never redirected back — it stuck on "Redirecting back to the agent…". Retrying usually worked ("worked on the 2nd attempt").Root cause
Not the Phase-2 registration-delegation work — that is flag-gated off for prod, so the connect protocol is unchanged. The regression is in the shared OAuth token path.
The access-token lifetime was changed to track the II grant expiration (so the session duration the user picks is honored instead of a flat 1h). But that expiration arrives on the connect-completion POST — a best-effort, unauthenticated message that races the token exchange — and it was used verbatim, with no floor, for both the client's
expires_inand the server's internal token-validity gate (TokenInfo.ttl, checked atauth.rsinfo.created.elapsed() < info.ttl):When that value lands at/near/behind the server clock (clock skew, or a stale/dropped POST that won the race),
saturating_subcollapses to ~0, so the token is born invalid. ChatGPT's first authenticated MCP call then 401s and the flow never leaves the callback page. When the completion POST instead lost the race, the TTL fell back to the default 1h and the connect succeeded — hence the intermittent "2nd attempt works."The pre-change code couldn't hit this: the internal TTL was always a solid 1h, never derived from the racy hint.
Fix
Make the grant expiration asymmetric in
token_ttl:nowexpiration now falls back to the default instead of zero, so a token is never born invalid.Behavior note
One deliberate change from the previous behavior: a grant shorter than the default (e.g. the user picks 10 min) now yields a client token floored at the default rather than a matching short token. This is intentional — a best-effort, unauthenticated, racy hint should never shorten the client's token, and II's hard ceiling still stops the token from acting past the real grant. If you'd rather keep short client tokens exactly matching short grants, we can instead floor at a small minimum (e.g. a few minutes) to kill only the born-invalid case; say the word.
Testing
cargo test— 79 pass (renamed/expandedtoken_ttl_…test now covers: long grant → full remaining, short grant → floored at default, and expiration at/behindnow→ default not zero). No new clippy warnings (the 3 that remain are pre-existing inmain.rs/management.rs). Only the production-shared token path changes; Phase-2 and per-instance gating are untouched.🤖 Generated with Claude Code
https://claude.ai/code/session_014T9N8USDfNK5yzznPGg7Ym
Generated by Claude Code