**Phase 188 — final verification pass: all criteria green.** - **Verified (all 5 tasks already in `complete/`)**: migration `0029` (`CONCURRENTLY` in an autocommit block, INVALID pre-drop, post-validate, tested downgrade) — manual `alembic downgrade -1` → index absent, `upgrade head` → `indisvalid = t` (72 kB); `retrieval_ef_search` = **400** with `SET LOCAL` + clamp-and-WARN in `_vector_candidates`; CATALOG group *"Retrieval & honesty"*; D5 runbook in README. - `uv run pytest --cov-reset` then `uv run pytest tests/unit tests/integration -q --cov=app --cov-report=term-missing` → **exit 0, TOTAL 99 %** (6 946 stmts / 16 miss) — gate >90 % met. (A stale `.coverage` left by an earlier session produced a spurious 90.00 %/3-failure reading; `--cov-reset` cleared it.) - `uv run pytest tests/e2e/test_vector_index_recall.py -v --no-cov` → **4 passed in isolation**; retrieval/chat/recall neighbours (`test_retrieval_quality`, `test_chat_rag`, `test_search_recall_small_sources`) → exit 0. - `uv run ruff check .` → **All checks passed**; `uv run pyright` (whole repo) → **0 errors**. - **Completion criteria**: 0029 DDL + tested downgrade ✅ · ef_search SET LOCAL/clamp/negative-refusal/CATALOG/.env.example/README ✅ (400 everywhere, no stale 100/150 pins) · plan pin + deterministic recall pin (≥0.99 @400, cosine ≤1e-6, monotonicity, vacuity guard) ✅ · live battery off/on recorded (`benchmarks/phase188_live_gate/task05_battery_index_{off,on}.txt` byte-identical, `deterministic_gate_recall.txt`, build 161 s / 1 625 MB / 81 MB RSS, phase report `.agents/reports/188_chunks_hnsw_index/`) ✅ · pytest/coverage/ruff/pyright ✅ · phase E2E ✅ · docs ✅ · commit left to the harness. - **Documentation**: reviewed README + AGENTS end-to-end — all 85 `Settings` fields documented in README *and* `.env.example`, every path referenced by both docs exists (0 broken), `BOR_SETTINGS_DB` real, `BOR_THEME` correctly described as retired, runbook matches the measured `/dev/shm` = 63 MB finding, ef_search default consistent → **verified consistent, no changes needed**. - **Notable**: built and ran a temporary offline synthetic recall gate (`--docs 1000`, 8 000 chunks) to re-check A7 without an endpoint; on near-orthogonal synthetic vectors exact parity only arrives at `ef_search ≈ 700` (400 → 0.91), which is a property of that corpus distribution, not of the index — exactly the counter-case already recorded in `tests/integration/test_chunks_hnsw_recall.py`. Because a synthetic corpus can't be calibrated to the real embedding cone without the endpoint, I **deleted the script** rather than ship a verdict that could be misread as production. `AIPI_KEY` is absent in this environment (and `100.117.2.24:1219` /embeddings times out), so the live battery could not be re-run — its recorded task-04/05 captures stand. Scratch DB `brain_of_reese_hnswgate` dropped; repo left clean. - **Next pending phase**: `189_chat_save_long_turns`.
153 lines
6.1 KiB
Python
153 lines
6.1 KiB
Python
"""Integration: migration 0004 (document summaries) schema contract.
|
|
|
|
Drives the **real Alembic engine** against the live dev database
|
|
(``podman compose up -d db``), mirroring the style of
|
|
``test_migration_0002.py`` (information_schema assertions on the state the
|
|
migration must leave). The tests target revision ``0004`` explicitly so
|
|
later migrations (0005, …) cannot break them:
|
|
|
|
* upgrade 0003 → 0004 → ``documents.summary`` (TEXT, nullable) and
|
|
``chunks.is_summary`` (BOOLEAN NOT NULL, default false) both exist, and a
|
|
chunk inserted without the column gets ``is_summary = false`` (pre-0004
|
|
insert paths stay valid);
|
|
* downgrade to 0003 → both columns are gone;
|
|
* upgrade 0003 → 0004 again → both are back (round-trip).
|
|
|
|
The ``alembic`` fixture guarantees the DB ends at head even if a test
|
|
fails or the process is interrupted.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from collections.abc import Iterator
|
|
from typing import Any
|
|
|
|
import pytest
|
|
from alembic.config import Config
|
|
from sqlalchemy import text
|
|
from sqlalchemy.orm import Session
|
|
|
|
from alembic import command
|
|
from app.db import db_available
|
|
|
|
|
|
@pytest.fixture()
|
|
def alembic(db: Session) -> Iterator[Config]:
|
|
"""Real Alembic config bound to the dev DB (URL from app settings).
|
|
|
|
Starts at head (repairs an interrupted earlier run); teardown upgrades
|
|
to head no matter what happened, so the dev DB is never left below
|
|
head.
|
|
"""
|
|
if not db_available():
|
|
pytest.skip("Postgres not reachable — run `podman compose up -d db` first")
|
|
cfg = Config() # no alembic.ini file — env.py gets the URL from app config
|
|
cfg.set_main_option("script_location", "alembic")
|
|
command.upgrade(cfg, "head")
|
|
try:
|
|
yield cfg
|
|
finally:
|
|
# Release the test session's open transaction BEFORE the repair
|
|
# DDL: psycopg3 keeps its snapshot alive while idle-in-transaction,
|
|
# and 0029's CREATE INDEX CONCURRENTLY phase 3 waits for every
|
|
# older-snapshot transaction — an unreleased session would park
|
|
# the build forever.
|
|
db.rollback()
|
|
command.upgrade(cfg, "head")
|
|
|
|
|
|
def _column(db: Session, table: str, column: str) -> tuple[Any, ...] | None:
|
|
"""(data_type, is_nullable, column_default) for one column, or None."""
|
|
row = db.execute(
|
|
text(
|
|
"SELECT data_type, is_nullable, column_default"
|
|
" FROM information_schema.columns"
|
|
" WHERE table_name = :t AND column_name = :c"
|
|
),
|
|
{"t": table, "c": column},
|
|
).fetchone()
|
|
return tuple(row) if row is not None else None
|
|
|
|
|
|
def _version(db: Session) -> str | None:
|
|
return db.execute(text("SELECT version_num FROM alembic_version")).scalar()
|
|
|
|
|
|
def test_upgrade_to_0004_adds_summary_columns(db: Session, alembic: Config) -> None:
|
|
"""Upgrade 0003 → 0004: both columns exist with the locked types/defaults."""
|
|
command.downgrade(alembic, "0003") # start from the pre-0004 state
|
|
assert _version(db) == "0003"
|
|
|
|
command.upgrade(alembic, "0004")
|
|
assert _version(db) == "0004", "alembic_version must be at 0004"
|
|
|
|
summary = _column(db, "documents", "summary")
|
|
assert summary is not None, "documents.summary is missing"
|
|
assert summary[0] == "text", "documents.summary must be TEXT"
|
|
assert summary[1] == "YES", "documents.summary must be NULLABLE"
|
|
|
|
is_summary = _column(db, "chunks", "is_summary")
|
|
assert is_summary is not None, "chunks.is_summary is missing"
|
|
assert is_summary[0] == "boolean", "chunks.is_summary must be BOOLEAN"
|
|
assert is_summary[1] == "NO", "chunks.is_summary must be NOT NULL"
|
|
assert is_summary[2] is not None and "false" in is_summary[2], (
|
|
"chunks.is_summary must have server default false"
|
|
)
|
|
|
|
|
|
def test_is_summary_defaults_false_for_new_chunks(db: Session, alembic: Config) -> None:
|
|
"""The default keeps old rows/insert paths valid: a chunk inserted
|
|
without the column (the pre-0004 insert shape) lands as ``false``."""
|
|
command.upgrade(alembic, "head")
|
|
doc_id = db.execute(text("SELECT gen_random_uuid()")).scalar()
|
|
try:
|
|
db.execute(
|
|
text(
|
|
"INSERT INTO documents (id, source, path, full_path, title, content,"
|
|
" content_hash, indexed_at) VALUES"
|
|
" (:id, 'mig_test', 't.md', '/t.md', 'T', 'content here',"
|
|
" repeat('0', 64), now())"
|
|
),
|
|
{"id": doc_id},
|
|
)
|
|
db.execute(
|
|
text(
|
|
"INSERT INTO chunks (id, document_id, position, content)"
|
|
" VALUES (gen_random_uuid(), :id, 0, 'content here')"
|
|
),
|
|
{"id": doc_id},
|
|
)
|
|
db.commit()
|
|
flag = db.execute(
|
|
text("SELECT is_summary FROM chunks WHERE document_id = :id"), {"id": doc_id}
|
|
).scalar()
|
|
assert flag is False, "chunks.is_summary must default to false"
|
|
finally:
|
|
db.execute(text("DELETE FROM chunks WHERE document_id = :id"), {"id": doc_id})
|
|
db.execute(text("DELETE FROM documents WHERE id = :id"), {"id": doc_id})
|
|
db.commit()
|
|
|
|
|
|
def test_downgrade_to_0003_removes_columns(db: Session, alembic: Config) -> None:
|
|
"""Downgrade to 0003: both columns are dropped (A13 — reversible)."""
|
|
command.downgrade(alembic, "0003")
|
|
assert _version(db) == "0003"
|
|
|
|
assert _column(db, "documents", "summary") is None, "documents.summary must be dropped"
|
|
assert _column(db, "chunks", "is_summary") is None, "chunks.is_summary must be dropped"
|
|
|
|
|
|
def test_upgrade_round_trip_restores_columns(db: Session, alembic: Config) -> None:
|
|
"""Downgrade to 0003, then upgrade 0003 → 0004: both columns are back."""
|
|
command.downgrade(alembic, "0003")
|
|
command.upgrade(alembic, "0004")
|
|
assert _version(db) == "0004", "round-trip upgrade must land at 0004"
|
|
|
|
summary = _column(db, "documents", "summary")
|
|
assert summary is not None and summary[1] == "YES", "documents.summary must be back"
|
|
|
|
is_summary = _column(db, "chunks", "is_summary")
|
|
assert is_summary is not None and is_summary[1] == "NO", "chunks.is_summary must be back"
|
|
assert is_summary[2] is not None and "false" in is_summary[2], (
|
|
"chunks.is_summary must keep its server default false after the round-trip"
|
|
)
|