fix(cli): add --data-dir flag + AGENTMEMORY_DATA_DIR so engine state lives outside repos#314
fix(cli): add --data-dir flag + AGENTMEMORY_DATA_DIR so engine state lives outside repos#314mvanhorn wants to merge 1 commit into
Conversation
…lives outside repos (rohitg00#303) Signed-off-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
|
@mvanhorn is attempting to deploy a commit to the rohitg00's projects Team on Vercel. A member of the Team first needs to authorize it. |
📝 WalkthroughWalkthroughThis PR adds configurable data directory support to agentmemory to prevent the engine from writing state into user project directories when started from within a git repository. It introduces a new CLI module to resolve data directories with precedence (flag > env var > platform default), integrates it into the CLI startup and runtime config generation, updates Docker Compose to use the resolved path, and documents the feature. ChangesConfigurable data directory to prevent repo pollution
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
test/cli-data-dir.test.ts (1)
1-73: ⚡ Quick winAdd test coverage for git relocation behavior.
The test suite doesn't verify the
relocatedFromfield that's returned when the default data directory is inside a git worktree. Adding tests for this behavior would have caught the logic bug insrc/cli-data-dir.tslines 99-110.📝 Example test to add
it("relocates default when cwd is inside a git repo", () => { const cwd = "/repo/project"; const home = "/home/alex"; const resolved = resolveDataDir({ args: [], env: {}, cwd, home, platform: "linux", }); // Should relocate away from XDG default when inside a git repo expect(resolved.source).toBe("default"); expect(resolved.relocatedFrom).toBeDefined(); });🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@test/cli-data-dir.test.ts` around lines 1 - 73, The tests are missing coverage for the git relocation behavior: add a test calling resolveDataDir with no args/env and cwd inside a git worktree to assert source === "default" and that relocatedFrom is defined; if that test fails, fix the relocation logic inside resolveDataDir so that when the computed default dataDir would live inside the current cwd (or git worktree) the function moves it out and sets the relocatedFrom property to the original path (ensure the branch that computes the alternate dataDir assigns relocatedFrom before returning). Target the resolveDataDir function and the relocatedFrom field to locate and update the code and add the corresponding test case to the test suite.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/cli-data-dir.ts`:
- Around line 99-110: The relocation check is using nearestGitParent(defaultDir)
but should detect if the current working directory is inside a git repo; change
the condition to call nearestGitParent(cwd) instead of
nearestGitParent(defaultDir) so that the block which computes relocated via
platformDefaultDataDir(env, home, nodePlatform, false) and returns { dataDir:
relocated, source: "default", relocatedFrom: defaultDir } executes when the
process is started inside a git repository; update any variable references
around that if needed to keep the existing logic (defaultDir, relocated) intact.
---
Nitpick comments:
In `@test/cli-data-dir.test.ts`:
- Around line 1-73: The tests are missing coverage for the git relocation
behavior: add a test calling resolveDataDir with no args/env and cwd inside a
git worktree to assert source === "default" and that relocatedFrom is defined;
if that test fails, fix the relocation logic inside resolveDataDir so that when
the computed default dataDir would live inside the current cwd (or git worktree)
the function moves it out and sets the relocatedFrom property to the original
path (ensure the branch that computes the alternate dataDir assigns
relocatedFrom before returning). Target the resolveDataDir function and the
relocatedFrom field to locate and update the code and add the corresponding test
case to the test suite.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: e47788fc-09f1-41a2-a834-a3ab49a9e16e
📒 Files selected for processing (5)
README.mddocker-compose.ymlsrc/cli-data-dir.tssrc/cli.tstest/cli-data-dir.test.ts
| const defaultDir = platformDefaultDataDir(env, home, nodePlatform); | ||
| const gitParent = nearestGitParent(defaultDir); | ||
| if (gitParent) { | ||
| const relocated = platformDefaultDataDir(env, home, nodePlatform, false); | ||
| if (relocated !== defaultDir) { | ||
| return { | ||
| dataDir: relocated, | ||
| source: "default", | ||
| relocatedFrom: defaultDir, | ||
| }; | ||
| } | ||
| } |
There was a problem hiding this comment.
Git relocation logic checks the wrong path.
Lines 100-110 check whether the platform default data directory is inside a git repo (nearestGitParent(defaultDir)), but the PR objective states "auto-relocate when started inside a git repo" — meaning when the user's current working directory is inside a git repo.
The logic should check nearestGitParent(cwd) instead of nearestGitParent(defaultDir). Currently, relocation only happens if the data directory default (e.g., ~/.local/share/agentmemory) itself is inside a git repo, which is almost never the case.
🐛 Proposed fix
const defaultDir = platformDefaultDataDir(env, home, nodePlatform);
- const gitParent = nearestGitParent(defaultDir);
+ const gitParent = nearestGitParent(cwd);
if (gitParent) {🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@src/cli-data-dir.ts` around lines 99 - 110, The relocation check is using
nearestGitParent(defaultDir) but should detect if the current working directory
is inside a git repo; change the condition to call nearestGitParent(cwd) instead
of nearestGitParent(defaultDir) so that the block which computes relocated via
platformDefaultDataDir(env, home, nodePlatform, false) and returns { dataDir:
relocated, source: "default", relocatedFrom: defaultDir } executes when the
process is started inside a git repository; update any variable references
around that if needed to keep the existing logic (defaultDir, relocated) intact.
Summary
Adds a
--data-dirflag andAGENTMEMORY_DATA_DIRenv var so the engine writes its state outside the caller's cwd. Defaults to a platform-appropriate user dir, so the documentednpx @agentmemory/agentmemoryflow no longer drops adata/tree into whatever repo you happen to be in.What changed
src/cli-data-dir.ts— new resolver: flag > env > platform default.~expansion,${VAR}interpolation,.and./datahonored as explicit opt-ins for repo-local state.src/cli.ts—--data-dir <path>parsed alongside--port; writes a runtimeiii-config.yamlinto the resolved dir sostate_store.dbandstream_storelive there too.--helpdocuments the flag.docker-compose.yml— fallback mount becomes${AGENTMEMORY_DATA_DIR:-iii-data}to/data.README.md— short section on the new flag and env var.test/cli-data-dir.test.ts— covers flag-wins, env-fallback, default-not-cwd, repo-local opt-in,~expansion.Default by platform:
~/Library/Application Support/agentmemory$XDG_DATA_HOME/agentmemory(falls back to~/.local/share/agentmemory)~/AppData/Local/agentmemoryTesting
npm testandnpm run buildcouldn't run locally becausevitest/tsdownaren't installed in this sandbox (npm installblocked by network). The new tests are written against the existing vitest shape used elsewhere intest/; CI / CodeRabbit will exercise them.Fixes #303
Signed-off-by: Matt Van Horn mvanhorn@gmail.com
Summary by CodeRabbit
New Features
--data-dircommand-line flag orAGENTMEMORY_DATA_DIRenvironment variable. Platform-specific default storage locations are supported.Documentation