Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/vouch/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io
import json
import os
import sqlite3
import sys
from collections.abc import Iterator
from contextlib import contextmanager, suppress
Expand Down Expand Up @@ -2312,14 +2313,20 @@ def search(
)
used = "embedding" if hits else used
if not hits and backend in ("auto", "fts5"):
hits = index_db.search(store.kb_dir, q, limit=fetch_limit)
used = "fts5" if hits else used
try:
hits = index_db.search(store.kb_dir, q, limit=fetch_limit)
used = "fts5" if hits else used
except sqlite3.Error:
hits = []
if not hits and backend in ("auto", "substring"):
hits = store.search_substring(q, limit=fetch_limit)
used = "substring"
if backend == "hybrid":
emb = index_db.search_semantic(store.kb_dir, q, limit=fetch_limit * 2)
fts = index_db.search(store.kb_dir, q, limit=fetch_limit * 2)
try:
fts = index_db.search(store.kb_dir, q, limit=fetch_limit * 2)
except sqlite3.Error:
fts = []
hits = rrf_fuse(emb, fts, limit=fetch_limit)
used = "hybrid"

Expand Down
54 changes: 54 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import json
import os
import sqlite3
import subprocess
import sys
from pathlib import Path
Expand Down Expand Up @@ -294,6 +295,59 @@ def test_search_substring_backend_label(store: KBStore, monkeypatch: pytest.Monk
assert "(substring)" in result.output


def test_search_auto_falls_back_when_fts_raises(
store: KBStore,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Auto search should mirror context/server resilience on FTS errors."""
from vouch import index_db
from vouch.proposals import approve as do_approve
from vouch.proposals import propose_claim

src = store.put_source(b"e")
pr = propose_claim(store, text="findable token", evidence=[src.id], proposed_by="agent")
do_approve(store, pr.id, approved_by="reviewer")
monkeypatch.setattr(
index_db,
"search",
lambda *a, **k: (_ for _ in ()).throw(sqlite3.Error("fts unavailable")),
)
monkeypatch.setattr(index_db, "search_semantic", lambda *a, **k: [])

result = CliRunner().invoke(cli, ["search", "findable"])

assert result.exit_code == 0, result.output
assert "(substring)" in result.output


def test_search_hybrid_keeps_semantic_hits_when_fts_raises(
store: KBStore,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Hybrid search should degrade to semantic-only when FTS5 is unavailable."""
from vouch import index_db
from vouch.models import Claim

src = store.put_source(b"e")
store.put_claim(Claim(id="c1", text="semantic token", evidence=[src.id]))
monkeypatch.setattr(
index_db,
"search_semantic",
lambda *a, **k: [("claim", "c1", "semantic token", 0.9)],
)
monkeypatch.setattr(
index_db,
"search",
lambda *a, **k: (_ for _ in ()).throw(sqlite3.Error("fts unavailable")),
)

result = CliRunner().invoke(cli, ["search", "semantic", "--backend", "hybrid"])

assert result.exit_code == 0, result.output
assert "claim/c1" in result.output
assert "(hybrid)" in result.output


def test_crystallize_cli_all_failures_exits_1(store: KBStore) -> None:
with patch.object(KBStore, "_embed_and_store"):
src = store.put_source(b"e")
Expand Down
Loading