Skip to content

fix(retrieval): keep orthogonal members in the hierarchy member pool - #498

Open
gouyu-hou wants to merge 1 commit into
Tencent-Hunyuan:mainfrom
gouyu-hou:fix/hierarchy-member-zero-similarity
Open

gouyu-hou wants to merge 1 commit into
Tencent-Hunyuan:mainfrom
gouyu-hou:fix/hierarchy-member-zero-similarity

Conversation

@gouyu-hou

Copy link
Copy Markdown

Problem

build_hierarchy_bundle_candidate ranks the members of each selected topic before truncating to per_node_pool. The rank key used:

-(cosine(query_embedding, embeddings.get(memory_id) or []) or -1.0)

cosine() (defined in this file) returns None when a member has no usable vector (missing, zero-norm, or dimension mismatch), but 0.0 is a perfectly valid similarity for orthogonal vectors. Because 0.0 or -1.0 evaluates to -1.0, an orthogonal member gets +1.0 as its rank key and sorts behind a member with negative similarity (-(-0.2) = +0.2). When the pool is truncated, the orthogonal member is dropped first and a strictly worse member is kept.

Fix

Resolve None explicitly instead of relying on truthiness, matching the score_of() convention already used in the same module:

similarity = cosine(query_embedding, embeddings.get(memory_id) or [])
-(similarity if similarity is not None else -1.0)

Members with no usable vector still sort last (the -1.0 fallback is retained).

Evidence

tests/test_hierarchy_member_pool.py drives the real build_hierarchy_bundle_candidate with a stub graph/vector store and three members with cosines 1.0, 0.0, -0.2, truncating the pool to 2:

  • On main: test_orthogonal_member_beats_a_negative_one_in_the_pool FAILS ('m_zero' in ['m_pos', 'm_neg'] is false), i.e. the orthogonal member is lost.
  • With this patch: both tests pass.
  • A second test guards the fallback: a zero-vector member (cosine -> None) still sorts last and is not promoted by the change.

ruff check passes on both changed files.

Scope

Only the member-pool sort key changes. No public API, config, or storage format changes; no new dependencies.

cosine() returns None when a member has no usable embedding, but 0.0 is a valid similarity for orthogonal vectors. The old rank key used -(cosine(...) or -1.0), which folded 0.0 into -1.0 and made the key +1.0, so a genuinely orthogonal member sorted behind a negative-similarity one and was dropped first by [:per_node_pool].

Use 'similarity if similarity is not None else -1.0' instead, matching the score_of() convention in the same file. Members without a vector still sort last.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant