Skip to content

Commit 411c855

Browse files
committed
fix(methodology): query_methodology lists hot memories under the domain remember writes (#721, slice 9)
query_methodology handed the cognitive-profile id returned by detect_domain to get_memories_for_domain. remember without an explicit domain writes under resolve_cwd(cwd) and, outside a known repository, under the detected profile resolved as a hint (remember._resolve_domain). Whenever the profile id differs from that scope (a legacy profile id inside a known repository, or `cortex-cowork` outside its checkout), session start listed the hot memories of a scope no write used. Read the scope remember would write for the same cwd. detect_gaps was checked and needs no change: _temporal_gaps passes only domains read back from the store (get_domain_entity_counts), and the caller's `domain` reaches only the profile blind-spot detector. Refs #721 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_018kyZYqpLwKt5qrkU1aMWnb
1 parent 6b54dde commit 411c855

3 files changed

Lines changed: 138 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,15 @@ adheres to [Semantic Versioning](https://semver.org/).
171171
registry entry. The window's `claude.exe` pid is now read from `CLAUDE_PID`
172172
first, used only when it names a `claude.exe` not younger than the hook,
173173
and the walk answers otherwise.
174+
- `query_methodology` lists the hot memories of the domain `remember` writes
175+
(#721, slice 9). It read them under the cognitive-profile id `detect_domain`
176+
returns, while `remember` without an explicit domain writes under the
177+
session's repository (`resolve_cwd`) and, outside a known repository, under
178+
the detected profile resolved as a hint. A profile id that differs from that
179+
scope (a legacy id, or `cortex-cowork` outside its checkout) listed a scope no
180+
write used. It now reads the scope `remember` would write for the same `cwd`.
181+
`detect_gaps` was checked and needs no change: it reads only domains taken
182+
from the store.
174183
- `validate_memory` validates the domain identity `remember` writes (#721,
175184
slice 8). It passed an explicit `domain` unresolved to
176185
`get_memories_for_domain`, so `validate_memory(domain="cortex-cowork")`

‎mcp_server/handlers/query_methodology.py‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
from mcp_server.handlers._telemetry_wrap import instrument
2222
from mcp_server.infrastructure.memory_config import get_memory_settings
2323
from mcp_server.infrastructure.memory_store import get_shared_store
24+
from mcp_server.shared.domain_mapping import resolve_cwd
25+
from mcp_server.shared.domain_mapping import resolve_domain as resolve_hint
2426

2527
logger = logging.getLogger(__name__)
2628

@@ -154,6 +156,22 @@ def _get_hot_memories(
154156
]
155157

156158

159+
def _memory_scope(cwd: str, profile_domain: str) -> str:
160+
"""The domain ``remember`` writes a memory under for this session.
161+
162+
``detect_domain`` returns a cognitive-profile id, not a memory scope.
163+
``remember`` without an explicit domain writes under ``resolve_cwd(cwd)``
164+
and, outside a known repository, under the detected profile resolved as a
165+
hint (``remember._resolve_domain``). Reading hot memories under the raw
166+
profile id listed a scope no write used whenever the two differ.
167+
168+
source: issue #721; ADR-0647"""
169+
resolved = resolve_cwd(cwd) if cwd else ""
170+
if resolved:
171+
return resolved
172+
return resolve_hint(profile_domain) if profile_domain else ""
173+
174+
157175
def _get_fired_triggers(directory: str, first_message: str) -> list[dict[str, Any]]:
158176
"""Check all active prospective triggers against current context."""
159177
store = _try_get_memory_store()
@@ -314,7 +332,7 @@ async def handler(args: dict | None = None) -> dict:
314332

315333
domain_id = detection.get("domain")
316334
profile = profiles.get("domains", {}).get(domain_id) if domain_id else None
317-
hot_memories = _get_hot_memories(domain_id or "", cwd)
335+
hot_memories = _get_hot_memories(_memory_scope(cwd, domain_id or ""), cwd)
318336
fired_triggers = _get_fired_triggers(cwd, first_message)
319337

320338
if not profile:
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
"""query_methodology reads hot memories under the scope remember writes (#721).
2+
3+
``detect_domain`` returns a cognitive-profile id. ``query_methodology`` handed
4+
that id to ``get_memories_for_domain``, while ``remember`` without an explicit
5+
domain writes under ``resolve_cwd(cwd)`` and, outside a known repository,
6+
under the detected profile resolved as a hint. When the profile id and the
7+
written scope differ, session start listed the hot memories of a scope no
8+
write used. Each test rebuilds the repository registry from real directories
9+
under ``tmp_path``, runs the public handler, and reads the domain it hands to
10+
the store.
11+
12+
source: issue #721; ADR-0647"""
13+
14+
from __future__ import annotations
15+
16+
import asyncio
17+
from pathlib import Path
18+
from unittest.mock import patch
19+
20+
import pytest
21+
22+
from mcp_server.handlers import query_methodology as methodology
23+
from mcp_server.handlers import remember
24+
from mcp_server.shared import domain_mapping as dm
25+
26+
27+
class _FakeStore:
28+
"""Records which scope it is asked for and holds no memory."""
29+
30+
def __init__(self) -> None:
31+
self.domains: list[str] = []
32+
33+
def get_memories_for_domain(self, domain, **_kwargs):
34+
self.domains.append(domain)
35+
return []
36+
37+
def get_memories_for_directory(self, _directory, **_kwargs):
38+
return []
39+
40+
def get_hot_memories(self, **_kwargs):
41+
return []
42+
43+
44+
def _make_repo(dev_root: Path, name: str) -> Path:
45+
git_dir = dev_root / name / ".git"
46+
git_dir.mkdir(parents=True)
47+
(git_dir / "config").write_text(
48+
f'[remote "origin"]\n\turl = https://github.com/org/{name}.git\n'
49+
)
50+
return dev_root / name
51+
52+
53+
@pytest.fixture
54+
def dirs(tmp_path, monkeypatch):
55+
dev_root = tmp_path / "dev"
56+
for name in ("cortex", "cortex-cowork", "harness-comparison"):
57+
_make_repo(dev_root, name)
58+
outside = tmp_path / "outside"
59+
outside.mkdir()
60+
monkeypatch.setattr(dm, "_candidate_dev_roots", lambda: [dev_root])
61+
dm._build_registry.cache_clear()
62+
yield {"repo": str(dev_root / "cortex"), "outside": str(outside)}
63+
dm._build_registry.cache_clear()
64+
65+
66+
def _read_scope(cwd: str, profile_domain: str) -> str:
67+
store = _FakeStore()
68+
with patch.multiple(
69+
methodology,
70+
load_profiles=lambda: {"domains": {}},
71+
detect_domain=lambda *_a, **_k: {"domain": profile_domain},
72+
_try_get_memory_store=lambda: store,
73+
_get_fired_triggers=lambda *_a: [],
74+
):
75+
with patch("mcp_server.core.telemetry.record"):
76+
asyncio.run(methodology.handler({"cwd": cwd}))
77+
return store.domains[-1]
78+
79+
80+
def _write_scope(monkeypatch, cwd: str, profile_domain: str) -> str:
81+
monkeypatch.setattr(remember, "load_profiles", lambda: {"domains": {}})
82+
monkeypatch.setattr(
83+
remember, "detect_domain", lambda *_a, **_k: {"domain": profile_domain}
84+
)
85+
return remember._resolve_domain(cwd, "")
86+
87+
88+
def test_known_repository_is_read_under_its_canonical(dirs):
89+
assert _read_scope(dirs["repo"], "legacy-profile-id") == "cortex"
90+
91+
92+
def test_profile_outside_a_repository_is_read_as_remember_resolves_it(dirs):
93+
assert _read_scope(dirs["outside"], "cortex-cowork") == "cortex"
94+
95+
96+
@pytest.mark.parametrize(
97+
("where", "profile_domain"),
98+
[
99+
("repo", "legacy-profile-id"),
100+
("repo", "cortex"),
101+
("outside", "cortex-cowork"),
102+
("outside", "SciFact-Comparison"),
103+
],
104+
)
105+
def test_read_scope_equals_remember_write_scope(
106+
dirs, monkeypatch, where, profile_domain
107+
):
108+
cwd = dirs[where]
109+
read = _read_scope(cwd, profile_domain)
110+
assert read == _write_scope(monkeypatch, cwd, profile_domain)

0 commit comments

Comments
 (0)