1515import logging
1616from unittest import mock
1717
18+ from google .adk .events .event import Event
1819from google .adk .memory .base_memory_service import SearchMemoryResponse
20+ from google .adk .memory .in_memory_memory_service import InMemoryMemoryService
1921from google .adk .memory .memory_entry import MemoryEntry
2022from google .adk .models .gemini_context_cache_manager import GeminiContextCacheManager
2123from google .adk .models .llm_request import LlmRequest
24+ from google .adk .sessions .session import Session
2225from google .adk .tools .preload_memory_tool import PreloadMemoryTool
2326from google .genai import types
2427import pytest
@@ -41,6 +44,132 @@ def _memory(text: str) -> MemoryEntry:
4144 )
4245
4346
47+ @pytest .mark .parametrize ('thought' , [True , False , None ])
48+ @pytest .mark .asyncio
49+ async def test_preload_memory_preserves_visible_text_only (
50+ thought : bool | None ,
51+ ) -> None :
52+ """Recalled reasoning is not presented as a previous conversation answer."""
53+ memory = MemoryEntry (
54+ content = types .Content (
55+ role = 'model' ,
56+ parts = [
57+ types .Part (text = 'Consider a zeppelin.' , thought = thought ),
58+ types .Part (text = 'Take the train to Paris.' ),
59+ ],
60+ ),
61+ author = 'assistant' ,
62+ )
63+ request = LlmRequest ()
64+ original_content = memory .content .model_copy (deep = True )
65+
66+ await PreloadMemoryTool ().process_llm_request (
67+ tool_context = _tool_context (memory ), llm_request = request
68+ )
69+
70+ assert request .contents [0 ].parts
71+ recalled_text = request .contents [0 ].parts [0 ].text
72+ assert recalled_text is not None
73+ assert 'Take the train to Paris.' in recalled_text
74+ assert ('Consider a zeppelin.' in recalled_text ) == (not thought )
75+ assert memory .content == original_content
76+
77+
78+ @pytest .mark .parametrize (
79+ 'parts' ,
80+ [
81+ [],
82+ [types .Part (text = 'Consider a zeppelin.' , thought = True )],
83+ [
84+ types .Part (
85+ inline_data = types .Blob (data = b'hello' , mime_type = 'text/plain' )
86+ )
87+ ],
88+ ],
89+ )
90+ @pytest .mark .asyncio
91+ async def test_preload_memory_omits_entries_without_visible_text (
92+ parts : list [types .Part ],
93+ ) -> None :
94+ """Empty, non-text and thought-only memories do not add conversation text."""
95+ memory = MemoryEntry (
96+ content = types .Content (role = 'model' , parts = parts ),
97+ author = 'assistant' ,
98+ timestamp = '2026-07-13T12:00:00Z' ,
99+ )
100+ request = LlmRequest ()
101+
102+ await PreloadMemoryTool ().process_llm_request (
103+ tool_context = _tool_context (memory ), llm_request = request
104+ )
105+
106+ assert not request .contents
107+
108+
109+ @pytest .mark .asyncio
110+ async def test_preload_memory_in_memory_service_skips_thought_parts () -> None :
111+ """Thought-only events do not match or crowd out visible search results."""
112+ memory_service = InMemoryMemoryService ()
113+ events = [
114+ Event (
115+ id = f'thought-{ i } ' ,
116+ author = 'model' ,
117+ timestamp = float (i ),
118+ content = types .Content (
119+ role = 'model' ,
120+ parts = [
121+ types .Part (text = 'Paris travel plan' , thought = True ),
122+ types .Part .from_function_call (name = 'lookup' , args = {}),
123+ ],
124+ ),
125+ )
126+ for i in range (10 )
127+ ]
128+ events .append (
129+ Event (
130+ id = 'visible' ,
131+ author = 'model' ,
132+ timestamp = 11.0 ,
133+ content = types .Content (
134+ role = 'model' ,
135+ parts = [
136+ types .Part (text = 'Secret internal note' , thought = True ),
137+ types .Part (text = 'Take the train to Paris.' ),
138+ ],
139+ ),
140+ )
141+ )
142+ await memory_service .add_session_to_memory (
143+ Session (
144+ app_name = 'app' ,
145+ user_id = 'user' ,
146+ id = 'session-1' ,
147+ events = events ,
148+ )
149+ )
150+
151+ async def search_memory (query : str ) -> SearchMemoryResponse :
152+ return await memory_service .search_memory (
153+ app_name = 'app' , user_id = 'user' , query = query
154+ )
155+
156+ tool_context = mock .Mock ()
157+ tool_context .user_content = types .UserContent ('Paris' )
158+ tool_context .search_memory = search_memory
159+ request = LlmRequest ()
160+
161+ await PreloadMemoryTool ().process_llm_request (
162+ tool_context = tool_context , llm_request = request
163+ )
164+
165+ assert request .contents [0 ].parts
166+ recalled_text = request .contents [0 ].parts [0 ].text
167+ assert recalled_text is not None
168+ assert 'Take the train to Paris.' in recalled_text
169+ assert 'Paris travel plan' not in recalled_text
170+ assert 'Secret internal note' not in recalled_text
171+
172+
44173@pytest .mark .asyncio
45174async def test_preload_memory_keeps_system_prefix_stable ():
46175 """Recalled memory goes into contents, never into the system instruction."""
0 commit comments