basic-memory icon indicating copy to clipboard operation
basic-memory copied to clipboard

[BUG] bm_search mode=permalink cannot find notes whose stored permalink lacks the active project prefix

Open SWSAmor opened this issue 1 month ago • 1 comments

Bug Description

Since 1.0, bm_search with mode="permalink" rewrites every query into the active project's index form before matching: canonical_memory_path_for_active_route() (called from mcp/tools/ranked_search.py) prepends the project slug to any pattern that does not already start with it. This is the #1812 fix (#1830, bd0eaaf50, "permalink patterns match project-relative when a project is given").

The rewrite assumes every stored permalink carries the active project's prefix. Two kinds of stored permalink do not, and both become unreachable in permalink mode, exact or glob, in every query form:

  1. An explicit permalink set in frontmatter (permalink: notes/bare). Basic Memory stores it verbatim, and permalinks_include_project is documented as "Existing permalinks remain unchanged unless explicitly updated" — so every permalink written before the prefix was introduced is of this kind too.
  2. A permalink carrying another project's slug (other/notes/foreign), e.g. notes copied, or a directory cloned, from project other into project repro.

bm_read on the same permalink and bm_find by file name resolve both notes, so the note looks present everywhere except permalink search, which answers an empty success.

Steps To Reproduce

A fresh project under a throwaway HOME, so no stale index is involved:

export HOME=$(mktemp -d) BASIC_MEMORY_CONFIG_DIR="$HOME/.basic-memory"
mkdir -p "$HOME/repro"
bm project add repro "$HOME/repro" --local
bm tool write --project repro --identifier notes/generated.md --content $'# Generated\n\nBody.'
bm tool write --project repro --identifier notes/bare.md \
  --content $'---\npermalink: notes/bare\n---\n\n# Bare\n\nBody.'
bm tool write --project repro --identifier notes/foreign.md \
  --content $'---\npermalink: other/notes/foreign\n---\n\n# Foreign\n\nBody.'

for q in notes/generated notes/bare notes/foreign other/notes/foreign 'notes/*' '*bare' '*foreign'; do
  bm tool search --project repro --mode permalink --json "$q"
done

Expected Behavior

A query equal to a stored permalink finds that note, and a glob that matches a stored permalink returns it: notes/bare and *bare find notes/bare, other/notes/foreign and *foreign find other/notes/foreign — in addition to the project-relative convenience #1812 added.

Actual Behavior

Stored permalinks (bm tool read --json):

notes/generated.md -> repro/notes/generated
notes/bare.md -> notes/bare
notes/foreign.md -> other/notes/foreign

bm tool search --mode permalink:

'notes/generated'        total=1 -> ['repro/notes/generated']
'notes/bare'             total=0 -> []
'notes/foreign'          total=0 -> []
'other/notes/foreign'    total=0 -> []
'notes/*'                total=1 -> ['repro/notes/generated']
'*bare'                  total=0 -> []
'*foreign'               total=0 -> []

Controls: bm tool read notes/bare and bm tool read other/notes/foreign resolve both notes; bm tool find notes --name '*.md' returns all 3.

Environment

  • OS: macOS 27.2 (Darwin 27.2.0)
  • Python version: 3.13 (the formula's)
  • Basic Memory version: 1.0.0
  • Installation method: Homebrew (basicmachines-co/basic-memory/basic-memory)
  • Claude Desktop version: not applicable — the MCP tool through a gateway, and bm tool search; identical results

Additional Context

  • Config: permalinks_include_project: true (the default).
  • Not index state: on a real project (353 notes, mixed bare / foreign-slug / prefixed permalinks), bm reindex -p <project> --full --search ("353 observed, 353 indexed, 0 failed") left every result unchanged.
  • Code path: mcp/tools/ranked_search.py (the search_mode == "permalink" branch before _build_search_query) → mcp/project_context_identifiers.py canonical_memory_path_for_active_route() → repository/sqlite_search_query.py (permalink = :permalink / GLOB :permalink).
  • Same class as #1549: a stored permalink that is not a fixed point of the normalisation is unreachable through the normalising path, and the miss is a silent empty result.

Possible Solution

As suggested in #1549: match the literal pattern as well as the project-prefixed one — e.g. permalink = :literal OR permalink = :prefixed (and the same pair for GLOB) — so #1812's project-relative convenience stays and explicit or foreign-slug permalinks are reachable again. Failing that, an empty answer that names the rewritten pattern would make the miss diagnosable.

SWSAmor avatar Nov 02 '26 14:11 SWSAmor

I'd like to fix the permalink search mismatch and add regression tests for exact and wildcard queries.

gezerui888 avatar Nov 05 '26 09:11 gezerui888