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
39 changes: 39 additions & 0 deletions configs/caliper.sample.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Caliper user configuration template (FR-11).
#
# Copy to `caliper.toml` at the repo root and edit. Version presets
# (configs/v1.toml, v2.toml, v3.toml) are merged UNDER this file, so values
# set here win — that is how you switch provider/model (e.g. to Ollama per
# assumption A2) without editing the committed presets.
#
# Paths, provider selection, and secrets are environment variables, not TOML
# (§11.2): CALIPER_PROVIDER, CALIPER_DB, CALIPER_CACHE_DIR, GEMINI_API_KEY...

[answer]
# Fixed answerer — identical across V1/V2/V3 (FR-10). Override only when
# switching provider (a new fingerprint and re-run follows by design, FR-09).
model = "gemini-3.5-flash-lite"
temperature = 0.0
seed = 42
# Initial 0.35; re-verified once against V1 eval data, then frozen (§7.7, §9.7).
refusal_threshold = 0.35
high_confidence_threshold = 0.60
# SHA-256 of the frozen prompt template. Placeholder until T11 freezes the
# template file; changing it changes the fingerprint (§11.2).
prompt_template_sha256 = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"

[embeddings]
model = "gemini-embedding-001"
dimensions = 768

[chunking]
unit = "heading" # "heading" (V1) or "fixed" (V2/V3)
size_tokens = 800
overlap_tokens = 0

[retrieval]
mode = "vector" # "vector" (V1/V2) or "hybrid_rrf" (V3)
vector_top_k = 5
fts_top_k = 0 # must be 0 when mode = "vector"
rrf_k = 60
rerank = "none" # "cross-encoder" (V3 only)
final_k = 5
27 changes: 27 additions & 0 deletions configs/v1.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# V1 baseline preset — cosine-only retrieval over heading chunks (§9.7).
# All three presets share identical answer settings (FR-10 invariant).

[answer]
model = "gemini-3.5-flash-lite"
temperature = 0.0
seed = 42
refusal_threshold = 0.35 # initial; re-verified once against V1 data, then frozen
high_confidence_threshold = 0.60
prompt_template_sha256 = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" # placeholder until T11

[embeddings]
model = "gemini-embedding-001"
dimensions = 768

[chunking]
unit = "heading"
size_tokens = 800
overlap_tokens = 0

[retrieval]
mode = "vector"
vector_top_k = 5
fts_top_k = 0
rrf_k = 60
rerank = "none"
final_k = 5
27 changes: 27 additions & 0 deletions configs/v2.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# V2 preset — improved segmentation: fixed-size windows with overlap (§9.7).
# Only the chunking block differs from V1; retrieval and answerer unchanged.

[answer]
model = "gemini-3.5-flash-lite"
temperature = 0.0
seed = 42
refusal_threshold = 0.35 # initial; re-verified once against V1 data, then frozen
high_confidence_threshold = 0.60
prompt_template_sha256 = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" # placeholder until T11

[embeddings]
model = "gemini-embedding-001"
dimensions = 768

[chunking]
unit = "fixed"
size_tokens = 350
overlap_tokens = 50

[retrieval]
mode = "vector"
vector_top_k = 5
fts_top_k = 0
rrf_k = 60
rerank = "none"
final_k = 5
27 changes: 27 additions & 0 deletions configs/v3.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# V3 preset — improved retrieval/ranking: hybrid RRF fusion + cross-encoder
# rerank (§9.7, ADR-006/ADR-011). Chunking identical to V2; answerer unchanged.

[answer]
model = "gemini-3.5-flash-lite"
temperature = 0.0
seed = 42
refusal_threshold = 0.35 # initial; re-verified once against V1 data, then frozen
high_confidence_threshold = 0.60
prompt_template_sha256 = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" # placeholder until T11

[embeddings]
model = "gemini-embedding-001"
dimensions = 768

[chunking]
unit = "fixed"
size_tokens = 350
overlap_tokens = 50

