| 1 |
"""Folder-trust gating: a missing, emptied, or swapped store folder is never |
| 2 |
trusted as a basis for deletions or uploads. |
| 3 |
|
| 4 |
`download` stamps `MARKER_PATH` (holding the store's id) into the |
| 5 |
folder. A sync pass that finds the folder destroyed re-downloads it and |
| 6 |
pushes nothing; a sync that finds files without a matching marker leaves |
| 7 |
the folder as found and syncs nothing at all. |
| 8 |
""" |
| 9 |
|
| 10 |
from __future__ import annotations |
| 11 |
|
| 12 |
import shutil |
| 13 |
from pathlib import Path |
| 14 |
|
| 15 |
import pytest |
| 16 |
|
| 17 |
import anthropic.lib.tools._memories as memories_mod |
| 18 |
from anthropic.lib.tools._memories import MARKER_PATH, SessionMemoryStores |
| 19 |
|
| 20 |
from .conftest import Clock, run_sync, listing_interrupted_by |
| 21 |
from ._fake_anthropic import MemoryServer, deleted, updated, fake_anthropic |
| 22 |
|
| 23 |
LOGGER = "anthropic.lib.tools.agent_toolset" |
| 24 |
|
| 25 |
STORE_ID = "memstore_notes" |
| 26 |
|
| 27 |
|
| 28 |
def marker_stamp(local: Path) -> str: |
| 29 |
return (local / MARKER_PATH).read_text() |
| 30 |
|
| 31 |
|
| 32 |
async def _downloaded(tmp_path: Path, initial: dict[str, str]) -> tuple[Path, MemoryServer, SessionMemoryStores]: |
| 33 |
"""A `SessionMemoryStores` with one store already downloaded to disk.""" |
| 34 |
client, server = fake_anthropic(initial) |
| 35 |
stores = SessionMemoryStores(client, workdir=tmp_path) |
| 36 |
await stores.download(await client.beta.sessions.retrieve("s1")) |
| 37 |
return tmp_path / "memory" / "notes", server, stores |
| 38 |
|
| 39 |
|
| 40 |
@pytest.mark.asyncio() |
| 41 |
async def test_a_deleted_folder_issues_no_deletes_and_is_rebuilt( |
| 42 |
tmp_path: Path, caplog: pytest.LogCaptureFixture, clock: Clock |
| 43 |
) -> None: |
| 44 |
"""rm -rf of the whole folder between syncs: the next sync must not read |
| 45 |
the emptiness as "the agent deleted everything" and wipe the server.""" |
| 46 |
caplog.set_level("WARNING", logger=LOGGER) |
| 47 |
local, server, stores = await _downloaded(tmp_path, {"a.md": "v1", "b.md": "v1"}) |
| 48 |
shutil.rmtree(local) |
| 49 |
|
| 50 |
await run_sync(stores) |
| 51 |
|
| 52 |
assert server.received == [] |
| 53 |
assert server.files == {"a.md": "v1", "b.md": "v1"} |
| 54 |
assert (local / "a.md").read_text() == "v1" |
| 55 |
assert (local / "b.md").read_text() == "v1" |
| 56 |
assert marker_stamp(local) == f"version 1\n{STORE_ID}" |
| 57 |
assert "the folder or its marker is gone; re-downloading" in caplog.text |
| 58 |
|
| 59 |
# The rebuilt folder syncs normally: an edit and a deletion both propagate |
| 60 |
# (the deletion after its waiting window). |
| 61 |
(local / "a.md").write_text("v2") |
| 62 |
(local / "b.md").unlink() |
| 63 |
await run_sync(stores) |
| 64 |
clock.now = memories_mod.DELETE_CORROBORATION_SECONDS + 1 |
| 65 |
await run_sync(stores) |
| 66 |
assert server.received == [updated("a.md", "v2", was="v1"), deleted("b.md", was="v1")] |
| 67 |
assert server.files == {"a.md": "v2"} |
| 68 |
|
| 69 |
|
| 70 |
@pytest.mark.asyncio() |
| 71 |
async def test_an_emptied_folder_issues_no_deletes_and_is_repopulated( |
| 72 |
tmp_path: Path, caplog: pytest.LogCaptureFixture, clock: Clock |
| 73 |
) -> None: |
| 74 |
caplog.set_level("WARNING", logger=LOGGER) |
| 75 |
local, server, stores = await _downloaded(tmp_path, {"a.md": "v1", "b.md": "v1"}) |
| 76 |
for child in local.iterdir(): |
| 77 |
child.unlink() |
| 78 |
|
| 79 |
await run_sync(stores) |
| 80 |
|
| 81 |
assert server.received == [] |
| 82 |
assert server.files == {"a.md": "v1", "b.md": "v1"} |
| 83 |
assert (local / "a.md").read_text() == "v1" |
| 84 |
assert marker_stamp(local) == f"version 1\n{STORE_ID}" |
| 85 |
assert "the folder or its marker is gone; re-downloading" in caplog.text |
| 86 |
|
| 87 |
(local / "a.md").unlink() |
| 88 |
await run_sync(stores) |
| 89 |
clock.now = memories_mod.DELETE_CORROBORATION_SECONDS + 1 |
| 90 |
await run_sync(stores) |
| 91 |
assert server.received == [deleted("a.md", was="v1")] |
| 92 |
|
| 93 |
|
| 94 |
@pytest.mark.asyncio() |
| 95 |
async def test_an_emptied_folder_with_the_marker_left_behind_issues_no_deletes( |
| 96 |
tmp_path: Path, caplog: pytest.LogCaptureFixture, clock: Clock |
| 97 |
) -> None: |
| 98 |
"""`rm <mount>/*`: shell globs skip dotfiles, so the marker survives the |
| 99 |
wipe — an intact marker alone must not clear a folder whose every |
| 100 |
memory file vanished at once.""" |
| 101 |
caplog.set_level("WARNING", logger=LOGGER) |
| 102 |
local, server, stores = await _downloaded(tmp_path, {"a.md": "v1", "b.md": "v1"}) |
| 103 |
for child in local.iterdir(): |
| 104 |
if child.name != MARKER_PATH: |
| 105 |
child.unlink() |
| 106 |
|
| 107 |
await run_sync(stores) |
| 108 |
|
| 109 |
assert server.received == [] |
| 110 |
assert server.files == {"a.md": "v1", "b.md": "v1"} |
| 111 |
assert (local / "a.md").read_text() == "v1" |
| 112 |
assert "every memory file is gone at once; re-downloading" in caplog.text |
| 113 |
|
| 114 |
# With files present again, an ordinary single deletion still propagates |
| 115 |
# (after its waiting window). |
| 116 |
(local / "a.md").unlink() |
| 117 |
await run_sync(stores) |
| 118 |
clock.now = memories_mod.DELETE_CORROBORATION_SECONDS + 1 |
| 119 |
await run_sync(stores) |
| 120 |
assert server.received == [deleted("a.md", was="v1")] |
| 121 |
|
| 122 |
|
| 123 |
@pytest.mark.asyncio() |
| 124 |
async def test_a_swapped_folder_neither_deletes_nor_uploads_ever( |
| 125 |
tmp_path: Path, caplog: pytest.LogCaptureFixture |
| 126 |
) -> None: |
| 127 |
"""A folder carrying another store's marker is foreign: the pass must not |
| 128 |
adopt it — no deletes, no uploads, no restamp — on this pass or any later |
| 129 |
one, or the foreign files leak into the customer's store one pass later.""" |
| 130 |
caplog.set_level("WARNING", logger=LOGGER) |
| 131 |
local, server, stores = await _downloaded(tmp_path, {"a.md": "v1"}) |
| 132 |
shutil.rmtree(local) |
| 133 |
local.mkdir() |
| 134 |
(local / MARKER_PATH).write_text("memstore_other") |
| 135 |
(local / "foreign.md").write_text("someone else's notes") |
| 136 |
|
| 137 |
await run_sync(stores) |
| 138 |
await run_sync(stores) |
| 139 |
|
| 140 |
assert server.received == [] |
| 141 |
assert server.files == {"a.md": "v1"} |
| 142 |
assert (local / MARKER_PATH).read_text() == "memstore_other" |
| 143 |
assert (local / "foreign.md").read_text() == "someone else's notes" |
| 144 |
assert not (local / "a.md").exists() |
| 145 |
assert "the marker file does not match this store; leaving the memory store folder as found" in caplog.text |
| 146 |
|
| 147 |
|
| 148 |
@pytest.mark.asyncio() |
| 149 |
async def test_files_without_a_marker_are_neither_pushed_nor_pulled_over( |
| 150 |
tmp_path: Path, caplog: pytest.LogCaptureFixture |
| 151 |
) -> None: |
| 152 |
"""Only the marker was deleted (a dotfile cleanup): the remaining files may |
| 153 |
hold un-pushed edits, so the pass must neither upload them, nor delete by |
| 154 |
them, nor destroy them by re-downloading over the folder.""" |
| 155 |
caplog.set_level("WARNING", logger=LOGGER) |
| 156 |
local, server, stores = await _downloaded(tmp_path, {"a.md": "v1", "b.md": "v1"}) |
| 157 |
(local / MARKER_PATH).unlink() |
| 158 |
(local / "a.md").write_text("un-pushed edit") |
| 159 |
(local / "b.md").unlink() |
| 160 |
|
| 161 |
await run_sync(stores) |
| 162 |
|
| 163 |
assert server.received == [] |
| 164 |
assert server.files == {"a.md": "v1", "b.md": "v1"} |
| 165 |
assert (local / "a.md").read_text() == "un-pushed edit" |
| 166 |
assert not (local / MARKER_PATH).exists() |
| 167 |
assert "the marker file is gone; leaving the memory store folder as found" in caplog.text |
| 168 |
|
| 169 |
|
| 170 |
@pytest.mark.asyncio() |
| 171 |
async def test_a_single_local_deletion_with_the_marker_intact_still_propagates(tmp_path: Path, clock: Clock) -> None: |
| 172 |
local, server, stores = await _downloaded(tmp_path, {"a.md": "v1", "b.md": "v1"}) |
| 173 |
(local / "a.md").unlink() |
| 174 |
|
| 175 |
await run_sync(stores) |
| 176 |
clock.now = memories_mod.DELETE_CORROBORATION_SECONDS + 1 |
| 177 |
await run_sync(stores) |
| 178 |
|
| 179 |
assert server.received == [deleted("a.md", was="v1")] |
| 180 |
assert server.files == {"b.md": "v1"} |
| 181 |
assert marker_stamp(local) == f"version 1\n{STORE_ID}" |
| 182 |
|
| 183 |
|
| 184 |
@pytest.mark.asyncio() |
| 185 |
async def test_the_marker_never_syncs_in_either_direction(tmp_path: Path, caplog: pytest.LogCaptureFixture) -> None: |
| 186 |
caplog.set_level("WARNING", logger=LOGGER) |
| 187 |
local, server, stores = await _downloaded(tmp_path, {"a.md": "v1"}) |
| 188 |
|
| 189 |
# The marker sits in the folder yet is never uploaded or remote-deleted. |
| 190 |
await run_sync(stores) |
| 191 |
assert server.received == [] |
| 192 |
|
| 193 |
# A marker path the server lists is refused: skipped with a warning, |
| 194 |
# never written over the real marker on disk. |
| 195 |
server.write(MARKER_PATH, "not a store id") |
| 196 |
await run_sync(stores) |
| 197 |
assert server.received == [] |
| 198 |
assert marker_stamp(local) == f"version 1\n{STORE_ID}" |
| 199 |
assert "reserved marker path" in caplog.text |
| 200 |
|
| 201 |
|
| 202 |
@pytest.mark.asyncio() |
| 203 |
async def test_dispose_tolerates_an_already_deleted_folder(tmp_path: Path) -> None: |
| 204 |
local, _server, stores = await _downloaded(tmp_path, {"a.md": "v1"}) |
| 205 |
shutil.rmtree(local) |
| 206 |
|
| 207 |
await stores.dispose() |
| 208 |
|
| 209 |
assert not local.exists() |
| 210 |
|
| 211 |
|
| 212 |
@pytest.mark.asyncio() |
| 213 |
async def test_dispose_keeps_a_folder_that_fails_the_marker_check( |
| 214 |
tmp_path: Path, caplog: pytest.LogCaptureFixture |
| 215 |
) -> None: |
| 216 |
"""Sync left the folder as found after a failed marker check; teardown |
| 217 |
must not then delete the files sync refused to touch.""" |
| 218 |
caplog.set_level("WARNING", logger=LOGGER) |
| 219 |
local, _server, stores = await _downloaded(tmp_path, {"a.md": "v1"}) |
| 220 |
(local / MARKER_PATH).unlink() |
| 221 |
(local / "a.md").write_text("un-pushed edit") |
| 222 |
|
| 223 |
await stores.dispose() |
| 224 |
|
| 225 |
assert (local / "a.md").read_text() == "un-pushed edit" |
| 226 |
assert "leaving the memory store folder on disk" in caplog.text |
| 227 |
|
| 228 |
|
| 229 |
@pytest.mark.asyncio() |
| 230 |
async def test_dispose_never_raises_when_a_store_root_is_replaced_by_a_file(tmp_path: Path) -> None: |
| 231 |
"""The worker calls dispose() from its teardown: a store whose folder |
| 232 |
something replaced with a regular file must log and be skipped, not |
| 233 |
raise out of the teardown.""" |
| 234 |
local, _server, stores = await _downloaded(tmp_path, {"a.md": "v1"}) |
| 235 |
shutil.rmtree(local) |
| 236 |
local.write_text("a file where the folder was") |
| 237 |
|
| 238 |
await stores.dispose() |
| 239 |
|
| 240 |
assert local.read_text() == "a file where the folder was" |
| 241 |
|
| 242 |
|
| 243 |
@pytest.mark.asyncio() |
| 244 |
async def test_a_marker_from_an_older_format_no_longer_clears_the_folder( |
| 245 |
tmp_path: Path, caplog: pytest.LogCaptureFixture |
| 246 |
) -> None: |
| 247 |
"""The marker carries a format version so an SDK upgrade can retire every |
| 248 |
folder written before it: a marker in the old format (the bare store id) |
| 249 |
must fail the check, and the folder is left as found.""" |
| 250 |
caplog.set_level("WARNING", logger=LOGGER) |
| 251 |
local, server, stores = await _downloaded(tmp_path, {"a.md": "v1", "b.md": "v1"}) |
| 252 |
(local / MARKER_PATH).write_text(STORE_ID) |
| 253 |
(local / "a.md").write_text("edit under the old marker") |
| 254 |
(local / "b.md").unlink() |
| 255 |
|
| 256 |
await run_sync(stores) |
| 257 |
|
| 258 |
assert server.received == [] |
| 259 |
assert server.files == {"a.md": "v1", "b.md": "v1"} |
| 260 |
assert (local / "a.md").read_text() == "edit under the old marker" |
| 261 |
assert "the marker file does not match this store" in caplog.text |
| 262 |
|
| 263 |
|
| 264 |
@pytest.mark.asyncio() |
| 265 |
@pytest.mark.parametrize("rel", ["z.md", "sub/z.md"]) |
| 266 |
async def test_a_folder_wiped_while_a_pull_is_in_flight_is_rebuilt_not_distrusted( |
| 267 |
tmp_path: Path, caplog: pytest.LogCaptureFixture, rel: str |
| 268 |
) -> None: |
| 269 |
"""rm -rf lands between a sync's scan and its content write: the write |
| 270 |
must not bring the folder back without its marker, or every later sync |
| 271 |
reads the folder as foreign and the session's memory writes are lost.""" |
| 272 |
caplog.set_level("WARNING", logger=LOGGER) |
| 273 |
local, server, stores = await _downloaded(tmp_path, {"x.md": "v1", "y.md": "v1", rel: "v1"}) |
| 274 |
server.write(rel, "v2") |
| 275 |
server.fetch_hook = lambda _p: shutil.rmtree(local) |
| 276 |
|
| 277 |
await run_sync(stores) |
| 278 |
|
| 279 |
assert not local.exists() |
| 280 |
assert server.received == [] |
| 281 |
|
| 282 |
server.fetch_hook = None |
| 283 |
await run_sync(stores) |
| 284 |
|
| 285 |
assert marker_stamp(local) == f"version 1\n{STORE_ID}" |
| 286 |
assert (local / "x.md").read_text() == "v1" |
| 287 |
assert (local / rel).read_text() == "v2" |
| 288 |
assert "re-downloading the memory store folder" in caplog.text |
| 289 |
assert "leaving the memory store folder as found" not in caplog.text |
| 290 |
|
| 291 |
await stores.finish() |
| 292 |
await stores.flush_writes() |
| 293 |
await stores.dispose() |
| 294 |
assert not local.exists() |
| 295 |
assert server.received == [] |
| 296 |
assert server.files == {"x.md": "v1", "y.md": "v1", rel: "v2"} |
| 297 |
|
| 298 |
|
| 299 |
@pytest.mark.asyncio() |
| 300 |
async def test_a_folder_wiped_while_a_locally_deleted_memory_is_being_restored_is_rebuilt( |
| 301 |
tmp_path: Path, caplog: pytest.LogCaptureFixture, clock: Clock |
| 302 |
) -> None: |
| 303 |
"""A file deleted locally but changed on the server is restored by the |
| 304 |
next sync; an rm -rf that lands while that restore is downloading must |
| 305 |
leave no folder behind, the sync after it must rebuild the whole store, |
| 306 |
and the earlier absence must never become a server delete.""" |
| 307 |
caplog.set_level("WARNING", logger=LOGGER) |
| 308 |
local, server, stores = await _downloaded(tmp_path, {"x.md": "v1", "y.md": "v1", "z.md": "v1"}) |
| 309 |
(local / "z.md").unlink() |
| 310 |
server.write("z.md", "v2") |
| 311 |
server.fetch_hook = lambda _p: shutil.rmtree(local) |
| 312 |
|
| 313 |
await run_sync(stores) |
| 314 |
assert not local.exists() |
| 315 |
|
| 316 |
server.fetch_hook = None |
| 317 |
await run_sync(stores) |
| 318 |
assert marker_stamp(local) == f"version 1\n{STORE_ID}" |
| 319 |
assert (local / "x.md").read_text() == "v1" |
| 320 |
assert (local / "y.md").read_text() == "v1" |
| 321 |
assert (local / "z.md").read_text() == "v2" |
| 322 |
|
| 323 |
clock.now = memories_mod.DELETE_CORROBORATION_SECONDS + 1 |
| 324 |
await run_sync(stores) |
| 325 |
assert server.received == [] |
| 326 |
assert server.files == {"x.md": "v1", "y.md": "v1", "z.md": "v2"} |
| 327 |
assert "leaving the memory store folder as found" not in caplog.text |
| 328 |
|
| 329 |
|
| 330 |
@pytest.mark.asyncio() |
| 331 |
async def test_a_folder_wiped_again_mid_recovery_is_rebuilt_by_the_next_sync( |
| 332 |
tmp_path: Path, caplog: pytest.LogCaptureFixture |
| 333 |
) -> None: |
| 334 |
"""A re-download writes the marker first and the memories after it; a |
| 335 |
second rm -rf between those writes must not leave memories on disk |
| 336 |
without a marker — the next sync re-downloads once more instead.""" |
| 337 |
caplog.set_level("WARNING", logger=LOGGER) |
| 338 |
local, server, stores = await _downloaded(tmp_path, {"a.md": "v1", "b.md": "v1", "c.md": "v1"}) |
| 339 |
shutil.rmtree(local) |
| 340 |
|
| 341 |
with listing_interrupted_by(stores, lambda: shutil.rmtree(local), after_items=1): |
| 342 |
await run_sync(stores) |
| 343 |
|
| 344 |
assert not local.exists() |
| 345 |
assert server.received == [] |
| 346 |
|
| 347 |
await run_sync(stores) |
| 348 |
|
| 349 |
assert marker_stamp(local) == f"version 1\n{STORE_ID}" |
| 350 |
for name in ("a.md", "b.md", "c.md"): |
| 351 |
assert (local / name).read_text() == "v1" |
| 352 |
assert caplog.text.count("re-downloading the memory store folder") == 2 |
| 353 |
assert "leaving the memory store folder as found" not in caplog.text |
| 354 |
assert server.received == [] |
| 355 |
|
| 356 |
await stores.dispose() |
| 357 |
assert not local.exists() |