Source Grounding Phase 1 — Implemented
Changes
packages/semantic-search/src/index.ts
Expanded STOP_WORDS (~40 code tokens added): null, undefined, return, const, function, true, false, value, result, error, check, data, type, interface, object, string, number, boolean, array, and ~20 more. These tokens appear in virtually every TypeScript file and were the primary cause of false-positive citations (a sentence mentioning "null" would match every file).
Added DOMAIN_TOKENS + 3× boosting: Trading-specific terms (rsi, ema, macd, drawdown, backtest, candle, oversold, etc.) count three times in the weighted overlap score. Sentences about RSI now strongly prefer RSI-related sources.
Raised threshold 0.18 → 0.35: Requires 35% weighted token overlap (up from 18%). Also requires ≥ 2 meaningful token matches — a single common token can no longer attach a citation.
Min-sentence guard: Sentences shorter than 40 characters or with fewer than 4 meaningful tokens (headings, status lines) are skipped entirely.
Source-type affinity: Strategy sources gain +0.08 for sentences containing domain terms; tool_result sources gain +0.06 for sentences containing metric numbers (e.g. 42.3%, 1.0842).
services/backend/src/routes/bots/prompts/core.ts
Added TOOL RESULT FAITHFULNESS system instruction:
- Quote exact numbers verbatim — no rounding or estimation
- Null fields → "not available", never substitute a plausible number
- Empty results → state explicitly, never invent examples
- Backtest metrics reproduced verbatim
- Tool errors reported as exact messages
Expected improvement
Phase 1 target: Precision@1 > 60% (up from estimated 25–35% baseline).