[retrieval]
mode = "hybrid_rrf"
vector_top_k = 20
fts_top_k = 20
rrf_k = 60
rerank = "cross-encoder" # optional dependency group "rerank" (§5)
final_k = 5
11 changes: 9 additions & 2 deletions docs/TASKS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ Statuses: ✅ done · 🔄 doing · ⬜ remaining · ✂️ cut (stretch only, p

| ID | Commit(s) | Verified |
|---|---|---|
| T01 | `c76a2bc` (tracker), `d0446f6` (scaffold) | `uv sync` OK (CPython 3.12.16); `uv run caliper --help` renders; `ruff check` + `ruff format --check` + `mypy` clean; 2 tests pass. *CI run on GitHub pending — first push.* |
| T01 | `c76a2bc` (tracker), `d0446f6` (scaffold) | `uv sync` OK (CPython 3.12.16); `uv run caliper --help` renders; `ruff check` + `ruff format --check` + `mypy` clean; 2 tests pass. CI green on push/PR and on `main` after PR #1 merge (run 43717112766). |
| T02 | `489c035` | `uv run pytest` — 38 passed (36 new config tests); `ruff check` + `ruff format --check` clean; `mypy` strict clean. Verified: user-over-preset deep merge; §9.7 contents binding for v1/v2/v3 + shared fixed answerer; fingerprint stable across reloads, differs across versions, and reacts to every binding-field change (parametrized, 17 fields); invalid configs rejected with key path. |

## Doing

_(none — next up: **T02**, pending review)_
_(none — next up: **T03**, pending review; T04 is parallelizable with it per §14.3)_

## Remaining

Expand Down Expand Up @@ -84,3 +85,9 @@ _(none — next up: **T02**, pending review)_
- Max concurrency: 2 agents; ownership rules in ARCHITECTURE §6.3 keep parallel tasks disjoint.
- Contract-touching tasks follow the shared-file protocol (§14.4): PR touches only the owned file + its tests, updates the binding §7–§9 section in the same change, and names dependent tasks.
- Never edit `data/golden/questions.jsonl` after the first measured run; never commit `data/caliper.db`, `data/cache/`, `caliper.toml`, `.env`.

### Decisions recorded during the build

- **T02 — merge order (§11.2 ambiguity):** the version preset is loaded first and the user config is deep-merged *over* it (user wins on conflicts). Rationale: this is the only reading consistent with A2's fallback path ("flip `CALIPER_PROVIDER=ollama`" requires overriding `answer.model`/`embeddings.model`, which preset-wins would forbid since presets are committed). FR-10 comparability is not weakened: identity is enforced by the fingerprint gate (§7.8), computed from the resolved config, not by the merge order.
- **T02 — `answer.prompt_template_sha256`:** presets carry the SHA-256 of the empty string as a placeholder until T11 stores the frozen prompt template file; T11 must replace it in all four TOML files (and in `PROMPT_TEMPLATE_SHA256_PLACEHOLDER` in `config.py`'s default) — the change is fingerprint-visible by construction.
- **T02 — `high_confidence_threshold`:** not pinned in §9.7; initialized to 0.60 in all presets. To be re-verified alongside the refusal threshold at T16 before the V1 baseline run freezes it.
196 changes: 196 additions & 0 deletions src/caliper/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
"""Configuration loading, merging, and fingerprinting (§7.1).

Single definition of the config model and of the canonical fingerprint used
everywhere (§9.7 contents binding, §11.2 fingerprint scope). This module is a
leaf: it has no knowledge of the DB layout or providers, and performs no I/O
beyond reading TOML files (§7.1 "Must not").

Merge order (§11.2 ambiguity resolution, recorded in docs/TASKS.md Notes):
the version preset is merged first, then the user config is merged over it —
the user config wins on conflicts. This is what makes the A2 fallback path
("flip CALIPER_PROVIDER=ollama") workable without editing committed presets,
and it stays safe for FR-10 because comparability is enforced by the
fingerprint gate (§7.8), not by the merge order.
"""

from __future__ import annotations

import hashlib
import json
import tomllib
from collections.abc import Mapping
from pathlib import Path
from typing import Any, Literal

from pydantic import BaseModel, ConfigDict, Field, ValidationError, model_validator

# SHA-256 of the empty string: placeholder for the frozen prompt template's
# hash until T11 stores the real template file. Changing this value changes
# the fingerprint, so T11's replacement is fingerprint-visible by construction.
PROMPT_TEMPLATE_SHA256_PLACEHOLDER = (
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
)


class ConfigError(ValueError):
"""A config file is missing, unreadable, or invalid (§11.4).

The message always names the offending key path. Mapped to the
`config_invalid` envelope code at the CLI/web edges (§9).
"""


class ChunkingConfig(BaseModel):
"""Segmentation settings (§9.7). The 1,900-token cap matches §7.4."""

model_config = ConfigDict(frozen=True, extra="forbid")

unit: Literal["heading", "fixed"] = "heading"
size_tokens: int = Field(default=800, ge=1, le=1900)
overlap_tokens: int = Field(default=0, ge=0)

@model_validator(mode="after")
def _overlap_below_size(self) -> ChunkingConfig:
if self.overlap_tokens >= self.size_tokens:
# The ValueError message names the offending key path (§11.4);
# pydantic prefixes it with the section name in ValidationError.loc.
raise ValueError("chunking.overlap_tokens must be smaller than chunking.size_tokens")
return self


class RetrievalConfig(BaseModel):
"""Search/ranking settings (§7.5, §9.7)."""

model_config = ConfigDict(frozen=True, extra="forbid")

mode: Literal["vector", "hybrid_rrf"] = "vector"
vector_top_k: int = Field(default=5, ge=1)
fts_top_k: int = Field(default=0, ge=0)
rrf_k: int = Field(default=60, ge=1)
rerank: Literal["none", "cross-encoder"] = "none"
final_k: int = Field(default=5, ge=1)

@model_validator(mode="after")
def _mode_consistency(self) -> RetrievalConfig:
# Stated rule (§11.4): fts_top_k=0 iff mode="vector".
if self.mode == "vector" and self.fts_top_k != 0:
raise ValueError("retrieval.fts_top_k must be 0 when mode is 'vector'")
if self.mode == "hybrid_rrf" and self.fts_top_k == 0:
raise ValueError("retrieval.fts_top_k must be > 0 when mode is 'hybrid_rrf'")
# A reranker only makes sense over a fused candidate list (§7.5).
if self.rerank != "none" and self.mode != "hybrid_rrf":
raise ValueError("retrieval.rerank other than 'none' requires mode 'hybrid_rrf'")
return self


class AnswerConfig(BaseModel):
"""Fixed answerer settings (§7.7, §9.7). Identical across V1–V3 (FR-10)."""

model_config = ConfigDict(frozen=True, extra="forbid")

model: str = "gemini-3.5-flash-lite"
temperature: float = Field(default=0.0, ge=0.0, le=2.0)
seed: int = Field(default=42, ge=0)
# Initial 0.35; re-verified once against V1 eval data, then frozen (§7.7, §9.7).
refusal_threshold: float = Field(default=0.35, ge=0.0, le=1.0)
high_confidence_threshold: float = Field(default=0.60, ge=0.0, le=1.0)
prompt_template_sha256: str = PROMPT_TEMPLATE_SHA256_PLACEHOLDER


class EmbeddingsConfig(BaseModel):
"""Embedding model settings (§5, §9.7)."""

model_config = ConfigDict(frozen=True, extra="forbid")

model: str = "gemini-embedding-001"
dimensions: int = Field(default=768, ge=1)


class CaliperConfig(BaseModel):
"""The full resolved Caliper configuration (§9.7).

Contains only fingerprint-relevant fields; paths, provider selection, and
secrets are environment variables (§11.2) and live outside this model.
"""

model_config = ConfigDict(frozen=True, extra="forbid")

chunking: ChunkingConfig = ChunkingConfig()
retrieval: RetrievalConfig = RetrievalConfig()
answer: AnswerConfig = AnswerConfig()
embeddings: EmbeddingsConfig = EmbeddingsConfig()


def _read_toml(path: Path) -> dict[str, Any]:
if not path.is_file():
raise ConfigError(f"config file not found: {path}")
try:
with path.open("rb") as fh:
return tomllib.load(fh)
except tomllib.TOMLDecodeError as exc:
raise ConfigError(f"invalid TOML in {path}: {exc}") from exc


def _deep_merge(base: dict[str, Any], override: Mapping[str, Any]) -> dict[str, Any]:
"""Return a deep merge of `override` on top of `base` (override wins)."""
merged: dict[str, Any] = dict(base)
for key, value in override.items():
if isinstance(value, Mapping) and isinstance(merged.get(key), dict):
merged[key] = _deep_merge(merged[key], value)
else:
merged[key] = value
return merged


def _format_validation_error(exc: ValidationError) -> str:
"""Render pydantic errors as 'section.key: message' key paths (§11.4)."""
parts = []
for err in exc.errors():
loc = ".".join(str(piece) for piece in err["loc"])
parts.append(f"{loc}: {err['msg']}")
return "; ".join(parts)


def load_config(user_path: Path, preset: Path) -> CaliperConfig:
"""Load the user config and the version preset, resolve to concrete values.

The preset is loaded first and the user config is merged over it (user
wins; see module docstring). Any unknown key, wrong type, or cross-field
violation raises ConfigError naming the offending key path (§11.4) before
any provider call could be made.
"""
preset_raw = _read_toml(preset)
user_raw = _read_toml(user_path)
merged = _deep_merge(preset_raw, user_raw)
try:
return CaliperConfig.model_validate(merged)
except ValidationError as exc:
raise ConfigError(f"invalid config: {_format_validation_error(exc)}") from exc


def fingerprint_payload(cfg: CaliperConfig) -> dict[str, Any]:
"""The fingerprinted fields, per §11.2: model IDs, embedding model +
dimensions, prompt template text hash, temperature, seed, thresholds,
chunking, retrieval mode and k-values — deliberately excluding paths and
the API key (neither is representable in this model)."""
return {
"chunking": cfg.chunking.model_dump(),
"retrieval": cfg.retrieval.model_dump(),
"answer": cfg.answer.model_dump(),
"embeddings": cfg.embeddings.model_dump(),
}


def fingerprint(cfg: CaliperConfig) -> str:
"""SHA-256 hex of the canonical JSON of the binding fields (§11.2).

Stable across runs on identical inputs; changes on any binding-field
change, which by design starts a new index/eval identity (FR-09).
"""
canonical = json.dumps(fingerprint_payload(cfg), sort_keys=True, separators=(",", ":"))
return hashlib.sha256(canonical.encode("utf-8")).hexdigest()


def fp8(cfg: CaliperConfig) -> str:
"""First 8 hex chars of the fingerprint — the per-fingerprint table suffix (§6.2)."""
return fingerprint(cfg)[:8]
Loading
Loading