Skip to content
Merged
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
6 changes: 6 additions & 0 deletions docs/TASKS.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Statuses: ✅ done · 🔄 doing · ⬜ remaining · ✂️ cut (stretch only, p
| ID | Branch | Status |
|---|---|---|
| T05 | arch/caliper/t05 | implemented, verification below |
| T09 | arch/caliper/t09 | implemented, verification below |

## Remaining

Expand Down Expand Up @@ -116,3 +117,8 @@ Statuses: ✅ done · 🔄 doing · ⬜ remaining · ✂️ cut (stretch only, p
- **T05 — cache layout + pin semantics:** `<CALIPER_CACHE_DIR>/azure-docs/<40-hex-sha>.tar.gz` plus a per-branch pin pointer `<CALIPER_CACHE_DIR>/azure-docs/main.json` (`branch`, `sha`, `source_ref`, `fetched_at`), written atomically (tmp + `os.replace`). Cache hit = pointer + tarball both present ⇒ zero network calls and the pin stays the first-fetch ref (fulfilling "pinned ref recorded at first ingest"); corrupt pointer raises `SourceError` (loud, never silently refetched); pointer with missing tarball is a repairable miss; a sha-keyed tarball already on disk is reused without redownload. Distinct refs coexist (keyed by sha); `refresh=True` re-resolves + refetches and re-points.
- **T05 — mapping/exclusion decisions:** `page_to_url` normalizes `\` → `/` and accepts nested slugs under the two trees (Learn mirrors the docs path); it rejects only out-of-tree paths, non-`.md` extensions, and empty slugs — `_`/`includes` filtering is `discover_pages`' job (segment-based, so a filename like `includes-guide.md` is kept). `discover_pages` raises `SourceError` when the root or either article tree is missing — that is the A1-failure signal for the `_fetch_via_toc_api` fallback seam, which stays deliberately unimplemented (`NotImplementedError`). The 5 URL-mapping test pages (`functions-timer`, `functions-bindings-timer`, `functions-create-first-function-vscode`, `service-bus-messaging-overview`, `service-bus-queues-topics-subscriptions`) are mapping tests per the T05 spec, not existence tests; the first two are the slugs already used in ARCHITECTURE §8 examples.
- **T05 — extraction contract handed to T07:** the upstream tarball has one top-level `azure-docs-<sha>/` directory (mirrored by the test fixtures); T07 must strip it and pass the inner root — the directory containing `articles/` — to `discover_pages`.
- **T09 — `Passage` carries a 7th field `stage` (§7.5 vs §9.3 reconciliation):** §7.5 lists six Passage fields, but §9.3 requires a per-passage `stage` tag and the `request_retrievals` schema check constrains it (`vector`|`fts`|`rrf`|`rerank`, §8.5). Smallest reconciliation: `stage: str = "vector"` as a defaulted 7th field on the frozen dataclass — the six §7.5 fields are unchanged, and T10 (rrf) / T18 (rerank) override it per passage.
- **T09 — vec0 KNN query lives in `retrieval/__init__.py`, not `db.py`:** no KNN accessor existed and `db.py` is T03-owned (§14.4 single-owner), so `retrieve` runs `SELECT rowid, distance FROM chunks_<fp8>_vec WHERE embedding MATCH ? AND k = ?` directly on the passed conn (verified on the pinned sqlite-vec 0.1.9 / Windows: bound-`k` works, `k` > row count returns all rows, `distance` is the cosine distance ≈ 0 for identical vectors). The fp8-suffix validation is replicated locally (8 lowercase hex, mirroring db's private `_check_suffix` rather than importing the private symbol); the suffix always derives from `config.fp8(cfg)` inside `retrieve`, so the check is defense in depth. `get_chunks` remains the metadata accessor (order-preserving).
- **T09 — "score sentinel handling" resolves to §9.3's fewer-returns:** no sentinel `score` value exists. If the index holds fewer rows than `final_k`, the KNN query returns fewer hits and `retrieve` returns that many passages (ranks still 1..n) — the caller refuses (§9.3, §11.5). A missing (never-ingested) or empty vec0 table likewise returns [] gracefully; distinguishing §9's `index_missing` envelope from a plain refusal is the caller's edge decision, not retrieval's.
- **T09 — public API frozen for T10/T11/T18:** `Passage` (frozen dataclass: `chunk_id`, `page_url`, `heading_path`, `body`, `score`, `rank`, `stage="vector"`), `RetrievalError` (hard failures only: empty question, unimplemented mode, query-embedding dimension ≠ 768 — graceful cases never raise), and `retrieve(question, conn, cfg, *, embeddings=None) -> list[Passage]`: the positional §9.3 signature is unchanged; the keyword-only `embeddings` seam accepts any `EmbeddingBackend` and defaults to the `get_embeddings(cfg)` factory path (T04's session/`keepalive` pattern), which is also how tests inject `FakeEmbeddings` without `patch()` (§15). `score` = cosine distance, **lower = more similar**; ties broken by ascending `chunk_id`; `hybrid_rrf` raises `RetrievalError("not implemented")` until T10 wires `fuse.py`; `vector_top_k` is unused in vector mode (KNN `k = final_k`, §7.5) — it is the hybrid candidate depth for T10. T11 consumes passages sorted by `rank` and must not assume sentinel scores.
- **T09 — verification (2026-11-16):** `uv run pytest -q` — 134 passed when T09 landed alone (122 prior + 12 new retrieval tests: determinism, exactly-`final_k` shape, closest-ranks-first with oracle cosine distances, fewer-rows, empty/missing index → [], typed errors, query-task-type seam); 166 passed on the combined stack with T05; `ruff check` + `ruff format --check` + `mypy` strict clean. Rebuilt onto `main` (post-#8-squash) as `1f2beb0` before PR #7 merge.
175 changes: 175 additions & 0 deletions src/caliper/retrieval/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
"""Retrieval core (§7.5, §9.3): question → final bounded passage set.

T09 implements `mode = "vector"`: cosine KNN top-`cfg.retrieval.final_k` over
the per-fingerprint `chunks_<fp8>_vec` table, using the stable brute-force
sqlite-vec path (`WHERE embedding MATCH ? AND k = ?`) — no ANN indexes
(§7.5). `mode = "hybrid_rrf"` (vector + FTS5 fusion in T10's `fuse.py`) and
cross-encoder reranking (T18) are not wired here and raise until they are.

Contract (§9.3): exactly `final_k` passages sorted by descending relevance —
ascending cosine distance, so LOWER `score` means MORE similar (the schema's
`distance_metric=cosine` yields ≈0 for identical vectors, per T03's tests) —
`rank` 1-based with 1 = best, and `stage = "vector"` on every passage. If the
index holds fewer rows than `final_k`, fewer passages come back and the
caller refuses (§9.3, §11.5); an empty or never-ingested index returns [].
No sentinel scores are ever fabricated.
"""

from __future__ import annotations

import re
import sqlite3
from dataclasses import dataclass

import sqlite_vec # type: ignore[import-untyped]

from caliper.config import CaliperConfig, fp8
from caliper.db import get_chunks
from caliper.providers import EmbeddingBackend, get_embeddings
from caliper.providers.base import EMBEDDING_DIMENSIONS


class RetrievalError(Exception):
"""Retrieval failed hard on its inputs or the index (§16 typed exception).

Raised for caller-contract violations only: an empty question, a
retrieval mode this module does not implement, or a query embedding whose
dimension is not 768. Mapped to the §9 error envelope at the CLI/web
edges (later tasks). Graceful retrieval outcomes — fewer index rows than
`final_k`, an empty index, a missing index — never raise; they return
fewer passages or [] (§9.3, §11.5). Dependency failures keep their own
typed exceptions (`DbError`, `ProviderError`) and propagate unchanged.
"""


@dataclass(frozen=True, slots=True)
class Passage:
"""One ranked passage with an observable score (§7.5) and stage tag (§9.3).

§7.5 lists six fields; `stage` — required per passage by §9.3 and by the
`request_retrievals` schema check (`vector`|`fts`|`rrf`|`rerank`, §8.5) —
is the seventh, defaulted to "vector", the only mode this module
implements. Smallest reconciliation; recorded in docs/TASKS.md (T09).
"""

chunk_id: int
page_url: str
heading_path: str
body: str
score: float
rank: int
stage: str = "vector"


_FP8_RE = re.compile(r"[0-9a-f]{8}")


def _check_suffix(suffix: str) -> None:
"""Validate an fp8 table suffix: 8 lowercase hex chars, like db does.

Mirrors db's private `_check_suffix` instead of importing it (db.py is a
single-owner file, §14.4): table names are interpolated into SQL, so the
suffix must never be arbitrary text. `retrieve` always derives the suffix
from `config.fp8(cfg)`, making this defense in depth.
"""
if _FP8_RE.fullmatch(suffix) is None:
raise RetrievalError(f"invalid table suffix {suffix!r}: expected 8 lowercase hex chars")


def _vec_table_exists(conn: sqlite3.Connection, suffix: str) -> bool:
"""True when the per-fingerprint vec0 table exists (created at ingest, §8.3)."""
row = conn.execute(
"SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?",
(f"chunks_{suffix}_vec",),
).fetchone()
return row is not None


def _knn(
conn: sqlite3.Connection, suffix: str, query_vector: list[float], k: int
) -> list[tuple[int, float]]:
"""Brute-force cosine KNN over the vec0 table (§7.5): (chunk_id, distance).

`MATCH ... AND k = ?` is the stable sqlite-vec path (bound parameters,
verified on the pinned sqlite-vec 0.1.9); no ANN indexes are used or
created. Fewer rows than `k` simply return fewer hits, which is how the
§9.3 fewer-rows contract is served. sqlite errors surface as
RetrievalError with the sqlite message attached (DEBUG detail stays in
the cause chain).
"""
_check_suffix(suffix)
try:
rows = conn.execute(
f"SELECT rowid, distance FROM chunks_{suffix}_vec WHERE embedding MATCH ? AND k = ?",
(sqlite_vec.serialize_float32(query_vector), k),
).fetchall()
except sqlite3.Error as exc:
raise RetrievalError(f"vector KNN query failed on chunks_{suffix}_vec: {exc}") from exc
return [(int(row[0]), float(row[1])) for row in rows]


def retrieve(
question: str,
conn: sqlite3.Connection,
cfg: CaliperConfig,
*,
embeddings: EmbeddingBackend | None = None,
) -> list[Passage]:
"""Turn a question into the final bounded passage set (§7.5, §9.3).

Vector mode: embed the question via the provider seam — `embed_query`,
which carries the retrieval *query* task type inside each backend and
emits 768-dim vectors (§7.2) — then cosine KNN top `cfg.retrieval.final_k`
over the fingerprint's vec0 table. Returns exactly `final_k` passages
ranked 1 (most similar) → `final_k`, `score` = cosine distance (lower is
more similar), `stage = "vector"`; deterministic on fixed input (ties are
broken by ascending chunk_id).

The embedding backend is built with the provider factory
(`get_embeddings`, which owns session setup per T04's `keepalive`
pattern). Tests inject a backend through the keyword-only `embeddings`
seam — the suite's fakes, §15 — so the positional §9.3 contract
`retrieve(question, conn, cfg)` is unchanged.

Failure modes (§16): empty question, unimplemented mode, or a
non-768-dim query embedding raise RetrievalError. An index with fewer
rows than `final_k`, an empty index, or a missing (never-ingested) index
return fewer passages or [] — the caller refuses per §9.3/§11.5; no
sentinel `score` value is introduced.
"""
if not question.strip():
raise RetrievalError("question must be a non-empty string")
mode = cfg.retrieval.mode
if mode != "vector":
raise RetrievalError(
f"retrieval mode {mode!r} is not implemented here (hybrid_rrf is T10, rerank is T18)"
)
backend = embeddings if embeddings is not None else get_embeddings(cfg)
query_vector = backend.embed_query(question)
if len(query_vector) != EMBEDDING_DIMENSIONS:
raise RetrievalError(
f"query embedding has dimension {len(query_vector)}, expected {EMBEDDING_DIMENSIONS}"
)
suffix = fp8(cfg)
if not _vec_table_exists(conn, suffix):
# Never-ingested fingerprint ≡ empty index (§11.5): graceful [] — the
# caller refuses; distinguishing §9's `index_missing` from a plain
# refusal is the caller's edge decision. No sentinel passages.
return []
hits = _knn(conn, suffix, query_vector, cfg.retrieval.final_k)
# Deterministic order regardless of vec0's internal tie handling:
# ascending distance (= descending relevance), chunk_id as tie-break.
hits.sort(key=lambda hit: (hit[1], hit[0]))
chunks = get_chunks(conn, suffix, [chunk_id for chunk_id, _ in hits])
return [
Passage(
chunk_id=chunk.chunk_id,
page_url=chunk.page_url,
heading_path=chunk.heading_path,
body=chunk.body,
score=distance,
rank=rank,
stage="vector",
)
for rank, (chunk, (_, distance)) in enumerate(zip(chunks, hits, strict=True), start=1)
]
Loading
Loading