Skip to content

Commit 2a7f7f8

Browse files
committed
Use branch-scoped events for auth responses
1 parent 9670ce2 commit 2a7f7f8

2 files changed

Lines changed: 37 additions & 5 deletions

File tree

src/google/adk/auth/auth_preprocessor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ async def run_async(
134134
agent = invocation_context.agent
135135
if not hasattr(agent, 'canonical_tools'):
136136
return
137-
events = invocation_context.session.events
137+
events = invocation_context._get_events(current_branch=True)
138138
if not events:
139139
return
140140

tests/unittests/auth/test_auth_preprocessor.py

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def mock_invocation_context(self, mock_llm_agent, mock_session):
6969
context = Mock(spec=InvocationContext)
7070
context.agent = mock_llm_agent
7171
context.session = mock_session
72+
context._get_events.side_effect = lambda **_: context.session.events
7273
return context
7374

7475
@pytest.fixture
@@ -165,8 +166,7 @@ async def test_non_llm_agent_returns_early(
165166
):
166167
"""Test that non-LLM agents return early."""
167168
mock_context = Mock(spec=InvocationContext)
168-
mock_context.agent = Mock()
169-
mock_context.agent.__class__.__name__ = 'BaseAgent'
169+
mock_context.agent = object()
170170
mock_context.session = mock_session
171171

172172
result = []
@@ -273,6 +273,38 @@ async def test_last_event_no_auth_responses_returns_early(
273273

274274
assert result == []
275275

276+
@pytest.mark.asyncio
277+
@patch('google.adk.auth.auth_preprocessor.AuthHandler')
278+
@patch('google.adk.auth.auth_tool.AuthConfig.model_validate')
279+
async def test_ignores_auth_responses_outside_current_branch(
280+
self,
281+
mock_auth_config_validate,
282+
mock_auth_handler_class,
283+
processor,
284+
mock_invocation_context,
285+
mock_llm_request,
286+
mock_user_event_with_auth_response,
287+
):
288+
"""Test auth responses hidden by branch filtering are ignored."""
289+
mock_invocation_context.session.events = [
290+
mock_user_event_with_auth_response
291+
]
292+
mock_invocation_context._get_events.side_effect = None
293+
mock_invocation_context._get_events.return_value = []
294+
295+
result = []
296+
async for event in processor.run_async(
297+
mock_invocation_context, mock_llm_request
298+
):
299+
result.append(event)
300+
301+
mock_invocation_context._get_events.assert_called_once_with(
302+
current_branch=True
303+
)
304+
mock_auth_config_validate.assert_not_called()
305+
mock_auth_handler_class.assert_not_called()
306+
assert result == []
307+
276308
@pytest.mark.asyncio
277309
@patch('google.adk.auth.auth_preprocessor.AuthHandler')
278310
@patch('google.adk.auth.auth_tool.AuthConfig.model_validate')
@@ -534,9 +566,9 @@ async def test_isinstance_check_for_llm_agent(
534566
"""Test that isinstance check works correctly for LlmAgent."""
535567
# This test ensures the isinstance check work as expected
536568

537-
# Create a mock that fails isinstance check
569+
# Create an object that does not expose canonical_tools.
538570
mock_context = Mock(spec=InvocationContext)
539-
mock_context.agent = Mock() # This will fail isinstance(agent, LlmAgent)
571+
mock_context.agent = object()
540572
mock_context.session = mock_session
541573

542574
result = []

0 commit comments

Comments
 (0)