- Add defense-in-depth validation for memory IDs before SQL interpolation in stage2, stage3, and stage4 IN clauses (validate alphanumeric/hyphen/underscore only) - Make scoring weights (name, description, corpus) configurable via RetrievalConfig instead of compile-time constants, with defaults 0.3/0.3/0.4 Closes #118 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2.3 KiB
Issue #118: Tech debt: minor findings from issue #30 review
Summary
Address two tech debt items from the staged retrieval pipeline review:
-
Validate memory IDs before SQL interpolation - Add defense-in-depth validation that memory IDs contain only safe characters (alphanumeric, hyphens) before string-interpolating them into SQL IN clauses in stage2, stage3, and stage4.
-
Make scoring weights configurable - Move the compile-time constants
NAME_WEIGHT,DESCRIPTION_WEIGHT, andCORPUS_WEIGHTfrom stage3.rs intoRetrievalConfig, making them tunable without recompilation.
Item 1: Validate memory IDs before SQL interpolation
Approach
Since the IDs originate from database query results (not user input) and are UUID-like strings, full parameterization via temp tables adds complexity without proportionate security benefit. Instead, add a validation function that asserts all IDs contain only safe characters ([a-zA-Z0-9-]) before interpolation, returning an error if any ID fails validation.
Files changed
services/memory/src/retrieval/mod.rs- Addvalidate_memory_ids()helper functionservices/memory/src/retrieval/stage2.rs- Call validation before building IN clauseservices/memory/src/retrieval/stage3.rs- Call validation before building IN clauseservices/memory/src/retrieval/stage4.rs- Call validation before building IN clause
Item 2: Make scoring weights configurable
Approach
Add three new fields to RetrievalConfig: name_weight, description_weight, corpus_weight with serde defaults matching the current constants (0.3, 0.3, 0.4). Thread these through RetrievalParams and into stage3::load_and_rerank().
Files changed
services/memory/src/config.rs- Add weight fields toRetrievalConfigservices/memory/src/retrieval/mod.rs- Add weight fields toRetrievalParams, updatefrom_config()services/memory/src/retrieval/stage3.rs- Accept weights as parameters instead of using constantsservices/memory/src/retrieval/pipeline.rs- Pass weights from params to stage3
Testing
- Add unit tests for
validate_memory_ids()with valid and invalid inputs - Add unit tests for configurable weights (custom values, TOML deserialization)
- Update existing stage3 tests to pass weight parameters