Add POST /api/semantic-diff endpoint for AI-powered code change explanations

Uses Anthropic claude-sonnet-4-6 server-side to explain the semantic meaning
of code diffs in the budget app domain (paychecks, bills, financing, actuals).
Input validation rejects empty or oversized (>50KB) diffs. Tests mock the
Anthropic client via direct method replacement (same pattern as db.pool.query).

Nightshift-Task: semantic-diff
Nightshift-Ref: https://github.com/marcus/nightshift
This commit is contained in:
2026-03-20 01:53:45 -04:00
parent ccd0fb2155
commit 5c5c777837
6 changed files with 183 additions and 0 deletions

View File

@@ -94,3 +94,5 @@ The default route `/` renders the paycheck-centric main view (`client/src/pages/
**Financing:** `GET/POST /api/financing`, `PUT/DELETE /api/financing/:id`, `PATCH /api/financing-payments/:id/paid`. Plans track a total amount, payoff due date, and `start_date`. Payment per period is auto-calculated as `(remaining balance) / (remaining periods)`. Split plans (`assigned_paycheck = null`) divide each period's payment across both paychecks. Plans auto-close when fully paid. Financing payments are included in the paycheck remaining balance. `start_date` prevents a plan from appearing on paycheck months before it was created — both virtual previews and `generate` respect this guard.
**Migrations:** SQL files in `db/migrations/` are applied in filename order on server startup. Add new migrations as `00N_description.sql` — they run once and are tracked in the `migrations` table.
**Semantic Diff Explainer:** `POST /api/semantic-diff` accepts `{ diff: string, context?: string }` and returns `{ explanation: string }`. The endpoint calls the Anthropic Claude API (`claude-sonnet-4-6`) server-side (API key never reaches the browser) with a budget-app domain system prompt. Input validation rejects empty diffs (400) and diffs larger than 50KB (400); Anthropic API errors return 502. Requires `ANTHROPIC_API_KEY` in the server environment. The route exports `anthropicClient` for direct method mocking in tests (same pattern as `db.pool.query`).