-
Notifications
You must be signed in to change notification settings - Fork 52.4k
fix(runtime): exchange raw Copilot OAuth token in runtime resolution #58830
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SvichkarevAnatoly
wants to merge
1
commit into
NousResearch:main
Choose a base branch
from
SvichkarevAnatoly:fix/copilot-token-exchange-runtime
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+113
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| """Regression tests for Copilot raw-token exchange during runtime resolution. | ||
|
|
||
| A raw GitHub OAuth token (``ghu_...``) sent as the Bearer to | ||
| ``api.githubcopilot.com`` makes GitHub ignore ``Copilot-Integration-Id: | ||
| vscode-chat`` and pin the request to integrator ``copilot-language-server``, | ||
| whose model allow-list is tiny (only a handful of GPT models). Claude, Gemini | ||
| and most GPT models then fail with HTTP 400 | ||
| ``model_not_available_for_integrator``. The credential pool can end up holding | ||
| the raw token (the env seeder overwrites the exchanged singleton entry under | ||
| the same source key), so runtime resolution must defensively exchange a raw | ||
| token for a short-lived Copilot API token (``tid=...``) before use. | ||
|
|
||
| These tests pin that behaviour without any network access. | ||
| """ | ||
| from unittest.mock import patch | ||
|
|
||
| from hermes_cli import runtime_provider as rp | ||
|
|
||
|
|
||
| def test_looks_like_raw_github_token_recognizes_prefixes(): | ||
| for raw in ( | ||
| "ghu_abc123", | ||
| "gho_abc123", | ||
| "ghp_abc123", | ||
| "ghs_abc123", | ||
| "github_pat_abc123", | ||
| " ghu_leading_ws ", | ||
| ): | ||
| assert rp._looks_like_raw_github_token(raw) is True | ||
|
|
||
| # Already-exchanged Copilot API tokens and empty values are not "raw". | ||
| for other in ("tid=abc;exp=123", "", "sk-proj-abc", "some-random-key"): | ||
| assert rp._looks_like_raw_github_token(other) is False | ||
|
|
||
|
|
||
| def test_exchange_copilot_api_key_exchanges_raw_token_and_adopts_base_url(): | ||
| with patch( | ||
| "hermes_cli.copilot_auth.get_copilot_api_token", | ||
| return_value=("tid=exchanged;exp=999", "https://api.business.githubcopilot.com/"), | ||
| ) as mock_exchange: | ||
| api_key, base_url = rp._exchange_copilot_api_key( | ||
| "ghu_rawtoken", "https://api.githubcopilot.com" | ||
| ) | ||
|
|
||
| mock_exchange.assert_called_once_with("ghu_rawtoken") | ||
| assert api_key == "tid=exchanged;exp=999" | ||
| # Account-specific endpoint advertised by the exchange is adopted (trailing | ||
| # slash stripped) so Business/Enterprise tenants hit the right host. | ||
| assert base_url == "https://api.business.githubcopilot.com" | ||
|
|
||
|
|
||
| def test_exchange_copilot_api_key_passes_through_already_exchanged_token(): | ||
| with patch("hermes_cli.copilot_auth.get_copilot_api_token") as mock_exchange: | ||
| api_key, base_url = rp._exchange_copilot_api_key( | ||
| "tid=already;exp=1", "https://api.githubcopilot.com" | ||
| ) | ||
|
|
||
| # No exchange attempted for a non-raw token; values pass through unchanged. | ||
| mock_exchange.assert_not_called() | ||
| assert api_key == "tid=already;exp=1" | ||
| assert base_url == "https://api.githubcopilot.com" | ||
|
|
||
|
|
||
| def test_exchange_copilot_api_key_keeps_base_url_when_exchange_returns_none(): | ||
| # get_copilot_api_token returns (raw_token, None) on exchange failure; the | ||
| # original base_url must be preserved rather than clobbered with None. | ||
| with patch( | ||
| "hermes_cli.copilot_auth.get_copilot_api_token", | ||
| return_value=("ghu_rawtoken", None), | ||
| ): | ||
| api_key, base_url = rp._exchange_copilot_api_key( | ||
| "ghu_rawtoken", "https://api.githubcopilot.com" | ||
| ) | ||
|
|
||
| assert api_key == "ghu_rawtoken" | ||
| assert base_url == "https://api.githubcopilot.com" | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a regression test through
_resolve_runtime_from_pool_entry()with a rawghu_...entry. These helper-only tests do not prove the changed pooled resolver returns the exchanged key and enterprise base URL.