-
Notifications
You must be signed in to change notification settings - Fork 48.1k
fix(gateway): install _profile_runtime_scope in _run_background_task when multiplexing is active #60746
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
Closed
liuhao1024
wants to merge
2
commits into
NousResearch:main
from
liuhao1024:liuhao/cron-bugfix-60726-multiplex-background-scope
Closed
fix(gateway): install _profile_runtime_scope in _run_background_task when multiplexing is active #60746
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,77 @@ | ||
| """Regression: background tasks respect profile secret scope when multiplexing. | ||
|
|
||
| Issue #60726: /background command runs _run_background_task without a profile | ||
| scope, causing UnscopedSecretError when multiplexing is active and credentials | ||
| are profile-scoped. | ||
| """ | ||
| import pytest | ||
| from unittest import mock | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| class TestBackgroundTaskProfileScope: | ||
| """_run_background_task installs _profile_runtime_scope when multiplexing is active.""" | ||
|
|
||
| def test_background_task_calls_inner_wrapped_in_scope_when_multiplex_active(self): | ||
| """When multiplex_profiles is True, _run_background_task wraps call in _profile_runtime_scope.""" | ||
| from gateway.run import GatewayRunner | ||
|
|
||
| config = {"multiplex_profiles": True} | ||
| gw = GatewayRunner(config=config) | ||
| gw._session_db = mock.MagicMock() | ||
| gw._adapter_for_source = mock.MagicMock(return_value=mock.MagicMock()) | ||
|
|
||
| mock_inner = mock.AsyncMock(return_value=None) | ||
| gw._run_background_task_inner = mock_inner | ||
|
|
||
| import asyncio | ||
| source = mock.MagicMock() | ||
| source.profile = "test_profile" | ||
|
|
||
| # Mock _resolve_profile_home_for_source to return a known path | ||
| with mock.patch.object(gw, "_resolve_profile_home_for_source", return_value=Path("/fake/profile")): | ||
| # Mock _profile_runtime_scope | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mocking |
||
| from gateway.run import _profile_runtime_scope | ||
| with mock.patch("gateway.run._profile_runtime_scope") as mock_scope: | ||
| mock_scope.return_value.__enter__ = mock.MagicMock() | ||
| mock_scope.return_value.__exit__ = mock.MagicMock() | ||
|
|
||
| asyncio.run( | ||
| gw._run_background_task( | ||
| prompt="test", | ||
| source=source, | ||
| task_id="test_task", | ||
| ) | ||
| ) | ||
|
|
||
| # _profile_runtime_scope should have been called with profile_home (Path object) | ||
| mock_scope.assert_called_once_with(Path("/fake/profile")) | ||
| mock_inner.assert_called_once() | ||
|
|
||
| def test_background_task_calls_inner_direct_when_multiplex_disabled(self): | ||
| """When multiplex_profiles is False, _run_background_task calls inner directly.""" | ||
| from gateway.run import GatewayRunner | ||
|
|
||
| config = {"multiplex_profiles": False} | ||
| gw = GatewayRunner(config=config) | ||
| gw._session_db = mock.MagicMock() | ||
| gw._adapter_for_source = mock.MagicMock(return_value=mock.MagicMock()) | ||
|
|
||
| mock_inner = mock.AsyncMock(return_value=None) | ||
| gw._run_background_task_inner = mock_inner | ||
|
|
||
| import asyncio | ||
| source = mock.MagicMock() | ||
|
|
||
| with mock.patch("gateway.run._profile_runtime_scope") as mock_scope: | ||
| asyncio.run( | ||
| gw._run_background_task( | ||
| prompt="test", | ||
| source=source, | ||
| task_id="test_task", | ||
| ) | ||
| ) | ||
|
|
||
| # _profile_runtime_scope should NOT have been called | ||
| mock_scope.assert_not_called() | ||
| mock_inner.assert_called_once() | ||
Oops, something went wrong.
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.
This production API change is only needed by the new test. Please construct
GatewayConfig(multiplex_profiles=...)in the test instead; real config mappings are normalized throughGatewayConfig.from_dict, not direct dataclass construction.