fix: cypher prefix matching in embeddings query#301
fix: cypher prefix matching in embeddings query#301portwebdesign wants to merge 2 commits intovitali87:mainfrom
Conversation
Summary of ChangesHello @portwebdesign, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves a bug in the Cypher query responsible for retrieving embeddings by adjusting how project name prefixes are handled. By eliminating an internal string concatenation within the Cypher query and instead relying on the Python application to provide the complete prefix, the change significantly improves the query's stability and prevents specific type-related errors, ensuring more dependable data retrieval. Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
Greptile OverviewGreptile Summary
Confidence Score: 2/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant GU as GraphUpdater
participant CS as constants.py
participant DB as Neo4j/Ingestor
GU->>CS: uses CYPHER_QUERY_EMBEDDINGS
GU->>DB: fetch_all(query, {project_name: project_name + "."})
DB-->>DB: MATCH Module->DEFINES->Function/Method
DB-->>DB: WHERE m.qualified_name STARTS WITH $project_name
DB-->>GU: results rows
GU-->>GU: embed_code() + store_embedding() for each row
|
codebase_rag/constants.py
Outdated
| CYPHER_QUERY_EMBEDDINGS = """ | ||
| MATCH (m:Module)-[:DEFINES]->(n) | ||
| WHERE (n:Function OR n:Method) | ||
| AND m.qualified_name STARTS WITH $project_name + '.' | ||
| AND m.qualified_name STARTS WITH $project_name |
There was a problem hiding this comment.
Prefix semantics mismatch
CYPHER_QUERY_EMBEDDINGS now uses m.qualified_name STARTS WITH $project_name, but graph_updater.py still calls this query with {"project_name": self.project_name + "."} (see codebase_rag/graph_updater.py:371-373). That changes the meaning from "module starts with <project>." to "module starts with <project>. (including dot)"; if qualified_name is stored without a trailing dot separator (common), this will return zero rows and embeddings will never generate. Either update the caller to pass the exact prefix you want (with/without dot) consistently, or restore the query-side concatenation in a type-safe way.
Prompt To Fix With AI
This is a comment left during a code review.
Path: codebase_rag/constants.py
Line: 420:423
Comment:
**Prefix semantics mismatch**
`CYPHER_QUERY_EMBEDDINGS` now uses `m.qualified_name STARTS WITH $project_name`, but `graph_updater.py` still calls this query with `{"project_name": self.project_name + "."}` (see `codebase_rag/graph_updater.py:371-373`). That changes the meaning from "module starts with `<project>.`" to "module starts with `<project>.` (including dot)"; if `qualified_name` is stored without a trailing dot separator (common), this will return zero rows and embeddings will never generate. Either update the caller to pass the exact prefix you want (with/without dot) consistently, or restore the query-side concatenation in a type-safe way.
How can I resolve this? If you propose a fix, please make it concise.There was a problem hiding this comment.
Code Review
This pull request aims to fix a type error in a Cypher query by removing string concatenation. While the intention is good, the change as-is could lead to incorrect query results by matching unintended projects. I've suggested a more robust query logic that handles edge cases correctly and prevents ambiguity. This proposed change relies on fixing the root cause of the type error in the calling Python code, which is the recommended approach.
codebase_rag/constants.py
Outdated
| MATCH (m:Module)-[:DEFINES]->(n) | ||
| WHERE (n:Function OR n:Method) | ||
| AND m.qualified_name STARTS WITH $project_name + '.' | ||
| AND m.qualified_name STARTS WITH $project_name |
There was a problem hiding this comment.
This change avoids the Cypher type error, but it can cause incorrect matches. For example, a search for project foo would also match foobar.
A more robust query should match either the exact project name (for root modules) or a dotted prefix. This prevents ambiguity and correctly includes root modules.
The original type error suggests $project_name is not always a string. This should be fixed in the calling Python code. Assuming that fix, the query can be improved as follows:
| AND m.qualified_name STARTS WITH $project_name | |
| AND (m.qualified_name = $project_name OR m.qualified_name STARTS WITH $project_name + '.') |
There was a problem hiding this comment.
Pull request overview
Updates the embeddings Cypher query to avoid string concatenation inside STARTS WITH, preventing Cypher type/precedence errors and relying on Python to pass the complete module-qualified-name prefix.
Changes:
- Remove Cypher-side concatenation (
$project_name + '.') fromCYPHER_QUERY_EMBEDDINGS. - Make
STARTS WITHcompare directly against the provided parameter value.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
codebase_rag/constants.py
Outdated
| MATCH (m:Module)-[:DEFINES]->(n) | ||
| WHERE (n:Function OR n:Method) | ||
| AND m.qualified_name STARTS WITH $project_name + '.' | ||
| AND m.qualified_name STARTS WITH $project_name |
There was a problem hiding this comment.
The Cypher parameter is now expected to be a fully-formed qualified-name prefix (e.g. "<project>."), but it’s still named $project_name. Renaming the parameter to something like $project_prefix/$module_qn_prefix (and updating the caller) would make the query contract clearer and reduce the risk of future callers passing the raw project name and accidentally matching similarly-prefixed projects.
| AND m.qualified_name STARTS WITH $project_name | |
| AND m.qualified_name STARTS WITH $project_prefix |
0281c40 to
0ba3b65
Compare
9b88a0b to
8370545
Compare
This change avoids string concatenation inside the Cypher query by passing the fully-formed prefix from Python.
It prevents type errors like Invalid types: bool and string for '+' when m.qualified_name contains unexpected values.