Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(hooks): resolve repo-root host-side for out-of-tree worktrees
omarteloc1a2303d2328
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex dd61e1cf..c9ebf33a 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -8,6 +8,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0## [Unreleased]### Fixed+- `project_strategy = "repo-root"` no longer falls back to `basename(cwd)` for+ git worktrees whose directory lives outside the main repo tree when the server+ runs in a container. The server resolves repo-root via libgit2 on the incoming+ `cwd`, which fails when that host path is not visible inside the container, so+ every such worktree became its own project. The POSIX shell hooks (and the+ PowerShell hook) now resolve the main repo root host-side — following the+ worktree commondir pointer with `git rev-parse --git-common-dir` — and send it+ as an explicit `project`, so linked worktrees collapse to one stable project+ regardless of where the worktree directory lives or how the server is+ deployed. git is only touched when `cwd` is inside a real work tree. (issue #110)- Unscoped MCP queries could resolve to the wrong project on a sharedinstall. The cwd-to-project resolver recorded the bare working directoryas a project's `repo_path` whenever the cwd was not inside a git repodiff --git a/docs/marker-file.md b/docs/marker-file.mdindex 0a4ae50e..32e2c30a 100644--- a/docs/marker-file.md+++ b/docs/marker-file.md@@ -113,6 +113,16 @@ Outcome:Without `project_strategy = "repo-root"`, those same paths keep thedefault behavior and resolve by their current directory basename.+Resolution is host-side: the lifecycle hook follows the worktree's+commondir pointer (`git rev-parse --git-common-dir`) to the main+repository and sends the resolved name as an explicit `project`. This+means it works even when the worktree directory lives **outside** the+main repo tree (some tools keep worktrees in a separate directory, so+the worktree has no `.ai-memory.toml` ancestor of its own) and even when+the server runs in a container that cannot see the host checkout. Put+the marker anywhere on the walk-up path from the worktree — commonly a+single `~/.ai-memory.toml` — to select the strategy.+### Single workspace, no per-repo overrides```diff --git a/hooks/_lib.sh b/hooks/_lib.shindex 0064510c..811e9b74 100644--- a/hooks/_lib.sh+++ b/hooks/_lib.sh@@ -65,6 +65,34 @@ ai_memory_url_encode() {| sed 's/%/%25/g; s/+/%2B/g; s/&/%26/g; s/=/%3D/g; s/?/%3F/g; s/#/%23/g; s/ /%20/g'}+# Resolve the basename of the MAIN git repository root for "$1" (a cwd),+# following the worktree commondir pointer so every linked worktree of a+# repo collapses to one stable name. Mirrors the server's+# `discover_main_repo_root` (libgit2) but runs host-side, where the+# checkout is always visible — the server cannot do this when it runs in a+# container that has no access to the host filesystem (its own discovery+# fails and falls back to basename(cwd), so out-of-tree worktrees each+# became their own project). Prints the name, or nothing when cwd is not+# inside a git work tree (caller keeps its basename(cwd) fallback).+ai_memory_repo_root_project() {+ cwd="$1"+ [ -z "$cwd" ] && return 0+ command -v git >/dev/null 2>&1 || return 0+ # Only touch git when cwd is genuinely inside a working tree. Outside any+ # repo, or inside a bare repo, `--is-inside-work-tree` is not "true" and+ # we stay silent rather than guess.+ [ "$(git -C "$cwd" rev-parse --is-inside-work-tree 2>/dev/null)" = "true" ] || return 0+ # `--git-common-dir` is the shared `.git` dir: for a worktree it points+ # at the MAIN repo's `.git`, so its parent is always the main repo root.+ common=$(git -C "$cwd" rev-parse --path-format=absolute --git-common-dir 2>/dev/null) || return 0+ [ -n "$common" ] || return 0+ root=$(dirname "$common")+ case "$root" in+ "" | /) return 0 ;;+ esac+ basename "$root"+}+# Build a query-string suffix from "$1" plus any marker file walked up from# it. Returns the suffix with the leading `&`, or nothing when cwd is absent.# `cwd` is always included so `GET /handoff` resolves the same basename project@@ -78,6 +106,17 @@ ai_memory_marker_qs() {ws=$(ai_memory_parse_toml_key "$marker" workspace)pr=$(ai_memory_parse_toml_key "$marker" project)st=$(ai_memory_parse_toml_key "$marker" project_strategy)+ # The repo-root strategy must be resolved here, on the host: a containerized+ # server cannot see this checkout, so its own libgit2 discovery fails and+ # falls back to basename(cwd). When the marker selects repo-root and pins no+ # explicit project, derive the main repo name now and send it as an explicit+ # `project` override. `project_strategy` is still forwarded so native servers+ # keep their existing resolution path.+ if [ -z "$pr" ]; then+ case "$st" in+ repo-root | repo_root) pr=$(ai_memory_repo_root_project "$cwd") ;;+ esac+ fi[ -n "$ws" ] && qs="${qs}&workspace=$(ai_memory_url_encode "$ws")"[ -n "$pr" ] && qs="${qs}&project=$(ai_memory_url_encode "$pr")"[ -n "$st" ] && qs="${qs}&project_strategy=$(ai_memory_url_encode "$st")"diff --git a/hooks/lib/ai-memory-hook.ps1 b/hooks/lib/ai-memory-hook.ps1index 79e30ff1..61f0eb60 100644--- a/hooks/lib/ai-memory-hook.ps1+++ b/hooks/lib/ai-memory-hook.ps1@@ -46,6 +46,24 @@ function Get-AiMemoryTomlKey {return $null}+# Resolve the basename of the MAIN git repository root for $Cwd, following the+# worktree commondir pointer so every linked worktree collapses to one stable+# name. Mirrors the POSIX `ai_memory_repo_root_project`: a containerized server+# cannot see the host checkout, so repo-root must be resolved here. Returns+# $null when git is unavailable or $Cwd is not inside a git work tree.+function Get-AiMemoryRepoRootProject {+ param([string] $Cwd)+ if (-not $Cwd) { return $null }+ if (-not (Get-Command git -ErrorAction SilentlyContinue)) { return $null }+ $inside = (& git -C $Cwd rev-parse --is-inside-work-tree 2>$null)+ if ($inside -ne "true") { return $null }+ $common = (& git -C $Cwd rev-parse --path-format=absolute --git-common-dir 2>$null)+ if (-not $common) { return $null }+ $root = Split-Path $common -Parent+ if (-not $root -or $root -eq [System.IO.Path]::GetPathRoot($root)) { return $null }+ return Split-Path $root -Leaf+}+function Get-AiMemoryMarkerQuery {param([string] $Cwd)if (-not $Cwd) { return "" }@@ -55,8 +73,13 @@ function Get-AiMemoryMarkerQuery {$ws = Get-AiMemoryTomlKey -File $marker -Key "workspace"if ($ws) { $qs += "&workspace=$([uri]::EscapeDataString($ws))" }$proj = Get-AiMemoryTomlKey -File $marker -Key "project"- if ($proj) { $qs += "&project=$([uri]::EscapeDataString($proj))" }$strategy = Get-AiMemoryTomlKey -File $marker -Key "project_strategy"+ # repo-root must be resolved host-side (the server may not see this checkout);+ # only when no explicit project is pinned. Explicit project always wins.+ if (-not $proj -and ($strategy -eq "repo-root" -or $strategy -eq "repo_root")) {+ $proj = Get-AiMemoryRepoRootProject -Cwd $Cwd+ }+ if ($proj) { $qs += "&project=$([uri]::EscapeDataString($proj))" }if ($strategy) { $qs += "&project_strategy=$([uri]::EscapeDataString($strategy))" }return $qs}diff --git a/tests/hooks/test_lib.sh b/tests/hooks/test_lib.shindex c08f3b32..fe17a57e 100755--- a/tests/hooks/test_lib.sh+++ b/tests/hooks/test_lib.sh@@ -80,6 +80,52 @@ assert_eq "closer marker wins" "&cwd=$TMP/a/b/c&workspace=ws1&project=p1&projectQS3=$(ai_memory_marker_qs "$TMP/nonexistent")assert_eq "no marker -> cwd only" "&cwd=$TMP/nonexistent" "$QS3"+# --- repo-root strategy: host-side resolution -------------------------+# Outside any git repo the helper stays silent (caller keeps basename(cwd)).+assert_eq "repo_root_project on non-git path is empty" "" \+ "$(ai_memory_repo_root_project "$TMP/nonexistent")"++if command -v git >/dev/null 2>&1; then+ REPO="$TMP/repos/acme-api"+ mkdir -p "$REPO"+ git init -q "$REPO"+ git -C "$REPO" -c [email protected] -c user.name=t \+ commit -q --allow-empty -m init++ # A subdirectory of the main checkout collapses to the repo basename+ # (not the subdir name) when the marker selects repo-root and pins no+ # explicit project.+ mkdir -p "$REPO/crates/cli"+ printf 'workspace = "oss"\nproject_strategy = "repo-root"\n' >"$REPO/.ai-memory.toml"+ QSR=$(ai_memory_marker_qs "$REPO/crates/cli")+ assert_eq "repo-root: subdir resolves to repo basename" \+ "&cwd=$REPO/crates/cli&workspace=oss&project=acme-api&project_strategy=repo-root" \+ "$QSR"++ # A linked worktree whose directory lives OUTSIDE the main repo tree+ # (a common layout: tools that keep worktrees in a separate directory)+ # has no .ai-memory.toml ancestor of its own, yet still collapses to the+ # MAIN repo basename via the commondir pointer. The strategy comes from a+ # marker placed above the worktrees directory.+ WT="$TMP/worktrees/acme-api/wt-feature"+ mkdir -p "$TMP/worktrees/acme-api"+ printf 'workspace = "oss"\nproject_strategy = "repo-root"\n' >"$TMP/worktrees/.ai-memory.toml"+ if git -C "$REPO" worktree add -q "$WT" >/dev/null 2>&1; then+ QSW=$(ai_memory_marker_qs "$WT")+ assert_eq "repo-root: out-of-tree worktree collapses to main repo" \+ "&cwd=$WT&workspace=oss&project=acme-api&project_strategy=repo-root" \+ "$QSW"+ fi++ # An explicit project pin always wins over repo-root resolution.+ printf 'workspace = "oss"\nproject = "pinned"\nproject_strategy = "repo-root"\n' \+ >"$REPO/.ai-memory.toml"+ QSP=$(ai_memory_marker_qs "$REPO/crates/cli")+ assert_eq "explicit project pin beats repo-root" \+ "&cwd=$REPO/crates/cli&workspace=oss&project=pinned&project_strategy=repo-root" \+ "$QSP"+fi+# --- url_encode -------------------------------------------------------assert_eq "url_encode passes safe slug" "movvia" "$(ai_memory_url_encode "movvia")"assert_eq "url_encode escapes ampersand" "a%26b" "$(ai_memory_url_encode "a&b")"