Repository navigation
Semantic search: add node_type and property-based filtering - #1064
Conversation
…1059) Extends SearchSemanticParams with two new optional filter parameters: - node_types: array of node types to filter by (AND logic with other filters) - property_filters: object with key-value pairs to match against node properties (AND logic) Updates the post-filter predicate in handle_search_semantic to: 1. Filter by node_types if specified (node.node_type must be in the list) 2. Filter by property_filters if specified (all key-value pairs must match) Logs debug messages when filtering out nodes due to these criteria for troubleshooting. Includes 11 unit tests covering: - Parameter parsing for both new fields - Empty/null handling - Property matching logic (partial match fails, AND logic) - Combined filters with other search parameters - Response structure includes applied filters Updates search_semantic JSON schema in tools.rs to document the new parameters with examples. Co-Authored-By: Claude Sonnet 4.6 (1M context) <[email protected]>
Code Review: Semantic Search Filtering (PR #1064)Review Type: Initial Review Requirements ValidationIssue #1059 Acceptance Criteria:
Code Review Findings🟡 Important: Acceptance Criteria Gap📁 Issue #1059 vs. Implementation Scope The issue acceptance criteria specify modifying the pub async fn semantic_search(
&self,
query: &str,
limit: usize,
threshold: f32,
node_type: Option<&str>, // NEW
property_filters: Option<&[PropertyFilter]>, // NEW
) -> Result<Vec<EmbeddingSearchResult>, NodeServiceError>Current Implementation: Filters exist only in Impact: This is a partial implementation that works for MCP clients but doesn't address the skill pipeline use case or enable other code paths to use filtering without duplicating filter logic. Recommendation: Clarify scope with the team:
✅ Code Quality: Well-Implemented MCP Handler Changes📁 packages/core/src/mcp/handlers/search.rs:140-153 Parameter parsing is clean and well-documented:
Engineering Principle: Using 🟢 Suggestion: Property Filter Implementation Detail📁 packages/core/src/mcp/handlers/search.rs:398-410 The property filter matching iterates through filter entries and compares JSON values directly: for (key, expected_value) in filter_obj {
match node.properties.get(key) {
Some(actual_value) => {
if actual_value != expected_value {
return false; // AND logic
}
}
None => {
return false; // Missing property fails the filter
}
}
}This is correct for equality matching. Consider documenting in the schema/code that:
Current: Already well-logged with ✅ Test Coverage: Comprehensive Unit Tests📁 packages/core/src/mcp/handlers/search.rs:888-1086 11 well-structured unit tests added:
Tests are isolated, clear, and don't require full service infrastructure. Good unit test hygiene. Engineering Principle: Unit tests verify the filter logic without mocking the full semantic search flow, reducing brittleness. ✅ Response Structure: Filter Echo📁 packages/core/src/mcp/handlers/search.rs:491-494 Filters are echoed back in the response JSON: {
"nodes": [...],
"count": 0,
"query": "test",
"node_types": ["task"],
"property_filters": { "status": "done" }
}Good for clients to verify which filters were applied (especially useful for debugging). ✅ JSON Schema: Well-Documented📁 packages/core/src/mcp/handlers/tools.rs:1004-1013 Schema updates include:
Example from schema: "property_filters": {
"type": "object",
"description": "Filter by node properties using key-value pairs. Only nodes whose properties contain all specified key-value pairs (AND logic) will be included. Example: {'status': 'done', 'priority': 'high'}"
}Excellent clarity for API users. 🟢 Suggestion: Database-Level Filtering📁 packages/core/src/mcp/handlers/search.rs:315-318 The code fetches 3x results to compensate for post-filtering: let has_post_filters = collection_member_ids.is_some()
|| !excluded_node_ids.is_empty()
|| !include_archived
|| scope_filters
|| params.node_types.is_some()
|| params.property_filters.is_some();
let effective_limit = if has_post_filters { limit * 3 } else { limit };This is pragmatic but could be optimized. The issue originally proposed pushing filters into the SurrealDB query (WHERE clause). Current implementation:
For large datasets with selective filters, this could waste capacity. However, this is acceptable as a v1 implementation since:
Recommendation for future: Consider database-level filtering optimization in a follow-up issue. Summary
RecommendationREQUEST CHANGES — The implementation is technically solid and works well for the MCP handler layer, but there's a scope mismatch with the acceptance criteria. Required Action:
Proceed with merge only after scope is clarified and aligned. |
…roactive review #1064) The original PR #1064 (closes #1059) implemented node_types and property_filters filtering only at the MCP handler layer. The acceptance criteria required the service-layer signature to be extended so any caller benefits from the filtering without duplicating logic. Changes: - Add SearchNodeFilters struct to services/mod.rs with matches() helper and comprehensive unit tests (13 tests covering all filter combinations) - Extend NodeEmbeddingService::semantic_search_nodes() to accept Option<&SearchNodeFilters> — when filters are active, fetch 3x results to compensate for post-filter attrition, then apply and truncate - Update handle_search_semantic() MCP handler to build SearchNodeFilters from params and pass it to the service instead of filtering post-hoc; remove the duplicate filter blocks from the handler's post-filter pass - Update search_ops.rs caller to pass None (no behavior change for that path) Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
…oactive #1059) The skill pipeline still used semantic_search() with limit*3 over-fetch and manual post-filtering by node_type == "skill". This is exactly the acceptance criterion that was not addressed by PR #1064. Use semantic_search_nodes() with SearchNodeFilters{node_types: Some(["skill"])} so filtering is applied at the service layer — no more over-fetching. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
…oactive #1064) * Move semantic search node_type/property filters to service layer (retroactive review #1064) The original PR #1064 (closes #1059) implemented node_types and property_filters filtering only at the MCP handler layer. The acceptance criteria required the service-layer signature to be extended so any caller benefits from the filtering without duplicating logic. Changes: - Add SearchNodeFilters struct to services/mod.rs with matches() helper and comprehensive unit tests (13 tests covering all filter combinations) - Extend NodeEmbeddingService::semantic_search_nodes() to accept Option<&SearchNodeFilters> — when filters are active, fetch 3x results to compensate for post-filter attrition, then apply and truncate - Update handle_search_semantic() MCP handler to build SearchNodeFilters from params and pass it to the service instead of filtering post-hoc; remove the duplicate filter blocks from the handler's post-filter pass - Update search_ops.rs caller to pass None (no behavior change for that path) Co-Authored-By: Claude Sonnet 4.6 <[email protected]> * Fix retroactive review findings for PRs #1063, #1065, #1069 ## PR #1063/#1065 findings — Metal crash fix (merged without review) ### tools.rs: Incomplete get_tool_schemas refactor (compile error) PR #1065 changed callers to pass Vec<SchemaNode> but never updated the get_tool_schemas function signature (still &[String]) or defined tool_desc and node_type_desc variables referenced in the json! macro. Fix: Update signature to &[SchemaNode], define tool_desc and node_type_desc dynamically from schema data. When user-defined schemas are present, their IDs and descriptions appear in the create_node tool description. ### context_assembly.rs: Missing filters arg (compile error) semantic_search_nodes gained a 4th filters: Option<&SearchNodeFilters> parameter (from e3a28fa). context_assembly.rs was missed. Fix: Pass None (no filter needed for general context assembly queries). ### tools_test.rs: Tests still use Vec<String> Two unit tests for get_tool_schemas still passed Vec<String> after the signature change. Fix: Construct minimal SchemaNode instances. ## PR #1069 finding — Per-turn context refresh (merged without review) ### initialize.rs: Dead function build_workspace_context_for_prompt build_workspace_context_for_prompt was added but is never called. The actual code uses build_workspace_context directly from context_ops. Fix: Remove the unused public function entirely. ## Non-functional changes - ollama_inference.rs: rustfmt alignment only - agent_e2e.rs: rustfmt alignment only Co-Authored-By: Claude Sonnet 4.6 <[email protected]> * Fix skill_ops.rs: use SearchNodeFilters service-layer filtering (retroactive #1059) The skill pipeline still used semantic_search() with limit*3 over-fetch and manual post-filtering by node_type == "skill". This is exactly the acceptance criterion that was not addressed by PR #1064. Use semantic_search_nodes() with SearchNodeFilters{node_types: Some(["skill"])} so filtering is applied at the service layer — no more over-fetching. Co-Authored-By: Claude Sonnet 4.6 <[email protected]> * Fix clippy: use array instead of vec! in test_node_types_filter_matching_logic Clippy -D warnings flags vec!["a", "b"] in a context where an array literal suffices. Switch to array to satisfy the lint. Co-Authored-By: Claude Sonnet 4.6 <[email protected]> * Address review findings: commit skill_ops indentation fix, document over-fetch compounding and DB filter TODO - Commit the unstaged skill_ops.rs indentation cleanup (loop body was indented one extra level after the if-let wrapper was removed in commit 6b2c7c4) - Add comments to embedding_service.rs documenting: - The 9× over-fetch compounding that occurs when both service-layer filters (node_types) and handler-layer filters (collection/scope) are active - A TODO for future DB-level node_type filtering (requires embedding table schema change to add node_type column to vector index WHERE clause) Co-Authored-By: Claude Sonnet 4.6 <[email protected]> --------- Co-authored-by: Michael Libio <[email protected]> Co-authored-by: Claude Sonnet 4.6 <[email protected]>
|
Retroactive review completed. Service-layer filter fix merged in PR #1073. All review findings were addressed before merge. |
…1059) (#1064) Extends SearchSemanticParams with two new optional filter parameters: - node_types: array of node types to filter by (AND logic with other filters) - property_filters: object with key-value pairs to match against node properties (AND logic) Updates the post-filter predicate in handle_search_semantic to: 1. Filter by node_types if specified (node.node_type must be in the list) 2. Filter by property_filters if specified (all key-value pairs must match) Logs debug messages when filtering out nodes due to these criteria for troubleshooting. Includes 11 unit tests covering: - Parameter parsing for both new fields - Empty/null handling - Property matching logic (partial match fails, AND logic) - Combined filters with other search parameters - Response structure includes applied filters Updates search_semantic JSON schema in tools.rs to document the new parameters with examples. Co-authored-by: Michael Libio <[email protected]> Co-authored-by: Claude Sonnet 4.6 (1M context) <[email protected]>
…oactive #1064) * Move semantic search node_type/property filters to service layer (retroactive review #1064) The original PR #1064 (closes #1059) implemented node_types and property_filters filtering only at the MCP handler layer. The acceptance criteria required the service-layer signature to be extended so any caller benefits from the filtering without duplicating logic. Changes: - Add SearchNodeFilters struct to services/mod.rs with matches() helper and comprehensive unit tests (13 tests covering all filter combinations) - Extend NodeEmbeddingService::semantic_search_nodes() to accept Option<&SearchNodeFilters> — when filters are active, fetch 3x results to compensate for post-filter attrition, then apply and truncate - Update handle_search_semantic() MCP handler to build SearchNodeFilters from params and pass it to the service instead of filtering post-hoc; remove the duplicate filter blocks from the handler's post-filter pass - Update search_ops.rs caller to pass None (no behavior change for that path) Co-Authored-By: Claude Sonnet 4.6 <[email protected]> * Fix retroactive review findings for PRs #1063, #1065, #1069 ## PR #1063/#1065 findings — Metal crash fix (merged without review) ### tools.rs: Incomplete get_tool_schemas refactor (compile error) PR #1065 changed callers to pass Vec<SchemaNode> but never updated the get_tool_schemas function signature (still &[String]) or defined tool_desc and node_type_desc variables referenced in the json! macro. Fix: Update signature to &[SchemaNode], define tool_desc and node_type_desc dynamically from schema data. When user-defined schemas are present, their IDs and descriptions appear in the create_node tool description. ### context_assembly.rs: Missing filters arg (compile error) semantic_search_nodes gained a 4th filters: Option<&SearchNodeFilters> parameter (from 14ead12b). context_assembly.rs was missed. Fix: Pass None (no filter needed for general context assembly queries). ### tools_test.rs: Tests still use Vec<String> Two unit tests for get_tool_schemas still passed Vec<String> after the signature change. Fix: Construct minimal SchemaNode instances. ## PR #1069 finding — Per-turn context refresh (merged without review) ### initialize.rs: Dead function build_workspace_context_for_prompt build_workspace_context_for_prompt was added but is never called. The actual code uses build_workspace_context directly from context_ops. Fix: Remove the unused public function entirely. ## Non-functional changes - ollama_inference.rs: rustfmt alignment only - agent_e2e.rs: rustfmt alignment only Co-Authored-By: Claude Sonnet 4.6 <[email protected]> * Fix skill_ops.rs: use SearchNodeFilters service-layer filtering (retroactive #1059) The skill pipeline still used semantic_search() with limit*3 over-fetch and manual post-filtering by node_type == "skill". This is exactly the acceptance criterion that was not addressed by PR #1064. Use semantic_search_nodes() with SearchNodeFilters{node_types: Some(["skill"])} so filtering is applied at the service layer — no more over-fetching. Co-Authored-By: Claude Sonnet 4.6 <[email protected]> * Fix clippy: use array instead of vec! in test_node_types_filter_matching_logic Clippy -D warnings flags vec!["a", "b"] in a context where an array literal suffices. Switch to array to satisfy the lint. Co-Authored-By: Claude Sonnet 4.6 <[email protected]> * Address review findings: commit skill_ops indentation fix, document over-fetch compounding and DB filter TODO - Commit the unstaged skill_ops.rs indentation cleanup (loop body was indented one extra level after the if-let wrapper was removed in commit cd7298f8) - Add comments to embedding_service.rs documenting: - The 9× over-fetch compounding that occurs when both service-layer filters (node_types) and handler-layer filters (collection/scope) are active - A TODO for future DB-level node_type filtering (requires embedding table schema change to add node_type column to vector index WHERE clause) Co-Authored-By: Claude Sonnet 4.6 <[email protected]> --------- Co-authored-by: Michael Libio <[email protected]> Co-authored-by: Claude Sonnet 4.6 <[email protected]>
Closes #1059