Skip to content

Commit a178775

Browse files
Copilotfriggeri
andcommitted
Return copies of cached models list and improve test robustness
Co-authored-by: friggeri <106686+friggeri@users.noreply.github.com>
1 parent 22008db commit a178775

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

python/copilot/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ async def list_models(self) -> list["ModelInfo"]:
737737
async with self._models_cache_lock:
738738
# Check cache (already inside lock)
739739
if self._models_cache is not None:
740-
return self._models_cache
740+
return list(self._models_cache) # Return a copy to prevent cache mutation
741741

742742
# Cache miss - fetch from backend while holding lock
743743
response = await self._client.request("models.list", {})
@@ -747,7 +747,7 @@ async def list_models(self) -> list["ModelInfo"]:
747747
# Update cache before releasing lock
748748
self._models_cache = models
749749

750-
return models
750+
return list(models) # Return a copy to prevent cache mutation
751751

752752
async def list_sessions(self) -> list["SessionMetadata"]:
753753
"""

python/e2e/test_client.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,25 @@ async def test_should_cache_models_list(self):
154154
models1 = await client.list_models()
155155
assert isinstance(models1, list)
156156

157-
# Second call should return cached results (same object reference)
157+
# Second call should return from cache (different list object but same content)
158158
models2 = await client.list_models()
159-
assert models2 is models1, "Second call should return cached results"
159+
assert models2 is not models1, "Should return a copy, not the same object"
160+
assert len(models2) == len(models1), "Cached results should have same content"
161+
if len(models1) > 0:
162+
assert models1[0].id == models2[0].id, "Cached models should match"
160163

161164
# After stopping, cache should be cleared
162165
await client.stop()
163166

164167
# Restart and verify cache is empty
165168
await client.start()
169+
170+
# Check authentication again after restart
171+
auth_status = await client.get_auth_status()
172+
if not auth_status.isAuthenticated:
173+
await client.stop()
174+
return
175+
166176
models3 = await client.list_models()
167177
assert models3 is not models1, "Cache should be cleared after disconnect"
168178

0 commit comments

Comments
 (0)