Skip to content

feat: add evidence-bearing search hits - #360

Merged
Sinity merged 1 commit into
masterfrom
feature/feat/evidence-bearing-search-hits
Apr 22, 2026
Merged

Sinity merged 1 commit into
masterfrom
feature/feat/evidence-bearing-search-hits

Conversation

@Sinity

@Sinity Sinity commented Apr 22, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • Add a shared evidence-bearing ConversationSearchHit contract for CLI, MCP, and archive operations.
  • Preserve ranked message FTS evidence for simple search/list result sets, including rank, score, retrieval lane, match surface, message id, and snippet.
  • Return the new evidence-bearing hit payload from MCP search while leaving list_conversations on compact summaries.

Problem

Closes #216.

Search and discovery surfaces returned conversation identities without explaining why each result matched. That made downstream retrieval work hard to verify, because CLI and MCP consumers could not inspect the matched message or distinguish the retrieval lane/surface that produced a hit.

Solution

  • Added polylogue/lib/search_hits.py and polylogue/lib/query_search_hits.py as the shared search-hit model and plan execution adapter.
  • Added ConversationSearchEvidenceHit and search_conversation_evidence_hits() in the storage query layer so FTS-ranked message searches keep their witness metadata before summary hydration.
  • Added repository and archive-operation methods for evidence-bearing summary hits.
  • Added ConversationSearchHitPayload and MCP list/no-result payloads for search results.
  • Updated CLI list/search output paths to render evidence-bearing hits for search-like result lists, including JSON/YAML/CSV/plain/rich table output.
  • Documented the evidence fields in README, library API, and MCP integration docs.
  • Added storage, CLI formatting/routing, and MCP tool contract coverage.

Verification

ruff check polylogue/lib/search_hits.py polylogue/lib/query_search_hits.py polylogue/cli/query.py polylogue/cli/query_output.py polylogue/mcp/payloads.py polylogue/mcp/server_tools.py polylogue/operations/archive.py polylogue/protocols.py polylogue/storage/backends/queries/conversations_search.py polylogue/storage/backends/query_store_archive.py polylogue/storage/repository_archive_search.py polylogue/storage/search_models.py polylogue/surface_payloads.py tests/unit/storage/test_archive_search_contracts.py tests/unit/cli/test_query_fmt.py tests/unit/cli/test_query_exec_laws.py tests/unit/mcp/test_tool_contracts.py
pytest -q tests/unit/storage/test_archive_search_contracts.py tests/unit/cli/test_query_fmt.py::TestListFormatting::test_format_search_hit_list_contract tests/unit/cli/test_query_exec_laws.py::test_async_execute_query_summary_list_uses_evidence_hits_for_search_contract tests/unit/mcp/test_tool_contracts.py::TestQueryTools
pytest -q tests/unit/cli/test_query_fmt.py tests/unit/cli/test_query_exec_laws.py tests/unit/mcp/test_tool_contracts.py tests/unit/storage/test_archive_search_contracts.py
mypy polylogue/
devtools render-all --check
pytest -q tests/unit/ui/test_tui.py::test_search_empty_db
pytest -q tests/unit/ui/test_tui.py
devtools verify

Pre-push hook also ran devtools verify --quick successfully during branch push.

Summary by CodeRabbit

Release Notes

  • Documentation

    • Updated documentation to clarify that search results now include match evidence: rank, retrieval lane, matched surface, message ID, and snippet.
  • New Features

    • Enhanced search functionality to display detailed match evidence in results, including ranking information and snippet context.
  • Tests

    • Added test coverage for search result formatting and evidence hit retrieval.

@coderabbitai

coderabbitai Bot commented Apr 22, 2026 •

Copy link
Copy Markdown

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

This pull request implements evidence-bearing search hits across storage, repository, CLI, MCP, and library layers. It adds data models for search hits with match evidence (rank, lane, surface, message ID, snippet), integrates ranked FTS search with snippet generation into the query path, and exposes these enriched results through CLI formatting and MCP payloads. Documentation is updated to reflect the new result contracts.

Changes

