Skip to content

Commit f4d7c89

Browse files
Aayush KatariaCopilot
authored andcommitted
Cache push_to_cosmos embeddings; clarify non-turn thread_id
- push_to_cosmos (sync + async): persist generated embeddings back to local_memory so a repeat push doesn't re-embed already-embedded non-turn records. - Update test comment: _make_memory auto-generates a UUID thread_id for non-turn types when omitted (it does not user-scope them). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent c5ece86 commit f4d7c89

3 files changed

Lines changed: 28 additions & 1 deletion

File tree

agent_memory_toolkit/aio/cosmos_memory_client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,9 @@ async def push_to_cosmos(self, batch_size: int = 25) -> None:
758758
vectors = await self._embeddings_client.generate_batch(to_embed_text)
759759
for i, vec in zip(to_embed_idx, vectors):
760760
bodies[i]["embedding"] = vec
761+
# Persist the embedding back to local_memory so a
762+
# repeat push_to_cosmos() doesn't re-embed.
763+
self.local_memory[start + i]["embedding"] = vec
761764
except Exception as exc: # noqa: BLE001
762765
logger.warning(
763766
"push_to_cosmos: batch embedding generation failed (%s); "

agent_memory_toolkit/cosmos_memory_client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,9 @@ def push_to_cosmos(self, batch_size: int = 25) -> None:
995995
vectors = self._embeddings_client.generate_batch(to_embed_text)
996996
for i, vec in zip(to_embed_idx, vectors):
997997
bodies[i]["embedding"] = vec
998+
# Persist the embedding back to local_memory so a
999+
# repeat push_to_cosmos() doesn't re-embed.
1000+
self.local_memory[start + i]["embedding"] = vec
9981001
except Exception as exc: # noqa: BLE001
9991002
logger.warning(
10001003
"push_to_cosmos: batch embedding generation failed (%s); "

tests/unit/test_cosmos_memory_client.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ def test_add_local_turn_requires_thread_id(self):
138138

139139
def test_add_local_non_turn_thread_id_optional(self):
140140
mem = _make_client()
141-
# Non-turn types (summary, fact, etc.) are user-scoped; thread_id is optional.
141+
# Non-turn types (summary, fact, etc.) accept an omitted thread_id;
142+
# _make_memory auto-generates a UUID for hierarchical-PK validity.
142143
mem.add_local(user_id="u1", role="user", content="profile", memory_type="user_summary")
143144
assert len(mem.local_memory) == 1
144145

@@ -444,6 +445,26 @@ def _generate_batch(texts: list[str]) -> list[list[float]]:
444445
assert fact_body["embedding"] == [0.1, 0.2, 0.3]
445446
assert "embedding" not in turn_body
446447

448+
def test_push_to_cosmos_caches_embeddings_in_local_memory(self):
449+
"""Repeat push_to_cosmos() must not re-embed the same non-turn records."""
450+
mem, container = _connected_client()
451+
embed_calls: list[list[str]] = []
452+
453+
def _generate_batch(texts: list[str]) -> list[list[float]]:
454+
embed_calls.append(list(texts))
455+
return [[0.5, 0.6, 0.7] for _ in texts]
456+
457+
mem._embeddings_client = MagicMock()
458+
mem._embeddings_client.generate_batch.side_effect = _generate_batch
459+
460+
mem.add_local(user_id="u1", role="user", content="fact one", memory_type="fact")
461+
mem.push_to_cosmos()
462+
mem.push_to_cosmos()
463+
464+
# Second push should not re-embed — embedding is cached on local_memory.
465+
assert embed_calls == [["fact one"]]
466+
assert mem.local_memory[0]["embedding"] == [0.5, 0.6, 0.7]
467+
447468

448469
class TestGetMemories:
449470
def test_no_filters(self):

0 commit comments

Comments
 (0)