| 1 |
"""The sync's two passes: a content-free listing, then targeted fetches. |
| 2 |
|
| 3 |
A sync lists shas only (`basic` view) and fetches a memory's body just |
| 4 |
before writing it to disk; only the download takes `full` pages, since |
| 5 |
it needs every memory anyway. These tests pin that fetch discipline: a |
| 6 |
sync that writes nothing fetches nothing, a changed path fetches exactly |
| 7 |
itself, fetches fan out rather than queue, and a fetch that fails — or |
| 8 |
races a server-side delete — leaves the old state for the next sync. |
| 9 |
""" |
| 10 |
|
| 11 |
from __future__ import annotations |
| 12 |
|
| 13 |
from typing import Any |
| 14 |
from pathlib import Path |
| 15 |
|
| 16 |
import pytest |
| 17 |
|
| 18 |
from anthropic.lib.tools._memories import SessionMemoryStores |
| 19 |
|
| 20 |
from .conftest import run_sync |
| 21 |
from ._fake_anthropic import MemoryServer, updated, _status_error, fake_anthropic |
| 22 |
|
| 23 |
|
| 24 |
async def _downloaded(tmp_path: Path, initial: dict[str, str]) -> tuple[Path, MemoryServer, SessionMemoryStores]: |
| 25 |
"""A `SessionMemoryStores` with one store already downloaded to disk.""" |
| 26 |
client, server = fake_anthropic(initial) |
| 27 |
stores = SessionMemoryStores(client, workdir=tmp_path) |
| 28 |
await stores.download(await client.beta.sessions.retrieve("s1")) |
| 29 |
return tmp_path / "memory" / "notes", server, stores |
| 30 |
|
| 31 |
|
| 32 |
@pytest.mark.asyncio() |
| 33 |
async def test_a_sync_that_pulls_nothing_fetches_no_content(tmp_path: Path) -> None: |
| 34 |
local, server, stores = await _downloaded(tmp_path, {"a.md": "v1", "b.md": "v1", "c.md": "v1"}) |
| 35 |
# The download took full listing pages: no single-memory fetches at all. |
| 36 |
assert server.content_fetches == [] |
| 37 |
|
| 38 |
(local / "a.md").write_text("v2 from agent") # a push needs no remote content |
| 39 |
await run_sync(stores) |
| 40 |
|
| 41 |
assert server.content_fetches == [] |
| 42 |
assert server.files["a.md"] == "v2 from agent" |
| 43 |
|
| 44 |
|
| 45 |
@pytest.mark.asyncio() |
| 46 |
async def test_only_the_changed_memory_is_fetched(tmp_path: Path) -> None: |
| 47 |
local, server, stores = await _downloaded(tmp_path, {"a.md": "v1", "b.md": "v1"}) |
| 48 |
server.content_fetches.clear() |
| 49 |
|
| 50 |
server.write("a.md", "v2 from server") |
| 51 |
await run_sync(stores) |
| 52 |
|
| 53 |
assert server.content_fetches == ["a.md"] |
| 54 |
assert (local / "a.md").read_text() == "v2 from server" |
| 55 |
assert (local / "b.md").read_text() == "v1" |
| 56 |
|
| 57 |
|
| 58 |
@pytest.mark.asyncio() |
| 59 |
async def test_a_file_already_holding_the_remote_content_is_adopted_without_a_fetch(tmp_path: Path) -> None: |
| 60 |
local, server, stores = await _downloaded(tmp_path, {"a.md": "v1"}) |
| 61 |
server.content_fetches.clear() |
| 62 |
|
| 63 |
# Both sides landed on the same bytes independently. |
| 64 |
(local / "a.md").write_text("v2 on both") |
| 65 |
server.write("a.md", "v2 on both") |
| 66 |
await run_sync(stores) |
| 67 |
|
| 68 |
assert server.content_fetches == [] |
| 69 |
assert server.received == [] |
| 70 |
|
| 71 |
# The baseline advanced to the adopted sha: the next edit pushes guarded by it. |
| 72 |
(local / "a.md").write_text("v3 from agent") |
| 73 |
await run_sync(stores) |
| 74 |
assert server.received == [updated("a.md", "v3 from agent", was="v2 on both")] |
| 75 |
|
| 76 |
|
| 77 |
@pytest.mark.asyncio() |
| 78 |
async def test_content_fetches_fan_out_within_a_store(tmp_path: Path) -> None: |
| 79 |
"""The first memory's fetch completes only after the second's starts — |
| 80 |
only parallel fetches satisfy that; serial ones would time out.""" |
| 81 |
import anyio |
| 82 |
|
| 83 |
client, server = fake_anthropic({"a.md": "v1", "b.md": "v1"}) |
| 84 |
stores = SessionMemoryStores(client, workdir=tmp_path) |
| 85 |
await stores.download(await client.beta.sessions.retrieve("s1")) |
| 86 |
local = tmp_path / "memory" / "notes" |
| 87 |
server.write("a.md", "v2 from server") |
| 88 |
server.write("b.md", "v2 from server") |
| 89 |
|
| 90 |
memories = client.beta.memory_stores.memories |
| 91 |
real_retrieve = memories.retrieve |
| 92 |
second_started = anyio.Event() |
| 93 |
|
| 94 |
async def gated(memory_id: str, **kwargs: Any) -> Any: |
| 95 |
if memory_id == "mem:a.md": |
| 96 |
with anyio.fail_after(10): |
| 97 |
await second_started.wait() |
| 98 |
else: |
| 99 |
second_started.set() |
| 100 |
return await real_retrieve(memory_id, **kwargs) |
| 101 |
|
| 102 |
memories.retrieve = gated |
| 103 |
await run_sync(stores) |
| 104 |
|
| 105 |
assert (local / "a.md").read_text() == "v2 from server" |
| 106 |
assert (local / "b.md").read_text() == "v2 from server" |
| 107 |
|
| 108 |
|
| 109 |
@pytest.mark.asyncio() |
| 110 |
async def test_a_failed_fetch_keeps_the_old_state_and_the_next_sync_retries(tmp_path: Path) -> None: |
| 111 |
local, server, stores = await _downloaded(tmp_path, {"a.md": "v1"}) |
| 112 |
server.write("a.md", "v2 from server") |
| 113 |
|
| 114 |
def boom(_path: str) -> None: |
| 115 |
raise _status_error(500) |
| 116 |
|
| 117 |
server.fetch_hook = boom |
| 118 |
await run_sync(stores) |
| 119 |
assert (local / "a.md").read_text() == "v1" |
| 120 |
|
| 121 |
server.fetch_hook = None |
| 122 |
await run_sync(stores) |
| 123 |
assert (local / "a.md").read_text() == "v2 from server" |
| 124 |
# The stale baseline drove no uploads or deletes along the way. |
| 125 |
assert server.received == [] |
| 126 |
|
| 127 |
|
| 128 |
@pytest.mark.asyncio() |
| 129 |
async def test_a_memory_deleted_between_listing_and_fetch_waits_for_the_next_sync(tmp_path: Path) -> None: |
| 130 |
local, server, stores = await _downloaded(tmp_path, {"a.md": "v1", "keep.md": "v1"}) |
| 131 |
server.write("a.md", "v2 from server") |
| 132 |
|
| 133 |
def vanish(path: str) -> None: |
| 134 |
server.files.pop(path, None) |
| 135 |
|
| 136 |
server.fetch_hook = vanish |
| 137 |
await run_sync(stores) |
| 138 |
# The fetch 404ed: disk and baseline keep the old version for now. |
| 139 |
assert (local / "a.md").read_text() == "v1" |
| 140 |
|
| 141 |
server.fetch_hook = None |
| 142 |
await run_sync(stores) |
| 143 |
# The next sync saw the server-side delete and took the local copy with it. |
| 144 |
assert not (local / "a.md").exists() |
| 145 |
assert server.received == [] |