Skip to content

fix(index): restrict find_related to the seed chunk's language - #123

Merged
amondnet merged 3 commits into
mainfrom
amondnet/113-find-related-same-language
Oct 14, 2026
Merged

amondnet merged 3 commits into
mainfrom
amondnet/113-find-related-same-language

Conversation

@amondnet

@amondnet amondnet commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Summary

CspIndex::find_related queried the whole semantic index, so related-code results mixed languages (for example a Rust seed returned Python chunks). Upstream semble restricts the query to the seed chunk's language. This PR mirrors that: when the seed has a language, find_related builds a same-language selector (reusing build_selector with filter_languages) and passes it to semantic_index.query. A seed without a language still searches the whole index. Exclude-seed and top_k truncation are unchanged. A selector smaller than top_k + 1 is safe: the backend clamps effective_k to the selector length.

.please/docs/references/semble.md section 4.9 already described find_related as same-language, so no doc change was needed.

E2E (temp dir with a.rs, b.py, c.rs, d.py, find-related on a.rs:1): before the fix the results were c.rs and b.py; after, only c.rs.

Related issue

Closes #113

Checklist

  • PR title follows Conventional Commits
  • Tests added or updated, and the suite passes (cargo test --workspace)
  • Lint/format pass (cargo fmt --all and cargo clippy --all-targets --all-features -- -D warnings)
  • Documentation updated if behavior changed (section 4.9 already matches the new behavior)
  • No breaking change, or a BREAKING CHANGE: note is included

Summary by cubic

Restricts CspIndex::find_related to the seed chunk's language so related-code results no longer mix languages.

  • When the seed has a non-empty language, a same-language selector is built and passed to the semantic query, mirroring upstream SembleIndex.find_related.
  • Seeds with no language, or an empty one, still search the whole index; exclude-seed and top_k truncation are unchanged.
  • When the seed's language matches no chunk in the index, find_related skips embedding and returns no results.
  • Adds tests covering the language-restricted, both unrestricted, and absent-language cases.

Closes #113.

Written for commit 5900043. Summary will update on new commits.

Turn on auto-fix

Mirror upstream SembleIndex.find_related: when the seed chunk has a
language, pass a same-language selector to the semantic query so related
results no longer mix languages. Seeds without a language still search
the whole index.
@amondnet amondnet linked an issue Oct 14, 2026 that may be closed by this pull request
2 tasks

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the find_related function in CspIndex to restrict search candidates to the seed's language when available, and introduces unit tests to validate this logic. Feedback on the changes highlights that user-specified query options, such as path and language filters, are currently ignored when building the selector. It is recommended to merge the seed's language constraint with the user-provided options to ensure all filters are respected.

Comment thread crates/csp/src/indexing/index.rs Outdated
@codacy-production

codacy-production Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 4 complexity · 6 duplication

Metric Results
Complexity 4
Duplication 6

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@codecov

codecov Bot commented Oct 14, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@codspeed

codspeed Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 5 untouched benchmarks


Comparing amondnet/113-find-related-same-language (5900043) with main (97b3fe9)

Open in CodSpeed

@amondnet
amondnet marked this pull request as ready for review October 14, 2026 09:08

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cubic analysis

All reported issues were addressed across 2 files

Linked issue analysis

Linked issue: pleaseai/code-search#113: find_related is not restricted to the seed chunk's language (upstream parity)

Status Acceptance criteria Notes
✅ A language-specific seed returns only chunks in that language find_related builds a language selector, and the mixed-language test confirms only the Rust result is returned.
✅ A seed without a language searches the whole index A seed without a language uses no selector, and the test confirms results from both languages.
Architecture diagram
sequenceDiagram
    participant Caller as Caller
    participant Csp as CspIndex
    participant Selector as Selector Builder
    participant Model as Embedding Model
    participant SI as Semantic Index

    Note over Caller,SI: CspIndex.find_related flow

    Caller->>Csp: find_related(seed, options)
    Csp->>Csp: Check top_k and chunk count
    alt top_k == 0 or no chunks
        Csp-->>Caller: Empty Vec
    else
        alt seed has language
            Csp->>Selector: build_selector(filter_languages=[seed.language])
            Selector-->>Csp: Same-language selector
        else seed has no language
            Note over Csp: No selector (whole-index search)
        end
        Csp->>Model: encode(seed.content)
        Model-->>Csp: Query embedding
        Csp->>SI: query(embedding, top_k+1, selector)
        SI-->>Csp: Candidate batch
        alt no candidates
            Csp-->>Caller: Empty Vec
        else
            Note over Csp: Exclude seed, truncate to top_k
            Csp-->>Caller: Vec of SearchResult
        end
    end
Loading

Reply with feedback, questions, or to request a fix.

Turn on auto-fix | Re-trigger cubic

Comment thread crates/csp/src/indexing/index.rs Outdated
@greptile-apps

greptile-apps Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

RetriggerConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; the new early return preserves the expected search results.

Summary

find_related now limits results to the seed's language. Seeds with no language or an empty language still search all chunks.

  • Related-code results follow the seed chunk’s language when it is known.

Reviews (3) · Last reviewed commit: "perf(index): skip embedding in find_rela..." · Reviewed by Greptile

@amondnet

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the find_related function in CspIndex to restrict search candidates to the seed's language when one is present, and adds corresponding unit tests to verify this behavior. The feedback suggests a performance optimization to return early when the language selector is empty, thereby avoiding expensive model encoding and index querying.

Comment thread crates/csp/src/indexing/index.rs
@amondnet

Copy link
Copy Markdown
Contributor Author

/gemini review

@sonarqubecloud

Copy link
Copy Markdown

@amondnet
amondnet merged commit 35e8bf1 into main Oct 14, 2026
12 checks passed
@amondnet
amondnet deleted the amondnet/113-find-related-same-language branch October 14, 2026 09:44

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the find_related function in CspIndex to restrict search candidates to the seed's language when one is present, and adds corresponding unit tests. Feedback highlights that the implementation currently ignores other query filters (such as path filters) provided in the input options when constructing the candidate selector, and suggests combining the seed's language restriction with the caller's options.

Comment on lines +268 to +277
let selector = seed
.language
.as_ref()
.filter(|lang| !lang.is_empty())
.and_then(|lang| {
self.build_selector(&QueryOptions {
filter_languages: Some(vec![lang.clone()]),
..Default::default()
})
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

[MEDIUM] Query filters in options are ignored in find_related

Problem: The find_related function accepts options: &QueryOptions, but it completely ignores any path filters (filter_paths) or language filters (filter_languages) specified in options when constructing the candidate selector.
Rationale: To ensure correctness and consistency, find_related should respect the query filters provided by the caller, while still overlaying the seed's language restriction when applicable.
Suggestion: Combine the seed's language restriction with the filters from options before building the selector.

Suggested change
let selector = seed
.language
.as_ref()
.filter(|lang| !lang.is_empty())
.and_then(|lang| {
self.build_selector(&QueryOptions {
filter_languages: Some(vec![lang.clone()]),
..Default::default()
})
});
let mut query_opts = QueryOptions {
filter_paths: options.filter_paths.clone(),
..Default::default()
};
if let Some(lang) = seed.language.as_ref().filter(|lang| !lang.is_empty()) {
query_opts.filter_languages = Some(vec![lang.clone()]);
} else {
query_opts.filter_languages = options.filter_languages.clone();
}
let selector = self.build_selector(&query_opts);

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.

find_related is not restricted to the seed chunk's language (upstream parity)

1 participant