Hook resolveProject() is not worktree-aware: each git worktree becomes a separate memory silo
Summary
The hook's resolveProject() derives the project scope from git rev-parse --show-toplevel, which for a git worktree returns the worktree's own root — not the main repo. So sessions run in a worktree get a separate project scope (the worktree directory name), and memory captured/seeded for the main repo never surfaces in worktree sessions (and vice-versa). Project memory ends up fragmented across worktrees of the same repo.
Ironically the engine already knows better: mem::detect-worktree (in src/functions/branch-aware.ts) returns mainRepoRoot, and branch-aware.ts uses worktreeInfo.mainRepoRoot || data.cwd. The hook just doesn't use it.
Where
src/hooks/_project.ts:
export function resolveProject(cwd?: string): string {
const explicit = process.env["AGENTMEMORY_PROJECT_NAME"];
if (explicit && explicit.trim()) return explicit.trim();
const dir = cwd && cwd.trim() ? cwd : process.cwd();
try {
const top = execSync("git rev-parse --show-toplevel", { cwd: dir, ... }).toString().trim();
if (top) return basename(top); // <-- worktree root, not the main repo
} catch {}
return basename(dir);
}
Reproduction
# main repo at /work/habreader, worktree under it:
git -C /work/habreader worktree add .claude/worktrees/feature
git -C /work/habreader/.claude/worktrees/feature rev-parse --show-toplevel
# /work/habreader/.claude/worktrees/feature -> basename "feature" (current behavior)
git -C /work/habreader/.claude/worktrees/feature rev-parse --git-common-dir
# /work/habreader/.git -> parent basename "habreader" (desired)
Run an agent session in the worktree → observations are stored under project feature; a memory_smart_search scoped to habreader (or the SessionStart context injection for the main repo) doesn't see them, and the worktree session doesn't see the main repo's memory.
Impact
- Worktrees are a normal Claude Code workflow (
claude --worktree <name>). Each worktree silently becomes its own memory silo. - Memory seeded/curated for a repo is invisible while working in any of its worktrees, which defeats the point for users who develop features in worktrees.
Suggested fix
Resolve the main repo root in resolveProject. The git-common-dir trick works for both plain checkouts and worktrees with a single command:
// basename(dirname(git --git-common-dir)) == main repo name in BOTH cases
const common = execSync("git rev-parse --path-format=absolute --git-common-dir", { cwd: dir, ... })
.toString().trim();
if (common) return basename(dirname(common));
(For a normal checkout --git-common-dir is <repo>/.git, so basename(dirname(...)) == basename(repo) — same as today. For a worktree it's <mainrepo>/.git, giving the parent repo name.) Alternatively, route through the existing mem::detect-worktree / mainRepoRoot.
Workaround
Set AGENTMEMORY_PROJECT_NAME=<main-repo-name> in the environment the agent (and its hooks) run under. A small claude shell wrapper that exports basename(dirname($(git rev-parse --git-common-dir))) makes every worktree inherit its parent repo's scope.
Environment
agentmemory v0.9.24 (main @ fd9e3bd), src/hooks/_project.ts.