feat: scaffold Brain of Reese — FastAPI RAG chat over Postgres 17 + pgvector

Foundation (phase 01, verified):
- FastAPI app: /api/health, /api/suggestions, /api/chat (placeholder),
  static frontend served locally (no CDN)
- Postgres 17 + pgvector via db/Containerfile + compose.yaml
  (podman compose up -d db), Alembic initial migration (documents,
  chunks with vector(768), query_log)
- LLM client targeting https://aipi.reeseapps.com/v1 (turbo/embed);
  scripts/llm_probe.py verified models + 768-dim embeddings live
- Conditional debugpy: imported only when DEBUGPY=1 (attach on demand,
  :5678); logging config for clean single-line logs
- Frontend shell: mobile-first chat + Sources pages, tokens, a11y baselines
- Tests: 24 unit+integration (99% coverage on app/), ruff + pyright clean,
  Playwright smoke E2E (3 tests) against a deterministic mock LLM
- Planning: .agent/PLAN.md (architecture + LOCKED decisions), AGENTS.md,
  6 user stories, 7 phase files (one story / one phase / one Playwright
  suite each)
This commit is contained in:
2026-08-21 13:42:21 -04:00
commit 022da8e2bc
63 changed files with 5225 additions and 0 deletions
+50
View File
@@ -0,0 +1,50 @@
"""Alembic migration environment (sync engine, URL from app settings)."""
from __future__ import annotations
import os
from logging.config import fileConfig
from sqlalchemy import engine_from_config, pool
import app.models # noqa: F401 (registers all models on Base.metadata)
from alembic import context
from app.config import get_settings
from app.db import Base
config = context.config
if config.config_file_name is not None and os.path.exists(config.config_file_name):
fileConfig(config.config_file_name)
config.set_main_option("sqlalchemy.url", get_settings().database_url)
target_metadata = Base.metadata
def run_migrations_offline() -> None:
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online() -> None:
connectable = engine_from_config(
config.get_section(config.config_ini_section, {}),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
with connectable.connect() as connection:
context.configure(connection=connection, target_metadata=target_metadata)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
+25
View File
@@ -0,0 +1,25 @@
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
"""
from __future__ import annotations
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}
branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}
def upgrade() -> None:
${upgrades if upgrades else "pass"}
def downgrade() -> None:
${downgrades if downgrades else "pass"}
+86
View File
@@ -0,0 +1,86 @@
"""initial schema: documents, chunks (pgvector), query_log
Revision ID: 0001
Revises:
Create Date: 2026-08-21
"""
from __future__ import annotations
import sqlalchemy as sa
from pgvector.sqlalchemy import Vector
from sqlalchemy.dialects import postgresql
from alembic import op
EMBEDDING_DIM = 768 # keep in sync with app/models.py (PLAN anchor A6)
revision = "0001"
down_revision = None
branch_labels = None
depends_on = None
def upgrade() -> None:
op.execute("CREATE EXTENSION IF NOT EXISTS vector")
op.create_table(
"documents",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
sa.Column("source", sa.String(120), nullable=False),
sa.Column("path", sa.String(1000), nullable=False),
sa.Column("full_path", sa.String(2000), nullable=False),
sa.Column("title", sa.String(500), nullable=False),
sa.Column("content", sa.Text(), nullable=False),
sa.Column("content_hash", sa.String(64), nullable=False),
sa.Column(
"indexed_at",
sa.DateTime(timezone=True),
server_default=sa.func.now(),
nullable=False,
),
sa.UniqueConstraint("source", "path", name="uq_documents_source_path"),
)
op.create_index("ix_documents_source", "documents", ["source"])
op.create_index("ix_documents_path", "documents", ["path"])
op.create_index("ix_documents_content_hash", "documents", ["content_hash"])
op.create_table(
"chunks",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
sa.Column(
"document_id",
postgresql.UUID(as_uuid=True),
sa.ForeignKey("documents.id", ondelete="CASCADE"),
nullable=False,
),
sa.Column("position", sa.Integer(), nullable=False),
sa.Column("content", sa.Text(), nullable=False),
sa.Column("embedding", Vector(EMBEDDING_DIM), nullable=True),
)
op.create_index("ix_chunks_document_id", "chunks", ["document_id"])
op.create_table(
"query_log",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
sa.Column("question", sa.Text(), nullable=False),
sa.Column("top_score", sa.Float(), nullable=False, server_default="0"),
sa.Column("chunk_hits", sa.Integer(), nullable=False, server_default="0"),
sa.Column("deflected", sa.Boolean(), nullable=False, server_default=sa.false()),
sa.Column("sources", sa.Text(), nullable=False, server_default=""),
sa.Column("latency_ms", sa.Integer(), nullable=False, server_default="0"),
sa.Column(
"created_at",
sa.DateTime(timezone=True),
server_default=sa.func.now(),
nullable=False,
),
)
op.create_index("ix_query_log_created_at", "query_log", ["created_at"])
def downgrade() -> None:
op.drop_table("query_log")
op.drop_table("chunks")
op.drop_table("documents")
op.execute("DROP EXTENSION IF EXISTS vector")