Cohort / File(s) Summary
Documentation
README.md, docs/library-api.md, docs/mcp-integration.md
Clarified that search results include match evidence fields (rank, lane, surface, message ID, snippet) in addition to conversation identity.
Data Models & Payloads
polylogue/lib/search_hits.py, polylogue/storage/search_models.py, polylogue/surface_payloads.py
Added immutable ConversationSearchHit and ConversationSearchEvidenceHit dataclasses; introduced ConversationSearchMatchPayload and ConversationSearchHitPayload for serialization; added utilities for snippet generation and query normalization.
Storage Layer
polylogue/storage/backends/queries/conversations_search.py, polylogue/storage/backends/queries/conversations.py, polylogue/storage/backends/query_store_archive.py, polylogue/storage/repository_archive_search.py
Implemented search_conversation_evidence_hits() for ranked FTS search with snippets; added search_summary_hits() in repository mixin to hydrate evidence hits with conversation summaries and message counts.
CLI Query Execution
polylogue/cli/query.py
Modified async_execute_query_request to detect and prioritize search evidence in plans; routes SUMMARY_LIST queries through evidence-hit path when available; added helper _search_hits_for_execution_plan().
CLI Output Formatting
polylogue/cli/query_output.py
Added search-hit formatting functions: format_search_hit_list(), output_search_hits(); supports multiple output formats (JSON, YAML, CSV, text) with machine/neutral rendering; includes rich-table rendering for interactive output with metadata (ID, Date, Provider, Title, Msgs) and assembled Match cells.
MCP Integration
polylogue/mcp/payloads.py, polylogue/mcp/server_tools.py
Added MCPConversationSearchHitListPayload, MCPConversationSearchNoResultsPayload, and helper functions conversation_search_hit_list_payload(), conversation_search_result_payload(); updated search tool to use search_conversation_hits() and return evidence-bearing payloads.
Library & Operations
polylogue/lib/query_search_hits.py, polylogue/operations/archive.py, polylogue/protocols.py
Added query_search_hits.py module with plan_has_search_hit_evidence() and search_hits_for_plan() for plan-based hit execution; extended ArchiveSearchMixin with search_conversation_hits(); added search_summary_hits() to SearchStore protocol.
Tests
tests/unit/cli/test_query_exec_laws.py, tests/unit/cli/test_query_fmt.py, tests/unit/mcp/test_tool_contracts.py, tests/unit/storage/test_archive_search_contracts.py
Added contract tests for evidence-hit query execution, search-hit formatting across output formats, MCP search-tool integration, and repository-level evidence-hit hydration.

Sequence Diagram(s)

sequenceDiagram
    participant CLI as CLI Query<br/>Executor
    participant Plan as Query Plan<br/>Analyzer
    participant Storage as Search<br/>Storage
    participant Repo as Repository<br/>Hydration
    participant Output as Output<br/>Formatter

    CLI->>Plan: Check for search evidence<br/>(fts_terms or similar_text)
    Plan-->>CLI: Evidence present?
    
    alt Evidence Found
        CLI->>Storage: search_hints_for_plan()<br/>(with query terms)
        Storage->>Storage: Ranked FTS search<br/>+ snippet generation
        Storage-->>CLI: list[ConversationSearchHit]
        
        CLI->>Repo: get_message_counts_batch()<br/>(for context enrichment)
        Repo-->>CLI: message counts dict
        
        CLI->>Output: output_search_hits()<br/>(hits + format spec)
        Output->>Output: Format per output_format<br/>(JSON/YAML/CSV/text)
        Output-->>CLI: Rendered results
        
        CLI-->>User: Search results with evidence
    else No Evidence
        CLI->>CLI: Fall back to<br/>summary list path
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related issues

  • feat: expose evidence-bearing search hits in CLI and MCP #216: This PR directly implements the acceptance criteria for "feat: expose evidence-bearing search hits in CLI and MCP"—all requirements (first-class search-hit contract, CLI excerpt display, MCP snippet exposure, message identification, and comprehensive test coverage) are fulfilled by these changes.

Possibly related PRs

Suggested labels

area:cli, area:storage

Poem

🐰 With evidence in every search, no longer must we wonder—
Each snippet and each message ID breaks confusion asunder!
From storage's ranked results to your screen they leap,
Ranked hits with their match-proof, making searches complete. ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 4.35% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The PR title 'feat: add evidence-bearing search hits' is clear, specific, and accurately summarizes the main feature being added.
Description check ✅ Passed The PR description includes all required template sections: Summary, Problem, Solution, and Verification with detailed implementation steps and test commands.
Linked Issues check ✅ Passed The PR fully addresses linked issue #216 by implementing evidence-bearing search hits with snippets, match provenance, retrieval lanes, and message IDs across CLI, MCP, storage, and archive layers.
Out of Scope Changes check ✅ Passed All changes are directly scoped to the objectives: documentation updates, new search-hit models/contracts, storage layer additions, CLI output formatting, and MCP payload integration with supporting tests.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/feat/evidence-bearing-search-hits

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@Sinity
Sinity merged commit eba5108 into master Apr 22, 2026
10 of 14 checks passed
@Sinity
Sinity deleted the feature/feat/evidence-bearing-search-hits branch April 22, 2026 16:46

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: bc2744ca34

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread polylogue/cli/query.py
Comment on lines +588 to +592
if output_diagnostics is None and (plan.output.list_mode or len(projected_results) > 1):
search_hits = await _search_hits_for_execution_plan(repo, plan, vector_provider=vector_provider)
if search_hits is not None:
await _query_output.output_search_hits(env, search_hits, plan.output, repo)
return

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Route multi-hit search output through standard output dispatch

This new branch returns early via output_search_hits(...) for search-like queries with multiple results, which bypasses the normal output_results(...) path. In practice that drops delivery behaviors handled by _send_output (e.g. --output file/browser/clipboard) and skips the already-projected result set, so users can get stdout-only output and unprojected list data for the same query shape.

Useful? React with 👍 / 👎.

Comment on lines +421 to +422
if selected:
data = [{key: value for key, value in item.items() if key in selected} for item in data]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve --fields semantics for search-hit machine output

Field filtering is applied to the top-level search-hit payload object, but those objects only expose conversation and match; the documented list fields like id, title, and provider now get filtered out entirely. For JSON/YAML search-hit output, requests such as --fields id,title can therefore degrade to empty objects instead of narrowed rows.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: expose evidence-bearing search hits in CLI and MCP

1 participant