fix(proxy): handle SSO session expiry — auto-reauth and clean Anthropic error fallback#4
Open
AntonYeromin wants to merge 8 commits into
Open
fix(proxy): handle SSO session expiry — auto-reauth and clean Anthropic error fallback#4AntonYeromin wants to merge 8 commits into
AntonYeromin wants to merge 8 commits into
Conversation
When the user's SSO session expires mid-session the upstream CodeMie API returns a 401 whose body is not Anthropic-format. The Bun-compiled codemie-opencode binary (JavaScriptCore runtime) crashes trying to read the usage object: 'undefined is not an object (evaluating _.input_tokens)'. Fix in three parts: 1. Store upstream HTTP status code in context.metadata before the onResponseHeaders plugin hooks run so SessionExpiryHandlerPlugin can detect 401/403 without a hook-signature change. 2. After onResponseHeaders, if context.metadata.sessionExpired is set: drain the 401 body, call attemptSSOReauth() which runs CodeMieSSO.authenticate() (same browser OAuth flow as startup), re-inject fresh cookies into the original request headers, and forward the retry — streaming the successful response to the binary. 3. If re-auth fails or the auth method is JWT, fall back to sendSessionExpiredResponse() which sends a well-formed Anthropic authentication_error JSON the binary handles without crashing. Fixes EPMCDME-12317. Generated with AI Co-Authored-By: codemie-ai <codemie.ai@gmail.com>
…attemptSSOReauth Generated with AI Co-Authored-By: codemie-ai <codemie.ai@gmail.com>
Real CodeMieProxy + real mock upstream HTTP server, zero vi.mock(). Covers: upstream 401 → authentication_error JSON, upstream 403 → same, upstream 200 → passthrough regression guard. Adds test:e2e npm script. Generated with AI Co-Authored-By: codemie-ai <codemie.ai@gmail.com>
Adds npm run test:e2e after integration tests in both the local ci script and the test-ubuntu GitHub Actions job. Skipped on Windows to avoid flaky HTTP server behaviour on that runner. Generated with AI Co-Authored-By: codemie-ai <codemie.ai@gmail.com>
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
SessionExpiryHandlerPluginthat detects upstream 401/403 responses and setscontext.metadata.sessionExpired = truevia theonResponseHeadershook, preventing the raw 401 body from reaching Bun/JSC and causing theundefined is not an object (evaluating '_.input_tokens')crashsso.proxy.tsto drain the 401 body after session expiry is detected, attempt SSO re-authentication (attemptSSOReauth), retry the original request with fresh cookies on success, or fall back to a clean Anthropicauthentication_errorJSON responseSSOSessionSyncPluginat priority 100)Closes EPMCDME-12317
Changes
src/providers/plugins/sso/proxy/plugins/session-expiry-handler.plugin.tssrc/providers/plugins/sso/proxy/plugins/__tests__/session-expiry-handler.plugin.test.tssrc/providers/plugins/sso/proxy/plugins/index.tsSessionExpiryHandlerPluginsrc/providers/plugins/sso/proxy/sso.proxy.tsupstreamStatusCodepropagation, session-expiry short-circuit,attemptSSOReauth,sendSessionExpiredResponsesrc/providers/plugins/sso/proxy/__tests__/sso.proxy.session-expiry.test.tsTest plan
npm run test:unit— 104 files, 1708 passed, 1 skippedSessionExpiryHandlerPluginsetssessionExpired=trueon 401 and 403, no-op on 200/500/absent statussendSessionExpiredResponsereturns HTTP 401 with valid Anthropicauthentication_errorJSON bodyattemptSSOReauthreturnsfalseimmediately whenauthMethod === 'jwt'without calling SSO