Skip to content

Commit 5c6cc80

Browse files
committed
fix(remember): keep an explicit domain instead of remapping it by substring (#721, slice 1)
remember(domain=...) went through resolve_domain, which treats its input as a hint and returns the canonical of the longest known repository fragment found inside it: with a harness-comparison repository in the dev root, domain="scifact-comparison" was persisted as harness-comparison through the shared fragment "comparison". resolve_explicit_domain maps only an exact known repository name (or its ADR-0647 family) to its canonical and keeps any other name as given, stripped and lowercased. remember uses it for the caller's domain; inferred hints (profile detection, slugs, paths) still use resolve_domain. Refs #721 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_018kyZYumzRRnGTwTkCk6tdj
1 parent 2126d2a commit 5c6cc80

3 files changed

Lines changed: 120 additions & 2 deletions

File tree

‎mcp_server/handlers/remember.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@
5050
from mcp_server.infrastructure.profile_store import load_profiles
5151
from mcp_server.infrastructure.memory_exact import find_exact
5252
from mcp_server.handlers.remember_response import duplicate_response
53-
from mcp_server.shared.domain_mapping import resolve_cwd, resolve_domain as resolve_hint
53+
from mcp_server.shared.domain_mapping import (
54+
resolve_cwd,
55+
resolve_domain as resolve_hint,
56+
resolve_explicit_domain,
57+
)
5458
from mcp_server.shared.wiki_page_candidate import PageCandidate
5559
from mcp_server.shared.content_hardening import harden_content
5660

@@ -76,7 +80,7 @@ def _resolve_domain(directory: str, domain: str) -> str:
7680
if resolved:
7781
return resolved
7882
if domain:
79-
return resolve_hint(domain)
83+
return resolve_explicit_domain(domain)
8084
if directory:
8185
# Fallback to profile-based detection
8286
profiles = load_profiles()

‎mcp_server/shared/domain_mapping.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,24 @@ def resolve_domain(input_str: str) -> str:
473473
return lower
474474

475475

476+
def resolve_explicit_domain(name: str) -> str:
477+
"""Resolve a domain the caller named explicitly, without guessing.
478+
479+
An explicit domain is an identity, not a hint: only an exact known
480+
repository name (remote or directory name, or the family it is grouped
481+
under per ADR-0647) maps to its canonical. Any other name is kept as
482+
given, stripped and lowercased. ``resolve_domain`` instead returns the
483+
canonical of the longest known fragment found as a substring of its
484+
input, so ``scifact-comparison`` became ``harness-comparison`` through
485+
the shared fragment ``comparison`` (issue #721).
486+
487+
source: issue #721; ADR-0647"""
488+
lower = (name or "").strip().lower()
489+
if not lower:
490+
return ""
491+
return _build_registry().name_to_canonical.get(lower, lower)
492+
493+
476494
def resolve_cwd(cwd: str) -> str:
477495
"""Resolve a working directory to a canonical domain.
478496
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
"""An explicit domain keeps its identity; it is never remapped by substring (#721).
2+
3+
In the SciFact ingestion campaign, ``remember(domain="scifact-comparison")``
4+
persisted every fact under ``harness-comparison``: ``resolve_domain`` treats
5+
its input as a hint and returns the canonical of the longest known repository
6+
fragment found inside it, and ``comparison`` is a fragment of the
7+
``harness-comparison`` repository. These tests rebuild that registry from real
8+
directories under ``tmp_path`` and check the explicit path through the
9+
public ``remember`` handler as well as the resolver.
10+
11+
source: issue #721; ADR-0647"""
12+
13+
from __future__ import annotations
14+
15+
import asyncio
16+
from pathlib import Path
17+
18+
import pytest
19+
20+
from mcp_server.shared import domain_mapping as dm
21+
22+
23+
def _make_repo(dev_root: Path, name: str) -> None:
24+
git_dir = dev_root / name / ".git"
25+
git_dir.mkdir(parents=True)
26+
(git_dir / "config").write_text(
27+
f'[remote "origin"]\n\turl = https://github.com/org/{name}.git\n'
28+
)
29+
30+
31+
@pytest.fixture
32+
def registry_of(tmp_path, monkeypatch):
33+
"""Point the domain registry at a dev root holding exactly ``names``."""
34+
35+
def build(*names: str) -> None:
36+
dev_root = tmp_path / ("dev-" + "-".join(names))
37+
dev_root.mkdir()
38+
for name in names:
39+
_make_repo(dev_root, name)
40+
monkeypatch.setattr(dm, "_candidate_dev_roots", lambda: [dev_root])
41+
dm._build_registry.cache_clear()
42+
43+
yield build
44+
dm._build_registry.cache_clear()
45+
46+
47+
def test_hint_resolution_reproduces_the_substring_collision(registry_of):
48+
registry_of("harness-comparison")
49+
assert dm.resolve_domain("scifact-comparison") == "harness-comparison"
50+
51+
52+
def test_explicit_domain_is_not_remapped_by_a_shared_fragment(registry_of):
53+
registry_of("harness-comparison")
54+
assert dm.resolve_explicit_domain("scifact-comparison") == "scifact-comparison"
55+
56+
57+
def test_unrelated_repository_cannot_change_an_explicit_domain(registry_of):
58+
registry_of("cortex")
59+
before = dm.resolve_explicit_domain("SciFact-Comparison ")
60+
registry_of("cortex", "harness-comparison")
61+
assert dm.resolve_explicit_domain("SciFact-Comparison ") == before
62+
assert before == "scifact-comparison"
63+
64+
65+
def test_exact_repository_name_still_maps_to_its_family(registry_of):
66+
registry_of("cortex", "cortex-cowork")
67+
assert dm.resolve_explicit_domain("cortex-cowork") == "cortex"
68+
assert dm.resolve_explicit_domain("Cortex") == "cortex"
69+
70+
71+
def test_empty_explicit_domain_stays_empty(registry_of):
72+
registry_of("cortex")
73+
assert dm.resolve_explicit_domain(" ") == ""
74+
75+
76+
def test_remember_persists_the_explicit_domain(registry_of):
77+
from mcp_server.handlers.forget import _get_store
78+
from mcp_server.handlers.remember import handler
79+
80+
registry_of("harness-comparison")
81+
result = asyncio.run(
82+
handler(
83+
{
84+
"content": "SciFact claim 4983: the explicit corpus domain "
85+
"survives a repository fragment collision (#721)",
86+
"domain": "scifact-comparison",
87+
"force": True,
88+
}
89+
)
90+
)
91+
assert result["stored"] is True, result
92+
with _get_store()._conn.cursor() as cur:
93+
cur.execute("SELECT domain FROM memories WHERE id = %s", (result["memory_id"],))
94+
row = cur.fetchone()
95+
assert row is not None
96+
assert row["domain"] == "scifact-comparison"

0 commit comments

Comments
 (0)