Repository navigation
feat(search): explainable results with matched terms, rank-first default, lane scores (#873) - #932
Conversation
Add explanation fields (matched_terms, score_components) to ConversationSearchHit and wire them through FTS5, hybrid, and action search paths. Fix default sort to be rank-first when search terms are present — previously sort=None was silently replaced with sort="date", destroying search provider rankings. Preserve per-lane rank contributions (text, action, vector) in hybrid RRF search so callers can see WHY a result matched. Add next_cursor to paginated search result envelopes for stable keyset pagination of ranked results. Ref #873 Co-Authored-By: Claude Opus 4.7 <[email protected]>
|
Warning Rate limit exceeded
To continue reviewing without waiting, purchase usage credits in the billing tab. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (13)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
This PR extends Polylogue’s search stack to return explainable ranked results (matched terms + score component breakdowns), adjusts default sorting to preserve provider ranking when search terms are present (“rank-first”), and enriches hybrid retrieval with per-lane rank contribution metadata.
Changes:
- Add
matched_termsandscore_componentsto search-hit payloads/models and propagate them through repository/surface adapters. - Preserve search-provider ordering by default (skip date-based sorting) when FTS terms exist and no explicit
sortis provided. - For hybrid retrieval, return per-conversation lane rank contributions and surface them as score components; add an MCP
next_cursorfield (cursor generation scaffolding).
Reviewed changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/unit/archive/test_query_search_runtime.py | Updates hybrid search test to assert returned per-lane rank contributions. |
| polylogue/surfaces/payloads.py | Adds matched_terms + score_components to surface search-match payloads. |
| polylogue/storage/search/models.py | Extends evidence-hit model with matched terms and scoring/ranking metadata fields. |
| polylogue/storage/repository/archive/search.py | Propagates matched terms / score components from evidence hits into search-hit construction. |
| polylogue/mcp/payloads.py | Adds next_cursor to paginated search payload and implements cursor construction. |
| polylogue/daemon/http.py | Changes daemon list behavior to return ranked “hits” envelope for query/contains searches (with match evidence). |
| polylogue/archive/query/spec.py | Removes implicit default sort, threads cursor into query plan. |
| polylogue/archive/query/sorting.py | Updates sort-plan protocol typing to allow sort=None. |
| polylogue/archive/query/search_hits.py | Adds evidence fields to ConversationSearchHit and special-cases hybrid to attach lane rank components. |
| polylogue/archive/query/retrieval_search.py | Changes hybrid search to return (conversations, lane_ranks) and computes per-lane rank maps. |
| polylogue/archive/query/retrieval_candidates.py | Adapts hybrid candidate fetching to the new return type. |
| polylogue/archive/query/plan.py | Makes sort optional; adds cursor + ranking policy fields to the plan. |
| polylogue/archive/query/plan_execution.py | Implements “rank-first default” by skipping sorting when FTS terms exist and sort is unset. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| since=parse_query_date("since", spec.since), | ||
| until=parse_query_date("until", spec.until), | ||
| sort=spec.sort or "date", | ||
| sort=spec.sort, |
| since_session_id=spec.since_session_id, | ||
| message_type=spec.message_type, | ||
| offset=spec.offset, | ||
| cursor=spec.cursor, |
| conversation_id: str | None = None | ||
| parent_id: str | None = None | ||
| since: datetime | None = None | ||
| until: datetime | None = None | ||
| sort: SortField = "date" | ||
| sort: SortField | None = None | ||
| reverse: bool = False | ||
| limit: int | None = None |
| next_offset = offset + len(hits) if len(hits) == limit and offset + limit < total else None | ||
| next_cursor = _build_search_cursor(hits) if len(hits) == limit else None |
| def _build_search_cursor(hits: Sequence[ConversationSearchHit]) -> str | None: | ||
| """Build an opaque cursor from the last hit for keyset pagination.""" | ||
| if not hits: | ||
| return None | ||
| import base64 | ||
|
|
||
| last = hits[-1] | ||
| payload = f"{last.rank}:{last.conversation_id}" | ||
| return base64.b64encode(payload.encode()).decode() |
| # When search terms are present, return ranked result envelope with | ||
| # per-hit match evidence instead of plain row dicts. | ||
| if query_text and not query_params.get("similar_text"): | ||
| return await self._do_search_list(poly, spec, limit, offset) |
| plan = spec.to_plan() | ||
| hits = await search_hits_for_plan(plan, poly.repository) | ||
| total = await spec.count(poly.repository) |
| with contextlib.suppress(Exception): | ||
| try: | ||
| raw_diag = await poly.operations.diagnose_query_miss(spec) | ||
| diagnostics = MCPQueryMissDiagnosticsPayload.from_diagnostics(raw_diag) | ||
| except Exception: | ||
| pass |
Polylogue had FTS hits, snippets, and hybrid retrieval, but no
stable shared contract for ranking explainability or pagination
across CLI, MCP, daemon HTTP, and reader. Hybrid RRF lane
contributions were also discarded before they reached the
surfaces, and default date sorting could silently override ranked
search order on non-hit paths.
- `archive/query/{plan,plan_execution,retrieval_candidates,
retrieval_search,search_hits,sorting,spec}.py`: ranked result
envelope with `matched_terms` and per-lane `score_components`
so consumers can show why each hit matched and which lane
contributed.
- Rank-first sort default on search paths; date sort only when
the surface explicitly asks for it.
- `polylogue/daemon/http.py`: daemon API surfaces the same
envelope so reader and external clients see one ranking
contract.
Refs #873
Polylogue had FTS hits, snippets, and hybrid retrieval, but no
stable shared contract for ranking explainability or pagination
across CLI, MCP, daemon HTTP, and reader. Hybrid RRF lane
contributions were also discarded before they reached the
surfaces, and default date sorting could silently override ranked
search order on non-hit paths.
- `archive/query/{plan,plan_execution,retrieval_candidates,
retrieval_search,search_hits,sorting,spec}.py`: ranked result
envelope with `matched_terms` and per-lane `score_components`
so consumers can show why each hit matched and which lane
contributed.
- Rank-first sort default on search paths; date sort only when
the surface explicitly asks for it.
- `polylogue/daemon/http.py`: daemon API surfaces the same
envelope so reader and external clients see one ranking
contract.
Refs #873
Explainable search with matched_terms, score_components, rank-first sort default. 13 files +194/-11.