|
1 | | -Welcome to the Tenant First Aid repository. This file contains the main points for new contributors. |
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
2 | 4 |
|
3 | 5 | ## Repository overview |
4 | 6 |
|
5 | | -- **Source code**: see [Architecture.md](../Architecture.md) for code organization |
6 | | -- **Tests**: see [Architecture.md](../Architecture.md) for test organization and [README.md](../README.md) for backend quality check flows/commands; see `frontend-builds` job in [pr-checks](../.github/workflows/pr-check.yml) for frontend commands |
7 | | -- **Documentation**: see [Architecture.md](../Architecture.md) for architectural documentation |
8 | | -- **Utilities**: developer commands are defined in the `Makefile`. |
9 | | -- **PR template**: [pull_request_template.md](../.github/pull_request_template.md) describes the information every PR must include. |
| 7 | +Tenant First Aid is a chatbot for Oregon housing/eviction legal information. Flask backend + React frontend monorepo, deployed on Digital Ocean. |
10 | 8 |
|
11 | | -## Local `./backend` workflow |
| 9 | +- **Architecture docs**: [Architecture.md](../Architecture.md) — RAG pipeline, endpoints, session management, frontend structure |
| 10 | +- **Deployment docs**: [Deployment.md](../Deployment.md) — CI/CD, secrets, infrastructure |
| 11 | +- **PR template**: [.github/pull_request_template.md](../.github/pull_request_template.md) |
| 12 | +- **Dev commands**: `backend/Makefile` |
12 | 13 |
|
13 | | -1. Format, lint and type‑check your changes: |
| 14 | +### Key architecture |
14 | 15 |
|
15 | | - ```bash |
16 | | - make fmt |
17 | | - make lint |
18 | | - make typecheck |
19 | | - ``` |
| 16 | +- **Backend** (`backend/`): Flask API with LangChain agent orchestration. The agent uses Vertex AI RAG to retrieve Oregon housing law documents and Google Gemini as the LLM. Key files: `langchain_chat_manager.py` (agent orchestration), `langchain_tools.py` (RAG retriever + letter tools), `schema.py` (Pydantic response chunk types shared with frontend). |
| 17 | +- **Frontend** (`frontend/`): React 19 + TypeScript + Vite + Tailwind CSS 4. Uses `@langchain/core` message types (`HumanMessage`/`AIMessage`) directly for chat state. Streaming via native `ReadableStream`. |
| 18 | +- **Type bridge**: Frontend TypeScript types in `src/types/` are auto-generated from backend Pydantic models via `generate-types` and gitignored. Must regenerate before building or type-checking. |
20 | 19 |
|
21 | | -2. Run the tests: |
| 20 | +## Backend workflow (run from `backend/`) |
22 | 21 |
|
23 | | - ```bash |
24 | | - make test |
25 | | - ``` |
| 22 | +```bash |
| 23 | +make fmt # Format + sort imports (ruff) |
| 24 | +make lint # Lint (ruff) |
| 25 | +make typecheck # Type-check (ty) |
| 26 | +make test # Run tests (pytest) |
| 27 | +make --keep-going check # All of the above in one shot |
26 | 28 |
|
27 | | - To run a single test, use `uv run pytest -s -k <test_name>`. |
| 29 | +# Single test |
| 30 | +uv run pytest -s -k <test_name> |
28 | 31 |
|
29 | | -3. Coverage can be generated with (optional but recommended for code changes): |
30 | | - ```bash |
31 | | - make test TEST_OPTIONS="--cov tenantfirstaid --cov-report html --cov-branch" |
32 | | - ``` |
| 32 | +# LangChain-specific tests |
| 33 | +uv run pytest -m langchain |
33 | 34 |
|
34 | | -All python commands should be run via `uv run python ...` |
| 35 | +# Coverage |
| 36 | +make test TEST_OPTIONS="--cov tenantfirstaid --cov-report html --cov-branch" |
| 37 | +``` |
35 | 38 |
|
36 | | -## LangChain Agent Architecture |
| 39 | +All python commands should be run via `uv run python ...` |
37 | 40 |
|
38 | | -The backend uses LangChain 1.0.8+ for agent-based conversation management with Vertex AI integration. |
| 41 | +### Environment variables |
39 | 42 |
|
40 | | -### Key Components |
41 | | -- **LangChainChatManager**: Main agent orchestration class (`backend/tenantfirstaid/langchain_chat_manager.py`) |
42 | | -- **retrieve_city_state_laws**: Tool for city/state-specific legal retrieval |
43 | | -- **ChatVertexAI**: LangChain wrapper for Google Gemini |
| 43 | +See `backend/.env.example`. Key ones: `MODEL_NAME`, `GOOGLE_APPLICATION_CREDENTIALS`, `VERTEX_AI_DATASTORE`, `LANGSMITH_API_KEY`. |
44 | 44 |
|
45 | | -### Environment Variables |
46 | | -```bash |
47 | | -MODEL_NAME=gemini-2.5-pro # LLM model name |
48 | | -VERTEX_AI_DATASTORE=projects/.../datastores/... # RAG corpus ID |
49 | | -SHOW_MODEL_THINKING=false # Enable Gemini thinking mode |
50 | | -LANGSMITH_API_KEY=... # Optional: Enable LLM evaluations |
51 | | -``` |
| 45 | +### LangSmith evaluations |
52 | 46 |
|
53 | | -### Testing LangChain Components |
54 | 47 | ```bash |
55 | | -# Run LangChain-specific tests |
56 | | -uv run pytest -m langchain |
57 | | - |
58 | | -# Run with LangSmith tracing (requires API key) |
59 | | -LANGSMITH_TRACING=true LANGCHAIN_TRACING_V2=true uv run pytest -m langchain |
60 | | - |
61 | | -# Run evaluations (see docs/EVALUATION.md) |
62 | 48 | uv run python scripts/run_langsmith_evaluation.py --num-samples 20 |
63 | 49 | ``` |
64 | 50 |
|
65 | | -## Local `./frontend` workflow |
| 51 | +See `docs/EVALUATION.md` for details. |
66 | 52 |
|
67 | | -Frontend TypeScript types in `src/types/` are auto-generated from the backend Python models and are not checked into source control. You must generate them before building or type-checking: |
| 53 | +## Frontend workflow (run from `frontend/`) |
68 | 54 |
|
69 | 55 | ```bash |
70 | | -npm run generate-types |
71 | | -# or equivalently: |
72 | | -make generate-types # (run from the backend/ directory) |
| 56 | +npm run generate-types # Required before build/typecheck — generates src/types/models.ts |
| 57 | +npm run lint # Lint (eslint) |
| 58 | +npm run format # Format (prettier) |
| 59 | +npm run typecheck # Type-check (tsc) |
| 60 | +npm run build # Build (auto-generates types first) |
| 61 | +npm run test -- --run # Run tests (vitest) |
| 62 | +npm run test -- --run --coverage # With coverage |
73 | 63 | ``` |
74 | 64 |
|
75 | | -This requires `uv` to be installed (see backend setup). |
76 | | - |
77 | | -1. Format, lint and type‑check your changes: |
78 | | - |
79 | | - ```bash |
80 | | - npm run lint |
81 | | - npx run format |
82 | | - ``` |
83 | | - |
84 | | -2. Build frontend code (automatically generates types first) |
85 | | - ```bash |
86 | | - npm run build |
87 | | - ``` |
88 | | - |
89 | | -3. Test frontend code |
90 | | - ```bash |
91 | | - npm run test -- --run |
92 | | - ``` |
93 | | - |
94 | | -4. Test Coverage can be generated with (optional but recommended for code changes): |
95 | | - ```bash |
96 | | - npm run test -- --run --coverage |
97 | | - ``` |
| 65 | +`generate-types` requires `uv` to be installed (it runs backend Python to emit JSON Schema, piped through `json2ts`). |
98 | 66 |
|
99 | 67 | ## Style notes |
100 | 68 |
|
101 | 69 | - Write comments as full sentences and end them with a period. |
102 | 70 |
|
103 | | -## Pull request expectations |
104 | | - |
105 | | -PRs should use the template located at [pull_request_template.md](../.github/pull_request_template.md). Provide a summary, test plan and issue number if applicable, then check that: |
| 71 | +## Commit messages |
106 | 72 |
|
107 | | -- for frontend, backend and backend/scripts |
108 | | - - New tests are added when needed. |
109 | | - - Documentation is updated. |
110 | | - - `make lint` and `make format` have been run. |
111 | | - - The full test suite passes. |
| 73 | +Concise, imperative mood, small focused commits. Write like a humble experienced engineer — casual, no listicles, highlight non-obvious choices. No robot speak, marketing buzzwords, or vague fluff. |
112 | 74 |
|
113 | | -## Commit Messages |
| 75 | +## Pull request expectations |
114 | 76 |
|
115 | | -Commit messages should be concise and written in the imperative mood. Small, focused commits are preferred. |
| 77 | +Use the PR template. For frontend, backend, and backend/scripts changes: |
| 78 | +- Add tests when needed |
| 79 | +- Update documentation |
| 80 | +- Run `make lint` and `make fmt` |
| 81 | +- Full test suite passes |
116 | 82 |
|
117 | | -Write commit messages and PR descriptions as a humble but experienced engineer would. Keep it casual, avoid listicles, briefly describe what we're doing and highlight non-obvious implementation choices but don't overthink it. |
| 83 | +## What reviewers look for |
118 | 84 |
|
119 | | -Don't embarrass me with robot speak, marketing buzzwords, or vague fluff. Just leave a meaningful trace so someone can understand the choices later. Assume the reader is able to follow the code perfectly fine. |
| 85 | +- Tests covering new behaviour |
| 86 | +- Consistent style: formatted with `ruff format`, imports sorted, type hints passing `make typecheck` |
| 87 | +- Clear documentation for public API changes |
| 88 | +- Clean history and helpful PR description |
| 89 | +- Consistent environment variable declarations across GitHub Actions, `.env.example`, tests, and docs — no orphaned secrets/variables |
120 | 90 |
|
121 | | -## GitHub Actions Security |
| 91 | +## GitHub Actions security |
122 | 92 |
|
123 | | -We pin all third-party actions to commit SHAs to prevent supply chain attacks: |
| 93 | +Pin third-party actions to commit SHAs: |
124 | 94 |
|
125 | 95 | ```yaml |
126 | | -# ✅ Good: Commit SHA with inline version comment |
| 96 | +# Good: SHA with version comment |
127 | 97 | uses: appleboy/scp-action@ff85246acaad7bdce478db94a363cd2bf7c90345 #v1.0.0 |
128 | 98 |
|
129 | | -# ❌ Bad: Floating version tags |
| 99 | +# Bad: floating tag |
130 | 100 | uses: appleboy/scp-action@v1.0.0 |
131 | 101 | ``` |
132 | 102 |
|
133 | | -We allow fully qualified semantic version tag from |
134 | | -- `Astral` (uv) github actions (note: CodeQL will warn about this) |
135 | | -- immutable tags |
136 | | - |
137 | | -```yaml |
138 | | -# ✅ Good: semantic version tag |
139 | | -uses: astral-sh/setup-uv@7.3.0 |
140 | | -
|
141 | | -# ❌ Bad: major-only version tag |
142 | | -uses: astral-sh/setup-uv@7 |
143 | | -``` |
144 | | - |
145 | | -## What reviewers look for |
146 | | - |
147 | | -- Tests covering new behaviour. |
148 | | -- Consistent style: code formatted with `uv run ruff format`, imports sorted, and type hints passing `make typecheck`. |
149 | | -- Clear documentation for any public API changes. |
150 | | -- Clean history and a helpful PR description. |
151 | | -- Inconsistent environment variables and secrets declarations across GitHub Actions, .env.example, tests, and relevant Markdown documentation; this includes secrets and variables which are declared but never referenced in the code. |
| 103 | +Exceptions: Astral (uv) actions may use fully qualified semver tags (e.g. `astral-sh/setup-uv@7.3.0`), and immutable tags are allowed. |
0 commit comments