| 1 |
"""Never destroy the only copy of a local edit: a memory deleted remotely |
| 2 |
while its file holds an un-pushed edit keeps the file — a writable store |
| 3 |
re-creates the memory from it, a read-only store keeps it on disk unsynced. |
| 4 |
Only an unedited file follows the remote delete off the disk. |
| 5 |
""" |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
from pathlib import Path |
| 10 |
|
| 11 |
import pytest |
| 12 |
|
| 13 |
from anthropic.lib.tools._memories import SessionMemoryStores |
| 14 |
|
| 15 |
from .conftest import run_sync, listing_interrupted_by |
| 16 |
from ._fake_anthropic import MemoryServer, created, updated, fake_anthropic |
| 17 |
|
| 18 |
LOGGER = "anthropic.lib.tools.agent_toolset" |
| 19 |
|
| 20 |
|
| 21 |
async def _downloaded( |
| 22 |
tmp_path: Path, initial: dict[str, str], *, access: str | None = None |
| 23 |
) -> tuple[Path, MemoryServer, SessionMemoryStores]: |
| 24 |
"""A `SessionMemoryStores` with one store already downloaded to disk.""" |
| 25 |
client, server = fake_anthropic(initial, access=access) |
| 26 |
stores = SessionMemoryStores(client, workdir=tmp_path) |
| 27 |
await stores.download(await client.beta.sessions.retrieve("s1")) |
| 28 |
return tmp_path / "memory" / "notes", server, stores |
| 29 |
|
| 30 |
|
| 31 |
@pytest.mark.asyncio() |
| 32 |
async def test_an_edited_file_survives_a_remote_delete_and_is_re_created( |
| 33 |
tmp_path: Path, caplog: pytest.LogCaptureFixture |
| 34 |
) -> None: |
| 35 |
caplog.set_level("INFO", logger=LOGGER) |
| 36 |
local, server, stores = await _downloaded(tmp_path, {"edited.md": "v1", "plain.md": "v1"}) |
| 37 |
(local / "edited.md").write_text("v2 from agent") |
| 38 |
server.delete("edited.md") |
| 39 |
server.delete("plain.md") |
| 40 |
|
| 41 |
await run_sync(stores) |
| 42 |
|
| 43 |
# The edit's only copy survives and the memory is re-created from it; |
| 44 |
# the unedited file follows the remote delete off the disk. |
| 45 |
assert (local / "edited.md").read_text() == "v2 from agent" |
| 46 |
assert server.files == {"edited.md": "v2 from agent"} |
| 47 |
assert server.received == [created("edited.md", "v2 from agent")] |
| 48 |
assert not (local / "plain.md").exists() |
| 49 |
assert "re-creating it from the file" in caplog.text |
| 50 |
|
| 51 |
# Settled: nothing more goes out. |
| 52 |
await run_sync(stores) |
| 53 |
assert server.received == [created("edited.md", "v2 from agent")] |
| 54 |
|
| 55 |
# The upload's sha became the baseline: the next edit is a guarded update. |
| 56 |
(local / "edited.md").write_text("v3") |
| 57 |
await run_sync(stores) |
| 58 |
assert server.received[-1] == updated("edited.md", "v3", was="v2 from agent") |
| 59 |
|
| 60 |
|
| 61 |
@pytest.mark.asyncio() |
| 62 |
async def test_a_read_only_store_keeps_the_edited_file_without_pushing( |
| 63 |
tmp_path: Path, caplog: pytest.LogCaptureFixture |
| 64 |
) -> None: |
| 65 |
caplog.set_level("WARNING", logger=LOGGER) |
| 66 |
local, server, stores = await _downloaded(tmp_path, {"edited.md": "v1", "plain.md": "v1"}, access="read_only") |
| 67 |
(local / "edited.md").write_text("v2 from agent") |
| 68 |
server.delete("edited.md") |
| 69 |
server.delete("plain.md") |
| 70 |
|
| 71 |
await run_sync(stores) |
| 72 |
|
| 73 |
assert (local / "edited.md").read_text() == "v2 from agent" |
| 74 |
assert not (local / "plain.md").exists() |
| 75 |
assert server.received == [] |
| 76 |
assert caplog.text.count("cannot push") == 1 |
| 77 |
|
| 78 |
# Later passes see a local-only file in a read-only store: a quiet no-op. |
| 79 |
await run_sync(stores) |
| 80 |
await run_sync(stores) |
| 81 |
assert server.received == [] |
| 82 |
assert (local / "edited.md").read_text() == "v2 from agent" |
| 83 |
assert caplog.text.count("cannot push") == 1 |
| 84 |
|
| 85 |
|
| 86 |
@pytest.mark.asyncio() |
| 87 |
async def test_a_refused_edit_survives_a_remote_delete_without_a_retry(tmp_path: Path) -> None: |
| 88 |
"""The server already refused this content, so the remote delete neither |
| 89 |
removes the file nor triggers a re-create the server would refuse again.""" |
| 90 |
local, server, stores = await _downloaded(tmp_path, {"big.md": "v1"}) |
| 91 |
server.max_content_bytes = 64 |
| 92 |
|
| 93 |
(local / "big.md").write_text("x" * 100) |
| 94 |
await run_sync(stores) |
| 95 |
assert len(server.received) == 1 # the one refused update |
| 96 |
|
| 97 |
server.delete("big.md") |
| 98 |
await run_sync(stores) |
| 99 |
await run_sync(stores) |
| 100 |
|
| 101 |
assert (local / "big.md").read_text() == "x" * 100 |
| 102 |
assert len(server.received) == 1 |
| 103 |
assert "big.md" not in server.files |
| 104 |
|
| 105 |
|
| 106 |
@pytest.mark.asyncio() |
| 107 |
async def test_a_memory_deleted_between_the_listing_and_the_update_is_re_created(tmp_path: Path) -> None: |
| 108 |
"""An update that meets a 404 re-creates the memory in the same pass.""" |
| 109 |
local, server, stores = await _downloaded(tmp_path, {"note.md": "v1"}) |
| 110 |
(local / "note.md").write_text("v2 from agent") |
| 111 |
|
| 112 |
async def deleted_meanwhile(path: str) -> None: |
| 113 |
server.files.pop(path, None) |
| 114 |
server.upload_hook = None |
| 115 |
|
| 116 |
server.upload_hook = deleted_meanwhile |
| 117 |
await run_sync(stores) |
| 118 |
|
| 119 |
assert server.files == {"note.md": "v2 from agent"} |
| 120 |
assert server.received == [updated("note.md", "v2 from agent", was="v1"), created("note.md", "v2 from agent")] |
| 121 |
|
| 122 |
# The create's sha is the new baseline. |
| 123 |
(local / "note.md").write_text("v3") |
| 124 |
await run_sync(stores) |
| 125 |
assert server.received[-1] == updated("note.md", "v3", was="v2 from agent") |
| 126 |
|
| 127 |
|
| 128 |
@pytest.mark.asyncio() |
| 129 |
async def test_an_edit_written_mid_sync_still_survives_a_remote_delete(tmp_path: Path) -> None: |
| 130 |
"""The directory scan saw the file unedited; an edit is written while |
| 131 |
the server listing is in flight. The removal must re-read the file |
| 132 |
instead of destroying the edit based on the stale scan.""" |
| 133 |
local, server, stores = await _downloaded(tmp_path, {"z.md": "v1", "keep.md": "v1"}) |
| 134 |
server.delete("z.md") |
| 135 |
|
| 136 |
with listing_interrupted_by(stores, lambda: (local / "z.md").write_text("late edit")): |
| 137 |
await run_sync(stores) |
| 138 |
|
| 139 |
assert (local / "z.md").read_text() == "late edit" |
| 140 |
assert server.files["z.md"] == "late edit" |
| 141 |
|
| 142 |
|
| 143 |
@pytest.mark.asyncio() |
| 144 |
async def test_a_read_only_pull_over_a_local_edit_is_warned(tmp_path: Path, caplog: pytest.LogCaptureFixture) -> None: |
| 145 |
"""Read-only stores always take the server version, but overwriting a |
| 146 |
local edit must leave a trace instead of happening silently.""" |
| 147 |
caplog.set_level("WARNING", logger=LOGGER) |
| 148 |
local, server, stores = await _downloaded(tmp_path, {"pull.md": "v1"}, access="read_only") |
| 149 |
(local / "pull.md").write_text("local edit") |
| 150 |
server.write("pull.md", "v2 from server") |
| 151 |
|
| 152 |
await run_sync(stores) |
| 153 |
|
| 154 |
assert (local / "pull.md").read_text() == "v2 from server" |
| 155 |
assert server.received == [] |
| 156 |
assert "changed both locally and remotely" in caplog.text |
| 157 |
|
| 158 |
|
| 159 |
@pytest.mark.asyncio() |
| 160 |
async def test_a_failed_re_read_does_not_resurrect_a_server_deleted_memory(tmp_path: Path) -> None: |
| 161 |
"""The re-read before removal can fail (a transient I/O error). That must |
| 162 |
mean "retry next sync", not "file gone": dropping the entry would make |
| 163 |
the next sync upload the file as new, re-creating the memory the server |
| 164 |
just deleted.""" |
| 165 |
from anthropic.lib.tools import _file_store |
| 166 |
|
| 167 |
local, server, stores = await _downloaded(tmp_path, {"z.md": "v1", "keep.md": "v1"}) |
| 168 |
server.delete("z.md") |
| 169 |
|
| 170 |
real_hash_file = _file_store.LocalFileStore.hash_file |
| 171 |
|
| 172 |
async def flaky_hash_file(self: _file_store.LocalFileStore, rel_path: str) -> str | None: |
| 173 |
if rel_path == "z.md": |
| 174 |
raise OSError(5, "I/O error") |
| 175 |
return await real_hash_file(self, rel_path) |
| 176 |
|
| 177 |
with pytest.MonkeyPatch.context() as mp: |
| 178 |
mp.setattr(_file_store.LocalFileStore, "hash_file", flaky_hash_file) |
| 179 |
await run_sync(stores) |
| 180 |
|
| 181 |
# The error held everything: file still on disk, nothing pushed. |
| 182 |
assert (local / "z.md").read_text() == "v1" |
| 183 |
assert server.received == [] |
| 184 |
|
| 185 |
# With the error gone, the next sync completes the server delete. |
| 186 |
await run_sync(stores) |
| 187 |
assert not (local / "z.md").exists() |
| 188 |
assert "z.md" not in server.files |
| 189 |
assert server.received == [] |