Skip to content

Commit 8ef4813

Browse files
authored
Merge pull request #864 from cdeust/fix/710-transcripts-follow-claude-config-dir
2 parents 7ac79b3 + 4a019c7 commit 8ef4813

23 files changed

Lines changed: 452 additions & 44 deletions

‎CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ adheres to [Semantic Versioning](https://semver.org/).
4444

4545
### Fixed
4646

47+
- Transcript discovery honours the host's config directory (#710, slice 1).
48+
Profile rebuilds, `import_sessions`, `backfill_memories`, explore features and
49+
the SessionStart session count read `$CLAUDE_CONFIG_DIR/projects` when the
50+
variable is set, instead of always `~/.claude/projects`. `CORTEX_CLAUDE_DIR`
51+
still wins, and Cortex's own data (store, profiles, wiki) does not move.
4752
- A user's own tags no longer hide a memory from default recall (#706,
4853
ADR-1123). The low-signal filter of `recall` and of trigger injection now
4954
drops only capture markers (`auto-captured`, `tool:<name>`, `_backfill`,

‎mcp_server/handlers/backfill_helpers.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import hashlib
88
from pathlib import Path
99

10-
from mcp_server.infrastructure.config import CLAUDE_DIR
10+
from mcp_server.infrastructure.config import CLAUDE_PROJECTS_DIR
1111
from mcp_server.infrastructure.memory_store import MemoryStore
1212
from mcp_server.observability import silent_failure
1313
from mcp_server.shared.domain_mapping import resolve_domain
@@ -123,7 +123,7 @@ def mark_backfilled(store: MemoryStore, path: Path, fhash: str, count: int) -> N
123123

124124
def discover_files(project_filter: str, max_files: int) -> list[tuple[Path, str]]:
125125
"""Return (path, project_slug) pairs for JSONL session files."""
126-
projects_dir = CLAUDE_DIR / "projects"
126+
projects_dir = CLAUDE_PROJECTS_DIR
127127
if not projects_dir.exists():
128128
return []
129129

@@ -146,11 +146,11 @@ def discover_files(project_filter: str, max_files: int) -> list[tuple[Path, str]
146146
)
147147
if not accept:
148148
continue
149-
results.append((jsonl_file, slug))
150149
if len(results) >= limit:
151-
break
150+
return results
151+
results.append((jsonl_file, slug))
152152

153-
return results[:limit]
153+
return results
154154

155155

156156
def slug_to_domain(slug: str) -> str:

‎mcp_server/handlers/import_sessions.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
extract_memorable_items,
2626
extract_session_summary,
2727
)
28-
from mcp_server.infrastructure.config import CLAUDE_DIR
28+
from mcp_server.infrastructure.config import CLAUDE_PROJECTS_DIR
2929
from mcp_server.infrastructure.scanner import read_head_tail
3030
from mcp_server.handlers._tool_meta import NON_IDEMPOTENT_WRITE
3131
from mcp_server.handlers.backfill_helpers import gist_oversized_content
@@ -109,7 +109,7 @@ def _discover_jsonl_files(
109109
project_filter: str,
110110
) -> list[tuple[Path, str]]:
111111
"""Find all JSONL conversation files, optionally filtered by project."""
112-
projects_dir = CLAUDE_DIR / "projects"
112+
projects_dir = CLAUDE_PROJECTS_DIR
113113
if not projects_dir.exists():
114114
return []
115115

‎mcp_server/hooks/session_start_db.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from pathlib import Path
1212

1313
from mcp_server.hooks.session_start_common import _DATABASE_URL, _PLUGIN_ROOT, _log
14+
from mcp_server.infrastructure.config import CLAUDE_PROJECTS_DIR
1415

1516

1617
def _try_setup_db() -> dict | None:
@@ -112,8 +113,8 @@ def _count_memories(conn) -> int:
112113

113114

114115
def _count_session_files() -> int:
115-
"""Count JSONL session files in ~/.claude/projects/."""
116-
projects_dir = Path.home() / ".claude" / "projects"
116+
"""Count JSONL session files under the host's transcript root."""
117+
projects_dir = CLAUDE_PROJECTS_DIR
117118
if not projects_dir.exists():
118119
return 0
119120
count = 0

‎mcp_server/infrastructure/config.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@
2020
if _CLAUDE_DIR_OVERRIDE
2121
else Path.home() / ".claude"
2222
)
23+
# Claude Code writes its session transcripts under its configuration directory,
24+
# ``$CLAUDE_CONFIG_DIR`` when set, else ``~/.claude`` (the same resolution as
25+
# scripts/init_plugin.py ``data_dir``). ``CORTEX_CLAUDE_DIR`` still wins: it is
26+
# the isolation seam that binds a test or sandboxed run to one throwaway tree.
27+
# Only the transcript root follows the host variable; Cortex's own data stays
28+
# under ``CLAUDE_DIR`` so setting it never relocates an existing store.
29+
# source: issue #710 ("The profile code ignores the host's config-directory
30+
# variable when locating transcripts.")
31+
_HOST_CONFIG_DIR = os.environ.get("CLAUDE_CONFIG_DIR", "").strip()
32+
CLAUDE_PROJECTS_DIR = (
33+
Path(_HOST_CONFIG_DIR).expanduser()
34+
if _HOST_CONFIG_DIR and not _CLAUDE_DIR_OVERRIDE
35+
else CLAUDE_DIR
36+
) / "projects"
2337
METHODOLOGY_DIR = CLAUDE_DIR / "methodology"
2438
PROFILES_PATH = METHODOLOGY_DIR / "profiles.json"
2539
SESSION_LOG_PATH = METHODOLOGY_DIR / "session-log.json"

