Skip to content

fix(memory): honor PI_CODING_AGENT_DIR for user-scope memory#166

Merged
tintinweb merged 3 commits into
tintinweb:masterfrom
biowaffeln:fix/memory-user-scope-agent-dir
Jul 23, 2026
Merged

fix(memory): honor PI_CODING_AGENT_DIR for user-scope memory#166
tintinweb merged 3 commits into
tintinweb:masterfrom
biowaffeln:fix/memory-user-scope-agent-dir

Conversation

@biowaffeln

Copy link
Copy Markdown
Contributor

as someone who likes to keep my dotfiles inside ~/.config it's a bit annoying that the agent-memory feature hardcodes ~/.pi as the global memory dir. This PR adds behaviour for respecting the PI_CODING_AGENT_DIR dir. Let me know if you have any feedback!

Code written by Claude Fable 5. Here's it's PR description:

 │ fix(memory): honor PI_CODING_AGENT_DIR for user-scope memory
 │
 │ While setting up agent memory I noticed that the user scope writes to a hardcoded ~/.pi/agent-memory/, ignoring PI_CODING_AGENT_DIR. Everything else in the
 │ codebase (custom-agents, enabled-models, settings, index) already routes through getAgentDir() — memory.ts seems to be the one exception. For anyone who
 │ relocated their config dir, the first memory: user agent silently resurrects ~/.pi.
 │
 │ Changes
 │ - user scope resolves to <agentDir>/agent-memory/<name> (default ~/.pi/agent/agent-memory/)
 │ - Back-compat: if the legacy ~/.pi/agent-memory/<name> exists and the new location doesn't, it keeps being used, so existing memories aren't orphaned. Once
 │   the new location exists, it wins.
 │ - Symlinked legacy dirs are ignored, consistent with the existing symlink defenses in this file
 │ - Tests for resolution, fallback, precedence, and symlink rejection (with homedir mocked); README updated
 │
 │ One thing worth a maintainer's eye: I went with lazy fallback rather than auto-migration — moving files felt too invasive for this fix. Happy to add
 │ migration if you'd prefer that.
 │
 │ npm test (751 passing), biome check, tsc --noEmit all clean.

resolveMemoryDir hardcoded ~/.pi/agent-memory/ for the user scope, ignoring
a relocated agent dir (PI_CODING_AGENT_DIR). Every other module already
routes through getAgentDir() — memory.ts was the one exception, silently
recreating ~/.pi for users who moved their config.

- user scope now resolves to <agentDir>/agent-memory/<name>
- legacy ~/.pi/agent-memory/<name> is still used (read + write) when it
  exists and the new location doesn't, so existing memories aren't orphaned
- symlinked legacy dirs are ignored (consistent with existing symlink defense)
- tests for new resolution, legacy fallback, preference order, and symlink
  rejection (homedir mocked); README updated
The user-scope tests leaked the real homedir: the legacy ~/.pi/agent-memory
fallback check hit actual home state, so they passed only in a clean
environment. memory-legacy-fallback.test.ts additionally crashed at import
because the hoisted homedir mock returned undefined while pi-coding-agent
evaluates getAgentDir() at module load.

- default the hoisted homedir mock to a valid string so import-time
  getAgentDir() no longer throws
- mock homedir in memory.test.ts and point it at a temp home so the legacy
  fallback check is deterministic regardless of the developer's ~/.pi state
- drop the vacuous "falls back to legacy" test in memory.test.ts; its real
  coverage lives in memory-legacy-fallback.test.ts
@tintinweb
tintinweb merged commit a47595a into tintinweb:master Jul 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants