Skip to content

Vector chunks after the first lose their note's title and permalink #1335

Closed

Description

@gkhnelbstn

Thanks for basic-memory — it backs a small bilingual (Turkish/English) engineering vault for our team, and the local-first design is why we can keep it on a shared repo of plain markdown at all.

Following CONTRIBUTING's "create an issue first to discuss the approach": while tuning multilingual retrieval I measured something in the chunk-building path that looks unintended, and the fix appears well contained.

Environment

  • basic-memory v0.22.1, sqlite backend, FastEmbed provider
  • Embedding model intfloat/multilingual-e5-large
  • Corpus: 8 shared team notes, Turkish queries against English notes
  • Code below re-checked against current main, where the logic moved to src/basic_memory/repository/semantic_chunking.py but is otherwise identical

What happens

compose_row_source_text() prepends the note's title and permalink to the row's source text. split_text_into_chunks() then splits that text on markdown headings and on every bullet. Title and permalink are ordinary non-bullet lines, so they merge into the first chunk and only the first: every later chunk — which for an observations-style note means every single - [fact] … bullet — is embedded with no indication of which note it belongs to.

For a vault whose notes are mostly bullet lists of discrete facts, that means most vectors carry no note identity at all.

Why

Two pointers in semantic_chunking.py (search_repository_base.py up to v0.22.x):

  1. compose_row_source_text() joins title, permalink, content_snippet into one blob. The title is contributed once, positionally.
  2. split_text_into_chunks() starts a new section whenever _BULLET_PATTERN matches, so the first bullet closes the title's section. Everything after inherits nothing.

Measurements

Read back from search_vector_chunks on a real vault, not estimated:

chunks 134 (8 notes)
median chunk 178 chars
p25 / p75 89 / 441 chars
longest 888 chars
under 60 chars 23 chunks (17%)

The short tail is structural rather than sloppy writing:

  • ## Relations is embedded as a standalone 12-character chunk, once per note that has one
  • each - relates_to [[Some Note]] line becomes its own chunk
  • - [name] abcws-monorepo (23 chars) is its own chunk

Direct probe of the cost, multilingual-e5-large, Turkish query against English bullets: naming the subject inside the bullet raised gold-chunk cosine from 0.769 to 0.807 (and top score 0.854 → 0.898) versus the bare bullet the chunker actually stores.

Being honest about that probe's limits: 6 candidate chunks, and it did not change ranking — both variants retrieved at rank 1. It measures the margin the missing context costs, not a recall win. On a larger vault where many notes share vocabulary, that margin is what has to separate the right note from a plausible wrong one.

Suggested shapes

Both are localized to semantic_chunking.py:

  1. Carry note identity into every chunk — prefix each chunk with the title (and optionally the permalink) rather than relying on positional inheritance in chunk 0.
  2. Don't emit content-free chunks — a chunk that is only a heading, or only a relates_to wikilink, spends an embedding and can never match a real query. Merging a bare heading into the following section would also make heading text useful to the chunks under it.

Either changes chunk text, so build_entity_fingerprint() invalidates and existing vaults re-embed on next sync — worth calling out in release notes.

Related but distinct

Offer

Happy to test a branch against this Turkish/English vault and report before/after chunk stats and retrieval scores — just point me at it. Not opening a PR directly since CONTRIBUTING asks for an issue first, and the CLA is a call for the repo owner on our side rather than mine.

Activity

  1. gkhnelbstn commented on Aug 26, 2026

    @gkhnelbstn
    Author

    Correction to my own "Related but distinct" note, before it misleads anyone reading this later.

    I wrote that embed_query() on main still delegates straight to embed_documents(), implying the prefix gap from #1264 was still open. That reading was wrong. #1044 landed in v0.23.0 and added semantic_embedding_document_prefix / semantic_embedding_query_prefix, applied by PrefixingEmbeddingProvider as a wrapper around any provider — so embed_query() is untouched by design, and the prefixes are very much applied. Including them in vector identity so a prefix change forces a re-embed is a nice touch.

    We are on v0.22.1, which predates that, which is why we saw no prefix support locally. Our own docs now say v0.23.0+ rather than "never applied".

    Nothing in the issue above depends on that — the chunk context loss and the content-free chunks reproduce on current main, which I re-checked in semantic_chunking.py after the 0.23 refactor moved it out of search_repository_base.py. Sorry for the noise.

  2. phernandez commented on Aug 28, 2026

    @phernandez
    Member

    Confirmed by reading src/basic_memory/repository/semantic_chunking.py on main: compose_row_source_text() contributes title and permalink once, positionally, and split_text_into_chunks() opens a new section at every header and every bullet (_BULLET_PATTERN), so only the first chunk carries the note's identity and a bare ## Relations heading becomes its own 12-character vector. For bullet-heavy vaults that's most of the index with no note context. The measurements make the case well; thanks for doing them.

    Happy to take a PR. Two things to design in:

    • Vector identity must change with the chunk text. Existing vectors are only re-embedded when their identity does (that's how feat(core): add semantic embedding text prefixes #1044 forces a re-embed on a prefix change), so the chunker needs a version component in the identity — otherwise upgraded installs keep the old context-free vectors until a --full reindex.
    • Keep single-relation bullets out of the "content-free" bucket unless you've measured them: - relates_to [[X]] lines are short but are how graph-adjacent retrieval currently surfaces; dropping the ## Relations heading chunk is clearly right, dropping the relation lines needs numbers.

    Same landing notes as for other contributors: DCO sign-off, CLA via the bot link on the PR, semantic title (fix(core): … or perf(core): …), and fork PRs don't get the test matrix so I'll cherry-pick onto an in-repo branch for CI and rebase-merge with your authorship. Include your before/after retrieval numbers in the PR; we'll run the internal retrieval eval on it too.

  3. phernandez commented on Aug 29, 2026

    @phernandez
    Member

    Fix is up in #1371.

    I went a different way than either suggested shape, because both treat the symptom. The root cause is that the entity body was being split into one chunk per bullet — and observations and relations are already embedded as their own search rows (observation:*, relation:*), each carrying the note title, permalink, and category/relation-type. So the per-bullet body chunks were duplicating those facts in a degraded, identity-stripped form.

    So instead of re-attaching the title to every bullet, or deciding which short chunks are "content-free" and dropping them:

    • The entity body stops splitting per bullet. It splits on headings and the size budget only; bullets stay packed with their surrounding text as context. The ## Relations heading is never stranded, because it chunks together with the list under it.
    • Per-fact retrieval keeps coming from the observation and relation rows. Nothing is dropped — a - relates_to [[X]] line still has its own vector via the relation row, so I didn't have to measure whether dropping relation bullets hurts graph-adjacent retrieval. That question goes away.

    On your two design points:

    1. Identity changing with chunk text — it does, automatically. entity_fingerprint is built from each chunk's source_hash (a hash of the chunk text), and the chunk text changes for any note with bullets. No chunker-version marker needed — the chunk text is the version signal. A plain bm reindex (no --force) re-evaluates every entity and re-embeds exactly the affected notes while prose-only notes skip; individual notes also re-embed on their next edit.
    2. Relation bullets — not dropped, so no numbers required to justify removing them.

    Your measurements are what made the shape obvious — thank you for reading them back out of search_vector_chunks rather than estimating. The offer to run your Turkish/English vault against a branch is very welcome: #1371 is the branch. Before/after chunk stats (I'd expect the sub-60-char tail to mostly disappear) and retrieval scores would be great to have on the PR, and we'll run the internal retrieval eval on it too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions