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
11 changes: 11 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ jobs:
- run: uv run ruff check .
- run: uv run ruff format --check .
- run: uv run pytest
# Starts the server with the same command as render.yaml, so a broken start
# command or import error fails here instead of in a Render deploy.
- name: Server starts and answers /health
run: |
uv run --no-sync uvicorn app.main:app --host 127.0.0.1 --port 8000 &
for i in $(seq 1 20); do
curl -fsS http://127.0.0.1:8000/health && exit 0
sleep 1
done
echo "::error::API did not answer /health within 20 seconds"
exit 1

# Applies every migration to a throwaway local Supabase on the runner. Uses no
# secrets and never touches a hosted project.
Expand Down
3 changes: 2 additions & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ side the evidence supports. Web app first, mobile later.
database directly (the Supabase MCP is read-only on purpose) and never edit a migration
that is already on `main`.
- `NEXT_PUBLIC_` variables ship to every browser: never put secrets in them. Secrets go in
`api/.env`. Keep `web/.env.example` and `api/.env.example` listing every variable.
`api/.env`. Keep `web/.env.example` and `api/.env.example` listing every variable; a new
backend variable also goes in `render.yaml` `envVars` (secrets as `sync: false`).
- Never print, log, commit or paste API keys or `.env` contents. The team shares keys.
- Never connect AI tools to the production database.
- Read AI models from settings (`CLAIM_MODEL`, `ANALYSIS_MODEL`); never hardcode a provider.
Expand Down
4 changes: 4 additions & 0 deletions api/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
# The team shares the AWS, AssemblyAI and Exa keys: Hani sends each one through a
# one-time link. Never paste keys into chat, commits, logs or AI prompts.

# Browser origins allowed to call the API, comma-separated. Local default shown; on Render,
# set it to the Vercel URL(s).
CORS_ORIGINS=http://localhost:3000

# Supabase dashboard > Connect > Session pooler connection string (includes the DB password)
DATABASE_URL=
# Supabase dashboard > Project Settings > API: Project URL
Expand Down
10 changes: 10 additions & 0 deletions api/app/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

from app.settings import get_settings

app = FastAPI(title="Dialectic.ai API")

app.add_middleware(
CORSMiddleware,
allow_origins=[o.strip() for o in get_settings().cors_origins.split(",") if o.strip()],
allow_methods=["*"],
allow_headers=["*"],
)


@app.get("/health")
def health() -> dict[str, str]:
Expand Down
3 changes: 3 additions & 0 deletions api/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ class Settings(BaseSettings):

model_config = SettingsConfigDict(env_file=".env", extra="ignore")

# Comma-separated browser origins allowed to call the API, e.g. the Vercel URL.
cors_origins: str = "http://localhost:3000"

database_url: str | None = None
supabase_url: str | None = None

Expand Down
22 changes: 22 additions & 0 deletions api/tests/test_cors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from fastapi.testclient import TestClient

from app.main import app

client = TestClient(app)


def preflight(origin: str):
return client.options(
"/health",
headers={"Origin": origin, "Access-Control-Request-Method": "GET"},
)


def test_allowed_origin() -> None:
response = preflight("http://localhost:3000")
assert response.headers["access-control-allow-origin"] == "http://localhost:3000"


def test_other_origin_rejected() -> None:
response = preflight("https://evil.example")
assert "access-control-allow-origin" not in response.headers
21 changes: 21 additions & 0 deletions docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@ which promotional credits may not cover; Amazon's Nova models are covered). Othe
use the Gemini API. At alpha, compare candidate models on sample debates with
`pydantic_evals` before settling.

## Backend hosting (Render)

FastAPI runs on Render as an always-running web service, defined in `render.yaml` at the
repo root. Vercel can't host it: its functions are short-lived and can't hold the long
WebSocket connections a live debate needs (browser audio in, claims and fact-checks out).

- Render builds from `api/` with `uv sync --locked --no-dev` and starts the same uvicorn
command the CI `api` job tests. It checks `/health` before sending traffic to a new
deploy.
- It deploys a commit on `main` only after that commit's GitHub checks pass, and only
when files under `api/` changed. Region: Virginia, next to Supabase.
- Secrets are entered once in the Render dashboard (`sync: false` in `render.yaml`).
`CORS_ORIGINS` lists the frontend URLs allowed to call the API.
- Plan: **Free** until alpha. It sleeps after 15 minutes without traffic and takes about a
minute to wake. 750 free hours a month is more than one service can use, so it never
runs out. Switch to **Starter** ($7/month, always on) before the first outside testers,
because a sleeping server would stall a live debate.
- The account is Hani's. Developers run the API locally; the Render service is the shared
deployed copy.

## Accounts and keys

- AWS: one account owned by Hani. Each developer gets their own IAM user and Bedrock API
Expand Down Expand Up @@ -93,6 +113,7 @@ MVP, alpha, pilot, beta, public release (web, then iOS and Android).
project; schema changes are tested by CI.
- **Alpha:** add Docker: a local Supabase per developer (`supabase start`) and containers
for consistent local setup. Google sign-in and a custom email sender. Model evaluation.
Render service on the Starter plan.
- **Before the pilot:** create the production database (checklist below). Invite-only sign-ups.
- **Beta:** `@virginia.edu` sign-ups.
- **Public:** mobile apps, Sign in with Apple.
Expand Down
44 changes: 44 additions & 0 deletions render.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Render Blueprint for the FastAPI backend. Render reads this file from the repo root.
# Secret values are never stored here: `sync: false` makes Render prompt for each one
# in its dashboard. Keep envVars in step with api/.env.example.
services:
- type: web
name: dialectic-api
runtime: python
plan: free
# Same region as the Supabase database (AWS us-east-1) and Vercel's default.
region: virginia
rootDir: api
buildCommand: uv sync --locked --no-dev
# --no-sync: the build already installed everything; don't reinstall on each start.
startCommand: uv run --no-sync uvicorn app.main:app --host 0.0.0.0 --port $PORT
healthCheckPath: /health
# Deploy a commit on main only after its GitHub checks pass.
autoDeployTrigger: checksPass
buildFilter:
paths:
- api/**
envVars:
# Render reads .python-version only from the repo root; ours lives in api/.
- key: PYTHON_VERSION
value: "3.14.10"
- key: CORS_ORIGINS
sync: false
- key: DATABASE_URL
sync: false
- key: SUPABASE_URL
sync: false
- key: AWS_REGION
value: us-east-1
- key: AWS_BEARER_TOKEN_BEDROCK
sync: false
- key: GEMINI_API_KEY
sync: false
- key: CLAIM_MODEL
sync: false
- key: ANALYSIS_MODEL
sync: false
- key: ASSEMBLYAI_API_KEY
sync: false
- key: EXA_API_KEY
sync: false