Name the database of a result by what the operation covers

`_rich_print_search_result` asked how many databases were configured, so
`--db-name alpha` labelled every line with the database the caller had just
named. Citations already ask the client what it covers, which is the same
question the scope answers.
This commit is contained in:
Yiorgis Gozadinos 2026-08-26 16:49:01 +03:00
parent b76c5c7db0
commit 1995a7da36
No known key found for this signature in database
3 changed files with 35 additions and 5 deletions

View file

@ -891,7 +891,7 @@ class HaikuRAGApp:
f"[repr.attrib_name]chunk_id[/repr.attrib_name]: {result.chunk_id} "
f"[repr.attrib_name]score[/repr.attrib_name]: {result.score:.4f}"
)
if result.source and len(self.config.lancedb.databases) > 1:
if result.source and self.scope.covers_multiple:
self.console.print(
f"[repr.attrib_name]database[/repr.attrib_name]: {result.source}"
)

View file

@ -11,6 +11,7 @@ from rich.console import Console
from haiku.rag.app import HaikuRAGApp
from haiku.rag.client import RebuildMode
from haiku.rag.client.scope import DatabaseScope
from haiku.rag.config.models import (
AppConfig,
LanceDBConfig,
@ -200,6 +201,34 @@ async def test_search_prints_results(app, client):
assert "hit one" in out(app)
async def test_a_result_names_its_database_only_across_several(tmp_path):
"""The label answers what this operation covers, not what is configured:
after narrowing to one, the caller has already named it."""
config = AppConfig(
lancedb=LanceDBConfig(
databases={
"alpha": str(tmp_path / "a.lancedb"),
"beta": str(tmp_path / "b.lancedb"),
}
)
)
hit = SearchResult(content="hit", score=0.9, chunk_id="c1", source="alpha")
covering = HaikuRAGApp(scope=DatabaseScope.resolve(config), config=config)
covering.console = Console(record=True, width=200)
covering._rich_print_search_result(hit)
assert "database: alpha" in covering.console.export_text()
narrowed = HaikuRAGApp(
scope=DatabaseScope.resolve(config, database_name="alpha"), config=config
)
narrowed.console = Console(record=True, width=200)
narrowed._rich_print_search_result(hit)
printed = narrowed.console.export_text()
assert "hit" in printed
assert "database:" not in printed
async def test_search_by_image_reads_the_bytes(app, client, tmp_path):
image = tmp_path / "query.png"
image.write_bytes(b"pixels")

View file

@ -749,12 +749,13 @@ class TestRenderingTheDatabase:
@staticmethod
def _app(tmp_path, **databases):
"""An app over the configured set. The scope is what decides the label,
so it has to be the one the configuration resolves to."""
from haiku.rag.app import HaikuRAGApp
from haiku.rag.client.scope import DatabaseScope
return HaikuRAGApp(
scope=for_path(tmp_path / "unused"),
config=AppConfig(lancedb=LanceDBConfig(databases=databases)),
)
config = AppConfig(lancedb=LanceDBConfig(databases=databases))
return HaikuRAGApp(scope=DatabaseScope.resolve(config), config=config)
@staticmethod
def _rendered(app, result) -> str: