Skip to content

Make unknown semantic and hybrid search totals explicit in structured responses #1068

Description

@phernandez

Summary

search_notes returns total: 0 for semantic and hybrid queries even when the current page contains results. The implementation intentionally uses zero as an unknown-total sentinel to avoid a second vector retrieval, but the structured response does not tell clients that the value is unknown.

This caused the 2026-07-16 development MCP acceptance run to classify working pagination as a regression until the source contract was inspected.

Reproduction

  1. Create at least two searchable notes.
  2. Call search_notes(search_type="semantic", page=1, page_size=1, min_similarity=0, output_format="json").
  3. Call page 2 with the same query.
  4. Observe distinct results and has_more=true, while both responses report total: 0.

The same behavior occurs for hybrid search.

Investigation

The behavior is currently intentional:

  • src/basic_memory/api/v2/routers/search_router.py skips the exact count for non-FTS retrieval, fetches one probe row, and sets total = 0.
  • src/basic_memory/mcp/tools/search.py documents that vector and hybrid callers should use has_more.
  • Existing CLI tests describe the value as an unknown total.

The problem is therefore the machine-readable contract, not retrieval or pagination.

Suggested fix

Represent unknown totals explicitly. Options include:

  • Change total to int | null and return null for non-exact modes.
  • Add total_is_exact: bool or total_status: "exact" | "unknown" while preserving total: 0 temporarily for compatibility.
  • Update MCP descriptions, API schemas, CLI rendering, and regression tests together.

Prefer an additive compatibility field first if changing total would break existing clients.

Acceptance criteria

  • A structured client can distinguish an exact zero from an unknown total without reading prose documentation.
  • Semantic and hybrid pagination continues to use the probe-row strategy and does not add a second vector retrieval.
  • Text, title, and permalink searches continue to report exact totals.
  • MCP, HTTP API, and CLI tests cover both exact and unknown totals.

Source

Development MCP acceptance run on 2026-07-16. Deployment matched basic-memory revision 796607fdfd0db7a5978d3d07fb71417938ec6bf9.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestneeds investigationIssue needs further investigation and/or refinement

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions