Skip to content

strip inherited claude provider routing from local agent launches - #9871

Merged
mahiatlinux merged 15 commits into
unslothai:mainfrom
Souravrajvi0:feat/strip-claude-provider-env-0b9d
Aug 28, 2026
Merged

mahiatlinux merged 15 commits into
unslothai:mainfrom
Souravrajvi0:feat/strip-claude-provider-env-0b9d

Conversation

@Souravrajvi0

Copy link
Copy Markdown
Contributor

Summary

Fixes #9864.

The Claude local-agent child (and unsloth claude) inherit the parent shell environment and only strip ANTHROPIC_API_KEY / CLAUDE_CODE_OAUTH_TOKEN. Claude Code's Foundry, Bedrock, Vertex, AWS, and Mantle routing flags take precedence over ANTHROPIC_BASE_URL, so a parent configured for Azure AI Foundry (or similar) silently sends "local" traffic to the remote gateway.

This extends _CLAUDE_ENV_UNSET so those provider-routing variables are dropped before spawn. The same tuple is used by the subagent MCP bridge and the main Claude launch path.

Test plan

  • pytest on test_claude_subagent_mcp.py and the Claude --no-launch / launch env-scrub tests (22 passed)
  • Reproduced the leak with Foundry env vars present, then confirmed they are absent from the child env after the fix

Inherited CLAUDE_CODE_USE_FOUNDRY and related provider routing vars
override ANTHROPIC_BASE_URL, so the local Claude subagent silently
sends traffic to a remote gateway instead of the Unsloth server.
@Souravrajvi0
Souravrajvi0 force-pushed the feat/strip-claude-provider-env-0b9d branch from 4162205 to 01c235c Compare August 27, 2026 15:17
@oobabooga

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 01c235c4b4

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen

Copy link
Copy Markdown
Member

Confirmed unsloth_cli/commands/start.py:165 still strips only ANTHROPIC_API_KEY and CLAUDE_CODE_OAUTH_TOKEN, so an inherited Foundry or Bedrock setup overrides ANTHROPIC_BASE_URL for both the launch path and the subagent bridge. Will get this reviewed.

lonexreb and others added 2 commits August 28, 2026 20:42
Move provider-routing assertions into a focused regression test with explicit selector coverage.
@mahiatlinux

Copy link
Copy Markdown
Contributor

Reviewed the routing path against #9864 and Claude Code 2.1.247. The bug is real: Claude evaluates cloud routing selectors and settings before the local Anthropic endpoint in several cases, so inherited configuration can bypass Studio.

Confirmed findings and fixes

  1. ANTHROPIC_UNIX_SOCKET bypassed the local TCP endpoint. An actual Claude run connected only to the inherited Unix socket. Added it to _CLAUDE_ENV_UNSET in unsloth_cli/commands/start.py and covered direct and MCP launches.
  2. User, project, and local Claude settings are applied after the process environment. They could restore Foundry, Bedrock, Vertex, Mantle, AWS, or Google selectors after Unsloth cleared them. _claude_settings_overlay now blanks every conflicting selector at the command settings tier and pins the local base URL, auth token, model, and compaction values at start.py.
  3. Settings-level ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN overrode the process values. Live request capture reproduced both remote routing and cloud bearer-token leakage. The same command-tier overlay now pins both local values.
  4. A forwarded --settings option appeared after Unsloth's overlay and won CLI precedence. _claude_local_command now places the enforced local options after forwarded options, but before a standalone --, at start.py.
  5. Claude Code 2.0.14 and 2.1.97 already support --settings; only the dynamic-system-prompt flag needs the 2.1.98 gate. _claude_flags now keeps routing enforcement on older clients at start.py.
  6. A fixed settings file would let a second concurrent launch overwrite the endpoint or key used by the first. _write_claude_settings now writes private content-addressed files with mode 0600 at start.py.
  7. MCP subagents lacked command-tier protection and WSL path handling for the private settings file. The plugin now carries the file path through its private MCP environment, and the bridge passes it to Claude at start.py and claude_subagent_mcp.py.

The fixes are split across ten focused commits. Tests cover direct launches, MCP subagents, Unix sockets, settings precedence, old Claude versions, forwarded arguments, WSL path translation, file permissions, and concurrent sessions. @lonexreb's focused inherited-provider regression coverage from #9907 remains credited in commit d2cf858da.

Verification

  • Full unsloth_cli/tests: 1,050 passed, 14 skipped
  • Security suite: 407 passed, 26 skipped
  • Whole-repository ruff: passed
  • Whole-repository compileall: passed, with two existing invalid-escape warnings outside this change
  • Actual Claude Code 2.1.247 routing, settings-precedence, auth-header, Unix-socket, and forwarded-settings reproductions: passed after the fixes
  • Claude Code 2.0.14 and 2.1.97 CLI compatibility checks: passed

@mahiatlinux

Copy link
Copy Markdown
Contributor

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: a1b1b4bf9e

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@mahiatlinux

Copy link
Copy Markdown
Contributor

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: cff165701d

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread unsloth_cli/commands/start.py Resolved
@mahiatlinux

Copy link
Copy Markdown
Contributor

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 16abad94ff

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@mahiatlinux

Copy link
Copy Markdown
Contributor

Convergence update

Codex found one P2 on cff165701: forwarded arguments had been moved ahead of all enforced flags, so unsloth start claude mcp list produced claude mcp list --model ... and Claude Code 2.1.247 exited with unknown option '--model'. Commit 16abad94f now moves only forwarded --settings values ahead of Unsloth's settings overlay. The enforced model and routing flags remain before every subcommand or prompt. Both --settings VALUE and --settings=VALUE have regression coverage. The next Codex review found no issues on 16abad94f.

Final verification on the exact PR head merged with current main:

  • Targeted fork CI: repository syntax and lint passed; 1,057 CLI tests passed, 9 skipped.
  • Full fork CPU job: auto-discovered CPU tests, hardware-spoof isolation, event-loop latency, all CLI tests, and shell installer tests passed.
  • Live Claude integration: server preflight, Claude installation, and unsloth start claude end-to-end drive passed.
  • Source lint: passed.

The upstream Python 3.13 job remains red with the same baseline failures as current main: PR job has 19 failed, 34,506 passed, 232 skipped; main job has the same 19 failed, 34,506 passed, 232 skipped. None of those failures touch the four changed Claude files.

@mahiatlinux mahiatlinux left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed through convergence on 16abad94f. Confirmed fixes, focused fork CI, full CPU tests, and live Claude integration all pass.

@mahiatlinux

Copy link
Copy Markdown
Contributor

All reviewed and fixed, LGTM. CI results

@mahiatlinux
mahiatlinux merged commit 9688c79 into unslothai:main Aug 28, 2026
39 of 43 checks passed
@mahiatlinux mahiatlinux changed the title Strip Claude Foundry/Bedrock/Vertex env from local agent launches strip inherited claude provider routing from local agent launches Aug 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

5 participants