[BUG] memory: project resolves .claude/agent-memory against the launch cwd, not the project root that supplied the agent definition (re-file of #25140)
Preflight Checklist
- [x] I have searched existing issues and this hasn't been reported yet (the prior report, #25140, is closed and locked; see Additional Information)
- [x] This is a single bug report
- [x] I am using the latest version of Claude Code
What's Wrong?
A custom subagent defined with memory: project gets its persistent memory directory resolved against the session's launch working directory, not against the project root that supplied the agent definition.
The two halves of the feature disagree. Agent discovery walks up from the cwd, so a session launched in a subdirectory still finds .claude/agents/<name>.md at the project root. Memory anchoring does not walk up, so the same session creates a fresh <launch-cwd>/.claude/agent-memory/<name>/ in the subdirectory. Each launch location grows its own store, and memory content fragments across them.
The directory is also created eagerly when the subagent spawns, even if the agent never writes memory, so read-only tasks still scatter empty .claude/agent-memory scaffolding.
In day-to-day use of a real project (a plain folder, not a git repository) I found four separate .claude/agent-memory/<agent>/ trees after normal use: the correct one beside .claude/agents/, plus strays inside two project subfolders and inside an additional working directory. Memory files had to be consolidated by hand.
What Should Happen?
The memory directory should be created next to the .claude/agents/ directory the agent definition was loaded from, using the same walk-up that discovery already performs. One project, one store, regardless of which subdirectory the session was launched from.
Error Messages/Logs
(none: the misplacement is silent)
Steps to Reproduce
- Create a folder that is not inside any git repository, with an agent definition and a subdirectory:
````
repro\
.claude\agents\memtest.md
sub\note.txt
memtest.md:
``markdown``
---
name: memtest
description: Test agent that reports its memory path and writes one file.
model: haiku
memory: project
---
Your system prompt contains persistent-memory instructions naming a specific memory directory path. When invoked: (1) state that exact absolute directory path verbatim in your reply, (2) write a file named probe.md in that directory containing the single line "probe", (3) end your reply with the full absolute path of the file you wrote.
- Launch a session from the subdirectory:
``shell``
cd repro\sub
claude -p --model haiku --max-turns 20 --dangerously-skip-permissions "Launch the memtest subagent using the Task tool with the prompt: 'Do your standing memory task.' Then repeat the subagent's reply verbatim."
- The agent is found (discovery walks up to
repro\.claude\agents\) and replies:
````
Standing memory task complete. File written to:
repro\sub\.claude\agent-memory\memtest\probe.md
- Inspect the tree.
repro\sub\.claude\agent-memory\memtest\probe.mdexists. The correct location,repro\.claude\agent-memory\, was never created.~/.claude/agent-memory/is untouched, so this is not the user-scope fallback.
Two isolating checks with the same layout:
- Eager creation: repeat step 2 with the subagent prompt telling it to write nothing and only report its path.
sub\.claude\agent-memory\memtest\is still created, empty. - Ruling out other anchors: launching from
repro\root with--add-dirpointing at a sibling directory, or having the session runcd subvia the Bash tool before spawning the agent, both place memory correctly inrepro\.claude\agent-memory\. The anchor is specifically the session launch cwd.
Claude Model
Other
Is this a regression?
I don't know
Claude Code Version
2.1.220 (Claude Code)
Platform
Anthropic API
Operating System
Windows
Terminal/Shell
PowerShell
Additional Information
This re-files #25140 ("memory: project resolves path relative to subagent's perceived project root instead of git root"), which reported the same root cause with a git monorepo repro on macOS. It was labelled bug / has repro, received no maintainer response, was auto-closed NOT_PLANNED by the stale bot, and its lock message asks anyone still affected to file a new issue referencing it. The repro above is minimal, current (2.1.220), and covers the non-git case, where there is no repository root to rescue the anchoring.
Related reports in the same anchoring family: #26429 and #33576 (subagent cwd and CLAUDE_PROJECT_DIR problems, both citing #25140 as sharing the root cause), #52772 (the auto-memory analog), #34473 (nested .claude/.claude when a subagent's cwd falls inside .claude/).
Suggested fix: resolve .claude/agent-memory/<name>/ relative to the directory containing the .claude/agents/<name>.md that was actually loaded, mirroring the discovery walk-up, with the git toplevel as a further fallback. Also skip creating the directory until the first memory write.