Skip to content

fix(oauth): keep an Anthropic refresh retryable when its first connection is refused - #8578

Merged
lidge-jun merged 1 commit into
devfrom
codex/8577-proxy-refusal-refresh
Nov 12, 2026
Merged

lidge-jun merged 1 commit into
devfrom
codex/8577-proxy-refusal-refresh

Conversation

@lidge-jun

@lidge-jun lidge-jun commented Nov 12, 2026 •

Copy link
Copy Markdown
Owner

Summary

On Windows, when OpenCodex starts before the configured local proxy (FlClash on http://127.0.0.1:7892 in the report), the first Anthropic OAuth refresh hits a closed proxy port. Bun reports ECONNREFUSED, so the token request never left the machine. Yet the durable refresh intent written before dispatch outlived the failure, and the next attempt marked the account needsReauth and raised OAuthLoginRequiredError without another token request (#8577). The operator had to log in again every morning.

postJson in src/oauth/anthropic.ts now treats a refused connect as proof that nothing was sent. It throws AnthropicTokenError(..., requestNotSent=true), which refreshAnthropicAccountWithLock already releases as pre-dispatch, as it does for the direct token-host DNS failure from #6586. The next attempt then retries the refresh token and recovers once the proxy is up. Only two exact shapes qualify:

  • Bun's native fetch, direct or through an HTTP(S) proxy: code: "ECONNREFUSED" with path equal to this token URL. Bun reports a reachable proxy that cannot open the upstream tunnel as ERR_PROXY_TUNNEL, and a response lost later as a reset or timeout, so neither matches.
  • The SOCKS5 transport (src/lib/socks5-fetch.ts), which only ever connects to the configured proxy: node:net's code: "ECONNREFUSED" with syscall: "connect".

Message text, a refusal reported for another URL, a refusal outside connect, resets, timeouts and redirects all keep the replay guard. Anthropic rotates refresh tokens, so an ambiguous outcome must never authorize a replay.

Closes #8577

Verification

  • Failing-first: tests/oauth/oauth-refresh-connection-refused.test.ts drives the real Anthropic adapter. Its first token request goes through a real closed transport: Bun's own fetch to a dead loopback HTTP proxy, and the real socks5Fetch to a dead SOCKS5 port. On dev both refusal cases fail: the intent survives and the second attempt demands a login. With the fix they pass, and the second request refreshes. Four controls keep the guard and pass both before and after: another URL, unstructured text, a node refusal on read, and ECONNRESET.
  • bun test tests/oauth/oauth-refresh.test.ts tests/oauth/oauth-code-login.test.ts tests/oauth/oauth-refresh-connection-refused.test.ts tests/providers/provider-outbound.test.ts: 141 pass, 0 fail. Typecheck, structure:check, privacy:scan, layout, ratchet and Lab boundary pass.
  • bun run test:changed: 43,022 pass, 105 skip, 7 fail across 1,848 files. Each failing file was rerun alone:
    • link-ocx-child-e2e needs cargo.
    • cli-codex-cli-update-boundary fails identically on dev.
    • codex-config-write-lock passes alone.
    • ultrafast-tier-honesty is a 5 s timing flake: it passed and failed on this branch and passed on dev.
  • NOT RUN: an end-to-end run against a real FlClash proxy or the real Anthropic token endpoint; macOS/Windows CI (manual dev batch only).

Checklist

  • Scope stays focused and avoids unrelated cleanup.
  • Docs or release notes were updated when needed.
  • Security-sensitive changes were reviewed for secrets, auth, and unsafe defaults.

Summary by CodeRabbit

  • Bug Fixes
    • OAuth refreshes can now be retried after a connection refusal that occurs before the token request is sent.
    • Ambiguous connection errors continue to require login rather than triggering another token request.
  • Tests
    • Added regression coverage for connection refusals through HTTP and SOCKS5 proxies.

…tion is refused

At startup the configured local proxy can still be down. Bun reports the refused connect as
ECONNREFUSED, the token request never left the machine, yet the durable refresh intent outlived
it and the next attempt demanded a new login without another token request (#8577).

A refused connect in either transport's exact shape now proves the request was unsent and
releases the intent as pre-dispatch: Bun fetch's ECONNREFUSED naming this token URL in path
(direct or through an HTTP proxy), or the SOCKS5 transport's node ECONNREFUSED on connect to its
proxy. Message text, another URL, a refusal outside connect, resets and timeouts keep the guard.

Closes #8577
@lidge-jun
lidge-jun requested a review from Ingwannu as a code owner November 12, 2026 02:32
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-12T02:34:00.643844Z 83630a9 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions

Copy link
Copy Markdown
Contributor

✅ Deterministic PR hygiene checks passed.

@github-actions github-actions Bot added the bug Something isn't working label Nov 12, 2026
@coderabbitai

coderabbitai Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Anthropic OAuth refresh now treats specific first-connection refusals as proof that the token request was not sent. The change adds regression tests for retry behavior and for ambiguous errors that must retain refresh intent.

Changes: Anthropic OAuth refresh refusal handling

Layer / File(s) Summary
Classify and handle initial connection refusals
src/oauth/anthropic.ts:65-79, src/oauth/anthropic.ts:104-106
The code recognizes ECONNREFUSED when Bun reports the token URL in path or a SOCKS5 transport reports syscall: "connect". It marks qualifying errors as requestNotSent; other errors are rethrown.
Verify refresh intent and retry behavior
tests/oauth/oauth-refresh-connection-refused.test.ts:1-101, structure/providers/anthropic-account-pool.md:342-347, scripts/test-layout/layout.json:1313, tests/fixtures/test-layout-expected.json:1123
Tests cover real HTTP-proxy and SOCKS5 refusals, plus ambiguous refusals and a connection reset. The documentation and test-layout mappings include the new regression test.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Sequence Diagram(s)

sequenceDiagram
  participant OAuthRefresh
  participant postJson
  participant ProxyTransport as Bun fetch or SOCKS5 transport
  participant RefreshIntent
  OAuthRefresh->>postJson: send token request
  postJson->>ProxyTransport: fetch token URL
  ProxyTransport-->>postJson: qualifying ECONNREFUSED
  postJson-->>OAuthRefresh: return error with requestNotSent true
  OAuthRefresh->>RefreshIntent: clear refresh intent
  OAuthRefresh->>postJson: retry refresh
Loading

Suggested labels: bug

Suggested reviewers: luvs01

Merge Risk: Minimal · up to 83630

The change lets Anthropic OAuth refresh retry after a proven proxy connection refusal and keeps the replay protection for ambiguous failures. No merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: an Anthropic OAuth refresh stays retryable when the first connection is refused. The summary, objectives, and linked issue #8577 all describe this change. The title us…
Linked Issues check Passed Linked issue #8577 is open and direct, so its coding requirements apply. The reviewed head implements the narrow replay-release rule in src/oauth/anthropic.ts. The helper connectionRefusedBeforeSend r…
Out of Scope Changes check Passed The changed files are src/oauth/anthropic.ts (the connection-refused helper and postJson branch), tests/oauth/oauth-refresh-connection-refused.test.ts (regression and control tests for #8577), structu…
Full details: Title check

Explanation

The title names the main change: an Anthropic OAuth refresh stays retryable when the first connection is refused. The summary, objectives, and linked issue #8577 all describe this change. The title uses the fix(oauth) convention and is specific, under 100 characters, and free of file lists or vague terms.

Full details: Linked Issues check

Explanation

Linked issue #8577 is open and direct, so its coding requirements apply. The reviewed head implements the narrow replay-release rule in src/oauth/anthropic.ts. The helper connectionRefusedBeforeSend returns true only for code === "ECONNREFUSED" plus either path === the request URL (Bun fetch) or syscall === "connect" (node:net, as used by src/lib/socks5-fetch.ts connectSocket, which rejects with the raw net error). postJson then throws AnthropicTokenError(..., requestNotSent=true) for that shape. This covers the issue requirement to retry only on transport evidence that the request was not sent. Ambiguous outcomes keep the replay guard: other fetch errors are rethrown, redirects throw an 'outcome unknown' error, and generic ECONNREFUSED text without the structured fields returns false. This matches the issue's requirement to avoid treating generic ECONNREFUSED strings as proof. The issue's optional sanitized diagnostics are not implemented, and the issue marks them optional. Limits of this review: I read only the source for anthropic.ts and the head of socks5-fetch.ts. I did not read the new test file, so the behavior claims in the PR's test results were not independently checked. I also did not confirm from the truncated socks5 output how global fetch is routed to socks5Fetch for this path. The runtime error shapes (Bun path field, node:net syscall) are taken from the code and PR text, not from a live run.

Full details: Out of Scope Changes check

Explanation

The changed files are src/oauth/anthropic.ts (the connection-refused helper and postJson branch), tests/oauth/oauth-refresh-connection-refused.test.ts (regression and control tests for #8577), structure/providers/anthropic-account-pool.md (documents the new unsent-request boundary), scripts/test-layout/layout.json and tests/fixtures/test-layout-expected.json (map the new test to the oauth group). Each change supports the #8577 objective or its test coverage. The docs and layout entries are supporting changes for the new test. No unrelated behavior change appears in the reviewed source. The test file and doc diff were not read directly, so this verdict rests on the PR summary for those files.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 180ecc7 and 83630a9.

Files selected for processing: 5

  • scripts/test-layout/layout.json
  • src/oauth/anthropic.ts
  • structure/providers/anthropic-account-pool.md
  • tests/fixtures/test-layout-expected.json
  • tests/oauth/oauth-refresh-connection-refused.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@lidge-jun

Copy link
Copy Markdown
Owner Author

Maintainer judgment

Value. This fixes the daily forced re-login on Windows when OpenCodex starts before its local proxy (#8577). A refused connect means the refresh token never left the machine, so the replay guard is released and the next attempt refreshes normally.

UX change scope. Only the refused-before-send case changes: that account no longer flips to needsReauth. Every other refresh failure (reset, timeout, ERR_PROXY_TUNNEL, refusal for another URL, unstructured text) keeps the existing guard and behavior.

Regression evidence for existing users. Anthropic rotates refresh tokens, so the risk would be replaying a consumed token. Both reviewers probed Bun 1.4.4's real transport shapes. A refusal with path equal to the token URL, and the SOCKS5 refusal with syscall: "connect", only occur before any request bytes are written. A proxy that accepts and then drops surfaces as ECONNRESET or EPROTO and stays guarded. Clearing the intent stays CAS-guarded on attemptId.

Windows impact. This is the reporter's platform; the fix is transport-level and platform-neutral. Windows CI runs only in the manual dev batch.

Reviews on head 83630a9ca. The correctness review (combo/deepseek-flash-pool) and the separate security review (combo/deepseek-flash-pool) both ran 73 tests, 0 failing, and both approved (APPROVE / SECURITY_APPROVE). Low, non-blocking follow-ups:

  • Tag the SOCKS5 connect error inside socks5-fetch.ts so the syscall: "connect" branch cannot trust a future transport by accident.
  • Add a real CONNECT-then-reset listener test, a reachable-proxy ERR_PROXY_TUNNEL case, and the tokenRequests()===2 / needsReauth assertions on the SOCKS5 case.
  • Fix the test comment that calls the destination loopback; only the proxy is.

Verification scope. Focused OAuth/provider tests passed (141/0), as did typecheck, structure:check, privacy:scan, layout, ratchet and Lab guards. test:changed had 7 failures; each was rerun alone and matched cargo absence, an identical failure on dev, or a 5 s timing flake. PR CI on 83630a9ca completed green (gates, test 1-5/5, structure gate, docker smoke, npm-global).

NOT RUN: an end-to-end run against a real FlClash proxy or the live Anthropic token endpoint; macOS/Windows CI (manual dev batch).

Merging at head 83630a9ca73a19d9588e5afde516fb940615f945.

@lidge-jun
lidge-jun merged commit e24c1dd into dev Nov 12, 2026
37 checks passed
@lidge-jun
lidge-jun deleted the codex/8577-proxy-refusal-refresh branch November 12, 2026 02:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug][Windows] Anthropic OAuth refresh requires re-login after local proxy connection refusal at startup

1 participant