Skip to content

Commit 8ac98f0

Browse files
authored
Merge pull request MemPalace#1147 from MemPalace/fix/3.3.3-followups
fix(3.3.3): two followups from MemPalace#1145 before tag cut
2 parents 6d252a0 + 6fcfd34 commit 8ac98f0

6 files changed

Lines changed: 87 additions & 18 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
1717
- Real `python3` resolution for `.sh` hooks with a `MEMPAL_PYTHON` override path. (#833)
1818
- Add optional `wing` parameter to `tool_diary_write` / `tool_diary_read` and derive per-project wing from the Claude Code transcript path when writing from the stop hook — diary entries from different projects no longer collapse into a shared default wing. (#659)
1919
- Treat empty string as "no filter" in `mempalace_search` `wing`/`room`; LLM agents that default to filling every optional parameter with `""` no longer get bounced with `must be a non-empty string`. (#1097, #1084)
20+
- Broaden `_wing_from_transcript_path` to handle Claude Code project folders without a `-Projects-` segment (e.g. `~/dev/<parent>/<project>`, `~/code/<project>`). The project name is now derived from the final dash-separated token of the encoded folder, so Linux users with code outside `~/Projects/` get per-project diary scoping instead of falling through to `wing_sessions`. (#1145, follow-up to #659)
21+
- `mempalace_diary_read(wing="")` now returns diary entries from every wing this agent has written to, matching the #1097 "empty-string as no filter" pattern. Previously defaulted to `wing_<agent>`, siloing entries that hooks wrote to project-derived wings. (#1145)
2022

2123
### Improvements
2224

mempalace/hooks_cli.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -490,14 +490,29 @@ def _parse_harness_input(data: dict, harness: str) -> dict:
490490
def _wing_from_transcript_path(transcript_path: str) -> str:
491491
"""Derive a project wing name from a Claude Code transcript path.
492492
493-
Claude Code stores transcripts at:
493+
Claude Code encodes the project's source directory by replacing path
494+
separators with dashes, producing folders like:
494495
~/.claude/projects/-home-<user>-Projects-<project>/session.jsonl
495-
We extract <project> and return ``wing_<project>`` to match the
496-
AAAK_SPEC convention (``wing_user``, ``wing_agent``, ``wing_code``,
497-
``wing_<project>``…). Falls back to ``wing_sessions``.
496+
~/.claude/projects/-home-<user>-dev-<parent>-<project>/session.jsonl
497+
~/.claude/projects/-Users-<user>-<folder>-<project>/session.jsonl
498+
499+
The project directory name is the final dash-separated token of the
500+
encoded folder. Returns ``wing_<project>`` (lowercased, spaces → ``_``).
501+
Falls back to ``wing_sessions`` if the path does not match a Claude Code
502+
project-folder layout.
498503
"""
499504
# Normalize path separators for cross-platform (Windows backslashes)
500505
normalized = transcript_path.replace("\\", "/")
506+
# Primary: pull the encoded project folder out of ``.claude/projects/``
507+
# and take its last dash-separated token.
508+
match = re.search(r"/\.claude/projects/-([^/]+)", normalized)
509+
if match:
510+
encoded = match.group(1)
511+
project = encoded.rsplit("-", 1)[-1]
512+
if project:
513+
return f"wing_{project.lower().replace(' ', '_')}"
514+
# Legacy fallback: explicit ``-Projects-<name>`` segment, useful for
515+
# transcripts not under the standard Claude Code projects dir.
501516
match = re.search(r"-Projects-([^/]+?)(?:/|$)", normalized)
502517
if match:
503518
project = match.group(1).lower().replace(" ", "_")

mempalace/mcp_server.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -995,10 +995,11 @@ def tool_diary_read(agent_name: str, last_n: int = 10, wing: str = ""):
995995
Read an agent's recent diary entries. Returns the last N entries
996996
in chronological order — the agent's personal journal.
997997
998-
When ``wing`` is provided, reads from that wing instead of the
999-
agent's default ``wing_<agent_name>`` wing. This lets hooks
1000-
direct diary reads to a project-specific wing derived from
1001-
the transcript path.
998+
When ``wing`` is provided, reads only from that wing. When ``wing``
999+
is empty or omitted, returns entries from every wing this agent has
1000+
written to. Diary writes from hooks land in project-derived wings
1001+
(``wing_<project>``), so requiring a specific wing on read would
1002+
silo those entries from agent-initiated reads.
10021003
"""
10031004
try:
10041005
agent_name = sanitize_name(agent_name, "agent_name")
@@ -1007,21 +1008,20 @@ def tool_diary_read(agent_name: str, last_n: int = 10, wing: str = ""):
10071008
except ValueError as e:
10081009
return {"error": str(e)}
10091010
last_n = max(1, min(last_n, 100))
1010-
if not wing:
1011-
wing = f"wing_{agent_name.lower().replace(' ', '_')}"
10121011
col = _get_collection()
10131012
if not col:
10141013
return _no_palace()
10151014

1015+
# Build filter: always scope by agent + room=diary. Wing is optional —
1016+
# when empty, return entries across all wings for this agent (matches
1017+
# the #1097 empty-string-as-no-filter convention for LLM ergonomics).
1018+
conditions = [{"room": "diary"}, {"agent": agent_name}]
1019+
if wing:
1020+
conditions.insert(0, {"wing": wing})
1021+
10161022
try:
10171023
results = col.get(
1018-
where={
1019-
"$and": [
1020-
{"wing": wing},
1021-
{"room": "diary"},
1022-
{"agent": agent_name},
1023-
]
1024-
},
1024+
where={"$and": conditions},
10251025
include=["documents", "metadatas"],
10261026
limit=10000,
10271027
)

tests/test_hooks_cli.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,24 @@ def test_wing_from_transcript_path_lowercases():
324324
assert _wing_from_transcript_path(path) == "wing_myproject"
325325

326326

327+
def test_wing_from_transcript_path_non_projects_layout():
328+
# Linux users with code under ~/dev/, ~/src/, ~/code/ — no -Projects- segment.
329+
# Project name is the final dash-separated token of the encoded folder.
330+
path = "/home/igor/.claude/projects/-home-igor-dev-MemPalace-mempalace/session.jsonl"
331+
assert _wing_from_transcript_path(path) == "wing_mempalace"
332+
333+
334+
def test_wing_from_transcript_path_macos_users_layout():
335+
# macOS ~/ layout without a Projects/ segment.
336+
path = "/Users/alice/.claude/projects/-Users-alice-code-MyApp/session.jsonl"
337+
assert _wing_from_transcript_path(path) == "wing_myapp"
338+
339+
340+
def test_wing_from_transcript_path_nested_deep():
341+
path = "/home/bob/.claude/projects/-home-bob-work-clients-acme-frontend/session.jsonl"
342+
assert _wing_from_transcript_path(path) == "wing_frontend"
343+
344+
327345
# --- _log ---
328346

329347

tests/test_mcp_server.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,40 @@ def now(cls):
740740
assert entry1 in contents
741741
assert entry2 in contents
742742

743+
def test_diary_read_empty_wing_spans_all_wings(self, monkeypatch, config, palace_path, kg):
744+
"""diary_read(wing='') must return entries from every wing this agent
745+
wrote to. Hooks write to project-derived wings (#659); a reader that
746+
silos by default wing would never see those entries."""
747+
_patch_mcp_server(monkeypatch, config, kg)
748+
_client, _col = _get_collection(palace_path, create=True)
749+
del _client
750+
from mempalace.mcp_server import tool_diary_read, tool_diary_write
751+
752+
w1 = tool_diary_write(
753+
agent_name="TestAgent",
754+
entry="default-wing entry",
755+
topic="general",
756+
)
757+
w2 = tool_diary_write(
758+
agent_name="TestAgent",
759+
entry="project-wing entry",
760+
topic="general",
761+
wing="wing_someproject",
762+
)
763+
assert w1["success"] and w2["success"]
764+
765+
# Empty wing → return both entries
766+
r = tool_diary_read(agent_name="TestAgent", wing="")
767+
assert r["total"] == 2
768+
contents = {e["content"] for e in r["entries"]}
769+
assert "default-wing entry" in contents
770+
assert "project-wing entry" in contents
771+
772+
# Explicit wing → return only that wing's entries
773+
r_scoped = tool_diary_read(agent_name="TestAgent", wing="wing_someproject")
774+
assert r_scoped["total"] == 1
775+
assert r_scoped["entries"][0]["content"] == "project-wing entry"
776+
743777

744778
# ── Cache Invalidation (inode/mtime) ──────────────────────────────────
745779

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)