|
| 1 | +"""Each GHSA-7848 refusal reports its exact fields, and each scope decision is pinned. |
| 2 | +
|
| 3 | +The mutation gate of #940 (`unit`, run 42377328684) left survivors in four places: |
| 4 | +the `details` of the global refusals, the environment each check reads, the scope |
| 5 | +`remember` matches an exact duplicate on, the refusal of a superseding global write, |
| 6 | +and the curation loop that skips a candidate this session may not change. Each test |
| 7 | +below fails on one of those mutations; the session runs in `beta`. |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +import subprocess |
| 13 | + |
| 14 | +import pytest |
| 15 | + |
| 16 | +from mcp_server.errors import ValidationError |
| 17 | +from mcp_server.handlers import _write_scope, anchor, remember |
| 18 | +from mcp_server.handlers.remember import handler as remember_handler |
| 19 | +from mcp_server.handlers.remember_helpers import try_curation |
| 20 | +from mcp_server.shared import domain_mapping |
| 21 | +from tests_py.handlers import test_ghsa_7848_cross_project_write as _base |
| 22 | + |
| 23 | +projects = _base.projects |
| 24 | +ALLOW = _write_scope.ALLOW_ENV |
| 25 | +SESSION = _write_scope.SESSION_ENV |
| 26 | +GLOBAL_REFUSAL = ( |
| 27 | + "{tool}: is_global=true writes into every project's memory; " |
| 28 | + f"set {ALLOW}=* in the environment to allow it" |
| 29 | +) |
| 30 | +# Detected as global by its content alone, and by its tags alone. |
| 31 | +BY_CONTENT = ("Always use tabs in every project.", []) |
| 32 | +BY_TAGS = ("beta keeps its build notes here", ["global", "shared", "universal"]) |
| 33 | + |
| 34 | + |
| 35 | +def _refused(call): |
| 36 | + with pytest.raises(ValidationError) as caught: |
| 37 | + call() |
| 38 | + return caught.value |
| 39 | + |
| 40 | + |
| 41 | +def _beta(projects): |
| 42 | + return {SESSION: str(projects / "beta")} |
| 43 | + |
| 44 | + |
| 45 | +# ── The refusals and the environment they read ────────────────────────────── |
| 46 | + |
| 47 | + |
| 48 | +def test_the_global_refusal_names_its_tool_and_field(projects): |
| 49 | + env = _beta(projects) |
| 50 | + for call in ( |
| 51 | + lambda: _write_scope.require_global_allowed("t", env), |
| 52 | + lambda: _write_scope.global_scope((True, "explicit"), True, "t", env), |
| 53 | + ): |
| 54 | + error = _refused(call) |
| 55 | + assert str(error) == GLOBAL_REFUSAL.format(tool="t") |
| 56 | + assert error.details == {"tool": "t", "field": "is_global"} |
| 57 | + |
| 58 | + |
| 59 | +def test_global_scope_reads_the_environment_it_is_given(projects, monkeypatch): |
| 60 | + monkeypatch.setenv(ALLOW, "*") # the process allows it; the argument does not |
| 61 | + env = _beta(projects) |
| 62 | + _refused(lambda: _write_scope.global_scope((True, "r"), True, "t", env)) |
| 63 | + detected = _write_scope.global_scope((True, "r"), False, "t", env) |
| 64 | + assert detected == (False, _write_scope.KEPT_LOCAL) |
| 65 | + star = {**env, ALLOW: "*"} |
| 66 | + assert _write_scope.global_scope((True, "r"), True, "t", star) == (True, "r") |
| 67 | + assert _write_scope.global_scope((False, "r"), True, "t", env) == (False, "r") |
| 68 | + |
| 69 | + |
| 70 | +def test_a_global_memory_refusal_names_the_memory(projects): |
| 71 | + row = {"id": 42, "domain": "beta", "is_global": True} |
| 72 | + error = _refused( |
| 73 | + lambda: _write_scope.require_writable_memory(row, "t", _beta(projects)) |
| 74 | + ) |
| 75 | + assert str(error) == ( |
| 76 | + "t: memory 42 is global, so changing it reaches every project; " |
| 77 | + f"set {ALLOW}=* in the environment to allow it" |
| 78 | + ) |
| 79 | + assert error.details == {"tool": "t", "field": "memory_id", "memory_id": 42} |
| 80 | + |
| 81 | + |
| 82 | +@pytest.mark.parametrize( |
| 83 | + "row", |
| 84 | + [ |
| 85 | + {"id": 7, "domain": "alpha", "is_global": False}, |
| 86 | + {"id": 7, "domain": "beta", "is_global": True}, |
| 87 | + ], |
| 88 | + ids=["other-project", "global"], |
| 89 | +) |
| 90 | +def test_a_memory_check_reads_the_environment_it_is_given(projects, monkeypatch, row): |
| 91 | + monkeypatch.setenv(ALLOW, "*") # the process allows it; the argument does not |
| 92 | + env = _beta(projects) |
| 93 | + _refused(lambda: _write_scope.require_writable_memory(row, "t", env)) |
| 94 | + assert _write_scope.memory_writable(row, env) is False |
| 95 | + |
| 96 | + |
| 97 | +def test_a_memory_with_no_domain_stays_writable_next_to_a_placeholder_project( |
| 98 | + projects, |
| 99 | +): |
| 100 | + subprocess.run(["git", "init", "-q", str(projects / "xxxx")], check=True) |
| 101 | + domain_mapping._build_registry.cache_clear() |
| 102 | + env = _beta(projects) |
| 103 | + for row in ({"id": 1, "domain": None}, {"id": 2, "domain": "beta"}): |
| 104 | + _write_scope.require_writable_memory(row, "t", env) |
| 105 | + assert _write_scope.memory_writable(row, env) is True |
| 106 | + |
| 107 | + |
| 108 | +# ── anchor ────────────────────────────────────────────────────────────────── |
| 109 | + |
| 110 | + |
| 111 | +@pytest.mark.asyncio |
| 112 | +async def test_anchor_refuses_a_global_scope_with_the_exact_refusal(projects): |
| 113 | + own = await remember_handler({"content": "beta anchor note", "force": True}) |
| 114 | + with pytest.raises(ValidationError) as caught: |
| 115 | + await anchor.handler({"memory_id": own["memory_id"], "is_global": True}) |
| 116 | + assert str(caught.value) == GLOBAL_REFUSAL.format(tool="anchor") |
| 117 | + assert caught.value.details == {"tool": "anchor", "field": "is_global"} |
| 118 | + |
| 119 | + |
| 120 | +# ── remember: the exact-duplicate scope and the superseding write ─────────── |
| 121 | + |
| 122 | + |
| 123 | +@pytest.mark.asyncio |
| 124 | +async def test_an_explicit_global_replay_is_refused_not_matched_as_a_duplicate( |
| 125 | + projects, |
| 126 | +): |
| 127 | + note = "beta release checklist lives in the wiki" |
| 128 | + await remember_handler({"content": note, "force": True}) |
| 129 | + with pytest.raises(ValidationError) as caught: |
| 130 | + await remember_handler({"content": note, "is_global": True, "force": True}) |
| 131 | + assert str(caught.value) == GLOBAL_REFUSAL.format(tool="remember") |
| 132 | + |
| 133 | + |
| 134 | +@pytest.mark.asyncio |
| 135 | +@pytest.mark.parametrize( |
| 136 | + ("content", "tags"), [BY_CONTENT, BY_TAGS], ids=["content", "tags"] |
| 137 | +) |
| 138 | +async def test_a_detected_global_memory_is_its_own_exact_duplicate( |
| 139 | + projects, monkeypatch, content, tags |
| 140 | +): |
| 141 | + monkeypatch.setenv(ALLOW, "*") |
| 142 | + first = await remember_handler({"content": content, "tags": tags, "force": True}) |
| 143 | + assert first["stored"] is True |
| 144 | + assert first["is_global"] is True |
| 145 | + replay = await remember_handler({"content": content, "tags": tags, "force": True}) |
| 146 | + assert replay["reason"] == "exact_duplicate" |
| 147 | + assert replay["memory_id"] == first["memory_id"] |
| 148 | + |
| 149 | + |
| 150 | +@pytest.mark.asyncio |
| 151 | +async def test_an_explicit_global_memory_is_its_own_exact_duplicate( |
| 152 | + projects, monkeypatch |
| 153 | +): |
| 154 | + monkeypatch.setenv(ALLOW, "*") |
| 155 | + args = {"content": "beta shares this one", "is_global": True, "force": True} |
| 156 | + first = await remember_handler(dict(args)) |
| 157 | + replay = await remember_handler(dict(args)) |
| 158 | + assert replay["reason"] == "exact_duplicate" |
| 159 | + assert replay["memory_id"] == first["memory_id"] |
| 160 | + |
| 161 | + |
| 162 | +@pytest.mark.asyncio |
| 163 | +async def test_a_superseding_global_write_is_refused(projects): |
| 164 | + own = await remember_handler({"content": "beta uses port 8080", "force": True}) |
| 165 | + with pytest.raises(ValidationError) as caught: |
| 166 | + await remember_handler( |
| 167 | + { |
| 168 | + "content": "beta uses port 9090", |
| 169 | + "supersedes_id": own["memory_id"], |
| 170 | + "is_global": True, |
| 171 | + "force": True, |
| 172 | + } |
| 173 | + ) |
| 174 | + assert str(caught.value) == GLOBAL_REFUSAL.format(tool="remember") |
| 175 | + assert caught.value.details == {"tool": "remember", "field": "is_global"} |
| 176 | + |
| 177 | + |
| 178 | +@pytest.mark.asyncio |
| 179 | +async def test_a_write_that_is_not_stored_reports_no_scope(projects, monkeypatch): |
| 180 | + refused = {"stored": False, "action": "superseded_conflict"} |
| 181 | + monkeypatch.setattr( |
| 182 | + remember, "insert_and_post_process", lambda *a, **k: dict(refused) |
| 183 | + ) |
| 184 | + content, tags = BY_CONTENT # kept local: a scope would be reported if stored |
| 185 | + result = await remember_handler({"content": content, "tags": tags, "force": True}) |
| 186 | + assert result == refused |
| 187 | + |
| 188 | + |
| 189 | +# ── curation skips, and does not stop at, a memory it may not change ──────── |
| 190 | + |
| 191 | + |
| 192 | +class _Engine: |
| 193 | + def similarity(self, a, b) -> float: # noqa: ARG002 — stub contract |
| 194 | + return 0.99 # high enough for the merge branch |
| 195 | + |
| 196 | + def encode(self, text: str): # noqa: ARG002 — only hit on merge |
| 197 | + return b"\x00" * 4 |
| 198 | + |
| 199 | + |
| 200 | +class _Store: |
| 201 | + """Two near-identical candidates: alpha's ranked first, then beta's.""" |
| 202 | + |
| 203 | + def __init__(self) -> None: |
| 204 | + self.merged_into: list[int] = [] |
| 205 | + row = {"content": "deploy target is server one", "embedding": b"\x01"} |
| 206 | + self._rows = { |
| 207 | + 1: {**row, "id": 1, "domain": "alpha", "superseded_by_id": None}, |
| 208 | + 2: {**row, "id": 2, "domain": "beta", "superseded_by_id": None}, |
| 209 | + } |
| 210 | + for r in self._rows.values(): |
| 211 | + r.update(compression_level=0, heat=0.5, is_global=False) |
| 212 | + |
| 213 | + def search_vectors(self, embedding, top_k=3, min_heat=0.0): # noqa: ARG002 |
| 214 | + return [(1, 0.01), (2, 0.02)] |
| 215 | + |
| 216 | + def get_memories_by_ids(self, ids): |
| 217 | + return {mid: self._rows[mid] for mid in ids if mid in self._rows} |
| 218 | + |
| 219 | + def update_memory_compression(self, mid, *args, **kwargs): # noqa: ARG002 |
| 220 | + self.merged_into.append(mid) |
| 221 | + |
| 222 | + def update_memory_heat(self, mid, heat): # noqa: ARG002 |
| 223 | + pass |
| 224 | + |
| 225 | + |
| 226 | +def test_curation_skips_another_projects_candidate_and_merges_the_next(projects): |
| 227 | + store = _Store() |
| 228 | + action, target = try_curation( |
| 229 | + "deploy target is server one", b"\x01", False, store, _Engine(), [], 0.5 |
| 230 | + ) |
| 231 | + assert (action, target) == ("merge", 2) |
| 232 | + assert store.merged_into == [2] |
0 commit comments