‎mcp_server/infrastructure/scanner.py‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from pathlib import Path
1414
from typing import Any
1515

16-
from mcp_server.infrastructure.config import CLAUDE_DIR
16+
from mcp_server.infrastructure.config import CLAUDE_PROJECTS_DIR
1717
from mcp_server.infrastructure.file_io import list_dir, read_text_file, stat_file
1818
from mcp_server.infrastructure.transcript_activity import transcript_activity
1919
from mcp_server.infrastructure.scanner_parse import (
@@ -112,7 +112,7 @@ def _parse_memory_file(
112112

113113
def discover_all_memories() -> list[dict[str, Any]]:
114114
"""Discover all memory files across Claude project directories."""
115-
projects_dir = CLAUDE_DIR / "projects"
115+
projects_dir = CLAUDE_PROJECTS_DIR
116116
entries = list_dir(projects_dir, with_file_types=True)
117117
if not entries:
118118
return []
@@ -210,7 +210,7 @@ def _scan_project_conversations(
210210

211211
def discover_conversations() -> list[dict[str, Any]]:
212212
"""Discover all conversation JSONL files across Claude project directories."""
213-
projects_dir = CLAUDE_DIR / "projects"
213+
projects_dir = CLAUDE_PROJECTS_DIR
214214
entries = list_dir(projects_dir, with_file_types=True)
215215
if not entries:
216216
return []
@@ -235,16 +235,16 @@ def discover_conversations_for_projects(
235235
236236
Precondition: project_ids names project directories as produced by
237237
discover_conversations()/group_by_project() (entries under
238-
CLAUDE_DIR/projects); limit > 0. Postcondition: returns at most `limit`
238+
CLAUDE_PROJECTS_DIR); limit > 0. Postcondition: returns at most `limit`
239239
conversation records, drawn
240240
only from JSONL files under the given project directories -- every
241-
other project directory under CLAUDE_DIR/projects is left unscanned.
241+
other project directory under CLAUDE_PROJECTS_DIR is left unscanned.
242242
243243
source: ADR-0595"""
244244
if not project_ids or limit <= 0:
245245
return []
246246

247-
projects_dir = CLAUDE_DIR / "projects"
247+
projects_dir = CLAUDE_PROJECTS_DIR
248248
conversations: list[dict[str, Any]] = []
249249

250250
for project_id in project_ids:

‎scripts/setup_db.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import shutil
2121
import subprocess
2222
import sys
23-
from pathlib import Path
2423

2524

2625
def _safe_ident(name: str) -> str:
@@ -249,9 +248,10 @@ def _count_memories(database_url: str) -> int:
249248

250249

251250
def _count_session_files() -> int:
252-
"""Count JSONL session files in ~/.claude/projects/."""
251+
"""Count JSONL session files under the host's transcript root (#710)."""
252+
from mcp_server.infrastructure.config import CLAUDE_PROJECTS_DIR # noqa: PLC0415 — reached only after _init_schema has imported mcp_server.infrastructure.pg_schema, so it adds no failure path; resolved at call time
253253

254-
projects_dir = Path.home() / ".claude" / "projects"
254+
projects_dir = CLAUDE_PROJECTS_DIR
255255
if not projects_dir.exists():
256256
return 0
257257
count = 0

‎tests_py/_pg_safety_guards.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,17 @@ def _resolved_real_data_roots() -> list[tuple[str, str]]:
7575
too early, or a future edit that moves the redirection below the first
7676
import, would leave these constants bound to the real `~/.claude`
7777
while the env vars still look correct."""
78-
from mcp_server.infrastructure.config import CLAUDE_DIR, WIKI_ROOT
78+
from mcp_server.infrastructure.config import (
79+
CLAUDE_DIR,
80+
CLAUDE_PROJECTS_DIR,
81+
WIKI_ROOT,
82+
)
7983
from mcp_server.infrastructure.memory_config import get_memory_settings
8084

8185
settings = get_memory_settings()
8286
return [
8387
("CLAUDE_DIR", str(CLAUDE_DIR)),
88+
("CLAUDE_PROJECTS_DIR", str(CLAUDE_PROJECTS_DIR)),
8489
("WIKI_ROOT", str(WIKI_ROOT)),
8590
("MemorySettings.DB_PATH", settings.DB_PATH),
8691
("MemorySettings.SQLITE_FALLBACK_PATH", settings.SQLITE_FALLBACK_PATH),

‎tests_py/handlers/conftest.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ def hermetic_claude_dirs(tmp_path, monkeypatch):
1313
stored profiles under ``<root>/methodology/``.
1414
"""
1515
methodology = tmp_path / "methodology"
16-
monkeypatch.setattr("mcp_server.infrastructure.scanner.CLAUDE_DIR", tmp_path)
16+
monkeypatch.setattr(
17+
"mcp_server.infrastructure.scanner.CLAUDE_PROJECTS_DIR", tmp_path / "projects"
18+
)
1719
monkeypatch.setattr(
1820
"mcp_server.infrastructure.brain_index_store.BRAIN_INDEX_PATH",
1921
tmp_path / "brain-index.json",

‎tests_py/handlers/test_backfill_contract.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
@pytest.fixture
1414
def machine(monkeypatch, tmp_path):
1515
root = tmp_path / "claude"
16-
monkeypatch.setattr(backfill_helpers, "CLAUDE_DIR", root)
16+
monkeypatch.setattr(backfill_helpers, "CLAUDE_PROJECTS_DIR", root / "projects")
1717
store, encoder = Mock(), Mock()
1818
store.get_entity_by_name.return_value = {"id": 90}
1919
monkeypatch.setattr(backfill, "get_shared_store", lambda *args: store)

0 commit comments

Comments
 (0)