You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fixes a test isolation bug in tests/agent/test_anthropic_adapter.py. Three test classes (TestResolveAnthropicToken, TestResolveWithRefresh, TestRunOauthSetupToken) call resolve_anthropic_token() / run_oauth_setup_token(), which internally check the macOS Keychain via _read_claude_code_credentials_from_keychain() before falling back to the mocked credential sources the tests set up.
On a Mac with Claude Code installed (real OAuth credentials present in Keychain), this causes two failure modes:
Tests asserting on mocked credential values fail because the real Keychain token is returned instead (e.g. assert None == 'cc-auto-token').
Tests in TestRunOauthSetupToken that mock subprocess.run globally (to fake the claude setup-token subprocess call) also intercept the Keychain's own subprocess.run(["security", ...]) call, returning a generic MagicMock that crashes json.loads() with TypeError: the JSON object must be str, bytes or bytearray, not MagicMock.
This makes the test suite non-deterministic. It passes or fails depending on what's in the developer's real Keychain, not on the code logic being tested.
The fix adds an autouseno_keychain pytest fixture to all three affected classes, mirroring the fixture already correctly used in TestReadClaudeCodeCredentials in the same file. This blocks the Keychain lookup so these tests only exercise the credential file and env var paths they're actually meant to test.
Related Issue
Fixes #
Type of Change
🐛 Bug fix (non-breaking change that fixes an issue)
✨ New feature (non-breaking change that adds functionality)
🔒 Security fix
✅ Tests (adding or improving test coverage)
♻️ Refactor (no behavior change)
🎯 New skill (bundled or hub)
Changes Made
tests/agent/test_anthropic_adapter.py: added an autouseno_keychain pytest fixture to TestResolveAnthropicToken, TestResolveWithRefresh, and TestRunOauthSetupToken, each mocking agent.anthropic_adapter._read_claude_code_credentials_from_keychain to return None. This matches the existing pattern already used in TestReadClaudeCodeCredentials in the same file.
How to Test
On macOS with Claude Code installed and logged in (real OAuth credentials present in Keychain), run scripts/run_tests.sh tests/agent/test_anthropic_adapter.py.
Before this fix, 5 to 6 of 160 tests fail non deterministically (varies by run) with either an AssertionError (real Keychain token returned instead of mocked value) or TypeError: the JSON object must be str, bytes or bytearray, not MagicMock (Keychain subprocess call intercepted by an unrelated subprocess.run mock).
After this fix, all 160 tests pass consistently. scripts/run_tests.sh tests/agent/test_anthropic_adapter.py returns 160 tests passed, 0 failed.
Checklist
Code
I've read the Contributing Guide
My commit messages follow Conventional Commits (fix(scope):, feat(scope):, etc.)
I searched for existing PRs to make sure this isn't a duplicate
My PR contains only changes related to this fix/feature (no unrelated commits)
I've run pytest tests/ -q and all tests pass
I've added tests for my changes (required for bug fixes, strongly encouraged for features)
I've tested on my platform: macOS 15 (Sequoia), Python 3.12.12
Duplicate of #43344 — same fix: both PRs add an autouseno_keychain fixture monkeypatching agent.anthropic_adapter._read_claude_code_credentials_from_keychain to None on the same three test classes (TestResolveAnthropicToken, TestResolveWithRefresh, TestRunOauthSetupToken) in tests/agent/test_anthropic_adapter.py. #43344 (open) is the earlier submission. The bug is still live on main (only TestReadClaudeCodeCredentials carries the fixture), so the fix is valid — but the two PRs are identical in mechanism.
Closing in favor of #43344 (same fix, opened first). Left a comment there confirming I independently hit and verified the same bug + fix, hopefully that helps move it along since it looks like it's been waiting on review for a couple weeks.
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
area/authAuthentication, OAuth, credential poolscomp/agentCore agent runtime: loop, agent_init, prompt builder, context-compression, responses endpointduplicateThis issue or pull request already existsP3Low — cosmetic, nice to haveprovider/anthropicAnthropic native Messages APItype/testTest coverage or test infrastructure
2 participants
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.
What does this PR do?
Fixes a test isolation bug in
tests/agent/test_anthropic_adapter.py. Three test classes (TestResolveAnthropicToken,TestResolveWithRefresh,TestRunOauthSetupToken) callresolve_anthropic_token()/run_oauth_setup_token(), which internally check the macOS Keychain via_read_claude_code_credentials_from_keychain()before falling back to the mocked credential sources the tests set up.On a Mac with Claude Code installed (real OAuth credentials present in Keychain), this causes two failure modes:
assert None == 'cc-auto-token').TestRunOauthSetupTokenthat mocksubprocess.runglobally (to fake theclaude setup-tokensubprocess call) also intercept the Keychain's ownsubprocess.run(["security", ...])call, returning a genericMagicMockthat crashesjson.loads()withTypeError: the JSON object must be str, bytes or bytearray, not MagicMock.This makes the test suite non-deterministic. It passes or fails depending on what's in the developer's real Keychain, not on the code logic being tested.
The fix adds an
autouseno_keychainpytest fixture to all three affected classes, mirroring the fixture already correctly used inTestReadClaudeCodeCredentialsin the same file. This blocks the Keychain lookup so these tests only exercise the credential file and env var paths they're actually meant to test.Related Issue
Fixes #
Type of Change
Changes Made
tests/agent/test_anthropic_adapter.py: added anautouseno_keychainpytest fixture toTestResolveAnthropicToken,TestResolveWithRefresh, andTestRunOauthSetupToken, each mockingagent.anthropic_adapter._read_claude_code_credentials_from_keychainto returnNone. This matches the existing pattern already used inTestReadClaudeCodeCredentialsin the same file.How to Test
scripts/run_tests.sh tests/agent/test_anthropic_adapter.py.AssertionError(real Keychain token returned instead of mocked value) orTypeError: the JSON object must be str, bytes or bytearray, not MagicMock(Keychain subprocess call intercepted by an unrelatedsubprocess.runmock).scripts/run_tests.sh tests/agent/test_anthropic_adapter.pyreturns 160 tests passed, 0 failed.Checklist
Code
fix(scope):,feat(scope):, etc.)pytest tests/ -qand all tests passDocumentation & Housekeeping
docs/, docstrings) — N/Acli-config.yaml.exampleif I added/changed config keys — N/ACONTRIBUTING.mdorAGENTS.mdif I changed architecture or workflows — N/AScreenshots / Logs
Before fix (failing)
After fix (passing)